blob: 03b3922ec2b3d7b04cef988d9cb9985a720045d7 [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,
Ethan Nicholas130fb3f2018-02-01 12:14:34 -050064 "/* HEADER */"
Ethan Nicholas762466e2017-06-29 10:03:38 -040065 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -040066 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -040067 "}",
68 *SkSL::ShaderCapsFactory::Default(),
69 {
Ethan Nicholas130fb3f2018-02-01 12:14:34 -050070 "/* HEADER */\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040071 "\n"
Ethan Nicholas130fb3f2018-02-01 12:14:34 -050072 "/**************************************************************************************************\n"
73 " *** This file was autogenerated from GrTest.fp; do not modify.\n"
74 " **************************************************************************************************/\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040075 "#ifndef GrTest_DEFINED\n"
76 "#define GrTest_DEFINED\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -040077 "#include \"SkTypes.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040078 "#include \"GrFragmentProcessor.h\"\n"
79 "#include \"GrCoordTransform.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040080 "class GrTest : public GrFragmentProcessor {\n"
81 "public:\n"
Brian Salomonaff329b2017-08-11 09:40:37 -040082 " static std::unique_ptr<GrFragmentProcessor> Make() {\n"
83 " return std::unique_ptr<GrFragmentProcessor>(new GrTest());\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040084 " }\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040085 " GrTest(const GrTest& src);\n"
Brian Salomonaff329b2017-08-11 09:40:37 -040086 " std::unique_ptr<GrFragmentProcessor> clone() const override;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040087 " const char* name() const override { return \"Test\"; }\n"
88 "private:\n"
89 " GrTest()\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -040090 " : INHERITED(kGrTest_ClassID, kNone_OptimizationFlags) {\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040091 " }\n"
92 " GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n"
93 " void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) "
94 "const override;\n"
95 " bool onIsEqual(const GrFragmentProcessor&) const override;\n"
Brian Salomon0c26a9d2017-07-06 10:09:38 -040096 " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040097 " typedef GrFragmentProcessor INHERITED;\n"
98 "};\n"
99 "#endif\n"
100 },
101 {
Ethan Nicholas130fb3f2018-02-01 12:14:34 -0500102 "/* HEADER */\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400103 "\n"
Ethan Nicholas130fb3f2018-02-01 12:14:34 -0500104 "/**************************************************************************************************\n"
105 " *** This file was autogenerated from GrTest.fp; do not modify.\n"
106 " **************************************************************************************************/\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400107 "#include \"GrTest.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400108 "#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
109 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
110 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas2d5f9b32017-12-13 14:36:14 -0500111 "#include \"GrTexture.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400112 "#include \"SkSLCPP.h\"\n"
113 "#include \"SkSLUtil.h\"\n"
114 "class GrGLSLTest : public GrGLSLFragmentProcessor {\n"
115 "public:\n"
116 " GrGLSLTest() {}\n"
117 " void emitCode(EmitArgs& args) override {\n"
118 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"
119 " const GrTest& _outer = args.fFp.cast<GrTest>();\n"
120 " (void) _outer;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400121 " fragBuilder->codeAppendf(\"%s = half4(1.0);\\n\", args.fOutputColor);\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400122 " }\n"
123 "private:\n"
124 " void onSetData(const GrGLSLProgramDataManager& pdman, "
125 "const GrFragmentProcessor& _proc) override {\n"
126 " }\n"
127 "};\n"
128 "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n"
129 " return new GrGLSLTest();\n"
130 "}\n"
131 "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
132 "GrProcessorKeyBuilder* b) const {\n"
133 "}\n"
134 "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n"
135 " const GrTest& that = other.cast<GrTest>();\n"
136 " (void) that;\n"
137 " return true;\n"
138 "}\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400139 "GrTest::GrTest(const GrTest& src)\n"
Ethan Nicholasabff9562017-10-09 10:54:08 -0400140 ": INHERITED(kGrTest_ClassID, src.optimizationFlags()) {\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400141 "}\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400142 "std::unique_ptr<GrFragmentProcessor> GrTest::clone() const {\n"
143 " return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this));\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400144 "}\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400145 });
146}
147
148DEF_TEST(SkSLFPInput, r) {
149 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400150 "in half2 point;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400151 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400152 "sk_OutColor = half4(point, point);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400153 "}",
154 *SkSL::ShaderCapsFactory::Default(),
155 {
156 "SkPoint point() const { return fPoint; }",
Brian Salomonaff329b2017-08-11 09:40:37 -0400157 "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {",
158 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(point));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400159 "GrTest(SkPoint point)",
160 ", fPoint(point)"
161 },
162 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400163 "fragBuilder->codeAppendf(\"%s = half4(half2(%f, %f), half2(%f, %f));\\n\", "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400164 "args.fOutputColor, _outer.point().fX, _outer.point().fY, "
165 "_outer.point().fX, _outer.point().fY);",
166 "if (fPoint != that.fPoint) return false;"
167 });
168}
169
170DEF_TEST(SkSLFPUniform, r) {
171 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400172 "uniform half4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400173 "void main() {"
174 "sk_OutColor = color;"
175 "}",
176 *SkSL::ShaderCapsFactory::Default(),
177 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400178 "static std::unique_ptr<GrFragmentProcessor> Make()"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400179 },
180 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400181 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400182 "kDefault_GrSLPrecision, \"color\");",
183 });
184}
185
186DEF_TEST(SkSLFPInUniform, r) {
187 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400188 "in uniform half4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400189 "void main() {"
190 "sk_OutColor = color;"
191 "}",
192 *SkSL::ShaderCapsFactory::Default(),
193 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400194 "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400195 },
196 {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400197 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400198 "kDefault_GrSLPrecision, \"color\");",
199 "const SkRect colorValue = _outer.color();",
Ethan Nicholasee338732017-07-17 15:13:45 -0400200 "pdman.set4fv(fColorVar, 1, (float*) &colorValue);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400201 });
202}
203
204DEF_TEST(SkSLFPSections, r) {
205 test(r,
206 "@header { header section }"
207 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400208 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400209 "}",
210 *SkSL::ShaderCapsFactory::Default(),
211 {
Greg Daniel3e8c3452018-04-06 10:37:55 -0400212 "header section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400213 },
214 {});
215 test(r,
216 "@class { class section }"
217 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400218 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400219 "}",
220 *SkSL::ShaderCapsFactory::Default(),
221 {
222 "class GrTest : public GrFragmentProcessor {\n"
223 "public:\n"
224 " class section"
225 },
226 {});
227 test(r,
228 "@cpp { cpp 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 {"cpp section"});
235 test(r,
236 "@constructorParams { int x, float y, std::vector<float> z }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400237 "in float w;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400238 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400239 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400240 "}",
241 *SkSL::ShaderCapsFactory::Default(),
242 {
243 "Make(float w, int x, float y, std::vector<float> z )",
Brian Salomonaff329b2017-08-11 09:40:37 -0400244 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400245 "GrTest(float w, int x, float y, std::vector<float> z )",
246 ", fW(w) {"
247 },
248 {});
249 test(r,
250 "@constructor { constructor section }"
251 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400252 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400253 "}",
254 *SkSL::ShaderCapsFactory::Default(),
255 {
256 "private:\n constructor section"
257 },
258 {});
259 test(r,
260 "@initializers { initializers section }"
261 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400262 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400263 "}",
264 *SkSL::ShaderCapsFactory::Default(),
265 {
Ethan Nicholasabff9562017-10-09 10:54:08 -0400266 ": INHERITED(kGrTest_ClassID, kNone_OptimizationFlags)\n , initializers section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400267 },
268 {});
269 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400270 "half x = 10;"
271 "@emitCode { fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2); }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400272 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400273 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400274 "}",
275 *SkSL::ShaderCapsFactory::Default(),
276 {},
277 {
278 "x = 10.0;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400279 " fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400280 });
281 test(r,
282 "@fields { fields section }"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400283 "@clone { }"
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 {
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400289 "GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400290 " fields section typedef GrFragmentProcessor INHERITED;"
291 },
292 {});
293 test(r,
294 "@make { make section }"
295 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400296 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400297 "}",
298 *SkSL::ShaderCapsFactory::Default(),
299 {
300 "public:\n"
301 " make section"
302 },
303 {});
304 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400305 "uniform half calculated;"
306 "in half provided;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400307 "@setData(varName) { varName.set1f(calculated, provided * 2); }"
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 {
314 "void onSetData(const GrGLSLProgramDataManager& varName, "
315 "const GrFragmentProcessor& _proc) override {\n",
316 "UniformHandle& calculated = fCalculatedVar;",
317 "auto provided = _outer.provided();",
318 "varName.set1f(calculated, provided * 2);"
319 });
320 test(r,
321 "@test(testDataName) { testDataName section }"
322 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400323 "sk_OutColor = half4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400324 "}",
325 *SkSL::ShaderCapsFactory::Default(),
326 {},
327 {
328 "#if GR_TEST_UTILS\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400329 "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400330 " testDataName section }\n"
331 "#endif"
332 });
333}
334
Ethan Nicholas762466e2017-06-29 10:03:38 -0400335DEF_TEST(SkSLFPTransformedCoords, r) {
336 test(r,
337 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400338 "sk_OutColor = half4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400339 "}",
340 *SkSL::ShaderCapsFactory::Default(),
341 {},
342 {
Brian Osman72a37be2017-08-15 09:19:53 -0400343 "SkString sk_TransformedCoords2D_0 = "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400344 "fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);",
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400345 "fragBuilder->codeAppendf(\"%s = half4(%s, %s);\\n\", args.fOutputColor, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400346 "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());"
347 });
348
349}
350
351DEF_TEST(SkSLFPLayoutWhen, r) {
352 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400353 "layout(when=someExpression(someOtherExpression())) uniform half sometimes;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400354 "void main() {"
355 "}",
356 *SkSL::ShaderCapsFactory::Default(),
357 {},
358 {
359 "if (someExpression(someOtherExpression())) {\n"
360 " fSometimesVar = args.fUniformHandler->addUniform"
361 });
362
363}
364
Ethan Nicholasc9472af2017-10-10 16:30:21 -0400365DEF_TEST(SkSLFPChildProcessors, r) {
366 test(r,
367 "in fragmentProcessor child1;"
368 "in fragmentProcessor child2;"
369 "void main() {"
370 " sk_OutColor = process(child1) * process(child2);"
371 "}",
372 *SkSL::ShaderCapsFactory::Default(),
373 {
374 "this->registerChildProcessor(std::move(child1));",
375 "this->registerChildProcessor(std::move(child2));"
376 },
377 {
378 "SkString _child0(\"_child0\");",
379 "this->emitChild(0, &_child0, args);",
380 "SkString _child1(\"_child1\");",
381 "this->emitChild(1, &_child1, args);",
382 "this->registerChildProcessor(src.childProcessor(0).clone());",
383 "this->registerChildProcessor(src.childProcessor(1).clone());"
384 });
385}
386
Ethan Nicholas762466e2017-06-29 10:03:38 -0400387#endif