Revert the namer changes.

This CL reverts the changes to use the top level namer in the various
backends. This is causing issues when rolling into Dawn in the case
where the tint generator is used to create SPIR-V which is sent to
SPIRV-Cross but then generator and inspector aren't used. The entry
points end up being incorrect as SPIRV-Cross gets the renamed entry
points.

Change-Id: I4749e1d773f2bd9edcce83e63555f07a443d5ca5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/37342
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2021-01-12 04:34:53 +00:00 committed by Commit Bot service account
parent b91e8a9fbe
commit 987376cd21
97 changed files with 3557 additions and 1468 deletions

View File

@ -590,6 +590,8 @@ source_set("libtint_msl_writer_src") {
"src/writer/msl/generator.h", "src/writer/msl/generator.h",
"src/writer/msl/generator_impl.cc", "src/writer/msl/generator_impl.cc",
"src/writer/msl/generator_impl.h", "src/writer/msl/generator_impl.h",
"src/writer/msl/namer.cc",
"src/writer/msl/namer.h",
] ]
configs += [ ":tint_common_config" ] configs += [ ":tint_common_config" ]
@ -609,6 +611,8 @@ source_set("libtint_hlsl_writer_src") {
"src/writer/hlsl/generator.h", "src/writer/hlsl/generator.h",
"src/writer/hlsl/generator_impl.cc", "src/writer/hlsl/generator_impl.cc",
"src/writer/hlsl/generator_impl.h", "src/writer/hlsl/generator_impl.h",
"src/writer/hlsl/namer.cc",
"src/writer/hlsl/namer.h",
] ]
configs += [ ":tint_common_config" ] configs += [ ":tint_common_config" ]
@ -834,8 +838,6 @@ source_set("tint_unittests_core_src") {
"src/validator/validator_function_test.cc", "src/validator/validator_function_test.cc",
"src/validator/validator_test.cc", "src/validator/validator_test.cc",
"src/writer/float_to_string_test.cc", "src/writer/float_to_string_test.cc",
"src/writer/test_namer.cc",
"src/writer/test_namer.h",
] ]
configs += [ configs += [
@ -1145,6 +1147,7 @@ source_set("tint_unittests_msl_writer_src") {
"src/writer/msl/generator_impl_type_test.cc", "src/writer/msl/generator_impl_type_test.cc",
"src/writer/msl/generator_impl_unary_op_test.cc", "src/writer/msl/generator_impl_unary_op_test.cc",
"src/writer/msl/generator_impl_variable_decl_statement_test.cc", "src/writer/msl/generator_impl_variable_decl_statement_test.cc",
"src/writer/msl/namer_test.cc",
"src/writer/msl/test_helper.h", "src/writer/msl/test_helper.h",
] ]
@ -1199,6 +1202,7 @@ source_set("tint_unittests_hlsl_writer_src") {
"src/writer/hlsl/generator_impl_type_test.cc", "src/writer/hlsl/generator_impl_type_test.cc",
"src/writer/hlsl/generator_impl_unary_op_test.cc", "src/writer/hlsl/generator_impl_unary_op_test.cc",
"src/writer/hlsl/generator_impl_variable_decl_statement_test.cc", "src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
"src/writer/hlsl/namer_test.cc",
"src/writer/hlsl/test_helper.h", "src/writer/hlsl/test_helper.h",
] ]

View File

@ -550,7 +550,7 @@ int main(int argc, const char** argv) {
#if TINT_BUILD_SPV_WRITER #if TINT_BUILD_SPV_WRITER
if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) { if (options.format == Format::kSpirv || options.format == Format::kSpvAsm) {
writer = std::make_unique<tint::writer::spirv::Generator>(&mod); writer = std::make_unique<tint::writer::spirv::Generator>(std::move(mod));
} }
#endif // TINT_BUILD_SPV_WRITER #endif // TINT_BUILD_SPV_WRITER
@ -562,13 +562,13 @@ int main(int argc, const char** argv) {
#if TINT_BUILD_MSL_WRITER #if TINT_BUILD_MSL_WRITER
if (options.format == Format::kMsl) { if (options.format == Format::kMsl) {
writer = std::make_unique<tint::writer::msl::Generator>(&mod); writer = std::make_unique<tint::writer::msl::Generator>(std::move(mod));
} }
#endif // TINT_BUILD_MSL_WRITER #endif // TINT_BUILD_MSL_WRITER
#if TINT_BUILD_HLSL_WRITER #if TINT_BUILD_HLSL_WRITER
if (options.format == Format::kHlsl) { if (options.format == Format::kHlsl) {
writer = std::make_unique<tint::writer::hlsl::Generator>(&mod); writer = std::make_unique<tint::writer::hlsl::Generator>(std::move(mod));
} }
#endif // TINT_BUILD_HLSL_WRITER #endif // TINT_BUILD_HLSL_WRITER

View File

@ -343,6 +343,8 @@ if(${TINT_BUILD_MSL_WRITER})
writer/msl/generator.h writer/msl/generator.h
writer/msl/generator_impl.cc writer/msl/generator_impl.cc
writer/msl/generator_impl.h writer/msl/generator_impl.h
writer/msl/namer.cc
writer/msl/namer.h
) )
endif() endif()
@ -352,6 +354,8 @@ if(${TINT_BUILD_HLSL_WRITER})
writer/hlsl/generator.h writer/hlsl/generator.h
writer/hlsl/generator_impl.cc writer/hlsl/generator_impl.cc
writer/hlsl/generator_impl.h writer/hlsl/generator_impl.h
writer/hlsl/namer.cc
writer/hlsl/namer.h
) )
endif() endif()
@ -467,8 +471,6 @@ if(${TINT_BUILD_TESTS})
validator/validator_test.cc validator/validator_test.cc
validator/validator_type_test.cc validator/validator_type_test.cc
writer/float_to_string_test.cc writer/float_to_string_test.cc
writer/test_namer.cc
writer/test_namer.h
) )
if(${TINT_BUILD_SPV_READER}) if(${TINT_BUILD_SPV_READER})
@ -691,6 +693,7 @@ if(${TINT_BUILD_TESTS})
writer/msl/generator_impl_type_test.cc writer/msl/generator_impl_type_test.cc
writer/msl/generator_impl_unary_op_test.cc writer/msl/generator_impl_unary_op_test.cc
writer/msl/generator_impl_variable_decl_statement_test.cc writer/msl/generator_impl_variable_decl_statement_test.cc
writer/msl/namer_test.cc
writer/msl/test_helper.h writer/msl/test_helper.h
) )
endif() endif()
@ -726,6 +729,7 @@ if(${TINT_BUILD_TESTS})
writer/hlsl/generator_impl_type_test.cc writer/hlsl/generator_impl_type_test.cc
writer/hlsl/generator_impl_unary_op_test.cc writer/hlsl/generator_impl_unary_op_test.cc
writer/hlsl/generator_impl_variable_decl_statement_test.cc writer/hlsl/generator_impl_variable_decl_statement_test.cc
writer/hlsl/namer_test.cc
writer/hlsl/test_helper.h writer/hlsl/test_helper.h
) )
endif() endif()

View File

@ -58,12 +58,6 @@ class IdentifierExpression : public Castable<IdentifierExpression, Expression> {
/// @returns true if this identifier is for an intrinsic /// @returns true if this identifier is for an intrinsic
bool IsIntrinsic() const { return intrinsic_ != Intrinsic::kNone; } bool IsIntrinsic() const { return intrinsic_ != Intrinsic::kNone; }
/// Sets the identifier as a swizzle
void SetIsSwizzle() { is_swizzle_ = true; }
/// @returns true if this is a swizzle identifier
bool IsSwizzle() const { return is_swizzle_; }
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.
/// @note Semantic information such as resolved expression type and intrinsic /// @note Semantic information such as resolved expression type and intrinsic
@ -84,9 +78,9 @@ class IdentifierExpression : public Castable<IdentifierExpression, Expression> {
IdentifierExpression(const IdentifierExpression&) = delete; IdentifierExpression(const IdentifierExpression&) = delete;
Symbol const sym_; Symbol const sym_;
Intrinsic intrinsic_ = Intrinsic::kNone; // Semantic info Intrinsic intrinsic_ = Intrinsic::kNone; // Semantic info
std::unique_ptr<intrinsic::Signature> intrinsic_sig_; // Semantic info std::unique_ptr<intrinsic::Signature> intrinsic_sig_; // Semantic info
bool is_swizzle_ = false; // Semantic info
}; };
} // namespace ast } // namespace ast

View File

@ -149,13 +149,13 @@ fn main() -> void {
// reconstruct the WGSL. // reconstruct the WGSL.
std::string src_wgsl; std::string src_wgsl;
{ {
writer::wgsl::Generator src_gen(&src); writer::wgsl::Generator src_gen(std::move(src));
ASSERT_TRUE(src_gen.Generate()); ASSERT_TRUE(src_gen.Generate());
src_wgsl = src_gen.result(); src_wgsl = src_gen.result();
} }
// Print the dst module, check it matches the original source // Print the dst module, check it matches the original source
writer::wgsl::Generator dst_gen(&dst); writer::wgsl::Generator dst_gen(std::move(dst));
ASSERT_TRUE(dst_gen.Generate()); ASSERT_TRUE(dst_gen.Generate());
auto dst_wgsl = dst_gen.result(); auto dst_wgsl = dst_gen.result();
ASSERT_EQ(src_wgsl, dst_wgsl); ASSERT_EQ(src_wgsl, dst_wgsl);

View File

@ -43,18 +43,9 @@
namespace tint { namespace tint {
namespace inspector { namespace inspector {
Inspector::Inspector(ast::Module& module, Namer* namer) Inspector::Inspector(const ast::Module& module) : module_(module) {}
: module_(module), namer_(namer), namer_is_owned_(false) {}
Inspector::Inspector(const ast::Module& module) Inspector::~Inspector() = default;
: module_(module),
namer_(new MangleNamer(&module_)),
namer_is_owned_(true) {}
Inspector::~Inspector() {
if (namer_is_owned_)
delete namer_;
}
std::vector<EntryPoint> Inspector::GetEntryPoints() { std::vector<EntryPoint> Inspector::GetEntryPoints() {
std::vector<EntryPoint> result; std::vector<EntryPoint> result;
@ -66,13 +57,13 @@ std::vector<EntryPoint> Inspector::GetEntryPoints() {
EntryPoint entry_point; EntryPoint entry_point;
entry_point.name = module_.SymbolToName(func->symbol()); entry_point.name = module_.SymbolToName(func->symbol());
entry_point.remapped_name = namer_->NameFor(func->symbol()); entry_point.remapped_name = module_.SymbolToName(func->symbol());
entry_point.stage = func->pipeline_stage(); entry_point.stage = func->pipeline_stage();
std::tie(entry_point.workgroup_size_x, entry_point.workgroup_size_y, std::tie(entry_point.workgroup_size_x, entry_point.workgroup_size_y,
entry_point.workgroup_size_z) = func->workgroup_size(); entry_point.workgroup_size_z) = func->workgroup_size();
for (auto* var : func->referenced_module_variables()) { for (auto* var : func->referenced_module_variables()) {
auto name = namer_->NameFor(var->symbol()); auto name = module_.SymbolToName(var->symbol());
if (var->storage_class() == ast::StorageClass::kInput) { if (var->storage_class() == ast::StorageClass::kInput) {
entry_point.input_variables.push_back(name); entry_point.input_variables.push_back(name);
} else { } else {
@ -87,11 +78,15 @@ std::vector<EntryPoint> Inspector::GetEntryPoints() {
std::string Inspector::GetRemappedNameForEntryPoint( std::string Inspector::GetRemappedNameForEntryPoint(
const std::string& entry_point) { const std::string& entry_point) {
auto* func = FindEntryPointByName(entry_point); // TODO(rharrison): Reenable once all of the backends are using the renamed
if (!func) { // entry points.
return {};
} // auto* func = FindEntryPointByName(entry_point);
return namer_->NameFor(func->symbol()); // if (!func) {
// return {};
// }
// return func->name();
return entry_point;
} }
std::map<uint32_t, Scalar> Inspector::GetConstantIDs() { std::map<uint32_t, Scalar> Inspector::GetConstantIDs() {

View File

@ -25,7 +25,6 @@
#include "src/ast/pipeline_stage.h" #include "src/ast/pipeline_stage.h"
#include "src/inspector/entry_point.h" #include "src/inspector/entry_point.h"
#include "src/inspector/scalar.h" #include "src/inspector/scalar.h"
#include "src/namer.h"
namespace tint { namespace tint {
namespace inspector { namespace inspector {
@ -71,10 +70,6 @@ struct ResourceBinding {
/// Extracts information from a module /// Extracts information from a module
class Inspector { class Inspector {
public: public:
/// Constructor
/// @param module Shader module to extract information from.
/// @param namer the namer to use with the inspector
Inspector(ast::Module& module, Namer* namer);
/// Constructor /// Constructor
/// @param module Shader module to extract information from. /// @param module Shader module to extract information from.
explicit Inspector(const ast::Module& module); explicit Inspector(const ast::Module& module);
@ -133,8 +128,6 @@ class Inspector {
private: private:
const ast::Module& module_; const ast::Module& module_;
Namer* namer_ = nullptr;
bool namer_is_owned_ = false;
std::string error_; std::string error_;
/// @param name name of the entry point to find /// @param name name of the entry point to find

View File

@ -61,7 +61,6 @@
#include "src/ast/variable_decoration.h" #include "src/ast/variable_decoration.h"
#include "src/ast/workgroup_decoration.h" #include "src/ast/workgroup_decoration.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/test_namer.h"
#include "tint/tint.h" #include "tint/tint.h"
namespace tint { namespace tint {
@ -72,8 +71,7 @@ class InspectorHelper : public ast::BuilderWithModule {
public: public:
InspectorHelper() InspectorHelper()
: td_(std::make_unique<TypeDeterminer>(mod)), : td_(std::make_unique<TypeDeterminer>(mod)),
namer_(mod), inspector_(std::make_unique<Inspector>(*mod)),
inspector_(std::make_unique<Inspector>(*mod, &namer_)),
sampler_type_(ast::type::SamplerKind::kSampler), sampler_type_(ast::type::SamplerKind::kSampler),
comparison_sampler_type_(ast::type::SamplerKind::kComparisonSampler) {} comparison_sampler_type_(ast::type::SamplerKind::kComparisonSampler) {}
@ -641,7 +639,6 @@ class InspectorHelper : public ast::BuilderWithModule {
private: private:
std::unique_ptr<TypeDeterminer> td_; std::unique_ptr<TypeDeterminer> td_;
writer::TestNamer namer_;
std::unique_ptr<Inspector> inspector_; std::unique_ptr<Inspector> inspector_;
ast::type::Sampler sampler_type_; ast::type::Sampler sampler_type_;
@ -723,12 +720,14 @@ TEST_F(InspectorGetEntryPointTest, OneEntryPoint) {
}); });
mod->AddFunction(foo); mod->AddFunction(foo);
// TODO(dsinclair): Update to run the namer transform when available.
auto result = inspector()->GetEntryPoints(); auto result = inspector()->GetEntryPoints();
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
EXPECT_EQ("foo", result[0].name); EXPECT_EQ("foo", result[0].name);
EXPECT_EQ("test_foo", result[0].remapped_name); EXPECT_EQ("foo", result[0].remapped_name);
EXPECT_EQ(ast::PipelineStage::kVertex, result[0].stage); EXPECT_EQ(ast::PipelineStage::kVertex, result[0].stage);
} }
@ -745,15 +744,17 @@ TEST_F(InspectorGetEntryPointTest, MultipleEntryPoints) {
}); });
mod->AddFunction(bar); mod->AddFunction(bar);
// TODO(dsinclair): Update to run the namer transform when available.
auto result = inspector()->GetEntryPoints(); auto result = inspector()->GetEntryPoints();
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
ASSERT_EQ(2u, result.size()); ASSERT_EQ(2u, result.size());
EXPECT_EQ("foo", result[0].name); EXPECT_EQ("foo", result[0].name);
EXPECT_EQ("test_foo", result[0].remapped_name); EXPECT_EQ("foo", result[0].remapped_name);
EXPECT_EQ(ast::PipelineStage::kVertex, result[0].stage); EXPECT_EQ(ast::PipelineStage::kVertex, result[0].stage);
EXPECT_EQ("bar", result[1].name); EXPECT_EQ("bar", result[1].name);
EXPECT_EQ("test_bar", result[1].remapped_name); EXPECT_EQ("bar", result[1].remapped_name);
EXPECT_EQ(ast::PipelineStage::kCompute, result[1].stage); EXPECT_EQ(ast::PipelineStage::kCompute, result[1].stage);
} }
@ -775,15 +776,17 @@ TEST_F(InspectorGetEntryPointTest, MixFunctionsAndEntryPoints) {
}); });
mod->AddFunction(bar); mod->AddFunction(bar);
// TODO(dsinclair): Update to run the namer transform when available.
auto result = inspector()->GetEntryPoints(); auto result = inspector()->GetEntryPoints();
EXPECT_FALSE(inspector()->has_error()); EXPECT_FALSE(inspector()->has_error());
ASSERT_EQ(2u, result.size()); ASSERT_EQ(2u, result.size());
EXPECT_EQ("foo", result[0].name); EXPECT_EQ("foo", result[0].name);
EXPECT_EQ("test_foo", result[0].remapped_name); EXPECT_EQ("foo", result[0].remapped_name);
EXPECT_EQ(ast::PipelineStage::kVertex, result[0].stage); EXPECT_EQ(ast::PipelineStage::kVertex, result[0].stage);
EXPECT_EQ("bar", result[1].name); EXPECT_EQ("bar", result[1].name);
EXPECT_EQ("test_bar", result[1].remapped_name); EXPECT_EQ("bar", result[1].remapped_name);
EXPECT_EQ(ast::PipelineStage::kFragment, result[1].stage); EXPECT_EQ(ast::PipelineStage::kFragment, result[1].stage);
} }
@ -862,9 +865,9 @@ TEST_F(InspectorGetEntryPointTest, EntryPointInOutVariables) {
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
ASSERT_EQ(1u, result[0].input_variables.size()); ASSERT_EQ(1u, result[0].input_variables.size());
EXPECT_EQ("test_in_var", result[0].input_variables[0]); EXPECT_EQ("in_var", result[0].input_variables[0]);
ASSERT_EQ(1u, result[0].output_variables.size()); ASSERT_EQ(1u, result[0].output_variables.size());
EXPECT_EQ("test_out_var", result[0].output_variables[0]); EXPECT_EQ("out_var", result[0].output_variables[0]);
} }
TEST_F(InspectorGetEntryPointTest, FunctionInOutVariables) { TEST_F(InspectorGetEntryPointTest, FunctionInOutVariables) {
@ -889,9 +892,9 @@ TEST_F(InspectorGetEntryPointTest, FunctionInOutVariables) {
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
ASSERT_EQ(1u, result[0].input_variables.size()); ASSERT_EQ(1u, result[0].input_variables.size());
EXPECT_EQ("test_in_var", result[0].input_variables[0]); EXPECT_EQ("in_var", result[0].input_variables[0]);
ASSERT_EQ(1u, result[0].output_variables.size()); ASSERT_EQ(1u, result[0].output_variables.size());
EXPECT_EQ("test_out_var", result[0].output_variables[0]); EXPECT_EQ("out_var", result[0].output_variables[0]);
} }
TEST_F(InspectorGetEntryPointTest, RepeatedInOutVariables) { TEST_F(InspectorGetEntryPointTest, RepeatedInOutVariables) {
@ -916,9 +919,9 @@ TEST_F(InspectorGetEntryPointTest, RepeatedInOutVariables) {
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
ASSERT_EQ(1u, result[0].input_variables.size()); ASSERT_EQ(1u, result[0].input_variables.size());
EXPECT_EQ("test_in_var", result[0].input_variables[0]); EXPECT_EQ("in_var", result[0].input_variables[0]);
ASSERT_EQ(1u, result[0].output_variables.size()); ASSERT_EQ(1u, result[0].output_variables.size());
EXPECT_EQ("test_out_var", result[0].output_variables[0]); EXPECT_EQ("out_var", result[0].output_variables[0]);
} }
TEST_F(InspectorGetEntryPointTest, EntryPointMultipleInOutVariables) { TEST_F(InspectorGetEntryPointTest, EntryPointMultipleInOutVariables) {
@ -939,11 +942,11 @@ TEST_F(InspectorGetEntryPointTest, EntryPointMultipleInOutVariables) {
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
ASSERT_EQ(2u, result[0].input_variables.size()); ASSERT_EQ(2u, result[0].input_variables.size());
EXPECT_TRUE(ContainsString(result[0].input_variables, "test_in_var")); EXPECT_TRUE(ContainsString(result[0].input_variables, "in_var"));
EXPECT_TRUE(ContainsString(result[0].input_variables, "test_in2_var")); EXPECT_TRUE(ContainsString(result[0].input_variables, "in2_var"));
ASSERT_EQ(2u, result[0].output_variables.size()); ASSERT_EQ(2u, result[0].output_variables.size());
EXPECT_TRUE(ContainsString(result[0].output_variables, "test_out_var")); EXPECT_TRUE(ContainsString(result[0].output_variables, "out_var"));
EXPECT_TRUE(ContainsString(result[0].output_variables, "test_out2_var")); EXPECT_TRUE(ContainsString(result[0].output_variables, "out2_var"));
} }
TEST_F(InspectorGetEntryPointTest, FunctionMultipleInOutVariables) { TEST_F(InspectorGetEntryPointTest, FunctionMultipleInOutVariables) {
@ -968,11 +971,11 @@ TEST_F(InspectorGetEntryPointTest, FunctionMultipleInOutVariables) {
ASSERT_EQ(1u, result.size()); ASSERT_EQ(1u, result.size());
ASSERT_EQ(2u, result[0].input_variables.size()); ASSERT_EQ(2u, result[0].input_variables.size());
EXPECT_TRUE(ContainsString(result[0].input_variables, "test_in_var")); EXPECT_TRUE(ContainsString(result[0].input_variables, "in_var"));
EXPECT_TRUE(ContainsString(result[0].input_variables, "test_in2_var")); EXPECT_TRUE(ContainsString(result[0].input_variables, "in2_var"));
ASSERT_EQ(2u, result[0].output_variables.size()); ASSERT_EQ(2u, result[0].output_variables.size());
EXPECT_TRUE(ContainsString(result[0].output_variables, "test_out_var")); EXPECT_TRUE(ContainsString(result[0].output_variables, "out_var"));
EXPECT_TRUE(ContainsString(result[0].output_variables, "test_out2_var")); EXPECT_TRUE(ContainsString(result[0].output_variables, "out2_var"));
} }
TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsInOutVariables) { TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsInOutVariables) {
@ -994,24 +997,26 @@ TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsInOutVariables) {
ASSERT_TRUE(td()->Determine()) << td()->error(); ASSERT_TRUE(td()->Determine()) << td()->error();
// TODO(dsinclair): Update to run the namer transform when available.
auto result = inspector()->GetEntryPoints(); auto result = inspector()->GetEntryPoints();
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
ASSERT_EQ(2u, result.size()); ASSERT_EQ(2u, result.size());
ASSERT_EQ("foo", result[0].name); ASSERT_EQ("foo", result[0].name);
ASSERT_EQ("test_foo", result[0].remapped_name); ASSERT_EQ("foo", result[0].remapped_name);
ASSERT_EQ(1u, result[0].input_variables.size()); ASSERT_EQ(1u, result[0].input_variables.size());
EXPECT_EQ("test_in_var", result[0].input_variables[0]); EXPECT_EQ("in_var", result[0].input_variables[0]);
ASSERT_EQ(1u, result[0].output_variables.size()); ASSERT_EQ(1u, result[0].output_variables.size());
EXPECT_EQ("test_out2_var", result[0].output_variables[0]); EXPECT_EQ("out2_var", result[0].output_variables[0]);
ASSERT_EQ("bar", result[1].name); ASSERT_EQ("bar", result[1].name);
ASSERT_EQ("test_bar", result[1].remapped_name); ASSERT_EQ("bar", result[1].remapped_name);
ASSERT_EQ(1u, result[1].input_variables.size()); ASSERT_EQ(1u, result[1].input_variables.size());
EXPECT_EQ("test_in2_var", result[1].input_variables[0]); EXPECT_EQ("in2_var", result[1].input_variables[0]);
ASSERT_EQ(1u, result[1].output_variables.size()); ASSERT_EQ(1u, result[1].output_variables.size());
EXPECT_EQ("test_out_var", result[1].output_variables[0]); EXPECT_EQ("out_var", result[1].output_variables[0]);
} }
TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsSharedInOutVariables) { TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsSharedInOutVariables) {
@ -1037,36 +1042,42 @@ TEST_F(InspectorGetEntryPointTest, MultipleEntryPointsSharedInOutVariables) {
ASSERT_TRUE(td()->Determine()) << td()->error(); ASSERT_TRUE(td()->Determine()) << td()->error();
// TODO(dsinclair): Update to run the namer transform when available.
auto result = inspector()->GetEntryPoints(); auto result = inspector()->GetEntryPoints();
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
ASSERT_EQ(2u, result.size()); ASSERT_EQ(2u, result.size());
ASSERT_EQ("foo", result[0].name); ASSERT_EQ("foo", result[0].name);
ASSERT_EQ("test_foo", result[0].remapped_name); ASSERT_EQ("foo", result[0].remapped_name);
EXPECT_EQ(2u, result[0].input_variables.size()); EXPECT_EQ(2u, result[0].input_variables.size());
EXPECT_TRUE(ContainsString(result[0].input_variables, "test_in_var")); EXPECT_TRUE(ContainsString(result[0].input_variables, "in_var"));
EXPECT_TRUE(ContainsString(result[0].input_variables, "test_in2_var")); EXPECT_TRUE(ContainsString(result[0].input_variables, "in2_var"));
EXPECT_EQ(2u, result[0].output_variables.size()); EXPECT_EQ(2u, result[0].output_variables.size());
EXPECT_TRUE(ContainsString(result[0].output_variables, "test_out_var")); EXPECT_TRUE(ContainsString(result[0].output_variables, "out_var"));
EXPECT_TRUE(ContainsString(result[0].output_variables, "test_out2_var")); EXPECT_TRUE(ContainsString(result[0].output_variables, "out2_var"));
ASSERT_EQ("bar", result[1].name); ASSERT_EQ("bar", result[1].name);
ASSERT_EQ("test_bar", result[1].remapped_name); ASSERT_EQ("bar", result[1].remapped_name);
EXPECT_EQ(1u, result[1].input_variables.size()); EXPECT_EQ(1u, result[1].input_variables.size());
EXPECT_EQ("test_in2_var", result[1].input_variables[0]); EXPECT_EQ("in2_var", result[1].input_variables[0]);
EXPECT_EQ(1u, result[1].output_variables.size()); EXPECT_EQ(1u, result[1].output_variables.size());
EXPECT_EQ("test_out2_var", result[1].output_variables[0]); EXPECT_EQ("out2_var", result[1].output_variables[0]);
} }
TEST_F(InspectorGetRemappedNameForEntryPointTest, NoFunctions) { // TODO(rharrison): Reenable once GetRemappedNameForEntryPoint isn't a pass
// through
TEST_F(InspectorGetRemappedNameForEntryPointTest, DISABLED_NoFunctions) {
auto result = inspector()->GetRemappedNameForEntryPoint("foo"); auto result = inspector()->GetRemappedNameForEntryPoint("foo");
ASSERT_TRUE(inspector()->has_error()); ASSERT_TRUE(inspector()->has_error());
EXPECT_EQ("", result); EXPECT_EQ("", result);
} }
TEST_F(InspectorGetRemappedNameForEntryPointTest, NoEntryPoints) { // TODO(rharrison): Reenable once GetRemappedNameForEntryPoint isn't a pass
// through
TEST_F(InspectorGetRemappedNameForEntryPointTest, DISABLED_NoEntryPoints) {
mod->AddFunction(MakeEmptyBodyFunction("foo", {})); mod->AddFunction(MakeEmptyBodyFunction("foo", {}));
auto result = inspector()->GetRemappedNameForEntryPoint("foo"); auto result = inspector()->GetRemappedNameForEntryPoint("foo");
@ -1075,26 +1086,35 @@ TEST_F(InspectorGetRemappedNameForEntryPointTest, NoEntryPoints) {
EXPECT_EQ("", result); EXPECT_EQ("", result);
} }
TEST_F(InspectorGetRemappedNameForEntryPointTest, OneEntryPoint) { // TODO(rharrison): Reenable once GetRemappedNameForEntryPoint isn't a pass
// through
TEST_F(InspectorGetRemappedNameForEntryPointTest, DISABLED_OneEntryPoint) {
auto* foo = MakeEmptyBodyFunction( auto* foo = MakeEmptyBodyFunction(
"foo", ast::FunctionDecorationList{ "foo", ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex), create<ast::StageDecoration>(ast::PipelineStage::kVertex),
}); });
mod->AddFunction(foo); mod->AddFunction(foo);
// TODO(dsinclair): Update to run the namer transform when available.
auto result = inspector()->GetRemappedNameForEntryPoint("foo"); auto result = inspector()->GetRemappedNameForEntryPoint("foo");
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
EXPECT_EQ("test_foo", result); EXPECT_EQ("foo", result);
} }
TEST_F(InspectorGetRemappedNameForEntryPointTest, MultipleEntryPoints) { // TODO(rharrison): Reenable once GetRemappedNameForEntryPoint isn't a pass
// through
TEST_F(InspectorGetRemappedNameForEntryPointTest,
DISABLED_MultipleEntryPoints) {
auto* foo = MakeEmptyBodyFunction( auto* foo = MakeEmptyBodyFunction(
"foo", ast::FunctionDecorationList{ "foo", ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kVertex), create<ast::StageDecoration>(ast::PipelineStage::kVertex),
}); });
mod->AddFunction(foo); mod->AddFunction(foo);
// TODO(dsinclair): Update to run the namer transform when available.
auto* bar = MakeEmptyBodyFunction( auto* bar = MakeEmptyBodyFunction(
"bar", ast::FunctionDecorationList{ "bar", ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kCompute), create<ast::StageDecoration>(ast::PipelineStage::kCompute),
@ -1104,12 +1124,12 @@ TEST_F(InspectorGetRemappedNameForEntryPointTest, MultipleEntryPoints) {
{ {
auto result = inspector()->GetRemappedNameForEntryPoint("foo"); auto result = inspector()->GetRemappedNameForEntryPoint("foo");
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
EXPECT_EQ("test_foo", result); EXPECT_EQ("foo", result);
} }
{ {
auto result = inspector()->GetRemappedNameForEntryPoint("bar"); auto result = inspector()->GetRemappedNameForEntryPoint("bar");
ASSERT_FALSE(inspector()->has_error()) << inspector()->error(); ASSERT_FALSE(inspector()->has_error()) << inspector()->error();
EXPECT_EQ("test_bar", result); EXPECT_EQ("bar", result);
} }
} }

View File

@ -22,14 +22,10 @@
namespace tint { namespace tint {
Namer::Namer(const ast::Module* mod) : module_(mod) {} Namer::Namer(ast::Module* mod) : module_(mod) {}
Namer::~Namer() = default; Namer::~Namer() = default;
void Namer::Reset() {
used_.clear();
}
bool Namer::IsUsed(const std::string& name) { bool Namer::IsUsed(const std::string& name) {
auto it = used_.find(name); auto it = used_.find(name);
return it != used_.end(); return it != used_.end();
@ -46,7 +42,7 @@ std::string Namer::GenerateName(const std::string& prefix) {
return name; return name;
} }
MangleNamer::MangleNamer(const ast::Module* mod) : Namer(mod) {} MangleNamer::MangleNamer(ast::Module* mod) : Namer(mod) {}
MangleNamer::~MangleNamer() = default; MangleNamer::~MangleNamer() = default;
@ -54,7 +50,7 @@ std::string MangleNamer::NameFor(const Symbol& sym) {
return sym.to_str(); return sym.to_str();
} }
UnsafeNamer::UnsafeNamer(const ast::Module* mod) : Namer(mod) {} UnsafeNamer::UnsafeNamer(ast::Module* mod) : Namer(mod) {}
UnsafeNamer::~UnsafeNamer() = default; UnsafeNamer::~UnsafeNamer() = default;

View File

@ -28,7 +28,7 @@ class Namer {
public: public:
/// Constructor /// Constructor
/// @param mod the module this namer works with /// @param mod the module this namer works with
explicit Namer(const ast::Module* mod); explicit Namer(ast::Module* mod);
/// Destructor /// Destructor
virtual ~Namer(); virtual ~Namer();
@ -42,9 +42,6 @@ class Namer {
/// @returns the unique name string /// @returns the unique name string
std::string GenerateName(const std::string& prefix); std::string GenerateName(const std::string& prefix);
/// Resets the namer, removing all known symbols.
void Reset();
protected: protected:
/// Checks if `name` has been used /// Checks if `name` has been used
/// @param name the name to check /// @param name the name to check
@ -52,7 +49,7 @@ class Namer {
bool IsUsed(const std::string& name); bool IsUsed(const std::string& name);
/// The module storing the symbol table /// The module storing the symbol table
const ast::Module* module_ = nullptr; ast::Module* module_ = nullptr;
private: private:
// The list of names taken by the remapper // The list of names taken by the remapper
@ -64,7 +61,7 @@ class MangleNamer : public Namer {
public: public:
/// Constructor /// Constructor
/// @param mod the module to retrieve names from /// @param mod the module to retrieve names from
explicit MangleNamer(const ast::Module* mod); explicit MangleNamer(ast::Module* mod);
/// Destructor /// Destructor
~MangleNamer() override; ~MangleNamer() override;
@ -81,7 +78,7 @@ class UnsafeNamer : public Namer {
public: public:
/// Constructor /// Constructor
/// @param mod the module to retrieve names from /// @param mod the module to retrieve names from
explicit UnsafeNamer(const ast::Module* mod); explicit UnsafeNamer(ast::Module* mod);
/// Destructor /// Destructor
~UnsafeNamer() override; ~UnsafeNamer() override;

View File

@ -68,7 +68,7 @@ class TransformTest : public testing::Test {
// Release the source module to ensure there's no uncloned data in result // Release the source module to ensure there's no uncloned data in result
{ auto tmp = std::move(module); } { auto tmp = std::move(module); }
writer::wgsl::Generator generator(&(result.module)); writer::wgsl::Generator generator(std::move(result.module));
if (!generator.Generate()) { if (!generator.Generate()) {
return "WGSL writer failed:\n" + generator.error(); return "WGSL writer failed:\n" + generator.error();
} }

View File

@ -1101,7 +1101,7 @@ bool TypeDeterminer::DetermineMemberAccessor(
ret = mod_->create<ast::type::Pointer>(ret, ptr->storage_class()); ret = mod_->create<ast::type::Pointer>(ret, ptr->storage_class());
} }
} else if (auto* vec = data_type->As<ast::type::Vector>()) { } else if (auto* vec = data_type->As<ast::type::Vector>()) {
expr->member()->SetIsSwizzle(); // TODO(dsinclair): Swizzle, record into the identifier experesion
auto size = mod_->SymbolToName(expr->member()->symbol()).size(); auto size = mod_->SymbolToName(expr->member()->symbol()).size();
if (size == 1) { if (size == 1) {

View File

@ -22,21 +22,14 @@ namespace hlsl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), : Text(std::move(module)),
namer_(std::make_unique<MangleNamer>(module_)), impl_(std::make_unique<GeneratorImpl>(&module_)) {}
impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::Generator(ast::Module* module)
: Text(module),
namer_(std::make_unique<MangleNamer>(module_)),
impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
set_error(""); set_error("");
out_ = std::ostringstream(); out_ = std::ostringstream();
namer_->Reset(); impl_ = std::make_unique<GeneratorImpl>(&module_);
impl_ = std::make_unique<GeneratorImpl>(module_, namer_.get());
} }
bool Generator::Generate() { bool Generator::Generate() {

View File

@ -19,7 +19,6 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include "src/namer.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/generator_impl.h"
#include "src/writer/text.h" #include "src/writer/text.h"
@ -31,12 +30,8 @@ namespace hlsl {
class Generator : public Text { class Generator : public Text {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator
@ -61,7 +56,6 @@ class Generator : public Text {
private: private:
std::ostringstream out_; std::ostringstream out_;
std::unique_ptr<Namer> namer_;
std::unique_ptr<GeneratorImpl> impl_; std::unique_ptr<GeneratorImpl> impl_;
}; };

View File

@ -126,8 +126,7 @@ const char* image_format_to_rwtexture_type(
} // namespace } // namespace
GeneratorImpl::GeneratorImpl(ast::Module* module, Namer* namer) GeneratorImpl::GeneratorImpl(ast::Module* module) : module_(module) {}
: module_(module), namer_(namer) {}
GeneratorImpl::~GeneratorImpl() = default; GeneratorImpl::~GeneratorImpl() = default;
@ -194,25 +193,36 @@ void GeneratorImpl::register_global(ast::Variable* global) {
global_variables_.set(global->symbol(), global); global_variables_.set(global->symbol(), global);
} }
Symbol GeneratorImpl::current_ep_var_symbol(VarType type) { std::string GeneratorImpl::generate_name(const std::string& prefix) {
Symbol sym; std::string name = prefix;
uint32_t i = 0;
while (namer_.IsMapped(name) || namer_.IsRemapped(name)) {
name = prefix + "_" + std::to_string(i);
++i;
}
namer_.RegisterRemappedName(name);
return name;
}
std::string GeneratorImpl::current_ep_var_name(VarType type) {
std::string name = "";
switch (type) { switch (type) {
case VarType::kIn: { case VarType::kIn: {
auto in_it = ep_sym_to_in_data_.find(current_ep_sym_); auto in_it = ep_sym_to_in_data_.find(current_ep_sym_);
if (in_it != ep_sym_to_in_data_.end()) { if (in_it != ep_sym_to_in_data_.end()) {
sym = in_it->second.var_symbol; name = in_it->second.var_name;
} }
break; break;
} }
case VarType::kOut: { case VarType::kOut: {
auto outit = ep_sym_to_out_data_.find(current_ep_sym_); auto outit = ep_sym_to_out_data_.find(current_ep_sym_);
if (outit != ep_sym_to_out_data_.end()) { if (outit != ep_sym_to_out_data_.end()) {
sym = outit->second.var_symbol; name = outit->second.var_name;
} }
break; break;
} }
} }
return sym; return name;
} }
bool GeneratorImpl::EmitConstructedType(std::ostream& out, bool GeneratorImpl::EmitConstructedType(std::ostream& out,
@ -223,18 +233,19 @@ bool GeneratorImpl::EmitConstructedType(std::ostream& out,
// HLSL typedef is for intrinsic types only. For an alias'd struct, // HLSL typedef is for intrinsic types only. For an alias'd struct,
// generate a secondary struct with the new name. // generate a secondary struct with the new name.
if (auto* str = alias->type()->As<ast::type::Struct>()) { if (auto* str = alias->type()->As<ast::type::Struct>()) {
if (!EmitStructType(out, str, alias->symbol())) { if (!EmitStructType(out, str, module_->SymbolToName(alias->symbol()))) {
return false; return false;
} }
return true; return true;
} }
out << "typedef "; out << "typedef ";
if (!EmitType(out, alias->type(), Symbol())) { if (!EmitType(out, alias->type(), "")) {
return false; return false;
} }
out << " " << namer_->NameFor(alias->symbol()) << ";" << std::endl; out << " " << namer_.NameFor(module_->SymbolToName(alias->symbol())) << ";"
<< std::endl;
} else if (auto* str = ty->As<ast::type::Struct>()) { } else if (auto* str = ty->As<ast::type::Struct>()) {
if (!EmitStructType(out, str, str->symbol())) { if (!EmitStructType(out, str, module_->SymbolToName(str->symbol()))) {
return false; return false;
} }
} else { } else {
@ -275,7 +286,7 @@ bool GeneratorImpl::EmitBitcast(std::ostream& pre,
} }
out << "as"; out << "as";
if (!EmitType(out, expr->type(), Symbol())) { if (!EmitType(out, expr->type(), "")) {
return false; return false;
} }
out << "("; out << "(";
@ -341,7 +352,7 @@ bool GeneratorImpl::EmitBinary(std::ostream& pre,
return false; return false;
} }
auto name = namer_->GenerateName(kTempNamePrefix); auto name = generate_name(kTempNamePrefix);
make_indent(pre); make_indent(pre);
pre << "bool " << name << " = " << lhs_out.str() << ";" << std::endl; pre << "bool " << name << " = " << lhs_out.str() << ";" << std::endl;
@ -601,13 +612,13 @@ bool GeneratorImpl::EmitCall(std::ostream& pre,
// // We create variables to hold the two parameters in case they're // // We create variables to hold the two parameters in case they're
// // function calls with side effects. // // function calls with side effects.
// auto* param0 = param[0].get(); // auto* param0 = param[0].get();
// auto* name0 = namer_->GenerateName("outer_product_expr_0"); // auto* name0 = generate_name("outer_product_expr_0");
// auto* param1 = param[1].get(); // auto* param1 = param[1].get();
// auto* name1 = namer_->GenerateName("outer_product_expr_1"); // auto* name1 = generate_name("outer_product_expr_1");
// make_indent(out); // make_indent(out);
// if (!EmitType(out, expr->result_type(), Symbol()) { // if (!EmitType(out, expr->result_type(), "")) {
// return false; // return false;
// } // }
// out << "("; // out << "(";
@ -669,12 +680,12 @@ bool GeneratorImpl::EmitCall(std::ostream& pre,
return true; return true;
} }
auto func_name_sym = ident->symbol(); auto name = module_->SymbolToName(ident->symbol());
auto it = auto caller_sym = ident->symbol();
ep_func_name_remapped_.find(module_->SymbolToName(current_ep_sym_) + "_" + auto it = ep_func_name_remapped_.find(current_ep_sym_.to_str() + "_" +
module_->SymbolToName(func_name_sym)); caller_sym.to_str());
if (it != ep_func_name_remapped_.end()) { if (it != ep_func_name_remapped_.end()) {
func_name_sym = it->second; name = it->second;
} }
auto* func = module_->FindFunctionBySymbol(ident->symbol()); auto* func = module_->FindFunctionBySymbol(ident->symbol());
@ -684,24 +695,24 @@ bool GeneratorImpl::EmitCall(std::ostream& pre,
return false; return false;
} }
out << namer_->NameFor(func_name_sym) << "("; out << name << "(";
bool first = true; bool first = true;
if (has_referenced_in_var_needing_struct(func)) { if (has_referenced_in_var_needing_struct(func)) {
auto var_sym = current_ep_var_symbol(VarType::kIn); auto var_name = current_ep_var_name(VarType::kIn);
if (var_sym.IsValid()) { if (!var_name.empty()) {
out << namer_->NameFor(var_sym); out << var_name;
first = false; first = false;
} }
} }
if (has_referenced_out_var_needing_struct(func)) { if (has_referenced_out_var_needing_struct(func)) {
auto var_sym = current_ep_var_symbol(VarType::kOut); auto var_name = current_ep_var_name(VarType::kOut);
if (var_sym.IsValid()) { if (!var_name.empty()) {
if (!first) { if (!first) {
out << ", "; out << ", ";
} }
first = false; first = false;
out << namer_->NameFor(var_sym); out << var_name;
} }
} }
@ -738,8 +749,8 @@ bool GeneratorImpl::EmitTextureCall(std::ostream& pre,
if (ident->intrinsic() == ast::Intrinsic::kTextureDimensions) { if (ident->intrinsic() == ast::Intrinsic::kTextureDimensions) {
// Declare a variable to hold the texture dimensions // Declare a variable to hold the texture dimensions
auto dims = namer_->GenerateName(kTempNamePrefix); auto dims = generate_name(kTempNamePrefix);
EmitType(pre, expr->result_type(), Symbol()); EmitType(pre, expr->result_type(), "");
pre << " " << dims << ";" << std::endl; pre << " " << dims << ";" << std::endl;
// Now call GetDimensions() on the texture object, populating the dims // Now call GetDimensions() on the texture object, populating the dims
@ -994,7 +1005,7 @@ bool GeneratorImpl::EmitTypeConstructor(std::ostream& pre,
if (expr->type()->Is<ast::type::Array>()) { if (expr->type()->Is<ast::type::Array>()) {
out << "{"; out << "{";
} else { } else {
if (!EmitType(out, expr->type(), Symbol())) { if (!EmitType(out, expr->type(), "")) {
return false; return false;
} }
out << "("; out << "(";
@ -1092,21 +1103,15 @@ bool GeneratorImpl::EmitIdentifier(std::ostream&,
auto var_type = var->storage_class() == ast::StorageClass::kInput auto var_type = var->storage_class() == ast::StorageClass::kInput
? VarType::kIn ? VarType::kIn
: VarType::kOut; : VarType::kOut;
auto sym = current_ep_var_symbol(var_type); auto name = current_ep_var_name(var_type);
if (!sym.IsValid()) { if (name.empty()) {
error_ = "unable to find entry point data for variable"; error_ = "unable to find entry point data for variable";
return false; return false;
} }
out << namer_->NameFor(sym) << "."; out << name << ".";
} }
} }
out << namer_.NameFor(module_->SymbolToName(ident->symbol()));
// Swizzle outputs the name directly
if (ident->IsSwizzle()) {
out << module_->SymbolToName(ident->symbol());
} else {
out << namer_->NameFor(ident->symbol());
}
return true; return true;
} }
@ -1255,22 +1260,28 @@ bool GeneratorImpl::EmitFunctionInternal(std::ostream& out,
ast::Function* func, ast::Function* func,
bool emit_duplicate_functions, bool emit_duplicate_functions,
Symbol ep_sym) { Symbol ep_sym) {
if (!EmitType(out, func->return_type(), Symbol())) { auto name = func->symbol().to_str();
if (!EmitType(out, func->return_type(), "")) {
return false; return false;
} }
out << " "; out << " ";
auto func_name_sym = func->symbol();
if (emit_duplicate_functions) { if (emit_duplicate_functions) {
auto func_name = module_->SymbolToName(func_name_sym); auto func_name = name;
auto ep_name = module_->SymbolToName(ep_sym); auto ep_name = ep_sym.to_str();
func_name_sym = module_->RegisterSymbol( // TODO(dsinclair): The SymbolToName should go away and just use
namer_->GenerateName(func_name + "_" + ep_name)); // to_str() here when the conversion is complete.
ep_func_name_remapped_[ep_name + "_" + func_name] = func_name_sym; name = generate_name(module_->SymbolToName(func->symbol()) + "_" +
module_->SymbolToName(ep_sym));
ep_func_name_remapped_[ep_name + "_" + func_name] = name;
} else {
// TODO(dsinclair): this should be updated to a remapped name
name = namer_.NameFor(module_->SymbolToName(func->symbol()));
} }
out << namer_->NameFor(func_name_sym) << "("; out << name << "(";
bool first = true; bool first = true;
@ -1281,8 +1292,8 @@ bool GeneratorImpl::EmitFunctionInternal(std::ostream& out,
if (emit_duplicate_functions) { if (emit_duplicate_functions) {
auto in_it = ep_sym_to_in_data_.find(ep_sym); auto in_it = ep_sym_to_in_data_.find(ep_sym);
if (in_it != ep_sym_to_in_data_.end()) { if (in_it != ep_sym_to_in_data_.end()) {
out << "in " << namer_->NameFor(in_it->second.struct_symbol) << " " out << "in " << in_it->second.struct_name << " "
<< namer_->NameFor(in_it->second.var_symbol); << in_it->second.var_name;
first = false; first = false;
} }
@ -1291,8 +1302,8 @@ bool GeneratorImpl::EmitFunctionInternal(std::ostream& out,
if (!first) { if (!first) {
out << ", "; out << ", ";
} }
out << "out " << namer_->NameFor(outit->second.struct_symbol) << " " out << "out " << outit->second.struct_name << " "
<< namer_->NameFor(outit->second.var_symbol); << outit->second.var_name;
first = false; first = false;
} }
} }
@ -1303,12 +1314,12 @@ bool GeneratorImpl::EmitFunctionInternal(std::ostream& out,
} }
first = false; first = false;
if (!EmitType(out, v->type(), v->symbol())) { if (!EmitType(out, v->type(), module_->SymbolToName(v->symbol()))) {
return false; return false;
} }
// Array name is output as part of the type // Array name is output as part of the type
if (!v->type()->Is<ast::type::Array>()) { if (!v->type()->Is<ast::type::Array>()) {
out << " " << namer_->NameFor(v->symbol()); out << " " << module_->SymbolToName(v->symbol());
} }
} }
@ -1376,8 +1387,8 @@ bool GeneratorImpl::EmitEntryPointData(
auto* type = var->type()->UnwrapIfNeeded(); auto* type = var->type()->UnwrapIfNeeded();
if (auto* strct = type->As<ast::type::Struct>()) { if (auto* strct = type->As<ast::type::Struct>()) {
out << "ConstantBuffer<" << namer_->NameFor(strct->symbol()) << "> " out << "ConstantBuffer<" << module_->SymbolToName(strct->symbol()) << "> "
<< namer_->NameFor(var->symbol()) << " : register(b" << module_->SymbolToName(var->symbol()) << " : register(b"
<< binding->value() << ");" << std::endl; << binding->value() << ");" << std::endl;
} else { } else {
// TODO(dsinclair): There is outstanding spec work to require all uniform // TODO(dsinclair): There is outstanding spec work to require all uniform
@ -1386,16 +1397,16 @@ bool GeneratorImpl::EmitEntryPointData(
// is not a block. // is not a block.
// Relevant: https://github.com/gpuweb/gpuweb/issues/1004 // Relevant: https://github.com/gpuweb/gpuweb/issues/1004
// https://github.com/gpuweb/gpuweb/issues/1008 // https://github.com/gpuweb/gpuweb/issues/1008
auto name = "cbuffer_" + namer_->NameFor(var->symbol()); auto name = "cbuffer_" + module_->SymbolToName(var->symbol());
out << "cbuffer " << name << " : register(b" << binding->value() << ") {" out << "cbuffer " << name << " : register(b" << binding->value() << ") {"
<< std::endl; << std::endl;
increment_indent(); increment_indent();
make_indent(out); make_indent(out);
if (!EmitType(out, type, Symbol())) { if (!EmitType(out, type, "")) {
return false; return false;
} }
out << " " << namer_->NameFor(var->symbol()) << ";" << std::endl; out << " " << module_->SymbolToName(var->symbol()) << ";" << std::endl;
decrement_indent(); decrement_indent();
out << "};" << std::endl; out << "};" << std::endl;
} }
@ -1427,7 +1438,7 @@ bool GeneratorImpl::EmitEntryPointData(
if (ac->IsReadWrite()) { if (ac->IsReadWrite()) {
out << "RW"; out << "RW";
} }
out << "ByteAddressBuffer " << namer_->NameFor(var->symbol()) out << "ByteAddressBuffer " << module_->SymbolToName(var->symbol())
<< " : register(u" << binding->value() << ");" << std::endl; << " : register(u" << binding->value() << ");" << std::endl;
emitted_storagebuffer = true; emitted_storagebuffer = true;
} }
@ -1436,14 +1447,13 @@ bool GeneratorImpl::EmitEntryPointData(
} }
if (!in_variables.empty()) { if (!in_variables.empty()) {
auto in_struct_sym = module_->RegisterSymbol(namer_->GenerateName( auto in_struct_name = generate_name(module_->SymbolToName(func->symbol()) +
module_->SymbolToName(func->symbol()) + "_" + kInStructNameSuffix)); "_" + kInStructNameSuffix);
auto in_var_name = namer_->GenerateName(kTintStructInVarPrefix); auto in_var_name = generate_name(kTintStructInVarPrefix);
ep_sym_to_in_data_[func->symbol()] = {in_struct_sym, ep_sym_to_in_data_[func->symbol()] = {in_struct_name, in_var_name};
module_->RegisterSymbol(in_var_name)};
make_indent(out); make_indent(out);
out << "struct " << namer_->NameFor(in_struct_sym) << " {" << std::endl; out << "struct " << in_struct_name << " {" << std::endl;
increment_indent(); increment_indent();
@ -1452,11 +1462,11 @@ bool GeneratorImpl::EmitEntryPointData(
auto* deco = data.second; auto* deco = data.second;
make_indent(out); make_indent(out);
if (!EmitType(out, var->type(), var->symbol())) { if (!EmitType(out, var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
out << " " << namer_->NameFor(var->symbol()) << " : "; out << " " << module_->SymbolToName(var->symbol()) << " : ";
if (auto* location = deco->As<ast::LocationDecoration>()) { if (auto* location = deco->As<ast::LocationDecoration>()) {
if (func->pipeline_stage() == ast::PipelineStage::kCompute) { if (func->pipeline_stage() == ast::PipelineStage::kCompute) {
error_ = "invalid location variable for pipeline stage"; error_ = "invalid location variable for pipeline stage";
@ -1483,14 +1493,13 @@ bool GeneratorImpl::EmitEntryPointData(
} }
if (!outvariables.empty()) { if (!outvariables.empty()) {
auto outstruct_sym = module_->RegisterSymbol(namer_->GenerateName( auto outstruct_name = generate_name(module_->SymbolToName(func->symbol()) +
module_->SymbolToName(func->symbol()) + "_" + kOutStructNameSuffix)); "_" + kOutStructNameSuffix);
auto outvar_name = namer_->GenerateName(kTintStructOutVarPrefix); auto outvar_name = generate_name(kTintStructOutVarPrefix);
ep_sym_to_out_data_[func->symbol()] = { ep_sym_to_out_data_[func->symbol()] = {outstruct_name, outvar_name};
outstruct_sym, module_->RegisterSymbol(outvar_name)};
make_indent(out); make_indent(out);
out << "struct " << namer_->NameFor(outstruct_sym) << " {" << std::endl; out << "struct " << outstruct_name << " {" << std::endl;
increment_indent(); increment_indent();
for (auto& data : outvariables) { for (auto& data : outvariables) {
@ -1498,11 +1507,11 @@ bool GeneratorImpl::EmitEntryPointData(
auto* deco = data.second; auto* deco = data.second;
make_indent(out); make_indent(out);
if (!EmitType(out, var->type(), var->symbol())) { if (!EmitType(out, var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
out << " " << namer_->NameFor(var->symbol()) << " : "; out << " " << module_->SymbolToName(var->symbol()) << " : ";
if (auto* location = deco->As<ast::LocationDecoration>()) { if (auto* location = deco->As<ast::LocationDecoration>()) {
auto loc = location->value(); auto loc = location->value();
@ -1580,16 +1589,16 @@ bool GeneratorImpl::EmitEntryPointFunction(std::ostream& out,
auto outdata = ep_sym_to_out_data_.find(current_ep_sym_); auto outdata = ep_sym_to_out_data_.find(current_ep_sym_);
bool has_outdata = outdata != ep_sym_to_out_data_.end(); bool has_outdata = outdata != ep_sym_to_out_data_.end();
if (has_outdata) { if (has_outdata) {
out << namer_->NameFor(outdata->second.struct_symbol); out << outdata->second.struct_name;
} else { } else {
out << "void"; out << "void";
} }
out << " " << namer_->NameFor(current_ep_sym_) << "("; // TODO(dsinclair): This should output the remapped name
out << " " << namer_.NameFor(module_->SymbolToName(current_ep_sym_)) << "(";
auto in_data = ep_sym_to_in_data_.find(current_ep_sym_); auto in_data = ep_sym_to_in_data_.find(current_ep_sym_);
if (in_data != ep_sym_to_in_data_.end()) { if (in_data != ep_sym_to_in_data_.end()) {
out << namer_->NameFor(in_data->second.struct_symbol) << " " out << in_data->second.struct_name << " " << in_data->second.var_name;
<< namer_->NameFor(in_data->second.var_symbol);
} }
out << ") {" << std::endl; out << ") {" << std::endl;
@ -1597,8 +1606,8 @@ bool GeneratorImpl::EmitEntryPointFunction(std::ostream& out,
if (has_outdata) { if (has_outdata) {
make_indent(out); make_indent(out);
out << namer_->NameFor(outdata->second.struct_symbol) << " " out << outdata->second.struct_name << " " << outdata->second.var_name << ";"
<< namer_->NameFor(outdata->second.var_symbol) << ";" << std::endl; << std::endl;
} }
generating_entry_point_ = true; generating_entry_point_ = true;
@ -1664,8 +1673,8 @@ bool GeneratorImpl::EmitZeroValue(std::ostream& out, ast::type::Type* type) {
bool GeneratorImpl::EmitLoop(std::ostream& out, ast::LoopStatement* stmt) { bool GeneratorImpl::EmitLoop(std::ostream& out, ast::LoopStatement* stmt) {
loop_emission_counter_++; loop_emission_counter_++;
std::string guard = namer_->GenerateName( std::string guard = namer_.NameFor("tint_hlsl_is_first_" +
"tint_hlsl_is_first_" + std::to_string(loop_emission_counter_)); std::to_string(loop_emission_counter_));
if (stmt->has_continuing()) { if (stmt->has_continuing()) {
make_indent(out); make_indent(out);
@ -1725,7 +1734,7 @@ bool GeneratorImpl::EmitLoop(std::ostream& out, ast::LoopStatement* stmt) {
} }
out << pre.str(); out << pre.str();
out << namer_->NameFor(var->symbol()) << " = "; out << module_->SymbolToName(var->symbol()) << " = ";
if (var->constructor() != nullptr) { if (var->constructor() != nullptr) {
out << constructor_out.str(); out << constructor_out.str();
} else { } else {
@ -1896,11 +1905,11 @@ bool GeneratorImpl::EmitStorageBufferAccessor(std::ostream& pre,
uint32_t stride = mat->rows() == 2 ? 8 : 16; uint32_t stride = mat->rows() == 2 ? 8 : 16;
if (is_store) { if (is_store) {
if (!EmitType(out, mat, Symbol())) { if (!EmitType(out, mat, "")) {
return false; return false;
} }
auto name = namer_->GenerateName(kTempNamePrefix); auto name = generate_name(kTempNamePrefix);
out << " " << name << " = "; out << " " << name << " = ";
if (!EmitExpression(pre, out, rhs)) { if (!EmitExpression(pre, out, rhs)) {
return false; return false;
@ -2025,7 +2034,7 @@ bool GeneratorImpl::EmitReturn(std::ostream& out, ast::ReturnStatement* stmt) {
out << "return"; out << "return";
auto outdata = ep_sym_to_out_data_.find(current_ep_sym_); auto outdata = ep_sym_to_out_data_.find(current_ep_sym_);
if (outdata != ep_sym_to_out_data_.end()) { if (outdata != ep_sym_to_out_data_.end()) {
out << " " << namer_->NameFor(outdata->second.var_symbol); out << " " << outdata->second.var_name;
} }
} else if (stmt->has_value()) { } else if (stmt->has_value()) {
std::ostringstream pre; std::ostringstream pre;
@ -2123,9 +2132,9 @@ bool GeneratorImpl::EmitSwitch(std::ostream& out, ast::SwitchStatement* stmt) {
bool GeneratorImpl::EmitType(std::ostream& out, bool GeneratorImpl::EmitType(std::ostream& out,
ast::type::Type* type, ast::type::Type* type,
const Symbol& sym) { const std::string& name) {
if (auto* alias = type->As<ast::type::Alias>()) { if (auto* alias = type->As<ast::type::Alias>()) {
out << namer_->NameFor(alias->symbol()); out << namer_.NameFor(module_->SymbolToName(alias->symbol()));
} else if (auto* ary = type->As<ast::type::Array>()) { } else if (auto* ary = type->As<ast::type::Array>()) {
ast::type::Type* base_type = ary; ast::type::Type* base_type = ary;
std::vector<uint32_t> sizes; std::vector<uint32_t> sizes;
@ -2140,11 +2149,11 @@ bool GeneratorImpl::EmitType(std::ostream& out,
} }
base_type = arr->type(); base_type = arr->type();
} }
if (!EmitType(out, base_type, Symbol())) { if (!EmitType(out, base_type, "")) {
return false; return false;
} }
if (sym.IsValid()) { if (!name.empty()) {
out << " " << namer_->NameFor(sym); out << " " << namer_.NameFor(name);
} }
for (uint32_t size : sizes) { for (uint32_t size : sizes) {
out << "[" << size << "]"; out << "[" << size << "]";
@ -2156,7 +2165,7 @@ bool GeneratorImpl::EmitType(std::ostream& out,
} else if (type->Is<ast::type::I32>()) { } else if (type->Is<ast::type::I32>()) {
out << "int"; out << "int";
} else if (auto* mat = type->As<ast::type::Matrix>()) { } else if (auto* mat = type->As<ast::type::Matrix>()) {
if (!EmitType(out, mat->type(), Symbol())) { if (!EmitType(out, mat->type(), "")) {
return false; return false;
} }
out << mat->rows() << "x" << mat->columns(); out << mat->rows() << "x" << mat->columns();
@ -2172,7 +2181,7 @@ bool GeneratorImpl::EmitType(std::ostream& out,
} }
out << "State"; out << "State";
} else if (auto* str = type->As<ast::type::Struct>()) { } else if (auto* str = type->As<ast::type::Struct>()) {
out << namer_->NameFor(str->symbol()); out << module_->SymbolToName(str->symbol());
} else if (auto* tex = type->As<ast::type::Texture>()) { } else if (auto* tex = type->As<ast::type::Texture>()) {
if (tex->Is<ast::type::StorageTexture>()) { if (tex->Is<ast::type::StorageTexture>()) {
out << "RW"; out << "RW";
@ -2227,7 +2236,7 @@ bool GeneratorImpl::EmitType(std::ostream& out,
out << "uint" << size; out << "uint" << size;
} else { } else {
out << "vector<"; out << "vector<";
if (!EmitType(out, vec->type(), Symbol())) { if (!EmitType(out, vec->type(), "")) {
return false; return false;
} }
out << ", " << size << ">"; out << ", " << size << ">";
@ -2244,11 +2253,11 @@ bool GeneratorImpl::EmitType(std::ostream& out,
bool GeneratorImpl::EmitStructType(std::ostream& out, bool GeneratorImpl::EmitStructType(std::ostream& out,
const ast::type::Struct* str, const ast::type::Struct* str,
const Symbol& sym) { const std::string& name) {
// TODO(dsinclair): Block decoration? // TODO(dsinclair): Block decoration?
// if (str->impl()->decoration() != ast::StructDecoration::kNone) { // if (str->impl()->decoration() != ast::StructDecoration::kNone) {
// } // }
out << "struct " << namer_->NameFor(sym) << " {" << std::endl; out << "struct " << name << " {" << std::endl;
increment_indent(); increment_indent();
for (auto* mem : str->impl()->members()) { for (auto* mem : str->impl()->members()) {
@ -2256,12 +2265,12 @@ bool GeneratorImpl::EmitStructType(std::ostream& out,
// TODO(dsinclair): Handle [[offset]] annotation on structs // TODO(dsinclair): Handle [[offset]] annotation on structs
// https://bugs.chromium.org/p/tint/issues/detail?id=184 // https://bugs.chromium.org/p/tint/issues/detail?id=184
if (!EmitType(out, mem->type(), mem->symbol())) { if (!EmitType(out, mem->type(), module_->SymbolToName(mem->symbol()))) {
return false; return false;
} }
// Array member name will be output with the type // Array member name will be output with the type
if (!mem->type()->Is<ast::type::Array>()) { if (!mem->type()->Is<ast::type::Array>()) {
out << " " << namer_->NameFor(mem->symbol()); out << " " << namer_.NameFor(module_->SymbolToName(mem->symbol()));
} }
out << ";" << std::endl; out << ";" << std::endl;
} }
@ -2320,11 +2329,11 @@ bool GeneratorImpl::EmitVariable(std::ostream& out,
if (var->is_const()) { if (var->is_const()) {
out << "const "; out << "const ";
} }
if (!EmitType(out, var->type(), var->symbol())) { if (!EmitType(out, var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
if (!var->type()->Is<ast::type::Array>()) { if (!var->type()->Is<ast::type::Array>()) {
out << " " << namer_->NameFor(var->symbol()); out << " " << module_->SymbolToName(var->symbol());
} }
out << constructor_out.str() << ";" << std::endl; out << constructor_out.str() << ";" << std::endl;
@ -2369,19 +2378,19 @@ bool GeneratorImpl::EmitProgramConstVariable(std::ostream& out,
} }
out << "#endif" << std::endl; out << "#endif" << std::endl;
out << "static const "; out << "static const ";
if (!EmitType(out, var->type(), var->symbol())) { if (!EmitType(out, var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
out << " " << namer_->NameFor(var->symbol()) << " = WGSL_SPEC_CONSTANT_" out << " " << module_->SymbolToName(var->symbol())
<< const_id << ";" << std::endl; << " = WGSL_SPEC_CONSTANT_" << const_id << ";" << std::endl;
out << "#undef WGSL_SPEC_CONSTANT_" << const_id << std::endl; out << "#undef WGSL_SPEC_CONSTANT_" << const_id << std::endl;
} else { } else {
out << "static const "; out << "static const ";
if (!EmitType(out, var->type(), var->symbol())) { if (!EmitType(out, var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
if (!var->type()->Is<ast::type::Array>()) { if (!var->type()->Is<ast::type::Array>()) {
out << " " << namer_->NameFor(var->symbol()); out << " " << module_->SymbolToName(var->symbol());
} }
if (var->constructor() != nullptr) { if (var->constructor() != nullptr) {
@ -2396,7 +2405,7 @@ bool GeneratorImpl::EmitProgramConstVariable(std::ostream& out,
std::string GeneratorImpl::get_buffer_name(ast::Expression* expr) { std::string GeneratorImpl::get_buffer_name(ast::Expression* expr) {
for (;;) { for (;;) {
if (auto* ident = expr->As<ast::IdentifierExpression>()) { if (auto* ident = expr->As<ast::IdentifierExpression>()) {
return namer_->NameFor(ident->symbol()); return module_->SymbolToName(ident->symbol());
} else if (auto* member = expr->As<ast::MemberAccessorExpression>()) { } else if (auto* member = expr->As<ast::MemberAccessorExpression>()) {
expr = member->structure(); expr = member->structure();
} else if (auto* array = expr->As<ast::ArrayAccessorExpression>()) { } else if (auto* array = expr->As<ast::ArrayAccessorExpression>()) {

View File

@ -15,7 +15,6 @@
#ifndef SRC_WRITER_HLSL_GENERATOR_IMPL_H_ #ifndef SRC_WRITER_HLSL_GENERATOR_IMPL_H_
#define SRC_WRITER_HLSL_GENERATOR_IMPL_H_ #define SRC_WRITER_HLSL_GENERATOR_IMPL_H_
#include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
@ -42,8 +41,8 @@
#include "src/ast/type/struct_type.h" #include "src/ast/type/struct_type.h"
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/ast/unary_op_expression.h" #include "src/ast/unary_op_expression.h"
#include "src/namer.h"
#include "src/scope_stack.h" #include "src/scope_stack.h"
#include "src/writer/hlsl/namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -54,8 +53,7 @@ class GeneratorImpl {
public: public:
/// Constructor /// Constructor
/// @param module the module to generate /// @param module the module to generate
/// @param namer the namer to use explicit GeneratorImpl(ast::Module* module);
GeneratorImpl(ast::Module* module, Namer* namer);
~GeneratorImpl(); ~GeneratorImpl();
/// Increment the emitter indent level /// Increment the emitter indent level
@ -290,17 +288,19 @@ class GeneratorImpl {
/// Handles generating type /// Handles generating type
/// @param out the output stream /// @param out the output stream
/// @param type the type to generate /// @param type the type to generate
/// @param sym the symbol of the variable, Only used for array emission /// @param name the name of the variable, only used for array emission
/// @returns true if the type is emitted /// @returns true if the type is emitted
bool EmitType(std::ostream& out, ast::type::Type* type, const Symbol& sym); bool EmitType(std::ostream& out,
ast::type::Type* type,
const std::string& name);
/// Handles generating a structure declaration /// Handles generating a structure declaration
/// @param out the output stream /// @param out the output stream
/// @param ty the struct to generate /// @param ty the struct to generate
/// @param sym the struct symbol /// @param name the struct name
/// @returns true if the struct is emitted /// @returns true if the struct is emitted
bool EmitStructType(std::ostream& out, bool EmitStructType(std::ostream& out,
const ast::type::Struct* ty, const ast::type::Struct* ty,
const Symbol& sym); const std::string& name);
/// Handles a unary op expression /// Handles a unary op expression
/// @param pre the preamble for the expression stream /// @param pre the preamble for the expression stream
/// @param out the output of the expression stream /// @param out the output of the expression stream
@ -375,22 +375,30 @@ class GeneratorImpl {
/// @returns true if an input or output struct is required. /// @returns true if an input or output struct is required.
bool has_referenced_var_needing_struct(ast::Function* func); bool has_referenced_var_needing_struct(ast::Function* func);
/// @returns the namer for testing
Namer* namer_for_testing() { return &namer_; }
/// Generate a unique name
/// @param prefix the name prefix
/// @returns a unique name
std::string generate_name(const std::string& prefix);
private: private:
enum class VarType { kIn, kOut }; enum class VarType { kIn, kOut };
struct EntryPointData { struct EntryPointData {
Symbol struct_symbol; std::string struct_name;
Symbol var_symbol; std::string var_name;
}; };
Symbol current_ep_var_symbol(VarType type); std::string current_ep_var_name(VarType type);
std::string get_buffer_name(ast::Expression* expr); std::string get_buffer_name(ast::Expression* expr);
std::string error_; std::string error_;
size_t indent_ = 0; size_t indent_ = 0;
Namer namer_;
ast::Module* module_ = nullptr; ast::Module* module_ = nullptr;
Namer* namer_;
Symbol current_ep_sym_; Symbol current_ep_sym_;
bool generating_entry_point_ = false; bool generating_entry_point_ = false;
uint32_t loop_emission_counter_ = 0; uint32_t loop_emission_counter_ = 0;
@ -401,7 +409,7 @@ class GeneratorImpl {
// This maps an input of "<entry_point_name>_<function_name>" to a remapped // This maps an input of "<entry_point_name>_<function_name>" to a remapped
// function name. If there is no entry for a given key then function did // function name. If there is no entry for a given key then function did
// not need to be remapped for the entry point and can be emitted directly. // not need to be remapped for the entry point and can be emitted directly.
std::unordered_map<std::string, Symbol> ep_func_name_remapped_; std::unordered_map<std::string, std::string> ep_func_name_remapped_;
}; };
} // namespace hlsl } // namespace hlsl

View File

@ -30,7 +30,15 @@ TEST_F(HlslGeneratorImplTest_Alias, EmitAlias_F32) {
auto* alias = ty.alias("a", ty.f32); auto* alias = ty.alias("a", ty.f32);
ASSERT_TRUE(gen.EmitConstructedType(out, alias)) << gen.error(); ASSERT_TRUE(gen.EmitConstructedType(out, alias)) << gen.error();
EXPECT_EQ(result(), R"(typedef float test_a; EXPECT_EQ(result(), R"(typedef float a;
)");
}
TEST_F(HlslGeneratorImplTest_Alias, EmitAlias_NameCollision) {
auto* alias = ty.alias("float", ty.f32);
ASSERT_TRUE(gen.EmitConstructedType(out, alias)) << gen.error();
EXPECT_EQ(result(), R"(typedef float float_tint_0;
)"); )");
} }
@ -44,9 +52,9 @@ TEST_F(HlslGeneratorImplTest_Alias, EmitAlias_Struct) {
auto* alias = ty.alias("B", s); auto* alias = ty.alias("B", s);
ASSERT_TRUE(gen.EmitConstructedType(out, alias)) << gen.error(); ASSERT_TRUE(gen.EmitConstructedType(out, alias)) << gen.error();
EXPECT_EQ(result(), R"(struct test_B { EXPECT_EQ(result(), R"(struct B {
float test_a; float a;
int test_b; int b;
}; };
)"); )");
} }

View File

@ -33,14 +33,14 @@ TEST_F(HlslGeneratorImplTest_Expression, EmitExpression_ArrayAccessor) {
auto* expr = IndexAccessor("ary", 5); auto* expr = IndexAccessor("ary", 5);
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "test_ary[5]"); EXPECT_EQ(result(), "ary[5]");
} }
TEST_F(HlslGeneratorImplTest_Expression, EmitArrayAccessor) { TEST_F(HlslGeneratorImplTest_Expression, EmitArrayAccessor) {
auto* expr = IndexAccessor("ary", "idx"); auto* expr = IndexAccessor("ary", "idx");
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "test_ary[test_idx]"); EXPECT_EQ(result(), "ary[idx]");
} }
} // namespace } // namespace

View File

@ -35,7 +35,7 @@ TEST_F(HlslGeneratorImplTest_Assign, Emit_Assign) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), " test_lhs = test_rhs;\n"); EXPECT_EQ(result(), " lhs = rhs;\n");
} }
} // namespace } // namespace

View File

@ -115,23 +115,22 @@ INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest, HlslGeneratorImplTest,
HlslBinaryTest, HlslBinaryTest,
testing::Values( testing::Values(
BinaryData{"(test_left & test_right)", ast::BinaryOp::kAnd}, BinaryData{"(left & right)", ast::BinaryOp::kAnd},
BinaryData{"(test_left | test_right)", ast::BinaryOp::kOr}, BinaryData{"(left | right)", ast::BinaryOp::kOr},
BinaryData{"(test_left ^ test_right)", ast::BinaryOp::kXor}, BinaryData{"(left ^ right)", ast::BinaryOp::kXor},
BinaryData{"(test_left == test_right)", ast::BinaryOp::kEqual}, BinaryData{"(left == right)", ast::BinaryOp::kEqual},
BinaryData{"(test_left != test_right)", ast::BinaryOp::kNotEqual}, BinaryData{"(left != right)", ast::BinaryOp::kNotEqual},
BinaryData{"(test_left < test_right)", ast::BinaryOp::kLessThan}, BinaryData{"(left < right)", ast::BinaryOp::kLessThan},
BinaryData{"(test_left > test_right)", ast::BinaryOp::kGreaterThan}, BinaryData{"(left > right)", ast::BinaryOp::kGreaterThan},
BinaryData{"(test_left <= test_right)", ast::BinaryOp::kLessThanEqual}, BinaryData{"(left <= right)", ast::BinaryOp::kLessThanEqual},
BinaryData{"(test_left >= test_right)", BinaryData{"(left >= right)", ast::BinaryOp::kGreaterThanEqual},
ast::BinaryOp::kGreaterThanEqual}, BinaryData{"(left << right)", ast::BinaryOp::kShiftLeft},
BinaryData{"(test_left << test_right)", ast::BinaryOp::kShiftLeft}, BinaryData{"(left >> right)", ast::BinaryOp::kShiftRight},
BinaryData{"(test_left >> test_right)", ast::BinaryOp::kShiftRight}, BinaryData{"(left + right)", ast::BinaryOp::kAdd},
BinaryData{"(test_left + test_right)", ast::BinaryOp::kAdd}, BinaryData{"(left - right)", ast::BinaryOp::kSubtract},
BinaryData{"(test_left - test_right)", ast::BinaryOp::kSubtract}, BinaryData{"(left * right)", ast::BinaryOp::kMultiply},
BinaryData{"(test_left * test_right)", ast::BinaryOp::kMultiply}, BinaryData{"(left / right)", ast::BinaryOp::kDivide},
BinaryData{"(test_left / test_right)", ast::BinaryOp::kDivide}, BinaryData{"(left % right)", ast::BinaryOp::kModulo}));
BinaryData{"(test_left % test_right)", ast::BinaryOp::kModulo}));
TEST_F(HlslGeneratorImplTest_Binary, Multiply_VectorScalar) { TEST_F(HlslGeneratorImplTest_Binary, Multiply_VectorScalar) {
auto* lhs = vec3<f32>(1.f, 1.f, 1.f); auto* lhs = vec3<f32>(1.f, 1.f, 1.f);
@ -173,7 +172,7 @@ TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixScalar) {
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "(test_mat * 1.0f)"); EXPECT_EQ(result(), "(mat * 1.0f)");
} }
TEST_F(HlslGeneratorImplTest_Binary, Multiply_ScalarMatrix) { TEST_F(HlslGeneratorImplTest_Binary, Multiply_ScalarMatrix) {
@ -188,7 +187,7 @@ TEST_F(HlslGeneratorImplTest_Binary, Multiply_ScalarMatrix) {
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "(1.0f * test_mat)"); EXPECT_EQ(result(), "(1.0f * mat)");
} }
TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixVector) { TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixVector) {
@ -203,7 +202,7 @@ TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixVector) {
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "mul(test_mat, float3(1.0f, 1.0f, 1.0f))"); EXPECT_EQ(result(), "mul(mat, float3(1.0f, 1.0f, 1.0f))");
} }
TEST_F(HlslGeneratorImplTest_Binary, Multiply_VectorMatrix) { TEST_F(HlslGeneratorImplTest_Binary, Multiply_VectorMatrix) {
@ -218,7 +217,7 @@ TEST_F(HlslGeneratorImplTest_Binary, Multiply_VectorMatrix) {
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "mul(float3(1.0f, 1.0f, 1.0f), test_mat)"); EXPECT_EQ(result(), "mul(float3(1.0f, 1.0f, 1.0f), mat)");
} }
TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixMatrix) { TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixMatrix) {
@ -233,7 +232,7 @@ TEST_F(HlslGeneratorImplTest_Binary, Multiply_MatrixMatrix) {
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); EXPECT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "mul(test_mat, test_mat)"); EXPECT_EQ(result(), "mul(mat, mat)");
} }
TEST_F(HlslGeneratorImplTest_Binary, Logical_And) { TEST_F(HlslGeneratorImplTest_Binary, Logical_And) {
@ -245,9 +244,9 @@ TEST_F(HlslGeneratorImplTest_Binary, Logical_And) {
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "(_tint_tmp)"); EXPECT_EQ(result(), "(_tint_tmp)");
EXPECT_EQ(pre_result(), R"(bool _tint_tmp = test_left; EXPECT_EQ(pre_result(), R"(bool _tint_tmp = left;
if (_tint_tmp) { if (_tint_tmp) {
_tint_tmp = test_right; _tint_tmp = right;
} }
)"); )");
} }
@ -266,15 +265,15 @@ TEST_F(HlslGeneratorImplTest_Binary, Logical_Multi) {
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "(_tint_tmp_0)"); EXPECT_EQ(result(), "(_tint_tmp_0)");
EXPECT_EQ(pre_result(), R"(bool _tint_tmp = test_a; EXPECT_EQ(pre_result(), R"(bool _tint_tmp = a;
if (_tint_tmp) { if (_tint_tmp) {
_tint_tmp = test_b; _tint_tmp = b;
} }
bool _tint_tmp_0 = (_tint_tmp); bool _tint_tmp_0 = (_tint_tmp);
if (!_tint_tmp_0) { if (!_tint_tmp_0) {
bool _tint_tmp_1 = test_c; bool _tint_tmp_1 = c;
if (!_tint_tmp_1) { if (!_tint_tmp_1) {
_tint_tmp_1 = test_d; _tint_tmp_1 = d;
} }
_tint_tmp_0 = (_tint_tmp_1); _tint_tmp_0 = (_tint_tmp_1);
} }
@ -290,9 +289,9 @@ TEST_F(HlslGeneratorImplTest_Binary, Logical_Or) {
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "(_tint_tmp)"); EXPECT_EQ(result(), "(_tint_tmp)");
EXPECT_EQ(pre_result(), R"(bool _tint_tmp = test_left; EXPECT_EQ(pre_result(), R"(bool _tint_tmp = left;
if (!_tint_tmp) { if (!_tint_tmp) {
_tint_tmp = test_right; _tint_tmp = right;
} }
)"); )");
} }
@ -333,16 +332,16 @@ TEST_F(HlslGeneratorImplTest_Binary, If_WithLogical) {
}); });
ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error();
EXPECT_EQ(result(), R"(bool _tint_tmp = test_a; EXPECT_EQ(result(), R"(bool _tint_tmp = a;
if (_tint_tmp) { if (_tint_tmp) {
_tint_tmp = test_b; _tint_tmp = b;
} }
if ((_tint_tmp)) { if ((_tint_tmp)) {
return 1; return 1;
} else { } else {
bool _tint_tmp_0 = test_b; bool _tint_tmp_0 = b;
if (!_tint_tmp_0) { if (!_tint_tmp_0) {
_tint_tmp_0 = test_c; _tint_tmp_0 = c;
} }
if ((_tint_tmp_0)) { if ((_tint_tmp_0)) {
return 2; return 2;
@ -364,13 +363,13 @@ TEST_F(HlslGeneratorImplTest_Binary, Return_WithLogical) {
create<ast::BinaryExpression>(ast::BinaryOp::kLogicalAnd, a, b), c)); create<ast::BinaryExpression>(ast::BinaryOp::kLogicalAnd, a, b), c));
ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error();
EXPECT_EQ(result(), R"(bool _tint_tmp = test_a; EXPECT_EQ(result(), R"(bool _tint_tmp = a;
if (_tint_tmp) { if (_tint_tmp) {
_tint_tmp = test_b; _tint_tmp = b;
} }
bool _tint_tmp_0 = (_tint_tmp); bool _tint_tmp_0 = (_tint_tmp);
if (!_tint_tmp_0) { if (!_tint_tmp_0) {
_tint_tmp_0 = test_c; _tint_tmp_0 = c;
} }
return (_tint_tmp_0); return (_tint_tmp_0);
)"); )");
@ -390,15 +389,15 @@ TEST_F(HlslGeneratorImplTest_Binary, Assign_WithLogical) {
create<ast::BinaryExpression>(ast::BinaryOp::kLogicalOr, b, c), d)); create<ast::BinaryExpression>(ast::BinaryOp::kLogicalOr, b, c), d));
ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error();
EXPECT_EQ(result(), R"(bool _tint_tmp = test_b; EXPECT_EQ(result(), R"(bool _tint_tmp = b;
if (!_tint_tmp) { if (!_tint_tmp) {
_tint_tmp = test_c; _tint_tmp = c;
} }
bool _tint_tmp_0 = (_tint_tmp); bool _tint_tmp_0 = (_tint_tmp);
if (_tint_tmp_0) { if (_tint_tmp_0) {
_tint_tmp_0 = test_d; _tint_tmp_0 = d;
} }
test_a = (_tint_tmp_0); a = (_tint_tmp_0);
)"); )");
} }
@ -419,15 +418,15 @@ TEST_F(HlslGeneratorImplTest_Binary, Decl_WithLogical) {
auto* expr = create<ast::VariableDeclStatement>(var); auto* expr = create<ast::VariableDeclStatement>(var);
ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error();
EXPECT_EQ(result(), R"(bool _tint_tmp = test_b; EXPECT_EQ(result(), R"(bool _tint_tmp = b;
if (_tint_tmp) { if (_tint_tmp) {
_tint_tmp = test_c; _tint_tmp = c;
} }
bool _tint_tmp_0 = (_tint_tmp); bool _tint_tmp_0 = (_tint_tmp);
if (!_tint_tmp_0) { if (!_tint_tmp_0) {
_tint_tmp_0 = test_d; _tint_tmp_0 = d;
} }
bool test_a = (_tint_tmp_0); bool a = (_tint_tmp_0);
)"); )");
} }
@ -445,11 +444,11 @@ TEST_F(HlslGeneratorImplTest_Binary, Bitcast_WithLogical) {
create<ast::BinaryExpression>(ast::BinaryOp::kLogicalOr, b, c))); create<ast::BinaryExpression>(ast::BinaryOp::kLogicalOr, b, c)));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(pre_result(), R"(bool _tint_tmp = test_a; EXPECT_EQ(pre_result(), R"(bool _tint_tmp = a;
if (_tint_tmp) { if (_tint_tmp) {
bool _tint_tmp_0 = test_b; bool _tint_tmp_0 = b;
if (!_tint_tmp_0) { if (!_tint_tmp_0) {
_tint_tmp_0 = test_c; _tint_tmp_0 = c;
} }
_tint_tmp = (_tint_tmp_0); _tint_tmp = (_tint_tmp_0);
} }
@ -479,27 +478,27 @@ TEST_F(HlslGeneratorImplTest_Binary, Call_WithLogical) {
auto* expr = create<ast::CallStatement>(Call("foo", params)); auto* expr = create<ast::CallStatement>(Call("foo", params));
ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, expr)) << gen.error();
EXPECT_EQ(result(), R"(bool _tint_tmp = test_a; EXPECT_EQ(result(), R"(bool _tint_tmp = a;
if (_tint_tmp) { if (_tint_tmp) {
_tint_tmp = test_b; _tint_tmp = b;
} }
bool _tint_tmp_0 = test_c; bool _tint_tmp_0 = c;
if (!_tint_tmp_0) { if (!_tint_tmp_0) {
_tint_tmp_0 = test_d; _tint_tmp_0 = d;
} }
bool _tint_tmp_1 = test_a; bool _tint_tmp_1 = a;
if (!_tint_tmp_1) { if (!_tint_tmp_1) {
_tint_tmp_1 = test_c; _tint_tmp_1 = c;
} }
bool _tint_tmp_2 = (_tint_tmp_1); bool _tint_tmp_2 = (_tint_tmp_1);
if (_tint_tmp_2) { if (_tint_tmp_2) {
bool _tint_tmp_3 = test_b; bool _tint_tmp_3 = b;
if (!_tint_tmp_3) { if (!_tint_tmp_3) {
_tint_tmp_3 = test_d; _tint_tmp_3 = d;
} }
_tint_tmp_2 = (_tint_tmp_3); _tint_tmp_2 = (_tint_tmp_3);
} }
test_foo((_tint_tmp), (_tint_tmp_0), (_tint_tmp_2)); foo((_tint_tmp), (_tint_tmp_0), (_tint_tmp_2));
)"); )");
} }

View File

@ -34,7 +34,7 @@ TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Float) {
auto* bitcast = create<ast::BitcastExpression>(ty.f32, id); auto* bitcast = create<ast::BitcastExpression>(ty.f32, id);
ASSERT_TRUE(gen.EmitExpression(pre, out, bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, bitcast)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_id)"); EXPECT_EQ(result(), "asfloat(id)");
} }
TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Int) { TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Int) {
@ -42,7 +42,7 @@ TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Int) {
auto* bitcast = create<ast::BitcastExpression>(ty.i32, id); auto* bitcast = create<ast::BitcastExpression>(ty.i32, id);
ASSERT_TRUE(gen.EmitExpression(pre, out, bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, bitcast)) << gen.error();
EXPECT_EQ(result(), "asint(test_id)"); EXPECT_EQ(result(), "asint(id)");
} }
TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Uint) { TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Uint) {
@ -50,7 +50,7 @@ TEST_F(HlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Uint) {
auto* bitcast = create<ast::BitcastExpression>(ty.u32, id); auto* bitcast = create<ast::BitcastExpression>(ty.u32, id);
ASSERT_TRUE(gen.EmitExpression(pre, out, bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, bitcast)) << gen.error();
EXPECT_EQ(result(), "asuint(test_id)"); EXPECT_EQ(result(), "asuint(id)");
} }
} // namespace } // namespace

View File

@ -37,7 +37,7 @@ TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithoutParams) {
mod->AddFunction(func); mod->AddFunction(func);
ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error();
EXPECT_EQ(result(), "test_my_func()"); EXPECT_EQ(result(), "my_func()");
} }
TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithParams) { TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithParams) {
@ -48,7 +48,7 @@ TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithParams) {
mod->AddFunction(func); mod->AddFunction(func);
ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error();
EXPECT_EQ(result(), "test_my_func(test_param1, test_param2)"); EXPECT_EQ(result(), "my_func(param1, param2)");
} }
TEST_F(HlslGeneratorImplTest_Call, EmitStatement_Call) { TEST_F(HlslGeneratorImplTest_Call, EmitStatement_Call) {
@ -59,7 +59,7 @@ TEST_F(HlslGeneratorImplTest_Call, EmitStatement_Call) {
mod->AddFunction(func); mod->AddFunction(func);
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, call)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, call)) << gen.error();
EXPECT_EQ(result(), " test_my_func(test_param1, test_param2);\n"); EXPECT_EQ(result(), " my_func(param1, param2);\n");
} }
} // namespace } // namespace

View File

@ -31,13 +31,13 @@ using HlslGeneratorImplTest_Cast = TestHelper;
TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Scalar) { TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Scalar) {
auto* cast = Construct<f32>("id"); auto* cast = Construct<f32>("id");
ASSERT_TRUE(gen.EmitExpression(pre, out, cast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, cast)) << gen.error();
EXPECT_EQ(result(), "float(test_id)"); EXPECT_EQ(result(), "float(id)");
} }
TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Vector) { TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Vector) {
auto* cast = vec3<f32>("id"); auto* cast = vec3<f32>("id");
ASSERT_TRUE(gen.EmitExpression(pre, out, cast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, cast)) << gen.error();
EXPECT_EQ(result(), "float3(test_id)"); EXPECT_EQ(result(), "float3(id)");
} }
} // namespace } // namespace

View File

@ -76,9 +76,9 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error();
EXPECT_EQ(result(), R"(struct test_vtx_main_in { EXPECT_EQ(result(), R"(struct vtx_main_in {
float test_foo : TEXCOORD0; float foo : TEXCOORD0;
int test_bar : TEXCOORD1; int bar : TEXCOORD1;
}; };
)"); )");
@ -126,9 +126,9 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error();
EXPECT_EQ(result(), R"(struct test_vtx_main_out { EXPECT_EQ(result(), R"(struct vtx_main_out {
float test_foo : TEXCOORD0; float foo : TEXCOORD0;
int test_bar : TEXCOORD1; int bar : TEXCOORD1;
}; };
)"); )");
@ -176,9 +176,9 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error();
EXPECT_EQ(result(), R"(struct test_main_in { EXPECT_EQ(result(), R"(struct main_in {
float test_foo : TEXCOORD0; float foo : TEXCOORD0;
int test_bar : TEXCOORD1; int bar : TEXCOORD1;
}; };
)"); )");
@ -226,9 +226,9 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error();
EXPECT_EQ(result(), R"(struct test_main_out { EXPECT_EQ(result(), R"(struct main_out {
float test_foo : SV_Target0; float foo : SV_Target0;
int test_bar : SV_Target1; int bar : SV_Target1;
}; };
)"); )");
@ -365,12 +365,12 @@ TEST_F(HlslGeneratorImplTest_EntryPoint,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(out, func, globals)) << gen.error();
EXPECT_EQ(result(), R"(struct test_main_in { EXPECT_EQ(result(), R"(struct main_in {
float4 test_coord : SV_Position; float4 coord : SV_Position;
}; };
struct test_main_out { struct main_out {
float test_depth : SV_Depth; float depth : SV_Depth;
}; };
)"); )");

View File

@ -63,7 +63,25 @@ TEST_F(HlslGeneratorImplTest_Function, Emit_Function) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"( void test_my_func() { EXPECT_EQ(result(), R"( void my_func() {
return;
}
)");
}
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_Name_Collision) {
auto* func = Func("GeometryShader", ast::VariableList{}, ty.void_,
ast::StatementList{
create<ast::ReturnStatement>(),
},
ast::FunctionDecorationList{});
mod->AddFunction(func);
gen.increment_indent();
ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"( void GeometryShader_tint_0() {
return; return;
} }
@ -85,7 +103,7 @@ TEST_F(HlslGeneratorImplTest_Function, Emit_Function_WithParams) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"( void test_my_func(float test_a, int test_b) { EXPECT_EQ(result(), R"( void my_func(float a, int b) {
return; return;
} }
@ -124,18 +142,18 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_frag_main_in { EXPECT_EQ(result(), R"(struct frag_main_in {
float test_foo : TEXCOORD0; float foo : TEXCOORD0;
}; };
struct test_frag_main_out { struct frag_main_out {
float test_bar : SV_Target1; float bar : SV_Target1;
}; };
test_frag_main_out test_frag_main(test_frag_main_in test_tint_in) { frag_main_out frag_main(frag_main_in tint_in) {
test_frag_main_out test_tint_out; frag_main_out tint_out;
test_tint_out.test_bar = test_tint_in.test_foo; tint_out.bar = tint_in.foo;
return test_tint_out; return tint_out;
} }
)"); )");
@ -176,18 +194,18 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_frag_main_in { EXPECT_EQ(result(), R"(struct frag_main_in {
float4 test_coord : SV_Position; float4 coord : SV_Position;
}; };
struct test_frag_main_out { struct frag_main_out {
float test_depth : SV_Depth; float depth : SV_Depth;
}; };
test_frag_main_out test_frag_main(test_frag_main_in test_tint_in) { frag_main_out frag_main(frag_main_in tint_in) {
test_frag_main_out test_tint_out; frag_main_out tint_out;
test_tint_out.test_depth = test_tint_in.test_coord.x; tint_out.depth = tint_in.coord.x;
return test_tint_out; return tint_out;
} }
)"); )");
@ -222,12 +240,12 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(cbuffer cbuffer_test_coord : register(b0) { EXPECT_EQ(result(), R"(cbuffer cbuffer_coord : register(b0) {
float4 test_coord; float4 coord;
}; };
void test_frag_main() { void frag_main() {
float test_v = test_coord.x; float v = coord.x;
return; return;
} }
@ -272,14 +290,14 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_Uniforms { EXPECT_EQ(result(), R"(struct Uniforms {
float4 test_coord; float4 coord;
}; };
ConstantBuffer<test_Uniforms> test_uniforms : register(b0); ConstantBuffer<Uniforms> uniforms : register(b0);
void test_frag_main() { void frag_main() {
float test_v = test_uniforms.test_coord.x; float v = uniforms.coord.x;
return; return;
} }
@ -323,10 +341,10 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(RWByteAddressBuffer test_coord : register(u0); EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
void test_frag_main() { void frag_main() {
float test_v = asfloat(test_coord.Load(4)); float v = asfloat(coord.Load(4));
return; return;
} }
@ -371,10 +389,10 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(ByteAddressBuffer test_coord : register(u0); EXPECT_EQ(result(), R"(ByteAddressBuffer coord : register(u0);
void test_frag_main() { void frag_main() {
float test_v = asfloat(test_coord.Load(4)); float v = asfloat(coord.Load(4));
return; return;
} }
@ -416,10 +434,10 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(RWByteAddressBuffer test_coord : register(u0); EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
void test_frag_main() { void frag_main() {
test_coord.Store(4, asuint(2.0f)); coord.Store(4, asuint(2.0f));
return; return;
} }
@ -480,25 +498,25 @@ TEST_F(
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_ep_1_in { EXPECT_EQ(result(), R"(struct ep_1_in {
float test_foo : TEXCOORD0; float foo : TEXCOORD0;
}; };
struct test_ep_1_out { struct ep_1_out {
float test_bar : SV_Target1; float bar : SV_Target1;
float test_val : SV_Target0; float val : SV_Target0;
}; };
float test_sub_func_ep_1(in test_ep_1_in test_tint_in, out test_ep_1_out test_tint_out, float test_param) { float sub_func_ep_1(in ep_1_in tint_in, out ep_1_out tint_out, float param) {
test_tint_out.test_bar = test_tint_in.test_foo; tint_out.bar = tint_in.foo;
test_tint_out.test_val = test_param; tint_out.val = param;
return test_tint_in.test_foo; return tint_in.foo;
} }
test_ep_1_out test_ep_1(test_ep_1_in test_tint_in) { ep_1_out ep_1(ep_1_in tint_in) {
test_ep_1_out test_tint_out; ep_1_out tint_out;
test_tint_out.test_bar = test_sub_func_ep_1(test_tint_in, test_tint_out, 1.0f); tint_out.bar = sub_func_ep_1(tint_in, tint_out, 1.0f);
return test_tint_out; return tint_out;
} }
)"); )");
@ -542,18 +560,18 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_ep_1_out { EXPECT_EQ(result(), R"(struct ep_1_out {
float test_depth : SV_Depth; float depth : SV_Depth;
}; };
float test_sub_func(float test_param) { float sub_func(float param) {
return test_param; return param;
} }
test_ep_1_out test_ep_1() { ep_1_out ep_1() {
test_ep_1_out test_tint_out; ep_1_out tint_out;
test_tint_out.test_depth = test_sub_func(1.0f); tint_out.depth = sub_func(1.0f);
return test_tint_out; return tint_out;
} }
)"); )");
@ -609,23 +627,23 @@ TEST_F(
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_ep_1_in { EXPECT_EQ(result(), R"(struct ep_1_in {
float4 test_coord : SV_Position; float4 coord : SV_Position;
}; };
struct test_ep_1_out { struct ep_1_out {
float test_depth : SV_Depth; float depth : SV_Depth;
}; };
float test_sub_func_ep_1(in test_ep_1_in test_tint_in, out test_ep_1_out test_tint_out, float test_param) { float sub_func_ep_1(in ep_1_in tint_in, out ep_1_out tint_out, float param) {
test_tint_out.test_depth = test_tint_in.test_coord.x; tint_out.depth = tint_in.coord.x;
return test_param; return param;
} }
test_ep_1_out test_ep_1(test_ep_1_in test_tint_in) { ep_1_out ep_1(ep_1_in tint_in) {
test_ep_1_out test_tint_out; ep_1_out tint_out;
test_tint_out.test_depth = test_sub_func_ep_1(test_tint_in, test_tint_out, 1.0f); tint_out.depth = sub_func_ep_1(tint_in, tint_out, 1.0f);
return test_tint_out; return tint_out;
} }
)"); )");
@ -672,16 +690,16 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(cbuffer cbuffer_test_coord : register(b0) { EXPECT_EQ(result(), R"(cbuffer cbuffer_coord : register(b0) {
float4 test_coord; float4 coord;
}; };
float test_sub_func(float test_param) { float sub_func(float param) {
return test_coord.x; return coord.x;
} }
void test_frag_main() { void frag_main() {
float test_v = test_sub_func(1.0f); float v = sub_func(1.0f);
return; return;
} }
@ -730,14 +748,14 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(RWByteAddressBuffer test_coord : register(u0); EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
float test_sub_func(float test_param) { float sub_func(float param) {
return asfloat(test_coord.Load((4 * 0))); return asfloat(coord.Load((4 * 0)));
} }
void test_frag_main() { void frag_main() {
float test_v = test_sub_func(1.0f); float v = sub_func(1.0f);
return; return;
} }
@ -775,17 +793,34 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_ep_1_out { EXPECT_EQ(result(), R"(struct ep_1_out {
float test_bar : SV_Target1; float bar : SV_Target1;
}; };
test_ep_1_out test_ep_1() { ep_1_out ep_1() {
test_ep_1_out test_tint_out; ep_1_out tint_out;
test_tint_out.test_bar = 1.0f; tint_out.bar = 1.0f;
if ((1 == 1)) { if ((1 == 1)) {
return test_tint_out; return tint_out;
} }
return test_tint_out; return tint_out;
}
)");
}
TEST_F(HlslGeneratorImplTest_Function,
Emit_FunctionDecoration_EntryPoint_WithNameCollision) {
auto* func = Func(
"GeometryShader", ast::VariableList{}, ty.void_, ast::StatementList{},
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kFragment),
});
mod->AddFunction(func);
ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(void GeometryShader_tint_0() {
} }
)"); )");
@ -807,7 +842,7 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"([numthreads(1, 1, 1)] EXPECT_EQ(result(), R"([numthreads(1, 1, 1)]
void test_main() { void main() {
return; return;
} }
@ -831,7 +866,7 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"([numthreads(2, 4, 6)] EXPECT_EQ(result(), R"([numthreads(2, 4, 6)]
void test_main() { void main() {
return; return;
} }
@ -852,7 +887,7 @@ TEST_F(HlslGeneratorImplTest_Function, Emit_Function_WithArrayParams) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"( void test_my_func(float test_a[5]) { EXPECT_EQ(result(), R"( void my_func(float a[5]) {
return; return;
} }
@ -930,21 +965,21 @@ TEST_F(HlslGeneratorImplTest_Function,
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(struct test_Data { EXPECT_EQ(result(), R"(struct Data {
float test_d; float d;
}; };
RWByteAddressBuffer test_data : register(u0); RWByteAddressBuffer data : register(u0);
[numthreads(1, 1, 1)] [numthreads(1, 1, 1)]
void test_a() { void a() {
float test_v = asfloat(test_data.Load(0)); float v = asfloat(data.Load(0));
return; return;
} }
[numthreads(1, 1, 1)] [numthreads(1, 1, 1)]
void test_b() { void b() {
float test_v = asfloat(test_data.Load(0)); float v = asfloat(data.Load(0));
return; return;
} }

View File

@ -26,7 +26,14 @@ using HlslGeneratorImplTest_Identifier = TestHelper;
TEST_F(HlslGeneratorImplTest_Identifier, EmitIdentifierExpression) { TEST_F(HlslGeneratorImplTest_Identifier, EmitIdentifierExpression) {
auto* i = Expr("foo"); auto* i = Expr("foo");
ASSERT_TRUE(gen.EmitExpression(pre, out, i)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, i)) << gen.error();
EXPECT_EQ(result(), "test_foo"); EXPECT_EQ(result(), "foo");
}
TEST_F(HlslGeneratorImplTest_Identifier,
EmitIdentifierExpression_Single_WithCollision) {
auto* i = Expr("virtual");
ASSERT_TRUE(gen.EmitExpression(pre, out, i)) << gen.error();
EXPECT_EQ(result(), "virtual_tint_0");
} }
} // namespace } // namespace

View File

@ -35,7 +35,7 @@ TEST_F(HlslGeneratorImplTest_If, Emit_If) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
EXPECT_EQ(result(), R"( if (test_cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} }
)"); )");
@ -58,10 +58,10 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
EXPECT_EQ(result(), R"( if (test_cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} else { } else {
if (test_else_cond) { if (else_cond) {
return; return;
} }
} }
@ -84,7 +84,7 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
EXPECT_EQ(result(), R"( if (test_cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} else { } else {
return; return;
@ -117,10 +117,10 @@ TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, i)) << gen.error();
EXPECT_EQ(result(), R"( if (test_cond) { EXPECT_EQ(result(), R"( if (cond) {
return; return;
} else { } else {
if (test_else_cond) { if (else_cond) {
return; return;
} else { } else {
return; return;

View File

@ -198,7 +198,7 @@ TEST_F(HlslGeneratorImplTest_Import, HlslImportData_Determinant) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitCall(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitCall(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), std::string("determinant(test_var)")); EXPECT_EQ(result(), std::string("determinant(var)"));
} }
} // namespace } // namespace

View File

@ -74,7 +74,7 @@ TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_OuterProduct) {
auto* a = Var("a", ast::StorageClass::kNone, ty.vec2<f32>()); auto* a = Var("a", ast::StorageClass::kNone, ty.vec2<f32>());
auto* b = Var("b", ast::StorageClass::kNone, ty.vec3<f32>()); auto* b = Var("b", ast::StorageClass::kNone, ty.vec3<f32>());
auto* call = Call("outerProduct", "a", "b"); auto* call = Call("outer_product", "a", "b");
td.RegisterVariableForTesting(a); td.RegisterVariableForTesting(a);
td.RegisterVariableForTesting(b); td.RegisterVariableForTesting(b);
@ -87,9 +87,7 @@ TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_OuterProduct) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error();
EXPECT_EQ( EXPECT_EQ(result(), " float3x2(a * b[0], a * b[1], a * b[2])");
result(),
" float3x2(test_a * test_b[0], test_a * test_b[1], test_a * test_b[2])");
} }
TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Bad_Name) { TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Bad_Name) {
@ -109,7 +107,7 @@ TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Call) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error();
EXPECT_EQ(result(), " dot(test_param1, test_param2)"); EXPECT_EQ(result(), " dot(param1, param2)");
} }
} // namespace } // namespace

View File

@ -22,7 +22,6 @@
#include "src/ast/type/sampled_texture_type.h" #include "src/ast/type/sampled_texture_type.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/generator_impl.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -49,7 +48,7 @@ ExpectedResult expected_texture_overload(
case ValidTextureOverload::kDimensionsStorageWO1dArray: case ValidTextureOverload::kDimensionsStorageWO1dArray:
return { return {
"int _tint_tmp;\n" "int _tint_tmp;\n"
"test_texture.GetDimensions(_tint_tmp);", "texture_tint_0.GetDimensions(_tint_tmp);",
"_tint_tmp", "_tint_tmp",
}; };
case ValidTextureOverload::kDimensions2d: case ValidTextureOverload::kDimensions2d:
@ -64,7 +63,7 @@ ExpectedResult expected_texture_overload(
case ValidTextureOverload::kDimensionsStorageWO2dArray: case ValidTextureOverload::kDimensionsStorageWO2dArray:
return { return {
"int2 _tint_tmp;\n" "int2 _tint_tmp;\n"
"test_texture.GetDimensions(_tint_tmp[0], _tint_tmp[1]);", "texture_tint_0.GetDimensions(_tint_tmp[0], _tint_tmp[1]);",
"_tint_tmp", "_tint_tmp",
}; };
case ValidTextureOverload::kDimensions3d: case ValidTextureOverload::kDimensions3d:
@ -76,7 +75,7 @@ ExpectedResult expected_texture_overload(
case ValidTextureOverload::kDimensionsStorageWO3d: case ValidTextureOverload::kDimensionsStorageWO3d:
return { return {
"int3 _tint_tmp;\n" "int3 _tint_tmp;\n"
"test_texture.GetDimensions(_tint_tmp[0], _tint_tmp[1], " "texture_tint_0.GetDimensions(_tint_tmp[0], _tint_tmp[1], "
"_tint_tmp[2]);", "_tint_tmp[2]);",
"_tint_tmp", "_tint_tmp",
}; };
@ -86,7 +85,7 @@ ExpectedResult expected_texture_overload(
case ValidTextureOverload::kDimensionsDepth2dArrayLevel: case ValidTextureOverload::kDimensionsDepth2dArrayLevel:
return { return {
"int2 _tint_tmp;\n" "int2 _tint_tmp;\n"
"test_texture.GetDimensions(1, _tint_tmp[0], _tint_tmp[1]);", "texture_tint_0.GetDimensions(1, _tint_tmp[0], _tint_tmp[1]);",
"_tint_tmp", "_tint_tmp",
}; };
case ValidTextureOverload::kDimensions3dLevel: case ValidTextureOverload::kDimensions3dLevel:
@ -96,186 +95,186 @@ ExpectedResult expected_texture_overload(
case ValidTextureOverload::kDimensionsDepthCubeArrayLevel: case ValidTextureOverload::kDimensionsDepthCubeArrayLevel:
return { return {
"int3 _tint_tmp;\n" "int3 _tint_tmp;\n"
"test_texture.GetDimensions(1, _tint_tmp[0], _tint_tmp[1], " "texture_tint_0.GetDimensions(1, _tint_tmp[0], _tint_tmp[1], "
"_tint_tmp[2]);", "_tint_tmp[2]);",
"_tint_tmp", "_tint_tmp",
}; };
case ValidTextureOverload::kSample1dF32: case ValidTextureOverload::kSample1dF32:
return R"(test_texture.Sample(test_sampler, 1.0f))"; return R"(texture_tint_0.Sample(sampler_tint_0, 1.0f))";
case ValidTextureOverload::kSample1dArrayF32: case ValidTextureOverload::kSample1dArrayF32:
return R"(test_texture.Sample(test_sampler, float2(1.0f, float(2))))"; return R"(texture_tint_0.Sample(sampler_tint_0, float2(1.0f, float(2))))";
case ValidTextureOverload::kSample2dF32: case ValidTextureOverload::kSample2dF32:
return R"(test_texture.Sample(test_sampler, float2(1.0f, 2.0f)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float2(1.0f, 2.0f)))";
case ValidTextureOverload::kSample2dOffsetF32: case ValidTextureOverload::kSample2dOffsetF32:
return R"(test_texture.Sample(test_sampler, float2(1.0f, 2.0f), int2(3, 4)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float2(1.0f, 2.0f), int2(3, 4)))";
case ValidTextureOverload::kSample2dArrayF32: case ValidTextureOverload::kSample2dArrayF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, float(3))))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, float(3))))";
case ValidTextureOverload::kSample2dArrayOffsetF32: case ValidTextureOverload::kSample2dArrayOffsetF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, float(3)), int2(4, 5)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, float(3)), int2(4, 5)))";
case ValidTextureOverload::kSample3dF32: case ValidTextureOverload::kSample3dF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, 3.0f)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSample3dOffsetF32: case ValidTextureOverload::kSample3dOffsetF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, 3.0f), int3(4, 5, 6)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), int3(4, 5, 6)))";
case ValidTextureOverload::kSampleCubeF32: case ValidTextureOverload::kSampleCubeF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, 3.0f)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSampleCubeArrayF32: case ValidTextureOverload::kSampleCubeArrayF32:
return R"(test_texture.Sample(test_sampler, float4(1.0f, 2.0f, 3.0f, float(4))))"; return R"(texture_tint_0.Sample(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(4))))";
case ValidTextureOverload::kSampleDepth2dF32: case ValidTextureOverload::kSampleDepth2dF32:
return R"(test_texture.Sample(test_sampler, float2(1.0f, 2.0f)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float2(1.0f, 2.0f)))";
case ValidTextureOverload::kSampleDepth2dOffsetF32: case ValidTextureOverload::kSampleDepth2dOffsetF32:
return R"(test_texture.Sample(test_sampler, float2(1.0f, 2.0f), int2(3, 4)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float2(1.0f, 2.0f), int2(3, 4)))";
case ValidTextureOverload::kSampleDepth2dArrayF32: case ValidTextureOverload::kSampleDepth2dArrayF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, float(3))))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, float(3))))";
case ValidTextureOverload::kSampleDepth2dArrayOffsetF32: case ValidTextureOverload::kSampleDepth2dArrayOffsetF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, float(3)), int2(4, 5)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, float(3)), int2(4, 5)))";
case ValidTextureOverload::kSampleDepthCubeF32: case ValidTextureOverload::kSampleDepthCubeF32:
return R"(test_texture.Sample(test_sampler, float3(1.0f, 2.0f, 3.0f)))"; return R"(texture_tint_0.Sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSampleDepthCubeArrayF32: case ValidTextureOverload::kSampleDepthCubeArrayF32:
return R"(test_texture.Sample(test_sampler, float4(1.0f, 2.0f, 3.0f, float(4))))"; return R"(texture_tint_0.Sample(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(4))))";
case ValidTextureOverload::kSampleBias2dF32: case ValidTextureOverload::kSampleBias2dF32:
return R"(test_texture.SampleBias(test_sampler, float2(1.0f, 2.0f), 3.0f))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float2(1.0f, 2.0f), 3.0f))";
case ValidTextureOverload::kSampleBias2dOffsetF32: case ValidTextureOverload::kSampleBias2dOffsetF32:
return R"(test_texture.SampleBias(test_sampler, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))";
case ValidTextureOverload::kSampleBias2dArrayF32: case ValidTextureOverload::kSampleBias2dArrayF32:
return R"(test_texture.SampleBias(test_sampler, float3(1.0f, 2.0f, float(4)), 3.0f))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float3(1.0f, 2.0f, float(4)), 3.0f))";
case ValidTextureOverload::kSampleBias2dArrayOffsetF32: case ValidTextureOverload::kSampleBias2dArrayOffsetF32:
return R"(test_texture.SampleBias(test_sampler, float3(1.0f, 2.0f, float(3)), 4.0f, int2(5, 6)))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float3(1.0f, 2.0f, float(3)), 4.0f, int2(5, 6)))";
case ValidTextureOverload::kSampleBias3dF32: case ValidTextureOverload::kSampleBias3dF32:
return R"(test_texture.SampleBias(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleBias3dOffsetF32: case ValidTextureOverload::kSampleBias3dOffsetF32:
return R"(test_texture.SampleBias(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f, int3(5, 6, 7)))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f, int3(5, 6, 7)))";
case ValidTextureOverload::kSampleBiasCubeF32: case ValidTextureOverload::kSampleBiasCubeF32:
return R"(test_texture.SampleBias(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleBiasCubeArrayF32: case ValidTextureOverload::kSampleBiasCubeArrayF32:
return R"(test_texture.SampleBias(test_sampler, float4(1.0f, 2.0f, 3.0f, float(3)), 4.0f))"; return R"(texture_tint_0.SampleBias(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(3)), 4.0f))";
case ValidTextureOverload::kSampleLevel2dF32: case ValidTextureOverload::kSampleLevel2dF32:
return R"(test_texture.SampleLevel(test_sampler, float2(1.0f, 2.0f), 3.0f))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float2(1.0f, 2.0f), 3.0f))";
case ValidTextureOverload::kSampleLevel2dOffsetF32: case ValidTextureOverload::kSampleLevel2dOffsetF32:
return R"(test_texture.SampleLevel(test_sampler, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))";
case ValidTextureOverload::kSampleLevel2dArrayF32: case ValidTextureOverload::kSampleLevel2dArrayF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, float(3)), 4.0f))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, float(3)), 4.0f))";
case ValidTextureOverload::kSampleLevel2dArrayOffsetF32: case ValidTextureOverload::kSampleLevel2dArrayOffsetF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, float(3)), 4.0f, int2(5, 6)))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, float(3)), 4.0f, int2(5, 6)))";
case ValidTextureOverload::kSampleLevel3dF32: case ValidTextureOverload::kSampleLevel3dF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleLevel3dOffsetF32: case ValidTextureOverload::kSampleLevel3dOffsetF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f, int3(5, 6, 7)))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f, int3(5, 6, 7)))";
case ValidTextureOverload::kSampleLevelCubeF32: case ValidTextureOverload::kSampleLevelCubeF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleLevelCubeArrayF32: case ValidTextureOverload::kSampleLevelCubeArrayF32:
return R"(test_texture.SampleLevel(test_sampler, float4(1.0f, 2.0f, 3.0f, float(4)), 5.0f))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(4)), 5.0f))";
case ValidTextureOverload::kSampleLevelDepth2dF32: case ValidTextureOverload::kSampleLevelDepth2dF32:
return R"(test_texture.SampleLevel(test_sampler, float2(1.0f, 2.0f), 3))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float2(1.0f, 2.0f), 3))";
case ValidTextureOverload::kSampleLevelDepth2dOffsetF32: case ValidTextureOverload::kSampleLevelDepth2dOffsetF32:
return R"(test_texture.SampleLevel(test_sampler, float2(1.0f, 2.0f), 3, int2(4, 5)))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float2(1.0f, 2.0f), 3, int2(4, 5)))";
case ValidTextureOverload::kSampleLevelDepth2dArrayF32: case ValidTextureOverload::kSampleLevelDepth2dArrayF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, float(3)), 4))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, float(3)), 4))";
case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32: case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, float(3)), 4, int2(5, 6)))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, float(3)), 4, int2(5, 6)))";
case ValidTextureOverload::kSampleLevelDepthCubeF32: case ValidTextureOverload::kSampleLevelDepthCubeF32:
return R"(test_texture.SampleLevel(test_sampler, float3(1.0f, 2.0f, 3.0f), 4))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4))";
case ValidTextureOverload::kSampleLevelDepthCubeArrayF32: case ValidTextureOverload::kSampleLevelDepthCubeArrayF32:
return R"(test_texture.SampleLevel(test_sampler, float4(1.0f, 2.0f, 3.0f, float(4)), 5))"; return R"(texture_tint_0.SampleLevel(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(4)), 5))";
case ValidTextureOverload::kSampleGrad2dF32: case ValidTextureOverload::kSampleGrad2dF32:
return R"(test_texture.SampleGrad(test_sampler, float2(1.0f, 2.0f), float2(3.0f, 4.0f), float2(5.0f, 6.0f)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float2(1.0f, 2.0f), float2(3.0f, 4.0f), float2(5.0f, 6.0f)))";
case ValidTextureOverload::kSampleGrad2dOffsetF32: case ValidTextureOverload::kSampleGrad2dOffsetF32:
return R"(test_texture.SampleGrad(test_sampler, float2(1.0f, 2.0f), float2(3.0f, 4.0f), float2(5.0f, 6.0f), int2(7, 8)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float2(1.0f, 2.0f), float2(3.0f, 4.0f), float2(5.0f, 6.0f), int2(7, 8)))";
case ValidTextureOverload::kSampleGrad2dArrayF32: case ValidTextureOverload::kSampleGrad2dArrayF32:
return R"(test_texture.SampleGrad(test_sampler, float3(1.0f, 2.0f, float(3)), float2(4.0f, 5.0f), float2(6.0f, 7.0f)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float3(1.0f, 2.0f, float(3)), float2(4.0f, 5.0f), float2(6.0f, 7.0f)))";
case ValidTextureOverload::kSampleGrad2dArrayOffsetF32: case ValidTextureOverload::kSampleGrad2dArrayOffsetF32:
return R"(test_texture.SampleGrad(test_sampler, float3(1.0f, 2.0f, float(3)), float2(4.0f, 5.0f), float2(6.0f, 7.0f), int2(8, 9)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float3(1.0f, 2.0f, float(3)), float2(4.0f, 5.0f), float2(6.0f, 7.0f), int2(8, 9)))";
case ValidTextureOverload::kSampleGrad3dF32: case ValidTextureOverload::kSampleGrad3dF32:
return R"(test_texture.SampleGrad(test_sampler, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)))";
case ValidTextureOverload::kSampleGrad3dOffsetF32: case ValidTextureOverload::kSampleGrad3dOffsetF32:
return R"(test_texture.SampleGrad(test_sampler, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f), int3(10, 11, 12)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f), int3(10, 11, 12)))";
case ValidTextureOverload::kSampleGradCubeF32: case ValidTextureOverload::kSampleGradCubeF32:
return R"(test_texture.SampleGrad(test_sampler, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)))";
case ValidTextureOverload::kSampleGradCubeArrayF32: case ValidTextureOverload::kSampleGradCubeArrayF32:
return R"(test_texture.SampleGrad(test_sampler, float4(1.0f, 2.0f, 3.0f, float(4)), float3(5.0f, 6.0f, 7.0f), float3(8.0f, 9.0f, 10.0f)))"; return R"(texture_tint_0.SampleGrad(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(4)), float3(5.0f, 6.0f, 7.0f), float3(8.0f, 9.0f, 10.0f)))";
case ValidTextureOverload::kSampleGradDepth2dF32: case ValidTextureOverload::kSampleGradDepth2dF32:
return R"(test_texture.SampleCmp(test_sampler, float2(1.0f, 2.0f), 3.0f))"; return R"(texture_tint_0.SampleCmp(sampler_tint_0, float2(1.0f, 2.0f), 3.0f))";
case ValidTextureOverload::kSampleGradDepth2dOffsetF32: case ValidTextureOverload::kSampleGradDepth2dOffsetF32:
return R"(test_texture.SampleCmp(test_sampler, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))"; return R"(texture_tint_0.SampleCmp(sampler_tint_0, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))";
case ValidTextureOverload::kSampleGradDepth2dArrayF32: case ValidTextureOverload::kSampleGradDepth2dArrayF32:
return R"(test_texture.SampleCmp(test_sampler, float3(1.0f, 2.0f, float(4)), 3.0f))"; return R"(texture_tint_0.SampleCmp(sampler_tint_0, float3(1.0f, 2.0f, float(4)), 3.0f))";
case ValidTextureOverload::kSampleGradDepth2dArrayOffsetF32: case ValidTextureOverload::kSampleGradDepth2dArrayOffsetF32:
return R"(test_texture.SampleCmp(test_sampler, float3(1.0f, 2.0f, float(4)), 3.0f, int2(5, 6)))"; return R"(texture_tint_0.SampleCmp(sampler_tint_0, float3(1.0f, 2.0f, float(4)), 3.0f, int2(5, 6)))";
case ValidTextureOverload::kSampleGradDepthCubeF32: case ValidTextureOverload::kSampleGradDepthCubeF32:
return R"(test_texture.SampleCmp(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))"; return R"(texture_tint_0.SampleCmp(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleGradDepthCubeArrayF32: case ValidTextureOverload::kSampleGradDepthCubeArrayF32:
return R"(test_texture.SampleCmp(test_sampler, float4(1.0f, 2.0f, 3.0f, float(4)), 5.0f))"; return R"(texture_tint_0.SampleCmp(sampler_tint_0, float4(1.0f, 2.0f, 3.0f, float(4)), 5.0f))";
case ValidTextureOverload::kLoad1dF32: case ValidTextureOverload::kLoad1dF32:
return R"(test_texture.Load(int2(1, 0)))"; return R"(texture_tint_0.Load(int2(1, 0)))";
case ValidTextureOverload::kLoad1dU32: case ValidTextureOverload::kLoad1dU32:
return R"(test_texture.Load(int2(1, 0)))"; return R"(texture_tint_0.Load(int2(1, 0)))";
case ValidTextureOverload::kLoad1dI32: case ValidTextureOverload::kLoad1dI32:
return R"(test_texture.Load(int2(1, 0)))"; return R"(texture_tint_0.Load(int2(1, 0)))";
case ValidTextureOverload::kLoad1dArrayF32: case ValidTextureOverload::kLoad1dArrayF32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoad1dArrayU32: case ValidTextureOverload::kLoad1dArrayU32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoad1dArrayI32: case ValidTextureOverload::kLoad1dArrayI32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoad2dF32: case ValidTextureOverload::kLoad2dF32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoad2dU32: case ValidTextureOverload::kLoad2dU32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoad2dI32: case ValidTextureOverload::kLoad2dI32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoad2dLevelF32: case ValidTextureOverload::kLoad2dLevelF32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoad2dLevelU32: case ValidTextureOverload::kLoad2dLevelU32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoad2dLevelI32: case ValidTextureOverload::kLoad2dLevelI32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoad2dArrayF32: case ValidTextureOverload::kLoad2dArrayF32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoad2dArrayU32: case ValidTextureOverload::kLoad2dArrayU32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoad2dArrayI32: case ValidTextureOverload::kLoad2dArrayI32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoad2dArrayLevelF32: case ValidTextureOverload::kLoad2dArrayLevelF32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoad2dArrayLevelU32: case ValidTextureOverload::kLoad2dArrayLevelU32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoad2dArrayLevelI32: case ValidTextureOverload::kLoad2dArrayLevelI32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoad3dF32: case ValidTextureOverload::kLoad3dF32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoad3dU32: case ValidTextureOverload::kLoad3dU32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoad3dI32: case ValidTextureOverload::kLoad3dI32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoad3dLevelF32: case ValidTextureOverload::kLoad3dLevelF32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoad3dLevelU32: case ValidTextureOverload::kLoad3dLevelU32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoad3dLevelI32: case ValidTextureOverload::kLoad3dLevelI32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoadMultisampled2dF32: case ValidTextureOverload::kLoadMultisampled2dF32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoadMultisampled2dU32: case ValidTextureOverload::kLoadMultisampled2dU32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoadMultisampled2dI32: case ValidTextureOverload::kLoadMultisampled2dI32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoadMultisampled2dArrayF32: case ValidTextureOverload::kLoadMultisampled2dArrayF32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoadMultisampled2dArrayU32: case ValidTextureOverload::kLoadMultisampled2dArrayU32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoadMultisampled2dArrayI32: case ValidTextureOverload::kLoadMultisampled2dArrayI32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoadDepth2dF32: case ValidTextureOverload::kLoadDepth2dF32:
return R"(test_texture.Load(int3(1, 2, 0)))"; return R"(texture_tint_0.Load(int3(1, 2, 0)))";
case ValidTextureOverload::kLoadDepth2dLevelF32: case ValidTextureOverload::kLoadDepth2dLevelF32:
return R"(test_texture.Load(int3(1, 2, 0), 3))"; return R"(texture_tint_0.Load(int3(1, 2, 0), 3))";
case ValidTextureOverload::kLoadDepth2dArrayF32: case ValidTextureOverload::kLoadDepth2dArrayF32:
return R"(test_texture.Load(int4(1, 2, 3, 0)))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0)))";
case ValidTextureOverload::kLoadDepth2dArrayLevelF32: case ValidTextureOverload::kLoadDepth2dArrayLevelF32:
return R"(test_texture.Load(int4(1, 2, 3, 0), 4))"; return R"(texture_tint_0.Load(int4(1, 2, 3, 0), 4))";
case ValidTextureOverload::kLoadStorageRO1dRgba32float: case ValidTextureOverload::kLoadStorageRO1dRgba32float:
return R"(test_texture.Load(1))"; return R"(texture_tint_0.Load(1))";
case ValidTextureOverload::kLoadStorageRO1dArrayRgba32float: case ValidTextureOverload::kLoadStorageRO1dArrayRgba32float:
return R"(test_texture.Load(int2(1, 2)))"; return R"(texture_tint_0.Load(int2(1, 2)))";
case ValidTextureOverload::kLoadStorageRO2dRgba8unorm: case ValidTextureOverload::kLoadStorageRO2dRgba8unorm:
case ValidTextureOverload::kLoadStorageRO2dRgba8snorm: case ValidTextureOverload::kLoadStorageRO2dRgba8snorm:
case ValidTextureOverload::kLoadStorageRO2dRgba8uint: case ValidTextureOverload::kLoadStorageRO2dRgba8uint:
@ -292,21 +291,21 @@ ExpectedResult expected_texture_overload(
case ValidTextureOverload::kLoadStorageRO2dRgba32uint: case ValidTextureOverload::kLoadStorageRO2dRgba32uint:
case ValidTextureOverload::kLoadStorageRO2dRgba32sint: case ValidTextureOverload::kLoadStorageRO2dRgba32sint:
case ValidTextureOverload::kLoadStorageRO2dRgba32float: case ValidTextureOverload::kLoadStorageRO2dRgba32float:
return R"(test_texture.Load(int2(1, 2)))"; return R"(texture_tint_0.Load(int2(1, 2)))";
case ValidTextureOverload::kLoadStorageRO2dArrayRgba32float: case ValidTextureOverload::kLoadStorageRO2dArrayRgba32float:
return R"(test_texture.Load(int3(1, 2, 3)))"; return R"(texture_tint_0.Load(int3(1, 2, 3)))";
case ValidTextureOverload::kLoadStorageRO3dRgba32float: case ValidTextureOverload::kLoadStorageRO3dRgba32float:
return R"(test_texture.Load(int3(1, 2, 3)))"; return R"(texture_tint_0.Load(int3(1, 2, 3)))";
case ValidTextureOverload::kStoreWO1dRgba32float: case ValidTextureOverload::kStoreWO1dRgba32float:
return R"(test_texture[1] = float4(2.0f, 3.0f, 4.0f, 5.0f))"; return R"(texture_tint_0[1] = float4(2.0f, 3.0f, 4.0f, 5.0f))";
case ValidTextureOverload::kStoreWO1dArrayRgba32float: case ValidTextureOverload::kStoreWO1dArrayRgba32float:
return R"(test_texture[int2(1, 2)] = float4(3.0f, 4.0f, 5.0f, 6.0f))"; return R"(texture_tint_0[int2(1, 2)] = float4(3.0f, 4.0f, 5.0f, 6.0f))";
case ValidTextureOverload::kStoreWO2dRgba32float: case ValidTextureOverload::kStoreWO2dRgba32float:
return R"(test_texture[int2(1, 2)] = float4(3.0f, 4.0f, 5.0f, 6.0f))"; return R"(texture_tint_0[int2(1, 2)] = float4(3.0f, 4.0f, 5.0f, 6.0f))";
case ValidTextureOverload::kStoreWO2dArrayRgba32float: case ValidTextureOverload::kStoreWO2dArrayRgba32float:
return R"(test_texture[int3(1, 2, 3)] = float4(4.0f, 5.0f, 6.0f, 7.0f))"; return R"(texture_tint_0[int3(1, 2, 3)] = float4(4.0f, 5.0f, 6.0f, 7.0f))";
case ValidTextureOverload::kStoreWO3dRgba32float: case ValidTextureOverload::kStoreWO3dRgba32float:
return R"(test_texture[int3(1, 2, 3)] = float4(4.0f, 5.0f, 6.0f, 7.0f))"; return R"(texture_tint_0[int3(1, 2, 3)] = float4(4.0f, 5.0f, 6.0f, 7.0f))";
} }
return "<unmatched texture overload>"; return "<unmatched texture overload>";
} // NOLINT - Ignore the length of this function } // NOLINT - Ignore the length of this function
@ -326,10 +325,8 @@ class HlslGeneratorIntrinsicTextureTest
/// The type determiner /// The type determiner
TypeDeterminer td{mod}; TypeDeterminer td{mod};
/// The namer
TestNamer namer{mod};
/// The generator /// The generator
GeneratorImpl gen{mod, &namer}; GeneratorImpl gen{mod};
/// The output stream /// The output stream
std::ostringstream out; std::ostringstream out;
/// The pre-output stream /// The pre-output stream

View File

@ -100,7 +100,7 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) {
bool tint_hlsl_is_first_1 = true; bool tint_hlsl_is_first_1 = true;
for(;;) { for(;;) {
if (!tint_hlsl_is_first_1) { if (!tint_hlsl_is_first_1) {
test_lhs = test_rhs; lhs = rhs;
} }
tint_hlsl_is_first_1 = false; tint_hlsl_is_first_1 = false;
@ -163,16 +163,16 @@ TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithVarUsedInContinuing) {
ASSERT_TRUE(gen.EmitStatement(out, outer)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, outer)) << gen.error();
EXPECT_EQ(result(), R"( { EXPECT_EQ(result(), R"( {
bool tint_hlsl_is_first_1 = true; bool tint_hlsl_is_first_1 = true;
float test_lhs; float lhs;
float test_other; float other;
for(;;) { for(;;) {
if (!tint_hlsl_is_first_1) { if (!tint_hlsl_is_first_1) {
test_lhs = test_rhs; lhs = rhs;
} }
tint_hlsl_is_first_1 = false; tint_hlsl_is_first_1 = false;
test_lhs = 2.400000095f; lhs = 2.400000095f;
test_other = 0.0f; other = 0.0f;
} }
} }
)"); )");

View File

@ -54,7 +54,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor, EmitExpression_MemberAccessor) {
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "test_str.test_mem"); EXPECT_EQ(result(), "str.mem");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -85,7 +85,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load(4))"); EXPECT_EQ(result(), "asfloat(data.Load(4))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -115,7 +115,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asint(test_data.Load(0))"); EXPECT_EQ(result(), "asint(data.Load(0))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_StorageBuffer_Store_Matrix) { EmitExpression_MemberAccessor_StorageBuffer_Store_Matrix) {
@ -156,9 +156,9 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)); ASSERT_TRUE(td.DetermineResultType(assign));
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), R"(float3x2 _tint_tmp = test_b; EXPECT_EQ(result(), R"(float3x2 _tint_tmp = b;
test_data.Store3(4 + 0, asuint(_tint_tmp[0])); data.Store3(4 + 0, asuint(_tint_tmp[0]));
test_data.Store3(4 + 16, asuint(_tint_tmp[1])); data.Store3(4 + 16, asuint(_tint_tmp[1]));
)"); )");
} }
@ -200,8 +200,8 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
EXPECT_EQ( EXPECT_EQ(
result(), result(),
R"(float3x2 _tint_tmp = float3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); R"(float3x2 _tint_tmp = float3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
test_data.Store3(4 + 0, asuint(_tint_tmp[0])); data.Store3(4 + 0, asuint(_tint_tmp[0]));
test_data.Store3(4 + 16, asuint(_tint_tmp[1])); data.Store3(4 + 16, asuint(_tint_tmp[1]));
)"); )");
} }
@ -236,8 +236,8 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(),
"asfloat(uint2x3(test_data.Load2(4 + 0), test_data.Load2(4 + 8), " "asfloat(uint2x3(data.Load2(4 + 0), data.Load2(4 + 8), "
"test_data.Load2(4 + 16)))"); "data.Load2(4 + 16)))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -274,9 +274,8 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ( EXPECT_EQ(result(),
result(), "asfloat(uint3x2(data.Load3(4 + 0), data.Load3(4 + 16)))");
"asfloat(uint3x2(test_data.Load3(4 + 0), test_data.Load3(4 + 16)))");
} }
TEST_F( TEST_F(
@ -308,8 +307,8 @@ TEST_F(
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(),
"asfloat(uint3x3(test_data.Load3(0 + 0), test_data.Load3(0 + 16), " "asfloat(uint3x3(data.Load3(0 + 0), data.Load3(0 + 16), "
"test_data.Load3(0 + 32)))"); "data.Load3(0 + 32)))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -341,7 +340,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load((4 * 1) + (16 * 2) + 16))"); EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + (16 * 2) + 16))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -373,7 +372,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asint(test_data.Load((4 * 2) + 0))"); EXPECT_EQ(result(), "asint(data.Load((4 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -406,7 +405,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asint(test_data.Load((4 * ((2 + 4) - 3)) + 0))"); EXPECT_EQ(result(), "asint(data.Load((4 * ((2 + 4) - 3)) + 0))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -440,7 +439,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)); ASSERT_TRUE(td.DetermineResultType(assign));
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), R"(test_data.Store(4, asuint(2.0f)); EXPECT_EQ(result(), R"(data.Store(4, asuint(2.0f));
)"); )");
} }
@ -478,7 +477,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)) << td.error(); ASSERT_TRUE(td.DetermineResultType(assign)) << td.error();
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), R"(test_data.Store((4 * 2) + 0, asuint(2)); EXPECT_EQ(result(), R"(data.Store((4 * 2) + 0, asuint(2));
)"); )");
} }
@ -513,7 +512,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)); ASSERT_TRUE(td.DetermineResultType(assign));
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), R"(test_data.Store(0, asuint(2)); EXPECT_EQ(result(), R"(data.Store(0, asuint(2));
)"); )");
} }
@ -546,7 +545,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load3(16))"); EXPECT_EQ(result(), "asfloat(data.Load3(16))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -582,7 +581,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)); ASSERT_TRUE(td.DetermineResultType(assign));
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(),
R"(test_data.Store3(16, asuint(float3(1.0f, 2.0f, 3.0f))); R"(data.Store3(16, asuint(float3(1.0f, 2.0f, 3.0f)));
)"); )");
} }
@ -632,7 +631,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load3(16 + (32 * 2) + 0))"); EXPECT_EQ(result(), "asfloat(data.Load3(16 + (32 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -680,7 +679,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load3(16 + (32 * 2) + 0)).xy"); EXPECT_EQ(result(), "asfloat(data.Load3(16 + (32 * 2) + 0)).xy");
} }
TEST_F( TEST_F(
@ -731,7 +730,7 @@ TEST_F(
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load((4 * 1) + 16 + (32 * 2) + 0))"); EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -781,7 +780,7 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(expr)); ASSERT_TRUE(td.DetermineResultType(expr));
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "asfloat(test_data.Load((4 * 1) + 16 + (32 * 2) + 0))"); EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor, TEST_F(HlslGeneratorImplTest_MemberAccessor,
@ -833,9 +832,8 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)); ASSERT_TRUE(td.DetermineResultType(assign));
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ( EXPECT_EQ(result(),
result(), R"(data.Store3(16 + (32 * 2) + 0, asuint(float3(1.0f, 2.0f, 3.0f)));
R"(test_data.Store3(16 + (32 * 2) + 0, asuint(float3(1.0f, 2.0f, 3.0f)));
)"); )");
} }
@ -890,34 +888,10 @@ TEST_F(HlslGeneratorImplTest_MemberAccessor,
ASSERT_TRUE(td.DetermineResultType(assign)); ASSERT_TRUE(td.DetermineResultType(assign));
ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, assign)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(),
R"(test_data.Store((4 * 1) + 16 + (32 * 2) + 0, asuint(1.0f)); R"(data.Store((4 * 1) + 16 + (32 * 2) + 0, asuint(1.0f));
)"); )");
} }
TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_Swizzle_xyz) {
auto* vec = Var("my_vec", ast::StorageClass::kPrivate, ty.vec4<f32>());
td.RegisterVariableForTesting(vec);
mod->AddGlobalVariable(vec);
auto* expr = MemberAccessor("my_vec", "xyz");
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "test_my_vec.xyz");
}
TEST_F(HlslGeneratorImplTest_MemberAccessor,
EmitExpression_MemberAccessor_Swizzle_gbr) {
auto* vec = Var("my_vec", ast::StorageClass::kPrivate, ty.vec4<f32>());
td.RegisterVariableForTesting(vec);
mod->AddGlobalVariable(vec);
auto* expr = MemberAccessor("my_vec", "gbr");
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(pre, out, expr)) << gen.error();
EXPECT_EQ(result(), "test_my_vec.gbr");
}
} // namespace } // namespace
} // namespace hlsl } // namespace hlsl
} // namespace writer } // namespace writer

View File

@ -38,7 +38,7 @@ TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_ModuleConstant) {
array<f32, 3>(1.f, 2.f, 3.f), ast::VariableDecorationList{}); array<f32, 3>(1.f, 2.f, 3.f), ast::VariableDecorationList{});
ASSERT_TRUE(gen.EmitProgramConstVariable(out, var)) << gen.error(); ASSERT_TRUE(gen.EmitProgramConstVariable(out, var)) << gen.error();
EXPECT_EQ(result(), "static const float test_pos[3] = {1.0f, 2.0f, 3.0f};\n"); EXPECT_EQ(result(), "static const float pos[3] = {1.0f, 2.0f, 3.0f};\n");
} }
TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant) { TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant) {
@ -51,7 +51,7 @@ TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant) {
EXPECT_EQ(result(), R"(#ifndef WGSL_SPEC_CONSTANT_23 EXPECT_EQ(result(), R"(#ifndef WGSL_SPEC_CONSTANT_23
#define WGSL_SPEC_CONSTANT_23 3.0f #define WGSL_SPEC_CONSTANT_23 3.0f
#endif #endif
static const float test_pos = WGSL_SPEC_CONSTANT_23; static const float pos = WGSL_SPEC_CONSTANT_23;
#undef WGSL_SPEC_CONSTANT_23 #undef WGSL_SPEC_CONSTANT_23
)"); )");
} }
@ -66,7 +66,7 @@ TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_SpecConstant_NoConstructor) {
EXPECT_EQ(result(), R"(#ifndef WGSL_SPEC_CONSTANT_23 EXPECT_EQ(result(), R"(#ifndef WGSL_SPEC_CONSTANT_23
#error spec constant required for constant id 23 #error spec constant required for constant id 23
#endif #endif
static const float test_pos = WGSL_SPEC_CONSTANT_23; static const float pos = WGSL_SPEC_CONSTANT_23;
#undef WGSL_SPEC_CONSTANT_23 #undef WGSL_SPEC_CONSTANT_23
)"); )");
} }

View File

@ -40,7 +40,7 @@ TEST_F(HlslGeneratorImplTest_Return, Emit_ReturnWithValue) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, r)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, r)) << gen.error();
EXPECT_EQ(result(), " return test_expr;\n"); EXPECT_EQ(result(), " return expr;\n");
} }
} // namespace } // namespace

View File

@ -54,7 +54,7 @@ TEST_F(HlslGeneratorImplTest_Switch, Emit_Switch) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, s)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, s)) << gen.error();
EXPECT_EQ(result(), R"( switch(test_cond) { EXPECT_EQ(result(), R"( switch(cond) {
case 5: { case 5: {
break; break;
} }

View File

@ -33,12 +33,31 @@ TEST_F(HlslGeneratorImplTest, Generate) {
mod->AddFunction(func); mod->AddFunction(func);
ASSERT_TRUE(gen.Generate(out)) << gen.error(); ASSERT_TRUE(gen.Generate(out)) << gen.error();
EXPECT_EQ(result(), R"(void test_my_func() { EXPECT_EQ(result(), R"(void my_func() {
} }
)"); )");
} }
TEST_F(HlslGeneratorImplTest, InputStructName) {
ASSERT_EQ(gen.generate_name("func_main_in"), "func_main_in");
}
TEST_F(HlslGeneratorImplTest, InputStructName_ConflictWithExisting) {
// Register the struct name as existing.
auto* namer = gen.namer_for_testing();
namer->NameFor("func_main_out");
ASSERT_EQ(gen.generate_name("func_main_out"), "func_main_out_0");
}
TEST_F(HlslGeneratorImplTest, NameConflictWith_InputStructName) {
ASSERT_EQ(gen.generate_name("func_main_in"), "func_main_in");
ASSERT_TRUE(gen.EmitIdentifier(pre, out, Expr("func_main_in")));
EXPECT_EQ(result(), "func_main_in_0");
}
struct HlslBuiltinData { struct HlslBuiltinData {
ast::Builtin builtin; ast::Builtin builtin;
const char* attribute_name; const char* attribute_name;

View File

@ -46,67 +46,80 @@ using HlslGeneratorImplTest_Type = TestHelper;
TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias) {
auto* alias = ty.alias("alias", ty.f32); auto* alias = ty.alias("alias", ty.f32);
ASSERT_TRUE(gen.EmitType(out, alias, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, alias, "")) << gen.error();
EXPECT_EQ(result(), "test_alias"); EXPECT_EQ(result(), "alias");
}
TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias_NameCollision) {
auto* alias = ty.alias("bool", ty.f32);
ASSERT_TRUE(gen.EmitType(out, alias, "")) << gen.error();
EXPECT_EQ(result(), "bool_tint_0");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Array) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Array) {
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(out, ty.array<bool, 4>(), "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(out, ty.array<bool, 4>(), sym)) << gen.error(); EXPECT_EQ(result(), "bool ary[4]");
EXPECT_EQ(result(), "bool test_ary[4]");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArray) { TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArray) {
auto* arr = ty.array(ty.array<bool, 4>(), 5); auto* arr = ty.array(ty.array<bool, 4>(), 5);
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(out, arr, "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(out, arr, sym)) << gen.error(); EXPECT_EQ(result(), "bool ary[5][4]");
EXPECT_EQ(result(), "bool test_ary[5][4]");
} }
// TODO(dsinclair): Is this possible? What order should it output in? // TODO(dsinclair): Is this possible? What order should it output in?
TEST_F(HlslGeneratorImplTest_Type, TEST_F(HlslGeneratorImplTest_Type,
DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) { DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) {
auto* arr = ty.array(ty.array(ty.array<bool, 4>(), 5), 0); auto* arr = ty.array(ty.array(ty.array<bool, 4>(), 5), 0);
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(out, arr, "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(out, arr, sym)) << gen.error(); EXPECT_EQ(result(), "bool ary[5][4][1]");
EXPECT_EQ(result(), "bool test_ary[5][4][1]");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArrayOfArray) { TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArrayOfArray) {
auto* arr = ty.array(ty.array(ty.array<bool, 4>(), 5), 6); auto* arr = ty.array(ty.array(ty.array<bool, 4>(), 5), 6);
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(out, arr, "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(out, arr, sym)) << gen.error(); EXPECT_EQ(result(), "bool ary[6][5][4]");
EXPECT_EQ(result(), "bool test_ary[6][5][4]"); }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_NameCollision) {
ASSERT_TRUE(gen.EmitType(out, ty.array<bool, 4>(), "bool")) << gen.error();
EXPECT_EQ(result(), "bool bool_tint_0[4]");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_WithoutName) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_WithoutName) {
ASSERT_TRUE(gen.EmitType(out, ty.array<bool, 4>(), Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.array<bool, 4>(), "")) << gen.error();
EXPECT_EQ(result(), "bool[4]"); EXPECT_EQ(result(), "bool[4]");
} }
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_RuntimeArray) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_RuntimeArray) {
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(out, ty.array<bool>(), "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(out, ty.array<bool>(), sym)) << gen.error();
EXPECT_EQ(result(), "bool ary[]"); EXPECT_EQ(result(), "bool ary[]");
} }
TEST_F(HlslGeneratorImplTest_Type,
DISABLED_EmitType_RuntimeArray_NameCollision) {
ASSERT_TRUE(gen.EmitType(out, ty.array<bool>(), "double")) << gen.error();
EXPECT_EQ(result(), "bool double_tint_0[]");
}
TEST_F(HlslGeneratorImplTest_Type, EmitType_Bool) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Bool) {
ASSERT_TRUE(gen.EmitType(out, ty.bool_, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.bool_, "")) << gen.error();
EXPECT_EQ(result(), "bool"); EXPECT_EQ(result(), "bool");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_F32) { TEST_F(HlslGeneratorImplTest_Type, EmitType_F32) {
ASSERT_TRUE(gen.EmitType(out, ty.f32, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.f32, "")) << gen.error();
EXPECT_EQ(result(), "float"); EXPECT_EQ(result(), "float");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_I32) { TEST_F(HlslGeneratorImplTest_Type, EmitType_I32) {
ASSERT_TRUE(gen.EmitType(out, ty.i32, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.i32, "")) << gen.error();
EXPECT_EQ(result(), "int"); EXPECT_EQ(result(), "int");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Matrix) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Matrix) {
ASSERT_TRUE(gen.EmitType(out, ty.mat2x3<f32>(), Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.mat2x3<f32>(), "")) << gen.error();
EXPECT_EQ(result(), "float3x2"); EXPECT_EQ(result(), "float3x2");
} }
@ -114,7 +127,7 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Matrix) {
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Pointer) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Pointer) {
ast::type::Pointer p(ty.f32, ast::StorageClass::kWorkgroup); ast::type::Pointer p(ty.f32, ast::StorageClass::kWorkgroup);
ASSERT_TRUE(gen.EmitType(out, &p, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &p, "")) << gen.error();
EXPECT_EQ(result(), "float*"); EXPECT_EQ(result(), "float*");
} }
@ -125,11 +138,10 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_StructDecl) {
ast::StructDecorationList{}); ast::StructDecorationList{});
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitStructType(out, s, mod->RegisterSymbol("S"))) ASSERT_TRUE(gen.EmitStructType(out, s, "S")) << gen.error();
<< gen.error(); EXPECT_EQ(result(), R"(struct S {
EXPECT_EQ(result(), R"(struct test_S { int a;
int test_a; float b;
float test_b;
}; };
)"); )");
} }
@ -141,8 +153,8 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct) {
ast::StructDecorationList{}); ast::StructDecorationList{});
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitType(out, s, mod->RegisterSymbol("S"))) << gen.error(); ASSERT_TRUE(gen.EmitType(out, s, "")) << gen.error();
EXPECT_EQ(result(), "test_S"); EXPECT_EQ(result(), "S");
} }
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_InjectPadding) { TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_InjectPadding) {
@ -153,14 +165,14 @@ TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_InjectPadding) {
ast::StructDecorationList{}); ast::StructDecorationList{});
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitType(out, s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, s, "")) << gen.error();
EXPECT_EQ(result(), R"(struct test_S { EXPECT_EQ(result(), R"(struct {
int8_t pad_0[4]; int8_t pad_0[4];
int test_a; int a;
int8_t pad_1[24]; int8_t pad_1[24];
float test_b; float b;
int8_t pad_2[92]; int8_t pad_2[92];
float test_c; float c;
})"); })");
} }
@ -170,11 +182,10 @@ TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct_NameCollision) {
ast::StructDecorationList{}); ast::StructDecorationList{});
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitStructType(out, s, mod->RegisterSymbol("S"))) ASSERT_TRUE(gen.EmitStructType(out, s, "S")) << gen.error();
<< gen.error(); EXPECT_EQ(result(), R"(struct S {
EXPECT_EQ(result(), R"(struct test_S { int double_tint_0;
int test_double; float float_tint_0;
float test_float;
}; };
)"); )");
} }
@ -190,40 +201,39 @@ TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_WithDecoration) {
decos); decos);
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitStructType(out, s, mod->RegisterSymbol("B"))) ASSERT_TRUE(gen.EmitStructType(out, s, "B")) << gen.error();
<< gen.error(); EXPECT_EQ(result(), R"(struct B {
EXPECT_EQ(result(), R"(struct test_B { int a;
int test_a; float b;
float test_b;
})"); })");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_U32) { TEST_F(HlslGeneratorImplTest_Type, EmitType_U32) {
ASSERT_TRUE(gen.EmitType(out, ty.u32, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.u32, "")) << gen.error();
EXPECT_EQ(result(), "uint"); EXPECT_EQ(result(), "uint");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Vector) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Vector) {
ASSERT_TRUE(gen.EmitType(out, ty.vec3<f32>(), Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.vec3<f32>(), "")) << gen.error();
EXPECT_EQ(result(), "float3"); EXPECT_EQ(result(), "float3");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) { TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) {
ASSERT_TRUE(gen.EmitType(out, ty.void_, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, ty.void_, "")) << gen.error();
EXPECT_EQ(result(), "void"); EXPECT_EQ(result(), "void");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitSampler) { TEST_F(HlslGeneratorImplTest_Type, EmitSampler) {
ast::type::Sampler sampler(ast::type::SamplerKind::kSampler); ast::type::Sampler sampler(ast::type::SamplerKind::kSampler);
ASSERT_TRUE(gen.EmitType(out, &sampler, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &sampler, "")) << gen.error();
EXPECT_EQ(result(), "SamplerState"); EXPECT_EQ(result(), "SamplerState");
} }
TEST_F(HlslGeneratorImplTest_Type, EmitSamplerComparison) { TEST_F(HlslGeneratorImplTest_Type, EmitSamplerComparison) {
ast::type::Sampler sampler(ast::type::SamplerKind::kComparisonSampler); ast::type::Sampler sampler(ast::type::SamplerKind::kComparisonSampler);
ASSERT_TRUE(gen.EmitType(out, &sampler, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &sampler, "")) << gen.error();
EXPECT_EQ(result(), "SamplerComparisonState"); EXPECT_EQ(result(), "SamplerComparisonState");
} }
@ -241,7 +251,7 @@ TEST_P(HlslDepthtexturesTest, Emit) {
ast::type::DepthTexture s(params.dim); ast::type::DepthTexture s(params.dim);
ASSERT_TRUE(gen.EmitType(out, &s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &s, "")) << gen.error();
EXPECT_EQ(result(), params.result); EXPECT_EQ(result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
@ -269,7 +279,7 @@ TEST_P(HlslSampledtexturesTest, Emit) {
ast::type::SampledTexture s(params.dim, ty.f32); ast::type::SampledTexture s(params.dim, ty.f32);
ASSERT_TRUE(gen.EmitType(out, &s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &s, "")) << gen.error();
EXPECT_EQ(result(), params.result); EXPECT_EQ(result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
@ -290,7 +300,7 @@ INSTANTIATE_TEST_SUITE_P(
TEST_F(HlslGeneratorImplTest_Type, EmitMultisampledTexture) { TEST_F(HlslGeneratorImplTest_Type, EmitMultisampledTexture) {
ast::type::MultisampledTexture s(ast::type::TextureDimension::k2d, ty.f32); ast::type::MultisampledTexture s(ast::type::TextureDimension::k2d, ty.f32);
ASSERT_TRUE(gen.EmitType(out, &s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &s, "")) << gen.error();
EXPECT_EQ(result(), "Texture2D"); EXPECT_EQ(result(), "Texture2D");
} }
@ -314,7 +324,7 @@ TEST_P(HlslStoragetexturesTest, Emit) {
: ast::AccessControl::kWriteOnly, : ast::AccessControl::kWriteOnly,
params.imgfmt); params.imgfmt);
ASSERT_TRUE(gen.EmitType(out, &s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(out, &s, "")) << gen.error();
EXPECT_EQ(result(), params.result); EXPECT_EQ(result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(

View File

@ -41,7 +41,7 @@ TEST_P(HlslUnaryOpTest, Emit) {
auto* op = create<ast::UnaryOpExpression>(params.op, expr); auto* op = create<ast::UnaryOpExpression>(params.op, expr);
ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error();
EXPECT_EQ(result(), std::string(params.name) + "(test_expr)"); EXPECT_EQ(result(), std::string(params.name) + "(expr)");
} }
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_UnaryOp, INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_UnaryOp,
HlslUnaryOpTest, HlslUnaryOpTest,

View File

@ -39,7 +39,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), " float test_a;\n"); EXPECT_EQ(result(), " float a;\n");
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
@ -49,7 +49,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), " const float test_a;\n"); EXPECT_EQ(result(), " const float a;\n");
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
@ -59,7 +59,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), " float test_a[5];\n"); EXPECT_EQ(result(), " float a[5];\n");
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, TEST_F(HlslGeneratorImplTest_VariableDecl,
@ -70,7 +70,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), " float test_a;\n"); EXPECT_EQ(result(), " float a;\n");
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) { TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
@ -80,7 +80,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), " float test_a;\n"); EXPECT_EQ(result(), " float a;\n");
} }
TEST_F(HlslGeneratorImplTest_VariableDecl, TEST_F(HlslGeneratorImplTest_VariableDecl,
@ -90,7 +90,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), R"(float test_a = test_initializer; EXPECT_EQ(result(), R"(float a = initializer;
)"); )");
} }
@ -101,7 +101,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), R"(float3 test_a = float3(0.0f); EXPECT_EQ(result(), R"(float3 a = float3(0.0f);
)"); )");
} }
@ -113,7 +113,7 @@ TEST_F(HlslGeneratorImplTest_VariableDecl,
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(out, stmt)) << gen.error();
EXPECT_EQ(result(), EXPECT_EQ(result(),
R"(float3x2 test_a = float3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f); R"(float3x2 a = float3x2(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
)"); )");
} }

696
src/writer/hlsl/namer.cc Normal file
View File

@ -0,0 +1,696 @@
// 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 "src/writer/hlsl/namer.h"
#include <algorithm>
namespace tint {
namespace writer {
namespace hlsl {
namespace {
// This list is used for a binary search and must be kept in sorted order.
const char* kNames[] = {"AddressU",
"AddressV",
"AddressW",
"AllMemoryBarrier",
"AllMemoryBarrierWithGroupSync",
"AppendStructuredBuffer",
"BINORMAL",
"BLENDINDICES",
"BLENDWEIGHT",
"BlendState",
"BorderColor",
"Buffer",
"ByteAddressBuffer",
"COLOR",
"CheckAccessFullyMapped",
"ComparisonFunc",
"CompileShader",
"ComputeShader",
"ConsumeStructuredBuffer",
"D3DCOLORtoUBYTE4",
"DEPTH",
"DepthStencilState",
"DepthStencilView",
"DeviceMemoryBarrier",
"DeviceMemroyBarrierWithGroupSync",
"DomainShader",
"EvaluateAttributeAtCentroid",
"EvaluateAttributeAtSample",
"EvaluateAttributeSnapped",
"FOG",
"Filter",
"GeometryShader",
"GetRenderTargetSampleCount",
"GetRenderTargetSamplePosition",
"GroupMemoryBarrier",
"GroupMemroyBarrierWithGroupSync",
"Hullshader",
"InputPatch",
"InterlockedAdd",
"InterlockedAnd",
"InterlockedCompareExchange",
"InterlockedCompareStore",
"InterlockedExchange",
"InterlockedMax",
"InterlockedMin",
"InterlockedOr",
"InterlockedXor",
"LineStream",
"MaxAnisotropy",
"MaxLOD",
"MinLOD",
"MipLODBias",
"NORMAL",
"NULL",
"Normal",
"OutputPatch",
"POSITION",
"POSITIONT",
"PSIZE",
"PixelShader",
"PointStream",
"Process2DQuadTessFactorsAvg",
"Process2DQuadTessFactorsMax",
"Process2DQuadTessFactorsMin",
"ProcessIsolineTessFactors",
"ProcessQuadTessFactorsAvg",
"ProcessQuadTessFactorsMax",
"ProcessQuadTessFactorsMin",
"ProcessTriTessFactorsAvg",
"ProcessTriTessFactorsMax",
"ProcessTriTessFactorsMin",
"RWBuffer",
"RWByteAddressBuffer",
"RWStructuredBuffer",
"RWTexture1D",
"RWTexture1DArray",
"RWTexture2D",
"RWTexture2DArray",
"RWTexture3D",
"RasterizerState",
"RenderTargetView",
"SV_ClipDistance",
"SV_Coverage",
"SV_CullDistance",
"SV_Depth",
"SV_DepthGreaterEqual",
"SV_DepthLessEqual",
"SV_DispatchThreadID",
"SV_DomainLocation",
"SV_GSInstanceID",
"SV_GroupID",
"SV_GroupIndex",
"SV_GroupThreadID",
"SV_InnerCoverage",
"SV_InsideTessFactor",
"SV_InstanceID",
"SV_IsFrontFace",
"SV_OutputControlPointID",
"SV_Position",
"SV_PrimitiveID",
"SV_RenderTargetArrayIndex",
"SV_SampleIndex",
"SV_StencilRef",
"SV_Target",
"SV_TessFactor",
"SV_VertexArrayIndex",
"SV_VertexID",
"Sampler",
"Sampler1D",
"Sampler2D",
"Sampler3D",
"SamplerCUBE",
"StructuredBuffer",
"TANGENT",
"TESSFACTOR",
"TEXCOORD",
"Texcoord",
"Texture",
"Texture1D",
"Texture1DArray",
"Texture2D",
"Texture2DArray",
"Texture2DMS",
"Texture2DMSArray",
"Texture3D",
"TextureCube",
"TextureCubeArray",
"TriangleStream",
"VFACE",
"VPOS",
"VertexShader",
"abort",
"abs",
"acos",
"all",
"allow_uav_condition",
"any",
"asdouble",
"asfloat",
"asin",
"asint",
"asm",
"asm_fragment",
"asuint",
"atan",
"atan2",
"auto",
"bool",
"bool1",
"bool1x1",
"bool1x2",
"bool1x3",
"bool1x4",
"bool2",
"bool2x1",
"bool2x2",
"bool2x3",
"bool2x4",
"bool3",
"bool3x1",
"bool3x2",
"bool3x3",
"bool3x4",
"bool4",
"bool4x1",
"bool4x2",
"bool4x3",
"bool4x4",
"branch",
"break",
"call",
"case",
"catch",
"cbuffer",
"ceil",
"centroid",
"char",
"clamp",
"class",
"clip",
"column_major",
"compile_fragment",
"const",
"const_cast",
"continue",
"cos",
"cosh",
"countbits",
"cross",
"ddx",
"ddx_coarse",
"ddx_fine",
"ddy",
"ddy_coarse",
"ddy_fine",
"degrees",
"delete",
"determinant",
"discard",
"distance",
"do",
"dot",
"double",
"double1",
"double1x1",
"double1x2",
"double1x3",
"double1x4",
"double2",
"double2x1",
"double2x2",
"double2x3",
"double2x4",
"double3",
"double3x1",
"double3x2",
"double3x3",
"double3x4",
"double4",
"double4x1",
"double4x2",
"double4x3",
"double4x4",
"dst",
"dword",
"dword1",
"dword1x1",
"dword1x2",
"dword1x3",
"dword1x4",
"dword2",
"dword2x1",
"dword2x2",
"dword2x3",
"dword2x4",
"dword3",
"dword3x1",
"dword3x2",
"dword3x3",
"dword3x4",
"dword4",
"dword4x1",
"dword4x2",
"dword4x3",
"dword4x4",
"dynamic_cast",
"else",
"enum",
"errorf",
"exp",
"exp2",
"explicit",
"export",
"extern",
"f16to32",
"f32tof16",
"faceforward",
"false",
"fastopt",
"firstbithigh",
"firstbitlow",
"flatten",
"float",
"float1",
"float1x1",
"float1x2",
"float1x3",
"float1x4",
"float2",
"float2x1",
"float2x2",
"float2x3",
"float2x4",
"float3",
"float3x1",
"float3x2",
"float3x3",
"float3x4",
"float4",
"float4x1",
"float4x2",
"float4x3",
"float4x4",
"floor",
"fma",
"fmod",
"for",
"forcecase",
"frac",
"frexp",
"friend",
"fwidth",
"fxgroup",
"goto",
"groupshared",
"half",
"half1",
"half1x1",
"half1x2",
"half1x3",
"half1x4",
"half2",
"half2x1",
"half2x2",
"half2x3",
"half2x4",
"half3",
"half3x1",
"half3x2",
"half3x3",
"half3x4",
"half4",
"half4x1",
"half4x2",
"half4x3",
"half4x4",
"if",
"in",
"inline",
"inout",
"int",
"int1",
"int1x1",
"int1x2",
"int1x3",
"int1x4",
"int2",
"int2x1",
"int2x2",
"int2x3",
"int2x4",
"int3",
"int3x1",
"int3x2",
"int3x3",
"int3x4",
"int4",
"int4x1",
"int4x2",
"int4x3",
"int4x4",
"interface",
"isfinite",
"isinf",
"isnan",
"ldexp",
"length",
"lerp",
"lineadj",
"linear",
"lit",
"log",
"log10",
"log2",
"long",
"loop",
"mad",
"matrix",
"max",
"min",
"min10float",
"min10float1",
"min10float1x1",
"min10float1x2",
"min10float1x3",
"min10float1x4",
"min10float2",
"min10float2x1",
"min10float2x2",
"min10float2x3",
"min10float2x4",
"min10float3",
"min10float3x1",
"min10float3x2",
"min10float3x3",
"min10float3x4",
"min10float4",
"min10float4x1",
"min10float4x2",
"min10float4x3",
"min10float4x4",
"min12int",
"min12int1",
"min12int1x1",
"min12int1x2",
"min12int1x3",
"min12int1x4",
"min12int2",
"min12int2x1",
"min12int2x2",
"min12int2x3",
"min12int2x4",
"min12int3",
"min12int3x1",
"min12int3x2",
"min12int3x3",
"min12int3x4",
"min12int4",
"min12int4x1",
"min12int4x2",
"min12int4x3",
"min12int4x4",
"min16float",
"min16float1",
"min16float1x1",
"min16float1x2",
"min16float1x3",
"min16float1x4",
"min16float2",
"min16float2x1",
"min16float2x2",
"min16float2x3",
"min16float2x4",
"min16float3",
"min16float3x1",
"min16float3x2",
"min16float3x3",
"min16float3x4",
"min16float4",
"min16float4x1",
"min16float4x2",
"min16float4x3",
"min16float4x4",
"min16int",
"min16int1",
"min16int1x1",
"min16int1x2",
"min16int1x3",
"min16int1x4",
"min16int2",
"min16int2x1",
"min16int2x2",
"min16int2x3",
"min16int2x4",
"min16int3",
"min16int3x1",
"min16int3x2",
"min16int3x3",
"min16int3x4",
"min16int4",
"min16int4x1",
"min16int4x2",
"min16int4x3",
"min16int4x4",
"min16uint",
"min16uint1",
"min16uint1x1",
"min16uint1x2",
"min16uint1x3",
"min16uint1x4",
"min16uint2",
"min16uint2x1",
"min16uint2x2",
"min16uint2x3",
"min16uint2x4",
"min16uint3",
"min16uint3x1",
"min16uint3x2",
"min16uint3x3",
"min16uint3x4",
"min16uint4",
"min16uint4x1",
"min16uint4x2",
"min16uint4x3",
"min16uint4x4",
"modf",
"msad4",
"mul",
"mutable",
"namespace",
"new",
"nointerpolation",
"noise",
"noperspective",
"normalize",
"numthreads",
"operator",
"out",
"packoffset",
"pass",
"pixelfragment",
"pixelshader",
"point",
"pow",
"precise",
"printf",
"private",
"protected",
"public",
"radians",
"rcp",
"reflect",
"refract",
"register",
"reinterpret_cast",
"return",
"reversebits",
"round",
"row_major",
"rsqrt",
"sample",
"sampler",
"sampler1D",
"sampler2D",
"sampler3D",
"samplerCUBE",
"sampler_state",
"saturate",
"shared",
"short",
"sign",
"signed",
"sin",
"sincos",
"sinh",
"sizeof",
"smoothstep",
"snorm",
"sqrt",
"stateblock",
"stateblock_state",
"static",
"static_cast",
"step",
"string",
"struct",
"switch",
"tan",
"tanh",
"tbuffer",
"technique",
"technique10",
"technique11",
"template",
"tex1D",
"tex1Dbias",
"tex1Dgrad",
"tex1Dlod",
"tex1Dproj",
"tex2D",
"tex2Dbias",
"tex2Dgrad",
"tex2Dlod",
"tex2Dproj",
"tex3D",
"tex3Dbias",
"tex3Dgrad",
"tex3Dlod",
"tex3Dproj",
"texCUBE",
"texCUBEbias",
"texCUBEgrad",
"texCUBElod",
"texCUBEproj",
"texture",
"texture1D",
"texture1DArray",
"texture2D",
"texture2DArray",
"texture2DMS",
"texture2DMSArray",
"texture3D",
"textureCube",
"textureCubeArray",
"this",
"throw",
"transpose",
"triangle",
"triangleadj",
"true",
"trunc",
"try",
"typedef",
"typename",
"uint",
"uint1",
"uint1x1",
"uint1x2",
"uint1x3",
"uint1x4",
"uint2",
"uint2x1",
"uint2x2",
"uint2x3",
"uint2x4",
"uint3",
"uint3x1",
"uint3x2",
"uint3x3",
"uint3x4",
"uint4",
"uint4x1",
"uint4x2",
"uint4x3",
"uint4x4",
"uniform",
"union",
"unorm",
"unroll",
"unsigned",
"using",
"vector",
"vertexfragment",
"vertexshader",
"virtual",
"void",
"volatile",
"while"};
} // namespace
Namer::Namer() = default;
Namer::~Namer() = default;
std::string Namer::NameFor(const std::string& name) {
// If it's in the name map we can just return it. There are no shadow names
// in WGSL so this has to be unique in the WGSL names, and we've already
// checked the name collisions with HLSL.
auto it = name_map_.find(name);
if (it != name_map_.end()) {
return it->second;
}
std::string ret_name = name;
if (std::binary_search(std::begin(kNames), std::end(kNames), ret_name)) {
uint32_t i = 0;
// Make sure there wasn't already a tint variable with the new name we've
// now created.
while (true) {
ret_name = name + "_tint_" + std::to_string(i);
it = name_map_.find(ret_name);
if (it == name_map_.end()) {
break;
}
i++;
}
RegisterRemappedName(ret_name);
} else {
uint32_t i = 0;
// Make sure the ident name wasn't assigned by a remapping.
while (true) {
if (!IsRemapped(ret_name)) {
break;
}
ret_name = name + "_" + std::to_string(i);
i++;
}
RegisterRemappedName(ret_name);
}
name_map_[name] = ret_name;
return ret_name;
}
bool Namer::IsMapped(const std::string& name) {
auto it = name_map_.find(name);
return it != name_map_.end();
}
bool Namer::IsRemapped(const std::string& name) {
auto it = remapped_names_.find(name);
return it != remapped_names_.end();
}
void Namer::RegisterRemappedName(const std::string& name) {
remapped_names_.insert(name);
}
} // namespace hlsl
} // namespace writer
} // namespace tint

63
src/writer/hlsl/namer.h Normal file
View File

@ -0,0 +1,63 @@
// 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.
#ifndef SRC_WRITER_HLSL_NAMER_H_
#define SRC_WRITER_HLSL_NAMER_H_
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace tint {
namespace writer {
namespace hlsl {
/// Remaps maps names to avoid reserved words and collisions for HLSL.
class Namer {
public:
/// Constructor
Namer();
~Namer();
/// Returns a sanitized version of `name`
/// @param name the name to sanitize
/// @returns the sanitized version of `name`
std::string NameFor(const std::string& name);
/// Registers a remapped name.
/// @param name the name to register
void RegisterRemappedName(const std::string& name);
/// Returns if the given name has been mapped already
/// @param name the name to check
/// @returns true if the name has been mapped
bool IsMapped(const std::string& name);
/// Returns if the given name has been remapped already
/// @param name the name to check
/// @returns true if the name has been remapped
bool IsRemapped(const std::string& name);
private:
/// Map of original name to new name. The two names may be the same.
std::unordered_map<std::string, std::string> name_map_;
// The list of names taken by the remapper
std::unordered_set<std::string> remapped_names_;
};
} // namespace hlsl
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_HLSL_NAMER_H_

View File

@ -0,0 +1,667 @@
// 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 "src/writer/hlsl/namer.h"
#include "gtest/gtest.h"
namespace tint {
namespace writer {
namespace hlsl {
namespace {
using HlslNamerTest = testing::Test;
TEST_F(HlslNamerTest, ReturnsName) {
Namer n;
EXPECT_EQ("my_name", n.NameFor("my_name"));
EXPECT_EQ("my_name", n.NameFor("my_name"));
}
TEST_F(HlslNamerTest, HandlesConflictWithRenamedReservedWordAfterIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_1", n.NameFor("float"));
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
}
TEST_F(HlslNamerTest, HandlesConflictWithRenamedReservedWordBeforeIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_0_0_0", n.NameFor("float_tint_0_0"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
}
using HlslReservedNameTest = testing::TestWithParam<std::string>;
TEST_P(HlslReservedNameTest, Emit) {
auto name = GetParam();
Namer n;
EXPECT_EQ(name + "_tint_0", n.NameFor(name));
}
INSTANTIATE_TEST_SUITE_P(HlslNamerTest,
HlslReservedNameTest,
testing::Values("AddressU",
"AddressV",
"AddressW",
"AllMemoryBarrier",
"AllMemoryBarrierWithGroupSync",
"AppendStructuredBuffer",
"BINORMAL",
"BLENDINDICES",
"BLENDWEIGHT",
"BlendState",
"BorderColor",
"Buffer",
"ByteAddressBuffer",
"COLOR",
"CheckAccessFullyMapped",
"ComparisonFunc",
"CompileShader",
"ComputeShader",
"ConsumeStructuredBuffer",
"D3DCOLORtoUBYTE4",
"DEPTH",
"DepthStencilState",
"DepthStencilView",
"DeviceMemoryBarrier",
"DeviceMemroyBarrierWithGroupSync",
"DomainShader",
"EvaluateAttributeAtCentroid",
"EvaluateAttributeAtSample",
"EvaluateAttributeSnapped",
"FOG",
"Filter",
"GeometryShader",
"GetRenderTargetSampleCount",
"GetRenderTargetSamplePosition",
"GroupMemoryBarrier",
"GroupMemroyBarrierWithGroupSync",
"Hullshader",
"InputPatch",
"InterlockedAdd",
"InterlockedAnd",
"InterlockedCompareExchange",
"InterlockedCompareStore",
"InterlockedExchange",
"InterlockedMax",
"InterlockedMin",
"InterlockedOr",
"InterlockedXor",
"LineStream",
"MaxAnisotropy",
"MaxLOD",
"MinLOD",
"MipLODBias",
"NORMAL",
"NULL",
"Normal",
"OutputPatch",
"POSITION",
"POSITIONT",
"PSIZE",
"PixelShader",
"PointStream",
"Process2DQuadTessFactorsAvg",
"Process2DQuadTessFactorsMax",
"Process2DQuadTessFactorsMin",
"ProcessIsolineTessFactors",
"ProcessQuadTessFactorsAvg",
"ProcessQuadTessFactorsMax",
"ProcessQuadTessFactorsMin",
"ProcessTriTessFactorsAvg",
"ProcessTriTessFactorsMax",
"ProcessTriTessFactorsMin",
"RWBuffer",
"RWByteAddressBuffer",
"RWStructuredBuffer",
"RWTexture1D",
"RWTexture1DArray",
"RWTexture2D",
"RWTexture2DArray",
"RWTexture3D",
"RasterizerState",
"RenderTargetView",
"SV_ClipDistance",
"SV_Coverage",
"SV_CullDistance",
"SV_Depth",
"SV_DepthGreaterEqual",
"SV_DepthLessEqual",
"SV_DispatchThreadID",
"SV_DomainLocation",
"SV_GSInstanceID",
"SV_GroupID",
"SV_GroupIndex",
"SV_GroupThreadID",
"SV_InnerCoverage",
"SV_InsideTessFactor",
"SV_InstanceID",
"SV_IsFrontFace",
"SV_OutputControlPointID",
"SV_Position",
"SV_PrimitiveID",
"SV_RenderTargetArrayIndex",
"SV_SampleIndex",
"SV_StencilRef",
"SV_Target",
"SV_TessFactor",
"SV_VertexArrayIndex",
"SV_VertexID",
"Sampler",
"Sampler1D",
"Sampler2D",
"Sampler3D",
"SamplerCUBE",
"StructuredBuffer",
"TANGENT",
"TESSFACTOR",
"TEXCOORD",
"Texcoord",
"Texture",
"Texture1D",
"Texture1DArray",
"Texture2D",
"Texture2DArray",
"Texture2DMS",
"Texture2DMSArray",
"Texture3D",
"TextureCube",
"TextureCubeArray",
"TriangleStream",
"VFACE",
"VPOS",
"VertexShader",
"abort",
"abs",
"acos",
"all",
"allow_uav_condition",
"any",
"asdouble",
"asfloat",
"asin",
"asint",
"asm",
"asm_fragment",
"asuint",
"atan",
"atan2",
"auto",
"bool",
"bool1",
"bool1x1",
"bool1x2",
"bool1x3",
"bool1x4",
"bool2",
"bool2x1",
"bool2x2",
"bool2x3",
"bool2x4",
"bool3",
"bool3x1",
"bool3x2",
"bool3x3",
"bool3x4",
"bool4",
"bool4x1",
"bool4x2",
"bool4x3",
"bool4x4",
"branch",
"break",
"call",
"case",
"catch",
"cbuffer",
"ceil",
"centroid",
"char",
"clamp",
"class",
"clip",
"column_major",
"compile_fragment",
"const",
"const_cast",
"continue",
"cos",
"cosh",
"countbits",
"cross",
"ddx",
"ddx_coarse",
"ddx_fine",
"ddy",
"ddy_coarse",
"ddy_fine",
"degrees",
"delete",
"determinant",
"discard",
"distance",
"do",
"dot",
"double",
"double1",
"double1x1",
"double1x2",
"double1x3",
"double1x4",
"double2",
"double2x1",
"double2x2",
"double2x3",
"double2x4",
"double3",
"double3x1",
"double3x2",
"double3x3",
"double3x4",
"double4",
"double4x1",
"double4x2",
"double4x3",
"double4x4",
"dst",
"dword",
"dword1",
"dword1x1",
"dword1x2",
"dword1x3",
"dword1x4",
"dword2",
"dword2x1",
"dword2x2",
"dword2x3",
"dword2x4",
"dword3",
"dword3x1",
"dword3x2",
"dword3x3",
"dword3x4",
"dword4",
"dword4x1",
"dword4x2",
"dword4x3",
"dword4x4",
"dynamic_cast",
"else",
"enum",
"errorf",
"exp",
"exp2",
"explicit",
"export",
"extern",
"f16to32",
"f32tof16",
"faceforward",
"false",
"fastopt",
"firstbithigh",
"firstbitlow",
"flatten",
"float",
"float1",
"float1x1",
"float1x2",
"float1x3",
"float1x4",
"float2",
"float2x1",
"float2x2",
"float2x3",
"float2x4",
"float3",
"float3x1",
"float3x2",
"float3x3",
"float3x4",
"float4",
"float4x1",
"float4x2",
"float4x3",
"float4x4",
"floor",
"fma",
"fmod",
"for",
"forcecase",
"frac",
"frexp",
"friend",
"fwidth",
"fxgroup",
"goto",
"groupshared",
"half",
"half1",
"half1x1",
"half1x2",
"half1x3",
"half1x4",
"half2",
"half2x1",
"half2x2",
"half2x3",
"half2x4",
"half3",
"half3x1",
"half3x2",
"half3x3",
"half3x4",
"half4",
"half4x1",
"half4x2",
"half4x3",
"half4x4",
"if",
"in",
"inline",
"inout",
"int",
"int1",
"int1x1",
"int1x2",
"int1x3",
"int1x4",
"int2",
"int2x1",
"int2x2",
"int2x3",
"int2x4",
"int3",
"int3x1",
"int3x2",
"int3x3",
"int3x4",
"int4",
"int4x1",
"int4x2",
"int4x3",
"int4x4",
"interface",
"isfinite",
"isinf",
"isnan",
"ldexp",
"length",
"lerp",
"lineadj",
"linear",
"lit",
"log",
"log10",
"log2",
"long",
"loop",
"mad",
"matrix",
"max",
"min",
"min10float",
"min10float1",
"min10float1x1",
"min10float1x2",
"min10float1x3",
"min10float1x4",
"min10float2",
"min10float2x1",
"min10float2x2",
"min10float2x3",
"min10float2x4",
"min10float3",
"min10float3x1",
"min10float3x2",
"min10float3x3",
"min10float3x4",
"min10float4",
"min10float4x1",
"min10float4x2",
"min10float4x3",
"min10float4x4",
"min12int",
"min12int1",
"min12int1x1",
"min12int1x2",
"min12int1x3",
"min12int1x4",
"min12int2",
"min12int2x1",
"min12int2x2",
"min12int2x3",
"min12int2x4",
"min12int3",
"min12int3x1",
"min12int3x2",
"min12int3x3",
"min12int3x4",
"min12int4",
"min12int4x1",
"min12int4x2",
"min12int4x3",
"min12int4x4",
"min16float",
"min16float1",
"min16float1x1",
"min16float1x2",
"min16float1x3",
"min16float1x4",
"min16float2",
"min16float2x1",
"min16float2x2",
"min16float2x3",
"min16float2x4",
"min16float3",
"min16float3x1",
"min16float3x2",
"min16float3x3",
"min16float3x4",
"min16float4",
"min16float4x1",
"min16float4x2",
"min16float4x3",
"min16float4x4",
"min16int",
"min16int1",
"min16int1x1",
"min16int1x2",
"min16int1x3",
"min16int1x4",
"min16int2",
"min16int2x1",
"min16int2x2",
"min16int2x3",
"min16int2x4",
"min16int3",
"min16int3x1",
"min16int3x2",
"min16int3x3",
"min16int3x4",
"min16int4",
"min16int4x1",
"min16int4x2",
"min16int4x3",
"min16int4x4",
"min16uint",
"min16uint1",
"min16uint1x1",
"min16uint1x2",
"min16uint1x3",
"min16uint1x4",
"min16uint2",
"min16uint2x1",
"min16uint2x2",
"min16uint2x3",
"min16uint2x4",
"min16uint3",
"min16uint3x1",
"min16uint3x2",
"min16uint3x3",
"min16uint3x4",
"min16uint4",
"min16uint4x1",
"min16uint4x2",
"min16uint4x3",
"min16uint4x4",
"modf",
"msad4",
"mul",
"mutable",
"namespace",
"new",
"nointerpolation",
"noise",
"noperspective",
"normalize",
"numthreads",
"operator",
"out",
"packoffset",
"pass",
"pixelfragment",
"pixelshader",
"point",
"pow",
"precise",
"printf",
"private",
"protected",
"public",
"radians",
"rcp",
"reflect",
"refract",
"register",
"reinterpret_cast",
"return",
"reversebits",
"round",
"row_major",
"rsqrt",
"sample",
"sampler1D",
"sampler2D",
"sampler3D",
"samplerCUBE",
"sampler_state",
"saturate",
"shared",
"short",
"sign",
"signed",
"sin",
"sincos",
"sinh",
"sizeof",
"smoothstep",
"snorm",
"sqrt",
"stateblock",
"stateblock_state",
"static",
"static_cast",
"step",
"string",
"struct",
"switch",
"tan",
"tanh",
"tbuffer",
"technique",
"technique10",
"technique11",
"template",
"tex1D",
"tex1Dbias",
"tex1Dgrad",
"tex1Dlod",
"tex1Dproj",
"tex2D",
"tex2Dbias",
"tex2Dgrad",
"tex2Dlod",
"tex2Dproj",
"tex3D",
"tex3Dbias",
"tex3Dgrad",
"tex3Dlod",
"tex3Dproj",
"texCUBE",
"texCUBEbias",
"texCUBEgrad",
"texCUBElod",
"texCUBEproj",
"texture",
"texture1D",
"texture1DArray",
"texture2D",
"texture2DArray",
"texture2DMS",
"texture2DMSArray",
"texture3D",
"textureCube",
"textureCubeArray",
"this",
"throw",
"transpose",
"triangle",
"triangleadj",
"true",
"trunc",
"try",
"typedef",
"typename",
"uint",
"uint1",
"uint1x1",
"uint1x2",
"uint1x3",
"uint1x4",
"uint2",
"uint2x1",
"uint2x2",
"uint2x3",
"uint2x4",
"uint3",
"uint3x1",
"uint3x2",
"uint3x3",
"uint3x4",
"uint4",
"uint4x1",
"uint4x2",
"uint4x3",
"uint4x4",
"uniform",
"union",
"unorm",
"unroll",
"unsigned",
"using",
"vector",
"vertexfragment",
"vertexshader",
"virtual",
"void",
"volatile",
"while"));
} // namespace
} // namespace hlsl
} // namespace writer
} // namespace tint

View File

@ -24,7 +24,6 @@
#include "src/ast/builder.h" #include "src/ast/builder.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/hlsl/generator_impl.h" #include "src/writer/hlsl/generator_impl.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -34,7 +33,7 @@ namespace hlsl {
template <typename BODY> template <typename BODY>
class TestHelperBase : public BODY, public ast::BuilderWithModule { class TestHelperBase : public BODY, public ast::BuilderWithModule {
public: public:
TestHelperBase() : td(mod), namer(mod), gen(mod, &namer) {} TestHelperBase() : td(mod), gen(mod) {}
~TestHelperBase() = default; ~TestHelperBase() = default;
/// @returns the result string /// @returns the result string
@ -45,8 +44,6 @@ class TestHelperBase : public BODY, public ast::BuilderWithModule {
/// The type determiner /// The type determiner
TypeDeterminer td; TypeDeterminer td;
/// The test namer
TestNamer namer;
/// The generator /// The generator
GeneratorImpl gen; GeneratorImpl gen;

View File

@ -22,20 +22,13 @@ namespace msl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), : Text(std::move(module)),
namer_(std::make_unique<MangleNamer>(module_)), impl_(std::make_unique<GeneratorImpl>(&module_)) {}
impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::Generator(ast::Module* module)
: Text(module),
namer_(std::make_unique<MangleNamer>(module_)),
impl_(std::make_unique<GeneratorImpl>(module_, namer_.get())) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
set_error(""); set_error("");
namer_->Reset(); impl_ = std::make_unique<GeneratorImpl>(&module_);
impl_ = std::make_unique<GeneratorImpl>(module_, namer_.get());
} }
bool Generator::Generate() { bool Generator::Generate() {

View File

@ -18,7 +18,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "src/namer.h"
#include "src/writer/msl/generator_impl.h" #include "src/writer/msl/generator_impl.h"
#include "src/writer/text.h" #include "src/writer/text.h"
@ -30,12 +29,8 @@ namespace msl {
class Generator : public Text { class Generator : public Text {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator
@ -59,7 +54,6 @@ class Generator : public Text {
std::string error() const; std::string error() const;
private: private:
std::unique_ptr<Namer> namer_;
std::unique_ptr<GeneratorImpl> impl_; std::unique_ptr<GeneratorImpl> impl_;
}; };

View File

@ -96,11 +96,22 @@ uint32_t adjust_for_alignment(uint32_t count, uint32_t alignment) {
} // namespace } // namespace
GeneratorImpl::GeneratorImpl(ast::Module* module, Namer* namer) GeneratorImpl::GeneratorImpl(ast::Module* module)
: TextGenerator(), module_(module), namer_(namer) {} : TextGenerator(), module_(module) {}
GeneratorImpl::~GeneratorImpl() = default; GeneratorImpl::~GeneratorImpl() = default;
std::string GeneratorImpl::generate_name(const std::string& prefix) {
std::string name = prefix;
uint32_t i = 0;
while (namer_.IsMapped(name)) {
name = prefix + "_" + std::to_string(i);
++i;
}
namer_.RegisterRemappedName(name);
return name;
}
bool GeneratorImpl::Generate() { bool GeneratorImpl::Generate() {
out_ << "#include <metal_stdlib>" << std::endl << std::endl; out_ << "#include <metal_stdlib>" << std::endl << std::endl;
@ -245,10 +256,11 @@ bool GeneratorImpl::EmitConstructedType(const ast::type::Type* ty) {
if (auto* alias = ty->As<ast::type::Alias>()) { if (auto* alias = ty->As<ast::type::Alias>()) {
out_ << "typedef "; out_ << "typedef ";
if (!EmitType(alias->type(), Symbol())) { if (!EmitType(alias->type(), "")) {
return false; return false;
} }
out_ << " " << namer_->NameFor(alias->symbol()) << ";" << std::endl; out_ << " " << namer_.NameFor(module_->SymbolToName(alias->symbol())) << ";"
<< std::endl;
} else if (auto* str = ty->As<ast::type::Struct>()) { } else if (auto* str = ty->As<ast::type::Struct>()) {
if (!EmitStructType(str)) { if (!EmitStructType(str)) {
return false; return false;
@ -277,7 +289,7 @@ bool GeneratorImpl::EmitArrayAccessor(ast::ArrayAccessorExpression* expr) {
bool GeneratorImpl::EmitBitcast(ast::BitcastExpression* expr) { bool GeneratorImpl::EmitBitcast(ast::BitcastExpression* expr) {
out_ << "as_type<"; out_ << "as_type<";
if (!EmitType(expr->type(), Symbol())) { if (!EmitType(expr->type(), "")) {
return false; return false;
} }
@ -396,25 +408,25 @@ bool GeneratorImpl::EmitBreak(ast::BreakStatement*) {
return true; return true;
} }
Symbol GeneratorImpl::current_ep_var_symbol(VarType type) { std::string GeneratorImpl::current_ep_var_name(VarType type) {
Symbol sym; std::string name = "";
switch (type) { switch (type) {
case VarType::kIn: { case VarType::kIn: {
auto in_it = ep_sym_to_in_data_.find(current_ep_sym_); auto in_it = ep_sym_to_in_data_.find(current_ep_sym_);
if (in_it != ep_sym_to_in_data_.end()) { if (in_it != ep_sym_to_in_data_.end()) {
sym = in_it->second.var_symbol; name = in_it->second.var_name;
} }
break; break;
} }
case VarType::kOut: { case VarType::kOut: {
auto out_it = ep_sym_to_out_data_.find(current_ep_sym_); auto out_it = ep_sym_to_out_data_.find(current_ep_sym_);
if (out_it != ep_sym_to_out_data_.end()) { if (out_it != ep_sym_to_out_data_.end()) {
sym = out_it->second.var_symbol; name = out_it->second.var_name;
} }
break; break;
} }
} }
return sym; return name;
} }
std::string GeneratorImpl::generate_intrinsic_name(ast::Intrinsic intrinsic) { std::string GeneratorImpl::generate_intrinsic_name(ast::Intrinsic intrinsic) {
@ -493,13 +505,13 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
// // We create variables to hold the two parameters in case they're // // We create variables to hold the two parameters in case they're
// // function calls with side effects. // // function calls with side effects.
// auto* param0 = param[0].get(); // auto* param0 = param[0].get();
// auto* name0 = namer_->GenerateName("outer_product_expr_0"); // auto* name0 = generate_name("outer_product_expr_0");
// auto* param1 = param[1].get(); // auto* param1 = param[1].get();
// auto* name1 = namer_->GenerateName("outer_product_expr_1"); // auto* name1 = generate_name("outer_product_expr_1");
// make_indent(); // make_indent();
// if (!EmitType(expr->result_type(), Symbol())) { // if (!EmitType(expr->result_type(), "")) {
// return false; // return false;
// } // }
// out_ << "("; // out_ << "(";
@ -561,11 +573,12 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
return true; return true;
} }
auto name_sym = ident->symbol(); auto name = module_->SymbolToName(ident->symbol());
auto it = ep_func_name_remapped_.find(module_->SymbolToName(current_ep_sym_) + auto caller_sym = ident->symbol();
"_" + module_->SymbolToName(name_sym)); auto it = ep_func_name_remapped_.find(current_ep_sym_.to_str() + "_" +
caller_sym.to_str());
if (it != ep_func_name_remapped_.end()) { if (it != ep_func_name_remapped_.end()) {
name_sym = it->second; name = it->second;
} }
auto* func = module_->FindFunctionBySymbol(ident->symbol()); auto* func = module_->FindFunctionBySymbol(ident->symbol());
@ -575,24 +588,24 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
return false; return false;
} }
out_ << namer_->NameFor(name_sym) << "("; out_ << name << "(";
bool first = true; bool first = true;
if (has_referenced_in_var_needing_struct(func)) { if (has_referenced_in_var_needing_struct(func)) {
auto var_sym = current_ep_var_symbol(VarType::kIn); auto var_name = current_ep_var_name(VarType::kIn);
if (var_sym.IsValid()) { if (!var_name.empty()) {
out_ << namer_->NameFor(var_sym); out_ << var_name;
first = false; first = false;
} }
} }
if (has_referenced_out_var_needing_struct(func)) { if (has_referenced_out_var_needing_struct(func)) {
auto var_sym = current_ep_var_symbol(VarType::kOut); auto var_name = current_ep_var_name(VarType::kOut);
if (var_sym.IsValid()) { if (!var_name.empty()) {
if (!first) { if (!first) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << namer_->NameFor(var_sym); out_ << var_name;
} }
} }
@ -605,7 +618,7 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << namer_->NameFor(var->symbol()); out_ << module_->SymbolToName(var->symbol());
} }
for (const auto& data : func->referenced_uniform_variables()) { for (const auto& data : func->referenced_uniform_variables()) {
@ -614,7 +627,7 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << namer_->NameFor(var->symbol()); out_ << module_->SymbolToName(var->symbol());
} }
for (const auto& data : func->referenced_storagebuffer_variables()) { for (const auto& data : func->referenced_storagebuffer_variables()) {
@ -623,7 +636,7 @@ bool GeneratorImpl::EmitCall(ast::CallExpression* expr) {
out_ << ", "; out_ << ", ";
} }
first = false; first = false;
out_ << namer_->NameFor(var->symbol()); out_ << module_->SymbolToName(var->symbol());
} }
const auto& params = expr->params(); const auto& params = expr->params();
@ -674,7 +687,7 @@ bool GeneratorImpl::EmitTextureCall(ast::CallExpression* expr) {
get_dim("width"); get_dim("width");
break; break;
case 2: case 2:
EmitType(expr->result_type(), Symbol()); EmitType(expr->result_type(), "");
out_ << "("; out_ << "(";
get_dim("width"); get_dim("width");
out_ << ", "; out_ << ", ";
@ -682,7 +695,7 @@ bool GeneratorImpl::EmitTextureCall(ast::CallExpression* expr) {
out_ << ")"; out_ << ")";
break; break;
case 3: case 3:
EmitType(expr->result_type(), Symbol()); EmitType(expr->result_type(), "");
out_ << "("; out_ << "(";
get_dim("width"); get_dim("width");
out_ << ", "; out_ << ", ";
@ -950,7 +963,7 @@ bool GeneratorImpl::EmitTypeConstructor(ast::TypeConstructorExpression* expr) {
if (expr->type()->Is<ast::type::Array>()) { if (expr->type()->Is<ast::type::Array>()) {
out_ << "{"; out_ << "{";
} else { } else {
if (!EmitType(expr->type(), Symbol())) { if (!EmitType(expr->type(), "")) {
return false; return false;
} }
out_ << "("; out_ << "(";
@ -1058,14 +1071,13 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
} }
if (!in_locations.empty()) { if (!in_locations.empty()) {
auto in_struct_sym = module_->RegisterSymbol(namer_->GenerateName( auto in_struct_name = generate_name(module_->SymbolToName(func->symbol()) +
module_->SymbolToName(func->symbol()) + "_" + kInStructNameSuffix)); "_" + kInStructNameSuffix);
auto in_var_name = namer_->GenerateName(kTintStructInVarPrefix); auto in_var_name = generate_name(kTintStructInVarPrefix);
ep_sym_to_in_data_[func->symbol()] = { ep_sym_to_in_data_[func->symbol()] = {in_struct_name, in_var_name};
in_struct_sym, module_->RegisterSymbol(in_var_name)};
make_indent(); make_indent();
out_ << "struct " << namer_->NameFor(in_struct_sym) << " {" << std::endl; out_ << "struct " << in_struct_name << " {" << std::endl;
increment_indent(); increment_indent();
@ -1074,11 +1086,11 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
uint32_t loc = data.second; uint32_t loc = data.second;
make_indent(); make_indent();
if (!EmitType(var->type(), var->symbol())) { if (!EmitType(var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
out_ << " " << namer_->NameFor(var->symbol()) << " [["; out_ << " " << module_->SymbolToName(var->symbol()) << " [[";
if (func->pipeline_stage() == ast::PipelineStage::kVertex) { if (func->pipeline_stage() == ast::PipelineStage::kVertex) {
out_ << "attribute(" << loc << ")"; out_ << "attribute(" << loc << ")";
} else if (func->pipeline_stage() == ast::PipelineStage::kFragment) { } else if (func->pipeline_stage() == ast::PipelineStage::kFragment) {
@ -1096,14 +1108,13 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
} }
if (!out_variables.empty()) { if (!out_variables.empty()) {
auto out_struct_sym = module_->RegisterSymbol(namer_->GenerateName( auto out_struct_name = generate_name(module_->SymbolToName(func->symbol()) +
module_->SymbolToName(func->symbol()) + "_" + kOutStructNameSuffix)); "_" + kOutStructNameSuffix);
auto out_var_name = namer_->GenerateName(kTintStructOutVarPrefix); auto out_var_name = generate_name(kTintStructOutVarPrefix);
ep_sym_to_out_data_[func->symbol()] = { ep_sym_to_out_data_[func->symbol()] = {out_struct_name, out_var_name};
out_struct_sym, module_->RegisterSymbol(out_var_name)};
make_indent(); make_indent();
out_ << "struct " << namer_->NameFor(out_struct_sym) << " {" << std::endl; out_ << "struct " << out_struct_name << " {" << std::endl;
increment_indent(); increment_indent();
for (auto& data : out_variables) { for (auto& data : out_variables) {
@ -1111,11 +1122,11 @@ bool GeneratorImpl::EmitEntryPointData(ast::Function* func) {
auto* deco = data.second; auto* deco = data.second;
make_indent(); make_indent();
if (!EmitType(var->type(), var->symbol())) { if (!EmitType(var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
out_ << " " << namer_->NameFor(var->symbol()) << " [["; out_ << " " << module_->SymbolToName(var->symbol()) << " [[";
if (auto* location = deco->As<ast::LocationDecoration>()) { if (auto* location = deco->As<ast::LocationDecoration>()) {
auto loc = location->value(); auto loc = location->value();
@ -1262,20 +1273,25 @@ bool GeneratorImpl::EmitFunction(ast::Function* func) {
bool GeneratorImpl::EmitFunctionInternal(ast::Function* func, bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
bool emit_duplicate_functions, bool emit_duplicate_functions,
Symbol ep_sym) { Symbol ep_sym) {
if (!EmitType(func->return_type(), Symbol())) { auto name = func->symbol().to_str();
if (!EmitType(func->return_type(), "")) {
return false; return false;
} }
out_ << " "; out_ << " ";
auto func_name_sym = func->symbol();
if (emit_duplicate_functions) { if (emit_duplicate_functions) {
auto func_name = module_->SymbolToName(func_name_sym); auto func_name = name;
auto ep_name = module_->SymbolToName(ep_sym); auto ep_name = ep_sym.to_str();
func_name_sym = module_->RegisterSymbol( // TODO(dsinclair): The SymbolToName should go away and just use
namer_->GenerateName(func_name + "_" + ep_name)); // to_str() here when the conversion is complete.
ep_func_name_remapped_[ep_name + "_" + func_name] = func_name_sym; name = generate_name(module_->SymbolToName(func->symbol()) + "_" +
module_->SymbolToName(ep_sym));
ep_func_name_remapped_[ep_name + "_" + func_name] = name;
} else {
// TODO(dsinclair): this should be updated to a remapped name
name = namer_.NameFor(module_->SymbolToName(func->symbol()));
} }
out_ << namer_->NameFor(func_name_sym) << "("; out_ << name << "(";
bool first = true; bool first = true;
@ -1286,8 +1302,8 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
if (emit_duplicate_functions) { if (emit_duplicate_functions) {
auto in_it = ep_sym_to_in_data_.find(ep_sym); auto in_it = ep_sym_to_in_data_.find(ep_sym);
if (in_it != ep_sym_to_in_data_.end()) { if (in_it != ep_sym_to_in_data_.end()) {
out_ << "thread " << namer_->NameFor(in_it->second.struct_symbol) << "& " out_ << "thread " << in_it->second.struct_name << "& "
<< namer_->NameFor(in_it->second.var_symbol); << in_it->second.var_name;
first = false; first = false;
} }
@ -1296,8 +1312,8 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
if (!first) { if (!first) {
out_ << ", "; out_ << ", ";
} }
out_ << "thread " << namer_->NameFor(out_it->second.struct_symbol) << "& " out_ << "thread " << out_it->second.struct_name << "& "
<< namer_->NameFor(out_it->second.var_symbol); << out_it->second.var_name;
first = false; first = false;
} }
} }
@ -1313,10 +1329,10 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
first = false; first = false;
out_ << "thread "; out_ << "thread ";
if (!EmitType(var->type(), Symbol())) { if (!EmitType(var->type(), "")) {
return false; return false;
} }
out_ << "& " << namer_->NameFor(var->symbol()); out_ << "& " << module_->SymbolToName(var->symbol());
} }
for (const auto& data : func->referenced_uniform_variables()) { for (const auto& data : func->referenced_uniform_variables()) {
@ -1328,10 +1344,10 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
out_ << "constant "; out_ << "constant ";
// TODO(dsinclair): Can arrays be uniform? If so, fix this ... // TODO(dsinclair): Can arrays be uniform? If so, fix this ...
if (!EmitType(var->type(), Symbol())) { if (!EmitType(var->type(), "")) {
return false; return false;
} }
out_ << "& " << namer_->NameFor(var->symbol()); out_ << "& " << module_->SymbolToName(var->symbol());
} }
for (const auto& data : func->referenced_storagebuffer_variables()) { for (const auto& data : func->referenced_storagebuffer_variables()) {
@ -1351,10 +1367,10 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
} }
out_ << "device "; out_ << "device ";
if (!EmitType(ac->type(), Symbol())) { if (!EmitType(ac->type(), "")) {
return false; return false;
} }
out_ << "& " << namer_->NameFor(var->symbol()); out_ << "& " << module_->SymbolToName(var->symbol());
} }
for (auto* v : func->params()) { for (auto* v : func->params()) {
@ -1363,12 +1379,12 @@ bool GeneratorImpl::EmitFunctionInternal(ast::Function* func,
} }
first = false; first = false;
if (!EmitType(v->type(), v->symbol())) { if (!EmitType(v->type(), module_->SymbolToName(v->symbol()))) {
return false; return false;
} }
// Array name is output as part of the type // Array name is output as part of the type
if (!v->type()->Is<ast::type::Array>()) { if (!v->type()->Is<ast::type::Array>()) {
out_ << " " << namer_->NameFor(v->symbol()); out_ << " " << module_->SymbolToName(v->symbol());
} }
} }
@ -1424,17 +1440,17 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
auto out_data = ep_sym_to_out_data_.find(current_ep_sym_); auto out_data = ep_sym_to_out_data_.find(current_ep_sym_);
bool has_out_data = out_data != ep_sym_to_out_data_.end(); bool has_out_data = out_data != ep_sym_to_out_data_.end();
if (has_out_data) { if (has_out_data) {
out_ << namer_->NameFor(out_data->second.struct_symbol); out_ << out_data->second.struct_name;
} else { } else {
out_ << "void"; out_ << "void";
} }
out_ << " " << namer_->NameFor(func->symbol()) << "("; out_ << " " << namer_.NameFor(module_->SymbolToName(func->symbol())) << "(";
bool first = true; bool first = true;
auto in_data = ep_sym_to_in_data_.find(current_ep_sym_); auto in_data = ep_sym_to_in_data_.find(current_ep_sym_);
if (in_data != ep_sym_to_in_data_.end()) { if (in_data != ep_sym_to_in_data_.end()) {
out_ << namer_->NameFor(in_data->second.struct_symbol) << " " out_ << in_data->second.struct_name << " " << in_data->second.var_name
<< namer_->NameFor(in_data->second.var_symbol) << " [[stage_in]]"; << " [[stage_in]]";
first = false; first = false;
} }
@ -1451,7 +1467,7 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
auto* builtin = data.second; auto* builtin = data.second;
if (!EmitType(var->type(), Symbol())) { if (!EmitType(var->type(), "")) {
return false; return false;
} }
@ -1460,7 +1476,8 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
error_ = "unknown builtin"; error_ = "unknown builtin";
return false; return false;
} }
out_ << " " << namer_->NameFor(var->symbol()) << " [[" << attr << "]]"; out_ << " " << module_->SymbolToName(var->symbol()) << " [[" << attr
<< "]]";
} }
for (auto data : func->referenced_uniform_variables()) { for (auto data : func->referenced_uniform_variables()) {
@ -1484,10 +1501,10 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
out_ << "constant "; out_ << "constant ";
// TODO(dsinclair): Can you have a uniform array? If so, this needs to be // TODO(dsinclair): Can you have a uniform array? If so, this needs to be
// updated to handle arrays property. // updated to handle arrays property.
if (!EmitType(var->type(), Symbol())) { if (!EmitType(var->type(), "")) {
return false; return false;
} }
out_ << "& " << namer_->NameFor(var->symbol()) << " [[buffer(" out_ << "& " << module_->SymbolToName(var->symbol()) << " [[buffer("
<< binding->value() << ")]]"; << binding->value() << ")]]";
} }
@ -1514,10 +1531,10 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
} }
out_ << "device "; out_ << "device ";
if (!EmitType(ac->type(), Symbol())) { if (!EmitType(ac->type(), "")) {
return false; return false;
} }
out_ << "& " << namer_->NameFor(var->symbol()) << " [[buffer(" out_ << "& " << module_->SymbolToName(var->symbol()) << " [[buffer("
<< binding->value() << ")]]"; << binding->value() << ")]]";
} }
@ -1527,9 +1544,8 @@ bool GeneratorImpl::EmitEntryPointFunction(ast::Function* func) {
if (has_out_data) { if (has_out_data) {
make_indent(); make_indent();
out_ << namer_->NameFor(out_data->second.struct_symbol) << " " out_ << out_data->second.struct_name << " " << out_data->second.var_name
<< namer_->NameFor(out_data->second.var_symbol) << " = {};" << " = {};" << std::endl;
<< std::endl;
} }
generating_entry_point_ = true; generating_entry_point_ = true;
@ -1567,21 +1583,15 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
auto var_type = var->storage_class() == ast::StorageClass::kInput auto var_type = var->storage_class() == ast::StorageClass::kInput
? VarType::kIn ? VarType::kIn
: VarType::kOut; : VarType::kOut;
auto sym = current_ep_var_symbol(var_type); auto name = current_ep_var_name(var_type);
if (!sym.IsValid()) { if (name.empty()) {
error_ = "unable to find entry point data for variable"; error_ = "unable to find entry point data for variable";
return false; return false;
} }
out_ << namer_->NameFor(sym) << "."; out_ << name << ".";
} }
} }
out_ << namer_.NameFor(module_->SymbolToName(ident->symbol()));
// Swizzles get written out directly
if (ident->IsSwizzle()) {
out_ << module_->SymbolToName(ident->symbol());
} else {
out_ << namer_->NameFor(ident->symbol());
}
return true; return true;
} }
@ -1589,8 +1599,8 @@ bool GeneratorImpl::EmitIdentifier(ast::IdentifierExpression* expr) {
bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) { bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
loop_emission_counter_++; loop_emission_counter_++;
std::string guard = namer_->GenerateName( std::string guard = namer_.NameFor("tint_msl_is_first_" +
"tint_msl_is_first_" + std::to_string(loop_emission_counter_)); std::to_string(loop_emission_counter_));
if (stmt->has_continuing()) { if (stmt->has_continuing()) {
make_indent(); make_indent();
@ -1640,7 +1650,7 @@ bool GeneratorImpl::EmitLoop(ast::LoopStatement* stmt) {
make_indent(); make_indent();
auto* var = decl->variable(); auto* var = decl->variable();
out_ << namer_->NameFor(var->symbol()) << " = "; out_ << module_->SymbolToName(var->symbol()) << " = ";
if (var->constructor() != nullptr) { if (var->constructor() != nullptr) {
if (!EmitExpression(var->constructor())) { if (!EmitExpression(var->constructor())) {
return false; return false;
@ -1736,7 +1746,7 @@ bool GeneratorImpl::EmitReturn(ast::ReturnStatement* stmt) {
if (generating_entry_point_) { if (generating_entry_point_) {
auto out_data = ep_sym_to_out_data_.find(current_ep_sym_); auto out_data = ep_sym_to_out_data_.find(current_ep_sym_);
if (out_data != ep_sym_to_out_data_.end()) { if (out_data != ep_sym_to_out_data_.end()) {
out_ << " " << namer_->NameFor(out_data->second.var_symbol); out_ << " " << out_data->second.var_name;
} }
} else if (stmt->has_value()) { } else if (stmt->has_value()) {
out_ << " "; out_ << " ";
@ -1855,9 +1865,9 @@ bool GeneratorImpl::EmitSwitch(ast::SwitchStatement* stmt) {
return true; return true;
} }
bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) { bool GeneratorImpl::EmitType(ast::type::Type* type, const std::string& name) {
if (auto* alias = type->As<ast::type::Alias>()) { if (auto* alias = type->As<ast::type::Alias>()) {
out_ << namer_->NameFor(alias->symbol()); out_ << namer_.NameFor(module_->SymbolToName(alias->symbol()));
} else if (auto* ary = type->As<ast::type::Array>()) { } else if (auto* ary = type->As<ast::type::Array>()) {
ast::type::Type* base_type = ary; ast::type::Type* base_type = ary;
std::vector<uint32_t> sizes; std::vector<uint32_t> sizes;
@ -1869,11 +1879,11 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) {
} }
base_type = arr->type(); base_type = arr->type();
} }
if (!EmitType(base_type, Symbol())) { if (!EmitType(base_type, "")) {
return false; return false;
} }
if (symbol.IsValid()) { if (!name.empty()) {
out_ << " " << namer_->NameFor(symbol); out_ << " " << namer_.NameFor(name);
} }
for (uint32_t size : sizes) { for (uint32_t size : sizes) {
out_ << "[" << size << "]"; out_ << "[" << size << "]";
@ -1885,13 +1895,13 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) {
} else if (type->Is<ast::type::I32>()) { } else if (type->Is<ast::type::I32>()) {
out_ << "int"; out_ << "int";
} else if (auto* mat = type->As<ast::type::Matrix>()) { } else if (auto* mat = type->As<ast::type::Matrix>()) {
if (!EmitType(mat->type(), Symbol())) { if (!EmitType(mat->type(), "")) {
return false; return false;
} }
out_ << mat->columns() << "x" << mat->rows(); out_ << mat->columns() << "x" << mat->rows();
} else if (auto* ptr = type->As<ast::type::Pointer>()) { } else if (auto* ptr = type->As<ast::type::Pointer>()) {
// TODO(dsinclair): Storage class? // TODO(dsinclair): Storage class?
if (!EmitType(ptr->type(), Symbol())) { if (!EmitType(ptr->type(), "")) {
return false; return false;
} }
out_ << "*"; out_ << "*";
@ -1900,7 +1910,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) {
} else if (auto* str = type->As<ast::type::Struct>()) { } else if (auto* str = type->As<ast::type::Struct>()) {
// The struct type emits as just the name. The declaration would be emitted // The struct type emits as just the name. The declaration would be emitted
// as part of emitting the constructed types. // as part of emitting the constructed types.
out_ << namer_->NameFor(str->symbol()); out_ << module_->SymbolToName(str->symbol());
} else if (auto* tex = type->As<ast::type::Texture>()) { } else if (auto* tex = type->As<ast::type::Texture>()) {
if (tex->Is<ast::type::DepthTexture>()) { if (tex->Is<ast::type::DepthTexture>()) {
out_ << "depth"; out_ << "depth";
@ -1941,7 +1951,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) {
if (tex->Is<ast::type::DepthTexture>()) { if (tex->Is<ast::type::DepthTexture>()) {
out_ << "float, access::sample"; out_ << "float, access::sample";
} else if (auto* storage = tex->As<ast::type::StorageTexture>()) { } else if (auto* storage = tex->As<ast::type::StorageTexture>()) {
if (!EmitType(storage->type(), Symbol())) { if (!EmitType(storage->type(), "")) {
return false; return false;
} }
out_ << ", access::"; out_ << ", access::";
@ -1954,12 +1964,12 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) {
return false; return false;
} }
} else if (auto* ms = tex->As<ast::type::MultisampledTexture>()) { } else if (auto* ms = tex->As<ast::type::MultisampledTexture>()) {
if (!EmitType(ms->type(), Symbol())) { if (!EmitType(ms->type(), "")) {
return false; return false;
} }
out_ << ", access::sample"; out_ << ", access::sample";
} else if (auto* sampled = tex->As<ast::type::SampledTexture>()) { } else if (auto* sampled = tex->As<ast::type::SampledTexture>()) {
if (!EmitType(sampled->type(), Symbol())) { if (!EmitType(sampled->type(), "")) {
return false; return false;
} }
out_ << ", access::sample"; out_ << ", access::sample";
@ -1972,7 +1982,7 @@ bool GeneratorImpl::EmitType(ast::type::Type* type, const Symbol& symbol) {
} else if (type->Is<ast::type::U32>()) { } else if (type->Is<ast::type::U32>()) {
out_ << "uint"; out_ << "uint";
} else if (auto* vec = type->As<ast::type::Vector>()) { } else if (auto* vec = type->As<ast::type::Vector>()) {
if (!EmitType(vec->type(), Symbol())) { if (!EmitType(vec->type(), "")) {
return false; return false;
} }
out_ << vec->size(); out_ << vec->size();
@ -1990,7 +2000,8 @@ bool GeneratorImpl::EmitStructType(const ast::type::Struct* str) {
// TODO(dsinclair): Block decoration? // TODO(dsinclair): Block decoration?
// if (str->impl()->decoration() != ast::StructDecoration::kNone) { // if (str->impl()->decoration() != ast::StructDecoration::kNone) {
// } // }
out_ << "struct " << namer_->NameFor(str->symbol()) << " {" << std::endl; out_ << "struct " << module_->SymbolToName(str->symbol()) << " {"
<< std::endl;
increment_indent(); increment_indent();
uint32_t current_offset = 0; uint32_t current_offset = 0;
@ -2013,7 +2024,7 @@ bool GeneratorImpl::EmitStructType(const ast::type::Struct* str) {
} }
} }
if (!EmitType(mem->type(), mem->symbol())) { if (!EmitType(mem->type(), module_->SymbolToName(mem->symbol()))) {
return false; return false;
} }
auto size = calculate_alignment_size(mem->type()); auto size = calculate_alignment_size(mem->type());
@ -2025,7 +2036,7 @@ bool GeneratorImpl::EmitStructType(const ast::type::Struct* str) {
// Array member name will be output with the type // Array member name will be output with the type
if (!mem->type()->Is<ast::type::Array>()) { if (!mem->type()->Is<ast::type::Array>()) {
out_ << " " << namer_->NameFor(mem->symbol()); out_ << " " << namer_.NameFor(module_->SymbolToName(mem->symbol()));
} }
out_ << ";" << std::endl; out_ << ";" << std::endl;
} }
@ -2067,11 +2078,11 @@ bool GeneratorImpl::EmitVariable(ast::Variable* var, bool skip_constructor) {
if (var->is_const()) { if (var->is_const()) {
out_ << "const "; out_ << "const ";
} }
if (!EmitType(var->type(), var->symbol())) { if (!EmitType(var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
if (!var->type()->Is<ast::type::Array>()) { if (!var->type()->Is<ast::type::Array>()) {
out_ << " " << namer_->NameFor(var->symbol()); out_ << " " << module_->SymbolToName(var->symbol());
} }
if (!skip_constructor) { if (!skip_constructor) {
@ -2109,11 +2120,11 @@ bool GeneratorImpl::EmitProgramConstVariable(const ast::Variable* var) {
} }
out_ << "constant "; out_ << "constant ";
if (!EmitType(var->type(), var->symbol())) { if (!EmitType(var->type(), module_->SymbolToName(var->symbol()))) {
return false; return false;
} }
if (!var->type()->Is<ast::type::Array>()) { if (!var->type()->Is<ast::type::Array>()) {
out_ << " " << namer_->NameFor(var->symbol()); out_ << " " << module_->SymbolToName(var->symbol());
} }
if (var->HasConstantIdDecoration()) { if (var->HasConstantIdDecoration()) {

View File

@ -42,8 +42,8 @@
#include "src/ast/type/struct_type.h" #include "src/ast/type/struct_type.h"
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/ast/unary_op_expression.h" #include "src/ast/unary_op_expression.h"
#include "src/namer.h"
#include "src/scope_stack.h" #include "src/scope_stack.h"
#include "src/writer/msl/namer.h"
#include "src/writer/text_generator.h" #include "src/writer/text_generator.h"
namespace tint { namespace tint {
@ -55,8 +55,7 @@ class GeneratorImpl : public TextGenerator {
public: public:
/// Constructor /// Constructor
/// @param module the module to generate /// @param module the module to generate
/// @param namer the namer to use for generation explicit GeneratorImpl(ast::Module* module);
GeneratorImpl(ast::Module* module, Namer* namer);
~GeneratorImpl(); ~GeneratorImpl();
/// @returns true on successful generation; false otherwise /// @returns true on successful generation; false otherwise
@ -203,9 +202,9 @@ class GeneratorImpl : public TextGenerator {
bool EmitSwitch(ast::SwitchStatement* stmt); bool EmitSwitch(ast::SwitchStatement* stmt);
/// Handles generating type /// Handles generating type
/// @param type the type to generate /// @param type the type to generate
/// @param symbol the symbol of the variable, only used for array emission /// @param name the name of the variable, only used for array emission
/// @returns true if the type is emitted /// @returns true if the type is emitted
bool EmitType(ast::type::Type* type, const Symbol& symbol); bool EmitType(ast::type::Type* type, const std::string& name);
/// Handles generating a struct declaration /// Handles generating a struct declaration
/// @param str the struct to generate /// @param str the struct to generate
/// @returns true if the struct is emitted /// @returns true if the struct is emitted
@ -245,6 +244,10 @@ class GeneratorImpl : public TextGenerator {
/// @returns true if an input or output struct is required. /// @returns true if an input or output struct is required.
bool has_referenced_var_needing_struct(ast::Function* func); bool has_referenced_var_needing_struct(ast::Function* func);
/// Generates a name for the prefix
/// @param prefix the prefix of the name to generate
/// @returns the name
std::string generate_name(const std::string& prefix);
/// Generates an intrinsic name from the given name /// Generates an intrinsic name from the given name
/// @param intrinsic the intrinsic to convert to an method name /// @param intrinsic the intrinsic to convert to an method name
/// @returns the intrinsic name or blank on error /// @returns the intrinsic name or blank on error
@ -264,22 +267,25 @@ class GeneratorImpl : public TextGenerator {
/// @returns the string name of the builtin or blank on error /// @returns the string name of the builtin or blank on error
std::string builtin_to_attribute(ast::Builtin builtin) const; std::string builtin_to_attribute(ast::Builtin builtin) const;
/// @returns the namer for testing purposes
Namer* namer_for_testing() { return &namer_; }
private: private:
enum class VarType { kIn, kOut }; enum class VarType { kIn, kOut };
struct EntryPointData { struct EntryPointData {
Symbol struct_symbol; std::string struct_name;
Symbol var_symbol; std::string var_name;
}; };
Symbol current_ep_var_symbol(VarType type); std::string current_ep_var_name(VarType type);
Namer namer_;
ScopeStack<ast::Variable*> global_variables_; ScopeStack<ast::Variable*> global_variables_;
Symbol current_ep_sym_; Symbol current_ep_sym_;
bool generating_entry_point_ = false; bool generating_entry_point_ = false;
ast::Module* module_ = nullptr; const ast::Module* module_ = nullptr;
uint32_t loop_emission_counter_ = 0; uint32_t loop_emission_counter_ = 0;
Namer* namer_;
std::unordered_map<Symbol, EntryPointData> ep_sym_to_in_data_; std::unordered_map<Symbol, EntryPointData> ep_sym_to_in_data_;
std::unordered_map<Symbol, EntryPointData> ep_sym_to_out_data_; std::unordered_map<Symbol, EntryPointData> ep_sym_to_out_data_;
@ -287,7 +293,7 @@ class GeneratorImpl : public TextGenerator {
// This maps an input of "<entry_point_name>_<function_name>" to a remapped // This maps an input of "<entry_point_name>_<function_name>" to a remapped
// function name. If there is no entry for a given key then function did // function name. If there is no entry for a given key then function did
// not need to be remapped for the entry point and can be emitted directly. // not need to be remapped for the entry point and can be emitted directly.
std::unordered_map<std::string, Symbol> ep_func_name_remapped_; std::unordered_map<std::string, std::string> ep_func_name_remapped_;
}; };
} // namespace msl } // namespace msl

View File

@ -31,7 +31,15 @@ TEST_F(MslGeneratorImplTest, EmitConstructedType_F32) {
auto* alias = ty.alias("a", ty.f32); auto* alias = ty.alias("a", ty.f32);
ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error(); ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error();
EXPECT_EQ(gen.result(), R"(typedef float test_a; EXPECT_EQ(gen.result(), R"(typedef float a;
)");
}
TEST_F(MslGeneratorImplTest, EmitConstructedType_NameCollision) {
auto* alias = ty.alias("float", ty.f32);
ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error();
EXPECT_EQ(gen.result(), R"(typedef float float_tint_0;
)"); )");
} }
@ -43,9 +51,9 @@ TEST_F(MslGeneratorImplTest, EmitConstructedType_Struct) {
auto* s = ty.struct_("a", str); auto* s = ty.struct_("a", str);
ASSERT_TRUE(gen.EmitConstructedType(s)) << gen.error(); ASSERT_TRUE(gen.EmitConstructedType(s)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_a { EXPECT_EQ(gen.result(), R"(struct a {
float test_a; float a;
int test_b; int b;
}; };
)"); )");
} }
@ -60,7 +68,7 @@ TEST_F(MslGeneratorImplTest, EmitConstructedType_AliasStructIdent) {
auto* alias = ty.alias("a", s); auto* alias = ty.alias("a", s);
ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error(); ASSERT_TRUE(gen.EmitConstructedType(alias)) << gen.error();
EXPECT_EQ(gen.result(), R"(typedef test_b test_a; EXPECT_EQ(gen.result(), R"(typedef b a;
)"); )");
} }

View File

@ -35,7 +35,7 @@ TEST_F(MslGeneratorImplTest, EmitExpression_ArrayAccessor) {
auto* expr = IndexAccessor(Expr("ary"), 5); auto* expr = IndexAccessor(Expr("ary"), 5);
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "test_ary[5]"); EXPECT_EQ(gen.result(), "ary[5]");
} }
TEST_F(MslGeneratorImplTest, EmitArrayAccessor) { TEST_F(MslGeneratorImplTest, EmitArrayAccessor) {
@ -43,7 +43,7 @@ TEST_F(MslGeneratorImplTest, EmitArrayAccessor) {
ASSERT_TRUE(gen.EmitArrayAccessor(expr->As<ast::ArrayAccessorExpression>())) ASSERT_TRUE(gen.EmitArrayAccessor(expr->As<ast::ArrayAccessorExpression>()))
<< gen.error(); << gen.error();
EXPECT_EQ(gen.result(), "test_ary[test_idx]"); EXPECT_EQ(gen.result(), "ary[idx]");
} }
} // namespace } // namespace

View File

@ -34,7 +34,7 @@ TEST_F(MslGeneratorImplTest, Emit_Assign) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(assign)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(assign)) << gen.error();
EXPECT_EQ(gen.result(), " test_lhs = test_rhs;\n"); EXPECT_EQ(gen.result(), " lhs = rhs;\n");
} }
} // namespace } // namespace

View File

@ -47,25 +47,24 @@ INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest, MslGeneratorImplTest,
MslBinaryTest, MslBinaryTest,
testing::Values( testing::Values(
BinaryData{"(test_left & test_right)", ast::BinaryOp::kAnd}, BinaryData{"(left & right)", ast::BinaryOp::kAnd},
BinaryData{"(test_left | test_right)", ast::BinaryOp::kOr}, BinaryData{"(left | right)", ast::BinaryOp::kOr},
BinaryData{"(test_left ^ test_right)", ast::BinaryOp::kXor}, BinaryData{"(left ^ right)", ast::BinaryOp::kXor},
BinaryData{"(test_left && test_right)", ast::BinaryOp::kLogicalAnd}, BinaryData{"(left && right)", ast::BinaryOp::kLogicalAnd},
BinaryData{"(test_left || test_right)", ast::BinaryOp::kLogicalOr}, BinaryData{"(left || right)", ast::BinaryOp::kLogicalOr},
BinaryData{"(test_left == test_right)", ast::BinaryOp::kEqual}, BinaryData{"(left == right)", ast::BinaryOp::kEqual},
BinaryData{"(test_left != test_right)", ast::BinaryOp::kNotEqual}, BinaryData{"(left != right)", ast::BinaryOp::kNotEqual},
BinaryData{"(test_left < test_right)", ast::BinaryOp::kLessThan}, BinaryData{"(left < right)", ast::BinaryOp::kLessThan},
BinaryData{"(test_left > test_right)", ast::BinaryOp::kGreaterThan}, BinaryData{"(left > right)", ast::BinaryOp::kGreaterThan},
BinaryData{"(test_left <= test_right)", ast::BinaryOp::kLessThanEqual}, BinaryData{"(left <= right)", ast::BinaryOp::kLessThanEqual},
BinaryData{"(test_left >= test_right)", BinaryData{"(left >= right)", ast::BinaryOp::kGreaterThanEqual},
ast::BinaryOp::kGreaterThanEqual}, BinaryData{"(left << right)", ast::BinaryOp::kShiftLeft},
BinaryData{"(test_left << test_right)", ast::BinaryOp::kShiftLeft}, BinaryData{"(left >> right)", ast::BinaryOp::kShiftRight},
BinaryData{"(test_left >> test_right)", ast::BinaryOp::kShiftRight}, BinaryData{"(left + right)", ast::BinaryOp::kAdd},
BinaryData{"(test_left + test_right)", ast::BinaryOp::kAdd}, BinaryData{"(left - right)", ast::BinaryOp::kSubtract},
BinaryData{"(test_left - test_right)", ast::BinaryOp::kSubtract}, BinaryData{"(left * right)", ast::BinaryOp::kMultiply},
BinaryData{"(test_left * test_right)", ast::BinaryOp::kMultiply}, BinaryData{"(left / right)", ast::BinaryOp::kDivide},
BinaryData{"(test_left / test_right)", ast::BinaryOp::kDivide}, BinaryData{"(left % right)", ast::BinaryOp::kModulo}));
BinaryData{"(test_left % test_right)", ast::BinaryOp::kModulo}));
} // namespace } // namespace
} // namespace msl } // namespace msl

View File

@ -32,7 +32,7 @@ using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, EmitExpression_Bitcast) { TEST_F(MslGeneratorImplTest, EmitExpression_Bitcast) {
auto* bitcast = create<ast::BitcastExpression>(ty.f32, Expr("id")); auto* bitcast = create<ast::BitcastExpression>(ty.f32, Expr("id"));
ASSERT_TRUE(gen.EmitExpression(bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(bitcast)) << gen.error();
EXPECT_EQ(gen.result(), "as_type<float>(test_id)"); EXPECT_EQ(gen.result(), "as_type<float>(id)");
} }
} // namespace } // namespace

View File

@ -38,7 +38,7 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
mod->AddFunction(func); mod->AddFunction(func);
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), "test_my_func()"); EXPECT_EQ(gen.result(), "my_func()");
} }
TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithParams) { TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithParams) {
@ -48,7 +48,7 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithParams) {
mod->AddFunction(func); mod->AddFunction(func);
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), "test_my_func(test_param1, test_param2)"); EXPECT_EQ(gen.result(), "my_func(param1, param2)");
} }
TEST_F(MslGeneratorImplTest, EmitStatement_Call) { TEST_F(MslGeneratorImplTest, EmitStatement_Call) {
@ -61,7 +61,7 @@ TEST_F(MslGeneratorImplTest, EmitStatement_Call) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(expr)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(expr)) << gen.error();
EXPECT_EQ(gen.result(), " test_my_func(test_param1, test_param2);\n"); EXPECT_EQ(gen.result(), " my_func(param1, param2);\n");
} }
} // namespace } // namespace

View File

@ -34,14 +34,14 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Scalar) {
auto* cast = Construct<f32>("id"); auto* cast = Construct<f32>("id");
ASSERT_TRUE(gen.EmitExpression(cast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(cast)) << gen.error();
EXPECT_EQ(gen.result(), "float(test_id)"); EXPECT_EQ(gen.result(), "float(id)");
} }
TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) { TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) {
auto* cast = vec3<f32>("id"); auto* cast = vec3<f32>("id");
ASSERT_TRUE(gen.EmitExpression(cast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(cast)) << gen.error();
EXPECT_EQ(gen.result(), "float3(test_id)"); EXPECT_EQ(gen.result(), "float3(id)");
} }
} // namespace } // namespace

View File

@ -76,9 +76,9 @@ TEST_F(MslGeneratorImplTest, Emit_Function_EntryPointData_Vertex_Input) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_vtx_main_in { EXPECT_EQ(gen.result(), R"(struct vtx_main_in {
float test_foo [[attribute(0)]]; float foo [[attribute(0)]];
int test_bar [[attribute(1)]]; int bar [[attribute(1)]];
}; };
)"); )");
@ -122,9 +122,9 @@ TEST_F(MslGeneratorImplTest, Emit_Function_EntryPointData_Vertex_Output) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_vtx_main_out { EXPECT_EQ(gen.result(), R"(struct vtx_main_out {
float test_foo [[user(locn0)]]; float foo [[user(locn0)]];
int test_bar [[user(locn1)]]; int bar [[user(locn1)]];
}; };
)"); )");
@ -168,9 +168,9 @@ TEST_F(MslGeneratorImplTest, Emit_Function_EntryPointData_Fragment_Input) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_main_in { EXPECT_EQ(gen.result(), R"(struct main_in {
float test_foo [[user(locn0)]]; float foo [[user(locn0)]];
int test_bar [[user(locn1)]]; int bar [[user(locn1)]];
}; };
)"); )");
@ -214,9 +214,9 @@ TEST_F(MslGeneratorImplTest, Emit_Function_EntryPointData_Fragment_Output) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_main_out { EXPECT_EQ(gen.result(), R"(struct main_out {
float test_foo [[color(0)]]; float foo [[color(0)]];
int test_bar [[color(1)]]; int bar [[color(1)]];
}; };
)"); )");
@ -338,8 +338,8 @@ TEST_F(MslGeneratorImplTest, Emit_Function_EntryPointData_Builtins) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error(); ASSERT_TRUE(gen.EmitEntryPointData(func)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_main_out { EXPECT_EQ(gen.result(), R"(struct main_out {
float test_depth [[depth(any)]]; float depth [[depth(any)]];
}; };
)"); )");

View File

@ -68,7 +68,27 @@ TEST_F(MslGeneratorImplTest, Emit_Function) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
void test_my_func() { void my_func() {
return;
}
)");
}
TEST_F(MslGeneratorImplTest, Emit_Function_Name_Collision) {
auto* func = Func("main", ast::VariableList{}, ty.void_,
ast::StatementList{
create<ast::ReturnStatement>(),
},
ast::FunctionDecorationList{});
mod->AddFunction(func);
gen.increment_indent();
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
void main_tint_0() {
return; return;
} }
@ -92,7 +112,7 @@ TEST_F(MslGeneratorImplTest, Emit_Function_WithParams) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
void test_my_func(float test_a, int test_b) { void my_func(float a, int b) {
return; return;
} }
@ -129,18 +149,18 @@ TEST_F(MslGeneratorImplTest, Emit_FunctionDecoration_EntryPoint_WithInOutVars) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_frag_main_in { struct frag_main_in {
float test_foo [[user(locn0)]]; float foo [[user(locn0)]];
}; };
struct test_frag_main_out { struct frag_main_out {
float test_bar [[color(1)]]; float bar [[color(1)]];
}; };
fragment test_frag_main_out test_frag_main(test_frag_main_in test_tint_in [[stage_in]]) { fragment frag_main_out frag_main(frag_main_in tint_in [[stage_in]]) {
test_frag_main_out test_tint_out = {}; frag_main_out tint_out = {};
test_tint_out.test_bar = test_tint_in.test_foo; tint_out.bar = tint_in.foo;
return test_tint_out; return tint_out;
} }
)"); )");
@ -182,14 +202,14 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_frag_main_out { struct frag_main_out {
float test_depth [[depth(any)]]; float depth [[depth(any)]];
}; };
fragment test_frag_main_out test_frag_main(float4 test_coord [[position]]) { fragment frag_main_out frag_main(float4 coord [[position]]) {
test_frag_main_out test_tint_out = {}; frag_main_out tint_out = {};
test_tint_out.test_depth = test_coord.x; tint_out.depth = coord.x;
return test_tint_out; return tint_out;
} }
)"); )");
@ -225,8 +245,8 @@ TEST_F(MslGeneratorImplTest, Emit_FunctionDecoration_EntryPoint_With_Uniform) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
fragment void test_frag_main(constant float4& test_coord [[buffer(0)]]) { fragment void frag_main(constant float4& coord [[buffer(0)]]) {
float test_v = test_coord.x; float v = coord.x;
return; return;
} }
@ -273,13 +293,13 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_Data { struct Data {
int test_a; int a;
float test_b; float b;
}; };
fragment void test_frag_main(device test_Data& test_coord [[buffer(0)]]) { fragment void frag_main(device Data& coord [[buffer(0)]]) {
float test_v = test_coord.test_b; float v = coord.b;
return; return;
} }
@ -290,7 +310,7 @@ TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_EntryPoint_With_RO_StorageBuffer) { Emit_FunctionDecoration_EntryPoint_With_RO_StorageBuffer) {
auto* str = create<ast::Struct>( auto* str = create<ast::Struct>(
ast::StructMemberList{Member("a", ty.i32, {MemberOffset(0)}), ast::StructMemberList{Member("a", ty.i32, {MemberOffset(0)}),
Member("x", ty.f32, {MemberOffset(4)})}, Member("b", ty.f32, {MemberOffset(4)})},
ast::StructDecorationList{}); ast::StructDecorationList{});
auto* s = ty.struct_("Data", str); auto* s = ty.struct_("Data", str);
@ -306,7 +326,7 @@ TEST_F(MslGeneratorImplTest,
mod->AddGlobalVariable(coord_var); mod->AddGlobalVariable(coord_var);
auto* var = Var("v", ast::StorageClass::kFunction, ty.f32, auto* var = Var("v", ast::StorageClass::kFunction, ty.f32,
MemberAccessor("coord", "x"), ast::VariableDecorationList{}); MemberAccessor("coord", "b"), ast::VariableDecorationList{});
auto* func = auto* func =
Func("frag_main", ast::VariableList{}, ty.void_, Func("frag_main", ast::VariableList{}, ty.void_,
@ -325,13 +345,13 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_Data { struct Data {
int test_a; int a;
float test_x; float b;
}; };
fragment void test_frag_main(const device test_Data& test_coord [[buffer(0)]]) { fragment void frag_main(const device Data& coord [[buffer(0)]]) {
float test_v = test_coord.test_x; float v = coord.b;
return; return;
} }
@ -390,25 +410,25 @@ TEST_F(
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_ep_1_in { struct ep_1_in {
float test_foo [[user(locn0)]]; float foo [[user(locn0)]];
}; };
struct test_ep_1_out { struct ep_1_out {
float test_bar [[color(1)]]; float bar [[color(1)]];
float test_val [[color(0)]]; float val [[color(0)]];
}; };
float test_sub_func_ep_1(thread test_ep_1_in& test_tint_in, thread test_ep_1_out& test_tint_out, float test_param) { float sub_func_ep_1(thread ep_1_in& tint_in, thread ep_1_out& tint_out, float param) {
test_tint_out.test_bar = test_tint_in.test_foo; tint_out.bar = tint_in.foo;
test_tint_out.test_val = test_param; tint_out.val = param;
return test_tint_in.test_foo; return tint_in.foo;
} }
fragment test_ep_1_out test_ep_1(test_ep_1_in test_tint_in [[stage_in]]) { fragment ep_1_out ep_1(ep_1_in tint_in [[stage_in]]) {
test_ep_1_out test_tint_out = {}; ep_1_out tint_out = {};
test_tint_out.test_bar = test_sub_func_ep_1(test_tint_in, test_tint_out, 1.0f); tint_out.bar = sub_func_ep_1(tint_in, tint_out, 1.0f);
return test_tint_out; return tint_out;
} }
)"); )");
@ -453,18 +473,18 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_ep_1_out { struct ep_1_out {
float test_depth [[depth(any)]]; float depth [[depth(any)]];
}; };
float test_sub_func(float test_param) { float sub_func(float param) {
return test_param; return param;
} }
fragment test_ep_1_out test_ep_1() { fragment ep_1_out ep_1() {
test_ep_1_out test_tint_out = {}; ep_1_out tint_out = {};
test_tint_out.test_depth = test_sub_func(1.0f); tint_out.depth = sub_func(1.0f);
return test_tint_out; return tint_out;
} }
)"); )");
@ -518,19 +538,19 @@ TEST_F(
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_ep_1_out { struct ep_1_out {
float test_depth [[depth(any)]]; float depth [[depth(any)]];
}; };
float test_sub_func_ep_1(thread test_ep_1_out& test_tint_out, thread float4& test_coord, float test_param) { float sub_func_ep_1(thread ep_1_out& tint_out, thread float4& coord, float param) {
test_tint_out.test_depth = test_coord.x; tint_out.depth = coord.x;
return test_param; return param;
} }
fragment test_ep_1_out test_ep_1(float4 test_coord [[position]]) { fragment ep_1_out ep_1(float4 coord [[position]]) {
test_ep_1_out test_tint_out = {}; ep_1_out tint_out = {};
test_tint_out.test_depth = test_sub_func_ep_1(test_tint_out, test_coord, 1.0f); tint_out.depth = sub_func_ep_1(tint_out, coord, 1.0f);
return test_tint_out; return tint_out;
} }
)"); )");
@ -579,12 +599,12 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
float test_sub_func(constant float4& test_coord, float test_param) { float sub_func(constant float4& coord, float param) {
return test_coord.x; return coord.x;
} }
fragment void test_frag_main(constant float4& test_coord [[buffer(0)]]) { fragment void frag_main(constant float4& coord [[buffer(0)]]) {
float test_v = test_sub_func(test_coord, 1.0f); float v = sub_func(coord, 1.0f);
return; return;
} }
@ -641,17 +661,17 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_Data { struct Data {
int test_a; int a;
float test_b; float b;
}; };
float test_sub_func(device test_Data& test_coord, float test_param) { float sub_func(device Data& coord, float param) {
return test_coord.test_b; return coord.b;
} }
fragment void test_frag_main(device test_Data& test_coord [[buffer(0)]]) { fragment void frag_main(device Data& coord [[buffer(0)]]) {
float test_v = test_sub_func(test_coord, 1.0f); float v = sub_func(coord, 1.0f);
return; return;
} }
@ -711,17 +731,17 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_Data { struct Data {
int test_a; int a;
float test_b; float b;
}; };
float test_sub_func(const device test_Data& test_coord, float test_param) { float sub_func(const device Data& coord, float param) {
return test_coord.test_b; return coord.b;
} }
fragment void test_frag_main(const device test_Data& test_coord [[buffer(0)]]) { fragment void frag_main(const device Data& coord [[buffer(0)]]) {
float test_v = test_sub_func(test_coord, 1.0f); float v = sub_func(coord, 1.0f);
return; return;
} }
@ -761,17 +781,36 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_ep_1_out { struct ep_1_out {
float test_bar [[color(1)]]; float bar [[color(1)]];
}; };
fragment test_ep_1_out test_ep_1() { fragment ep_1_out ep_1() {
test_ep_1_out test_tint_out = {}; ep_1_out tint_out = {};
test_tint_out.test_bar = 1.0f; tint_out.bar = 1.0f;
if ((1 == 1)) { if ((1 == 1)) {
return test_tint_out; return tint_out;
} }
return test_tint_out; return tint_out;
}
)");
}
TEST_F(MslGeneratorImplTest,
Emit_FunctionDecoration_EntryPoint_WithNameCollision) {
auto* func =
Func("main", ast::VariableList{}, ty.void_, ast::StatementList{},
ast::FunctionDecorationList{
create<ast::StageDecoration>(ast::PipelineStage::kCompute),
});
mod->AddFunction(func);
ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
kernel void main_tint_0() {
} }
)"); )");
@ -793,7 +832,7 @@ TEST_F(MslGeneratorImplTest, Emit_Function_WithArrayParams) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
void test_my_func(float test_a[5]) { void my_func(float a[5]) {
return; return;
} }
@ -872,17 +911,17 @@ TEST_F(MslGeneratorImplTest,
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
struct test_Data { struct Data {
float test_d; float d;
}; };
kernel void test_a(device test_Data& test_data [[buffer(0)]]) { kernel void a(device Data& data [[buffer(0)]]) {
float test_v = test_data.test_d; float v = data.d;
return; return;
} }
kernel void test_b(device test_Data& test_data [[buffer(0)]]) { kernel void b(device Data& data [[buffer(0)]]) {
float test_v = test_data.test_d; float v = data.d;
return; return;
} }

View File

@ -28,7 +28,13 @@ using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, EmitIdentifierExpression) { TEST_F(MslGeneratorImplTest, EmitIdentifierExpression) {
auto* i = Expr("foo"); auto* i = Expr("foo");
ASSERT_TRUE(gen.EmitExpression(i)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(i)) << gen.error();
EXPECT_EQ(gen.result(), "test_foo"); EXPECT_EQ(gen.result(), "foo");
}
TEST_F(MslGeneratorImplTest, EmitIdentifierExpression_Single_WithCollision) {
auto* i = Expr("virtual");
ASSERT_TRUE(gen.EmitExpression(i)) << gen.error();
EXPECT_EQ(gen.result(), "virtual_tint_0");
} }
} // namespace } // namespace

View File

@ -38,7 +38,7 @@ TEST_F(MslGeneratorImplTest, Emit_If) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
EXPECT_EQ(gen.result(), R"( if (test_cond) { EXPECT_EQ(gen.result(), R"( if (cond) {
return; return;
} }
)"); )");
@ -63,9 +63,9 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
EXPECT_EQ(gen.result(), R"( if (test_cond) { EXPECT_EQ(gen.result(), R"( if (cond) {
return; return;
} else if (test_else_cond) { } else if (else_cond) {
return; return;
} }
)"); )");
@ -89,7 +89,7 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElse) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
EXPECT_EQ(gen.result(), R"( if (test_cond) { EXPECT_EQ(gen.result(), R"( if (cond) {
return; return;
} else { } else {
return; return;
@ -122,9 +122,9 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(i)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
EXPECT_EQ(gen.result(), R"( if (test_cond) { EXPECT_EQ(gen.result(), R"( if (cond) {
return; return;
} else if (test_else_cond) { } else if (else_cond) {
return; return;
} else { } else {
return; return;

View File

@ -187,7 +187,7 @@ TEST_F(MslGeneratorImplTest, MslImportData_Determinant) {
ASSERT_TRUE(td.Determine()) << td.error(); ASSERT_TRUE(td.Determine()) << td.error();
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error(); ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error(); ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string("metal::determinant(test_var)")); EXPECT_EQ(gen.result(), std::string("metal::determinant(var)"));
} }
} // namespace } // namespace

View File

@ -69,7 +69,7 @@ TEST_F(MslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) {
auto* a = Var("a", ast::StorageClass::kNone, ty.vec2<f32>()); auto* a = Var("a", ast::StorageClass::kNone, ty.vec2<f32>());
auto* b = Var("b", ast::StorageClass::kNone, ty.vec3<f32>()); auto* b = Var("b", ast::StorageClass::kNone, ty.vec3<f32>());
auto* call = Call("outerProduct", "a", "b"); auto* call = Call("outer_product", "a", "b");
td.RegisterVariableForTesting(a); td.RegisterVariableForTesting(a);
td.RegisterVariableForTesting(b); td.RegisterVariableForTesting(b);
@ -81,9 +81,7 @@ TEST_F(MslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ( EXPECT_EQ(gen.result(), " float3x2(a * b[0], a * b[1], a * b[2])");
gen.result(),
" float3x2(test_a * test_b[0], test_a * test_b[1], test_a * test_b[2])");
} }
TEST_F(MslGeneratorImplTest, Intrinsic_Bad_Name) { TEST_F(MslGeneratorImplTest, Intrinsic_Bad_Name) {
@ -103,7 +101,7 @@ TEST_F(MslGeneratorImplTest, Intrinsic_Call) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), " dot(test_param1, test_param2)"); EXPECT_EQ(gen.result(), " dot(param1, param2)");
} }
} // namespace } // namespace

View File

@ -22,7 +22,6 @@
#include "src/ast/type/sampled_texture_type.h" #include "src/ast/type/sampled_texture_type.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/msl/generator_impl.h" #include "src/writer/msl/generator_impl.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -39,7 +38,7 @@ std::string expected_texture_overload(
case ValidTextureOverload::kDimensionsStorageRO1dArray: case ValidTextureOverload::kDimensionsStorageRO1dArray:
case ValidTextureOverload::kDimensionsStorageWO1d: case ValidTextureOverload::kDimensionsStorageWO1d:
case ValidTextureOverload::kDimensionsStorageWO1dArray: case ValidTextureOverload::kDimensionsStorageWO1dArray:
return R"(test_texture.get_width())"; return R"(texture_tint_0.get_width())";
case ValidTextureOverload::kDimensions2d: case ValidTextureOverload::kDimensions2d:
case ValidTextureOverload::kDimensions2dArray: case ValidTextureOverload::kDimensions2dArray:
case ValidTextureOverload::kDimensionsMultisampled_2d: case ValidTextureOverload::kDimensionsMultisampled_2d:
@ -50,7 +49,7 @@ std::string expected_texture_overload(
case ValidTextureOverload::kDimensionsStorageRO2dArray: case ValidTextureOverload::kDimensionsStorageRO2dArray:
case ValidTextureOverload::kDimensionsStorageWO2d: case ValidTextureOverload::kDimensionsStorageWO2d:
case ValidTextureOverload::kDimensionsStorageWO2dArray: case ValidTextureOverload::kDimensionsStorageWO2dArray:
return R"(int2(test_texture.get_width(), test_texture.get_height()))"; return R"(int2(texture_tint_0.get_width(), texture_tint_0.get_height()))";
case ValidTextureOverload::kDimensions3d: case ValidTextureOverload::kDimensions3d:
case ValidTextureOverload::kDimensionsCube: case ValidTextureOverload::kDimensionsCube:
case ValidTextureOverload::kDimensionsCubeArray: case ValidTextureOverload::kDimensionsCubeArray:
@ -58,194 +57,194 @@ std::string expected_texture_overload(
case ValidTextureOverload::kDimensionsDepthCubeArray: case ValidTextureOverload::kDimensionsDepthCubeArray:
case ValidTextureOverload::kDimensionsStorageRO3d: case ValidTextureOverload::kDimensionsStorageRO3d:
case ValidTextureOverload::kDimensionsStorageWO3d: case ValidTextureOverload::kDimensionsStorageWO3d:
return R"(int3(test_texture.get_width(), test_texture.get_height(), test_texture.get_depth()))"; return R"(int3(texture_tint_0.get_width(), texture_tint_0.get_height(), texture_tint_0.get_depth()))";
case ValidTextureOverload::kDimensions2dLevel: case ValidTextureOverload::kDimensions2dLevel:
case ValidTextureOverload::kDimensions2dArrayLevel: case ValidTextureOverload::kDimensions2dArrayLevel:
case ValidTextureOverload::kDimensionsDepth2dLevel: case ValidTextureOverload::kDimensionsDepth2dLevel:
case ValidTextureOverload::kDimensionsDepth2dArrayLevel: case ValidTextureOverload::kDimensionsDepth2dArrayLevel:
return R"(int2(test_texture.get_width(1), test_texture.get_height(1)))"; return R"(int2(texture_tint_0.get_width(1), texture_tint_0.get_height(1)))";
case ValidTextureOverload::kDimensions3dLevel: case ValidTextureOverload::kDimensions3dLevel:
case ValidTextureOverload::kDimensionsCubeLevel: case ValidTextureOverload::kDimensionsCubeLevel:
case ValidTextureOverload::kDimensionsCubeArrayLevel: case ValidTextureOverload::kDimensionsCubeArrayLevel:
case ValidTextureOverload::kDimensionsDepthCubeLevel: case ValidTextureOverload::kDimensionsDepthCubeLevel:
case ValidTextureOverload::kDimensionsDepthCubeArrayLevel: case ValidTextureOverload::kDimensionsDepthCubeArrayLevel:
return R"(int3(test_texture.get_width(1), test_texture.get_height(1), test_texture.get_depth(1)))"; return R"(int3(texture_tint_0.get_width(1), texture_tint_0.get_height(1), texture_tint_0.get_depth(1)))";
case ValidTextureOverload::kSample1dF32: case ValidTextureOverload::kSample1dF32:
return R"(test_texture.sample(test_sampler, 1.0f))"; return R"(texture_tint_0.sample(sampler_tint_0, 1.0f))";
case ValidTextureOverload::kSample1dArrayF32: case ValidTextureOverload::kSample1dArrayF32:
return R"(test_texture.sample(test_sampler, 1.0f, 2))"; return R"(texture_tint_0.sample(sampler_tint_0, 1.0f, 2))";
case ValidTextureOverload::kSample2dF32: case ValidTextureOverload::kSample2dF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f)))";
case ValidTextureOverload::kSample2dOffsetF32: case ValidTextureOverload::kSample2dOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), int2(3, 4)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), int2(3, 4)))";
case ValidTextureOverload::kSample2dArrayF32: case ValidTextureOverload::kSample2dArrayF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3))";
case ValidTextureOverload::kSample2dArrayOffsetF32: case ValidTextureOverload::kSample2dArrayOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, int2(4, 5)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, int2(4, 5)))";
case ValidTextureOverload::kSample3dF32: case ValidTextureOverload::kSample3dF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSample3dOffsetF32: case ValidTextureOverload::kSample3dOffsetF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), int3(4, 5, 6)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), int3(4, 5, 6)))";
case ValidTextureOverload::kSampleCubeF32: case ValidTextureOverload::kSampleCubeF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSampleCubeArrayF32: case ValidTextureOverload::kSampleCubeArrayF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), 4))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4))";
case ValidTextureOverload::kSampleDepth2dF32: case ValidTextureOverload::kSampleDepth2dF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f)))";
case ValidTextureOverload::kSampleDepth2dOffsetF32: case ValidTextureOverload::kSampleDepth2dOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), int2(3, 4)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), int2(3, 4)))";
case ValidTextureOverload::kSampleDepth2dArrayF32: case ValidTextureOverload::kSampleDepth2dArrayF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3))";
case ValidTextureOverload::kSampleDepth2dArrayOffsetF32: case ValidTextureOverload::kSampleDepth2dArrayOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, int2(4, 5)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, int2(4, 5)))";
case ValidTextureOverload::kSampleDepthCubeF32: case ValidTextureOverload::kSampleDepthCubeF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f)))";
case ValidTextureOverload::kSampleDepthCubeArrayF32: case ValidTextureOverload::kSampleDepthCubeArrayF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), 4))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4))";
case ValidTextureOverload::kSampleBias2dF32: case ValidTextureOverload::kSampleBias2dF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), bias(3.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), bias(3.0f)))";
case ValidTextureOverload::kSampleBias2dOffsetF32: case ValidTextureOverload::kSampleBias2dOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), bias(3.0f), int2(4, 5)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), bias(3.0f), int2(4, 5)))";
case ValidTextureOverload::kSampleBias2dArrayF32: case ValidTextureOverload::kSampleBias2dArrayF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 4, bias(3.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 4, bias(3.0f)))";
case ValidTextureOverload::kSampleBias2dArrayOffsetF32: case ValidTextureOverload::kSampleBias2dArrayOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, bias(4.0f), int2(5, 6)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, bias(4.0f), int2(5, 6)))";
case ValidTextureOverload::kSampleBias3dF32: case ValidTextureOverload::kSampleBias3dF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))";
case ValidTextureOverload::kSampleBias3dOffsetF32: case ValidTextureOverload::kSampleBias3dOffsetF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), bias(4.0f), int3(5, 6, 7)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), bias(4.0f), int3(5, 6, 7)))";
case ValidTextureOverload::kSampleBiasCubeF32: case ValidTextureOverload::kSampleBiasCubeF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), bias(4.0f)))";
case ValidTextureOverload::kSampleBiasCubeArrayF32: case ValidTextureOverload::kSampleBiasCubeArrayF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), 3, bias(4.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 3, bias(4.0f)))";
case ValidTextureOverload::kSampleLevel2dF32: case ValidTextureOverload::kSampleLevel2dF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), level(3.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3.0f)))";
case ValidTextureOverload::kSampleLevel2dOffsetF32: case ValidTextureOverload::kSampleLevel2dOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), level(3.0f), int2(4, 5)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3.0f), int2(4, 5)))";
case ValidTextureOverload::kSampleLevel2dArrayF32: case ValidTextureOverload::kSampleLevel2dArrayF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, level(4.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4.0f)))";
case ValidTextureOverload::kSampleLevel2dArrayOffsetF32: case ValidTextureOverload::kSampleLevel2dArrayOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, level(4.0f), int2(5, 6)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4.0f), int2(5, 6)))";
case ValidTextureOverload::kSampleLevel3dF32: case ValidTextureOverload::kSampleLevel3dF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), level(4.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4.0f)))";
case ValidTextureOverload::kSampleLevel3dOffsetF32: case ValidTextureOverload::kSampleLevel3dOffsetF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), level(4.0f), int3(5, 6, 7)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4.0f), int3(5, 6, 7)))";
case ValidTextureOverload::kSampleLevelCubeF32: case ValidTextureOverload::kSampleLevelCubeF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), level(4.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4.0f)))";
case ValidTextureOverload::kSampleLevelCubeArrayF32: case ValidTextureOverload::kSampleLevelCubeArrayF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), 4, level(5.0f)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, level(5.0f)))";
case ValidTextureOverload::kSampleLevelDepth2dF32: case ValidTextureOverload::kSampleLevelDepth2dF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), level(3)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3)))";
case ValidTextureOverload::kSampleLevelDepth2dOffsetF32: case ValidTextureOverload::kSampleLevelDepth2dOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), level(3), int2(4, 5)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), level(3), int2(4, 5)))";
case ValidTextureOverload::kSampleLevelDepth2dArrayF32: case ValidTextureOverload::kSampleLevelDepth2dArrayF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, level(4)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4)))";
case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32: case ValidTextureOverload::kSampleLevelDepth2dArrayOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, level(4), int2(5, 6)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, level(4), int2(5, 6)))";
case ValidTextureOverload::kSampleLevelDepthCubeF32: case ValidTextureOverload::kSampleLevelDepthCubeF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), level(4)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), level(4)))";
case ValidTextureOverload::kSampleLevelDepthCubeArrayF32: case ValidTextureOverload::kSampleLevelDepthCubeArrayF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), 4, level(5)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, level(5)))";
case ValidTextureOverload::kSampleGrad2dF32: case ValidTextureOverload::kSampleGrad2dF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f))))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f))))";
case ValidTextureOverload::kSampleGrad2dOffsetF32: case ValidTextureOverload::kSampleGrad2dOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f)), int2(7, 8)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), gradient2d(float2(3.0f, 4.0f), float2(5.0f, 6.0f)), int2(7, 8)))";
case ValidTextureOverload::kSampleGrad2dArrayF32: case ValidTextureOverload::kSampleGrad2dArrayF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f))))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f))))";
case ValidTextureOverload::kSampleGrad2dArrayOffsetF32: case ValidTextureOverload::kSampleGrad2dArrayOffsetF32:
return R"(test_texture.sample(test_sampler, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f)), int2(8, 9)))"; return R"(texture_tint_0.sample(sampler_tint_0, float2(1.0f, 2.0f), 3, gradient2d(float2(4.0f, 5.0f), float2(6.0f, 7.0f)), int2(8, 9)))";
case ValidTextureOverload::kSampleGrad3dF32: case ValidTextureOverload::kSampleGrad3dF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))";
case ValidTextureOverload::kSampleGrad3dOffsetF32: case ValidTextureOverload::kSampleGrad3dOffsetF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)), int3(10, 11, 12)))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), gradient3d(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f)), int3(10, 11, 12)))";
case ValidTextureOverload::kSampleGradCubeF32: case ValidTextureOverload::kSampleGradCubeF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), gradientcube(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), gradientcube(float3(4.0f, 5.0f, 6.0f), float3(7.0f, 8.0f, 9.0f))))";
case ValidTextureOverload::kSampleGradCubeArrayF32: case ValidTextureOverload::kSampleGradCubeArrayF32:
return R"(test_texture.sample(test_sampler, float3(1.0f, 2.0f, 3.0f), 4, gradientcube(float3(5.0f, 6.0f, 7.0f), float3(8.0f, 9.0f, 10.0f))))"; return R"(texture_tint_0.sample(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, gradientcube(float3(5.0f, 6.0f, 7.0f), float3(8.0f, 9.0f, 10.0f))))";
case ValidTextureOverload::kSampleGradDepth2dF32: case ValidTextureOverload::kSampleGradDepth2dF32:
return R"(test_texture.sample_compare(test_sampler, float2(1.0f, 2.0f), 3.0f))"; return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 3.0f))";
case ValidTextureOverload::kSampleGradDepth2dOffsetF32: case ValidTextureOverload::kSampleGradDepth2dOffsetF32:
return R"(test_texture.sample_compare(test_sampler, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))"; return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 3.0f, int2(4, 5)))";
case ValidTextureOverload::kSampleGradDepth2dArrayF32: case ValidTextureOverload::kSampleGradDepth2dArrayF32:
return R"(test_texture.sample_compare(test_sampler, float2(1.0f, 2.0f), 4, 3.0f))"; return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 4, 3.0f))";
case ValidTextureOverload::kSampleGradDepth2dArrayOffsetF32: case ValidTextureOverload::kSampleGradDepth2dArrayOffsetF32:
return R"(test_texture.sample_compare(test_sampler, float2(1.0f, 2.0f), 4, 3.0f, int2(5, 6)))"; return R"(texture_tint_0.sample_compare(sampler_tint_0, float2(1.0f, 2.0f), 4, 3.0f, int2(5, 6)))";
case ValidTextureOverload::kSampleGradDepthCubeF32: case ValidTextureOverload::kSampleGradDepthCubeF32:
return R"(test_texture.sample_compare(test_sampler, float3(1.0f, 2.0f, 3.0f), 4.0f))"; return R"(texture_tint_0.sample_compare(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4.0f))";
case ValidTextureOverload::kSampleGradDepthCubeArrayF32: case ValidTextureOverload::kSampleGradDepthCubeArrayF32:
return R"(test_texture.sample_compare(test_sampler, float3(1.0f, 2.0f, 3.0f), 4, 5.0f))"; return R"(texture_tint_0.sample_compare(sampler_tint_0, float3(1.0f, 2.0f, 3.0f), 4, 5.0f))";
case ValidTextureOverload::kLoad1dF32: case ValidTextureOverload::kLoad1dF32:
return R"(test_texture.read(1))"; return R"(texture_tint_0.read(1))";
case ValidTextureOverload::kLoad1dU32: case ValidTextureOverload::kLoad1dU32:
return R"(test_texture.read(1))"; return R"(texture_tint_0.read(1))";
case ValidTextureOverload::kLoad1dI32: case ValidTextureOverload::kLoad1dI32:
return R"(test_texture.read(1))"; return R"(texture_tint_0.read(1))";
case ValidTextureOverload::kLoad1dArrayF32: case ValidTextureOverload::kLoad1dArrayF32:
return R"(test_texture.read(1, 2))"; return R"(texture_tint_0.read(1, 2))";
case ValidTextureOverload::kLoad1dArrayU32: case ValidTextureOverload::kLoad1dArrayU32:
return R"(test_texture.read(1, 2))"; return R"(texture_tint_0.read(1, 2))";
case ValidTextureOverload::kLoad1dArrayI32: case ValidTextureOverload::kLoad1dArrayI32:
return R"(test_texture.read(1, 2))"; return R"(texture_tint_0.read(1, 2))";
case ValidTextureOverload::kLoad2dF32: case ValidTextureOverload::kLoad2dF32:
return R"(test_texture.read(int2(1, 2)))"; return R"(texture_tint_0.read(int2(1, 2)))";
case ValidTextureOverload::kLoad2dU32: case ValidTextureOverload::kLoad2dU32:
return R"(test_texture.read(int2(1, 2)))"; return R"(texture_tint_0.read(int2(1, 2)))";
case ValidTextureOverload::kLoad2dI32: case ValidTextureOverload::kLoad2dI32:
return R"(test_texture.read(int2(1, 2)))"; return R"(texture_tint_0.read(int2(1, 2)))";
case ValidTextureOverload::kLoad2dLevelF32: case ValidTextureOverload::kLoad2dLevelF32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dLevelU32: case ValidTextureOverload::kLoad2dLevelU32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dLevelI32: case ValidTextureOverload::kLoad2dLevelI32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dArrayF32: case ValidTextureOverload::kLoad2dArrayF32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dArrayU32: case ValidTextureOverload::kLoad2dArrayU32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dArrayI32: case ValidTextureOverload::kLoad2dArrayI32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoad2dArrayLevelF32: case ValidTextureOverload::kLoad2dArrayLevelF32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoad2dArrayLevelU32: case ValidTextureOverload::kLoad2dArrayLevelU32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoad2dArrayLevelI32: case ValidTextureOverload::kLoad2dArrayLevelI32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoad3dF32: case ValidTextureOverload::kLoad3dF32:
return R"(test_texture.read(int3(1, 2, 3)))"; return R"(texture_tint_0.read(int3(1, 2, 3)))";
case ValidTextureOverload::kLoad3dU32: case ValidTextureOverload::kLoad3dU32:
return R"(test_texture.read(int3(1, 2, 3)))"; return R"(texture_tint_0.read(int3(1, 2, 3)))";
case ValidTextureOverload::kLoad3dI32: case ValidTextureOverload::kLoad3dI32:
return R"(test_texture.read(int3(1, 2, 3)))"; return R"(texture_tint_0.read(int3(1, 2, 3)))";
case ValidTextureOverload::kLoad3dLevelF32: case ValidTextureOverload::kLoad3dLevelF32:
return R"(test_texture.read(int3(1, 2, 3), 4))"; return R"(texture_tint_0.read(int3(1, 2, 3), 4))";
case ValidTextureOverload::kLoad3dLevelU32: case ValidTextureOverload::kLoad3dLevelU32:
return R"(test_texture.read(int3(1, 2, 3), 4))"; return R"(texture_tint_0.read(int3(1, 2, 3), 4))";
case ValidTextureOverload::kLoad3dLevelI32: case ValidTextureOverload::kLoad3dLevelI32:
return R"(test_texture.read(int3(1, 2, 3), 4))"; return R"(texture_tint_0.read(int3(1, 2, 3), 4))";
case ValidTextureOverload::kLoadMultisampled2dF32: case ValidTextureOverload::kLoadMultisampled2dF32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadMultisampled2dU32: case ValidTextureOverload::kLoadMultisampled2dU32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadMultisampled2dI32: case ValidTextureOverload::kLoadMultisampled2dI32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadMultisampled2dArrayF32: case ValidTextureOverload::kLoadMultisampled2dArrayF32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadMultisampled2dArrayU32: case ValidTextureOverload::kLoadMultisampled2dArrayU32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadMultisampled2dArrayI32: case ValidTextureOverload::kLoadMultisampled2dArrayI32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadDepth2dF32: case ValidTextureOverload::kLoadDepth2dF32:
return R"(test_texture.read(int2(1, 2)))"; return R"(texture_tint_0.read(int2(1, 2)))";
case ValidTextureOverload::kLoadDepth2dLevelF32: case ValidTextureOverload::kLoadDepth2dLevelF32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadDepth2dArrayF32: case ValidTextureOverload::kLoadDepth2dArrayF32:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadDepth2dArrayLevelF32: case ValidTextureOverload::kLoadDepth2dArrayLevelF32:
return R"(test_texture.read(int2(1, 2), 3, 4))"; return R"(texture_tint_0.read(int2(1, 2), 3, 4))";
case ValidTextureOverload::kLoadStorageRO1dRgba32float: case ValidTextureOverload::kLoadStorageRO1dRgba32float:
return R"(test_texture.read(1))"; return R"(texture_tint_0.read(1))";
case ValidTextureOverload::kLoadStorageRO1dArrayRgba32float: case ValidTextureOverload::kLoadStorageRO1dArrayRgba32float:
return R"(test_texture.read(1, 2))"; return R"(texture_tint_0.read(1, 2))";
case ValidTextureOverload::kLoadStorageRO2dRgba8unorm: case ValidTextureOverload::kLoadStorageRO2dRgba8unorm:
case ValidTextureOverload::kLoadStorageRO2dRgba8snorm: case ValidTextureOverload::kLoadStorageRO2dRgba8snorm:
case ValidTextureOverload::kLoadStorageRO2dRgba8uint: case ValidTextureOverload::kLoadStorageRO2dRgba8uint:
@ -262,21 +261,21 @@ std::string expected_texture_overload(
case ValidTextureOverload::kLoadStorageRO2dRgba32uint: case ValidTextureOverload::kLoadStorageRO2dRgba32uint:
case ValidTextureOverload::kLoadStorageRO2dRgba32sint: case ValidTextureOverload::kLoadStorageRO2dRgba32sint:
case ValidTextureOverload::kLoadStorageRO2dRgba32float: case ValidTextureOverload::kLoadStorageRO2dRgba32float:
return R"(test_texture.read(int2(1, 2)))"; return R"(texture_tint_0.read(int2(1, 2)))";
case ValidTextureOverload::kLoadStorageRO2dArrayRgba32float: case ValidTextureOverload::kLoadStorageRO2dArrayRgba32float:
return R"(test_texture.read(int2(1, 2), 3))"; return R"(texture_tint_0.read(int2(1, 2), 3))";
case ValidTextureOverload::kLoadStorageRO3dRgba32float: case ValidTextureOverload::kLoadStorageRO3dRgba32float:
return R"(test_texture.read(int3(1, 2, 3)))"; return R"(texture_tint_0.read(int3(1, 2, 3)))";
case ValidTextureOverload::kStoreWO1dRgba32float: case ValidTextureOverload::kStoreWO1dRgba32float:
return R"(test_texture.write(float4(2.0f, 3.0f, 4.0f, 5.0f), 1))"; return R"(texture_tint_0.write(float4(2.0f, 3.0f, 4.0f, 5.0f), 1))";
case ValidTextureOverload::kStoreWO1dArrayRgba32float: case ValidTextureOverload::kStoreWO1dArrayRgba32float:
return R"(test_texture.write(float4(3.0f, 4.0f, 5.0f, 6.0f), 1, 2))"; return R"(texture_tint_0.write(float4(3.0f, 4.0f, 5.0f, 6.0f), 1, 2))";
case ValidTextureOverload::kStoreWO2dRgba32float: case ValidTextureOverload::kStoreWO2dRgba32float:
return R"(test_texture.write(float4(3.0f, 4.0f, 5.0f, 6.0f), int2(1, 2)))"; return R"(texture_tint_0.write(float4(3.0f, 4.0f, 5.0f, 6.0f), int2(1, 2)))";
case ValidTextureOverload::kStoreWO2dArrayRgba32float: case ValidTextureOverload::kStoreWO2dArrayRgba32float:
return R"(test_texture.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int2(1, 2), 3))"; return R"(texture_tint_0.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int2(1, 2), 3))";
case ValidTextureOverload::kStoreWO3dRgba32float: case ValidTextureOverload::kStoreWO3dRgba32float:
return R"(test_texture.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int3(1, 2, 3)))"; return R"(texture_tint_0.write(float4(4.0f, 5.0f, 6.0f, 7.0f), int3(1, 2, 3)))";
} }
return "<unmatched texture overload>"; return "<unmatched texture overload>";
} // NOLINT - Ignore the length of this function } // NOLINT - Ignore the length of this function
@ -291,10 +290,8 @@ class MslGeneratorIntrinsicTextureTest
/// The type determiner /// The type determiner
TypeDeterminer td{mod}; TypeDeterminer td{mod};
/// The namer
TestNamer namer{mod};
/// The generator /// The generator
GeneratorImpl gen{mod, &namer}; GeneratorImpl gen{mod};
}; };
TEST_P(MslGeneratorIntrinsicTextureTest, Call) { TEST_P(MslGeneratorIntrinsicTextureTest, Call) {

View File

@ -102,7 +102,7 @@ TEST_F(MslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
bool tint_msl_is_first_1 = true; bool tint_msl_is_first_1 = true;
for(;;) { for(;;) {
if (!tint_msl_is_first_1) { if (!tint_msl_is_first_1) {
test_lhs = test_rhs; lhs = rhs;
} }
tint_msl_is_first_1 = false; tint_msl_is_first_1 = false;
@ -162,16 +162,16 @@ TEST_F(MslGeneratorImplTest, Emit_LoopWithVarUsedInContinuing) {
ASSERT_TRUE(gen.EmitStatement(outer)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(outer)) << gen.error();
EXPECT_EQ(gen.result(), R"( { EXPECT_EQ(gen.result(), R"( {
bool tint_msl_is_first_1 = true; bool tint_msl_is_first_1 = true;
float test_lhs; float lhs;
float test_other; float other;
for(;;) { for(;;) {
if (!tint_msl_is_first_1) { if (!tint_msl_is_first_1) {
test_lhs = test_rhs; lhs = rhs;
} }
tint_msl_is_first_1 = false; tint_msl_is_first_1 = false;
test_lhs = 2.400000095f; lhs = 2.400000095f;
test_other = 0.0f; other = 0.0f;
} }
} }
)"); )");

View File

@ -32,29 +32,7 @@ TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) {
auto* expr = MemberAccessor("str", "mem"); auto* expr = MemberAccessor("str", "mem");
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "test_str.test_mem"); EXPECT_EQ(gen.result(), "str.mem");
}
TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor_Swizzle_xyz) {
auto* vec = Var("my_vec", ast::StorageClass::kPrivate, ty.vec4<f32>());
td.RegisterVariableForTesting(vec);
mod->AddGlobalVariable(vec);
auto* expr = MemberAccessor("my_vec", "xyz");
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "test_my_vec.xyz");
}
TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor_Swizzle_gbr) {
auto* vec = Var("my_vec", ast::StorageClass::kPrivate, ty.vec4<f32>());
td.RegisterVariableForTesting(vec);
mod->AddGlobalVariable(vec);
auto* expr = MemberAccessor("my_vec", "gbr");
ASSERT_TRUE(td.DetermineResultType(expr)) << td.error();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "test_my_vec.gbr");
} }
} // namespace } // namespace

View File

@ -40,7 +40,7 @@ TEST_F(MslGeneratorImplTest, Emit_ModuleConstant) {
array<f32, 3>(1.f, 2.f, 3.f), ast::VariableDecorationList{}); array<f32, 3>(1.f, 2.f, 3.f), ast::VariableDecorationList{});
ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error(); ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
EXPECT_EQ(gen.result(), "constant float test_pos[3] = {1.0f, 2.0f, 3.0f};\n"); EXPECT_EQ(gen.result(), "constant float pos[3] = {1.0f, 2.0f, 3.0f};\n");
} }
TEST_F(MslGeneratorImplTest, Emit_SpecConstant) { TEST_F(MslGeneratorImplTest, Emit_SpecConstant) {
@ -50,8 +50,7 @@ TEST_F(MslGeneratorImplTest, Emit_SpecConstant) {
}); });
ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error(); ASSERT_TRUE(gen.EmitProgramConstVariable(var)) << gen.error();
EXPECT_EQ(gen.result(), EXPECT_EQ(gen.result(), "constant float pos [[function_constant(23)]];\n");
"constant float test_pos [[function_constant(23)]];\n");
} }
} // namespace } // namespace

View File

@ -42,7 +42,7 @@ TEST_F(MslGeneratorImplTest, Emit_ReturnWithValue) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(r)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(r)) << gen.error();
EXPECT_EQ(gen.result(), " return test_expr;\n"); EXPECT_EQ(gen.result(), " return expr;\n");
} }
} // namespace } // namespace

View File

@ -55,7 +55,7 @@ TEST_F(MslGeneratorImplTest, Emit_Switch) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(s)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(s)) << gen.error();
EXPECT_EQ(gen.result(), R"( switch(test_cond) { EXPECT_EQ(gen.result(), R"( switch(cond) {
case 5: { case 5: {
break; break;
} }

View File

@ -37,6 +37,7 @@
#include "src/ast/type/u32_type.h" #include "src/ast/type/u32_type.h"
#include "src/ast/type/vector_type.h" #include "src/ast/type/vector_type.h"
#include "src/ast/type/void_type.h" #include "src/ast/type/void_type.h"
#include "src/writer/msl/namer.h"
#include "src/writer/msl/test_helper.h" #include "src/writer/msl/test_helper.h"
namespace tint { namespace tint {
@ -57,12 +58,27 @@ TEST_F(MslGeneratorImplTest, Generate) {
ASSERT_TRUE(gen.Generate()) << gen.error(); ASSERT_TRUE(gen.Generate()) << gen.error();
EXPECT_EQ(gen.result(), R"(#include <metal_stdlib> EXPECT_EQ(gen.result(), R"(#include <metal_stdlib>
kernel void test_my_func() { kernel void my_func() {
} }
)"); )");
} }
TEST_F(MslGeneratorImplTest, InputStructName) {
ASSERT_EQ(gen.generate_name("func_main_in"), "func_main_in");
}
TEST_F(MslGeneratorImplTest, InputStructName_ConflictWithExisting) {
gen.namer_for_testing()->NameFor("func_main_out");
ASSERT_EQ(gen.generate_name("func_main_out"), "func_main_out_0");
}
TEST_F(MslGeneratorImplTest, NameConflictWith_InputStructName) {
ASSERT_EQ(gen.generate_name("func_main_in"), "func_main_in");
ASSERT_TRUE(gen.EmitIdentifier(Expr("func_main_in")));
EXPECT_EQ(gen.result(), "func_main_in_0");
}
struct MslBuiltinData { struct MslBuiltinData {
ast::Builtin builtin; ast::Builtin builtin;
const char* attribute_name; const char* attribute_name;

View File

@ -48,22 +48,26 @@ using MslGeneratorImplTest = TestHelper;
TEST_F(MslGeneratorImplTest, EmitType_Alias) { TEST_F(MslGeneratorImplTest, EmitType_Alias) {
auto* alias = ty.alias("alias", ty.f32); auto* alias = ty.alias("alias", ty.f32);
ASSERT_TRUE(gen.EmitType(alias, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(alias, "")) << gen.error();
EXPECT_EQ(gen.result(), "test_alias"); EXPECT_EQ(gen.result(), "alias");
}
TEST_F(MslGeneratorImplTest, EmitType_Alias_NameCollision) {
auto* alias = ty.alias("bool", ty.f32);
ASSERT_TRUE(gen.EmitType(alias, "")) << gen.error();
EXPECT_EQ(gen.result(), "bool_tint_0");
} }
TEST_F(MslGeneratorImplTest, EmitType_Array) { TEST_F(MslGeneratorImplTest, EmitType_Array) {
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(ty.array<bool, 4>(), "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(ty.array<bool, 4>(), sym)) << gen.error(); EXPECT_EQ(gen.result(), "bool ary[4]");
EXPECT_EQ(gen.result(), "bool test_ary[4]");
} }
TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArray) { TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArray) {
auto* a = ty.array<bool, 4>(); auto* a = ty.array<bool, 4>();
auto* b = ty.array(a, 5); auto* b = ty.array(a, 5);
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(b, "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(b, sym)) << gen.error(); EXPECT_EQ(gen.result(), "bool ary[5][4]");
EXPECT_EQ(gen.result(), "bool test_ary[5][4]");
} }
// TODO(dsinclair): Is this possible? What order should it output in? // TODO(dsinclair): Is this possible? What order should it output in?
@ -71,48 +75,56 @@ TEST_F(MslGeneratorImplTest, DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) {
auto* a = ty.array<bool, 4>(); auto* a = ty.array<bool, 4>();
auto* b = ty.array(a, 5); auto* b = ty.array(a, 5);
auto* c = ty.array(b, 0); auto* c = ty.array(b, 0);
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(c, "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(c, sym)) << gen.error(); EXPECT_EQ(gen.result(), "bool ary[5][4][1]");
EXPECT_EQ(gen.result(), "bool test_ary[5][4][1]");
} }
TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) { TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) {
auto* a = ty.array<bool, 4>(); auto* a = ty.array<bool, 4>();
auto* b = ty.array(a, 5); auto* b = ty.array(a, 5);
auto* c = ty.array(b, 6); auto* c = ty.array(b, 6);
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(c, "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(c, sym)) << gen.error(); EXPECT_EQ(gen.result(), "bool ary[6][5][4]");
EXPECT_EQ(gen.result(), "bool test_ary[6][5][4]"); }
TEST_F(MslGeneratorImplTest, EmitType_Array_NameCollision) {
ASSERT_TRUE(gen.EmitType(ty.array<bool, 4>(), "bool")) << gen.error();
EXPECT_EQ(gen.result(), "bool bool_tint_0[4]");
} }
TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) { TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) {
ASSERT_TRUE(gen.EmitType(ty.array<bool, 4>(), Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.array<bool, 4>(), "")) << gen.error();
EXPECT_EQ(gen.result(), "bool[4]"); EXPECT_EQ(gen.result(), "bool[4]");
} }
TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray) { TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray) {
auto sym = mod->RegisterSymbol("ary"); ASSERT_TRUE(gen.EmitType(ty.array<bool, 1>(), "ary")) << gen.error();
ASSERT_TRUE(gen.EmitType(ty.array<bool, 1>(), sym)) << gen.error(); EXPECT_EQ(gen.result(), "bool ary[1]");
EXPECT_EQ(gen.result(), "bool test_ary[1]"); }
TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray_NameCollision) {
ASSERT_TRUE(gen.EmitType(ty.array<bool, 1>(), "discard_fragment"))
<< gen.error();
EXPECT_EQ(gen.result(), "bool discard_fragment_tint_0[1]");
} }
TEST_F(MslGeneratorImplTest, EmitType_Bool) { TEST_F(MslGeneratorImplTest, EmitType_Bool) {
ASSERT_TRUE(gen.EmitType(ty.bool_, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.bool_, "")) << gen.error();
EXPECT_EQ(gen.result(), "bool"); EXPECT_EQ(gen.result(), "bool");
} }
TEST_F(MslGeneratorImplTest, EmitType_F32) { TEST_F(MslGeneratorImplTest, EmitType_F32) {
ASSERT_TRUE(gen.EmitType(ty.f32, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.f32, "")) << gen.error();
EXPECT_EQ(gen.result(), "float"); EXPECT_EQ(gen.result(), "float");
} }
TEST_F(MslGeneratorImplTest, EmitType_I32) { TEST_F(MslGeneratorImplTest, EmitType_I32) {
ASSERT_TRUE(gen.EmitType(ty.i32, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.i32, "")) << gen.error();
EXPECT_EQ(gen.result(), "int"); EXPECT_EQ(gen.result(), "int");
} }
TEST_F(MslGeneratorImplTest, EmitType_Matrix) { TEST_F(MslGeneratorImplTest, EmitType_Matrix) {
ASSERT_TRUE(gen.EmitType(ty.mat2x3<f32>(), Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.mat2x3<f32>(), "")) << gen.error();
EXPECT_EQ(gen.result(), "float2x3"); EXPECT_EQ(gen.result(), "float2x3");
} }
@ -120,7 +132,7 @@ TEST_F(MslGeneratorImplTest, EmitType_Matrix) {
TEST_F(MslGeneratorImplTest, DISABLED_EmitType_Pointer) { TEST_F(MslGeneratorImplTest, DISABLED_EmitType_Pointer) {
ast::type::Pointer p(ty.f32, ast::StorageClass::kWorkgroup); ast::type::Pointer p(ty.f32, ast::StorageClass::kWorkgroup);
ASSERT_TRUE(gen.EmitType(&p, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&p, "")) << gen.error();
EXPECT_EQ(gen.result(), "float*"); EXPECT_EQ(gen.result(), "float*");
} }
@ -131,8 +143,8 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct) {
ast::StructDecorationList{}); ast::StructDecorationList{});
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitType(s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(s, "")) << gen.error();
EXPECT_EQ(gen.result(), "test_S"); EXPECT_EQ(gen.result(), "S");
} }
TEST_F(MslGeneratorImplTest, EmitType_StructDecl) { TEST_F(MslGeneratorImplTest, EmitType_StructDecl) {
@ -144,9 +156,9 @@ TEST_F(MslGeneratorImplTest, EmitType_StructDecl) {
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitStructType(s)) << gen.error(); ASSERT_TRUE(gen.EmitStructType(s)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_S { EXPECT_EQ(gen.result(), R"(struct S {
int test_a; int a;
float test_b; float b;
}; };
)"); )");
} }
@ -162,13 +174,27 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct_InjectPadding) {
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitStructType(s)) << gen.error(); ASSERT_TRUE(gen.EmitStructType(s)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_S { EXPECT_EQ(gen.result(), R"(struct S {
int8_t pad_0[4]; int8_t pad_0[4];
int test_a; int a;
int8_t pad_1[24]; int8_t pad_1[24];
float test_b; float b;
int8_t pad_2[92]; int8_t pad_2[92];
float test_c; float c;
};
)");
}
TEST_F(MslGeneratorImplTest, EmitType_Struct_NameCollision) {
auto* str = create<ast::Struct>(
ast::StructMemberList{Member("main", ty.i32), Member("float", ty.f32)},
ast::StructDecorationList{});
auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitStructType(s)) << gen.error();
EXPECT_EQ(gen.result(), R"(struct S {
int main_tint_0;
float float_tint_0;
}; };
)"); )");
} }
@ -183,39 +209,39 @@ TEST_F(MslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) {
decos); decos);
auto* s = ty.struct_("S", str); auto* s = ty.struct_("S", str);
ASSERT_TRUE(gen.EmitType(s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(s, "")) << gen.error();
EXPECT_EQ(gen.result(), R"(struct test_S { EXPECT_EQ(gen.result(), R"(struct {
int test_a; int a;
float test_b; float b;
})"); })");
} }
TEST_F(MslGeneratorImplTest, EmitType_U32) { TEST_F(MslGeneratorImplTest, EmitType_U32) {
ASSERT_TRUE(gen.EmitType(ty.u32, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.u32, "")) << gen.error();
EXPECT_EQ(gen.result(), "uint"); EXPECT_EQ(gen.result(), "uint");
} }
TEST_F(MslGeneratorImplTest, EmitType_Vector) { TEST_F(MslGeneratorImplTest, EmitType_Vector) {
ASSERT_TRUE(gen.EmitType(ty.vec3<f32>(), Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.vec3<f32>(), "")) << gen.error();
EXPECT_EQ(gen.result(), "float3"); EXPECT_EQ(gen.result(), "float3");
} }
TEST_F(MslGeneratorImplTest, EmitType_Void) { TEST_F(MslGeneratorImplTest, EmitType_Void) {
ASSERT_TRUE(gen.EmitType(ty.void_, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(ty.void_, "")) << gen.error();
EXPECT_EQ(gen.result(), "void"); EXPECT_EQ(gen.result(), "void");
} }
TEST_F(MslGeneratorImplTest, EmitType_Sampler) { TEST_F(MslGeneratorImplTest, EmitType_Sampler) {
ast::type::Sampler sampler(ast::type::SamplerKind::kSampler); ast::type::Sampler sampler(ast::type::SamplerKind::kSampler);
ASSERT_TRUE(gen.EmitType(&sampler, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&sampler, "")) << gen.error();
EXPECT_EQ(gen.result(), "sampler"); EXPECT_EQ(gen.result(), "sampler");
} }
TEST_F(MslGeneratorImplTest, EmitType_SamplerComparison) { TEST_F(MslGeneratorImplTest, EmitType_SamplerComparison) {
ast::type::Sampler sampler(ast::type::SamplerKind::kComparisonSampler); ast::type::Sampler sampler(ast::type::SamplerKind::kComparisonSampler);
ASSERT_TRUE(gen.EmitType(&sampler, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&sampler, "")) << gen.error();
EXPECT_EQ(gen.result(), "sampler"); EXPECT_EQ(gen.result(), "sampler");
} }
@ -233,7 +259,7 @@ TEST_P(MslDepthTexturesTest, Emit) {
ast::type::DepthTexture s(params.dim); ast::type::DepthTexture s(params.dim);
ASSERT_TRUE(gen.EmitType(&s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&s, "")) << gen.error();
EXPECT_EQ(gen.result(), params.result); EXPECT_EQ(gen.result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
@ -263,7 +289,7 @@ TEST_P(MslSampledtexturesTest, Emit) {
ast::type::SampledTexture s(params.dim, ty.f32); ast::type::SampledTexture s(params.dim, ty.f32);
ASSERT_TRUE(gen.EmitType(&s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&s, "")) << gen.error();
EXPECT_EQ(gen.result(), params.result); EXPECT_EQ(gen.result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
@ -288,7 +314,7 @@ INSTANTIATE_TEST_SUITE_P(
TEST_F(MslGeneratorImplTest, Emit_TypeMultisampledTexture) { TEST_F(MslGeneratorImplTest, Emit_TypeMultisampledTexture) {
ast::type::MultisampledTexture s(ast::type::TextureDimension::k2d, ty.u32); ast::type::MultisampledTexture s(ast::type::TextureDimension::k2d, ty.u32);
ASSERT_TRUE(gen.EmitType(&s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&s, "")) << gen.error();
EXPECT_EQ(gen.result(), "texture2d_ms<uint, access::sample>"); EXPECT_EQ(gen.result(), "texture2d_ms<uint, access::sample>");
} }
@ -311,7 +337,7 @@ TEST_P(MslStorageTexturesTest, Emit) {
ast::type::ImageFormat::kR16Float); ast::type::ImageFormat::kR16Float);
ASSERT_TRUE(td.DetermineStorageTextureSubtype(&s)) << td.error(); ASSERT_TRUE(td.DetermineStorageTextureSubtype(&s)) << td.error();
ASSERT_TRUE(gen.EmitType(&s, Symbol())) << gen.error(); ASSERT_TRUE(gen.EmitType(&s, "")) << gen.error();
EXPECT_EQ(gen.result(), params.result); EXPECT_EQ(gen.result(), params.result);
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(

View File

@ -40,7 +40,7 @@ TEST_P(MslUnaryOpTest, Emit) {
auto params = GetParam(); auto params = GetParam();
auto* op = create<ast::UnaryOpExpression>(params.op, Expr("expr")); auto* op = create<ast::UnaryOpExpression>(params.op, Expr("expr"));
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), std::string(params.name) + "(test_expr)"); EXPECT_EQ(gen.result(), std::string(params.name) + "(expr)");
} }
INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest, INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest,
MslUnaryOpTest, MslUnaryOpTest,

View File

@ -47,7 +47,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " float test_a = 0.0f;\n"); EXPECT_EQ(gen.result(), " float a = 0.0f;\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
@ -57,7 +57,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " const float test_a = 0.0f;\n"); EXPECT_EQ(gen.result(), " const float a = 0.0f;\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
@ -69,7 +69,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " float test_a[5] = {0.0f};\n"); EXPECT_EQ(gen.result(), " float a[5] = {0.0f};\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Struct) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Struct) {
@ -85,7 +85,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Struct) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), R"( test_S test_a = {}; EXPECT_EQ(gen.result(), R"( S a = {};
)"); )");
} }
@ -96,7 +96,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Vector) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " float2 test_a = 0.0f;\n"); EXPECT_EQ(gen.result(), " float2 a = 0.0f;\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Matrix) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Matrix) {
@ -107,7 +107,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Matrix) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " float3x2 test_a = 0.0f;\n"); EXPECT_EQ(gen.result(), " float3x2 a = 0.0f;\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
@ -117,7 +117,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), " float test_a = 0.0f;\n"); EXPECT_EQ(gen.result(), " float a = 0.0f;\n");
} }
TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) { TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
@ -126,7 +126,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), R"(float test_a = test_initializer; EXPECT_EQ(gen.result(), R"(float a = initializer;
)"); )");
} }
@ -138,7 +138,7 @@ TEST_F(MslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
auto* stmt = create<ast::VariableDeclStatement>(var); auto* stmt = create<ast::VariableDeclStatement>(var);
ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error(); ASSERT_TRUE(gen.EmitStatement(stmt)) << gen.error();
EXPECT_EQ(gen.result(), R"(float3 test_a = float3(0.0f); EXPECT_EQ(gen.result(), R"(float3 a = float3(0.0f);
)"); )");
} }

323
src/writer/msl/namer.cc Normal file
View File

@ -0,0 +1,323 @@
// 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 "src/writer/msl/namer.h"
#include <algorithm>
namespace tint {
namespace writer {
namespace msl {
namespace {
const char* kNames[] = {"access",
"alignas",
"alignof",
"and",
"and_eq",
"array",
"array_ref",
"as_type",
"asm",
"atomic",
"atomic_bool",
"atomic_int",
"atomic_uint",
"auto",
"bitand",
"bitor",
"bool",
"bool2",
"bool3",
"bool4",
"break",
"buffer",
"case",
"catch",
"char",
"char16_t",
"char2",
"char3",
"char32_t",
"char4",
"class",
"compl",
"const",
"const_cast",
"const_reference",
"constant",
"constexpr",
"continue",
"decltype",
"default",
"delete",
"depth2d",
"depth2d_array",
"depth2d_ms",
"depth2d_ms_array",
"depthcube",
"depthcube_array",
"device",
"discard_fragment",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"extern",
"extern",
"false",
"final",
"float",
"float2",
"float2x2",
"float2x3",
"float2x4",
"float3",
"float3x2",
"float3x3",
"float3x4",
"float4",
"float4x2",
"float4x3",
"float4x4",
"for",
"fragment",
"friend",
"goto",
"half",
"half2",
"half2x2",
"half2x3",
"half2x4",
"half3",
"half3x2",
"half3x3",
"half3x4",
"half4",
"half4x2",
"half4x3",
"half4x4",
"if",
"imageblock",
"inline",
"inline",
"int",
"int16_t",
"int2",
"int3",
"int32_t",
"int4",
"int64_t",
"int8_t",
"kernel",
"long",
"long2",
"long3",
"long4",
"main",
"metal",
"mutable"
"mutable",
"namespace",
"new",
"noexcept"
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
"packed_bool2",
"packed_bool3",
"packed_bool4",
"packed_char2",
"packed_char3",
"packed_char4",
"packed_float2",
"packed_float3",
"packed_float4",
"packed_half2",
"packed_half3",
"packed_half4",
"packed_int2",
"packed_int3",
"packed_int4",
"packed_short2",
"packed_short3",
"packed_short4",
"packed_uchar2",
"packed_uchar3",
"packed_uchar4",
"packed_uint2",
"packed_uint3",
"packed_uint4",
"packed_ushort2",
"packed_ushort3",
"packed_ushort4",
"patch_control_point",
"private",
"protected",
"ptrdiff_t",
"public",
"r16snorm",
"r16unorm",
"r8unorm",
"reference",
"register",
"reinterpret_cast",
"return",
"rg11b10f",
"rg16snorm",
"rg16unorm",
"rg8snorm",
"rg8unorm",
"rgb10a2",
"rgb9e5",
"rgba16snorm",
"rgba16unorm",
"rgba8snorm",
"rgba8unorm",
"sampler",
"short",
"short2",
"short3",
"short4",
"signed",
"size_t",
"sizeof",
"srgba8unorm",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"texture",
"texture1d",
"texture1d_array",
"texture2d",
"texture2d_array",
"texture2d_ms",
"texture2d_ms_array",
"texture3d",
"texture_buffer",
"texturecube",
"texturecube_array",
"this",
"thread",
"thread_local",
"threadgroup",
"threadgroup_imageblock",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"uchar",
"uchar2",
"uchar3",
"uchar4",
"uint",
"uint16_t",
"uint2",
"uint3",
"uint32_t",
"uint4",
"uint64_t",
"uint8_t",
"ulong2",
"ulong3",
"ulong4",
"uniform",
"union",
"unsigned",
"ushort",
"ushort2",
"ushort3",
"ushort4",
"using",
"vec",
"vertex",
"virtual",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq"};
} // namespace
Namer::Namer() = default;
Namer::~Namer() = default;
std::string Namer::NameFor(const std::string& name) {
// If it's in the name map we can just return it. There are no shadow names
// in WGSL so this has to be unique in the WGSL names, and we've already
// checked the name collisions with MSL.
auto it = name_map_.find(name);
if (it != name_map_.end()) {
return it->second;
}
std::string ret_name = name;
if (std::binary_search(std::begin(kNames), std::end(kNames), ret_name)) {
uint32_t i = 0;
// Make sure there wasn't already a tint variable with the new name we've
// now created.
while (true) {
ret_name = name + "_tint_" + std::to_string(i);
it = name_map_.find(ret_name);
if (it == name_map_.end()) {
break;
}
i++;
}
RegisterRemappedName(ret_name);
} else {
uint32_t i = 0;
// Make sure the ident name wasn't assigned by a remapping.
while (true) {
auto remap_it = remapped_names_.find(ret_name);
if (remap_it == remapped_names_.end()) {
break;
}
ret_name = name + "_" + std::to_string(i);
i++;
}
RegisterRemappedName(ret_name);
}
name_map_[name] = ret_name;
return ret_name;
}
bool Namer::IsMapped(const std::string& name) {
auto it = name_map_.find(name);
return it != name_map_.end();
}
void Namer::RegisterRemappedName(const std::string& name) {
remapped_names_.insert(name);
}
} // namespace msl
} // namespace writer
} // namespace tint

58
src/writer/msl/namer.h Normal file
View File

@ -0,0 +1,58 @@
// 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.
#ifndef SRC_WRITER_MSL_NAMER_H_
#define SRC_WRITER_MSL_NAMER_H_
#include <string>
#include <unordered_map>
#include <unordered_set>
namespace tint {
namespace writer {
namespace msl {
/// Remaps maps names to avoid reserved words and collisions for MSL.
class Namer {
public:
/// Constructor
Namer();
~Namer();
/// Returns a sanitized version of `name`
/// @param name the name to sanitize
/// @returns the sanitized version of `name`
std::string NameFor(const std::string& name);
/// Registers a remapped name.
/// @param name the name to register
void RegisterRemappedName(const std::string& name);
/// Returns if the given name has been mapped alread
/// @param name the name to check
/// @returns true if the name has been mapped
bool IsMapped(const std::string& name);
private:
/// Map of original name to new name. The two names may be the same.
std::unordered_map<std::string, std::string> name_map_;
// The list of names taken by the remapper
std::unordered_set<std::string> remapped_names_;
};
} // namespace msl
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_MSL_NAMER_H_

View File

@ -0,0 +1,304 @@
// 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 "src/writer/msl/namer.h"
#include "gtest/gtest.h"
namespace tint {
namespace writer {
namespace msl {
namespace {
using MslNamerTest = testing::Test;
TEST_F(MslNamerTest, ReturnsName) {
Namer n;
EXPECT_EQ("my_name", n.NameFor("my_name"));
EXPECT_EQ("my_name", n.NameFor("my_name"));
}
TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWordAfterIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_1", n.NameFor("float"));
EXPECT_EQ("float_tint_0", n.NameFor("float_tint_0"));
}
TEST_F(MslNamerTest, HandlesConflictWithRenamedReservedWordBeforeIdentSeen) {
Namer n;
EXPECT_EQ("float_tint_0", n.NameFor("float"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
EXPECT_EQ("float_tint_0_0_0", n.NameFor("float_tint_0_0"));
EXPECT_EQ("float_tint_0_0", n.NameFor("float_tint_0"));
}
using MslReservedNameTest = testing::TestWithParam<std::string>;
TEST_P(MslReservedNameTest, Emit) {
auto name = GetParam();
Namer n;
EXPECT_EQ(name + "_tint_0", n.NameFor(name));
}
INSTANTIATE_TEST_SUITE_P(MslNamerTest,
MslReservedNameTest,
testing::Values(
// c++14 spec
"alignas",
"alignof",
"and",
"and_eq",
"asm",
"auto",
"bitand",
"bitor",
"bool",
"break",
"case",
"catch",
"char",
"char16_t",
"char32_t",
"class",
"compl",
"const",
"const_cast",
"constexpr",
"continue",
"decltype",
"default",
"delete",
"do",
"double",
"dynamic_cast",
"else",
"enum",
"explicit",
"extern",
"extern",
"false",
"final",
"float",
"for",
"friend",
"goto",
"if",
"inline",
"inline",
"int",
"long",
"mutable"
"mutable",
"namespace",
"new",
"noexcept"
"not",
"not_eq",
"nullptr",
"operator",
"or",
"or_eq",
"override",
"private",
"protected",
"public",
"register",
"reinterpret_cast",
"return",
"short",
"signed",
"sizeof",
"static",
"static_assert",
"static_cast",
"struct",
"switch",
"template",
"this",
"thread_local",
"throw",
"true",
"try",
"typedef",
"typeid",
"typename",
"union",
"unsigned",
"using",
"virtual",
"virtual",
"void",
"volatile",
"wchar_t",
"while",
"xor",
"xor_eq",
// Metal Spec
"access",
"array",
"array_ref",
"as_type",
"atomic",
"atomic_bool",
"atomic_int",
"atomic_uint",
"bool2",
"bool3",
"bool4",
"buffer",
"char2",
"char3",
"char4",
"const_reference",
"constant",
"depth2d",
"depth2d_array",
"depth2d_ms",
"depth2d_ms_array",
"depthcube",
"depthcube_array",
"device",
"discard_fragment",
"float2",
"float2x2",
"float2x3",
"float2x4",
"float3",
"float3x2",
"float3x3",
"float3x4",
"float4",
"float4x2",
"float4x3",
"float4x4",
"fragment",
"half",
"half2",
"half2x2",
"half2x3",
"half2x4",
"half3",
"half3x2",
"half3x3",
"half3x4",
"half4",
"half4x2",
"half4x3",
"half4x4",
"imageblock",
"int16_t",
"int2",
"int3",
"int32_t",
"int4",
"int64_t",
"int8_t",
"kernel",
"long2",
"long3",
"long4",
"main", // No functions called main
"metal", // The namespace
"packed_bool2",
"packed_bool3",
"packed_bool4",
"packed_char2",
"packed_char3",
"packed_char4",
"packed_float2",
"packed_float3",
"packed_float4",
"packed_half2",
"packed_half3",
"packed_half4",
"packed_int2",
"packed_int3",
"packed_int4",
"packed_short2",
"packed_short3",
"packed_short4",
"packed_uchar2",
"packed_uchar3",
"packed_uchar4",
"packed_uint2",
"packed_uint3",
"packed_uint4",
"packed_ushort2",
"packed_ushort3",
"packed_ushort4",
"patch_control_point",
"ptrdiff_t",
"r16snorm",
"r16unorm",
"r8unorm",
"reference",
"rg11b10f",
"rg16snorm",
"rg16unorm",
"rg8snorm",
"rg8unorm",
"rgb10a2",
"rgb9e5",
"rgba16snorm",
"rgba16unorm",
"rgba8snorm",
"rgba8unorm",
"sampler",
"short2",
"short3",
"short4",
"size_t",
"srgba8unorm",
"texture",
"texture1d",
"texture1d_array",
"texture2d",
"texture2d_array",
"texture2d_ms",
"texture2d_ms_array",
"texture3d",
"texture_buffer",
"texturecube",
"texturecube_array",
"thread",
"threadgroup",
"threadgroup_imageblock",
"uchar",
"uchar2",
"uchar3",
"uchar4",
"uint",
"uint16_t",
"uint2",
"uint3",
"uint32_t",
"uint4",
"uint64_t",
"uint8_t",
"ulong2",
"ulong3",
"ulong4",
"uniform",
"ushort",
"ushort2",
"ushort3",
"ushort4",
"vec",
"vertex"));
} // namespace
} // namespace msl
} // namespace writer
} // namespace tint

View File

@ -23,7 +23,6 @@
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/msl/generator_impl.h" #include "src/writer/msl/generator_impl.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -33,13 +32,11 @@ namespace msl {
template <typename BASE> template <typename BASE>
class TestHelperBase : public BASE, public ast::BuilderWithModule { class TestHelperBase : public BASE, public ast::BuilderWithModule {
public: public:
TestHelperBase() : td(mod), namer_(mod), gen(mod, &namer_) {} TestHelperBase() : td(mod), gen(mod) {}
~TestHelperBase() = default; ~TestHelperBase() = default;
/// The type determiner /// The type determiner
TypeDeterminer td; TypeDeterminer td;
/// The namer
TestNamer namer_;
/// The generator /// The generator
GeneratorImpl gen; GeneratorImpl gen;
}; };

View File

@ -278,8 +278,7 @@ Builder::AccessorInfo::AccessorInfo() : source_id(0), source_type(nullptr) {}
Builder::AccessorInfo::~AccessorInfo() {} Builder::AccessorInfo::~AccessorInfo() {}
Builder::Builder(ast::Module* mod, Namer* namer) Builder::Builder(ast::Module* mod) : mod_(mod), scope_stack_({}) {}
: mod_(mod), namer_(namer), scope_stack_({}) {}
Builder::~Builder() = default; Builder::~Builder() = default;
@ -442,7 +441,7 @@ bool Builder::GenerateEntryPoint(ast::Function* func, uint32_t id) {
} }
OperandList operands = {Operand::Int(stage), Operand::Int(id), OperandList operands = {Operand::Int(stage), Operand::Int(id),
Operand::String(namer_->NameFor(func->symbol()))}; Operand::String(mod_->SymbolToName(func->symbol()))};
for (const auto* var : func->referenced_module_variables()) { for (const auto* var : func->referenced_module_variables()) {
// For SPIR-V 1.3 we only output Input/output variables. If we update to // For SPIR-V 1.3 we only output Input/output variables. If we update to
@ -535,7 +534,7 @@ bool Builder::GenerateFunction(ast::Function* func) {
push_debug(spv::Op::OpName, push_debug(spv::Op::OpName,
{Operand::Int(func_id), {Operand::Int(func_id),
Operand::String(namer_->NameFor(func->symbol()))}); Operand::String(mod_->SymbolToName(func->symbol()))});
auto ret_id = GenerateTypeIfNeeded(func->return_type()); auto ret_id = GenerateTypeIfNeeded(func->return_type());
if (ret_id == 0) { if (ret_id == 0) {
@ -561,7 +560,7 @@ bool Builder::GenerateFunction(ast::Function* func) {
push_debug(spv::Op::OpName, push_debug(spv::Op::OpName,
{Operand::Int(param_id), {Operand::Int(param_id),
Operand::String(namer_->NameFor(param->symbol()))}); Operand::String(mod_->SymbolToName(param->symbol()))});
params.push_back(Instruction{spv::Op::OpFunctionParameter, params.push_back(Instruction{spv::Op::OpFunctionParameter,
{Operand::Int(param_type_id), param_op}}); {Operand::Int(param_type_id), param_op}});
@ -650,9 +649,9 @@ bool Builder::GenerateFunctionVariable(ast::Variable* var) {
return false; return false;
} }
push_debug( push_debug(spv::Op::OpName,
spv::Op::OpName, {Operand::Int(var_id),
{Operand::Int(var_id), Operand::String(namer_->NameFor(var->symbol()))}); Operand::String(mod_->SymbolToName(var->symbol()))});
// TODO(dsinclair) We could detect if the constructor is fully const and emit // TODO(dsinclair) We could detect if the constructor is fully const and emit
// an initializer value for the variable instead of doing the OpLoad. // an initializer value for the variable instead of doing the OpLoad.
@ -704,7 +703,7 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
} }
push_debug(spv::Op::OpName, push_debug(spv::Op::OpName,
{Operand::Int(init_id), {Operand::Int(init_id),
Operand::String(namer_->NameFor(var->symbol()))}); Operand::String(mod_->SymbolToName(var->symbol()))});
scope_stack_.set_global(var->symbol(), init_id); scope_stack_.set_global(var->symbol(), init_id);
spirv_id_to_variable_[init_id] = var; spirv_id_to_variable_[init_id] = var;
@ -724,9 +723,9 @@ bool Builder::GenerateGlobalVariable(ast::Variable* var) {
return false; return false;
} }
push_debug( push_debug(spv::Op::OpName,
spv::Op::OpName, {Operand::Int(var_id),
{Operand::Int(var_id), Operand::String(namer_->NameFor(var->symbol()))}); Operand::String(mod_->SymbolToName(var->symbol()))});
auto* type = var->type()->UnwrapAll(); auto* type = var->type()->UnwrapAll();
@ -2863,7 +2862,7 @@ bool Builder::GenerateStructType(ast::type::Struct* struct_type,
if (struct_type->symbol().IsValid()) { if (struct_type->symbol().IsValid()) {
push_debug(spv::Op::OpName, push_debug(spv::Op::OpName,
{Operand::Int(struct_id), {Operand::Int(struct_id),
Operand::String(namer_->NameFor(struct_type->symbol()))}); Operand::String(mod_->SymbolToName(struct_type->symbol()))});
} }
OperandList ops; OperandList ops;
@ -2906,7 +2905,7 @@ uint32_t Builder::GenerateStructMember(uint32_t struct_id,
ast::StructMember* member) { ast::StructMember* member) {
push_debug(spv::Op::OpMemberName, push_debug(spv::Op::OpMemberName,
{Operand::Int(struct_id), Operand::Int(idx), {Operand::Int(struct_id), Operand::Int(idx),
Operand::String(namer_->NameFor(member->symbol()))}); Operand::String(mod_->SymbolToName(member->symbol()))});
bool has_layout = false; bool has_layout = false;
for (auto* deco : member->decorations()) { for (auto* deco : member->decorations()) {

View File

@ -16,7 +16,6 @@
#define SRC_WRITER_SPIRV_BUILDER_H_ #define SRC_WRITER_SPIRV_BUILDER_H_
#include <functional> #include <functional>
#include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
@ -52,7 +51,6 @@
#include "src/ast/type_constructor_expression.h" #include "src/ast/type_constructor_expression.h"
#include "src/ast/unary_op_expression.h" #include "src/ast/unary_op_expression.h"
#include "src/ast/variable_decl_statement.h" #include "src/ast/variable_decl_statement.h"
#include "src/namer.h"
#include "src/scope_stack.h" #include "src/scope_stack.h"
#include "src/writer/spirv/function.h" #include "src/writer/spirv/function.h"
#include "src/writer/spirv/instruction.h" #include "src/writer/spirv/instruction.h"
@ -85,8 +83,7 @@ class Builder {
/// Constructor /// Constructor
/// @param mod the module to generate from /// @param mod the module to generate from
/// @param namer the namer to use explicit Builder(ast::Module* mod);
Builder(ast::Module* mod, Namer* namer);
~Builder(); ~Builder();
/// Generates the SPIR-V instructions for the given module /// Generates the SPIR-V instructions for the given module
@ -492,7 +489,6 @@ class Builder {
Operand result_op(); Operand result_op();
ast::Module* mod_; ast::Module* mod_;
Namer* namer_ = nullptr;
std::string error_; std::string error_;
uint32_t next_id_ = 1; uint32_t next_id_ = 1;
uint32_t current_label_id_ = 0; uint32_t current_label_id_ = 0;

View File

@ -61,10 +61,10 @@ TEST_F(BuilderTest, Expression_Call) {
ASSERT_TRUE(b.GenerateFunction(func)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_EQ(b.GenerateCallExpression(expr), 14u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 14u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
OpName %4 "test_a" OpName %4 "a"
OpName %5 "test_b" OpName %5 "b"
OpName %12 "test_main" OpName %12 "main"
%2 = OpTypeFloat 32 %2 = OpTypeFloat 32
%1 = OpTypeFunction %2 %2 %2 %1 = OpTypeFunction %2 %2 %2
%11 = OpTypeVoid %11 = OpTypeVoid
@ -110,10 +110,10 @@ TEST_F(BuilderTest, Statement_Call) {
ASSERT_TRUE(b.GenerateFunction(func)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_TRUE(b.GenerateStatement(expr)) << b.error(); EXPECT_TRUE(b.GenerateStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %4 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %4 "a_func"
OpName %5 "test_a" OpName %5 "a"
OpName %6 "test_b" OpName %6 "b"
OpName %12 "test_main" OpName %12 "main"
%2 = OpTypeVoid %2 = OpTypeVoid
%3 = OpTypeFloat 32 %3 = OpTypeFloat 32
%1 = OpTypeFunction %2 %3 %3 %1 = OpTypeFunction %2 %3 %3

View File

@ -45,7 +45,7 @@ TEST_F(BuilderTest, FunctionDecoration_Stage) {
ASSERT_TRUE(b.GenerateFunction(func)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_EQ(DumpInstructions(b.entry_points()), EXPECT_EQ(DumpInstructions(b.entry_points()),
R"(OpEntryPoint Vertex %3 "test_main" R"(OpEntryPoint Vertex %3 "main"
)"); )");
} }
@ -105,10 +105,10 @@ TEST_F(BuilderTest, FunctionDecoration_Stage_WithUnusedInterfaceIds) {
mod->AddGlobalVariable(v_wg); mod->AddGlobalVariable(v_wg);
ASSERT_TRUE(b.GenerateFunction(func)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_my_in" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "my_in"
OpName %4 "test_my_out" OpName %4 "my_out"
OpName %7 "test_my_wg" OpName %7 "my_wg"
OpName %11 "test_main" OpName %11 "main"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Input %3 %2 = OpTypePointer Input %3
@ -122,7 +122,7 @@ OpName %11 "test_main"
%9 = OpTypeFunction %10 %9 = OpTypeFunction %10
)"); )");
EXPECT_EQ(DumpInstructions(b.entry_points()), EXPECT_EQ(DumpInstructions(b.entry_points()),
R"(OpEntryPoint Vertex %11 "test_main" R"(OpEntryPoint Vertex %11 "main"
)"); )");
} }
@ -158,10 +158,10 @@ TEST_F(BuilderTest, FunctionDecoration_Stage_WithUsedInterfaceIds) {
mod->AddGlobalVariable(v_wg); mod->AddGlobalVariable(v_wg);
ASSERT_TRUE(b.GenerateFunction(func)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_my_in" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "my_in"
OpName %4 "test_my_out" OpName %4 "my_out"
OpName %7 "test_my_wg" OpName %7 "my_wg"
OpName %11 "test_main" OpName %11 "main"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Input %3 %2 = OpTypePointer Input %3
@ -175,7 +175,7 @@ OpName %11 "test_main"
%9 = OpTypeFunction %10 %9 = OpTypeFunction %10
)"); )");
EXPECT_EQ(DumpInstructions(b.entry_points()), EXPECT_EQ(DumpInstructions(b.entry_points()),
R"(OpEntryPoint Vertex %11 "test_main" %4 %1 R"(OpEntryPoint Vertex %11 "main" %4 %1
)"); )");
} }
@ -235,12 +235,12 @@ TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_MultipleFragment) {
ASSERT_TRUE(b.GenerateFunction(func1)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func1)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func2)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func2)) << b.error();
EXPECT_EQ(DumpBuilder(b), EXPECT_EQ(DumpBuilder(b),
R"(OpEntryPoint Fragment %3 "test_main1" R"(OpEntryPoint Fragment %3 "main1"
OpEntryPoint Fragment %5 "test_main2" OpEntryPoint Fragment %5 "main2"
OpExecutionMode %3 OriginUpperLeft OpExecutionMode %3 OriginUpperLeft
OpExecutionMode %5 OriginUpperLeft OpExecutionMode %5 OriginUpperLeft
OpName %3 "test_main1" OpName %3 "main1"
OpName %5 "test_main2" OpName %5 "main2"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%3 = OpFunction %2 None %1 %3 = OpFunction %2 None %1

View File

@ -50,7 +50,7 @@ TEST_F(BuilderTest, Function_Empty) {
ast::FunctionDecorationList{}); ast::FunctionDecorationList{});
ASSERT_TRUE(b.GenerateFunction(func)); ASSERT_TRUE(b.GenerateFunction(func));
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%3 = OpFunction %2 None %1 %3 = OpFunction %2 None %1
@ -68,7 +68,7 @@ TEST_F(BuilderTest, Function_Terminator_Return) {
ast::FunctionDecorationList{}); ast::FunctionDecorationList{});
ASSERT_TRUE(b.GenerateFunction(func)); ASSERT_TRUE(b.GenerateFunction(func));
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%3 = OpFunction %2 None %1 %3 = OpFunction %2 None %1
@ -89,8 +89,8 @@ TEST_F(BuilderTest, Function_Terminator_ReturnValue) {
ASSERT_TRUE(td.DetermineFunction(func)) << td.error(); ASSERT_TRUE(td.DetermineFunction(func)) << td.error();
ASSERT_TRUE(b.GenerateGlobalVariable(var_a)) << b.error(); ASSERT_TRUE(b.GenerateGlobalVariable(var_a)) << b.error();
ASSERT_TRUE(b.GenerateFunction(func)) << b.error(); ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "test_a" EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "a"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeFloat 32 %3 = OpTypeFloat 32
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3
@ -113,7 +113,7 @@ TEST_F(BuilderTest, Function_Terminator_Discard) {
ast::FunctionDecorationList{}); ast::FunctionDecorationList{});
ASSERT_TRUE(b.GenerateFunction(func)); ASSERT_TRUE(b.GenerateFunction(func));
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%3 = OpFunction %2 None %1 %3 = OpFunction %2 None %1
@ -136,9 +136,9 @@ TEST_F(BuilderTest, Function_WithParams) {
EXPECT_TRUE(td.DetermineFunction(func)); EXPECT_TRUE(td.DetermineFunction(func));
ASSERT_TRUE(b.GenerateFunction(func)); ASSERT_TRUE(b.GenerateFunction(func));
EXPECT_EQ(DumpBuilder(b), R"(OpName %4 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %4 "a_func"
OpName %5 "test_a" OpName %5 "a"
OpName %6 "test_b" OpName %6 "b"
%2 = OpTypeFloat 32 %2 = OpTypeFloat 32
%3 = OpTypeInt 32 1 %3 = OpTypeInt 32 1
%1 = OpTypeFunction %2 %2 %3 %1 = OpTypeFunction %2 %2 %3
@ -160,7 +160,7 @@ TEST_F(BuilderTest, Function_WithBody) {
ast::FunctionDecorationList{}); ast::FunctionDecorationList{});
ASSERT_TRUE(b.GenerateFunction(func)); ASSERT_TRUE(b.GenerateFunction(func));
EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "test_a_func" EXPECT_EQ(DumpBuilder(b), R"(OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%3 = OpFunction %2 None %1 %3 = OpFunction %2 None %1
@ -269,17 +269,17 @@ TEST_F(BuilderTest, Emit_Multiple_EntryPoint_With_Same_ModuleVar) {
ASSERT_TRUE(b.Build()); ASSERT_TRUE(b.Build());
EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader EXPECT_EQ(DumpBuilder(b), R"(OpCapability Shader
OpMemoryModel Logical GLSL450 OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %7 "test_a" OpEntryPoint GLCompute %7 "a"
OpEntryPoint GLCompute %17 "test_b" OpEntryPoint GLCompute %17 "b"
OpExecutionMode %7 LocalSize 1 1 1 OpExecutionMode %7 LocalSize 1 1 1
OpExecutionMode %17 LocalSize 1 1 1 OpExecutionMode %17 LocalSize 1 1 1
OpName %3 "test_Data" OpName %3 "Data"
OpMemberName %3 0 "test_d" OpMemberName %3 0 "d"
OpName %1 "test_data" OpName %1 "data"
OpName %7 "test_a" OpName %7 "a"
OpName %14 "test_v" OpName %14 "v"
OpName %17 "test_b" OpName %17 "b"
OpName %21 "test_v" OpName %21 "v"
OpDecorate %3 Block OpDecorate %3 Block
OpMemberDecorate %3 0 Offset 0 OpMemberDecorate %3 0 Offset 0
OpDecorate %1 Binding 0 OpDecorate %1 Binding 0

View File

@ -49,7 +49,7 @@ TEST_F(BuilderTest, FunctionVar_NoStorageClass) {
b.push_function(Function{}); b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Function %3 %2 = OpTypePointer Function %3
@ -74,7 +74,7 @@ TEST_F(BuilderTest, FunctionVar_WithConstantConstructor) {
EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeVector %2 3 %1 = OpTypeVector %2 3
@ -103,7 +103,7 @@ TEST_F(BuilderTest, FunctionVar_WithNonConstantConstructor) {
EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %7 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %7 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeVector %2 2 %1 = OpTypeVector %2 2
@ -142,8 +142,8 @@ TEST_F(BuilderTest, FunctionVar_WithNonConstantConstructorLoadedFromVar) {
EXPECT_TRUE(b.GenerateFunctionVariable(v2)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v2)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_v" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "v"
OpName %7 "test_v2" OpName %7 "v2"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
%2 = OpConstant %1 1 %2 = OpConstant %1 1
@ -181,8 +181,8 @@ TEST_F(BuilderTest, FunctionVar_ConstWithVarInitializer) {
EXPECT_TRUE(b.GenerateFunctionVariable(v2)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v2)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_v" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "v"
OpName %7 "test_v2" OpName %7 "v2"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
%2 = OpConstant %1 1 %2 = OpConstant %1 1

View File

@ -52,7 +52,7 @@ using BuilderTest = TestHelper;
TEST_F(BuilderTest, GlobalVar_NoStorageClass) { TEST_F(BuilderTest, GlobalVar_NoStorageClass) {
auto* v = Var("var", ast::StorageClass::kNone, ty.f32); auto* v = Var("var", ast::StorageClass::kNone, ty.f32);
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
@ -64,7 +64,7 @@ TEST_F(BuilderTest, GlobalVar_NoStorageClass) {
TEST_F(BuilderTest, GlobalVar_WithStorageClass) { TEST_F(BuilderTest, GlobalVar_WithStorageClass) {
auto* v = Var("var", ast::StorageClass::kOutput, ty.f32); auto* v = Var("var", ast::StorageClass::kOutput, ty.f32);
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Output %3 %2 = OpTypePointer Output %3
@ -76,7 +76,7 @@ TEST_F(BuilderTest, GlobalVar_WithStorageClass) {
TEST_F(BuilderTest, GlobalVar_WithStorageClass_Input) { TEST_F(BuilderTest, GlobalVar_WithStorageClass_Input) {
auto* v = Var("var", ast::StorageClass::kInput, ty.f32); auto* v = Var("var", ast::StorageClass::kInput, ty.f32);
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Input %3 %2 = OpTypePointer Input %3
@ -95,7 +95,7 @@ TEST_F(BuilderTest, GlobalVar_WithConstructor) {
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %6 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeVector %2 3 %1 = OpTypeVector %2 3
@ -118,7 +118,7 @@ TEST_F(BuilderTest, GlobalVar_Const) {
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
ASSERT_FALSE(b.has_error()) << b.error(); ASSERT_FALSE(b.has_error()) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %5 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %5 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeVector %2 3 %1 = OpTypeVector %2 3
@ -183,7 +183,7 @@ TEST_F(BuilderTest, GlobalVar_WithLocation) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 Location 5 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 Location 5
)"); )");
@ -202,7 +202,7 @@ TEST_F(BuilderTest, GlobalVar_WithBindingAndSet) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 Binding 2 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 Binding 2
OpDecorate %1 DescriptorSet 3 OpDecorate %1 DescriptorSet 3
@ -221,7 +221,7 @@ TEST_F(BuilderTest, GlobalVar_WithBuiltin) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 BuiltIn Position EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 BuiltIn Position
)"); )");
@ -239,7 +239,7 @@ TEST_F(BuilderTest, GlobalVar_ConstantId_Bool) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %2 SpecId 1200 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %2 SpecId 1200
)"); )");
@ -257,7 +257,7 @@ TEST_F(BuilderTest, GlobalVar_ConstantId_Bool_NoConstructor) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 1200 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 1200
)"); )");
@ -275,7 +275,7 @@ TEST_F(BuilderTest, GlobalVar_ConstantId_Scalar) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %2 SpecId 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %2 SpecId 0
)"); )");
@ -293,7 +293,7 @@ TEST_F(BuilderTest, GlobalVar_ConstantId_Scalar_F32_NoConstructor) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 0
)"); )");
@ -311,7 +311,7 @@ TEST_F(BuilderTest, GlobalVar_ConstantId_Scalar_I32_NoConstructor) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 0
)"); )");
@ -329,7 +329,7 @@ TEST_F(BuilderTest, GlobalVar_ConstantId_Scalar_U32_NoConstructor) {
}); });
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %4 SpecId 0
)"); )");
@ -392,10 +392,10 @@ TEST_F(BuilderTest, GlobalVar_DeclReadOnly) {
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable
OpMemberDecorate %3 1 NonWritable OpMemberDecorate %3 1 NonWritable
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_A" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "A"
OpMemberName %3 0 "test_a" OpMemberName %3 0 "a"
OpMemberName %3 1 "test_b" OpMemberName %3 1 "b"
OpName %1 "test_b" OpName %1 "b"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
%3 = OpTypeStruct %4 %4 %3 = OpTypeStruct %4 %4
@ -421,9 +421,9 @@ TEST_F(BuilderTest, GlobalVar_TypeAliasDeclReadOnly) {
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_A" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "A"
OpMemberName %3 0 "test_a" OpMemberName %3 0 "a"
OpName %1 "test_b" OpName %1 "b"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
%3 = OpTypeStruct %4 %3 = OpTypeStruct %4
@ -449,9 +449,9 @@ TEST_F(BuilderTest, GlobalVar_TypeAliasAssignReadOnly) {
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_A" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "A"
OpMemberName %3 0 "test_a" OpMemberName %3 0 "a"
OpName %1 "test_b" OpName %1 "b"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
%3 = OpTypeStruct %4 %3 = OpTypeStruct %4
@ -480,12 +480,12 @@ TEST_F(BuilderTest, GlobalVar_TwoVarDeclReadOnly) {
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %3 0 NonWritable
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "test_A" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %3 "A"
OpMemberName %3 0 "test_a" OpMemberName %3 0 "a"
OpName %1 "test_b" OpName %1 "b"
OpName %7 "test_A" OpName %7 "A"
OpMemberName %7 0 "test_a" OpMemberName %7 0 "a"
OpName %5 "test_c" OpName %5 "c"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
%3 = OpTypeStruct %4 %3 = OpTypeStruct %4

View File

@ -66,7 +66,7 @@ TEST_F(BuilderTest, IdentifierExpression_GlobalVar) {
b.push_function(Function{}); b.push_function(Function{});
EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateGlobalVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Output %3 %2 = OpTypePointer Output %3
@ -108,7 +108,7 @@ TEST_F(BuilderTest, IdentifierExpression_FunctionVar) {
b.push_function(Function{}); b.push_function(Function{});
EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error(); EXPECT_TRUE(b.GenerateFunctionVariable(v)) << b.error();
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_var" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "var"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
%2 = OpTypePointer Function %3 %2 = OpTypePointer Function %3

View File

@ -45,7 +45,6 @@
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/spirv/builder.h" #include "src/writer/spirv/builder.h"
#include "src/writer/spirv/spv_dump.h" #include "src/writer/spirv/spv_dump.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -60,8 +59,7 @@ class IntrinsicBuilderTest : public ast::BuilderWithModule,
} }
TypeDeterminer td{mod}; TypeDeterminer td{mod};
TestNamer namer{mod}; spirv::Builder b{mod};
spirv::Builder b{mod, &namer};
}; };
template <typename T> template <typename T>
@ -481,8 +479,8 @@ TEST_F(IntrinsicBuilderTest, Call_GLSLMethod_WithLoad) {
EXPECT_EQ(b.GenerateCallExpression(expr), 9u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 9u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%10 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%10 = OpExtInstImport "GLSL.std.450"
OpName %1 "test_ident" OpName %1 "ident"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeFloat 32 %3 = OpTypeFloat 32
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3
@ -513,7 +511,7 @@ TEST_P(Intrinsic_Builtin_SingleParam_Float_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -540,7 +538,7 @@ TEST_P(Intrinsic_Builtin_SingleParam_Float_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeFloat 32 %7 = OpTypeFloat 32
@ -593,7 +591,7 @@ TEST_F(IntrinsicBuilderTest, Call_Length_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -617,7 +615,7 @@ TEST_F(IntrinsicBuilderTest, Call_Length_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -643,7 +641,7 @@ TEST_F(IntrinsicBuilderTest, Call_Normalize) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeFloat 32 %7 = OpTypeFloat 32
@ -674,7 +672,7 @@ TEST_P(Intrinsic_Builtin_DualParam_Float_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -702,7 +700,7 @@ TEST_P(Intrinsic_Builtin_DualParam_Float_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeFloat 32 %7 = OpTypeFloat 32
@ -738,7 +736,7 @@ TEST_F(IntrinsicBuilderTest, Call_Distance_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -763,7 +761,7 @@ TEST_F(IntrinsicBuilderTest, Call_Distance_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -791,7 +789,7 @@ TEST_F(IntrinsicBuilderTest, Call_Cross) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeFloat 32 %7 = OpTypeFloat 32
@ -821,7 +819,7 @@ TEST_P(Intrinsic_Builtin_ThreeParam_Float_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeFloat 32 %6 = OpTypeFloat 32
@ -850,7 +848,7 @@ TEST_P(Intrinsic_Builtin_ThreeParam_Float_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeFloat 32 %7 = OpTypeFloat 32
@ -890,7 +888,7 @@ TEST_P(Intrinsic_Builtin_SingleParam_Sint_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeInt 32 1 %6 = OpTypeInt 32 1
@ -917,7 +915,7 @@ TEST_P(Intrinsic_Builtin_SingleParam_Sint_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeInt 32 1 %7 = OpTypeInt 32 1
@ -951,7 +949,7 @@ TEST_P(Intrinsic_Builtin_SingleParam_Uint_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeInt 32 0 %6 = OpTypeInt 32 0
@ -978,7 +976,7 @@ TEST_P(Intrinsic_Builtin_SingleParam_Uint_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeInt 32 0 %7 = OpTypeInt 32 0
@ -1012,7 +1010,7 @@ TEST_P(Intrinsic_Builtin_DualParam_SInt_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeInt 32 1 %6 = OpTypeInt 32 1
@ -1039,7 +1037,7 @@ TEST_P(Intrinsic_Builtin_DualParam_SInt_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeInt 32 1 %7 = OpTypeInt 32 1
@ -1074,7 +1072,7 @@ TEST_P(Intrinsic_Builtin_DualParam_UInt_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeInt 32 0 %6 = OpTypeInt 32 0
@ -1101,7 +1099,7 @@ TEST_P(Intrinsic_Builtin_DualParam_UInt_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeInt 32 0 %7 = OpTypeInt 32 0
@ -1136,7 +1134,7 @@ TEST_P(Intrinsic_Builtin_ThreeParam_Sint_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeInt 32 1 %6 = OpTypeInt 32 1
@ -1165,7 +1163,7 @@ TEST_P(Intrinsic_Builtin_ThreeParam_Sint_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeInt 32 1 %7 = OpTypeInt 32 1
@ -1199,7 +1197,7 @@ TEST_P(Intrinsic_Builtin_ThreeParam_Uint_Test, Call_Scalar) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%7 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%6 = OpTypeInt 32 0 %6 = OpTypeInt 32 0
@ -1228,7 +1226,7 @@ TEST_P(Intrinsic_Builtin_ThreeParam_Uint_Test, Call_Vector) {
EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 5u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%8 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%7 = OpTypeInt 32 0 %7 = OpTypeInt 32 0
@ -1262,8 +1260,8 @@ TEST_F(IntrinsicBuilderTest, Call_Determinant) {
EXPECT_EQ(b.GenerateCallExpression(expr), 11u) << b.error(); EXPECT_EQ(b.GenerateCallExpression(expr), 11u) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(%12 = OpExtInstImport "GLSL.std.450" EXPECT_EQ(DumpBuilder(b), R"(%12 = OpExtInstImport "GLSL.std.450"
OpName %3 "test_a_func" OpName %3 "a_func"
OpName %5 "test_var" OpName %5 "var"
%2 = OpTypeVoid %2 = OpTypeVoid
%1 = OpTypeFunction %2 %1 = OpTypeFunction %2
%9 = OpTypeFloat 32 %9 = OpTypeFloat 32

View File

@ -24,7 +24,6 @@
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/spirv/builder.h" #include "src/writer/spirv/builder.h"
#include "src/writer/spirv/spv_dump.h" #include "src/writer/spirv/spv_dump.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -3695,8 +3694,7 @@ class IntrinsicTextureTest
} }
TypeDeterminer td{mod}; TypeDeterminer td{mod};
TestNamer namer{mod}; spirv::Builder b{mod};
spirv::Builder b{mod, &namer};
}; };
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(

View File

@ -103,9 +103,9 @@ TEST_F(BuilderTest, Switch_WithCase) {
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error(); EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "test_v" EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "test_a" OpName %5 "a"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeInt 32 1 %3 = OpTypeInt 32 1
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3
@ -164,9 +164,9 @@ TEST_F(BuilderTest, Switch_WithDefault) {
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error(); EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "test_v" EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "test_a" OpName %5 "a"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeInt 32 1 %3 = OpTypeInt 32 1
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3
@ -238,9 +238,9 @@ TEST_F(BuilderTest, Switch_WithCaseAndDefault) {
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error(); EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "test_v" EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "test_a" OpName %5 "a"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeInt 32 1 %3 = OpTypeInt 32 1
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3
@ -321,9 +321,9 @@ TEST_F(BuilderTest, Switch_CaseWithFallthrough) {
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error(); EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "test_v" EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "test_a" OpName %5 "a"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeInt 32 1 %3 = OpTypeInt 32 1
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3
@ -431,9 +431,9 @@ TEST_F(BuilderTest, Switch_WithNestedBreak) {
EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error(); EXPECT_TRUE(b.GenerateSwitchStatement(expr)) << b.error();
EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "test_v" EXPECT_EQ(DumpBuilder(b), R"(OpName %1 "v"
OpName %5 "test_a" OpName %5 "a"
OpName %7 "test_a_func" OpName %7 "a_func"
%3 = OpTypeInt 32 1 %3 = OpTypeInt 32 1
%2 = OpTypePointer Private %3 %2 = OpTypePointer Private %3
%4 = OpConstantNull %3 %4 = OpConstantNull %3

View File

@ -251,7 +251,7 @@ TEST_F(BuilderTest_Type, GenerateStruct_Empty) {
EXPECT_EQ(id, 1u); EXPECT_EQ(id, 1u);
EXPECT_EQ(b.types().size(), 1u); EXPECT_EQ(b.types().size(), 1u);
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_S" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "S"
)"); )");
EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeStruct EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeStruct
)"); )");
@ -269,8 +269,8 @@ TEST_F(BuilderTest_Type, GenerateStruct) {
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeStruct %2 %1 = OpTypeStruct %2
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_my_struct" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "my_struct"
OpMemberName %1 0 "test_a" OpMemberName %1 0 "a"
)"); )");
} }
@ -289,8 +289,8 @@ TEST_F(BuilderTest_Type, GenerateStruct_Decorated) {
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeStruct %2 %1 = OpTypeStruct %2
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_my_struct" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "my_struct"
OpMemberName %1 0 "test_a" OpMemberName %1 0 "a"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 Block EXPECT_EQ(DumpInstructions(b.annots()), R"(OpDecorate %1 Block
)"); )");
@ -310,9 +310,9 @@ TEST_F(BuilderTest_Type, GenerateStruct_DecoratedMembers) {
EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
%1 = OpTypeStruct %2 %2 %1 = OpTypeStruct %2 %2
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_S" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "S"
OpMemberName %1 0 "test_a" OpMemberName %1 0 "a"
OpMemberName %1 1 "test_b" OpMemberName %1 1 "b"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %1 0 Offset 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %1 0 Offset 0
OpMemberDecorate %1 1 Offset 8 OpMemberDecorate %1 1 Offset 8
@ -340,10 +340,10 @@ TEST_F(BuilderTest_Type, GenerateStruct_NonLayout_Matrix) {
%7 = OpTypeMatrix %8 4 %7 = OpTypeMatrix %8 4
%1 = OpTypeStruct %2 %5 %7 %1 = OpTypeStruct %2 %5 %7
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_S" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "S"
OpMemberName %1 0 "test_a" OpMemberName %1 0 "a"
OpMemberName %1 1 "test_b" OpMemberName %1 1 "b"
OpMemberName %1 2 "test_c" OpMemberName %1 2 "c"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), ""); EXPECT_EQ(DumpInstructions(b.annots()), "");
} }
@ -370,10 +370,10 @@ TEST_F(BuilderTest_Type, GenerateStruct_DecoratedMembers_LayoutMatrix) {
%7 = OpTypeMatrix %8 4 %7 = OpTypeMatrix %8 4
%1 = OpTypeStruct %2 %5 %7 %1 = OpTypeStruct %2 %5 %7
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_S" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "S"
OpMemberName %1 0 "test_a" OpMemberName %1 0 "a"
OpMemberName %1 1 "test_b" OpMemberName %1 1 "b"
OpMemberName %1 2 "test_c" OpMemberName %1 2 "c"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %1 0 Offset 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %1 0 Offset 0
OpMemberDecorate %1 0 ColMajor OpMemberDecorate %1 0 ColMajor
@ -426,10 +426,10 @@ TEST_F(BuilderTest_Type, GenerateStruct_DecoratedMembers_LayoutArraysOfMatrix) {
%11 = OpTypeRuntimeArray %12 %11 = OpTypeRuntimeArray %12
%1 = OpTypeStruct %2 %8 %11 %1 = OpTypeStruct %2 %8 %11
)"); )");
EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "test_S" EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "S"
OpMemberName %1 0 "test_a" OpMemberName %1 0 "a"
OpMemberName %1 1 "test_b" OpMemberName %1 1 "b"
OpMemberName %1 2 "test_c" OpMemberName %1 2 "c"
)"); )");
EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %1 0 Offset 0 EXPECT_EQ(DumpInstructions(b.annots()), R"(OpMemberDecorate %1 0 Offset 0
OpMemberDecorate %1 0 ColMajor OpMemberDecorate %1 0 ColMajor

View File

@ -22,21 +22,13 @@ namespace spirv {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: writer::Writer(std::move(module)), : writer::Writer(std::move(module)),
namer_(std::make_unique<MangleNamer>(module_)), builder_(std::make_unique<Builder>(&module_)),
builder_(std::make_unique<Builder>(module_, namer_.get())),
writer_(std::make_unique<BinaryWriter>()) {}
Generator::Generator(ast::Module* module)
: writer::Writer(module),
namer_(std::make_unique<MangleNamer>(module_)),
builder_(std::make_unique<Builder>(module_, namer_.get())),
writer_(std::make_unique<BinaryWriter>()) {} writer_(std::make_unique<BinaryWriter>()) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
namer_->Reset(); builder_ = std::make_unique<Builder>(&module_);
builder_ = std::make_unique<Builder>(module_, namer_.get());
writer_ = std::make_unique<BinaryWriter>(); writer_ = std::make_unique<BinaryWriter>();
} }
@ -51,22 +43,6 @@ bool Generator::Generate() {
return true; return true;
} }
bool Generator::GenerateUnsafe() {
auto unsafe_namer = std::make_unique<UnsafeNamer>(module_);
builder_ = std::make_unique<Builder>(module_, unsafe_namer.get());
if (!builder_->Build()) {
set_error(builder_->error());
return false;
}
writer_->WriteHeader(builder_->id_bound());
writer_->WriteBuilder(builder_.get());
builder_ = std::make_unique<Builder>(module_, namer_.get());
return true;
}
bool Generator::GenerateEntryPoint(ast::PipelineStage, const std::string&) { bool Generator::GenerateEntryPoint(ast::PipelineStage, const std::string&) {
return false; return false;
} }

View File

@ -20,7 +20,6 @@
#include <vector> #include <vector>
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/namer.h"
#include "src/writer/spirv/binary_writer.h" #include "src/writer/spirv/binary_writer.h"
#include "src/writer/spirv/builder.h" #include "src/writer/spirv/builder.h"
#include "src/writer/writer.h" #include "src/writer/writer.h"
@ -33,12 +32,8 @@ namespace spirv {
class Generator : public writer::Writer { class Generator : public writer::Writer {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator
@ -48,11 +43,6 @@ class Generator : public writer::Writer {
/// @returns true on successful generation; false otherwise /// @returns true on successful generation; false otherwise
bool Generate() override; bool Generate() override;
/// Generates the result data
/// DO NOT USE. Temporary fix for Dawn usage of SPRIV-Cross
/// @returns true on successful generation; false otherwise
bool GenerateUnsafe();
/// Converts a single entry point /// Converts a single entry point
/// @param stage the pipeline stage /// @param stage the pipeline stage
/// @param name the entry point name /// @param name the entry point name
@ -64,7 +54,6 @@ class Generator : public writer::Writer {
const std::vector<uint32_t>& result() const { return writer_->result(); } const std::vector<uint32_t>& result() const { return writer_->result(); }
private: private:
std::unique_ptr<Namer> namer_;
std::unique_ptr<Builder> builder_; std::unique_ptr<Builder> builder_;
std::unique_ptr<BinaryWriter> writer_; std::unique_ptr<BinaryWriter> writer_;
}; };

View File

@ -23,7 +23,6 @@
#include "src/ast/module.h" #include "src/ast/module.h"
#include "src/type_determiner.h" #include "src/type_determiner.h"
#include "src/writer/spirv/builder.h" #include "src/writer/spirv/builder.h"
#include "src/writer/test_namer.h"
namespace tint { namespace tint {
namespace writer { namespace writer {
@ -33,13 +32,11 @@ namespace spirv {
template <typename BASE> template <typename BASE>
class TestHelperBase : public ast::BuilderWithModule, public BASE { class TestHelperBase : public ast::BuilderWithModule, public BASE {
public: public:
TestHelperBase() : td(mod), namer(mod), b(mod, &namer) {} TestHelperBase() : td(mod), b(mod) {}
~TestHelperBase() override = default; ~TestHelperBase() override = default;
/// The type determiner /// The type determiner
TypeDeterminer td; TypeDeterminer td;
/// The test namer
TestNamer namer;
/// The generator /// The generator
spirv::Builder b; spirv::Builder b;

View File

@ -1,29 +0,0 @@
// Copyright 2021 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 "src/writer/test_namer.h"
namespace tint {
namespace writer {
TestNamer::TestNamer(const ast::Module* mod) : Namer(mod) {}
TestNamer::~TestNamer() = default;
std::string TestNamer::NameFor(const Symbol& sym) {
return "test_" + module_->SymbolToName(sym);
}
} // namespace writer
} // namespace tint

View File

@ -1,43 +0,0 @@
// Copyright 2021 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.
#ifndef SRC_WRITER_TEST_NAMER_H_
#define SRC_WRITER_TEST_NAMER_H_
#include <string>
#include "src/namer.h"
namespace tint {
namespace writer {
/// A namer which returns the provided name prefixed with `test_`.
class TestNamer : public Namer {
public:
/// Constructor
/// @param mod the module to retrieve names from
explicit TestNamer(const ast::Module* mod);
/// Destructor
~TestNamer() override;
/// Returns `name`
/// @param sym the symbol
/// @returns `name` or "" if not found
std::string NameFor(const Symbol& sym) override;
};
} // namespace writer
} // namespace tint
#endif // SRC_WRITER_TEST_NAMER_H_

View File

@ -21,8 +21,6 @@ namespace writer {
Text::Text(ast::Module module) : Writer(std::move(module)) {} Text::Text(ast::Module module) : Writer(std::move(module)) {}
Text::Text(ast::Module* module) : Writer(module) {}
Text::~Text() = default; Text::~Text() = default;
} // namespace writer } // namespace writer

View File

@ -28,9 +28,6 @@ class Text : public Writer {
/// Constructor /// Constructor
/// @param module the module to convert /// @param module the module to convert
explicit Text(ast::Module module); explicit Text(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Text(ast::Module* module);
~Text() override; ~Text() override;
/// @returns the result data /// @returns the result data

View File

@ -22,16 +22,13 @@ namespace wgsl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), : Text(std::move(module)),
impl_(std::make_unique<GeneratorImpl>(module_)) {} impl_(std::make_unique<GeneratorImpl>(&module_)) {}
Generator::Generator(ast::Module* module)
: Text(module), impl_(std::make_unique<GeneratorImpl>(module_)) {}
Generator::~Generator() = default; Generator::~Generator() = default;
void Generator::Reset() { void Generator::Reset() {
set_error(""); set_error("");
impl_ = std::make_unique<GeneratorImpl>(module_); impl_ = std::make_unique<GeneratorImpl>(&module_);
} }
bool Generator::Generate() { bool Generator::Generate() {

View File

@ -29,12 +29,8 @@ namespace wgsl {
class Generator : public Text { class Generator : public Text {
public: public:
/// Constructor /// Constructor
/// DEPRECATED
/// @param module the module to convert /// @param module the module to convert
explicit Generator(ast::Module module); explicit Generator(ast::Module module);
/// Constructor
/// @param module the module to convert
explicit Generator(ast::Module* module);
~Generator() override; ~Generator() override;
/// Resets the generator /// Resets the generator

View File

@ -19,10 +19,7 @@
namespace tint { namespace tint {
namespace writer { namespace writer {
Writer::Writer(ast::Module module) Writer::Writer(ast::Module module) : module_(std::move(module)) {}
: owned_module_(std::move(module)), module_(&owned_module_) {}
Writer::Writer(ast::Module* module) : module_(module) {}
Writer::~Writer() = default; Writer::~Writer() = default;

View File

@ -51,9 +51,6 @@ class Writer {
/// Constructor /// Constructor
/// @param module the tint module to convert /// @param module the tint module to convert
explicit Writer(ast::Module module); explicit Writer(ast::Module module);
/// Constructor
/// @param module the tint module to convert
explicit Writer(ast::Module* module);
/// Sets the error string /// Sets the error string
/// @param msg the error message /// @param msg the error message
@ -61,11 +58,8 @@ class Writer {
/// An error message, if an error was encountered /// An error message, if an error was encountered
std::string error_; std::string error_;
/// Temporary owned module until we can update the API ...
ast::Module owned_module_;
/// The module being converted /// The module being converted
ast::Module* module_; ast::Module module_;
}; };
} // namespace writer } // namespace writer