D3D12: Fix RO storage buffer mismatches.

If the shader declares a storage buffer RO but uses storage buffer in
the BGL, the shader compiler will be told to treat these bindings as
UAV instead of SRV to avoid PSO mismatches.

Bug: dawn:410
Change-Id: I3be3257449de55fd2d35e914233b48c6f7121b58
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/22322
Commit-Queue: Bryan Bernhart <bryan.bernhart@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Bryan Bernhart 2020-06-02 19:16:20 +00:00 committed by Commit Bot service account
parent 3fb9c1bbfb
commit adbf9153d9
2 changed files with 69 additions and 0 deletions

View File

@ -164,6 +164,13 @@ namespace dawn_native { namespace d3d12 {
BindingNumber bindingNumber = it.first;
BindingIndex bindingIndex = bgl->GetBindingIndex(bindingNumber);
// Declaring a read-only storage buffer in HLSL but specifying a storage buffer in
// the BGL produces the wrong output. Force read-only storage buffer bindings to
// be treated as UAV instead of SRV.
const bool forceStorageBufferAsUAV =
(bindingInfo.type == wgpu::BindingType::ReadonlyStorageBuffer &&
bgl->GetBindingInfo(bindingIndex).type == wgpu::BindingType::StorageBuffer);
uint32_t bindingOffset = bindingOffsets[bindingIndex];
if (GetDevice()->IsToggleEnabled(Toggle::UseSpvc)) {
DAWN_TRY(CheckSpvcSuccess(
@ -171,8 +178,16 @@ namespace dawn_native { namespace d3d12 {
bindingOffset),
"Unable to set decorating binding before generating HLSL shader w/ "
"spvc"));
if (forceStorageBufferAsUAV) {
DAWN_TRY(CheckSpvcSuccess(
mSpvcContext.SetHLSLForceStorageBufferAsUAV(group, bindingNumber),
"Unable to force read-only storage buffer as UAV w/ spvc"));
}
} else {
compiler->set_decoration(bindingInfo.id, spv::DecorationBinding, bindingOffset);
if (forceStorageBufferAsUAV) {
compiler->set_hlsl_force_storage_buffer_as_uav(group, bindingNumber);
}
}
}
}

View File

@ -1038,4 +1038,58 @@ TEST_P(BindGroupTests, EmptyLayout) {
queue.Submit(1, &commands);
}
// Test creating a BGL with a storage buffer binding but declared readonly in the shader works.
// This is a regression test for crbug.com/dawn/410 which tests that it can successfully compile and
// execute the shader.
TEST_P(BindGroupTests, ReadonlyStorage) {
utils::ComboRenderPipelineDescriptor pipelineDescriptor(device);
pipelineDescriptor.vertexStage.module =
utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"(
#version 450
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 =
utils::CreateShaderModule(device, utils::SingleShaderStage::Fragment, R"(
#version 450
layout(set = 0, binding = 0) readonly buffer buffer0 {
vec4 color;
};
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = color;
})");
constexpr uint32_t kRTSize = 4;
utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, kRTSize, kRTSize);
pipelineDescriptor.cColorStates[0].format = renderPass.colorFormat;
wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout(
device, {{0, wgpu::ShaderStage::Fragment, wgpu::BindingType::StorageBuffer}});
pipelineDescriptor.layout = utils::MakeBasicPipelineLayout(device, &bgl);
wgpu::RenderPipeline renderPipeline = device.CreateRenderPipeline(&pipelineDescriptor);
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::RenderPassEncoder pass = encoder.BeginRenderPass(&renderPass.renderPassInfo);
std::array<float, 4> greenColor = {0, 1, 0, 1};
wgpu::Buffer storageBuffer = utils::CreateBufferFromData(
device, &greenColor, sizeof(greenColor), wgpu::BufferUsage::Storage);
pass.SetPipeline(renderPipeline);
pass.SetBindGroup(0, utils::MakeBindGroup(device, bgl, {{0, storageBuffer}}));
pass.Draw(3);
pass.EndPass();
wgpu::CommandBuffer commands = encoder.Finish();
queue.Submit(1, &commands);
EXPECT_PIXEL_RGBA8_EQ(RGBA8::kGreen, renderPass.color, 0, 0);
}
DAWN_INSTANTIATE_TEST(BindGroupTests, D3D12Backend(), MetalBackend(), OpenGLBackend(), VulkanBackend());