diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-08 08:33:55 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-08 08:33:55 +0100 |
| commit | 16c2cdce6ad1d89d3c537f2c2cff743449925125 (patch) | |
| tree | 44792b3f8909dba5622143179c95787fd4de5149 /src/gpu | |
| parent | d19aec6690bdf515435f4052275828b061c3f71f (diff) | |
feat(platform): Centralize platform-specific WebGPU code and improve shader composition
Diffstat (limited to 'src/gpu')
| -rw-r--r-- | src/gpu/effect.cc | 6 | ||||
| -rw-r--r-- | src/gpu/effects/shader_composer.cc | 10 | ||||
| -rw-r--r-- | src/gpu/effects/shader_composer.h | 5 | ||||
| -rw-r--r-- | src/gpu/gpu.cc | 111 | ||||
| -rw-r--r-- | src/gpu/texture_manager.cc | 17 |
5 files changed, 24 insertions, 125 deletions
diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc index a1b45f1..9e769eb 100644 --- a/src/gpu/effect.cc +++ b/src/gpu/effect.cc @@ -260,9 +260,7 @@ void MainSequence::render_frame(float global_time, float beat, float peak, scene_attachment.loadOp = WGPULoadOp_Clear; scene_attachment.storeOp = WGPUStoreOp_Store; scene_attachment.clearValue = {0, 0, 0, 1}; -#if !defined(DEMO_CROSS_COMPILE_WIN32) scene_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ WGPURenderPassDepthStencilAttachment depth_attachment = {}; depth_attachment.view = depth_view_; @@ -302,9 +300,7 @@ void MainSequence::render_frame(float global_time, float beat, float peak, final_attachment.resolveTarget = nullptr; final_attachment.loadOp = WGPULoadOp_Load; final_attachment.storeOp = WGPUStoreOp_Store; -#if !defined(DEMO_CROSS_COMPILE_WIN32) final_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ WGPURenderPassDescriptor final_desc = { .colorAttachmentCount = 1, .colorAttachments = &final_attachment}; WGPURenderPassEncoder final_pass = @@ -338,9 +334,7 @@ void MainSequence::render_frame(float global_time, float beat, float peak, pp_attachment.resolveTarget = nullptr; pp_attachment.loadOp = WGPULoadOp_Load; pp_attachment.storeOp = WGPUStoreOp_Store; -#if !defined(DEMO_CROSS_COMPILE_WIN32) pp_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; -#endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ WGPURenderPassDescriptor pp_desc = {.colorAttachmentCount = 1, .colorAttachments = &pp_attachment}; WGPURenderPassEncoder pp_pass = diff --git a/src/gpu/effects/shader_composer.cc b/src/gpu/effects/shader_composer.cc index b746f8b..5cd8699 100644 --- a/src/gpu/effects/shader_composer.cc +++ b/src/gpu/effects/shader_composer.cc @@ -2,6 +2,7 @@ // It implements the ShaderComposer class. #include "gpu/effects/shader_composer.h" +#include "util/asset_manager.h" #include <set> #include <sstream> @@ -86,3 +87,12 @@ ShaderComposer::Compose(const std::vector<std::string>& dependencies, return ss.str(); } + +std::string +ShaderComposer::Compose(const std::vector<std::string>& dependencies, + AssetId main_code_asset_id, + const CompositionMap& substitutions) { + size_t size; + const char* main_code = reinterpret_cast<const char*>(GetAsset(main_code_asset_id, &size)); + return Compose(dependencies, std::string(main_code, size), substitutions); +} diff --git a/src/gpu/effects/shader_composer.h b/src/gpu/effects/shader_composer.h index 9eb43f4..961ffcb 100644 --- a/src/gpu/effects/shader_composer.h +++ b/src/gpu/effects/shader_composer.h @@ -3,6 +3,7 @@ #pragma once +#include "generated/assets.h" // For AssetId #include <map> #include <set> #include <string> @@ -23,6 +24,10 @@ class ShaderComposer { std::string Compose(const std::vector<std::string>& dependencies, const std::string& main_code, const CompositionMap& substitutions = {}); + + std::string Compose(const std::vector<std::string>& dependencies, + AssetId main_code_asset_id, + const CompositionMap& substitutions = {}); private: ShaderComposer() = default; diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index 9776eac..c61c605 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -57,11 +57,7 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, RenderPass pass = {}; // Create Shader Module - WGPUShaderSourceWGSL wgsl_src = {}; - wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(shader_code); - WGPUShaderModuleDescriptor shader_desc = {}; - shader_desc.nextInChain = &wgsl_src.chain; + WGPUShaderModuleDescriptor shader_desc = platform_create_shader_module_descriptor(shader_code); WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(device, &shader_desc); @@ -145,7 +141,7 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, depth_stencil.format = WGPUTextureFormat_Depth24Plus; depth_stencil.depthWriteEnabled = WGPUOptionalBool_False; depth_stencil.depthCompare = WGPUCompareFunction_Always; - pipeline_desc.depthStencil = &depth_stencil; + PLATFORM_SET_PIPELINE_DEPTH_STENCIL(pipeline_desc, &depth_stencil); pass.pipeline = wgpuDeviceCreateRenderPipeline(device, &pipeline_desc); @@ -157,11 +153,7 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, int num_bindings) { ComputePass pass = {}; - WGPUShaderSourceWGSL wgsl_src = {}; - wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(shader_code); - WGPUShaderModuleDescriptor shader_desc = {}; - shader_desc.nextInChain = &wgsl_src.chain; + WGPUShaderModuleDescriptor shader_desc = platform_create_shader_module_descriptor(shader_code); WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(device, &shader_desc); @@ -214,49 +206,11 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, #if !defined(STRIP_ALL) #if defined(DEMO_CROSS_COMPILE_WIN32) -static void handle_request_adapter(WGPURequestAdapterStatus status, - WGPUAdapter adapter, const char* message, - void* userdata) { - if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter*)userdata) = adapter; - } else { - printf("Request adapter failed: %s\n", message ? message : "Unknown"); - } -} -static void handle_request_device(WGPURequestDeviceStatus status, - WGPUDevice device, const char* message, - void* userdata) { - if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice*)userdata) = device; - } else { - printf("Request device failed: %s\n", message ? message : "Unknown"); - } -} static void handle_device_error(WGPUErrorType type, const char* message, void* userdata) { printf("WebGPU Error: %s\n", message ? message : "Unknown"); } #else -static void handle_request_adapter(WGPURequestAdapterStatus status, - WGPUAdapter adapter, WGPUStringView message, - void* userdata, void* userdata2) { - (void)userdata2; - if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter*)userdata) = adapter; - } else { - printf("Request adapter failed: %.*s\n", (int)message.length, message.data); - } -} -static void handle_request_device(WGPURequestDeviceStatus status, - WGPUDevice device, WGPUStringView message, - void* userdata, void* userdata2) { - (void)userdata2; - if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice*)userdata) = device; - } else { - printf("Request device failed: %.*s\n", (int)message.length, message.data); - } -} static void handle_device_error(const WGPUDevice* device, WGPUErrorType type, WGPUStringView message, void* userdata, void* userdata2) { @@ -266,41 +220,6 @@ static void handle_device_error(const WGPUDevice* device, WGPUErrorType type, printf("WebGPU Error: %.*s\n", (int)message.length, message.data); } #endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ -#else -// STRIP_ALL versions -#if defined(DEMO_CROSS_COMPILE_WIN32) -static void handle_request_adapter(WGPURequestAdapterStatus status, - WGPUAdapter adapter, const char* message, - void* userdata) { - if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter*)userdata) = adapter; - } -} -static void handle_request_device(WGPURequestDeviceStatus status, - WGPUDevice device, const char* message, - void* userdata) { - if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice*)userdata) = device; - } -} -#else -static void handle_request_adapter(WGPURequestAdapterStatus status, - WGPUAdapter adapter, WGPUStringView message, - void* userdata, void* userdata2) { - (void)userdata2; - if (status == WGPURequestAdapterStatus_Success) { - *((WGPUAdapter*)userdata) = adapter; - } -} -static void handle_request_device(WGPURequestDeviceStatus status, - WGPUDevice device, WGPUStringView message, - void* userdata, void* userdata2) { - (void)userdata2; - if (status == WGPURequestDeviceStatus_Success) { - *((WGPUDevice*)userdata) = device; - } -} -#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ #endif /* !defined(STRIP_ALL) */ void gpu_init(PlatformState* platform_state) { @@ -311,16 +230,8 @@ void gpu_init(PlatformState* platform_state) { adapter_opts.compatibleSurface = g_surface; adapter_opts.powerPreference = WGPUPowerPreference_HighPerformance; -#if defined(DEMO_CROSS_COMPILE_WIN32) - wgpuInstanceRequestAdapter(g_instance, &adapter_opts, handle_request_adapter, - &g_adapter); -#else - WGPURequestAdapterCallbackInfo adapter_cb = {}; - adapter_cb.mode = WGPUCallbackMode_WaitAnyOnly; - adapter_cb.callback = handle_request_adapter; - adapter_cb.userdata1 = &g_adapter; - wgpuInstanceRequestAdapter(g_instance, &adapter_opts, adapter_cb); -#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ + platform_wgpu_request_adapter(g_instance, &adapter_opts, &g_adapter); + while (!g_adapter) platform_wgpu_wait_any(g_instance); @@ -331,16 +242,8 @@ void gpu_init(PlatformState* platform_state) { #endif /* !defined(DEMO_CROSS_COMPILE_WIN32) */ #endif /* !defined(STRIP_ALL) */ -#if defined(DEMO_CROSS_COMPILE_WIN32) - wgpuAdapterRequestDevice(g_adapter, &device_desc, handle_request_device, - &g_device); -#else - WGPURequestDeviceCallbackInfo device_cb = {}; - device_cb.mode = WGPUCallbackMode_WaitAnyOnly; - device_cb.callback = handle_request_device; - device_cb.userdata1 = &g_device; - wgpuAdapterRequestDevice(g_adapter, &device_desc, device_cb); -#endif /* defined(DEMO_CROSS_COMPILE_WIN32) */ + platform_wgpu_request_device(g_adapter, &device_desc, &g_device); + while (!g_device) platform_wgpu_wait_any(g_instance); diff --git a/src/gpu/texture_manager.cc b/src/gpu/texture_manager.cc index 0c30c94..e69425f 100644 --- a/src/gpu/texture_manager.cc +++ b/src/gpu/texture_manager.cc @@ -2,19 +2,10 @@ // It implements the TextureManager. #include "gpu/texture_manager.h" +#include "platform/platform.h" // Include for WGPU_TEX_COPY_INFO and WGPU_TEX_DATA_LAYOUT #include <cstdio> #include <vector> -#if defined(DEMO_CROSS_COMPILE_WIN32) -// Old API -#define WGPU_TEX_COPY_INFO WGPUImageCopyTexture -#define WGPU_TEX_DATA_LAYOUT WGPUTextureDataLayout -#else -// New API -#define WGPU_TEX_COPY_INFO WGPUTexelCopyTextureInfo -#define WGPU_TEX_DATA_LAYOUT WGPUTexelCopyBufferLayout -#endif - void TextureManager::init(WGPUDevice device, WGPUQueue queue) { device_ = device; queue_ = queue; @@ -60,11 +51,7 @@ void TextureManager::create_texture(const std::string& name, int width, tex_desc.format = WGPUTextureFormat_RGBA8Unorm; tex_desc.mipLevelCount = 1; tex_desc.sampleCount = 1; -#if defined(DEMO_CROSS_COMPILE_WIN32) - tex_desc.label = nullptr; -#else - tex_desc.label = {nullptr, 0}; -#endif + tex_desc.label = label_view(name.c_str()); WGPUTexture texture = wgpuDeviceCreateTexture(device_, &tex_desc); |
