[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 <dsinclair@chromium.org>
Reviewed-by: David Neto <dneto@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index c833695..f90847b 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1073,6 +1073,7 @@
"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 e1f7c53..562a1a7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -584,6 +584,7 @@
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 df457a8..b710fbb 100644
--- a/src/writer/hlsl/generator_impl.cc
+++ b/src/writer/hlsl/generator_impl.cc
@@ -83,6 +83,15 @@
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 @@
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 25789ab..6fb7f5a 100644
--- a/src/writer/hlsl/generator_impl.h
+++ b/src/writer/hlsl/generator_impl.h
@@ -180,6 +180,10 @@
/// @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 0000000..74226e1
--- /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 <memory>
+#include <vector>
+
+#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<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::FloatLiteral>(&f32, 1.0f)));
+ exprs.push_back(std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::FloatLiteral>(&f32, 2.0f)));
+ exprs.push_back(std::make_unique<ast::ScalarConstructorExpression>(
+ std::make_unique<ast::FloatLiteral>(&f32, 3.0f)));
+
+ auto var =
+ std::make_unique<ast::Variable>("pos", ast::StorageClass::kNone, &ary);
+ var->set_is_const(true);
+ var->set_constructor(
+ std::make_unique<ast::TypeConstructorExpression>(&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