skia2/tests/sksl/intrinsics/MatrixCompMult.metal

75 lines
2.9 KiB
Metal
Raw Normal View History

#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct Uniforms {
float4 colorGreen;
float4 colorRed;
};
struct Inputs {
};
struct Outputs {
float4 sk_FragColor [[color(0)]];
};
struct Globals {
float3x3 a;
float3x3 b;
float4x4 c;
float4x4 d;
};
Reland "Fix array-of-matrix/struct comparisons in Metal." This is a reland of 23d8f94535813d7c973af69aaeb67a0e5d7c9140 Original change's description: > Fix array-of-matrix/struct comparisons in Metal. > > Metal needs helper functions in order to compare arrays, structs, and > matrices. Depending on the input code, it was possible for the > array-comparison helper to be emitted before a matrix-comparison > or struct-comparison helper. If this occurred, array comparisons of that > matrix or struct type would fail, because the operator== for the array's > inner type was defined after array==, and Metal (like C++) parses > top-to-bottom and only considers functions declared above the current > function. > > We now emit prototypes for all the array, struct and matrix helper > function. These prototypes are emitted above any helper functions. This > ensures visibility no matter how your comparisons are organized. > > Change-Id: Ib3d8828c301fd0fa6c209788f9ea60800371edbe > Bug: skia:12326 > Reviewed-on: https://skia-review.googlesource.com/c/skia/+/437739 > Commit-Queue: John Stiles <johnstiles@google.com> > Auto-Submit: John Stiles <johnstiles@google.com> > Reviewed-by: Brian Osman <brianosman@google.com> Bug: skia:12326 Change-Id: Ife68020f6b01fae973b97f76099c6d5e8215636c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/438296 Reviewed-by: John Stiles <johnstiles@google.com> Commit-Queue: John Stiles <johnstiles@google.com> Auto-Submit: John Stiles <johnstiles@google.com>
2021-08-10 20:03:23 +00:00
thread bool operator==(const float2x2 left, const float2x2 right);
thread bool operator!=(const float2x2 left, const float2x2 right);
thread bool operator==(const float4x4 left, const float4x4 right);
thread bool operator!=(const float4x4 left, const float4x4 right);
thread bool operator==(const float4x3 left, const float4x3 right);
thread bool operator!=(const float4x3 left, const float4x3 right);
template <int C, int R>
matrix<float, C, R> matrixCompMult(matrix<float, C, R> a, matrix<float, C, R> b) {
matrix<float, C, R> result;
for (int c = 0; c < C; ++c) {
result[c] = a[c] * b[c];
}
return result;
}
thread bool operator==(const float2x2 left, const float2x2 right) {
return all(left[0] == right[0]) &&
all(left[1] == right[1]);
}
thread bool operator!=(const float2x2 left, const float2x2 right) {
return !(left == right);
}
thread bool operator==(const float4x4 left, const float4x4 right) {
return all(left[0] == right[0]) &&
all(left[1] == right[1]) &&
all(left[2] == right[2]) &&
all(left[3] == right[3]);
}
thread bool operator!=(const float4x4 left, const float4x4 right) {
return !(left == right);
}
thread bool operator==(const float4x3 left, const float4x3 right) {
return all(left[0] == right[0]) &&
all(left[1] == right[1]) &&
all(left[2] == right[2]) &&
all(left[3] == right[3]);
}
thread bool operator!=(const float4x3 left, const float4x3 right) {
return !(left == right);
}
fragment Outputs fragmentMain(Inputs _in [[stage_in]], constant Uniforms& _uniforms [[buffer(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
Globals _globals{{}, {}, {}, {}};
(void)_globals;
Outputs _out;
(void)_out;
float2x2 h22 = float2x2(float2(0.0, 5.0), float2(10.0, 15.0));
float4x4 h44 = float4x4(float4(0.5, 0.0, 0.0, 0.0), float4(0.0, 3.0, 0.0, 0.0), float4(0.0, 0.0, 5.5, 0.0), float4(0.0, 0.0, 0.0, 8.0));
float4x3 f43 = float4x3(float3(12.0, 22.0, 30.0), float3(36.0, 40.0, 42.0), float3(42.0, 40.0, 36.0), float3(30.0, 22.0, 12.0));
_out.sk_FragColor.xyz = matrixCompMult(_globals.a, _globals.b)[0];
_out.sk_FragColor = matrixCompMult(_globals.c, _globals.d)[0];
_out.sk_FragColor = (h22 == float2x2(float2(0.0, 5.0), float2(10.0, 15.0)) && h44 == float4x4(float4(0.5, 0.0, 0.0, 0.0), float4(0.0, 3.0, 0.0, 0.0), float4(0.0, 0.0, 5.5, 0.0), float4(0.0, 0.0, 0.0, 8.0))) && f43 == float4x3(float3(12.0, 22.0, 30.0), float3(36.0, 40.0, 42.0), float3(42.0, 40.0, 36.0), float3(30.0, 22.0, 12.0)) ? _uniforms.colorGreen : _uniforms.colorRed;
return _out;
}