Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame^] | 1 | /* |
| 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 | |
| 14 | static 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 = ∩︀ |
| 18 | SkSL::Compiler compiler; |
| 19 | SkSL::StringStream output; |
| 20 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
| 21 | SkSL::Program::kFragmentProcessor_Kind, |
| 22 | SkString(src), |
| 23 | 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 | |
| 62 | DEF_TEST(SkSLFPHelloWorld, r) { |
| 63 | test(r, |
| 64 | "void main() {" |
| 65 | "sk_OutColor = vec4(1);" |
| 66 | "}", |
| 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" |
| 81 | "#include \"GrFragmentProcessor.h\"\n" |
| 82 | "#include \"GrCoordTransform.h\"\n" |
| 83 | "#include \"effects/GrProxyMove.h\"\n" |
| 84 | "class GrTest : public GrFragmentProcessor {\n" |
| 85 | "public:\n" |
| 86 | " static sk_sp<GrFragmentProcessor> Make() {\n" |
| 87 | " return sk_sp<GrFragmentProcessor>(new GrTest());\n" |
| 88 | " }\n" |
| 89 | " const char* name() const override { return \"Test\"; }\n" |
| 90 | "private:\n" |
| 91 | " GrTest()\n" |
| 92 | " : INHERITED(kNone_OptimizationFlags) {\n" |
| 93 | " this->initClassID<GrTest>();\n" |
| 94 | " }\n" |
| 95 | " GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n" |
| 96 | " void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) " |
| 97 | "const override;\n" |
| 98 | " bool onIsEqual(const GrFragmentProcessor&) const override;\n" |
| 99 | " GR_DECLARE_FRAGMENT_PROCESSOR_TEST;\n" |
| 100 | " typedef GrFragmentProcessor INHERITED;\n" |
| 101 | "};\n" |
| 102 | "#endif\n" |
| 103 | }, |
| 104 | { |
| 105 | "/*\n" |
| 106 | " * Copyright 2017 Google Inc.\n" |
| 107 | " *\n" |
| 108 | " * Use of this source code is governed by a BSD-style license that can be\n" |
| 109 | " * found in the LICENSE file.\n" |
| 110 | " */\n" |
| 111 | "\n" |
| 112 | "/*\n" |
| 113 | " * This file was autogenerated from GrTest.fp; do not modify.\n" |
| 114 | " */\n" |
| 115 | "#include \"GrTest.h\"\n" |
| 116 | "#include \"glsl/GrGLSLColorSpaceXformHelper.h\"\n" |
| 117 | "#include \"glsl/GrGLSLFragmentProcessor.h\"\n" |
| 118 | "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n" |
| 119 | "#include \"glsl/GrGLSLProgramBuilder.h\"\n" |
| 120 | "#include \"GrResourceProvider.h\"\n" |
| 121 | "#include \"SkSLCPP.h\"\n" |
| 122 | "#include \"SkSLUtil.h\"\n" |
| 123 | "class GrGLSLTest : public GrGLSLFragmentProcessor {\n" |
| 124 | "public:\n" |
| 125 | " GrGLSLTest() {}\n" |
| 126 | " void emitCode(EmitArgs& args) override {\n" |
| 127 | " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n" |
| 128 | " const GrTest& _outer = args.fFp.cast<GrTest>();\n" |
| 129 | " (void) _outer;\n" |
| 130 | " fragBuilder->codeAppendf(\"%s = vec4(1.0);\\n\", args.fOutputColor);\n" |
| 131 | " }\n" |
| 132 | "private:\n" |
| 133 | " void onSetData(const GrGLSLProgramDataManager& pdman, " |
| 134 | "const GrFragmentProcessor& _proc) override {\n" |
| 135 | " }\n" |
| 136 | "};\n" |
| 137 | "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n" |
| 138 | " return new GrGLSLTest();\n" |
| 139 | "}\n" |
| 140 | "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, " |
| 141 | "GrProcessorKeyBuilder* b) const {\n" |
| 142 | "}\n" |
| 143 | "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n" |
| 144 | " const GrTest& that = other.cast<GrTest>();\n" |
| 145 | " (void) that;\n" |
| 146 | " return true;\n" |
| 147 | "}\n" |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | DEF_TEST(SkSLFPInput, r) { |
| 152 | test(r, |
| 153 | "in vec2 point;" |
| 154 | "void main() {" |
| 155 | "sk_OutColor = vec4(point, point);" |
| 156 | "}", |
| 157 | *SkSL::ShaderCapsFactory::Default(), |
| 158 | { |
| 159 | "SkPoint point() const { return fPoint; }", |
| 160 | "static sk_sp<GrFragmentProcessor> Make(SkPoint point) {", |
| 161 | "return sk_sp<GrFragmentProcessor>(new GrTest(point));", |
| 162 | "GrTest(SkPoint point)", |
| 163 | ", fPoint(point)" |
| 164 | }, |
| 165 | { |
| 166 | "fragBuilder->codeAppendf(\"%s = vec4(vec2(%f, %f), vec2(%f, %f));\\n\", " |
| 167 | "args.fOutputColor, _outer.point().fX, _outer.point().fY, " |
| 168 | "_outer.point().fX, _outer.point().fY);", |
| 169 | "if (fPoint != that.fPoint) return false;" |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | DEF_TEST(SkSLFPUniform, r) { |
| 174 | test(r, |
| 175 | "uniform vec4 color;" |
| 176 | "void main() {" |
| 177 | "sk_OutColor = color;" |
| 178 | "}", |
| 179 | *SkSL::ShaderCapsFactory::Default(), |
| 180 | { |
| 181 | "static sk_sp<GrFragmentProcessor> Make()" |
| 182 | }, |
| 183 | { |
| 184 | "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kVec4f_GrSLType, " |
| 185 | "kDefault_GrSLPrecision, \"color\");", |
| 186 | }); |
| 187 | } |
| 188 | |
| 189 | DEF_TEST(SkSLFPInUniform, r) { |
| 190 | test(r, |
| 191 | "in uniform vec4 color;" |
| 192 | "void main() {" |
| 193 | "sk_OutColor = color;" |
| 194 | "}", |
| 195 | *SkSL::ShaderCapsFactory::Default(), |
| 196 | { |
| 197 | "static sk_sp<GrFragmentProcessor> Make(SkRect color) {", |
| 198 | }, |
| 199 | { |
| 200 | "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kVec4f_GrSLType, " |
| 201 | "kDefault_GrSLPrecision, \"color\");", |
| 202 | "const SkRect colorValue = _outer.color();", |
| 203 | "pdman.set4fv(fColorVar, 4, (float*) &colorValue);" |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | DEF_TEST(SkSLFPSections, r) { |
| 208 | test(r, |
| 209 | "@header { header section }" |
| 210 | "void main() {" |
| 211 | "sk_OutColor = vec4(1);" |
| 212 | "}", |
| 213 | *SkSL::ShaderCapsFactory::Default(), |
| 214 | { |
| 215 | "header section class GrTest", |
| 216 | }, |
| 217 | {}); |
| 218 | test(r, |
| 219 | "@class { class section }" |
| 220 | "void main() {" |
| 221 | "sk_OutColor = vec4(1);" |
| 222 | "}", |
| 223 | *SkSL::ShaderCapsFactory::Default(), |
| 224 | { |
| 225 | "class GrTest : public GrFragmentProcessor {\n" |
| 226 | "public:\n" |
| 227 | " class section" |
| 228 | }, |
| 229 | {}); |
| 230 | test(r, |
| 231 | "@cpp { cpp section }" |
| 232 | "void main() {" |
| 233 | "sk_OutColor = vec4(1);" |
| 234 | "}", |
| 235 | *SkSL::ShaderCapsFactory::Default(), |
| 236 | {}, |
| 237 | {"cpp section"}); |
| 238 | test(r, |
| 239 | "@constructorParams { int x, float y, std::vector<float> z }" |
| 240 | "in float w;" |
| 241 | "void main() {" |
| 242 | "sk_OutColor = vec4(1);" |
| 243 | "}", |
| 244 | *SkSL::ShaderCapsFactory::Default(), |
| 245 | { |
| 246 | "Make(float w, int x, float y, std::vector<float> z )", |
| 247 | "return sk_sp<GrFragmentProcessor>(new GrTest(w, x, y, z));", |
| 248 | "GrTest(float w, int x, float y, std::vector<float> z )", |
| 249 | ", fW(w) {" |
| 250 | }, |
| 251 | {}); |
| 252 | test(r, |
| 253 | "@constructor { constructor section }" |
| 254 | "void main() {" |
| 255 | "sk_OutColor = vec4(1);" |
| 256 | "}", |
| 257 | *SkSL::ShaderCapsFactory::Default(), |
| 258 | { |
| 259 | "private:\n constructor section" |
| 260 | }, |
| 261 | {}); |
| 262 | test(r, |
| 263 | "@initializers { initializers section }" |
| 264 | "void main() {" |
| 265 | "sk_OutColor = vec4(1);" |
| 266 | "}", |
| 267 | *SkSL::ShaderCapsFactory::Default(), |
| 268 | { |
| 269 | ": INHERITED(kNone_OptimizationFlags)\n , initializers section" |
| 270 | }, |
| 271 | {}); |
| 272 | test(r, |
| 273 | "float x = 10;" |
| 274 | "@emitCode { fragBuilder->codeAppendf(\"float y = %d\\n\", x * 2); }" |
| 275 | "void main() {" |
| 276 | "sk_OutColor = vec4(1);" |
| 277 | "}", |
| 278 | *SkSL::ShaderCapsFactory::Default(), |
| 279 | {}, |
| 280 | { |
| 281 | "x = 10.0;\n" |
| 282 | " fragBuilder->codeAppendf(\"float y = %d\\n\", x * 2);" |
| 283 | }); |
| 284 | test(r, |
| 285 | "@fields { fields section }" |
| 286 | "void main() {" |
| 287 | "sk_OutColor = vec4(1);" |
| 288 | "}", |
| 289 | *SkSL::ShaderCapsFactory::Default(), |
| 290 | { |
| 291 | "GR_DECLARE_FRAGMENT_PROCESSOR_TEST;\n" |
| 292 | " fields section typedef GrFragmentProcessor INHERITED;" |
| 293 | }, |
| 294 | {}); |
| 295 | test(r, |
| 296 | "@make { make section }" |
| 297 | "void main() {" |
| 298 | "sk_OutColor = vec4(1);" |
| 299 | "}", |
| 300 | *SkSL::ShaderCapsFactory::Default(), |
| 301 | { |
| 302 | "public:\n" |
| 303 | " make section" |
| 304 | }, |
| 305 | {}); |
| 306 | test(r, |
| 307 | "uniform float calculated;" |
| 308 | "in float provided;" |
| 309 | "@setData(varName) { varName.set1f(calculated, provided * 2); }" |
| 310 | "void main() {" |
| 311 | "sk_OutColor = vec4(1);" |
| 312 | "}", |
| 313 | *SkSL::ShaderCapsFactory::Default(), |
| 314 | {}, |
| 315 | { |
| 316 | "void onSetData(const GrGLSLProgramDataManager& varName, " |
| 317 | "const GrFragmentProcessor& _proc) override {\n", |
| 318 | "UniformHandle& calculated = fCalculatedVar;", |
| 319 | "auto provided = _outer.provided();", |
| 320 | "varName.set1f(calculated, provided * 2);" |
| 321 | }); |
| 322 | test(r, |
| 323 | "@test(testDataName) { testDataName section }" |
| 324 | "void main() {" |
| 325 | "sk_OutColor = vec4(1);" |
| 326 | "}", |
| 327 | *SkSL::ShaderCapsFactory::Default(), |
| 328 | {}, |
| 329 | { |
| 330 | "#if GR_TEST_UTILS\n" |
| 331 | "sk_sp<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n" |
| 332 | " testDataName section }\n" |
| 333 | "#endif" |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | DEF_TEST(SkSLFPColorSpaceXform, r) { |
| 338 | test(r, |
| 339 | "in uniform sampler2D image;" |
| 340 | "in uniform colorSpaceXform colorXform;" |
| 341 | "void main() {" |
| 342 | "sk_OutColor = sk_InColor * texture(image, vec2(0, 0), colorXform);" |
| 343 | "}", |
| 344 | *SkSL::ShaderCapsFactory::Default(), |
| 345 | { |
| 346 | "sk_sp<GrColorSpaceXform> colorXform() const { return fColorXform; }", |
| 347 | "GrTest(sk_sp<GrTextureProxy> image, sk_sp<GrColorSpaceXform> colorXform)", |
| 348 | "this->addTextureSampler(&fImage);", |
| 349 | "sk_sp<GrColorSpaceXform> fColorXform;" |
| 350 | }, |
| 351 | { |
| 352 | "fragBuilder->codeAppendf(\"vec4 _tmp0;\\n%s = %s * " |
| 353 | "(_tmp0 = texture(%s, vec2(0.0, 0.0)) , %s != mat4(1.0) ? " |
| 354 | "vec4(clamp((%s * vec4(_tmp0.xyz, 1.0)).xyz, 0.0, _tmp0.w), _tmp0.w) : " |
| 355 | "_tmp0);\\n\", args.fOutputColor, args.fInputColor ? args.fInputColor : " |
| 356 | "\"vec4(1)\", fragBuilder->getProgramBuilder()->samplerVariable(" |
| 357 | "args.fTexSamplers[0]).c_str(), fColorSpaceHelper.isValid() ? " |
| 358 | "args.fUniformHandler->getUniformCStr(fColorSpaceHelper.gamutXformUniform()) : " |
| 359 | "\"mat4(1.0)\", fColorSpaceHelper.isValid() ? " |
| 360 | "args.fUniformHandler->getUniformCStr(fColorSpaceHelper.gamutXformUniform()) : " |
| 361 | "\"mat4(1.0)\");" |
| 362 | }); |
| 363 | } |
| 364 | |
| 365 | DEF_TEST(SkSLFPTransformedCoords, r) { |
| 366 | test(r, |
| 367 | "void main() {" |
| 368 | "sk_OutColor = vec4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);" |
| 369 | "}", |
| 370 | *SkSL::ShaderCapsFactory::Default(), |
| 371 | {}, |
| 372 | { |
| 373 | "SkSL::String sk_TransformedCoords2D_0 = " |
| 374 | "fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);", |
| 375 | "fragBuilder->codeAppendf(\"%s = vec4(%s, %s);\\n\", args.fOutputColor, " |
| 376 | "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());" |
| 377 | }); |
| 378 | |
| 379 | } |
| 380 | |
| 381 | DEF_TEST(SkSLFPLayoutWhen, r) { |
| 382 | test(r, |
| 383 | "layout(when=someExpression(someOtherExpression())) uniform float sometimes;" |
| 384 | "void main() {" |
| 385 | "}", |
| 386 | *SkSL::ShaderCapsFactory::Default(), |
| 387 | {}, |
| 388 | { |
| 389 | "if (someExpression(someOtherExpression())) {\n" |
| 390 | " fSometimesVar = args.fUniformHandler->addUniform" |
| 391 | }); |
| 392 | |
| 393 | } |
| 394 | |
| 395 | #endif |