writer/hlsl: Implement select()

Fixed: tint:304
Change-Id: I8821bc6e4bfa0487161b8d915a6a107afd474105
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/53389
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-07 19:17:34 +00:00 committed by Tint LUCI CQ
parent 53426a4538
commit 2d9ba5d8ad
19 changed files with 267 additions and 178 deletions

View File

@ -587,8 +587,7 @@ bool GeneratorImpl::EmitCall(std::ostream& pre,
return EmitTextureCall(pre, out, expr, intrinsic); return EmitTextureCall(pre, out, expr, intrinsic);
} }
if (intrinsic->Type() == sem::IntrinsicType::kSelect) { if (intrinsic->Type() == sem::IntrinsicType::kSelect) {
diagnostics_.add_error("select not supported in HLSL backend yet"); return EmitSelectCall(pre, out, expr);
return false;
} else if (intrinsic->Type() == sem::IntrinsicType::kIsNormal) { } else if (intrinsic->Type() == sem::IntrinsicType::kIsNormal) {
diagnostics_.add_error("is_normal not supported in HLSL backend yet"); diagnostics_.add_error("is_normal not supported in HLSL backend yet");
return false; return false;
@ -676,6 +675,32 @@ bool GeneratorImpl::EmitCall(std::ostream& pre,
return true; return true;
} }
bool GeneratorImpl::EmitSelectCall(std::ostream& pre,
std::ostream& out,
ast::CallExpression* expr) {
auto* expr_true = expr->params()[0];
auto* expr_false = expr->params()[1];
auto* expr_cond = expr->params()[2];
ScopedParen paren(out);
if (!EmitExpression(pre, out, expr_cond)) {
return false;
}
out << " ? ";
if (!EmitExpression(pre, out, expr_true)) {
return false;
}
out << " : ";
if (!EmitExpression(pre, out, expr_false)) {
return false;
}
return true;
}
bool GeneratorImpl::EmitDataPackingCall(std::ostream& pre, bool GeneratorImpl::EmitDataPackingCall(std::ostream& pre,
std::ostream& out, std::ostream& out,
ast::CallExpression* expr, ast::CallExpression* expr,

View File

@ -139,6 +139,16 @@ class GeneratorImpl : public TextGenerator {
std::ostream& out, std::ostream& out,
ast::CallExpression* expr, ast::CallExpression* expr,
const sem::Intrinsic* intrinsic); const sem::Intrinsic* intrinsic);
/// Handles generating a call to the `select()` intrinsic
/// @param pre the preamble of the expression stream
/// @param out the output of the expression stream
/// @param expr the call expression
/// @returns true if the call expression is emitted
bool EmitSelectCall(std::ostream& pre,
std::ostream& out,
ast::CallExpression* expr);
/// Handles generating a call to data packing intrinsic /// Handles generating a call to data packing intrinsic
/// @param pre the preamble of the expression stream /// @param pre the preamble of the expression stream
/// @param out the output of the expression stream /// @param out the output of the expression stream

View File

@ -272,6 +272,28 @@ TEST_F(HlslGeneratorImplTest_Intrinsic, Intrinsic_Call) {
EXPECT_EQ(result(), "dot(param1, param2)"); EXPECT_EQ(result(), "dot(param1, param2)");
} }
TEST_F(HlslGeneratorImplTest_Intrinsic, Select_Scalar) {
auto* call = Call("select", 1.0f, 2.0f, true);
WrapInFunction(call);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error();
EXPECT_EQ(result(), "(true ? 1.0f : 2.0f)");
}
TEST_F(HlslGeneratorImplTest_Intrinsic, Select_Vector) {
auto* call =
Call("select", vec2<i32>(1, 2), vec2<i32>(3, 4), vec2<bool>(true, false));
WrapInFunction(call);
GeneratorImpl& gen = Build();
gen.increment_indent();
ASSERT_TRUE(gen.EmitExpression(pre, out, call)) << gen.error();
EXPECT_EQ(result(),
"(vector<bool, 2>(true, false) ? int2(1, 2) : int2(3, 4))");
}
TEST_F(HlslGeneratorImplTest_Intrinsic, Pack4x8Snorm) { TEST_F(HlslGeneratorImplTest_Intrinsic, Pack4x8Snorm) {
auto* call = Call("pack4x8snorm", "p1"); auto* call = Call("pack4x8snorm", "p1");
Global("p1", ty.vec4<f32>(), ast::StorageClass::kPrivate); Global("p1", ty.vec4<f32>(), ast::StorageClass::kPrivate);

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_00b848() {
fn select_00b848() { int2 res = (vector<bool, 2>(false, false) ? int2(0, 0) : int2(0, 0));
var res : vec2<i32> = select(vec2<i32>(), vec2<i32>(), vec2<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_00b848(); select_00b848();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_00b848(); select_00b848();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_00b848(); select_00b848();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_01e2cd() {
fn select_01e2cd() { int3 res = (vector<bool, 3>(false, false, false) ? int3(0, 0, 0) : int3(0, 0, 0));
var res : vec3<i32> = select(vec3<i32>(), vec3<i32>(), vec3<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_01e2cd(); select_01e2cd();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_01e2cd(); select_01e2cd();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_01e2cd(); select_01e2cd();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_1e960b() {
fn select_1e960b() { uint2 res = (vector<bool, 2>(false, false) ? uint2(0u, 0u) : uint2(0u, 0u));
var res : vec2<u32> = select(vec2<u32>(), vec2<u32>(), vec2<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_1e960b(); select_1e960b();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_1e960b(); select_1e960b();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_1e960b(); select_1e960b();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_266aff() {
fn select_266aff() { float2 res = (vector<bool, 2>(false, false) ? float2(0.0f, 0.0f) : float2(0.0f, 0.0f));
var res : vec2<f32> = select(vec2<f32>(), vec2<f32>(), vec2<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_266aff(); select_266aff();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_266aff(); select_266aff();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_266aff(); select_266aff();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_28a27e() {
fn select_28a27e() { uint3 res = (vector<bool, 3>(false, false, false) ? uint3(0u, 0u, 0u) : uint3(0u, 0u, 0u));
var res : vec3<u32> = select(vec3<u32>(), vec3<u32>(), vec3<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_28a27e(); select_28a27e();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_28a27e(); select_28a27e();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_28a27e(); select_28a27e();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_416e14() {
fn select_416e14() { float res = (false ? 1.0f : 1.0f);
var res : f32 = select(1.0, 1.0, bool());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_416e14(); select_416e14();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_416e14(); select_416e14();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_416e14(); select_416e14();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_80a9a9() {
fn select_80a9a9() { vector<bool, 3> res = (vector<bool, 3>(false, false, false) ? vector<bool, 3>(false, false, false) : vector<bool, 3>(false, false, false));
var res : vec3<bool> = select(vec3<bool>(), vec3<bool>(), vec3<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_80a9a9(); select_80a9a9();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_80a9a9(); select_80a9a9();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_80a9a9(); select_80a9a9();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_99f883() {
fn select_99f883() { uint res = (false ? 1u : 1u);
var res : u32 = select(1u, 1u, bool());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_99f883(); select_99f883();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_99f883(); select_99f883();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_99f883(); select_99f883();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_a2860e() {
fn select_a2860e() { int4 res = (vector<bool, 4>(false, false, false, false) ? int4(0, 0, 0, 0) : int4(0, 0, 0, 0));
var res : vec4<i32> = select(vec4<i32>(), vec4<i32>(), vec4<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_a2860e(); select_a2860e();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_a2860e(); select_a2860e();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_a2860e(); select_a2860e();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_bb8aae() {
fn select_bb8aae() { float4 res = (vector<bool, 4>(false, false, false, false) ? float4(0.0f, 0.0f, 0.0f, 0.0f) : float4(0.0f, 0.0f, 0.0f, 0.0f));
var res : vec4<f32> = select(vec4<f32>(), vec4<f32>(), vec4<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_bb8aae(); select_bb8aae();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_bb8aae(); select_bb8aae();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_bb8aae(); select_bb8aae();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_c31f9e() {
fn select_c31f9e() { bool res = (false ? false : false);
var res : bool = select(bool(), bool(), bool());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_c31f9e(); select_c31f9e();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_c31f9e(); select_c31f9e();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_c31f9e(); select_c31f9e();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_c4a4ef() {
fn select_c4a4ef() { uint4 res = (vector<bool, 4>(false, false, false, false) ? uint4(0u, 0u, 0u, 0u) : uint4(0u, 0u, 0u, 0u));
var res : vec4<u32> = select(vec4<u32>(), vec4<u32>(), vec4<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_c4a4ef(); select_c4a4ef();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_c4a4ef(); select_c4a4ef();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_c4a4ef(); select_c4a4ef();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_cb9301() {
fn select_cb9301() { vector<bool, 2> res = (vector<bool, 2>(false, false) ? vector<bool, 2>(false, false) : vector<bool, 2>(false, false));
var res : vec2<bool> = select(vec2<bool>(), vec2<bool>(), vec2<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_cb9301(); select_cb9301();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_cb9301(); select_cb9301();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_cb9301(); select_cb9301();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_e3e028() {
fn select_e3e028() { vector<bool, 4> res = (vector<bool, 4>(false, false, false, false) ? vector<bool, 4>(false, false, false, false) : vector<bool, 4>(false, false, false, false));
var res : vec4<bool> = select(vec4<bool>(), vec4<bool>(), vec4<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_e3e028(); select_e3e028();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_e3e028(); select_e3e028();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_e3e028(); select_e3e028();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_ebfea2() {
fn select_ebfea2() { float3 res = (vector<bool, 3>(false, false, false) ? float3(0.0f, 0.0f, 0.0f) : float3(0.0f, 0.0f, 0.0f));
var res : vec3<f32> = select(vec3<f32>(), vec3<f32>(), vec3<bool>());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_ebfea2(); select_ebfea2();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_ebfea2(); select_ebfea2();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_ebfea2(); select_ebfea2();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet

View File

@ -1,23 +1,25 @@
SKIP: FAILED struct tint_symbol {
float4 value : SV_Position;
};
void select_ed8a15() {
fn select_ed8a15() { int res = (false ? 1 : 1);
var res : i32 = select(1, 1, bool());
} }
[[stage(vertex)]] tint_symbol vertex_main() {
fn vertex_main() {
select_ed8a15(); select_ed8a15();
const tint_symbol tint_symbol_1 = {float4(0.0f, 0.0f, 0.0f, 0.0f)};
return tint_symbol_1;
} }
[[stage(fragment)]] void fragment_main() {
fn fragment_main() {
select_ed8a15(); select_ed8a15();
return;
} }
[[stage(compute)]] [numthreads(1, 1, 1)]
fn compute_main() { void compute_main() {
select_ed8a15(); select_ed8a15();
return;
} }
Failed to generate: error: select not supported in HLSL backend yet