| // Copyright 2025 The Dawn & Tint Authors | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // 1. Redistributions of source code must retain the above copyright notice, this | |
| // list of conditions and the following disclaimer. | |
| // | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // | |
| // 3. Neither the name of the copyright holder nor the names of its | |
| // contributors may be used to endorse or promote products derived from | |
| // this software without specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| #include <array> | |
| #include <limits> | |
| #include <string> | |
| #include <vector> | |
| #include "absl/strings/str_format.h" | |
| #include "src/dawn/common/Math.h" | |
| #include "src/dawn/tests/DawnTest.h" | |
| #include "src/dawn/utils/ComboRenderPipelineDescriptor.h" | |
| #include "src/dawn/utils/WGPUHelpers.h" | |
| #include "src/utils/compiler.h" | |
| namespace dawn { | |
| namespace { | |
| constexpr uint32_t kRTSize = 1; | |
| class ImmediateDataTests : public DawnTest { | |
| protected: | |
| void GetRequiredLimits(const dawn::utils::ComboLimits& supported, | |
| dawn::utils::ComboLimits& required) override { | |
| // In the "MaxedOut" tests we'll use as many immediates, uniform/storage bindings, and | |
| // dynamic buffer offsets as possible. | |
| required.maxImmediateSize = supported.maxImmediateSize; | |
| required.maxUniformBuffersPerShaderStage = supported.maxUniformBuffersPerShaderStage; | |
| required.maxStorageBuffersPerShaderStage = supported.maxStorageBuffersPerShaderStage; | |
| required.maxDynamicUniformBuffersPerPipelineLayout = | |
| supported.maxDynamicUniformBuffersPerPipelineLayout; | |
| required.maxDynamicStorageBuffersPerPipelineLayout = | |
| supported.maxDynamicStorageBuffersPerPipelineLayout; | |
| required.maxStorageBuffersInFragmentStage = supported.maxStorageBuffersInFragmentStage; | |
| } | |
| void SetUp() override { | |
| DawnTest::SetUp(); | |
| mShaderModule = utils::CreateShaderModule(device, R"( | |
| struct Immediate { | |
| color: vec3<f32>, | |
| colorDiff: f32, | |
| }; | |
| var<immediate> constants: Immediate; | |
| struct VertexOut { | |
| @location(0) color : vec3f, | |
| @builtin(position) position : vec4f, | |
| } | |
| @vertex fn vsMain(@builtin(vertex_index) VertexIndex : u32) -> VertexOut { | |
| const pos = array( | |
| vec2( 1.0, -1.0), | |
| vec2(-1.0, -1.0), | |
| vec2( 0.0, 1.0), | |
| ); | |
| var output: VertexOut; | |
| output.position = vec4f(pos[VertexIndex], 0.0, 1.0); | |
| output.color = constants.color; | |
| return output; | |
| } | |
| // to reuse the same pipeline layout | |
| @fragment fn fsMain(@location(0) color:vec3f) -> @location(0) vec4f { | |
| return vec4f(color + vec3f(constants.colorDiff), 1.0); | |
| } | |
| var<immediate> computeConstants: vec4u; | |
| @group(0) @binding(0) var<storage, read_write> output : vec4u; | |
| @compute @workgroup_size(1, 1, 1) | |
| fn csMain() { | |
| output = computeConstants; | |
| })"); | |
| wgpu::BufferDescriptor bufferDesc; | |
| bufferDesc.size = sizeof(uint32_t) * 4; | |
| bufferDesc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Storage; | |
| mStorageBuffer = device.CreateBuffer(&bufferDesc); | |
| } | |
| wgpu::BindGroupLayout CreateBindGroupLayout() { | |
| wgpu::BindGroupLayoutEntry entries[1]; | |
| entries[0].binding = 0; | |
| entries[0].visibility = wgpu::ShaderStage::Compute; | |
| entries[0].buffer.type = wgpu::BufferBindingType::Storage; | |
| wgpu::BindGroupLayoutDescriptor bindGroupLayoutDesc; | |
| bindGroupLayoutDesc.entryCount = 1; | |
| bindGroupLayoutDesc.entries = entries; | |
| return device.CreateBindGroupLayout(&bindGroupLayoutDesc); | |
| } | |
| wgpu::PipelineLayout CreatePipelineLayout() { | |
| wgpu::BindGroupLayout bindGroupLayout = CreateBindGroupLayout(); | |
| wgpu::PipelineLayoutDescriptor pipelineLayoutDesc; | |
| pipelineLayoutDesc.bindGroupLayoutCount = 1; | |
| pipelineLayoutDesc.bindGroupLayouts = &bindGroupLayout; | |
| pipelineLayoutDesc.immediateSize = 16; | |
| return device.CreatePipelineLayout(&pipelineLayoutDesc); | |
| } | |
| wgpu::RenderPipeline CreateRenderPipeline() { | |
| utils::ComboRenderPipelineDescriptor pipelineDescriptor; | |
| pipelineDescriptor.vertex.module = mShaderModule; | |
| pipelineDescriptor.cFragment.module = mShaderModule; | |
| pipelineDescriptor.cFragment.targetCount = 1; | |
| pipelineDescriptor.layout = CreatePipelineLayout(); | |
| return device.CreateRenderPipeline(&pipelineDescriptor); | |
| } | |
| wgpu::ComputePipeline CreateComputePipeline() { | |
| wgpu::ComputePipelineDescriptor csDesc; | |
| csDesc.compute.module = mShaderModule; | |
| csDesc.layout = CreatePipelineLayout(); | |
| return device.CreateComputePipeline(&csDesc); | |
| } | |
| wgpu::BindGroup CreateBindGroup() { | |
| return utils::MakeBindGroup(device, CreateBindGroupLayout(), {{0, mStorageBuffer}}); | |
| } | |
| void TestMaxedOutUserAndInternalImmediates(bool isRender); | |
| wgpu::ShaderModule mShaderModule; | |
| wgpu::Buffer mStorageBuffer; | |
| }; | |
| // ImmediateData has been uploaded successfully. | |
| TEST_P(ImmediateDataTests, BasicRenderPipeline) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| // rgba8unorm: {0.1, 0.3, 0.5} + {0.1 diff} => {0.2, 0.4, 0.6} => {51, 102, 153, 255} | |
| std::array<float, 4> immediateData = {0.1, 0.3, 0.5, 0.1}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| renderPassEncoder.SetImmediates(0, immediateData.data(), | |
| immediateData.size() * sizeof(uint32_t)); | |
| renderPassEncoder.SetPipeline(CreateRenderPipeline()); | |
| renderPassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // Test separately-declared immediate vars of different sizes in frag and vert shaders. | |
| TEST_P(ImmediateDataTests, FragAndVertSeparateImmediateVars) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| mShaderModule = utils::CreateShaderModule(device, R"( | |
| var<immediate> v_imm: vec2f; | |
| var<immediate> f_imm: mat4x4f; | |
| @vertex fn vsMain(@location(0) pos : vec2f) -> @builtin(position) vec4f { | |
| return vec4f(pos + v_imm, 0.0, 1.0); | |
| } | |
| @fragment fn fsMain() -> @location(0) vec4f { | |
| return f_imm[3]; | |
| } | |
| )"); | |
| // Elements {0..1} will offset the vertex positions in the vertex shader so that the resulting | |
| // triangle covers the framebuffer. Elements {12..15} will be output as the fragment color in | |
| // the fragment shader. | |
| std::array<float, 16> immediateData = {-2.0f, -3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, | |
| 0.0f, 0.0f, 0.0f, 0.0f, 0.2f, 0.4f, 0.6f, 1.0f}; | |
| utils::ComboRenderPipelineDescriptor pipelineDescriptor; | |
| pipelineDescriptor.vertex.module = mShaderModule; | |
| pipelineDescriptor.vertex.bufferCount = 1; | |
| pipelineDescriptor.cBuffers[0].attributeCount = 1; | |
| pipelineDescriptor.cBuffers[0].arrayStride = 2 * sizeof(float); | |
| pipelineDescriptor.cAttributes[0].format = wgpu::VertexFormat::Float32x2; | |
| pipelineDescriptor.cFragment.module = mShaderModule; | |
| pipelineDescriptor.cFragment.targetCount = 1; | |
| auto pipeline = device.CreateRenderPipeline(&pipelineDescriptor); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| float verts[3][2] = { | |
| { | |
| 3.0f, | |
| 2.0f, | |
| }, | |
| { | |
| 1.0f, | |
| 2.0f, | |
| }, | |
| { | |
| 2.0f, | |
| 4.0f, | |
| }, | |
| }; | |
| wgpu::BufferDescriptor bufferDesc; | |
| bufferDesc.size = sizeof(verts); | |
| bufferDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex; | |
| auto vertexBuffer = device.CreateBuffer(&bufferDesc); | |
| queue.WriteBuffer(vertexBuffer, 0, verts, sizeof(verts)); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| renderPassEncoder.SetVertexBuffer(0, vertexBuffer); | |
| renderPassEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * sizeof(float)); | |
| renderPassEncoder.SetPipeline(pipeline); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // Test that SetImmediates works in RenderBundle. | |
| TEST_P(ImmediateDataTests, SetImmediatesInRenderBundle) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| // rgba8unorm: {0.1, 0.3, 0.5} + {0.1 diff} => {0.2, 0.4, 0.6} => {51, 102, 153, 255} | |
| std::array<float, 4> immediateData = {0.1, 0.3, 0.5, 0.1}; | |
| wgpu::RenderBundleEncoderDescriptor bundleDesc; | |
| bundleDesc.colorFormatCount = 1; | |
| bundleDesc.colorFormats = &renderPass.colorFormat; | |
| wgpu::RenderBundleEncoder bundleEncoder = device.CreateRenderBundleEncoder(&bundleDesc); | |
| bundleEncoder.SetPipeline(pipeline); | |
| bundleEncoder.SetBindGroup(0, CreateBindGroup()); | |
| bundleEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * sizeof(float)); | |
| bundleEncoder.Draw(3); | |
| wgpu::RenderBundle bundle = bundleEncoder.Finish(); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| renderPassEncoder.ExecuteBundles(1, &bundle); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // Test that SetImmediates works after ExecuteBundles. | |
| TEST_P(ImmediateDataTests, SetImmediatesAfterExecuteBundles) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| // Bundle sets immediate data to {0.6, 0.7, 0.8, 0.9} | |
| std::array<float, 4> bundleImmediateData = {0.6, 0.7, 0.8, 0.9}; | |
| wgpu::RenderBundleEncoderDescriptor bundleDesc; | |
| bundleDesc.colorFormatCount = 1; | |
| bundleDesc.colorFormats = &renderPass.colorFormat; | |
| wgpu::RenderBundleEncoder bundleEncoder = device.CreateRenderBundleEncoder(&bundleDesc); | |
| bundleEncoder.SetPipeline(pipeline); | |
| bundleEncoder.SetBindGroup(0, CreateBindGroup()); | |
| bundleEncoder.SetImmediates(0, bundleImmediateData.data(), | |
| bundleImmediateData.size() * sizeof(float)); | |
| bundleEncoder.Draw(3); | |
| wgpu::RenderBundle bundle = bundleEncoder.Finish(); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| // Execute bundle. This should set the immediate data to {0.6, 0.7, 0.8, 0.9}. | |
| renderPassEncoder.ExecuteBundles(1, &bundle); | |
| // Set immediate data to {0.1, 0.3, 0.5, 0.1}. | |
| // rgba8unorm: {0.1, 0.3, 0.5} + {0.1 diff} => {0.2, 0.4, 0.6} => {51, 102, 153, 255} | |
| std::array<float, 4> immediateData = {0.1, 0.3, 0.5, 0.1}; | |
| renderPassEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * sizeof(float)); | |
| // ExecuteBundles clears the current pipeline, so we need to set it again. | |
| renderPassEncoder.SetPipeline(pipeline); | |
| renderPassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // ImmediateData has been uploaded successfully. | |
| TEST_P(ImmediateDataTests, BasicComputePipeline) { | |
| std::array<uint32_t, 4> immediateData = {25, 128, 240, 255}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass(); | |
| computePassEncoder.SetPipeline(CreateComputePipeline()); | |
| computePassEncoder.SetImmediates(0, immediateData.data(), | |
| immediateData.size() * sizeof(uint32_t)); | |
| computePassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| computePassEncoder.DispatchWorkgroups(1); | |
| computePassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_BUFFER_U32_RANGE_EQ(immediateData.data(), mStorageBuffer, 0, immediateData.size()); | |
| } | |
| TEST_P(ImmediateDataTests, IndirectDispatchPreservesUserImmediates) { | |
| wgpu::ComputePipelineDescriptor pipelineDesc; | |
| pipelineDesc.compute.module = utils::CreateShaderModule(device, R"( | |
| struct Data { | |
| values0: vec4u, | |
| values1: vec4u, | |
| values2: vec4u, | |
| values3: vec4u, | |
| } | |
| var<immediate> data: Data; | |
| @group(0) @binding(0) var<storage, read_write> output: Data; | |
| @compute @workgroup_size(1) fn main() { | |
| output = data; | |
| } | |
| )"); | |
| wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDesc); | |
| const std::array<uint32_t, 16> immediateData = {100, 101, 102, 103, 104, 105, 106, 107, | |
| 108, 109, 110, 111, 112, 113, 114, 115}; | |
| wgpu::BufferDescriptor bufferDesc; | |
| bufferDesc.size = sizeof(immediateData); | |
| bufferDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc; | |
| wgpu::Buffer output = device.CreateBuffer(&bufferDesc); | |
| wgpu::BindGroup bindGroup = | |
| utils::MakeBindGroup(device, pipeline.GetBindGroupLayout(0), {{0, output}}); | |
| const std::array<uint32_t, 3> workgroups = {1, 1, 1}; | |
| wgpu::Buffer indirectBuffer = utils::CreateBufferFromData( | |
| device, workgroups.data(), sizeof(workgroups), wgpu::BufferUsage::Indirect); | |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = encoder.BeginComputePass(); | |
| pass.SetPipeline(pipeline); | |
| pass.SetBindGroup(0, bindGroup); | |
| pass.SetImmediates(0, immediateData.data(), sizeof(immediateData)); | |
| pass.DispatchWorkgroupsIndirect(indirectBuffer, 0); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = encoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_BUFFER_U32_RANGE_EQ(immediateData.data(), output, 0, immediateData.size()); | |
| } | |
| // SetImmediates with offset on immediate data range. | |
| TEST_P(ImmediateDataTests, SetImmediatesWithRangeOffset) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| constexpr uint32_t kHalfImmediateDataSize = 8; | |
| // Render Pipeline | |
| { | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| // rgba8unorm: {0.1, 0.3, 0.5} + {0.1 diff} => {0.2, 0.4, 0.6} => {51, 102, 153, 255} | |
| std::array<float, 4> immediateData = {0.1, 0.3, 0.5, 0.1}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| renderPassEncoder.SetImmediates(0, immediateData.data(), 16); | |
| // Update {0.1, 0.3, 0.5} to {0.1,0.5,0.7} and + {0.1 diff} => {0.2, 0.6, 0.8} => {51, | |
| // 153, 204, 255} | |
| std::array<float, 2> immediateDataUpdated = {0.5, 0.7}; | |
| renderPassEncoder.SetImmediates(4, immediateDataUpdated.data(), 8); | |
| renderPassEncoder.SetPipeline(CreateRenderPipeline()); | |
| renderPassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 153, 204, 255), renderPass.color, 0, 0); | |
| } | |
| // Compute Pipeline | |
| { | |
| std::array<uint32_t, 4> immediateData = {25, 128, 240, 255}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass(); | |
| computePassEncoder.SetPipeline(CreateComputePipeline()); | |
| // Using two SetImmediates + Offset to swap first half and second half value in immediate | |
| // data range. | |
| computePassEncoder.SetImmediates(kHalfImmediateDataSize, immediateData.data(), | |
| kHalfImmediateDataSize); | |
| computePassEncoder.SetImmediates(0, &immediateData[2], kHalfImmediateDataSize); | |
| computePassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| computePassEncoder.DispatchWorkgroups(1); | |
| computePassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 4> expected = {240, 255, 25, 128}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), mStorageBuffer, 0, expected.size()); | |
| } | |
| } | |
| // SetImmediates should upload dirtied, latest contents between pipeline switches before draw or | |
| // dispatch. | |
| TEST_P(ImmediateDataTests, SetImmediatesMultipleTimes) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| // Render Pipeline | |
| { | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| // rgba8unorm: {0.1, 0.3, 0.5} + {0.1 diff} => {0.2, 0.4, 0.6} => {51, 102, 153, 255} | |
| std::array<float, 4> immediateData = {0.1, 0.3, 0.5, 0.1}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| // Using 4 SetImmediates to update all immediate data to 0.1. | |
| renderPassEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * 4); | |
| renderPassEncoder.SetImmediates(4, immediateData.data(), (immediateData.size() - 1) * 4); | |
| renderPassEncoder.SetPipeline(CreateRenderPipeline()); | |
| renderPassEncoder.SetImmediates(8, immediateData.data(), 8); | |
| renderPassEncoder.SetPipeline(CreateRenderPipeline()); | |
| renderPassEncoder.SetImmediates(12, immediateData.data(), 4); | |
| renderPassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 51, 51, 255), renderPass.color, 0, 0); | |
| } | |
| // Compute Pipeline | |
| { | |
| std::array<uint32_t, 4> immediateData = {25, 128, 240, 255}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass(); | |
| // Using 4 SetImmediates to update all immediate data to 25. | |
| computePassEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * 4); | |
| computePassEncoder.SetImmediates(4, immediateData.data(), (immediateData.size() - 1) * 4); | |
| computePassEncoder.SetPipeline(CreateComputePipeline()); | |
| computePassEncoder.SetImmediates(8, immediateData.data(), 8); | |
| computePassEncoder.SetPipeline(CreateComputePipeline()); | |
| computePassEncoder.SetImmediates(12, immediateData.data(), 4); | |
| computePassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| computePassEncoder.DispatchWorkgroups(1); | |
| computePassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 4> expected = {25, 25, 25, 25}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), mStorageBuffer, 0, expected.size()); | |
| } | |
| } | |
| // Test that clamp frag depth(supported by internal immediates) | |
| // works fine when shaders have user immediate data | |
| TEST_P(ImmediateDataTests, UsingImmediateDataDontAffectClampFragDepth) { | |
| // TODO(crbug.com/473870505): [Capture] support depth/stencil and multi-planar textures. | |
| DAWN_SUPPRESS_TEST_IF(IsCaptureReplayCheckingEnabled()); | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| var<immediate> constants: vec4f; | |
| @vertex fn vs() -> @builtin(position) vec4f { | |
| return vec4f(0.0, 0.0, 0.5, 1.0); | |
| } | |
| @fragment fn fs() -> @builtin(frag_depth) f32 { | |
| return constants.r; | |
| } | |
| )"); | |
| // Create the pipeline that uses frag_depth to output the depth. | |
| utils::ComboRenderPipelineDescriptor pDesc; | |
| pDesc.vertex.module = module; | |
| pDesc.primitive.topology = wgpu::PrimitiveTopology::PointList; | |
| pDesc.cFragment.module = module; | |
| pDesc.cFragment.targetCount = 0; | |
| wgpu::DepthStencilState* pDescDS = pDesc.EnableDepthStencil(wgpu::TextureFormat::Depth32Float); | |
| pDescDS->depthWriteEnabled = wgpu::OptionalBool::True; | |
| pDescDS->depthCompare = wgpu::CompareFunction::Always; | |
| wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pDesc); | |
| // Create a depth-only render pass. | |
| wgpu::TextureDescriptor depthDesc; | |
| depthDesc.size = {1, 1}; | |
| depthDesc.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc; | |
| depthDesc.format = wgpu::TextureFormat::Depth32Float; | |
| wgpu::Texture depthTexture = device.CreateTexture(&depthDesc); | |
| std::array<float, 4> immediateData = {1.0, 1.0, 1.0, 1.0}; | |
| utils::ComboRenderPassDescriptor renderPassDesc({}, depthTexture.CreateView()); | |
| renderPassDesc.cDepthStencilAttachmentInfo.stencilLoadOp = wgpu::LoadOp::Undefined; | |
| renderPassDesc.cDepthStencilAttachmentInfo.stencilStoreOp = wgpu::StoreOp::Undefined; | |
| // Draw a point with a skewed viewport, so 1.0 depth gets clamped to 0.5. | |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPassDesc); | |
| pass.SetViewport(0, 0, 1, 1, 0.0, 0.5); | |
| pass.SetImmediates(0, immediateData.data(), immediateData.size() * 4); | |
| pass.SetPipeline(pipeline); | |
| pass.Draw(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = encoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_FLOAT_EQ(0.5f, depthTexture, 0, 0); | |
| } | |
| // Test that a render pipeline using user immediate data, @builtin(frag_depth), and dynamic control | |
| // flow works properly alongside internal immediate slots (clampFragDepth + nonConstantZero). | |
| // See https://crbug.com/589368929. | |
| TEST_P(ImmediateDataTests, FragDepthWithUserImmediateAndControlFlow) { | |
| // TODO(crbug.com/473870505): [Capture] support depth/stencil and multi-planar textures. | |
| DAWN_SUPPRESS_TEST_IF(IsCaptureReplayCheckingEnabled()); | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| var<immediate> mode: u32; | |
| struct FsOut { | |
| @location(0) color: vec4f, | |
| @builtin(frag_depth) depth: f32, | |
| } | |
| @vertex fn vs() -> @builtin(position) vec4f { | |
| return vec4f(0.0, 0.0, 0.5, 1.0); | |
| } | |
| @fragment fn fs() -> FsOut { | |
| var out: FsOut; | |
| if (mode != 0u) { | |
| out.color = vec4f(0.0, 1.0, 0.0, 1.0); | |
| } else { | |
| out.color = vec4f(1.0, 0.0, 0.0, 1.0); | |
| } | |
| switch (mode) { | |
| case 1u: { | |
| out.depth = 1.0; | |
| } | |
| default: { | |
| out.depth = 0.25; | |
| } | |
| } | |
| return out; | |
| } | |
| )"); | |
| wgpu::PipelineLayoutDescriptor plDesc = {}; | |
| plDesc.immediateSize = 4; | |
| wgpu::PipelineLayout layout = device.CreatePipelineLayout(&plDesc); | |
| constexpr wgpu::TextureFormat kDepthFormat = wgpu::TextureFormat::Depth32Float; | |
| utils::ComboRenderPipelineDescriptor pDesc; | |
| pDesc.layout = layout; | |
| pDesc.vertex.module = module; | |
| pDesc.primitive.topology = wgpu::PrimitiveTopology::PointList; | |
| pDesc.cFragment.module = module; | |
| pDesc.cFragment.targetCount = 1; | |
| pDesc.cTargets[0].format = wgpu::TextureFormat::RGBA8Unorm; | |
| wgpu::DepthStencilState* pDescDS = pDesc.EnableDepthStencil(kDepthFormat); | |
| pDescDS->depthWriteEnabled = wgpu::OptionalBool::True; | |
| pDescDS->depthCompare = wgpu::CompareFunction::Always; | |
| wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pDesc); | |
| wgpu::TextureDescriptor colorDesc = {}; | |
| colorDesc.size = {1, 1}; | |
| colorDesc.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc; | |
| colorDesc.format = wgpu::TextureFormat::RGBA8Unorm; | |
| wgpu::Texture colorTexture = device.CreateTexture(&colorDesc); | |
| wgpu::TextureDescriptor depthDesc = {}; | |
| depthDesc.size = {1, 1}; | |
| depthDesc.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc; | |
| depthDesc.format = kDepthFormat; | |
| wgpu::Texture depthTexture = device.CreateTexture(&depthDesc); | |
| utils::ComboRenderPassDescriptor renderPassDesc({colorTexture.CreateView()}, | |
| depthTexture.CreateView()); | |
| renderPassDesc.cDepthStencilAttachmentInfo.stencilLoadOp = wgpu::LoadOp::Undefined; | |
| renderPassDesc.cDepthStencilAttachmentInfo.stencilStoreOp = wgpu::StoreOp::Undefined; | |
| uint32_t mode = 1u; | |
| wgpu::CommandEncoder encoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPassDesc); | |
| pass.SetViewport(0, 0, 1, 1, 0.0, 0.5); | |
| pass.SetPipeline(pipeline); | |
| pass.SetImmediates(0, &mode, sizeof(mode)); | |
| pass.Draw(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = encoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8::kGreen, colorTexture, 0, 0); | |
| EXPECT_PIXEL_FLOAT_EQ(0.5f, depthTexture, 0, 0); | |
| } | |
| // Test that vertex_index (supported by internal immediates) | |
| // works fine when the immediates are unused and optimized out by the driver. | |
| TEST_P(ImmediateDataTests, VertexIndexOptimizedOut) { | |
| DAWN_SUPPRESS_TEST_IF(IsCaptureReplayCheckingEnabled()); | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| fn v(index : u32) -> vec4f { | |
| return vec4f(0.0, 0.0, 0.0, 1.0); | |
| } | |
| @vertex fn vs(@builtin(vertex_index) index : u32) -> @builtin(position) vec4f { | |
| return v(index); | |
| } | |
| @fragment fn fs() -> @location(0) vec4f { | |
| return vec4f(1.0, 1.0, 1.0, 1.0); | |
| } | |
| )"); | |
| // Create the pipeline that uses frag_depth to output the depth. | |
| utils::ComboRenderPipelineDescriptor pDesc; | |
| pDesc.vertex.module = module; | |
| pDesc.primitive.topology = wgpu::PrimitiveTopology::PointList; | |
| pDesc.cFragment.module = module; | |
| pDesc.cFragment.targetCount = 1; | |
| wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pDesc); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| renderPassEncoder.SetPipeline(pipeline); | |
| renderPassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(255, 255, 255, 255), renderPass.color, 0, 0); | |
| } | |
| // SetImmediates Multiple times should upload dirtied, latest contents. | |
| TEST_P(ImmediateDataTests, SetImmediatesWithPipelineSwitch) { | |
| wgpu::ShaderModule shaderModuleWithLessImmediateData = utils::CreateShaderModule(device, R"( | |
| struct Immediate { | |
| color: vec3<f32>, | |
| }; | |
| var<immediate> constants: Immediate; | |
| struct VertexOut { | |
| @location(0) color : vec3f, | |
| @builtin(position) position : vec4f, | |
| } | |
| @vertex fn vsMain(@builtin(vertex_index) VertexIndex : u32) -> VertexOut { | |
| const pos = array( | |
| vec2( 1.0, -1.0), | |
| vec2(-1.0, -1.0), | |
| vec2( 0.0, 1.0), | |
| ); | |
| var output: VertexOut; | |
| output.position = vec4f(pos[VertexIndex], 0.0, 1.0); | |
| output.color = constants.color; | |
| return output; | |
| } | |
| // to reuse the same pipeline layout | |
| @fragment fn fsMain(@location(0) color:vec3f) -> @location(0) vec4f { | |
| return vec4f(color, 1.0); | |
| } | |
| var<immediate> computeConstants: vec3u; | |
| @group(0) @binding(0) var<storage, read_write> output : vec3u; | |
| @compute @workgroup_size(1, 1, 1) | |
| fn csMain() { | |
| output = computeConstants; | |
| })"); | |
| // Render Pipeline | |
| { | |
| utils::ComboRenderPipelineDescriptor pipelineDescriptor; | |
| pipelineDescriptor.vertex.module = shaderModuleWithLessImmediateData; | |
| pipelineDescriptor.cFragment.module = shaderModuleWithLessImmediateData; | |
| pipelineDescriptor.cFragment.targetCount = 1; | |
| wgpu::RenderPipeline pipelineWithLessImmediateData = | |
| device.CreateRenderPipeline(&pipelineDescriptor); | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| // rgba8unorm: {0.2, 0.4, 0.6} + {0.1 diff} => {0.3, 0.5, 0.7} | |
| std::array<float, 4> immediateData = {0.2, 0.4, 0.6, 0.1}; | |
| renderPassEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * 4); | |
| renderPassEncoder.SetPipeline(CreateRenderPipeline()); | |
| // replace the pipeline and rgba8unorm: {0.4, 0.4, 0.6} => {102, 102, 153} | |
| float data = 0.4; | |
| renderPassEncoder.SetImmediates(0, &data, 4); | |
| renderPassEncoder.SetPipeline(pipelineWithLessImmediateData); | |
| renderPassEncoder.Draw(3); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(102, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // Compute Pipeline | |
| { | |
| wgpu::ComputePipelineDescriptor csDesc; | |
| csDesc.compute.module = shaderModuleWithLessImmediateData; | |
| wgpu::ComputePipeline pipelineWithLessImmediateData = device.CreateComputePipeline(&csDesc); | |
| wgpu::BindGroup bindGroup = utils::MakeBindGroup( | |
| device, pipelineWithLessImmediateData.GetBindGroupLayout(0), {{0, mStorageBuffer}}); | |
| std::array<uint32_t, 4> immediateData = {25, 128, 240, 255}; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass(); | |
| computePassEncoder.SetImmediates(0, immediateData.data(), immediateData.size() * 4); | |
| computePassEncoder.SetPipeline(CreateComputePipeline()); | |
| uint32_t data = 128; | |
| computePassEncoder.SetImmediates(0, &data, 4); | |
| computePassEncoder.SetPipeline(pipelineWithLessImmediateData); | |
| computePassEncoder.SetBindGroup(0, bindGroup); | |
| computePassEncoder.DispatchWorkgroups(1); | |
| computePassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 3> expected = {128, 128, 240}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), mStorageBuffer, 0, expected.size()); | |
| } | |
| } | |
| // Test that SetImmediates works with multiple ExecuteBundles calls. | |
| TEST_P(ImmediateDataTests, SetImmediatesInMultipleExecuteBundles) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| wgpu::RenderBundleEncoderDescriptor bundleDesc; | |
| bundleDesc.colorFormatCount = 1; | |
| bundleDesc.colorFormats = &renderPass.colorFormat; | |
| // Bundle 1 | |
| std::array<float, 4> data1 = {0.6, 0.7, 0.8, 0.1}; | |
| wgpu::RenderBundleEncoder bundleEncoder1 = device.CreateRenderBundleEncoder(&bundleDesc); | |
| bundleEncoder1.SetPipeline(pipeline); | |
| bundleEncoder1.SetBindGroup(0, CreateBindGroup()); | |
| bundleEncoder1.SetImmediates(0, data1.data(), data1.size() * sizeof(float)); | |
| bundleEncoder1.Draw(3); | |
| wgpu::RenderBundle bundle1 = bundleEncoder1.Finish(); | |
| // Bundle 2 | |
| // Use values that avoid rounding ambiguity. | |
| // rgba8unorm: {0.1, 0.3, 0.5} + {0.1 diff} => {0.2, 0.4, 0.6} => {51, 102, 153, 255} | |
| std::array<float, 4> data2 = {0.1, 0.3, 0.5, 0.1}; | |
| wgpu::RenderBundleEncoder bundleEncoder2 = device.CreateRenderBundleEncoder(&bundleDesc); | |
| bundleEncoder2.SetPipeline(pipeline); | |
| bundleEncoder2.SetBindGroup(0, CreateBindGroup()); | |
| bundleEncoder2.SetImmediates(0, data2.data(), data2.size() * sizeof(float)); | |
| bundleEncoder2.Draw(3); | |
| wgpu::RenderBundle bundle2 = bundleEncoder2.Finish(); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| renderPassEncoder.ExecuteBundles(1, &bundle1); | |
| renderPassEncoder.ExecuteBundles(1, &bundle2); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // Test that RenderBundle immediate state is not affected by previous SetImmediates state in | |
| // RenderPass. | |
| TEST_P(ImmediateDataTests, BundlesDontCarePreviousImmediatesState) { | |
| // TODO(crbug.com/479563279): diagnose failures on Pixel 4 OpenGLES | |
| DAWN_SUPPRESS_TEST_IF(IsOpenGLES() && IsAndroid() && IsQualcomm()); | |
| wgpu::RenderPipeline pipeline = CreateRenderPipeline(); | |
| utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| // Bundle | |
| std::array<float, 4> bundleData = {0.1, 0.3, 0.5, 0.1}; | |
| wgpu::RenderBundleEncoderDescriptor bundleDesc; | |
| bundleDesc.colorFormatCount = 1; | |
| bundleDesc.colorFormats = &renderPass.colorFormat; | |
| wgpu::RenderBundleEncoder bundleEncoder = device.CreateRenderBundleEncoder(&bundleDesc); | |
| bundleEncoder.SetPipeline(pipeline); | |
| bundleEncoder.SetBindGroup(0, CreateBindGroup()); | |
| bundleEncoder.SetImmediates(0, bundleData.data(), bundleData.size() * sizeof(float)); | |
| bundleEncoder.Draw(3); | |
| wgpu::RenderBundle bundle = bundleEncoder.Finish(); | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder renderPassEncoder = | |
| commandEncoder.BeginRenderPass(&renderPass.renderPassInfo); | |
| std::array<float, 4> dataA = {0.6, 0.7, 0.8, 0.1}; | |
| renderPassEncoder.SetImmediates(0, dataA.data(), dataA.size() * sizeof(float)); | |
| renderPassEncoder.SetPipeline(pipeline); | |
| renderPassEncoder.SetBindGroup(0, CreateBindGroup()); | |
| // Execute Bundle (should draw with 0.{0.1, 0.3, 0.5, 0.1}}) | |
| renderPassEncoder.ExecuteBundles(1, &bundle); | |
| renderPassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(51, 102, 153, 255), renderPass.color, 0, 0); | |
| } | |
| // Switching between render pipelines that trigger different internal immediates changes the | |
| // internal push-constant / root-signature size, which must rebind all bind groups. Pipeline A uses | |
| // neither vertex_index nor frag_depth; pipeline B uses both (firstVertex + clampFragDepth) and | |
| // reads the bind group. The size difference appears on D3D11/D3D12 (firstVertex), Vulkan/Metal | |
| // (clampFragDepth) and OpenGL (both). | |
| // TODO(crbug.com/366291600): On D3D12 firstVertex is always allocated and clampFragDepth is absent, | |
| // so both pipelines share one PipelineLayoutHandle and the switch is a no-op there (control case) | |
| // until the internal root constants become conditionally allocated. | |
| TEST_P(ImmediateDataTests, RenderBindGroupsReboundOnDifferentInternalImmediateSize) { | |
| // TODO(crbug.com/522872463): Produces incorrect result on Pixel 10. | |
| DAWN_SUPPRESS_TEST_IF(IsAndroid() && IsImgTec() && IsVulkan()); | |
| wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( | |
| device, {{0, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform}}); | |
| wgpu::PipelineLayout layout = utils::MakePipelineLayout(device, {bgl}, 4); | |
| // Single module, two pipelines each drawing one point (a point covers the 1x1 target without | |
| // needing vertex_index in pipeline A): | |
| // - Pipeline A (vsPlain + fsRed): no vertex_index, no frag_depth -> smallest internal | |
| // immediate. | |
| // - Pipeline B (vsIndexed + fsFromBindGroup): uses vertex_index and writes frag_depth, and | |
| // reads the bind group. flat+either keeps vertex_index valid in compatibility mode. | |
| wgpu::ShaderModule shaderModule = utils::CreateShaderModule(device, R"( | |
| @group(0) @binding(0) var<uniform> color: vec4f; | |
| var<immediate> imm: f32; | |
| @vertex fn vsPlain() -> @builtin(position) vec4f { return vec4f(0, 0, 0, 1); } | |
| @fragment fn fsRed() -> @location(0) vec4f { return vec4f(1, 0, 0, 1); } | |
| struct VsOut { | |
| @builtin(position) position: vec4f, | |
| @location(0) @interpolate(flat, either) vertexIndex: u32, | |
| } | |
| @vertex fn vsIndexed(@builtin(vertex_index) i: u32) -> VsOut { | |
| return VsOut(vec4f(0, 0, 0, 1), i); | |
| } | |
| struct FsOut { @location(0) c: vec4f, @builtin(frag_depth) d: f32 } | |
| @fragment fn fsFromBindGroup(@location(0) @interpolate(flat, either) vertexIndex: u32) | |
| -> FsOut { | |
| return FsOut(vec4f(f32(vertexIndex) / 255.0, color.g, color.b, 1.0), 0.5); | |
| } | |
| )"); | |
| auto MakePipeline = [&](const char* vsEntry, const char* fsEntry, bool useDepthWrite) { | |
| utils::ComboRenderPipelineDescriptor desc; | |
| desc.vertex.module = shaderModule; | |
| desc.vertex.entryPoint = vsEntry; | |
| desc.primitive.topology = wgpu::PrimitiveTopology::PointList; | |
| desc.cFragment.module = shaderModule; | |
| desc.cFragment.entryPoint = fsEntry; | |
| desc.cFragment.targetCount = 1; | |
| desc.layout = layout; | |
| auto* ds = desc.EnableDepthStencil(wgpu::TextureFormat::Depth32Float); | |
| ds->depthWriteEnabled = | |
| useDepthWrite ? wgpu::OptionalBool::True : wgpu::OptionalBool::False; | |
| ds->depthCompare = wgpu::CompareFunction::Always; | |
| return device.CreateRenderPipeline(&desc); | |
| }; | |
| // Pipeline A: no internal immediates. Pipeline B: firstVertex + clampFragDepth. | |
| wgpu::RenderPipeline pipelineWithoutInternalImmediates = | |
| MakePipeline("vsPlain", "fsRed", false); | |
| wgpu::RenderPipeline pipelineWithInternalImmediates = | |
| MakePipeline("vsIndexed", "fsFromBindGroup", true); | |
| // color = {_, 0.4, 0.6, _} -> G = 102, B = 153. | |
| wgpu::Buffer colorBuf = | |
| utils::CreateBufferFromData(device, wgpu::BufferUsage::Uniform, {0.2f, 0.4f, 0.6f, 1.0f}); | |
| wgpu::BindGroup bg = utils::MakeBindGroup(device, bgl, {{0, colorBuf}}); | |
| utils::BasicRenderPass rp = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| wgpu::TextureDescriptor depthDesc; | |
| depthDesc.size = {kRTSize, kRTSize}; | |
| depthDesc.usage = wgpu::TextureUsage::RenderAttachment; | |
| depthDesc.format = wgpu::TextureFormat::Depth32Float; | |
| wgpu::Texture depthTex = device.CreateTexture(&depthDesc); | |
| utils::ComboRenderPassDescriptor rpDesc({rp.color.CreateView()}, depthTex.CreateView()); | |
| rpDesc.UnsetDepthStencilLoadStoreOpsForFormat(wgpu::TextureFormat::Depth32Float); | |
| float immData = 0.0f; | |
| constexpr uint32_t kFirstVertex = 5; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder pass = enc.BeginRenderPass(&rpDesc); | |
| pass.SetBindGroup(0, bg); | |
| pass.SetImmediates(0, &immData, sizeof(float)); | |
| // Draw Pipeline A, then switch to Pipeline B without re-setting the bind group. A backend whose | |
| // internal immediate size changes across the switch must reapply the bind group bindings. | |
| pass.SetPipeline(pipelineWithoutInternalImmediates); | |
| pass.Draw(1); | |
| pass.SetPipeline(pipelineWithInternalImmediates); | |
| pass.Draw(1, 1, kFirstVertex, 0); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| // R == firstVertex proves vertex_index includes the offset (the D3D12 internal root constant); | |
| // G/B prove the bind group is still applied after switching internal immediate sizes. | |
| EXPECT_PIXEL_RGBA8_EQ(utils::RGBA8(kFirstVertex, 102, 153, 255), rp.color, 0, 0); | |
| } | |
| // Switching between compute pipelines that trigger different internal immediates must rebind all | |
| // bind groups. Pipeline A uses no num_workgroups; pipeline B uses num_workgroups (internal root | |
| // constant on D3D12, internal immediate on D3D11; native on Vulkan/Metal/OpenGL). After switching | |
| // A->B without re-setting the bind group, B's write to the bound storage buffer must still land. | |
| TEST_P(ImmediateDataTests, ComputeBindGroupsReboundOnDifferentInternalImmediateSize) { | |
| wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( | |
| device, {{0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}}); | |
| wgpu::PipelineLayout layout = utils::MakeBasicPipelineLayout(device, &bgl); | |
| // Single module, two entry points sharing the layout: | |
| // - csPlain: no num_workgroups -> smallest internal immediate footprint. | |
| // - csNumWorkgroups: uses num_workgroups -> internal root constant (D3D12) / immediate | |
| // (D3D11). | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| @group(0) @binding(0) var<storage, read_write> output : vec3u; | |
| @compute @workgroup_size(1, 1, 1) fn csPlain() { | |
| output = vec3u(111u, 111u, 111u); | |
| } | |
| @compute @workgroup_size(1, 1, 1) fn csNumWorkgroups(@builtin(num_workgroups) n : vec3u) { | |
| output = n; | |
| } | |
| )"); | |
| auto MakePipeline = [&](const char* entryPoint) { | |
| wgpu::ComputePipelineDescriptor desc; | |
| desc.layout = layout; | |
| desc.compute.module = module; | |
| desc.compute.entryPoint = entryPoint; | |
| return device.CreateComputePipeline(&desc); | |
| }; | |
| wgpu::ComputePipeline pipelineWithoutInternalImmediates = MakePipeline("csPlain"); | |
| wgpu::ComputePipeline pipelineWithInternalImmediates = MakePipeline("csNumWorkgroups"); | |
| wgpu::BufferDescriptor bufferDesc; | |
| bufferDesc.size = sizeof(uint32_t) * 4; | |
| bufferDesc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Storage; | |
| wgpu::Buffer output = device.CreateBuffer(&bufferDesc); | |
| wgpu::BindGroup bg = utils::MakeBindGroup(device, bgl, {{0, output}}); | |
| constexpr uint32_t kX = 2, kY = 3, kZ = 4; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| pass.SetBindGroup(0, bg); | |
| // Dispatch Pipeline A, then switch to Pipeline B without re-setting the bind group. A backend | |
| // whose internal immediate size changes across the switch must reapply the bind group. | |
| pass.SetPipeline(pipelineWithoutInternalImmediates); | |
| pass.DispatchWorkgroups(1, 1, 1); | |
| pass.SetPipeline(pipelineWithInternalImmediates); | |
| pass.DispatchWorkgroups(kX, kY, kZ); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| // {2,3,4} (not A's 111) proves B's num_workgroups is correct and the bind group is still | |
| // applied after the pipeline switch. | |
| std::array<uint32_t, 3> expected = {kX, kY, kZ}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), output, 0, expected.size()); | |
| } | |
| // Switching pipelines with different calculated PipelineLayout immediateSize must rebind all | |
| // bind groups. | |
| TEST_P(ImmediateDataTests, BindGroupsReboundOnDifferentPipelineLayoutImmediateSize) { | |
| wgpu::BindGroupLayout bgl0 = CreateBindGroupLayout(); | |
| wgpu::PipelineLayout layout4Bytes = utils::MakePipelineLayout(device, {bgl0}, 4); | |
| wgpu::PipelineLayout layout16Bytes = utils::MakePipelineLayout(device, {bgl0}, 16); | |
| wgpu::ShaderModule module4Bytes = utils::CreateShaderModule(device, R"( | |
| var<immediate> imm: u32; | |
| @group(0) @binding(0) var<storage, read_write> output: vec4u; | |
| @compute @workgroup_size(1) fn csMain() { output = vec4u(imm, 0, 0, 0); } | |
| )"); | |
| wgpu::ShaderModule module16Bytes = utils::CreateShaderModule(device, R"( | |
| var<immediate> imm: vec4u; | |
| @group(0) @binding(0) var<storage, read_write> output: vec4u; | |
| @compute @workgroup_size(1) fn csMain() { output = imm; } | |
| )"); | |
| wgpu::ComputePipelineDescriptor csDesc4Bytes; | |
| csDesc4Bytes.compute.module = module4Bytes; | |
| csDesc4Bytes.layout = layout4Bytes; | |
| wgpu::ComputePipeline pipeline4Bytes = device.CreateComputePipeline(&csDesc4Bytes); | |
| wgpu::ComputePipelineDescriptor csDesc16Bytes; | |
| csDesc16Bytes.compute.module = module16Bytes; | |
| csDesc16Bytes.layout = layout16Bytes; | |
| wgpu::ComputePipeline pipeline16Bytes = device.CreateComputePipeline(&csDesc16Bytes); | |
| wgpu::BindGroup bg0 = utils::MakeBindGroup(device, bgl0, {{0, mStorageBuffer}}); | |
| uint32_t imm4Bytes = 42; | |
| std::array<uint32_t, 4> imm16Bytes = {10, 20, 30, 40}; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| // Dispatch with Pipeline A, then switch to Pipeline B without re-setting | |
| // bind groups. | |
| pass.SetBindGroup(0, bg0); | |
| pass.SetImmediates(0, &imm4Bytes, sizeof(uint32_t)); | |
| pass.SetPipeline(pipeline4Bytes); | |
| pass.DispatchWorkgroups(1); | |
| pass.SetImmediates(0, imm16Bytes.data(), sizeof(imm16Bytes)); | |
| pass.SetPipeline(pipeline16Bytes); | |
| pass.DispatchWorkgroups(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 4> expected = {10, 20, 30, 40}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), mStorageBuffer, 0, expected.size()); | |
| } | |
| // Switching pipelines with different calculated PipelineLayout immediateSize must rebind all | |
| // bind groups. Also tests that defaulted pipeline layouts work correctly. | |
| TEST_P(ImmediateDataTests, AutoCalculatedPipelineLayoutImmediateSize) { | |
| wgpu::BindGroupLayout bgl0 = CreateBindGroupLayout(); | |
| wgpu::ShaderModule module4Bytes = utils::CreateShaderModule(device, R"( | |
| var<immediate> imm: u32; | |
| @group(0) @binding(0) var<storage, read_write> output: vec4u; | |
| @compute @workgroup_size(1) fn csMain() { output = vec4u(imm, 0, 0, 0); } | |
| )"); | |
| wgpu::ShaderModule module16Bytes = utils::CreateShaderModule(device, R"( | |
| var<immediate> imm: vec4u; | |
| @group(0) @binding(0) var<storage, read_write> output: vec4u; | |
| @compute @workgroup_size(1) fn csMain() { output = imm; } | |
| )"); | |
| wgpu::ComputePipelineDescriptor csDesc4Bytes; | |
| csDesc4Bytes.compute.module = module4Bytes; | |
| wgpu::ComputePipelineDescriptor csDesc16Bytes; | |
| csDesc16Bytes.compute.module = module16Bytes; | |
| wgpu::ComputePipeline pipeline4Bytes = device.CreateComputePipeline(&csDesc4Bytes); | |
| wgpu::ComputePipeline pipeline16Bytes = device.CreateComputePipeline(&csDesc16Bytes); | |
| wgpu::BindGroup bg4Bytes = | |
| utils::MakeBindGroup(device, pipeline4Bytes.GetBindGroupLayout(0), {{0, mStorageBuffer}}); | |
| wgpu::BindGroup bg16Bytes = | |
| utils::MakeBindGroup(device, pipeline16Bytes.GetBindGroupLayout(0), {{0, mStorageBuffer}}); | |
| uint32_t imm4Bytes = 42; | |
| std::array<uint32_t, 4> imm16Bytes = {10, 20, 30, 40}; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| // Dispatch with Pipeline A, then switch to Pipeline B using auto-calculated layout | |
| pass.SetBindGroup(0, bg4Bytes); | |
| pass.SetImmediates(0, &imm4Bytes, sizeof(uint32_t)); | |
| pass.SetPipeline(pipeline4Bytes); | |
| pass.DispatchWorkgroups(1); | |
| // Defaulted layouts are not compatible between pipelines at all. | |
| // so we re-bind the properly derived bind group for pipeline16Bytes. | |
| pass.SetBindGroup(0, bg16Bytes); | |
| pass.SetImmediates(0, imm16Bytes.data(), sizeof(imm16Bytes)); | |
| pass.SetPipeline(pipeline16Bytes); | |
| pass.DispatchWorkgroups(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 4> expected = {10, 20, 30, 40}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), mStorageBuffer, 0, expected.size()); | |
| } | |
| // A compute entry point that uses a vec3u user immediate (12 bytes, not a multiple of 16) together | |
| // with @builtin(num_workgroups). The user immediate block precedes num_workgroups in the internal | |
| // immediate layout, so the 12-byte user block pushes num_workgroups onto a 4-aligned, non-16-byte | |
| // offset. Both the user immediate and num_workgroups must read back correctly. | |
| TEST_P(ImmediateDataTests, ComputeVec3UserImmediateWithNumWorkgroups) { | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| var<immediate> userImm : vec3u; | |
| struct Output { | |
| userImm : vec3u, | |
| numWorkgroups : vec3u, | |
| }; | |
| @group(0) @binding(0) var<storage, read_write> output : Output; | |
| @compute @workgroup_size(1, 1, 1) fn csMain(@builtin(num_workgroups) n : vec3u) { | |
| output.userImm = userImm; | |
| output.numWorkgroups = n; | |
| } | |
| )"); | |
| wgpu::ComputePipelineDescriptor desc; | |
| desc.compute.module = module; | |
| wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&desc); | |
| wgpu::BufferDescriptor bufferDesc; | |
| bufferDesc.size = sizeof(uint32_t) * 8; | |
| bufferDesc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Storage; | |
| wgpu::Buffer output = device.CreateBuffer(&bufferDesc); | |
| wgpu::BindGroup bg = | |
| utils::MakeBindGroup(device, pipeline.GetBindGroupLayout(0), {{0, output}}); | |
| std::array<uint32_t, 3> userImm = {11, 22, 33}; | |
| constexpr uint32_t kX = 2, kY = 3, kZ = 4; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| pass.SetPipeline(pipeline); | |
| pass.SetBindGroup(0, bg); | |
| pass.SetImmediates(0, userImm.data(), userImm.size() * sizeof(uint32_t)); | |
| pass.DispatchWorkgroups(kX, kY, kZ); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| // The Output struct places userImm at byte offset 0 and numWorkgroups at byte offset 16 (vec3u | |
| // members are 16-byte aligned in std430). The EXPECT_BUFFER offset argument is in bytes. | |
| std::array<uint32_t, 3> expectedUserImm = {11, 22, 33}; | |
| std::array<uint32_t, 3> expectedNumWorkgroups = {kX, kY, kZ}; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expectedUserImm.data(), output, 0, expectedUserImm.size()); | |
| EXPECT_BUFFER_U32_RANGE_EQ(expectedNumWorkgroups.data(), output, 16, | |
| expectedNumWorkgroups.size()); | |
| } | |
| void ImmediateDataTests::TestMaxedOutUserAndInternalImmediates(bool isRender) { | |
| const auto& limits = GetSupportedLimits(); | |
| const uint32_t numImmediates = limits.maxImmediateSize / sizeof(uint32_t); | |
| const uint32_t numUniforms = limits.maxUniformBuffersPerShaderStage; | |
| const uint32_t numStorage = | |
| isRender ? limits.maxStorageBuffersInFragmentStage : limits.maxStorageBuffersPerShaderStage; | |
| constexpr uint32_t kStride = 256; | |
| constexpr uint32_t kStrideU32 = kStride / sizeof(uint32_t); | |
| // Create one input buffer that we'll bind many times, at different offsets, as uniform/storage. | |
| const size_t numBufferBindings = numUniforms + numStorage; | |
| std::vector<uint32_t> inputBufferData(numBufferBindings * kStrideU32, 0); | |
| for (size_t k = 0; k < numBufferBindings; ++k) { | |
| inputBufferData[k * kStrideU32] = k + 1; | |
| } | |
| wgpu::Buffer inputBuffer = utils::CreateBufferFromData( | |
| device, inputBufferData.data(), inputBufferData.size() * sizeof(uint32_t), | |
| wgpu::BufferUsage::Uniform | wgpu::BufferUsage::Storage); | |
| wgpu::BufferDescriptor outputBufferDesc{ | |
| .usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, | |
| .size = 2ULL * kStride, | |
| }; | |
| wgpu::Buffer outputBuffer = device.CreateBuffer(&outputBufferDesc); | |
| // We'll create two bind groups, one for uniform and one for storage. The only reason to use two | |
| // is to exercise packing logic for buffers into the buffer lengths array. | |
| std::vector<wgpu::BindGroupLayoutEntry> bgl0Entries; | |
| std::vector<wgpu::BindGroupEntry> bg0Entries; | |
| std::vector<uint32_t> bg0DynamicOffsets; | |
| const wgpu::ShaderStage storageStages = | |
| isRender ? wgpu::ShaderStage::Fragment : wgpu::ShaderStage::Compute; | |
| // @group(1) @binding(0) is the output storage buffer. Input storage buffers start at 1. | |
| std::vector<wgpu::BindGroupLayoutEntry> bgl1Entries = { | |
| { | |
| .binding = 0, | |
| .visibility = storageStages, | |
| .buffer = {.type = wgpu::BufferBindingType::Storage, .hasDynamicOffset = true}, | |
| }, | |
| }; | |
| std::vector<wgpu::BindGroupEntry> bg1Entries = { | |
| {.binding = 0, .buffer = outputBuffer, .size = 4ULL * sizeof(uint32_t)}, | |
| }; | |
| std::vector<uint32_t> bg1DynamicOffsets = {kStride}; | |
| // Loop up to the limits, accumulating: | |
| // - Shader snippets | |
| // - Immediate data to upload | |
| // - Expected output values to test at the end | |
| std::string userImmediatesMembers; | |
| std::string bindingDeclarations; | |
| std::string body; | |
| std::vector<uint32_t> userImmediates(numImmediates); | |
| uint32_t expectedUserImmediatesSum = 0; | |
| uint32_t expectedUniformAndStorageBufferValuesSum = 0; | |
| uint32_t expectedBufferLengthsSum = 0; | |
| // User immediates. | |
| for (uint32_t i = 0; i < numImmediates; ++i) { | |
| userImmediatesMembers += absl::StrFormat("v%u : u32,\n", i); | |
| body += absl::StrFormat("userImmediatesSum += userImm.v%u;\n", i); | |
| userImmediates[i] = i + 1; | |
| expectedUserImmediatesSum += userImmediates[i]; | |
| } | |
| // Uniform buffers. | |
| for (uint32_t i = 0; i < numUniforms; ++i) { | |
| bool isDynamic = i < limits.maxDynamicUniformBuffersPerPipelineLayout; | |
| uint32_t binding = i; | |
| uint32_t offset = i * kStride; | |
| bindingDeclarations += | |
| absl::StrFormat("@group(0) @binding(%u) var<uniform> u%u : u32;\n", binding, binding); | |
| // In the render pipeline test, the first uniform will be accessed from the vertex shader | |
| // instead (this is hardcoded in the shader below). | |
| if (!isRender || binding > 0) { | |
| body += absl::StrFormat("uniformAndStorageValuesSum += u%u;\n", binding); | |
| } | |
| bgl0Entries.push_back({ | |
| .binding = binding, | |
| .visibility = isRender ? (wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Fragment) | |
| : wgpu::ShaderStage::Compute, | |
| .buffer = {.type = wgpu::BufferBindingType::Uniform, .hasDynamicOffset = isDynamic}, | |
| }); | |
| bg0Entries.push_back({ | |
| .binding = binding, | |
| .buffer = inputBuffer, | |
| .size = 16, | |
| }); | |
| // Same offset will be applied regardless, but may be static or dynamic. | |
| if (isDynamic) { | |
| bg0DynamicOffsets.push_back(offset); | |
| } else { | |
| bg0Entries.back().offset = offset; | |
| } | |
| expectedUniformAndStorageBufferValuesSum += i + 1; | |
| } | |
| // Storage buffers. (Binding 0 is the output, so here start at 1.) | |
| for (uint32_t i = 1; i < numStorage; ++i) { | |
| bool isDynamic = i < limits.maxDynamicStorageBuffersPerPipelineLayout; | |
| uint32_t slot = numUniforms + i; | |
| uint32_t offset = slot * kStride; | |
| uint32_t binding = i; | |
| uint32_t arrayLength = i + 1; | |
| bindingDeclarations += absl::StrFormat( | |
| "@group(1) @binding(%u) var<storage, read> s%u : array<u32>;\n", binding, binding); | |
| body += absl::StrFormat( | |
| R"( | |
| uniformAndStorageValuesSum += s%u[0]; | |
| bufferLengthsSum += arrayLength(&s%u); | |
| )", | |
| binding, binding); | |
| bgl1Entries.push_back({ | |
| .binding = binding, | |
| .visibility = storageStages, | |
| .buffer = {.type = wgpu::BufferBindingType::ReadOnlyStorage, | |
| .hasDynamicOffset = isDynamic}, | |
| }); | |
| bg1Entries.push_back({ | |
| .binding = binding, | |
| .buffer = inputBuffer, | |
| .size = uint64_t{arrayLength} * sizeof(uint32_t), | |
| }); | |
| // Same offset will be applied regardless, but may be static or dynamic. | |
| if (isDynamic) { | |
| bg1DynamicOffsets.push_back(offset); | |
| } else { | |
| bg1Entries.back().offset = offset; | |
| } | |
| expectedUniformAndStorageBufferValuesSum += slot + 1; | |
| expectedBufferLengthsSum += arrayLength; | |
| } | |
| // From the accumulated stuff, construct the bind groups and WGSL code. | |
| wgpu::BindGroupLayoutDescriptor bgl0Desc = { | |
| .entryCount = bgl0Entries.size(), | |
| .entries = bgl0Entries.data(), | |
| }; | |
| wgpu::BindGroupLayout bgl0 = device.CreateBindGroupLayout(&bgl0Desc); | |
| wgpu::BindGroupDescriptor bg0Desc = { | |
| .layout = bgl0, | |
| .entryCount = bg0Entries.size(), | |
| .entries = bg0Entries.data(), | |
| }; | |
| wgpu::BindGroup bg0 = device.CreateBindGroup(&bg0Desc); | |
| wgpu::BindGroupLayoutDescriptor bgl1Desc = { | |
| .entryCount = bgl1Entries.size(), | |
| .entries = bgl1Entries.data(), | |
| }; | |
| wgpu::BindGroupLayout bgl1 = device.CreateBindGroupLayout(&bgl1Desc); | |
| wgpu::BindGroupDescriptor bg1Desc = { | |
| .layout = bgl1, | |
| .entryCount = bg1Entries.size(), | |
| .entries = bg1Entries.data(), | |
| }; | |
| wgpu::BindGroup bg1 = device.CreateBindGroup(&bg1Desc); | |
| std::string wgsl = absl::StrFormat( | |
| R"( | |
| struct UserImm { | |
| %s | |
| } | |
| var<immediate> userImm : UserImm; | |
| @group(1) @binding(0) var<storage, read_write> outBuf : array<u32, 4>; | |
| %s | |
| )", | |
| userImmediatesMembers, bindingDeclarations); | |
| if (isRender) { | |
| wgsl += absl::StrFormat( | |
| R"( | |
| struct VsOut { | |
| @builtin(position) pos : vec4f, | |
| @location(0) @interpolate(flat, either) u0Value : u32, | |
| } | |
| @vertex fn vsMain() -> VsOut { | |
| return VsOut(vec4f(0.0, 0.0, 0.5, 1.0), u0); | |
| } | |
| struct FsOut { | |
| @location(0) color : vec4f, | |
| @builtin(frag_depth) depth : f32, | |
| } | |
| @fragment fn fsMain(@location(0) @interpolate(flat, either) u0Value : u32) -> FsOut { | |
| var userImmediatesSum = 0u; | |
| var uniformAndStorageValuesSum = u0Value; | |
| var bufferLengthsSum = 0u; | |
| %s | |
| outBuf[0] = userImmediatesSum; | |
| outBuf[1] = uniformAndStorageValuesSum; | |
| outBuf[2] = bufferLengthsSum; | |
| outBuf[3] = 0xffffffff; | |
| return FsOut(vec4f(1.0), 0.5); | |
| } | |
| )", | |
| body); | |
| } else { | |
| wgsl += absl::StrFormat( | |
| R"( | |
| @compute @workgroup_size(1) fn csMain( | |
| @builtin(num_workgroups) numWg : vec3u) { | |
| var userImmediatesSum = 0u; | |
| var uniformAndStorageValuesSum = 0u; | |
| var bufferLengthsSum = 0u; | |
| %s | |
| outBuf[0] = userImmediatesSum; | |
| outBuf[1] = uniformAndStorageValuesSum; | |
| outBuf[2] = bufferLengthsSum; | |
| outBuf[3] = numWg.x + numWg.y + numWg.z; | |
| } | |
| )", | |
| body); | |
| } | |
| wgpu::PipelineLayout pipelineLayout = | |
| utils::MakePipelineLayout(device, {bgl0, bgl1}, limits.maxImmediateSize); | |
| wgpu::ShaderModule shaderModule = utils::CreateShaderModule(device, wgsl); | |
| // And finally build the pipelines and execute. | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| if (isRender) { | |
| utils::BasicRenderPass rp = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| wgpu::TextureDescriptor depthDesc; | |
| depthDesc.size = {kRTSize, kRTSize}; | |
| depthDesc.usage = wgpu::TextureUsage::RenderAttachment; | |
| depthDesc.format = wgpu::TextureFormat::Depth32Float; | |
| wgpu::Texture depthTex = device.CreateTexture(&depthDesc); | |
| utils::ComboRenderPassDescriptor rpDesc({rp.color.CreateView()}, depthTex.CreateView()); | |
| rpDesc.UnsetDepthStencilLoadStoreOpsForFormat(wgpu::TextureFormat::Depth32Float); | |
| utils::ComboRenderPipelineDescriptor pipeDesc; | |
| pipeDesc.layout = pipelineLayout; | |
| pipeDesc.vertex.module = shaderModule; | |
| pipeDesc.primitive.topology = wgpu::PrimitiveTopology::PointList; | |
| pipeDesc.cFragment.module = shaderModule; | |
| // Enable depth to test clampFragDepth. | |
| wgpu::DepthStencilState* ds = | |
| pipeDesc.EnableDepthStencil(wgpu::TextureFormat::Depth32Float); | |
| ds->depthWriteEnabled = true; | |
| wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pipeDesc); | |
| wgpu::RenderPassEncoder pass = enc.BeginRenderPass(&rpDesc); | |
| pass.SetPipeline(pipeline); | |
| pass.SetViewport(0, 0, 1, 1, 0.0f, 0.5f); | |
| pass.SetImmediates(0, userImmediates.data(), limits.maxImmediateSize); | |
| pass.SetBindGroup(0, bg0, bg0DynamicOffsets.size(), bg0DynamicOffsets.data()); | |
| pass.SetBindGroup(1, bg1, bg1DynamicOffsets.size(), bg1DynamicOffsets.data()); | |
| pass.Draw(1); | |
| pass.End(); | |
| } else { | |
| wgpu::ComputePipelineDescriptor csDesc; | |
| csDesc.layout = pipelineLayout; | |
| csDesc.compute.module = shaderModule; | |
| wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&csDesc); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| pass.SetPipeline(pipeline); | |
| pass.SetImmediates(0, userImmediates.data(), limits.maxImmediateSize); | |
| pass.SetBindGroup(0, bg0, bg0DynamicOffsets.size(), bg0DynamicOffsets.data()); | |
| pass.SetBindGroup(1, bg1, bg1DynamicOffsets.size(), bg1DynamicOffsets.data()); | |
| pass.DispatchWorkgroups(1); | |
| pass.End(); | |
| } | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 4> expected = { | |
| expectedUserImmediatesSum, | |
| expectedUniformAndStorageBufferValuesSum, | |
| expectedBufferLengthsSum, | |
| isRender ? 0xffffffff : 3, | |
| }; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), outputBuffer, kStride, expected.size()); | |
| } | |
| // Tests that user immediate data up to maxImmediateSize, @builtin(num_workgroups), dynamic buffer | |
| // offsets up to the binding limits across multiple bind groups, and storage buffer arrayLength() | |
| // values all coexist in the immediate block without overwriting each other or overflowing the | |
| // staging immediate buffer in a compute pass. | |
| TEST_P(ImmediateDataTests, ComputeMaxedOutUserAndInternalImmediates) { | |
| // TODO(https://crbug.com/585864576): arrayLength() seems to return the remaining buffer size | |
| // rather than the bound range on dynamic storage buffers on Intel Windows Vulkan drivers. | |
| DAWN_SUPPRESS_TEST_IF(IsWindows() && IsVulkan() && IsIntel()); | |
| // TODO(https://crbug.com/42242119): Fails on Qualcomm Adreno X1. | |
| DAWN_SUPPRESS_TEST_IF(IsD3D11() && IsQualcomm()); | |
| TestMaxedOutUserAndInternalImmediates(false); | |
| } | |
| // Tests that user immediate data up to maxImmediateSize, clampFragDepth, dynamic buffer offsets | |
| // up to the binding limits across multiple bind groups, and storage buffer arrayLength() values | |
| // all coexist in the immediate block without overwriting each other or overflowing the staging | |
| // immediate buffer in a render pass. | |
| TEST_P(ImmediateDataTests, RenderMaxedOutUserAndInternalImmediates) { | |
| // TODO(https://crbug.com/585864576): arrayLength() seems to return the remaining buffer size | |
| // rather than the bound range on dynamic storage buffers on Intel Windows Vulkan drivers. | |
| DAWN_SUPPRESS_TEST_IF(IsWindows() && IsVulkan() && IsIntel()); | |
| // TODO(https://crbug.com/42242119): Fails on Qualcomm Adreno X1. | |
| DAWN_SUPPRESS_TEST_IF(IsD3D11() && IsQualcomm()); | |
| // TODO(crbug.com/473870505): [Capture] support depth/stencil and multi-planar textures. | |
| DAWN_SUPPRESS_TEST_IF(IsCaptureReplayCheckingEnabled()); | |
| TestMaxedOutUserAndInternalImmediates(true); | |
| } | |
| // Tests that in a render pass, user immediates, clampFragDepth, vertex/fragment dynamic buffer | |
| // offsets, and storage buffer arrayLength() all coexist in the immediate block, and remain valid | |
| // across a pipeline switch that shifts internal immediate offsets while inheriting bind group 0. | |
| TEST_P(ImmediateDataTests, RenderAllImmediateTypesWithPipelineSwitch) { | |
| // TODO(https://crbug.com/585864576): arrayLength() seems to return the remaining buffer size | |
| // rather than the bound range on dynamic storage buffers on Intel Windows Vulkan drivers. | |
| DAWN_SUPPRESS_TEST_IF(IsWindows() && IsVulkan() && IsIntel()); | |
| // TODO(https://crbug.com/473870505): [Capture] support depth/stencil and multi-planar textures. | |
| DAWN_SUPPRESS_TEST_IF(IsCaptureReplayCheckingEnabled()); | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| var<immediate> userImm : vec4u; | |
| @group(0) @binding(0) var<uniform> uVert : u32; | |
| @group(0) @binding(1) var<uniform> uFrag : u32; | |
| @group(0) @binding(2) var<storage, read> sDynamic : array<u32>; | |
| @group(0) @binding(3) var<storage, read> sStatic : array<u32>; | |
| struct VsOut { | |
| @builtin(position) pos : vec4f, | |
| @location(0) @interpolate(flat, either) uVertValue : u32, | |
| } | |
| struct FsOut { | |
| @location(0) color : vec4f, | |
| @builtin(frag_depth) depth : f32, | |
| } | |
| @vertex fn vsMain() -> VsOut { | |
| return VsOut(vec4f(0.0, 0.0, 0.5, 1.0), uVert); | |
| } | |
| @fragment fn fsWithoutFragDepth(@location(0) @interpolate(flat, either) uVertValue : u32) | |
| -> @location(0) vec4f { | |
| return vec4f(1.0, f32(uVertValue + uFrag) / 255.0, 0.0, 1.0); | |
| } | |
| @fragment fn fsWithFragDepth(@location(0) @interpolate(flat, either) uVertValue : u32) | |
| -> FsOut { | |
| let userImmediatesSum = userImm.x + userImm.y + userImm.z + userImm.w; | |
| let bufferLengthsSum = arrayLength(&sDynamic) + arrayLength(&sStatic); | |
| return FsOut( | |
| vec4f( | |
| f32(userImmediatesSum), | |
| f32(uVertValue + uFrag), | |
| f32(sDynamic[0]), | |
| f32(bufferLengthsSum), | |
| ) / 255.0, | |
| 1.0, | |
| ); | |
| } | |
| )"); | |
| wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( | |
| device, | |
| { | |
| {0, wgpu::ShaderStage::Vertex, wgpu::BufferBindingType::Uniform, true}, | |
| {1, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::Uniform, true}, | |
| {2, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::ReadOnlyStorage, true}, | |
| {3, wgpu::ShaderStage::Fragment, wgpu::BufferBindingType::ReadOnlyStorage}, | |
| }); | |
| wgpu::PipelineLayout pipelineLayout = utils::MakePipelineLayout(device, {bgl}, 16); | |
| utils::ComboRenderPipelineDescriptor pipeDesc; | |
| pipeDesc.layout = pipelineLayout; | |
| pipeDesc.vertex.module = module; | |
| pipeDesc.primitive.topology = wgpu::PrimitiveTopology::PointList; | |
| pipeDesc.cFragment.module = module; | |
| wgpu::DepthStencilState* ds = pipeDesc.EnableDepthStencil(wgpu::TextureFormat::Depth32Float); | |
| pipeDesc.cFragment.entryPoint = "fsWithoutFragDepth"; | |
| wgpu::RenderPipeline pipelineWithoutFragDepth = device.CreateRenderPipeline(&pipeDesc); | |
| pipeDesc.cFragment.entryPoint = "fsWithFragDepth"; | |
| // Enable depth to test clampFragDepth. | |
| ds->depthWriteEnabled = true; | |
| wgpu::RenderPipeline pipelineWithFragDepth = device.CreateRenderPipeline(&pipeDesc); | |
| constexpr uint32_t kStride = 256; | |
| constexpr uint32_t kStrideU32 = kStride / sizeof(uint32_t); | |
| std::array<uint32_t, 4 * kStrideU32> data = {}; | |
| data[kStrideU32] = 10; // uVert | |
| data[2 * kStrideU32] = 20; // uFrag | |
| data[3 * kStrideU32] = 40; // sDynamic[0] | |
| wgpu::Buffer buf = utils::CreateBufferFromData( | |
| device, data.data(), sizeof(data), wgpu::BufferUsage::Uniform | wgpu::BufferUsage::Storage); | |
| wgpu::BindGroup bg = utils::MakeBindGroup(device, bgl, | |
| { | |
| {0, buf, 0, 16}, | |
| {1, buf, 0, 16}, | |
| {2, buf, 0, 5ULL * sizeof(uint32_t)}, | |
| {3, buf, 0, 7ULL * sizeof(uint32_t)}, | |
| }); | |
| utils::BasicRenderPass rp = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); | |
| wgpu::TextureDescriptor depthDesc; | |
| depthDesc.size = {kRTSize, kRTSize}; | |
| depthDesc.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc; | |
| depthDesc.format = wgpu::TextureFormat::Depth32Float; | |
| wgpu::Texture depthTex = device.CreateTexture(&depthDesc); | |
| utils::ComboRenderPassDescriptor rpDesc({rp.color.CreateView()}, depthTex.CreateView()); | |
| rpDesc.UnsetDepthStencilLoadStoreOpsForFormat(wgpu::TextureFormat::Depth32Float); | |
| std::array<uint32_t, 4> userImm = {5, 6, 7, 8}; | |
| std::array<uint32_t, 3> dynOffsets = {kStride, 2 * kStride, 3 * kStride}; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::RenderPassEncoder pass = enc.BeginRenderPass(&rpDesc); | |
| pass.SetViewport(0, 0, 1, 1, 0.0f, 0.5f); | |
| pass.SetImmediates(0, userImm.data(), sizeof(userImm)); | |
| pass.SetBindGroup(0, bg, dynOffsets.size(), dynOffsets.data()); | |
| // Switch pipelines without re-setting bind groups, immediates, or viewport: group 0 is | |
| // inherited while clampFragDepth shifts dynamicOffsets and storageBufferLengths. | |
| pass.SetPipeline(pipelineWithoutFragDepth); | |
| pass.Draw(1); | |
| pass.SetPipeline(pipelineWithFragDepth); | |
| pass.Draw(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_PIXEL_RGBA8_EQ((utils::RGBA8{ | |
| 5 + 6 + 7 + 8, // immediates | |
| 10 + 20, // uniforms | |
| 40, // storage buffer content | |
| 5 + 7, // array lengths | |
| }), | |
| rp.color, 0, 0); | |
| EXPECT_PIXEL_FLOAT_EQ(0.5f, depthTex, 0, 0); | |
| } | |
| // Verify that updating only dynamic offsets on an already-bound BindGroup updates dynamicOffsets | |
| // while preserving previously uploaded user immediates and storage buffer lengths (arrayLength). | |
| TEST_P(ImmediateDataTests, UpdatingOnlyDynamicOffsetPreservesImmediatesAndArrayLength) { | |
| constexpr uint32_t kOffset = 256; | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| var<immediate> imm : u32; | |
| @group(0) @binding(0) var<storage, read> inBuf : array<u32>; | |
| @group(0) @binding(1) var<storage, read_write> outBuf : array<u32>; | |
| @compute @workgroup_size(1) fn main() { | |
| outBuf[inBuf[0]] = imm + inBuf[1] + arrayLength(&inBuf); | |
| } | |
| )"); | |
| wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( | |
| device, { | |
| {0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::ReadOnlyStorage, true}, | |
| {1, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}, | |
| }); | |
| wgpu::ComputePipelineDescriptor csDesc = { | |
| .layout = utils::MakePipelineLayout(device, {bgl}, 4), | |
| .compute = {.module = module}, | |
| }; | |
| wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&csDesc); | |
| constexpr uint32_t kBoundElements = 6; | |
| std::vector<uint32_t> sData(kOffset / sizeof(uint32_t) + kBoundElements, 0); | |
| sData[0] = 0; // Output index for dispatch 1 | |
| sData[1] = 10; // Value for dispatch 1 | |
| sData[kOffset / sizeof(uint32_t)] = 1; // Output index for dispatch 2 | |
| sData[kOffset / sizeof(uint32_t) + 1] = 20; // Value for dispatch 2 | |
| wgpu::Buffer inBuf = utils::CreateBufferFromData( | |
| device, sData.data(), sData.size() * sizeof(uint32_t), wgpu::BufferUsage::Storage); | |
| wgpu::Buffer outBuf = utils::CreateBufferFromData( | |
| device, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, {0u, 0u}); | |
| wgpu::BindGroup bg = utils::MakeBindGroup(device, bgl, | |
| { | |
| {0, inBuf, 0, kBoundElements * sizeof(uint32_t)}, | |
| {1, outBuf}, | |
| }); | |
| uint32_t imm = 42; | |
| uint32_t offset0 = 0; | |
| uint32_t offset1 = kOffset; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| pass.SetPipeline(pipeline); | |
| pass.SetImmediates(0, &imm, sizeof(imm)); | |
| pass.SetBindGroup(0, bg, 1, &offset0); | |
| pass.DispatchWorkgroups(1); | |
| // Second dispatch updates ONLY dynamic offsets on the same BindGroup. | |
| pass.SetBindGroup(0, bg, 1, &offset1); | |
| pass.DispatchWorkgroups(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| std::array<uint32_t, 2> expected = { | |
| 42 + 10 + 6, | |
| 42 + 20 + 6, | |
| }; | |
| EXPECT_BUFFER_U32_RANGE_EQ(expected.data(), outBuf, 0, 2); | |
| } | |
| DAWN_INSTANTIATE_TEST(ImmediateDataTests, | |
| D3D11Backend(), | |
| D3D12Backend(), | |
| MetalBackend(), | |
| OpenGLESBackend(), | |
| VulkanBackend(), | |
| WebGPUBackend()); | |
| // f16 immediates are tested in their own suite so it can require the shader-f16 feature and be | |
| // instantiated only on backends that can enable it (on D3D12 that means DXC). | |
| class ImmediateDataF16Tests : public DawnTest { | |
| protected: | |
| std::vector<wgpu::FeatureName> GetRequiredFeatures() override { | |
| if (SupportsFeatures({wgpu::FeatureName::ShaderF16})) { | |
| return {wgpu::FeatureName::ShaderF16}; | |
| } | |
| return {}; | |
| } | |
| void SetUp() override { | |
| DawnTest::SetUp(); | |
| // GetRequiredFeatures only requests shader-f16 when the adapter advertises it, but the | |
| // device can still be created without it: on D3D12 the adapter reports support even when | |
| // the pipeline uses FXC, while enabling the feature requires DXC, so device creation drops | |
| // it. Skip when the feature did not make it onto the device. This runs per test because | |
| // DawnTest recreates the device for each test case. | |
| DAWN_TEST_UNSUPPORTED_IF(!device.HasFeature(wgpu::FeatureName::ShaderF16)); | |
| } | |
| }; | |
| // Authoritative check that Tint's f16 immediate member byte offsets agree with the bytes Dawn | |
| // uploads via SetImmediates. Three f16 members sit at byte offsets 0/2/4, so the last of the two | |
| // uint32_t slots is only partially used, exercising the padded-tail case. | |
| TEST_P(ImmediateDataF16Tests, Vec3) { | |
| wgpu::ShaderModule module = utils::CreateShaderModule(device, R"( | |
| enable f16; | |
| struct Immediate { | |
| a: f16, | |
| b: f16, | |
| c: f16, | |
| }; | |
| var<immediate> imm: Immediate; | |
| @group(0) @binding(0) var<storage, read_write> output : vec3f; | |
| @compute @workgroup_size(1, 1, 1) | |
| fn csMain() { | |
| output = vec3f(f32(imm.a), f32(imm.b), f32(imm.c)); | |
| })"); | |
| wgpu::BufferDescriptor bufferDesc; | |
| bufferDesc.size = sizeof(float) * 3; | |
| bufferDesc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::Storage; | |
| wgpu::Buffer storageBuffer = device.CreateBuffer(&bufferDesc); | |
| wgpu::ComputePipelineDescriptor csDesc; | |
| csDesc.compute.module = module; | |
| wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&csDesc); | |
| wgpu::BindGroup bindGroup = | |
| utils::MakeBindGroup(device, pipeline.GetBindGroupLayout(0), {{0, storageBuffer}}); | |
| // These values are exactly representable in f16 but carry dense mantissa bits and mixed signs, | |
| // so they catch bit-level packing errors as well as offset/stride/ordering errors. Packed | |
| // contiguously, the three uint16_t values occupy byte offsets 0/2/4 -- matching the WGSL struct | |
| // member offsets. | |
| std::array<float, 3> values = {1.5f, -2.25f, 3.75f}; | |
| std::array<uint16_t, 4> packed = { | |
| Float32ToFloat16(values[0]), | |
| Float32ToFloat16(values[1]), | |
| Float32ToFloat16(values[2]), | |
| 0, | |
| }; | |
| wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass(); | |
| computePassEncoder.SetPipeline(pipeline); | |
| computePassEncoder.SetImmediates(0, packed.data(), packed.size() * sizeof(uint16_t)); | |
| computePassEncoder.SetBindGroup(0, bindGroup); | |
| computePassEncoder.DispatchWorkgroups(1); | |
| computePassEncoder.End(); | |
| wgpu::CommandBuffer commands = commandEncoder.Finish(); | |
| queue.Submit(1, &commands); | |
| EXPECT_BUFFER_FLOAT_RANGE_EQ(values.data(), storageBuffer, 0, values.size()); | |
| } | |
| DAWN_INSTANTIATE_TEST(ImmediateDataF16Tests, | |
| D3D12Backend({"use_dxc"}), | |
| MetalBackend(), | |
| VulkanBackend()); | |
| class ImmediateDataWithResourceTableTests : public DawnTest { | |
| protected: | |
| void SetUp() override { | |
| DawnTest::SetUp(); | |
| DAWN_TEST_UNSUPPORTED_IF( | |
| !SupportsFeatures({wgpu::FeatureName::ChromiumExperimentalSamplingResourceTable})); | |
| // TODO(https://issues.chromium.org/435317394): The Subzero compiler used by Swiftshader | |
| // produces bad code and crashes on some VK_EXT_descriptor_indexing workloads. Skip tests on | |
| // it, but still run them with Swiftshader LLVM 10.0. On ARM64 the only supported compiler | |
| // is LLVM10.0 so use that signal to choose when Swiftshader can be tested. | |
| DAWN_SUPPRESS_TEST_IF(IsSwiftshader() && !DAWN_PLATFORM_IS(ARM64)); | |
| } | |
| std::vector<wgpu::FeatureName> GetRequiredFeatures() override { | |
| if (SupportsFeatures({wgpu::FeatureName::ChromiumExperimentalSamplingResourceTable})) { | |
| return {wgpu::FeatureName::ChromiumExperimentalSamplingResourceTable}; | |
| } | |
| return {}; | |
| } | |
| wgpu::PipelineLayout MakePipelineLayoutWithTable(std::vector<wgpu::BindGroupLayout> bgls, | |
| uint32_t immediateSize) { | |
| wgpu::PipelineLayoutResourceTable plTable; | |
| plTable.usesResourceTable = true; | |
| wgpu::PipelineLayoutDescriptor desc{ | |
| .nextInChain = &plTable, | |
| .bindGroupLayoutCount = bgls.size(), | |
| .bindGroupLayouts = bgls.data(), | |
| .immediateSize = immediateSize, | |
| }; | |
| return device.CreatePipelineLayout(&desc); | |
| } | |
| }; | |
| // Resource table (VkDescriptorSet 0) must be rebound when push constant range changes | |
| // between pipelines, even if the resource table itself doesn't change. | |
| // The VVL will complain if the resource table is not correctly rebound (with | |
| // --enable-backend-validation). | |
| TEST_P(ImmediateDataWithResourceTableTests, ResourceTableReboundOnDifferentImmediateSize) { | |
| // TODO(473444514): Re-enable when compute shaders support ResourceTable on metal | |
| DAWN_SUPPRESS_TEST_IF(IsMetal()); | |
| wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( | |
| device, {{0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}}); | |
| wgpu::PipelineLayout layout4Bytes = MakePipelineLayoutWithTable({bgl}, 4); | |
| wgpu::PipelineLayout layout16Bytes = MakePipelineLayoutWithTable({bgl}, 16); | |
| wgpu::ShaderModule module4Bytes = utils::CreateShaderModule(device, R"( | |
| enable chromium_experimental_resource_table; | |
| var<immediate> imm: u32; | |
| @group(0) @binding(0) var<storage, read_write> output: u32; | |
| @compute @workgroup_size(1) fn main() { | |
| output = imm; | |
| } | |
| )"); | |
| wgpu::ShaderModule module16Bytes = utils::CreateShaderModule(device, R"( | |
| enable chromium_experimental_resource_table; | |
| var<immediate> imm: vec4u; | |
| @group(0) @binding(0) var<storage, read_write> output: u32; | |
| @compute @workgroup_size(1) fn main() { | |
| output = imm.x; | |
| } | |
| )"); | |
| wgpu::ComputePipelineDescriptor csDesc4Bytes; | |
| csDesc4Bytes.compute.module = module4Bytes; | |
| csDesc4Bytes.layout = layout4Bytes; | |
| wgpu::ComputePipeline pipeline4Bytes = device.CreateComputePipeline(&csDesc4Bytes); | |
| wgpu::ComputePipelineDescriptor csDesc16Bytes; | |
| csDesc16Bytes.compute.module = module16Bytes; | |
| csDesc16Bytes.layout = layout16Bytes; | |
| wgpu::ComputePipeline pipeline16Bytes = device.CreateComputePipeline(&csDesc16Bytes); | |
| wgpu::BufferDescriptor bufDesc = { | |
| .usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, | |
| .size = sizeof(uint32_t), | |
| }; | |
| wgpu::Buffer resultBuffer = device.CreateBuffer(&bufDesc); | |
| wgpu::BindGroup bg = utils::MakeBindGroup(device, bgl, {{0, resultBuffer}}); | |
| // The resource table just needs to exist, and be present in the pipeline layout, to activate | |
| // the resource table code path in the backend. Its contents don't matter — we're testing that | |
| // the resource table descriptor set gets rebound when push constant ranges change between | |
| // pipelines. | |
| wgpu::ResourceTableDescriptor rtDesc; | |
| rtDesc.size = 1; | |
| wgpu::ResourceTable table = device.CreateResourceTable(&rtDesc); | |
| uint32_t imm4Bytes = 100; | |
| std::array<uint32_t, 4> imm16Bytes = {200, 0, 0, 0}; | |
| wgpu::CommandEncoder enc = device.CreateCommandEncoder(); | |
| wgpu::ComputePassEncoder pass = enc.BeginComputePass(); | |
| pass.SetResourceTable(table); | |
| pass.SetBindGroup(0, bg); | |
| // Dispatch with pipeline4Bytes, then switch to pipeline16Bytes (different immediate size). | |
| // If the resource table is not rebound, the VVL should report an error. | |
| pass.SetImmediates(0, &imm4Bytes, sizeof(imm4Bytes)); | |
| pass.SetPipeline(pipeline4Bytes); | |
| pass.DispatchWorkgroups(1); | |
| pass.SetImmediates(0, imm16Bytes.data(), sizeof(imm16Bytes)); | |
| pass.SetPipeline(pipeline16Bytes); | |
| pass.DispatchWorkgroups(1); | |
| pass.End(); | |
| wgpu::CommandBuffer commands = enc.Finish(); | |
| queue.Submit(1, &commands); | |
| // Pipeline B writes imm.x = 200 to output. | |
| uint32_t expected = 200; | |
| EXPECT_BUFFER_U32_EQ(expected, resultBuffer, 0); | |
| } | |
| DAWN_INSTANTIATE_TEST(ImmediateDataWithResourceTableTests, | |
| D3D12Backend(), | |
| MetalBackend(), | |
| VulkanBackend()); | |
| } // anonymous namespace | |
| } // namespace dawn |