blob: 4dcf1feb3bd0cf3de4786845a1c98c19f0c7d324 [file] [log] [blame]
Brandon Jonesb432f232021-05-05 16:38:12 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/transform/external_texture_transform.h"
Brandon Jones97a7d262021-05-11 10:35:43 +000016
Brandon Jonesb432f232021-05-05 16:38:12 +000017#include "src/program_builder.h"
Brandon Jones97a7d262021-05-11 10:35:43 +000018#include "src/sem/call.h"
19#include "src/sem/variable.h"
Brandon Jonesb432f232021-05-05 16:38:12 +000020
Ben Claytonb5cd10c2021-06-25 10:26:26 +000021TINT_INSTANTIATE_TYPEINFO(tint::transform::ExternalTextureTransform);
22
Brandon Jonesb432f232021-05-05 16:38:12 +000023namespace tint {
24namespace transform {
25
26ExternalTextureTransform::ExternalTextureTransform() = default;
27ExternalTextureTransform::~ExternalTextureTransform() = default;
28
Ben Claytonb5cd10c2021-06-25 10:26:26 +000029void ExternalTextureTransform::Run(CloneContext& ctx,
30 const DataMap&,
31 DataMap&) {
Brandon Jones97a7d262021-05-11 10:35:43 +000032 auto& sem = ctx.src->Sem();
33
34 // Within this transform, usages of texture_external are replaced with a
35 // texture_2d<f32>, which will allow us perform operations on a
36 // texture_external without maintaining texture_external-specific code
37 // generation paths in the backends.
38
39 // When replacing instances of texture_external with texture_2d<f32> we must
Brandon Jonesecd31c52021-05-12 10:37:03 +000040 // also modify calls to the texture_external overloads of textureLoad and
41 // textureSampleLevel, which unlike their texture_2d<f32> overloads do not
42 // require a level parameter. To do this we identify calls to textureLoad and
43 // textureSampleLevel that use texture_external as the first parameter and add
44 // a parameter for the level (which is always 0).
Brandon Jones97a7d262021-05-11 10:35:43 +000045
Brandon Jonesecd31c52021-05-12 10:37:03 +000046 // Scan the AST nodes for calls to textureLoad or textureSampleLevel.
Brandon Jones97a7d262021-05-11 10:35:43 +000047 for (auto* node : ctx.src->ASTNodes().Objects()) {
48 if (auto* call_expr = node->As<ast::CallExpression>()) {
49 if (auto* intrinsic =
50 sem.Get(call_expr)->Target()->As<sem::Intrinsic>()) {
Brandon Jonesecd31c52021-05-12 10:37:03 +000051 if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad ||
52 intrinsic->Type() == sem::IntrinsicType::kTextureSampleLevel) {
53 // When a textureLoad or textureSampleLevel has been identified, check
54 // if the first parameter is an external texture.
Brandon Jones97a7d262021-05-11 10:35:43 +000055 if (auto* var =
Ben Clayton4f3ff572021-10-15 17:33:10 +000056 sem.Get(call_expr->args[0])->As<sem::VariableUser>()) {
Ben Clayton9b54a2e2021-05-18 10:28:48 +000057 if (var->Variable()
58 ->Type()
59 ->UnwrapRef()
60 ->Is<sem::ExternalTexture>()) {
Brandon Jonesecd31c52021-05-12 10:37:03 +000061 if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad &&
Ben Clayton4f3ff572021-10-15 17:33:10 +000062 call_expr->args.size() != 2) {
Ben Claytonffd28e22021-06-24 11:27:36 +000063 TINT_ICE(Transform, ctx.dst->Diagnostics())
Brandon Jonesecd31c52021-05-12 10:37:03 +000064 << "expected textureLoad call with a texture_external to "
Brandon Jones97a7d262021-05-11 10:35:43 +000065 "have 2 parameters, found "
Ben Clayton4f3ff572021-10-15 17:33:10 +000066 << call_expr->args.size() << " parameters";
Brandon Jones97a7d262021-05-11 10:35:43 +000067 }
Brandon Jonesecd31c52021-05-12 10:37:03 +000068
69 if (intrinsic->Type() ==
70 sem::IntrinsicType::kTextureSampleLevel &&
Ben Clayton4f3ff572021-10-15 17:33:10 +000071 call_expr->args.size() != 3) {
Ben Claytonffd28e22021-06-24 11:27:36 +000072 TINT_ICE(Transform, ctx.dst->Diagnostics())
Brandon Jonesecd31c52021-05-12 10:37:03 +000073 << "expected textureSampleLevel call with a "
74 "texture_external to have 3 parameters, found "
Ben Clayton4f3ff572021-10-15 17:33:10 +000075 << call_expr->args.size() << " parameters";
Brandon Jonesecd31c52021-05-12 10:37:03 +000076 }
77
78 // Replace the call with another that has the same parameters in
79 // addition to a level parameter (always zero for external
80 // textures).
Ben Clayton735dca82021-11-15 20:45:50 +000081 auto* exp = ctx.Clone(call_expr->target.name);
Ben Clayton4f3ff572021-10-15 17:33:10 +000082 auto* externalTextureParam = ctx.Clone(call_expr->args[0]);
Brandon Jones97a7d262021-05-11 10:35:43 +000083
Brandon Jonesecd31c52021-05-12 10:37:03 +000084 ast::ExpressionList params;
85 if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad) {
Ben Clayton4f3ff572021-10-15 17:33:10 +000086 auto* coordsParam = ctx.Clone(call_expr->args[1]);
Brandon Jonesecd31c52021-05-12 10:37:03 +000087 auto* levelParam = ctx.dst->Expr(0);
88 params = {externalTextureParam, coordsParam, levelParam};
89 } else if (intrinsic->Type() ==
90 sem::IntrinsicType::kTextureSampleLevel) {
Ben Clayton4f3ff572021-10-15 17:33:10 +000091 auto* samplerParam = ctx.Clone(call_expr->args[1]);
92 auto* coordsParam = ctx.Clone(call_expr->args[2]);
Brandon Jonesecd31c52021-05-12 10:37:03 +000093 auto* levelParam = ctx.dst->Expr(0.0f);
94 params = {externalTextureParam, samplerParam, coordsParam,
95 levelParam};
96 }
Brandon Jones97a7d262021-05-11 10:35:43 +000097
98 auto* newCall = ctx.dst->create<ast::CallExpression>(exp, params);
99 ctx.Replace(call_expr, newCall);
100 }
101 }
102 }
103 }
104 }
105 }
Brandon Jonesb432f232021-05-05 16:38:12 +0000106
107 // Scan the AST nodes for external texture declarations.
108 for (auto* node : ctx.src->ASTNodes().Objects()) {
109 if (auto* var = node->As<ast::Variable>()) {
Ben Clayton4f3ff572021-10-15 17:33:10 +0000110 if (::tint::Is<ast::ExternalTexture>(var->type)) {
Brandon Jonesb432f232021-05-05 16:38:12 +0000111 // Replace a single-plane external texture with a 2D, f32 sampled
112 // texture.
Ben Clayton19d32052021-05-20 15:10:48 +0000113 auto* newType = ctx.dst->ty.sampled_texture(ast::TextureDimension::k2d,
114 ctx.dst->ty.f32());
Ben Clayton4f3ff572021-10-15 17:33:10 +0000115 auto clonedSrc = ctx.Clone(var->source);
116 auto clonedSym = ctx.Clone(var->symbol);
117 auto* clonedConstructor = ctx.Clone(var->constructor);
118 auto clonedDecorations = ctx.Clone(var->decorations);
Brandon Jonesb432f232021-05-05 16:38:12 +0000119 auto* newVar = ctx.dst->create<ast::Variable>(
Ben Clayton4f3ff572021-10-15 17:33:10 +0000120 clonedSrc, clonedSym, var->declared_storage_class,
121 var->declared_access, newType, var->is_const, clonedConstructor,
Ben Clayton93e8f522021-06-04 20:41:47 +0000122 clonedDecorations);
Brandon Jonesb432f232021-05-05 16:38:12 +0000123
124 ctx.Replace(var, newVar);
125 }
126 }
127 }
128
129 ctx.Clone();
Brandon Jonesb432f232021-05-05 16:38:12 +0000130}
131
132} // namespace transform
133} // namespace tint