gpu: Rework driver name queries, add GetGPUShaderFormats

This commit is contained in:
Ethan Lee
2024-09-13 11:16:43 -04:00
parent 6d92de5d3a
commit 96e147b2b9
12 changed files with 129 additions and 67 deletions

View File

@@ -371,7 +371,7 @@ void SDL_GPU_BlitCommon(
// Driver Functions
#ifndef SDL_GPU_DISABLED
static SDL_GPUDriver SDL_GPUSelectBackend(
static const SDL_GPUBootstrap * SDL_GPUSelectBackend(
SDL_VideoDevice *_this,
const char *gpudriver,
SDL_GPUShaderFormat format_flags)
@@ -384,16 +384,16 @@ static SDL_GPUDriver SDL_GPUSelectBackend(
if (SDL_strcasecmp(gpudriver, backends[i]->name) == 0) {
if (!(backends[i]->shader_formats & format_flags)) {
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Required shader format for backend %s not provided!", gpudriver);
return SDL_GPU_DRIVER_INVALID;
return NULL;
}
if (backends[i]->PrepareDriver(_this)) {
return backends[i]->backendflag;
return backends[i];
}
}
}
SDL_LogError(SDL_LOG_CATEGORY_GPU, "SDL_HINT_GPU_DRIVER %s unsupported!", gpudriver);
return SDL_GPU_DRIVER_INVALID;
return NULL;
}
for (i = 0; backends[i]; i += 1) {
@@ -402,12 +402,12 @@ static SDL_GPUDriver SDL_GPUSelectBackend(
continue;
}
if (backends[i]->PrepareDriver(_this)) {
return backends[i]->backendflag;
return backends[i];
}
}
SDL_LogError(SDL_LOG_CATEGORY_GPU, "No supported SDL_GPU backend found!");
return SDL_GPU_DRIVER_INVALID;
return NULL;
}
#endif // SDL_GPU_DISABLED
@@ -455,10 +455,9 @@ SDL_GPUDevice *SDL_CreateGPUDeviceWithProperties(SDL_PropertiesID props)
bool debug_mode;
bool preferLowPower;
int i;
const char *gpudriver;
SDL_GPUDevice *result = NULL;
SDL_GPUDriver selectedBackend;
const SDL_GPUBootstrap *selectedBackend;
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (_this == NULL) {
@@ -494,17 +493,12 @@ SDL_GPUDevice *SDL_CreateGPUDeviceWithProperties(SDL_PropertiesID props)
}
selectedBackend = SDL_GPUSelectBackend(_this, gpudriver, format_flags);
if (selectedBackend != SDL_GPU_DRIVER_INVALID) {
for (i = 0; backends[i]; i += 1) {
if (backends[i]->backendflag == selectedBackend) {
result = backends[i]->CreateDevice(debug_mode, preferLowPower, props);
if (result != NULL) {
result->backend = backends[i]->backendflag;
result->shader_formats = backends[i]->shader_formats;
result->debug_mode = debug_mode;
break;
}
}
if (selectedBackend != NULL) {
result = selectedBackend->CreateDevice(debug_mode, preferLowPower, props);
if (result != NULL) {
result->backend = selectedBackend->name;
result->shader_formats = selectedBackend->shader_formats;
result->debug_mode = debug_mode;
}
}
return result;
@@ -521,13 +515,34 @@ void SDL_DestroyGPUDevice(SDL_GPUDevice *device)
device->DestroyDevice(device);
}
SDL_GPUDriver SDL_GetGPUDriver(SDL_GPUDevice *device)
int SDL_GetNumGPUDrivers(void)
{
CHECK_DEVICE_MAGIC(device, SDL_GPU_DRIVER_INVALID);
return SDL_arraysize(backends) - 1;
}
const char * SDL_GetGPUDriver(int index)
{
if (index < 0 || index >= SDL_GetNumGPUDrivers()) {
SDL_InvalidParamError("index");
return NULL;
}
return backends[index]->name;
}
const char * SDL_GetGPUDeviceDriver(SDL_GPUDevice *device)
{
CHECK_DEVICE_MAGIC(device, NULL);
return device->backend;
}
SDL_GPUShaderFormat SDL_GetGPUShaderFormats(SDL_GPUDevice *device)
{
CHECK_DEVICE_MAGIC(device, SDL_GPU_SHADERFORMAT_INVALID);
return device->shader_formats;
}
Uint32 SDL_GPUTextureFormatTexelBlockSize(
SDL_GPUTextureFormat format)
{

View File

@@ -693,12 +693,14 @@ struct SDL_GPUDevice
// Opaque pointer for the Driver
SDL_GPURenderer *driverData;
// Store this for SDL_GetGPUDriver()
SDL_GPUDriver backend;
// Store this for SDL_GetGPUDeviceDriver()
const char *backend;
// Store this for SDL_GetGPUShaderFormats()
SDL_GPUShaderFormat shader_formats;
// Store this for SDL_gpu.c's debug layer
bool debug_mode;
SDL_GPUShaderFormat shader_formats;
};
#define ASSIGN_DRIVER_FUNC(func, name) \
@@ -786,7 +788,6 @@ struct SDL_GPUDevice
typedef struct SDL_GPUBootstrap
{
const char *name;
const SDL_GPUDriver backendflag;
const SDL_GPUShaderFormat shader_formats;
bool (*PrepareDriver)(SDL_VideoDevice *_this);
SDL_GPUDevice *(*CreateDevice)(bool debug_mode, bool prefer_low_power, SDL_PropertiesID props);

View File

@@ -6432,7 +6432,6 @@ tryCreateDevice:
SDL_GPUBootstrap D3D11Driver = {
"direct3d11",
SDL_GPU_DRIVER_D3D11,
SDL_GPU_SHADERFORMAT_DXBC,
D3D11_PrepareDriver,
D3D11_CreateDevice

View File

@@ -8348,7 +8348,6 @@ static SDL_GPUDevice *D3D12_CreateDevice(bool debugMode, bool preferLowPower, SD
SDL_GPUBootstrap D3D12Driver = {
"direct3d12",
SDL_GPU_DRIVER_D3D12,
SDL_GPU_SHADERFORMAT_DXIL,
D3D12_PrepareDriver,
D3D12_CreateDevice

View File

@@ -4120,7 +4120,6 @@ static SDL_GPUDevice *METAL_CreateDevice(bool debugMode, bool preferLowPower, SD
SDL_GPUBootstrap MetalDriver = {
"metal",
SDL_GPU_DRIVER_METAL,
SDL_GPU_SHADERFORMAT_MSL | SDL_GPU_SHADERFORMAT_METALLIB,
METAL_PrepareDriver,
METAL_CreateDevice

View File

@@ -11889,7 +11889,6 @@ static SDL_GPUDevice *VULKAN_CreateDevice(bool debugMode, bool preferLowPower, S
SDL_GPUBootstrap VulkanDriver = {
"vulkan",
SDL_GPU_DRIVER_VULKAN,
SDL_GPU_SHADERFORMAT_SPIRV,
VULKAN_PrepareDriver,
VULKAN_CreateDevice