[ast] Rename Builtin::kSampleId to Builtin::kSampleIndex

Other builtins use WGSL terms instead of SPIR-V terms too, and the
WGSL writer is relying on the output of `operator<<(Builtin)`, which
just stringifies the name of the enum. This also matches the
equivalent `semantic::Usage::kSampleIndex` enum.

Added test coverage for WGSL builtin generation.

Bug: tint:372
Change-Id: I8077d22c4a5ddf67b1ad07e7365453db74db8e7d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/41660
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Auto-Submit: James Price <jrprice@google.com>
This commit is contained in:
James Price 2021-02-16 18:21:41 +00:00 committed by Commit Bot service account
parent d086c147d0
commit e7dab3c9ea
12 changed files with 67 additions and 20 deletions

View File

@ -59,8 +59,8 @@ std::ostream& operator<<(std::ostream& out, Builtin builtin) {
out << "global_invocation_id";
break;
}
case Builtin::kSampleId: {
out << "sample_id";
case Builtin::kSampleIndex: {
out << "sample_index";
break;
}
case Builtin::kSampleMaskIn: {

View File

@ -32,7 +32,7 @@ enum class Builtin {
kLocalInvocationId,
kLocalInvocationIndex,
kGlobalInvocationId,
kSampleId,
kSampleIndex,
kSampleMaskIn,
kSampleMaskOut,

View File

@ -87,7 +87,7 @@ ast::Builtin EnumConverter::ToBuiltin(SpvBuiltIn b, ast::StorageClass sc) {
case SpvBuiltInGlobalInvocationId:
return ast::Builtin::kGlobalInvocationId;
case SpvBuiltInSampleId:
return ast::Builtin::kSampleId;
return ast::Builtin::kSampleIndex;
case SpvBuiltInSampleMask:
return sc == ast::StorageClass::kInput ? ast::Builtin::kSampleMaskIn
: ast::Builtin::kSampleMaskOut;

View File

@ -225,7 +225,7 @@ INSTANTIATE_TEST_SUITE_P(
BuiltinCase{SpvBuiltInGlobalInvocationId, ast::StorageClass::kInput,
true, ast::Builtin::kGlobalInvocationId},
BuiltinCase{SpvBuiltInSampleId, ast::StorageClass::kInput, true,
ast::Builtin::kSampleId},
ast::Builtin::kSampleIndex},
BuiltinCase{SpvBuiltInSampleMask, ast::StorageClass::kInput, true,
ast::Builtin::kSampleMaskIn}));

View File

@ -2062,7 +2062,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_I32_Load_Direct) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in
@ -2104,7 +2104,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_I32_Load_CopyObject) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in
@ -2146,7 +2146,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_I32_Load_AccessChain) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in
@ -2213,7 +2213,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_U32_Load_Direct) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in
@ -2252,7 +2252,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_U32_Load_CopyObject) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in
@ -2301,7 +2301,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_U32_Load_AccessChain) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in
@ -2349,7 +2349,7 @@ TEST_F(SpvModuleScopeVarParserTest, SampleId_U32_FunctParam) {
EXPECT_THAT(module_str, HasSubstr(R"(
Variable{
Decorations{
BuiltinDecoration{sample_id}
BuiltinDecoration{sample_index}
}
x_1
in

View File

@ -1776,7 +1776,7 @@ std::string GeneratorImpl::builtin_to_attribute(ast::Builtin builtin) const {
return "SV_GroupIndex";
case ast::Builtin::kGlobalInvocationId:
return "SV_DispatchThreadID";
case ast::Builtin::kSampleId:
case ast::Builtin::kSampleIndex:
return "SV_SampleIndex";
case ast::Builtin::kSampleMaskIn:
return "SV_Coverage";

View File

@ -96,7 +96,7 @@ INSTANTIATE_TEST_SUITE_P(
HlslBuiltinData{ast::Builtin::kLocalInvocationIndex, "SV_GroupIndex"},
HlslBuiltinData{ast::Builtin::kGlobalInvocationId,
"SV_DispatchThreadID"},
HlslBuiltinData{ast::Builtin::kSampleId, "SV_SampleIndex"},
HlslBuiltinData{ast::Builtin::kSampleIndex, "SV_SampleIndex"},
HlslBuiltinData{ast::Builtin::kSampleMaskIn, "SV_Coverage"},
HlslBuiltinData{ast::Builtin::kSampleMaskOut, "SV_Coverage"}));

View File

@ -1455,7 +1455,7 @@ std::string GeneratorImpl::builtin_to_attribute(ast::Builtin builtin) const {
return "thread_index_in_threadgroup";
case ast::Builtin::kGlobalInvocationId:
return "thread_position_in_grid";
case ast::Builtin::kSampleId:
case ast::Builtin::kSampleIndex:
return "sample_id";
case ast::Builtin::kSampleMaskIn:
return "sample_mask";

View File

@ -120,7 +120,7 @@ INSTANTIATE_TEST_SUITE_P(
"thread_index_in_threadgroup"},
MslBuiltinData{ast::Builtin::kGlobalInvocationId,
"thread_position_in_grid"},
MslBuiltinData{ast::Builtin::kSampleId, "sample_id"},
MslBuiltinData{ast::Builtin::kSampleIndex, "sample_id"},
MslBuiltinData{ast::Builtin::kSampleMaskIn, "sample_mask"},
MslBuiltinData{ast::Builtin::kSampleMaskOut,
"sample_mask"}));

View File

@ -3194,7 +3194,7 @@ SpvBuiltIn Builder::ConvertBuiltin(ast::Builtin builtin) const {
return SpvBuiltInGlobalInvocationId;
case ast::Builtin::kPointSize:
return SpvBuiltInPointSize;
case ast::Builtin::kSampleId: // TODO(dneto)
case ast::Builtin::kSampleIndex: // TODO(dneto)
case ast::Builtin::kSampleMaskIn: // TODO(dneto)
case ast::Builtin::kSampleMaskOut: // TODO(dneto)
case ast::Builtin::kNone:

View File

@ -18,7 +18,9 @@
#include "gtest/gtest.h"
#include "src/ast/function.h"
#include "src/ast/variable.h"
#include "src/program.h"
#include "src/semantic/variable.h"
#include "src/type/void_type.h"
#include "src/writer/wgsl/test_helper.h"
@ -41,6 +43,50 @@ TEST_F(WgslGeneratorImplTest, Generate) {
)");
}
struct WgslBuiltinData {
ast::Builtin builtin;
const char* attribute_name;
};
inline std::ostream& operator<<(std::ostream& out, WgslBuiltinData data) {
out << data.builtin;
return out;
}
using WgslBuiltinConversionTest = TestParamHelper<WgslBuiltinData>;
TEST_P(WgslBuiltinConversionTest, Emit) {
auto params = GetParam();
auto* var = Global("a", ast::StorageClass::kNone, ty.f32(), nullptr,
ast::VariableDecorationList{
create<ast::BuiltinDecoration>(params.builtin),
});
GeneratorImpl& gen = Build();
gen.EmitVariableDecorations(program->Sem().Get(var));
EXPECT_EQ(gen.result(),
"[[builtin(" + std::string(params.attribute_name) + ")]] ");
}
INSTANTIATE_TEST_SUITE_P(
WgslGeneratorImplTest,
WgslBuiltinConversionTest,
testing::Values(
WgslBuiltinData{ast::Builtin::kPosition, "position"},
WgslBuiltinData{ast::Builtin::kVertexIndex, "vertex_index"},
WgslBuiltinData{ast::Builtin::kInstanceIndex, "instance_index"},
WgslBuiltinData{ast::Builtin::kFrontFacing, "front_facing"},
WgslBuiltinData{ast::Builtin::kFragCoord, "frag_coord"},
WgslBuiltinData{ast::Builtin::kFragDepth, "frag_depth"},
WgslBuiltinData{ast::Builtin::kLocalInvocationId,
"local_invocation_id"},
WgslBuiltinData{ast::Builtin::kLocalInvocationIndex,
"local_invocation_index"},
WgslBuiltinData{ast::Builtin::kGlobalInvocationId,
"global_invocation_id"},
WgslBuiltinData{ast::Builtin::kSampleIndex, "sample_index"},
WgslBuiltinData{ast::Builtin::kSampleMaskIn, "sample_mask_in"},
WgslBuiltinData{ast::Builtin::kSampleMaskOut, "sample_mask_out"}));
} // namespace
} // namespace wgsl
} // namespace writer

View File

@ -43,16 +43,17 @@ class TestHelperBase : public BASE, public ProgramBuilder {
if (gen_) {
return *gen_;
}
program_ = std::make_unique<Program>(std::move(*this));
gen_ = std::make_unique<GeneratorImpl>(program_.get());
program = std::make_unique<Program>(std::move(*this));
gen_ = std::make_unique<GeneratorImpl>(program.get());
return *gen_;
}
/// The type determiner
TypeDeterminer td;
// The program built with a call to Build()
std::unique_ptr<Program> program;
private:
std::unique_ptr<Program> program_;
std::unique_ptr<GeneratorImpl> gen_;
};
using TestHelper = TestHelperBase<testing::Test>;