blob: c3f9468ca85978c04e53cc0c7fa8d55f5f74b47d [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() {"
Brian Salomon1d816b92017-08-17 11:07:59 -040065 "sk_OutColor = float4(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 Nicholas68990be2017-07-13 09:36:52 -040085 "#include \"GrColorSpaceXform.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040086 "class GrTest : public GrFragmentProcessor {\n"
87 "public:\n"
Brian Salomonaff329b2017-08-11 09:40:37 -040088 " static std::unique_ptr<GrFragmentProcessor> Make() {\n"
89 " return std::unique_ptr<GrFragmentProcessor>(new GrTest());\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040090 " }\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040091 " GrTest(const GrTest& src);\n"
Brian Salomonaff329b2017-08-11 09:40:37 -040092 " std::unique_ptr<GrFragmentProcessor> clone() const override;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -040093 " const char* name() const override { return \"Test\"; }\n"
94 "private:\n"
95 " GrTest()\n"
96 " : INHERITED(kNone_OptimizationFlags) {\n"
97 " this->initClassID<GrTest>();\n"
98 " }\n"
99 " GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n"
100 " void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) "
101 "const override;\n"
102 " bool onIsEqual(const GrFragmentProcessor&) const override;\n"
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400103 " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400104 " typedef GrFragmentProcessor INHERITED;\n"
105 "};\n"
106 "#endif\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400107 "#endif\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400108 },
109 {
110 "/*\n"
111 " * Copyright 2017 Google Inc.\n"
112 " *\n"
113 " * Use of this source code is governed by a BSD-style license that can be\n"
114 " * found in the LICENSE file.\n"
115 " */\n"
116 "\n"
117 "/*\n"
118 " * This file was autogenerated from GrTest.fp; do not modify.\n"
119 " */\n"
120 "#include \"GrTest.h\"\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400121 "#if SK_SUPPORT_GPU\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400122 "#include \"glsl/GrGLSLColorSpaceXformHelper.h\"\n"
123 "#include \"glsl/GrGLSLFragmentProcessor.h\"\n"
124 "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n"
125 "#include \"glsl/GrGLSLProgramBuilder.h\"\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400126 "#include \"SkSLCPP.h\"\n"
127 "#include \"SkSLUtil.h\"\n"
128 "class GrGLSLTest : public GrGLSLFragmentProcessor {\n"
129 "public:\n"
130 " GrGLSLTest() {}\n"
131 " void emitCode(EmitArgs& args) override {\n"
132 " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n"
133 " const GrTest& _outer = args.fFp.cast<GrTest>();\n"
134 " (void) _outer;\n"
Brian Salomon1d816b92017-08-17 11:07:59 -0400135 " fragBuilder->codeAppendf(\"%s = float4(1.0);\\n\", args.fOutputColor);\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400136 " }\n"
137 "private:\n"
138 " void onSetData(const GrGLSLProgramDataManager& pdman, "
139 "const GrFragmentProcessor& _proc) override {\n"
140 " }\n"
141 "};\n"
142 "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n"
143 " return new GrGLSLTest();\n"
144 "}\n"
145 "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
146 "GrProcessorKeyBuilder* b) const {\n"
147 "}\n"
148 "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n"
149 " const GrTest& that = other.cast<GrTest>();\n"
150 " (void) that;\n"
151 " return true;\n"
152 "}\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400153 "GrTest::GrTest(const GrTest& src)\n"
154 ": INHERITED(src.optimizationFlags()) {\n"
155 " this->initClassID<GrTest>();\n"
156 "}\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400157 "std::unique_ptr<GrFragmentProcessor> GrTest::clone() const {\n"
158 " return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this));\n"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400159 "}\n"
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400160 "#endif\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400161 });
162}
163
164DEF_TEST(SkSLFPInput, r) {
165 test(r,
Brian Salomon1d816b92017-08-17 11:07:59 -0400166 "in float2 point;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400167 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400168 "sk_OutColor = float4(point, point);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400169 "}",
170 *SkSL::ShaderCapsFactory::Default(),
171 {
172 "SkPoint point() const { return fPoint; }",
Brian Salomonaff329b2017-08-11 09:40:37 -0400173 "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {",
174 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(point));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400175 "GrTest(SkPoint point)",
176 ", fPoint(point)"
177 },
178 {
Brian Salomon1d816b92017-08-17 11:07:59 -0400179 "fragBuilder->codeAppendf(\"%s = float4(float2(%f, %f), float2(%f, %f));\\n\", "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400180 "args.fOutputColor, _outer.point().fX, _outer.point().fY, "
181 "_outer.point().fX, _outer.point().fY);",
182 "if (fPoint != that.fPoint) return false;"
183 });
184}
185
186DEF_TEST(SkSLFPUniform, r) {
187 test(r,
Brian Salomon1d816b92017-08-17 11:07:59 -0400188 "uniform float4 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()"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400195 },
196 {
Brian Salomon1d816b92017-08-17 11:07:59 -0400197 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kVec4f_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400198 "kDefault_GrSLPrecision, \"color\");",
199 });
200}
201
202DEF_TEST(SkSLFPInUniform, r) {
203 test(r,
Brian Salomon1d816b92017-08-17 11:07:59 -0400204 "in uniform float4 color;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400205 "void main() {"
206 "sk_OutColor = color;"
207 "}",
208 *SkSL::ShaderCapsFactory::Default(),
209 {
Brian Salomonaff329b2017-08-11 09:40:37 -0400210 "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400211 },
212 {
Brian Salomon1d816b92017-08-17 11:07:59 -0400213 "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kVec4f_GrSLType, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400214 "kDefault_GrSLPrecision, \"color\");",
215 "const SkRect colorValue = _outer.color();",
Ethan Nicholasee338732017-07-17 15:13:45 -0400216 "pdman.set4fv(fColorVar, 1, (float*) &colorValue);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400217 });
218}
219
220DEF_TEST(SkSLFPSections, r) {
221 test(r,
222 "@header { header section }"
223 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400224 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400225 "}",
226 *SkSL::ShaderCapsFactory::Default(),
227 {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400228 "#if SK_SUPPORT_GPU\n header section"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400229 },
230 {});
231 test(r,
232 "@class { class section }"
233 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400234 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400235 "}",
236 *SkSL::ShaderCapsFactory::Default(),
237 {
238 "class GrTest : public GrFragmentProcessor {\n"
239 "public:\n"
240 " class section"
241 },
242 {});
243 test(r,
244 "@cpp { cpp section }"
245 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400246 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400247 "}",
248 *SkSL::ShaderCapsFactory::Default(),
249 {},
250 {"cpp section"});
251 test(r,
252 "@constructorParams { int x, float y, std::vector<float> z }"
Brian Salomon1d816b92017-08-17 11:07:59 -0400253 "in float w;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400254 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400255 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400256 "}",
257 *SkSL::ShaderCapsFactory::Default(),
258 {
259 "Make(float w, int x, float y, std::vector<float> z )",
Brian Salomonaff329b2017-08-11 09:40:37 -0400260 "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));",
Ethan Nicholas762466e2017-06-29 10:03:38 -0400261 "GrTest(float w, int x, float y, std::vector<float> z )",
262 ", fW(w) {"
263 },
264 {});
265 test(r,
266 "@constructor { constructor section }"
267 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400268 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400269 "}",
270 *SkSL::ShaderCapsFactory::Default(),
271 {
272 "private:\n constructor section"
273 },
274 {});
275 test(r,
276 "@initializers { initializers section }"
277 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400278 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400279 "}",
280 *SkSL::ShaderCapsFactory::Default(),
281 {
282 ": INHERITED(kNone_OptimizationFlags)\n , initializers section"
283 },
284 {});
285 test(r,
Brian Salomon1d816b92017-08-17 11:07:59 -0400286 "float x = 10;"
287 "@emitCode { fragBuilder->codeAppendf(\"float y = %d\\n\", x * 2); }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400288 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400289 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400290 "}",
291 *SkSL::ShaderCapsFactory::Default(),
292 {},
293 {
294 "x = 10.0;\n"
Brian Salomon1d816b92017-08-17 11:07:59 -0400295 " fragBuilder->codeAppendf(\"float y = %d\\n\", x * 2);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400296 });
297 test(r,
298 "@fields { fields section }"
Ethan Nicholasf57c0d62017-07-31 11:18:22 -0400299 "@clone { }"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400300 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400301 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400302 "}",
303 *SkSL::ShaderCapsFactory::Default(),
304 {
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400305 "GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400306 " fields section typedef GrFragmentProcessor INHERITED;"
307 },
308 {});
309 test(r,
310 "@make { make section }"
311 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400312 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400313 "}",
314 *SkSL::ShaderCapsFactory::Default(),
315 {
316 "public:\n"
317 " make section"
318 },
319 {});
320 test(r,
Brian Salomon1d816b92017-08-17 11:07:59 -0400321 "uniform float calculated;"
322 "in float provided;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400323 "@setData(varName) { varName.set1f(calculated, provided * 2); }"
324 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400325 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400326 "}",
327 *SkSL::ShaderCapsFactory::Default(),
328 {},
329 {
330 "void onSetData(const GrGLSLProgramDataManager& varName, "
331 "const GrFragmentProcessor& _proc) override {\n",
332 "UniformHandle& calculated = fCalculatedVar;",
333 "auto provided = _outer.provided();",
334 "varName.set1f(calculated, provided * 2);"
335 });
336 test(r,
337 "@test(testDataName) { testDataName section }"
338 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400339 "sk_OutColor = float4(1);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400340 "}",
341 *SkSL::ShaderCapsFactory::Default(),
342 {},
343 {
344 "#if GR_TEST_UTILS\n"
Brian Salomonaff329b2017-08-11 09:40:37 -0400345 "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400346 " testDataName section }\n"
347 "#endif"
348 });
349}
350
351DEF_TEST(SkSLFPColorSpaceXform, r) {
352 test(r,
353 "in uniform sampler2D image;"
354 "in uniform colorSpaceXform colorXform;"
355 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400356 "sk_OutColor = sk_InColor * texture(image, float2(0, 0), colorXform);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400357 "}",
358 *SkSL::ShaderCapsFactory::Default(),
359 {
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400360 "sk_sp<GrColorSpaceXform> colorXform() const { return fColorXform; }",
361 "GrTest(sk_sp<GrTextureProxy> image, sk_sp<GrColorSpaceXform> colorXform)",
362 "this->addTextureSampler(&fImage);",
363 "sk_sp<GrColorSpaceXform> fColorXform;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400364 },
365 {
Brian Salomon1d816b92017-08-17 11:07:59 -0400366 "fragBuilder->codeAppendf(\"float4 _tmpVar1;%s = %s * %stexture(%s, "
367 "float2(0.0, 0.0)).%s%s;\\n\", args.fOutputColor, args.fInputColor ? args.fInputColor : "
368 "\"float4(1)\", fColorSpaceHelper.isValid() ? \"(_tmpVar1 = \" : \"\", "
Ethan Nicholas68990be2017-07-13 09:36:52 -0400369 "fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), "
Ethan Nicholasceb4d482017-07-10 15:40:20 -0400370 "fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), "
Brian Salomon1d816b92017-08-17 11:07:59 -0400371 "fColorSpaceHelper.isValid() ? SkStringPrintf(\", float4(clamp((%s * float4(_tmpVar1.rgb, "
Ethan Nicholas68990be2017-07-13 09:36:52 -0400372 "1.0)).rgb, 0.0, _tmpVar1.a), _tmpVar1.a))\", args.fUniformHandler->getUniformCStr("
373 "fColorSpaceHelper.gamutXformUniform())).c_str() : \"\");"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400374 });
375}
376
377DEF_TEST(SkSLFPTransformedCoords, r) {
378 test(r,
379 "void main() {"
Brian Salomon1d816b92017-08-17 11:07:59 -0400380 "sk_OutColor = float4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400381 "}",
382 *SkSL::ShaderCapsFactory::Default(),
383 {},
384 {
Brian Osman72a37be2017-08-15 09:19:53 -0400385 "SkString sk_TransformedCoords2D_0 = "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400386 "fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);",
Brian Salomon1d816b92017-08-17 11:07:59 -0400387 "fragBuilder->codeAppendf(\"%s = float4(%s, %s);\\n\", args.fOutputColor, "
Ethan Nicholas762466e2017-06-29 10:03:38 -0400388 "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());"
389 });
390
391}
392
393DEF_TEST(SkSLFPLayoutWhen, r) {
394 test(r,
Brian Salomon1d816b92017-08-17 11:07:59 -0400395 "layout(when=someExpression(someOtherExpression())) uniform float sometimes;"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400396 "void main() {"
397 "}",
398 *SkSL::ShaderCapsFactory::Default(),
399 {},
400 {
401 "if (someExpression(someOtherExpression())) {\n"
402 " fSometimesVar = args.fUniformHandler->addUniform"
403 });
404
405}
406
407#endif