[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 <dsinclair@google.com>
This commit is contained in:
David Neto 2020-04-01 17:56:05 +00:00 committed by dan sinclair
parent 06faf3bd5b
commit e0bcb544ac
3 changed files with 114 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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<SpvExecutionModel>(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<SpvStorageClass>(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<BuiltinCase> {
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<SpvBuiltIn>(9999), false,
ast::Builtin::kNone}));
} // namespace
} // namespace spirv
} // namespace reader