From fcf9fdcad813691915fc313a7a1af1bbf3d9523f Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Sun, 26 Jun 2022 12:40:40 +0000 Subject: [PATCH] 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 Commit-Queue: Ben Clayton Kokoro: Kokoro --- src/tint/writer/wgsl/generator_impl.cc | 20 ++++++---- .../wgsl/generator_impl_global_decl_test.cc | 30 +++++++++++++++ .../wgsl/generator_impl_variable_test.cc | 37 ++++++++++++++++++- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/tint/writer/wgsl/generator_impl.cc b/src/tint/writer/wgsl/generator_impl.cc index 7df4fdf33c..a448827aff 100644 --- a/src/tint/writer/wgsl/generator_impl.cc +++ b/src/tint/writer/wgsl/generator_impl.cc @@ -645,14 +645,6 @@ bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* v) { bool ok = Switch( v, // - [&](const ast::Let* ) { - out << "let"; - return true; - }, - [&](const ast::Override* ) { - out << "override"; - return true; - }, [&](const ast::Var* var) { out << "var"; auto sc = var->declared_storage_class; @@ -669,6 +661,18 @@ bool GeneratorImpl::EmitVariable(std::ostream& out, const ast::Variable* v) { } return true; }, + [&](const ast::Let*) { + out << "let"; + return true; + }, + [&](const ast::Override*) { + out << "override"; + return true; + }, + [&](const ast::Const*) { + out << "const"; + return true; + }, [&](Default) { TINT_ICE(Writer, diagnostics_) << "unhandled variable type " << v->TypeInfo().name; return false; diff --git a/src/tint/writer/wgsl/generator_impl_global_decl_test.cc b/src/tint/writer/wgsl/generator_impl_global_decl_test.cc index dfad2a957c..19ff7d7135 100644 --- a/src/tint/writer/wgsl/generator_impl_global_decl_test.cc +++ b/src/tint/writer/wgsl/generator_impl_global_decl_test.cc @@ -131,6 +131,36 @@ TEST_F(WgslGeneratorImplTest, Emit_Global_Texture) { EXPECT_EQ(gen.result(), " @group(0) @binding(0) var t : texture_1d;\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) { Override("a", ty.f32(), nullptr); Override("b", ty.f32(), nullptr, {Id(7u)}); diff --git a/src/tint/writer/wgsl/generator_impl_variable_test.cc b/src/tint/writer/wgsl/generator_impl_variable_test.cc index 9a8775d9e6..a117459d32 100644 --- a/src/tint/writer/wgsl/generator_impl_variable_test.cc +++ b/src/tint/writer/wgsl/generator_impl_variable_test.cc @@ -111,9 +111,9 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Constructor) { EXPECT_EQ(out.str(), R"(var a : f32 = 1.0f;)"); } -TEST_F(WgslGeneratorImplTest, EmitVariable_Const) { +TEST_F(WgslGeneratorImplTest, EmitVariable_Let_Explicit) { auto* v = Let("a", ty.f32(), Expr(1_f)); - WrapInFunction(Decl(v)); + WrapInFunction(v); GeneratorImpl& gen = Build(); @@ -122,5 +122,38 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Const) { 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 tint::writer::wgsl