Refactor Inspector::GetTextureQueries
Use a switch on builtin->Fn() to simplify the logic.
Change-Id: I6e770ad6964728b0b8bf6bfe233b93062eb3b5f0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/185720
Commit-Queue: Shrek Shao <shrekshao@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
diff --git a/src/tint/lang/wgsl/inspector/inspector.cc b/src/tint/lang/wgsl/inspector/inspector.cc
index dedcdc7..34700c8 100644
--- a/src/tint/lang/wgsl/inspector/inspector.cc
+++ b/src/tint/lang/wgsl/inspector/inspector.cc
@@ -1028,17 +1028,6 @@
std::unordered_set<BindingPoint> seen = {};
- auto sample_type_for_call_and_type = [](wgsl::BuiltinFn builtin) {
- if (builtin == wgsl::BuiltinFn::kTextureNumLevels ||
- builtin == wgsl::BuiltinFn::kTextureDimensions ||
- builtin == wgsl::BuiltinFn::kTextureLoad) {
- return TextureQueryType::kTextureNumLevels;
- }
-
- TINT_ASSERT(builtin == wgsl::BuiltinFn::kTextureNumSamples);
- return TextureQueryType::kTextureNumSamples;
- };
-
Hashmap<const sem::Function*, Hashmap<const ast::Parameter*, TextureQueryType, 4>, 8>
fn_to_data;
@@ -1089,35 +1078,62 @@
tint::Switch(
call->Target(),
[&](const sem::BuiltinFn* builtin) {
- if (builtin->Fn() != wgsl::BuiltinFn::kTextureNumLevels &&
- builtin->Fn() != wgsl::BuiltinFn::kTextureNumSamples &&
- builtin->Fn() != wgsl::BuiltinFn::kTextureLoad &&
- // When textureDimension takes level as the input,
- // it requires calls to textureNumLevels to clamp mip levels.
- !(builtin->Fn() == wgsl::BuiltinFn::kTextureDimensions &&
- call->Declaration()->args.Length() > 1)) {
- return;
- }
+ auto queryTextureBuiltin = [&](TextureQueryType type,
+ const sem::Call* builtin_call,
+ const sem::Variable* texture_sem = nullptr) {
+ TINT_ASSERT(builtin_call);
+ if (!texture_sem) {
+ auto* texture_expr = builtin_call->Declaration()->args[0];
+ texture_sem = sem.GetVal(texture_expr)->RootIdentifier();
+ }
+ tint::Switch(
+ texture_sem, //
+ [&](const sem::GlobalVariable* global) {
+ save_if_needed(global, type);
+ },
+ [&](const sem::Parameter* param) {
+ record_function_param(fn, param->Declaration(), type);
+ },
+ TINT_ICE_ON_NO_MATCH);
+ };
- auto* texture_expr = call->Declaration()->args[0];
- auto* texture_sem = sem.GetVal(texture_expr)->RootIdentifier();
- TINT_ASSERT(texture_sem);
- if (builtin->Fn() == wgsl::BuiltinFn::kTextureLoad &&
- texture_sem->Type()
- ->UnwrapRef()
- ->IsAnyOf<core::type::MultisampledTexture,
- core::type::DepthMultisampledTexture>()) {
- return;
+ switch (builtin->Fn()) {
+ case wgsl::BuiltinFn::kTextureNumLevels: {
+ queryTextureBuiltin(TextureQueryType::kTextureNumLevels, call);
+ break;
+ }
+ case wgsl::BuiltinFn::kTextureDimensions: {
+ if (call->Declaration()->args.Length() <= 1) {
+ // When textureDimension only takes a texture as the input,
+ // it doesn't require calls to textureNumLevels to clamp mip levels.
+ return;
+ }
+ queryTextureBuiltin(TextureQueryType::kTextureNumLevels, call);
+ break;
+ }
+ case wgsl::BuiltinFn::kTextureLoad: {
+ auto* texture_expr = call->Declaration()->args[0];
+ auto* texture_sem = sem.GetVal(texture_expr)->RootIdentifier();
+ TINT_ASSERT(texture_sem);
+ if (texture_sem->Type()
+ ->UnwrapRef()
+ ->IsAnyOf<core::type::MultisampledTexture,
+ core::type::DepthMultisampledTexture>()) {
+ // When textureLoad takes a multisampled texture as the input,
+ // it doesn't require to query the mip level.
+ return;
+ }
+ queryTextureBuiltin(TextureQueryType::kTextureNumLevels, call,
+ texture_sem);
+ break;
+ }
+ case wgsl::BuiltinFn::kTextureNumSamples: {
+ queryTextureBuiltin(TextureQueryType::kTextureNumSamples, call);
+ break;
+ }
+ default:
+ return;
}
-
- auto type = sample_type_for_call_and_type(builtin->Fn());
- tint::Switch(
- texture_sem, //
- [&](const sem::GlobalVariable* global) { save_if_needed(global, type); },
- [&](const sem::Parameter* param) {
- record_function_param(fn, param->Declaration(), type);
- },
- TINT_ICE_ON_NO_MATCH);
},
[&](const sem::Function* func) {
// A function call, check to see if any params needed to be tracked back to a