GLSL: implement fma().

Bug: tint:1448
Change-Id: I7e331a2eabd507a4babce756fc79d68b0bf7d7be
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/82145
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
This commit is contained in:
Stephen White
2022-02-28 20:03:11 +00:00
committed by Tint LUCI CQ
parent 7028077a6a
commit 9b152e655f
14 changed files with 64 additions and 152 deletions

View File

@@ -610,6 +610,9 @@ bool GeneratorImpl::EmitBuiltinCall(std::ostream& out,
if (builtin->Type() == sem::BuiltinType::kInsertBits) {
return EmitInsertBits(out, expr);
}
if (builtin->Type() == sem::BuiltinType::kFma && version_.IsES()) {
return EmitEmulatedFMA(out, expr);
}
if (builtin->IsDataPacking()) {
return EmitDataPackingCall(out, expr, builtin);
}
@@ -859,6 +862,24 @@ bool GeneratorImpl::EmitInsertBits(std::ostream& out,
return true;
}
bool GeneratorImpl::EmitEmulatedFMA(std::ostream& out,
const ast::CallExpression* expr) {
out << "((";
if (!EmitExpression(out, expr->args[0])) {
return false;
}
out << ") * (";
if (!EmitExpression(out, expr->args[1])) {
return false;
}
out << ") + (";
if (!EmitExpression(out, expr->args[2])) {
return false;
}
out << "))";
return true;
}
bool GeneratorImpl::EmitSelectCall(std::ostream& out,
const ast::CallExpression* expr) {
auto* expr_false = expr->args[0];
@@ -1619,7 +1640,7 @@ std::string GeneratorImpl::generate_builtin_name(const sem::Builtin* builtin) {
case sem::BuiltinType::kFract:
return "fract";
case sem::BuiltinType::kFma:
return "mad";
return "fma";
case sem::BuiltinType::kFwidth:
case sem::BuiltinType::kFwidthCoarse:
case sem::BuiltinType::kFwidthFine:

View File

@@ -181,6 +181,11 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the call expression
/// @returns true if the expression is emitted
bool EmitInsertBits(std::ostream& out, const ast::CallExpression* expr);
/// Emulates 'fma' on GLSL ES, where it is unsupported.
/// @param out the output of the expression stream
/// @param expr the fma() expression
/// @returns true if the expression is emitted
bool EmitEmulatedFMA(std::ostream& out, const ast::CallExpression* expr);
/// Handles generating a call to a texture function (`textureSample`,
/// `textureSampleGrad`, etc)
/// @param out the output of the expression stream

View File

@@ -213,7 +213,7 @@ INSTANTIATE_TEST_SUITE_P(
BuiltinData{BuiltinType::kExp2, ParamType::kF32, "exp2"},
BuiltinData{BuiltinType::kFaceForward, ParamType::kF32, "faceforward"},
BuiltinData{BuiltinType::kFloor, ParamType::kF32, "floor"},
BuiltinData{BuiltinType::kFma, ParamType::kF32, "mad"},
BuiltinData{BuiltinType::kFma, ParamType::kF32, "fma"},
BuiltinData{BuiltinType::kFract, ParamType::kF32, "fract"},
BuiltinData{BuiltinType::kFwidth, ParamType::kF32, "fwidth"},
BuiltinData{BuiltinType::kFwidthCoarse, ParamType::kF32, "fwidth"},
@@ -873,6 +873,23 @@ void main() {
)");
}
TEST_F(GlslGeneratorImplTest_Builtin, FMA) {
auto* call = Call("fma", "a", "b", "c");
Global("a", ty.vec3<f32>(), ast::StorageClass::kPrivate);
Global("b", ty.vec3<f32>(), ast::StorageClass::kPrivate);
Global("c", ty.vec3<f32>(), ast::StorageClass::kPrivate);
WrapInFunction(CallStmt(call));
GeneratorImpl& gen = Build();
gen.increment_indent();
std::stringstream out;
ASSERT_TRUE(gen.EmitExpression(out, call)) << gen.error();
EXPECT_EQ(out.str(), "((a) * (b) + (c))");
}
TEST_F(GlslGeneratorImplTest_Builtin, DotU32) {
Global("v", ty.vec3<u32>(), ast::StorageClass::kPrivate);
WrapInFunction(CallStmt(Call("dot", "v", "v")));

View File

@@ -212,8 +212,7 @@ TEST_P(GlslImportData_TripleParam_ScalarTest, Float) {
}
INSTANTIATE_TEST_SUITE_P(GlslGeneratorImplTest_Import,
GlslImportData_TripleParam_ScalarTest,
testing::Values(GlslImportData{"fma", "mad"},
GlslImportData{"mix", "mix"},
testing::Values(GlslImportData{"mix", "mix"},
GlslImportData{"clamp", "clamp"},
GlslImportData{"smoothStep",
"smoothstep"}));
@@ -239,7 +238,6 @@ INSTANTIATE_TEST_SUITE_P(
GlslGeneratorImplTest_Import,
GlslImportData_TripleParam_VectorTest,
testing::Values(GlslImportData{"faceForward", "faceforward"},
GlslImportData{"fma", "mad"},
GlslImportData{"clamp", "clamp"},
GlslImportData{"smoothStep", "smoothstep"}));