spirv-reader: textureLoad explicit Lod sometimes

WGSL requires an explicit level-of-detail for textureLoad
for sampled textures and depth textures

Fixed: tint:462
Change-Id: I43758b002da91af9901d12664861ace971833020
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38828
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Auto-Submit: David Neto <dneto@google.com>
diff --git a/src/reader/spirv/function.cc b/src/reader/spirv/function.cc
index 6ca91fa..596f90e 100644
--- a/src/reader/spirv/function.cc
+++ b/src/reader/spirv/function.cc
@@ -68,6 +68,7 @@
 #include "src/type/i32_type.h"
 #include "src/type/matrix_type.h"
 #include "src/type/pointer_type.h"
+#include "src/type/sampled_texture_type.h"
 #include "src/type/storage_texture_type.h"
 #include "src/type/texture_type.h"
 #include "src/type/type.h"
@@ -4212,6 +4213,12 @@
     params.push_back(lod.expr);
     image_operands_mask ^= SpvImageOperandsLodMask;
     arg_index++;
+  } else if ((opcode == SpvOpImageFetch) &&
+             (texture_type->Is<type::SampledTexture>() ||
+              texture_type->Is<type::DepthTexture>())) {
+    // textureLoad on sampled texture and depth texture requires an explicit
+    // level-of-detail parameter.
+    params.push_back(parser_impl_.MakeNullValue(i32_));
   }
   if (arg_index + 1 < num_args &&
       (image_operands_mask & SpvImageOperandsGradMask)) {
diff --git a/src/reader/spirv/parser_impl_handle_test.cc b/src/reader/spirv/parser_impl_handle_test.cc
index 1c95533..2977958 100644
--- a/src/reader/spirv/parser_impl_handle_test.cc
+++ b/src/reader/spirv/parser_impl_handle_test.cc
@@ -3036,7 +3036,8 @@
     ImageFetch_OptionalParams,
     SpvParserTest_ImageAccessTest,
     ::testing::ValuesIn(std::vector<ImageAccessCase>{
-        // OpImageFetch with no extra params
+        // OpImageFetch with no extra params, on sampled texture
+        // Level of detail is injected for sampled texture
         {"%float 2D 0 0 0 1 Unknown", "%99 = OpImageFetch %v4float %im %vi12",
          R"(Variable{
     Decorations{
@@ -3058,12 +3059,13 @@
             (
               Identifier[not set]{x_20}
               Identifier[not set]{vi12}
+              ScalarConstructor[not set]{0}
             )
           }
         }
       }
     })"},
-        // OpImageFetch with explicit level
+        // OpImageFetch with explicit level, on sampled texture
         {"%float 2D 0 0 0 1 Unknown",
          "%99 = OpImageFetch %v4float %im %vi12 Lod %int_3",
          R"(Variable{
@@ -3091,6 +3093,76 @@
           }
         }
       }
+    })"},
+        // OpImageFetch with no extra params, on depth texture
+        // Level of detail is injected for depth texture
+        {"%float 2D 1 0 0 1 Unknown", "%99 = OpImageFetch %v4float %im %vi12",
+         R"(Variable{
+    Decorations{
+      GroupDecoration{2}
+      BindingDecoration{1}
+    }
+    x_20
+    uniform_constant
+    __depth_texture_2d
+  })",
+         R"(VariableDeclStatement{
+      VariableConst{
+        x_99
+        none
+        __vec_4__f32
+        {
+          TypeConstructor[not set]{
+            __vec_4__f32
+            Call[not set]{
+              Identifier[not set]{textureLoad}
+              (
+                Identifier[not set]{x_20}
+                Identifier[not set]{vi12}
+                ScalarConstructor[not set]{0}
+              )
+            }
+            ScalarConstructor[not set]{0.000000}
+            ScalarConstructor[not set]{0.000000}
+            ScalarConstructor[not set]{0.000000}
+          }
+        }
+      }
+    })"},
+        // OpImageFetch with extra params, on depth texture
+        {"%float 2D 1 0 0 1 Unknown",
+         "%99 = OpImageFetch %v4float %im %vi12 Lod %int_3",
+         R"(Variable{
+    Decorations{
+      GroupDecoration{2}
+      BindingDecoration{1}
+    }
+    x_20
+    uniform_constant
+    __depth_texture_2d
+  })",
+         R"(VariableDeclStatement{
+      VariableConst{
+        x_99
+        none
+        __vec_4__f32
+        {
+          TypeConstructor[not set]{
+            __vec_4__f32
+            Call[not set]{
+              Identifier[not set]{textureLoad}
+              (
+                Identifier[not set]{x_20}
+                Identifier[not set]{vi12}
+                ScalarConstructor[not set]{3}
+              )
+            }
+            ScalarConstructor[not set]{0.000000}
+            ScalarConstructor[not set]{0.000000}
+            ScalarConstructor[not set]{0.000000}
+          }
+        }
+      }
     })"}}));
 
 INSTANTIATE_TEST_SUITE_P(ImageFetch_Depth,
@@ -3125,6 +3197,7 @@
               (
                 Identifier[not set]{x_20}
                 Identifier[not set]{vi12}
+                ScalarConstructor[not set]{0}
               )
             }
             ScalarConstructor[not set]{0.000000}
@@ -3284,6 +3357,7 @@
             (
               Identifier[not set]{x_20}
               Identifier[not set]{vi12}
+              ScalarConstructor[not set]{0}
             )
           }
         }
@@ -3311,6 +3385,7 @@
             (
               Identifier[not set]{x_20}
               Identifier[not set]{vi12}
+              ScalarConstructor[not set]{0}
             )
           }
         }
@@ -3339,6 +3414,7 @@
               (
                 Identifier[not set]{x_20}
                 Identifier[not set]{vi12}
+                ScalarConstructor[not set]{0}
               )
             }
           }
@@ -3367,6 +3443,7 @@
             (
               Identifier[not set]{x_20}
               Identifier[not set]{vi12}
+              ScalarConstructor[not set]{0}
             )
           }
         }
@@ -3395,6 +3472,7 @@
               (
                 Identifier[not set]{x_20}
                 Identifier[not set]{vi12}
+                ScalarConstructor[not set]{0}
               )
             }
           }