spirv-reader: Reject dref sampling with Bias or Grad

Bias and Grad image operands are not supported for
depth-referencde sampling.

Fixed: tint:440
Change-Id: Ic2bf995f7c7b791b8e3a7ceb3514b10220dda8a7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/37062
Auto-Submit: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc
index 69ec724..ec99005 100644
--- a/src/reader/spirv/function.cc
+++ b/src/reader/spirv/function.cc
@@ -4078,6 +4078,7 @@
 
   std::string builtin_name;
   bool use_level_of_detail_suffix = true;
+  bool is_dref = false;
   switch (opcode) {
     case SpvOpImageSampleImplicitLod:
     case SpvOpImageSampleExplicitLod:
@@ -4085,6 +4086,7 @@
       break;
     case SpvOpImageSampleDrefImplicitLod:
     case SpvOpImageSampleDrefExplicitLod:
+      is_dref = true;
       builtin_name = "textureSampleCompare";
       if (arg_index < num_args) {
         params.push_back(MakeOperand(inst, arg_index).expr);
@@ -4141,6 +4143,11 @@
   }
   if (arg_index < num_args &&
       (image_operands_mask & SpvImageOperandsBiasMask)) {
+    if (is_dref) {
+      return Fail() << "WGSL does not support depth-reference sampling with "
+                       "level-of-detail bias: "
+                    << inst.PrettyPrint();
+    }
     builtin_name += "Bias";
     params.push_back(MakeOperand(inst, arg_index).expr);
     image_operands_mask ^= SpvImageOperandsBiasMask;
@@ -4162,6 +4169,11 @@
   }
   if (arg_index + 1 < num_args &&
       (image_operands_mask & SpvImageOperandsGradMask)) {
+    if (is_dref) {
+      return Fail() << "WGSL does not support depth-reference sampling with "
+                       "explicit gradient: "
+                    << inst.PrettyPrint();
+    }
     builtin_name += "Grad";
     params.push_back(MakeOperand(inst, arg_index).expr);
     params.push_back(MakeOperand(inst, arg_index + 1).expr);
diff --git a/src/reader/spirv/parser_impl_handle_test.cc b/src/reader/spirv/parser_impl_handle_test.cc
index 5cccc1a..619bfe3 100644
--- a/src/reader/spirv/parser_impl_handle_test.cc
+++ b/src/reader/spirv/parser_impl_handle_test.cc
@@ -1702,7 +1702,7 @@
 
 INSTANTIATE_TEST_SUITE_P(
     // This test shows the use of a sampled image used with both regular
-    // sampling and depth-refernce sampling.  The texture is a depth-texture,
+    // sampling and depth-reference sampling.  The texture is a depth-texture,
     // and we use builtins textureSample and textureSampleCompare
     ImageSampleImplicitLod_BothDrefAndNonDref,
     SpvParserTest_SampledImageAccessTest,
@@ -4428,6 +4428,44 @@
          "ConstOffset is only permitted for 2D, 2D Arrayed, and 3D textures: ",
          {}}}));
 
+INSTANTIATE_TEST_SUITE_P(
+    ImageSampleDref_Bias_IsError,
+    SpvParserTest_ImageCoordsTest,
+    ::testing::ValuesIn(std::vector<ImageCoordsCase>{
+        // Implicit Lod
+        {"%float 2D 1 0 0 1 Unknown",
+         "%result = OpImageSampleDrefImplicitLod %float %sampled_image %vf1234 "
+         "%depth Bias %float_null",
+         "WGSL does not support depth-reference sampling with level-of-detail "
+         "bias: ",
+         {}},
+        // Explicit Lod
+        {"%float 2D 1 0 0 1 Unknown",
+         "%result = OpImageSampleDrefExplicitLod %float %sampled_image %vf1234 "
+         "%depth Lod|Bias %float_null %float_null",
+         "WGSL does not support depth-reference sampling with level-of-detail "
+         "bias: ",
+         {}}}));
+
+INSTANTIATE_TEST_SUITE_P(
+    ImageSampleDref_Grad_IsError,
+    SpvParserTest_ImageCoordsTest,
+    ::testing::ValuesIn(std::vector<ImageCoordsCase>{
+        // Implicit Lod
+        {"%float 2D 1 0 0 1 Unknown",
+         "%result = OpImageSampleDrefImplicitLod %float %sampled_image %vf1234 "
+         "%depth Grad %float_1 %float_2",
+         "WGSL does not support depth-reference sampling with explicit "
+         "gradient: ",
+         {}},
+        // Explicit Lod
+        {"%float 2D 1 0 0 1 Unknown",
+         "%result = OpImageSampleDrefExplicitLod %float %sampled_image %vf1234 "
+         "%depth Lod|Grad %float_null %float_1  %float_2",
+         "WGSL does not support depth-reference sampling with explicit "
+         "gradient: ",
+         {}}}));
+
 }  // namespace
 }  // namespace spirv
 }  // namespace reader