Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,10 @@ dep_option(SDL_RENDER_D3D12 "Enable the Direct3D 12 render driver" ON "SD
dep_option(SDL_RENDER_METAL "Enable the Metal render driver" ON "SDL_RENDER;${APPLE}" OFF)
dep_option(SDL_RENDER_GPU "Enable the SDL_GPU render driver" ON "SDL_RENDER;SDL_GPU" OFF)
dep_option(SDL_VIVANTE "Use Vivante EGL video driver" ON "${UNIX_SYS};SDL_CPU_ARM32" OFF)
dep_option(SDL_WEBGPU "Enable WebGPU support" OFF "SDL_RENDER;${EMSCRIPTEN}" OFF)
dep_option(SDL_VULKAN "Enable Vulkan support" ON "SDL_VIDEO;ANDROID OR APPLE OR LINUX OR FREEBSD OR WINDOWS" OFF)
dep_option(SDL_RENDER_VULKAN "Enable the Vulkan render driver" ON "SDL_RENDER;SDL_VULKAN" OFF)
dep_option(SDL_RENDER_WEBGPU "Enable the WebGPU render driver" OFF "SDL_RENDER;SDL_WEBGPU" OFF)
dep_option(SDL_METAL "Enable Metal support" ON "APPLE" OFF)
set_option(SDL_OPENVR "Use OpenVR video driver" OFF)
dep_option(SDL_KMSDRM "Use KMS DRM video driver" ${UNIX_SYS} "SDL_VIDEO" OFF)
Expand Down Expand Up @@ -1543,6 +1545,12 @@ elseif(EMSCRIPTEN)
set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/video/emscripten/*.c")
set(HAVE_SDL_VIDEO TRUE)
#enable webgpu
if(SDL_GPU AND SDL_WEBGPU)
set(SDL_RENDER_WEBGPU ON)
set(HAVE_WEBGPU TRUE)
set(HAVE_RENDER_WEBGPU TRUE)
endif()

#enable gles
if(SDL_OPENGLES)
Expand Down Expand Up @@ -1890,6 +1898,42 @@ elseif(UNIX AND NOT APPLE AND NOT RISCOS AND NOT HAIKU)
set (HAVE_VSNPRINTF 0)
set (USE_POSIX_SPAWN 1)
endif()

if(SDL_WEBGPU) # Native platform WebGPU handling
set(SDL_VIDEO_WEBGPU 1)

# This should probably require a compilation flag with the path but for now
# SDL3/SDL_webgpu.h points to wgpu/ for the webgpu header.
# The user must provide a wgpu library of their choice.
set(SDL_WEBGPU_PATH "wgpu" CACHE PATH
"Path to WebGPU library (must contain include/ and lib/ directories)")
if(NOT SDL_WEBGPU_PATH)
message(FATAL_ERROR "SDL_WEBGPU enabled but SDL_WEBGPU_PATH not specified! "
"Please provide path to WebGPU library installation.")
endif()

# Verify library structure
find_path(WEBGPU_INCLUDE_DIR webgpu.h
PATHS "${SDL_WEBGPU_PATH}/include/webgpu/"
NO_DEFAULT_PATH
REQUIRED
)
find_library(WEBGPU_LIBRARY
NAMES wgpu_native
PATHS "${SDL_WEBGPU_PATH}/lib/"
NO_DEFAULT_PATH
REQUIRED
)

# Add to build system
sdl_include_directories(PRIVATE SYSTEM "${WEBGPU_INCLUDE_DIR}")
sdl_link_dependency(WebGPU LIBS "${WEBGPU_LIBRARY}")

set(SDL_RENDER_WEBGPU ON)
set(HAVE_WEBGPU TRUE)
set(HAVE_RENDER_WEBGPU TRUE)
endif()

elseif(WINDOWS)
check_c_source_compiles("
#include <windows.h>
Expand Down Expand Up @@ -2107,6 +2151,39 @@ elseif(WINDOWS)
set(HAVE_RENDER_VULKAN TRUE)
endif()
endif()
if(SDL_WEBGPU) # Native platform WebGPU handling
set(SDL_VIDEO_WEBGPU 1)
# This should probably require a compilation flag with the path but for now
# SDL3/SDL_webgpu.h points to wgpu/ for the webgpu header.
# The user must provide a webgpu library of their choice.
set(SDL_WEBGPU_PATH "wgpu" CACHE PATH
"Path to WebGPU library (must contain include/ and lib/ directories)")
if(NOT SDL_WEBGPU_PATH)
message(FATAL_ERROR "SDL_WEBGPU enabled but SDL_WEBGPU_PATH not specified! "
"Please provide path to WebGPU library installation.")
endif()
# Locate headers and static library
find_path(WEBGPU_INCLUDE_DIR webgpu.h
PATHS "${SDL_WEBGPU_PATH}/include/webgpu/"
NO_DEFAULT_PATH
REQUIRED
)
find_library(WEBGPU_LIBRARY
NAMES wgpu_native
PATHS "${SDL_WEBGPU_PATH}/lib/"
NO_DEFAULT_PATH
REQUIRED
)

# Add include directories
sdl_include_directories(PRIVATE SYSTEM "${WEBGPU_INCLUDE_DIR}")
sdl_link_dependency(WebGPU LIBS "${WEBGPU_LIBRARY}")

set(SDL_RENDER_WEBGPU ON)
set(HAVE_WEBGPU TRUE)
set(HAVE_RENDER_WEBGPU TRUE)
endif()


sdl_glob_sources("${SDL3_SOURCE_DIR}/src/tray/windows/*.c")
set(HAVE_SDL_TRAY TRUE)
Expand Down Expand Up @@ -3026,6 +3103,11 @@ if(SDL_GPU)
set(SDL_GPU_VULKAN 1)
set(HAVE_SDL_GPU TRUE)
endif()
if(SDL_RENDER_WEBGPU)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/gpu/webgpu/*.c")
set(SDL_GPU_WEBGPU 1)
set(HAVE_SDL_GPU TRUE)
endif()
if(SDL_RENDER_GPU)
set(SDL_VIDEO_RENDER_GPU 1)
set(HAVE_RENDER_GPU TRUE)
Expand Down
2 changes: 2 additions & 0 deletions include/SDL3/SDL_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ typedef Uint32 SDL_GPUShaderFormat;
#define SDL_GPU_SHADERFORMAT_DXIL (1u << 3) /**< DXIL SM6_0 shaders for D3D12. */
#define SDL_GPU_SHADERFORMAT_MSL (1u << 4) /**< MSL shaders for Metal. */
#define SDL_GPU_SHADERFORMAT_METALLIB (1u << 5) /**< Precompiled metallib shaders for Metal. */
#define SDL_GPU_SHADERFORMAT_WGSL (1u << 6) /**< WGSL shaders for WebGPU. */

/**
* Specifies the format of a vertex attribute.
Expand Down Expand Up @@ -2187,6 +2188,7 @@ extern SDL_DECLSPEC SDL_GPUDevice * SDLCALL SDL_CreateGPUDeviceWithProperties(
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN "SDL.gpu.device.create.shaders.msl"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN "SDL.gpu.device.create.shaders.metallib"
#define SDL_PROP_GPU_DEVICE_CREATE_D3D12_SEMANTIC_NAME_STRING "SDL.gpu.device.create.d3d12.semantic"
#define SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOLEAN "SDL.gpu.device.create.shaders.wgsl"

/**
* Destroys a GPU context previously returned by SDL_CreateGPUDevice.
Expand Down
49 changes: 49 additions & 0 deletions include/SDL3/SDL_webgpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
// Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <[email protected]>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

/**
* # CategoryWebGPU
**/

/*
NOTE: Some of the enum conversion functions are a nightmare to manage between the different flavours of webgpu.h

wgpu-native source code pulls it's header from here: https://github.com/webgpu-native/webgpu-headers/blob/main/webgpu.h
however the header provided in the release version of the library: https://github.com/gfx-rs/wgpu-native/releases
have some changes to the enum values that kind of screw everything up.

Gotta wait until the header is finalized to get this fixed.

webgpu.h from webgpu-headers SHOULD work for all backends, except for Emscripten and wgpu-native apparently!
So as I suspected, Dawn would have been the best pick to work with like I had originally done before swapping to wgpu-native out of convenience.
*/

#ifndef __EMSCRIPTEN__
// #include "../../wgpu/include/webgpu/webgpu.h"
#include "../../wgpu/include/webgpu/wgpu.h"
#endif // webgpu.h is provided by emscripten when using emcc with -sUSE_WEBGPU=1

#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_video.h>

extern SDL_DECLSPEC bool SDLCALL SDL_WebGPU_CreateSurface(SDL_Window *window,
WGPUInstance instance,
WGPUSurface *retSurface);
4 changes: 4 additions & 0 deletions include/build_config/SDL_build_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,15 @@
/* Enable Metal support */
#cmakedefine SDL_VIDEO_METAL 1

/* Enable WebGPU support */
#cmakedefine SDL_VIDEO_WEBGPU 1

/* Enable GPU support */
#cmakedefine SDL_GPU_D3D11 1
#cmakedefine SDL_GPU_D3D12 1
#cmakedefine SDL_GPU_VULKAN 1
#cmakedefine SDL_GPU_METAL 1
#cmakedefine SDL_GPU_WEBGPU 1

/* Enable system power support */
#cmakedefine SDL_POWER_ANDROID 1
Expand Down
1 change: 1 addition & 0 deletions include/build_config/SDL_build_config_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
#define SDL_GPU_METAL 1
#define SDL_VIDEO_VULKAN 1
#define SDL_GPU_VULKAN 1
#define SDL_GPU_WEBGPU 1
#define SDL_VIDEO_RENDER_GPU 1
#endif

Expand Down
9 changes: 9 additions & 0 deletions src/gpu/SDL_gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ static const SDL_GPUBootstrap *backends[] = {
#endif
#ifdef SDL_GPU_D3D12
&D3D12Driver,
#endif
#ifdef SDL_GPU_WEBGPU
&WebGPUDriver,
#endif
NULL
};
Expand Down Expand Up @@ -404,6 +407,9 @@ static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props)
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_METALLIB;
}
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOLEAN, false)) {
format_flags |= SDL_GPU_SHADERFORMAT_WGSL;
}

