[wgsl-writer] Generate builtin and location decorations on struct members
Bug: tint:576 Change-Id: Ie8ace8dd77095abedcca97caca330e2d11a7559c Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/44321 Commit-Queue: James Price <jrprice@google.com> Reviewed-by: Ben Clayton <bclayton@chromium.org>
This commit is contained in:
parent
95d4077648
commit
d3e3681d63
|
@ -500,14 +500,14 @@ bool GeneratorImpl::EmitStructType(const type::Struct* str) {
|
||||||
|
|
||||||
increment_indent();
|
increment_indent();
|
||||||
for (auto* mem : impl->members()) {
|
for (auto* mem : impl->members()) {
|
||||||
for (auto* deco : mem->decorations()) {
|
if (!mem->decorations().empty()) {
|
||||||
make_indent();
|
make_indent();
|
||||||
|
if (!EmitDecorations(mem->decorations())) {
|
||||||
// TODO(dsinclair): Split this out when we have more then one
|
return false;
|
||||||
auto* offset = deco->As<ast::StructMemberOffsetDecoration>();
|
|
||||||
assert(offset != nullptr);
|
|
||||||
out_ << "[[offset(" << offset->offset() << ")]]" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
out_ << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
make_indent();
|
make_indent();
|
||||||
out_ << program_->Symbols().NameFor(mem->symbol()) << " : ";
|
out_ << program_->Symbols().NameFor(mem->symbol()) << " : ";
|
||||||
if (!EmitType(mem->type())) {
|
if (!EmitType(mem->type())) {
|
||||||
|
@ -527,9 +527,12 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) {
|
||||||
|
|
||||||
make_indent();
|
make_indent();
|
||||||
|
|
||||||
if (!var->decorations().empty() && !EmitVariableDecorations(sem)) {
|
if (!var->decorations().empty()) {
|
||||||
|
if (!EmitDecorations(var->decorations())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
out_ << " ";
|
||||||
|
}
|
||||||
|
|
||||||
if (var->is_const()) {
|
if (var->is_const()) {
|
||||||
out_ << "const";
|
out_ << "const";
|
||||||
|
@ -558,12 +561,10 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorImpl::EmitVariableDecorations(const semantic::Variable* var) {
|
bool GeneratorImpl::EmitDecorations(const ast::DecorationList& decos) {
|
||||||
auto* decl = var->Declaration();
|
|
||||||
|
|
||||||
out_ << "[[";
|
out_ << "[[";
|
||||||
bool first = true;
|
bool first = true;
|
||||||
for (auto* deco : decl->decorations()) {
|
for (auto* deco : decos) {
|
||||||
if (!first) {
|
if (!first) {
|
||||||
out_ << ", ";
|
out_ << ", ";
|
||||||
}
|
}
|
||||||
|
@ -579,6 +580,8 @@ bool GeneratorImpl::EmitVariableDecorations(const semantic::Variable* var) {
|
||||||
out_ << "builtin(" << builtin->value() << ")";
|
out_ << "builtin(" << builtin->value() << ")";
|
||||||
} else if (auto* constant = deco->As<ast::ConstantIdDecoration>()) {
|
} else if (auto* constant = deco->As<ast::ConstantIdDecoration>()) {
|
||||||
out_ << "constant_id(" << constant->value() << ")";
|
out_ << "constant_id(" << constant->value() << ")";
|
||||||
|
} else if (auto* offset = deco->As<ast::StructMemberOffsetDecoration>()) {
|
||||||
|
out_ << "offset(" << offset->offset() << ")";
|
||||||
} else {
|
} else {
|
||||||
diagnostics_.add_error("unknown variable decoration");
|
diagnostics_.add_error("unknown variable decoration");
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -193,10 +193,10 @@ class GeneratorImpl : public TextGenerator {
|
||||||
/// @param var the variable to generate
|
/// @param var the variable to generate
|
||||||
/// @returns true if the variable was emitted
|
/// @returns true if the variable was emitted
|
||||||
bool EmitVariable(ast::Variable* var);
|
bool EmitVariable(ast::Variable* var);
|
||||||
/// Handles generating variable decorations
|
/// Handles generating a decoration list
|
||||||
/// @param var the decorated variable
|
/// @param decos the decoration list
|
||||||
/// @returns true if the variable decoration was emitted
|
/// @returns true if the decorations were emitted
|
||||||
bool EmitVariableDecorations(const semantic::Variable* var);
|
bool EmitDecorations(const ast::DecorationList& decos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Program const* const program_;
|
Program const* const program_;
|
||||||
|
|
|
@ -53,7 +53,7 @@ TEST_P(WgslBuiltinConversionTest, Emit) {
|
||||||
|
|
||||||
GeneratorImpl& gen = Build();
|
GeneratorImpl& gen = Build();
|
||||||
|
|
||||||
gen.EmitVariableDecorations(program->Sem().Get(var));
|
gen.EmitDecorations(var->decorations());
|
||||||
|
|
||||||
EXPECT_EQ(gen.result(),
|
EXPECT_EQ(gen.result(),
|
||||||
"[[builtin(" + std::string(params.attribute_name) + ")]]");
|
"[[builtin(" + std::string(params.attribute_name) + ")]]");
|
||||||
|
|
|
@ -210,6 +210,31 @@ struct S {
|
||||||
)");
|
)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(WgslGeneratorImplTest, EmitType_Struct_WithEntryPointDecorations) {
|
||||||
|
ast::DecorationList decos;
|
||||||
|
decos.push_back(create<ast::StructBlockDecoration>());
|
||||||
|
|
||||||
|
auto* str = create<ast::Struct>(
|
||||||
|
ast::StructMemberList{
|
||||||
|
Member("a", ty.u32(),
|
||||||
|
{create<ast::BuiltinDecoration>(ast::Builtin::kVertexIndex)}),
|
||||||
|
Member("b", ty.f32(), {create<ast::LocationDecoration>(2u)})},
|
||||||
|
decos);
|
||||||
|
|
||||||
|
auto* s = ty.struct_("S", str);
|
||||||
|
GeneratorImpl& gen = Build();
|
||||||
|
|
||||||
|
ASSERT_TRUE(gen.EmitStructType(s)) << gen.error();
|
||||||
|
EXPECT_EQ(gen.result(), R"([[block]]
|
||||||
|
struct S {
|
||||||
|
[[builtin(vertex_index)]]
|
||||||
|
a : u32;
|
||||||
|
[[location(2)]]
|
||||||
|
b : f32;
|
||||||
|
};
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(WgslGeneratorImplTest, EmitType_U32) {
|
TEST_F(WgslGeneratorImplTest, EmitType_U32) {
|
||||||
auto* u32 = ty.u32();
|
auto* u32 = ty.u32();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue