Skip to content

Commit

Permalink
Removed spv->wgsl shader cross-compilation from here and moved it to …
Browse files Browse the repository at this point in the history
…SDL_gpu_shadercross.
  • Loading branch information
klukaszek committed Oct 1, 2024
1 parent 94a82fe commit 50a72ef
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 498 deletions.
50 changes: 0 additions & 50 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1495,56 +1495,6 @@ elseif(EMSCRIPTEN)
if(SDL_GPU)
set(SDL_WEBGPU ON)
set(SDL_RENDER_WEBGPU ON)
set(SPIRV_CROSS_EMSC ${SDL3_SOURCE_DIR}/SPIRV-Cross/build)

if (NOT EXISTS ${SDL3_SOURCE_DIR}/SPIRV-Cross/build)
file(MAKE_DIRECTORY ${SDL3_SOURCE_DIR}/SPIRV-Cross/build)
endif()

if (NOT EXISTS ${SDL3_SOURCE_DIR}/SPIRV-Cross/build/CMakeCache.txt)
execute_process(
COMMAND emcmake cmake ..
WORKING_DIRECTORY ${SDL3_SOURCE_DIR}/SPIRV-Cross/build
)
endif()

if (NOT EXISTS ${SDL3_SOURCE_DIR}/SPIRV-Cross/build/libspirv-cross-c.a)
execute_process(
COMMAND emmake make
WORKING_DIRECTORY ${SDL3_SOURCE_DIR}/SPIRV-Cross/build
)
endif()

add_subdirectory("${SDL3_SOURCE_DIR}/SPIRV-Cross" EXCLUDE_FROM_ALL)

# We must link against libspirv-cross for Shader reflection
set(SPIRV_CROSS_LIBRARIES
${SPIRV_CROSS_EMSC}/libspirv-cross-c.a
${SPIRV_CROSS_EMSC}/libspirv-cross-cpp.a
${SPIRV_CROSS_EMSC}/libspirv-cross-core.a
${SPIRV_CROSS_EMSC}/libspirv-cross-glsl.a
${SPIRV_CROSS_EMSC}/libspirv-cross-hlsl.a
${SPIRV_CROSS_EMSC}/libspirv-cross-msl.a
${SPIRV_CROSS_EMSC}/libspirv-cross-reflect.a
${SPIRV_CROSS_EMSC}/libspirv-cross-util.a
)

# Link against SPIRV-Cross
sdl_link_dependency(spirv-cross-c LIBS ${SPIRV_CROSS_LIBRARIES})
sdl_include_directories(PRIVATE ${SDL3_SOURCE_DIR}/SPIRV-Cross)
link_directories(${SPIRV_CROSS_LIBRARY_DIRS})

sdl_include_directories(PRIVATE /usr/include/c++/11/)
sdl_include_directories(PRIVATE /usr/include/x86_64-linux-gnu/c++/11/)
sdl_link_dependency(stdc++ LIBS stdc++)

# Link against Tint for SPIR-V to WGSL conversion
set(TINT_DIR ${SDL3_SOURCE_DIR}/src/gpu/webgpu/tint-wasm/)
set(TINT_WASM ${TINT_DIR}/libtint_wasm.a)
sdl_link_dependency(tint LIBS ${TINT_WASM})
sdl_include_directories(PRIVATE ${TINT_DIR})
link_directories(${TINT_DIR})

