• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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"
16 
17 #include "src/program_builder.h"
18 #include "src/sem/call.h"
19 #include "src/sem/variable.h"
20 
21 TINT_INSTANTIATE_TYPEINFO(tint::transform::ExternalTextureTransform);
22 
23 namespace tint {
24 namespace transform {
25 
26 ExternalTextureTransform::ExternalTextureTransform() = default;
27 ExternalTextureTransform::~ExternalTextureTransform() = default;
28 
Run(CloneContext & ctx,const DataMap &,DataMap &)29 void ExternalTextureTransform::Run(CloneContext& ctx,
30                                    const DataMap&,
31                                    DataMap&) {
32   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
40   // 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).
45 
46   // Scan the AST nodes for calls to textureLoad or textureSampleLevel.
47   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>()) {
51         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.
55           if (auto* var =
56                   sem.Get(call_expr->args[0])->As<sem::VariableUser>()) {
57             if (var->Variable()
58                     ->Type()
59                     ->UnwrapRef()
60                     ->Is<sem::ExternalTexture>()) {
61               if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad &&
62                   call_expr->args.size() != 2) {
63                 TINT_ICE(Transform, ctx.dst->Diagnostics())
64                     << "expected textureLoad call with a texture_external to "
65                        "have 2 parameters, found "
66                     << call_expr->args.size() << " parameters";
67               }
68 
69               if (intrinsic->Type() ==
70                       sem::IntrinsicType::kTextureSampleLevel &&
71                   call_expr->args.size() != 3) {
72                 TINT_ICE(Transform, ctx.dst->Diagnostics())
73                     << "expected textureSampleLevel call with a "
74                        "texture_external to have 3 parameters, found "
75                     << call_expr->args.size() << " parameters";
76               }
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).
81               auto* exp = ctx.Clone(call_expr->target.name);
82               auto* externalTextureParam = ctx.Clone(call_expr->args[0]);
83 
84               ast::ExpressionList params;
85               if (intrinsic->Type() == sem::IntrinsicType::kTextureLoad) {
86                 auto* coordsParam = ctx.Clone(call_expr->args[1]);
87                 auto* levelParam = ctx.dst->Expr(0);
88                 params = {externalTextureParam, coordsParam, levelParam};
89               } else if (intrinsic->Type() ==
90                          sem::IntrinsicType::kTextureSampleLevel) {
91                 auto* samplerParam = ctx.Clone(call_expr->args[1]);
92                 auto* coordsParam = ctx.Clone(call_expr->args[2]);
93                 auto* levelParam = ctx.dst->Expr(0.0f);
94                 params = {externalTextureParam, samplerParam, coordsParam,
95                           levelParam};
96               }
97 
98               auto* newCall = ctx.dst->create<ast::CallExpression>(exp, params);
99               ctx.Replace(call_expr, newCall);
100             }
101           }
102         }
103       }
104     }
105   }
106 
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>()) {
110       if (::tint::Is<ast::ExternalTexture>(var->type)) {
111         // Replace a single-plane external texture with a 2D, f32 sampled
112         // texture.
113         auto* newType = ctx.dst->ty.sampled_texture(ast::TextureDimension::k2d,
114                                                     ctx.dst->ty.f32());
115         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);
119         auto* newVar = ctx.dst->create<ast::Variable>(
120             clonedSrc, clonedSym, var->declared_storage_class,
121             var->declared_access, newType, var->is_const, clonedConstructor,
122             clonedDecorations);
123 
124         ctx.Replace(var, newVar);
125       }
126     }
127   }
128 
129   ctx.Clone();
130 }
131 
132 }  // namespace transform
133 }  // namespace tint
134