writer/msl: Rework string printing

Add `out` parameters to expression and type generators.

Use the new helper classes in TextGenerator.
Cleans up bad formatting.

Prepares the writer generating 'pre' statements, required for atomics.

If-else statements are generated slightly differently. This is done so that 'pre' statements for the else conditions are scoped correctly. This is identical to the HLSL writer.

Bug tint:892

Change-Id: I4c6e96c90673ba30898b3682bf3198497d63a2d4
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/56067
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2021-06-28 15:30:57 +00:00 committed by Tint LUCI CQ
parent a5715e3320
commit f24b37e122
319 changed files with 1126 additions and 1081 deletions

File diff suppressed because it is too large Load Diff

View File

@ -64,62 +64,63 @@ class GeneratorImpl : public TextGenerator {
/// @returns true if the declared type was emitted
bool EmitTypeDecl(const sem::Type* ty);
/// Handles an array accessor expression
/// @param out the output of the expression 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 an assignment statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitAssign(ast::AssignmentStatement* stmt);
/// Handles generating a binary expression
/// @param out the output of the expression 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 generating a bitcast expression
/// @param out the output of the expression stream
/// @param expr the bitcast expression
/// @returns true if the bitcast was emitted
bool EmitBitcast(ast::BitcastExpression* expr);
bool EmitBitcast(std::ostream& out, ast::BitcastExpression* expr);
/// Handles a block statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBlock(const ast::BlockStatement* stmt);
/// Handles a block statement with a newline at the end
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitIndentedBlockAndNewline(ast::BlockStatement* stmt);
/// Handles a block statement with a newline at the end
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBlockAndNewline(const ast::BlockStatement* stmt);
/// Handles a break statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
bool EmitBreak(ast::BreakStatement* stmt);
/// Handles generating a call expression
/// @param out the output of the expression 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 generating an intrinsic call expression
/// @param out the output of the expression stream
/// @param expr the call expression
/// @param intrinsic the intrinsic being called
/// @returns true if the call expression is emitted
bool EmitIntrinsicCall(ast::CallExpression* expr,
bool EmitIntrinsicCall(std::ostream& out,
ast::CallExpression* expr,
const sem::Intrinsic* intrinsic);
/// Handles generating a call to a texture function (`textureSample`,
/// `textureSampleGrad`, etc)
/// @param out the output of the expression stream
/// @param expr the call expression
/// @param intrinsic the semantic information for the texture intrinsic
/// @returns true if the call expression is emitted
bool EmitTextureCall(ast::CallExpression* expr,
bool EmitTextureCall(std::ostream& out,
ast::CallExpression* expr,
const sem::Intrinsic* intrinsic);
/// Handles a case statement
/// @param stmt the statement
/// @returns true if the statement was emitted successfully
bool EmitCase(ast::CaseStatement* stmt);
/// Handles generating constructor expressions
/// @param out the output of the expression 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 a continue statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted successfully
@ -128,85 +129,106 @@ class GeneratorImpl : public TextGenerator {
/// @param stmt the discard statement
/// @returns true if the statement was successfully emitted
bool EmitDiscard(ast::DiscardStatement* stmt);
/// Handles generating an else statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitElse(ast::ElseStatement* stmt);
/// Handles emitting the entry point function
/// @param func the entry point function
/// @returns true if the entry point function was emitted
bool EmitEntryPointFunction(ast::Function* func);
/// Handles generate an Expression
/// @param out the output of the expression 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 func the function to generate
/// @returns true if the function was emitted
bool EmitFunction(ast::Function* func);
/// Handles generating an identifier expression
/// @param out the output of the expression stream
/// @param expr the identifier expression
/// @returns true if the identifier was emitted
bool EmitIdentifier(ast::IdentifierExpression* expr);
bool EmitIdentifier(std::ostream& out, ast::IdentifierExpression* expr);
/// Handles an if statement
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitIf(ast::IfStatement* stmt);
/// Handles a literal
/// @param out the output of the expression 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 stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitLoop(ast::LoopStatement* stmt);
/// Handles a member accessor expression
/// @param out the output of the expression 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 return statements
/// @param stmt the statement to emit
/// @returns true if the statement was successfully emitted
bool EmitReturn(ast::ReturnStatement* stmt);
/// Handles generating a scalar constructor
/// @param out the output of the expression 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 pipeline stage name
/// @param out the output of the expression stream
/// @param stage the stage to emit
void EmitStage(ast::PipelineStage stage);
void EmitStage(std::ostream& out, ast::PipelineStage stage);
/// Handles statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitStatement(ast::Statement* stmt);
/// Emits a list of statements
/// @param stmts the statement list
/// @returns true if the statements were emitted successfully
bool EmitStatements(const ast::StatementList& stmts);
/// Emits a list of statements with an indentation
/// @param stmts the statement list
/// @returns true if the statements were emitted successfully
bool EmitStatementsWithIndent(const ast::StatementList& stmts);
/// Handles generating a switch statement
/// @param stmt the statement to emit
/// @returns true if the statement was emitted
bool EmitSwitch(ast::SwitchStatement* stmt);
/// Handles generating a type
/// @param out the output of the type 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(const sem::Type* type, const std::string& name);
bool EmitType(std::ostream& out,
const sem::Type* type,
const std::string& name);
/// Handles generating an MSL-packed storage type.
/// If the type does not have a packed form, the standard non-packed form is
/// emitted.
/// @param out the output of the type 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 EmitPackedType(const sem::Type* type, const std::string& name);
bool EmitPackedType(std::ostream& out,
const sem::Type* type,
const std::string& name);
/// Handles generating a struct declaration
/// @param str the struct to generate
/// @returns true if the struct is emitted
bool EmitStructType(const sem::Struct* str);
/// Handles emitting a type constructor
/// @param out the output of the expression 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 unary op expression
/// @param out the output of the expression 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);
/// Handles generating a variable
/// @param var the variable to generate
/// @returns true if the variable was emitted
@ -216,9 +238,10 @@ class GeneratorImpl : public TextGenerator {
/// @returns true if the variable was emitted
bool EmitProgramConstVariable(const ast::Variable* var);
/// Emits the zero value for the given type
/// @param out the output of the expression stream
/// @param type the type to emit the value for
/// @returns true if the zero value was successfully emitted.
bool EmitZeroValue(const sem::Type* type);
bool EmitZeroValue(std::ostream& out, const sem::Type* type);
/// Handles generating a builtin name
/// @param intrinsic the semantic info for the intrinsic
@ -259,15 +282,6 @@ class GeneratorImpl : public TextGenerator {
/// type.
SizeAndAlign MslPackedTypeSizeAndAlign(const sem::Type* ty);
/// Emits `prefix`, followed by an opening brace `{`, then calls `cb` to emit
/// the block body, then finally emits the closing brace `}`.
/// @param prefix the string to emit before the opening brace
/// @param cb a function or function-like object with the signature `bool()`
/// that emits the block body.
/// @returns the return value of `cb`.
template <typename F>
bool EmitBlockBraces(const std::string& prefix, F&& cb);
const Program* program_ = nullptr;
std::function<bool()> emit_continuing_;
};

View File

@ -28,8 +28,9 @@ TEST_F(MslGeneratorImplTest, ArrayAccessor) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "ary[5]");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "ary[5]");
}
TEST_F(MslGeneratorImplTest, ArrayAccessor_OfDref) {
@ -41,8 +42,9 @@ TEST_F(MslGeneratorImplTest, ArrayAccessor_OfDref) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "(*(p))[5]");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "(*(p))[5]");
}
} // namespace

