[msl-writer] Emitting of program constants.
This CL adds code to emit program constants in the MSL backend. Bug: tint:8 Change-Id: I63e40983253349d2e293904fbe9b6f543b885b34 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/24940 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
3f10421cee
commit
4264af9d3d
1
BUILD.gn
1
BUILD.gn
|
@ -863,6 +863,7 @@ source_set("tint_unittests_wgsl_writer_src") {
|
|||
"src/writer/wgsl/generator_impl_kill_test.cc",
|
||||
"src/writer/wgsl/generator_impl_loop_test.cc",
|
||||
"src/writer/wgsl/generator_impl_member_accessor_test.cc",
|
||||
"src/writer/wgsl/generator_impl_module_constant_test.cc",
|
||||
"src/writer/wgsl/generator_impl_return_test.cc",
|
||||
"src/writer/wgsl/generator_impl_switch_test.cc",
|
||||
"src/writer/wgsl/generator_impl_test.cc",
|
||||
|
|
|
@ -512,6 +512,7 @@ if(${TINT_BUILD_MSL_WRITER})
|
|||
writer/msl/generator_impl_kill_test.cc
|
||||
writer/msl/generator_impl_loop_test.cc
|
||||
writer/msl/generator_impl_member_accessor_test.cc
|
||||
writer/msl/generator_impl_module_constant_test.cc
|
||||
writer/msl/generator_impl_return_test.cc
|
||||
writer/msl/generator_impl_switch_test.cc
|
||||
writer/msl/generator_impl_test.cc
|
||||
|
|
|
@ -118,6 +118,15 @@ bool GeneratorImpl::Generate(const ast::Module& module) {
|
|||
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;
|
||||
|
@ -1467,6 +1476,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_ << "constant ";
|
||||
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 msl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
||||
|
|
|
@ -195,6 +195,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);
|
||||
/// Emits the zero value for the given type
|
||||
/// @param type the type to emit the value for
|
||||
/// @returns true if the zero value was successfully emitted.
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
// 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/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/msl/generator_impl.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace msl {
|
||||
namespace {
|
||||
|
||||
using MslGeneratorImplTest = testing::Test;
|
||||
|
||||
TEST_F(MslGeneratorImplTest, 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)));
|
||||
|
||||
GeneratorImpl g;
|
||||
ASSERT_TRUE(g.EmitProgramConstVariable(var.get())) << g.error();
|
||||
EXPECT_EQ(
|
||||
g.result(),
|
||||
"constant float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace msl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
Loading…
Reference in New Issue