[hlsl-writer] Refactor output emission.
This CL updates the HLSL backend to take the output stream as a parameter. This is needed because there are cases where we have to generate the resulting stream out of order. This will allow that to happen. Bug: tint:7 Change-Id: Id1877a07e536a84da0555f207d1030588d44c034 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/27440 Commit-Queue: dan sinclair <dsinclair@chromium.org> Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
parent
bdb86723e8
commit
df503f0e85
2
BUILD.gn
2
BUILD.gn
|
@ -1088,6 +1088,8 @@ source_set("tint_unittests_hlsl_writer_src") {
|
|||
"src/writer/hlsl/generator_impl_unary_op_test.cc",
|
||||
"src/writer/hlsl/generator_impl_variable_decl_statement_test.cc",
|
||||
"src/writer/hlsl/namer_test.cc",
|
||||
"src/writer/hlsl/test_helper.cc",
|
||||
"src/writer/hlsl/test_helper.h",
|
||||
]
|
||||
|
||||
configs += [
|
||||
|
|
|
@ -599,6 +599,8 @@ if (${TINT_BUILD_HLSL_WRITER})
|
|||
writer/hlsl/generator_impl_unary_op_test.cc
|
||||
writer/hlsl/generator_impl_variable_decl_statement_test.cc
|
||||
writer/hlsl/namer_test.cc
|
||||
writer/hlsl/test_helper.cc
|
||||
writer/hlsl/test_helper.h
|
||||
)
|
||||
endif()
|
||||
|
||||
|
|
|
@ -831,6 +831,7 @@ ast::type::Type* TypeDeterminer::GetImportData(
|
|||
const ast::ExpressionList& params,
|
||||
uint32_t* id) {
|
||||
if (path != "GLSL.std.450") {
|
||||
set_error(source, "unknown import path " + path);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ Generator::Generator(ast::Module module)
|
|||
Generator::~Generator() = default;
|
||||
|
||||
bool Generator::Generate() {
|
||||
auto ret = impl_.Generate();
|
||||
auto ret = impl_.Generate(out_);
|
||||
if (!ret) {
|
||||
error_ = impl_.error();
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ bool Generator::Generate() {
|
|||
}
|
||||
|
||||
std::string Generator::result() const {
|
||||
return impl_.result();
|
||||
return out_.str();
|
||||
}
|
||||
|
||||
std::string Generator::error() const {
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#ifndef SRC_WRITER_HLSL_GENERATOR_H_
|
||||
#define SRC_WRITER_HLSL_GENERATOR_H_
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
@ -43,6 +44,7 @@ class Generator : public Text {
|
|||
std::string error() const;
|
||||
|
||||
private:
|
||||
std::ostringstream out_;
|
||||
GeneratorImpl impl_;
|
||||
};
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -21,182 +21,247 @@
|
|||
#include "src/ast/type_constructor_expression.h"
|
||||
#include "src/scope_stack.h"
|
||||
#include "src/writer/hlsl/namer.h"
|
||||
#include "src/writer/text_generator.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
|
||||
/// Implementation class for HLSL generator
|
||||
class GeneratorImpl : public TextGenerator {
|
||||
class GeneratorImpl {
|
||||
public:
|
||||
/// Constructor
|
||||
/// @param module the module to generate
|
||||
explicit GeneratorImpl(ast::Module* module);
|
||||
~GeneratorImpl();
|
||||
|
||||
/// Increment the emitter indent level
|
||||
void increment_indent() { indent_ += 2; }
|
||||
/// Decrement the emiter indent level
|
||||
void decrement_indent() {
|
||||
if (indent_ < 2) {
|
||||
indent_ = 0;
|
||||
return;
|
||||
}
|
||||
indent_ -= 2;
|
||||
}
|
||||
|
||||
/// Writes the current indent to the output stream
|
||||
/// @param out the output stream
|
||||
void make_indent(std::ostream& out);
|
||||
|
||||
/// @returns the error
|
||||
std::string error() const { return error_; }
|
||||
|
||||
/// @param out the output stream
|
||||
/// @returns true on successful generation; false otherwise
|
||||
bool Generate();
|
||||
bool Generate(std::ostream& out);
|
||||
|
||||
/// Handles generating an alias
|
||||
/// @param out the output stream
|
||||
/// @param alias the alias to generate
|
||||
/// @returns true if the alias was emitted
|
||||
bool EmitAliasType(const ast::type::AliasType* alias);
|
||||
bool EmitAliasType(std::ostream& out, const ast::type::AliasType* alias);
|
||||
/// Handles an array accessor expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the expression to emit
|
||||
/// @returns true if the array accessor was emitted
|
||||
bool EmitArrayAccessor(ast::ArrayAccessorExpression* expr);
|
||||
bool EmitArrayAccessor(std::ostream& out, ast::ArrayAccessorExpression* expr);
|
||||
/// Handles generating an as expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the as expression
|
||||
/// @returns true if the as was emitted
|
||||
bool EmitAs(ast::AsExpression* expr);
|
||||
bool EmitAs(std::ostream& out, ast::AsExpression* expr);
|
||||
/// Handles an assignment statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
bool EmitAssign(ast::AssignmentStatement* stmt);
|
||||
bool EmitAssign(std::ostream& out, ast::AssignmentStatement* stmt);
|
||||
/// Handles generating a binary expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the binary expression
|
||||
/// @returns true if the expression was emitted, false otherwise
|
||||
bool EmitBinary(ast::BinaryExpression* expr);
|
||||
bool EmitBinary(std::ostream& out, ast::BinaryExpression* expr);
|
||||
/// Handles a block statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
bool EmitBlock(const ast::BlockStatement* stmt);
|
||||
bool EmitBlock(std::ostream& out, const ast::BlockStatement* stmt);
|
||||
/// Handles a block statement with a newline at the end
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
bool EmitIndentedBlockAndNewline(ast::BlockStatement* stmt);
|
||||
bool EmitIndentedBlockAndNewline(std::ostream& out,
|
||||
ast::BlockStatement* stmt);
|
||||
/// Handles a block statement with a newline at the end
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
bool EmitBlockAndNewline(const ast::BlockStatement* stmt);
|
||||
bool EmitBlockAndNewline(std::ostream& out, const ast::BlockStatement* stmt);
|
||||
/// Handles a break statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
bool EmitBreak(ast::BreakStatement* stmt);
|
||||
bool EmitBreak(std::ostream& out, ast::BreakStatement* stmt);
|
||||
/// Handles generating a call expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the call expression
|
||||
/// @returns true if the call expression is emitted
|
||||
bool EmitCall(ast::CallExpression* expr);
|
||||
bool EmitCall(std::ostream& out, ast::CallExpression* expr);
|
||||
/// Handles a case statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement
|
||||
/// @returns true if the statment was emitted successfully
|
||||
bool EmitCase(ast::CaseStatement* stmt);
|
||||
bool EmitCase(std::ostream& out, ast::CaseStatement* stmt);
|
||||
/// Handles generating a cast expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the cast expression
|
||||
/// @returns true if the cast was emitted
|
||||
bool EmitCast(ast::CastExpression* expr);
|
||||
bool EmitCast(std::ostream& out, ast::CastExpression* expr);
|
||||
/// Handles generating constructor expressions
|
||||
/// @param out the output stream
|
||||
/// @param expr the constructor expression
|
||||
/// @returns true if the expression was emitted
|
||||
bool EmitConstructor(ast::ConstructorExpression* expr);
|
||||
bool EmitConstructor(std::ostream& out, ast::ConstructorExpression* expr);
|
||||
/// Handles generating a discard statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the discard statement
|
||||
/// @returns true if the statement was successfully emitted
|
||||
bool EmitDiscard(ast::DiscardStatement* stmt);
|
||||
bool EmitDiscard(std::ostream& out, ast::DiscardStatement* stmt);
|
||||
/// Handles generating a scalar constructor
|
||||
/// @param out the output stream
|
||||
/// @param expr the scalar constructor expression
|
||||
/// @returns true if the scalar constructor is emitted
|
||||
bool EmitScalarConstructor(ast::ScalarConstructorExpression* expr);
|
||||
bool EmitScalarConstructor(std::ostream& out,
|
||||
ast::ScalarConstructorExpression* expr);
|
||||
/// Handles emitting a type constructor
|
||||
/// @param out the output stream
|
||||
/// @param expr the type constructor expression
|
||||
/// @returns true if the constructor is emitted
|
||||
bool EmitTypeConstructor(ast::TypeConstructorExpression* expr);
|
||||
bool EmitTypeConstructor(std::ostream& out,
|
||||
ast::TypeConstructorExpression* expr);
|
||||
/// Handles a continue statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted successfully
|
||||
bool EmitContinue(ast::ContinueStatement* stmt);
|
||||
bool EmitContinue(std::ostream& out, ast::ContinueStatement* stmt);
|
||||
/// Handles generating an else statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted
|
||||
bool EmitElse(ast::ElseStatement* stmt);
|
||||
bool EmitElse(std::ostream& out, ast::ElseStatement* stmt);
|
||||
/// Handles generate an Expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the expression
|
||||
/// @returns true if the expression was emitted
|
||||
bool EmitExpression(ast::Expression* expr);
|
||||
bool EmitExpression(std::ostream& out, ast::Expression* expr);
|
||||
/// Handles generating a function
|
||||
/// @param out the output stream
|
||||
/// @param func the function to generate
|
||||
/// @returns true if the function was emitted
|
||||
bool EmitFunction(ast::Function* func);
|
||||
bool EmitFunction(std::ostream& out, ast::Function* func);
|
||||
/// Internal helper for emitting functions
|
||||
/// @param out the output stream
|
||||
/// @param func the function to emit
|
||||
/// @param emit_duplicate_functions set true if we need to duplicate per entry
|
||||
/// point
|
||||
/// @param ep_name the current entry point or blank if none set
|
||||
/// @returns true if the function was emitted.
|
||||
bool EmitFunctionInternal(ast::Function* func,
|
||||
bool EmitFunctionInternal(std::ostream& out,
|
||||
ast::Function* func,
|
||||
bool emit_duplicate_functions,
|
||||
const std::string& ep_name);
|
||||
/// Handles emitting information for an entry point
|
||||
/// @param out the output stream
|
||||
/// @param ep the entry point
|
||||
/// @returns true if the entry point data was emitted
|
||||
bool EmitEntryPointData(ast::EntryPoint* ep);
|
||||
bool EmitEntryPointData(std::ostream& out, ast::EntryPoint* ep);
|
||||
/// Handles emitting the entry point function
|
||||
/// @param out the output stream
|
||||
/// @param ep the entry point
|
||||
/// @returns true if the entry point function was emitted
|
||||
bool EmitEntryPointFunction(ast::EntryPoint* ep);
|
||||
bool EmitEntryPointFunction(std::ostream& out, ast::EntryPoint* ep);
|
||||
/// Handles an if statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was successfully emitted
|
||||
bool EmitIf(ast::IfStatement* stmt);
|
||||
bool EmitIf(std::ostream& out, ast::IfStatement* stmt);
|
||||
/// Handles genreating an import expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the expression
|
||||
/// @returns true if the expression was successfully emitted.
|
||||
bool EmitImportFunction(ast::CallExpression* expr);
|
||||
bool EmitImportFunction(std::ostream& out, ast::CallExpression* expr);
|
||||
/// Handles a literal
|
||||
/// @param out the output stream
|
||||
/// @param lit the literal to emit
|
||||
/// @returns true if the literal was successfully emitted
|
||||
bool EmitLiteral(ast::Literal* lit);
|
||||
bool EmitLiteral(std::ostream& out, ast::Literal* lit);
|
||||
/// Handles a loop statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted
|
||||
bool EmitLoop(ast::LoopStatement* stmt);
|
||||
bool EmitLoop(std::ostream& out, ast::LoopStatement* stmt);
|
||||
/// Handles generating an identifier expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the identifier expression
|
||||
/// @returns true if the identifeir was emitted
|
||||
bool EmitIdentifier(ast::IdentifierExpression* expr);
|
||||
bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr);
|
||||
/// Handles a member accessor expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the member accessor expression
|
||||
/// @returns true if the member accessor was emitted
|
||||
bool EmitMemberAccessor(ast::MemberAccessorExpression* expr);
|
||||
bool EmitMemberAccessor(std::ostream& out,
|
||||
ast::MemberAccessorExpression* expr);
|
||||
/// Handles a storage buffer accessor expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the storage buffer accessor expression
|
||||
/// @param rhs the right side of a store expression. Set to nullptr for a load
|
||||
/// @returns true if the storage buffer accessor was emitted
|
||||
bool EmitStorageBufferAccessor(ast::Expression* expr, ast::Expression* rhs);
|
||||
bool EmitStorageBufferAccessor(std::ostream& out,
|
||||
ast::Expression* expr,
|
||||
ast::Expression* rhs);
|
||||
/// Handles return statements
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was successfully emitted
|
||||
bool EmitReturn(ast::ReturnStatement* stmt);
|
||||
bool EmitReturn(std::ostream& out, ast::ReturnStatement* stmt);
|
||||
/// Handles statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted
|
||||
bool EmitStatement(ast::Statement* stmt);
|
||||
bool EmitStatement(std::ostream& out, ast::Statement* stmt);
|
||||
/// Handles generating a switch statement
|
||||
/// @param out the output stream
|
||||
/// @param stmt the statement to emit
|
||||
/// @returns true if the statement was emitted
|
||||
bool EmitSwitch(ast::SwitchStatement* stmt);
|
||||
bool EmitSwitch(std::ostream& out, ast::SwitchStatement* stmt);
|
||||
/// Handles generating type
|
||||
/// @param out the output stream
|
||||
/// @param type the type to generate
|
||||
/// @param name the name of the variable, only used for array emission
|
||||
/// @returns true if the type is emitted
|
||||
bool EmitType(ast::type::Type* type, const std::string& name);
|
||||
bool EmitType(std::ostream& out,
|
||||
ast::type::Type* type,
|
||||
const std::string& name);
|
||||
/// Handles a unary op expression
|
||||
/// @param out the output stream
|
||||
/// @param expr the expression to emit
|
||||
/// @returns true if the expression was emitted
|
||||
bool EmitUnaryOp(ast::UnaryOpExpression* expr);
|
||||
bool EmitUnaryOp(std::ostream& out, ast::UnaryOpExpression* expr);
|
||||
/// Emits the zero value for the given type
|
||||
/// @param out the output stream
|
||||
/// @param type the type to emit the value for
|
||||
/// @returns true if the zero value was successfully emitted.
|
||||
bool EmitZeroValue(ast::type::Type* type);
|
||||
bool EmitZeroValue(std::ostream& out, ast::type::Type* type);
|
||||
/// Handles generating a variable
|
||||
/// @param out the output stream
|
||||
/// @param var the variable to generate
|
||||
/// @returns true if the variable was emitted
|
||||
bool EmitVariable(ast::Variable* var);
|
||||
bool EmitVariable(std::ostream& out, ast::Variable* var);
|
||||
/// Handles generating a program scope constant variable
|
||||
/// @param out the output stream
|
||||
/// @param var the variable to emit
|
||||
/// @returns true if the variable was emitted
|
||||
bool EmitProgramConstVariable(const ast::Variable* var);
|
||||
bool EmitProgramConstVariable(std::ostream& out, const ast::Variable* var);
|
||||
|
||||
/// Returns true if the accessor is accessing a storage buffer.
|
||||
/// @param expr the expression to check
|
||||
|
@ -252,6 +317,9 @@ class GeneratorImpl : public TextGenerator {
|
|||
|
||||
std::string current_ep_var_name(VarType type);
|
||||
|
||||
std::string error_;
|
||||
size_t indent_ = 0;
|
||||
|
||||
Namer namer_;
|
||||
ast::Module* module_ = nullptr;
|
||||
std::string current_ep_name_;
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/struct.h"
|
||||
#include "src/ast/struct_member.h"
|
||||
|
@ -21,38 +20,35 @@
|
|||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/ast/type/struct_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_AliasType : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitAliasType_F32) {
|
||||
TEST_F(HlslGeneratorImplTest_AliasType, EmitAliasType_F32) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::AliasType alias("a", &f32);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(typedef float a;
|
||||
ASSERT_TRUE(gen().EmitAliasType(out(), &alias)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(typedef float a;
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitAliasType_NameCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_AliasType, EmitAliasType_NameCollision) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::AliasType alias("float", &f32);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(typedef float float_tint_0;
|
||||
ASSERT_TRUE(gen().EmitAliasType(out(), &alias)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(typedef float float_tint_0;
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitAliasType_Struct) {
|
||||
TEST_F(HlslGeneratorImplTest_AliasType, EmitAliasType_Struct) {
|
||||
ast::type::I32Type i32;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
|
@ -73,8 +69,8 @@ TEST_F(HlslGeneratorImplTest, EmitAliasType_Struct) {
|
|||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitAliasType(&alias)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct a {
|
||||
ASSERT_TRUE(gen().EmitAliasType(out(), &alias)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct a {
|
||||
float a;
|
||||
int b;
|
||||
};
|
||||
|
|
|
@ -14,23 +14,23 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/array_accessor_expression.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/scalar_constructor_expression.h"
|
||||
#include "src/ast/sint_literal.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Expression : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_ArrayAccessor) {
|
||||
TEST_F(HlslGeneratorImplTest_Expression, EmitExpression_ArrayAccessor) {
|
||||
ast::type::I32Type i32;
|
||||
auto lit = std::make_unique<ast::SintLiteral>(&i32, 5);
|
||||
auto idx = std::make_unique<ast::ScalarConstructorExpression>(std::move(lit));
|
||||
|
@ -38,22 +38,18 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_ArrayAccessor) {
|
|||
|
||||
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "ary[5]");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "ary[5]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitArrayAccessor) {
|
||||
TEST_F(HlslGeneratorImplTest_Expression, EmitArrayAccessor) {
|
||||
auto ary = std::make_unique<ast::IdentifierExpression>("ary");
|
||||
auto idx = std::make_unique<ast::IdentifierExpression>("idx");
|
||||
|
||||
ast::ArrayAccessorExpression expr(std::move(ary), std::move(idx));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "ary[idx]");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "ary[idx]");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,53 +14,46 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/as_expression.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/ast/type/u32_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_As : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Float) {
|
||||
TEST_F(HlslGeneratorImplTest_As, EmitExpression_As_Float) {
|
||||
ast::type::F32Type f32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::AsExpression as(&f32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(id)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &as)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(id)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Int) {
|
||||
TEST_F(HlslGeneratorImplTest_As, EmitExpression_As_Int) {
|
||||
ast::type::I32Type i32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::AsExpression as(&i32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asint(id)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &as)) << gen().error();
|
||||
EXPECT_EQ(result(), "asint(id)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_As_Uint) {
|
||||
TEST_F(HlslGeneratorImplTest_As, EmitExpression_As_Uint) {
|
||||
ast::type::U32Type u32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::AsExpression as(&u32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&as)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asuint(id)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &as)) << gen().error();
|
||||
EXPECT_EQ(result(), "asuint(id)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -15,30 +15,27 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/assignment_statement.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Assign : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Assign) {
|
||||
TEST_F(HlslGeneratorImplTest_Assign, Emit_Assign) {
|
||||
auto lhs = std::make_unique<ast::IdentifierExpression>("lhs");
|
||||
auto rhs = std::make_unique<ast::IdentifierExpression>("rhs");
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
EXPECT_EQ(g.result(), " lhs = rhs;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(result(), " lhs = rhs;\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,11 +14,10 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/binary_expression.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
|
@ -33,7 +32,9 @@ inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
|
|||
out << data.op;
|
||||
return out;
|
||||
}
|
||||
using HlslBinaryTest = testing::TestWithParam<BinaryData>;
|
||||
|
||||
class HlslBinaryTest : public TestHelper,
|
||||
public testing::TestWithParam<BinaryData> {};
|
||||
TEST_P(HlslBinaryTest, Emit) {
|
||||
auto params = GetParam();
|
||||
|
||||
|
@ -42,10 +43,8 @@ TEST_P(HlslBinaryTest, Emit) {
|
|||
|
||||
ast::BinaryExpression expr(params.op, std::move(left), std::move(right));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), params.result);
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), params.result);
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
HlslGeneratorImplTest,
|
||||
|
|
|
@ -14,43 +14,38 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/block_statement.h"
|
||||
#include "src/ast/discard_statement.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Block : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Block) {
|
||||
TEST_F(HlslGeneratorImplTest_Block, Emit_Block) {
|
||||
ast::BlockStatement b;
|
||||
b.append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &b)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( {
|
||||
discard;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Block_WithoutNewline) {
|
||||
TEST_F(HlslGeneratorImplTest_Block, Emit_Block_WithoutNewline) {
|
||||
ast::BlockStatement b;
|
||||
b.append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitBlock(&b)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"({
|
||||
ASSERT_TRUE(gen().EmitBlock(out(), &b)) << gen().error();
|
||||
EXPECT_EQ(result(), R"({
|
||||
discard;
|
||||
})");
|
||||
}
|
||||
|
|
|
@ -15,27 +15,24 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/break_statement.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Break : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Break) {
|
||||
TEST_F(HlslGeneratorImplTest_Break, Emit_Break) {
|
||||
ast::BreakStatement b;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&b)) << g.error();
|
||||
EXPECT_EQ(g.result(), " break;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &b)) << gen().error();
|
||||
EXPECT_EQ(result(), " break;\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,23 +14,22 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/call_expression.h"
|
||||
#include "src/ast/call_statement.h"
|
||||
#include "src/ast/function.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/void_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Call : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
|
||||
TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithoutParams) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
|
||||
|
@ -38,16 +37,13 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
|
|||
|
||||
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
|
||||
&void_type);
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
|
||||
EXPECT_EQ(g.result(), "my_func()");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
|
||||
EXPECT_EQ(result(), "my_func()");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithParams) {
|
||||
TEST_F(HlslGeneratorImplTest_Call, EmitExpression_Call_WithParams) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
|
||||
|
@ -58,16 +54,13 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_Call_WithParams) {
|
|||
|
||||
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
|
||||
&void_type);
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
|
||||
EXPECT_EQ(g.result(), "my_func(param1, param2)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
|
||||
EXPECT_EQ(result(), "my_func(param1, param2)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitStatement_Call) {
|
||||
TEST_F(HlslGeneratorImplTest_Call, EmitStatement_Call) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("my_func");
|
||||
|
@ -79,14 +72,10 @@ TEST_F(HlslGeneratorImplTest, EmitStatement_Call) {
|
|||
|
||||
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
|
||||
&void_type);
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
ASSERT_TRUE(g.EmitStatement(&call)) << g.error();
|
||||
EXPECT_EQ(g.result(), " my_func(param1, param2);\n");
|
||||
mod()->AddFunction(std::move(func));
|
||||
gen().increment_indent();
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &call)) << gen().error();
|
||||
EXPECT_EQ(result(), " my_func(param1, param2);\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/break_statement.h"
|
||||
#include "src/ast/case_statement.h"
|
||||
#include "src/ast/fallthrough_statement.h"
|
||||
|
@ -22,16 +21,16 @@
|
|||
#include "src/ast/module.h"
|
||||
#include "src/ast/sint_literal.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Case : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Case) {
|
||||
TEST_F(HlslGeneratorImplTest_Case, Emit_Case) {
|
||||
ast::type::I32Type i32;
|
||||
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
|
@ -41,36 +40,32 @@ TEST_F(HlslGeneratorImplTest, Emit_Case) {
|
|||
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
|
||||
ast::CaseStatement c(std::move(lit), std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( case 5: {
|
||||
ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( case 5: {
|
||||
break;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Case_BreaksByDefault) {
|
||||
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_BreaksByDefault) {
|
||||
ast::type::I32Type i32;
|
||||
|
||||
ast::CaseSelectorList lit;
|
||||
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
|
||||
ast::CaseStatement c(std::move(lit), std::make_unique<ast::BlockStatement>());
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( case 5: {
|
||||
ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( case 5: {
|
||||
break;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Case_WithFallthrough) {
|
||||
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_WithFallthrough) {
|
||||
ast::type::I32Type i32;
|
||||
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
|
@ -80,18 +75,16 @@ TEST_F(HlslGeneratorImplTest, Emit_Case_WithFallthrough) {
|
|||
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 5));
|
||||
ast::CaseStatement c(std::move(lit), std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( case 5: {
|
||||
ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( case 5: {
|
||||
/* fallthrough */
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Case_MultipleSelectors) {
|
||||
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_MultipleSelectors) {
|
||||
ast::type::I32Type i32;
|
||||
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
|
@ -102,31 +95,27 @@ TEST_F(HlslGeneratorImplTest, Emit_Case_MultipleSelectors) {
|
|||
lit.push_back(std::make_unique<ast::SintLiteral>(&i32, 6));
|
||||
ast::CaseStatement c(std::move(lit), std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( case 5:
|
||||
ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( case 5:
|
||||
case 6: {
|
||||
break;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Case_Default) {
|
||||
TEST_F(HlslGeneratorImplTest_Case, Emit_Case_Default) {
|
||||
ast::CaseStatement c;
|
||||
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::BreakStatement>());
|
||||
c.set_body(std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitCase(&c)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( default: {
|
||||
ASSERT_TRUE(gen().EmitCase(out(), &c)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( default: {
|
||||
break;
|
||||
}
|
||||
)");
|
||||
|
|
|
@ -14,43 +14,38 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/cast_expression.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Cast : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_Cast_Scalar) {
|
||||
TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Scalar) {
|
||||
ast::type::F32Type f32;
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::CastExpression cast(&f32, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
|
||||
EXPECT_EQ(g.result(), "float(id)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &cast)) << gen().error();
|
||||
EXPECT_EQ(result(), "float(id)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_Cast_Vector) {
|
||||
TEST_F(HlslGeneratorImplTest_Cast, EmitExpression_Cast_Vector) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec3(&f32, 3);
|
||||
|
||||
auto id = std::make_unique<ast::IdentifierExpression>("id");
|
||||
ast::CastExpression cast(&vec3, std::move(id));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&cast)) << g.error();
|
||||
EXPECT_EQ(g.result(), "vector<float, 3>(id)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &cast)) << gen().error();
|
||||
EXPECT_EQ(result(), "vector<float, 3>(id)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/bool_literal.h"
|
||||
#include "src/ast/float_literal.h"
|
||||
#include "src/ast/module.h"
|
||||
|
@ -27,61 +26,54 @@
|
|||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/ast/type_constructor_expression.h"
|
||||
#include "src/ast/uint_literal.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Constructor : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Bool) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Bool) {
|
||||
ast::type::BoolType bool_type;
|
||||
auto lit = std::make_unique<ast::BoolLiteral>(&bool_type, false);
|
||||
ast::ScalarConstructorExpression expr(std::move(lit));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "false");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "false");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Int) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Int) {
|
||||
ast::type::I32Type i32;
|
||||
auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
|
||||
ast::ScalarConstructorExpression expr(std::move(lit));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "-12345");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "-12345");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_UInt) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_UInt) {
|
||||
ast::type::U32Type u32;
|
||||
auto lit = std::make_unique<ast::UintLiteral>(&u32, 56779);
|
||||
ast::ScalarConstructorExpression expr(std::move(lit));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "56779u");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "56779u");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Float) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Float) {
|
||||
ast::type::F32Type f32;
|
||||
// Use a number close to 1<<30 but whose decimal representation ends in 0.
|
||||
auto lit = std::make_unique<ast::FloatLiteral>(&f32, float((1 << 30) - 4));
|
||||
ast::ScalarConstructorExpression expr(std::move(lit));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "1.07374182e+09f");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "1.07374182e+09f");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Float) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Float) {
|
||||
ast::type::F32Type f32;
|
||||
|
||||
auto lit = std::make_unique<ast::FloatLiteral>(&f32, -1.2e-5);
|
||||
|
@ -91,13 +83,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Float) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&f32, std::move(values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "float(-1.20000004e-05f)");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "float(-1.20000004e-05f)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Bool) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Bool) {
|
||||
ast::type::BoolType b;
|
||||
|
||||
auto lit = std::make_unique<ast::BoolLiteral>(&b, true);
|
||||
|
@ -107,13 +97,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Bool) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&b, std::move(values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool(true)");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "bool(true)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Int) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Int) {
|
||||
ast::type::I32Type i32;
|
||||
|
||||
auto lit = std::make_unique<ast::SintLiteral>(&i32, -12345);
|
||||
|
@ -123,13 +111,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Int) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&i32, std::move(values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "int(-12345)");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "int(-12345)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Uint) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Uint) {
|
||||
ast::type::U32Type u32;
|
||||
|
||||
auto lit = std::make_unique<ast::UintLiteral>(&u32, 12345);
|
||||
|
@ -139,13 +125,11 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Uint) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&u32, std::move(values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "uint(12345u)");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "uint(12345u)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec(&f32, 3);
|
||||
|
||||
|
@ -162,27 +146,23 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&vec, std::move(values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(),
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(),
|
||||
"vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Vec_Empty) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Vec_Empty) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec(&f32, 3);
|
||||
|
||||
ast::ExpressionList values;
|
||||
ast::TypeConstructorExpression expr(&vec, std::move(values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "vector<float, 3>(0.0f)");
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "vector<float, 3>(0.0f)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Mat) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Mat) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::MatrixType mat(&f32, 3, 2); // 3 ROWS, 2 COLUMNS
|
||||
ast::type::VectorType vec(&f32, 3);
|
||||
|
@ -214,19 +194,17 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Mat) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&mat, std::move(mat_values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
|
||||
// A matrix of type T with n columns and m rows can also be constructed from
|
||||
// n vectors of type T with m components.
|
||||
EXPECT_EQ(g.result(),
|
||||
EXPECT_EQ(result(),
|
||||
std::string("matrix<float, 3, 2>(vector<float, 3>(1.00000000f, "
|
||||
"2.00000000f, 3.00000000f), ") +
|
||||
"vector<float, 3>(3.00000000f, 4.00000000f, 5.00000000f))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) {
|
||||
TEST_F(HlslGeneratorImplTest_Constructor, EmitConstructor_Type_Array) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec(&f32, 3);
|
||||
ast::type::ArrayType ary(&vec, 3);
|
||||
|
@ -255,10 +233,8 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) {
|
|||
|
||||
ast::TypeConstructorExpression expr(&ary, std::move(ary_values));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitConstructor(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(),
|
||||
ASSERT_TRUE(gen().EmitConstructor(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(),
|
||||
std::string("{") +
|
||||
"vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), " +
|
||||
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f), " +
|
||||
|
@ -266,7 +242,8 @@ TEST_F(HlslGeneratorImplTest, EmitConstructor_Type_Array) {
|
|||
}
|
||||
|
||||
// TODO(dsinclair): Add struct constructor test.
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitConstructor_Type_Struct) {}
|
||||
TEST_F(HlslGeneratorImplTest_Constructor,
|
||||
DISABLED_EmitConstructor_Type_Struct) {}
|
||||
|
||||
} // namespace
|
||||
} // namespace hlsl
|
||||
|
|
|
@ -15,27 +15,25 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/continue_statement.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Continue : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Continue) {
|
||||
TEST_F(HlslGeneratorImplTest_Continue, Emit_Continue) {
|
||||
ast::ContinueStatement c;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&c)) << g.error();
|
||||
EXPECT_EQ(g.result(), " continue;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &c)) << gen().error();
|
||||
EXPECT_EQ(result(), " continue;\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -12,27 +12,25 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/discard_statement.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Discard : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Discard) {
|
||||
TEST_F(HlslGeneratorImplTest_Discard, Emit_Discard) {
|
||||
ast::DiscardStatement stmt;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " discard;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), " discard;\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/assignment_statement.h"
|
||||
#include "src/ast/decorated_variable.h"
|
||||
#include "src/ast/entry_point.h"
|
||||
|
@ -27,16 +26,17 @@
|
|||
#include "src/ast/variable.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_EntryPoint : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Vertex_Input) {
|
||||
// [[location 0]] var<in> foo : f32;
|
||||
// [[location 1]] var<in> bar : i32;
|
||||
//
|
||||
|
@ -60,14 +60,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func =
|
||||
|
@ -82,19 +79,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
|
|||
std::make_unique<ast::IdentifierExpression>("bar")));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kVertex, "",
|
||||
"vtx_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct vtx_main_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct vtx_main_in {
|
||||
float foo : TEXCOORD0;
|
||||
int bar : TEXCOORD1;
|
||||
};
|
||||
|
@ -102,7 +97,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Input) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Vertex_Output) {
|
||||
// [[location 0]] var<out> foo : f32;
|
||||
// [[location 1]] var<out> bar : i32;
|
||||
//
|
||||
|
@ -126,14 +121,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func =
|
||||
|
@ -148,19 +140,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
|
|||
std::make_unique<ast::IdentifierExpression>("bar")));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kVertex, "",
|
||||
"vtx_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct vtx_main_out {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct vtx_main_out {
|
||||
float foo : TEXCOORD0;
|
||||
int bar : TEXCOORD1;
|
||||
};
|
||||
|
@ -168,7 +158,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Vertex_Output) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Fragment_Input) {
|
||||
// [[location 0]] var<in> foo : f32;
|
||||
// [[location 1]] var<in> bar : i32;
|
||||
//
|
||||
|
@ -192,14 +182,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func =
|
||||
|
@ -214,19 +201,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
|
|||
std::make_unique<ast::IdentifierExpression>("bar")));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"main", "frag_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct main_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct main_in {
|
||||
float foo : TEXCOORD0;
|
||||
int bar : TEXCOORD1;
|
||||
};
|
||||
|
@ -234,7 +219,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Input) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Fragment_Output) {
|
||||
// [[location 0]] var<out> foo : f32;
|
||||
// [[location 1]] var<out> bar : i32;
|
||||
//
|
||||
|
@ -258,14 +243,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func =
|
||||
|
@ -280,19 +262,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
|
|||
std::make_unique<ast::IdentifierExpression>("bar")));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"main", "frag_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct main_out {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct main_out {
|
||||
float foo : SV_Target0;
|
||||
int bar : SV_Target1;
|
||||
};
|
||||
|
@ -300,7 +280,7 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Fragment_Output) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Input) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Compute_Input) {
|
||||
// [[location 0]] var<in> foo : f32;
|
||||
// [[location 1]] var<in> bar : i32;
|
||||
//
|
||||
|
@ -321,14 +301,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Input) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func =
|
||||
|
@ -343,22 +320,20 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Input) {
|
|||
std::make_unique<ast::IdentifierExpression>("bar")));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
|
||||
"main", "comp_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_FALSE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.error(), R"(invalid location variable for pipeline stage)");
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_FALSE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(gen().error(), R"(invalid location variable for pipeline stage)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Output) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Compute_Output) {
|
||||
// [[location 0]] var<out> foo : f32;
|
||||
// [[location 1]] var<out> bar : i32;
|
||||
//
|
||||
|
@ -379,14 +354,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Output) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func =
|
||||
|
@ -401,22 +373,20 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Compute_Output) {
|
|||
std::make_unique<ast::IdentifierExpression>("bar")));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
|
||||
"main", "comp_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_FALSE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.error(), R"(invalid location variable for pipeline stage)");
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_FALSE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(gen().error(), R"(invalid location variable for pipeline stage)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Builtins) {
|
||||
TEST_F(HlslGeneratorImplTest_EntryPoint, EmitEntryPointData_Builtins) {
|
||||
// [[builtin frag_coord]] var<in> coord : vec4<f32>;
|
||||
// [[builtin frag_depth]] var<out> depth : f32;
|
||||
//
|
||||
|
@ -448,14 +418,11 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Builtins) {
|
|||
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
|
||||
depth_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
td.RegisterVariableForTesting(depth_var.get());
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
td().RegisterVariableForTesting(depth_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
mod.AddGlobalVariable(std::move(depth_var));
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
mod()->AddGlobalVariable(std::move(depth_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -469,19 +436,17 @@ TEST_F(HlslGeneratorImplTest, EmitEntryPointData_Builtins) {
|
|||
std::make_unique<ast::IdentifierExpression>("x"))));
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"main", "frag_main");
|
||||
auto* ep_ptr = ep.get();
|
||||
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitEntryPointData(ep_ptr)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct main_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().EmitEntryPointData(out(), ep_ptr)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct main_in {
|
||||
vector<float, 4> coord : SV_Position;
|
||||
};
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/assignment_statement.h"
|
||||
#include "src/ast/binary_expression.h"
|
||||
#include "src/ast/binding_decoration.h"
|
||||
|
@ -42,16 +41,17 @@
|
|||
#include "src/ast/variable_decl_statement.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Function : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto func = std::make_unique<ast::Function>("my_func", ast::VariableList{},
|
||||
|
@ -61,21 +61,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
gen().increment_indent();
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( void my_func() {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"( void my_func() {
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_Name_Collision) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_Name_Collision) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto func = std::make_unique<ast::Function>("GeometryShader",
|
||||
|
@ -85,21 +82,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_Name_Collision) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
gen().increment_indent();
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( void GeometryShader_tint_0() {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"( void GeometryShader_tint_0() {
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_WithParams) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_WithParams) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::I32Type i32;
|
||||
|
||||
|
@ -117,21 +111,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_WithParams) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
gen().increment_indent();
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( void my_func(float a, int b) {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"( void my_func(float a, int b) {
|
||||
return;
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_NoName) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_NoName) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto func = std::make_unique<ast::Function>("frag_main", ast::VariableList{},
|
||||
|
@ -139,19 +130,17 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_NoName) {
|
|||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
m.AddEntryPoint(std::move(ep));
|
||||
mod()->AddFunction(std::move(func));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(void frag_main() {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(void frag_main() {
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOutVars) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_WithInOutVars) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
|
@ -167,14 +156,11 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOutVars) {
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -187,17 +173,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOutVars) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct frag_main_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct frag_main_in {
|
||||
float foo : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
@ -214,7 +198,8 @@ frag_main_out frag_main(frag_main_in tint_in) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOut_Builtins) {
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_EntryPoint_WithInOut_Builtins) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec4(&f32, 4);
|
||||
|
@ -235,14 +220,11 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOut_Builtins) {
|
|||
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
|
||||
depth_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
td.RegisterVariableForTesting(depth_var.get());
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
td().RegisterVariableForTesting(depth_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
mod.AddGlobalVariable(std::move(depth_var));
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
mod()->AddGlobalVariable(std::move(depth_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -257,17 +239,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithInOut_Builtins) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct frag_main_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct frag_main_in {
|
||||
vector<float, 4> coord : SV_Position;
|
||||
};
|
||||
|
||||
|
@ -284,7 +264,7 @@ frag_main_out frag_main(frag_main_in tint_in) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_Uniform) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_With_Uniform) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec4(&f32, 4);
|
||||
|
@ -298,12 +278,8 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_Uniform) {
|
|||
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||
coord_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -320,17 +296,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_Uniform) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(cbuffer : register(b0) {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(cbuffer : register(b0) {
|
||||
vector<float, 4> coord;
|
||||
};
|
||||
|
||||
|
@ -342,7 +316,8 @@ void frag_main() {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_UniformStruct) {
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_EntryPoint_With_UniformStruct) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec4(&f32, 4);
|
||||
|
@ -358,23 +333,19 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_UniformStruct) {
|
|||
s.set_name("Uniforms");
|
||||
auto alias = std::make_unique<ast::type::AliasType>("Uniforms", &s);
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
|
||||
auto coord_var =
|
||||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"uniforms", ast::StorageClass::kUniform, alias.get()));
|
||||
|
||||
mod.AddAliasType(alias.get());
|
||||
mod()->AddAliasType(alias.get());
|
||||
|
||||
ast::VariableDecorationList decos;
|
||||
decos.push_back(std::make_unique<ast::BindingDecoration>(0));
|
||||
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||
coord_var->set_decorations(std::move(decos));
|
||||
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -393,17 +364,15 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_With_UniformStruct) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct Uniforms {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct Uniforms {
|
||||
vector<float, 4> coord;
|
||||
};
|
||||
|
||||
|
@ -417,7 +386,7 @@ void frag_main() {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_EntryPoint_With_StorageBuffer_Read) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -449,11 +418,8 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||
coord_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -470,17 +436,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(RWByteAddressBuffer coord : register(u0);
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
|
||||
|
||||
void frag_main() {
|
||||
float v = asfloat(coord.Load(4));
|
||||
|
@ -490,7 +454,7 @@ void frag_main() {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_EntryPoint_With_StorageBuffer_Store) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -522,12 +486,9 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||
coord_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
|
@ -545,17 +506,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(RWByteAddressBuffer coord : register(u0);
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(RWByteAddressBuffer coord : register(u0);
|
||||
|
||||
void frag_main() {
|
||||
coord.Store(4, asuint(2.00000000f));
|
||||
|
@ -565,7 +524,7 @@ void frag_main() {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_Called_By_EntryPoints_WithLocationGlobals_And_Params) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -587,16 +546,13 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(0));
|
||||
val_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td.RegisterVariableForTesting(val_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(val_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod.AddGlobalVariable(std::move(val_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(val_var));
|
||||
|
||||
ast::VariableList params;
|
||||
params.push_back(std::make_unique<ast::Variable>(
|
||||
|
@ -615,7 +571,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("foo")));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func_1 = std::make_unique<ast::Function>("frag_1_main",
|
||||
std::move(params), &void_type);
|
||||
|
@ -633,17 +589,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func_1->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func_1));
|
||||
mod()->AddFunction(std::move(func_1));
|
||||
|
||||
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_1", "frag_1_main");
|
||||
mod.AddEntryPoint(std::move(ep1));
|
||||
mod()->AddEntryPoint(std::move(ep1));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct ep_1_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct ep_1_in {
|
||||
float foo : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
@ -667,7 +621,7 @@ ep_1_out ep_1(ep_1_in tint_in) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_Called_By_EntryPoints_NoUsedGlobals) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -682,12 +636,9 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
|
||||
depth_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(depth_var.get());
|
||||
td().RegisterVariableForTesting(depth_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(depth_var));
|
||||
mod()->AddGlobalVariable(std::move(depth_var));
|
||||
|
||||
ast::VariableList params;
|
||||
params.push_back(std::make_unique<ast::Variable>(
|
||||
|
@ -700,7 +651,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("param")));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func_1 = std::make_unique<ast::Function>("frag_1_main",
|
||||
std::move(params), &void_type);
|
||||
|
@ -718,17 +669,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func_1->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func_1));
|
||||
mod()->AddFunction(std::move(func_1));
|
||||
|
||||
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_1", "frag_1_main");
|
||||
mod.AddEntryPoint(std::move(ep1));
|
||||
mod()->AddEntryPoint(std::move(ep1));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct ep_1_out {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct ep_1_out {
|
||||
float depth : SV_Depth;
|
||||
};
|
||||
|
||||
|
@ -745,7 +694,7 @@ ep_1_out ep_1() {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_Called_By_EntryPoints_WithBuiltinGlobals_And_Params) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -767,14 +716,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::BuiltinDecoration>(ast::Builtin::kFragDepth));
|
||||
depth_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
td.RegisterVariableForTesting(depth_var.get());
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
td().RegisterVariableForTesting(depth_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
mod.AddGlobalVariable(std::move(depth_var));
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
mod()->AddGlobalVariable(std::move(depth_var));
|
||||
|
||||
ast::VariableList params;
|
||||
params.push_back(std::make_unique<ast::Variable>(
|
||||
|
@ -792,7 +738,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("param")));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func_1 = std::make_unique<ast::Function>("frag_1_main",
|
||||
std::move(params), &void_type);
|
||||
|
@ -810,17 +756,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func_1->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func_1));
|
||||
mod()->AddFunction(std::move(func_1));
|
||||
|
||||
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_1", "frag_1_main");
|
||||
mod.AddEntryPoint(std::move(ep1));
|
||||
mod()->AddEntryPoint(std::move(ep1));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct ep_1_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct ep_1_in {
|
||||
vector<float, 4> coord : SV_Position;
|
||||
};
|
||||
|
||||
|
@ -842,7 +786,7 @@ ep_1_out ep_1(ep_1_in tint_in) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
DISABLED_Emit_Function_Called_By_EntryPoint_With_Uniform) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -857,12 +801,9 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||
coord_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ast::VariableList params;
|
||||
params.push_back(std::make_unique<ast::Variable>(
|
||||
|
@ -877,7 +818,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("x"))));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
&void_type);
|
||||
|
@ -897,20 +838,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( ... )");
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"( ... )");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
DISABLED_Emit_Function_Called_By_EntryPoint_With_StorageBuffer) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -925,12 +864,9 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::SetDecoration>(1));
|
||||
coord_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ast::VariableList params;
|
||||
params.push_back(std::make_unique<ast::Variable>(
|
||||
|
@ -945,7 +881,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("x"))));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func = std::make_unique<ast::Function>("frag_main", std::move(params),
|
||||
&void_type);
|
||||
|
@ -965,20 +901,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
|
||||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment, "",
|
||||
"frag_main");
|
||||
mod.AddEntryPoint(std::move(ep));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( ... )");
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"( ... )");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
DISABLED_Emit_Function_Called_Two_EntryPoints_WithGlobals) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -995,14 +929,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(foo_var.get());
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
td().RegisterVariableForTesting(foo_var.get());
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
|
||||
mod.AddGlobalVariable(std::move(foo_var));
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
mod()->AddGlobalVariable(std::move(foo_var));
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto sub_func =
|
||||
|
@ -1016,7 +947,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("foo")));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func_1 = std::make_unique<ast::Function>("frag_1_main",
|
||||
std::move(params), &void_type);
|
||||
|
@ -1030,20 +961,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func_1->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func_1));
|
||||
mod()->AddFunction(std::move(func_1));
|
||||
|
||||
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_1", "frag_1_main");
|
||||
auto ep2 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_2", "frag_1_main");
|
||||
mod.AddEntryPoint(std::move(ep1));
|
||||
mod.AddEntryPoint(std::move(ep2));
|
||||
mod()->AddEntryPoint(std::move(ep1));
|
||||
mod()->AddEntryPoint(std::move(ep2));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct ep_1_in {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct ep_1_in {
|
||||
float foo : TEXCOORD0;
|
||||
};
|
||||
|
||||
|
@ -1084,7 +1013,7 @@ ep_2_out ep_2(ep_2_in tint_in) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
DISABLED_Emit_Function_EntryPoints_WithGlobal_Nested_Return) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
@ -1096,11 +1025,8 @@ TEST_F(HlslGeneratorImplTest,
|
|||
decos.push_back(std::make_unique<ast::LocationDecoration>(1));
|
||||
bar_var->set_decorations(std::move(decos));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
td.RegisterVariableForTesting(bar_var.get());
|
||||
mod.AddGlobalVariable(std::move(bar_var));
|
||||
td().RegisterVariableForTesting(bar_var.get());
|
||||
mod()->AddGlobalVariable(std::move(bar_var));
|
||||
|
||||
ast::VariableList params;
|
||||
auto func_1 = std::make_unique<ast::Function>("frag_1_main",
|
||||
|
@ -1127,17 +1053,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func_1->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func_1));
|
||||
mod()->AddFunction(std::move(func_1));
|
||||
|
||||
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_1", "frag_1_main");
|
||||
mod.AddEntryPoint(std::move(ep1));
|
||||
mod()->AddEntryPoint(std::move(ep1));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct ep_1_out {
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct ep_1_out {
|
||||
float bar : SV_Target0;
|
||||
};
|
||||
|
||||
|
@ -1153,15 +1077,11 @@ ep_1_out ep_1() {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_Called_Two_EntryPoints_WithoutGlobals) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
|
||||
ast::VariableList params;
|
||||
auto sub_func =
|
||||
std::make_unique<ast::Function>("sub_func", std::move(params), &f32);
|
||||
|
@ -1172,7 +1092,7 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::FloatLiteral>(&f32, 1.0))));
|
||||
sub_func->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(sub_func));
|
||||
mod()->AddFunction(std::move(sub_func));
|
||||
|
||||
auto func_1 = std::make_unique<ast::Function>("frag_1_main",
|
||||
std::move(params), &void_type);
|
||||
|
@ -1188,20 +1108,19 @@ TEST_F(HlslGeneratorImplTest,
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func_1->set_body(std::move(body));
|
||||
|
||||
mod.AddFunction(std::move(func_1));
|
||||
mod()->AddFunction(std::move(func_1));
|
||||
|
||||
auto ep1 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_1", "frag_1_main");
|
||||
auto ep2 = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kFragment,
|
||||
"ep_2", "frag_1_main");
|
||||
mod.AddEntryPoint(std::move(ep1));
|
||||
mod.AddEntryPoint(std::move(ep2));
|
||||
mod()->AddEntryPoint(std::move(ep1));
|
||||
mod()->AddEntryPoint(std::move(ep2));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(float sub_func() {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(float sub_func() {
|
||||
return 1.00000000f;
|
||||
}
|
||||
|
||||
|
@ -1217,7 +1136,7 @@ void ep_2() {
|
|||
|
||||
)");
|
||||
}
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithName) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_EntryPoint_WithName) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto func = std::make_unique<ast::Function>("comp_main", ast::VariableList{},
|
||||
|
@ -1225,19 +1144,18 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithName) {
|
|||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
|
||||
"my_main", "comp_main");
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
m.AddEntryPoint(std::move(ep));
|
||||
mod()->AddFunction(std::move(func));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(void my_main() {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(void my_main() {
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithNameCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_Function,
|
||||
Emit_Function_EntryPoint_WithNameCollision) {
|
||||
ast::type::VoidType void_type;
|
||||
|
||||
auto func = std::make_unique<ast::Function>("comp_main", ast::VariableList{},
|
||||
|
@ -1245,19 +1163,17 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_EntryPoint_WithNameCollision) {
|
|||
auto ep = std::make_unique<ast::EntryPoint>(ast::PipelineStage::kCompute,
|
||||
"GeometryShader", "comp_main");
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
m.AddEntryPoint(std::move(ep));
|
||||
mod()->AddFunction(std::move(func));
|
||||
mod()->AddEntryPoint(std::move(ep));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(void GeometryShader_tint_0() {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(void GeometryShader_tint_0() {
|
||||
}
|
||||
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Function_WithArrayParams) {
|
||||
TEST_F(HlslGeneratorImplTest_Function, Emit_Function_WithArrayParams) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::ArrayType ary(&f32, 5);
|
||||
|
||||
|
@ -1273,14 +1189,11 @@ TEST_F(HlslGeneratorImplTest, Emit_Function_WithArrayParams) {
|
|||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
func->set_body(std::move(body));
|
||||
|
||||
ast::Module m;
|
||||
m.AddFunction(std::move(func));
|
||||
mod()->AddFunction(std::move(func));
|
||||
gen().increment_indent();
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( void my_func(float a[5]) {
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"( void my_func(float a[5]) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,53 +12,43 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Identifier : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitExpression_Identifier) {
|
||||
TEST_F(HlslGeneratorImplTest_Identifier, DISABLED_EmitExpression_Identifier) {
|
||||
ast::IdentifierExpression i(std::vector<std::string>{"std", "glsl"});
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), "std::glsl");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), "std::glsl");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitIdentifierExpression_Single) {
|
||||
TEST_F(HlslGeneratorImplTest_Identifier, EmitIdentifierExpression_Single) {
|
||||
ast::IdentifierExpression i("foo");
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), "foo");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), "foo");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitIdentifierExpression_Single_WithCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_Identifier,
|
||||
EmitIdentifierExpression_Single_WithCollision) {
|
||||
ast::IdentifierExpression i("virtual");
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), "virtual_tint_0");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), "virtual_tint_0");
|
||||
}
|
||||
|
||||
// TODO(dsinclair): Handle import names
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitIdentifierExpression_MultipleNames) {
|
||||
TEST_F(HlslGeneratorImplTest_Identifier,
|
||||
DISABLED_EmitIdentifierExpression_MultipleNames) {
|
||||
ast::IdentifierExpression i({"std", "glsl", "init"});
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), "std::glsl::init");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), "std::glsl::init");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -12,40 +12,36 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/else_statement.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/if_statement.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/return_statement.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_If : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_If) {
|
||||
TEST_F(HlslGeneratorImplTest_If, Emit_If) {
|
||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::ReturnStatement>());
|
||||
|
||||
ast::IfStatement i(std::move(cond), std::move(body));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( if (cond) {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( if (cond) {
|
||||
return;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_IfWithElseIf) {
|
||||
TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElseIf) {
|
||||
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
||||
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||
else_body->append(std::make_unique<ast::ReturnStatement>());
|
||||
|
@ -61,12 +57,10 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElseIf) {
|
|||
ast::IfStatement i(std::move(cond), std::move(body));
|
||||
i.set_else_statements(std::move(elses));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( if (cond) {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( if (cond) {
|
||||
return;
|
||||
} else if (else_cond) {
|
||||
return;
|
||||
|
@ -74,7 +68,7 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElseIf) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_IfWithElse) {
|
||||
TEST_F(HlslGeneratorImplTest_If, Emit_IfWithElse) {
|
||||
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||
else_body->append(std::make_unique<ast::ReturnStatement>());
|
||||
|
||||
|
@ -88,12 +82,10 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElse) {
|
|||
ast::IfStatement i(std::move(cond), std::move(body));
|
||||
i.set_else_statements(std::move(elses));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( if (cond) {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( if (cond) {
|
||||
return;
|
||||
} else {
|
||||
return;
|
||||
|
@ -101,7 +93,7 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithElse) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_IfWithMultiple) {
|
||||
TEST_F(HlslGeneratorImplTest_If, Emit_IfWithMultiple) {
|
||||
auto else_cond = std::make_unique<ast::IdentifierExpression>("else_cond");
|
||||
|
||||
auto else_body = std::make_unique<ast::BlockStatement>();
|
||||
|
@ -122,12 +114,10 @@ TEST_F(HlslGeneratorImplTest, Emit_IfWithMultiple) {
|
|||
ast::IfStatement i(std::move(cond), std::move(body));
|
||||
i.set_else_statements(std::move(elses));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
gen().increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&i)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( if (cond) {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &i)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( if (cond) {
|
||||
return;
|
||||
} else if (else_cond) {
|
||||
return;
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/call_expression.h"
|
||||
#include "src/ast/float_literal.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
|
@ -30,14 +29,14 @@
|
|||
#include "src/ast/type_constructor_expression.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Import : public TestHelper, public testing::Test {};
|
||||
|
||||
struct HlslImportData {
|
||||
const char* name;
|
||||
|
@ -47,7 +46,10 @@ inline std::ostream& operator<<(std::ostream& out, HlslImportData data) {
|
|||
out << data.name;
|
||||
return out;
|
||||
}
|
||||
using HlslImportData_SingleParamTest = testing::TestWithParam<HlslImportData>;
|
||||
|
||||
class HlslImportData_SingleParamTest
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_SingleParamTest, FloatScalar) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -61,19 +63,14 @@ TEST_P(HlslImportData_SingleParamTest, FloatScalar) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1.00000000f)");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1.00000000f)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
HlslGeneratorImplTest,
|
||||
HlslGeneratorImplTest_Import,
|
||||
HlslImportData_SingleParamTest,
|
||||
testing::Values(HlslImportData{"acos", "acos"},
|
||||
HlslImportData{"asin", "asin"},
|
||||
|
@ -104,20 +101,21 @@ INSTANTIATE_TEST_SUITE_P(
|
|||
HlslImportData{"tanh", "tanh"},
|
||||
HlslImportData{"trunc", "trunc"}));
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_Acosh) {
|
||||
TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_Acosh) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_ASinh) {
|
||||
TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_ASinh) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_ATanh) {
|
||||
TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_ATanh) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
using HlslImportData_SingleIntParamTest =
|
||||
testing::TestWithParam<HlslImportData>;
|
||||
class HlslImportData_SingleIntParamTest
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_SingleIntParamTest, IntScalar) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -131,23 +129,20 @@ TEST_P(HlslImportData_SingleIntParamTest, IntScalar) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1)");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
|
||||
HlslImportData_SingleIntParamTest,
|
||||
testing::Values(HlslImportData{"sabs", "abs"},
|
||||
HlslImportData{"ssign", "sign"}));
|
||||
|
||||
using HlslImportData_DualParamTest = testing::TestWithParam<HlslImportData>;
|
||||
class HlslImportData_DualParamTest
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_DualParamTest, FloatScalar) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -163,19 +158,14 @@ TEST_P(HlslImportData_DualParamTest, FloatScalar) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(),
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(),
|
||||
std::string(param.hlsl_name) + "(1.00000000f, 2.00000000f)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
|
||||
HlslImportData_DualParamTest,
|
||||
testing::Values(HlslImportData{"atan2", "atan2"},
|
||||
HlslImportData{"distance", "distance"},
|
||||
|
@ -187,8 +177,9 @@ INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
|||
HlslImportData{"reflect", "reflect"},
|
||||
HlslImportData{"step", "step"}));
|
||||
|
||||
using HlslImportData_DualParam_VectorTest =
|
||||
testing::TestWithParam<HlslImportData>;
|
||||
class HlslImportData_DualParam_VectorTest
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -220,26 +211,22 @@ TEST_P(HlslImportData_DualParam_VectorTest, FloatVector) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(),
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(),
|
||||
std::string(param.hlsl_name) +
|
||||
"(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f), "
|
||||
"vector<float, 3>(4.00000000f, 5.00000000f, 6.00000000f))");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
|
||||
HlslImportData_DualParam_VectorTest,
|
||||
testing::Values(HlslImportData{"cross", "cross"}));
|
||||
|
||||
using HlslImportData_DualParam_Int_Test =
|
||||
testing::TestWithParam<HlslImportData>;
|
||||
class HlslImportData_DualParam_Int_Test
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_DualParam_Int_Test, IntScalar) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -255,25 +242,22 @@ TEST_P(HlslImportData_DualParam_Int_Test, IntScalar) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1, 2)");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1, 2)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
|
||||
HlslImportData_DualParam_Int_Test,
|
||||
testing::Values(HlslImportData{"smax", "max"},
|
||||
HlslImportData{"smin", "min"},
|
||||
HlslImportData{"umax", "max"},
|
||||
HlslImportData{"umin", "min"}));
|
||||
|
||||
using HlslImportData_TripleParamTest = testing::TestWithParam<HlslImportData>;
|
||||
class HlslImportData_TripleParamTest
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_TripleParamTest, FloatScalar) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -291,20 +275,15 @@ TEST_P(HlslImportData_TripleParamTest, FloatScalar) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string(param.hlsl_name) +
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string(param.hlsl_name) +
|
||||
"(1.00000000f, 2.00000000f, 3.00000000f)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
HlslGeneratorImplTest,
|
||||
HlslGeneratorImplTest_Import,
|
||||
HlslImportData_TripleParamTest,
|
||||
testing::Values(HlslImportData{"faceforward", "faceforward"},
|
||||
HlslImportData{"fma", "fma"},
|
||||
|
@ -312,12 +291,13 @@ INSTANTIATE_TEST_SUITE_P(
|
|||
HlslImportData{"nclamp", "clamp"},
|
||||
HlslImportData{"smoothstep", "smoothstep"}));
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_HlslImportData_FMix) {
|
||||
TEST_F(HlslGeneratorImplTest_Import, DISABLED_HlslImportData_FMix) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
using HlslImportData_TripleParam_Int_Test =
|
||||
testing::TestWithParam<HlslImportData>;
|
||||
class HlslImportData_TripleParam_Int_Test
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslImportData> {};
|
||||
TEST_P(HlslImportData_TripleParam_Int_Test, IntScalar) {
|
||||
auto param = GetParam();
|
||||
|
||||
|
@ -335,23 +315,18 @@ TEST_P(HlslImportData_TripleParam_Int_Test, IntScalar) {
|
|||
std::vector<std::string>{"std", param.name}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string(param.hlsl_name) + "(1, 2, 3)");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string(param.hlsl_name) + "(1, 2, 3)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_Import,
|
||||
HlslImportData_TripleParam_Int_Test,
|
||||
testing::Values(HlslImportData{"sclamp", "clamp"},
|
||||
HlslImportData{"uclamp", "clamp"}));
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, HlslImportData_Determinant) {
|
||||
TEST_F(HlslGeneratorImplTest_Import, HlslImportData_Determinant) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::MatrixType mat(&f32, 3, 3);
|
||||
|
||||
|
@ -365,19 +340,14 @@ TEST_F(HlslGeneratorImplTest, HlslImportData_Determinant) {
|
|||
std::vector<std::string>{"std", "determinant"}),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
mod.AddGlobalVariable(std::move(var));
|
||||
mod.AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
mod()->AddGlobalVariable(std::move(var));
|
||||
mod()->AddImport(std::make_unique<ast::Import>("GLSL.std.450", "std"));
|
||||
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
// Register the global
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitImportFunction(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string("determinant(var)"));
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitImportFunction(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string("determinant(var)"));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/call_expression.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
|
@ -20,14 +19,15 @@
|
|||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Intrinsic : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
struct IntrinsicData {
|
||||
const char* name;
|
||||
|
@ -37,16 +37,14 @@ inline std::ostream& operator<<(std::ostream& out, IntrinsicData data) {
|
|||
out << data.name;
|
||||
return out;
|
||||
}
|
||||
using HlslIntrinsicTest = testing::TestWithParam<IntrinsicData>;
|
||||
class HlslIntrinsicTest : public TestHelper,
|
||||
public testing::TestWithParam<IntrinsicData> {};
|
||||
TEST_P(HlslIntrinsicTest, Emit) {
|
||||
auto param = GetParam();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
EXPECT_EQ(g.generate_intrinsic_name(param.name), param.hlsl_name);
|
||||
EXPECT_EQ(gen().generate_intrinsic_name(param.name), param.hlsl_name);
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
HlslGeneratorImplTest,
|
||||
HlslGeneratorImplTest_Intrinsic,
|
||||
HlslIntrinsicTest,
|
||||
testing::Values(IntrinsicData{"any", "any"},
|
||||
IntrinsicData{"all", "all"},
|
||||
|
@ -64,15 +62,15 @@ INSTANTIATE_TEST_SUITE_P(
|
|||
IntrinsicData{"is_inf", "isinf"},
|
||||
IntrinsicData{"is_nan", "isnan"}));
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_IsNormal) {
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_IsNormal) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_Select) {
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_Select) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) {
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, DISABLED_Intrinsic_OuterProduct) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec2(&f32, 2);
|
||||
ast::type::VectorType vec3(&f32, 3);
|
||||
|
@ -90,32 +88,25 @@ TEST_F(HlslGeneratorImplTest, DISABLED_Intrinsic_OuterProduct) {
|
|||
std::make_unique<ast::IdentifierExpression>("outer_product"),
|
||||
std::move(params));
|
||||
|
||||
Context ctx;
|
||||
ast::Module m;
|
||||
TypeDeterminer td(&ctx, &m);
|
||||
td.RegisterVariableForTesting(a.get());
|
||||
td.RegisterVariableForTesting(b.get());
|
||||
td().RegisterVariableForTesting(a.get());
|
||||
td().RegisterVariableForTesting(b.get());
|
||||
|
||||
m.AddGlobalVariable(std::move(a));
|
||||
m.AddGlobalVariable(std::move(b));
|
||||
mod()->AddGlobalVariable(std::move(a));
|
||||
mod()->AddGlobalVariable(std::move(b));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td.DetermineResultType(&call)) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&call)) << td().error();
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
|
||||
g.increment_indent();
|
||||
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float3x2(a * b[0], a * b[1], a * b[2])");
|
||||
gen().increment_indent();
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
|
||||
EXPECT_EQ(result(), " float3x2(a * b[0], a * b[1], a * b[2])");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Intrinsic_Bad_Name) {
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
EXPECT_EQ(g.generate_intrinsic_name("unknown name"), "");
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Bad_Name) {
|
||||
EXPECT_EQ(gen().generate_intrinsic_name("unknown name"), "");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Intrinsic_Call) {
|
||||
TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Call) {
|
||||
ast::ExpressionList params;
|
||||
params.push_back(std::make_unique<ast::IdentifierExpression>("param1"));
|
||||
params.push_back(std::make_unique<ast::IdentifierExpression>("param2"));
|
||||
|
@ -123,11 +114,9 @@ TEST_F(HlslGeneratorImplTest, Intrinsic_Call) {
|
|||
ast::CallExpression call(std::make_unique<ast::IdentifierExpression>("dot"),
|
||||
std::move(params));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
ASSERT_TRUE(g.EmitExpression(&call)) << g.error();
|
||||
EXPECT_EQ(g.result(), " dot(param1, param2)");
|
||||
gen().increment_indent();
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &call)) << gen().error();
|
||||
EXPECT_EQ(result(), " dot(param1, param2)");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/assignment_statement.h"
|
||||
#include "src/ast/discard_statement.h"
|
||||
#include "src/ast/float_literal.h"
|
||||
|
@ -25,33 +24,30 @@
|
|||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/variable.h"
|
||||
#include "src/ast/variable_decl_statement.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Loop : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Loop) {
|
||||
TEST_F(HlslGeneratorImplTest_Loop, Emit_Loop) {
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
ast::LoopStatement l(std::move(body), {});
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( for(;;) {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &l)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( for(;;) {
|
||||
discard;
|
||||
}
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) {
|
||||
TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopWithContinuing) {
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
body->append(std::make_unique<ast::DiscardStatement>());
|
||||
|
||||
|
@ -59,13 +55,10 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) {
|
|||
continuing->append(std::make_unique<ast::ReturnStatement>());
|
||||
|
||||
ast::LoopStatement l(std::move(body), std::move(continuing));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&l)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &l)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( {
|
||||
bool tint_hlsl_is_first_1 = true;
|
||||
for(;;) {
|
||||
if (!tint_hlsl_is_first_1) {
|
||||
|
@ -79,7 +72,7 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopWithContinuing) {
|
|||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
||||
TEST_F(HlslGeneratorImplTest_Loop, Emit_LoopNestedWithContinuing) {
|
||||
ast::type::F32Type f32;
|
||||
|
||||
auto body = std::make_unique<ast::BlockStatement>();
|
||||
|
@ -102,13 +95,10 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
|||
std::move(lhs), std::move(rhs)));
|
||||
|
||||
ast::LoopStatement outer(std::move(body), std::move(continuing));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&outer)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &outer)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( {
|
||||
bool tint_hlsl_is_first_1 = true;
|
||||
for(;;) {
|
||||
if (!tint_hlsl_is_first_1) {
|
||||
|
@ -134,7 +124,7 @@ TEST_F(HlslGeneratorImplTest, Emit_LoopNestedWithContinuing) {
|
|||
|
||||
// TODO(dsinclair): Handle pulling declared variables up and out of the for() if
|
||||
// there is a continuing block.
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
||||
TEST_F(HlslGeneratorImplTest_Loop, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
||||
ast::type::F32Type f32;
|
||||
|
||||
auto var = std::make_unique<ast::Variable>(
|
||||
|
@ -156,13 +146,10 @@ TEST_F(HlslGeneratorImplTest, DISABLED_Emit_LoopWithVarUsedInContinuing) {
|
|||
std::move(lhs), std::move(rhs)));
|
||||
|
||||
ast::LoopStatement outer(std::move(body), std::move(continuing));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&outer)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &outer)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( {
|
||||
float lhs;
|
||||
bool tint_hlsl_is_first_1 = true;
|
||||
for(;;) {
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/array_accessor_expression.h"
|
||||
#include "src/ast/assignment_statement.h"
|
||||
#include "src/ast/binary_expression.h"
|
||||
|
@ -36,16 +35,17 @@
|
|||
#include "src/ast/type_constructor_expression.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_MemberAccessor : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitExpression_MemberAccessor) {
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor, EmitExpression_MemberAccessor) {
|
||||
ast::type::F32Type f32;
|
||||
|
||||
ast::StructMemberList members;
|
||||
|
@ -68,20 +68,16 @@ TEST_F(HlslGeneratorImplTest, EmitExpression_MemberAccessor) {
|
|||
|
||||
ast::MemberAccessorExpression expr(std::move(str), std::move(mem));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(str_var.get());
|
||||
g.register_global(str_var.get());
|
||||
mod.AddGlobalVariable(std::move(str_var));
|
||||
td().RegisterVariableForTesting(str_var.get());
|
||||
gen().register_global(str_var.get());
|
||||
mod()->AddGlobalVariable(std::move(str_var));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "str.mem");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "str.mem");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : i32;
|
||||
|
@ -119,23 +115,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("data"),
|
||||
std::make_unique<ast::IdentifierExpression>("b"));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(data.Load(4))");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(data.Load(4))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load_Int) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : i32;
|
||||
|
@ -173,22 +164,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("data"),
|
||||
std::make_unique<ast::IdentifierExpression>("a"));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asint(data.Load(0))");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asint(data.Load(0))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_ArrayAccessor_StorageBuffer_Load_Int_FromArray) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : [[stride 4]] array<i32, 5>;
|
||||
|
@ -225,22 +212,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 2)));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asint(data.Load((4 * 2) + 0))");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asint(data.Load((4 * 2) + 0))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_ArrayAccessor_StorageBuffer_Load_Int_FromArray_ExprIdx) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : [[stride 4]] array<i32, 5>;
|
||||
|
@ -285,22 +268,18 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 3))));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr)) << td().error();
|
||||
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asint(data.Load((4 * ((2 + 4) - 3)) + 0))");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asint(data.Load((4 * ((2 + 4) - 3)) + 0))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Store) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : i32;
|
||||
|
@ -335,15 +314,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &s));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
auto lhs = std::make_unique<ast::MemberAccessorExpression>(
|
||||
std::make_unique<ast::IdentifierExpression>("data"),
|
||||
|
@ -352,13 +327,13 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::FloatLiteral>(&f32, 2.0f));
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&assign));
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(data.Store(4, asuint(2.00000000f));
|
||||
ASSERT_TRUE(td().DetermineResultType(&assign));
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(data.Store(4, asuint(2.00000000f));
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Store_ToArray) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : [[stride 4]] array<i32, 5>;
|
||||
|
@ -389,15 +364,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &s));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
auto lhs = std::make_unique<ast::ArrayAccessorExpression>(
|
||||
std::make_unique<ast::MemberAccessorExpression>(
|
||||
|
@ -409,13 +380,13 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::SintLiteral>(&i32, 2));
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&assign)) << td.error();
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(data.Store((4 * 2) + 0, asuint(2));
|
||||
ASSERT_TRUE(td().DetermineResultType(&assign)) << td().error();
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(data.Store((4 * 2) + 0, asuint(2));
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Store_Int) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : i32;
|
||||
|
@ -450,15 +421,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &s));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
auto lhs = std::make_unique<ast::MemberAccessorExpression>(
|
||||
std::make_unique<ast::IdentifierExpression>("data"),
|
||||
|
@ -467,13 +434,13 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::SintLiteral>(&i32, 2));
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&assign));
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(data.Store(0, asuint(2));
|
||||
ASSERT_TRUE(td().DetermineResultType(&assign));
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(data.Store(0, asuint(2));
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load_Vec3) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -510,26 +477,22 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &s));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
ast::MemberAccessorExpression expr(
|
||||
std::make_unique<ast::IdentifierExpression>("data"),
|
||||
std::make_unique<ast::IdentifierExpression>("b"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(data.Load3(16))");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(data.Load3(16))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Store_Vec3) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -566,15 +529,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &s));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
auto lit1 = std::make_unique<ast::FloatLiteral>(&f32, 1.f);
|
||||
auto lit2 = std::make_unique<ast::FloatLiteral>(&f32, 2.f);
|
||||
|
@ -595,15 +554,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&assign));
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&assign));
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(
|
||||
g.result(),
|
||||
result(),
|
||||
R"(data.Store3(16, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)));
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -656,15 +615,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &pre));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
ast::MemberAccessorExpression expr(
|
||||
std::make_unique<ast::ArrayAccessorExpression>(
|
||||
|
@ -675,12 +630,12 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::SintLiteral>(&i32, 2))),
|
||||
std::make_unique<ast::IdentifierExpression>("b"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(data.Load3(16 + (32 * 2) + 0))");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(data.Load3(16 + (32 * 2) + 0))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Swizzle) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -733,15 +688,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &pre));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
ast::MemberAccessorExpression expr(
|
||||
std::make_unique<ast::MemberAccessorExpression>(
|
||||
|
@ -754,13 +705,13 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::IdentifierExpression>("b")),
|
||||
std::make_unique<ast::IdentifierExpression>("xy"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(data.Load3(16 + (32 * 2) + 0)).xy");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(data.Load3(16 + (32 * 2) + 0)).xy");
|
||||
}
|
||||
|
||||
TEST_F(
|
||||
HlslGeneratorImplTest,
|
||||
HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Swizzle_SingleLetter) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -813,15 +764,11 @@ TEST_F(
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &pre));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
ast::MemberAccessorExpression expr(
|
||||
std::make_unique<ast::MemberAccessorExpression>(
|
||||
|
@ -834,12 +781,12 @@ TEST_F(
|
|||
std::make_unique<ast::IdentifierExpression>("b")),
|
||||
std::make_unique<ast::IdentifierExpression>("g"));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Load_MultiLevel_Index) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -892,15 +839,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &pre));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
ast::ArrayAccessorExpression expr(
|
||||
std::make_unique<ast::MemberAccessorExpression>(
|
||||
|
@ -914,12 +857,12 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::ScalarConstructorExpression>(
|
||||
std::make_unique<ast::SintLiteral>(&i32, 1)));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&expr));
|
||||
ASSERT_TRUE(g.EmitExpression(&expr)) << g.error();
|
||||
EXPECT_EQ(g.result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
|
||||
ASSERT_TRUE(td().DetermineResultType(&expr));
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &expr)) << gen().error();
|
||||
EXPECT_EQ(result(), "asfloat(data.Load((4 * 1) + 16 + (32 * 2) + 0))");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Store_MultiLevel) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -972,15 +915,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &pre));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
auto lhs = std::make_unique<ast::MemberAccessorExpression>(
|
||||
std::make_unique<ast::ArrayAccessorExpression>(
|
||||
|
@ -1007,15 +946,15 @@ TEST_F(HlslGeneratorImplTest,
|
|||
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&assign));
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
ASSERT_TRUE(td().DetermineResultType(&assign));
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(
|
||||
g.result(),
|
||||
result(),
|
||||
R"(data.Store3(16 + (32 * 2) + 0, asuint(vector<float, 3>(1.00000000f, 2.00000000f, 3.00000000f)));
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest,
|
||||
TEST_F(HlslGeneratorImplTest_MemberAccessor,
|
||||
EmitExpression_MemberAccessor_StorageBuffer_Store_Swizzle_SingleLetter) {
|
||||
// struct Data {
|
||||
// [[offset 0]] a : vec3<i32>;
|
||||
|
@ -1068,15 +1007,11 @@ TEST_F(HlslGeneratorImplTest,
|
|||
std::make_unique<ast::DecoratedVariable>(std::make_unique<ast::Variable>(
|
||||
"data", ast::StorageClass::kStorageBuffer, &pre));
|
||||
|
||||
Context ctx;
|
||||
ast::Module mod;
|
||||
TypeDeterminer td(&ctx, &mod);
|
||||
GeneratorImpl g(&mod);
|
||||
td.RegisterVariableForTesting(coord_var.get());
|
||||
g.register_global(coord_var.get());
|
||||
mod.AddGlobalVariable(std::move(coord_var));
|
||||
td().RegisterVariableForTesting(coord_var.get());
|
||||
gen().register_global(coord_var.get());
|
||||
mod()->AddGlobalVariable(std::move(coord_var));
|
||||
|
||||
ASSERT_TRUE(td.Determine()) << td.error();
|
||||
ASSERT_TRUE(td().Determine()) << td().error();
|
||||
|
||||
auto lhs = std::make_unique<ast::MemberAccessorExpression>(
|
||||
std::make_unique<ast::MemberAccessorExpression>(
|
||||
|
@ -1094,9 +1029,9 @@ TEST_F(HlslGeneratorImplTest,
|
|||
|
||||
ast::AssignmentStatement assign(std::move(lhs), std::move(rhs));
|
||||
|
||||
ASSERT_TRUE(td.DetermineResultType(&assign));
|
||||
ASSERT_TRUE(g.EmitStatement(&assign)) << g.error();
|
||||
EXPECT_EQ(g.result(),
|
||||
ASSERT_TRUE(td().DetermineResultType(&assign));
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &assign)) << gen().error();
|
||||
EXPECT_EQ(result(),
|
||||
R"(data.Store((4 * 1) + 16 + (32 * 2) + 0, asuint(1.00000000f));
|
||||
)");
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/float_literal.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/scalar_constructor_expression.h"
|
||||
|
@ -23,16 +22,17 @@
|
|||
#include "src/ast/type/f32_type.h"
|
||||
#include "src/ast/type_constructor_expression.h"
|
||||
#include "src/ast/variable.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_ModuleConstant : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_ModuleConstant) {
|
||||
TEST_F(HlslGeneratorImplTest_ModuleConstant, Emit_ModuleConstant) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::ArrayType ary(&f32, 3);
|
||||
|
||||
|
@ -50,11 +50,10 @@ TEST_F(HlslGeneratorImplTest, Emit_ModuleConstant) {
|
|||
var->set_constructor(
|
||||
std::make_unique<ast::TypeConstructorExpression>(&ary, std::move(exprs)));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitProgramConstVariable(var.get())) << g.error();
|
||||
ASSERT_TRUE(gen().EmitProgramConstVariable(out(), var.get()))
|
||||
<< gen().error();
|
||||
EXPECT_EQ(
|
||||
g.result(),
|
||||
result(),
|
||||
"static const float pos[3] = {1.00000000f, 2.00000000f, 3.00000000f};\n");
|
||||
}
|
||||
|
||||
|
|
|
@ -15,40 +15,33 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/return_statement.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Return : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Return) {
|
||||
TEST_F(HlslGeneratorImplTest_Return, Emit_Return) {
|
||||
ast::ReturnStatement r;
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
|
||||
EXPECT_EQ(g.result(), " return;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &r)) << gen().error();
|
||||
EXPECT_EQ(result(), " return;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_ReturnWithValue) {
|
||||
TEST_F(HlslGeneratorImplTest_Return, Emit_ReturnWithValue) {
|
||||
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
|
||||
ast::ReturnStatement r(std::move(expr));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&r)) << g.error();
|
||||
EXPECT_EQ(g.result(), " return expr;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &r)) << gen().error();
|
||||
EXPECT_EQ(result(), " return expr;\n");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/break_statement.h"
|
||||
#include "src/ast/case_statement.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
|
@ -22,16 +21,16 @@
|
|||
#include "src/ast/sint_literal.h"
|
||||
#include "src/ast/switch_statement.h"
|
||||
#include "src/ast/type/i32_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Switch : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_Switch) {
|
||||
TEST_F(HlslGeneratorImplTest_Switch, Emit_Switch) {
|
||||
auto def = std::make_unique<ast::CaseStatement>();
|
||||
auto def_body = std::make_unique<ast::BlockStatement>();
|
||||
def_body->append(std::make_unique<ast::BreakStatement>());
|
||||
|
@ -53,13 +52,10 @@ TEST_F(HlslGeneratorImplTest, Emit_Switch) {
|
|||
|
||||
auto cond = std::make_unique<ast::IdentifierExpression>("cond");
|
||||
ast::SwitchStatement s(std::move(cond), std::move(body));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&s)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"( switch(cond) {
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &s)) << gen().error();
|
||||
EXPECT_EQ(result(), R"( switch(cond) {
|
||||
case 5: {
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -12,35 +12,31 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/entry_point.h"
|
||||
#include "src/ast/function.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/void_type.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_Generate) {
|
||||
ast::type::VoidType void_type;
|
||||
ast::Module m;
|
||||
m.AddFunction(std::make_unique<ast::Function>("my_func", ast::VariableList{},
|
||||
&void_type));
|
||||
m.AddEntryPoint(std::make_unique<ast::EntryPoint>(
|
||||
mod()->AddFunction(std::make_unique<ast::Function>(
|
||||
"my_func", ast::VariableList{}, &void_type));
|
||||
mod()->AddEntryPoint(std::make_unique<ast::EntryPoint>(
|
||||
ast::PipelineStage::kFragment, "my_func", ""));
|
||||
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.Generate()) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(#import <metal_lib>
|
||||
ASSERT_TRUE(gen().Generate(out())) << gen().error();
|
||||
EXPECT_EQ(result(), R"(#import <metal_lib>
|
||||
|
||||
void my_func() {
|
||||
}
|
||||
|
@ -48,30 +44,23 @@ void my_func() {
|
|||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, InputStructName) {
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_EQ(g.generate_name("func_main_in"), "func_main_in");
|
||||
ASSERT_EQ(gen().generate_name("func_main_in"), "func_main_in");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, InputStructName_ConflictWithExisting) {
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
|
||||
// Register the struct name as existing.
|
||||
auto* namer = g.namer_for_testing();
|
||||
auto* namer = gen().namer_for_testing();
|
||||
namer->NameFor("func_main_out");
|
||||
|
||||
ASSERT_EQ(g.generate_name("func_main_out"), "func_main_out_0");
|
||||
ASSERT_EQ(gen().generate_name("func_main_out"), "func_main_out_0");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, NameConflictWith_InputStructName) {
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_EQ(g.generate_name("func_main_in"), "func_main_in");
|
||||
ASSERT_EQ(gen().generate_name("func_main_in"), "func_main_in");
|
||||
|
||||
ast::IdentifierExpression ident("func_main_in");
|
||||
ASSERT_TRUE(g.EmitIdentifier(&ident));
|
||||
EXPECT_EQ(g.result(), "func_main_in_0");
|
||||
ASSERT_TRUE(gen().EmitIdentifier(out(), &ident));
|
||||
EXPECT_EQ(result(), "func_main_in_0");
|
||||
}
|
||||
|
||||
struct HlslBuiltinData {
|
||||
|
@ -82,13 +71,12 @@ inline std::ostream& operator<<(std::ostream& out, HlslBuiltinData data) {
|
|||
out << data.builtin;
|
||||
return out;
|
||||
}
|
||||
using HlslBuiltinConversionTest = testing::TestWithParam<HlslBuiltinData>;
|
||||
class HlslBuiltinConversionTest
|
||||
: public TestHelper,
|
||||
public testing::TestWithParam<HlslBuiltinData> {};
|
||||
TEST_P(HlslBuiltinConversionTest, Emit) {
|
||||
auto params = GetParam();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
EXPECT_EQ(g.builtin_to_attribute(params.builtin),
|
||||
EXPECT_EQ(gen().builtin_to_attribute(params.builtin),
|
||||
std::string(params.attribute_name));
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/struct.h"
|
||||
#include "src/ast/struct_decoration.h"
|
||||
|
@ -29,170 +28,142 @@
|
|||
#include "src/ast/type/u32_type.h"
|
||||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/ast/type/void_type.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_Type : public TestHelper, public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Alias) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::AliasType alias("alias", &f32);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&alias, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "alias");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &alias, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "alias");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Alias_NameCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Alias_NameCollision) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::AliasType alias("bool", &f32);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&alias, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool_tint_0");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &alias, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool_tint_0");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Array) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Array) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b, 4);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&a, "ary")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool ary[4]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &a, "ary")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool ary[4]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_ArrayOfArray) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArray) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b, 4);
|
||||
ast::type::ArrayType c(&a, 5);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&c, "ary")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool ary[5][4]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &c, "ary")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool ary[5][4]");
|
||||
}
|
||||
|
||||
// TODO(dsinclair): Is this possible? What order should it output in?
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) {
|
||||
TEST_F(HlslGeneratorImplTest_Type,
|
||||
DISABLED_EmitType_ArrayOfArrayOfRuntimeArray) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b, 4);
|
||||
ast::type::ArrayType c(&a, 5);
|
||||
ast::type::ArrayType d(&c);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&c, "ary")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool ary[5][4][1]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &c, "ary")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool ary[5][4][1]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_ArrayOfArrayOfArray) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b, 4);
|
||||
ast::type::ArrayType c(&a, 5);
|
||||
ast::type::ArrayType d(&c, 6);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&d, "ary")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool ary[6][5][4]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &d, "ary")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool ary[6][5][4]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Array_NameCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_NameCollision) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b, 4);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&a, "bool")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool bool_tint_0[4]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &a, "bool")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool bool_tint_0[4]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Array_WithoutName) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Array_WithoutName) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b, 4);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&a, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool[4]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &a, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool[4]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_RuntimeArray) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_RuntimeArray) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&a, "ary")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool ary[]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &a, "ary")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool ary[]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_RuntimeArray_NameCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_Type,
|
||||
DISABLED_EmitType_RuntimeArray_NameCollision) {
|
||||
ast::type::BoolType b;
|
||||
ast::type::ArrayType a(&b);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&a, "double")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool double_tint_0[]");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &a, "double")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool double_tint_0[]");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Bool) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Bool) {
|
||||
ast::type::BoolType b;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&b, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "bool");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &b, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "bool");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_F32) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_F32) {
|
||||
ast::type::F32Type f32;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&f32, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "float");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &f32, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "float");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_I32) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_I32) {
|
||||
ast::type::I32Type i32;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&i32, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "int");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &i32, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "int");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Matrix) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Matrix) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::MatrixType m(&f32, 3, 2);
|
||||
|
||||
ast::Module mod;
|
||||
GeneratorImpl g(&mod);
|
||||
ASSERT_TRUE(g.EmitType(&m, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "matrix<float, 3, 2>");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &m, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "matrix<float, 3, 2>");
|
||||
}
|
||||
|
||||
// TODO(dsinclair): How to annotate as workgroup?
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Pointer) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Pointer) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::PointerType p(&f32, ast::StorageClass::kWorkgroup);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&p, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "float*");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &p, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "float*");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Struct) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct) {
|
||||
ast::type::I32Type i32;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
|
@ -210,16 +181,14 @@ TEST_F(HlslGeneratorImplTest, EmitType_Struct) {
|
|||
|
||||
ast::type::StructType s(std::move(str));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct {
|
||||
ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct {
|
||||
int a;
|
||||
float b;
|
||||
})");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_InjectPadding) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_InjectPadding) {
|
||||
ast::type::I32Type i32;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
|
@ -243,10 +212,8 @@ TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_InjectPadding) {
|
|||
|
||||
ast::type::StructType s(std::move(str));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct {
|
||||
ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct {
|
||||
int8_t pad_0[4];
|
||||
int a;
|
||||
int8_t pad_1[24];
|
||||
|
@ -256,7 +223,7 @@ TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_InjectPadding) {
|
|||
})");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Struct_NameCollision) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Struct_NameCollision) {
|
||||
ast::type::I32Type i32;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
|
@ -273,17 +240,15 @@ TEST_F(HlslGeneratorImplTest, EmitType_Struct_NameCollision) {
|
|||
|
||||
ast::type::StructType s(std::move(str));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct {
|
||||
ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct {
|
||||
int double_tint_0;
|
||||
float float_tint_0;
|
||||
})");
|
||||
}
|
||||
|
||||
// TODO(dsinclair): How to translate [[block]]
|
||||
TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, DISABLED_EmitType_Struct_WithDecoration) {
|
||||
ast::type::I32Type i32;
|
||||
ast::type::F32Type f32;
|
||||
|
||||
|
@ -302,41 +267,33 @@ TEST_F(HlslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) {
|
|||
|
||||
ast::type::StructType s(std::move(str));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&s, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(struct {
|
||||
ASSERT_TRUE(gen().EmitType(out(), &s, "")) << gen().error();
|
||||
EXPECT_EQ(result(), R"(struct {
|
||||
int a;
|
||||
float b;
|
||||
})");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_U32) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_U32) {
|
||||
ast::type::U32Type u32;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&u32, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "uint");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &u32, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "uint");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Vector) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Vector) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType v(&f32, 3);
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&v, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "vector<float, 3>");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &v, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "vector<float, 3>");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, EmitType_Void) {
|
||||
TEST_F(HlslGeneratorImplTest_Type, EmitType_Void) {
|
||||
ast::type::VoidType v;
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitType(&v, "")) << g.error();
|
||||
EXPECT_EQ(g.result(), "void");
|
||||
ASSERT_TRUE(gen().EmitType(out(), &v, "")) << gen().error();
|
||||
EXPECT_EQ(result(), "void");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -15,11 +15,10 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/unary_op_expression.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
|
@ -34,19 +33,18 @@ inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
|
|||
out << data.op;
|
||||
return out;
|
||||
}
|
||||
using HlslUnaryOpTest = testing::TestWithParam<UnaryOpData>;
|
||||
class HlslUnaryOpTest : public TestHelper,
|
||||
public testing::TestWithParam<UnaryOpData> {};
|
||||
TEST_P(HlslUnaryOpTest, Emit) {
|
||||
auto params = GetParam();
|
||||
|
||||
auto expr = std::make_unique<ast::IdentifierExpression>("expr");
|
||||
ast::UnaryOpExpression op(params.op, std::move(expr));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitExpression(&op)) << g.error();
|
||||
EXPECT_EQ(g.result(), std::string(params.name) + "(expr)");
|
||||
ASSERT_TRUE(gen().EmitExpression(out(), &op)) << gen().error();
|
||||
EXPECT_EQ(result(), std::string(params.name) + "(expr)");
|
||||
}
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest,
|
||||
INSTANTIATE_TEST_SUITE_P(HlslGeneratorImplTest_UnaryOp,
|
||||
HlslUnaryOpTest,
|
||||
testing::Values(UnaryOpData{"!", ast::UnaryOp::kNot},
|
||||
UnaryOpData{"-",
|
||||
|
|
|
@ -15,7 +15,6 @@
|
|||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/identifier_expression.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/ast/type/array_type.h"
|
||||
|
@ -23,47 +22,42 @@
|
|||
#include "src/ast/type/vector_type.h"
|
||||
#include "src/ast/variable.h"
|
||||
#include "src/ast/variable_decl_statement.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
#include "src/writer/hlsl/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
namespace {
|
||||
|
||||
using HlslGeneratorImplTest = testing::Test;
|
||||
class HlslGeneratorImplTest_VariableDecl : public TestHelper,
|
||||
public testing::Test {};
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Const) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Const) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &f32);
|
||||
var->set_is_const(true);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " const float a;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), " const float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Array) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::ArrayType ary(&f32, 5);
|
||||
|
||||
|
@ -71,46 +65,39 @@ TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Array) {
|
|||
std::make_unique<ast::Variable>("a", ast::StorageClass::kNone, &ary);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a[5];\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), " float a[5];\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Function) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl,
|
||||
Emit_VariableDeclStatement_Function) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kFunction, &f32);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Private) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl, Emit_VariableDeclStatement_Private) {
|
||||
ast::type::F32Type f32;
|
||||
auto var =
|
||||
std::make_unique<ast::Variable>("a", ast::StorageClass::kPrivate, &f32);
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
gen().increment_indent();
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
g.increment_indent();
|
||||
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), " float a;\n");
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), " float a;\n");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl,
|
||||
Emit_VariableDeclStatement_Initializer_Private) {
|
||||
auto ident = std::make_unique<ast::IdentifierExpression>("initializer");
|
||||
|
||||
ast::type::F32Type f32;
|
||||
|
@ -119,15 +106,13 @@ TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_Private) {
|
|||
var->set_constructor(std::move(ident));
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(float a = initializer;
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(float a = initializer;
|
||||
)");
|
||||
}
|
||||
|
||||
TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
|
||||
TEST_F(HlslGeneratorImplTest_VariableDecl,
|
||||
Emit_VariableDeclStatement_Initializer_ZeroVec) {
|
||||
ast::type::F32Type f32;
|
||||
ast::type::VectorType vec(&f32, 3);
|
||||
|
||||
|
@ -140,11 +125,8 @@ TEST_F(HlslGeneratorImplTest, Emit_VariableDeclStatement_Initializer_ZeroVec) {
|
|||
var->set_constructor(std::move(zero_vec));
|
||||
|
||||
ast::VariableDeclStatement stmt(std::move(var));
|
||||
|
||||
ast::Module m;
|
||||
GeneratorImpl g(&m);
|
||||
ASSERT_TRUE(g.EmitStatement(&stmt)) << g.error();
|
||||
EXPECT_EQ(g.result(), R"(vector<float, 3> a = vector<float, 3>(0.0f);
|
||||
ASSERT_TRUE(gen().EmitStatement(out(), &stmt)) << gen().error();
|
||||
EXPECT_EQ(result(), R"(vector<float, 3> a = vector<float, 3>(0.0f);
|
||||
)");
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
// 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/test_helper.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
|
||||
TestHelper::TestHelper() : td_(&ctx_, &mod_), impl_(&mod_) {}
|
||||
|
||||
TestHelper::~TestHelper() = default;
|
||||
|
||||
} // namespace hlsl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
|
@ -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_TEST_HELPER_H_
|
||||
#define SRC_WRITER_HLSL_TEST_HELPER_H_
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "src/ast/module.h"
|
||||
#include "src/context.h"
|
||||
#include "src/type_determiner.h"
|
||||
#include "src/writer/hlsl/generator_impl.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
namespace hlsl {
|
||||
|
||||
/// Helper class for testing
|
||||
class TestHelper {
|
||||
public:
|
||||
TestHelper();
|
||||
~TestHelper();
|
||||
|
||||
/// @returns the generator implementation
|
||||
GeneratorImpl& gen() { return impl_; }
|
||||
|
||||
/// @returns the module
|
||||
ast::Module* mod() { return &mod_; }
|
||||
|
||||
/// @returns the type determiner
|
||||
TypeDeterminer& td() { return td_; }
|
||||
|
||||
/// @returns the output stream
|
||||
std::ostream& out() { return out_; }
|
||||
|
||||
/// @returns the result string
|
||||
std::string result() const { return out_.str(); }
|
||||
|
||||
private:
|
||||
Context ctx_;
|
||||
ast::Module mod_;
|
||||
TypeDeterminer td_;
|
||||
GeneratorImpl impl_;
|
||||
std::ostringstream out_;
|
||||
};
|
||||
|
||||
} // namespace hlsl
|
||||
} // namespace writer
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_WRITER_HLSL_TEST_HELPER_H_
|
Loading…
Reference in New Issue