View File

@ -47,8 +47,9 @@ TEST_P(MslBinaryTest, Emit) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), params.result);
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), params.result);
}
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,

View File

@ -27,8 +27,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Bitcast) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(bitcast)) << gen.error();
EXPECT_EQ(gen.result(), "as_type<float>(1)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error();
EXPECT_EQ(out.str(), "as_type<float>(1)");
}
} // namespace

View File

@ -47,7 +47,8 @@ TEST_F(MslGeneratorImplTest, Emit_Block_WithoutNewline) {
ASSERT_TRUE(gen.EmitBlock(b)) << gen.error();
EXPECT_EQ(gen.result(), R"( {
discard_fragment();
})");
}
)");
}
} // namespace

View File

@ -31,8 +31,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithoutParams) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), "my_func()");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "my_func()");
}
TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithParams) {
@ -50,8 +51,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Call_WithParams) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), "my_func(param1, param2)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "my_func(param1, param2)");
}
TEST_F(MslGeneratorImplTest, EmitStatement_Call) {

View File

@ -27,8 +27,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Scalar) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(cast)) << gen.error();
EXPECT_EQ(gen.result(), "float(1)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.error();
EXPECT_EQ(out.str(), "float(1)");
}
TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) {
@ -37,8 +38,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_Cast_Vector) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(cast)) << gen.error();
EXPECT_EQ(gen.result(), "float3(int3(1, 2, 3))");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, cast)) << gen.error();
EXPECT_EQ(out.str(), "float3(int3(1, 2, 3))");
}
} // namespace

View File

