From e7cc4ccf6e7c64db9077944ee4d747ef972bd79a Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Thu, 8 Aug 2024 14:57:01 -0500 Subject: [PATCH] Remove HLSL shader format (#145) --- include/SDL3/SDL_gpu.h | 1 - src/gpu/d3d11/SDL_gpu_d3d11.c | 151 +++------------------------------- src/gpu/d3d12/SDL_gpu_d3d12.c | 110 +------------------------ 3 files changed, 17 insertions(+), 245 deletions(-) diff --git a/include/SDL3/SDL_gpu.h b/include/SDL3/SDL_gpu.h index 36b3dde5864a99..92c036dd9cb53b 100644 --- a/include/SDL3/SDL_gpu.h +++ b/include/SDL3/SDL_gpu.h @@ -195,7 +195,6 @@ typedef enum SDL_GpuShaderFormat SDL_GPU_SHADERFORMAT_INVALID, SDL_GPU_SHADERFORMAT_SECRET, /* NDA'd platforms */ SDL_GPU_SHADERFORMAT_SPIRV, /* Vulkan */ - SDL_GPU_SHADERFORMAT_HLSL, /* D3D11, D3D12 */ SDL_GPU_SHADERFORMAT_DXBC, /* D3D11, D3D12 */ SDL_GPU_SHADERFORMAT_DXIL, /* D3D12 */ SDL_GPU_SHADERFORMAT_MSL, /* Metal */ diff --git a/src/gpu/d3d11/SDL_gpu_d3d11.c b/src/gpu/d3d11/SDL_gpu_d3d11.c index ce4abc72b0c99f..2a1190ff40f036 100644 --- a/src/gpu/d3d11/SDL_gpu_d3d11.c +++ b/src/gpu/d3d11/SDL_gpu_d3d11.c @@ -29,59 +29,12 @@ #include #include -#include #include #include #include #include "../SDL_sysgpu.h" -/* __stdcall declaration, largely taken from vkd3d_windows.h */ -#ifdef _WIN32 -#define D3DCOMPILER_API STDMETHODCALLTYPE -#else -#ifdef __stdcall -#undef __stdcall -#endif -#ifdef __x86_64__ -#define __stdcall __attribute__((ms_abi)) -#else -#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) || defined(__APPLE__) -#define __stdcall __attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__)) -#else -#define __stdcall __attribute__((__stdcall__)) -#endif -#endif -#define D3DCOMPILER_API __stdcall -#endif - -/* vkd3d uses stdcall for its ID3D10Blob implementation */ -#ifndef _WIN32 -typedef struct VKD3DBlob VKD3DBlob; -typedef struct VKD3DBlobVtbl -{ - HRESULT(__stdcall *QueryInterface) - ( - VKD3DBlob *This, - REFIID riid, - void **ppvObject); - ULONG(__stdcall *AddRef) - (VKD3DBlob *This); - ULONG(__stdcall *Release) - (VKD3DBlob *This); - LPVOID(__stdcall *GetBufferPointer) - (VKD3DBlob *This); - SIZE_T(__stdcall *GetBufferSize) - (VKD3DBlob *This); -} VKD3DBlobVtbl; -struct VKD3DBlob -{ - const VKD3DBlobVtbl *lpVtbl; -}; -#define ID3D10Blob VKD3DBlob -#define ID3DBlob VKD3DBlob -#endif - /* MinGW doesn't implement this yet */ #ifdef _WIN32 #define HAVE_IDXGIINFOQUEUE @@ -90,7 +43,6 @@ struct VKD3DBlob /* Function Pointer Signatures */ typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY1)(const GUID *riid, void **ppFactory); typedef HRESULT(WINAPI *PFN_DXGI_GET_DEBUG_INTERFACE)(const GUID *riid, void **ppDebug); -typedef HRESULT(D3DCOMPILER_API *PFN_D3DCOMPILE)(LPCVOID pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName, const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2, ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs); /* IIDs (from https://www.magnumdb.com/) */ static const IID D3D_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } }; @@ -116,27 +68,18 @@ static const GUID D3D_IID_DXGI_DEBUG_ALL = { 0xe48ae283, 0xda80, 0x490b, { 0x87, #define D3D11_DLL "d3d11.dll" #define DXGI_DLL "dxgi.dll" #define DXGIDEBUG_DLL "dxgidebug.dll" - -/* FIXME: Reuse the d3dcompiler loader in SDL_egl.c */ -#ifdef D3DCOMPILER_DLL -#undef D3DCOMPILER_DLL -#endif -#define D3DCOMPILER_DLL "d3dcompiler_47.dll" #elif defined(__APPLE__) #define D3D11_DLL "libdxvk_d3d11.0.dylib" #define DXGI_DLL "libdxvk_dxgi.0.dylib" #define DXGIDEBUG_DLL "libdxvk_dxgidebug.0.dylib" -#define D3DCOMPILER_DLL "libvkd3d-utils.1.dylib" #else #define D3D11_DLL "libdxvk_d3d11.so.0" #define DXGI_DLL "libdxvk_dxgi.so.0" #define DXGIDEBUG_DLL "libdxvk_dxgidebug.so.0" -#define D3DCOMPILER_DLL "libvkd3d-utils.so.1" #endif #define D3D11_CREATE_DEVICE_FUNC "D3D11CreateDevice" #define CREATE_DXGI_FACTORY1_FUNC "CreateDXGIFactory1" -#define D3DCOMPILE_FUNC "D3DCompile" #define DXGI_GET_DEBUG_INTERFACE_FUNC "DXGIGetDebugInterface" #define WINDOW_PROPERTY_DATA "SDL_GpuD3D11WindowPropertyData" @@ -727,9 +670,6 @@ struct D3D11Renderer void *dxgi_dll; void *dxgidebug_dll; - void *d3dcompiler_dll; - PFN_D3DCOMPILE D3DCompile_func; - Uint8 debugMode; BOOL supportsTearing; Uint8 supportsFlipDiscard; @@ -1009,7 +949,6 @@ static void D3D11_DestroyDevice( /* Release the DLLs */ SDL_UnloadObject(renderer->d3d11_dll); SDL_UnloadObject(renderer->dxgi_dll); - SDL_UnloadObject(renderer->d3dcompiler_dll); if (renderer->dxgidebug_dll) { SDL_UnloadObject(renderer->dxgidebug_dll); } @@ -1463,38 +1402,10 @@ static ID3D11DeviceChild *D3D11_INTERNAL_CreateID3D11Shader( void **pBytecode, size_t *pBytecodeSize) { - const char *profiles[3] = { "vs_5_0", "ps_5_0", "cs_5_0" }; - ID3DBlob *blob = NULL; - ID3DBlob *errorBlob; - const Uint8 *bytecode; - size_t bytecodeSize; ID3D11DeviceChild *handle = NULL; HRESULT res; - if (format == SDL_GPU_SHADERFORMAT_HLSL) { - res = renderer->D3DCompile_func( - code, - codeSize, - NULL, - NULL, - NULL, - entryPointName, - profiles[stage], - 0, - 0, - &blob, - &errorBlob); - if (res < 0) { - SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s", (const char *)ID3D10Blob_GetBufferPointer(errorBlob)); - ID3D10Blob_Release(errorBlob); - return NULL; - } - bytecode = ID3D10Blob_GetBufferPointer(blob); - bytecodeSize = ID3D10Blob_GetBufferSize(blob); - } else if (format == SDL_GPU_SHADERFORMAT_DXBC) { - bytecode = code; - bytecodeSize = codeSize; - } else { + if (format != SDL_GPU_SHADERFORMAT_DXBC) { SDL_LogError(SDL_LOG_CATEGORY_GPU, "Incompatible shader format for D3D11"); return NULL; } @@ -1503,8 +1414,8 @@ static ID3D11DeviceChild *D3D11_INTERNAL_CreateID3D11Shader( if (stage == SDL_GPU_SHADERSTAGE_VERTEX) { res = ID3D11Device_CreateVertexShader( renderer->device, - bytecode, - bytecodeSize, + code, + codeSize, NULL, (ID3D11VertexShader **)&handle); if (FAILED(res)) { @@ -1514,8 +1425,8 @@ static ID3D11DeviceChild *D3D11_INTERNAL_CreateID3D11Shader( } else if (stage == SDL_GPU_SHADERSTAGE_FRAGMENT) { res = ID3D11Device_CreatePixelShader( renderer->device, - bytecode, - bytecodeSize, + code, + codeSize, NULL, (ID3D11PixelShader **)&handle); if (FAILED(res)) { @@ -1525,8 +1436,8 @@ static ID3D11DeviceChild *D3D11_INTERNAL_CreateID3D11Shader( } else if (stage == SDL_GPU_SHADERSTAGE_COMPUTE) { res = ID3D11Device_CreateComputeShader( renderer->device, - bytecode, - bytecodeSize, + code, + codeSize, NULL, (ID3D11ComputeShader **)&handle); if (FAILED(res)) { @@ -1536,14 +1447,9 @@ static ID3D11DeviceChild *D3D11_INTERNAL_CreateID3D11Shader( } if (pBytecode != NULL) { - *pBytecode = SDL_malloc(bytecodeSize); - SDL_memcpy(*pBytecode, bytecode, bytecodeSize); - *pBytecodeSize = bytecodeSize; - } - - /* Clean up */ - if (blob) { - ID3D10Blob_Release(blob); + *pBytecode = SDL_malloc(codeSize); + SDL_memcpy(*pBytecode, code, codeSize); + *pBytecodeSize = codeSize; } return handle; @@ -1886,8 +1792,8 @@ SDL_GpuShader *D3D11_CreateShader( { D3D11Renderer *renderer = (D3D11Renderer *)driverData; ID3D11DeviceChild *handle; - void *bytecode; - size_t bytecodeSize; + void *bytecode = NULL; + size_t bytecodeSize = 0; D3D11Shader *shader; handle = D3D11_INTERNAL_CreateID3D11Shader( @@ -5742,11 +5648,10 @@ static SDL_bool D3D11_IsTextureFormatSupported( static SDL_bool D3D11_PrepareDriver(SDL_VideoDevice *_this) { - void *d3d11_dll, *dxgi_dll, *d3dcompiler_dll; + void *d3d11_dll, *dxgi_dll; PFN_D3D11_CREATE_DEVICE D3D11CreateDeviceFunc; D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_1 }; PFN_CREATE_DXGI_FACTORY1 CreateDXGIFactoryFunc; - PFN_D3DCOMPILE D3DCompileFunc; HRESULT res; /* Can we load D3D11? */ @@ -5804,23 +5709,6 @@ static SDL_bool D3D11_PrepareDriver(SDL_VideoDevice *_this) return SDL_FALSE; } - /* Can we load D3DCompiler? */ - - d3dcompiler_dll = SDL_LoadObject(D3DCOMPILER_DLL); - if (d3dcompiler_dll == NULL) { - SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D11: Could not find " D3DCOMPILER_DLL); - return SDL_FALSE; - } - - D3DCompileFunc = (PFN_D3DCOMPILE)SDL_LoadFunction( - d3dcompiler_dll, - D3DCOMPILE_FUNC); - SDL_UnloadObject(d3dcompiler_dll); /* We're not going to call this function, so we can just unload now. */ - if (D3DCompileFunc == NULL) { - SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D11: Could not find function D3DCompile in " D3DCOMPILER_DLL); - return SDL_FALSE; - } - return SDL_TRUE; } @@ -6035,19 +5923,6 @@ static SDL_GpuDevice *D3D11_CreateDevice(SDL_bool debugMode, SDL_bool preferLowP /* Allocate and zero out the renderer */ renderer = (D3D11Renderer *)SDL_calloc(1, sizeof(D3D11Renderer)); - /* Load the D3DCompiler library */ - renderer->d3dcompiler_dll = SDL_LoadObject(D3DCOMPILER_DLL); - if (renderer->d3dcompiler_dll == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_GPU, "Could not find " D3DCOMPILER_DLL); - return NULL; - } - - renderer->D3DCompile_func = (PFN_D3DCOMPILE)SDL_LoadFunction(renderer->d3dcompiler_dll, D3DCOMPILE_FUNC); - if (renderer->D3DCompile_func == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_GPU, "Could not load function: " D3DCOMPILE_FUNC); - return NULL; - } - /* Load the DXGI library */ renderer->dxgi_dll = SDL_LoadObject(DXGI_DLL); if (renderer->dxgi_dll == NULL) { diff --git a/src/gpu/d3d12/SDL_gpu_d3d12.c b/src/gpu/d3d12/SDL_gpu_d3d12.c index 59ba227fb2055b..b3c2d097ce420f 100644 --- a/src/gpu/d3d12/SDL_gpu_d3d12.c +++ b/src/gpu/d3d12/SDL_gpu_d3d12.c @@ -27,8 +27,6 @@ #include "../SDL_sysgpu.h" #include "SDL_hashtable.h" -#include - /* Built-in shaders, compiled with compile_shaders.bat */ #define g_main D3D12_BlitFrom2D @@ -47,8 +45,6 @@ /* Macros */ -#define D3DCOMPILER_API STDMETHODCALLTYPE - #define ERROR_CHECK(msg) \ if (FAILED(res)) { \ D3D12_INTERNAL_LogError(renderer->device, msg, res); \ @@ -73,28 +69,19 @@ #define D3D12_DLL "d3d12.dll" #define DXGI_DLL "dxgi.dll" #define DXGIDEBUG_DLL "dxgidebug.dll" - -/* FIXME: Reuse the d3dcompiler loader in SDL_egl.c */ -#ifdef D3DCOMPILER_DLL -#undef D3DCOMPILER_DLL -#endif -#define D3DCOMPILER_DLL "d3dcompiler_47.dll" #elif defined(__APPLE__) #define D3D12_DLL "libdxvk_d3d12.dylib" #define DXGI_DLL "libdxvk_dxgi.dylib" #define DXGIDEBUG_DLL "libdxvk_dxgidebug.dylib" -#define D3DCOMPILER_DLL "libvkd3d-utils.1.dylib" #else #define D3D12_DLL "libdxvk_d3d12.so" #define DXGI_DLL "libdxvk_dxgi.so" #define DXGIDEBUG_DLL "libdxvk_dxgidebug.so" -#define D3DCOMPILER_DLL "libvkd3d-utils.so.1" #endif #define D3D12_CREATE_DEVICE_FUNC "D3D12CreateDevice" #define D3D12_SERIALIZE_ROOT_SIGNATURE_FUNC "D3D12SerializeRootSignature" #define CREATE_DXGI_FACTORY1_FUNC "CreateDXGIFactory1" -#define D3DCOMPILE_FUNC "D3DCompile" #define DXGI_GET_DEBUG_INTERFACE_FUNC "DXGIGetDebugInterface" #define D3D12_GET_DEBUG_INTERFACE_FUNC "D3D12GetDebugInterface" #define WINDOW_PROPERTY_DATA "SDL_GpuD3D12WindowPropertyData" @@ -132,7 +119,6 @@ /* Function Pointer Signatures */ typedef HRESULT(WINAPI *PFN_CREATE_DXGI_FACTORY1)(const GUID *riid, void **ppFactory); typedef HRESULT(WINAPI *PFN_DXGI_GET_DEBUG_INTERFACE)(const GUID *riid, void **ppDebug); -typedef HRESULT(D3DCOMPILER_API *PFN_D3DCOMPILE)(LPCVOID pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName, const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2, ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs); /* IIDs (from https://www.magnumdb.com/) */ static const IID D3D_IID_IDXGIFactory1 = { 0x770aae78, 0xf26f, 0x4dba, { 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3, 0x87 } }; @@ -162,8 +148,6 @@ static const IID D3D_IID_ID3D12PipelineState = { 0x765a30f3, 0xf624, 0x4c6f, { 0 static const IID D3D_IID_ID3D12Debug = { 0x344488b7, 0x6846, 0x474b, { 0xb9, 0x89, 0xf0, 0x27, 0x44, 0x82, 0x45, 0xe0 } }; static const IID D3D_IID_ID3D12InfoQueue = { 0x0742a90b, 0xc387, 0x483f, { 0xb9, 0x46, 0x30, 0xa7, 0xe4, 0xe6, 0x14, 0x58 } }; -static const char *D3D12ShaderProfiles[3] = { "vs_5_1", "ps_5_1", "cs_5_1" }; - /* Enums */ typedef enum D3D12BufferType @@ -493,8 +477,6 @@ struct D3D12Renderer IDXGIDebug *dxgiDebug; IDXGIInfoQueue *dxgiInfoQueue; ID3D12Debug *d3d12Debug; - void *d3dcompiler_dll; - PFN_D3DCOMPILE D3DCompile_func; void *dxgi_dll; IDXGIFactory4 *factory; SDL_bool supportsTearing; @@ -1307,15 +1289,10 @@ static void D3D12_INTERNAL_DestroyRenderer(D3D12Renderer *renderer) SDL_UnloadObject(renderer->dxgi_dll); renderer->dxgi_dll = NULL; } - if (renderer->d3dcompiler_dll) { - SDL_UnloadObject(renderer->d3dcompiler_dll); - renderer->d3dcompiler_dll = NULL; - } if (renderer->dxgidebug_dll) { SDL_UnloadObject(renderer->dxgidebug_dll); renderer->dxgidebug_dll = NULL; } - renderer->D3DCompile_func = NULL; renderer->D3D12SerializeRootSignature_func = NULL; SDL_DestroyMutex(renderer->stagingDescriptorHeapLock); @@ -1979,64 +1956,19 @@ static SDL_bool D3D12_INTERNAL_CreateShaderBytecode( void **pBytecode, size_t *pBytecodeSize) { - ID3DBlob *blob = NULL; - ID3DBlob *errorBlob = NULL; - const Uint8 *bytecode; - size_t bytecodeSize; - HRESULT res; - /* TODO: accept DXIL */ - if (format == SDL_GPU_SHADERFORMAT_HLSL) { - res = renderer->D3DCompile_func( - code, - codeSize, - NULL, - NULL, - NULL, - entryPointName, - D3D12ShaderProfiles[stage], - 0, - 0, - &blob, - &errorBlob); - if (FAILED(res)) { - if (errorBlob) { - SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s", (const char *)ID3D10Blob_GetBufferPointer(errorBlob)); - ID3D10Blob_Release(errorBlob); - } - if (blob) { - ID3D10Blob_Release(blob); - } - return SDL_FALSE; - } - if (errorBlob) { - ID3D10Blob_Release(errorBlob); - } - bytecode = (const Uint8 *)ID3D10Blob_GetBufferPointer(blob); - bytecodeSize = ID3D10Blob_GetBufferSize(blob); - } else if (format == SDL_GPU_SHADERFORMAT_DXBC) { - bytecode = code; - bytecodeSize = codeSize; - } else { + if (format != SDL_GPU_SHADERFORMAT_DXBC) { SDL_LogError(SDL_LOG_CATEGORY_GPU, "Incompatible shader format for D3D12"); return SDL_FALSE; } if (pBytecode != NULL) { - *pBytecode = SDL_malloc(bytecodeSize); + *pBytecode = SDL_malloc(codeSize); if (!*pBytecode) { - if (blob) { - ID3D10Blob_Release(blob); - } return SDL_FALSE; } - SDL_memcpy(*pBytecode, bytecode, bytecodeSize); - *pBytecodeSize = bytecodeSize; - } - - // Clean up - if (blob) { - ID3D10Blob_Release(blob); + SDL_memcpy(*pBytecode, code, codeSize); + *pBytecodeSize = codeSize; } return SDL_TRUE; @@ -6731,10 +6663,8 @@ static SDL_bool D3D12_PrepareDriver(SDL_VideoDevice *_this) { void *d3d12_dll; void *dxgi_dll; - void *d3dcompiler_dll; PFN_D3D12_CREATE_DEVICE D3D12CreateDeviceFunc; PFN_CREATE_DXGI_FACTORY1 CreateDXGIFactoryFunc; - PFN_D3DCOMPILE D3DCompileFunc; HRESULT res; ID3D12Device *device; IDXGIFactory1 *factory; @@ -6849,23 +6779,6 @@ static SDL_bool D3D12_PrepareDriver(SDL_VideoDevice *_this) return SDL_FALSE; } - /* Can we load D3DCompiler? */ - - d3dcompiler_dll = SDL_LoadObject(D3DCOMPILER_DLL); - if (d3dcompiler_dll == NULL) { - SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D12: Could not find " D3DCOMPILER_DLL); - return SDL_FALSE; - } - - D3DCompileFunc = (PFN_D3DCOMPILE)SDL_LoadFunction( - d3dcompiler_dll, - D3DCOMPILE_FUNC); - SDL_UnloadObject(d3dcompiler_dll); /* We're not going to call this function, so we can just unload now. */ - if (D3DCompileFunc == NULL) { - SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "D3D12: Could not find function D3DCompile in " D3DCOMPILER_DLL); - return SDL_FALSE; - } - return SDL_TRUE; } @@ -6972,21 +6885,6 @@ static SDL_GpuDevice *D3D12_CreateDevice(SDL_bool debugMode, SDL_bool preferLowP renderer = (D3D12Renderer *)SDL_calloc(1, sizeof(D3D12Renderer)); - /* Load the D3DCompiler library */ - renderer->d3dcompiler_dll = SDL_LoadObject(D3DCOMPILER_DLL); - if (renderer->d3dcompiler_dll == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not find " D3DCOMPILER_DLL); - D3D12_INTERNAL_DestroyRenderer(renderer); - return NULL; - } - - renderer->D3DCompile_func = (PFN_D3DCOMPILE)SDL_LoadFunction(renderer->d3dcompiler_dll, D3DCOMPILE_FUNC); - if (renderer->D3DCompile_func == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load function: " D3DCOMPILE_FUNC); - D3D12_INTERNAL_DestroyRenderer(renderer); - return NULL; - } - /* Load the DXGI library */ renderer->dxgi_dll = SDL_LoadObject(DXGI_DLL); if (renderer->dxgi_dll == NULL) {