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