Transform textureLoad calls for texture_external

Adds a transform that adds an additional 'level' parameter to textureLoad calls
when used with a texture_external. Adds a test for the textureLoad
transform. Additionally adds a test for calls to textureDimensions with
a texture_external.

Bug: dawn:728
Change-Id: I613ce8185e9a4c49529fd8e48323e586c95dde04
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50420
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Brandon Jones 2021-05-11 10:35:43 +00:00 committed by Commit Bot service account
parent 5a839b5818
commit 97a7d266e0
2 changed files with 107 additions and 1 deletions

View File

@ -13,7 +13,10 @@
// limitations under the License. // limitations under the License.
#include "src/transform/external_texture_transform.h" #include "src/transform/external_texture_transform.h"
#include "src/program_builder.h" #include "src/program_builder.h"
#include "src/sem/call.h"
#include "src/sem/variable.h"
namespace tint { namespace tint {
namespace transform { namespace transform {
@ -24,6 +27,57 @@ ExternalTextureTransform::~ExternalTextureTransform() = default;
Output ExternalTextureTransform::Run(const Program* in, const DataMap&) { Output ExternalTextureTransform::Run(const Program* in, const DataMap&) {
ProgramBuilder out; ProgramBuilder out;
CloneContext ctx(&out, in); CloneContext ctx(&out, in);
auto& sem = ctx.src->Sem();
// Within this transform, usages of texture_external are replaced with a
// texture_2d<f32>, which will allow us perform operations on a
// texture_external without maintaining texture_external-specific code
// 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).
// Scan the AST nodes for calls to textureLoad.
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 (auto* var =
sem.Get(call_expr->params()[0])->As<sem::VariableUser>()) {
if (var->Variable()->Type()->Is<sem::ExternalTexture>()) {
if (call_expr->params().size() != 2) {
TINT_ICE(ctx.dst->Diagnostics())
<< "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.
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};
auto* newCall = ctx.dst->create<ast::CallExpression>(exp, params);
ctx.Replace(call_expr, newCall);
}
}
}
}
}
}
// Scan the AST nodes for external texture declarations. // Scan the AST nodes for external texture declarations.
for (auto* node : ctx.src->ASTNodes().Objects()) { for (auto* node : ctx.src->ASTNodes().Objects()) {

View File

@ -22,7 +22,7 @@ namespace {
using ExternalTextureTransformTest = TransformTest; using ExternalTextureTransformTest = TransformTest;
TEST_F(ExternalTextureTransformTest, SinglePlane) { TEST_F(ExternalTextureTransformTest, SampleSinglePlane) {
auto* src = R"( auto* src = R"(
[[group(0), binding(0)]] var s : sampler; [[group(0), binding(0)]] var s : sampler;
@ -50,6 +50,58 @@ fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
EXPECT_EQ(expect, str(got)); EXPECT_EQ(expect, str(got));
} }
TEST_F(ExternalTextureTransformTest, LoadSinglePlane) {
auto* src = R"(
[[group(0), binding(0)]] var t : texture_external;
[[stage(fragment)]]
fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
return textureLoad(t, vec2<i32>(1, 1));
}
)";
auto* expect = R"(
[[group(0), binding(0)]] var t : texture_2d<f32>;
[[stage(fragment)]]
fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
return textureLoad(t, vec2<i32>(1, 1), 0);
}
)";
auto got = Run<ExternalTextureTransform>(src);
EXPECT_EQ(expect, str(got));
}
TEST_F(ExternalTextureTransformTest, DimensionsSinglePlane) {
auto* src = R"(
[[group(0), binding(0)]] var t : texture_external;
[[stage(fragment)]]
fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
var dim : vec2<i32>;
dim = textureDimensions(t);
return vec4<f32>(0.0, 0.0, 0.0, 0.0);
}
)";
auto* expect = R"(
[[group(0), binding(0)]] var t : texture_2d<f32>;
[[stage(fragment)]]
fn main([[builtin(position)]] coord : vec4<f32>) -> [[location(0)]] vec4<f32> {
var dim : vec2<i32>;
dim = textureDimensions(t);
return vec4<f32>(0.0, 0.0, 0.0, 0.0);
}
)";
auto got = Run<ExternalTextureTransform>(src);
EXPECT_EQ(expect, str(got));
}
} // namespace } // namespace
} // namespace transform } // namespace transform
} // namespace tint } // namespace tint