Skip to content

Commit 6cf39ee

Browse files
committed
gpu: Add SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION property
1 parent b8a9a37 commit 6cf39ee

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

include/SDL3/SDL_gpu.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,6 +2307,14 @@ extern SDL_DECLSPEC SDL_GPUDevice * SDLCALL SDL_CreateGPUDevice(
23072307
* either supports Tier 2 Resource Binding or does not support D3D12 in any
23082308
* capacity. Defaults to false.
23092309
*
2310+
* With the Vulkan renderer:
2311+
* - `SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION`: By
2312+
* default, Vulkan device enumeration includes drivers of all types, including
2313+
* software renderers (for example, the Lavapipe Mesa driver). This can be
2314+
* useful if your application _requires_ SDL_GPU, but if you can provide your
2315+
* own fallback renderer (for example, an OpenGL renderer) this property can
2316+
* be set to true. Defaults to false.
2317+
*
23102318
* \param props the properties to use.
23112319
* \returns a GPU context on success or NULL on failure; call SDL_GetError()
23122320
* for more information.
@@ -2337,6 +2345,7 @@ extern SDL_DECLSPEC SDL_GPUDevice * SDLCALL SDL_CreateGPUDeviceWithProperties(
23372345
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN "SDL.gpu.device.create.shaders.metallib"
23382346
#define SDL_PROP_GPU_DEVICE_CREATE_D3D12_ALLOW_FEWER_RESOURCE_SLOTS_BOOLEAN "SDL.gpu.device.create.d3d12.allowtier1resourcebinding"
23392347
#define SDL_PROP_GPU_DEVICE_CREATE_D3D12_SEMANTIC_NAME_STRING "SDL.gpu.device.create.d3d12.semantic"
2348+
#define SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION "SDL.gpu.device.create.vulkan.requirehardwareacceleration"
23402349

23412350
/**
23422351
* Destroys a GPU context previously returned by SDL_CreateGPUDevice.

src/gpu/vulkan/SDL_gpu_vulkan.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ struct VulkanRenderer
10961096

10971097
bool debugMode;
10981098
bool preferLowPower;
1099+
bool requireHardwareAcceleration;
10991100
SDL_PropertiesID props;
11001101
Uint32 allowedFramesInFlight;
11011102

@@ -11343,6 +11344,15 @@ static bool VULKAN_INTERNAL_GetDeviceRank(
1134311344
deviceType = physicalDeviceProperties.deviceType;
1134411345
}
1134511346

11347+
if (renderer->requireHardwareAcceleration) {
11348+
if (deviceType != VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
11349+
deviceType != VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU &&
11350+
deviceType != VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU) {
11351+
// In addition to CPU, "Other" drivers (including layered drivers) don't count as hardware-accelerated
11352+
return 0;
11353+
}
11354+
}
11355+
1134611356
/* Apply a large bias on the devicePriority so that we always respect the order in the priority arrays.
1134711357
* We also rank by e.g. VRAM which should have less influence than the device type.
1134811358
*/
@@ -11818,6 +11828,8 @@ static bool VULKAN_PrepareDriver(SDL_VideoDevice *_this, SDL_PropertiesID props)
1181811828
renderer->desiredDeviceFeatures.sampleRateShading = VK_TRUE;
1181911829
renderer->desiredDeviceFeatures.imageCubeArray = VK_TRUE;
1182011830

11831+
renderer->requireHardwareAcceleration = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION, false);
11832+
1182111833
result = VULKAN_INTERNAL_PrepareVulkan(renderer);
1182211834
if (result) {
1182311835
renderer->vkDestroyInstance(renderer->instance, NULL);
@@ -11867,6 +11879,8 @@ static SDL_GPUDevice *VULKAN_CreateDevice(bool debugMode, bool preferLowPower, S
1186711879
renderer->desiredDeviceFeatures.sampleRateShading = VK_TRUE;
1186811880
renderer->desiredDeviceFeatures.imageCubeArray = VK_TRUE;
1186911881

11882+
renderer->requireHardwareAcceleration = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_VULKAN_REQUIRE_HARDWARE_ACCELERATION, false);
11883+
1187011884
if (!VULKAN_INTERNAL_PrepareVulkan(renderer)) {
1187111885
SET_STRING_ERROR("Failed to initialize Vulkan!");
1187211886
SDL_free(renderer);

0 commit comments

Comments
 (0)