gpu: Check shader format support in PrepareDriver

This commit is contained in:
Ethan Lee
2025-05-19 08:56:29 -04:00
committed by Sam Lantinga
parent 8289656a4e
commit 510126ee63
5 changed files with 37 additions and 32 deletions

View File

@@ -517,7 +517,6 @@ void SDL_GPU_BlitCommon(
static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props) static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props)
{ {
Uint32 i; Uint32 i;
SDL_GPUShaderFormat format_flags = 0;
const char *gpudriver; const char *gpudriver;
SDL_VideoDevice *_this = SDL_GetVideoDevice(); SDL_VideoDevice *_this = SDL_GetVideoDevice();
@@ -526,25 +525,6 @@ static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props)
return NULL; return NULL;
} }
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_PRIVATE_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_PRIVATE;
}
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_SPIRV;
}
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_DXBC;
}
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_DXIL;
}
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_MSL;
}
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_METALLIB;
}
gpudriver = SDL_GetHint(SDL_HINT_GPU_DRIVER); gpudriver = SDL_GetHint(SDL_HINT_GPU_DRIVER);
if (gpudriver == NULL) { if (gpudriver == NULL) {
gpudriver = SDL_GetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, NULL); gpudriver = SDL_GetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, NULL);
@@ -554,10 +534,6 @@ static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props)
if (gpudriver != NULL) { if (gpudriver != NULL) {
for (i = 0; backends[i]; i += 1) { for (i = 0; backends[i]; i += 1) {
if (SDL_strcasecmp(gpudriver, backends[i]->name) == 0) { if (SDL_strcasecmp(gpudriver, backends[i]->name) == 0) {
if (!(backends[i]->shader_formats & format_flags)) {
SDL_SetError("Required shader format for backend %s not provided!", gpudriver);
return NULL;
}
if (backends[i]->PrepareDriver(_this, props)) { if (backends[i]->PrepareDriver(_this, props)) {
return backends[i]; return backends[i];
} }
@@ -569,10 +545,6 @@ static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props)
} }
for (i = 0; backends[i]; i += 1) { for (i = 0; backends[i]; i += 1) {
if ((backends[i]->shader_formats & format_flags) == 0) {
// Don't select a backend which doesn't support the app's shaders.
continue;
}
if (backends[i]->PrepareDriver(_this, props)) { if (backends[i]->PrepareDriver(_this, props)) {
return backends[i]; return backends[i];
} }

View File

@@ -1141,7 +1141,6 @@ struct SDL_GPUDevice
typedef struct SDL_GPUBootstrap typedef struct SDL_GPUBootstrap
{ {
const char *name; const char *name;
const SDL_GPUShaderFormat shader_formats;
bool (*PrepareDriver)(SDL_VideoDevice *_this, SDL_PropertiesID props); bool (*PrepareDriver)(SDL_VideoDevice *_this, SDL_PropertiesID props);
SDL_GPUDevice *(*CreateDevice)(bool debug_mode, bool prefer_low_power, SDL_PropertiesID props); SDL_GPUDevice *(*CreateDevice)(bool debug_mode, bool prefer_low_power, SDL_PropertiesID props);
} SDL_GPUBootstrap; } SDL_GPUBootstrap;

View File

@@ -8333,6 +8333,17 @@ static bool D3D12_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
IDXGIFactory6 *factory6; IDXGIFactory6 *factory6;
IDXGIAdapter1 *adapter; IDXGIAdapter1 *adapter;
// Early check to see if the app has _any_ D3D12 formats, if not we don't
// have to fuss with loading D3D in the first place.
bool has_dxbc = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOLEAN, false);
bool has_dxil = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, false);
bool supports_dxil = false;
// TODO SM7: bool has_spirv = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, false);
// TODO SM7: bool supports_spirv = false;
if (!has_dxbc && !has_dxil) {
return false;
}
// Can we load D3D12? // Can we load D3D12?
d3d12Dll = SDL_LoadObject(D3D12_DLL); d3d12Dll = SDL_LoadObject(D3D12_DLL);
@@ -8427,6 +8438,18 @@ static bool D3D12_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
(void **)&device); (void **)&device);
if (SUCCEEDED(res)) { if (SUCCEEDED(res)) {
D3D12_FEATURE_DATA_SHADER_MODEL shaderModel;
shaderModel.HighestShaderModel = D3D_SHADER_MODEL_6_0;
res = ID3D12Device_CheckFeatureSupport(
device,
D3D12_FEATURE_SHADER_MODEL,
&shaderModel,
sizeof(shaderModel));
if (SUCCEEDED(res) && shaderModel.HighestShaderModel >= D3D_SHADER_MODEL_6_0) {
supports_dxil = true;
}
ID3D12Device_Release(device); ID3D12Device_Release(device);
} }
IDXGIAdapter1_Release(adapter); IDXGIAdapter1_Release(adapter);
@@ -8435,6 +8458,11 @@ static bool D3D12_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
SDL_UnloadObject(d3d12Dll); SDL_UnloadObject(d3d12Dll);
SDL_UnloadObject(dxgiDll); SDL_UnloadObject(dxgiDll);
if (!supports_dxil && !has_dxbc) {
SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: DXIL is not supported and DXBC is not being provided");
return false;
}
if (FAILED(res)) { if (FAILED(res)) {
SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not create D3D12Device with feature level " D3D_FEATURE_LEVEL_CHOICE_STR); SDL_LogWarn(SDL_LOG_CATEGORY_GPU, "D3D12: Could not create D3D12Device with feature level " D3D_FEATURE_LEVEL_CHOICE_STR);
return false; return false;
@@ -9207,7 +9235,6 @@ static SDL_GPUDevice *D3D12_CreateDevice(bool debugMode, bool preferLowPower, SD
SDL_GPUBootstrap D3D12Driver = { SDL_GPUBootstrap D3D12Driver = {
"direct3d12", "direct3d12",
SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_DXBC,
D3D12_PrepareDriver, D3D12_PrepareDriver,
D3D12_CreateDevice D3D12_CreateDevice
}; };

View File

@@ -4309,6 +4309,11 @@ static bool METAL_SupportsTextureFormat(
static bool METAL_PrepareDriver(SDL_VideoDevice *this, SDL_PropertiesID props) static bool METAL_PrepareDriver(SDL_VideoDevice *this, SDL_PropertiesID props)
{ {
if (!SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN, false) &&
!SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN, false)) {
return false;
}
if (@available(macOS 10.14, iOS 13.0, tvOS 13.0, *)) { if (@available(macOS 10.14, iOS 13.0, tvOS 13.0, *)) {
return (this->Metal_CreateView != NULL); return (this->Metal_CreateView != NULL);
} }
@@ -4619,7 +4624,6 @@ static SDL_GPUDevice *METAL_CreateDevice(bool debugMode, bool preferLowPower, SD
SDL_GPUBootstrap MetalDriver = { SDL_GPUBootstrap MetalDriver = {
"metal", "metal",
SDL_GPU_SHADERFORMAT_MSL | SDL_GPU_SHADERFORMAT_METALLIB,
METAL_PrepareDriver, METAL_PrepareDriver,
METAL_CreateDevice METAL_CreateDevice
}; };

View File

@@ -11605,6 +11605,10 @@ static bool VULKAN_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
VulkanRenderer *renderer; VulkanRenderer *renderer;
bool result = false; bool result = false;
if (!SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, false)) {
return false;
}
if (_this->Vulkan_CreateSurface == NULL) { if (_this->Vulkan_CreateSurface == NULL) {
return false; return false;
} }
@@ -11987,7 +11991,6 @@ static SDL_GPUDevice *VULKAN_CreateDevice(bool debugMode, bool preferLowPower, S
SDL_GPUBootstrap VulkanDriver = { SDL_GPUBootstrap VulkanDriver = {
"vulkan", "vulkan",
SDL_GPU_SHADERFORMAT_SPIRV,
VULKAN_PrepareDriver, VULKAN_PrepareDriver,
VULKAN_CreateDevice VULKAN_CreateDevice
}; };