• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "tests/DawnTest.h"
16 
17 #include "common/Assert.h"
18 #include "utils/ComboRenderPipelineDescriptor.h"
19 #include "utils/WGPUHelpers.h"
20 
21 class OpArrayLengthTest : public DawnTest {
22   protected:
SetUp()23     void SetUp() {
24         DawnTest::SetUp();
25 
26         // Create buffers of various size to check the length() implementation
27         wgpu::BufferDescriptor bufferDesc;
28         bufferDesc.size = 4;
29         bufferDesc.usage = wgpu::BufferUsage::Storage;
30         mStorageBuffer4 = device.CreateBuffer(&bufferDesc);
31 
32         bufferDesc.size = 256;
33         mStorageBuffer256 = device.CreateBuffer(&bufferDesc);
34 
35         bufferDesc.size = 512 + 256;
36         mStorageBuffer512 = device.CreateBuffer(&bufferDesc);
37 
38         // Put them all in a bind group for tests to bind them easily.
39         wgpu::ShaderStage kAllStages =
40             wgpu::ShaderStage::Fragment | wgpu::ShaderStage::Vertex | wgpu::ShaderStage::Compute;
41         mBindGroupLayout = utils::MakeBindGroupLayout(
42             device, {{0, kAllStages, wgpu::BufferBindingType::ReadOnlyStorage},
43                      {1, kAllStages, wgpu::BufferBindingType::ReadOnlyStorage},
44                      {2, kAllStages, wgpu::BufferBindingType::ReadOnlyStorage}});
45 
46         mBindGroup = utils::MakeBindGroup(device, mBindGroupLayout,
47                                           {
48                                               {0, mStorageBuffer4, 0, 4},
49                                               {1, mStorageBuffer256, 0, wgpu::kWholeSize},
50                                               {2, mStorageBuffer512, 256, wgpu::kWholeSize},
51                                           });
52 
53         // Common shader code to use these buffers in shaders, assuming they are in bindgroup index
54         // 0.
55         mShaderInterface = R"(
56             [[block]] struct DataBuffer {
57                 data : [[stride(4)]] array<f32>;
58             };
59 
60             // The length should be 1 because the buffer is 4-byte long.
61             [[group(0), binding(0)]] var<storage, read> buffer1 : DataBuffer;
62 
63             // The length should be 64 because the buffer is 256 bytes long.
64             [[group(0), binding(1)]] var<storage, read> buffer2 : DataBuffer;
65 
66             // The length should be (512 - 16*4) / 8 = 56 because the buffer is 512 bytes long
67             // and the structure is 8 bytes big.
68             struct Buffer3Data {
69                 a : f32;
70                 b : i32;
71             };
72 
73             [[block]] struct Buffer3 {
74                 [[size(64)]] garbage : mat4x4<f32>;
75                 data : [[stride(8)]] array<Buffer3Data>;
76             };
77             [[group(0), binding(2)]] var<storage, read> buffer3 : Buffer3;
78         )";
79 
80         // See comments in the shader for an explanation of these values
81         mExpectedLengths = {1, 64, 56};
82     }
83 
84     wgpu::Buffer mStorageBuffer4;
85     wgpu::Buffer mStorageBuffer256;
86     wgpu::Buffer mStorageBuffer512;
87 
88     wgpu::BindGroupLayout mBindGroupLayout;
89     wgpu::BindGroup mBindGroup;
90     std::string mShaderInterface;
91     std::array<uint32_t, 3> mExpectedLengths;
92 };
93 
94 // Test OpArrayLength in the compute stage
TEST_P(OpArrayLengthTest,Compute)95 TEST_P(OpArrayLengthTest, Compute) {
96     // TODO(crbug.com/dawn/197): The computations for length() of unsized buffer is broken on
97     // Nvidia OpenGL.
98     DAWN_SUPPRESS_TEST_IF(IsNvidia() && (IsOpenGL() || IsOpenGLES()));
99 
100     // Create a buffer to hold the result sizes and create a bindgroup for it.
101     wgpu::BufferDescriptor bufferDesc;
102     bufferDesc.usage = wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc;
103     bufferDesc.size = sizeof(uint32_t) * mExpectedLengths.size();
104     wgpu::Buffer resultBuffer = device.CreateBuffer(&bufferDesc);
105 
106     wgpu::BindGroupLayout resultLayout = utils::MakeBindGroupLayout(
107         device, {{0, wgpu::ShaderStage::Compute, wgpu::BufferBindingType::Storage}});
108 
109     wgpu::BindGroup resultBindGroup =
110         utils::MakeBindGroup(device, resultLayout, {{0, resultBuffer, 0, wgpu::kWholeSize}});
111 
112     // Create the compute pipeline that stores the length()s in the result buffer.
113     wgpu::BindGroupLayout bgls[] = {mBindGroupLayout, resultLayout};
114     wgpu::PipelineLayoutDescriptor plDesc;
115     plDesc.bindGroupLayoutCount = 2;
116     plDesc.bindGroupLayouts = bgls;
117     wgpu::PipelineLayout pl = device.CreatePipelineLayout(&plDesc);
118 
119     wgpu::ComputePipelineDescriptor pipelineDesc;
120     pipelineDesc.layout = pl;
121     pipelineDesc.compute.entryPoint = "main";
122     pipelineDesc.compute.module = utils::CreateShaderModule(device, (R"(
123         [[block]] struct ResultBuffer {
124             data : [[stride(4)]] array<u32, 3>;
125         };
126         [[group(1), binding(0)]] var<storage, read_write> result : ResultBuffer;
127         )" + mShaderInterface + R"(
128         [[stage(compute), workgroup_size(1)]] fn main() {
129             result.data[0] = arrayLength(&buffer1.data);
130             result.data[1] = arrayLength(&buffer2.data);
131             result.data[2] = arrayLength(&buffer3.data);
132         })")
133                                                                         .c_str());
134     wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDesc);
135 
136     // Run a single instance of the compute shader
137     wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
138     wgpu::ComputePassEncoder pass = encoder.BeginComputePass();
139     pass.SetPipeline(pipeline);
140     pass.SetBindGroup(0, mBindGroup);
141     pass.SetBindGroup(1, resultBindGroup);
142     pass.Dispatch(1);
143     pass.EndPass();
144 
145     wgpu::CommandBuffer commands = encoder.Finish();
146     queue.Submit(1, &commands);
147 
148     EXPECT_BUFFER_U32_RANGE_EQ(mExpectedLengths.data(), resultBuffer, 0, 3);
149 }
150 
151 // Test OpArrayLength in the fragment stage
TEST_P(OpArrayLengthTest,Fragment)152 TEST_P(OpArrayLengthTest, Fragment) {
153     // TODO(crbug.com/dawn/197): The computations for length() of unsized buffer is broken on
154     // Nvidia OpenGL.
155     DAWN_SUPPRESS_TEST_IF(IsNvidia() && (IsOpenGL() || IsOpenGLES()));
156 
157     utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, 1, 1);
158 
159     // Create the pipeline that computes the length of the buffers and writes it to the only render
160     // pass pixel.
161     wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, R"(
162         [[stage(vertex)]] fn main() -> [[builtin(position)]] vec4<f32> {
163             return vec4<f32>(0.0, 0.0, 0.0, 1.0);
164         })");
165 
166     wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, (mShaderInterface + R"(
167         [[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {
168             var fragColor : vec4<f32>;
169             fragColor.r = f32(arrayLength(&buffer1.data)) / 255.0;
170             fragColor.g = f32(arrayLength(&buffer2.data)) / 255.0;
171             fragColor.b = f32(arrayLength(&buffer3.data)) / 255.0;
172             fragColor.a = 0.0;
173             return fragColor;
174         })")
175                                                                         .c_str());
176 
177     utils::ComboRenderPipelineDescriptor descriptor;
178     descriptor.vertex.module = vsModule;
179     descriptor.cFragment.module = fsModule;
180     descriptor.primitive.topology = wgpu::PrimitiveTopology::PointList;
181     descriptor.cTargets[0].format = renderPass.colorFormat;
182     descriptor.layout = utils::MakeBasicPipelineLayout(device, &mBindGroupLayout);
183     wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&descriptor);
184 
185     // "Draw" the lengths to the texture.
186     wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
187     {
188         wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
189         pass.SetPipeline(pipeline);
190         pass.SetBindGroup(0, mBindGroup);
191         pass.Draw(1);
192         pass.EndPass();
193     }
194 
195     wgpu::CommandBuffer commands = encoder.Finish();
196     queue.Submit(1, &commands);
197 
198     RGBA8 expectedColor = RGBA8(mExpectedLengths[0], mExpectedLengths[1], mExpectedLengths[2], 0);
199     EXPECT_PIXEL_RGBA8_EQ(expectedColor, renderPass.color, 0, 0);
200 }
201 
202 // Test OpArrayLength in the vertex stage
TEST_P(OpArrayLengthTest,Vertex)203 TEST_P(OpArrayLengthTest, Vertex) {
204     // TODO(crbug.com/dawn/197): The computations for length() of unsized buffer is broken on
205     // Nvidia OpenGL. Also failing on all GLES (NV, Intel, SwANGLE).
206     DAWN_SUPPRESS_TEST_IF(IsNvidia() && IsOpenGL());
207     DAWN_SUPPRESS_TEST_IF(IsOpenGLES());
208 
209     utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, 1, 1);
210 
211     // Create the pipeline that computes the length of the buffers and writes it to the only render
212     // pass pixel.
213     wgpu::ShaderModule vsModule = utils::CreateShaderModule(device, (mShaderInterface + R"(
214         struct VertexOut {
215             [[location(0)]] color : vec4<f32>;
216             [[builtin(position)]] position : vec4<f32>;
217         };
218 
219         [[stage(vertex)]] fn main() -> VertexOut {
220             var output : VertexOut;
221             output.color.r = f32(arrayLength(&buffer1.data)) / 255.0;
222             output.color.g = f32(arrayLength(&buffer2.data)) / 255.0;
223             output.color.b = f32(arrayLength(&buffer3.data)) / 255.0;
224             output.color.a = 0.0;
225 
226             output.position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
227             return output;
228         })")
229                                                                         .c_str());
230 
231     wgpu::ShaderModule fsModule = utils::CreateShaderModule(device, R"(
232         [[stage(fragment)]]
233         fn main([[location(0)]] color : vec4<f32>) -> [[location(0)]] vec4<f32> {
234             return color;
235         })");
236 
237     utils::ComboRenderPipelineDescriptor descriptor;
238     descriptor.vertex.module = vsModule;
239     descriptor.cFragment.module = fsModule;
240     descriptor.primitive.topology = wgpu::PrimitiveTopology::PointList;
241     descriptor.cTargets[0].format = renderPass.colorFormat;
242     descriptor.layout = utils::MakeBasicPipelineLayout(device, &mBindGroupLayout);
243     wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&descriptor);
244 
245     // "Draw" the lengths to the texture.
246     wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
247     {
248         wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
249         pass.SetPipeline(pipeline);
250         pass.SetBindGroup(0, mBindGroup);
251         pass.Draw(1);
252         pass.EndPass();
253     }
254 
255     wgpu::CommandBuffer commands = encoder.Finish();
256     queue.Submit(1, &commands);
257 
258     RGBA8 expectedColor = RGBA8(mExpectedLengths[0], mExpectedLengths[1], mExpectedLengths[2], 0);
259     EXPECT_PIXEL_RGBA8_EQ(expectedColor, renderPass.color, 0, 0);
260 }
261 
262 DAWN_INSTANTIATE_TEST(OpArrayLengthTest,
263                       D3D12Backend(),
264                       MetalBackend(),
265                       OpenGLBackend(),
266                       OpenGLESBackend(),
267                       VulkanBackend());
268