Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkData.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkSurface.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 14 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 16 | #include "src/core/SkColorSpacePriv.h" |
Brian Osman | 4f00982 | 2021-04-28 09:41:06 -0400 | [diff] [blame] | 17 | #include "src/core/SkRuntimeEffectPriv.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 18 | #include "src/core/SkTLazy.h" |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrColor.h" |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrFragmentProcessor.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 21 | #include "tests/Test.h" |
| 22 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 23 | #include <algorithm> |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 24 | #include <thread> |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 25 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 26 | void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 27 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src)); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 28 | REPORTER_ASSERT(r, !effect); |
| 29 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 30 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 31 | expected, errorText.c_str()); |
| 32 | }; |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 33 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 34 | #define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }" |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 35 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 36 | DEF_TEST(SkRuntimeEffectInvalid_FPOnly, r) { |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 37 | // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked). |
| 38 | // Ensure that these fail, and the error messages contain the relevant keyword. |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 39 | test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key"); |
| 40 | test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform"); |
| 41 | test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype"); |
| 42 | test_invalid_effect(r, "in bool Flag; " |
| 43 | "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when"); |
| 44 | test_invalid_effect(r, "layout(tracked) uniform float Input;" EMPTY_MAIN, "tracked"); |
| 45 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 46 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 47 | DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) { |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 48 | // Runtime SkSL supports a limited set of uniform types. No bool, for example: |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 49 | test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 50 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 51 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 52 | DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) { |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 53 | // 'in' variables aren't allowed at all: |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 54 | test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'"); |
| 55 | test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'"); |
| 56 | test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'"); |
| 57 | test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'"); |
| 58 | } |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 59 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 60 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 61 | test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }", |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 62 | "undefined function"); |
| 63 | } |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 64 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 65 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) { |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 66 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 67 | test_invalid_effect(r, "", "main"); |
| 68 | } |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 69 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 70 | DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) { |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 71 | // sk_Caps is an internal system. It should not be visible to runtime effects |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 72 | test_invalid_effect( |
| 73 | r, |
| 74 | "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }", |
| 75 | "unknown identifier 'sk_Caps'"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 76 | } |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 77 | |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 78 | DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) { |
| 79 | auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) { |
| 80 | SkRuntimeEffect::Options opt; |
| 81 | opt.enforceES2Restrictions = false; |
| 82 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt); |
| 83 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
| 84 | }; |
| 85 | |
| 86 | test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type"); |
| 87 | test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN); |
| 88 | } |
| 89 | |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 90 | DEF_TEST(SkRuntimeEffectForColorFilter, r) { |
| 91 | // Tests that the color filter factory rejects or accepts certain SkSL constructs |
| 92 | auto test_valid = [r](const char* sksl) { |
| 93 | auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl)); |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 94 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | auto test_invalid = [r](const char* sksl, const char* expected) { |
| 98 | auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl)); |
| 99 | REPORTER_ASSERT(r, !effect); |
| 100 | REPORTER_ASSERT(r, |
| 101 | errorText.contains(expected), |
| 102 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 103 | expected, |
| 104 | errorText.c_str()); |
| 105 | }; |
| 106 | |
| 107 | // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4 |
| 108 | test_valid("half4 main(half4 c) { return c; }"); |
| 109 | test_valid("float4 main(half4 c) { return c; }"); |
| 110 | test_valid("half4 main(float4 c) { return c; }"); |
| 111 | test_valid("float4 main(float4 c) { return c; }"); |
| 112 | test_valid("vec4 main(half4 c) { return c; }"); |
| 113 | test_valid("half4 main(vec4 c) { return c; }"); |
| 114 | test_valid("vec4 main(vec4 c) { return c; }"); |
| 115 | |
| 116 | // Invalid return types |
| 117 | test_invalid("void main(half4 c) {}", "'main' must return"); |
| 118 | test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return"); |
| 119 | |
| 120 | // Invalid argument types (some are valid as shaders, but not color filters) |
| 121 | test_invalid("half4 main() { return half4(1); }", "'main' parameter"); |
| 122 | test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter"); |
| 123 | test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter"); |
| 124 | |
| 125 | // sk_FragCoord should not be available |
| 126 | test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier"); |
| 127 | |
| 128 | // Sampling a child shader requires that we pass explicit coords |
| 129 | test_valid("uniform shader child;" |
| 130 | "half4 main(half4 c) { return sample(child, c.rg); }"); |
| 131 | |
| 132 | test_invalid( |
| 133 | "uniform shader child;" |
| 134 | "half4 main(half4 c) { return sample(child); }", |
Greg Daniel | c2cca5a | 2021-05-04 13:36:16 +0000 | [diff] [blame] | 135 | "expected 2 arguments"); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 136 | test_invalid( |
| 137 | "uniform shader child;" |
| 138 | "half4 main(half4 c) { return sample(child, float3x3(1)); }", |
Greg Daniel | c2cca5a | 2021-05-04 13:36:16 +0000 | [diff] [blame] | 139 | "expected 'float2'"); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | DEF_TEST(SkRuntimeEffectForShader, r) { |
| 143 | // Tests that the shader factory rejects or accepts certain SkSL constructs |
| 144 | auto test_valid = [r](const char* sksl) { |
| 145 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl)); |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 146 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | auto test_invalid = [r](const char* sksl, const char* expected) { |
| 150 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl)); |
| 151 | REPORTER_ASSERT(r, !effect); |
| 152 | REPORTER_ASSERT(r, |
| 153 | errorText.contains(expected), |
| 154 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 155 | expected, |
| 156 | errorText.c_str()); |
| 157 | }; |
| 158 | |
| 159 | // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature |
| 160 | // Either color can be half4/float4/vec4, but the coords must be float2/vec2 |
| 161 | test_valid("half4 main(float2 p) { return p.xyxy; }"); |
| 162 | test_valid("float4 main(float2 p) { return p.xyxy; }"); |
| 163 | test_valid("vec4 main(float2 p) { return p.xyxy; }"); |
| 164 | test_valid("half4 main(vec2 p) { return p.xyxy; }"); |
| 165 | test_valid("vec4 main(vec2 p) { return p.xyxy; }"); |
| 166 | test_valid("half4 main(float2 p, half4 c) { return c; }"); |
| 167 | test_valid("half4 main(float2 p, float4 c) { return c; }"); |
| 168 | test_valid("half4 main(float2 p, vec4 c) { return c; }"); |
| 169 | test_valid("float4 main(float2 p, half4 c) { return c; }"); |
| 170 | test_valid("vec4 main(float2 p, half4 c) { return c; }"); |
| 171 | test_valid("vec4 main(vec2 p, vec4 c) { return c; }"); |
| 172 | |
| 173 | // Invalid return types |
| 174 | test_invalid("void main(float2 p) {}", "'main' must return"); |
| 175 | test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return"); |
| 176 | |
| 177 | // Invalid argument types (some are valid as color filters, but not shaders) |
| 178 | test_invalid("half4 main() { return half4(1); }", "'main' parameter"); |
| 179 | test_invalid("half4 main(half4 c) { return c; }", "'main' parameter"); |
| 180 | |
| 181 | // sk_FragCoord should be available |
| 182 | test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }"); |
| 183 | |
| 184 | // Sampling a child shader requires that we pass explicit coords |
| 185 | test_valid("uniform shader child;" |
| 186 | "half4 main(float2 p) { return sample(child, p); }"); |
| 187 | |
| 188 | test_invalid( |
| 189 | "uniform shader child;" |
| 190 | "half4 main(float2 p) { return sample(child); }", |
Greg Daniel | c2cca5a | 2021-05-04 13:36:16 +0000 | [diff] [blame] | 191 | "expected 2 arguments"); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 192 | test_invalid( |
| 193 | "uniform shader child;" |
| 194 | "half4 main(float2 p) { return sample(child, float3x3(1)); }", |
Greg Daniel | c2cca5a | 2021-05-04 13:36:16 +0000 | [diff] [blame] | 195 | "expected 'float2'"); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 198 | class TestEffect { |
| 199 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 200 | TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 201 | : fReporter(r), fSurface(std::move(surface)) {} |
| 202 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 203 | void build(const char* src) { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 204 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 205 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 206 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 207 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 208 | return; |
| 209 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 210 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 213 | SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) { |
| 214 | return fBuilder->uniform(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 215 | } |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 216 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 217 | return fBuilder->child(name); |
| 218 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 219 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 220 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 221 | |
| 222 | void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR, |
| 223 | PreTestFn preTestCallback = nullptr) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 224 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 225 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 226 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 230 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 231 | SkPaint paint; |
| 232 | paint.setShader(std::move(shader)); |
| 233 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 234 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 235 | canvas->save(); |
| 236 | if (preTestCallback) { |
| 237 | preTestCallback(canvas, &paint); |
| 238 | } |
| 239 | canvas->drawPaint(paint); |
| 240 | canvas->restore(); |
| 241 | |
| 242 | GrColor actual[4]; |
| 243 | SkImageInfo info = fSurface->imageInfo(); |
| 244 | if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 245 | REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 249 | GrColor expected[4] = {TL, TR, BL, BR}; |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 250 | if (0 != memcmp(actual, expected, sizeof(actual))) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 251 | REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 252 | SkStringPrintf("\n" |
| 253 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 254 | "Got : [ %08x %08x %08x %08x ]\n" |
| 255 | "SkSL:\n%s\n", |
| 256 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 257 | actual[3], fBuilder->effect()->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 261 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 262 | this->test(expected, expected, expected, expected, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 266 | skiatest::Reporter* fReporter; |
| 267 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 268 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 269 | }; |
| 270 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 271 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 272 | // [ Red, Green ] |
| 273 | // [ Blue, White ] |
| 274 | static sk_sp<SkShader> make_RGBW_shader() { |
| 275 | SkBitmap bmp; |
| 276 | bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 277 | SkIRect topLeft = SkIRect::MakeWH(1, 1); |
| 278 | bmp.pixmap().erase(SK_ColorRED, topLeft); |
| 279 | bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0)); |
| 280 | bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1)); |
| 281 | bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1)); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 282 | return bmp.makeShader(SkSamplingOptions()); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 285 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 286 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 287 | sk_sp<SkSurface> surface = rContext |
| 288 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 289 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 290 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 291 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 292 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 293 | using float4 = std::array<float, 4>; |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 294 | using int4 = std::array<int, 4>; |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 295 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 296 | // Local coords |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 297 | effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 298 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 299 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 300 | // Use of a simple uniform. (Draw twice with two values to ensure it's updated). |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 301 | effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 302 | effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 303 | effect.test(0xFFBF4000); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 304 | effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 305 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 306 | |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 307 | // Same, with integer uniforms |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 308 | effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }"); |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 309 | effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF }; |
| 310 | effect.test(0xFFBF4000); |
| 311 | effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F }; |
| 312 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
| 313 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 314 | // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords. |
| 315 | // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to |
| 316 | // make sure we're not saturating unexpectedly. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 317 | effect.build( |
| 318 | "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 319 | effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, |
| 320 | [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); }); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 321 | |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 322 | // Runtime effects should use relaxed precision rules by default |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 323 | effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 324 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 325 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 326 | // ... and support *returning* float4 (aka vec4), not just half4 |
| 327 | effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
| 328 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 329 | effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 330 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 331 | |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 332 | // Mutating coords should work. (skbug.com/10918) |
| 333 | effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }"); |
| 334 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 335 | effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }" |
| 336 | "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }"); |
| 337 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 338 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 339 | // |
| 340 | // Sampling children |
| 341 | // |
| 342 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 343 | // Sampling a null child should return the paint color |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 344 | effect.build("uniform shader child;" |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 345 | "half4 main(float2 p) { return sample(child, p); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 346 | effect.child("child") = nullptr; |
| 347 | effect.test(0xFF00FFFF, |
| 348 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 349 | |
| 350 | sk_sp<SkShader> rgbwShader = make_RGBW_shader(); |
| 351 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 352 | // Sampling a simple child at our coordinates |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 353 | effect.build("uniform shader child;" |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 354 | "half4 main(float2 p) { return sample(child, p); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 355 | effect.child("child") = rgbwShader; |
| 356 | effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF); |
| 357 | |
| 358 | // Sampling with explicit coordinates (reflecting about the diagonal) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 359 | effect.build("uniform shader child;" |
| 360 | "half4 main(float2 p) { return sample(child, p.yx); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 361 | effect.child("child") = rgbwShader; |
| 362 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
| 363 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 364 | // |
| 365 | // Helper functions |
| 366 | // |
| 367 | |
| 368 | // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526): |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 369 | effect.build("float2 helper(float2 x) { return x + 1; }" |
| 370 | "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }"); |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 371 | effect.test(0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 375 | test_RuntimeEffect_Shaders(r, nullptr); |
| 376 | } |
| 377 | |
| 378 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 379 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 380 | } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 381 | |
| 382 | DEF_TEST(SkRuntimeShaderBuilderReuse, r) { |
| 383 | const char* kSource = R"( |
| 384 | uniform half x; |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 385 | half4 main(float2 p) { return half4(x); } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 386 | )"; |
| 387 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 388 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect; |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 389 | REPORTER_ASSERT(r, effect); |
| 390 | |
| 391 | // Test passes if this sequence doesn't assert. skbug.com/10667 |
| 392 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 393 | b.uniform("x") = 0.0f; |
| 394 | auto shader_0 = b.makeShader(nullptr, false); |
| 395 | |
| 396 | b.uniform("x") = 1.0f; |
| 397 | auto shader_1 = b.makeShader(nullptr, true); |
| 398 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 399 | |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 400 | DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) { |
| 401 | const char* kSource = R"( |
| 402 | uniform half x; |
| 403 | uniform vec2 offset; |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 404 | half4 main(float2 p) { return half4(x); } |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 405 | )"; |
| 406 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 407 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect; |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 408 | REPORTER_ASSERT(r, effect); |
| 409 | |
| 410 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 411 | |
| 412 | // Test passes if this sequence doesn't assert. |
| 413 | float x = 1.0f; |
| 414 | REPORTER_ASSERT(r, b.uniform("x").set(&x, 1)); |
| 415 | |
| 416 | // add extra value to ensure that set doesn't try to use sizeof(array) |
| 417 | float origin[] = { 2.0f, 3.0f, 4.0f }; |
| 418 | REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2)); |
| 419 | |
| 420 | #ifndef SK_DEBUG |
| 421 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1)); |
| 422 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3)); |
| 423 | #endif |
| 424 | |
| 425 | |
| 426 | auto shader = b.makeShader(nullptr, false); |
| 427 | } |
| 428 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 429 | DEF_TEST(SkRuntimeEffectThreaded, r) { |
| 430 | // SkRuntimeEffect uses a single compiler instance, but it's mutex locked. |
| 431 | // This tests that we can safely use it from more than one thread, and also |
| 432 | // that programs don't refer to shared structures owned by the compiler. |
| 433 | // skbug.com/10589 |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 434 | static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }"; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 435 | |
| 436 | std::thread threads[16]; |
| 437 | for (auto& thread : threads) { |
| 438 | thread = std::thread([r]() { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 439 | auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource)); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 440 | REPORTER_ASSERT(r, effect); |
| 441 | }); |
| 442 | } |
| 443 | |
| 444 | for (auto& thread : threads) { |
| 445 | thread.join(); |
| 446 | } |
| 447 | } |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 448 | |
| 449 | DEF_TEST(SkRuntimeColorFilterSingleColor, r) { |
| 450 | // Test runtime colorfilters support filterColor4f(). |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 451 | auto [effect, err] = |
| 452 | SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"}); |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 453 | REPORTER_ASSERT(r, effect); |
| 454 | REPORTER_ASSERT(r, err.isEmpty()); |
| 455 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 456 | sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 457 | REPORTER_ASSERT(r, cf); |
| 458 | |
| 459 | SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0}, |
| 460 | sk_srgb_singleton(), sk_srgb_singleton()); |
| 461 | REPORTER_ASSERT(r, c.fR == 0.0625f); |
| 462 | REPORTER_ASSERT(r, c.fG == 0.25f); |
| 463 | REPORTER_ASSERT(r, c.fB == 0.5625f); |
| 464 | REPORTER_ASSERT(r, c.fA == 1.0f); |
| 465 | } |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 466 | |
| 467 | static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) { |
| 468 | // Test that two different runtime effects can reuse struct names in a single paint operation |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 469 | auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString( |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 470 | "uniform shader paint;" |
| 471 | "struct S { half4 rgba; };" |
| 472 | "void process(inout S s) { s.rgba.rgb *= 0.5; }" |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 473 | "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }" |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 474 | )); |
| 475 | REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str()); |
| 476 | sk_sp<SkShader> nullChild = nullptr; |
| 477 | sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild, |
| 478 | /*childCount=*/1, /*localMatrix=*/nullptr, |
| 479 | /*isOpaque=*/false); |
| 480 | |
| 481 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 482 | sk_sp<SkSurface> surface = rContext |
| 483 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 484 | : SkSurface::MakeRaster(info); |
| 485 | REPORTER_ASSERT(r, surface); |
| 486 | |
| 487 | TestEffect effect(r, surface); |
| 488 | effect.build( |
| 489 | "uniform shader child;" |
| 490 | "struct S { float2 coord; };" |
| 491 | "void process(inout S s) { s.coord = s.coord.yx; }" |
| 492 | "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); " |
| 493 | "}"); |
| 494 | effect.child("child") = child; |
| 495 | effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) { |
| 496 | paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f}); |
| 497 | }); |
| 498 | } |
| 499 | |
| 500 | DEF_TEST(SkRuntimeStructNameReuse, r) { |
| 501 | test_RuntimeEffectStructNameReuse(r, nullptr); |
| 502 | } |
| 503 | |
| 504 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) { |
| 505 | test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext()); |
| 506 | } |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 507 | |
| 508 | DEF_TEST(SkRuntimeColorFilterFlags, r) { |
| 509 | { // Here's a non-trivial filter that doesn't change alpha. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 510 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 511 | "half4 main(half4 color) { return color + half4(1,1,1,0); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 512 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 513 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 514 | REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged()); |
| 515 | } |
| 516 | |
| 517 | { // Here's one that definitely changes alpha. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 518 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 519 | "half4 main(half4 color) { return color + half4(0,0,0,4); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 520 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 521 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 522 | REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged()); |
| 523 | } |
| 524 | } |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 525 | |
| 526 | DEF_TEST(SkRuntimeShaderSampleUsage, r) { |
| 527 | auto test = [&](const char* src, bool expectExplicit) { |
| 528 | auto [effect, err] = |
| 529 | SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src)); |
| 530 | REPORTER_ASSERT(r, effect); |
| 531 | |
| 532 | auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 }); |
| 533 | auto fp = effect->makeFP(nullptr, &child, 1); |
| 534 | REPORTER_ASSERT(r, fp); |
| 535 | |
| 536 | REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit); |
| 537 | }; |
| 538 | |
| 539 | // This test verifies that we detect calls to sample where the coords are the same as those |
| 540 | // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough" |
| 541 | // sampling. This optimization is implemented very conservatively. |
| 542 | |
| 543 | // Cases where our optimization is valid, and works: |
| 544 | |
| 545 | // Direct use of passed-in coords |
| 546 | test("half4 main(float2 xy) { return sample(child, xy); }", false); |
| 547 | // Sample with passed-in coords, read (but don't write) sample coords elsewhere |
| 548 | test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false); |
| 549 | |
| 550 | // Cases where our optimization is not valid, and does not happen: |
| 551 | |
| 552 | // Sampling with values completely unrelated to passed-in coords |
| 553 | test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true); |
| 554 | // Use of expression involving passed in coords |
| 555 | test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true); |
| 556 | // Use of coords after modification |
| 557 | test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true); |
| 558 | // Use of coords after modification via out-param call |
| 559 | test("void adjust(inout float2 xy) { xy *= 2; }" |
| 560 | "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true); |
| 561 | |
| 562 | // There should (must) not be any false-positive cases. There are false-negatives. |
| 563 | // In all of these cases, our optimization would be valid, but does not happen: |
| 564 | |
| 565 | // Direct use of passed-in coords, modified after use |
| 566 | test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true); |
| 567 | // Passed-in coords copied to a temp variable |
| 568 | test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true); |
| 569 | // Use of coords passed to helper function |
| 570 | test("half4 helper(float2 xy) { return sample(child, xy); }" |
| 571 | "half4 main(float2 xy) { return helper(xy); }", true); |
| 572 | } |