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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
Ethan Nicholas | fc99416 | 2019-06-06 10:04:27 -0400 | [diff] [blame] | 9 | #include "src/sksl/SkSLStringStream.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "tests/Test.h" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 12 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 13 | static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, |
| 14 | std::vector<const char*> expectedH, std::vector<const char*> expectedCPP) { |
| 15 | SkSL::Program::Settings settings; |
| 16 | settings.fCaps = ∩︀ |
| 17 | SkSL::Compiler compiler; |
| 18 | SkSL::StringStream output; |
| 19 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
| 20 | SkSL::Program::kFragmentProcessor_Kind, |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 21 | SkSL::String(src), |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 22 | settings); |
| 23 | if (!program) { |
| 24 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 25 | return; |
| 26 | } |
| 27 | REPORTER_ASSERT(r, program); |
| 28 | bool success = compiler.toH(*program, "Test", output); |
| 29 | if (!success) { |
| 30 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 31 | } |
| 32 | REPORTER_ASSERT(r, success); |
| 33 | if (success) { |
| 34 | for (const char* expected : expectedH) { |
| 35 | bool found = strstr(output.str().c_str(), expected); |
| 36 | if (!found) { |
| 37 | SkDebugf("HEADER MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
| 38 | expected, output.str().c_str()); |
| 39 | } |
| 40 | REPORTER_ASSERT(r, found); |
| 41 | } |
| 42 | } |
| 43 | output.reset(); |
| 44 | success = compiler.toCPP(*program, "Test", output); |
| 45 | if (!success) { |
| 46 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 47 | } |
| 48 | REPORTER_ASSERT(r, success); |
| 49 | if (success) { |
| 50 | for (const char* expected : expectedCPP) { |
| 51 | bool found = strstr(output.str().c_str(), expected); |
| 52 | if (!found) { |
| 53 | SkDebugf("CPP MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
| 54 | expected, output.str().c_str()); |
| 55 | } |
| 56 | REPORTER_ASSERT(r, found); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 61 | static void test_failure(skiatest::Reporter* r, const char* src, const char* error) { |
| 62 | SkSL::Compiler compiler; |
| 63 | SkSL::Program::Settings settings; |
| 64 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 65 | settings.fCaps = caps.get(); |
| 66 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
| 67 | SkSL::Program::kFragmentProcessor_Kind, |
| 68 | SkSL::String(src), |
| 69 | settings); |
| 70 | if (!compiler.errorCount()) { |
| 71 | compiler.optimize(*program); |
| 72 | } |
| 73 | SkSL::String skError(error); |
| 74 | if (compiler.errorText() != skError) { |
| 75 | SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", src, error, |
| 76 | compiler.errorText().c_str()); |
| 77 | } |
| 78 | REPORTER_ASSERT(r, compiler.errorText() == skError); |
| 79 | } |
| 80 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 81 | DEF_TEST(SkSLFPHelloWorld, r) { |
| 82 | test(r, |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 83 | "/* HEADER */" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 84 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 85 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 86 | "}", |
| 87 | *SkSL::ShaderCapsFactory::Default(), |
| 88 | { |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 89 | "/* HEADER */\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 90 | "\n" |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 91 | "/**************************************************************************************************\n" |
| 92 | " *** This file was autogenerated from GrTest.fp; do not modify.\n" |
| 93 | " **************************************************************************************************/\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 94 | "#ifndef GrTest_DEFINED\n" |
| 95 | "#define GrTest_DEFINED\n" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 96 | "#include \"include/core/SkTypes.h\"\n\n" |
| 97 | "#include \"src/gpu/GrCoordTransform.h\"\n" |
| 98 | "#include \"src/gpu/GrFragmentProcessor.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 99 | "class GrTest : public GrFragmentProcessor {\n" |
| 100 | "public:\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 101 | " static std::unique_ptr<GrFragmentProcessor> Make() {\n" |
| 102 | " return std::unique_ptr<GrFragmentProcessor>(new GrTest());\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 103 | " }\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 104 | " GrTest(const GrTest& src);\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 105 | " std::unique_ptr<GrFragmentProcessor> clone() const override;\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 106 | " const char* name() const override { return \"Test\"; }\n" |
| 107 | "private:\n" |
| 108 | " GrTest()\n" |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 109 | " : INHERITED(kGrTest_ClassID, kNone_OptimizationFlags) {\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 110 | " }\n" |
| 111 | " GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n" |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 112 | " void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) const " |
| 113 | "override;\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 114 | " bool onIsEqual(const GrFragmentProcessor&) const override;\n" |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 115 | " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 116 | " typedef GrFragmentProcessor INHERITED;\n" |
| 117 | "};\n" |
| 118 | "#endif\n" |
| 119 | }, |
| 120 | { |
Ethan Nicholas | 130fb3f | 2018-02-01 12:14:34 -0500 | [diff] [blame] | 121 | "/**************************************************************************************************\n" |
| 122 | " *** This file was autogenerated from GrTest.fp; do not modify.\n" |
| 123 | " **************************************************************************************************/\n" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 124 | "#include \"GrTest.h\"\n\n" |
| 125 | "#include \"include/gpu/GrTexture.h\"\n" |
| 126 | "#include \"src/gpu/glsl/GrGLSLFragmentProcessor.h\"\n" |
| 127 | "#include \"src/gpu/glsl/GrGLSLFragmentShaderBuilder.h\"\n" |
| 128 | "#include \"src/gpu/glsl/GrGLSLProgramBuilder.h\"\n" |
| 129 | "#include \"src/sksl/SkSLCPP.h\"\n" |
| 130 | "#include \"src/sksl/SkSLUtil.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 131 | "class GrGLSLTest : public GrGLSLFragmentProcessor {\n" |
| 132 | "public:\n" |
| 133 | " GrGLSLTest() {}\n" |
| 134 | " void emitCode(EmitArgs& args) override {\n" |
| 135 | " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n" |
| 136 | " const GrTest& _outer = args.fFp.cast<GrTest>();\n" |
| 137 | " (void) _outer;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 138 | " fragBuilder->codeAppendf(\"%s = half4(1.0);\\n\", args.fOutputColor);\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 139 | " }\n" |
| 140 | "private:\n" |
| 141 | " void onSetData(const GrGLSLProgramDataManager& pdman, " |
| 142 | "const GrFragmentProcessor& _proc) override {\n" |
| 143 | " }\n" |
| 144 | "};\n" |
| 145 | "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n" |
| 146 | " return new GrGLSLTest();\n" |
| 147 | "}\n" |
| 148 | "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, " |
| 149 | "GrProcessorKeyBuilder* b) const {\n" |
| 150 | "}\n" |
| 151 | "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n" |
| 152 | " const GrTest& that = other.cast<GrTest>();\n" |
| 153 | " (void) that;\n" |
| 154 | " return true;\n" |
| 155 | "}\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 156 | "GrTest::GrTest(const GrTest& src)\n" |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 157 | ": INHERITED(kGrTest_ClassID, src.optimizationFlags()) {\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 158 | "}\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 159 | "std::unique_ptr<GrFragmentProcessor> GrTest::clone() const {\n" |
| 160 | " return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this));\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 161 | "}\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 162 | }); |
| 163 | } |
| 164 | |
| 165 | DEF_TEST(SkSLFPInput, r) { |
| 166 | test(r, |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 167 | "layout(key) in half2 point;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 168 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 169 | "sk_OutColor = half4(point, point);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 170 | "}", |
| 171 | *SkSL::ShaderCapsFactory::Default(), |
| 172 | { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 173 | "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {", |
| 174 | "return std::unique_ptr<GrFragmentProcessor>(new GrTest(point));", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 175 | "GrTest(SkPoint point)", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 176 | ", point(point)" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 177 | }, |
| 178 | { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 179 | "fragBuilder->codeAppendf(\"%s = half4(half2(%f, %f), half2(%f, %f));\\n\", " |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 180 | "args.fOutputColor, _outer.point.fX, _outer.point.fY, " |
| 181 | "_outer.point.fX, _outer.point.fY);", |
| 182 | "if (point != that.point) return false;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 183 | }); |
| 184 | } |
| 185 | |
| 186 | DEF_TEST(SkSLFPUniform, r) { |
| 187 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 188 | "uniform half4 color;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 189 | "void main() {" |
| 190 | "sk_OutColor = color;" |
| 191 | "}", |
| 192 | *SkSL::ShaderCapsFactory::Default(), |
| 193 | { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 194 | "static std::unique_ptr<GrFragmentProcessor> Make()" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 195 | }, |
| 196 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 197 | "colorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
| 198 | "\"color\");", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 199 | }); |
| 200 | } |
| 201 | |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 202 | // SkSLFPInUniform tests the simplest plumbing case, default type, no tracking |
| 203 | // with a setUniform template that supports inlining the value call with no |
| 204 | // local variable. |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 205 | DEF_TEST(SkSLFPInUniform, r) { |
| 206 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 207 | "in uniform half4 color;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 208 | "void main() {" |
| 209 | "sk_OutColor = color;" |
| 210 | "}", |
| 211 | *SkSL::ShaderCapsFactory::Default(), |
| 212 | { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 213 | "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 214 | }, |
| 215 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 216 | "colorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
| 217 | "\"color\");", |
| 218 | "pdman.set4fv(colorVar, 1, reinterpret_cast<const float*>(&(_outer.color)));" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 219 | }); |
| 220 | } |
| 221 | |
| 222 | // As above, but tests in uniform's ability to override the default ctype. |
| 223 | DEF_TEST(SkSLFPInUniformCType, r) { |
| 224 | test(r, |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 225 | "layout(ctype=SkPMColor4f) in uniform half4 color;" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 226 | "void main() {" |
| 227 | "sk_OutColor = color;" |
| 228 | "}", |
| 229 | *SkSL::ShaderCapsFactory::Default(), |
| 230 | { |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 231 | "static std::unique_ptr<GrFragmentProcessor> Make(SkPMColor4f color) {", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 232 | }, |
| 233 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 234 | "colorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
| 235 | "\"color\");", |
| 236 | "pdman.set4fv(colorVar, 1, (_outer.color).vec());" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 237 | }); |
| 238 | } |
| 239 | |
| 240 | // Add state tracking to the default typed SkRect <-> half4 uniform. But since |
| 241 | // it now has to track state, the value inlining previously done for the |
| 242 | // setUniform call is removed in favor of a local variable. |
| 243 | DEF_TEST(SkSLFPTrackedInUniform, r) { |
| 244 | test(r, |
| 245 | "layout(tracked) in uniform half4 color;" |
| 246 | "void main() {" |
| 247 | "sk_OutColor = color;" |
| 248 | "}", |
| 249 | *SkSL::ShaderCapsFactory::Default(), |
| 250 | { |
| 251 | "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {", |
| 252 | }, |
| 253 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 254 | "SkRect colorPrev = SkRect::MakeEmpty();", |
| 255 | "colorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
| 256 | "\"color\");", |
| 257 | "const SkRect& colorValue = _outer.color;", |
| 258 | "if (colorPrev.isEmpty() || colorPrev != colorValue) {", |
| 259 | "colorPrev = colorValue;", |
| 260 | "pdman.set4fv(colorVar, 1, reinterpret_cast<const float*>(&colorValue));" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 261 | }); |
| 262 | } |
| 263 | |
| 264 | // Test the case where the template does not support variable inlining in |
| 265 | // setUniform (i.e. it references the value multiple times). |
| 266 | DEF_TEST(SkSLFPNonInlinedInUniform, r) { |
| 267 | test(r, |
| 268 | "in uniform half2 point;" |
| 269 | "void main() {" |
| 270 | "sk_OutColor = half4(point, point);" |
| 271 | "}", |
| 272 | *SkSL::ShaderCapsFactory::Default(), |
| 273 | { |
| 274 | "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {", |
| 275 | }, |
| 276 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 277 | "pointVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf2_GrSLType, " |
| 278 | "\"point\");", |
| 279 | "const SkPoint& pointValue = _outer.point;", |
| 280 | "pdman.set2f(pointVar, pointValue.fX, pointValue.fY);" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 281 | }); |
| 282 | } |
| 283 | |
| 284 | // Test handling conditional uniforms (that use when= in layout), combined with |
| 285 | // state tracking and custom ctypes to really put the code generation through its paces. |
| 286 | DEF_TEST(SkSLFPConditionalInUniform, r) { |
| 287 | test(r, |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 288 | "layout(key) in bool test;" |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 289 | "layout(ctype=SkPMColor4f, tracked, when=test) in uniform half4 color;" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 290 | "void main() {" |
| 291 | " if (test) {" |
| 292 | " sk_OutColor = color;" |
| 293 | " } else {" |
| 294 | " sk_OutColor = half4(1);" |
| 295 | " }" |
| 296 | "}", |
| 297 | *SkSL::ShaderCapsFactory::Default(), |
| 298 | { |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 299 | "static std::unique_ptr<GrFragmentProcessor> Make(bool test, SkPMColor4f color) {", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 300 | }, |
| 301 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 302 | "SkPMColor4f colorPrev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}", |
| 303 | "auto test = _outer.test;", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 304 | "if (test) {", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 305 | "colorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
| 306 | "\"color\");", |
| 307 | "if (colorVar.isValid()) {", |
| 308 | "const SkPMColor4f& colorValue = _outer.color;", |
| 309 | "if (colorPrev != colorValue) {", |
| 310 | "colorPrev = colorValue;", |
| 311 | "pdman.set4fv(colorVar, 1, colorValue.vec());" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 312 | }); |
| 313 | } |
| 314 | |
| 315 | DEF_TEST(SkSLFPSections, r) { |
| 316 | test(r, |
| 317 | "@header { header section }" |
| 318 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 319 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 320 | "}", |
| 321 | *SkSL::ShaderCapsFactory::Default(), |
| 322 | { |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 323 | "header section" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 324 | }, |
| 325 | {}); |
| 326 | test(r, |
| 327 | "@class { class section }" |
| 328 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 329 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 330 | "}", |
| 331 | *SkSL::ShaderCapsFactory::Default(), |
| 332 | { |
| 333 | "class GrTest : public GrFragmentProcessor {\n" |
| 334 | "public:\n" |
| 335 | " class section" |
| 336 | }, |
| 337 | {}); |
| 338 | test(r, |
| 339 | "@cpp { cpp section }" |
| 340 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 341 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 342 | "}", |
| 343 | *SkSL::ShaderCapsFactory::Default(), |
| 344 | {}, |
| 345 | {"cpp section"}); |
| 346 | test(r, |
| 347 | "@constructorParams { int x, float y, std::vector<float> z }" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 348 | "in float w;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 349 | "void main() {" |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 350 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 351 | "}", |
| 352 | *SkSL::ShaderCapsFactory::Default(), |
| 353 | { |
| 354 | "Make(float w, int x, float y, std::vector<float> z )", |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 355 | "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 356 | "GrTest(float w, int x, float y, std::vector<float> z )", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 357 | ", w(w) {" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 358 | }, |
| 359 | {}); |
| 360 | test(r, |
| 361 | "@constructor { constructor section }" |
| 362 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 363 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 364 | "}", |
| 365 | *SkSL::ShaderCapsFactory::Default(), |
| 366 | { |
| 367 | "private:\n constructor section" |
| 368 | }, |
| 369 | {}); |
| 370 | test(r, |
| 371 | "@initializers { initializers section }" |
| 372 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 373 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 374 | "}", |
| 375 | *SkSL::ShaderCapsFactory::Default(), |
| 376 | { |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 377 | ": INHERITED(kGrTest_ClassID, kNone_OptimizationFlags)\n , initializers section" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 378 | }, |
| 379 | {}); |
| 380 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 381 | "half x = 10;" |
| 382 | "@emitCode { fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2); }" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 383 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 384 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 385 | "}", |
| 386 | *SkSL::ShaderCapsFactory::Default(), |
| 387 | {}, |
| 388 | { |
| 389 | "x = 10.0;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 390 | " fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 391 | }); |
| 392 | test(r, |
| 393 | "@fields { fields section }" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 394 | "@clone { }" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 395 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 396 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 397 | "}", |
| 398 | *SkSL::ShaderCapsFactory::Default(), |
| 399 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 400 | "const char* name() const override { return \"Test\"; }\n" |
| 401 | " fields section private:" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 402 | }, |
| 403 | {}); |
| 404 | test(r, |
| 405 | "@make { make section }" |
| 406 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 407 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 408 | "}", |
| 409 | *SkSL::ShaderCapsFactory::Default(), |
| 410 | { |
| 411 | "public:\n" |
| 412 | " make section" |
| 413 | }, |
| 414 | {}); |
| 415 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 416 | "uniform half calculated;" |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 417 | "layout(key) in half provided;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 418 | "@setData(varName) { varName.set1f(calculated, provided * 2); }" |
| 419 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 420 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 421 | "}", |
| 422 | *SkSL::ShaderCapsFactory::Default(), |
| 423 | {}, |
| 424 | { |
| 425 | "void onSetData(const GrGLSLProgramDataManager& varName, " |
| 426 | "const GrFragmentProcessor& _proc) override {\n", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 427 | "UniformHandle& calculated = calculatedVar;", |
| 428 | "auto provided = _outer.provided;", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 429 | "varName.set1f(calculated, provided * 2);" |
| 430 | }); |
| 431 | test(r, |
| 432 | "@test(testDataName) { testDataName section }" |
| 433 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 434 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 435 | "}", |
| 436 | *SkSL::ShaderCapsFactory::Default(), |
| 437 | {}, |
| 438 | { |
| 439 | "#if GR_TEST_UTILS\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 440 | "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 441 | " testDataName section }\n" |
| 442 | "#endif" |
| 443 | }); |
| 444 | } |
| 445 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 446 | DEF_TEST(SkSLFPTransformedCoords, r) { |
| 447 | test(r, |
| 448 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 449 | "sk_OutColor = half4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 450 | "}", |
| 451 | *SkSL::ShaderCapsFactory::Default(), |
| 452 | {}, |
| 453 | { |
Brian Osman | 72a37be | 2017-08-15 09:19:53 -0400 | [diff] [blame] | 454 | "SkString sk_TransformedCoords2D_0 = " |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 455 | "fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint);", |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 456 | "fragBuilder->codeAppendf(\"%s = half4(%s, %s);\\n\", args.fOutputColor, " |
Brian Salomon | bf5c0c0 | 2019-11-11 14:55:28 -0500 | [diff] [blame] | 457 | "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 458 | }); |
| 459 | |
| 460 | } |
| 461 | |
| 462 | DEF_TEST(SkSLFPLayoutWhen, r) { |
| 463 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 464 | "layout(when=someExpression(someOtherExpression())) uniform half sometimes;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 465 | "void main() {" |
| 466 | "}", |
| 467 | *SkSL::ShaderCapsFactory::Default(), |
| 468 | {}, |
| 469 | { |
| 470 | "if (someExpression(someOtherExpression())) {\n" |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 471 | " sometimesVar = args.fUniformHandler->addUniform" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 472 | }); |
| 473 | |
| 474 | } |
| 475 | |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 476 | DEF_TEST(SkSLFPChildProcessors, r) { |
| 477 | test(r, |
| 478 | "in fragmentProcessor child1;" |
| 479 | "in fragmentProcessor child2;" |
| 480 | "void main() {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 481 | " sk_OutColor = sample(child1) * sample(child2);" |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 482 | "}", |
| 483 | *SkSL::ShaderCapsFactory::Default(), |
| 484 | { |
| 485 | "this->registerChildProcessor(std::move(child1));", |
| 486 | "this->registerChildProcessor(std::move(child2));" |
| 487 | }, |
| 488 | { |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 489 | "SkString _sample93(\"_sample93\");\n", |
| 490 | "this->invokeChild(_outer.child1_index, &_sample93, args);\n", |
| 491 | "SkString _sample110(\"_sample110\");\n", |
| 492 | "this->invokeChild(_outer.child2_index, &_sample110, args);\n", |
| 493 | "fragBuilder->codeAppendf(\"%s = %s * %s;\\n\", args.fOutputColor, _sample93.c_str(), " |
| 494 | "_sample110.c_str());\n", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 495 | "this->registerChildProcessor(src.childProcessor(child1_index).clone());", |
| 496 | "this->registerChildProcessor(src.childProcessor(child2_index).clone());" |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 497 | }); |
| 498 | } |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 499 | |
| 500 | DEF_TEST(SkSLFPChildProcessorsWithInput, r) { |
| 501 | test(r, |
| 502 | "in fragmentProcessor child1;" |
| 503 | "in fragmentProcessor child2;" |
| 504 | "void main() {" |
| 505 | " half4 childIn = sk_InColor;" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 506 | " half4 childOut1 = sample(child1, childIn);" |
| 507 | " half4 childOut2 = sample(child2, childOut1);" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 508 | " sk_OutColor = childOut2;" |
| 509 | "}", |
| 510 | *SkSL::ShaderCapsFactory::Default(), |
| 511 | { |
| 512 | "this->registerChildProcessor(std::move(child1));", |
| 513 | "this->registerChildProcessor(std::move(child2));" |
| 514 | }, |
| 515 | { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 516 | "SkString _input128(\"childIn\");", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 517 | "SkString _sample128(\"_sample128\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 518 | "this->invokeChild(_outer.child1_index, _input128.c_str(), &_sample128, args);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 519 | "fragBuilder->codeAppendf(\"\\nhalf4 childOut1 = %s;\", _sample128.c_str());", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 520 | "SkString _input174(\"childOut1\");", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 521 | "SkString _sample174(\"_sample174\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 522 | "this->invokeChild(_outer.child2_index, _input174.c_str(), &_sample174, args);", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 523 | "this->registerChildProcessor(src.childProcessor(child1_index).clone());", |
| 524 | "this->registerChildProcessor(src.childProcessor(child2_index).clone());" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 525 | }); |
| 526 | } |
| 527 | |
| 528 | DEF_TEST(SkSLFPChildProcessorWithInputExpression, r) { |
| 529 | test(r, |
| 530 | "in fragmentProcessor child;" |
| 531 | "void main() {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 532 | " sk_OutColor = sample(child, sk_InColor * half4(0.5));" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 533 | "}", |
| 534 | *SkSL::ShaderCapsFactory::Default(), |
| 535 | { |
| 536 | "this->registerChildProcessor(std::move(child));", |
| 537 | }, |
| 538 | { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 539 | "SkString _input64 = SkStringPrintf(\"%s * half4(0.5)\", args.fInputColor);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 540 | "SkString _sample64(\"_sample64\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 541 | "this->invokeChild(_outer.child_index, _input64.c_str(), &_sample64, args);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 542 | "fragBuilder->codeAppendf(\"%s = %s;\\n\", args.fOutputColor, _sample64.c_str());", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 543 | "this->registerChildProcessor(src.childProcessor(child_index).clone());", |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 544 | }); |
| 545 | } |
| 546 | |
| 547 | DEF_TEST(SkSLFPNestedChildProcessors, r) { |
| 548 | test(r, |
| 549 | "in fragmentProcessor child1;" |
| 550 | "in fragmentProcessor child2;" |
| 551 | "void main() {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 552 | " sk_OutColor = sample(child2, sk_InColor * sample(child1, sk_InColor * half4(0.5)));" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 553 | "}", |
| 554 | *SkSL::ShaderCapsFactory::Default(), |
| 555 | { |
| 556 | "this->registerChildProcessor(std::move(child1));", |
| 557 | "this->registerChildProcessor(std::move(child2));" |
| 558 | }, |
| 559 | { |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 560 | "SkString _input121 = SkStringPrintf(\"%s * half4(0.5)\", args.fInputColor);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 561 | "SkString _sample121(\"_sample121\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 562 | "this->invokeChild(_outer.child1_index, _input121.c_str(), &_sample121, args);", |
| 563 | "SkString _input93 = SkStringPrintf(\"%s * %s\", args.fInputColor, _sample121.c_str());", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 564 | "SkString _sample93(\"_sample93\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 565 | "this->invokeChild(_outer.child2_index, _input93.c_str(), &_sample93, args);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 566 | "fragBuilder->codeAppendf(\"%s = %s;\\n\", args.fOutputColor, _sample93.c_str());", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 567 | "this->registerChildProcessor(src.childProcessor(child1_index).clone());", |
| 568 | "this->registerChildProcessor(src.childProcessor(child2_index).clone());" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 569 | }); |
| 570 | } |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 571 | |
| 572 | DEF_TEST(SkSLFPChildFPAndGlobal, r) { |
| 573 | test(r, |
| 574 | "in fragmentProcessor child;" |
| 575 | "bool hasCap = sk_Caps.externalTextureSupport;" |
| 576 | "void main() {" |
| 577 | " if (hasCap) {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 578 | " sk_OutColor = sample(child, sk_InColor);" |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 579 | " } else {" |
| 580 | " sk_OutColor = half4(1);" |
| 581 | " }" |
| 582 | "}", |
| 583 | *SkSL::ShaderCapsFactory::Default(), |
| 584 | { |
| 585 | "this->registerChildProcessor(std::move(child));" |
| 586 | }, |
| 587 | { |
| 588 | "hasCap = sk_Caps.externalTextureSupport;", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 589 | "fragBuilder->codeAppendf(\"bool hasCap = %s;\\nif (hasCap) {\", (hasCap ? \"true\" : " |
| 590 | "\"false\"));", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 591 | "SkString _input130 = SkStringPrintf(\"%s\", args.fInputColor);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 592 | "SkString _sample130(\"_sample130\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 593 | "this->invokeChild(_outer.child_index, _input130.c_str(), &_sample130, args);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 594 | "fragBuilder->codeAppendf(\"\\n %s = %s;\\n} else {\\n %s = half4(1.0);\\n}\\n\"," |
| 595 | " args.fOutputColor, _sample130.c_str(), args.fOutputColor);", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 596 | "this->registerChildProcessor(src.childProcessor(child_index).clone());" |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 597 | }); |
| 598 | } |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 599 | |
| 600 | DEF_TEST(SkSLFPChildProcessorInlineFieldAccess, r) { |
| 601 | test(r, |
| 602 | "in fragmentProcessor child;" |
| 603 | "void main() {" |
| 604 | " if (child.preservesOpaqueInput) {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 605 | " sk_OutColor = sample(child, sk_InColor);" |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 606 | " } else {" |
| 607 | " sk_OutColor = half4(1);" |
| 608 | " }" |
| 609 | "}", |
| 610 | *SkSL::ShaderCapsFactory::Default(), |
| 611 | { |
| 612 | "this->registerChildProcessor(std::move(child));" |
| 613 | }, |
| 614 | { |
| 615 | "fragBuilder->codeAppendf(\"if (%s) {\", " |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 616 | "(_outer.childProcessor(_outer.child_index).preservesOpaqueInput() ? ", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 617 | "SkString _input105 = SkStringPrintf(\"%s\", args.fInputColor);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 618 | "SkString _sample105(\"_sample105\");", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 619 | "this->invokeChild(_outer.child_index, _input105.c_str(), &_sample105, args);", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 620 | "fragBuilder->codeAppendf(\"\\n %s = %s;\\n} else {\\n %s = half4(1.0);\\n}\\n\"," |
| 621 | " args.fOutputColor, _sample105.c_str(), args.fOutputColor);", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 622 | "this->registerChildProcessor(src.childProcessor(child_index).clone());" |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 623 | }); |
| 624 | } |
| 625 | |
| 626 | DEF_TEST(SkSLFPChildProcessorFieldAccess, r) { |
| 627 | test(r, |
| 628 | "in fragmentProcessor child;" |
| 629 | "bool opaque = child.preservesOpaqueInput;" |
| 630 | "void main() {" |
| 631 | " if (opaque) {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 632 | " sk_OutColor = sample(child);" |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 633 | " } else {" |
| 634 | " sk_OutColor = half4(0.5);" |
| 635 | " }" |
| 636 | "}", |
| 637 | *SkSL::ShaderCapsFactory::Default(), |
| 638 | { |
| 639 | "this->registerChildProcessor(std::move(child));" |
| 640 | }, |
| 641 | { |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 642 | "opaque = _outer.childProcessor(_outer.child_index).preservesOpaqueInput();", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 643 | "fragBuilder->codeAppendf(\"bool opaque = %s;\\nif (opaque) {\", (opaque ? \"true\" : " |
| 644 | "\"false\"));", |
| 645 | "SkString _sample126(\"_sample126\");", |
| 646 | "this->invokeChild(_outer.child_index, &_sample126, args);", |
| 647 | "fragBuilder->codeAppendf(\"\\n %s = %s;\\n} else {\\n %s = half4(0.5);\\n}\\n\"," |
| 648 | " args.fOutputColor, _sample126.c_str(), args.fOutputColor);", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 649 | "this->registerChildProcessor(src.childProcessor(child_index).clone());" |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 650 | }); |
| 651 | } |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 652 | |
| 653 | DEF_TEST(SkSLFPNullableChildProcessor, r) { |
| 654 | test(r, |
| 655 | "in fragmentProcessor? child;" |
| 656 | "void main() {" |
| 657 | " if (child != null) {" |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 658 | " sk_OutColor = sample(child);" |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 659 | " } else {" |
| 660 | " sk_OutColor = half4(0.5);" |
| 661 | " }" |
| 662 | "}", |
| 663 | *SkSL::ShaderCapsFactory::Default(), |
| 664 | {}, |
| 665 | { |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 666 | "fragBuilder->codeAppendf(\"if (%s) {\", _outer.child_index >= 0 ? \"true\" : " |
| 667 | "\"false\");", |
| 668 | "SkString _sample93(\"_sample93\");", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 669 | "if (_outer.child_index >= 0) {", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 670 | "this->invokeChild(_outer.child_index, &_sample93, args);", |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 671 | "}", |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 672 | "fragBuilder->codeAppendf(\"\\n %s = %s;\\n} else {\\n %s = half4(0.5);\\n}\\n\"," |
| 673 | " args.fOutputColor, _sample93.c_str(), args.fOutputColor);" |
| 674 | |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 675 | }); |
| 676 | } |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 677 | |
| 678 | DEF_TEST(SkSLFPBadIn, r) { |
| 679 | test_failure(r, |
| 680 | "in half4 c;" |
| 681 | "void main() {" |
| 682 | " sk_OutColor = c;" |
| 683 | "}", |
| 684 | "error: 1: 'in' variable must be either 'uniform' or 'layout(key)', or there must be a " |
| 685 | "custom @setData function\n1 error\n"); |
| 686 | } |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 687 | |
| 688 | DEF_TEST(SkSLFPSampleCoords, r) { |
| 689 | test(r, |
| 690 | "in fragmentProcessor child;" |
| 691 | "@coordTransform { SkMatrix() }" |
| 692 | "void main() {" |
| 693 | " sk_OutColor = sample(child) + sample(child, sk_TransformedCoords2D[0] / 2);" |
| 694 | "}", |
| 695 | *SkSL::ShaderCapsFactory::Default(), |
| 696 | {}, |
| 697 | { |
| 698 | "SkString _sample94(\"_sample94\");\n", |
| 699 | "this->invokeChild(_outer.child_index, &_sample94, args);\n", |
| 700 | "SkString _sample110(\"_sample110\");\n", |
| 701 | "SkString sk_TransformedCoords2D_0 = fragBuilder->ensureCoords2D(" |
| 702 | "args.fTransformedCoords[0].fVaryingPoint);\n", |
| 703 | "SkString _coords110 = SkStringPrintf(\"%s / 2.0\", " |
Brian Salomon | bf5c0c0 | 2019-11-11 14:55:28 -0500 | [diff] [blame] | 704 | "sk_TransformedCoords2D_0.c_str());\n", |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 705 | "this->invokeChild(_outer.child_index, &_sample110, args, _coords110.c_str());\n", |
| 706 | "fragBuilder->codeAppendf(\"%s = %s + %s;\\n\", args.fOutputColor, _sample94.c_str(), " |
| 707 | "_sample110.c_str());\n" |
| 708 | }); |
| 709 | } |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 710 | |
| 711 | DEF_TEST(SkSLFPFunction, r) { |
| 712 | test(r, |
| 713 | "in fragmentProcessor? child;" |
| 714 | "half4 flip(half4 c) { return c.abgr; }" |
| 715 | "void main() {" |
| 716 | " sk_OutColor = flip(sk_InColor);" |
| 717 | "}", |
| 718 | *SkSL::ShaderCapsFactory::Default(), |
| 719 | {}, |
| 720 | { |
| 721 | "SkString flip_name;", |
| 722 | "const GrShaderVar flip_args[] = { GrShaderVar(\"c\", kHalf4_GrSLType)};", |
| 723 | "fragBuilder->emitFunction(kHalf4_GrSLType, \"flip\", 1, flip_args, " |
| 724 | "\"return c.wzyx;\\n\", &flip_name);", |
| 725 | "fragBuilder->codeAppendf(\"%s = %s(%s);\\n\", args.fOutputColor, flip_name.c_str(), " |
| 726 | "args.fInputColor);" |
| 727 | }); |
| 728 | } |