Made instructions for system install of dawn and switched to SDL to make that work

This commit is contained in:
Warwick New 2026-07-29 16:43:53 +01:00
parent 888abd97e4
commit 144edab071
7 changed files with 107 additions and 34 deletions

3
.gitmodules vendored
View file

@ -2,3 +2,6 @@
path = extern/dawn path = extern/dawn
url = https://github.com/google/dawn.git url = https://github.com/google/dawn.git
shallow = true shallow = true
[submodule "extern/sdl3webgpu"]
path = extern/sdl3webgpu
url = https://github.com/eliemichel/sdl3webgpu.git

View file

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.22)
project(webgpu) project(webgpu-engine)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
# Compile commands for dev # Compile commands for dev
@ -9,40 +9,77 @@ if(CMAKE_EXPORT_COMPILE_COMMANDS)
${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}) ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})
endif() endif()
# Binaries add_executable(${PROJECT_NAME})
add_executable( ${PROJECT_NAME} )
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
$<INSTALL_INTERFACE:include/>
)
file(GLOB_RECURSE SOURCE_FILES
${CMAKE_SOURCE_DIR}/src/*.cpp)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_FILES})
# Error on memory address issues in debug mode # Error on memory address issues in debug mode
if(CMAKE_BUILD_TYPE MATCHES "Debug") if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(CMAKE_C_FLAGS set(CMAKE_CXX_FLAGS
"${CMAKE_C_FLAGS} -Werror -fsanitize=undefined -fsanitize=address" "${CMAKE_CXX_FLAGS} -Werror -fsanitize=undefined -fsanitize=address"
) )
target_link_options(${PROJECT_NAME} target_link_options(${PROJECT_NAME}
BEFORE PUBLIC -fsanitize=undefined PUBLIC -fsanitize=address BEFORE PUBLIC -fsanitize=undefined PUBLIC -fsanitize=address
) )
endif() endif()
# Dawn library set up # Dawn library setup
# If we're in debug mode fast linking is the priority over easy distribution. find_package(Threads REQUIRED)
if(CMAKE_BUILD_TYPE MATCHES "Debug") find_package(Dawn)
set(DAWN_BUILD_MONOLITHIC_LIBRARY "SHARED")
endif()
set(DAWN_FETCH_DEPENDENCIES ON)
add_subdirectory("extern/dawn" EXCLUDE_FROM_ALL)
if (NOT Dawn_FOUND)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(DAWN_BUILD_MONOLITHIC_LIBRARY "SHARED")
endif()
set(DAWN_FETCH_DEPENDENCIES ON CACHE BOOL "" FORCE)
# Prevent Dawn from building excessive tools/samples to save compile time
set(DAWN_BUILD_SAMPLES OFF CACHE BOOL "" FORCE)
set(DAWN_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory("extern/dawn" EXCLUDE_FROM_ALL)
# Linking set(DAWN_LINK_TARGET webgpu_dawn)
if(EMSCRIPTEN)
set_target_properties(app PROPERTIES SUFFIX ".html")
target_link_libraries(app PRIVATE emdawnwebgpu_cpp webgpu_glfw)
target_link_options(app PRIVATE "-sASYNCIFY=1" "-sUSE_GLFW=3")
else() else()
target_link_libraries(${PROJECT_NAME} PRIVATE webgpu_dawn webgpu_glfw glfw) set(DAWN_LINK_TARGET dawn::webgpu_dawn)
endif()
# Find SDL3 Packages
find_package(SDL3 REQUIRED)
find_package(SDL3_image REQUIRED)
set(SDL3_image_LIBRARIES SDL3_image::SDL3_image)
# Force the 'webgpu' dependency expected by sdl3webgpu to point to Dawn instead
if(NOT TARGET webgpu)
add_library(webgpu ALIAS ${DAWN_LINK_TARGET})
endif()
# Configure and include sdl3webgpu
add_subdirectory(extern/sdl3webgpu)
# Include directories
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
$<INSTALL_INTERFACE:include/>
${SDL3_INCLUDE_DIRS}
${SDL3_image_INCLUDE_DIRS}
)
file(GLOB_RECURSE SOURCE_FILES ${CMAKE_SOURCE_DIR}/src/*.cpp)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_FILES})
# Linking Layer
if(EMSCRIPTEN)
# Note: your target is ${PROJECT_NAME}, updated 'app' references to match
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
target_link_libraries(${PROJECT_NAME} PRIVATE emdawnwebgpu_cpp webgpu_glfw)
target_link_options(${PROJECT_NAME} PRIVATE "-sASYNCIFY=1" "-sUSE_GLFW=3")
else()
target_link_libraries(${PROJECT_NAME} PRIVATE
${DAWN_LINK_TARGET}
sdl3webgpu
SDL3::SDL3
${SDL3_image_LIBRARIES}
)
endif() endif()

View file

@ -2,3 +2,14 @@ My build command
```bash ```bash
CXX=clang++ CC=clang cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -GNinja -B build . && cmake --build build -j16 CXX=clang++ CC=clang cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -GNinja -B build . && cmake --build build -j16
``` ```
## Development
This project relies on a webgpu native implementation called dawn. If you plan
on doing any development in this project you should install it systemwide to
reduce the chances of having to re-install it while working on the project.
I like to use this command to generate the build project so it has debug headers
and is a shared library for faster linking `cmake -S . -B out/Debug -G Ninja
-DDAWN_FETCH_DEPENDENCIES=ON -DDAWN_ENABLE_INSTALL=ON -DGLFW_INSTALL=ON
-DCMAKE_BUILD_TYPE=Debug -DDAWN_BUILD_MONOLITHIC_LIBRARY=SHARED
-DDAWN_BUILD_SAMPLES=OFF` and use the rest of the instructions
[here](https://dawn.googlesource.com/dawn.git/+/HEAD/docs/quickstart-cmake.md).

2
extern/dawn vendored

@ -1 +1 @@
Subproject commit 98747c789b4be3793d8676413bb1ab163b8e08a9 Subproject commit 92c4ae4cfeb209d513ef619014084b79fd6f1af1

1
extern/sdl3webgpu vendored Submodule

@ -0,0 +1 @@
Subproject commit 4fa9d70935e41c075f664b231ee10a64262a8ac7

View file

@ -3,12 +3,12 @@
#include <iostream> #include <iostream>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <SDL3/SDL.h>
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
#include <dawn/webgpu_cpp_print.h> #include <dawn/webgpu_cpp_print.h>
#include <webgpu/webgpu_cpp.h> #include <webgpu/webgpu_cpp.h>
#include <webgpu/webgpu_glfw.h>
namespace wne { namespace wne {
// TODO: load me in a better way // TODO: load me in a better way

View file

@ -1,5 +1,10 @@
#include "wne/window.hpp" #include "wne/window.hpp"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_video.h>
#include <sdl3webgpu.h>
namespace wne { namespace wne {
void window::ConfigureSurface() { void window::ConfigureSurface() {
@ -91,26 +96,42 @@ window::window() {
instance.WaitAny(f2, UINT64_MAX); instance.WaitAny(f2, UINT64_MAX);
} }
void window::Start() { void window::Start() {
if (!glfwInit()) { if (!SDL_Init(SDL_INIT_VIDEO)) {
return; return;
} }
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); SDL_Window *window = SDL_CreateWindow("Hello World!", kWidth, kHeight, 0);
GLFWwindow *window =
glfwCreateWindow(kWidth, kHeight, "WebGPU window", nullptr, nullptr); // surface = wgpu::glfw::CreateSurfaceForWindow(instance, window);
surface = wgpu::glfw::CreateSurfaceForWindow(instance, window); WGPUSurface c_surface = SDL_GetWGPUSurface(instance.Get(), window);
surface = wgpu::Surface::Acquire(c_surface);
InitGraphics(); InitGraphics();
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
emscripten_set_main_loop(Render, 0, false); emscripten_set_main_loop(Render, 0, false);
#else #else
while (!glfwWindowShouldClose(window)) { bool wants_running = true;
glfwPollEvents(); while (wants_running) {
// TODO: Create event handler files
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
wants_running = false;
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.key == SDLK_ESCAPE) {
wants_running = false;
}
break;
}
}
Render(); Render();
surface.Present(); surface.Present();
instance.ProcessEvents(); instance.ProcessEvents();
} }
#endif #endif
SDL_Quit();
} }
} // namespace wne } // namespace wne