[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>
This commit is contained in:
dan sinclair 2020-08-19 19:03:24 +00:00 committed by Commit Bot service account
parent 1811639709
commit 6fdc77a966
5 changed files with 110 additions and 0 deletions

View File

@ -1073,6 +1073,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_if_test.cc", "src/writer/hlsl/generator_impl_if_test.cc",
"src/writer/hlsl/generator_impl_loop_test.cc", "src/writer/hlsl/generator_impl_loop_test.cc",
"src/writer/hlsl/generator_impl_member_accessor_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_return_test.cc",
"src/writer/hlsl/generator_impl_switch_test.cc", "src/writer/hlsl/generator_impl_switch_test.cc",
"src/writer/hlsl/generator_impl_test.cc", "src/writer/hlsl/generator_impl_test.cc",

View File

@ -584,6 +584,7 @@ if (${TINT_BUILD_HLSL_WRITER})
writer/hlsl/generator_impl_if_test.cc writer/hlsl/generator_impl_if_test.cc
writer/hlsl/generator_impl_loop_test.cc writer/hlsl/generator_impl_loop_test.cc
writer/hlsl/generator_impl_member_accessor_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_return_test.cc
writer/hlsl/generator_impl_switch_test.cc writer/hlsl/generator_impl_switch_test.cc
writer/hlsl/generator_impl_test.cc writer/hlsl/generator_impl_test.cc

View File

@ -83,6 +83,15 @@ bool GeneratorImpl::Generate() {
out_ << std::endl; 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()) { for (const auto& ep : module_->entry_points()) {
if (!EmitEntryPointData(ep.get())) { if (!EmitEntryPointData(ep.get())) {
return false; return false;
@ -1371,6 +1380,37 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var) {
return true; 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 hlsl
} // namespace writer } // namespace writer
} // namespace tint } // namespace tint

View File

@ -180,6 +180,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 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 /// Checks if the global variable is in an input or output struct
/// @param var the variable to check /// @param var the variable to check

View File

@ -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