GLSL: fix textureSampleLevel() on depth textures.

Multiple bugs here:

1) Like texture(), GLSL's textureLod() on depth textures returns a
   scalar, and does not need to be swizzled. So set glsl_ret_width to
   1 in that case.
2) Like texture(), GLSL's textureLod() always requires a Dref
   parameter, so append a zero if not present.
3) GLSL's "lod" parameter to textureLod() is always a float, unlike
   WGSL's textureSampleLevel() which is an i32 for depth textures,
   so cast it.

Along the way, I discovered that textureLod() is not supported on
samplerCubeShadow or sampler2DArrayShadow (even on Desktop GL). So some
tests will never pass. Logged as https://crbug.com/dawn/1313

Bug: tint:1456
Change-Id: If67d8d288704142278d7a4e52b46e8010776f381
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/82300
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
diff --git a/src/tint/writer/glsl/generator_impl.cc b/src/tint/writer/glsl/generator_impl.cc
index 0df3c2c..8091e37 100644
--- a/src/tint/writer/glsl/generator_impl.cc
+++ b/src/tint/writer/glsl/generator_impl.cc
@@ -1369,6 +1369,9 @@
       break;
     case sem::BuiltinType::kTextureSampleLevel:
       out << "textureLod";
+      if (is_depth) {
+        glsl_ret_width = 1u;
+      }
       break;
     case sem::BuiltinType::kTextureGather:
     case sem::BuiltinType::kTextureGatherCompare:
