blob: 982591082bf87ccc5ed303568413681c4f6708b1 [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 Nicholas2d5f9b32017-12-13 14:36:14 -0500123 "#include \"GrTexture.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400124 "#include \"SkSLCPP.h\"\n"
125 "#include \"SkSLUtil.h\"\n"
126 "class GrGLSLTest : public GrGLSLFragmentProcessor {\n"
127 "public:\n"
128 " GrGLSLTest() {}\n"
129 " void emitCode(EmitArgs& args) override {\n"
130 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"
131 " const GrTest& _outer = args.fFp.cast<GrTest>();\n"
132 " (void) _outer;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400133 " fragBuilder->codeAppendf(\"%s = half4(1.0);\\n\", args.fOutputColor);\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400134 " }\n"
135 "private:\n"
136 " void onSetData(const GrGLSLProgramDataManager& pdman, "
137 "const GrFragmentProcessor& _proc) override {\n"
138 " }\n"
139 "};\n"
140 "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n"
141 " return new GrGLSLTest();\n"
142 "}\n"
143 "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
144 "GrProcessorKeyBuilder* b) const {\n"
145 "}\n"
146 "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n"
147 " const GrTest& that = other.cast<GrTest>();\n"
148 " (void) that;\n"
149 " return true;\n"
150 "}\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400151 "GrTest::GrTest(const GrTest& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400152 ": INHERITED(kGrTest_ClassID, src.optimizationFlags()) {\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400153 "}\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400154 "std::unique_ptr<GrFragmentProcessor> GrTest::clone() const {\n"
155 " return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this));\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400156 "}\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400157 "#endif\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400158 });
159}
160
161DEF_TEST(SkSLFPInput, r) {
162 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400163 "in half2 point;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400164 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400165 "sk_OutColor = half4(point, point);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400166 "}",
167 *SkSL::ShaderCapsFactory::Default(),
168 {
169 "SkPoint point() const { return fPoint; }",
Brian Salomonaff329b2017-08-11 09:40:37 -0400170 "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {",
171 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(point));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400172 "GrTest(SkPoint point)",
173 ", fPoint(point)"
174 },
175 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400176 "fragBuilder->codeAppendf(\"%s = half4(half2(%f, %f), half2(%f, %f));\\n\", "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400177 "args.fOutputColor, _outer.point().fX, _outer.point().fY, "
178 "_outer.point().fX, _outer.point().fY);",
179 "if (fPoint != that.fPoint) return false;"
180 });
181}
182
183DEF_TEST(SkSLFPUniform, r) {
184 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400185 "uniform half4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400186 "void main() {"
187 "sk_OutColor = color;"
188 "}",
189 *SkSL::ShaderCapsFactory::Default(),
190 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400191 "static std::unique_ptr<GrFragmentProcessor> Make()"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400192 },
193 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400194 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400195 "kDefault_GrSLPrecision, \"color\");",
196 });
197}
198
199DEF_TEST(SkSLFPInUniform, r) {
200 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400201 "in uniform half4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400202 "void main() {"
203 "sk_OutColor = color;"
204 "}",
205 *SkSL::ShaderCapsFactory::Default(),
206 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400207 "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400208 },
209 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400210 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400211 "kDefault_GrSLPrecision, \"color\");",
212 "const SkRect colorValue = _outer.color();",
Ethan Nicholasee338732017-07-17 15:13:45 -0400213 "pdman.set4fv(fColorVar, 1, (float*) &colorValue);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400214 });
215}
216
217DEF_TEST(SkSLFPSections, r) {
218 test(r,
219 "@header { header section }"
220 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400221 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400222 "}",
223 *SkSL::ShaderCapsFactory::Default(),
224 {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400225 "#if SK_SUPPORT_GPU\n header section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400226 },
227 {});
228 test(r,
229 "@class { class section }"
230 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400231 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400232 "}",
233 *SkSL::ShaderCapsFactory::Default(),
234 {
235 "class GrTest : public GrFragmentProcessor {\n"
236 "public:\n"
237 " class section"
238 },
239 {});
240 test(r,
241 "@cpp { cpp section }"
242 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400243 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400244 "}",
245 *SkSL::ShaderCapsFactory::Default(),
246 {},
247 {"cpp section"});
248 test(r,
249 "@constructorParams { int x, float y, std::vector<float> z }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400250 "in float w;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400251 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400252 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400253 "}",
254 *SkSL::ShaderCapsFactory::Default(),
255 {
256 "Make(float w, int x, float y, std::vector<float> z )",
Brian Salomonaff329b2017-08-11 09:40:37 -0400257 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400258 "GrTest(float w, int x, float y, std::vector<float> z )",
259 ", fW(w) {"
260 },
261 {});
262 test(r,
263 "@constructor { constructor section }"
264 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400265 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400266 "}",
267 *SkSL::ShaderCapsFactory::Default(),
268 {
269 "private:\n constructor section"
270 },
271 {});
272 test(r,
273 "@initializers { initializers section }"
274 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400275 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400276 "}",
277 *SkSL::ShaderCapsFactory::Default(),
278 {
Ethan Nicholasabff9562017-10-09 10:54:08 -0400279 ": INHERITED(kGrTest_ClassID, kNone_OptimizationFlags)\n , initializers section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400280 },
281 {});
282 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400283 "half x = 10;"
284 "@emitCode { fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2); }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400285 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400286 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400287 "}",
288 *SkSL::ShaderCapsFactory::Default(),
289 {},
290 {
291 "x = 10.0;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400292 " fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400293 });
294 test(r,
295 "@fields { fields section }"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400296 "@clone { }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400297 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400298 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400299 "}",
300 *SkSL::ShaderCapsFactory::Default(),
301 {
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400302 "GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400303 " fields section typedef GrFragmentProcessor INHERITED;"
304 },
305 {});
306 test(r,
307 "@make { make section }"
308 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400309 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400310 "}",
311 *SkSL::ShaderCapsFactory::Default(),
312 {
313 "public:\n"
314 " make section"
315 },
316 {});
317 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400318 "uniform half calculated;"
319 "in half provided;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400320 "@setData(varName) { varName.set1f(calculated, provided * 2); }"
321 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400322 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400323 "}",
324 *SkSL::ShaderCapsFactory::Default(),
325 {},
326 {
327 "void onSetData(const GrGLSLProgramDataManager& varName, "
328 "const GrFragmentProcessor& _proc) override {\n",
329 "UniformHandle& calculated = fCalculatedVar;",
330 "auto provided = _outer.provided();",
331 "varName.set1f(calculated, provided * 2);"
332 });
333 test(r,
334 "@test(testDataName) { testDataName section }"
335 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400336 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400337 "}",
338 *SkSL::ShaderCapsFactory::Default(),
339 {},
340 {
341 "#if GR_TEST_UTILS\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400342 "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400343 " testDataName section }\n"
344 "#endif"
345 });
346}
347
Ethan Nicholas762466e2017-06-29 10:03:38 -0400348DEF_TEST(SkSLFPTransformedCoords, r) {
349 test(r,
350 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400351 "sk_OutColor = half4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400352 "}",
353 *SkSL::ShaderCapsFactory::Default(),
354 {},
355 {
Brian Osman72a37be2017-08-15 09:19:53 -0400356 "SkString sk_TransformedCoords2D_0 = "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400357 "fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);",
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400358 "fragBuilder->codeAppendf(\"%s = half4(%s, %s);\\n\", args.fOutputColor, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400359 "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());"
360 });
361
362}
363
364DEF_TEST(SkSLFPLayoutWhen, r) {
365 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400366 "layout(when=someExpression(someOtherExpression())) uniform half sometimes;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400367 "void main() {"
368 "}",
369 *SkSL::ShaderCapsFactory::Default(),
370 {},
371 {
372 "if (someExpression(someOtherExpression())) {\n"
373 " fSometimesVar = args.fUniformHandler->addUniform"
374 });
375
376}
377
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400378DEF_TEST(SkSLFPChildProcessors, r) {
379 test(r,
380 "in fragmentProcessor child1;"
381 "in fragmentProcessor child2;"
382 "void main() {"
383 " sk_OutColor = process(child1) * process(child2);"
384 "}",
385 *SkSL::ShaderCapsFactory::Default(),
386 {
387 "this->registerChildProcessor(std::move(child1));",
388 "this->registerChildProcessor(std::move(child2));"
389 },
390 {
391 "SkString _child0(\"_child0\");",
392 "this->emitChild(0, &_child0, args);",
393 "SkString _child1(\"_child1\");",
394 "this->emitChild(1, &_child1, args);",
395 "this->registerChildProcessor(src.childProcessor(0).clone());",
396 "this->registerChildProcessor(src.childProcessor(1).clone());"
397 });
398}
399
Ethan Nicholas762466e2017-06-29 10:03:38 -0400400#endif