@ -29,8 +29,9 @@ TEST_F(MslGeneratorImplTest, EmitIdentifierExpression) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(i)) << gen.error();
EXPECT_EQ(gen.result(), "foo");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, i)) << gen.error();
EXPECT_EQ(out.str(), "foo");
}
} // namespace

View File

@ -50,9 +50,11 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithElseIf) {
ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
EXPECT_EQ(gen.result(), R"( if (cond) {
return;
} else if (else_cond) {
} else {
if (else_cond) {
return;
}
}
)");
}
@ -88,11 +90,13 @@ TEST_F(MslGeneratorImplTest, Emit_IfWithMultiple) {
ASSERT_TRUE(gen.EmitStatement(i)) << gen.error();
EXPECT_EQ(gen.result(), R"( if (cond) {
return;
} else if (else_cond) {
} else {
if (else_cond) {
return;
} else {
return;
}
}
)");
}

View File

@ -81,8 +81,9 @@ TEST_F(MslGeneratorImplTest, MslImportData_SingleParamTest_IntScalar) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), R"(abs(1))");
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), R"(abs(1))");
}
using MslImportData_DualParamTest = TestParamHelper<MslImportData>;
@ -94,8 +95,9 @@ TEST_P(MslImportData_DualParamTest, FloatScalar) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string(param.msl_name) + "(1.0f, 2.0f)");
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.msl_name) + "(1.0f, 2.0f)");
}
INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest,
MslImportData_DualParamTest,
@ -117,8 +119,9 @@ TEST_P(MslImportData_DualParam_VectorTest, FloatVector) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string(param.msl_name) +
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.msl_name) +
"(float3(1.0f, 2.0f, 3.0f), "
"float3(4.0f, 5.0f, 6.0f))");
}
@ -135,8 +138,9 @@ TEST_P(MslImportData_DualParam_Int_Test, IntScalar) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string(param.msl_name) + "(1, 2)");
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.msl_name) + "(1, 2)");
}
INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest,
MslImportData_DualParam_Int_Test,
@ -152,8 +156,9 @@ TEST_P(MslImportData_TripleParamTest, FloatScalar) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string(param.msl_name) + "(1.0f, 2.0f, 3.0f)");
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.msl_name) + "(1.0f, 2.0f, 3.0f)");
}
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,
@ -173,8 +178,9 @@ TEST_P(MslImportData_TripleParam_Int_Test, IntScalar) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string(param.msl_name) + "(1, 2, 3)");
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string(param.msl_name) + "(1, 2, 3)");
}
INSTANTIATE_TEST_SUITE_P(MslGeneratorImplTest,
MslImportData_TripleParam_Int_Test,
@ -190,8 +196,9 @@ TEST_F(MslGeneratorImplTest, MslImportData_Determinant) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitCall(expr)) << gen.error();
EXPECT_EQ(gen.result(), std::string("determinant(var)"));
std::stringstream out;
ASSERT_TRUE(gen.EmitCall(out, expr)) << gen.error();
EXPECT_EQ(out.str(), std::string("determinant(var)"));
}
} // namespace

View File

@ -291,9 +291,9 @@ TEST_F(MslGeneratorImplTest, Intrinsic_Call) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), " dot(param1, param2)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "dot(param1, param2)");
}
TEST_F(MslGeneratorImplTest, StorageBarrier) {
@ -302,9 +302,9 @@ TEST_F(MslGeneratorImplTest, StorageBarrier) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), " threadgroup_barrier(mem_flags::mem_device)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "threadgroup_barrier(mem_flags::mem_device)");
}
TEST_F(MslGeneratorImplTest, WorkgroupBarrier) {
@ -313,9 +313,9 @@ TEST_F(MslGeneratorImplTest, WorkgroupBarrier) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), " threadgroup_barrier(mem_flags::mem_threadgroup)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "threadgroup_barrier(mem_flags::mem_threadgroup)");
}
TEST_F(MslGeneratorImplTest, Pack2x16Float) {
@ -325,9 +325,9 @@ TEST_F(MslGeneratorImplTest, Pack2x16Float) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), " as_type<uint>(half2(p1))");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "as_type<uint>(half2(p1))");
}
TEST_F(MslGeneratorImplTest, Unpack2x16Float) {
@ -337,9 +337,9 @@ TEST_F(MslGeneratorImplTest, Unpack2x16Float) {
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
EXPECT_EQ(gen.result(), " float2(as_type<half2>(p1))");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "float2(as_type<half2>(p1))");
}
TEST_F(MslGeneratorImplTest, Ignore) {

View File

@ -281,10 +281,11 @@ TEST_P(MslGeneratorIntrinsicTextureTest, Call) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(call)) << gen.error();
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
auto expected = expected_texture_overload(param.overload);
EXPECT_EQ(expected, gen.result());
EXPECT_EQ(expected, out.str());
}
INSTANTIATE_TEST_SUITE_P(

View File

@ -29,8 +29,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "str.mem");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "str.mem");
}
TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor_Swizzle_xyz) {
@ -40,8 +41,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor_Swizzle_xyz) {
WrapInFunction(expr);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "my_vec.xyz");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "my_vec.xyz");
}
TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor_Swizzle_gbr) {
@ -51,8 +53,9 @@ TEST_F(MslGeneratorImplTest, EmitExpression_MemberAccessor_Swizzle_gbr) {
WrapInFunction(expr);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(expr)) << gen.error();
EXPECT_EQ(gen.result(), "my_vec.gbr");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, expr)) << gen.error();
EXPECT_EQ(out.str(), "my_vec.gbr");
}
} // namespace

