From 4f79c84050287dab127ba5410e770889ed11cb05 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Tue, 20 Oct 2020 21:43:29 +0000 Subject: [PATCH] [spirv-writer] Deduplicate sampler and comparision_sampler. Both sampler types generate an `OpTypeSampler` in SPIR-V so we need to make sure they're considered duplicates in the SPIR-V backend. This CL registers the generated ID with for both sampler type_names when either one is emitted. Bug: tint:272 Change-Id: If459bbb34fe8670a7e29d101686b70bf83b184c2 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/30662 Commit-Queue: dan sinclair Commit-Queue: David Neto Reviewed-by: David Neto --- src/writer/spirv/builder.cc | 6 ++++++ src/writer/spirv/builder_type_test.cc | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 8708769dba..92c239f071 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -2307,6 +2307,12 @@ uint32_t Builder::GenerateTypeIfNeeded(ast::type::Type* type) { } } else if (type->IsSampler()) { push_type(spv::Op::OpTypeSampler, {result}); + + // Register both of the sampler type names. In SPIR-V they're the same + // sampler type, so we need to match that when we do the dedup check. + type_name_to_id_["__sampler_sampler"] = id; + type_name_to_id_["__sampler_comparison"] = id; + } else { error_ = "unable to convert type: " + type->type_name(); return 0; diff --git a/src/writer/spirv/builder_type_test.cc b/src/writer/spirv/builder_type_test.cc index 892e812965..0c153ebdaf 100644 --- a/src/writer/spirv/builder_type_test.cc +++ b/src/writer/spirv/builder_type_test.cc @@ -1181,6 +1181,21 @@ TEST_F(BuilderTest_Type, ComparisonSampler) { EXPECT_EQ(DumpInstructions(b.types()), "%1 = OpTypeSampler\n"); } +TEST_F(BuilderTest_Type, Dedup_Sampler_And_ComparisonSampler) { + ast::Module mod; + Builder b(&mod); + + ast::type::SamplerType comp_sampler( + ast::type::SamplerKind::kComparisonSampler); + EXPECT_EQ(b.GenerateTypeIfNeeded(&comp_sampler), 1u); + + ast::type::SamplerType sampler(ast::type::SamplerKind::kSampler); + EXPECT_EQ(b.GenerateTypeIfNeeded(&sampler), 1u); + + ASSERT_FALSE(b.has_error()) << b.error(); + EXPECT_EQ(DumpInstructions(b.types()), "%1 = OpTypeSampler\n"); +} + } // namespace } // namespace spirv } // namespace writer