@@ -1427,7 +1430,8 @@
     if (auto* depth_ref = arg(Usage::kDepthRef)) {
       param_coords =
           AppendVector(&builder_, param_coords, depth_ref)->Declaration();
-    } else if (builtin->Type() == sem::BuiltinType::kTextureSample) {
+    } else if (builtin->Type() == sem::BuiltinType::kTextureSample ||
+               builtin->Type() == sem::BuiltinType::kTextureSampleLevel) {
       // Sampling a depth texture in GLSL always requires a depth reference, so
       // append zero here.
       auto* f32 = builder_.create<sem::F32>();
@@ -1448,7 +1452,15 @@
                      Usage::kSampleIndex, Usage::kValue}) {
     if (auto* e = arg(usage)) {
       out << ", ";
-      if (!EmitExpression(out, e)) {
+      if (usage == Usage::kLevel && is_depth) {
+        // WGSL's textureSampleLevel() "level" param is i32 for depth textures,
+        // whereas GLSL's textureLod() "lod" param is always float, so cast it.
+        out << "float(";
+        if (!EmitExpression(out, e)) {
+          return false;
+        }
+        out << ")";
+      } else if (!EmitExpression(out, e)) {
         return false;
       }
     }
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 f06aee7..9735ee8 100644
--- a/src/tint/writer/glsl/generator_impl_builtin_texture_test.cc
+++ b/src/tint/writer/glsl/generator_impl_builtin_texture_test.cc
@@ -182,17 +182,17 @@
     case ValidTextureOverload::kSampleLevelCubeArrayF32:
       return R"(textureLod(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, float(4)), 5.0f);)";
     case ValidTextureOverload::kSampleLevelDepth2dF32:
-      return R"(textureLod(tint_symbol_sampler, vec2(1.0f, 2.0f), 3).x;)";
+      return R"(textureLod(tint_symbol_sampler, vec3(1.0f, 2.0f, 0.0f), float(3));)";
     case ValidTextureOverload::kSampleLevelDepth2dOffsetF32:
-      return R"(textureLodOffset(tint_symbol_sampler, vec2(1.0f, 2.0f), 3, ivec2(4, 5)).x;)";
+      return R"(textureLodOffset(tint_symbol_sampler, vec3(1.0f, 2.0f, 0.0f), float(3), ivec2(4, 5));)";
     case ValidTextureOverload::kSampleLevelDepth2dArrayF32:
-      return R"(textureLod(tint_symbol_sampler, vec3(1.0f, 2.0f, float(3)), 4).x;)";
+      return R"(textureLod(tint_symbol_sampler, vec4(1.0f, 2.0f, float(3), 0.0f), float(4));)";
     case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32:
-      return R"(textureLodOffset(tint_symbol_sampler, vec3(1.0f, 2.0f, float(3)), 4, ivec2(5, 6)).x;)";
+      return R"(textureLodOffset(tint_symbol_sampler, vec4(1.0f, 2.0f, float(3), 0.0f), float(4), ivec2(5, 6));)";
     case ValidTextureOverload::kSampleLevelDepthCubeF32:
-      return R"(textureLod(tint_symbol_sampler, vec3(1.0f, 2.0f, 3.0f), 4).x;)";
+      return R"(textureLod(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, 0.0f), float(4)))";
     case ValidTextureOverload::kSampleLevelDepthCubeArrayF32:
-      return R"(textureLod(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, float(4)), 5).x;)";
+      return R"(textureLod(tint_symbol_sampler, vec4(1.0f, 2.0f, 3.0f, float(4)), float(5));)";
     case ValidTextureOverload::kSampleGrad2dF32:
       return R"(textureGrad(tint_symbol_sampler, vec2(1.0f, 2.0f), vec2(3.0f, 4.0f), vec2(5.0f, 6.0f));)";
     case ValidTextureOverload::kSampleGrad2dOffsetF32:
diff --git a/test/tint/builtins/gen/textureDimensions/ba1481.wgsl.expected.glsl b/test/tint/builtins/gen/textureDimensions/ba1481.wgsl.expected.glsl
index d158448..52dc125 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:2553 internal compiler error: Multiplanar external texture transform was not run.
+../../src/tint/writer/glsl/generator_impl.cc:2563 internal compiler error: Multiplanar external texture transform was not run.
 
 
 ********************************************************************
diff --git a/test/tint/builtins/gen/textureLoad/8acf41.wgsl.expected.glsl b/test/tint/builtins/gen/textureLoad/8acf41.wgsl.expected.glsl
index d158448..52dc125 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:2553 internal compiler error: Multiplanar external texture transform was not run.
+../../src/tint/writer/glsl/generator_impl.cc:2563 internal compiler error: Multiplanar external texture transform was not run.
 
 
 ********************************************************************
diff --git a/test/tint/builtins/gen/textureSampleLevel/02be59.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/02be59.wgsl.expected.glsl
index 73e17ed..cd1f4da 100644
--- a/test/tint/builtins/gen/textureSampleLevel/02be59.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/02be59.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureSampleLevel_02be59() {
-  float res = textureLod(arg_0_arg_1, vec2(0.0f, 0.0f), 0).x;
+  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), float(0));
 }
 
 vec4 vertex_main() {
@@ -20,20 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureLod' : no matching overloaded function found 
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureSampleLevel_02be59() {
-  float res = textureLod(arg_0_arg_1, vec2(0.0f, 0.0f), 0).x;
+  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), float(0));
 }
 
 void fragment_main() {
@@ -44,19 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureLod' : no matching overloaded function found 
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureSampleLevel_02be59() {
-  float res = textureLod(arg_0_arg_1, vec2(0.0f, 0.0f), 0).x;
+  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), float(0));
 }
 
 void compute_main() {
@@ -68,10 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureLod' : no matching overloaded function found 
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureSampleLevel/1b0291.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/1b0291.wgsl.expected.glsl
index 09bec15..b6aa9de 100644
--- a/test/tint/builtins/gen/textureSampleLevel/1b0291.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/1b0291.wgsl.expected.glsl
@@ -5,7 +5,7 @@
 uniform highp samplerCubeShadow arg_0_arg_1;
 
 void textureSampleLevel_1b0291() {
-  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, 0.0f), float(0));
 }
 
 vec4 vertex_main() {
@@ -33,7 +33,7 @@
 uniform highp samplerCubeShadow arg_0_arg_1;
 
 void textureSampleLevel_1b0291() {
-  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, 0.0f), float(0));
 }
 
 void fragment_main() {
@@ -56,7 +56,7 @@
 uniform highp samplerCubeShadow arg_0_arg_1;
 
 void textureSampleLevel_1b0291() {
-  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, 0.0f), float(0));
 }
 
 void compute_main() {
diff --git a/test/tint/builtins/gen/textureSampleLevel/1bf73e.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/1bf73e.wgsl.expected.glsl
index 64e0b83..277310c 100644
--- a/test/tint/builtins/gen/textureSampleLevel/1bf73e.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/1bf73e.wgsl.expected.glsl
@@ -5,7 +5,7 @@
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_1bf73e() {
-  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 0.0f), float(0));
 }
 
 vec4 vertex_main() {
@@ -33,7 +33,7 @@
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_1bf73e() {
-  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 0.0f), float(0));
 }
 
 void fragment_main() {
@@ -56,7 +56,7 @@
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_1bf73e() {
-  float res = textureLod(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 0.0f), float(0));
 }
 
 void compute_main() {
diff --git a/test/tint/builtins/gen/textureSampleLevel/47daa4.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/47daa4.wgsl.expected.glsl
index f5f4d2d..bd35b54 100644
--- a/test/tint/builtins/gen/textureSampleLevel/47daa4.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/47daa4.wgsl.expected.glsl
@@ -1,11 +1,9 @@
-SKIP: FAILED
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureSampleLevel_47daa4() {
-  float res = textureLodOffset(arg_0_arg_1, vec2(0.0f, 0.0f), 0, ivec2(0, 0)).x;
+  float res = textureLodOffset(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), float(0), ivec2(0, 0));
 }
 
 vec4 vertex_main() {
@@ -20,20 +18,13 @@
   gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureLodOffset' : no matching overloaded function found 
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-
 #version 310 es
 precision mediump float;
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureSampleLevel_47daa4() {
-  float res = textureLodOffset(arg_0_arg_1, vec2(0.0f, 0.0f), 0, ivec2(0, 0)).x;
+  float res = textureLodOffset(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), float(0), ivec2(0, 0));
 }
 
 void fragment_main() {
@@ -44,19 +35,12 @@
   fragment_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:7: 'textureLodOffset' : no matching overloaded function found 
-ERROR: 0:7: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-
 #version 310 es
 
 uniform highp sampler2DShadow arg_0_arg_1;
 
 void textureSampleLevel_47daa4() {
-  float res = textureLodOffset(arg_0_arg_1, vec2(0.0f, 0.0f), 0, ivec2(0, 0)).x;
+  float res = textureLodOffset(arg_0_arg_1, vec3(0.0f, 0.0f, 0.0f), float(0), ivec2(0, 0));
 }
 
 void compute_main() {
@@ -68,10 +52,3 @@
   compute_main();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:6: 'textureLodOffset' : no matching overloaded function found 
-ERROR: 0:6: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-
diff --git a/test/tint/builtins/gen/textureSampleLevel/979816.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/979816.wgsl.expected.glsl
index d158448..52dc125 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:2553 internal compiler error: Multiplanar external texture transform was not run.
+../../src/tint/writer/glsl/generator_impl.cc:2563 internal compiler error: Multiplanar external texture transform was not run.
 
 
 ********************************************************************
diff --git a/test/tint/builtins/gen/textureSampleLevel/ae5e39.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/ae5e39.wgsl.expected.glsl
index eea7343..e36edf6 100644
--- a/test/tint/builtins/gen/textureSampleLevel/ae5e39.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/ae5e39.wgsl.expected.glsl
@@ -5,7 +5,7 @@
 uniform highp samplerCubeArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_ae5e39() {
-  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, float(1)), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, float(1)), float(0));
 }
 
 vec4 vertex_main() {
@@ -33,7 +33,7 @@
 uniform highp samplerCubeArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_ae5e39() {
-  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, float(1)), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, float(1)), float(0));
 }
 
 void fragment_main() {
@@ -56,7 +56,7 @@
 uniform highp samplerCubeArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_ae5e39() {
-  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, float(1)), 0).x;
+  float res = textureLod(arg_0_arg_1, vec4(0.0f, 0.0f, 0.0f, float(1)), float(0));
 }
 
 void compute_main() {
diff --git a/test/tint/builtins/gen/textureSampleLevel/ba93b3.wgsl.expected.glsl b/test/tint/builtins/gen/textureSampleLevel/ba93b3.wgsl.expected.glsl
index 53360a2..5547d35 100644
--- a/test/tint/builtins/gen/textureSampleLevel/ba93b3.wgsl.expected.glsl
+++ b/test/tint/builtins/gen/textureSampleLevel/ba93b3.wgsl.expected.glsl
@@ -5,7 +5,7 @@
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_ba93b3() {
-  float res = textureLodOffset(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 0, ivec2(0, 0)).x;
+  float res = textureLodOffset(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 0.0f), float(0), ivec2(0, 0));
 }
 
 vec4 vertex_main() {
@@ -33,7 +33,7 @@
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_ba93b3() {
-  float res = textureLodOffset(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 0, ivec2(0, 0)).x;
+  float res = textureLodOffset(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 0.0f), float(0), ivec2(0, 0));
 }
 
 void fragment_main() {
@@ -56,7 +56,7 @@
 uniform highp sampler2DArrayShadow arg_0_arg_1;
 
 void textureSampleLevel_ba93b3() {
-  float res = textureLodOffset(arg_0_arg_1, vec3(0.0f, 0.0f, float(1)), 0, ivec2(0, 0)).x;
+  float res = textureLodOffset(arg_0_arg_1, vec4(0.0f, 0.0f, float(1), 0.0f), float(0), ivec2(0, 0));
 }
 
 void compute_main() {
diff --git a/test/tint/unittest/reader/spirv/ImageSampleExplicitLod_DepthTexture_SpvParserHandleTest_SampledImageAccessTest_Variable_1.spvasm.expected.glsl b/test/tint/unittest/reader/spirv/ImageSampleExplicitLod_DepthTexture_SpvParserHandleTest_SampledImageAccessTest_Variable_1.spvasm.expected.glsl
index b8afb19..946f2cd 100644
--- a/test/tint/unittest/reader/spirv/ImageSampleExplicitLod_DepthTexture_SpvParserHandleTest_SampledImageAccessTest_Variable_1.spvasm.expected.glsl
+++ b/test/tint/unittest/reader/spirv/ImageSampleExplicitLod_DepthTexture_SpvParserHandleTest_SampledImageAccessTest_Variable_1.spvasm.expected.glsl
@@ -1,5 +1,3 @@
-SKIP: FAILED
-
 #version 310 es
 precision mediump float;
 
@@ -23,7 +21,7 @@
   vec2 coords12 = vf12;
   vec3 coords123 = vf123;
   vec4 coords1234 = vf1234;
-  vec4 x_79 = vec4(textureLod(x_20_x_10, vf12, int(f1)).x, 0.0f, 0.0f, 0.0f);
+  vec4 x_79 = vec4(textureLod(x_20_x_10, vec3(vf12, 0.0f), float(int(f1))), 0.0f, 0.0f, 0.0f);
   return;
 }
 
@@ -35,10 +33,3 @@
   tint_symbol();
   return;
 }
-Error parsing GLSL shader:
-ERROR: 0:24: 'textureLod' : no matching overloaded function found 
-ERROR: 0:24: '' : compilation terminated 
-ERROR: 2 compilation errors.  No code generated.
-
-
-