Skip to content

Commit

Permalink
native WebGPU backend stage 1 (dawn) (#128)
Browse files Browse the repository at this point in the history
feat: Adds stage 1 WebGPU/Dawn experimental backend.

What that means is that the project now supports Dawn as a backend, but that no HAL has been written yet, and using the WebGPU layer directly isn't properly functioning yet either (though much of it is already present). These will come at a later time.

* added dawn as a native webgpu backend
* added 64 bit native WebGPU support to windows

* More fixes to make Native WebGPU through Dawn work
- removed API specific includes in general headers
- added the WebGPU graphics backend
- updated Dawn to the latest version and using eliemichel's solution resolved compilation issues
- fixed os/surface relying on Vulkan specific code
- added a temporary main_webgpu untill branch stable

* Adds a temp swapchain, renderpass, etc..

* Added agnostic abstractions for WebGPU
context and swapchain now have WebGPU implementations in the gfx namespace.

* moved logging initialization from main

* more feature implementation and fixes
Implemented the requirements to render to the screen in an agnostic manner
- cleaned up some of the includes
- fixed a potential incomplete type being deleted (gfx::render_graph)
- psl::library now supports being initialized without physical backing

* fixed extra loggers being defined for Android

Additionally some small maintenance changes were done such as:
- `psl::library` to handle obvious error cases of missing files.
- Logging initializer now no longer returns a bool as there's no false return to begin with.
- Fixed an issue on windows when running clang-format on WSL
- Reading a file in debug no longer causes an assert to be hit as an empty return is a valid return for the function.
  • Loading branch information
JessyDL authored May 28, 2024
1 parent c29151e commit e44b857
Show file tree
Hide file tree
Showing 57 changed files with 1,917 additions and 262 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@
"stop_token": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp"
}
},
"git.ignoreLimitWarning": true
}
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ OPTION(VK_STATIC "Should we statically bind Vulkan (if possible)" OFF)
OPTION(PE_GLES "controls OpenGL ES support" ON)
OPTION(PE_VULKAN "controls Vulkan support (experimental - not done)" ON)
OPTION(PE_MOLTEN "controls Metal support (not implemented)" OFF)
OPTION(PE_WEBGPU_NATIVE "controls native WebGPU support" OFF)
OPTION(PE_WEBGPU "controls WebGPU support, note this is also on when PE_WEBGPU_NATIVE is enabled." OFF)
OPTION(PE_MAKE_EXE "make example executable based on Paradigm" ON)
OPTION(PE_DEV_MAKE_PSL_EXE "For development you can make an executable " OFF)
OPTION(PE_TESTS "make the tests" ON)
Expand Down Expand Up @@ -162,6 +164,10 @@ endif()
string(TOUPPER ${PE_MODE} PE_MODE)
list(APPEND PE_DEFINES -DPE_${PE_MODE})

if(PE_WEBGPU_NATIVE)
list(APPEND PE_DEFINES -DPE_WEBGPU_NATIVE -DPE_WEBGPU)
endif()

if(PE_VULKAN)
message("enabling VULKAN")
list(APPEND PE_DEFINES -DPE_VULKAN)
Expand Down Expand Up @@ -204,6 +210,10 @@ if(PE_ECS_DISABLE_LOOKUP_CACHE_BEHAVIOUR)
list(APPEND PE_DEFINES -DPE_ECS_DISABLE_LOOKUP_CACHE)
endif()

if(PE_WEBGPU_NATIVE)
set(PE_WEBGPU ON)
endif()

# here we register the local hooks for git, this is done to ensure that the
# formatting is done before the commit is made
find_package(Git QUIET REQUIRED)
Expand Down
52 changes: 51 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"PE_GLES": "OFF",
"PE_VULKAN": "OFF",
"PE_MOLTEN": "OFF",
"VK_STATIC": "OFF"
"VK_STATIC": "OFF",
"PE_WEBGPU_NATIVE": "OFF"
}
},
{
Expand Down Expand Up @@ -43,6 +44,43 @@
"PE_MOLTEN": "OFF"
}
},
{
"hidden": true,
"name": "webgpu-native",
"displayName": "WebGPU Native",
"inherits": [
"settings-project"
],
"cacheVariables": {
"PE_WEBGPU_NATIVE": "ON"
}
},
{
"hidden": false,
"name": "windows-64-debug-webgpu-native",
"displayName": "WebGPU Native Windows (Debug)",
"inherits": [
"webgpu-native",
"windows-64-base"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"PE_MODE": "debug"
}
},
{
"hidden": false,
"name": "windows-64-release-webgpu-native",
"displayName": "WebGPU Native Windows (Release)",
"inherits": [
"webgpu-native",
"windows-64-base"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"PE_MODE": "release"
}
},
{
"hidden": true,
"name": "macos-base",
Expand Down Expand Up @@ -435,6 +473,18 @@
"configurePreset": "windows-64-release-all",
"configuration": "Release"
},
{
"name": "windows-64-debug-webgpu-native",
"displayName": "Windows WebGPU Native 64bit (Debug)",
"configurePreset": "windows-64-debug-webgpu-native",
"configuration": "Debug"
},
{
"name": "windows-64-release-webgpu-native",
"displayName": "Windows WebGPU Native 64bit (Release)",
"configurePreset": "windows-64-release-webgpu-native",
"configuration": "Release"
},
{
"name": "windows-64-debug-vulkan",
"displayName": "Windows Vulkan 64bit (Debug)",
Expand Down
11 changes: 9 additions & 2 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/inc" PREFIX "inc" FILES ${INC_GL
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "src" FILES ${SRC_GLES})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/inc" PREFIX "inc" FILES ${INC_VULKAN})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "src" FILES ${SRC_VULKAN})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/inc" PREFIX "inc" FILES ${INC_WEBGPU})
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "src" FILES ${SRC_WEBGPU})

