Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL3 GPU WebGPU Backend #12046

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
850caed
Nuked my repo due to it being 600 commits ahead of upstream even thou…
klukaszek Nov 17, 2024
35afb6e
Got Point, Linear, and Aniso samplers working with Clamp and Repeat
klukaszek Nov 20, 2024
fd2e2f5
Began work on PushVertexUniformData. Investigated bug where certain G…
klukaszek Dec 16, 2024
b5b0b4e
Cleaned up logging in preparation for managing Uniform Buffers.
klukaszek Dec 21, 2024
37d08d1
Refactored buffer and texture data structures to be more simple as th…
klukaszek Dec 27, 2024
34f0a47
PushVertexUniformData() and PushFragmentUniformData() no longer cause…
klukaszek Dec 27, 2024
c380e4d
Fixed SDLToWGPUTextureUsageFlags() to properly handle bit flags inste…
klukaszek Dec 28, 2024
df19be0
Working on getting WebGPU_Blit() ready for implementation by solving …
klukaszek Dec 28, 2024
7605ea7
Implemented simple miscellaneous functions to knock them off of the c…
klukaszek Dec 28, 2024
9496c2e
Trying to get array layers working for 3D textures since WebGPU only …
klukaszek Jan 7, 2025
478b4cd
Implemented a blitting pipeline for WebGPU since there is no blit fun…
klukaszek Jan 8, 2025
ee68d05
Refactored to use SDL_GPU_BlitCommon instead of writing my own WebGPU…
klukaszek Jan 9, 2025
abae635
Made more progess on 2D texture array blitting. Now I need to investi…
klukaszek Jan 9, 2025
9b8e259
Refactored several calls to SDL_malloc to SDL_stack_alloc if data is …
klukaszek Jan 9, 2025
cecebf8
Feat: Added stencil reference adjustment
klukaszek Jan 10, 2025
fd085f6
Feat: Got BlitCube example working
klukaszek Jan 10, 2025
4fe10d0
Refactor: Eliminated many unnecessary dynamic memory allocations. Far…
klukaszek Jan 11, 2025
8a42bb4
Removed print statements that angered different test pipelines.
klukaszek Jan 14, 2025
51a5c38
Misc: Moved SDL3 WebGPU Checklist to src/gpu/webgpu/
klukaszek Jan 14, 2025
cb91daf
Misc: Moved SDL3 WebGPU Checklist to src/gpu/webgpu/
klukaszek Jan 14, 2025
140802a
Misc: Added SDL3 GPU ReadMe
klukaszek Jan 14, 2025
bcdc2d5
Misc: Added SDL3 GPU ReadMe
klukaszek Jan 14, 2025
9e6dc5a
Misc: Updated emscripten's version for the test pipeline.
klukaszek Jan 14, 2025
5565a59
Misc: Updated emscripten's version for the test pipeline.
klukaszek Jan 14, 2025
f967732
Misc: trying to get Emscripten test pipeline working.
klukaszek Jan 14, 2025
467c870
Fix: Rebased with upstream and solved a recently implemented bug with…
klukaszek Jan 21, 2025
c21747f
Feat: Made swapchain a static allocation.
klukaszek Jan 21, 2025
58e6140
Refactor: Removed Emscripten specific Swapchain logic. Now I manually…
klukaszek Jan 21, 2025
d570ca5
Feat: Implemented indirect drawing support for WebGPU.
klukaszek Jan 21, 2025
11d8ef7
Refactor: Added explicit loop termination while waiting for the devic…
klukaszek Jan 22, 2025
8d601ec
Refactor: Setting of viewport and scissor is done in WebGPU_BeginRend…
klukaszek Jan 22, 2025
a385d47
Misc: Added standard text to top of file.
klukaszek Jan 22, 2025
c226ca5
Misc: Changed some imports.
klukaszek Jan 22, 2025
2b22917
Misc: Changed some more imports.
klukaszek Jan 22, 2025
e24094d
Refactor: Changed default renderpass state initialization to better m…
klukaszek Jan 22, 2025
c1d8428
Refactor: Removed ~0u conversion as I had forgotten that I had resolv…
klukaszek Jan 22, 2025
d2fbc02
Refactor: Moved SDL_internal.h to the very top
klukaszek Jan 22, 2025
09523e4
Refactor: Started implementing fencing properly.
klukaszek Jan 23, 2025
8e72939
Refactor: Added fence pool and associated internal functions.
klukaszek Jan 23, 2025
bc4a11c
Refactor: Added uniform buffer pool.
klukaszek Jan 23, 2025
0533e73
Refactor: Implemented placeholder fencing logic. Has not actually bee…
klukaszek Jan 23, 2025
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
4 changes: 2 additions & 2 deletions .github/workflows/generic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
- uses: mymindstorm/setup-emsdk@v14
if: ${{ matrix.platform.platform == 'emscripten' }}
with:
version: 3.1.35
version: 3.1.74
- uses: browser-actions/setup-chrome@v1
id: setup-chrome
if: ${{ matrix.platform.platform == 'emscripten' }}
Expand Down Expand Up @@ -382,4 +382,4 @@ jobs:
with:
if-no-files-found: error
name: '${{ matrix.platform.artifact }}-apks'
path: build/test/*.apk
path: build/test/*.apk
27 changes: 20 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ set(SDL_STATIC_AVAILABLE ON)
# All these *_DEFAULT vars will default to ON if not specified,
# so you only need to override them if they need to be disabled.
if(EMSCRIPTEN)

# Set up default values for the currently supported set of subsystems:
# Emscripten/Javascript does not have assembly support, a dynamic library
# loading architecture, or low-level CPU inspection.
Expand Down Expand Up @@ -365,8 +366,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" ON "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" ON "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 @@ -1535,14 +1538,19 @@ elseif(EMSCRIPTEN)
set(SDL_VIDEO_DRIVER_EMSCRIPTEN 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/video/emscripten/*.c")
set(HAVE_SDL_VIDEO TRUE)

#enable gles
if(SDL_OPENGLES)
set(SDL_VIDEO_OPENGL_EGL 1)
set(HAVE_OPENGLES TRUE)
set(SDL_VIDEO_OPENGL_ES2 1)
set(SDL_VIDEO_RENDER_OGL_ES2 1)
if(SDL_GPU)
set(SDL_WEBGPU ON)
set(SDL_RENDER_WEBGPU ON)
set(HAVE_WEBGPU TRUE)
set(HAVE_RENDER_WEBGPU TRUE)
endif()
# #enable gles
# if(SDL_OPENGLES)
# set(SDL_VIDEO_OPENGL_EGL 1)
# set(HAVE_OPENGLES TRUE)
# set(SDL_VIDEO_OPENGL_ES2 1)
# set(SDL_VIDEO_RENDER_OGL_ES2 1)
# endif()
endif()

CheckPTHREAD()
Expand Down Expand Up @@ -3017,6 +3025,11 @@ if(SDL_GPU)
set(SDL_GPU_VULKAN 1)
set(HAVE_SDL_GPU TRUE)
endif()
if(SDL_RENDER_WEBGPU AND EMSCRIPTEN)
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
527 changes: 270 additions & 257 deletions include/SDL3/SDL_gpu.h

Large diffs are not rendered by default.

235 changes: 104 additions & 131 deletions src/gpu/SDL_gpu.c

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/gpu/SDL_sysgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect #include

#include "../video/SDL_sysvideo.h"
#include "SDL_internal.h"

Expand Down Expand Up @@ -977,6 +978,7 @@ extern "C" {
extern SDL_GPUBootstrap VulkanDriver;
extern SDL_GPUBootstrap D3D12Driver;
extern SDL_GPUBootstrap MetalDriver;
extern SDL_GPUBootstrap WebGPUDriver;
extern SDL_GPUBootstrap PS5Driver;

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

Author: Kyle Lukaszek

## Compiling with Emscripten
**Emscripten version 3.1.69+ is required to make use of SDL3 WebGPU.**
**There is an issue with Emscripten versions (>=3.1.65 & < 3.1.69) that breaks buffer copying**

To clone and build SDL WebGPU from source:
```
git clone https://github.com/klukaszek/SDL
cd SDL
mkdir build
cd build
emcmake 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
emcmake cmake .. -DSDL3_DIR="full/path/to/SDL/build"
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.
136 changes: 136 additions & 0 deletions src/gpu/webgpu/SDL_WebGPU_Checklist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# SDL WebGPU Driver Roadmap / Checklist

These methods are tested by the example suite:
- https://github.com/klukaszek/SDL3-WebGPU-Examples

Getting my fork of the example suite merged with the original shouldn't be too much work but it needs to tested first.

The original example suite can be found at:
- https://github.com/TheSpydog/SDL_gpu_examples/

Currently, the WebGPU backend only supports Emscripten as the compilation target, though I've added Elie Michel's cross-platform surface configuration logic to the backend. I have not tested it with any native implementations however.

# Checklist

## General
- [x] DestroyDevice
- [x] SupportsPresentMode
- [x] ClaimWindow
- [x] ReleaseWindow

## Swapchains
- [x] SetSwapchainParameters
- [x] SupportsTextureFormat
- [x] SupportsSampleCount
- [x] SupportsSwapchainComposition

## Command Buffers and Fences
- [x] AcquireCommandBuffer
- [x] AcquireSwapchainTexture
- [x] GetSwapchainTextureFormat
- [x] Submit
- [x] SubmitAndAcquireFence (Should just call Submit)
- [x] Cancel (Should be no-op for WebGPU)
- [x] Wait (Should be no-op for WebGPU)
- [x] WaitForFences (Should be no-op for WebGPU)
- [x] QueryFence (Should be no-op for WebGPU)
- [x] ReleaseFence (Should be no-op for WebGPU)

Note: WebGPU has no exposed fence API.

## Buffers
- [x] CreateBuffer
- [x] ReleaseBuffer
- [x] SetBufferName
- [x] CreateTransferBuffer
- [x] ReleaseTransferBuffer
- [x] MapTransferBuffer
- [x] UnmapTransferBuffer
- [x] UploadToBuffer
- [x] DownloadFromBuffer
- [x] CopyBufferToBuffer

## Textures
- [x] CreateTexture
- [x] ReleaseTexture
- [x] SetTextureName
- [x] UploadToTexture
- [x] DownloadFromTexture (needs to be tested)
- [x] CopyTextureToTexture (needs to be tested)
- [ ] GenerateMipmaps
- Requires custom compute pipeline implementation.
- https://eliemichel.github.io/LearnWebGPU/basic-compute/image-processing/mipmap-generation.html

## Samplers
- [x] CreateSampler
- [x] ReleaseSampler

## Debugging
- [x] InsertDebugLabel
- [x] PushDebugGroup
- [x] PopDebugGroup

## Graphics Pipelines
- [x] CreateGraphicsPipeline
- [x] BindGraphicsPipeline
- [x] ReleaseGraphicsPipeline

## Compute Pipelines
- [ ] CreateComputePipeline
- [ ] BindComputePipeline
- [ ] ReleaseComputePipeline

## Shaders
- [x] CreateShader
- [x] ReleaseShader

## Rendering
- [x] BeginRenderPass
- [x] EndRenderPass
- [x] DrawPrimitivesIndirect
- [x] DrawPrimitives
- [x] DrawIndexedPrimitives
- [x] DrawIndexedPrimitivesIndirect

## Copy Passes
- [x] BeginCopyPass
- [x] EndCopyPass

## Compute Passes
- [ ] BeginComputePass
- [ ] EndComputePass
- [ ] DispatchCompute
- [ ] DispatchComputeIndirect
- [ ] BindComputeSamplers
- [ ] BindComputeStorageTextures
- [ ] BindComputeStorageBuffers
- [ ] PushComputeUniformData

## Fragment Stage
- [x] BindFragmentSamplers
- [ ] BindFragmentStorageTextures
- [ ] BindFragmentStorageBuffers
- [ ] PushFragmentUniformData
- Needs to be rewritten.

## Vertex Stage
- [x] BindVertexBuffers
- [x] BindIndexBuffer
- [x] BindVertexSamplers
- [ ] BindVertexStorageTextures
- [ ] BindVertexStorageBuffers
- [ ] PushVertexUniformData
- Needs to be rewritten.

## Rendering States
- [x] SetViewport
- [x] SetScissor
- [x] SetBlendConstants
- [x] SetStencilReference

## Composition
- [ ] Blit
- Mostly functional.
- Bug: Example "Blit2DArray.c" has a sampler issue where the RHS is not downsampled.
- Bug: Example "TriangleMSAA.c" does not cycle between different sample counts.

Loading