set(HAVE_WEBGPU TRUE)
set(HAVE_RENDER_WEBGPU TRUE)
endif()
Expand Down
124 changes: 8 additions & 116 deletions src/gpu/SDL_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,11 @@ static const SDL_GPUBootstrap *SDL_GPUSelectBackend(SDL_PropertiesID props)
if (gpudriver != NULL) {
for (i = 0; backends[i]; i += 1) {
if (SDL_strcasecmp(gpudriver, backends[i]->name) == 0) {
if (!(backends[i]->shader_formats & format_flags)) {
if (!(backends[i]->shader_formats | format_flags)) {
SDL_Log("Selected GPU driver: %s", gpudriver);
SDL_Log("Expected Flags: %u", format_flags);
SDL_Log("WGSL Flag: %u", SDL_GPU_SHADERFORMAT_WGSL);
SDL_Log("Backend Flags: %u", backends[i]->shader_formats);
SDL_LogError(SDL_LOG_CATEGORY_GPU, "Required shader format for backend %s not provided!", gpudriver);
return NULL;
}
Expand Down Expand Up @@ -451,127 +455,12 @@ static const SDL_GPUBootstrap *SDL_GPUSelectBackend(SDL_PropertiesID props)
return NULL;
}

/*SDL_GPUDevice *SDL_CreateGPUDevice(*/
/* SDL_GPUShaderFormat formatFlags,*/
/* bool debugMode,*/
/* const char *name)*/
/*{*/
/* SDL_GPUDevice *result;*/
/*#ifdef __EMSCRIPTEN__*/
/* SDL_SetHint(SDL_HINT_GPU_DRIVER, "webgpu");*/
/*#endif*/
/* SDL_PropertiesID props = SDL_CreateProperties();*/
/* if (formatFlags & SDL_GPU_SHADERFORMAT_PRIVATE) {*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_PRIVATE_BOOL, true);*/
/* }*/
/* if (formatFlags & SDL_GPU_SHADERFORMAT_SPIRV) {*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOL, true);*/
/* }*/
/* if (formatFlags & SDL_GPU_SHADERFORMAT_DXBC) {*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOL, true);*/
/* }*/
/* if (formatFlags & SDL_GPU_SHADERFORMAT_DXIL) {*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOL, true);*/
/* }*/
/* if (formatFlags & SDL_GPU_SHADERFORMAT_MSL) {*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOL, true);*/
/* }*/
/* if (formatFlags & SDL_GPU_SHADERFORMAT_METALLIB) {*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOL, true);*/
/* }*/
/* SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOL, debugMode);*/
/* SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, name);*/
/* result = SDL_CreateGPUDeviceWithProperties(props);*/
/* SDL_DestroyProperties(props);*/
/* SDL_Log("SDL_CreateGPUDevice: %s", result ? "success" : "failure");*/
/* return result;*/
/*}*/

/*SDL_GPUDevice *SDL_CreateGPUDeviceWithProperties(SDL_PropertiesID props)*/
/*{*/
/* SDL_GPUShaderFormat formatFlags = 0;*/
/* bool debugMode;*/
/* bool preferLowPower;*/
/**/
/* const char *gpudriver;*/
/* SDL_VideoDevice *_this = SDL_GetVideoDevice();*/
/**/
/* if (_this == NULL) {*/
/* SDL_SetError("Video subsystem not initialized");*/
/* return NULL;*/
/* }*/
/**/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_PRIVATE_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_PRIVATE;*/
/* }*/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_SPIRV;*/
/* }*/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_DXBC;*/
/* }*/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_DXIL;*/
/* }*/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_MSL;*/
/* }*/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_METALLIB;*/
/* }*/
/* if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOL, false)) {*/
/* formatFlags |= SDL_GPU_SHADERFORMAT_WGSL;*/
/* }*/
/**/
/* gpudriver = SDL_GetHint(SDL_HINT_GPU_DRIVER);*/
/* if (gpudriver == NULL) {*/
/*#ifdef __EMSCRIPTEN__*/
/* gpudriver = SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, "webgpu");*/
/*#else*/
/* gpudriver = SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, NULL);*/
/*#endif*/
/* }*/
/**/
/* selectedBackend = SDL_GPUSelectBackend(props);*/
/* if (selectedBackend != SDL_GPU_DRIVER_INVALID) {*/
/* for (size_t i = 0; backends[i]; i += 1) {*/
/* if (backends[i]->backendflag == selectedBackend) {*/
/* result = backends[i]->CreateDevice(debugMode, preferLowPower, props);*/
/* if (result != NULL) {*/
/* result->backend = backends[i]->backendflag;*/
/* result->shaderFormats = backends[i]->shaderFormats;*/
/* result->debugMode = debugMode;*/
/* break;*/
/* }*/
/* }*/
/* }*/
/**/
/* SDL_SetError("SDL_HINT_GPU_DRIVER %s unsupported!", gpudriver);*/
/* return NULL;*/
/* }*/
/**/
/* for (size_t i = 0; backends[i]; i += 1) {*/
/* if ((backends[i]->shader_formats & formatFlags) == 0) {*/
/* // Don't select a backend which doesn't support the app's shaders.*/
/* continue;*/
/* }*/
/* if (backends[i]->PrepareDriver(_this)) {*/
/* return backends[i];*/
/* }*/
/* }*/
/**/
/* SDL_SetError("No supported SDL_GPU backend found!");*/
/* return NULL;*/
/*}*/

static void SDL_GPU_FillProperties(
SDL_PropertiesID props,
SDL_GPUShaderFormat formatFlags,
bool debug_mode,
const char *name)
{

SDL_GPUDevice *result;
#ifdef __EMSCRIPTEN__
SDL_SetHint(SDL_HINT_GPU_DRIVER, "webgpu");
#endif
Expand All @@ -594,6 +483,9 @@ static void SDL_GPU_FillProperties(
if (formatFlags & SDL_GPU_SHADERFORMAT_METALLIB) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOL, true);
}
if (formatFlags & SDL_GPU_SHADERFORMAT_WGSL) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOL, true);
}
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOL, debug_mode);
SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, name);
}
Expand Down
Loading

0 comments on commit 50a72ef

Please sign in to comment.