if(PE_USE_NATVIS)
file(GLOB_RECURSE NATVIS nvs/*.natvis)
Expand Down Expand Up @@ -101,7 +103,7 @@ add_custom_command(TARGET core_generator
BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/gen/core/paradigm.hpp
COMMENT "Generating headers for Paradigm")

add_library(core STATIC ${FWD} ${SRC} ${INC} ${GEN} ${INC_GLES} ${SRC_GLES} ${INC_VULKAN} ${SRC_VULKAN} ${NATVIS})
add_library(core STATIC ${FWD} ${SRC} ${INC} ${GEN} ${INC_GLES} ${SRC_GLES} ${INC_VULKAN} ${SRC_VULKAN} ${INC_WEBGPU} ${SRC_WEBGPU} ${NATVIS})
add_library(paradigm::core ALIAS core)

add_dependencies(core core_generator)
Expand Down Expand Up @@ -132,7 +134,12 @@ target_include_directories(core
${WSI_INC}
)

target_link_libraries(core PRIVATE gli)
target_link_libraries(core
PRIVATE
gli
PUBLIC
$<$<BOOL:${PE_WEBGPU_NATIVE}>:webgpu_dawn webgpu_cpp>
)

if(WSI_LIB OR NOT WSI_LIB STREQUAL "")
target_link_libraries(core PUBLIC ${WSI_LIB})
Expand Down
11 changes: 11 additions & 0 deletions core/fwd/core/fwd/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace core::igles {
class context;
}
#endif
#ifdef PE_WEBGPU
namespace core::iwgpu {
class context;
}
#endif
namespace core::gfx {
class context;

Expand All @@ -28,4 +33,10 @@ struct backend_type<context, graphics_backend::gles> {
using type = core::igles::context;
};
#endif
#ifdef PE_WEBGPU
template <>
struct backend_type<context, graphics_backend::webgpu> {
using type = core::iwgpu::context;
};
#endif
} // namespace core::gfx
12 changes: 12 additions & 0 deletions core/fwd/core/fwd/gfx/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class swapchain;
}
#endif

#if defined(PE_WEBGPU)
namespace core::iwgpu {
class swapchain;
}
#endif

namespace core::gfx {
class swapchain;

Expand All @@ -30,4 +36,10 @@ struct backend_type<swapchain, graphics_backend::gles> {
using type = core::igles::swapchain;
};
#endif
#if defined(PE_WEBGPU)
template <>
struct backend_type<swapchain, graphics_backend::webgpu> {
using type = core::iwgpu::swapchain;
};
#endif
} // namespace core::gfx
18 changes: 18 additions & 0 deletions core/fwd/core/fwd/wgpu/shader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "core/defines.hpp"
#include "core/fwd/resource/resource.hpp"


namespace core::iwgpu {
class shader;
} // namespace core::iwgpu

namespace core::meta {
class shader;
}
namespace core::resource {
template <>
struct resource_traits<core::iwgpu::shader> {
using meta_type = core::meta::shader;
};
} // namespace core::resource
16 changes: 16 additions & 0 deletions core/fwd/core/fwd/wgpu/texture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "core/fwd/resource/resource.hpp"

namespace core::iwgpu {
class texture_t;
} // namespace core::iwgpu

namespace core::meta {
class texture_t;
}
namespace core::resource {
template <>
struct resource_traits<core::iwgpu::texture_t> {
using meta_type = core::meta::texture_t;
};
} // namespace core::resource
22 changes: 22 additions & 0 deletions core/inc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,27 @@ if(${PE_GLES})
list(TRANSFORM INC_GLES APPEND .hpp)
endif()

# include files for the WebGPU API
if(${PE_WEBGPU})
set(INC_WEBGPU
iwgpu
context
computepass
drawpass
pipeline
shader
swapchain
)

list(APPEND FWD

wgpu/shader
wgpu/texture
)

list(TRANSFORM INC_WEBGPU PREPEND inc/core/wgpu/)
list(TRANSFORM INC_WEBGPU APPEND .hpp)
endif()

list(TRANSFORM FWD PREPEND fwd/core/fwd/)
list(TRANSFORM FWD APPEND .hpp)
1 change: 0 additions & 1 deletion core/inc/core/ecs/systems/geometry_instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "core/ecs/components/transform.hpp"
#include "core/gfx/bundle.hpp"
#include "core/resource/resource.hpp"
#include "core/vk/geometry.hpp"
#include "psl/ecs/order_by.hpp"
#include "psl/ecs/state.hpp"

Expand Down
13 changes: 12 additions & 1 deletion core/inc/core/gfx/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class context {
#ifdef PE_GLES
explicit context(core::resource::handle<core::igles::context>& handle);
#endif
#ifdef PE_WEBGPU
explicit context(core::resource::handle<core::iwgpu::context>& handle);
#endif

context(core::resource::cache_t& cache,
const core::resource::metadata& metaData,
psl::meta::file* metaFile,
graphics_backend backend,
const psl::string8_t& name);
const psl::string8_t& name,
[[maybe_unused]] core::resource::handle<core::os::surface> surface);

~context() {};

Expand All @@ -47,6 +51,10 @@ class context {
#ifdef PE_GLES
if constexpr(backend == graphics_backend::gles)
return m_GLESHandle;
#endif
#ifdef PE_WEBGPU
if constexpr(backend == graphics_backend::webgpu)
return m_WebGPUHandle;
#endif
};
void wait_idle();
Expand All @@ -59,5 +67,8 @@ class context {
#ifdef PE_GLES
core::resource::handle<core::igles::context> m_GLESHandle;
#endif
#ifdef PE_WEBGPU
core::resource::handle<core::iwgpu::context> m_WebGPUHandle;
#endif
};
} // namespace core::gfx
29 changes: 26 additions & 3 deletions core/inc/core/gfx/drawpass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "core/gfx/types.hpp"
#include "core/resource/resource.hpp"
#include "psl/view_ptr.hpp"
#include <variant>
#ifdef PE_GLES
namespace core::igles {
class drawpass;
Expand All @@ -13,6 +12,11 @@ namespace core::ivk {
class drawpass;
}
#endif
#if defined(PE_WEBGPU)
namespace core::iwgpu {
class drawpass;
}
#endif

namespace core::gfx {
class context;
Expand All @@ -36,6 +40,12 @@ struct backend_type<drawpass, graphics_backend::gles> {
using type = core::igles::drawpass;
};
#endif
#if defined(PE_WEBGPU)
template <>
struct backend_type<drawpass, graphics_backend::webgpu> {
using type = core::iwgpu::drawpass;
};
#endif

class drawpass {
public:
Expand All @@ -45,9 +55,13 @@ class drawpass {
#ifdef PE_GLES
explicit drawpass(core::igles::drawpass* handle);
#endif
#if defined(PE_WEBGPU)
explicit drawpass(core::iwgpu::drawpass* handle);
#endif

drawpass(core::resource::handle<context> context, core::resource::handle<framebuffer_t> framebuffer);
drawpass(core::resource::handle<context> context, core::resource::handle<swapchain> swapchain);
drawpass([[maybe_unused]] core::resource::handle<context> context,
core::resource::handle<framebuffer_t> framebuffer);
drawpass([[maybe_unused]] core::resource::handle<context> context, core::resource::handle<swapchain> swapchain);
~drawpass();

drawpass(const drawpass& other) = delete;
Expand Down Expand Up @@ -89,6 +103,12 @@ class drawpass {
if constexpr(backend == graphics_backend::gles)
return m_GLESHandle;
#endif
#if defined(PE_WEBGPU)
if constexpr(backend == graphics_backend::webgpu)
return m_WGPUHandle;
#endif

return nullptr;
};

private:
Expand All @@ -98,6 +118,9 @@ class drawpass {
#endif
#ifdef PE_GLES
core::igles::drawpass* m_GLESHandle {nullptr};
#endif
#if defined(PE_WEBGPU)
core::iwgpu::drawpass* m_WGPUHandle {nullptr};
#endif
bool m_Dirty {true};
};
Expand Down
1 change: 1 addition & 0 deletions core/inc/core/gfx/pipeline_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class pipeline_cache {
if constexpr(backend == graphics_backend::gles)
return m_GLESHandle;
#endif
psl::fatal("unknown/unsupported backend");
};

private:
Expand Down
Loading

0 comments on commit e44b857

Please sign in to comment.