Update BindGroupTests to use WGSL

Bug: dawn:572
Change-Id: I1e7cd05ff184f7d26a75006d10b216aea28abb04
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/32513
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Austin Eng 2020-12-23 19:54:30 +00:00 committed by Commit Bot service account
parent b3ab21e1e2
commit 700809a7f7
1 changed files with 271 additions and 200 deletions

View File

@ -45,11 +45,17 @@ class BindGroupTests : public DawnTest {
} }
wgpu::ShaderModule MakeSimpleVSModule() const { wgpu::ShaderModule MakeSimpleVSModule() const {
return utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( return utils::CreateShaderModuleFromWGSL(device, R"(
#version 450 [[builtin(vertex_idx)]] var<in> VertexIndex : u32;
void main() { [[builtin(position)]] var<out> Position : vec4<f32>;
const vec2 pos[3] = vec2[3](vec2(-1.f, 1.f), vec2(1.f, 1.f), vec2(-1.f, -1.f));
gl_Position = vec4(pos[gl_VertexIndex], 0.f, 1.f); [[stage(vertex)]] fn main() -> void {
const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>(-1.0, 1.0),
vec2<f32>( 1.0, 1.0),
vec2<f32>(-1.0, -1.0));
Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
})"); })");
} }
@ -57,43 +63,33 @@ class BindGroupTests : public DawnTest {
ASSERT(bindingTypes.size() <= kMaxBindGroups); ASSERT(bindingTypes.size() <= kMaxBindGroups);
std::ostringstream fs; std::ostringstream fs;
fs << R"( fs << "[[location(0)]] var<out> fragColor : vec4<f32>;\n";
#version 450
layout(location = 0) out vec4 fragColor;
)";
for (size_t i = 0; i < bindingTypes.size(); ++i) { for (size_t i = 0; i < bindingTypes.size(); ++i) {
fs << "[[block]] struct Buffer" << i << R"( {
[[offset(0)]] color : vec4<f32>;
};)";
switch (bindingTypes[i]) { switch (bindingTypes[i]) {
case wgpu::BufferBindingType::Uniform: case wgpu::BufferBindingType::Uniform:
fs << "layout (std140, set = " << i << ", binding = 0) uniform UniformBuffer" fs << "\n[[set(" << i << "), binding(0)]] var<uniform> buffer" << i
<< i << R"( { << " : Buffer" << i << ";";
vec4 color;
} buffer)"
<< i << ";\n";
break; break;
case wgpu::BufferBindingType::Storage: case wgpu::BufferBindingType::Storage:
fs << "layout (std430, set = " << i << ", binding = 0) buffer StorageBuffer" fs << "\n[[set(" << i << "), binding(0)]] var<storage_buffer> buffer" << i
<< i << R"( { << " : [[access(read)]] Buffer" << i << ";";
vec4 color;
} buffer)"
<< i << ";\n";
break; break;
default: default:
UNREACHABLE(); UNREACHABLE();
} }
} }
fs << R"( fs << "\n[[stage(fragment)]] fn main() -> void {\n";
void main() {
fragColor = vec4(0.0);
)";
for (size_t i = 0; i < bindingTypes.size(); ++i) { for (size_t i = 0; i < bindingTypes.size(); ++i) {
fs << "fragColor += buffer" << i << ".color;\n"; fs << "fragColor = fragColor + buffer" << i << ".color;\n";
} }
fs << "}\n"; fs << "}\n";
return utils::CreateShaderModuleFromWGSL(device, fs.str().c_str());
return utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment,
fs.str().c_str());
} }
wgpu::RenderPipeline MakeTestPipeline(const utils::BasicRenderPass& renderPass, wgpu::RenderPipeline MakeTestPipeline(const utils::BasicRenderPass& renderPass,
@ -123,17 +119,14 @@ class BindGroupTests : public DawnTest {
// Test a bindgroup reused in two command buffers in the same call to queue.Submit(). // Test a bindgroup reused in two command buffers in the same call to queue.Submit().
// This test passes by not asserting or crashing. // This test passes by not asserting or crashing.
TEST_P(BindGroupTests, ReusedBindGroupSingleSubmit) { TEST_P(BindGroupTests, ReusedBindGroupSingleSubmit) {
const char* shader = R"( wgpu::ShaderModule module = utils::CreateShaderModuleFromWGSL(device, R"(
#version 450 [[block]] struct Contents {
layout(std140, set = 0, binding = 0) uniform Contents { [[offset(0)]] f : f32;
float f; };
} contents; [[set(0), binding(0)]] var <uniform> contents: Contents;
void main() {
}
)";
wgpu::ShaderModule module = [[stage(compute)]] fn main() -> void {
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, shader); })");
wgpu::ComputePipelineDescriptor cpDesc; wgpu::ComputePipelineDescriptor cpDesc;
cpDesc.computeStage.module = module; cpDesc.computeStage.module = module;
@ -163,26 +156,39 @@ TEST_P(BindGroupTests, ReusedUBO) {
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( # TODO(crbug.com/tint/369): Use a mat2x2 when Tint translates it correctly.
#version 450 [[block]] struct VertexUniformBuffer {
layout (set = 0, binding = 0) uniform vertexUniformBuffer { [[offset(0)]] transform : vec4<f32>;
mat2 transform;
}; };
void main() {
const vec2 pos[3] = vec2[3](vec2(-1.f, 1.f), vec2(1.f, 1.f), vec2(-1.f, -1.f)); [[set(0), binding(0)]] var <uniform> vertexUbo : VertexUniformBuffer;
gl_Position = vec4(transform * pos[gl_VertexIndex], 0.f, 1.f);
[[builtin(vertex_idx)]] var<in> VertexIndex : u32;
[[builtin(position)]] var<out> Position : vec4<f32>;
[[stage(vertex)]] fn main() -> void {
const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>(-1.0, 1.0),
vec2<f32>( 1.0, 1.0),
vec2<f32>(-1.0, -1.0));
var transform : mat2x2<f32> = mat2x2<f32>(
vec2<f32>(vertexUbo.transform[0], vertexUbo.transform[1]),
vec2<f32>(vertexUbo.transform[2], vertexUbo.transform[3]));
Position = vec4<f32>(transform * pos[VertexIndex], 0.0, 1.0);
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[block]] struct FragmentUniformBuffer {
#version 450 [[offset(0)]] color : vec4<f32>;
layout (set = 0, binding = 1) uniform fragmentUniformBuffer {
vec4 color;
}; };
layout(location = 0) out vec4 fragColor; [[set(0), binding(1)]] var <uniform> fragmentUbo : FragmentUniformBuffer;
void main() {
fragColor = color; [[location(0)]] var<out> fragColor : vec4<f32>;
[[stage(fragment)]] fn main() -> void {
fragColor = fragmentUbo.color;
})"); })");
utils::ComboRenderPipelineDescriptor textureDescriptor(device); utils::ComboRenderPipelineDescriptor textureDescriptor(device);
@ -198,9 +204,8 @@ TEST_P(BindGroupTests, ReusedUBO) {
float color[4]; float color[4];
}; };
ASSERT(offsetof(Data, color) == 256); ASSERT(offsetof(Data, color) == 256);
constexpr float dummy = 0.0f;
Data data{ Data data{
{1.f, 0.f, dummy, dummy, 0.f, 1.0f, dummy, dummy}, {1.f, 0.f, 0.f, 1.0f},
{0}, {0},
{0.f, 1.f, 0.f, 1.f}, {0.f, 1.f, 0.f, 1.f},
}; };
@ -239,25 +244,37 @@ TEST_P(BindGroupTests, UBOSamplerAndTexture) {
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( # TODO(crbug.com/tint/369): Use a mat2x2 when Tint translates it correctly.
#version 450 [[block]] struct VertexUniformBuffer {
layout (set = 0, binding = 0) uniform vertexUniformBuffer { [[offset(0)]] transform : vec4<f32>;
mat2 transform;
}; };
void main() { [[set(0), binding(0)]] var <uniform> vertexUbo : VertexUniformBuffer;
const vec2 pos[3] = vec2[3](vec2(-1.f, 1.f), vec2(1.f, 1.f), vec2(-1.f, -1.f));
gl_Position = vec4(transform * pos[gl_VertexIndex], 0.f, 1.f); [[builtin(vertex_idx)]] var<in> VertexIndex : u32;
[[builtin(position)]] var<out> Position : vec4<f32>;
[[stage(vertex)]] fn main() -> void {
const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>(-1.0, 1.0),
vec2<f32>( 1.0, 1.0),
vec2<f32>(-1.0, -1.0));
var transform : mat2x2<f32> = mat2x2<f32>(
vec2<f32>(vertexUbo.transform[0], vertexUbo.transform[1]),
vec2<f32>(vertexUbo.transform[2], vertexUbo.transform[3]));
Position = vec4<f32>(transform * pos[VertexIndex], 0.0, 1.0);
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( [[set(0), binding(1)]] var <uniform_constant> samp : sampler;
#version 450 [[set(0), binding(2)]] var <uniform_constant> tex : texture_2d<f32>;
layout (set = 0, binding = 1) uniform sampler samp; [[builtin(frag_coord)]] var<in> FragCoord : vec4<f32>;
layout (set = 0, binding = 2) uniform texture2D tex;
layout (location = 0) out vec4 fragColor; [[location(0)]] var<out> fragColor : vec4<f32>;
void main() {
fragColor = texture(sampler2D(tex, samp), gl_FragCoord.xy); [[stage(fragment)]] fn main() -> void {
fragColor = textureSample(tex, samp, FragCoord.xy);
})"); })");
utils::ComboRenderPipelineDescriptor pipelineDescriptor(device); utils::ComboRenderPipelineDescriptor pipelineDescriptor(device);
@ -267,8 +284,7 @@ TEST_P(BindGroupTests, UBOSamplerAndTexture) {
wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pipelineDescriptor); wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&pipelineDescriptor);
constexpr float dummy = 0.0f; constexpr float transform[] = {1.f, 0.f, 0.f, 1.f};
constexpr float transform[] = {1.f, 0.f, dummy, dummy, 0.f, 1.f, dummy, dummy};
wgpu::Buffer buffer = utils::CreateBufferFromData(device, &transform, sizeof(transform), wgpu::Buffer buffer = utils::CreateBufferFromData(device, &transform, sizeof(transform),
wgpu::BufferUsage::Uniform); wgpu::BufferUsage::Uniform);
@ -343,32 +359,54 @@ TEST_P(BindGroupTests, MultipleBindLayouts) {
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( # TODO(crbug.com/tint/369): Use a mat2x2 when Tint translates it correctly.
#version 450 # TODO(crbug.com/tint/386): Use the same struct.
layout (set = 0, binding = 0) uniform vertexUniformBuffer1 { [[block]] struct VertexUniformBuffer1 {
mat2 transform1; [[offset(0)]] transform : vec4<f32>;
}; };
layout (set = 1, binding = 0) uniform vertexUniformBuffer2 {
mat2 transform2; [[block]] struct VertexUniformBuffer2 {
[[offset(0)]] transform : vec4<f32>;
}; };
void main() {
const vec2 pos[3] = vec2[3](vec2(-1.f, 1.f), vec2(1.f, 1.f), vec2(-1.f, -1.f)); # TODO(crbug.com/tint/386): Use the same struct definition.
gl_Position = vec4((transform1 + transform2) * pos[gl_VertexIndex], 0.f, 1.f); [[set(0), binding(0)]] var <uniform> vertexUbo1 : VertexUniformBuffer1;
[[set(1), binding(0)]] var <uniform> vertexUbo2 : VertexUniformBuffer2;
[[builtin(vertex_idx)]] var<in> VertexIndex : u32;
[[builtin(position)]] var<out> Position : vec4<f32>;
[[stage(vertex)]] fn main() -> void {
const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>(-1.0, 1.0),
vec2<f32>( 1.0, 1.0),
vec2<f32>(-1.0, -1.0));
Position = vec4<f32>(mat2x2<f32>(
vertexUbo1.transform.xy + vertexUbo2.transform.xy,
vertexUbo1.transform.zw + vertexUbo2.transform.zw
) * pos[VertexIndex], 0.0, 1.0);
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( # TODO(crbug.com/tint/386): Use the same struct
#version 450 [[block]] struct FragmentUniformBuffer1 {
layout (set = 0, binding = 1) uniform fragmentUniformBuffer1 { [[offset(0)]] color : vec4<f32>;
vec4 color1;
}; };
layout (set = 1, binding = 1) uniform fragmentUniformBuffer2 {
vec4 color2; [[block]] struct FragmentUniformBuffer2 {
[[offset(0)]] color : vec4<f32>;
}; };
layout(location = 0) out vec4 fragColor;
void main() { # TODO(crbug.com/tint/386): Use the same struct definition.
fragColor = color1 + color2; [[set(0), binding(1)]] var <uniform> fragmentUbo1 : FragmentUniformBuffer1;
[[set(1), binding(1)]] var <uniform> fragmentUbo2 : FragmentUniformBuffer2;
[[location(0)]] var<out> fragColor : vec4<f32>;
[[stage(fragment)]] fn main() -> void {
fragColor = fragmentUbo1.color + fragmentUbo2.color;
})"); })");
utils::ComboRenderPipelineDescriptor textureDescriptor(device); utils::ComboRenderPipelineDescriptor textureDescriptor(device);
@ -379,8 +417,8 @@ TEST_P(BindGroupTests, MultipleBindLayouts) {
wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&textureDescriptor); wgpu::RenderPipeline pipeline = device.CreateRenderPipeline(&textureDescriptor);
struct Data { struct Data {
float transform[8]; float transform[4];
char padding[256 - 8 * sizeof(float)]; char padding[256 - 4 * sizeof(float)];
float color[4]; float color[4];
}; };
ASSERT(offsetof(Data, color) == 256); ASSERT(offsetof(Data, color) == 256);
@ -389,11 +427,9 @@ TEST_P(BindGroupTests, MultipleBindLayouts) {
std::vector<wgpu::Buffer> buffers; std::vector<wgpu::Buffer> buffers;
std::vector<wgpu::BindGroup> bindGroups; std::vector<wgpu::BindGroup> bindGroups;
data.push_back( data.push_back({{1.0f, 0.0f, 0.0f, 0.0f}, {0}, {0.0f, 1.0f, 0.0f, 1.0f}});
{{1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}, {0}, {0.0f, 1.0f, 0.0f, 1.0f}});
data.push_back( data.push_back({{0.0f, 0.0f, 0.0f, 1.0f}, {0}, {1.0f, 0.0f, 0.0f, 1.0f}});
{{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f}, {0}, {1.0f, 0.0f, 0.0f, 1.0f}});
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
wgpu::Buffer buffer = wgpu::Buffer buffer =
@ -809,25 +845,32 @@ TEST_P(BindGroupTests, DynamicOffsetOrder) {
}); });
wgpu::ComputePipelineDescriptor pipelineDescriptor; wgpu::ComputePipelineDescriptor pipelineDescriptor;
pipelineDescriptor.computeStage.module = pipelineDescriptor.computeStage.module = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"( # TODO(crbug.com/tint/386): Use the same struct
#version 450 [[block]] struct Buffer0 {
layout(std140, set = 0, binding = 2) uniform Buffer2 { [[offset(0)]] value : u32;
uint value2;
}; };
layout(std430, set = 0, binding = 3) readonly buffer Buffer3 {
uint value3; [[block]] struct Buffer2 {
[[offset(0)]] value : u32;
}; };
layout(std430, set = 0, binding = 0) readonly buffer Buffer0 {
uint value0; [[block]] struct Buffer3 {
[[offset(0)]] value : u32;
}; };
layout(std430, set = 0, binding = 4) buffer OutputBuffer {
uvec3 outputBuffer; [[block]] struct OutputBuffer {
[[offset(0)]] value : vec3<u32>;
}; };
void main() {
outputBuffer = uvec3(value0, value2, value3); [[set(0), binding(2)]] var<uniform> buffer2 : Buffer2;
} [[set(0), binding(3)]] var<storage_buffer> buffer3 : [[access(read)]] Buffer3;
)"); [[set(0), binding(0)]] var<storage_buffer> buffer0 : [[access(read)]] Buffer0;
[[set(0), binding(4)]] var<storage_buffer> outputBuffer : [[access(read_write)]] OutputBuffer;
[[stage(compute)]] fn main() -> void {
outputBuffer.value = vec3<u32>(buffer0.value, buffer2.value, buffer3.value);
})");
pipelineDescriptor.computeStage.entryPoint = "main"; pipelineDescriptor.computeStage.entryPoint = "main";
pipelineDescriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl); pipelineDescriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDescriptor); wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDescriptor);
@ -917,29 +960,42 @@ TEST_P(BindGroupTests, DynamicBindingNoneVisibility) {
TEST_P(BindGroupTests, ArbitraryBindingNumbers) { TEST_P(BindGroupTests, ArbitraryBindingNumbers) {
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
wgpu::ShaderModule vsModule = wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( [[builtin(vertex_idx)]] var<in> VertexIndex : u32;
#version 450 [[builtin(position)]] var<out> Position : vec4<f32>;
void main() {
const vec2 pos[3] = vec2[3](vec2(-1.f, 1.f), vec2(1.f, 1.f), vec2(-1.f, -1.f)); [[stage(vertex)]] fn main() -> void {
gl_Position = vec4(pos[gl_VertexIndex], 0.f, 1.f); const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
vec2<f32>(-1.0, 1.0),
vec2<f32>( 1.0, 1.0),
vec2<f32>(-1.0, -1.0));
Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
})"); })");
wgpu::ShaderModule fsModule = wgpu::ShaderModule fsModule = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( # TODO(crbug.com/tint/386): Use the same struct
#version 450 [[block]] struct Ubo1 {
layout (set = 0, binding = 953) uniform ubo1 { [[offset(0)]] color : vec4<f32>;
vec4 color1;
}; };
layout (set = 0, binding = 47) uniform ubo2 {
vec4 color2; [[block]] struct Ubo2 {
[[offset(0)]] color : vec4<f32>;
}; };
layout (set = 0, binding = 111) uniform ubo3 {
vec4 color3; [[block]] struct Ubo3 {
[[offset(0)]] color : vec4<f32>;
}; };
layout(location = 0) out vec4 fragColor;
void main() { # TODO(crbug.com/tint/386): Use the same struct definition.
fragColor = color1 + 2 * color2 + 4 * color3; [[set(0), binding(953)]] var <uniform> ubo1 : Ubo1;
[[set(0), binding(47)]] var <uniform> ubo2 : Ubo2;
[[set(0), binding(111)]] var <uniform> ubo3 : Ubo3;
[[location(0)]] var<out> fragColor : vec4<f32>;
[[stage(fragment)]] fn main() -> void {
fragColor = ubo1.color + 2.0 * ubo2.color + 4.0 * ubo3.color;
})"); })");
utils::ComboRenderPipelineDescriptor pipelineDescriptor(device); utils::ComboRenderPipelineDescriptor pipelineDescriptor(device);
@ -1037,10 +1093,8 @@ TEST_P(BindGroupTests, EmptyLayout) {
wgpu::ComputePipelineDescriptor pipelineDesc; wgpu::ComputePipelineDescriptor pipelineDesc;
pipelineDesc.layout = utils::MakeBasicPipelineLayout(device, &bgl); pipelineDesc.layout = utils::MakeBasicPipelineLayout(device, &bgl);
pipelineDesc.computeStage.entryPoint = "main"; pipelineDesc.computeStage.entryPoint = "main";
pipelineDesc.computeStage.module = pipelineDesc.computeStage.module = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, R"( [[stage(compute)]] fn main() -> void {
#version 450
void main() {
})"); })");
wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDesc); wgpu::ComputePipeline pipeline = device.CreateComputePipeline(&pipelineDesc);
@ -1062,24 +1116,29 @@ TEST_P(BindGroupTests, EmptyLayout) {
TEST_P(BindGroupTests, ReadonlyStorage) { TEST_P(BindGroupTests, ReadonlyStorage) {
utils::ComboRenderPipelineDescriptor pipelineDescriptor(device); utils::ComboRenderPipelineDescriptor pipelineDescriptor(device);
pipelineDescriptor.vertexStage.module = pipelineDescriptor.vertexStage.module = utils::CreateShaderModuleFromWGSL(device, R"(
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( [[builtin(vertex_idx)]] var<in> VertexIndex : u32;
#version 450 [[builtin(position)]] var<out> Position : vec4<f32>;
void main() {
const vec2 pos[3] = vec2[3](vec2(-1.f, 1.f), vec2(1.f, 1.f), vec2(-1.f, -1.f));
gl_Position = vec4(pos[gl_VertexIndex], 0.f, 1.f);
})");
pipelineDescriptor.cFragmentStage.module = [[stage(vertex)]] fn main() -> void {
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"( const pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
#version 450 vec2<f32>(-1.0, 1.0),
layout(set = 0, binding = 0) readonly buffer buffer0 { vec2<f32>( 1.0, 1.0),
vec4 color; vec2<f32>(-1.0, -1.0));
};
layout(location = 0) out vec4 fragColor; Position = vec4<f32>(pos[VertexIndex], 0.0, 1.0);
void main() { })");
fragColor = color;
})"); pipelineDescriptor.cFragmentStage.module = utils::CreateShaderModuleFromWGSL(device, R"(
[[block]] struct Buffer0 {
[[offset(0)]] color : vec4<f32>;
};
[[set(0), binding(0)]] var<storage_buffer> buffer0 : [[access(read)]] Buffer0;
[[location(0)]] var<out> fragColor : vec4<f32>;
[[stage(fragment)]] fn main() -> void {
fragColor = buffer0.color;
})");
constexpr uint32_t kRTSize = 4; constexpr uint32_t kRTSize = 4;
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize); utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
@ -1115,22 +1174,27 @@ TEST_P(BindGroupTests, ReadonlyStorage) {
// buffer if all values are correct. // buffer if all values are correct.
TEST_P(BindGroupTests, ReallyLargeBindGroup) { TEST_P(BindGroupTests, ReallyLargeBindGroup) {
DAWN_SKIP_TEST_IF(IsOpenGLES()); DAWN_SKIP_TEST_IF(IsOpenGLES());
std::string interface = "#version 450\n"; std::ostringstream interface;
std::string body; std::ostringstream body;
uint32_t binding = 0; uint32_t binding = 0;
uint32_t expectedValue = 42; uint32_t expectedValue = 42;
wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder(); wgpu::CommandEncoder commandEncoder = device.CreateCommandEncoder();
auto CreateTextureWithRedData = [&](uint32_t value, wgpu::TextureUsage usage) { auto CreateTextureWithRedData = [&](wgpu::TextureFormat format, uint32_t value,
wgpu::TextureUsage usage) {
wgpu::TextureDescriptor textureDesc = {}; wgpu::TextureDescriptor textureDesc = {};
textureDesc.usage = wgpu::TextureUsage::CopyDst | usage; textureDesc.usage = wgpu::TextureUsage::CopyDst | usage;
textureDesc.size = {1, 1, 1}; textureDesc.size = {1, 1, 1};
textureDesc.format = wgpu::TextureFormat::R32Uint; textureDesc.format = format;
wgpu::Texture texture = device.CreateTexture(&textureDesc); wgpu::Texture texture = device.CreateTexture(&textureDesc);
if (format == wgpu::TextureFormat::R8Unorm) {
ASSERT(expectedValue < 255u);
}
wgpu::Buffer textureData = wgpu::Buffer textureData =
utils::CreateBufferFromData(device, wgpu::BufferUsage::CopySrc, {expectedValue}); utils::CreateBufferFromData(device, wgpu::BufferUsage::CopySrc, {value});
wgpu::BufferCopyView bufferCopyView = {}; wgpu::BufferCopyView bufferCopyView = {};
bufferCopyView.buffer = textureData; bufferCopyView.buffer = textureData;
bufferCopyView.layout.bytesPerRow = 256; bufferCopyView.layout.bytesPerRow = 256;
@ -1147,52 +1211,55 @@ TEST_P(BindGroupTests, ReallyLargeBindGroup) {
std::vector<wgpu::BindGroupEntry> bgEntries; std::vector<wgpu::BindGroupEntry> bgEntries;
static_assert(kMaxSampledTexturesPerShaderStage == kMaxSamplersPerShaderStage, static_assert(kMaxSampledTexturesPerShaderStage == kMaxSamplersPerShaderStage,
"Please update this test"); "Please update this test");
body += "result = 0;\n";
for (uint32_t i = 0; i < kMaxSampledTexturesPerShaderStage; ++i) { for (uint32_t i = 0; i < kMaxSampledTexturesPerShaderStage; ++i) {
wgpu::Texture texture = wgpu::Texture texture = CreateTextureWithRedData(
CreateTextureWithRedData(expectedValue, wgpu::TextureUsage::Sampled); wgpu::TextureFormat::R8Unorm, expectedValue, wgpu::TextureUsage::Sampled);
bgEntries.push_back({binding, nullptr, 0, 0, nullptr, texture.CreateView()}); bgEntries.push_back({binding, nullptr, 0, 0, nullptr, texture.CreateView()});
interface += "layout(set = 0, binding = " + std::to_string(binding++) + interface << "[[set(0), binding(" << binding++ << ")]] "
") uniform utexture2D tex" + std::to_string(i) + ";\n"; << "var<uniform_constant> tex" << i << " : texture_2d<f32>;\n";
wgpu::SamplerDescriptor samplerDesc = {}; wgpu::SamplerDescriptor samplerDesc = {};
bgEntries.push_back({binding, nullptr, 0, 0, device.CreateSampler(&samplerDesc), nullptr}); bgEntries.push_back({binding, nullptr, 0, 0, device.CreateSampler(&samplerDesc), nullptr});
interface += "layout(set = 0, binding = " + std::to_string(binding++) + interface << "[[set(0), binding(" << binding++ << ")]]"
") uniform sampler samp" + std::to_string(i) + ";\n"; << "var<uniform_constant> samp" << i << " : sampler;\n";
body += "if (texelFetch(usampler2D(tex" + std::to_string(i) + ", samp" + std::to_string(i) + body << "if (abs(textureSampleLevel(tex" << i << ", samp" << i
"), ivec2(0, 0), 0).r != " + std::to_string(expectedValue++) + ") {\n"; << ", vec2<f32>(0.5, 0.5), 0.0).r - " << expectedValue++
body += " return;\n"; << ".0 / 255.0) > 0.0001) {\n";
body += "}\n"; body << " return;\n";
body << "}\n";
} }
for (uint32_t i = 0; i < kMaxStorageTexturesPerShaderStage; ++i) { for (uint32_t i = 0; i < kMaxStorageTexturesPerShaderStage; ++i) {
wgpu::Texture texture = wgpu::Texture texture = CreateTextureWithRedData(
CreateTextureWithRedData(expectedValue, wgpu::TextureUsage::Storage); wgpu::TextureFormat::R32Uint, expectedValue, wgpu::TextureUsage::Storage);
bgEntries.push_back({binding, nullptr, 0, 0, nullptr, texture.CreateView()}); bgEntries.push_back({binding, nullptr, 0, 0, nullptr, texture.CreateView()});
interface += "layout(set = 0, binding = " + std::to_string(binding++) + interface << "[[set(0), binding(" << binding++ << ")]] "
", r32ui) uniform readonly uimage2D image" + std::to_string(i) + ";\n"; << "var<uniform_constant> image" << i << " : texture_storage_ro_2d<r32uint>;\n";
body += "if (imageLoad(image" + std::to_string(i) + body << "if (textureLoad(image" << i << ", vec2<i32>(0, 0)).r != " << expectedValue++
", ivec2(0, 0)).r != " + std::to_string(expectedValue++) + ") {\n"; << "u) {\n";
body += " return;\n"; body << " return;\n";
body += "}\n"; body << "}\n";
} }
for (uint32_t i = 0; i < kMaxUniformBuffersPerShaderStage; ++i) { for (uint32_t i = 0; i < kMaxUniformBuffersPerShaderStage; ++i) {
wgpu::Buffer buffer = utils::CreateBufferFromData<uint32_t>( wgpu::Buffer buffer = utils::CreateBufferFromData<uint32_t>(
device, wgpu::BufferUsage::Uniform, {expectedValue, 0, 0, 0}); device, wgpu::BufferUsage::Uniform, {expectedValue, 0, 0, 0});
bgEntries.push_back({binding, buffer, 0, 4 * sizeof(uint32_t), nullptr, nullptr}); bgEntries.push_back({binding, buffer, 0, 4 * sizeof(uint32_t), nullptr, nullptr});
interface += "layout(std140, set = 0, binding = " + std::to_string(binding++) + interface << "[[block]] struct UniformBuffer" << i << R"({
") uniform UBuf" + std::to_string(i) + " {\n"; [[offset(0)]] value : u32;
interface += " uint ubuf" + std::to_string(i) + ";\n"; };
interface += "};\n"; )";
interface << "[[set(0), binding(" << binding++ << ")]] "
<< "var<uniform> ubuf" << i << " : UniformBuffer" << i << ";\n";
body += "if (ubuf" + std::to_string(i) + " != " + std::to_string(expectedValue++) + ") {\n"; body << "if (ubuf" << i << ".value != " << expectedValue++ << "u) {\n";
body += " return;\n"; body << " return;\n";
body += "}\n"; body << "}\n";
} }
// Save one storage buffer for writing the result // Save one storage buffer for writing the result
for (uint32_t i = 0; i < kMaxStorageBuffersPerShaderStage - 1; ++i) { for (uint32_t i = 0; i < kMaxStorageBuffersPerShaderStage - 1; ++i) {
@ -1200,32 +1267,36 @@ TEST_P(BindGroupTests, ReallyLargeBindGroup) {
device, wgpu::BufferUsage::Storage, {expectedValue}); device, wgpu::BufferUsage::Storage, {expectedValue});
bgEntries.push_back({binding, buffer, 0, sizeof(uint32_t), nullptr, nullptr}); bgEntries.push_back({binding, buffer, 0, sizeof(uint32_t), nullptr, nullptr});
interface += "layout(std430, set = 0, binding = " + std::to_string(binding++) + interface << "[[block]] struct ReadOnlyStorageBuffer" << i << R"({
") readonly buffer SBuf" + std::to_string(i) + " {\n"; [[offset(0)]] value : u32;
interface += " uint sbuf" + std::to_string(i) + ";\n"; };
interface += "};\n"; )";
interface << "[[set(0), binding(" << binding++ << ")]] "
<< "var<storage_buffer> sbuf" << i << " : [[access(read)]] ReadOnlyStorageBuffer"
<< i << ";\n";
body += "if (sbuf" + std::to_string(i) + " != " + std::to_string(expectedValue++) + ") {\n"; body << "if (sbuf" << i << ".value != " << expectedValue++ << "u) {\n";
body += " return;\n"; body << " return;\n";
body += "}\n"; body << "}\n";
} }
wgpu::Buffer result = utils::CreateBufferFromData<uint32_t>( wgpu::Buffer result = utils::CreateBufferFromData<uint32_t>(
device, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, {0}); device, wgpu::BufferUsage::Storage | wgpu::BufferUsage::CopySrc, {0});
bgEntries.push_back({binding, result, 0, sizeof(uint32_t), nullptr, nullptr}); bgEntries.push_back({binding, result, 0, sizeof(uint32_t), nullptr, nullptr});
interface += "layout(std430, set = 0, binding = " + std::to_string(binding++) + interface << R"([[block]] struct ReadWriteStorageBuffer{
") writeonly buffer Result {\n"; [[offset(0)]] value : u32;
interface += " uint result;\n"; };
interface += "};\n"; )";
interface << "[[set(0), binding(" << binding++ << ")]] "
<< "var<storage_buffer> result : [[access(read_write)]] ReadWriteStorageBuffer;\n";
body += "result = 1;\n"; body << "result.value = 1u;\n";
std::string shader = interface + "void main() {\n" + body + "}\n";
std::string shader =
interface.str() + "[[stage(compute)]] fn main() -> void {\n" + body.str() + "}\n";
wgpu::ComputePipelineDescriptor cpDesc; wgpu::ComputePipelineDescriptor cpDesc;
cpDesc.computeStage.module = cpDesc.computeStage.module = utils::CreateShaderModuleFromWGSL(device, shader.c_str());
utils::CreateShaderModule(device, utils::SingleShaderStage::Compute, shader.c_str());
cpDesc.computeStage.entryPoint = "main"; cpDesc.computeStage.entryPoint = "main";
wgpu::ComputePipeline cp = device.CreateComputePipeline(&cpDesc); wgpu::ComputePipeline cp = device.CreateComputePipeline(&cpDesc);