From 1229110f9c4ded0fcf8a39028e99c4a9758b636d Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Thu, 3 Dec 2020 18:51:53 +0000 Subject: [PATCH] Update TextureFormatTests to use WGSL Bug: dawn:572 Change-Id: I104465bfaf3056e0546cdf08b0aebfb43d84b488 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/33886 Reviewed-by: Ben Clayton Commit-Queue: Austin Eng --- src/tests/end2end/TextureFormatTests.cpp | 54 ++++++++++-------------- src/utils/TextureFormatUtils.cpp | 49 +++++++++++++++++++++ src/utils/TextureFormatUtils.h | 1 + 3 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/tests/end2end/TextureFormatTests.cpp b/src/tests/end2end/TextureFormatTests.cpp index 13a78c66a5..66b21c5ebe 100644 --- a/src/tests/end2end/TextureFormatTests.cpp +++ b/src/tests/end2end/TextureFormatTests.cpp @@ -146,34 +146,33 @@ class TextureFormatTest : public DawnTest { FormatTestInfo renderFormatInfo) { utils::ComboRenderPipelineDescriptor desc(device); - wgpu::ShaderModule vsModule = - utils::CreateShaderModule(device, utils::SingleShaderStage::Vertex, R"( - #version 450 - void main() { - const vec2 pos[3] = vec2[3]( - vec2(-3.0f, -1.0f), - vec2( 3.0f, -1.0f), - vec2( 0.0f, 2.0f) - ); - gl_Position = vec4(pos[gl_VertexIndex], 0.0f, 1.0f); + wgpu::ShaderModule vsModule = utils::CreateShaderModuleFromWGSL(device, R"( + [[builtin(vertex_idx)]] var VertexIndex : u32; + [[builtin(position)]] var Position : vec4; + + [[stage(vertex)]] fn main() -> void { + const pos : array, 3> = array, 3>( + vec2(-3.0, -1.0), + vec2( 3.0, -1.0), + vec2( 0.0, 2.0)); + + Position = vec4(pos[VertexIndex], 0.0, 1.0); })"); - // Compute the prefix needed for GLSL types that handle our texture's data. - const char* prefix = utils::GetColorTextureComponentTypePrefix(sampleFormatInfo.format); + // Compute the WGSL type of the texture's data. + const char* type = utils::GetColorTextureComponentWGSLType(sampleFormatInfo.format); std::ostringstream fsSource; - fsSource << "#version 450\n"; - fsSource << "layout(set=0, binding=0) uniform sampler mySampler;\n"; - fsSource << "layout(set=0, binding=1) uniform " << prefix << "texture2D myTexture;\n"; - fsSource << "layout(location=0) out " << prefix << "vec4 fragColor;\n"; - - fsSource << "void main() {\n"; - fsSource << " fragColor = texelFetch(" << prefix - << "sampler2D(myTexture, mySampler), ivec2(gl_FragCoord), 0);\n"; + fsSource << "[[set(0), binding(0)]] var myTexture : texture_2d<" << type + << ">;\n"; + fsSource << "[[builtin(frag_coord)]] var FragCoord : vec4;\n"; + fsSource << "[[location(0)]] var fragColor : vec4<" << type << ">;\n"; + fsSource << "[[stage(fragment)]] fn main() -> void {\n"; + fsSource << " fragColor = textureLoad(myTexture, vec2(FragCoord.xy), 0);\n"; fsSource << "}"; - wgpu::ShaderModule fsModule = utils::CreateShaderModule( - device, utils::SingleShaderStage::Fragment, fsSource.str().c_str()); + wgpu::ShaderModule fsModule = + utils::CreateShaderModuleFromWGSL(device, fsSource.str().c_str()); desc.vertexStage.module = vsModule; desc.cFragmentStage.module = fsModule; @@ -229,15 +228,8 @@ class TextureFormatTest : public DawnTest { // Prepare objects needed to sample from texture in the renderpass wgpu::RenderPipeline pipeline = CreateSamplePipeline(sampleFormatInfo, renderFormatInfo); - - // In this test we always use the default values of mag/min/mipmap filter - // (FilterMode::Nearest) because integer/unsigned integer textures must be sampled with - // FilterMode::Nearest. - wgpu::SamplerDescriptor samplerDesc; - wgpu::Sampler sampler = device.CreateSampler(&samplerDesc); - wgpu::BindGroup bindGroup = - utils::MakeBindGroup(device, pipeline.GetBindGroupLayout(0), - {{0, sampler}, {1, sampleTexture.CreateView()}}); + wgpu::BindGroup bindGroup = utils::MakeBindGroup(device, pipeline.GetBindGroupLayout(0), + {{0, sampleTexture.CreateView()}}); // Encode commands for the test that fill texture, sample it to render to renderTarget then // copy renderTarget in a buffer so we can read it easily. diff --git a/src/utils/TextureFormatUtils.cpp b/src/utils/TextureFormatUtils.cpp index 8430058b2c..7f9dc509b4 100644 --- a/src/utils/TextureFormatUtils.cpp +++ b/src/utils/TextureFormatUtils.cpp @@ -64,6 +64,55 @@ namespace utils { } } + const char* GetColorTextureComponentWGSLType(wgpu::TextureFormat textureFormat) { + switch (textureFormat) { + case wgpu::TextureFormat::R8Unorm: + case wgpu::TextureFormat::R8Snorm: + case wgpu::TextureFormat::R16Float: + case wgpu::TextureFormat::RG8Unorm: + case wgpu::TextureFormat::RG8Snorm: + case wgpu::TextureFormat::R32Float: + case wgpu::TextureFormat::RG16Float: + case wgpu::TextureFormat::RGBA8Unorm: + case wgpu::TextureFormat::RGBA8Snorm: + case wgpu::TextureFormat::RGB10A2Unorm: + case wgpu::TextureFormat::RG11B10Ufloat: + case wgpu::TextureFormat::RGB9E5Ufloat: + case wgpu::TextureFormat::RG32Float: + case wgpu::TextureFormat::RGBA16Float: + case wgpu::TextureFormat::RGBA32Float: + case wgpu::TextureFormat::BGRA8Unorm: + case wgpu::TextureFormat::BGRA8UnormSrgb: + case wgpu::TextureFormat::RGBA8UnormSrgb: + return "f32"; + + case wgpu::TextureFormat::R8Uint: + case wgpu::TextureFormat::R16Uint: + case wgpu::TextureFormat::RG8Uint: + case wgpu::TextureFormat::R32Uint: + case wgpu::TextureFormat::RG16Uint: + case wgpu::TextureFormat::RGBA8Uint: + case wgpu::TextureFormat::RG32Uint: + case wgpu::TextureFormat::RGBA16Uint: + case wgpu::TextureFormat::RGBA32Uint: + return "u32"; + + case wgpu::TextureFormat::R8Sint: + case wgpu::TextureFormat::R16Sint: + case wgpu::TextureFormat::RG8Sint: + case wgpu::TextureFormat::R32Sint: + case wgpu::TextureFormat::RG16Sint: + case wgpu::TextureFormat::RGBA8Sint: + case wgpu::TextureFormat::RG32Sint: + case wgpu::TextureFormat::RGBA16Sint: + case wgpu::TextureFormat::RGBA32Sint: + return "i32"; + + default: + UNREACHABLE(); + } + } + bool TextureFormatSupportsStorageTexture(wgpu::TextureFormat format) { switch (format) { case wgpu::TextureFormat::R32Uint: diff --git a/src/utils/TextureFormatUtils.h b/src/utils/TextureFormatUtils.h index 4602e8b5d2..d83d5c0506 100644 --- a/src/utils/TextureFormatUtils.h +++ b/src/utils/TextureFormatUtils.h @@ -88,6 +88,7 @@ namespace utils { wgpu::TextureFormat::BC7RGBAUnorm, wgpu::TextureFormat::BC7RGBAUnormSrgb}; const char* GetColorTextureComponentTypePrefix(wgpu::TextureFormat textureFormat); + const char* GetColorTextureComponentWGSLType(wgpu::TextureFormat textureFormat); bool TextureFormatSupportsStorageTexture(wgpu::TextureFormat format); uint32_t GetTexelBlockSizeInBytes(wgpu::TextureFormat textureFormat);