From 6fdc77a966ace7a898f6ffa277953f2b9ee82636 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 19 Aug 2020 19:03:24 +0000 Subject: [PATCH] [hlsl-writer] Emit module constants. This CL adds emission of module constants to the HLSL backend. Bug: tint:7 Change-Id: Iff07b0c0de7351f400dc35ca2ac07b44b22f8499 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26924 Commit-Queue: dan sinclair Reviewed-by: David Neto --- BUILD.gn | 1 + src/CMakeLists.txt | 1 + src/writer/hlsl/generator_impl.cc | 40 ++++++++++++ src/writer/hlsl/generator_impl.h | 4 ++ .../generator_impl_module_constant_test.cc | 64 +++++++++++++++++++ 5 files changed, 110 insertions(+) create mode 100644 src/writer/hlsl/generator_impl_module_constant_test.cc diff --git a/BUILD.gn b/BUILD.gn index c833695641..f90847b5c1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1073,6 +1073,7 @@ source_set("tint_unittests_hlsl_writer_src") { "src/writer/hlsl/generator_impl_if_test.cc", "src/writer/hlsl/generator_impl_loop_test.cc", "src/writer/hlsl/generator_impl_member_accessor_test.cc", + "src/writer/hlsl/generator_impl_module_constant_test.cc", "src/writer/hlsl/generator_impl_return_test.cc", "src/writer/hlsl/generator_impl_switch_test.cc", "src/writer/hlsl/generator_impl_test.cc", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e1f7c533de..562a1a70be 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -584,6 +584,7 @@ if (${TINT_BUILD_HLSL_WRITER}) writer/hlsl/generator_impl_if_test.cc writer/hlsl/generator_impl_loop_test.cc writer/hlsl/generator_impl_member_accessor_test.cc + writer/hlsl/generator_impl_module_constant_test.cc writer/hlsl/generator_impl_return_test.cc writer/hlsl/generator_impl_switch_test.cc writer/hlsl/generator_impl_test.cc diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index df457a85c0..b710fbb2f9 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -83,6 +83,15 @@ bool GeneratorImpl::Generate() { out_ << std::endl; } + for (const auto& var : module_->global_variables()) { + if (!var->is_const()) { + continue; + } + if (!EmitProgramConstVariable(var.get())) { + return false; + } + } + for (const auto& ep : module_->entry_points()) { if (!EmitEntryPointData(ep.get())) { return false; @@ -1371,6 +1380,37 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) { return true; } +bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) { + make_indent(); + + if (var->IsDecorated()) { + error_ = "Decorated const values not valid"; + return false; + } + if (!var->is_const()) { + error_ = "Expected a const value"; + return false; + } + + out_ << "static const "; + if (!EmitType(var->type(), var->name())) { + return false; + } + if (!var->type()->IsArray()) { + out_ << " " << var->name(); + } + + if (var->constructor() != nullptr) { + out_ << " = "; + if (!EmitExpression(var->constructor())) { + return false; + } + } + out_ << ";" << std::endl; + + return true; +} + } // namespace hlsl } // namespace writer } // namespace tint diff --git a/src/writer/hlsl/generator_impl.h b/src/writer/hlsl/generator_impl.h index 25789ab587..6fb7f5aaa2 100644 --- a/src/writer/hlsl/generator_impl.h +++ b/src/writer/hlsl/generator_impl.h @@ -180,6 +180,10 @@ class GeneratorImpl : public TextGenerator { /// @param var the variable to generate /// @returns true if the variable was emitted bool EmitVariable(ast::Variable* var); + /// Handles generating a program scope constant variable + /// @param var the variable to emit + /// @returns true if the variable was emitted + bool EmitProgramConstVariable(const ast::Variable* var); /// Checks if the global variable is in an input or output struct /// @param var the variable to check diff --git a/src/writer/hlsl/generator_impl_module_constant_test.cc b/src/writer/hlsl/generator_impl_module_constant_test.cc new file mode 100644 index 0000000000..74226e1c0a --- /dev/null +++ b/src/writer/hlsl/generator_impl_module_constant_test.cc @@ -0,0 +1,64 @@ +// Copyright 2020 The Tint Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "gtest/gtest.h" +#include "src/ast/float_literal.h" +#include "src/ast/module.h" +#include "src/ast/scalar_constructor_expression.h" +#include "src/ast/type/array_type.h" +#include "src/ast/type/f32_type.h" +#include "src/ast/type_constructor_expression.h" +#include "src/ast/variable.h" +#include "src/writer/hlsl/generator_impl.h" + +namespace tint { +namespace writer { +namespace hlsl { +namespace { + +using HlslGeneratorImplTest = testing::Test; + +TEST_F(HlslGeneratorImplTest, Emit_ModuleConstant) { + ast::type::F32Type f32; + ast::type::ArrayType ary(&f32, 3); + + ast::ExpressionList exprs; + exprs.push_back(std::make_unique( + std::make_unique(&f32, 1.0f))); + exprs.push_back(std::make_unique( + std::make_unique(&f32, 2.0f))); + exprs.push_back(std::make_unique( + std::make_unique(&f32, 3.0f))); + + auto var = + std::make_unique("pos", ast::StorageClass::kNone, &ary); + var->set_is_const(true); + var->set_constructor( + std::make_unique(&ary, std::move(exprs))); + + ast::Module m; + GeneratorImpl g(&m); + ASSERT_TRUE(g.EmitProgramConstVariable(var.get())) << g.error(); + EXPECT_EQ( + g.result(), + "static const float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n"); +} + +} // namespace +} // namespace hlsl +} // namespace writer +} // namespace tint