Remove texture_external overload for textureSample and add textureSampleLevel

Removes the texture_external overload for textureSample and replaces it
with a texture_external overload of textureSampleLevel to match merged
spec. Adds a transform that adds the implicit level parameter to
textureSampleLevel. Modifies unit tests to reflect change.

Bug: dawn:728
Change-Id: I2dbc9232b4343db1075be79fda0054231860f3b1
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50701
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/intrinsic_table.cc b/src/intrinsic_table.cc
index 9881c20..16b9066 100644
--- a/src/intrinsic_table.cc
+++ b/src/intrinsic_table.cc
@@ -1214,7 +1214,6 @@
   Register(I::kTextureSample, f32,      {{t, tex_depth_cube},       {s, sampler}, {coords, vec3_f32},                                         }); // NOLINT
   Register(I::kTextureSample, f32,      {{t, tex_depth_cube_array}, {s, sampler}, {coords, vec3_f32}, {array_index, i32},                     }); // NOLINT
   Register(I::kTextureSample, vec4_f32, {{t, tex_external},         {s, sampler}, {coords, vec2_f32},                                         }); // NOLINT
-  Register(I::kTextureSample, vec4_f32, {{t, tex_external},         {s, sampler}, {coords, vec2_f32},                     {offset, vec2_i32}, }); // NOLINT
 
   Register(I::kTextureSampleBias, vec4_f32,    {{t, tex_2d_f32},           {s, sampler}, {coords, vec2_f32},                     {bias, f32},                     }); // NOLINT
   Register(I::kTextureSampleBias, vec4_f32,    {{t, tex_2d_f32},           {s, sampler}, {coords, vec2_f32},                     {bias, f32}, {offset, vec2_i32}, }); // NOLINT
@@ -1255,6 +1254,7 @@
   Register(I::kTextureSampleLevel, f32,          {{t, tex_depth_2d_array},  {s, sampler}, {coords, vec2_f32}, {array_index, i32}, {level, i32}, {offset, vec2_i32}, }); // NOLINT
   Register(I::kTextureSampleLevel, f32,          {{t, tex_depth_cube},      {s, sampler}, {coords, vec3_f32},                     {level, i32},                     }); // NOLINT
   Register(I::kTextureSampleLevel, f32,          {{t, tex_depth_cube_array},{s, sampler}, {coords, vec3_f32}, {array_index, i32}, {level, i32},                     }); // NOLINT
+  Register(I::kTextureSampleLevel, vec4_f32,     {{t, tex_external},        {s, sampler}, {coords, vec2_f32},                                                       }); // NOLINT
 
   Register(I::kTextureStore, void_, {{t, tex_storage_wo_1d_FT},      {coords, i32},                          {value, vec4_T}, }); // NOLINT
   Register(I::kTextureStore, void_, {{t, tex_storage_wo_2d_FT},      {coords, vec2_i32},                     {value, vec4_T}, }); // NOLINT
diff --git a/src/transform/external_texture_transform.cc b/src/transform/external_texture_transform.cc
index 755624b..d4095f5 100644
--- a/src/transform/external_texture_transform.cc
+++ b/src/transform/external_texture_transform.cc
@@ -35,40 +35,60 @@
   // generation paths in the backends.
 
   // When replacing instances of texture_external with texture_2d<f32> we must
-  // also modify calls to the texture_external overload of textureLoad, which
-  // unlike its texture_2d<f32> overload does not require a level parameter.
-  // To do this we identify calls to textureLoad that use texture_external as
-  // the first parameter and add a parameter for the level (which is always 0).
+  // also modify calls to the texture_external overloads of textureLoad and
+  // textureSampleLevel, which unlike their texture_2d<f32> overloads do not
+  // require a level parameter. To do this we identify calls to textureLoad and
+  // textureSampleLevel that use texture_external as the first parameter and add
+  // a parameter for the level (which is always 0).
 
