GLSL: fix textureGather() and textureGatherOffset() with depth textures.

Unlike other texture functions in GLSL, textureGather() and
textureGatherOffset() do not expect the refZ value to be appended to
the texture coordinates. It is passed as a regular argument. So append
refZ to coordinates by default, and pass as a regular parameter only
for the gather functions.

Bug: tint:1459
Change-Id: Iad1255be3de5915aeff4adb9054479b9e92c45cb
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/82340
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc
index 8091e37..fe42ee3 100644
--- a/src/tint/writer/glsl/generator_impl.cc
+++ b/src/tint/writer/glsl/generator_impl.cc
@@ -1357,6 +1357,7 @@
   }
 
   uint32_t glsl_ret_width = 4u;
+  bool append_depth_ref_to_coords = true;
   bool is_depth = texture_type->Is<sem::DepthTexture>();
 
   switch (builtin->Type()) {
@@ -1376,6 +1377,7 @@
     case sem::BuiltinType::kTextureGather:
     case sem::BuiltinType::kTextureGatherCompare:
       out << "textureGather";
+      append_depth_ref_to_coords = false;
       break;
     case sem::BuiltinType::kTextureSampleGrad:
       out << "textureGrad";
@@ -1421,17 +1423,19 @@
     param_coords =
         AppendVector(&builder_, param_coords, array_index)->Declaration();
   }
-  bool is_cube_array = texture_type->dim() == ast::TextureDimension::kCubeArray;
 
   // GLSL requires Dref to be appended to the coordinates, *unless* it's
   // samplerCubeArrayShadow, in which case it will be handled as a separate