View File

@ -66,8 +66,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Array) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(arr), "ary")) << gen.error();
EXPECT_EQ(gen.result(), "bool ary[4]");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(arr), "ary")) << gen.error();
EXPECT_EQ(out.str(), "bool ary[4]");
}
TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArray) {
@ -77,8 +78,9 @@ TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArray) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(b), "ary")) << gen.error();
EXPECT_EQ(gen.result(), "bool ary[5][4]");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(b), "ary")) << gen.error();
EXPECT_EQ(out.str(), "bool ary[5][4]");
}
TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) {
@ -89,8 +91,9 @@ TEST_F(MslGeneratorImplTest, EmitType_ArrayOfArrayOfArray) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(c), "ary")) << gen.error();
EXPECT_EQ(gen.result(), "bool ary[6][5][4]");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(c), "ary")) << gen.error();
EXPECT_EQ(out.str(), "bool ary[6][5][4]");
}
TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) {
@ -99,8 +102,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Array_WithoutName) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(arr), "")) << gen.error();
EXPECT_EQ(gen.result(), "bool[4]");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(arr), "")) << gen.error();
EXPECT_EQ(out.str(), "bool[4]");
}
TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray) {
@ -109,8 +113,9 @@ TEST_F(MslGeneratorImplTest, EmitType_RuntimeArray) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(arr), "ary")) << gen.error();
EXPECT_EQ(gen.result(), "bool ary[1]");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(arr), "ary")) << gen.error();
EXPECT_EQ(out.str(), "bool ary[1]");
}
TEST_F(MslGeneratorImplTest, EmitType_ArrayWithStride) {
@ -143,8 +148,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Bool) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(bool_, "")) << gen.error();
EXPECT_EQ(gen.result(), "bool");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, bool_, "")) << gen.error();
EXPECT_EQ(out.str(), "bool");
}
TEST_F(MslGeneratorImplTest, EmitType_F32) {
@ -152,8 +158,9 @@ TEST_F(MslGeneratorImplTest, EmitType_F32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(f32, "")) << gen.error();
EXPECT_EQ(gen.result(), "float");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, f32, "")) << gen.error();
EXPECT_EQ(out.str(), "float");
}
TEST_F(MslGeneratorImplTest, EmitType_I32) {
@ -161,8 +168,9 @@ TEST_F(MslGeneratorImplTest, EmitType_I32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(i32, "")) << gen.error();
EXPECT_EQ(gen.result(), "int");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, i32, "")) << gen.error();
EXPECT_EQ(out.str(), "int");
}
TEST_F(MslGeneratorImplTest, EmitType_Matrix) {
@ -172,8 +180,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Matrix) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(mat2x3, "")) << gen.error();
EXPECT_EQ(gen.result(), "float2x3");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, mat2x3, "")) << gen.error();
EXPECT_EQ(out.str(), "float2x3");
}
TEST_F(MslGeneratorImplTest, EmitType_Pointer) {
@ -183,8 +192,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Pointer) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(p, "")) << gen.error();
EXPECT_EQ(gen.result(), "threadgroup float* ");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, p, "")) << gen.error();
EXPECT_EQ(out.str(), "threadgroup float* ");
}
TEST_F(MslGeneratorImplTest, EmitType_Struct) {
@ -195,8 +205,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Struct) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(s), "")) << gen.error();
EXPECT_EQ(gen.result(), "S");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(s), "")) << gen.error();
EXPECT_EQ(out.str(), "S");
}
TEST_F(MslGeneratorImplTest, EmitType_StructDecl) {
@ -670,8 +681,9 @@ TEST_F(MslGeneratorImplTest, DISABLED_EmitType_Struct_WithDecoration) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(s), "")) << gen.error();
EXPECT_EQ(gen.result(), R"(struct {
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(s), "")) << gen.error();
EXPECT_EQ(out.str(), R"(struct {
/* 0x0000 */ int a;
/* 0x0004 */ float b;
})");
@ -682,8 +694,9 @@ TEST_F(MslGeneratorImplTest, EmitType_U32) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(u32, "")) << gen.error();
EXPECT_EQ(gen.result(), "uint");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, u32, "")) << gen.error();
EXPECT_EQ(out.str(), "uint");
}
TEST_F(MslGeneratorImplTest, EmitType_Vector) {
@ -692,8 +705,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Vector) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(vec3, "")) << gen.error();
EXPECT_EQ(gen.result(), "float3");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, vec3, "")) << gen.error();
EXPECT_EQ(out.str(), "float3");
}
TEST_F(MslGeneratorImplTest, EmitType_Void) {
@ -701,8 +715,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Void) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(void_, "")) << gen.error();
EXPECT_EQ(gen.result(), "void");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, void_, "")) << gen.error();
EXPECT_EQ(out.str(), "void");
}
TEST_F(MslGeneratorImplTest, EmitType_Sampler) {
@ -710,8 +725,9 @@ TEST_F(MslGeneratorImplTest, EmitType_Sampler) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(sampler, "")) << gen.error();
EXPECT_EQ(gen.result(), "sampler");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, sampler, "")) << gen.error();
EXPECT_EQ(out.str(), "sampler");
}
TEST_F(MslGeneratorImplTest, EmitType_SamplerComparison) {
@ -719,8 +735,9 @@ TEST_F(MslGeneratorImplTest, EmitType_SamplerComparison) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(sampler, "")) << gen.error();
EXPECT_EQ(gen.result(), "sampler");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, sampler, "")) << gen.error();
EXPECT_EQ(out.str(), "sampler");
}
struct MslDepthTextureData {
@ -739,8 +756,9 @@ TEST_P(MslDepthTexturesTest, Emit) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(&s, "")) << gen.error();
EXPECT_EQ(gen.result(), params.result);
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, &s, "")) << gen.error();
EXPECT_EQ(out.str(), params.result);
}
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,
@ -772,8 +790,9 @@ TEST_P(MslSampledtexturesTest, Emit) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(s, "")) << gen.error();
EXPECT_EQ(gen.result(), params.result);
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, s, "")) << gen.error();
EXPECT_EQ(out.str(), params.result);
}
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,
@ -798,8 +817,9 @@ TEST_F(MslGeneratorImplTest, Emit_TypeMultisampledTexture) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(ms, "")) << gen.error();
EXPECT_EQ(gen.result(), "texture2d_ms<uint, access::read>");
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, ms, "")) << gen.error();
EXPECT_EQ(out.str(), "texture2d_ms<uint, access::read>");
}
struct MslStorageTextureData {
@ -826,8 +846,9 @@ TEST_P(MslStorageTexturesTest, Emit) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitType(program->TypeOf(s), "")) << gen.error();
EXPECT_EQ(gen.result(), params.result);
std::stringstream out;
ASSERT_TRUE(gen.EmitType(out, program->TypeOf(s), "")) << gen.error();
EXPECT_EQ(out.str(), params.result);
}
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,