-  // Scan the AST nodes for calls to textureLoad.
+  // Scan the AST nodes for calls to textureLoad or textureSampleLevel.
   for (auto* node : ctx.src->ASTNodes().Objects()) {
     if (auto* call_expr = node->As<ast::CallExpression>()) {
       if (auto* intrinsic =
               sem.Get(call_expr)->Target()->As<sem::Intrinsic>()) {
-        if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad) {
-          // When a textureLoad has been identified, check if the first
-          // parameter is an external texture.
+        if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad ||
+            intrinsic->Type() == sem::IntrinsicType::kTextureSampleLevel) {
+          // When a textureLoad or textureSampleLevel has been identified, check
+          // if the first parameter is an external texture.
           if (auto* var =
                   sem.Get(call_expr->params()[0])->As<sem::VariableUser>()) {
             if (var->Variable()->Type()->Is<sem::ExternalTexture>()) {
-              if (call_expr->params().size() != 2) {
+              if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad &&
+                  call_expr->params().size() != 2) {
                 TINT_ICE(ctx.dst->Diagnostics())
-                    << "expected TextureLoad call with a texture_external to "
+                    << "expected textureLoad call with a texture_external to "
                        "have 2 parameters, found "
                     << call_expr->params().size() << " parameters";
               }
-              // Replace the textureLoad call with another that has the same
-              // parameters in addition to a signed integer literal for the
-              // level parameter.
+
+              if (intrinsic->Type() ==
+                      sem::IntrinsicType::kTextureSampleLevel &&
+                  call_expr->params().size() != 3) {
+                TINT_ICE(ctx.dst->Diagnostics())
+                    << "expected textureSampleLevel call with a "
+                       "texture_external to have 3 parameters, found "
+                    << call_expr->params().size() << " parameters";
+              }
+
+              // Replace the call with another that has the same parameters in
+              // addition to a level parameter (always zero for external
+              // textures).
               auto* exp = ctx.Clone(call_expr->func());
-
               auto* externalTextureParam = ctx.Clone(call_expr->params()[0]);
-              auto* coordsParam = ctx.Clone(call_expr->params()[1]);
-              // Level is always 0 for an external texture.
-              auto* levelParam = ctx.dst->Expr(0);
 
-              ast::ExpressionList params = {externalTextureParam, coordsParam,
-                                            levelParam};
+              ast::ExpressionList params;
+              if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad) {
+                auto* coordsParam = ctx.Clone(call_expr->params()[1]);
+                auto* levelParam = ctx.dst->Expr(0);
+                params = {externalTextureParam, coordsParam, levelParam};
+              } else if (intrinsic->Type() ==
+                         sem::IntrinsicType::kTextureSampleLevel) {
+                auto* samplerParam = ctx.Clone(call_expr->params()[1]);
+                auto* coordsParam = ctx.Clone(call_expr->params()[2]);
+                auto* levelParam = ctx.dst->Expr(0.0f);
+                params = {externalTextureParam, samplerParam, coordsParam,
+                          levelParam};
+              }
 
               auto* newCall = ctx.dst->create<ast::CallExpression>(exp, params);
               ctx.Replace(call_expr, newCall);
diff --git a/src/transform/external_texture_transform_test.cc b/src/transform/external_texture_transform_test.cc
index 9e9cf24..d9fef8f 100644
--- a/src/transform/external_texture_transform_test.cc
+++ b/src/transform/external_texture_transform_test.cc
@@ -22,7 +22,7 @@
 
 using ExternalTextureTransformTest = TransformTest;
 
-TEST_F(ExternalTextureTransformTest, SampleSinglePlane) {
+TEST_F(ExternalTextureTransformTest, SampleLevelSinglePlane) {
   auto* src = R"(
 [[group(0), binding(0)]] var s : sampler;
 
@@ -30,7 +30,7 @@
 
 [[stage(fragment)]]
 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
-  return textureSample(t, s, (coord.xy / vec2<f32>(4.0, 4.0)));
+  return textureSampleLevel(t, s, (coord.xy / vec2<f32>(4.0, 4.0)));
 }
 )";
 
@@ -41,7 +41,7 @@
 
 [[stage(fragment)]]
 fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
-  return textureSample(t, s, (coord.xy / vec2<f32>(4.0, 4.0)));
+  return textureSampleLevel(t, s, (coord.xy / vec2<f32>(4.0, 4.0)), 0.0);
 }
 )";