skia2/tests/sksl/metal/golden/SwizzleHelper.metal
John Stiles 06b84efcb3 Improve Metal support for out parameters.
We now insert helper functions which defer the assignment of out-
parameters back into their original variables to the end of the
function call. This allows us to match the semantics listed the GLSL
spec in section 6.1.1:

"All arguments are evaluated at call time, exactly once, in order, from
left to right. [...] Evaluation of an out parameter results in an
l-value that is used to copy out a value when the function returns.
Evaluation of an inout parameter results in both a value and an l-value;
the value is copied to the formal parameter at call time and the lvalue
is used to copy out a value when the function returns."

This technique also allows us to support swizzled out-parameters in
Metal, by reading the swizzle into a temp variable, calling the original
function, and then re-assigning the result back into the original
swizzle expression.

At present, we don't deduplicate these helper functions, so in theory
there could be a fair amount of redundant code generated if a function
with out parameters is called many times in a row. The cost of properly
deduplicating them is probably larger than the benefit in the 99% case.

Change-Id: Iefc922ac9e2b24ef2ff1e9dacb17a735a75ec8ea
Bug: skia:10855, skia:11052
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/341162
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Auto-Submit: John Stiles <johnstiles@google.com>
2020-12-09 21:13:57 +00:00

42 lines
1.4 KiB
Metal

#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct Inputs {
};
struct Outputs {
float4 sk_FragColor [[color(0)]];
};
struct Globals {
float2 glob;
};
float4 fn(thread Outputs* _out, thread Globals* _globals, float a, thread float2& b, thread float2& c, thread float3& d);
float4 _skOutParamHelper0_fn(thread Outputs* _out, thread Globals* _globals, float _var0, thread float3& b, thread float2& glob, thread float3x3& d) {
float2 _var1;
float2 _var2 = glob.yx;
float3 _var3 = d[1].zyx;
float4 _skResult = fn(_out, _globals, _var0, _var1, _var2, _var3);
b.yz = _var1;
glob.yx = _var2;
d[1].zyx = _var3;
return _skResult;
}
float4 fn(thread Outputs* _out, thread Globals* _globals, float a, thread float2& b, thread float2& c, thread float3& d) {
a = _out->sk_FragColor.x + a;
b = _out->sk_FragColor.yz - _globals->glob.y;
c *= a;
d = _out->sk_FragColor.www / d;
return float4(a, b.x, c.y, d.x);
}
fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Globals globalStruct{float2(1.0)};
thread Globals* _globals = &globalStruct;
(void)_globals;
Outputs _outputStruct;
thread Outputs* _out = &_outputStruct;
float3 b = float3(2.0);
float3x3 d = float3x3(4.0);
_out->sk_FragColor = _skOutParamHelper0_fn(_out, _globals, 1.0, b, _globals->glob, d);
return *_out;
}