blob: 94d2ef086bc071a4c2f0b551f9cd2d3f456961c7 [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
John Stiles88a41072020-07-06 09:59:45 -040012static void test(skiatest::Reporter* r, const SkSL::Program::Settings& settings, const char* src,
Ethan Nicholas842d31b2019-01-22 10:59:11 -050013 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
John Stiles88a41072020-07-06 09:59:45 -040035static void test(skiatest::Reporter* r, const GrShaderCaps& caps, const char* src,
Ethan Nicholas842d31b2019-01-22 10:59:11 -050036 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;
John Stiles88a41072020-07-06 09:59:45 -040040 test(r, settings, src, expected, &inputs, kind);
Ethan Nicholas842d31b2019-01-22 10:59:11 -050041}
42
43DEF_TEST(SkSLMetalHelloWorld, r) {
John Stiles88a41072020-07-06 09:59:45 -040044 test(r, *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholase1f55022019-02-05 17:17:40 -050045 "void main() { sk_FragColor = half4(0.75); }",
Ethan Nicholas842d31b2019-01-22 10:59:11 -050046 "#include <metal_stdlib>\n"
47 "#include <simd/simd.h>\n"
48 "using namespace metal;\n"
49 "struct Inputs {\n"
50 "};\n"
51 "struct Outputs {\n"
52 " float4 sk_FragColor [[color(0)]];\n"
53 "};\n"
Jim Van Verth6bc650e2019-02-07 14:53:23 -050054 "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n"
Ethan Nicholas842d31b2019-01-22 10:59:11 -050055 " Outputs _outputStruct;\n"
56 " thread Outputs* _out = &_outputStruct;\n"
57 " _out->sk_FragColor = float4(0.75);\n"
58 " return *_out;\n"
59 "}\n");
60}
61
John Stiles1bdafbf2020-05-28 12:17:20 -040062DEF_TEST(SkSLMetal2x2MatrixCopyFromFloat2x2, r) {
John Stiles88a41072020-07-06 09:59:45 -040063 test(r, *SkSL::ShaderCapsFactory::Default(),
64R"__SkSL__(
John Stiles1bdafbf2020-05-28 12:17:20 -040065void 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__",
John Stiles88a41072020-07-06 09:59:45 -040071R"__MSL__(#include <metal_stdlib>
John Stiles1bdafbf2020-05-28 12:17:20 -040072#include <simd/simd.h>
73using namespace metal;
74struct Inputs {
75};
76struct Outputs {
77 float4 sk_FragColor [[color(0)]];
78};
79fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
80 Outputs _outputStruct;
81 thread Outputs* _out = &_outputStruct;
82 _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]);
83 return *_out;
84}
85)__MSL__");
86}
87
88DEF_TEST(SkSLMetal2x2MatrixCopyFromConstantPropagatedFloat4, r) {
John Stiles88a41072020-07-06 09:59:45 -040089 test(r, *SkSL::ShaderCapsFactory::Default(),
90R"__SkSL__(
John Stiles1bdafbf2020-05-28 12:17:20 -040091void 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__",
John Stiles1bdafbf2020-05-28 12:17:20 -040097R"__MSL__(#include <metal_stdlib>
98#include <simd/simd.h>
99using namespace metal;
100struct Inputs {
101};
102struct Outputs {
103 float4 sk_FragColor [[color(0)]];
104};
105float2x2 float2x2_from_float4(float4 x0) {
106 return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3]));
107}
108fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
109 Outputs _outputStruct;
110 thread Outputs* _out = &_outputStruct;
111 _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]);
112 return *_out;
113}
114)__MSL__");
115}
116
John Stilesfcf8cb22020-08-06 14:29:22 -0400117DEF_TEST(SkSLMetalCastMat2x2ToMat3x3, r) {
118 test(r, *SkSL::ShaderCapsFactory::Default(),
119R"__SkSL__(
120void main() {
121float3x3 a = float3x3(1);
122float3x3 b = float3x3(float2x2(1));
123sk_FragColor.x = (a[0] == b[0]) ? 0 : 1;
124}
125)__SkSL__",
126R"__MSL__(#include <metal_stdlib>
127#include <simd/simd.h>
128using namespace metal;
129struct Inputs {
130};
131struct Outputs {
132 float4 sk_FragColor [[color(0)]];
133};
134float3x3 float3x3_from_float2x2(float2x2 x0) {
135 return float3x3(float3(x0[0].xy, 0.0), float3(x0[1].xy, 0.0), float3(0.0, 0.0, 1.0));
136}
137fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
138 Outputs _outputStruct;
139 thread Outputs* _out = &_outputStruct;
140 _out->sk_FragColor.x = float(all(float3x3(1.0)[0] == float3x3_from_float2x2(float2x2(1.0))[0]) ? 0 : 1);
141 return *_out;
142}
143)__MSL__");
144}
145
146DEF_TEST(SkSLMetalCastMat2x3ToMat4x4, r) {
147 test(r, *SkSL::ShaderCapsFactory::Default(),
148R"__SkSL__(
149void main() {
150float4x4 a = float4x4(6);
151float4x4 b = float4x4(float2x3(7));
152sk_FragColor.x = (a[1] == b[1]) ? 0 : 1;
153}
154)__SkSL__",
155R"__MSL__(#include <metal_stdlib>
156#include <simd/simd.h>
157using namespace metal;
158struct Inputs {
159};
160struct Outputs {
161 float4 sk_FragColor [[color(0)]];
162};
163float4x4 float4x4_from_float2x3(float2x3 x0) {
164 return float4x4(float4(x0[0].xyz, 0.0), float4(x0[1].xyz, 0.0), float4(0.0, 0.0, 1.0, 0.0), float4(0.0, 0.0, 0.0, 1.0));
165}
166fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
167 Outputs _outputStruct;
168 thread Outputs* _out = &_outputStruct;
169 _out->sk_FragColor.x = float(all(float4x4(6.0)[1] == float4x4_from_float2x3(float2x3(7.0))[1]) ? 0 : 1);
170 return *_out;
171}
172)__MSL__");
173}
174
175DEF_TEST(SkSLMetalCastMat4x4ToMat3x4, r) {
176 test(r, *SkSL::ShaderCapsFactory::Default(),
177R"__SkSL__(
178void main() {
179float3x4 a = float3x4(1);
180float3x4 b = float3x4(float4x4(1));
181sk_FragColor.x = (a[0] == b[0]) ? 0 : 1;
182}
183)__SkSL__",
184R"__MSL__(#include <metal_stdlib>
185#include <simd/simd.h>
186using namespace metal;
187struct Inputs {
188};
189struct Outputs {
190 float4 sk_FragColor [[color(0)]];
191};
192float3x4 float3x4_from_float4x4(float4x4 x0) {
193 return float3x4(float4(x0[0].xyzw), float4(x0[1].xyzw), float4(x0[2].xyzw));
194}
195fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
196 Outputs _outputStruct;
197 thread Outputs* _out = &_outputStruct;
198 _out->sk_FragColor.x = float(all(float3x4(1.0)[0] == float3x4_from_float4x4(float4x4(1.0))[0]) ? 0 : 1);
199 return *_out;
200}
201)__MSL__");
202}
203
204DEF_TEST(SkSLMetalCastMat4x4ToMat4x3, r) {
205 test(r, *SkSL::ShaderCapsFactory::Default(),
206R"__SkSL__(
207void main() {
208float4x3 a = float4x3(1);
209float4x3 b = float4x3(float4x4(1));
210sk_FragColor.x = (a[0] == b[0]) ? 0 : 1;
211}
212)__SkSL__",
213R"__MSL__(#include <metal_stdlib>
214#include <simd/simd.h>
215using namespace metal;
216struct Inputs {
217};
218struct Outputs {
219 float4 sk_FragColor [[color(0)]];
220};
221float4x3 float4x3_from_float4x4(float4x4 x0) {
222 return float4x3(float3(x0[0].xyz), float3(x0[1].xyz), float3(x0[2].xyz), float3(x0[3].xyz));
223}
224fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
225 Outputs _outputStruct;
226 thread Outputs* _out = &_outputStruct;
227 _out->sk_FragColor.x = float(all(float4x3(1.0)[0] == float4x3_from_float4x4(float4x4(1.0))[0]) ? 0 : 1);
228 return *_out;
229}
230)__MSL__");
231}
232
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500233DEF_TEST(SkSLMetalMatrices, r) {
John Stiles88a41072020-07-06 09:59:45 -0400234 test(r, *SkSL::ShaderCapsFactory::Default(),
235R"__SkSL__(
John Stiles1bdafbf2020-05-28 12:17:20 -0400236void main() {
237 float2x2 m1 = float2x2(float4(1, 2, 3, 4));
238 float2x2 m2 = float2x2(float4(0));
239 float2x2 m3 = float2x2(m1);
240 float2x2 m4 = float2x2(1);
241 float2x2 m5 = float2x2(m1[0][0]);
242 float2x2 m6 = float2x2(1, 2, 3, 4);
243 float2x2 m7 = float2x2(5, float3(6, 7, 8));
244 float3x2 m8 = float3x2(float2(1, 2), 3, float3(4, 5, 6));
245 float3x3 m9 = float3x3(1);
246 float4x4 m10 = float4x4(1);
247 float4x4 m11 = float4x4(2);
248 sk_FragColor = half4(half(m1[0][0] + m2[0][0] + m3[0][0] + m4[0][0] + m5[0][0] +
249 m6[0][0] + m7[0][0] + m8[0][0] + m9[0][0] + m10[0][0] + m11[0][0]));
250})__SkSL__",
John Stiles1bdafbf2020-05-28 12:17:20 -0400251R"__MSL__(#include <metal_stdlib>
252#include <simd/simd.h>
253using namespace metal;
254struct Inputs {
255};
256struct Outputs {
257 float4 sk_FragColor [[color(0)]];
258};
259float2x2 float2x2_from_float4(float4 x0) {
260 return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3]));
261}
262float2x2 float2x2_from_float_float3(float x0, float3 x1) {
263 return float2x2(float2(x0, x1[0]), float2(x1[1], x1[2]));
264}
265float3x2 float3x2_from_float2_float_float3(float2 x0, float x1, float3 x2) {
266 return float3x2(float2(x0[0], x0[1]), float2(x1, x2[0]), float2(x2[1], x2[2]));
267}
268fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
269 Outputs _outputStruct;
270 thread Outputs* _out = &_outputStruct;
271 float2x2 m5 = float2x2(float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]);
272 _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]);
273 return *_out;
274}
275)__MSL__");
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500276}
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500277
278DEF_TEST(SkSLMetalConstantSwizzle, r) {
John Stiles88a41072020-07-06 09:59:45 -0400279 test(r, *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500280 "void main() {"
281 "sk_FragColor = half4(0.5).rgb1;"
282 "}",
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500283 "#include <metal_stdlib>\n"
284 "#include <simd/simd.h>\n"
285 "using namespace metal;\n"
286 "struct Inputs {\n"
287 "};\n"
288 "struct Outputs {\n"
289 " float4 sk_FragColor [[color(0)]];\n"
290 "};\n"
291 "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n"
292 " Outputs _outputStruct;\n"
293 " thread Outputs* _out = &_outputStruct;\n"
294 " _out->sk_FragColor = float4(float4(0.5).xyz, 1);\n"
295 " return *_out;\n"
296 "}\n");
297}
John Stiles88a41072020-07-06 09:59:45 -0400298
299DEF_TEST(SkSLMetalNumericGlobals, r) {
300 test(r, *SkSL::ShaderCapsFactory::Default(),
301R"__SkSL__(
302half attr1;
303int attr2 = 123;
304float attr3;
305half4 attr4 = half4(4, 5, 6, 7);
306void main()
307{
308 sk_FragColor = half4(attr1, attr2, half(attr3), attr4.x);
309}
310)__SkSL__",
311R"__MSL__(#include <metal_stdlib>
312#include <simd/simd.h>
313using namespace metal;
314struct Inputs {
315};
316struct Outputs {
317 float4 sk_FragColor [[color(0)]];
318};
319struct Globals {
320 float attr1;
321 int attr2;
322 float attr3;
323 float4 attr4;
324};
325
326
327
328fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
329 Globals globalStruct{{}, 123, {}, float4(4.0, 5.0, 6.0, 7.0)};
330 thread Globals* _globals = &globalStruct;
331 (void)_globals;
332 Outputs _outputStruct;
333 thread Outputs* _out = &_outputStruct;
334 _out->sk_FragColor = float4(_globals->attr1, float(_globals->attr2), _globals->attr3, _globals->attr4.x);
335 return *_out;
336}
337)__MSL__");
338}
339
340DEF_TEST(SkSLMetalSamplerGlobals, r) {
341 test(r, *SkSL::ShaderCapsFactory::Default(),
342R"__SkSL__(
343layout(binding=1) uniform sampler2D texA;
344layout(binding=0) uniform sampler2D texB;
345void main()
346{
347 sk_FragColor = sample(texA, half2(0)) * sample(texB, half2(0));
348}
349)__SkSL__",
350R"__MSL__(#include <metal_stdlib>
351#include <simd/simd.h>
352using namespace metal;
353struct Inputs {
354};
355struct Outputs {
356 float4 sk_FragColor [[color(0)]];
357};
358struct Globals {
359 texture2d<float> texA;
360 sampler texASmplr;
361 texture2d<float> texB;
362 sampler texBSmplr;
363};
364
365fragment Outputs fragmentMain(Inputs _in [[stage_in]], texture2d<float> texA[[texture(1)]], sampler texASmplr[[sampler(1)]], texture2d<float> texB[[texture(0)]], sampler texBSmplr[[sampler(0)]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
366 Globals globalStruct{texA, texASmplr, texB, texBSmplr};
367 thread Globals* _globals = &globalStruct;
368 (void)_globals;
369 Outputs _outputStruct;
370 thread Outputs* _out = &_outputStruct;
371 _out->sk_FragColor = _globals->texA.sample(_globals->texASmplr, float2(0.0)) * _globals->texB.sample(_globals->texBSmplr, float2(0.0));
372 return *_out;
373}
374)__MSL__");
375}