GLSL: implement bitcast operators.

Bug: tint:1265
Change-Id: I6768bde2be8da7281c9516528485e49f6c7c0460
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/68380
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
This commit is contained in:
Stephen White 2021-11-04 17:40:15 +00:00 committed by Tint LUCI CQ
parent e0e45a4d47
commit c5aab619f3
3 changed files with 32 additions and 15 deletions

View File

@ -177,22 +177,39 @@ bool GeneratorImpl::EmitArrayAccessor(
bool GeneratorImpl::EmitBitcast(std::ostream& out, bool GeneratorImpl::EmitBitcast(std::ostream& out,
const ast::BitcastExpression* expr) { const ast::BitcastExpression* expr) {
auto* type = TypeOf(expr); auto* src_type = TypeOf(expr->expr);
if (auto* vec = type->UnwrapRef()->As<sem::Vector>()) { auto* dst_type = TypeOf(expr);
type = vec->type();
}
if (!type->is_integer_scalar() && !type->is_float_scalar()) { if (!dst_type->is_integer_scalar_or_vector() &&
diagnostics_.add_error(diag::System::Writer, !dst_type->is_float_scalar_or_vector()) {
"Unable to do bitcast to type " + type->type_name()); diagnostics_.add_error(
diag::System::Writer,
"Unable to do bitcast to type " + dst_type->type_name());
return false; return false;
} }
out << "as"; if (src_type == dst_type) {
if (!EmitType(out, type, ast::StorageClass::kNone, ast::Access::kReadWrite, return EmitExpression(out, expr->expr);
"")) { }
if (src_type->is_float_scalar_or_vector() &&
dst_type->is_signed_scalar_or_vector()) {
out << "floatBitsToInt";
} else if (src_type->is_float_scalar_or_vector() &&
dst_type->is_unsigned_scalar_or_vector()) {
out << "floatBitsToUint";
} else if (src_type->is_signed_scalar_or_vector() &&
dst_type->is_float_scalar_or_vector()) {
out << "intBitsToFloat";
} else if (src_type->is_unsigned_scalar_or_vector() &&
dst_type->is_float_scalar_or_vector()) {
out << "uintBitsToFloat";
} else {
if (!EmitType(out, dst_type, ast::StorageClass::kNone,
ast::Access::kReadWrite, "")) {
return false; return false;
} }
}
out << "("; out << "(";
if (!EmitExpression(out, expr->expr)) { if (!EmitExpression(out, expr->expr)) {
return false; return false;

View File

@ -491,7 +491,7 @@ if (tint_tmp) {
tint_tmp = (tint_tmp_1); tint_tmp = (tint_tmp_1);
} }
)"); )");
EXPECT_EQ(out.str(), R"(asint((tint_tmp)))"); EXPECT_EQ(out.str(), R"(int((tint_tmp)))");
} }
TEST_F(GlslGeneratorImplTest_Binary, Call_WithLogical) { TEST_F(GlslGeneratorImplTest_Binary, Call_WithLogical) {

View File

@ -29,7 +29,7 @@ TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Float) {
std::stringstream out; std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error();
EXPECT_EQ(out.str(), "asfloat(1)"); EXPECT_EQ(out.str(), "intBitsToFloat(1)");
} }
TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Int) { TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Int) {
@ -40,7 +40,7 @@ TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Int) {
std::stringstream out; std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error();
EXPECT_EQ(out.str(), "asint(1u)"); EXPECT_EQ(out.str(), "int(1u)");
} }
TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Uint) { TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Uint) {
@ -51,7 +51,7 @@ TEST_F(GlslGeneratorImplTest_Bitcast, EmitExpression_Bitcast_Uint) {
std::stringstream out; std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(out, bitcast)) << gen.error();
EXPECT_EQ(out.str(), "asuint(1)"); EXPECT_EQ(out.str(), "uint(1)");
} }
} // namespace } // namespace