gpudriver = SDL_GetHint(SDL_HINT_GPU_DRIVER);
if (gpudriver == NULL) {
Expand Down Expand Up @@ -466,6 +472,9 @@ static void SDL_GPU_FillProperties(
if (format_flags & SDL_GPU_SHADERFORMAT_METALLIB) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN, true);
}
if (format_flags & SDL_GPU_SHADERFORMAT_WGSL) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_WGSL_BOOLEAN, true);
}
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, debug_mode);
SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, name);
}
Expand Down
1 change: 1 addition & 0 deletions src/gpu/SDL_sysgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ extern "C" {
extern SDL_GPUBootstrap VulkanDriver;
extern SDL_GPUBootstrap D3D12Driver;
extern SDL_GPUBootstrap MetalDriver;
extern SDL_GPUBootstrap WebGPUDriver;
extern SDL_GPUBootstrap PrivateGPUDriver;

#ifdef __cplusplus
Expand Down
40 changes: 40 additions & 0 deletions src/gpu/webgpu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SDL3 GPU WebGPU Backend
Status: Work In Progress

Author: Kyle Lukaszek

# Currently Native Only
Web demo will be back eventually.

# Building
Find your flavour of [wgpu-native](https://github.com/gfx-rs/wgpu-native/releases) and download then unzip the contents into a `wgpu/` directory.

To clone and build SDL WebGPU from source:
```
git clone https://github.com/klukaszek/SDL
mv wgpu/ SDL/wgpu/
cd SDL
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DSDL_WEBGPU=ON -DSDL_RENDER_WEBGPU=ON
cmake --build . --config Release --parallel
sudo cmake --install . --config Release
```

To clone and build examples:
```
git clone https://github.com/klukaszek/SDL3-WebGPU-Examples
cd SDL3-WebGPU-Examples
git submodule update --init
git submodule update --remote
mkdir build
cd build
cmake .. -DSDL3_DIR="full/path/to/SDL/build" -DWEBGPU_PATH="full/path/to/your-wgpu-install/"
make
```

You can also provide the `-GNinja` flag to any of the `emcmake` commands to use Ninja instead of Make.

## More Info

- See checklist for a list of completed SDL3 API functions.
Loading