From b6981fb6a4c1f6c2dd044d19782ab6ed428b2e56 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Wed, 8 Sep 2021 15:11:27 -0400 Subject: [PATCH] Add float-packing intrinsics from sksl_gpu to Metal. It turned out that Metal had equivalent intrinsics/casts all along; we just needed to emit them. Tests will be improved in a followup CL which adds the ES3-compatible packing intrinsics into sksl_public. Change-Id: Iec8a20b9f9fe9b1badea2944eb0b1f0a17c74560 Bug: skia:12351 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/446744 Commit-Queue: John Stiles Commit-Queue: Brian Osman Auto-Submit: John Stiles Reviewed-by: Brian Osman --- src/sksl/codegen/SkSLMetalCodeGenerator.cpp | 61 ++++++++++++++++++++- tests/sksl/intrinsics/Pack.metal | 10 ++-- tests/sksl/intrinsics/Unpack.metal | 10 ++-- 3 files changed, 70 insertions(+), 11 deletions(-) diff --git a/src/sksl/codegen/SkSLMetalCodeGenerator.cpp b/src/sksl/codegen/SkSLMetalCodeGenerator.cpp index bd5b2d0cda..22a24a72ab 100644 --- a/src/sksl/codegen/SkSLMetalCodeGenerator.cpp +++ b/src/sksl/codegen/SkSLMetalCodeGenerator.cpp @@ -595,7 +595,66 @@ bool MetalCodeGenerator::writeIntrinsicCall(const FunctionCall& c, IntrinsicKind this->write(")"); return true; } - + case k_packUnorm2x16_IntrinsicKind: { + this->write("pack_float_to_unorm2x16("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_unpackUnorm2x16_IntrinsicKind: { + this->write("unpack_unorm2x16_to_float("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_packSnorm2x16_IntrinsicKind: { + this->write("pack_float_to_snorm2x16("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_unpackSnorm2x16_IntrinsicKind: { + this->write("unpack_snorm2x16_to_float("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_packUnorm4x8_IntrinsicKind: { + this->write("pack_float_to_unorm4x8("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_unpackUnorm4x8_IntrinsicKind: { + this->write("unpack_unorm4x8_to_float("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_packSnorm4x8_IntrinsicKind: { + this->write("pack_float_to_snorm4x8("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_unpackSnorm4x8_IntrinsicKind: { + this->write("unpack_snorm4x8_to_float("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write(")"); + return true; + } + case k_packHalf2x16_IntrinsicKind: { + this->write("as_type(half2("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write("))"); + return true; + } + case k_unpackHalf2x16_IntrinsicKind: { + this->write("float2(as_type("); + this->writeExpression(*arguments[0], Precedence::kSequence); + this->write("))"); + return true; + } case k_floatBitsToInt_IntrinsicKind: case k_floatBitsToUint_IntrinsicKind: case k_intBitsToFloat_IntrinsicKind: diff --git a/tests/sksl/intrinsics/Pack.metal b/tests/sksl/intrinsics/Pack.metal index 8bd83c8717..0e2e10cbff 100644 --- a/tests/sksl/intrinsics/Pack.metal +++ b/tests/sksl/intrinsics/Pack.metal @@ -13,10 +13,10 @@ struct Outputs { fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { Outputs _out; (void)_out; - _out.sk_FragColor.x = float(packHalf2x16(_uniforms.a)); - _out.sk_FragColor.x = float(packUnorm2x16(_uniforms.a)); - _out.sk_FragColor.x = float(packSnorm2x16(_uniforms.a)); - _out.sk_FragColor.x = float(packUnorm4x8(_uniforms.b)); - _out.sk_FragColor.x = float(packSnorm4x8(_uniforms.b)); + _out.sk_FragColor.x = float(as_type(half2(_uniforms.a))); + _out.sk_FragColor.x = float(pack_float_to_unorm2x16(_uniforms.a)); + _out.sk_FragColor.x = float(pack_float_to_snorm2x16(_uniforms.a)); + _out.sk_FragColor.x = float(pack_float_to_unorm4x8(_uniforms.b)); + _out.sk_FragColor.x = float(pack_float_to_snorm4x8(_uniforms.b)); return _out; } diff --git a/tests/sksl/intrinsics/Unpack.metal b/tests/sksl/intrinsics/Unpack.metal index 811701a807..a04d3f0027 100644 --- a/tests/sksl/intrinsics/Unpack.metal +++ b/tests/sksl/intrinsics/Unpack.metal @@ -12,10 +12,10 @@ struct Outputs { fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) { Outputs _out; (void)_out; - _out.sk_FragColor.xy = unpackHalf2x16(_uniforms.a); - _out.sk_FragColor.xy = unpackUnorm2x16(_uniforms.a); - _out.sk_FragColor.xy = unpackSnorm2x16(_uniforms.a); - _out.sk_FragColor = unpackUnorm4x8(_uniforms.a); - _out.sk_FragColor = unpackSnorm4x8(_uniforms.a); + _out.sk_FragColor.xy = float2(as_type(_uniforms.a)); + _out.sk_FragColor.xy = unpack_unorm2x16_to_float(_uniforms.a); + _out.sk_FragColor.xy = unpack_snorm2x16_to_float(_uniforms.a); + _out.sk_FragColor = unpack_unorm4x8_to_float(_uniforms.a); + _out.sk_FragColor = unpack_snorm4x8_to_float(_uniforms.a); return _out; }