From 3e354fd524e7f93a63c7923195a30e64e2ae505d Mon Sep 17 00:00:00 2001 From: Stephen White Date: Mon, 10 Jan 2022 20:46:35 +0000 Subject: [PATCH] Resolver: collect unique texture/samplers pairs. GLSL does not support separate textures and samplers, so they must be replaced with combined samplers. This is the first stage of that change, where we collect the unique texture/sampler pairs. Within a function, texture and sampler must be either a global variable or a function parameter. At the entry point level, all references must resolve to global variables, so by recursing the call graph we can determine all of the global pairs required. This information will be used by an upcoming transform to modify the AST to be GLSL-compliant: modifying function signatures, call sites, removing separate globals and adding combined globals. It will also eventually replace the pair-gathering currently performed by Inspector::GetSamplerTextureUses(). Bug: tint:1366 Change-Id: I89451b195649da26e45641ea2f6955683ae9fc66 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/75960 Reviewed-by: Ben Clayton Kokoro: Kokoro Commit-Queue: Stephen White --- src/resolver/resolver.cc | 42 +++++++++- src/resolver/resolver_test.cc | 140 ++++++++++++++++++++++++++++++++++ src/sem/function.h | 18 +++++ src/sem/variable.h | 19 +++++ src/transform/transform.cc | 3 + 5 files changed, 219 insertions(+), 3 deletions(-) diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 7cf80f5174..e018a56eed 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -1400,9 +1400,25 @@ sem::Call* Resolver::IntrinsicCall( current_function_->AddDirectlyCalledIntrinsic(intrinsic); - if (IsTextureIntrinsic(intrinsic_type) && - !ValidateTextureIntrinsicFunction(call)) { - return nullptr; + if (IsTextureIntrinsic(intrinsic_type)) { + if (!ValidateTextureIntrinsicFunction(call)) { + return nullptr; + } + // Collect a texture/sampler pair for this intrinsic. + const auto& signature = intrinsic->Signature(); + int texture_index = signature.IndexOf(sem::ParameterUsage::kTexture); + if (texture_index == -1) { + TINT_ICE(Resolver, diagnostics_) + << "texture intrinsic without texture parameter"; + } + + auto* texture = args[texture_index]->As()->Variable(); + int sampler_index = signature.IndexOf(sem::ParameterUsage::kSampler); + const sem::Variable* sampler = + sampler_index != -1 + ? args[sampler_index]->As()->Variable() + : nullptr; + current_function_->AddTextureSamplerPair(texture, sampler); } if (!ValidateIntrinsicCall(call)) { @@ -1439,6 +1455,26 @@ sem::Call* Resolver::FunctionCall( for (auto* var : target->TransitivelyReferencedGlobals()) { current_function_->AddTransitivelyReferencedGlobal(var); } + + // Map all texture/sampler pairs from the target function to the + // current function. These can only be global or parameter + // variables. Resolve any parameter variables to the corresponding + // argument passed to the current function. Leave global variables + // as-is. Then add the mapped pair to the current function's list of + // texture/sampler pairs. + for (sem::VariablePair pair : target->TextureSamplerPairs()) { + const sem::Variable* texture = pair.first; + const sem::Variable* sampler = pair.second; + if (auto* param = texture->As()) { + texture = args[param->Index()]->As()->Variable(); + } + if (sampler) { + if (auto* param = sampler->As()) { + sampler = args[param->Index()]->As()->Variable(); + } + } + current_function_->AddTextureSamplerPair(texture, sampler); + } } target->AddCallSite(call); diff --git a/src/resolver/resolver_test.cc b/src/resolver/resolver_test.cc index 47e9e86671..2656abe179 100644 --- a/src/resolver/resolver_test.cc +++ b/src/resolver/resolver_test.cc @@ -2021,6 +2021,146 @@ TEST_F(ResolverTest, UnaryOp_Negation) { EXPECT_FALSE(r()->Resolve()); EXPECT_EQ(r()->error(), "12:34 error: cannot negate expression of type 'u32"); } + +TEST_F(ResolverTest, TextureSampler_TextureSample) { + Global("t", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()), + GroupAndBinding(1, 1)); + Global("s", ty.sampler(ast::SamplerKind::kSampler), GroupAndBinding(1, 2)); + + auto* call = CallStmt(Call("textureSample", "t", "s", vec2(1.0f, 2.0f))); + const ast::Function* f = Func("test_function", {}, ty.void_(), {call}, + {Stage(ast::PipelineStage::kFragment)}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + const sem::Function* sf = Sem().Get(f); + auto pairs = sf->TextureSamplerPairs(); + ASSERT_EQ(pairs.size(), 1u); + EXPECT_TRUE(pairs[0].first != nullptr); + EXPECT_TRUE(pairs[0].second != nullptr); +} + +TEST_F(ResolverTest, TextureSampler_TextureSampleInFunction) { + Global("t", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()), + GroupAndBinding(1, 1)); + Global("s", ty.sampler(ast::SamplerKind::kSampler), GroupAndBinding(1, 2)); + + auto* inner_call = + CallStmt(Call("textureSample", "t", "s", vec2(1.0f, 2.0f))); + const ast::Function* inner_func = + Func("inner_func", {}, ty.void_(), {inner_call}); + auto* outer_call = CallStmt(Call("inner_func")); + const ast::Function* outer_func = + Func("outer_func", {}, ty.void_(), {outer_call}, + {Stage(ast::PipelineStage::kFragment)}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto inner_pairs = Sem().Get(inner_func)->TextureSamplerPairs(); + ASSERT_EQ(inner_pairs.size(), 1u); + EXPECT_TRUE(inner_pairs[0].first != nullptr); + EXPECT_TRUE(inner_pairs[0].second != nullptr); + + auto outer_pairs = Sem().Get(outer_func)->TextureSamplerPairs(); + ASSERT_EQ(outer_pairs.size(), 1u); + EXPECT_TRUE(outer_pairs[0].first != nullptr); + EXPECT_TRUE(outer_pairs[0].second != nullptr); +} + +TEST_F(ResolverTest, TextureSampler_TextureSampleFunctionDiamondSameVariables) { + Global("t", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()), + GroupAndBinding(1, 1)); + Global("s", ty.sampler(ast::SamplerKind::kSampler), GroupAndBinding(1, 2)); + + auto* inner_call_1 = + CallStmt(Call("textureSample", "t", "s", vec2(1.0f, 2.0f))); + const ast::Function* inner_func_1 = + Func("inner_func_1", {}, ty.void_(), {inner_call_1}); + auto* inner_call_2 = + CallStmt(Call("textureSample", "t", "s", vec2(3.0f, 4.0f))); + const ast::Function* inner_func_2 = + Func("inner_func_2", {}, ty.void_(), {inner_call_2}); + auto* outer_call_1 = CallStmt(Call("inner_func_1")); + auto* outer_call_2 = CallStmt(Call("inner_func_2")); + const ast::Function* outer_func = + Func("outer_func", {}, ty.void_(), {outer_call_1, outer_call_2}, + {Stage(ast::PipelineStage::kFragment)}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto inner_pairs_1 = Sem().Get(inner_func_1)->TextureSamplerPairs(); + ASSERT_EQ(inner_pairs_1.size(), 1u); + EXPECT_TRUE(inner_pairs_1[0].first != nullptr); + EXPECT_TRUE(inner_pairs_1[0].second != nullptr); + + auto inner_pairs_2 = Sem().Get(inner_func_2)->TextureSamplerPairs(); + ASSERT_EQ(inner_pairs_1.size(), 1u); + EXPECT_TRUE(inner_pairs_2[0].first != nullptr); + EXPECT_TRUE(inner_pairs_2[0].second != nullptr); + + auto outer_pairs = Sem().Get(outer_func)->TextureSamplerPairs(); + ASSERT_EQ(outer_pairs.size(), 1u); + EXPECT_TRUE(outer_pairs[0].first != nullptr); + EXPECT_TRUE(outer_pairs[0].second != nullptr); +} + +TEST_F(ResolverTest, + TextureSampler_TextureSampleFunctionDiamondDifferentVariables) { + Global("t1", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()), + GroupAndBinding(1, 1)); + Global("t2", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()), + GroupAndBinding(1, 2)); + Global("s", ty.sampler(ast::SamplerKind::kSampler), GroupAndBinding(1, 3)); + + auto* inner_call_1 = + CallStmt(Call("textureSample", "t1", "s", vec2(1.0f, 2.0f))); + const ast::Function* inner_func_1 = + Func("inner_func_1", {}, ty.void_(), {inner_call_1}); + auto* inner_call_2 = + CallStmt(Call("textureSample", "t2", "s", vec2(3.0f, 4.0f))); + const ast::Function* inner_func_2 = + Func("inner_func_2", {}, ty.void_(), {inner_call_2}); + auto* outer_call_1 = CallStmt(Call("inner_func_1")); + auto* outer_call_2 = CallStmt(Call("inner_func_2")); + const ast::Function* outer_func = + Func("outer_func", {}, ty.void_(), {outer_call_1, outer_call_2}, + {Stage(ast::PipelineStage::kFragment)}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + auto inner_pairs_1 = Sem().Get(inner_func_1)->TextureSamplerPairs(); + ASSERT_EQ(inner_pairs_1.size(), 1u); + EXPECT_TRUE(inner_pairs_1[0].first != nullptr); + EXPECT_TRUE(inner_pairs_1[0].second != nullptr); + + auto inner_pairs_2 = Sem().Get(inner_func_2)->TextureSamplerPairs(); + ASSERT_EQ(inner_pairs_2.size(), 1u); + EXPECT_TRUE(inner_pairs_2[0].first != nullptr); + EXPECT_TRUE(inner_pairs_2[0].second != nullptr); + + auto outer_pairs = Sem().Get(outer_func)->TextureSamplerPairs(); + ASSERT_EQ(outer_pairs.size(), 2u); + EXPECT_TRUE(outer_pairs[0].first == inner_pairs_1[0].first); + EXPECT_TRUE(outer_pairs[0].second == inner_pairs_1[0].second); + EXPECT_TRUE(outer_pairs[1].first == inner_pairs_2[0].first); + EXPECT_TRUE(outer_pairs[1].second == inner_pairs_2[0].second); +} + +TEST_F(ResolverTest, TextureSampler_TextureDimensions) { + Global("t", ty.sampled_texture(ast::TextureDimension::k2d, ty.f32()), + GroupAndBinding(1, 2)); + + auto* call = Call("textureDimensions", "t"); + const ast::Function* f = WrapInFunction(call); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + + const sem::Function* sf = Sem().Get(f); + auto pairs = sf->TextureSamplerPairs(); + ASSERT_EQ(pairs.size(), 1u); + EXPECT_TRUE(pairs[0].first != nullptr); + EXPECT_TRUE(pairs[0].second == nullptr); +} } // namespace } // namespace resolver } // namespace tint diff --git a/src/sem/function.h b/src/sem/function.h index 6d980c5f49..75e3a4847e 100644 --- a/src/sem/function.h +++ b/src/sem/function.h @@ -132,6 +132,23 @@ class Function : public Castable { directly_called_intrinsics_.add(intrinsic); } + /// Adds the given texture/sampler pair to the list of unique pairs + /// that this function uses (directly or indirectly). These can only + /// be parameters to this function or global variables. Uniqueness is + /// ensured by texture_sampler_pairs_ being a UniqueVector. + /// @param texture the texture (must be non-null) + /// @param sampler the sampler (null indicates a texture-only reference) + void AddTextureSamplerPair(const sem::Variable* texture, + const sem::Variable* sampler) { + texture_sampler_pairs_.add(VariablePair(texture, sampler)); + } + + /// @returns the list of texture/sampler pairs that this function uses + /// (directly or indirectly). + const std::vector& TextureSamplerPairs() const { + return texture_sampler_pairs_; + } + /// @returns the list of direct calls to functions / intrinsics made by this /// function std::vector DirectCallStatements() const { @@ -259,6 +276,7 @@ class Function : public Castable { utils::UniqueVector transitively_referenced_globals_; utils::UniqueVector transitively_called_functions_; utils::UniqueVector directly_called_intrinsics_; + utils::UniqueVector texture_sampler_pairs_; std::vector direct_calls_; std::vector callsites_; std::vector ancestor_entry_points_; diff --git a/src/sem/variable.h b/src/sem/variable.h index 1f66ac7f23..0641183848 100644 --- a/src/sem/variable.h +++ b/src/sem/variable.h @@ -15,6 +15,7 @@ #ifndef SRC_SEM_VARIABLE_H_ #define SRC_SEM_VARIABLE_H_ +#include #include #include "src/ast/access.h" @@ -248,7 +249,25 @@ class VariableUser : public Castable { const sem::Variable* const variable_; }; +/// A pair of sem::Variables. Can be hashed. +typedef std::pair VariablePair; + } // namespace sem } // namespace tint +namespace std { + +/// Custom std::hash specialization for VariablePair +template <> +class hash { + public: + /// @param i the variable pair to create a hash for + /// @return the hash value + inline std::size_t operator()(const tint::sem::VariablePair& i) const { + return tint::utils::Hash(i.first, i.second); + } +}; + +} // namespace std + #endif // SRC_SEM_VARIABLE_H_ diff --git a/src/transform/transform.cc b/src/transform/transform.cc index a7ad0a2abd..21c4d817d2 100644 --- a/src/transform/transform.cc +++ b/src/transform/transform.cc @@ -142,6 +142,9 @@ const ast::Type* Transform::CreateASTTypeFor(CloneContext& ctx, if (auto* t = ty->As()) { return ctx.dst->create(t->dim()); } + if (ty->Is()) { + return ctx.dst->create(); + } if (auto* t = ty->As()) { return ctx.dst->create( t->dim(), CreateASTTypeFor(ctx, t->type()));