blob: 0358f07d59a199bcba95944f579b05c02be7c6b9 [file] [log] [blame]
Ethan Nicholas762466e2017-06-29 10:03:38 -04001/*
2 * Copyright 2017 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
8#include "SkSLCompiler.h"
9
10#include "Test.h"
11
12#if SK_SUPPORT_GPU
13
14static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
15 std::vector<const char*> expectedH, std::vector<const char*> expectedCPP) {
16 SkSL::Program::Settings settings;
17 settings.fCaps = &caps;
18 SkSL::Compiler compiler;
19 SkSL::StringStream output;
20 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(
21 SkSL::Program::kFragmentProcessor_Kind,
Brian Osman93ba0a42017-08-14 14:48:10 -040022 SkSL::String(src),
Ethan Nicholas762466e2017-06-29 10:03:38 -040023 settings);
24 if (!program) {
25 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
26 return;
27 }
28 REPORTER_ASSERT(r, program);
29 bool success = compiler.toH(*program, "Test", output);
30 if (!success) {
31 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
32 }
33 REPORTER_ASSERT(r, success);
34 if (success) {
35 for (const char* expected : expectedH) {
36 bool found = strstr(output.str().c_str(), expected);
37 if (!found) {
38 SkDebugf("HEADER MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
39 expected, output.str().c_str());
40 }
41 REPORTER_ASSERT(r, found);
42 }
43 }
44 output.reset();
45 success = compiler.toCPP(*program, "Test", output);
46 if (!success) {
47 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
48 }
49 REPORTER_ASSERT(r, success);
50 if (success) {
51 for (const char* expected : expectedCPP) {
52 bool found = strstr(output.str().c_str(), expected);
53 if (!found) {
54 SkDebugf("CPP MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
55 expected, output.str().c_str());
56 }
57 REPORTER_ASSERT(r, found);
58 }
59 }
60}
61
62DEF_TEST(SkSLFPHelloWorld, r) {
63 test(r,
64 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -040065 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -040066 "}",
67 *SkSL::ShaderCapsFactory::Default(),
68 {
69 "/*\n"
70 " * Copyright 2017 Google Inc.\n"
71 " *\n"
72 " * Use of this source code is governed by a BSD-style license that can be\n"
73 " * found in the LICENSE file.\n"
74 " */\n"
75 "\n"
76 "/*\n"
77 " * This file was autogenerated from GrTest.fp; do not modify.\n"
78 " */\n"
79 "#ifndef GrTest_DEFINED\n"
80 "#define GrTest_DEFINED\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -040081 "#include \"SkTypes.h\"\n"
82 "#if SK_SUPPORT_GPU\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040083 "#include \"GrFragmentProcessor.h\"\n"
84 "#include \"GrCoordTransform.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040085 "class GrTest : public GrFragmentProcessor {\n"
86 "public:\n"
Brian Salomonaff329b2017-08-11 09:40:37 -040087 " static std::unique_ptr<GrFragmentProcessor> Make() {\n"
88 " return std::unique_ptr<GrFragmentProcessor>(new GrTest());\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040089 " }\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040090 " GrTest(const GrTest& src);\n"
Brian Salomonaff329b2017-08-11 09:40:37 -040091 " std::unique_ptr<GrFragmentProcessor> clone() const override;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040092 " const char* name() const override { return \"Test\"; }\n"
93 "private:\n"
94 " GrTest()\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -040095 " : INHERITED(kGrTest_ClassID, kNone_OptimizationFlags) {\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040096 " }\n"
97 " GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n"
98 " void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) "
99 "const override;\n"
100 " bool onIsEqual(const GrFragmentProcessor&) const override;\n"
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400101 " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400102 " typedef GrFragmentProcessor INHERITED;\n"
103 "};\n"
104 "#endif\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400105 "#endif\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400106 },
107 {
108 "/*\n"
109 " * Copyright 2017 Google Inc.\n"
110 " *\n"
111 " * Use of this source code is governed by a BSD-style license that can be\n"
112 " * found in the LICENSE file.\n"
113 " */\n"
114 "\n"
115 "/*\n"
116 " * This file was autogenerated from GrTest.fp; do not modify.\n"
117 " */\n"
118 "#include \"GrTest.h\"\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400119 "#if SK_SUPPORT_GPU\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400120 "#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
121 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
122 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400123 "#include \"SkSLCPP.h\"\n"
124 "#include \"SkSLUtil.h\"\n"
125 "class GrGLSLTest : public GrGLSLFragmentProcessor {\n"
126 "public:\n"
127 " GrGLSLTest() {}\n"
128 " void emitCode(EmitArgs& args) override {\n"
129 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"
130 " const GrTest& _outer = args.fFp.cast<GrTest>();\n"
131 " (void) _outer;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400132 " fragBuilder->codeAppendf(\"%s = half4(1.0);\\n\", args.fOutputColor);\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400133 " }\n"
134 "private:\n"
135 " void onSetData(const GrGLSLProgramDataManager& pdman, "
136 "const GrFragmentProcessor& _proc) override {\n"
137 " }\n"
138 "};\n"
139 "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n"
140 " return new GrGLSLTest();\n"
141 "}\n"
142 "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
143 "GrProcessorKeyBuilder* b) const {\n"
144 "}\n"
145 "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n"
146 " const GrTest& that = other.cast<GrTest>();\n"
147 " (void) that;\n"
148 " return true;\n"
149 "}\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400150 "GrTest::GrTest(const GrTest& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400151 ": INHERITED(kGrTest_ClassID, src.optimizationFlags()) {\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400152 "}\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400153 "std::unique_ptr<GrFragmentProcessor> GrTest::clone() const {\n"
154 " return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this));\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400155 "}\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400156 "#endif\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400157 });
158}
159
160DEF_TEST(SkSLFPInput, r) {
161 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400162 "in half2 point;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400163 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400164 "sk_OutColor = half4(point, point);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400165 "}",
166 *SkSL::ShaderCapsFactory::Default(),
167 {
168 "SkPoint point() const { return fPoint; }",
Brian Salomonaff329b2017-08-11 09:40:37 -0400169 "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {",
170 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(point));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400171 "GrTest(SkPoint point)",
172 ", fPoint(point)"
173 },
174 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400175 "fragBuilder->codeAppendf(\"%s = half4(half2(%f, %f), half2(%f, %f));\\n\", "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400176 "args.fOutputColor, _outer.point().fX, _outer.point().fY, "
177 "_outer.point().fX, _outer.point().fY);",
178 "if (fPoint != that.fPoint) return false;"
179 });
180}
181
182DEF_TEST(SkSLFPUniform, r) {
183 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400184 "uniform half4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400185 "void main() {"
186 "sk_OutColor = color;"
187 "}",
188 *SkSL::ShaderCapsFactory::Default(),
189 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400190 "static std::unique_ptr<GrFragmentProcessor> Make()"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400191 },
192 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400193 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400194 "kDefault_GrSLPrecision, \"color\");",
195 });
196}
197
198DEF_TEST(SkSLFPInUniform, r) {
199 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400200 "in uniform half4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400201 "void main() {"
202 "sk_OutColor = color;"
203 "}",
204 *SkSL::ShaderCapsFactory::Default(),
205 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400206 "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400207 },
208 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400209 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400210 "kDefault_GrSLPrecision, \"color\");",
211 "const SkRect colorValue = _outer.color();",
Ethan Nicholasee338732017-07-17 15:13:45 -0400212 "pdman.set4fv(fColorVar, 1, (float*) &colorValue);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400213 });
214}
215
216DEF_TEST(SkSLFPSections, r) {
217 test(r,
218 "@header { header section }"
219 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400220 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400221 "}",
222 *SkSL::ShaderCapsFactory::Default(),
223 {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400224 "#if SK_SUPPORT_GPU\n header section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400225 },
226 {});
227 test(r,
228 "@class { class section }"
229 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400230 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400231 "}",
232 *SkSL::ShaderCapsFactory::Default(),
233 {
234 "class GrTest : public GrFragmentProcessor {\n"
235 "public:\n"
236 " class section"
237 },
238 {});
239 test(r,
240 "@cpp { cpp section }"
241 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400242 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400243 "}",
244 *SkSL::ShaderCapsFactory::Default(),
245 {},
246 {"cpp section"});
247 test(r,
248 "@constructorParams { int x, float y, std::vector<float> z }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400249 "in float w;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400250 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400251 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400252 "}",
253 *SkSL::ShaderCapsFactory::Default(),
254 {
255 "Make(float w, int x, float y, std::vector<float> z )",
Brian Salomonaff329b2017-08-11 09:40:37 -0400256 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400257 "GrTest(float w, int x, float y, std::vector<float> z )",
258 ", fW(w) {"
259 },
260 {});
261 test(r,
262 "@constructor { constructor section }"
263 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400264 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400265 "}",
266 *SkSL::ShaderCapsFactory::Default(),
267 {
268 "private:\n constructor section"
269 },
270 {});
271 test(r,
272 "@initializers { initializers section }"
273 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400274 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400275 "}",
276 *SkSL::ShaderCapsFactory::Default(),
277 {
Ethan Nicholasabff9562017-10-09 10:54:08 -0400278 ": INHERITED(kGrTest_ClassID, kNone_OptimizationFlags)\n , initializers section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400279 },
280 {});
281 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400282 "half x = 10;"
283 "@emitCode { fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2); }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400284 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400285 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400286 "}",
287 *SkSL::ShaderCapsFactory::Default(),
288 {},
289 {
290 "x = 10.0;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400291 " fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400292 });
293 test(r,
294 "@fields { fields section }"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400295 "@clone { }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400296 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400297 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400298 "}",
299 *SkSL::ShaderCapsFactory::Default(),
300 {
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400301 "GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400302 " fields section typedef GrFragmentProcessor INHERITED;"
303 },
304 {});
305 test(r,
306 "@make { make section }"
307 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400308 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400309 "}",
310 *SkSL::ShaderCapsFactory::Default(),
311 {
312 "public:\n"
313 " make section"
314 },
315 {});
316 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400317 "uniform half calculated;"
318 "in half provided;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400319 "@setData(varName) { varName.set1f(calculated, provided * 2); }"
320 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400321 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400322 "}",
323 *SkSL::ShaderCapsFactory::Default(),
324 {},
325 {
326 "void onSetData(const GrGLSLProgramDataManager& varName, "
327 "const GrFragmentProcessor& _proc) override {\n",
328 "UniformHandle& calculated = fCalculatedVar;",
329 "auto provided = _outer.provided();",
330 "varName.set1f(calculated, provided * 2);"
331 });
332 test(r,
333 "@test(testDataName) { testDataName section }"
334 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400335 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400336 "}",
337 *SkSL::ShaderCapsFactory::Default(),
338 {},
339 {
340 "#if GR_TEST_UTILS\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400341 "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400342 " testDataName section }\n"
343 "#endif"
344 });
345}
346
Ethan Nicholas762466e2017-06-29 10:03:38 -0400347DEF_TEST(SkSLFPTransformedCoords, r) {
348 test(r,
349 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400350 "sk_OutColor = half4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400351 "}",
352 *SkSL::ShaderCapsFactory::Default(),
353 {},
354 {
Brian Osman72a37be2017-08-15 09:19:53 -0400355 "SkString sk_TransformedCoords2D_0 = "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400356 "fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);",
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400357 "fragBuilder->codeAppendf(\"%s = half4(%s, %s);\\n\", args.fOutputColor, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400358 "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());"
359 });
360
361}
362
363DEF_TEST(SkSLFPLayoutWhen, r) {
364 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400365 "layout(when=someExpression(someOtherExpression())) uniform half sometimes;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400366 "void main() {"
367 "}",
368 *SkSL::ShaderCapsFactory::Default(),
369 {},
370 {
371 "if (someExpression(someOtherExpression())) {\n"
372 " fSometimesVar = args.fUniformHandler->addUniform"
373 });
374
375}
376
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400377DEF_TEST(SkSLFPChildProcessors, r) {
378 test(r,
379 "in fragmentProcessor child1;"
380 "in fragmentProcessor child2;"
381 "void main() {"
382 " sk_OutColor = process(child1) * process(child2);"
383 "}",
384 *SkSL::ShaderCapsFactory::Default(),
385 {
386 "this->registerChildProcessor(std::move(child1));",
387 "this->registerChildProcessor(std::move(child2));"
388 },
389 {
390 "SkString _child0(\"_child0\");",
391 "this->emitChild(0, &_child0, args);",
392 "SkString _child1(\"_child1\");",
393 "this->emitChild(1, &_child1, args);",
394 "this->registerChildProcessor(src.childProcessor(0).clone());",
395 "this->registerChildProcessor(src.childProcessor(1).clone());"
396 });
397}
398
Ethan Nicholas762466e2017-06-29 10:03:38 -0400399#endif