feat(webgpu): support WebGPU compatibility mode (#12949) 5d05097962 Use the WebGPU v2 API (FeatureLevel::Compatibility) to request compatibility mode when needed (Wagyu OpenGL ES), as well to accommodate the limits it imposes: - Hook up the exiting texture-based polyfill for when maxStorageBuffersInVertexStage is too low. - Rewrite @interpolate(flat) to @interpolate(flat, either), which compatibility mode requires. Co-authored-by: Chris Dalton <99840794+csmartdalton@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index 510832f..f630871 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -d7280b696290c3b9ae9f1ec58803a337fe6a6ee8 +5d050979620f5ae76f652c3afeab3381efe8df28
diff --git a/renderer/include/rive/renderer/webgpu/render_context_webgpu_impl.hpp b/renderer/include/rive/renderer/webgpu/render_context_webgpu_impl.hpp index 26a078a..b9cb68e 100644 --- a/renderer/include/rive/renderer/webgpu/render_context_webgpu_impl.hpp +++ b/renderer/include/rive/renderer/webgpu/render_context_webgpu_impl.hpp
@@ -11,10 +11,6 @@ #include <memory> #include <webgpu/webgpu_cpp.h> -#ifdef RIVE_WAGYU -#include "rive/renderer/gl/load_store_actions_ext.hpp" -#endif - namespace rive::gpu { class RenderTargetWebGPU; @@ -28,7 +24,12 @@ { public: struct ContextOptions - {}; + { + // True when the adapter/device were created in WebGPU compatibility + // mode. The embedder knows this (it requests the feature level at + // adapter creation); the renderer can't query it back. + bool compatibilityMode = false; + }; enum class PixelLocalStorageType { @@ -51,6 +52,13 @@ struct Capabilities { wgpu::BackendType backendType = wgpu::BackendType::Undefined; + + // Rive uses storage buffers in the vertex shader. We polyfill them via + // textures if the device doesn't support a sufficient number of + // vertex-stage storage buffers (specifically in WebGPU compatibility + // mode). + bool polyfillVertexStorageBuffers = false; + #ifdef RIVE_WAGYU // Driver extensions. bool VK_EXT_rasterization_order_attachment_access = false; @@ -58,10 +66,6 @@ bool GL_EXT_shader_pixel_local_storage2 = false; PixelLocalStorageType plsType = PixelLocalStorageType::none; - - // Rive requires 4 storage buffers in the vertex shader. We polyfill - // them if the hardware doesn't support this. - bool polyfillVertexStorageBuffers = false; #endif };
diff --git a/renderer/premake5_pls_renderer.lua b/renderer/premake5_pls_renderer.lua index 44345a3..a74e446 100644 --- a/renderer/premake5_pls_renderer.lua +++ b/renderer/premake5_pls_renderer.lua
@@ -190,9 +190,15 @@ error('\nWagyu does not support webgpu-version < 2\n ') end defines({ 'RIVE_WAGYU' }) - RIVE_WAGYU_PORT = '--use-port=' - .. RIVE_RUNTIME_DIR - .. '/renderer/src/webgpu/wagyu-port/webgpu-port.py:wagyu=true' + if _ACTION == 'export-compile-commands' then + -- Manually add the Wagyu include path when building a compilation + -- database (e.g., for auto-complete tools). + includedirs({ 'src/webgpu/wagyu-port/include' }) + else + RIVE_WAGYU_PORT = '--use-port=' + .. RIVE_RUNTIME_DIR + .. '/renderer/src/webgpu/wagyu-port/webgpu-port.py:wagyu=true' + end end newoption({
diff --git a/renderer/src/shaders/Makefile b/renderer/src/shaders/Makefile index 2f63318..aebd167 100644 --- a/renderer/src/shaders/Makefile +++ b/renderer/src/shaders/Makefile
@@ -203,6 +203,16 @@ spirv/draw_msaa_path.main \ spirv/draw_msaa_stencil.main \ +# WebGPU (compatibility mode) doesn't always support storage buffers in the +# vertex shader. These files specifically need WebGPU "nossbo" build variants +# that polyfill the buffers via textures. +WEBGPU_NOSSBO_NOCLIPDISTANCE_INPUTS := \ + spirv/draw_msaa_path.main \ + spirv/draw_msaa_atlas_blit.main +WEBGPU_NOSSBO_INPUTS := \ + $(WEBGPU_NOSSBO_NOCLIPDISTANCE_INPUTS) \ + spirv/tessellate.main \ + spirv/render_atlas.vert ## Helpers for use in SPIRV_LIST_RULE (using lower_snake_case to distinguish things that use inputs like $1, $2, etc) spirv_typed_filename = $(basename $1).$2 @@ -378,6 +388,14 @@ webgpu_noclipdistance_vert, \ $(SPIRV_COMMON_WEBGPU_PARAMS) -DDISABLE_CLIP_DISTANCE_FOR_UBERSHADERS)) $(eval $(call make_spirv_rules, \ + $(WEBGPU_NOSSBO_INPUTS), \ + webgpu_nossbo_vert, \ + $(SPIRV_COMMON_WEBGPU_PARAMS) -DDISABLE_SHADER_STORAGE_BUFFERS)) +$(eval $(call make_spirv_rules, \ + $(WEBGPU_NOSSBO_NOCLIPDISTANCE_INPUTS), \ + webgpu_nossbo_noclipdistance_vert, \ + $(SPIRV_COMMON_WEBGPU_PARAMS) -DDISABLE_SHADER_STORAGE_BUFFERS -DDISABLE_CLIP_DISTANCE_FOR_UBERSHADERS)) +$(eval $(call make_spirv_rules, \ $(SPIRV_WEBGPU_INPUTS) \ $(SPIRV_DRAW_ATOMIC_INPUTS) \ $(SPIRV_DRAW_MSAA_INPUTS), \ @@ -404,6 +422,8 @@ $(patsubst $(OUT)/spirv/%.spv,$(OUT)/wgsl/%.wgsl, \ $(filter %.webgpu_vert.spv \ %.webgpu_noclipdistance_vert.spv \ + %.webgpu_nossbo_vert.spv \ + %.webgpu_nossbo_noclipdistance_vert.spv \ %.webgpu_frag.spv \ %.webgpu_fixedcolor_frag.spv, \ $(SPIRV_OUTPUTS_BINARY))) \
diff --git a/renderer/src/shaders/wgsl_to_header.py b/renderer/src/shaders/wgsl_to_header.py index 00a1bb3..ad361c5 100644 --- a/renderer/src/shaders/wgsl_to_header.py +++ b/renderer/src/shaders/wgsl_to_header.py
@@ -56,6 +56,17 @@ with open(inPath, "r", encoding="utf-8") as f: wgsl = f.read() +# WebGPU compatibility mode rejects @interpolate(flat) and +# @interpolate(flat, first). +# Since all of Rive's flat varyings are constant across the primitive, it +# doesn't matter which vertex is provoking and we can dodge this validation +# error by post-converting all flat varyings to 'either'. +wgsl = re.sub( + r"@interpolate\(\s*flat\s*(?:,\s*first\s*)?\)", + "@interpolate(flat, either)", + wgsl, +) + # Minify the wgsl, unless otherwise specified. # NOTE: This could have ideally been done with wgsl-minifier, but it doesn't # support "enable clip_distances", so we roll out our own basic minifier.
diff --git a/renderer/src/webgpu/render_context_webgpu_impl.cpp b/renderer/src/webgpu/render_context_webgpu_impl.cpp index 44dbbc4..881c433 100644 --- a/renderer/src/webgpu/render_context_webgpu_impl.cpp +++ b/renderer/src/webgpu/render_context_webgpu_impl.cpp
@@ -23,8 +23,10 @@ #include "generated/shaders/wgsl/color_ramp.vert.hpp" #include "generated/shaders/wgsl/color_ramp.frag.hpp" #include "generated/shaders/wgsl/tessellate.webgpu_vert.hpp" +#include "generated/shaders/wgsl/tessellate.webgpu_nossbo_vert.hpp" #include "generated/shaders/wgsl/tessellate.webgpu_frag.hpp" #include "generated/shaders/wgsl/render_atlas.webgpu_vert.hpp" +#include "generated/shaders/wgsl/render_atlas.webgpu_nossbo_vert.hpp" #include "generated/shaders/wgsl/render_atlas_fill.webgpu_frag.hpp" #include "generated/shaders/wgsl/render_atlas_stroke.webgpu_frag.hpp" @@ -55,10 +57,14 @@ // InterlockMode::msaa shaders. #include "generated/shaders/wgsl/draw_msaa_path.webgpu_vert.hpp" #include "generated/shaders/wgsl/draw_msaa_path.webgpu_noclipdistance_vert.hpp" +#include "generated/shaders/wgsl/draw_msaa_path.webgpu_nossbo_vert.hpp" +#include "generated/shaders/wgsl/draw_msaa_path.webgpu_nossbo_noclipdistance_vert.hpp" #include "generated/shaders/wgsl/draw_msaa_path.webgpu_frag.hpp" #include "generated/shaders/wgsl/draw_msaa_path.webgpu_fixedcolor_frag.hpp" #include "generated/shaders/wgsl/draw_msaa_atlas_blit.webgpu_vert.hpp" #include "generated/shaders/wgsl/draw_msaa_atlas_blit.webgpu_noclipdistance_vert.hpp" +#include "generated/shaders/wgsl/draw_msaa_atlas_blit.webgpu_nossbo_vert.hpp" +#include "generated/shaders/wgsl/draw_msaa_atlas_blit.webgpu_nossbo_noclipdistance_vert.hpp" #include "generated/shaders/wgsl/draw_msaa_atlas_blit.webgpu_frag.hpp" #include "generated/shaders/wgsl/draw_msaa_atlas_blit.webgpu_fixedcolor_frag.hpp" #include "generated/shaders/wgsl/draw_msaa_image_mesh.webgpu_vert.hpp" @@ -74,8 +80,6 @@ #ifdef RIVE_WEBGPU #include <webgpu/webgpu_cpp.h> -#include <emscripten.h> -#include <emscripten/html5.h> #if RIVE_WEBGPU == 1 #include "webgpu_compat.h" #endif @@ -249,6 +253,8 @@ } #ifdef RIVE_WAGYU +#include "rive/renderer/gl/load_store_actions_ext.hpp" + #include <webgpu/webgpu_wagyu.h> #include "generated/shaders/glsl.glsl.hpp" @@ -273,12 +279,6 @@ // base instance. constexpr static char BASE_INSTANCE_UNIFORM_NAME[] = "nrdp_BaseInstance"; -EM_JS(int, gl_max_vertex_shader_storage_blocks, (), { - const version = globalThis.nrdp ?.version || navigator.getNrdpVersion(); - return version.libraries.opengl.options.limits - .GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS; -}); - static wgpu::ShaderModule compile_shader_module_wagyu( wgpu::Device device, const char* source, @@ -478,7 +478,6 @@ .type = wgpu::BufferBindingType::Uniform, }, }, -#ifdef RIVE_WAGYU impl->m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupLayoutEntry{ .binding = PATH_BUFFER_IDX, @@ -489,7 +488,6 @@ .viewDimension = wgpu::TextureViewDimension::e2D, }, } : -#endif wgpu::BindGroupLayoutEntry{ .binding = PATH_BUFFER_IDX, .visibility = pathStorageVisibility, @@ -498,7 +496,6 @@ .type = wgpu::BufferBindingType::ReadOnlyStorage, }, }, -#ifdef RIVE_WAGYU impl->m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupLayoutEntry{ .binding = PAINT_BUFFER_IDX, @@ -509,7 +506,6 @@ .viewDimension = wgpu::TextureViewDimension::e2D, }, } : -#endif wgpu::BindGroupLayoutEntry{ .binding = PAINT_BUFFER_IDX, .visibility = paintStorageVisibility, @@ -518,7 +514,6 @@ .type = wgpu::BufferBindingType::ReadOnlyStorage, }, }, -#ifdef RIVE_WAGYU impl->m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupLayoutEntry{ .binding = PAINT_AUX_BUFFER_IDX, @@ -530,7 +525,6 @@ .viewDimension = wgpu::TextureViewDimension::e2D, }, } : -#endif wgpu::BindGroupLayoutEntry{ .binding = PAINT_AUX_BUFFER_IDX, .visibility = paintStorageVisibility, @@ -539,7 +533,6 @@ .type = wgpu::BufferBindingType::ReadOnlyStorage, }, }, -#ifdef RIVE_WAGYU impl->m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupLayoutEntry{ .binding = CONTOUR_BUFFER_IDX, @@ -550,7 +543,6 @@ .viewDimension = wgpu::TextureViewDimension::e2D, }, } : -#endif wgpu::BindGroupLayoutEntry{ .binding = CONTOUR_BUFFER_IDX, .visibility = wgpu::ShaderStage::Vertex, @@ -806,7 +798,7 @@ if (impl->m_capabilities.backendType == wgpu::BackendType::OpenGLES) { // Rive shaders tend to be long and prone to vendor bugs in the - // compiler. Instead of SPIRV, send down the raw Rive GLSL sources, + // compiler. Instead of wgsl, send down the raw Rive GLSL sources, // which have various workarounds for known issues and are tested // regularly. std::ostringstream glsl; @@ -943,7 +935,7 @@ if (impl->m_capabilities.backendType == wgpu::BackendType::OpenGLES) { // Rive shaders tend to be long and prone to vendor bugs in the - // compiler. Instead of SPIRV, send down the raw Rive GLSL sources, + // compiler. Instead of wgsl, send down the raw Rive GLSL sources, // which have various workarounds for known issues and are tested // regularly. std::ostringstream glsl; @@ -981,9 +973,11 @@ else #endif { - vertexShaderModule = - compile_shader_module_wgsl(device, - wgsl::tessellate_webgpu_vert); + vertexShaderModule = compile_shader_module_wgsl( + device, + impl->m_capabilities.polyfillVertexStorageBuffers + ? wgsl::tessellate_webgpu_nossbo_vert + : wgsl::tessellate_webgpu_vert); fragmentShaderModule = compile_shader_module_wgsl(device, wgsl::tessellate_webgpu_frag); @@ -1104,7 +1098,7 @@ if (impl->m_capabilities.backendType == wgpu::BackendType::OpenGLES) { // Rive shaders tend to be long and prone to vendor bugs in the - // compiler. Instead of SPIRV, send down the raw Rive GLSL sources, + // compiler. Instead of wgsl, send down the raw Rive GLSL sources, // which have various workarounds for known issues and are tested // regularly. std::ostringstream glsl; @@ -1161,9 +1155,11 @@ else #endif { - vertexShaderModule = - compile_shader_module_wgsl(device, - wgsl::render_atlas_webgpu_vert); + vertexShaderModule = compile_shader_module_wgsl( + device, + impl->m_capabilities.polyfillVertexStorageBuffers + ? wgsl::render_atlas_webgpu_nossbo_vert + : wgsl::render_atlas_webgpu_vert); fillFragmentShaderModule = compile_shader_module_wgsl(device, wgsl::render_atlas_fill_webgpu_frag); @@ -1596,12 +1592,27 @@ case DrawType::msaaMidpointFanStencilReset: case DrawType::msaaMidpointFanPathsStencil: case DrawType::msaaMidpointFanPathsCover: - vertexShader = - enums::is_flag_set(shaderFeatures, - ShaderFeatures::ENABLE_CLIP_RECT) - ? &wgsl::draw_msaa_path_webgpu_vert - : &wgsl:: - draw_msaa_path_webgpu_noclipdistance_vert; + if (context->m_capabilities + .polyfillVertexStorageBuffers) + { + vertexShader = + enums::is_flag_set( + shaderFeatures, + ShaderFeatures::ENABLE_CLIP_RECT) + ? &wgsl::draw_msaa_path_webgpu_nossbo_vert + : &wgsl:: + draw_msaa_path_webgpu_nossbo_noclipdistance_vert; + } + else + { + vertexShader = + enums::is_flag_set( + shaderFeatures, + ShaderFeatures::ENABLE_CLIP_RECT) + ? &wgsl::draw_msaa_path_webgpu_vert + : &wgsl:: + draw_msaa_path_webgpu_noclipdistance_vert; + } fragmentShader = fixedFunctionColorOutput ? &wgsl::draw_msaa_path_webgpu_fixedcolor_frag @@ -1620,12 +1631,28 @@ break; case DrawType::atlasBlit: - vertexShader = - enums::is_flag_set(shaderFeatures, - ShaderFeatures::ENABLE_CLIP_RECT) - ? &wgsl::draw_msaa_atlas_blit_webgpu_vert - : &wgsl:: - draw_msaa_atlas_blit_webgpu_noclipdistance_vert; + if (context->m_capabilities + .polyfillVertexStorageBuffers) + { + vertexShader = + enums::is_flag_set( + shaderFeatures, + ShaderFeatures::ENABLE_CLIP_RECT) + ? &wgsl:: + draw_msaa_atlas_blit_webgpu_nossbo_vert + : &wgsl:: + draw_msaa_atlas_blit_webgpu_nossbo_noclipdistance_vert; + } + else + { + vertexShader = + enums::is_flag_set( + shaderFeatures, + ShaderFeatures::ENABLE_CLIP_RECT) + ? &wgsl::draw_msaa_atlas_blit_webgpu_vert + : &wgsl:: + draw_msaa_atlas_blit_webgpu_noclipdistance_vert; + } fragmentShader = fixedFunctionColorOutput ? &wgsl:: @@ -1767,6 +1794,15 @@ m_device(device), m_queue(queue), m_contextOptions(contextOptions) { wgpu::Limits deviceLimits = {}; + // CompatibilityModeLimits (and compatibility mode itself) don't exist on + // legacy WebGPU v1. +#if RIVE_WEBGPU > 1 + wgpu::CompatibilityModeLimits compatModeLimits = {}; + if (contextOptions.compatibilityMode) + { + deviceLimits.nextInChain = &compatModeLimits; + } +#endif const bool deviceLimitsValid = m_device.GetLimits(&deviceLimits); #ifdef RIVE_WAGYU @@ -1791,16 +1827,44 @@ m_platformFeatures.supportsClockwiseFixedFunctionMode = m_capabilities.GL_EXT_shader_pixel_local_storage2; } +#endif - // Compatibility workarounds. - if (m_capabilities.backendType == wgpu::BackendType::OpenGLES && - gl_max_vertex_shader_storage_blocks() < 4) + uint32_t maxStorageBuffersInVertexStage = wgpu::kLimitU32Undefined; + if (deviceLimitsValid) { - // Rive requires 4 storage buffers in the vertex shader. Polyfill them - // if the hardware doesn't support this. + maxStorageBuffersInVertexStage = +#if RIVE_WEBGPU > 1 + m_contextOptions.compatibilityMode + // In compatibility mode, the vertex stage has its own + // storage-buffer limit. + ? compatModeLimits.maxStorageBuffersInVertexStage + : +#endif + // Outside compatibility mode, there's no such split: + // maxStorageBuffersPerShaderStage applies to every stage, + // vertex included + deviceLimits.maxStorageBuffersPerShaderStage; + } + if (maxStorageBuffersInVertexStage == wgpu::kLimitU32Undefined) + { + maxStorageBuffersInVertexStage = +#if RIVE_WEBGPU > 1 + m_contextOptions.compatibilityMode + ? 0 // Compat mode doesn't guarantee any vertex storage buffers. + : +#endif + 8; // Core WebGPU guarantees at least 8 buffers per stage. + } + if (maxStorageBuffersInVertexStage < gpu::kMaxStorageBuffers) + { + // Rive uses storage buffers in the vertex shader. Polyfill them via + // textures if the device doesn't support a sufficient number of + // vertex-stage storage buffers. +#if RIVE_WEBGPU > 1 + assert(m_contextOptions.compatibilityMode); +#endif m_capabilities.polyfillVertexStorageBuffers = true; } -#endif // InterlockMode::atomics binds the color, clip, and coverage PLS planes as // storage buffers in the fragment stage. Only advertise support for atomic @@ -2770,7 +2834,6 @@ size_t capacityInBytes, gpu::StorageBufferStructure bufferStructure) { -#ifdef RIVE_WAGYU if (m_capabilities.polyfillVertexStorageBuffers) { return std::make_unique<StorageTextureBufferWebGPU>(m_device, @@ -2779,7 +2842,6 @@ bufferStructure); } else -#endif { return std::make_unique<BufferWebGPU>(m_device, m_queue, @@ -3254,7 +3316,7 @@ WGPUWagyuFragmentState wagyuFragmentState = WGPU_WAGYU_FRAGMENT_STATE_INIT; if (usingPLSInputAttachments) { - for (size_t i = 0; i < PLS_PLANE_COUNT; ++i) + for (uint32_t i = 0; i < PLS_PLANE_COUNT; ++i) { inputAttachments[i] = WGPU_WAGYU_INPUT_ATTACHMENT_STATE_INIT; inputAttachments[i].format = colorAttachments[i].format; @@ -3894,7 +3956,6 @@ commandEncoder = wgpu::CommandEncoder::Acquire(wgpuEncoder); } -#ifdef RIVE_WAGYU // If storage buffers are disabled, copy their contents to textures. if (m_capabilities.polyfillVertexStorageBuffers) { @@ -3922,7 +3983,6 @@ commandEncoder); } } -#endif wgpu::BindGroupEntry perFlushBindingEntries[DRAW_BINDINGS_COUNT] = { { @@ -3930,49 +3990,41 @@ .buffer = webgpu_buffer(flushUniformBufferRing()), .offset = desc.flushUniformDataOffsetInBytes, }, -#ifdef RIVE_WAGYU m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupEntry{.binding = PATH_BUFFER_IDX, .textureView = webgpu_storage_texture_view( pathBufferRing())} : -#endif wgpu::BindGroupEntry{ .binding = PATH_BUFFER_IDX, .buffer = webgpu_buffer(pathBufferRing()), .offset = desc.firstPath * sizeof(gpu::PathData), }, -#ifdef RIVE_WAGYU m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupEntry{ .binding = PAINT_BUFFER_IDX, .textureView = webgpu_storage_texture_view(paintBufferRing()), } : -#endif wgpu::BindGroupEntry{ .binding = PAINT_BUFFER_IDX, .buffer = webgpu_buffer(paintBufferRing()), .offset = desc.firstPaint * sizeof(gpu::PaintData), }, -#ifdef RIVE_WAGYU m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupEntry{ .binding = PAINT_AUX_BUFFER_IDX, .textureView = webgpu_storage_texture_view(paintAuxBufferRing()), } : -#endif wgpu::BindGroupEntry{ .binding = PAINT_AUX_BUFFER_IDX, .buffer = webgpu_buffer(paintAuxBufferRing()), .offset = desc.firstPaintAux * sizeof(gpu::PaintAuxData), }, -#ifdef RIVE_WAGYU m_capabilities.polyfillVertexStorageBuffers ? wgpu::BindGroupEntry{ .binding = CONTOUR_BUFFER_IDX, .textureView = webgpu_storage_texture_view(contourBufferRing()), } : -#endif wgpu::BindGroupEntry{ .binding = CONTOUR_BUFFER_IDX, .buffer = webgpu_buffer(contourBufferRing()),
diff --git a/tests/common/testing_window_wgpu.cpp b/tests/common/testing_window_wgpu.cpp index 6fabc52..de43c9f 100644 --- a/tests/common/testing_window_wgpu.cpp +++ b/tests/common/testing_window_wgpu.cpp
@@ -15,6 +15,8 @@ #include "rive/renderer/rive_render_image.hpp" #include "rive/renderer/webgpu/render_context_webgpu_impl.hpp" +#include <algorithm> + #ifdef RIVE_WAGYU #include <webgpu/webgpu_wagyu.h> #endif @@ -88,6 +90,18 @@ : wgpu::PowerPreference::HighPerformance, }; +#ifdef RIVE_WAGYU + // Wagyu's OpenGL ES backend requires WebGPU compatibility mode. + const bool compatibilityMode = + wgpuWagyuInstanceGetBackend(m_instance.Get()) == + WGPUBackendType_OpenGLES; + if (compatibilityMode) + { + requestAdapterOptions.featureLevel = + wgpu::FeatureLevel::Compatibility; + } +#endif + m_instance.RequestAdapter( &requestAdapterOptions, wgpu::CallbackMode::AllowSpontaneous, @@ -104,8 +118,36 @@ emscripten_sleep(1); } + wgpu::DeviceDescriptor deviceDesc = {}; +#ifdef RIVE_WAGYU + wgpu::Limits requiredLimits = {}; + wgpu::CompatibilityModeLimits compatLimits = {}; + if (compatibilityMode) + { + // Rive uses storage buffers in the vertex shader. In compatibility + // mode this isn't allowed by default, so explicitly request it if + // supported. Otherwise, the renderer can fall back to polyfilling + // vertex storage buffers via textures. + wgpu::Limits adapterLimits = {}; + wgpu::CompatibilityModeLimits adapterCompatLimits = {}; + adapterLimits.nextInChain = &adapterCompatLimits; + m_adapter.GetLimits(&adapterLimits); + + if (adapterCompatLimits.maxStorageBuffersInVertexStage != + wgpu::kLimitU32Undefined) + { + compatLimits.maxStorageBuffersInVertexStage = + std::min<uint32_t>( + gpu::kMaxStorageBuffers, + adapterCompatLimits.maxStorageBuffersInVertexStage); + requiredLimits.nextInChain = &compatLimits; + deviceDesc.requiredLimits = &requiredLimits; + } + } +#endif + m_adapter.RequestDevice( - {}, + &deviceDesc, wgpu::CallbackMode::AllowSpontaneous, [](wgpu::RequestDeviceStatus status, wgpu::Device device, @@ -174,6 +216,9 @@ } RenderContextWebGPUImpl::ContextOptions contextOptions; +#ifdef RIVE_WAGYU + contextOptions.compatibilityMode = compatibilityMode; +#endif m_renderContext = RenderContextWebGPUImpl::MakeContext(m_adapter, m_device, m_queue,