mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-09 16:13:34 +00:00
Start WGSL writer
This CL is the start of a WGSL writer. Bug: tint:4 Change-Id: Ib2c2580d9e50c36664088556d9b8feec76935211 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16560 Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
37dbf99e34
commit
d9e9ff3be5
@ -181,6 +181,8 @@ set(TINT_LIB_SRCS
|
|||||||
writer/spv/generator.h
|
writer/spv/generator.h
|
||||||
writer/wgsl/generator.cc
|
writer/wgsl/generator.cc
|
||||||
writer/wgsl/generator.h
|
writer/wgsl/generator.h
|
||||||
|
writer/wgsl/generator_impl.cc
|
||||||
|
writer/wgsl/generator_impl.h
|
||||||
writer/writer.cc
|
writer/writer.cc
|
||||||
writer/writer.h
|
writer/writer.h
|
||||||
)
|
)
|
||||||
@ -298,6 +300,7 @@ set(TINT_TEST_SRCS
|
|||||||
reader/wgsl/token_test.cc
|
reader/wgsl/token_test.cc
|
||||||
type_manager_test.cc
|
type_manager_test.cc
|
||||||
validator_impl_import_test.cc
|
validator_impl_import_test.cc
|
||||||
|
writer/wgsl/generator_impl_test.cc
|
||||||
)
|
)
|
||||||
|
|
||||||
## Tint library
|
## Tint library
|
||||||
|
@ -24,11 +24,11 @@ std::ostream& operator<<(std::ostream& out, StorageClass sc) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case StorageClass::kInput: {
|
case StorageClass::kInput: {
|
||||||
out << "input";
|
out << "in";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case StorageClass::kOutput: {
|
case StorageClass::kOutput: {
|
||||||
out << "output";
|
out << "out";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case StorageClass::kUniform: {
|
case StorageClass::kUniform: {
|
||||||
|
@ -25,7 +25,7 @@ Generator::Generator(ast::Module module) : writer::Writer(std::move(module)) {}
|
|||||||
Generator::~Generator() = default;
|
Generator::~Generator() = default;
|
||||||
|
|
||||||
bool Generator::Generate() {
|
bool Generator::Generate() {
|
||||||
return true;
|
return impl_.Generate(module_);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace wgsl
|
} // namespace wgsl
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "src/writer/wgsl/generator_impl.h"
|
||||||
#include "src/writer/writer.h"
|
#include "src/writer/writer.h"
|
||||||
|
|
||||||
namespace tint {
|
namespace tint {
|
||||||
@ -36,10 +37,10 @@ class Generator : public writer::Writer {
|
|||||||
bool Generate() override;
|
bool Generate() override;
|
||||||
|
|
||||||
/// @returns the result data
|
/// @returns the result data
|
||||||
const std::string& result() const { return result_; }
|
std::string result() const { return impl_.result(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string result_;
|
GeneratorImpl impl_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace wgsl
|
} // namespace wgsl
|
||||||
|
252
src/writer/wgsl/generator_impl.cc
Normal file
252
src/writer/wgsl/generator_impl.cc
Normal file
@ -0,0 +1,252 @@
|
|||||||
|
// 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/wgsl/generator_impl.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "src/ast/binding_decoration.h"
|
||||||
|
#include "src/ast/builtin_decoration.h"
|
||||||
|
#include "src/ast/decorated_variable.h"
|
||||||
|
#include "src/ast/location_decoration.h"
|
||||||
|
#include "src/ast/set_decoration.h"
|
||||||
|
#include "src/ast/struct.h"
|
||||||
|
#include "src/ast/struct_member.h"
|
||||||
|
#include "src/ast/struct_member_offset_decoration.h"
|
||||||
|
#include "src/ast/type/array_type.h"
|
||||||
|
#include "src/ast/type/matrix_type.h"
|
||||||
|
#include "src/ast/type/pointer_type.h"
|
||||||
|
#include "src/ast/type/struct_type.h"
|
||||||
|
#include "src/ast/type/vector_type.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace wgsl {
|
||||||
|
|
||||||
|
GeneratorImpl::GeneratorImpl() = default;
|
||||||
|
|
||||||
|
GeneratorImpl::~GeneratorImpl() = default;
|
||||||
|
|
||||||
|
bool GeneratorImpl::Generate(const ast::Module& module) {
|
||||||
|
for (const auto& import : module.imports()) {
|
||||||
|
if (!EmitImport(import.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!module.imports().empty()) {
|
||||||
|
out_ << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto& ep : module.entry_points()) {
|
||||||
|
if (!EmitEntryPoint(ep.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!module.entry_points().empty())
|
||||||
|
out_ << std::endl;
|
||||||
|
|
||||||
|
for (const auto& alias : module.alias_types()) {
|
||||||
|
if (!EmitAliasType(alias)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!module.alias_types().empty())
|
||||||
|
out_ << std::endl;
|
||||||
|
|
||||||
|
for (const auto& var : module.global_variables()) {
|
||||||
|
if (!EmitVariable(var.get())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!module.global_variables().empty()) {
|
||||||
|
out_ << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitAliasType(const ast::type::AliasType* alias) {
|
||||||
|
out_ << "type " << alias->name() << " = ";
|
||||||
|
if (!EmitType(alias->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << ";" << std::endl;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitEntryPoint(const ast::EntryPoint* ep) {
|
||||||
|
out_ << "entry_point " << ep->stage() << " ";
|
||||||
|
if (!ep->name().empty() && ep->name() != ep->function_name()) {
|
||||||
|
out_ << R"(as ")" << ep->name() << R"(" )";
|
||||||
|
}
|
||||||
|
out_ << "= " << ep->function_name() << ";" << std::endl;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitImport(const ast::Import* import) {
|
||||||
|
out_ << R"(import ")" << import->path() << R"(" as )" << import->name() << ";"
|
||||||
|
<< std::endl;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitType(ast::type::Type* type) {
|
||||||
|
if (type->IsAlias()) {
|
||||||
|
auto alias = type->AsAlias();
|
||||||
|
out_ << alias->name();
|
||||||
|
} else if (type->IsArray()) {
|
||||||
|
auto ary = type->AsArray();
|
||||||
|
out_ << "array<";
|
||||||
|
if (!EmitType(ary->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ary->IsRuntimeArray())
|
||||||
|
out_ << ", " << ary->size();
|
||||||
|
|
||||||
|
out_ << ">";
|
||||||
|
} else if (type->IsBool()) {
|
||||||
|
out_ << "bool";
|
||||||
|
} else if (type->IsF32()) {
|
||||||
|
out_ << "f32";
|
||||||
|
} else if (type->IsI32()) {
|
||||||
|
out_ << "i32";
|
||||||
|
} else if (type->IsMatrix()) {
|
||||||
|
auto mat = type->AsMatrix();
|
||||||
|
out_ << "mat" << mat->columns() << "x" << mat->rows() << "<";
|
||||||
|
if (!EmitType(mat->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << ">";
|
||||||
|
} else if (type->IsPointer()) {
|
||||||
|
auto ptr = type->AsPointer();
|
||||||
|
out_ << "ptr<" << ptr->storage_class() << ", ";
|
||||||
|
if (!EmitType(ptr->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << ">";
|
||||||
|
} else if (type->IsStruct()) {
|
||||||
|
auto str = type->AsStruct()->impl();
|
||||||
|
if (str->decoration() != ast::StructDecoration::kNone) {
|
||||||
|
out_ << "[[" << str->decoration() << "]] ";
|
||||||
|
}
|
||||||
|
out_ << "struct {" << std::endl;
|
||||||
|
for (const auto& mem : str->members()) {
|
||||||
|
// TODO(dsinclair): This formats bad with nested structs
|
||||||
|
out_ << " ";
|
||||||
|
if (!mem->decorations().empty()) {
|
||||||
|
out_ << "[[";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& deco : mem->decorations()) {
|
||||||
|
if (!first) {
|
||||||
|
out_ << ", ";
|
||||||
|
}
|
||||||
|
|
||||||
|
first = false;
|
||||||
|
// TODO(dsinclair): Split this out when we have more then one
|
||||||
|
assert(deco->IsOffset());
|
||||||
|
|
||||||
|
out_ << "offset " << deco->AsOffset()->offset();
|
||||||
|
}
|
||||||
|
out_ << "]] ";
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << mem->name() << " : ";
|
||||||
|
if (!EmitType(mem->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << ";" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << "}";
|
||||||
|
} else if (type->IsU32()) {
|
||||||
|
out_ << "u32";
|
||||||
|
} else if (type->IsVector()) {
|
||||||
|
auto vec = type->AsVector();
|
||||||
|
out_ << "vec" << vec->size() << "<";
|
||||||
|
if (!EmitType(vec->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_ << ">";
|
||||||
|
} else if (type->IsVoid()) {
|
||||||
|
out_ << "void";
|
||||||
|
} else {
|
||||||
|
error_ = "unknown type in EmitType";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitVariable(ast::Variable* var) {
|
||||||
|
if (var->IsDecorated()) {
|
||||||
|
if (!EmitVariableDecorations(var->AsDecorated())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (var->is_const()) {
|
||||||
|
out_ << "const";
|
||||||
|
} else {
|
||||||
|
out_ << "var";
|
||||||
|
if (var->storage_class() != ast::StorageClass::kNone) {
|
||||||
|
out_ << "<" << var->storage_class() << ">";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out_ << " " << var->name() << " : ";
|
||||||
|
if (!EmitType(var->type())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (var->initializer() != nullptr) {
|
||||||
|
// out_ << " = ";
|
||||||
|
// EmitExpr(var-initializer());
|
||||||
|
}
|
||||||
|
out_ << ";" << std::endl;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GeneratorImpl::EmitVariableDecorations(ast::DecoratedVariable* var) {
|
||||||
|
out_ << "[[";
|
||||||
|
bool first = true;
|
||||||
|
for (const auto& deco : var->decorations()) {
|
||||||
|
if (!first) {
|
||||||
|
out_ << ", ";
|
||||||
|
}
|
||||||
|
first = false;
|
||||||
|
|
||||||
|
if (deco->IsBinding()) {
|
||||||
|
out_ << "binding " << deco->AsBinding()->value();
|
||||||
|
} else if (deco->IsSet()) {
|
||||||
|
out_ << "set " << deco->AsSet()->value();
|
||||||
|
} else if (deco->IsLocation()) {
|
||||||
|
out_ << "location " << deco->AsLocation()->value();
|
||||||
|
} else if (deco->IsBuiltin()) {
|
||||||
|
out_ << "builtin " << deco->AsBuiltin()->value();
|
||||||
|
} else {
|
||||||
|
error_ = "unknown variable decoration";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out_ << "]] ";
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace wgsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
84
src/writer/wgsl/generator_impl.h
Normal file
84
src/writer/wgsl/generator_impl.h
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// 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_WGSL_GENERATOR_IMPL_H_
|
||||||
|
#define SRC_WRITER_WGSL_GENERATOR_IMPL_H_
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "src/ast/entry_point.h"
|
||||||
|
#include "src/ast/import.h"
|
||||||
|
#include "src/ast/module.h"
|
||||||
|
#include "src/ast/type/alias_type.h"
|
||||||
|
#include "src/ast/type/type.h"
|
||||||
|
#include "src/ast/variable.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace wgsl {
|
||||||
|
|
||||||
|
/// Implementation class for WGSL generator
|
||||||
|
class GeneratorImpl {
|
||||||
|
public:
|
||||||
|
/// Constructor
|
||||||
|
GeneratorImpl();
|
||||||
|
~GeneratorImpl();
|
||||||
|
|
||||||
|
/// Generates the result data
|
||||||
|
/// @param module the module to generate
|
||||||
|
/// @returns true on successful generation; false otherwise
|
||||||
|
bool Generate(const ast::Module& module);
|
||||||
|
|
||||||
|
/// @returns the result data
|
||||||
|
std::string result() const { return out_.str(); }
|
||||||
|
|
||||||
|
/// @returns the error from the generator
|
||||||
|
std::string error() const { return error_; }
|
||||||
|
|
||||||
|
/// Handles generating an alias
|
||||||
|
/// @param alias the alias to generate
|
||||||
|
/// @returns true if the alias was emitted
|
||||||
|
bool EmitAliasType(const ast::type::AliasType* alias);
|
||||||
|
/// Handles generating an entry_point command
|
||||||
|
/// @param ep the entry point
|
||||||
|
/// @returns true if the entry point was emitted
|
||||||
|
bool EmitEntryPoint(const ast::EntryPoint* ep);
|
||||||
|
/// Handles generating an import command
|
||||||
|
/// @param import the import to generate
|
||||||
|
/// @returns true if the import was emitted
|
||||||
|
bool EmitImport(const ast::Import* import);
|
||||||
|
/// Handles generating type
|
||||||
|
/// @param type the type to generate
|
||||||
|
/// @returns true if the type is emitted
|
||||||
|
bool EmitType(ast::type::Type* type);
|
||||||
|
/// Handles generating a variable
|
||||||
|
/// @param var the variable to generate
|
||||||
|
/// @returns true if the variable was emitted
|
||||||
|
bool EmitVariable(ast::Variable* var);
|
||||||
|
/// Handles generating variable decorations
|
||||||
|
/// @param var the decorated variable
|
||||||
|
/// @returns true if the variable decoration was emitted
|
||||||
|
bool EmitVariableDecorations(ast::DecoratedVariable* var);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::ostringstream out_;
|
||||||
|
std::string error_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace wgsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
||||||
|
|
||||||
|
#endif // SRC_WRITER_WGSL_GENERATOR_IMPL_H_
|
332
src/writer/wgsl/generator_impl_test.cc
Normal file
332
src/writer/wgsl/generator_impl_test.cc
Normal file
@ -0,0 +1,332 @@
|
|||||||
|
// 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/wgsl/generator_impl.h"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
#include "src/ast/binding_decoration.h"
|
||||||
|
#include "src/ast/builtin.h"
|
||||||
|
#include "src/ast/builtin_decoration.h"
|
||||||
|
#include "src/ast/decorated_variable.h"
|
||||||
|
#include "src/ast/location_decoration.h"
|
||||||
|
#include "src/ast/set_decoration.h"
|
||||||
|
#include "src/ast/struct.h"
|
||||||
|
#include "src/ast/struct_member.h"
|
||||||
|
#include "src/ast/struct_member_decoration.h"
|
||||||
|
#include "src/ast/struct_member_offset_decoration.h"
|
||||||
|
#include "src/ast/type/alias_type.h"
|
||||||
|
#include "src/ast/type/array_type.h"
|
||||||
|
#include "src/ast/type/bool_type.h"
|
||||||
|
#include "src/ast/type/f32_type.h"
|
||||||
|
#include "src/ast/type/i32_type.h"
|
||||||
|
#include "src/ast/type/matrix_type.h"
|
||||||
|
#include "src/ast/type/pointer_type.h"
|
||||||
|
#include "src/ast/type/struct_type.h"
|
||||||
|
#include "src/ast/type/u32_type.h"
|
||||||
|
#include "src/ast/type/vector_type.h"
|
||||||
|
#include "src/ast/type/void_type.h"
|
||||||
|
#include "src/ast/variable.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace wgsl {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using GeneratorImplTest = testing::Test;
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, DISABLED_Generate) {}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitAliasType_F32) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::AliasType alias("a", &f32);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitAliasType(&alias));
|
||||||
|
EXPECT_EQ(g.result(), R"(type a = f32;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, DISABLED_EmitAliasType_Struct) {
|
||||||
|
ast::type::I32Type i32;
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::StructMember>> members;
|
||||||
|
members.push_back(std::make_unique<ast::StructMember>(
|
||||||
|
"a", &f32, std::vector<std::unique_ptr<ast::StructMemberDecoration>>{}));
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::StructMemberDecoration>> b_deco;
|
||||||
|
b_deco.push_back(std::make_unique<ast::StructMemberOffsetDecoration>(4));
|
||||||
|
members.push_back(
|
||||||
|
std::make_unique<ast::StructMember>("b", &i32, std::move(b_deco)));
|
||||||
|
|
||||||
|
auto str = std::make_unique<ast::Struct>();
|
||||||
|
str->set_members(std::move(members));
|
||||||
|
|
||||||
|
ast::type::StructType s(std::move(str));
|
||||||
|
ast::type::AliasType alias("a", &s);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitAliasType(&alias));
|
||||||
|
EXPECT_EQ(g.result(), R"(type a = struct {
|
||||||
|
a: f32;
|
||||||
|
[[offset 4]] b : i32;
|
||||||
|
}
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitEntryPoint_NoName) {
|
||||||
|
ast::EntryPoint ep(ast::PipelineStage::kFragment, "", "frag_main");
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitEntryPoint(&ep));
|
||||||
|
EXPECT_EQ(g.result(), R"(entry_point fragment = frag_main;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitEntryPoint_WithName) {
|
||||||
|
ast::EntryPoint ep(ast::PipelineStage::kFragment, "main", "frag_main");
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitEntryPoint(&ep));
|
||||||
|
EXPECT_EQ(g.result(), R"(entry_point fragment as "main" = frag_main;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitImport) {
|
||||||
|
ast::Import import("GLSL.std.450", "std::glsl");
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitImport(&import));
|
||||||
|
EXPECT_EQ(g.result(), R"(import "GLSL.std.450" as std::glsl;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Alias) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::AliasType alias("alias", &f32);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&alias));
|
||||||
|
EXPECT_EQ(g.result(), "alias");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Array) {
|
||||||
|
ast::type::BoolType b;
|
||||||
|
ast::type::ArrayType a(&b, 4);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&a));
|
||||||
|
EXPECT_EQ(g.result(), "array<bool, 4>");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_RuntimeArray) {
|
||||||
|
ast::type::BoolType b;
|
||||||
|
ast::type::ArrayType a(&b);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&a));
|
||||||
|
EXPECT_EQ(g.result(), "array<bool>");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Bool) {
|
||||||
|
ast::type::BoolType b;
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&b));
|
||||||
|
EXPECT_EQ(g.result(), "bool");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_F32) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&f32));
|
||||||
|
EXPECT_EQ(g.result(), "f32");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_I32) {
|
||||||
|
ast::type::I32Type i32;
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&i32));
|
||||||
|
EXPECT_EQ(g.result(), "i32");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Matrix) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::MatrixType m(&f32, 3, 2);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&m));
|
||||||
|
EXPECT_EQ(g.result(), "mat2x3<f32>");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Pointer) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::PointerType p(&f32, ast::StorageClass::kWorkgroup);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&p));
|
||||||
|
EXPECT_EQ(g.result(), "ptr<workgroup, f32>");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Struct) {
|
||||||
|
ast::type::I32Type i32;
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::StructMember>> members;
|
||||||
|
members.push_back(std::make_unique<ast::StructMember>(
|
||||||
|
"a", &i32, std::vector<std::unique_ptr<ast::StructMemberDecoration>>{}));
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::StructMemberDecoration>> b_deco;
|
||||||
|
b_deco.push_back(std::make_unique<ast::StructMemberOffsetDecoration>(4));
|
||||||
|
members.push_back(
|
||||||
|
std::make_unique<ast::StructMember>("b", &f32, std::move(b_deco)));
|
||||||
|
|
||||||
|
auto str = std::make_unique<ast::Struct>();
|
||||||
|
str->set_members(std::move(members));
|
||||||
|
|
||||||
|
ast::type::StructType s(std::move(str));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&s));
|
||||||
|
EXPECT_EQ(g.result(), R"(struct {
|
||||||
|
a : i32;
|
||||||
|
[[offset 4]] b : f32;
|
||||||
|
})");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Struct_WithDecoration) {
|
||||||
|
ast::type::I32Type i32;
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::StructMember>> members;
|
||||||
|
members.push_back(std::make_unique<ast::StructMember>(
|
||||||
|
"a", &i32, std::vector<std::unique_ptr<ast::StructMemberDecoration>>{}));
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::StructMemberDecoration>> b_deco;
|
||||||
|
b_deco.push_back(std::make_unique<ast::StructMemberOffsetDecoration>(4));
|
||||||
|
members.push_back(
|
||||||
|
std::make_unique<ast::StructMember>("b", &f32, std::move(b_deco)));
|
||||||
|
|
||||||
|
auto str = std::make_unique<ast::Struct>();
|
||||||
|
str->set_members(std::move(members));
|
||||||
|
str->set_decoration(ast::StructDecoration::kBlock);
|
||||||
|
|
||||||
|
ast::type::StructType s(std::move(str));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&s));
|
||||||
|
EXPECT_EQ(g.result(), R"([[block]] struct {
|
||||||
|
a : i32;
|
||||||
|
[[offset 4]] b : f32;
|
||||||
|
})");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_U32) {
|
||||||
|
ast::type::U32Type u32;
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&u32));
|
||||||
|
EXPECT_EQ(g.result(), "u32");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Vector) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::type::VectorType v(&f32, 3);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&v));
|
||||||
|
EXPECT_EQ(g.result(), "vec3<f32>");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitType_Void) {
|
||||||
|
ast::type::VoidType v;
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitType(&v));
|
||||||
|
EXPECT_EQ(g.result(), "void");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitVariable) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::Variable v("a", ast::StorageClass::kNone, &f32);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitVariable(&v));
|
||||||
|
EXPECT_EQ(g.result(), R"(var a : f32;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitVariable_StorageClass) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
ast::Variable v("a", ast::StorageClass::kInput, &f32);
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitVariable(&v));
|
||||||
|
EXPECT_EQ(g.result(), R"(var<in> a : f32;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitVariable_Decorated) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::VariableDecoration>> decos;
|
||||||
|
decos.push_back(std::make_unique<ast::LocationDecoration>(2));
|
||||||
|
|
||||||
|
ast::DecoratedVariable dv;
|
||||||
|
dv.set_name("a");
|
||||||
|
dv.set_type(&f32);
|
||||||
|
dv.set_decorations(std::move(decos));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitVariable(&dv));
|
||||||
|
EXPECT_EQ(g.result(), R"([[location 2]] var a : f32;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, EmitVariable_Decorated_Multiple) {
|
||||||
|
ast::type::F32Type f32;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<ast::VariableDecoration>> decos;
|
||||||
|
decos.push_back(
|
||||||
|
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kPosition));
|
||||||
|
decos.push_back(std::make_unique<ast::BindingDecoration>(0));
|
||||||
|
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||||
|
decos.push_back(std::make_unique<ast::LocationDecoration>(2));
|
||||||
|
|
||||||
|
ast::DecoratedVariable dv;
|
||||||
|
dv.set_name("a");
|
||||||
|
dv.set_type(&f32);
|
||||||
|
dv.set_decorations(std::move(decos));
|
||||||
|
|
||||||
|
GeneratorImpl g;
|
||||||
|
ASSERT_TRUE(g.EmitVariable(&dv));
|
||||||
|
EXPECT_EQ(g.result(),
|
||||||
|
R"([[builtin position, binding 0, set 1, location 2]] var a : f32;
|
||||||
|
)");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, DISABLED_EmitVariable_Initializer) {}
|
||||||
|
|
||||||
|
TEST_F(GeneratorImplTest, DISABLED_EmitVariable_Const) {}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
} // namespace wgsl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
Loading…
x
Reference in New Issue
Block a user