From 24c7faad4db35cbc22affbb9736687b8e018dc68 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Tue, 30 Dec 2025 22:13:55 -0600 Subject: [PATCH] load shader from resource --- .gitignore | 1 + build.bat | 11 ++++++++-- link.rsp | 7 ++++-- main.cpp | 64 +++++++++++++++++++++++++++++++++++------------------- main.rc | 1 + 5 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 main.rc diff --git a/.gitignore b/.gitignore index f8a2440..082349a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.manifest *.idb *.obj +*.res \ No newline at end of file diff --git a/build.bat b/build.bat index 8fd2526..02fb037 100644 --- a/build.bat +++ b/build.bat @@ -1,8 +1,15 @@ +fxc /Od /Zi /T fx_4_0 /nologo /Fo main.fxo main.fx + +rem build main resource +rc.exe /d "_UNICODE" /d "UNICODE" /fo"Debug\main.res" ".\main.rc" + +rem compile cl.exe @"compile.rsp" "main.cpp" -link.exe @"link.rsp" /OUT:"Debug\d3d10.exe" /PDB:"Debug\d3d10.pdb" ".\Debug\main.obj" /NOLOGO /ERRORREPORT:PROMPT +rem link +link.exe @"link.rsp" /NOLOGO /ERRORREPORT:PROMPT -fxc /Od /Zi /T fx_4_0 /nologo /Fo main.fxo main.fx +mt.exe -manifest d3d10.exe.manifest -outputresource:Debug\d3d10.exe;1 @if %errorlevel% neq 0 exit /b %errorlevel% diff --git a/link.rsp b/link.rsp index 994f7f4..107f36c 100644 --- a/link.rsp +++ b/link.rsp @@ -1,7 +1,10 @@ /INCREMENTAL /DEBUG /SUBSYSTEM:WINDOWS +/MANIFEST:NO /MACHINE:X86 +/OUT:"Debug\d3d10.exe" +/PDB:"Debug\d3d10.pdb" d3d10.lib d3dx10.lib kernel32.lib @@ -16,5 +19,5 @@ oleaut32.lib uuid.lib odbc32.lib odbccp32.lib - - +".\Debug\main.obj" +".\Debug\main.res" \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4726ffd..1a892c4 100644 --- a/main.cpp +++ b/main.cpp @@ -65,6 +65,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi if (FAILED(InitDirect3DDevice())) { print("InitDirect3DDevice\n"); + system("pause"); return 0; } @@ -78,6 +79,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi } } + system("pause"); return 0; } @@ -159,19 +161,33 @@ HRESULT InitDirect3DDevice() sd.SampleDesc.Quality = 0; sd.Windowed = TRUE; + D3D10_DRIVER_TYPE driverTypes[] = { + D3D10_DRIVER_TYPE_HARDWARE, + D3D10_DRIVER_TYPE_REFERENCE, + }; + UINT numDriverTypes = (sizeof (driverTypes)) / (sizeof (driverTypes[0])); + HRESULT hr; - hr = D3D10CreateDeviceAndSwapChain(NULL, - D3D10_DRIVER_TYPE_REFERENCE, - NULL, - D3D10_CREATE_DEVICE_DEBUG, - D3D10_SDK_VERSION, - &sd, - &g_pSwapChain, - &g_pd3dDevice); + D3D10_DRIVER_TYPE driverType = D3D10_DRIVER_TYPE_NULL; + for (UINT i = 0; i < numDriverTypes; i++) { + driverType = driverTypes[i]; + hr = D3D10CreateDeviceAndSwapChain(NULL, + driverType, + NULL, + 0, + //D3D10_CREATE_DEVICE_DEBUG, + D3D10_SDK_VERSION, + &sd, + &g_pSwapChain, + &g_pd3dDevice); + if (SUCCEEDED(hr)) + break; + } if (FAILED(hr)) { print("D3D10CreateDeviceAndSwapChain\n"); return hr; } + print("driverType %d\n", driverType); ID3D10Texture2D * pBackBuffer; hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID *)&pBackBuffer); @@ -202,22 +218,26 @@ HRESULT InitDirect3DDevice() ID3D10Blob * pBlobErrors = NULL; DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS | D3D10_SHADER_DEBUG; - hr = D3DX10CreateEffectFromFile(L"c:/Users/dot/d3d10/main.fx", - NULL, - NULL, - "fx_4_0", - dwShaderFlags, - 0, - g_pd3dDevice, - NULL, - NULL, - &g_pEffect, - &pBlobErrors, - NULL); + hr = D3DX10CreateEffectFromResource(NULL, + L"RES_MAIN_FXO", + L"main.fxo", + NULL, + NULL, + "fx_4_0", + dwShaderFlags, + 0, + g_pd3dDevice, + NULL, + NULL, + &g_pEffect, + &pBlobErrors, + NULL); if (FAILED(hr)) { print("D3DX10CreateEffectFromFile\n"); - const char * pError = (const char *)pBlobErrors->GetBufferPointer(); - (void)pError; + if (pBlobErrors != NULL) { + const char * pError = (const char *)pBlobErrors->GetBufferPointer(); + print("pError: %p\n", pError); + } return hr; } diff --git a/main.rc b/main.rc new file mode 100644 index 0000000..e01d09e --- /dev/null +++ b/main.rc @@ -0,0 +1 @@ +RES_MAIN_FXO RCDATA "main.fxo" \ No newline at end of file