-  // parameter [1].
-  if (is_depth && !is_cube_array) {
+  // parameter.
+  if (texture_type->dim() == ast::TextureDimension::kCubeArray) {
+    append_depth_ref_to_coords = false;
+  }
+
+  if (is_depth && append_depth_ref_to_coords) {
     if (auto* depth_ref = arg(Usage::kDepthRef)) {
       param_coords =
           AppendVector(&builder_, param_coords, depth_ref)->Declaration();
-    } else if (builtin->Type() == sem::BuiltinType::kTextureSample ||
-               builtin->Type() == sem::BuiltinType::kTextureSampleLevel) {
+    } else {
       // Sampling a depth texture in GLSL always requires a depth reference, so
       // append zero here.
       auto* f32 = builder_.create<sem::F32>();
@@ -1471,17 +1475,8 @@
     out << ", 0.0";
   }
 
-  for (auto usage : {Usage::kOffset, Usage::kComponent, Usage::kBias}) {
-    if (auto* e = arg(usage)) {
-      out << ", ";
-      if (!EmitExpression(out, e)) {
-        return false;
-      }
-    }
-  }
-
   // [1] samplerCubeArrayShadow requires a separate depthRef parameter
-  if (is_depth && is_cube_array) {
+  if (is_depth && !append_depth_ref_to_coords) {
     if (auto* e = arg(Usage::kDepthRef)) {
       out << ", ";
       if (!EmitExpression(out, e)) {
@@ -1492,6 +1487,15 @@
     }
   }
 
+  for (auto usage : {Usage::kOffset, Usage::kComponent, Usage::kBias}) {
+    if (auto* e = arg(usage)) {
+      out << ", ";
+      if (!EmitExpression(out, e)) {
+        return false;
+      }
+    }
+  }
+
   out << ")";
 
   if (builtin->ReturnType()->Is<sem::Void>()) {
diff --git a/src/tint/writer/glsl/generator_impl_builtin_texture_test.cc b/src/tint/writer/glsl/generator_impl_builtin_texture_test.cc
index 9735ee8..78d7267 100644
--- a/src/tint/writer/glsl/generator_impl_builtin_texture_test.cc
+++ b/src/tint/writer/glsl/generator_impl_builtin_texture_test.cc
@@ -88,15 +88,15 @@
     case ValidTextureOverload::kGatherDepthCubeArrayF32:
       return R"(textureGather(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, float(4)), 0.0))";
     case ValidTextureOverload::kGatherCompareDepth2dF32:
-      return R"(textureGather(tint_symbol_sampler, vec3(1.0f, 2.0f, 3.0f))";
+      return R"(textureGather(tint_symbol_sampler, vec2(1.0f, 2.0f), 3.0f))";
     case ValidTextureOverload::kGatherCompareDepth2dOffsetF32:
-      return R"(textureGatherOffset(tint_symbol_sampler, vec3(1.0f, 2.0f, 3.0f), ivec2(4, 5)))";
+      return R"(textureGatherOffset(tint_symbol_sampler, vec2(1.0f, 2.0f), 3.0f, ivec2(4, 5)))";
     case ValidTextureOverload::kGatherCompareDepth2dArrayF32:
-      return R"(textureGather(tint_symbol_sampler, vec4(1.0f, 2.0f, float(3), 4.0f)))";
+      return R"(textureGather(tint_symbol_sampler, vec3(1.0f, 2.0f, float(3)), 4.0f))";
     case ValidTextureOverload::kGatherCompareDepth2dArrayOffsetF32:
-      return R"(textureGatherOffset(tint_symbol_sampler, vec4(1.0f, 2.0f, float(3), 4.0f), ivec2(5, 6)))";
+      return R"(textureGatherOffset(tint_symbol_sampler, vec3(1.0f, 2.0f, float(3)), 4.0f, ivec2(5, 6)))";
     case ValidTextureOverload::kGatherCompareDepthCubeF32:
-      return R"(textureGather(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, 4.0f)))";
+      return R"(textureGather(tint_symbol_sampler, vec3(1.0f, 2.0f, 3.0f), 4.0f))";
     case ValidTextureOverload::kGatherCompareDepthCubeArrayF32:
       return R"(textureGather(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, float(4)), 5.0f))";
     case ValidTextureOverload::kNumLayers2dArray:
diff --git a/test/tint/builtins/gen/textureDimensions/ba1481.wgsl.expected.glsl b/test/tint/builtins/gen/textureDimensions/ba1481.wgsl.expected.glsl
index 52dc125..31b244c 100644
--- a/test/tint/builtins/gen/textureDimensions/ba1481.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureDimensions/ba1481.wgsl.expected.glsl
@@ -1,6 +1,6 @@
 SKIP: FAILED
 
-../../src/tint/writer/glsl/generator_impl.cc:2563 internal compiler error: Multiplanar external texture transform was not run.
+../../src/tint/writer/glsl/generator_impl.cc:2569 internal compiler error: Multiplanar external texture transform was not run.
 
 
 ********************************************************************
diff --git a/test/tint/builtins/gen/textureGatherCompare/182fd4.wgsl.expected.glsl b/test/tint/builtins/gen/textureGatherCompare/182fd4.wgsl.expected.glsl
index 703dc75..c7830e5 100644
--- a/test/tint/builtins/gen/textureGatherCompare/182fd4.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureGatherCompare/182fd4.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp samplerCubeShadow arg_0_arg_1;
 
 void textureGatherCompare_182fd4() {
-  vec4 res = textureGather(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), 1.0f);
 }
 
 vec4 vertex_main() {
@@ -20,21 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGather' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp samplerCubeShadow arg_0_arg_1;
 
 void textureGatherCompare_182fd4() {
-  vec4 res = textureGather(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), 1.0f);
 }
 
 void fragment_main() {
@@ -45,20 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureGather' : no matching overloaded function found 
-ERROR: 0:7: '=' :  cannot convert from ' const float' to ' temp mediump 4-component vector of float'
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp samplerCubeShadow arg_0_arg_1;
 
 void textureGatherCompare_182fd4() {
-  vec4 res = textureGather(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), 1.0f);
 }
 
 void compute_main() {
@@ -70,11 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGather' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureGatherCompare/6d9352.wgsl.expected.glsl b/test/tint/builtins/gen/textureGatherCompare/6d9352.wgsl.expected.glsl
index 723f83e..48a5176 100644
--- a/test/tint/builtins/gen/textureGatherCompare/6d9352.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureGatherCompare/6d9352.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureGatherCompare_6d9352() {
-  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec2(0.0f, 0.0f), 1.0f);
 }
 
 vec4 vertex_main() {
@@ -20,21 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGather' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureGatherCompare_6d9352() {
-  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec2(0.0f, 0.0f), 1.0f);
 }
 
 void fragment_main() {
@@ -45,20 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureGather' : no matching overloaded function found 
-ERROR: 0:7: '=' :  cannot convert from ' const float' to ' temp mediump 4-component vector of float'
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureGatherCompare_6d9352() {
-  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec2(0.0f, 0.0f), 1.0f);
 }
 
 void compute_main() {
@@ -70,11 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGather' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureGatherCompare/6f1267.wgsl.expected.glsl b/test/tint/builtins/gen/textureGatherCompare/6f1267.wgsl.expected.glsl
index 4012e12..cd264dc 100644
--- a/test/tint/builtins/gen/textureGatherCompare/6f1267.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureGatherCompare/6f1267.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureGatherCompare_6f1267() {
-  vec4 res = textureGatherOffset(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 1.0f), ivec2(0, 0));
+  vec4 res = textureGatherOffset(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 1.0f, ivec2(0, 0));
 }
 
 vec4 vertex_main() {
@@ -20,21 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGatherOffset' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureGatherCompare_6f1267() {
-  vec4 res = textureGatherOffset(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 1.0f), ivec2(0, 0));
+  vec4 res = textureGatherOffset(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 1.0f, ivec2(0, 0));
 }
 
 void fragment_main() {
@@ -45,20 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureGatherOffset' : no matching overloaded function found 
-ERROR: 0:7: '=' :  cannot convert from ' const float' to ' temp mediump 4-component vector of float'
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureGatherCompare_6f1267() {
-  vec4 res = textureGatherOffset(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 1.0f), ivec2(0, 0));
+  vec4 res = textureGatherOffset(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 1.0f, ivec2(0, 0));
 }
 
 void compute_main() {
@@ -70,11 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGatherOffset' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureGatherCompare/783e65.wgsl.expected.glsl b/test/tint/builtins/gen/textureGatherCompare/783e65.wgsl.expected.glsl
index 07f87a2..7567fcd 100644
--- a/test/tint/builtins/gen/textureGatherCompare/783e65.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureGatherCompare/783e65.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureGatherCompare_783e65() {
-  vec4 res = textureGather(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 1.0f);
 }
 
 vec4 vertex_main() {
@@ -20,21 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGather' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureGatherCompare_783e65() {
-  vec4 res = textureGather(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 1.0f);
 }
 
 void fragment_main() {
@@ -45,20 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureGather' : no matching overloaded function found 
-ERROR: 0:7: '=' :  cannot convert from ' const float' to ' temp mediump 4-component vector of float'
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureGatherCompare_783e65() {
-  vec4 res = textureGather(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 1.0f));
+  vec4 res = textureGather(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 1.0f);
 }
 
 void compute_main() {
@@ -70,11 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGather' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureGatherCompare/a5f587.wgsl.expected.glsl b/test/tint/builtins/gen/textureGatherCompare/a5f587.wgsl.expected.glsl
index 890c346..9cf8c88 100644
--- a/test/tint/builtins/gen/textureGatherCompare/a5f587.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureGatherCompare/a5f587.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureGatherCompare_a5f587() {
-  vec4 res = textureGatherOffset(arg_0_arg_1, vec3(0.0f, 0.0f, 1.0f), ivec2(0, 0));
+  vec4 res = textureGatherOffset(arg_0_arg_1, vec2(0.0f, 0.0f), 1.0f, ivec2(0, 0));
 }
 
 vec4 vertex_main() {
@@ -20,21 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGatherOffset' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureGatherCompare_a5f587() {
-  vec4 res = textureGatherOffset(arg_0_arg_1, vec3(0.0f, 0.0f, 1.0f), ivec2(0, 0));
+  vec4 res = textureGatherOffset(arg_0_arg_1, vec2(0.0f, 0.0f), 1.0f, ivec2(0, 0));
 }
 
 void fragment_main() {
@@ -45,20 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureGatherOffset' : no matching overloaded function found 
-ERROR: 0:7: '=' :  cannot convert from ' const float' to ' temp mediump 4-component vector of float'
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureGatherCompare_a5f587() {
-  vec4 res = textureGatherOffset(arg_0_arg_1, vec3(0.0f, 0.0f, 1.0f), ivec2(0, 0));
+  vec4 res = textureGatherOffset(arg_0_arg_1, vec2(0.0f, 0.0f), 1.0f, ivec2(0, 0));
 }
 
 void compute_main() {
@@ -70,11 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureGatherOffset' : no matching overloaded function found 
-ERROR: 0:6: '=' :  cannot convert from ' const float' to ' temp highp 4-component vector of float'
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 3 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureLoad/8acf41.wgsl.expected.glsl b/test/tint/builtins/gen/textureLoad/8acf41.wgsl.expected.glsl
index 52dc125..31b244c 100644
--- a/test/tint/builtins/gen/textureLoad/8acf41.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureLoad/8acf41.wgsl.expected.glsl
@@ -1,6 +1,6 @@
 SKIP: FAILED
 
-../../src/tint/writer/glsl/generator_impl.cc:2563 internal compiler error: Multiplanar external texture transform was not run.
+../../src/tint/writer/glsl/generator_impl.cc:2569 internal compiler error: Multiplanar external texture transform was not run.
 
 
 ********************************************************************
diff --git a/test/tint/builtins/gen/textureSampleLevel/979816.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/979816.wgsl.expected.glsl
index 52dc125..31b244c 100644
--- a/test/tint/builtins/gen/textureSampleLevel/979816.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/979816.wgsl.expected.glsl
@@ -1,6 +1,6 @@
 SKIP: FAILED
 
-../../src/tint/writer/glsl/generator_impl.cc:2563 internal compiler error: Multiplanar external texture transform was not run.
+../../src/tint/writer/glsl/generator_impl.cc:2569 internal compiler error: Multiplanar external texture transform was not run.
 
 
 ********************************************************************