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

View File

@ -181,6 +181,11 @@ class GeneratorImpl : public TextGenerator {
/// @param expr the call expression /// @param expr the call expression
/// @returns true if the expression is emitted /// @returns true if the expression is emitted
bool EmitInsertBits(std::ostream& out, const ast::CallExpression* expr); 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`, /// Handles generating a call to a texture function (`textureSample`,
/// `textureSampleGrad`, etc) /// `textureSampleGrad`, etc)
/// @param out the output of the expression stream /// @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::kExp2, ParamType::kF32, "exp2"},
BuiltinData{BuiltinType::kFaceForward, ParamType::kF32, "faceforward"}, BuiltinData{BuiltinType::kFaceForward, ParamType::kF32, "faceforward"},
BuiltinData{BuiltinType::kFloor, ParamType::kF32, "floor"}, 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::kFract, ParamType::kF32, "fract"},
BuiltinData{BuiltinType::kFwidth, ParamType::kF32, "fwidth"}, BuiltinData{BuiltinType::kFwidth, ParamType::kF32, "fwidth"},
BuiltinData{BuiltinType::kFwidthCoarse, 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) { TEST_F(GlslGeneratorImplTest_Builtin, DotU32) {
Global("v", ty.vec3<u32>(), ast::StorageClass::kPrivate); Global("v", ty.vec3<u32>(), ast::StorageClass::kPrivate);
WrapInFunction(CallStmt(Call("dot", "v", "v"))); WrapInFunction(CallStmt(Call("dot", "v", "v")));

View File

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

View File

@ -1,5 +1,3 @@
SKIP: FAILED
benchmark/skinned-shadowed-pbr-fragment.wgsl:51:13 warning: use of deprecated language feature: the @stride attribute is deprecated; use a larger type if necessary benchmark/skinned-shadowed-pbr-fragment.wgsl:51:13 warning: use of deprecated language feature: the @stride attribute is deprecated; use a larger type if necessary
lights : @stride(32) array<Light>; lights : @stride(32) array<Light>;
^^^^^^ ^^^^^^
@ -79,7 +77,7 @@ layout(binding = 2, std430) buffer GlobalLights_1 {
} globalLights; } globalLights;
const uvec3 tileCount = uvec3(32u, 18u, 48u); const uvec3 tileCount = uvec3(32u, 18u, 48u);
float linearDepth(float depthSample) { float linearDepth(float depthSample) {
return ((camera.zFar * camera.zNear) / mad(depthSample, (camera.zNear - camera.zFar), camera.zFar)); return ((camera.zFar * camera.zNear) / ((depthSample) * ((camera.zNear - camera.zFar)) + (camera.zFar)));
} }
uvec3 getTile(vec4 fragCoord) { uvec3 getTile(vec4 fragCoord) {
@ -370,10 +368,3 @@ void main() {
emissive_1 = inner_result.emissive; emissive_1 = inner_result.emissive;
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:76: 'mad' : no matching overloaded function found
ERROR: 0:76: '' : compilation terminated
ERROR: 2 compilation errors. No code generated.

View File

@ -1,9 +1,7 @@
SKIP: FAILED
#version 310 es #version 310 es
void fma_26a7a9() { void fma_26a7a9() {
vec2 res = mad(vec2(0.0f, 0.0f), vec2(0.0f, 0.0f), vec2(0.0f, 0.0f)); vec2 res = ((vec2(0.0f, 0.0f)) * (vec2(0.0f, 0.0f)) + (vec2(0.0f, 0.0f)));
} }
vec4 vertex_main() { vec4 vertex_main() {
@ -18,19 +16,11 @@ void main() {
gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w); gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '=' : cannot convert from ' const float' to ' temp highp 2-component vector of float'
ERROR: 0:4: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
#version 310 es #version 310 es
precision mediump float; precision mediump float;
void fma_26a7a9() { void fma_26a7a9() {
vec2 res = mad(vec2(0.0f, 0.0f), vec2(0.0f, 0.0f), vec2(0.0f, 0.0f)); vec2 res = ((vec2(0.0f, 0.0f)) * (vec2(0.0f, 0.0f)) + (vec2(0.0f, 0.0f)));
} }
void fragment_main() { void fragment_main() {
@ -41,18 +31,10 @@ void main() {
fragment_main(); fragment_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:5: 'mad' : no matching overloaded function found
ERROR: 0:5: '=' : cannot convert from ' const float' to ' temp mediump 2-component vector of float'
ERROR: 0:5: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
#version 310 es #version 310 es
void fma_26a7a9() { void fma_26a7a9() {
vec2 res = mad(vec2(0.0f, 0.0f), vec2(0.0f, 0.0f), vec2(0.0f, 0.0f)); vec2 res = ((vec2(0.0f, 0.0f)) * (vec2(0.0f, 0.0f)) + (vec2(0.0f, 0.0f)));
} }
void compute_main() { void compute_main() {
@ -64,11 +46,3 @@ void main() {
compute_main(); compute_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '=' : cannot convert from ' const float' to ' temp highp 2-component vector of float'
ERROR: 0:4: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.

View File

@ -1,9 +1,7 @@
SKIP: FAILED
#version 310 es #version 310 es
void fma_6a3283() { void fma_6a3283() {
vec4 res = mad(vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 0.0f, 0.0f)); vec4 res = ((vec4(0.0f, 0.0f, 0.0f, 0.0f)) * (vec4(0.0f, 0.0f, 0.0f, 0.0f)) + (vec4(0.0f, 0.0f, 0.0f, 0.0f)));
} }
vec4 vertex_main() { vec4 vertex_main() {
@ -18,19 +16,11 @@ void main() {
gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w); gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '=' : cannot convert from ' const float' to ' temp highp 4-component vector of float'
ERROR: 0:4: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
#version 310 es #version 310 es
precision mediump float; precision mediump float;
void fma_6a3283() { void fma_6a3283() {
vec4 res = mad(vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 0.0f, 0.0f)); vec4 res = ((vec4(0.0f, 0.0f, 0.0f, 0.0f)) * (vec4(0.0f, 0.0f, 0.0f, 0.0f)) + (vec4(0.0f, 0.0f, 0.0f, 0.0f)));
} }
void fragment_main() { void fragment_main() {
@ -41,18 +31,10 @@ void main() {
fragment_main(); fragment_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:5: 'mad' : no matching overloaded function found
ERROR: 0:5: '=' : cannot convert from ' const float' to ' temp mediump 4-component vector of float'
ERROR: 0:5: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
#version 310 es #version 310 es
void fma_6a3283() { void fma_6a3283() {
vec4 res = mad(vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 0.0f, 0.0f), vec4(0.0f, 0.0f, 0.0f, 0.0f)); vec4 res = ((vec4(0.0f, 0.0f, 0.0f, 0.0f)) * (vec4(0.0f, 0.0f, 0.0f, 0.0f)) + (vec4(0.0f, 0.0f, 0.0f, 0.0f)));
} }
void compute_main() { void compute_main() {
@ -64,11 +46,3 @@ void main() {
compute_main(); compute_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '=' : cannot convert from ' const float' to ' temp highp 4-component vector of float'
ERROR: 0:4: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.

View File

@ -1,9 +1,7 @@
SKIP: FAILED
#version 310 es #version 310 es
void fma_c10ba3() { void fma_c10ba3() {
float res = mad(1.0f, 1.0f, 1.0f); float res = ((1.0f) * (1.0f) + (1.0f));
} }
vec4 vertex_main() { vec4 vertex_main() {
@ -18,18 +16,11 @@ void main() {
gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w); gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '' : compilation terminated
ERROR: 2 compilation errors. No code generated.
#version 310 es #version 310 es
precision mediump float; precision mediump float;
void fma_c10ba3() { void fma_c10ba3() {
float res = mad(1.0f, 1.0f, 1.0f); float res = ((1.0f) * (1.0f) + (1.0f));
} }
void fragment_main() { void fragment_main() {
@ -40,17 +31,10 @@ void main() {
fragment_main(); fragment_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:5: 'mad' : no matching overloaded function found
ERROR: 0:5: '' : compilation terminated
ERROR: 2 compilation errors. No code generated.
#version 310 es #version 310 es
void fma_c10ba3() { void fma_c10ba3() {
float res = mad(1.0f, 1.0f, 1.0f); float res = ((1.0f) * (1.0f) + (1.0f));
} }
void compute_main() { void compute_main() {
@ -62,10 +46,3 @@ void main() {
compute_main(); compute_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '' : compilation terminated
ERROR: 2 compilation errors. No code generated.

View File

@ -1,9 +1,7 @@
SKIP: FAILED
#version 310 es #version 310 es
void fma_e17c5c() { void fma_e17c5c() {
vec3 res = mad(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f)); vec3 res = ((vec3(0.0f, 0.0f, 0.0f)) * (vec3(0.0f, 0.0f, 0.0f)) + (vec3(0.0f, 0.0f, 0.0f)));
} }
vec4 vertex_main() { vec4 vertex_main() {
@ -18,19 +16,11 @@ void main() {
gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w); gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '=' : cannot convert from ' const float' to ' temp highp 3-component vector of float'
ERROR: 0:4: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
#version 310 es #version 310 es
precision mediump float; precision mediump float;
void fma_e17c5c() { void fma_e17c5c() {
vec3 res = mad(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f)); vec3 res = ((vec3(0.0f, 0.0f, 0.0f)) * (vec3(0.0f, 0.0f, 0.0f)) + (vec3(0.0f, 0.0f, 0.0f)));
} }
void fragment_main() { void fragment_main() {
@ -41,18 +31,10 @@ void main() {
fragment_main(); fragment_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:5: 'mad' : no matching overloaded function found
ERROR: 0:5: '=' : cannot convert from ' const float' to ' temp mediump 3-component vector of float'
ERROR: 0:5: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.
#version 310 es #version 310 es
void fma_e17c5c() { void fma_e17c5c() {
vec3 res = mad(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 0.0f)); vec3 res = ((vec3(0.0f, 0.0f, 0.0f)) * (vec3(0.0f, 0.0f, 0.0f)) + (vec3(0.0f, 0.0f, 0.0f)));
} }
void compute_main() { void compute_main() {
@ -64,11 +46,3 @@ void main() {
compute_main(); compute_main();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:4: 'mad' : no matching overloaded function found
ERROR: 0:4: '=' : cannot convert from ' const float' to ' temp highp 3-component vector of float'
ERROR: 0:4: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.

View File

@ -1,6 +1,6 @@
SKIP: FAILED SKIP: FAILED
../../src/tint/writer/glsl/generator_impl.cc:2576 internal compiler error: Multiplanar external texture transform was not run. ../../src/tint/writer/glsl/generator_impl.cc:2589 internal compiler error: Multiplanar external texture transform was not run.
******************************************************************** ********************************************************************

View File

@ -1,6 +1,6 @@
SKIP: FAILED SKIP: FAILED
../../src/tint/writer/glsl/generator_impl.cc:2576 internal compiler error: Multiplanar external texture transform was not run. ../../src/tint/writer/glsl/generator_impl.cc:2589 internal compiler error: Multiplanar external texture transform was not run.
******************************************************************** ********************************************************************

View File

@ -1,6 +1,6 @@
SKIP: FAILED SKIP: FAILED
../../src/tint/writer/glsl/generator_impl.cc:2576 internal compiler error: Multiplanar external texture transform was not run. ../../src/tint/writer/glsl/generator_impl.cc:2589 internal compiler error: Multiplanar external texture transform was not run.
******************************************************************** ********************************************************************

View File

@ -1,5 +1,3 @@
SKIP: FAILED
#version 310 es #version 310 es
void main_1() { void main_1() {
@ -25,7 +23,7 @@ void main_1() {
vec3 v3f2 = vec3(60.0f, 70.0f, 50.0f); vec3 v3f2 = vec3(60.0f, 70.0f, 50.0f);
vec4 v4f1 = vec4(50.0f, 50.0f, 50.0f, 50.0f); vec4 v4f1 = vec4(50.0f, 50.0f, 50.0f, 50.0f);
vec4 v4f2 = v4f1; vec4 v4f2 = v4f1;
float x_1 = mad(f1, f2, f3); float x_1 = ((f1) * (f2) + (f3));
return; return;
} }
@ -38,10 +36,3 @@ void main() {
tint_symbol(); tint_symbol();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:26: 'mad' : no matching overloaded function found
ERROR: 0:26: '' : compilation terminated
ERROR: 2 compilation errors. No code generated.

View File

@ -1,5 +1,3 @@
SKIP: FAILED
#version 310 es #version 310 es
void main_1() { void main_1() {
@ -25,7 +23,7 @@ void main_1() {
vec3 v3f2 = vec3(60.0f, 70.0f, 50.0f); vec3 v3f2 = vec3(60.0f, 70.0f, 50.0f);
vec4 v4f1 = vec4(50.0f, 50.0f, 50.0f, 50.0f); vec4 v4f1 = vec4(50.0f, 50.0f, 50.0f, 50.0f);
vec4 v4f2 = v4f1; vec4 v4f2 = v4f1;
vec2 x_1 = mad(v2f1, v2f2, v2f3); vec2 x_1 = ((v2f1) * (v2f2) + (v2f3));
return; return;
} }
@ -38,11 +36,3 @@ void main() {
tint_symbol(); tint_symbol();
return; return;
} }
Error parsing GLSL shader:
ERROR: 0:26: 'mad' : no matching overloaded function found
ERROR: 0:26: '=' : cannot convert from ' const float' to ' temp highp 2-component vector of float'
ERROR: 0:26: '' : compilation terminated
ERROR: 3 compilation errors. No code generated.