-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumBufferSizeValidationTests.cpp
587 lines (502 loc) · 27.3 KB
/
MinimumBufferSizeValidationTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright 2020 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "tests/unittests/validation/ValidationTest.h"
#include "common/Assert.h"
#include "common/Constants.h"
#include "utils/ComboRenderPipelineDescriptor.h"
#include "utils/WGPUHelpers.h"
namespace {
// Helper for describing bindings throughout the tests
struct BindingDescriptor {
uint32_t set;
uint32_t binding;
std::string text;
uint64_t size;
wgpu::BindingType type = wgpu::BindingType::StorageBuffer;
wgpu::ShaderStage visibility = wgpu::ShaderStage::Compute | wgpu::ShaderStage::Fragment;
};
// Runs |func| with a modified version of |originalSizes| as an argument, adding |offset| to
// each element one at a time This is useful to verify some behavior happens if any element is
// offset from original
template <typename F>
void WithEachSizeOffsetBy(int64_t offset, const std::vector<uint64_t>& originalSizes, F func) {
std::vector<uint64_t> modifiedSizes = originalSizes;
for (size_t i = 0; i < originalSizes.size(); ++i) {
if (offset < 0) {
ASSERT(originalSizes[i] >= static_cast<uint64_t>(-offset));
}
// Run the function with an element offset, and restore element afterwards
modifiedSizes[i] += offset;
func(modifiedSizes);
modifiedSizes[i] -= offset;
}
}
// Runs |func| with |correctSizes|, and an expectation of success and failure
template <typename F>
void CheckSizeBounds(const std::vector<uint64_t>& correctSizes, F func) {
// To validate size:
// Check invalid with bind group with one less
// Check valid with bind group with correct size
// Make sure (every size - 1) produces an error
WithEachSizeOffsetBy(-1, correctSizes,
[&](const std::vector<uint64_t>& sizes) { func(sizes, false); });
// Make sure correct sizes work
func(correctSizes, true);
// Make sure (every size + 1) works
WithEachSizeOffsetBy(1, correctSizes,
[&](const std::vector<uint64_t>& sizes) { func(sizes, true); });
}
// Convert binding type to a glsl string
std::string BindingTypeToStr(wgpu::BindingType type) {
switch (type) {
case wgpu::BindingType::UniformBuffer:
return "uniform";
case wgpu::BindingType::StorageBuffer:
return "buffer";
case wgpu::BindingType::ReadonlyStorageBuffer:
return "readonly buffer";
default:
UNREACHABLE();
return "";
}
}
// Creates a bind group with given bindings for shader text
std::string GenerateBindingString(const std::string& layout,
const std::vector<BindingDescriptor>& bindings) {
std::ostringstream ostream;
size_t ctr = 0;
for (const BindingDescriptor& b : bindings) {
ostream << "layout(" << layout << ", set = " << b.set << ", binding = " << b.binding
<< ") " << BindingTypeToStr(b.type) << " b" << ctr++ << "{\n"
<< b.text << ";\n};\n";
}
return ostream.str();
}
// Used for adding custom types available throughout the tests
static const std::string kStructs = "struct ThreeFloats{float f1; float f2; float f3;};\n";
// Creates a compute shader with given bindings
std::string CreateComputeShaderWithBindings(const std::string& layoutType,
const std::vector<BindingDescriptor>& bindings) {
return R"(
#version 450
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
)" +
kStructs + GenerateBindingString(layoutType, bindings) + "void main() {}";
}
// Creates a vertex shader with given bindings
std::string CreateVertexShaderWithBindings(const std::string& layoutType,
const std::vector<BindingDescriptor>& bindings) {
return "#version 450\n" + kStructs + GenerateBindingString(layoutType, bindings) +
"void main() {}";
}
// Creates a fragment shader with given bindings
std::string CreateFragmentShaderWithBindings(const std::string& layoutType,
const std::vector<BindingDescriptor>& bindings) {
return R"(
#version 450
layout(location = 0) out vec4 fragColor;
)" +
kStructs + GenerateBindingString(layoutType, bindings) + "void main() {}";
}
// Concatenates vectors containing BindingDescriptor
std::vector<BindingDescriptor> CombineBindings(
std::initializer_list<std::vector<BindingDescriptor>> bindings) {
std::vector<BindingDescriptor> result;
for (const std::vector<BindingDescriptor>& b : bindings) {
result.insert(result.end(), b.begin(), b.end());
}
return result;
}
} // namespace
class MinBufferSizeTestsBase : public ValidationTest {
public:
void SetUp() override {
ValidationTest::SetUp();
}
wgpu::Buffer CreateBuffer(uint64_t bufferSize, wgpu::BufferUsage usage) {
wgpu::BufferDescriptor bufferDescriptor;
bufferDescriptor.size = bufferSize;
bufferDescriptor.usage = usage;
return device.CreateBuffer(&bufferDescriptor);
}
// Creates compute pipeline given a layout and shader
wgpu::ComputePipeline CreateComputePipeline(const std::vector<wgpu::BindGroupLayout>& layouts,
const std::string& shader) {
wgpu::ShaderModule csModule =
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, shader.c_str());
wgpu::ComputePipelineDescriptor csDesc;
csDesc.layout = nullptr;
if (!layouts.empty()) {
wgpu::PipelineLayoutDescriptor descriptor;
descriptor.bindGroupLayoutCount = layouts.size();
descriptor.bindGroupLayouts = layouts.data();
csDesc.layout = device.CreatePipelineLayout(&descriptor);
}
csDesc.computeStage.module = csModule;
csDesc.computeStage.entryPoint = "main";
return device.CreateComputePipeline(&csDesc);
}
// Creates compute pipeline with default layout
wgpu::ComputePipeline CreateComputePipelineWithDefaultLayout(const std::string& shader) {
return CreateComputePipeline({}, shader);
}
// Creates render pipeline give na layout and shaders
wgpu::RenderPipeline CreateRenderPipeline(const std::vector<wgpu::BindGroupLayout>& layouts,
const std::string& vertexShader,
const std::string& fragShader) {
wgpu::ShaderModule vsModule = utils::CreateShaderModule(
device, utils::SingleShaderStage::Vertex, vertexShader.c_str());
wgpu::ShaderModule fsModule = utils::CreateShaderModule(
device, utils::SingleShaderStage::Fragment, fragShader.c_str());
utils::ComboRenderPipelineDescriptor pipelineDescriptor(device);
pipelineDescriptor.vertexStage.module = vsModule;
pipelineDescriptor.cFragmentStage.module = fsModule;
pipelineDescriptor.layout = nullptr;
if (!layouts.empty()) {
wgpu::PipelineLayoutDescriptor descriptor;
descriptor.bindGroupLayoutCount = layouts.size();
descriptor.bindGroupLayouts = layouts.data();
pipelineDescriptor.layout = device.CreatePipelineLayout(&descriptor);
}
return device.CreateRenderPipeline(&pipelineDescriptor);
}
// Creates render pipeline with default layout
wgpu::RenderPipeline CreateRenderPipelineWithDefaultLayout(const std::string& vertexShader,
const std::string& fragShader) {
return CreateRenderPipeline({}, vertexShader, fragShader);
}
// Creates bind group layout with given minimum sizes for each binding
wgpu::BindGroupLayout CreateBindGroupLayout(const std::vector<BindingDescriptor>& bindings,
const std::vector<uint64_t>& minimumSizes) {
ASSERT(bindings.size() == minimumSizes.size());
std::vector<wgpu::BindGroupLayoutEntry> entries;
for (size_t i = 0; i < bindings.size(); ++i) {
const BindingDescriptor& b = bindings[i];
wgpu::BindGroupLayoutEntry e = {};
e.binding = b.binding;
e.type = b.type;
e.visibility = b.visibility;
e.minBufferBindingSize = minimumSizes[i];
entries.push_back(e);
}
wgpu::BindGroupLayoutDescriptor descriptor;
descriptor.entryCount = static_cast<uint32_t>(entries.size());
descriptor.entries = entries.data();
return device.CreateBindGroupLayout(&descriptor);
}
// Extract the first bind group from a compute shader
wgpu::BindGroupLayout GetBGLFromComputeShader(const std::string& shader, uint32_t index) {
wgpu::ComputePipeline pipeline = CreateComputePipelineWithDefaultLayout(shader);
return pipeline.GetBindGroupLayout(index);
}
// Extract the first bind group from a render pass
wgpu::BindGroupLayout GetBGLFromRenderShaders(const std::string& vertexShader,
const std::string& fragShader,
uint32_t index) {
wgpu::RenderPipeline pipeline =
CreateRenderPipelineWithDefaultLayout(vertexShader, fragShader);
return pipeline.GetBindGroupLayout(index);
}
// Create a bind group with given binding sizes for each entry (backed by the same buffer)
wgpu::BindGroup CreateBindGroup(wgpu::BindGroupLayout layout,
const std::vector<BindingDescriptor>& bindings,
const std::vector<uint64_t>& bindingSizes) {
ASSERT(bindings.size() == bindingSizes.size());
wgpu::Buffer buffer =
CreateBuffer(1024, wgpu::BufferUsage::Uniform | wgpu::BufferUsage::Storage);
std::vector<wgpu::BindGroupEntry> entries;
entries.reserve(bindingSizes.size());
for (uint32_t i = 0; i < bindingSizes.size(); ++i) {
wgpu::BindGroupEntry entry = {};
entry.binding = bindings[i].binding;
entry.buffer = buffer;
ASSERT(bindingSizes[i] < 1024);
entry.size = bindingSizes[i];
entries.push_back(entry);
}
wgpu::BindGroupDescriptor descriptor;
descriptor.layout = layout;
descriptor.entryCount = entries.size();
descriptor.entries = entries.data();
return device.CreateBindGroup(&descriptor);
}
// Runs a single dispatch with given pipeline and bind group (to test lazy validation during
// dispatch)
void TestDispatch(const wgpu::ComputePipeline& computePipeline,
const std::vector<wgpu::BindGroup>& bindGroups,
bool expectation) {
wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
wgpu::ComputePassEncoder computePassEncoder = commandEncoder.BeginComputePass();
computePassEncoder.SetPipeline(computePipeline);
for (size_t i = 0; i < bindGroups.size(); ++i) {
computePassEncoder.SetBindGroup(i, bindGroups[i]);
}
computePassEncoder.Dispatch(1);
computePassEncoder.EndPass();
if (!expectation) {
ASSERT_DEVICE_ERROR(commandEncoder.Finish());
} else {
commandEncoder.Finish();
}
}
// Runs a single draw with given pipeline and bind group (to test lazy validation during draw)
void TestDraw(const wgpu::RenderPipeline& renderPipeline,
const std::vector<wgpu::BindGroup>& bindGroups,
bool expectation) {
DummyRenderPass renderPass(device);
wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder renderPassEncoder = commandEncoder.BeginRenderPass(&renderPass);
renderPassEncoder.SetPipeline(renderPipeline);
for (size_t i = 0; i < bindGroups.size(); ++i) {
renderPassEncoder.SetBindGroup(i, bindGroups[i]);
}
renderPassEncoder.Draw(3);
renderPassEncoder.EndPass();
if (!expectation) {
ASSERT_DEVICE_ERROR(commandEncoder.Finish());
} else {
commandEncoder.Finish();
}
}
};
// The check between BGL and pipeline at pipeline creation time
class MinBufferSizePipelineCreationTests : public MinBufferSizeTestsBase {};
// Pipeline can be created if minimum buffer size in layout is specified as 0
TEST_F(MinBufferSizePipelineCreationTests, ZeroMinBufferSize) {
std::vector<BindingDescriptor> bindings = {{0, 0, "float a; float b", 8}, {0, 1, "float c", 4}};
std::string computeShader = CreateComputeShaderWithBindings("std140", bindings);
std::string vertexShader = CreateVertexShaderWithBindings("std140", {});
std::string fragShader = CreateFragmentShaderWithBindings("std140", bindings);
wgpu::BindGroupLayout layout = CreateBindGroupLayout(bindings, {0, 0});
CreateRenderPipeline({layout}, vertexShader, fragShader);
CreateComputePipeline({layout}, computeShader);
}
// Fail if layout given has non-zero minimum sizes smaller than shader requirements
TEST_F(MinBufferSizePipelineCreationTests, LayoutSizesTooSmall) {
std::vector<BindingDescriptor> bindings = {{0, 0, "float a; float b", 8}, {0, 1, "float c", 4}};
std::string computeShader = CreateComputeShaderWithBindings("std140", bindings);
std::string vertexShader = CreateVertexShaderWithBindings("std140", {});
std::string fragShader = CreateFragmentShaderWithBindings("std140", bindings);
CheckSizeBounds({8, 4}, [&](const std::vector<uint64_t>& sizes, bool expectation) {
wgpu::BindGroupLayout layout = CreateBindGroupLayout(bindings, sizes);
if (expectation) {
CreateRenderPipeline({layout}, vertexShader, fragShader);
CreateComputePipeline({layout}, computeShader);
} else {
ASSERT_DEVICE_ERROR(CreateRenderPipeline({layout}, vertexShader, fragShader));
ASSERT_DEVICE_ERROR(CreateComputePipeline({layout}, computeShader));
}
});
}
// Fail if layout given has non-zero minimum sizes smaller than shader requirements
TEST_F(MinBufferSizePipelineCreationTests, LayoutSizesTooSmallMultipleGroups) {
std::vector<BindingDescriptor> bg0Bindings = {{0, 0, "float a; float b", 8},
{0, 1, "float c", 4}};
std::vector<BindingDescriptor> bg1Bindings = {{1, 0, "float d; float e; float f", 12},
{1, 1, "mat2 g", 32}};
std::vector<BindingDescriptor> bindings = CombineBindings({bg0Bindings, bg1Bindings});
std::string computeShader = CreateComputeShaderWithBindings("std140", bindings);
std::string vertexShader = CreateVertexShaderWithBindings("std140", {});
std::string fragShader = CreateFragmentShaderWithBindings("std140", bindings);
CheckSizeBounds({8, 4, 12, 32}, [&](const std::vector<uint64_t>& sizes, bool expectation) {
wgpu::BindGroupLayout layout0 = CreateBindGroupLayout(bg0Bindings, {sizes[0], sizes[1]});
wgpu::BindGroupLayout layout1 = CreateBindGroupLayout(bg1Bindings, {sizes[2], sizes[3]});
if (expectation) {
CreateRenderPipeline({layout0, layout1}, vertexShader, fragShader);
CreateComputePipeline({layout0, layout1}, computeShader);
} else {
ASSERT_DEVICE_ERROR(CreateRenderPipeline({layout0, layout1}, vertexShader, fragShader));
ASSERT_DEVICE_ERROR(CreateComputePipeline({layout0, layout1}, computeShader));
}
});
}
// The check between the BGL and the bindings at bindgroup creation time
class MinBufferSizeBindGroupCreationTests : public MinBufferSizeTestsBase {};
// Fail if a binding is smaller than minimum buffer size
TEST_F(MinBufferSizeBindGroupCreationTests, BindingTooSmall) {
std::vector<BindingDescriptor> bindings = {{0, 0, "float a; float b", 8}, {0, 1, "float c", 4}};
wgpu::BindGroupLayout layout = CreateBindGroupLayout(bindings, {8, 4});
CheckSizeBounds({8, 4}, [&](const std::vector<uint64_t>& sizes, bool expectation) {
if (expectation) {
CreateBindGroup(layout, bindings, sizes);
} else {
ASSERT_DEVICE_ERROR(CreateBindGroup(layout, bindings, sizes));
}
});
}
// Check two layouts with different minimum size are unequal
TEST_F(MinBufferSizeBindGroupCreationTests, LayoutEquality) {
auto MakeLayout = [&](uint64_t size) {
return utils::MakeBindGroupLayout(
device, {{0, wgpu::ShaderStage::Compute, wgpu::BindingType::UniformBuffer, false, false,
wgpu::TextureViewDimension::Undefined, wgpu::TextureComponentType::Float,
wgpu::TextureFormat::Undefined, size}});
};
EXPECT_EQ(MakeLayout(0).Get(), MakeLayout(0).Get());
EXPECT_NE(MakeLayout(0).Get(), MakeLayout(4).Get());
}
// The check between the bindgroup binding sizes and the required pipeline sizes at draw time
class MinBufferSizeDrawTimeValidationTests : public MinBufferSizeTestsBase {};
// Fail if binding sizes are too small at draw time
TEST_F(MinBufferSizeDrawTimeValidationTests, ZeroMinSizeAndTooSmallBinding) {
std::vector<BindingDescriptor> bindings = {{0, 0, "float a; float b", 8}, {0, 1, "float c", 4}};
std::string computeShader = CreateComputeShaderWithBindings("std140", bindings);
std::string vertexShader = CreateVertexShaderWithBindings("std140", {});
std::string fragShader = CreateFragmentShaderWithBindings("std140", bindings);
wgpu::BindGroupLayout layout = CreateBindGroupLayout(bindings, {0, 0});
wgpu::ComputePipeline computePipeline = CreateComputePipeline({layout}, computeShader);
wgpu::RenderPipeline renderPipeline = CreateRenderPipeline({layout}, vertexShader, fragShader);
CheckSizeBounds({8, 4}, [&](const std::vector<uint64_t>& sizes, bool expectation) {
wgpu::BindGroup bindGroup = CreateBindGroup(layout, bindings, sizes);
TestDispatch(computePipeline, {bindGroup}, expectation);
TestDraw(renderPipeline, {bindGroup}, expectation);
});
}
// Draw time validation works for non-contiguous bindings
TEST_F(MinBufferSizeDrawTimeValidationTests, UnorderedBindings) {
std::vector<BindingDescriptor> bindings = {{0, 2, "float a; float b", 8},
{0, 0, "float c", 4},
{0, 4, "float d; float e; float f", 12}};
std::string computeShader = CreateComputeShaderWithBindings("std140", bindings);
std::string vertexShader = CreateVertexShaderWithBindings("std140", {});
std::string fragShader = CreateFragmentShaderWithBindings("std140", bindings);
wgpu::BindGroupLayout layout = CreateBindGroupLayout(bindings, {0, 0, 0});
wgpu::ComputePipeline computePipeline = CreateComputePipeline({layout}, computeShader);
wgpu::RenderPipeline renderPipeline = CreateRenderPipeline({layout}, vertexShader, fragShader);
CheckSizeBounds({8, 4, 12}, [&](const std::vector<uint64_t>& sizes, bool expectation) {
wgpu::BindGroup bindGroup = CreateBindGroup(layout, bindings, sizes);
TestDispatch(computePipeline, {bindGroup}, expectation);
TestDraw(renderPipeline, {bindGroup}, expectation);
});
}
// Draw time validation works for multiple bind groups
TEST_F(MinBufferSizeDrawTimeValidationTests, MultipleGroups) {
std::vector<BindingDescriptor> bg0Bindings = {{0, 0, "float a; float b", 8},
{0, 1, "float c", 4}};
std::vector<BindingDescriptor> bg1Bindings = {{1, 0, "float d; float e; float f", 12},
{1, 1, "mat2 g", 32}};
std::vector<BindingDescriptor> bindings = CombineBindings({bg0Bindings, bg1Bindings});
std::string computeShader = CreateComputeShaderWithBindings("std140", bindings);
std::string vertexShader = CreateVertexShaderWithBindings("std140", {});
std::string fragShader = CreateFragmentShaderWithBindings("std140", bindings);
wgpu::BindGroupLayout layout0 = CreateBindGroupLayout(bg0Bindings, {0, 0});
wgpu::BindGroupLayout layout1 = CreateBindGroupLayout(bg1Bindings, {0, 0});
wgpu::ComputePipeline computePipeline =
CreateComputePipeline({layout0, layout1}, computeShader);
wgpu::RenderPipeline renderPipeline =
CreateRenderPipeline({layout0, layout1}, vertexShader, fragShader);
CheckSizeBounds({8, 4, 12, 32}, [&](const std::vector<uint64_t>& sizes, bool expectation) {
wgpu::BindGroup bindGroup0 = CreateBindGroup(layout0, bg0Bindings, {sizes[0], sizes[1]});
wgpu::BindGroup bindGroup1 = CreateBindGroup(layout0, bg0Bindings, {sizes[2], sizes[3]});
TestDispatch(computePipeline, {bindGroup0, bindGroup1}, expectation);
TestDraw(renderPipeline, {bindGroup0, bindGroup1}, expectation);
});
}
// The correctness of minimum buffer size for the defaulted layout for a pipeline
class MinBufferSizeDefaultLayoutTests : public MinBufferSizeTestsBase {
public:
// Checks BGL |layout| has minimum buffer sizes equal to sizes in |bindings|
void CheckLayoutBindingSizeValidation(const wgpu::BindGroupLayout& layout,
const std::vector<BindingDescriptor>& bindings) {
std::vector<uint64_t> correctSizes;
correctSizes.reserve(bindings.size());
for (const BindingDescriptor& b : bindings) {
correctSizes.push_back(b.size);
}
CheckSizeBounds(correctSizes, [&](const std::vector<uint64_t>& sizes, bool expectation) {
if (expectation) {
CreateBindGroup(layout, bindings, sizes);
} else {
ASSERT_DEVICE_ERROR(CreateBindGroup(layout, bindings, sizes));
}
});
}
// Constructs shaders with given layout type and bindings, checking defaulted sizes match sizes
// in |bindings|
void CheckShaderBindingSizeReflection(
const std::string& layoutType,
std::initializer_list<std::vector<BindingDescriptor>> bindings) {
std::vector<BindingDescriptor> combinedBindings = CombineBindings(bindings);
std::string computeShader = CreateComputeShaderWithBindings(layoutType, combinedBindings);
std::string vertexShader = CreateVertexShaderWithBindings(layoutType, {});
std::string fragShader = CreateFragmentShaderWithBindings(layoutType, combinedBindings);
size_t i = 0;
for (const std::vector<BindingDescriptor>& b : bindings) {
wgpu::BindGroupLayout computeLayout = GetBGLFromComputeShader(computeShader, i);
wgpu::BindGroupLayout renderLayout =
GetBGLFromRenderShaders(vertexShader, fragShader, i);
CheckLayoutBindingSizeValidation(computeLayout, b);
CheckLayoutBindingSizeValidation(renderLayout, b);
++i;
}
}
};
// Various bindings in std140 have correct minimum size reflection
TEST_F(MinBufferSizeDefaultLayoutTests, std140Inferred) {
CheckShaderBindingSizeReflection("std140", {{{0, 0, "float a", 4},
{0, 1, "float b[]", 16},
{0, 2, "mat2 c", 32},
{0, 3, "int d; float e[]", 32},
{0, 4, "ThreeFloats f", 12},
{0, 5, "ThreeFloats g[]", 16}}});
}
// Various bindings in std430 have correct minimum size reflection
TEST_F(MinBufferSizeDefaultLayoutTests, std430Inferred) {
CheckShaderBindingSizeReflection("std430", {{{0, 0, "float a", 4},
{0, 1, "float b[]", 4},
{0, 2, "mat2 c", 16},
{0, 3, "int d; float e[]", 8},
{0, 4, "ThreeFloats f", 12},
{0, 5, "ThreeFloats g[]", 12}}});
}
// Sizes are inferred for all binding types with std140 layout
TEST_F(MinBufferSizeDefaultLayoutTests, std140BindingTypes) {
CheckShaderBindingSizeReflection(
"std140", {{{0, 0, "int d; float e[]", 32, wgpu::BindingType::UniformBuffer},
{0, 1, "ThreeFloats f", 12, wgpu::BindingType::StorageBuffer},
{0, 2, "ThreeFloats g[]", 16, wgpu::BindingType::ReadonlyStorageBuffer}}});
}
// Sizes are inferred for all binding types with std430 layout
TEST_F(MinBufferSizeDefaultLayoutTests, std430BindingTypes) {
CheckShaderBindingSizeReflection(
"std430", {{{0, 0, "float a", 4, wgpu::BindingType::StorageBuffer},
{0, 1, "ThreeFloats b[]", 12, wgpu::BindingType::ReadonlyStorageBuffer}}});
}
// Various bindings have correct size across multiple groups
TEST_F(MinBufferSizeDefaultLayoutTests, std140MultipleBindGroups) {
CheckShaderBindingSizeReflection("std140",
{{{0, 0, "float a", 4}, {0, 1, "float b[]", 16}},
{{1, 2, "mat2 c", 32}, {1, 3, "int d; float e[]", 32}},
{{2, 4, "ThreeFloats f", 12}},
{{3, 5, "ThreeFloats g[]", 16}}});
}
// Various bindings have correct size across multiple groups
TEST_F(MinBufferSizeDefaultLayoutTests, std430MultipleBindGroups) {
CheckShaderBindingSizeReflection("std430",
{{{0, 0, "float a", 4}, {0, 1, "float b[]", 4}},
{{1, 2, "mat2 c", 16}, {1, 3, "int d; float e[]", 8}},
{{2, 4, "ThreeFloats f", 12}},
{{3, 5, "ThreeFloats g[]", 12}}});
}
// Minimum size should be the max requirement of both vertex and fragment stages
TEST_F(MinBufferSizeDefaultLayoutTests, RenderPassConsidersBothStages) {
std::string vertexShader = CreateVertexShaderWithBindings(
"std140", {{0, 0, "float a", 4, wgpu::BindingType::UniformBuffer},
{0, 1, "float b[]", 16, wgpu::BindingType::UniformBuffer}});
std::string fragShader = CreateFragmentShaderWithBindings(
"std140", {{0, 0, "float a; float b", 8, wgpu::BindingType::UniformBuffer},
{0, 1, "float c; float d", 8, wgpu::BindingType::UniformBuffer}});
wgpu::BindGroupLayout renderLayout = GetBGLFromRenderShaders(vertexShader, fragShader, 0);
CheckLayoutBindingSizeValidation(renderLayout, {{0, 0, "", 8}, {0, 1, "", 16}});
}