From fd1555535eee498bb6ff9ed61fa7f470ea3cffe0 Mon Sep 17 00:00:00 2001 From: Anthony Miles Date: Mon, 2 Dec 2019 22:47:59 +1300 Subject: [PATCH] HACK Get HLSL compiling --- projects/cxbx/CMakeLists.txt | 1 + src/core/hle/D3D8/Direct3D9/Direct3D9.cpp | 54 +++++++++---------- src/core/hle/D3D8/XbVertexShader.cpp | 65 +++++++++++++---------- src/core/hle/D3D8/XbVertexShader.h | 4 +- 4 files changed, 69 insertions(+), 55 deletions(-) diff --git a/projects/cxbx/CMakeLists.txt b/projects/cxbx/CMakeLists.txt index d80e95868..d269042b6 100644 --- a/projects/cxbx/CMakeLists.txt +++ b/projects/cxbx/CMakeLists.txt @@ -160,6 +160,7 @@ endif() set(WINS_LIB legacy_stdio_definitions d3d9 + d3dcompiler dinput8 dxguid odbc32 diff --git a/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp b/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp index 7722a4436..453c1ab43 100644 --- a/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp +++ b/src/core/hle/D3D8/Direct3D9/Direct3D9.cpp @@ -4033,7 +4033,7 @@ HRESULT WINAPI XTL::EMUPATCH(D3DDevice_CreateVertexShader) g_pD3DDevice->SetVertexDeclaration(pCxbxVertexShader->pHostVertexDeclaration); DEBUG_D3DRESULT(hRet, "g_pD3DDevice->SetVertexDeclaration"); - LPD3DXBUFFER pRecompiledBuffer = nullptr; + ID3DBlob *pRecompiledBuffer = nullptr; DWORD XboxFunctionSize = 0; DWORD *pRecompiledFunction = nullptr; if (SUCCEEDED(hRet) && pFunction) @@ -4074,36 +4074,36 @@ HRESULT WINAPI XTL::EMUPATCH(D3DDevice_CreateVertexShader) } //* Fallback to dummy shader. - if (FAILED(hRet)) - { - static const char dummy[] = - "vs.1.1\n" - "dcl_position v0\n" - "dp4 oPos.x, v0, c96\n" - "dp4 oPos.y, v0, c97\n" - "dp4 oPos.z, v0, c98\n" - "dp4 oPos.w, v0, c99\n"; + //if (FAILED(hRet)) + //{ + // static const char dummy[] = + // "vs.1.1\n" + // "dcl_position v0\n" + // "dp4 oPos.x, v0, c96\n" + // "dp4 oPos.y, v0, c97\n" + // "dp4 oPos.z, v0, c98\n" + // "dp4 oPos.w, v0, c99\n"; - EmuLog(LOG_LEVEL::WARNING, "Trying fallback:\n%s", dummy); + // EmuLog(LOG_LEVEL::WARNING, "Trying fallback:\n%s", dummy); - hRet = D3DXAssembleShader( - dummy, - strlen(dummy), - /*pDefines=*/nullptr, - /*pInclude=*/nullptr, - /*Flags=*/0, // Was D3DXASM_SKIPVALIDATION - /*ppCompiledShader=*/&pRecompiledBuffer, - /*ppCompilationErrors*/nullptr); + // hRet = D3DXAssembleShader( + // dummy, + // strlen(dummy), + // /*pDefines=*/nullptr, + // /*pInclude=*/nullptr, + // /*Flags=*/0, // Was D3DXASM_SKIPVALIDATION + // /*ppCompiledShader=*/&pRecompiledBuffer, + // /*ppCompilationErrors*/nullptr); - DEBUG_D3DRESULT(hRet, "D3DXAssembleShader"); + // DEBUG_D3DRESULT(hRet, "D3DXAssembleShader"); - hRet = g_pD3DDevice->CreateVertexShader - ( - (DWORD*)pRecompiledBuffer->GetBufferPointer(), - &pHostVertexShader - ); - DEBUG_D3DRESULT(hRet, "g_pD3DDevice->CreateVertexShader(fallback)"); - } + // hRet = g_pD3DDevice->CreateVertexShader + // ( + // (DWORD*)pRecompiledBuffer->GetBufferPointer(), + // &pHostVertexShader + // ); + // DEBUG_D3DRESULT(hRet, "g_pD3DDevice->CreateVertexShader(fallback)"); + //} if (pRecompiledBuffer != nullptr) { diff --git a/src/core/hle/D3D8/XbVertexShader.cpp b/src/core/hle/D3D8/XbVertexShader.cpp index 231ceaf2b..83c3939c5 100644 --- a/src/core/hle/D3D8/XbVertexShader.cpp +++ b/src/core/hle/D3D8/XbVertexShader.cpp @@ -2663,6 +2663,9 @@ std::string VshPostProcess_TruncateMovA(std::string shader) { return std::regex_replace(shader, movA, truncate); } +#include +#include + // Post process the shader as a string std::string VshPostProcess(std::string shader) { shader = VshPostProcess_Expp(shader); @@ -2680,14 +2683,14 @@ extern HRESULT EmuRecompileVshFunction D3DVERTEXELEMENT *pRecompiledDeclaration, bool *pbUseDeclarationOnly, DWORD *pXboxFunctionSize, - LPD3DXBUFFER *ppRecompiledShader + ID3DBlob **ppRecompiledShader ) { XTL::X_VSH_SHADER_HEADER *pXboxVertexShaderHeader = (XTL::X_VSH_SHADER_HEADER*)pXboxFunction; - DWORD *pToken; + DWORD *pToken; boolean EOI = false; - VSH_XBOX_SHADER *pShader = (VSH_XBOX_SHADER*)calloc(1, sizeof(VSH_XBOX_SHADER)); - LPD3DXBUFFER pErrors = nullptr; + VSH_XBOX_SHADER *pShader = (VSH_XBOX_SHADER*)calloc(1, sizeof(VSH_XBOX_SHADER)); + ID3DBlob *pErrors; HRESULT hRet = 0; // TODO: support this situation.. @@ -2749,19 +2752,29 @@ extern HRESULT EmuRecompileVshFunction std::stringstream& pXboxShaderDisassembly = std::stringstream(); std::stringstream& pHostShaderDisassembly = std::stringstream(); + //static std::ifstream t("Xb.hlsl"); + static std::ifstream t("C:\\Users\\OEM\\Desktop\\repos\\Cxbx-Reloaded\\src\\core\\hle\\D3D8\\Direct3D9\\Xb.hlsl"); + static std::string hlslTemplate((std::istreambuf_iterator(t)), + std::istreambuf_iterator()); + DbgVshPrintf("-- Before conversion --\n"); VshWriteShader(pShader, pXboxShaderDisassembly, pRecompiledDeclaration, FALSE); DbgVshPrintf("%s", pXboxShaderDisassembly.str().c_str()); DbgVshPrintf("-----------------------\n"); + auto hlslTest = BuildShader(pShader); + hlslTest = std::regex_replace(hlslTemplate, std::regex("// "), hlslTest); DbgVshPrintf("-- HLSL conversion 1 ---\n"); - DbgVshPrintf(BuildShader(pShader).c_str()); + DbgVshPrintf(hlslTest.c_str()); DbgVshPrintf("-----------------------\n"); VshConvertShader(pShader, bNoReservedConstants); VshWriteShader(pShader, pHostShaderDisassembly, pRecompiledDeclaration, TRUE); + //auto hlslTest = BuildShader(pShader); + //hlslTest = std::regex_replace(hlslTemplate, std::regex("// "), hlslTest); + // Post process the final shader auto finalHostShader = VshPostProcess(pHostShaderDisassembly.str()); @@ -2778,43 +2791,41 @@ extern HRESULT EmuRecompileVshFunction { EmuLog(LOG_LEVEL::WARNING, "Replacing empty vertex shader with fallback"); - static const char dummy[] = + finalHostShader = std::string( "vs.2.x\n" "dcl_position v0\n" "dp4 oPos.x, v0, c96\n" "dp4 oPos.y, v0, c97\n" "dp4 oPos.z, v0, c98\n" - "dp4 oPos.w, v0, c99\n"; - - hRet = D3DXAssembleShader( - dummy, - strlen(dummy), - /*pDefines=*/nullptr, - /*pInclude=*/nullptr, - /*Flags=*/0, // Was D3DXASM_SKIPVALIDATION, - /*ppCompiledShader=*/ppRecompiledShader, - /*ppCompilationErrors*/nullptr); + "dp4 oPos.w, v0, c99\n" + ); } else { - hRet = D3DXAssembleShader( - finalHostShader.c_str(), - finalHostShader.length(), - /*pDefines=*/nullptr, - /*pInclude=*/nullptr, - /*Flags=*/0, // Was D3DXASM_SKIPVALIDATION, - /*ppCompiledShader=*/ppRecompiledShader, - /*ppCompilationErrors*/&pErrors); + hRet = D3DCompile( + hlslTest.c_str(), + hlslTest.length(), + nullptr, // pSourceName + nullptr, // pDefines + nullptr, // pInclude // TODO precompile x_* HLSL functions? + "main", // shader entry poiint + "vs_3_0", // shader profile + 0, // flags1 + 0, // flags2 + ppRecompiledShader, // out + &pErrors // ppErrorMsgs out + ); } if (FAILED(hRet)) { EmuLog(LOG_LEVEL::WARNING, "Couldn't assemble recompiled vertex shader"); - EmuLog(LOG_LEVEL::WARNING, "%s", pErrors->GetBufferPointer()); + EmuLog(LOG_LEVEL::WARNING, "%s", (char*)(pErrors)->GetBufferPointer()); + LOG_TEST_CASE((char *)pErrors->GetBufferPointer()); } - if( pErrors ) - pErrors->Release(); + if (pErrors) + (pErrors)->Release(); } free(pShader); diff --git a/src/core/hle/D3D8/XbVertexShader.h b/src/core/hle/D3D8/XbVertexShader.h index 88997a1b9..36b963a49 100644 --- a/src/core/hle/D3D8/XbVertexShader.h +++ b/src/core/hle/D3D8/XbVertexShader.h @@ -25,6 +25,8 @@ #ifndef XBVERTEXSHADER_H #define XBVERTEXSHADER_H +#include + #include "core\hle\D3D8\XbD3D8Types.h" // for X_VSH_MAX_ATTRIBUTES // Host vertex shader counts @@ -112,7 +114,7 @@ extern HRESULT EmuRecompileVshFunction D3DVERTEXELEMENT *pRecompiledDeclaration, bool *pbUseDeclarationOnly, DWORD *pXboxFunctionSize, - LPD3DXBUFFER *ppRecompiledShader + ID3DBlob **ppRecompiledShader ); extern void FreeVertexDynamicPatch(CxbxVertexShader *pVertexShader);