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 | |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 13 | static void test(skiatest::Reporter* r, const GrShaderCaps& caps, const char* src, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 14 | std::vector<const char*> expectedH, std::vector<const char*> expectedCPP) { |
| 15 | SkSL::Program::Settings settings; |
| 16 | settings.fCaps = ∩︀ |
Ethan Nicholas | e8ad02c | 2020-06-03 16:58:20 -0400 | [diff] [blame] | 17 | settings.fRemoveDeadFunctions = false; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 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) { |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 38 | SkDebugf("HEADER MISMATCH:\nsource:\n%s\n\n" |
| 39 | "header expected:\n'%s'\n\n" |
| 40 | "header received:\n'%s'", |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 41 | src, expected, output.str().c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 42 | } |
| 43 | REPORTER_ASSERT(r, found); |
| 44 | } |
| 45 | } |
| 46 | output.reset(); |
| 47 | success = compiler.toCPP(*program, "Test", output); |
| 48 | if (!success) { |
| 49 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 50 | } |
| 51 | REPORTER_ASSERT(r, success); |
| 52 | if (success) { |
| 53 | for (const char* expected : expectedCPP) { |
| 54 | bool found = strstr(output.str().c_str(), expected); |
| 55 | if (!found) { |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 56 | SkDebugf("CPP MISMATCH:\nsource:\n%s\n\n" |
| 57 | "cpp expected:\n'%s'\n\n" |
| 58 | "cpp received:\n'%s'", |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 59 | src, expected, output.str().c_str()); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 60 | } |
| 61 | REPORTER_ASSERT(r, found); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 66 | static void test_failure(skiatest::Reporter* r, const char* src, const char* error) { |
| 67 | SkSL::Compiler compiler; |
| 68 | SkSL::Program::Settings settings; |
| 69 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default(); |
| 70 | settings.fCaps = caps.get(); |
| 71 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
| 72 | SkSL::Program::kFragmentProcessor_Kind, |
| 73 | SkSL::String(src), |
| 74 | settings); |
| 75 | if (!compiler.errorCount()) { |
| 76 | compiler.optimize(*program); |
| 77 | } |
| 78 | SkSL::String skError(error); |
| 79 | if (compiler.errorText() != skError) { |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 80 | SkDebugf("SKSL ERROR:\n source: %s\n expected: %s received: %s", |
| 81 | src, error, compiler.errorText().c_str()); |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 82 | } |
| 83 | REPORTER_ASSERT(r, compiler.errorText() == skError); |
| 84 | } |
| 85 | |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 86 | DEF_TEST(SkSLFPHelloWorld, r) { |
| 87 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 88 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 89 | R"__SkSL__( |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 90 | /* HELLO WORLD */ |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 91 | void main() { |
| 92 | sk_OutColor = half4(1); |
| 93 | } |
| 94 | )__SkSL__", |
| 95 | /*expectedH=*/{ |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 96 | R"__Header__(/* HELLO WORLD */ |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 97 | |
| 98 | /************************************************************************************************** |
| 99 | *** This file was autogenerated from GrTest.fp; do not modify. |
| 100 | **************************************************************************************************/ |
| 101 | #ifndef GrTest_DEFINED |
| 102 | #define GrTest_DEFINED |
| 103 | |
| 104 | #include "include/core/SkM44.h" |
| 105 | #include "include/core/SkTypes.h" |
| 106 | |
| 107 | |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 108 | #include "src/gpu/GrFragmentProcessor.h" |
| 109 | |
| 110 | class GrTest : public GrFragmentProcessor { |
| 111 | public: |
| 112 | static std::unique_ptr<GrFragmentProcessor> Make() { |
| 113 | return std::unique_ptr<GrFragmentProcessor>(new GrTest()); |
| 114 | } |
| 115 | GrTest(const GrTest& src); |
| 116 | std::unique_ptr<GrFragmentProcessor> clone() const override; |
| 117 | const char* name() const override { return "Test"; } |
| 118 | private: |
| 119 | GrTest() |
| 120 | : INHERITED(kGrTest_ClassID, kNone_OptimizationFlags) { |
| 121 | } |
| 122 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 123 | void onGetGLSLProcessorKey(const GrShaderCaps&,GrProcessorKeyBuilder*) const override; |
| 124 | bool onIsEqual(const GrFragmentProcessor&) const override; |
| 125 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST |
| 126 | typedef GrFragmentProcessor INHERITED; |
| 127 | }; |
| 128 | #endif |
| 129 | )__Header__" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 130 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 131 | /*expectedCPP=*/{ |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 132 | R"__Cpp__(/* HELLO WORLD */ |
| 133 | |
| 134 | /************************************************************************************************** |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 135 | *** This file was autogenerated from GrTest.fp; do not modify. |
| 136 | **************************************************************************************************/ |
| 137 | #include "GrTest.h" |
| 138 | |
| 139 | #include "src/gpu/GrTexture.h" |
| 140 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 141 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 142 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
| 143 | #include "src/sksl/SkSLCPP.h" |
| 144 | #include "src/sksl/SkSLUtil.h" |
| 145 | class GrGLSLTest : public GrGLSLFragmentProcessor { |
| 146 | public: |
| 147 | GrGLSLTest() {} |
| 148 | void emitCode(EmitArgs& args) override { |
| 149 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 150 | const GrTest& _outer = args.fFp.cast<GrTest>(); |
| 151 | (void) _outer; |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 152 | fragBuilder->codeAppendf( |
| 153 | R"SkSL(%s = half4(1.0); |
| 154 | )SkSL" |
| 155 | , args.fOutputColor); |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 156 | } |
| 157 | private: |
| 158 | void onSetData(const GrGLSLProgramDataManager& pdman, const GrFragmentProcessor& _proc) override { |
| 159 | } |
| 160 | }; |
| 161 | GrGLSLFragmentProcessor* GrTest::onCreateGLSLInstance() const { |
| 162 | return new GrGLSLTest(); |
| 163 | } |
| 164 | void GrTest::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
| 165 | } |
| 166 | bool GrTest::onIsEqual(const GrFragmentProcessor& other) const { |
| 167 | const GrTest& that = other.cast<GrTest>(); |
| 168 | (void) that; |
| 169 | return true; |
| 170 | } |
| 171 | GrTest::GrTest(const GrTest& src) |
| 172 | : INHERITED(kGrTest_ClassID, src.optimizationFlags()) { |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 173 | this->cloneAndRegisterAllChildProcessors(src); |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 174 | } |
| 175 | std::unique_ptr<GrFragmentProcessor> GrTest::clone() const { |
| 176 | return std::unique_ptr<GrFragmentProcessor>(new GrTest(*this)); |
| 177 | } |
| 178 | )__Cpp__" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 179 | }); |
| 180 | } |
| 181 | |
| 182 | DEF_TEST(SkSLFPInput, r) { |
| 183 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 184 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 185 | R"__SkSL__( |
| 186 | layout(key) in half2 point; |
| 187 | void main() { |
| 188 | sk_OutColor = half4(point, point); |
| 189 | } |
| 190 | )__SkSL__", |
| 191 | /*expectedH=*/{ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 192 | "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {", |
| 193 | "return std::unique_ptr<GrFragmentProcessor>(new GrTest(point));", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 194 | "GrTest(SkPoint point)", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 195 | ", point(point)" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 196 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 197 | /*expectedCPP=*/{ |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 198 | "fragBuilder->codeAppendf(\n" |
| 199 | "R\"SkSL(%s = half4(half2(%f, %f), half2(%f, %f));\n" |
| 200 | ")SkSL\"\n" |
| 201 | ", args.fOutputColor, _outer.point.fX, _outer.point.fY, _outer.point.fX, _outer.point.fY);", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 202 | "if (point != that.point) return false;" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 203 | }); |
| 204 | } |
| 205 | |
| 206 | DEF_TEST(SkSLFPUniform, r) { |
| 207 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 208 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 209 | R"__SkSL__( |
| 210 | uniform half4 color; |
| 211 | void main() { |
| 212 | sk_OutColor = color; |
| 213 | } |
| 214 | )__SkSL__", |
| 215 | /*expectedH=*/{ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 216 | "static std::unique_ptr<GrFragmentProcessor> Make()" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 217 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 218 | /*expectedCPP=*/{ |
| 219 | "colorVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, " |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 220 | "kHalf4_GrSLType, \"color\");", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 221 | }); |
| 222 | } |
| 223 | |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 224 | // SkSLFPInUniform tests the simplest plumbing case, default type, no tracking |
| 225 | // with a setUniform template that supports inlining the value call with no |
| 226 | // local variable. |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 227 | DEF_TEST(SkSLFPInUniform, r) { |
| 228 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 229 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 230 | R"__SkSL__( |
| 231 | in uniform half4 color; |
| 232 | void main() { |
| 233 | sk_OutColor = color; |
| 234 | } |
| 235 | )__SkSL__", |
| 236 | /*expectedH=*/{ |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 237 | "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 238 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 239 | /*expectedCPP=*/{ |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 240 | "colorVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, " |
| 241 | "kHalf4_GrSLType, \"color\");", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 242 | "pdman.set4fv(colorVar, 1, reinterpret_cast<const float*>(&(_outer.color)));" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 243 | }); |
| 244 | } |
| 245 | |
| 246 | // As above, but tests in uniform's ability to override the default ctype. |
| 247 | DEF_TEST(SkSLFPInUniformCType, r) { |
| 248 | test(r, |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 249 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 250 | R"__SkSL__( |
| 251 | layout(ctype=SkPMColor4f) in uniform half4 color; |
| 252 | void main() { |
| 253 | sk_OutColor = color; |
| 254 | } |
| 255 | )__SkSL__", |
| 256 | /*expectedH=*/{ |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 257 | "static std::unique_ptr<GrFragmentProcessor> Make(SkPMColor4f color) {", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 258 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 259 | /*expectedCPP=*/{ |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 260 | "colorVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, " |
| 261 | "kHalf4_GrSLType, \"color\");", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 262 | "pdman.set4fv(colorVar, 1, (_outer.color).vec());" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 263 | }); |
| 264 | } |
| 265 | |
| 266 | // Add state tracking to the default typed SkRect <-> half4 uniform. But since |
| 267 | // it now has to track state, the value inlining previously done for the |
| 268 | // setUniform call is removed in favor of a local variable. |
| 269 | DEF_TEST(SkSLFPTrackedInUniform, r) { |
| 270 | test(r, |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 271 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 272 | R"__SkSL__( |
| 273 | layout(tracked) in uniform half4 color; |
| 274 | void main() { |
| 275 | sk_OutColor = color; |
| 276 | } |
| 277 | )__SkSL__", |
| 278 | /*expectedH=*/{ |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 279 | "static std::unique_ptr<GrFragmentProcessor> Make(SkRect color) {", |
| 280 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 281 | /*expectedCPP=*/{ |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 282 | "SkRect colorPrev = SkRect::MakeEmpty();", |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 283 | "colorVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, " |
| 284 | "kHalf4_GrSLType, \"color\");", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 285 | "const SkRect& colorValue = _outer.color;", |
| 286 | "if (colorPrev.isEmpty() || colorPrev != colorValue) {", |
| 287 | "colorPrev = colorValue;", |
| 288 | "pdman.set4fv(colorVar, 1, reinterpret_cast<const float*>(&colorValue));" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 289 | }); |
| 290 | } |
| 291 | |
| 292 | // Test the case where the template does not support variable inlining in |
| 293 | // setUniform (i.e. it references the value multiple times). |
| 294 | DEF_TEST(SkSLFPNonInlinedInUniform, r) { |
| 295 | test(r, |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 296 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 297 | R"__SkSL__( |
| 298 | in uniform half2 point; |
| 299 | void main() { |
| 300 | sk_OutColor = half4(point, point); |
| 301 | } |
| 302 | )__SkSL__", |
| 303 | /*expectedH=*/{ |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 304 | "static std::unique_ptr<GrFragmentProcessor> Make(SkPoint point) {", |
| 305 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 306 | /*expectedCPP=*/{ |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 307 | "pointVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, " |
| 308 | "kHalf2_GrSLType, \"point\");", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 309 | "const SkPoint& pointValue = _outer.point;", |
| 310 | "pdman.set2f(pointVar, pointValue.fX, pointValue.fY);" |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 311 | }); |
| 312 | } |
| 313 | |
| 314 | // Test handling conditional uniforms (that use when= in layout), combined with |
| 315 | // state tracking and custom ctypes to really put the code generation through its paces. |
| 316 | DEF_TEST(SkSLFPConditionalInUniform, r) { |
| 317 | test(r, |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 318 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 319 | R"__SkSL__( |
| 320 | layout(key) in bool test; |
| 321 | layout(ctype=SkPMColor4f, tracked, when=test) in uniform half4 color; |
| 322 | void main() { |
| 323 | if (test) { |
| 324 | sk_OutColor = color; |
| 325 | } else { |
| 326 | sk_OutColor = half4(1); |
| 327 | } |
| 328 | } |
| 329 | )__SkSL__", |
| 330 | /*expectedH=*/{ |
Brian Osman | 495993a | 2018-10-16 15:45:55 -0400 | [diff] [blame] | 331 | "static std::unique_ptr<GrFragmentProcessor> Make(bool test, SkPMColor4f color) {", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 332 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 333 | /*expectedCPP=*/{ |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 334 | "SkPMColor4f colorPrev = {SK_FloatNaN, SK_FloatNaN, SK_FloatNaN, SK_FloatNaN}", |
| 335 | "auto test = _outer.test;", |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 336 | "if (test) {", |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 337 | "colorVar = args.fUniformHandler->addUniform(&_outer, kFragment_GrShaderFlag, " |
| 338 | "kHalf4_GrSLType, \"color\");", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 339 | "if (colorVar.isValid()) {", |
| 340 | "const SkPMColor4f& colorValue = _outer.color;", |
| 341 | "if (colorPrev != colorValue) {", |
| 342 | "colorPrev = colorValue;", |
| 343 | "pdman.set4fv(colorVar, 1, colorValue.vec());" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 344 | }); |
| 345 | } |
| 346 | |
| 347 | DEF_TEST(SkSLFPSections, r) { |
| 348 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 349 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 350 | R"__SkSL__( |
| 351 | @header { header section } |
| 352 | void main() { |
| 353 | sk_OutColor = half4(1); |
| 354 | } |
| 355 | )__SkSL__", |
| 356 | /*expectedH=*/{ |
Greg Daniel | 3e8c345 | 2018-04-06 10:37:55 -0400 | [diff] [blame] | 357 | "header section" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 358 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 359 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 360 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 361 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 362 | R"__SkSL__( |
| 363 | @class { class section } |
| 364 | void main() { |
| 365 | sk_OutColor = half4(1); |
| 366 | } |
| 367 | )__SkSL__", |
| 368 | /*expectedH=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 369 | "class GrTest : public GrFragmentProcessor {\n" |
| 370 | "public:\n" |
| 371 | " class section" |
| 372 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 373 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 374 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 375 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 376 | R"__SkSL__( |
| 377 | @cpp { cpp section } |
| 378 | void main() { |
| 379 | sk_OutColor = half4(1); |
| 380 | } |
| 381 | )__SkSL__", |
| 382 | /*expectedH=*/{}, |
| 383 | /*expectedCPP=*/{ |
| 384 | "cpp section" |
| 385 | }); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 386 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 387 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 388 | R"__SkSL__( |
| 389 | @constructorParams { int x, float y, std::vector<float> z } |
| 390 | in float w; |
| 391 | void main() { |
| 392 | sk_OutColor = half4(1); |
| 393 | } |
| 394 | )__SkSL__", |
| 395 | /*expectedH=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 396 | "Make(float w, int x, float y, std::vector<float> z )", |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 397 | "return std::unique_ptr<GrFragmentProcessor>(new GrTest(w, x, y, z));", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 398 | "GrTest(float w, int x, float y, std::vector<float> z )", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 399 | ", w(w) {" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 400 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 401 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 402 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 403 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 404 | R"__SkSL__( |
| 405 | @constructor { constructor section } |
| 406 | void main() { |
| 407 | sk_OutColor = half4(1); |
| 408 | } |
| 409 | )__SkSL__", |
| 410 | /*expectedH=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 411 | "private:\n constructor section" |
| 412 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 413 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 414 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 415 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 416 | R"__SkSL__( |
| 417 | @initializers { initializers section } |
| 418 | void main() { |
| 419 | sk_OutColor = half4(1); |
| 420 | } |
| 421 | )__SkSL__", |
| 422 | /*expectedH=*/{ |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 423 | ": INHERITED(kGrTest_ClassID, kNone_OptimizationFlags)\n , initializers section" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 424 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 425 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 426 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 427 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 428 | R"__SkSL__( |
| 429 | half x = 10; |
| 430 | @emitCode { fragBuilder->codeAppendf("half y = %d\n", x * 2); } |
| 431 | void main() { |
| 432 | sk_OutColor = half4(1); |
| 433 | } |
| 434 | )__SkSL__", |
| 435 | /*expectedH=*/{}, |
| 436 | /*expectedCPP=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 437 | "x = 10.0;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 438 | " fragBuilder->codeAppendf(\"half y = %d\\n\", x * 2);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 439 | }); |
| 440 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 441 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 442 | R"__SkSL__( |
| 443 | @fields { fields section } |
| 444 | @clone { } |
| 445 | void main() { |
| 446 | sk_OutColor = half4(1); |
| 447 | } |
| 448 | )__SkSL__", |
| 449 | /*expectedH=*/{ |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 450 | "const char* name() const override { return \"Test\"; }\n" |
| 451 | " fields section private:" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 452 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 453 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 454 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 455 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 456 | R"__SkSL__( |
| 457 | @make { make section } |
| 458 | void main() { |
| 459 | sk_OutColor = half4(1); |
| 460 | } |
| 461 | )__SkSL__", |
| 462 | /*expectedH=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 463 | "public:\n" |
| 464 | " make section" |
| 465 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 466 | /*expectedCPP=*/{}); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 467 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 468 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 469 | R"__SkSL__( |
| 470 | uniform half calculated; |
| 471 | layout(key) in half provided; |
| 472 | @setData(varName) { varName.set1f(calculated, provided * 2); } |
| 473 | void main() { |
| 474 | sk_OutColor = half4(1); |
| 475 | } |
| 476 | )__SkSL__", |
| 477 | /*expectedH=*/{}, |
| 478 | /*expectedCPP=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 479 | "void onSetData(const GrGLSLProgramDataManager& varName, " |
| 480 | "const GrFragmentProcessor& _proc) override {\n", |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 481 | "UniformHandle& calculated = calculatedVar;", |
| 482 | "auto provided = _outer.provided;", |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 483 | "varName.set1f(calculated, provided * 2);" |
| 484 | }); |
| 485 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 486 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 487 | R"__SkSL__( |
| 488 | @test(testDataName) { testDataName section } |
| 489 | void main() { |
| 490 | sk_OutColor = half4(1); |
| 491 | } |
| 492 | )__SkSL__", |
| 493 | /*expectedH=*/{}, |
| 494 | /*expectedCPP=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 495 | "#if GR_TEST_UTILS\n" |
Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 496 | "std::unique_ptr<GrFragmentProcessor> GrTest::TestCreate(GrProcessorTestData* testDataName) {\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 497 | " testDataName section }\n" |
| 498 | "#endif" |
| 499 | }); |
| 500 | } |
| 501 | |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 502 | DEF_TEST(SkSLFPMainCoords, r) { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 503 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 504 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 505 | R"__SkSL__( |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 506 | void main(float2 coord) { |
| 507 | sk_OutColor = half4(coord, coord); |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 508 | } |
| 509 | )__SkSL__", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 510 | /*expectedH=*/{ |
| 511 | "this->setUsesSampleCoordsDirectly();" |
| 512 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 513 | /*expectedCPP=*/{ |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 514 | "fragBuilder->codeAppendf(\n" |
| 515 | "R\"SkSL(%s = half4(%s, %s);\n" |
| 516 | ")SkSL\"\n" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 517 | ", args.fOutputColor, args.fSampleCoord, args.fSampleCoord);" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 518 | }); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | DEF_TEST(SkSLFPLayoutWhen, r) { |
| 522 | test(r, |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 523 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 524 | R"__SkSL__( |
| 525 | layout(when=someExpression(someOtherExpression())) uniform half sometimes; |
| 526 | void main() { |
| 527 | } |
| 528 | )__SkSL__", |
| 529 | /*expectedH=*/{}, |
| 530 | /*expectedCPP=*/{ |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 531 | "if (someExpression(someOtherExpression())) {\n" |
Ethan Nicholas | bcd51e8 | 2019-04-09 10:40:41 -0400 | [diff] [blame] | 532 | " sometimesVar = args.fUniformHandler->addUniform" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 533 | }); |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 534 | } |
| 535 | |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 536 | DEF_TEST(SkSLFPChildProcessors, r) { |
| 537 | test(r, |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 538 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 539 | R"__SkSL__( |
| 540 | in fragmentProcessor child1; |
| 541 | in fragmentProcessor child2; |
| 542 | void main() { |
| 543 | sk_OutColor = sample(child1) * sample(child2); |
| 544 | } |
| 545 | )__SkSL__", |
| 546 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 547 | "this->registerChild(std::move(child1), SkSL::SampleUsage::PassThrough());", |
| 548 | "this->registerChild(std::move(child2), SkSL::SampleUsage::PassThrough());" |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 549 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 550 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 551 | "SkString _sample149 = this->invokeChild(0, args);\n", |
| 552 | "SkString _sample166 = this->invokeChild(1, args);\n", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 553 | "fragBuilder->codeAppendf(\n" |
| 554 | "R\"SkSL(%s = %s * %s;\n" |
| 555 | ")SkSL\"\n" |
| 556 | ", args.fOutputColor, _sample149.c_str(), _sample166.c_str());", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 557 | "this->cloneAndRegisterAllChildProcessors(src);", |
Ethan Nicholas | c9472af | 2017-10-10 16:30:21 -0400 | [diff] [blame] | 558 | }); |
| 559 | } |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 560 | |
| 561 | DEF_TEST(SkSLFPChildProcessorsWithInput, r) { |
| 562 | test(r, |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 563 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 564 | R"__SkSL__( |
| 565 | in fragmentProcessor child1; |
| 566 | in fragmentProcessor child2; |
| 567 | void main() { |
| 568 | half4 childIn = sk_InColor; |
| 569 | half4 childOut1 = sample(child1, childIn); |
| 570 | half4 childOut2 = sample(child2, childOut1); |
| 571 | sk_OutColor = childOut2; |
| 572 | } |
| 573 | )__SkSL__", |
| 574 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 575 | "this->registerChild(std::move(child1), SkSL::SampleUsage::PassThrough());", |
| 576 | "this->registerChild(std::move(child2), SkSL::SampleUsage::PassThrough());" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 577 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 578 | /*expectedCPP=*/{ |
| 579 | "SkString _input198(\"childIn\");", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 580 | "SkString _sample198 = this->invokeChild(0, _input198.c_str(), args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 581 | "fragBuilder->codeAppendf(\n" |
| 582 | "R\"SkSL(\n" |
| 583 | "half4 childOut1 = %s;)SkSL\"\n" |
| 584 | ", _sample198.c_str());", |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 585 | "SkString _input258(\"childOut1\");", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 586 | "SkString _sample258 = this->invokeChild(1, _input258.c_str(), args);", |
| 587 | "this->cloneAndRegisterAllChildProcessors(src);", |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 588 | }); |
| 589 | } |
| 590 | |
| 591 | DEF_TEST(SkSLFPChildProcessorWithInputExpression, r) { |
| 592 | test(r, |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 593 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 594 | R"__SkSL__( |
| 595 | in fragmentProcessor child; |
| 596 | void main() { |
| 597 | sk_OutColor = sample(child, sk_InColor * half4(0.5)); |
| 598 | } |
| 599 | )__SkSL__", |
| 600 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 601 | "this->registerChild(std::move(child), SkSL::SampleUsage::PassThrough());", |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 602 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 603 | /*expectedCPP=*/{ |
| 604 | "SkString _input106 = SkStringPrintf(\"%s * half4(0.5)\", args.fInputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 605 | "SkString _sample106 = this->invokeChild(0, _input106.c_str(), args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 606 | "fragBuilder->codeAppendf(\n" |
| 607 | "R\"SkSL(%s = %s;\n" |
| 608 | ")SkSL\"\n" |
| 609 | ", args.fOutputColor, _sample106.c_str());", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 610 | "this->cloneAndRegisterAllChildProcessors(src);", |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 611 | }); |
| 612 | } |
| 613 | |
| 614 | DEF_TEST(SkSLFPNestedChildProcessors, r) { |
| 615 | test(r, |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 616 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 617 | R"__SkSL__( |
| 618 | in fragmentProcessor child1; |
| 619 | in fragmentProcessor child2; |
| 620 | void main() { |
| 621 | sk_OutColor = sample(child2, sk_InColor * sample(child1, sk_InColor * half4(0.5))); |
| 622 | } |
| 623 | )__SkSL__", |
| 624 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 625 | "this->registerChild(std::move(child1), SkSL::SampleUsage::PassThrough());", |
| 626 | "this->registerChild(std::move(child2), SkSL::SampleUsage::PassThrough());" |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 627 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 628 | /*expectedCPP=*/{ |
| 629 | "SkString _input177 = SkStringPrintf(\"%s * half4(0.5)\", args.fInputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 630 | "SkString _sample177 = this->invokeChild(0, _input177.c_str(), args);", |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 631 | "SkString _input149 = SkStringPrintf(\"%s * %s\", args.fInputColor, _sample177.c_str());", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 632 | "SkString _sample149 = this->invokeChild(1, _input149.c_str(), args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 633 | "fragBuilder->codeAppendf(\n" |
| 634 | "R\"SkSL(%s = %s;\n" |
| 635 | ")SkSL\"\n" |
| 636 | ", args.fOutputColor, _sample149.c_str());", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 637 | "this->cloneAndRegisterAllChildProcessors(src);", |
Brian Salomon | b243b43 | 2020-02-20 14:41:47 -0500 | [diff] [blame] | 638 | }); |
Michael Ludwig | 92e4c7f | 2018-08-30 16:08:18 -0400 | [diff] [blame] | 639 | } |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 640 | |
| 641 | DEF_TEST(SkSLFPChildFPAndGlobal, r) { |
| 642 | test(r, |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 643 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 644 | R"__SkSL__( |
| 645 | in fragmentProcessor child; |
| 646 | bool hasCap = sk_Caps.externalTextureSupport; |
| 647 | void main() { |
| 648 | if (hasCap) { |
| 649 | sk_OutColor = sample(child, sk_InColor); |
| 650 | } else { |
| 651 | sk_OutColor = half4(1); |
| 652 | } |
| 653 | } |
| 654 | )__SkSL__", |
| 655 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 656 | "this->registerChild(std::move(child), SkSL::SampleUsage::PassThrough());" |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 657 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 658 | /*expectedCPP=*/{ |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 659 | "hasCap = sk_Caps.externalTextureSupport;", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 660 | |
| 661 | "fragBuilder->codeAppendf(\n" |
| 662 | "R\"SkSL(bool hasCap = %s;\n" |
| 663 | "if (hasCap) {)SkSL\"\n" |
| 664 | ", (hasCap ? \"true\" : \"false\"));", |
| 665 | "SkString _input200(args.fInputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 666 | "SkString _sample200 = this->invokeChild(0, _input200.c_str(), args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 667 | "fragBuilder->codeAppendf(\n" |
| 668 | "R\"SkSL(\n" |
| 669 | " %s = %s;\n" |
| 670 | "} else {\n" |
| 671 | " %s = half4(1.0);\n" |
| 672 | "}\n" |
| 673 | ")SkSL\"\n" |
| 674 | ", args.fOutputColor, _sample200.c_str(), args.fOutputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 675 | "this->cloneAndRegisterAllChildProcessors(src);", |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 676 | }); |
Michael Ludwig | 1fc5fbd | 2018-09-07 13:13:06 -0400 | [diff] [blame] | 677 | } |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 678 | |
| 679 | DEF_TEST(SkSLFPChildProcessorInlineFieldAccess, r) { |
| 680 | test(r, |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 681 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 682 | R"__SkSL__( |
| 683 | in fragmentProcessor child; |
| 684 | void main() { |
| 685 | if (child.preservesOpaqueInput) { |
| 686 | sk_OutColor = sample(child, sk_InColor); |
| 687 | } else { |
| 688 | sk_OutColor = half4(1); |
| 689 | } |
| 690 | } |
| 691 | )__SkSL__", |
| 692 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 693 | "this->registerChild(std::move(child), SkSL::SampleUsage::PassThrough());" |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 694 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 695 | /*expectedCPP=*/{ |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 696 | "fragBuilder->codeAppendf(\n" |
| 697 | "R\"SkSL(if (%s) {)SkSL\"\n" |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 698 | ", (_outer.childProcessor(0)->preservesOpaqueInput() ? \"true\" : \"false\"));", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 699 | "SkString _input161(args.fInputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 700 | "SkString _sample161 = this->invokeChild(0, _input161.c_str(), args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 701 | "fragBuilder->codeAppendf(\n" |
| 702 | "R\"SkSL(\n" |
| 703 | " %s = %s;\n" |
| 704 | "} else {\n" |
| 705 | " %s = half4(1.0);\n" |
| 706 | "}\n" |
| 707 | ")SkSL\"\n" |
| 708 | ", args.fOutputColor, _sample161.c_str(), args.fOutputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 709 | "this->cloneAndRegisterAllChildProcessors(src);", |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 710 | }); |
| 711 | } |
| 712 | |
| 713 | DEF_TEST(SkSLFPChildProcessorFieldAccess, r) { |
| 714 | test(r, |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 715 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 716 | R"__SkSL__( |
| 717 | in fragmentProcessor child; |
| 718 | bool opaque = child.preservesOpaqueInput; |
| 719 | void main() { |
| 720 | if (opaque) { |
| 721 | sk_OutColor = sample(child); |
| 722 | } else { |
| 723 | sk_OutColor = half4(0.5); |
| 724 | } |
| 725 | } |
| 726 | )__SkSL__", |
| 727 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 728 | "this->registerChild(std::move(child), SkSL::SampleUsage::PassThrough());" |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 729 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 730 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 731 | "opaque = _outer.childProcessor(0)->preservesOpaqueInput();", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 732 | "fragBuilder->codeAppendf(\n" |
| 733 | "R\"SkSL(bool opaque = %s;\n" |
| 734 | "if (opaque) {)SkSL\"\n" |
| 735 | ", (opaque ? \"true\" : \"false\"));", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 736 | "SkString _sample196 = this->invokeChild(0, args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 737 | "fragBuilder->codeAppendf(\n" |
| 738 | "R\"SkSL(\n" |
| 739 | " %s = %s;\n" |
| 740 | "} else {\n" |
| 741 | " %s = half4(0.5);\n" |
| 742 | "}\n" |
| 743 | ")SkSL\"\n" |
| 744 | ", args.fOutputColor, _sample196.c_str(), args.fOutputColor);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 745 | "this->cloneAndRegisterAllChildProcessors(src);", |
Michael Ludwig | 9094f2c | 2018-09-07 13:44:21 -0400 | [diff] [blame] | 746 | }); |
| 747 | } |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 748 | |
| 749 | DEF_TEST(SkSLFPNullableChildProcessor, r) { |
| 750 | test(r, |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 751 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 752 | R"__SkSL__( |
| 753 | in fragmentProcessor? child; |
| 754 | void main() { |
| 755 | if (child != null) { |
| 756 | sk_OutColor = sample(child); |
| 757 | } else { |
| 758 | sk_OutColor = half4(0.5); |
| 759 | } |
| 760 | } |
| 761 | )__SkSL__", |
| 762 | /*expectedH=*/{}, |
| 763 | /*expectedCPP=*/{ |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 764 | "fragBuilder->codeAppendf(\n" |
| 765 | "R\"SkSL(if (%s) {)SkSL\"\n" |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 766 | ", _outer.childProcessor(0) ? \"true\" : \"false\");", |
| 767 | "SkString _sample149 = this->invokeChild(0, args);", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 768 | "fragBuilder->codeAppendf(\n" |
| 769 | "R\"SkSL(\n" |
| 770 | " %s = %s;\n" |
| 771 | "} else {\n" |
| 772 | " %s = half4(0.5);\n" |
| 773 | "}\n" |
| 774 | ")SkSL\"\n" |
| 775 | ", args.fOutputColor, _sample149.c_str(), args.fOutputColor);", |
Florin Malita | 390f9bd | 2019-03-04 12:25:57 -0500 | [diff] [blame] | 776 | }); |
| 777 | } |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 778 | |
| 779 | DEF_TEST(SkSLFPBadIn, r) { |
| 780 | test_failure(r, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 781 | R"__SkSL__( |
| 782 | in half4 c; |
| 783 | void main() { |
| 784 | sk_OutColor = c; |
| 785 | } |
| 786 | )__SkSL__", |
| 787 | "error: 4: 'in' variable must be either 'uniform' or 'layout(key)', or there must be a " |
Ethan Nicholas | 33c59ed | 2019-08-13 10:21:38 -0400 | [diff] [blame] | 788 | "custom @setData function\n1 error\n"); |
| 789 | } |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 790 | |
| 791 | DEF_TEST(SkSLFPSampleCoords, r) { |
| 792 | test(r, |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 793 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 794 | R"__SkSL__( |
| 795 | in fragmentProcessor child; |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 796 | void main(float2 coord) { |
| 797 | sk_OutColor = sample(child) + sample(child, coord / 2); |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 798 | } |
| 799 | )__SkSL__", |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 800 | /*expectedH=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 801 | "this->registerChild(std::move(child), SkSL::SampleUsage(SkSL::SampleUsage::Kind::kNone, \"\", false, true, true));", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 802 | "this->setUsesSampleCoordsDirectly();" |
Michael Ludwig | 9aba625 | 2020-06-22 14:46:36 -0400 | [diff] [blame] | 803 | }, |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 804 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 805 | "SkString _sample118 = this->invokeChild(0, args);\n", |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 806 | "SkString _coords134 = SkStringPrintf(\"%s / 2.0\", args.fSampleCoord);\n", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 807 | "SkString _sample134 = this->invokeChild(0, args, _coords134.c_str());\n", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 808 | "fragBuilder->codeAppendf(\n" |
| 809 | "R\"SkSL(%s = %s + %s;\n" |
| 810 | ")SkSL\"\n" |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 811 | ", args.fOutputColor, _sample118.c_str(), _sample134.c_str());" |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 812 | }); |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 813 | } |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 814 | |
| 815 | DEF_TEST(SkSLFPFunction, r) { |
| 816 | test(r, |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 817 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 818 | R"__SkSL__( |
| 819 | in fragmentProcessor? child; |
| 820 | half4 flip(half4 c) { return c.abgr; } |
| 821 | void main() { |
| 822 | sk_OutColor = flip(sk_InColor); |
| 823 | } |
| 824 | )__SkSL__", |
| 825 | /*expectedH=*/{}, |
| 826 | /*expectedCPP=*/{ |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 827 | "SkString flip_name;", |
| 828 | "const GrShaderVar flip_args[] = { GrShaderVar(\"c\", kHalf4_GrSLType)};", |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 829 | "fragBuilder->emitFunction(kHalf4_GrSLType, \"flip\", 1, flip_args,\n" |
| 830 | "R\"SkSL(return c.wzyx;\n" |
| 831 | ")SkSL\", &flip_name);", |
| 832 | "fragBuilder->codeAppendf(\n" |
Michael Ludwig | 9861b7c | 2020-06-23 18:37:17 -0400 | [diff] [blame] | 833 | "R\"SkSL(half4 inlineResult0;\n" |
| 834 | "half4 inlineArg1_0 = %s;\n" |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 835 | "{\n" |
Michael Ludwig | 9861b7c | 2020-06-23 18:37:17 -0400 | [diff] [blame] | 836 | " inlineResult0 = inlineArg1_0.wzyx;\n" |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 837 | "}\n" |
Michael Ludwig | 9861b7c | 2020-06-23 18:37:17 -0400 | [diff] [blame] | 838 | "%s = inlineResult0;\n" |
John Stiles | 5081942 | 2020-06-18 13:00:38 -0400 | [diff] [blame] | 839 | "\n" |
| 840 | ")SkSL\"\n" |
| 841 | ", args.fInputColor, args.fOutputColor);" |
Ethan Nicholas | 095f5b4 | 2019-08-30 11:51:41 -0400 | [diff] [blame] | 842 | }); |
| 843 | } |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 844 | |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 845 | DEF_TEST(SkSLFPMatrixSampleConstant, r) { |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 846 | test(r, |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 847 | *SkSL::ShaderCapsFactory::Default(), |
John Stiles | 8818390 | 2020-06-10 16:40:38 -0400 | [diff] [blame] | 848 | R"__SkSL__( |
| 849 | in fragmentProcessor? child; |
| 850 | void main() { |
| 851 | sk_OutColor = sample(child, float3x3(2)); |
| 852 | } |
| 853 | )__SkSL__", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 854 | /*expectedH=*/{ |
| 855 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 856 | "SkSL::SampleUsage::UniformMatrix(\"float3x3(2.0)\", true));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 857 | }, |
| 858 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 859 | "this->invokeChildWithMatrix(0, args)" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 860 | }); |
| 861 | } |
| 862 | |
| 863 | DEF_TEST(SkSLFPMatrixSampleUniform, r) { |
| 864 | test(r, |
| 865 | *SkSL::ShaderCapsFactory::Default(), |
| 866 | R"__SkSL__( |
| 867 | in fragmentProcessor? child; |
| 868 | uniform float3x3 matrix; |
| 869 | void main() { |
| 870 | sk_OutColor = sample(child, matrix); |
| 871 | } |
| 872 | )__SkSL__", |
| 873 | /*expectedH=*/{ |
| 874 | // Since 'matrix' is just a uniform, the generated code can't determine perspective. |
| 875 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 876 | "SkSL::SampleUsage::UniformMatrix(\"matrix\", true));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 877 | }, |
| 878 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 879 | "this->invokeChildWithMatrix(0, args)" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 880 | }); |
| 881 | } |
| 882 | |
| 883 | DEF_TEST(SkSLFPMatrixSampleInUniform, r) { |
| 884 | test(r, |
| 885 | *SkSL::ShaderCapsFactory::Default(), |
| 886 | R"__SkSL__( |
| 887 | in fragmentProcessor? child; |
| 888 | in uniform float3x3 matrix; |
| 889 | void main() { |
| 890 | sk_OutColor = sample(child, matrix); |
| 891 | } |
| 892 | )__SkSL__", |
| 893 | /*expectedH=*/{ |
| 894 | // Since 'matrix' is marked 'in', we can detect perspective at runtime |
| 895 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 896 | "SkSL::SampleUsage::UniformMatrix(\"matrix\", matrix.hasPerspective()));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 897 | }, |
| 898 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 899 | "this->invokeChildWithMatrix(0, args)" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 900 | }); |
| 901 | } |
| 902 | |
| 903 | DEF_TEST(SkSLFPMatrixSampleMultipleInUniforms, r) { |
| 904 | test(r, |
| 905 | *SkSL::ShaderCapsFactory::Default(), |
| 906 | R"__SkSL__( |
| 907 | in fragmentProcessor? child; |
| 908 | in uniform float3x3 matrixA; |
| 909 | in uniform float3x3 matrixB; |
| 910 | void main() { |
| 911 | sk_OutColor = sample(child, matrixA); |
| 912 | sk_OutColor += sample(child, matrixB); |
| 913 | } |
| 914 | )__SkSL__", |
| 915 | /*expectedH=*/{ |
| 916 | // FIXME it would be nice if codegen can produce |
| 917 | // (matrixA.hasPerspective() || matrixB.hasPerspective()) even though it's variable. |
| 918 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 919 | "SkSL::SampleUsage::VariableMatrix(true));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 920 | }, |
| 921 | /*expectedCPP=*/{ |
| 922 | "SkString _matrix191(args.fUniformHandler->getUniformCStr(matrixAVar));", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 923 | "this->invokeChildWithMatrix(0, args, _matrix191.c_str());", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 924 | "SkString _matrix247(args.fUniformHandler->getUniformCStr(matrixBVar));", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 925 | "this->invokeChildWithMatrix(0, args, _matrix247.c_str());" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 926 | }); |
| 927 | } |
| 928 | |
| 929 | DEF_TEST(SkSLFPMatrixSampleConstUniformExpression, r) { |
| 930 | test(r, |
| 931 | *SkSL::ShaderCapsFactory::Default(), |
| 932 | R"__SkSL__( |
| 933 | in fragmentProcessor? child; |
| 934 | uniform float3x3 matrix; |
| 935 | void main() { |
| 936 | sk_OutColor = sample(child, 0.5 * matrix); |
| 937 | } |
| 938 | )__SkSL__", |
| 939 | /*expectedH=*/{ |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 940 | // FIXME: "0.5 * matrix" is a uniform expression and could be lifted to the vertex |
| 941 | // shader, once downstream code is able to properly map 'matrix' within the expression. |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 942 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 943 | "SkSL::SampleUsage::VariableMatrix(true));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 944 | }, |
| 945 | /*expectedCPP=*/{ |
| 946 | "SkString _matrix145 = SkStringPrintf(\"0.5 * %s\", " |
| 947 | "args.fUniformHandler->getUniformCStr(matrixVar));", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 948 | "this->invokeChildWithMatrix(0, args, _matrix145.c_str());" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 949 | }); |
| 950 | } |
| 951 | |
| 952 | DEF_TEST(SkSLFPMatrixSampleConstantAndExplicitly, r) { |
| 953 | test(r, |
| 954 | *SkSL::ShaderCapsFactory::Default(), |
| 955 | R"__SkSL__( |
| 956 | in fragmentProcessor? child; |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 957 | void main(float2 coord) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 958 | sk_OutColor = sample(child, float3x3(0.5)); |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 959 | sk_OutColor = sample(child, coord / 2); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 960 | } |
| 961 | )__SkSL__", |
| 962 | /*expectedH=*/{ |
| 963 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 964 | "SkSL::SampleUsage(SkSL::SampleUsage::Kind::kUniform, \"float3x3(0.5)\", true, true, false));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 965 | }, |
| 966 | /*expectedCPP=*/{ |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 967 | "this->invokeChildWithMatrix(0, args)", |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 968 | "SkString _coords180 = SkStringPrintf(\"%s / 2.0\", args.fSampleCoord);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 969 | "this->invokeChild(0, args, _coords180.c_str())", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 970 | }); |
| 971 | } |
| 972 | |
| 973 | DEF_TEST(SkSLFPMatrixSampleVariableAndExplicitly, r) { |
| 974 | test(r, |
| 975 | *SkSL::ShaderCapsFactory::Default(), |
| 976 | R"__SkSL__( |
| 977 | in fragmentProcessor? child; |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 978 | void main(float2 coord) { |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 979 | float3x3 matrix = float3x3(sk_InColor.a); |
| 980 | sk_OutColor = sample(child, matrix); |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 981 | sk_OutColor = sample(child, coord / 2); |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 982 | } |
| 983 | )__SkSL__", |
| 984 | /*expectedH=*/{ |
| 985 | "this->registerChild(std::move(child), " |
Brian Osman | 1298bc4 | 2020-06-30 13:39:35 -0400 | [diff] [blame] | 986 | "SkSL::SampleUsage(SkSL::SampleUsage::Kind::kVariable, \"\", true, true, false));" |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 987 | }, |
| 988 | /*expectedCPP=*/{ |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 989 | "SkString _matrix178(\"matrix\");", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 990 | "this->invokeChildWithMatrix(0, args, _matrix178.c_str())", |
Michael Ludwig | fc2fdf0 | 2020-06-29 17:20:13 -0400 | [diff] [blame] | 991 | "SkString _coords232 = SkStringPrintf(\"%s / 2.0\", args.fSampleCoord);", |
Brian Osman | 12c5d29 | 2020-07-13 16:11:35 -0400 | [diff] [blame] | 992 | "this->invokeChild(0, args, _coords232.c_str()", |
Michael Ludwig | e88320b | 2020-06-24 09:04:56 -0400 | [diff] [blame] | 993 | }); |
Ethan Nicholas | 5843012 | 2020-04-14 09:54:02 -0400 | [diff] [blame] | 994 | } |