Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2020 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/tint/inspector/inspector.h" |
| 16 | |
| 17 | #include <limits> |
| 18 | #include <utility> |
| 19 | |
| 20 | #include "src/tint/ast/bool_literal_expression.h" |
| 21 | #include "src/tint/ast/call_expression.h" |
| 22 | #include "src/tint/ast/float_literal_expression.h" |
| 23 | #include "src/tint/ast/id_attribute.h" |
Ben Clayton | ce31d18 | 2023-02-09 10:34:14 +0000 | [diff] [blame] | 24 | #include "src/tint/ast/identifier.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 25 | #include "src/tint/ast/int_literal_expression.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 26 | #include "src/tint/ast/interpolate_attribute.h" |
| 27 | #include "src/tint/ast/location_attribute.h" |
| 28 | #include "src/tint/ast/module.h" |
Ben Clayton | 72876c1 | 2022-06-29 10:58:41 +0000 | [diff] [blame] | 29 | #include "src/tint/ast/override.h" |
| 30 | #include "src/tint/ast/var.h" |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 31 | #include "src/tint/builtin/builtin_value.h" |
dan sinclair | e4039c7 | 2023-02-17 21:58:59 +0000 | [diff] [blame] | 32 | #include "src/tint/builtin/extension.h" |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 33 | #include "src/tint/builtin/interpolation_sampling.h" |
| 34 | #include "src/tint/builtin/interpolation_type.h" |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 35 | #include "src/tint/sem/builtin_enum_expression.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 36 | #include "src/tint/sem/call.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | #include "src/tint/sem/function.h" |
Ben Clayton | 7f2b8cd | 2022-05-18 22:41:48 +0000 | [diff] [blame] | 38 | #include "src/tint/sem/module.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 39 | #include "src/tint/sem/statement.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | #include "src/tint/sem/struct.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 41 | #include "src/tint/sem/variable.h" |
Ben Clayton | 23946b3 | 2023-03-09 16:50:19 +0000 | [diff] [blame] | 42 | #include "src/tint/switch.h" |
dan sinclair | 946858a | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 43 | #include "src/tint/type/array.h" |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 44 | #include "src/tint/type/bool.h" |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 45 | #include "src/tint/type/depth_multisampled_texture.h" |
| 46 | #include "src/tint/type/depth_texture.h" |
| 47 | #include "src/tint/type/external_texture.h" |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 48 | #include "src/tint/type/f16.h" |
| 49 | #include "src/tint/type/f32.h" |
| 50 | #include "src/tint/type/i32.h" |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 51 | #include "src/tint/type/matrix.h" |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 52 | #include "src/tint/type/multisampled_texture.h" |
| 53 | #include "src/tint/type/sampled_texture.h" |
| 54 | #include "src/tint/type/storage_texture.h" |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 55 | #include "src/tint/type/u32.h" |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 56 | #include "src/tint/type/vector.h" |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 57 | #include "src/tint/type/void.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 58 | #include "src/tint/utils/math.h" |
Ben Clayton | 4808584 | 2022-07-26 22:51:36 +0000 | [diff] [blame] | 59 | #include "src/tint/utils/string.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | #include "src/tint/utils/unique_vector.h" |
| 61 | |
dan sinclair | aba5a21 | 2022-04-07 17:59:54 +0000 | [diff] [blame] | 62 | namespace tint::inspector { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 63 | |
| 64 | namespace { |
| 65 | |
| 66 | void AppendResourceBindings(std::vector<ResourceBinding>* dest, |
| 67 | const std::vector<ResourceBinding>& orig) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 68 | TINT_ASSERT(Inspector, dest); |
| 69 | if (!dest) { |
| 70 | return; |
| 71 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 72 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 73 | dest->reserve(dest->size() + orig.size()); |
| 74 | dest->insert(dest->end(), orig.begin(), orig.end()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 77 | std::tuple<ComponentType, CompositionType> CalculateComponentAndComposition( |
| 78 | const type::Type* type) { |
Zhaoming Jiang | 6198bea | 2022-12-07 04:33:24 +0000 | [diff] [blame] | 79 | // entry point in/out variables must of numeric scalar or vector types. |
| 80 | TINT_ASSERT(Inspector, type->is_numeric_scalar_or_vector()); |
| 81 | |
| 82 | ComponentType componentType = Switch( |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 83 | type::Type::DeepestElementOf(type), // |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 84 | [&](const type::F32*) { return ComponentType::kF32; }, |
| 85 | [&](const type::F16*) { return ComponentType::kF16; }, |
| 86 | [&](const type::I32*) { return ComponentType::kI32; }, |
| 87 | [&](const type::U32*) { return ComponentType::kU32; }, |
Zhaoming Jiang | 6198bea | 2022-12-07 04:33:24 +0000 | [diff] [blame] | 88 | [&](Default) { |
| 89 | tint::diag::List diagnostics; |
| 90 | TINT_UNREACHABLE(Inspector, diagnostics) << "unhandled component type"; |
| 91 | return ComponentType::kUnknown; |
| 92 | }); |
| 93 | |
| 94 | CompositionType compositionType; |
dan sinclair | 0e780da | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 95 | if (auto* vec = type->As<type::Vector>()) { |
Zhaoming Jiang | 6198bea | 2022-12-07 04:33:24 +0000 | [diff] [blame] | 96 | switch (vec->Width()) { |
| 97 | case 2: { |
| 98 | compositionType = CompositionType::kVec2; |
| 99 | break; |
| 100 | } |
| 101 | case 3: { |
| 102 | compositionType = CompositionType::kVec3; |
| 103 | break; |
| 104 | } |
| 105 | case 4: { |
| 106 | compositionType = CompositionType::kVec4; |
| 107 | break; |
| 108 | } |
| 109 | default: { |
| 110 | tint::diag::List diagnostics; |
| 111 | TINT_UNREACHABLE(Inspector, diagnostics) << "unhandled composition type"; |
| 112 | compositionType = CompositionType::kUnknown; |
| 113 | break; |
| 114 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 115 | } |
Zhaoming Jiang | 6198bea | 2022-12-07 04:33:24 +0000 | [diff] [blame] | 116 | } else { |
| 117 | compositionType = CompositionType::kScalar; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 118 | } |
Zhaoming Jiang | 6198bea | 2022-12-07 04:33:24 +0000 | [diff] [blame] | 119 | |
| 120 | return {componentType, compositionType}; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 123 | } // namespace |
| 124 | |
| 125 | Inspector::Inspector(const Program* program) : program_(program) {} |
| 126 | |
| 127 | Inspector::~Inspector() = default; |
| 128 | |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 129 | EntryPoint Inspector::GetEntryPoint(const tint::ast::Function* func) { |
| 130 | EntryPoint entry_point; |
| 131 | TINT_ASSERT(Inspector, func != nullptr); |
| 132 | TINT_ASSERT(Inspector, func->IsEntryPoint()); |
| 133 | |
| 134 | auto* sem = program_->Sem().Get(func); |
| 135 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 136 | entry_point.name = func->name->symbol.Name(); |
| 137 | entry_point.remapped_name = func->name->symbol.Name(); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 138 | |
| 139 | switch (func->PipelineStage()) { |
| 140 | case ast::PipelineStage::kCompute: { |
| 141 | entry_point.stage = PipelineStage::kCompute; |
| 142 | |
| 143 | auto wgsize = sem->WorkgroupSize(); |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 144 | if (wgsize[0].has_value() && wgsize[1].has_value() && wgsize[2].has_value()) { |
| 145 | entry_point.workgroup_size = {wgsize[0].value(), wgsize[1].value(), |
| 146 | wgsize[2].value()}; |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 147 | } |
| 148 | break; |
| 149 | } |
| 150 | case ast::PipelineStage::kFragment: { |
| 151 | entry_point.stage = PipelineStage::kFragment; |
| 152 | break; |
| 153 | } |
| 154 | case ast::PipelineStage::kVertex: { |
| 155 | entry_point.stage = PipelineStage::kVertex; |
| 156 | break; |
| 157 | } |
| 158 | default: { |
| 159 | TINT_UNREACHABLE(Inspector, diagnostics_) |
| 160 | << "invalid pipeline stage for entry point '" << entry_point.name << "'"; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | for (auto* param : sem->Parameters()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 166 | AddEntryPointInOutVariables(param->Declaration()->name->symbol.Name(), param->Type(), |
| 167 | param->Declaration()->attributes, param->Location(), |
| 168 | entry_point.input_variables); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 169 | |
| 170 | entry_point.input_position_used |= ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 171 | builtin::BuiltinValue::kPosition, param->Type(), param->Declaration()->attributes); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 172 | entry_point.front_facing_used |= ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 173 | builtin::BuiltinValue::kFrontFacing, param->Type(), param->Declaration()->attributes); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 174 | entry_point.sample_index_used |= ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 175 | builtin::BuiltinValue::kSampleIndex, param->Type(), param->Declaration()->attributes); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 176 | entry_point.input_sample_mask_used |= ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 177 | builtin::BuiltinValue::kSampleMask, param->Type(), param->Declaration()->attributes); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 178 | entry_point.num_workgroups_used |= ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 179 | builtin::BuiltinValue::kNumWorkgroups, param->Type(), param->Declaration()->attributes); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 180 | } |
| 181 | |
dan sinclair | d37ecf9 | 2022-12-08 16:39:59 +0000 | [diff] [blame] | 182 | if (!sem->ReturnType()->Is<type::Void>()) { |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 183 | AddEntryPointInOutVariables("<retval>", sem->ReturnType(), func->return_type_attributes, |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 184 | sem->ReturnLocation(), entry_point.output_variables); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 185 | |
| 186 | entry_point.output_sample_mask_used = ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 187 | builtin::BuiltinValue::kSampleMask, sem->ReturnType(), func->return_type_attributes); |
Corentin Wallez | 8d3d4f6 | 2022-10-11 09:52:59 +0000 | [diff] [blame] | 188 | entry_point.frag_depth_used = ContainsBuiltin( |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 189 | builtin::BuiltinValue::kFragDepth, sem->ReturnType(), func->return_type_attributes); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | for (auto* var : sem->TransitivelyReferencedGlobals()) { |
| 193 | auto* decl = var->Declaration(); |
| 194 | |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 195 | auto name = decl->name->symbol.Name(); |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 196 | |
| 197 | auto* global = var->As<sem::GlobalVariable>(); |
| 198 | if (global && global->Declaration()->Is<ast::Override>()) { |
| 199 | Override override; |
| 200 | override.name = name; |
| 201 | override.id = global->OverrideId(); |
| 202 | auto* type = var->Type(); |
| 203 | TINT_ASSERT(Inspector, type->is_scalar()); |
| 204 | if (type->is_bool_scalar_or_vector()) { |
| 205 | override.type = Override::Type::kBool; |
| 206 | } else if (type->is_float_scalar()) { |
Zhaoming Jiang | 6af073c | 2023-03-20 10:32:45 +0000 | [diff] [blame] | 207 | if (type->Is<type::F16>()) { |
| 208 | override.type = Override::Type::kFloat16; |
| 209 | } else { |
| 210 | override.type = Override::Type::kFloat32; |
| 211 | } |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 212 | } else if (type->is_signed_integer_scalar()) { |
| 213 | override.type = Override::Type::kInt32; |
| 214 | } else if (type->is_unsigned_integer_scalar()) { |
| 215 | override.type = Override::Type::kUint32; |
| 216 | } else { |
| 217 | TINT_UNREACHABLE(Inspector, diagnostics_); |
| 218 | } |
| 219 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 220 | override.is_initialized = global->Declaration()->initializer; |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 221 | override.is_id_specified = |
| 222 | ast::HasAttribute<ast::IdAttribute>(global->Declaration()->attributes); |
| 223 | |
| 224 | entry_point.overrides.push_back(override); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return entry_point; |
| 229 | } |
| 230 | |
| 231 | EntryPoint Inspector::GetEntryPoint(const std::string& entry_point_name) { |
| 232 | auto* func = FindEntryPointByName(entry_point_name); |
| 233 | if (!func) { |
| 234 | return EntryPoint(); |
| 235 | } |
| 236 | return GetEntryPoint(func); |
| 237 | } |
| 238 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 239 | std::vector<EntryPoint> Inspector::GetEntryPoints() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 240 | std::vector<EntryPoint> result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 241 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 242 | for (auto* func : program_->AST().Functions()) { |
| 243 | if (!func->IsEntryPoint()) { |
| 244 | continue; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 245 | } |
| 246 | |
shrekshao | 145337f | 2022-09-07 20:09:54 +0000 | [diff] [blame] | 247 | result.push_back(GetEntryPoint(func)); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 248 | } |
| 249 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 250 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Ben Clayton | 9a6acc4 | 2022-07-27 20:50:40 +0000 | [diff] [blame] | 253 | std::map<OverrideId, Scalar> Inspector::GetOverrideDefaultValues() { |
| 254 | std::map<OverrideId, Scalar> result; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 255 | for (auto* var : program_->AST().GlobalVariables()) { |
| 256 | auto* global = program_->Sem().Get<sem::GlobalVariable>(var); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 257 | if (!global || !global->Declaration()->Is<ast::Override>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | |
Ben Clayton | 9a6acc4 | 2022-07-27 20:50:40 +0000 | [diff] [blame] | 261 | // If there are conflicting defintions for an override id, that is invalid |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 262 | // WGSL, so the resolver should catch it. Thus here the inspector just |
Ben Clayton | 9a6acc4 | 2022-07-27 20:50:40 +0000 | [diff] [blame] | 263 | // assumes all definitions of the override id are the same, so only needs |
| 264 | // to find the first reference to override id. |
| 265 | OverrideId override_id = global->OverrideId(); |
| 266 | if (result.find(override_id) != result.end()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 267 | continue; |
| 268 | } |
| 269 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 270 | if (global->Initializer()) { |
| 271 | if (auto* value = global->Initializer()->ConstantValue()) { |
Ben Clayton | aecf1a2 | 2022-10-14 12:09:59 +0000 | [diff] [blame] | 272 | result[override_id] = Switch( |
| 273 | value->Type(), // |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 274 | [&](const type::I32*) { return Scalar(value->ValueAs<i32>()); }, |
| 275 | [&](const type::U32*) { return Scalar(value->ValueAs<u32>()); }, |
| 276 | [&](const type::F32*) { return Scalar(value->ValueAs<f32>()); }, |
Zhaoming Jiang | 6af073c | 2023-03-20 10:32:45 +0000 | [diff] [blame] | 277 | [&](const type::F16*) { |
| 278 | // Default value of f16 override is also stored as float scalar. |
| 279 | return Scalar(static_cast<float>(value->ValueAs<f16>())); |
| 280 | }, |
dan sinclair | 5addefb | 2022-12-14 20:46:32 +0000 | [diff] [blame] | 281 | [&](const type::Bool*) { return Scalar(value->ValueAs<bool>()); }); |
Ben Clayton | aecf1a2 | 2022-10-14 12:09:59 +0000 | [diff] [blame] | 282 | continue; |
Ben Clayton | 8822e29 | 2022-05-04 22:18:49 +0000 | [diff] [blame] | 283 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Ben Clayton | aecf1a2 | 2022-10-14 12:09:59 +0000 | [diff] [blame] | 286 | // No const-expression initializer for the override |
Ben Clayton | 9a6acc4 | 2022-07-27 20:50:40 +0000 | [diff] [blame] | 287 | result[override_id] = Scalar(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 288 | } |
| 289 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 290 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Ben Clayton | 9a6acc4 | 2022-07-27 20:50:40 +0000 | [diff] [blame] | 293 | std::map<std::string, OverrideId> Inspector::GetNamedOverrideIds() { |
| 294 | std::map<std::string, OverrideId> result; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 295 | for (auto* var : program_->AST().GlobalVariables()) { |
| 296 | auto* global = program_->Sem().Get<sem::GlobalVariable>(var); |
Ben Clayton | dcdf66e | 2022-06-17 12:48:51 +0000 | [diff] [blame] | 297 | if (global && global->Declaration()->Is<ast::Override>()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 298 | auto name = var->name->symbol.Name(); |
Ben Clayton | 9a6acc4 | 2022-07-27 20:50:40 +0000 | [diff] [blame] | 299 | result[name] = global->OverrideId(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 300 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 301 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 302 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | uint32_t Inspector::GetStorageSize(const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 306 | auto* func = FindEntryPointByName(entry_point); |
| 307 | if (!func) { |
| 308 | return 0; |
| 309 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 310 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 311 | size_t size = 0; |
| 312 | auto* func_sem = program_->Sem().Get(func); |
| 313 | for (auto& ruv : func_sem->TransitivelyReferencedUniformVariables()) { |
| 314 | size += ruv.first->Type()->UnwrapRef()->Size(); |
| 315 | } |
| 316 | for (auto& rsv : func_sem->TransitivelyReferencedStorageBufferVariables()) { |
| 317 | size += rsv.first->Type()->UnwrapRef()->Size(); |
| 318 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 319 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 320 | if (static_cast<uint64_t>(size) > static_cast<uint64_t>(std::numeric_limits<uint32_t>::max())) { |
| 321 | return std::numeric_limits<uint32_t>::max(); |
| 322 | } |
| 323 | return static_cast<uint32_t>(size); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 324 | } |
| 325 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 326 | std::vector<ResourceBinding> Inspector::GetResourceBindings(const std::string& entry_point) { |
| 327 | auto* func = FindEntryPointByName(entry_point); |
| 328 | if (!func) { |
| 329 | return {}; |
| 330 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 331 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 332 | std::vector<ResourceBinding> result; |
| 333 | for (auto fn : { |
| 334 | &Inspector::GetUniformBufferResourceBindings, |
| 335 | &Inspector::GetStorageBufferResourceBindings, |
| 336 | &Inspector::GetReadOnlyStorageBufferResourceBindings, |
| 337 | &Inspector::GetSamplerResourceBindings, |
| 338 | &Inspector::GetComparisonSamplerResourceBindings, |
| 339 | &Inspector::GetSampledTextureResourceBindings, |
| 340 | &Inspector::GetMultisampledTextureResourceBindings, |
| 341 | &Inspector::GetWriteOnlyStorageTextureResourceBindings, |
| 342 | &Inspector::GetDepthTextureResourceBindings, |
| 343 | &Inspector::GetDepthMultisampledTextureResourceBindings, |
| 344 | &Inspector::GetExternalTextureResourceBindings, |
| 345 | }) { |
| 346 | AppendResourceBindings(&result, (this->*fn)(entry_point)); |
| 347 | } |
| 348 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | std::vector<ResourceBinding> Inspector::GetUniformBufferResourceBindings( |
| 352 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 353 | auto* func = FindEntryPointByName(entry_point); |
| 354 | if (!func) { |
| 355 | return {}; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 356 | } |
| 357 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 358 | std::vector<ResourceBinding> result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 359 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 360 | auto* func_sem = program_->Sem().Get(func); |
| 361 | for (auto& ruv : func_sem->TransitivelyReferencedUniformVariables()) { |
| 362 | auto* var = ruv.first; |
| 363 | auto binding_info = ruv.second; |
| 364 | |
| 365 | auto* unwrapped_type = var->Type()->UnwrapRef(); |
| 366 | |
| 367 | ResourceBinding entry; |
| 368 | entry.resource_type = ResourceBinding::ResourceType::kUniformBuffer; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 369 | entry.bind_group = binding_info.group; |
| 370 | entry.binding = binding_info.binding; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 371 | entry.size = unwrapped_type->Size(); |
| 372 | entry.size_no_padding = entry.size; |
| 373 | if (auto* str = unwrapped_type->As<sem::Struct>()) { |
| 374 | entry.size_no_padding = str->SizeNoPadding(); |
| 375 | } else { |
| 376 | entry.size_no_padding = entry.size; |
| 377 | } |
| 378 | |
| 379 | result.push_back(entry); |
| 380 | } |
| 381 | |
| 382 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | std::vector<ResourceBinding> Inspector::GetStorageBufferResourceBindings( |
| 386 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 387 | return GetStorageBufferResourceBindingsImpl(entry_point, false); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 388 | } |
| 389 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 390 | std::vector<ResourceBinding> Inspector::GetReadOnlyStorageBufferResourceBindings( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 391 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 392 | return GetStorageBufferResourceBindingsImpl(entry_point, true); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 393 | } |
| 394 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 395 | std::vector<ResourceBinding> Inspector::GetSamplerResourceBindings(const std::string& entry_point) { |
| 396 | auto* func = FindEntryPointByName(entry_point); |
| 397 | if (!func) { |
| 398 | return {}; |
| 399 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 400 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 401 | std::vector<ResourceBinding> result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 402 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 403 | auto* func_sem = program_->Sem().Get(func); |
| 404 | for (auto& rs : func_sem->TransitivelyReferencedSamplerVariables()) { |
| 405 | auto binding_info = rs.second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 406 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 407 | ResourceBinding entry; |
| 408 | entry.resource_type = ResourceBinding::ResourceType::kSampler; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 409 | entry.bind_group = binding_info.group; |
| 410 | entry.binding = binding_info.binding; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 411 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 412 | result.push_back(entry); |
| 413 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 414 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 415 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | std::vector<ResourceBinding> Inspector::GetComparisonSamplerResourceBindings( |
| 419 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 420 | auto* func = FindEntryPointByName(entry_point); |
| 421 | if (!func) { |
| 422 | return {}; |
| 423 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 424 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 425 | std::vector<ResourceBinding> result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 426 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 427 | auto* func_sem = program_->Sem().Get(func); |
| 428 | for (auto& rcs : func_sem->TransitivelyReferencedComparisonSamplerVariables()) { |
| 429 | auto binding_info = rcs.second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 430 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 431 | ResourceBinding entry; |
| 432 | entry.resource_type = ResourceBinding::ResourceType::kComparisonSampler; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 433 | entry.bind_group = binding_info.group; |
| 434 | entry.binding = binding_info.binding; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 435 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 436 | result.push_back(entry); |
| 437 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 438 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 439 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | std::vector<ResourceBinding> Inspector::GetSampledTextureResourceBindings( |
| 443 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 444 | return GetSampledTextureResourceBindingsImpl(entry_point, false); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | std::vector<ResourceBinding> Inspector::GetMultisampledTextureResourceBindings( |
| 448 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 449 | return GetSampledTextureResourceBindingsImpl(entry_point, true); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 450 | } |
| 451 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 452 | std::vector<ResourceBinding> Inspector::GetWriteOnlyStorageTextureResourceBindings( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 453 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 454 | return GetStorageTextureResourceBindingsImpl(entry_point); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | std::vector<ResourceBinding> Inspector::GetTextureResourceBindings( |
| 458 | const std::string& entry_point, |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 459 | const tint::utils::TypeInfo* texture_type, |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 460 | ResourceBinding::ResourceType resource_type) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 461 | auto* func = FindEntryPointByName(entry_point); |
| 462 | if (!func) { |
| 463 | return {}; |
| 464 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 465 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 466 | std::vector<ResourceBinding> result; |
| 467 | auto* func_sem = program_->Sem().Get(func); |
| 468 | for (auto& ref : func_sem->TransitivelyReferencedVariablesOfType(texture_type)) { |
| 469 | auto* var = ref.first; |
| 470 | auto binding_info = ref.second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 471 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 472 | ResourceBinding entry; |
| 473 | entry.resource_type = resource_type; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 474 | entry.bind_group = binding_info.group; |
| 475 | entry.binding = binding_info.binding; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 476 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 477 | auto* tex = var->Type()->UnwrapRef()->As<type::Texture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 478 | entry.dim = TypeTextureDimensionToResourceBindingTextureDimension(tex->dim()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 479 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 480 | result.push_back(entry); |
| 481 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 482 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 483 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | std::vector<ResourceBinding> Inspector::GetDepthTextureResourceBindings( |
| 487 | const std::string& entry_point) { |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 488 | return GetTextureResourceBindings(entry_point, &utils::TypeInfo::Of<type::DepthTexture>(), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 489 | ResourceBinding::ResourceType::kDepthTexture); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 490 | } |
| 491 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 492 | std::vector<ResourceBinding> Inspector::GetDepthMultisampledTextureResourceBindings( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 493 | const std::string& entry_point) { |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 494 | return GetTextureResourceBindings(entry_point, |
| 495 | &utils::TypeInfo::Of<type::DepthMultisampledTexture>(), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 496 | ResourceBinding::ResourceType::kDepthMultisampledTexture); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | std::vector<ResourceBinding> Inspector::GetExternalTextureResourceBindings( |
| 500 | const std::string& entry_point) { |
dan sinclair | 12fa303 | 2023-04-19 23:52:33 +0000 | [diff] [blame] | 501 | return GetTextureResourceBindings(entry_point, &utils::TypeInfo::Of<type::ExternalTexture>(), |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 502 | ResourceBinding::ResourceType::kExternalTexture); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 503 | } |
| 504 | |
dan sinclair | 2f689a7 | 2023-02-21 21:54:48 +0000 | [diff] [blame] | 505 | utils::VectorRef<SamplerTexturePair> Inspector::GetSamplerTextureUses( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 506 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 507 | auto* func = FindEntryPointByName(entry_point); |
| 508 | if (!func) { |
| 509 | return {}; |
| 510 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 511 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 512 | GenerateSamplerTargets(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 513 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 514 | auto it = sampler_targets_->find(entry_point); |
| 515 | if (it == sampler_targets_->end()) { |
| 516 | return {}; |
| 517 | } |
| 518 | return it->second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 519 | } |
| 520 | |
dan sinclair | 2f689a7 | 2023-02-21 21:54:48 +0000 | [diff] [blame] | 521 | std::vector<SamplerTexturePair> Inspector::GetSamplerTextureUses( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 522 | const std::string& entry_point, |
| 523 | const sem::BindingPoint& placeholder) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 524 | auto* func = FindEntryPointByName(entry_point); |
| 525 | if (!func) { |
| 526 | return {}; |
| 527 | } |
| 528 | auto* func_sem = program_->Sem().Get(func); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 529 | |
dan sinclair | 2f689a7 | 2023-02-21 21:54:48 +0000 | [diff] [blame] | 530 | std::vector<SamplerTexturePair> new_pairs; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 531 | for (auto pair : func_sem->TextureSamplerPairs()) { |
| 532 | auto* texture = pair.first->As<sem::GlobalVariable>(); |
| 533 | auto* sampler = pair.second ? pair.second->As<sem::GlobalVariable>() : nullptr; |
| 534 | SamplerTexturePair new_pair; |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 535 | new_pair.sampler_binding_point = sampler ? *sampler->BindingPoint() : placeholder; |
| 536 | new_pair.texture_binding_point = *texture->BindingPoint(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 537 | new_pairs.push_back(new_pair); |
| 538 | } |
| 539 | return new_pairs; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | uint32_t Inspector::GetWorkgroupStorageSize(const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 543 | auto* func = FindEntryPointByName(entry_point); |
| 544 | if (!func) { |
| 545 | return 0; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 546 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 547 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 548 | uint32_t total_size = 0; |
| 549 | auto* func_sem = program_->Sem().Get(func); |
| 550 | for (const sem::Variable* var : func_sem->TransitivelyReferencedGlobals()) { |
dan sinclair | 2a65163 | 2023-02-19 04:03:55 +0000 | [diff] [blame] | 551 | if (var->AddressSpace() == builtin::AddressSpace::kWorkgroup) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 552 | auto* ty = var->Type()->UnwrapRef(); |
| 553 | uint32_t align = ty->Align(); |
| 554 | uint32_t size = ty->Size(); |
| 555 | |
| 556 | // This essentially matches std430 layout rules from GLSL, which are in |
| 557 | // turn specified as an upper bound for Vulkan layout sizing. Since D3D |
| 558 | // and Metal are even less specific, we assume Vulkan behavior as a |
| 559 | // good-enough approximation everywhere. |
| 560 | total_size += utils::RoundUp(align, size); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return total_size; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Zhaoming Jiang | 7098d3d | 2022-04-27 02:27:52 +0000 | [diff] [blame] | 567 | std::vector<std::string> Inspector::GetUsedExtensionNames() { |
Ben Clayton | 7f2b8cd | 2022-05-18 22:41:48 +0000 | [diff] [blame] | 568 | auto& extensions = program_->Sem().Module()->Extensions(); |
| 569 | std::vector<std::string> out; |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 570 | out.reserve(extensions.Length()); |
Ben Clayton | 7f2b8cd | 2022-05-18 22:41:48 +0000 | [diff] [blame] | 571 | for (auto ext : extensions) { |
Ben Clayton | 4808584 | 2022-07-26 22:51:36 +0000 | [diff] [blame] | 572 | out.push_back(utils::ToString(ext)); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 573 | } |
Ben Clayton | 7f2b8cd | 2022-05-18 22:41:48 +0000 | [diff] [blame] | 574 | return out; |
Zhaoming Jiang | 7098d3d | 2022-04-27 02:27:52 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | std::vector<std::pair<std::string, Source>> Inspector::GetEnableDirectives() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 578 | std::vector<std::pair<std::string, Source>> result; |
Zhaoming Jiang | 7098d3d | 2022-04-27 02:27:52 +0000 | [diff] [blame] | 579 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 580 | // Ast nodes for enable directive are stored within global declarations list |
| 581 | auto global_decls = program_->AST().GlobalDeclarations(); |
Ben Clayton | e23283f | 2022-05-03 16:06:23 +0000 | [diff] [blame] | 582 | for (auto* node : global_decls) { |
Ben Clayton | 8f80d99 | 2023-03-15 05:06:57 +0000 | [diff] [blame] | 583 | if (auto* enable = node->As<ast::Enable>()) { |
| 584 | for (auto* ext : enable->extensions) { |
| 585 | result.push_back({utils::ToString(ext->name), ext->source}); |
| 586 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 587 | } |
Zhaoming Jiang | 7098d3d | 2022-04-27 02:27:52 +0000 | [diff] [blame] | 588 | } |
Zhaoming Jiang | 7098d3d | 2022-04-27 02:27:52 +0000 | [diff] [blame] | 589 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 590 | return result; |
Zhaoming Jiang | 7098d3d | 2022-04-27 02:27:52 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 593 | const ast::Function* Inspector::FindEntryPointByName(const std::string& name) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 594 | auto* func = program_->AST().Functions().Find(program_->Symbols().Get(name)); |
| 595 | if (!func) { |
| 596 | diagnostics_.add_error(diag::System::Inspector, name + " was not found!"); |
| 597 | return nullptr; |
| 598 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 599 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 600 | if (!func->IsEntryPoint()) { |
| 601 | diagnostics_.add_error(diag::System::Inspector, name + " is not an entry point!"); |
| 602 | return nullptr; |
| 603 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 604 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 605 | return func; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 606 | } |
| 607 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 608 | void Inspector::AddEntryPointInOutVariables(std::string name, |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 609 | const type::Type* type, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 610 | utils::VectorRef<const ast::Attribute*> attributes, |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 611 | std::optional<uint32_t> location, |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 612 | std::vector<StageVariable>& variables) const { |
| 613 | // Skip builtins. |
| 614 | if (ast::HasAttribute<ast::BuiltinAttribute>(attributes)) { |
| 615 | return; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 616 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 617 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 618 | auto* unwrapped_type = type->UnwrapRef(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 619 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 620 | if (auto* struct_ty = unwrapped_type->As<sem::Struct>()) { |
| 621 | // Recurse into members. |
| 622 | for (auto* member : struct_ty->Members()) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 623 | AddEntryPointInOutVariables(name + "." + member->Name().Name(), member->Type(), |
Ben Clayton | 576ba1c | 2023-04-27 17:58:25 +0000 | [diff] [blame] | 624 | member->Declaration()->attributes, |
| 625 | member->Attributes().location, variables); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 626 | } |
| 627 | return; |
| 628 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 629 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 630 | // Base case: add the variable. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 631 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 632 | StageVariable stage_variable; |
| 633 | stage_variable.name = name; |
| 634 | std::tie(stage_variable.component_type, stage_variable.composition_type) = |
| 635 | CalculateComponentAndComposition(type); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 636 | |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 637 | TINT_ASSERT(Inspector, location.has_value()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 638 | stage_variable.has_location_attribute = true; |
dan sinclair | f9eeed6 | 2022-09-07 22:25:24 +0000 | [diff] [blame] | 639 | stage_variable.location_attribute = location.value(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 640 | |
| 641 | std::tie(stage_variable.interpolation_type, stage_variable.interpolation_sampling) = |
| 642 | CalculateInterpolationData(type, attributes); |
| 643 | |
| 644 | variables.push_back(stage_variable); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 645 | } |
| 646 | |
dan sinclair | 6392579 | 2023-02-17 21:56:35 +0000 | [diff] [blame] | 647 | bool Inspector::ContainsBuiltin(builtin::BuiltinValue builtin, |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 648 | const type::Type* type, |
Ben Clayton | 783b169 | 2022-08-02 17:03:35 +0000 | [diff] [blame] | 649 | utils::VectorRef<const ast::Attribute*> attributes) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 650 | auto* unwrapped_type = type->UnwrapRef(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 651 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 652 | if (auto* struct_ty = unwrapped_type->As<sem::Struct>()) { |
| 653 | // Recurse into members. |
| 654 | for (auto* member : struct_ty->Members()) { |
| 655 | if (ContainsBuiltin(builtin, member->Type(), member->Declaration()->attributes)) { |
| 656 | return true; |
| 657 | } |
| 658 | } |
| 659 | return false; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 660 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 661 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 662 | // Base case: check for builtin |
| 663 | auto* builtin_declaration = ast::GetAttribute<ast::BuiltinAttribute>(attributes); |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 664 | if (!builtin_declaration) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 665 | return false; |
| 666 | } |
Ben Clayton | 4d3ff97 | 2023-02-21 17:33:54 +0000 | [diff] [blame] | 667 | return program_->Sem().Get(builtin_declaration)->Value() == builtin; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | std::vector<ResourceBinding> Inspector::GetStorageBufferResourceBindingsImpl( |
| 671 | const std::string& entry_point, |
| 672 | bool read_only) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 673 | auto* func = FindEntryPointByName(entry_point); |
| 674 | if (!func) { |
| 675 | return {}; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 676 | } |
| 677 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 678 | auto* func_sem = program_->Sem().Get(func); |
| 679 | std::vector<ResourceBinding> result; |
| 680 | for (auto& rsv : func_sem->TransitivelyReferencedStorageBufferVariables()) { |
| 681 | auto* var = rsv.first; |
| 682 | auto binding_info = rsv.second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 683 | |
dan sinclair | b6cc4cb | 2023-02-19 04:01:29 +0000 | [diff] [blame] | 684 | if (read_only != (var->Access() == builtin::Access::kRead)) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 685 | continue; |
| 686 | } |
| 687 | |
| 688 | auto* unwrapped_type = var->Type()->UnwrapRef(); |
| 689 | |
| 690 | ResourceBinding entry; |
| 691 | entry.resource_type = read_only ? ResourceBinding::ResourceType::kReadOnlyStorageBuffer |
| 692 | : ResourceBinding::ResourceType::kStorageBuffer; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 693 | entry.bind_group = binding_info.group; |
| 694 | entry.binding = binding_info.binding; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 695 | entry.size = unwrapped_type->Size(); |
| 696 | if (auto* str = unwrapped_type->As<sem::Struct>()) { |
| 697 | entry.size_no_padding = str->SizeNoPadding(); |
| 698 | } else { |
| 699 | entry.size_no_padding = entry.size; |
| 700 | } |
| 701 | |
| 702 | result.push_back(entry); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 703 | } |
| 704 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 705 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | std::vector<ResourceBinding> Inspector::GetSampledTextureResourceBindingsImpl( |
| 709 | const std::string& entry_point, |
| 710 | bool multisampled_only) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 711 | auto* func = FindEntryPointByName(entry_point); |
| 712 | if (!func) { |
| 713 | return {}; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 714 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 715 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 716 | std::vector<ResourceBinding> result; |
| 717 | auto* func_sem = program_->Sem().Get(func); |
| 718 | auto referenced_variables = multisampled_only |
| 719 | ? func_sem->TransitivelyReferencedMultisampledTextureVariables() |
| 720 | : func_sem->TransitivelyReferencedSampledTextureVariables(); |
| 721 | for (auto& ref : referenced_variables) { |
| 722 | auto* var = ref.first; |
| 723 | auto binding_info = ref.second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 724 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 725 | ResourceBinding entry; |
| 726 | entry.resource_type = multisampled_only |
| 727 | ? ResourceBinding::ResourceType::kMultisampledTexture |
| 728 | : ResourceBinding::ResourceType::kSampledTexture; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 729 | entry.bind_group = binding_info.group; |
| 730 | entry.binding = binding_info.binding; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 731 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 732 | auto* texture_type = var->Type()->UnwrapRef()->As<type::Texture>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 733 | entry.dim = TypeTextureDimensionToResourceBindingTextureDimension(texture_type->dim()); |
| 734 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 735 | const type::Type* base_type = nullptr; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 736 | if (multisampled_only) { |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 737 | base_type = texture_type->As<type::MultisampledTexture>()->type(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 738 | } else { |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 739 | base_type = texture_type->As<type::SampledTexture>()->type(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 740 | } |
| 741 | entry.sampled_kind = BaseTypeToSampledKind(base_type); |
| 742 | |
| 743 | result.push_back(entry); |
| 744 | } |
| 745 | |
| 746 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | std::vector<ResourceBinding> Inspector::GetStorageTextureResourceBindingsImpl( |
| 750 | const std::string& entry_point) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 751 | auto* func = FindEntryPointByName(entry_point); |
| 752 | if (!func) { |
| 753 | return {}; |
| 754 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 755 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 756 | auto* func_sem = program_->Sem().Get(func); |
| 757 | std::vector<ResourceBinding> result; |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 758 | for (auto& ref : func_sem->TransitivelyReferencedVariablesOfType<type::StorageTexture>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 759 | auto* var = ref.first; |
| 760 | auto binding_info = ref.second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 761 | |
dan sinclair | 4595fb7 | 2022-12-08 14:14:10 +0000 | [diff] [blame] | 762 | auto* texture_type = var->Type()->UnwrapRef()->As<type::StorageTexture>(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 763 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 764 | ResourceBinding entry; |
| 765 | entry.resource_type = ResourceBinding::ResourceType::kWriteOnlyStorageTexture; |
dan sinclair | acdf6e1 | 2022-08-24 15:47:25 +0000 | [diff] [blame] | 766 | entry.bind_group = binding_info.group; |
| 767 | entry.binding = binding_info.binding; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 768 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 769 | entry.dim = TypeTextureDimensionToResourceBindingTextureDimension(texture_type->dim()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 770 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 771 | auto* base_type = texture_type->type(); |
| 772 | entry.sampled_kind = BaseTypeToSampledKind(base_type); |
| 773 | entry.image_format = |
| 774 | TypeTexelFormatToResourceBindingTexelFormat(texture_type->texel_format()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 775 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 776 | result.push_back(entry); |
| 777 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 778 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 779 | return result; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void Inspector::GenerateSamplerTargets() { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 783 | // Do not re-generate, since |program_| should not change during the lifetime |
| 784 | // of the inspector. |
| 785 | if (sampler_targets_ != nullptr) { |
| 786 | return; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 787 | } |
| 788 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 789 | sampler_targets_ = std::make_unique< |
dan sinclair | 2f689a7 | 2023-02-21 21:54:48 +0000 | [diff] [blame] | 790 | std::unordered_map<std::string, utils::UniqueVector<SamplerTexturePair, 4>>>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 791 | |
| 792 | auto& sem = program_->Sem(); |
| 793 | |
| 794 | for (auto* node : program_->ASTNodes().Objects()) { |
| 795 | auto* c = node->As<ast::CallExpression>(); |
| 796 | if (!c) { |
| 797 | continue; |
| 798 | } |
| 799 | |
Ben Clayton | e5a67ac | 2022-05-19 21:50:59 +0000 | [diff] [blame] | 800 | auto* call = sem.Get(c)->UnwrapMaterialize()->As<sem::Call>(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 801 | if (!call) { |
| 802 | continue; |
| 803 | } |
| 804 | |
| 805 | auto* i = call->Target()->As<sem::Builtin>(); |
| 806 | if (!i) { |
| 807 | continue; |
| 808 | } |
| 809 | |
| 810 | const auto& signature = i->Signature(); |
| 811 | int sampler_index = signature.IndexOf(sem::ParameterUsage::kSampler); |
| 812 | if (sampler_index == -1) { |
| 813 | continue; |
| 814 | } |
| 815 | |
| 816 | int texture_index = signature.IndexOf(sem::ParameterUsage::kTexture); |
| 817 | if (texture_index == -1) { |
| 818 | continue; |
| 819 | } |
| 820 | |
| 821 | auto* call_func = call->Stmt()->Function(); |
| 822 | std::vector<const sem::Function*> entry_points; |
| 823 | if (call_func->Declaration()->IsEntryPoint()) { |
| 824 | entry_points = {call_func}; |
| 825 | } else { |
| 826 | entry_points = call_func->AncestorEntryPoints(); |
| 827 | } |
| 828 | |
| 829 | if (entry_points.empty()) { |
| 830 | continue; |
| 831 | } |
| 832 | |
dan sinclair | 3a2a279 | 2022-06-29 14:38:15 +0000 | [diff] [blame] | 833 | auto* t = c->args[static_cast<size_t>(texture_index)]; |
| 834 | auto* s = c->args[static_cast<size_t>(sampler_index)]; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 835 | |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 836 | GetOriginatingResources(std::array<const ast::Expression*, 2>{t, s}, |
| 837 | [&](std::array<const sem::GlobalVariable*, 2> globals) { |
Ben Clayton | 5f4847c | 2023-04-19 13:24:27 +0000 | [diff] [blame] | 838 | auto texture_binding_point = *globals[0]->BindingPoint(); |
| 839 | auto sampler_binding_point = *globals[1]->BindingPoint(); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 840 | |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 841 | for (auto* entry_point : entry_points) { |
dan sinclair | d026e13 | 2023-04-18 19:38:25 +0000 | [diff] [blame] | 842 | const auto& ep_name = |
| 843 | entry_point->Declaration()->name->symbol.Name(); |
Ben Clayton | 490d988 | 2022-09-21 21:05:45 +0000 | [diff] [blame] | 844 | (*sampler_targets_)[ep_name].Add( |
| 845 | {sampler_binding_point, texture_binding_point}); |
| 846 | } |
| 847 | }); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 848 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Ben Clayton | f0b4dbb | 2023-02-21 21:05:28 +0000 | [diff] [blame] | 851 | std::tuple<InterpolationType, InterpolationSampling> Inspector::CalculateInterpolationData( |
| 852 | const type::Type* type, |
| 853 | utils::VectorRef<const ast::Attribute*> attributes) const { |
| 854 | auto* interpolation_attribute = ast::GetAttribute<ast::InterpolateAttribute>(attributes); |
| 855 | if (type->is_integer_scalar_or_vector()) { |
| 856 | return {InterpolationType::kFlat, InterpolationSampling::kNone}; |
| 857 | } |
| 858 | |
| 859 | if (!interpolation_attribute) { |
| 860 | return {InterpolationType::kPerspective, InterpolationSampling::kCenter}; |
| 861 | } |
| 862 | |
| 863 | auto& sem = program_->Sem(); |
| 864 | |
| 865 | auto ast_interpolation_type = sem.Get<sem::BuiltinEnumExpression<builtin::InterpolationType>>( |
| 866 | interpolation_attribute->type) |
| 867 | ->Value(); |
| 868 | |
| 869 | auto ast_sampling_type = builtin::InterpolationSampling::kUndefined; |
| 870 | if (interpolation_attribute->sampling) { |
| 871 | ast_sampling_type = sem.Get<sem::BuiltinEnumExpression<builtin::InterpolationSampling>>( |
| 872 | interpolation_attribute->sampling) |
| 873 | ->Value(); |
| 874 | } |
| 875 | |
| 876 | if (ast_interpolation_type != builtin::InterpolationType::kFlat && |
| 877 | ast_sampling_type == builtin::InterpolationSampling::kUndefined) { |
| 878 | ast_sampling_type = builtin::InterpolationSampling::kCenter; |
| 879 | } |
| 880 | |
| 881 | auto interpolation_type = InterpolationType::kUnknown; |
| 882 | switch (ast_interpolation_type) { |
| 883 | case builtin::InterpolationType::kPerspective: |
| 884 | interpolation_type = InterpolationType::kPerspective; |
| 885 | break; |
| 886 | case builtin::InterpolationType::kLinear: |
| 887 | interpolation_type = InterpolationType::kLinear; |
| 888 | break; |
| 889 | case builtin::InterpolationType::kFlat: |
| 890 | interpolation_type = InterpolationType::kFlat; |
| 891 | break; |
| 892 | case builtin::InterpolationType::kUndefined: |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | auto sampling_type = InterpolationSampling::kUnknown; |
| 897 | switch (ast_sampling_type) { |
| 898 | case builtin::InterpolationSampling::kUndefined: |
| 899 | sampling_type = InterpolationSampling::kNone; |
| 900 | break; |
| 901 | case builtin::InterpolationSampling::kCenter: |
| 902 | sampling_type = InterpolationSampling::kCenter; |
| 903 | break; |
| 904 | case builtin::InterpolationSampling::kCentroid: |
| 905 | sampling_type = InterpolationSampling::kCentroid; |
| 906 | break; |
| 907 | case builtin::InterpolationSampling::kSample: |
| 908 | sampling_type = InterpolationSampling::kSample; |
| 909 | break; |
| 910 | } |
| 911 | |
| 912 | return {interpolation_type, sampling_type}; |
| 913 | } |
| 914 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 915 | template <size_t N, typename F> |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 916 | void Inspector::GetOriginatingResources(std::array<const ast::Expression*, N> exprs, F&& callback) { |
Ben Clayton | 884f952 | 2023-01-12 22:52:57 +0000 | [diff] [blame] | 917 | if (TINT_UNLIKELY(!program_->IsValid())) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 918 | TINT_ICE(Inspector, diagnostics_) |
| 919 | << "attempting to get originating resources in invalid program"; |
James Price | e6d7ea7 | 2022-04-22 17:40:33 +0000 | [diff] [blame] | 920 | return; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 921 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 922 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 923 | auto& sem = program_->Sem(); |
| 924 | |
| 925 | std::array<const sem::GlobalVariable*, N> globals{}; |
| 926 | std::array<const sem::Parameter*, N> parameters{}; |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 927 | utils::UniqueVector<const ast::CallExpression*, 8> callsites; |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 928 | |
| 929 | for (size_t i = 0; i < N; i++) { |
Ben Clayton | 0b4a2f1 | 2023-02-05 22:59:40 +0000 | [diff] [blame] | 930 | const sem::Variable* root_ident = sem.GetVal(exprs[i])->RootIdentifier(); |
James Price | a7cd3ae | 2022-11-09 12:16:56 +0000 | [diff] [blame] | 931 | if (auto* global = root_ident->As<sem::GlobalVariable>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 932 | globals[i] = global; |
James Price | a7cd3ae | 2022-11-09 12:16:56 +0000 | [diff] [blame] | 933 | } else if (auto* param = root_ident->As<sem::Parameter>()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 934 | auto* func = tint::As<sem::Function>(param->Owner()); |
| 935 | if (func->CallSites().empty()) { |
| 936 | // One or more of the expressions is a parameter, but this function |
| 937 | // is not called. Ignore. |
| 938 | return; |
| 939 | } |
| 940 | for (auto* call : func->CallSites()) { |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 941 | callsites.Add(call->Declaration()); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 942 | } |
| 943 | parameters[i] = param; |
| 944 | } else { |
| 945 | TINT_ICE(Inspector, diagnostics_) |
| 946 | << "cannot resolve originating resource with expression type " |
| 947 | << exprs[i]->TypeInfo().name; |
| 948 | return; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 949 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 950 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 951 | |
Ben Clayton | dce63f5 | 2022-08-17 18:07:20 +0000 | [diff] [blame] | 952 | if (callsites.Length()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 953 | for (auto* call_expr : callsites) { |
| 954 | // Make a copy of the expressions for this callsite |
| 955 | std::array<const ast::Expression*, N> call_exprs = exprs; |
| 956 | // Patch all the parameter expressions with their argument |
| 957 | for (size_t i = 0; i < N; i++) { |
| 958 | if (auto* param = parameters[i]) { |
| 959 | call_exprs[i] = call_expr->args[param->Index()]; |
| 960 | } |
| 961 | } |
| 962 | // Now call GetOriginatingResources() with from the callsite |
| 963 | GetOriginatingResources(call_exprs, callback); |
| 964 | } |
| 965 | } else { |
| 966 | // All the expressions resolved to globals |
| 967 | callback(globals); |
| 968 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 969 | } |
| 970 | |
dan sinclair | aba5a21 | 2022-04-07 17:59:54 +0000 | [diff] [blame] | 971 | } // namespace tint::inspector |