blob: 41c440fca22d5df61799bfaed405dfd69702b1f7 [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
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500117DEF_TEST(SkSLMetalMatrices, r) {
John Stiles88a41072020-07-06 09:59:45 -0400118 test(r, *SkSL::ShaderCapsFactory::Default(),
119R"__SkSL__(
John Stiles1bdafbf2020-05-28 12:17:20 -0400120void 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__",
John Stiles1bdafbf2020-05-28 12:17:20 -0400135R"__MSL__(#include <metal_stdlib>
136#include <simd/simd.h>
137using namespace metal;
138struct Inputs {
139};
140struct Outputs {
141 float4 sk_FragColor [[color(0)]];
142};
143float2x2 float2x2_from_float4(float4 x0) {
144 return float2x2(float2(x0[0], x0[1]), float2(x0[2], x0[3]));
145}
146float2x2 float2x2_from_float_float3(float x0, float3 x1) {
147 return float2x2(float2(x0, x1[0]), float2(x1[1], x1[2]));
148}
149float3x2 float3x2_from_float2_float_float3(float2 x0, float x1, float3 x2) {
150 return float3x2(float2(x0[0], x0[1]), float2(x1, x2[0]), float2(x2[1], x2[2]));
151}
152fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
153 Outputs _outputStruct;
154 thread Outputs* _out = &_outputStruct;
155 float2x2 m5 = float2x2(float2x2_from_float4(float4(1.0, 2.0, 3.0, 4.0))[0][0]);
156 _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]);
157 return *_out;
158}
159)__MSL__");
Ethan Nicholas842d31b2019-01-22 10:59:11 -0500160}
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500161
162DEF_TEST(SkSLMetalConstantSwizzle, r) {
John Stiles88a41072020-07-06 09:59:45 -0400163 test(r, *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500164 "void main() {"
165 "sk_FragColor = half4(0.5).rgb1;"
166 "}",
Ethan Nicholas5476f2e2019-03-07 15:11:31 -0500167 "#include <metal_stdlib>\n"
168 "#include <simd/simd.h>\n"
169 "using namespace metal;\n"
170 "struct Inputs {\n"
171 "};\n"
172 "struct Outputs {\n"
173 " float4 sk_FragColor [[color(0)]];\n"
174 "};\n"
175 "fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {\n"
176 " Outputs _outputStruct;\n"
177 " thread Outputs* _out = &_outputStruct;\n"
178 " _out->sk_FragColor = float4(float4(0.5).xyz, 1);\n"
179 " return *_out;\n"
180 "}\n");
181}
John Stiles88a41072020-07-06 09:59:45 -0400182
183DEF_TEST(SkSLMetalNumericGlobals, r) {
184 test(r, *SkSL::ShaderCapsFactory::Default(),
185R"__SkSL__(
186half attr1;
187int attr2 = 123;
188float attr3;
189half4 attr4 = half4(4, 5, 6, 7);
190void main()
191{
192 sk_FragColor = half4(attr1, attr2, half(attr3), attr4.x);
193}
194)__SkSL__",
195R"__MSL__(#include <metal_stdlib>
196#include <simd/simd.h>
197using namespace metal;
198struct Inputs {
199};
200struct Outputs {
201 float4 sk_FragColor [[color(0)]];
202};
203struct Globals {
204 float attr1;
205 int attr2;
206 float attr3;
207 float4 attr4;
208};
209
210
211
212fragment Outputs fragmentMain(Inputs _in [[stage_in]], bool _frontFacing [[front_facing]], float4 _fragCoord [[position]]) {
213 Globals globalStruct{{}, 123, {}, float4(4.0, 5.0, 6.0, 7.0)};
214 thread Globals* _globals = &globalStruct;
215 (void)_globals;
216 Outputs _outputStruct;
217 thread Outputs* _out = &_outputStruct;
218 _out->sk_FragColor = float4(_globals->attr1, float(_globals->attr2), _globals->attr3, _globals->attr4.x);
219 return *_out;
220}
221)__MSL__");
222}
223
224DEF_TEST(SkSLMetalSamplerGlobals, r) {
225 test(r, *SkSL::ShaderCapsFactory::Default(),
226R"__SkSL__(
227layout(binding=1) uniform sampler2D texA;
228layout(binding=0) uniform sampler2D texB;
229void main()
230{
231 sk_FragColor = sample(texA, half2(0)) * sample(texB, half2(0));
232}
233)__SkSL__",
234R"__MSL__(#include <metal_stdlib>
235#include <simd/simd.h>
236using namespace metal;
237struct Inputs {
238};
239struct Outputs {
240 float4 sk_FragColor [[color(0)]];
241};
242struct Globals {
243 texture2d<float> texA;
244 sampler texASmplr;
245 texture2d<float> texB;
246 sampler texBSmplr;
247};
248
249fragment 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]]) {
250 Globals globalStruct{texA, texASmplr, texB, texBSmplr};
251 thread Globals* _globals = &globalStruct;
252 (void)_globals;
253 Outputs _outputStruct;
254 thread Outputs* _out = &_outputStruct;
255 _out->sk_FragColor = _globals->texA.sample(_globals->texASmplr, float2(0.0)) * _globals->texB.sample(_globals->texBSmplr, float2(0.0));
256 return *_out;
257}
258)__MSL__");
259}