Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkSLCompiler.h" |
| 9 | |
| 10 | #include "Test.h" |
| 11 | |
| 12 | #if SK_SUPPORT_GPU |
| 13 | |
| 14 | static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, |
| 15 | std::vector<const char*> expectedH, std::vector<const char*> expectedCPP) { |
| 16 | SkSL::Program::Settings settings; |
| 17 | settings.fCaps = ∩︀ |
| 18 | SkSL::Compiler compiler; |
| 19 | SkSL::StringStream output; |
| 20 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
| 21 | SkSL::Program::kFragmentProcessor_Kind, |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 22 | SkSL::String(src), |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 23 | settings); |
| 24 | if (!program) { |
| 25 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 26 | return; |
| 27 | } |
| 28 | REPORTER_ASSERT(r, program); |
| 29 | bool success = compiler.toH(*program, "Test", output); |
| 30 | if (!success) { |
| 31 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 32 | } |
| 33 | REPORTER_ASSERT(r, success); |
| 34 | if (success) { |
| 35 | for (const char* expected : expectedH) { |
| 36 | bool found = strstr(output.str().c_str(), expected); |
| 37 | if (!found) { |
| 38 | SkDebugf("HEADER MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
| 39 | expected, output.str().c_str()); |
| 40 | } |
| 41 | REPORTER_ASSERT(r, found); |
| 42 | } |
| 43 | } |
| 44 | output.reset(); |
| 45 | success = compiler.toCPP(*program, "Test", output); |
| 46 | if (!success) { |
| 47 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 48 | } |
| 49 | REPORTER_ASSERT(r, success); |
| 50 | if (success) { |
| 51 | for (const char* expected : expectedCPP) { |
| 52 | bool found = strstr(output.str().c_str(), expected); |
| 53 | if (!found) { |
| 54 | SkDebugf("CPP MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
| 55 | expected, output.str().c_str()); |
| 56 | } |
| 57 | REPORTER_ASSERT(r, found); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | DEF_TEST(SkSLFPHelloWorld, r) { |
| 63 | test(r, |
| 64 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 65 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 66 | "}", |
| 67 | *SkSL::ShaderCapsFactory::Default(), |
| 68 | { |
| 69 | "/*\n" |
| 70 | " * Copyright 2017 Google Inc.\n" |
| 71 | " *\n" |
| 72 | " * Use of this source code is governed by a BSD-style license that can be\n" |
| 73 | " * found in the LICENSE file.\n" |
| 74 | " */\n" |
| 75 | "\n" |
| 76 | "/*\n" |
| 77 | " * This file was autogenerated from GrTest.fp; do not modify.\n" |
| 78 | " */\n" |
| 79 | "#ifndef GrTest_DEFINED\n" |
| 80 | "#define GrTest_DEFINED\n" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 81 | "#include \"SkTypes.h\"\n" |
| 82 | "#if SK_SUPPORT_GPU\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 83 | "#include \"GrFragmentProcessor.h\"\n" |
| 84 | "#include \"GrCoordTransform.h\"\n" |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 85 | "#include \"GrColorSpaceXform.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 86 | "class GrTest : public GrFragmentProcessor {\n" |
| 87 | "public:\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 88 | " static std::unique_ptr<GrFragmentProcessor> Make() {\n" |
| 89 | " return std::unique_ptr<GrFragmentProcessor>(new GrTest());\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 90 | " }\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 91 | " GrTest(const GrTest& src);\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 92 | " std::unique_ptr<GrFragmentProcessor> clone() const override;\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 93 | " const char* name() const override { return \"Test\"; }\n" |
| 94 | "private:\n" |
| 95 | " GrTest()\n" |
| 96 | " : INHERITED(kNone_OptimizationFlags) {\n" |
| 97 | " this->initClassID<GrTest>();\n" |
| 98 | " }\n" |
| 99 | " GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;\n" |
| 100 | " void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) " |
| 101 | "const override;\n" |
| 102 | " bool onIsEqual(const GrFragmentProcessor&) const override;\n" |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 103 | " GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 104 | " typedef GrFragmentProcessor INHERITED;\n" |
| 105 | "};\n" |
| 106 | "#endif\n" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 107 | "#endif\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 108 | }, |
| 109 | { |
| 110 | "/*\n" |
| 111 | " * Copyright 2017 Google Inc.\n" |
| 112 | " *\n" |
| 113 | " * Use of this source code is governed by a BSD-style license that can be\n" |
| 114 | " * found in the LICENSE file.\n" |
| 115 | " */\n" |
| 116 | "\n" |
| 117 | "/*\n" |
| 118 | " * This file was autogenerated from GrTest.fp; do not modify.\n" |
| 119 | " */\n" |
| 120 | "#include \"GrTest.h\"\n" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 121 | "#if SK_SUPPORT_GPU\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 122 | "#include \"glsl/GrGLSLColorSpaceXformHelper.h\"\n" |
| 123 | "#include \"glsl/GrGLSLFragmentProcessor.h\"\n" |
| 124 | "#include \"glsl/GrGLSLFragmentShaderBuilder.h\"\n" |
| 125 | "#include \"glsl/GrGLSLProgramBuilder.h\"\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 126 | "#include \"SkSLCPP.h\"\n" |
| 127 | "#include \"SkSLUtil.h\"\n" |
| 128 | "class GrGLSLTest : public GrGLSLFragmentProcessor {\n" |
| 129 | "public:\n" |
| 130 | " GrGLSLTest() {}\n" |
| 131 | " void emitCode(EmitArgs& args) override {\n" |
| 132 | " GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;\n" |
| 133 | " const GrTest& _outer = args.fFp.cast<GrTest>();\n" |
| 134 | " (void) _outer;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 135 | " fragBuilder->codeAppendf(\"%s = half4(1.0);\\n\", args.fOutputColor);\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 136 | " }\n" |
| 137 | "private:\n" |
| 138 | " void onSetData(const GrGLSLProgramDataManager& pdman, " |
| 139 | "const GrFragmentProcessor& _proc) override {\n" |
| 140 | " }\n" |
| 141 | "};\n" |
| 142 | "GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const {\n" |
| 143 | " return new GrGLSLTest();\n" |
| 144 | "}\n" |
| 145 | "void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, " |
| 146 | "GrProcessorKeyBuilder* b) const {\n" |
| 147 | "}\n" |
| 148 | "bool GrTest::onIsEqual(const GrFragmentProcessor& other) const {\n" |
| 149 | " const GrTest& that = other.cast<GrTest>();\n" |
| 150 | " (void) that;\n" |
| 151 | " return true;\n" |
| 152 | "}\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 153 | "GrTest::GrTest(const GrTest& src)\n" |
| 154 | ": INHERITED(src.optimizationFlags()) {\n" |
| 155 | " this->initClassID<GrTest>();\n" |
| 156 | "}\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 157 | "std::unique_ptr<GrFragmentProcessor> GrTest::clone() const {\n" |
| 158 | " return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this));\n" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 159 | "}\n" |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 160 | "#endif\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 161 | }); |
| 162 | } |
| 163 | |
| 164 | DEF_TEST(SkSLFPInput, r) { |
| 165 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 166 | "in half2 point;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 167 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 168 | "sk_OutColor = half4(point, point);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 169 | "}", |
| 170 | *SkSL::ShaderCapsFactory::Default(), |
| 171 | { |
| 172 | "SkPoint point() const { return fPoint; }", |
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)", |
| 176 | ", fPoint(point)" |
| 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 | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 180 | "args.fOutputColor, _outer.point().fX, _outer.point().fY, " |
| 181 | "_outer.point().fX, _outer.point().fY);", |
| 182 | "if (fPoint != that.fPoint) return false;" |
| 183 | }); |
| 184 | } |
| 185 | |
| 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 | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 197 | "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 198 | "kDefault_GrSLPrecision, \"color\");", |
| 199 | }); |
| 200 | } |
| 201 | |
| 202 | DEF_TEST(SkSLFPInUniform, r) { |
| 203 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 204 | "in uniform half4 color;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 205 | "void main() {" |
| 206 | "sk_OutColor = color;" |
| 207 | "}", |
| 208 | *SkSL::ShaderCapsFactory::Default(), |
| 209 | { |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 210 | "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 211 | }, |
| 212 | { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 213 | "fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType, " |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 214 | "kDefault_GrSLPrecision, \"color\");", |
| 215 | "const SkRect colorValue = _outer.color();", |
Ethan Nicholas | ee33873 | 2017-07-17 15:13:45 -0400 | [diff] [blame] | 216 | "pdman.set4fv(fColorVar, 1, (float*) &colorValue);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 217 | }); |
| 218 | } |
| 219 | |
| 220 | DEF_TEST(SkSLFPSections, r) { |
| 221 | test(r, |
| 222 | "@header { header section }" |
| 223 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 224 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 225 | "}", |
| 226 | *SkSL::ShaderCapsFactory::Default(), |
| 227 | { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 228 | "#if SK_SUPPORT_GPU\n header section" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 229 | }, |
| 230 | {}); |
| 231 | test(r, |
| 232 | "@class { class section }" |
| 233 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 234 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 235 | "}", |
| 236 | *SkSL::ShaderCapsFactory::Default(), |
| 237 | { |
| 238 | "class GrTest : public GrFragmentProcessor {\n" |
| 239 | "public:\n" |
| 240 | " class section" |
| 241 | }, |
| 242 | {}); |
| 243 | test(r, |
| 244 | "@cpp { cpp section }" |
| 245 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 246 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 247 | "}", |
| 248 | *SkSL::ShaderCapsFactory::Default(), |
| 249 | {}, |
| 250 | {"cpp section"}); |
| 251 | test(r, |
| 252 | "@constructorParams { int x, float y, std::vector<float> z }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 253 | "in highfloat w;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 254 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 255 | "sk_OutColor = highfloat4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 256 | "}", |
| 257 | *SkSL::ShaderCapsFactory::Default(), |
| 258 | { |
| 259 | "Make(float w, int x, float y, std::vector<float> z )", |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 260 | "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 261 | "GrTest(float w, int x, float y, std::vector<float> z )", |
| 262 | ", fW(w) {" |
| 263 | }, |
| 264 | {}); |
| 265 | test(r, |
| 266 | "@constructor { constructor section }" |
| 267 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 268 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 269 | "}", |
| 270 | *SkSL::ShaderCapsFactory::Default(), |
| 271 | { |
| 272 | "private:\n constructor section" |
| 273 | }, |
| 274 | {}); |
| 275 | test(r, |
| 276 | "@initializers { initializers section }" |
| 277 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 278 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 279 | "}", |
| 280 | *SkSL::ShaderCapsFactory::Default(), |
| 281 | { |
| 282 | ": INHERITED(kNone_OptimizationFlags)\n , initializers section" |
| 283 | }, |
| 284 | {}); |
| 285 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 286 | "half x = 10;" |
| 287 | "@emitCode { fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2); }" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 288 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 289 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 290 | "}", |
| 291 | *SkSL::ShaderCapsFactory::Default(), |
| 292 | {}, |
| 293 | { |
| 294 | "x = 10.0;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 295 | " fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 296 | }); |
| 297 | test(r, |
| 298 | "@fields { fields section }" |
Ethan Nicholas | f57c0d6 | 2017-07-31 11:18:22 -0400 | [diff] [blame] | 299 | "@clone { }" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 300 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 301 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 302 | "}", |
| 303 | *SkSL::ShaderCapsFactory::Default(), |
| 304 | { |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 305 | "GR_DECLARE_FRAGMENT_PROCESSOR_TEST\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 306 | " fields section typedef GrFragmentProcessor INHERITED;" |
| 307 | }, |
| 308 | {}); |
| 309 | test(r, |
| 310 | "@make { make section }" |
| 311 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 312 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 313 | "}", |
| 314 | *SkSL::ShaderCapsFactory::Default(), |
| 315 | { |
| 316 | "public:\n" |
| 317 | " make section" |
| 318 | }, |
| 319 | {}); |
| 320 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 321 | "uniform half calculated;" |
| 322 | "in half provided;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 323 | "@setData(varName) { varName.set1f(calculated, provided * 2); }" |
| 324 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 325 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 326 | "}", |
| 327 | *SkSL::ShaderCapsFactory::Default(), |
| 328 | {}, |
| 329 | { |
| 330 | "void onSetData(const GrGLSLProgramDataManager& varName, " |
| 331 | "const GrFragmentProcessor& _proc) override {\n", |
| 332 | "UniformHandle& calculated = fCalculatedVar;", |
| 333 | "auto provided = _outer.provided();", |
| 334 | "varName.set1f(calculated, provided * 2);" |
| 335 | }); |
| 336 | test(r, |
| 337 | "@test(testDataName) { testDataName section }" |
| 338 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 339 | "sk_OutColor = half4(1);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 340 | "}", |
| 341 | *SkSL::ShaderCapsFactory::Default(), |
| 342 | {}, |
| 343 | { |
| 344 | "#if GR_TEST_UTILS\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 345 | "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 346 | " testDataName section }\n" |
| 347 | "#endif" |
| 348 | }); |
| 349 | } |
| 350 | |
| 351 | DEF_TEST(SkSLFPColorSpaceXform, r) { |
| 352 | test(r, |
| 353 | "in uniform sampler2D image;" |
| 354 | "in uniform colorSpaceXform colorXform;" |
| 355 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 356 | "sk_OutColor = sk_InColor * texture(image, highfloat2(0, 0), colorXform);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 357 | "}", |
| 358 | *SkSL::ShaderCapsFactory::Default(), |
| 359 | { |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 360 | "sk_sp<GrColorSpaceXform> colorXform() const { return fColorXform; }", |
| 361 | "GrTest(sk_sp<GrTextureProxy> image, sk_sp<GrColorSpaceXform> colorXform)", |
| 362 | "this->addTextureSampler(&fImage);", |
| 363 | "sk_sp<GrColorSpaceXform> fColorXform;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 364 | }, |
| 365 | { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 366 | "fragBuilder->codeAppendf(\"half4 _tmpVar1;%s = %s * %stexture(%s, " |
| 367 | "highfloat2(0.0, 0.0)).%s%s;\\n\", args.fOutputColor, args.fInputColor ? args.fInputColor : " |
| 368 | "\"half4(1)\", fColorSpaceHelper.isValid() ? \"(_tmpVar1 = \" : \"\", " |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 369 | "fragBuilder->getProgramBuilder()->samplerVariable(args.fTexSamplers[0]).c_str(), " |
Ethan Nicholas | ceb4d48 | 2017-07-10 15:40:20 -0400 | [diff] [blame] | 370 | "fragBuilder->getProgramBuilder()->samplerSwizzle(args.fTexSamplers[0]).c_str(), " |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 371 | "fColorSpaceHelper.isValid() ? SkStringPrintf(\", half4(clamp((%s * half4(_tmpVar1.rgb, " |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 372 | "1.0)).rgb, 0.0, _tmpVar1.a), _tmpVar1.a))\", args.fUniformHandler->getUniformCStr(" |
| 373 | "fColorSpaceHelper.gamutXformUniform())).c_str() : \"\");" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 374 | }); |
| 375 | } |
| 376 | |
| 377 | DEF_TEST(SkSLFPTransformedCoords, r) { |
| 378 | test(r, |
| 379 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 380 | "sk_OutColor = half4(sk_TransformedCoords2D[0], sk_TransformedCoords2D[0]);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 381 | "}", |
| 382 | *SkSL::ShaderCapsFactory::Default(), |
| 383 | {}, |
| 384 | { |
Brian Osman | 72a37be | 2017-08-15 09:19:53 -0400 | [diff] [blame] | 385 | "SkString sk_TransformedCoords2D_0 = " |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 386 | "fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);", |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 387 | "fragBuilder->codeAppendf(\"%s = half4(%s, %s);\\n\", args.fOutputColor, " |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 388 | "sk_TransformedCoords2D_0.c_str(), sk_TransformedCoords2D_0.c_str());" |
| 389 | }); |
| 390 | |
| 391 | } |
| 392 | |
| 393 | DEF_TEST(SkSLFPLayoutWhen, r) { |
| 394 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame^] | 395 | "layout(when=someExpression(someOtherExpression())) uniform half sometimes;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 396 | "void main() {" |
| 397 | "}", |
| 398 | *SkSL::ShaderCapsFactory::Default(), |
| 399 | {}, |
| 400 | { |
| 401 | "if (someExpression(someOtherExpression())) {\n" |
| 402 | " fSometimesVar = args.fUniformHandler->addUniform" |
| 403 | }); |
| 404 | |
| 405 | } |
| 406 | |
| 407 | #endif |