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