From e0bcb544ac67010022ec948645ff8b5c92672c2f Mon Sep 17 00:00:00 2001 From: David Neto Date: Wed, 1 Apr 2020 17:56:05 +0000 Subject: [PATCH] [spirv-reader] Convert builtin enums Bug: tint:3 Change-Id: I1d94daab782e6e576f4b5a1c7c8816074d3ff307 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/18480 Reviewed-by: dan sinclair --- src/reader/spirv/enum_converter.cc | 32 ++++++++++ src/reader/spirv/enum_converter.h | 9 ++- src/reader/spirv/enum_converter_test.cc | 77 ++++++++++++++++++++++++- 3 files changed, 114 insertions(+), 4 deletions(-) diff --git a/src/reader/spirv/enum_converter.cc b/src/reader/spirv/enum_converter.cc index 40606cf78f..aea3bbf579 100644 --- a/src/reader/spirv/enum_converter.cc +++ b/src/reader/spirv/enum_converter.cc @@ -68,6 +68,38 @@ ast::StorageClass EnumConverter::ToStorageClass(SpvStorageClass sc) { return ast::StorageClass::kNone; } +ast::Builtin EnumConverter::ToBuiltin(SpvBuiltIn b) { + switch (b) { + case SpvBuiltInPosition: + return ast::Builtin::kPosition; + case SpvBuiltInVertexIndex: + return ast::Builtin::kVertexIdx; + case SpvBuiltInInstanceIndex: + return ast::Builtin::kInstanceIdx; + case SpvBuiltInFrontFacing: + return ast::Builtin::kFrontFacing; + case SpvBuiltInFragCoord: + return ast::Builtin::kFragCoord; + case SpvBuiltInFragDepth: + return ast::Builtin::kFragDepth; + case SpvBuiltInNumWorkgroups: + return ast::Builtin::kNumWorkgroups; + case SpvBuiltInWorkgroupSize: + return ast::Builtin::kWorkgroupSize; + case SpvBuiltInLocalInvocationId: + return ast::Builtin::kLocalInvocationId; + case SpvBuiltInLocalInvocationIndex: + return ast::Builtin::kLocalInvocationIdx; + case SpvBuiltInGlobalInvocationId: + return ast::Builtin::kGlobalInvocationId; + default: + break; + } + + Fail() << "unknown SPIR-V builtin: " << uint32_t(b); + return ast::Builtin::kNone; +} + } // namespace spirv } // namespace reader } // namespace tint diff --git a/src/reader/spirv/enum_converter.h b/src/reader/spirv/enum_converter.h index c910307ffe..ca7516ab09 100644 --- a/src/reader/spirv/enum_converter.h +++ b/src/reader/spirv/enum_converter.h @@ -16,6 +16,7 @@ #define SRC_READER_SPIRV_ENUM_CONVERTER_H_ #include "spirv/unified1/spirv.h" +#include "src/ast/builtin.h" #include "src/ast/pipeline_stage.h" #include "src/ast/storage_class.h" #include "src/reader/spirv/fail_stream.h" @@ -42,9 +43,15 @@ class EnumConverter { /// Converts a SPIR-V storage class to a Tint storage class. /// On failure, logs an error and returns kNone /// @param sc the SPIR-V storage class - /// @returns a Tint AST pipeline stage + /// @returns a Tint AST storage class ast::StorageClass ToStorageClass(SpvStorageClass sc); + /// Converts a SPIR-V Builtin value a Tint Builtin. + /// On failure, logs an error and returns kNone + /// @param b the SPIR-V builtin + /// @returns a Tint AST builtin + ast::Builtin ToBuiltin(SpvBuiltIn b); + private: /// Registers a failure and returns a stream for log diagnostics. /// @returns a failure stream diff --git a/src/reader/spirv/enum_converter_test.cc b/src/reader/spirv/enum_converter_test.cc index ee3aaa3745..0b2eb759ea 100644 --- a/src/reader/spirv/enum_converter_test.cc +++ b/src/reader/spirv/enum_converter_test.cc @@ -84,8 +84,8 @@ INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P( EnumConverterBad, SpvPipelineStageTest, - testing::Values(PipelineStageCase{SpvExecutionModel(9999), false, - ast::PipelineStage::kNone}, + testing::Values(PipelineStageCase{static_cast(9999), + false, ast::PipelineStage::kNone}, PipelineStageCase{SpvExecutionModelTessellationControl, false, ast::PipelineStage::kNone})); @@ -160,9 +160,80 @@ INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(EnumConverterBad, SpvStorageClassTest, testing::Values(StorageClassCase{ - SpvStorageClass(9999), false, + static_cast(9999), false, ast::StorageClass::kNone})); +// Builtin + +struct BuiltinCase { + SpvBuiltIn builtin; + bool expect_success; + ast::Builtin expected; +}; +inline std::ostream& operator<<(std::ostream& out, BuiltinCase bc) { + out << "BuiltinCase{ SpvBuiltIn:" << int(bc.builtin) + << " expect_success?:" << int(bc.expect_success) + << " expected:" << int(bc.expected) << "}"; + return out; +} + +class SpvBuiltinTest : public testing::TestWithParam { + public: + SpvBuiltinTest() + : success_(true), + fail_stream_(&success_, &errors_), + converter_(fail_stream_) {} + + std::string error() const { return errors_.str(); } + + protected: + bool success_ = true; + std::stringstream errors_; + FailStream fail_stream_; + EnumConverter converter_; +}; + +TEST_P(SpvBuiltinTest, Samples) { + const auto params = GetParam(); + + const auto result = converter_.ToBuiltin(params.builtin); + EXPECT_EQ(success_, params.expect_success); + if (params.expect_success) { + EXPECT_EQ(result, params.expected); + EXPECT_TRUE(error().empty()); + } else { + EXPECT_EQ(result, params.expected); + EXPECT_THAT(error(), ::testing::StartsWith("unknown SPIR-V builtin: ")); + } +} + +INSTANTIATE_TEST_SUITE_P( + EnumConverterGood, + SpvBuiltinTest, + testing::Values( + BuiltinCase{SpvBuiltInPosition, true, ast::Builtin::kPosition}, + BuiltinCase{SpvBuiltInVertexIndex, true, ast::Builtin::kVertexIdx}, + BuiltinCase{SpvBuiltInInstanceIndex, true, ast::Builtin::kInstanceIdx}, + BuiltinCase{SpvBuiltInFrontFacing, true, ast::Builtin::kFrontFacing}, + BuiltinCase{SpvBuiltInFragCoord, true, ast::Builtin::kFragCoord}, + BuiltinCase{SpvBuiltInFragDepth, true, ast::Builtin::kFragDepth}, + BuiltinCase{SpvBuiltInNumWorkgroups, true, + ast::Builtin::kNumWorkgroups}, + BuiltinCase{SpvBuiltInWorkgroupSize, true, + ast::Builtin::kWorkgroupSize}, + BuiltinCase{SpvBuiltInLocalInvocationId, true, + ast::Builtin::kLocalInvocationId}, + BuiltinCase{SpvBuiltInLocalInvocationIndex, true, + ast::Builtin::kLocalInvocationIdx}, + BuiltinCase{SpvBuiltInGlobalInvocationId, true, + ast::Builtin::kGlobalInvocationId})); + +INSTANTIATE_TEST_SUITE_P(EnumConverterBad, + SpvBuiltinTest, + testing::Values(BuiltinCase{ + static_cast(9999), false, + ast::Builtin::kNone})); + } // namespace } // namespace spirv } // namespace reader