blob: a8640bad7f4bba47351ed46abf0e7c50b1e10aba [file] [log] [blame]
Ethan Nicholas842d31b2019-01-22 10:59:11 -05001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
Ethan Nicholas842d31b2019-01-22 10:59:11 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
Ethan Nicholas842d31b2019-01-22 10:59:11 -050011
12static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings,
13 const char* expected, SkSL::Program::Inputs* inputs,
14 SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
15 SkSL::Compiler compiler;
16 SkSL::String output;
17 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src),
18 settings);
19 if (!program) {
20 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
21 }
22 REPORTER_ASSERT(r, program);
23 *inputs = program->fInputs;
24 REPORTER_ASSERT(r, compiler.toMetal(*program, &output));
25 if (program) {
26 SkSL::String skExpected(expected);
27 if (output != skExpected) {
28 SkDebugf("MSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
29 expected, output.c_str());
30 }
31 REPORTER_ASSERT(r, output == skExpected);
32 }
33}
34
35static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
36 const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
37 SkSL::Program::Settings settings;
38 settings.fCaps = &caps;
39 SkSL::Program::Inputs inputs;
40 test(r, src, settings, expected, &inputs, kind);
41}
42
43DEF_TEST(SkSLMetalHelloWorld, r) {
44 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -050045 "void main() { sk_FragColor = half4(0.75); }",
Ethan Nicholas842d31b2019-01-22 10:59:11 -050046 *SkSL::ShaderCapsFactory::Default(),
47 "#include <metal_stdlib>\n"
48 "#include <simd/simd.h>\n"
49 "using namespace metal;\n"
50 "struct Inputs {\n"
51 "};\n"
52 "struct Outputs {\n"
53 " float4 sk_FragColor [[color(0)]];\n"
54 "};\n"
Jim Van Verth6bc650e2019-02-07 14:53:23 -050055 "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n"
Ethan Nicholas842d31b2019-01-22 10:59:11 -050056 " Outputs _outputStruct;\n"
57 " thread Outputs* _out = &_outputStruct;\n"
58 " _out->sk_FragColor = float4(0.75);\n"
59 " return *_out;\n"
60 "}\n");
61}
62
John Stiles1bdafbf2020-05-28 12:17:20 -040063DEF_TEST(SkSLMetal2x2MatrixCopyFromFloat2x2, r) {
64 test(r, R"__SkSL__(
65void main() {
66 float2x2 m1 = float2x2(float2(1, 2), float2(3, 4));
67 float2x2 m2 = m1;
68 float2x2 m3 = float2x2(m1);
69 sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0]));
70})__SkSL__",
71 *SkSL::ShaderCapsFactory::Default(),
72 R"__MSL__(#include <metal_stdlib>
73#include <simd/simd.h>
74using namespace metal;
75struct Inputs {
76};
77struct Outputs {
78 float4 sk_FragColor [[color(0)]];
79};
80fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
81 Outputs _outputStruct;
82 thread Outputs* _out = &_outputStruct;
83 _out->sk_FragColor = float4((float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0] + float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0]) + float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0]);
84 return *_out;
85}
86)__MSL__");
87}
88
89DEF_TEST(SkSLMetal2x2MatrixCopyFromConstantPropagatedFloat4, r) {
90 test(r, R"__SkSL__(
91void main() {
92 float2x2 m1 = float2x2(float4(1, 2, 3, 4));
93 float2x2 m2 = m1;
94 float2x2 m3 = float2x2(m1);
95 sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0]));
96})__SkSL__",
97 *SkSL::ShaderCapsFactory::Default(),
98R"__MSL__(#include <metal_stdlib>
99#include <simd/simd.h>
100using namespace metal;
101struct Inputs {
102};
103struct Outputs {
104 float4 sk_FragColor [[color(0)]];
105};
106float2x2 float2x2_from_float4(float4 x0) {
107 return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3]));
108}
109fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
110 Outputs _outputStruct;
111 thread Outputs* _out = &_outputStruct;
112 _out->sk_FragColor = float4((float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0] + float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]) + float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]);
113 return *_out;
114}
115)__MSL__");
116}
117
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500118DEF_TEST(SkSLMetalMatrices, r) {
John Stiles1bdafbf2020-05-28 12:17:20 -0400119 test(r, R"__SkSL__(
120void main() {
121 float2x2 m1 = float2x2(float4(1, 2, 3, 4));
122 float2x2 m2 = float2x2(float4(0));
123 float2x2 m3 = float2x2(m1);
124 float2x2 m4 = float2x2(1);
125 float2x2 m5 = float2x2(m1[0][0]);
126 float2x2 m6 = float2x2(1, 2, 3, 4);
127 float2x2 m7 = float2x2(5, float3(6, 7, 8));
128 float3x2 m8 = float3x2(float2(1, 2), 3, float3(4, 5, 6));
129 float3x3 m9 = float3x3(1);
130 float4x4 m10 = float4x4(1);
131 float4x4 m11 = float4x4(2);
132 sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0] + m4[0][0] + m5[0][0] +
133 m6[0][0] + m7[0][0] + m8[0][0] + m9[0][0] + m10[0][0] + m11[0][0]));
134})__SkSL__",
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500135 *SkSL::ShaderCapsFactory::Default(),
John Stiles1bdafbf2020-05-28 12:17:20 -0400136R"__MSL__(#include <metal_stdlib>
137#include <simd/simd.h>
138using namespace metal;
139struct Inputs {
140};
141struct Outputs {
142 float4 sk_FragColor [[color(0)]];
143};
144float2x2 float2x2_from_float4(float4 x0) {
145 return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3]));
146}
147float2x2 float2x2_from_float_float3(float x0, float3 x1) {
148 return float2x2(float2(x0, x1[0]), float2(x1[1], x1[2]));
149}
150float3x2 float3x2_from_float2_float_float3(float2 x0, float x1, float3 x2) {
151 return float3x2(float2(x0[0], x0[1]), float2(x1, x2[0]), float2(x2[1], x2[2]));
152}
153fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
154 Outputs _outputStruct;
155 thread Outputs* _out = &_outputStruct;
156 float2x2 m5 = float2x2(float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]);
157 _out->sk_FragColor = float4((((((((((float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0] + float2x2_from_float4(float4(0.0))[0][0]) + float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]) + float2x2(1.0)[0][0]) + m5[0][0]) + float2x2(float2(1.0, 2.0), float2(3.0, 4.0))[0][0]) + float2x2_from_float_float3(5.0, float3(6.0, 7.0, 8.0))[0][0]) + float3x2_from_float2_float_float3(float2(1.0, 2.0), 3.0, float3(4.0, 5.0, 6.0))[0][0]) + float3x3(1.0)[0][0]) + float4x4(1.0)[0][0]) + float4x4(2.0)[0][0]);
158 return *_out;
159}
160)__MSL__");
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500161}
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500162
163DEF_TEST(SkSLMetalConstantSwizzle, r) {
164 test(r,
165 "void main() {"
166 "sk_FragColor = half4(0.5).rgb1;"
167 "}",
168 *SkSL::ShaderCapsFactory::Default(),
169 "#include <metal_stdlib>\n"
170 "#include <simd/simd.h>\n"
171 "using namespace metal;\n"
172 "struct Inputs {\n"
173 "};\n"
174 "struct Outputs {\n"
175 " float4 sk_FragColor [[color(0)]];\n"
176 "};\n"
177 "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n"
178 " Outputs _outputStruct;\n"
179 " thread Outputs* _out = &_outputStruct;\n"
180 " _out->sk_FragColor = float4(float4(0.5).xyz, 1);\n"
181 " return *_out;\n"
182 "}\n");
183}