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 <bclayton@google.com>
Commit-Queue: Austin Eng <enga@chromium.org>
diff --git a/src/tests/end2end/TextureFormatTests.cpp b/src/tests/end2end/TextureFormatTests.cpp
index 13a78c6..66b21c5 100644
--- a/src/tests/end2end/TextureFormatTests.cpp
+++ b/src/tests/end2end/TextureFormatTests.cpp
@@ -146,34 +146,33 @@
                                               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<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>(-3.0, -1.0),
+                    vec2<f32>( 3.0, -1.0),
+                    vec2<f32>( 0.0,  2.0));
+
+                Position = vec4<f32>(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<uniform_constant> myTexture : texture_2d<" << type
+                 << ">;\n";
+        fsSource << "[[builtin(frag_coord)]] var<in> FragCoord : vec4<f32>;\n";
+        fsSource << "[[location(0)]] var<out> fragColor : vec4<" << type << ">;\n";
+        fsSource << "[[stage(fragment)]] fn main() -> void {\n";
+        fsSource << "    fragColor = textureLoad(myTexture, vec2<i32>(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 @@
 
         // 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 8430058..7f9dc50 100644
--- a/src/utils/TextureFormatUtils.cpp
+++ b/src/utils/TextureFormatUtils.cpp
@@ -64,6 +64,55 @@
         }
     }
 
+    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 4602e8b..d83d5c0 100644
--- a/src/utils/TextureFormatUtils.h
+++ b/src/utils/TextureFormatUtils.h
@@ -88,6 +88,7 @@
         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);