View File

@ -29,8 +29,9 @@ TEST_F(MslUnaryOpTest, AddressOf) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "&(expr)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "&(expr)");
}
TEST_F(MslUnaryOpTest, Complement) {
@ -41,8 +42,9 @@ TEST_F(MslUnaryOpTest, Complement) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "~(expr)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "~(expr)");
}
TEST_F(MslUnaryOpTest, Indirection) {
@ -56,8 +58,9 @@ TEST_F(MslUnaryOpTest, Indirection) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "*(expr)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "*(expr)");
}
TEST_F(MslUnaryOpTest, Not) {
@ -67,8 +70,9 @@ TEST_F(MslUnaryOpTest, Not) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "!(expr)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "!(expr)");
}
TEST_F(MslUnaryOpTest, Negation) {
@ -79,8 +83,9 @@ TEST_F(MslUnaryOpTest, Negation) {
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "-(expr)");
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, op)) << gen.error();
EXPECT_EQ(out.str(), "-(expr)");
}
} // namespace

View File

@ -44,8 +44,9 @@ TextGenerator::LineWriter::~LineWriter() {
auto str = os.str();
if (!str.empty()) {
gen->make_indent();
gen->out_ << str << std::endl;
gen->out_ << str;
}
gen->out_ << std::endl;
}
}