tint/writer/wgsl: Emit 'const' variables

Bug: tint:1580
Change-Id: Iea2a38814757f79ba645c94cfa89fe84b5d200d1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/94681
Reviewed-by: David Neto <dneto@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton 2022-06-26 12:40:40 +00:00 committed by Dawn LUCI CQ
parent f90313396b
commit fcf9fdcad8
3 changed files with 77 additions and 10 deletions

View File

@ -645,14 +645,6 @@ bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* v) {
bool ok = Switch( bool ok = Switch(
v, // v, //
[&](const ast::Let* ) {
out << "let";
return true;
},
[&](const ast::Override* ) {
out << "override";
return true;
},
[&](const ast::Var* var) { [&](const ast::Var* var) {
out << "var"; out << "var";
auto sc = var->declared_storage_class; auto sc = var->declared_storage_class;
@ -669,6 +661,18 @@ bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* v) {
} }
return true; return true;
}, },
[&](const ast::Let*) {
out << "let";
return true;
},
[&](const ast::Override*) {
out << "override";
return true;
},
[&](const ast::Const*) {
out << "const";
return true;
},
[&](Default) { [&](Default) {
TINT_ICE(Writer, diagnostics_) << "unhandled variable type " << v->TypeInfo().name; TINT_ICE(Writer, diagnostics_) << "unhandled variable type " << v->TypeInfo().name;
return false; return false;

View File

@ -131,6 +131,36 @@ TEST_F(WgslGeneratorImplTest, Emit_Global_Texture) {
EXPECT_EQ(gen.result(), " @group(0) @binding(0) var t : texture_1d<f32>;\n"); EXPECT_EQ(gen.result(), " @group(0) @binding(0) var t : texture_1d<f32>;\n");
} }
TEST_F(WgslGeneratorImplTest, Emit_GlobalLet) {
GlobalLet("explicit", ty.f32(), Expr(1_f));
GlobalLet("inferred", nullptr, Expr(1_f));
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"( let explicit : f32 = 1.0f;
let inferred = 1.0f;
)");
}
TEST_F(WgslGeneratorImplTest, Emit_GlobalConst) {
GlobalConst("explicit", ty.f32(), Expr(1_f));
GlobalConst("inferred", nullptr, Expr(1_f));
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"( const explicit : f32 = 1.0f;
const inferred = 1.0f;
)");
}
TEST_F(WgslGeneratorImplTest, Emit_OverridableConstants) { TEST_F(WgslGeneratorImplTest, Emit_OverridableConstants) {
Override("a", ty.f32(), nullptr); Override("a", ty.f32(), nullptr);
Override("b", ty.f32(), nullptr, {Id(7u)}); Override("b", ty.f32(), nullptr, {Id(7u)});

View File

@ -111,9 +111,9 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Constructor) {
EXPECT_EQ(out.str(), R"(var<private> a : f32 = 1.0f;)"); EXPECT_EQ(out.str(), R"(var<private> a : f32 = 1.0f;)");
} }
TEST_F(WgslGeneratorImplTest, EmitVariable_Const) { TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Explicit) {
auto* v = Let("a", ty.f32(), Expr(1_f)); auto* v = Let("a", ty.f32(), Expr(1_f));
WrapInFunction(Decl(v)); WrapInFunction(v);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -122,5 +122,38 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Const) {
EXPECT_EQ(out.str(), R"(let a : f32 = 1.0f;)"); EXPECT_EQ(out.str(), R"(let a : f32 = 1.0f;)");
} }
TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Inferred) {
auto* v = Let("a", nullptr, Expr(1_f));
WrapInFunction(v);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(let a = 1.0f;)");
}
TEST_F(WgslGeneratorImplTest, EmitVariable_Const_Explicit) {
auto* v = Const("a", ty.f32(), Expr(1_f));
WrapInFunction(v);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(const a : f32 = 1.0f;)");
}
TEST_F(WgslGeneratorImplTest, EmitVariable_Const_Inferred) {
auto* v = Const("a", nullptr, Expr(1_f));
WrapInFunction(v);
GeneratorImpl& gen = Build();
std::stringstream out;
ASSERT_TRUE(gen.EmitVariable(out, v)) << gen.error();
EXPECT_EQ(out.str(), R"(const a = 1.0f;)");
}
} // namespace } // namespace
} // namespace tint::writer::wgsl } // namespace tint::writer::wgsl