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" |
John Stiles | 4912824 | 2021-06-16 22:37:15 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlender.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 11 | #include "include/core/SkColorFilter.h" |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkData.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkSurface.h" |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 15 | #include "include/effects/SkBlenders.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 16 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 17 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 18 | #include "src/core/SkColorSpacePriv.h" |
Brian Osman | 4f00982 | 2021-04-28 09:41:06 -0400 | [diff] [blame] | 19 | #include "src/core/SkRuntimeEffectPriv.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 20 | #include "src/core/SkTLazy.h" |
Robert Phillips | cc44feb | 2021-07-06 12:21:37 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrCaps.h" |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrColor.h" |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 24 | #include "src/gpu/GrFragmentProcessor.h" |
Brian Osman | 443b5da | 2021-06-04 16:52:21 -0400 | [diff] [blame] | 25 | #include "src/gpu/effects/GrSkSLFP.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 26 | #include "tests/Test.h" |
| 27 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 28 | #include <algorithm> |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 29 | #include <thread> |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 30 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 31 | 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] | 32 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src)); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 33 | REPORTER_ASSERT(r, !effect); |
| 34 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 35 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 36 | expected, errorText.c_str()); |
| 37 | }; |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 38 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 39 | #define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }" |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 40 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 41 | DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) { |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 42 | // 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] | 43 | test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 44 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 45 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 46 | DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) { |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 47 | // 'in' variables aren't allowed at all: |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 48 | test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'"); |
| 49 | test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'"); |
| 50 | test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'"); |
| 51 | test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'"); |
| 52 | } |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 53 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 54 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 55 | test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }", |
Brian Osman | 7da0657 | 2021-07-21 15:19:34 -0400 | [diff] [blame] | 56 | "function 'half4 missing()' is not defined"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 57 | } |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 58 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 59 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) { |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 60 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 61 | test_invalid_effect(r, "", "main"); |
| 62 | } |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 63 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 64 | DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) { |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 65 | // 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] | 66 | test_invalid_effect( |
| 67 | r, |
| 68 | "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }", |
| 69 | "unknown identifier 'sk_Caps'"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 70 | } |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 71 | |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 72 | DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) { |
| 73 | auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) { |
Brian Osman | e9ab391 | 2021-07-02 10:17:45 -0400 | [diff] [blame] | 74 | SkRuntimeEffect::Options opt = SkRuntimeEffectPriv::ES3Options(); |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 75 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt); |
| 76 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
| 77 | }; |
| 78 | |
| 79 | test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type"); |
| 80 | test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN); |
| 81 | } |
| 82 | |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 83 | DEF_TEST(SkRuntimeEffectForColorFilter, r) { |
| 84 | // Tests that the color filter factory rejects or accepts certain SkSL constructs |
| 85 | auto test_valid = [r](const char* sksl) { |
| 86 | auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl)); |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 87 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | auto test_invalid = [r](const char* sksl, const char* expected) { |
| 91 | auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl)); |
| 92 | REPORTER_ASSERT(r, !effect); |
| 93 | REPORTER_ASSERT(r, |
| 94 | errorText.contains(expected), |
| 95 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 96 | expected, |
| 97 | errorText.c_str()); |
| 98 | }; |
| 99 | |
| 100 | // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4 |
| 101 | test_valid("half4 main(half4 c) { return c; }"); |
| 102 | test_valid("float4 main(half4 c) { return c; }"); |
| 103 | test_valid("half4 main(float4 c) { return c; }"); |
| 104 | test_valid("float4 main(float4 c) { return c; }"); |
| 105 | test_valid("vec4 main(half4 c) { return c; }"); |
| 106 | test_valid("half4 main(vec4 c) { return c; }"); |
| 107 | test_valid("vec4 main(vec4 c) { return c; }"); |
| 108 | |
| 109 | // Invalid return types |
| 110 | test_invalid("void main(half4 c) {}", "'main' must return"); |
| 111 | test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return"); |
| 112 | |
| 113 | // Invalid argument types (some are valid as shaders, but not color filters) |
| 114 | test_invalid("half4 main() { return half4(1); }", "'main' parameter"); |
| 115 | test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter"); |
| 116 | test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter"); |
| 117 | |
| 118 | // sk_FragCoord should not be available |
| 119 | test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier"); |
| 120 | |
| 121 | // Sampling a child shader requires that we pass explicit coords |
| 122 | test_valid("uniform shader child;" |
| 123 | "half4 main(half4 c) { return sample(child, c.rg); }"); |
| 124 | |
Brian Osman | b5282f0 | 2021-08-24 10:11:56 -0400 | [diff] [blame] | 125 | // Sampling a colorFilter requires a color |
Brian Osman | c9125aa | 2021-04-21 09:57:19 -0400 | [diff] [blame] | 126 | test_valid("uniform colorFilter child;" |
| 127 | "half4 main(half4 c) { return sample(child, c); }"); |
| 128 | |
Brian Osman | b5282f0 | 2021-08-24 10:11:56 -0400 | [diff] [blame] | 129 | // Sampling a blender requires two colors |
| 130 | test_valid("uniform blender child;" |
| 131 | "half4 main(half4 c) { return sample(child, c, c); }"); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 132 | } |
| 133 | |
John Stiles | 9300391 | 2021-06-16 11:34:37 -0400 | [diff] [blame] | 134 | DEF_TEST(SkRuntimeEffectForBlender, r) { |
| 135 | // Tests that the blender factory rejects or accepts certain SkSL constructs |
| 136 | auto test_valid = [r](const char* sksl) { |
| 137 | auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl)); |
| 138 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
| 139 | }; |
| 140 | |
| 141 | auto test_invalid = [r](const char* sksl, const char* expected) { |
| 142 | auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl)); |
| 143 | REPORTER_ASSERT(r, !effect); |
| 144 | REPORTER_ASSERT(r, |
| 145 | errorText.contains(expected), |
| 146 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 147 | expected, |
| 148 | errorText.c_str()); |
| 149 | }; |
| 150 | |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 151 | // Blenders must use the 'half4 main(half4, half4)' signature. Any mixture of float4/vec4/half4 |
| 152 | // is allowed. |
John Stiles | 9300391 | 2021-06-16 11:34:37 -0400 | [diff] [blame] | 153 | test_valid("half4 main(half4 s, half4 d) { return s; }"); |
| 154 | test_valid("float4 main(float4 s, float4 d) { return d; }"); |
| 155 | test_valid("float4 main(half4 s, float4 d) { return s; }"); |
| 156 | test_valid("half4 main(float4 s, half4 d) { return d; }"); |
| 157 | test_valid("vec4 main(half4 s, half4 d) { return s; }"); |
| 158 | test_valid("half4 main(vec4 s, vec4 d) { return d; }"); |
| 159 | test_valid("vec4 main(vec4 s, vec4 d) { return s; }"); |
| 160 | |
| 161 | // Invalid return types |
| 162 | test_invalid("void main(half4 s, half4 d) {}", "'main' must return"); |
| 163 | test_invalid("half3 main(half4 s, half4 d) { return s.rgb; }", "'main' must return"); |
| 164 | |
| 165 | // Invalid argument types (some are valid as shaders/color filters) |
| 166 | test_invalid("half4 main() { return half4(1); }", "'main' parameter"); |
| 167 | test_invalid("half4 main(half4 c) { return c; }", "'main' parameter"); |
| 168 | test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter"); |
| 169 | test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter"); |
| 170 | test_invalid("half4 main(float2 p, half4 a, half4 b) { return a; }", "'main' parameter"); |
| 171 | test_invalid("half4 main(half4 a, half4 b, half4 c) { return a; }", "'main' parameter"); |
| 172 | |
| 173 | // sk_FragCoord should not be available |
| 174 | test_invalid("half4 main(half4 s, half4 d) { return sk_FragCoord.xy01; }", |
| 175 | "unknown identifier"); |
| 176 | |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 177 | // Sampling a child shader requires that we pass explicit coords |
| 178 | test_valid("uniform shader child;" |
| 179 | "half4 main(half4 s, half4 d) { return sample(child, s.rg); }"); |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 180 | |
Brian Osman | b5282f0 | 2021-08-24 10:11:56 -0400 | [diff] [blame] | 181 | // Sampling a colorFilter requires a color |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 182 | test_valid("uniform colorFilter child;" |
| 183 | "half4 main(half4 s, half4 d) { return sample(child, d); }"); |
| 184 | |
Brian Osman | b5282f0 | 2021-08-24 10:11:56 -0400 | [diff] [blame] | 185 | // Sampling a blender requires two colors |
| 186 | test_valid("uniform blender child;" |
| 187 | "half4 main(half4 s, half4 d) { return sample(child, s, d); }"); |
John Stiles | 9300391 | 2021-06-16 11:34:37 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 190 | DEF_TEST(SkRuntimeEffectForShader, r) { |
| 191 | // Tests that the shader factory rejects or accepts certain SkSL constructs |
Brian Osman | 77046a7 | 2021-07-20 13:16:57 -0400 | [diff] [blame] | 192 | auto test_valid = [r](const char* sksl, SkRuntimeEffect::Options options = {}) { |
| 193 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), options); |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 194 | REPORTER_ASSERT(r, effect, "%s", errorText.c_str()); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 195 | }; |
| 196 | |
Brian Osman | 77046a7 | 2021-07-20 13:16:57 -0400 | [diff] [blame] | 197 | auto test_invalid = [r](const char* sksl, |
| 198 | const char* expected, |
| 199 | SkRuntimeEffect::Options options = {}) { |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 200 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl)); |
| 201 | REPORTER_ASSERT(r, !effect); |
| 202 | REPORTER_ASSERT(r, |
| 203 | errorText.contains(expected), |
| 204 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 205 | expected, |
| 206 | errorText.c_str()); |
| 207 | }; |
| 208 | |
| 209 | // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature |
| 210 | // Either color can be half4/float4/vec4, but the coords must be float2/vec2 |
| 211 | test_valid("half4 main(float2 p) { return p.xyxy; }"); |
| 212 | test_valid("float4 main(float2 p) { return p.xyxy; }"); |
| 213 | test_valid("vec4 main(float2 p) { return p.xyxy; }"); |
| 214 | test_valid("half4 main(vec2 p) { return p.xyxy; }"); |
| 215 | test_valid("vec4 main(vec2 p) { return p.xyxy; }"); |
| 216 | test_valid("half4 main(float2 p, half4 c) { return c; }"); |
| 217 | test_valid("half4 main(float2 p, float4 c) { return c; }"); |
| 218 | test_valid("half4 main(float2 p, vec4 c) { return c; }"); |
| 219 | test_valid("float4 main(float2 p, half4 c) { return c; }"); |
| 220 | test_valid("vec4 main(float2 p, half4 c) { return c; }"); |
| 221 | test_valid("vec4 main(vec2 p, vec4 c) { return c; }"); |
| 222 | |
| 223 | // Invalid return types |
| 224 | test_invalid("void main(float2 p) {}", "'main' must return"); |
| 225 | test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return"); |
| 226 | |
| 227 | // Invalid argument types (some are valid as color filters, but not shaders) |
| 228 | test_invalid("half4 main() { return half4(1); }", "'main' parameter"); |
| 229 | test_invalid("half4 main(half4 c) { return c; }", "'main' parameter"); |
| 230 | |
Brian Osman | 77046a7 | 2021-07-20 13:16:57 -0400 | [diff] [blame] | 231 | // sk_FragCoord should be available, but only if we've enabled it via Options |
| 232 | test_invalid("half4 main(float2 p) { return sk_FragCoord.xy01; }", |
| 233 | "unknown identifier 'sk_FragCoord'"); |
| 234 | |
| 235 | SkRuntimeEffect::Options optionsWithFragCoord; |
| 236 | SkRuntimeEffectPriv::EnableFragCoord(&optionsWithFragCoord); |
| 237 | test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }", optionsWithFragCoord); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 238 | |
| 239 | // Sampling a child shader requires that we pass explicit coords |
| 240 | test_valid("uniform shader child;" |
| 241 | "half4 main(float2 p) { return sample(child, p); }"); |
| 242 | |
Brian Osman | b5282f0 | 2021-08-24 10:11:56 -0400 | [diff] [blame] | 243 | // Sampling a colorFilter requires a color |
Brian Osman | c9125aa | 2021-04-21 09:57:19 -0400 | [diff] [blame] | 244 | test_valid("uniform colorFilter child;" |
| 245 | "half4 main(float2 p, half4 c) { return sample(child, c); }"); |
| 246 | |
Brian Osman | b5282f0 | 2021-08-24 10:11:56 -0400 | [diff] [blame] | 247 | // Sampling a blender requires two colors |
| 248 | test_valid("uniform blender child;" |
| 249 | "half4 main(float2 p, half4 c) { return sample(child, c, c); }"); |
Brian Osman | af4e233 | 2021-04-20 12:00:10 -0400 | [diff] [blame] | 250 | } |
| 251 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 252 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 253 | |
| 254 | void paint_canvas(SkCanvas* canvas, SkPaint* paint, const PreTestFn& preTestCallback) { |
| 255 | canvas->save(); |
| 256 | if (preTestCallback) { |
| 257 | preTestCallback(canvas, paint); |
| 258 | } |
| 259 | canvas->drawPaint(*paint); |
| 260 | canvas->restore(); |
| 261 | } |
| 262 | |
| 263 | static void verify_2x2_surface_results(skiatest::Reporter* r, |
| 264 | const SkRuntimeEffect* effect, |
| 265 | SkSurface* surface, |
| 266 | std::array<GrColor, 4> expected) { |
| 267 | std::array<GrColor, 4> actual; |
| 268 | SkImageInfo info = surface->imageInfo(); |
| 269 | if (!surface->readPixels(info, actual.data(), info.minRowBytes(), /*srcX=*/0, /*srcY=*/0)) { |
| 270 | REPORT_FAILURE(r, "readPixels", SkString("readPixels failed")); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | if (actual != expected) { |
| 275 | REPORT_FAILURE(r, "Runtime effect didn't match expectations", |
| 276 | SkStringPrintf("\n" |
| 277 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 278 | "Got : [ %08x %08x %08x %08x ]\n" |
| 279 | "SkSL:\n%s\n", |
| 280 | expected[0], expected[1], expected[2], expected[3], |
| 281 | actual[0], actual[1], actual[2], actual[3], |
| 282 | effect->source().c_str())); |
| 283 | } |
| 284 | } |
| 285 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 286 | class TestEffect { |
| 287 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 288 | TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 289 | : fReporter(r), fSurface(std::move(surface)) {} |
| 290 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 291 | void build(const char* src) { |
Brian Osman | 77046a7 | 2021-07-20 13:16:57 -0400 | [diff] [blame] | 292 | SkRuntimeEffect::Options options; |
| 293 | SkRuntimeEffectPriv::EnableFragCoord(&options); |
| 294 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src), options); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 295 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 296 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 297 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 298 | return; |
| 299 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 300 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 303 | SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) { |
| 304 | return fBuilder->uniform(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 305 | } |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 306 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 307 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 308 | return fBuilder->child(name); |
| 309 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 310 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 311 | void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) { |
John Stiles | 4912824 | 2021-06-16 22:37:15 -0400 | [diff] [blame] | 312 | auto shader = fBuilder->makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 313 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 314 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 318 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 319 | SkPaint paint; |
| 320 | paint.setShader(std::move(shader)); |
| 321 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 322 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 323 | paint_canvas(canvas, &paint, preTestCallback); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 324 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 325 | verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 328 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 329 | this->test({expected, expected, expected, expected}, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 333 | skiatest::Reporter* fReporter; |
| 334 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 335 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 336 | }; |
| 337 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 338 | class TestBlend { |
| 339 | public: |
| 340 | TestBlend(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 341 | : fReporter(r), fSurface(std::move(surface)) {} |
| 342 | |
| 343 | void build(const char* src) { |
| 344 | auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(src)); |
| 345 | if (!effect) { |
| 346 | REPORT_FAILURE(fReporter, "effect", |
| 347 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 348 | return; |
| 349 | } |
| 350 | fBuilder.init(std::move(effect)); |
| 351 | } |
| 352 | |
| 353 | SkRuntimeBlendBuilder::BuilderUniform uniform(const char* name) { |
| 354 | return fBuilder->uniform(name); |
| 355 | } |
| 356 | |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 357 | SkRuntimeBlendBuilder::BuilderChild child(const char* name) { |
| 358 | return fBuilder->child(name); |
| 359 | } |
| 360 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 361 | void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) { |
| 362 | auto blender = fBuilder->makeBlender(); |
| 363 | if (!blender) { |
| 364 | REPORT_FAILURE(fReporter, "blender", SkString("Effect didn't produce a blender")); |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | SkCanvas* canvas = fSurface->getCanvas(); |
| 369 | SkPaint paint; |
Mike Reed | 3037d9f | 2021-07-06 11:29:45 -0400 | [diff] [blame] | 370 | paint.setBlender(std::move(blender)); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 371 | paint.setColor(SK_ColorGRAY); |
| 372 | |
| 373 | paint_canvas(canvas, &paint, preTestCallback); |
| 374 | |
| 375 | verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected); |
| 376 | } |
| 377 | |
| 378 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 379 | this->test({expected, expected, expected, expected}, preTestCallback); |
| 380 | } |
| 381 | |
| 382 | private: |
| 383 | skiatest::Reporter* fReporter; |
| 384 | sk_sp<SkSurface> fSurface; |
| 385 | SkTLazy<SkRuntimeBlendBuilder> fBuilder; |
| 386 | }; |
| 387 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 388 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 389 | // [ Red, Green ] |
| 390 | // [ Blue, White ] |
| 391 | static sk_sp<SkShader> make_RGBW_shader() { |
| 392 | SkBitmap bmp; |
| 393 | bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 394 | SkIRect topLeft = SkIRect::MakeWH(1, 1); |
| 395 | bmp.pixmap().erase(SK_ColorRED, topLeft); |
| 396 | bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0)); |
| 397 | bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1)); |
| 398 | bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1)); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 399 | return bmp.makeShader(SkSamplingOptions()); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 400 | } |
| 401 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 402 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 403 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 404 | sk_sp<SkSurface> surface = rContext |
| 405 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 406 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 407 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 408 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 409 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 410 | using float4 = std::array<float, 4>; |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 411 | using int4 = std::array<int, 4>; |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 412 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 413 | // Local coords |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 414 | effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 415 | effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF}); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 416 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 417 | // 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] | 418 | effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 419 | effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 420 | effect.test(0xFFBF4000); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 421 | effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 422 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 423 | |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 424 | // Same, with integer uniforms |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 425 | 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] | 426 | effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF }; |
| 427 | effect.test(0xFFBF4000); |
| 428 | effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F }; |
| 429 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
| 430 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 431 | // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords. |
| 432 | // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to |
| 433 | // make sure we're not saturating unexpectedly. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 434 | effect.build( |
| 435 | "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 436 | effect.test({0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F}, |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 437 | [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); }); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 438 | |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 439 | // Runtime effects should use relaxed precision rules by default |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 440 | effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 441 | effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF}); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 442 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 443 | // ... and support *returning* float4 (aka vec4), not just half4 |
| 444 | effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 445 | effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF}); |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 446 | effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 447 | effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF}); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 448 | |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 449 | // Mutating coords should work. (skbug.com/10918) |
| 450 | effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 451 | effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF}); |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 452 | effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }" |
| 453 | "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }"); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 454 | effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF}); |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 455 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 456 | // |
| 457 | // Sampling children |
| 458 | // |
| 459 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 460 | // Sampling a null child should return the paint color |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 461 | effect.build("uniform shader child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 462 | "half4 main(float2 p) { return shade(child, p); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 463 | effect.child("child") = nullptr; |
| 464 | effect.test(0xFF00FFFF, |
| 465 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 466 | |
| 467 | sk_sp<SkShader> rgbwShader = make_RGBW_shader(); |
| 468 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 469 | // Sampling a simple child at our coordinates |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 470 | effect.build("uniform shader child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 471 | "half4 main(float2 p) { return shade(child, p); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 472 | effect.child("child") = rgbwShader; |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 473 | effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF}); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 474 | |
| 475 | // Sampling with explicit coordinates (reflecting about the diagonal) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 476 | effect.build("uniform shader child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 477 | "half4 main(float2 p) { return shade(child, p.yx); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 478 | effect.child("child") = rgbwShader; |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 479 | effect.test({0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF}); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 480 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 481 | // |
| 482 | // Helper functions |
| 483 | // |
| 484 | |
| 485 | // 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] | 486 | effect.build("float2 helper(float2 x) { return x + 1; }" |
| 487 | "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] | 488 | effect.test(0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 492 | test_RuntimeEffect_Shaders(r, nullptr); |
| 493 | } |
| 494 | |
| 495 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 496 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 497 | } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 498 | |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 499 | static void test_RuntimeEffect_Blenders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
| 500 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 501 | sk_sp<SkSurface> surface = rContext |
| 502 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 503 | : SkSurface::MakeRaster(info); |
| 504 | REPORTER_ASSERT(r, surface); |
| 505 | TestBlend effect(r, surface); |
| 506 | |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 507 | using float2 = std::array<float, 2>; |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 508 | using float4 = std::array<float, 4>; |
| 509 | using int4 = std::array<int, 4>; |
| 510 | |
| 511 | // Use of a simple uniform. (Draw twice with two values to ensure it's updated). |
| 512 | effect.build("uniform float4 gColor; half4 main(half4 s, half4 d) { return half4(gColor); }"); |
| 513 | effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
| 514 | effect.test(0xFFBF4000); |
| 515 | effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f }; |
| 516 | effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here |
| 517 | |
| 518 | // Same, with integer uniforms |
| 519 | effect.build("uniform int4 gColor;" |
| 520 | "half4 main(half4 s, half4 d) { return half4(gColor) / 255.0; }"); |
| 521 | effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF }; |
| 522 | effect.test(0xFFBF4000); |
| 523 | effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F }; |
| 524 | effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here |
| 525 | |
| 526 | // Verify that mutating the source and destination colors is allowed |
| 527 | effect.build("half4 main(half4 s, half4 d) { s += d; d += s; return half4(1); }"); |
| 528 | effect.test(0xFFFFFFFF); |
| 529 | |
| 530 | // Verify that we can write out the source color (ignoring the dest color) |
| 531 | // This is equivalent to the kSrc blend mode. |
| 532 | effect.build("half4 main(half4 s, half4 d) { return s; }"); |
| 533 | effect.test(0xFF888888); |
| 534 | |
| 535 | // Fill the destination with a variety of colors (using the RGBW shader) |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 536 | SkPaint rgbwPaint; |
| 537 | rgbwPaint.setShader(make_RGBW_shader()); |
| 538 | rgbwPaint.setBlendMode(SkBlendMode::kSrc); |
| 539 | surface->getCanvas()->drawPaint(rgbwPaint); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 540 | |
| 541 | // Verify that we can read back the dest color exactly as-is (ignoring the source color) |
| 542 | // This is equivalent to the kDst blend mode. |
| 543 | effect.build("half4 main(half4 s, half4 d) { return d; }"); |
| 544 | effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF}); |
| 545 | |
| 546 | // Verify that we can invert the destination color (including the alpha channel). |
| 547 | // The expected outputs are the exact inverse of the previous test. |
| 548 | effect.build("half4 main(half4 s, half4 d) { return half4(1) - d; }"); |
| 549 | effect.test({0x00FFFF00, 0x00FF00FF, 0x0000FFFF, 0x00000000}); |
| 550 | |
| 551 | // Verify that color values are clamped to 0 and 1. |
| 552 | effect.build("half4 main(half4 s, half4 d) { return half4(-1); }"); |
| 553 | effect.test(0x00000000); |
| 554 | effect.build("half4 main(half4 s, half4 d) { return half4(2); }"); |
| 555 | effect.test(0xFFFFFFFF); |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 556 | |
| 557 | // |
| 558 | // Sampling children |
| 559 | // |
| 560 | |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 561 | // Sampling a null shader/color filter should return the paint color. |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 562 | effect.build("uniform shader child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 563 | "half4 main(half4 s, half4 d) { return shade(child, s.rg); }"); |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 564 | effect.child("child") = nullptr; |
| 565 | effect.test(0xFF00FFFF, |
| 566 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 567 | |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 568 | effect.build("uniform colorFilter child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 569 | "half4 main(half4 s, half4 d) { return filter(child, s); }"); |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 570 | effect.child("child") = nullptr; |
| 571 | effect.test(0xFF00FFFF, |
| 572 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 573 | |
| 574 | // Sampling a null blender should do a src-over blend. Draw 50% black over RGBW to verify this. |
| 575 | surface->getCanvas()->drawPaint(rgbwPaint); |
| 576 | effect.build("uniform blender child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 577 | "half4 main(half4 s, half4 d) { return blend(child, s, d); }"); |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 578 | effect.child("child") = nullptr; |
| 579 | effect.test({0xFF000080, 0xFF008000, 0xFF800000, 0xFF808080}, |
| 580 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({0.0f, 0.0f, 0.0f, 0.497f}); }); |
| 581 | |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 582 | // Sampling a shader at various coordinates |
| 583 | effect.build("uniform shader child;" |
| 584 | "uniform half2 pos;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 585 | "half4 main(half4 s, half4 d) { return shade(child, pos); }"); |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 586 | effect.child("child") = make_RGBW_shader(); |
| 587 | effect.uniform("pos") = float2{0, 0}; |
| 588 | effect.test(0xFF0000FF); |
| 589 | |
| 590 | effect.uniform("pos") = float2{1, 0}; |
| 591 | effect.test(0xFF00FF00); |
| 592 | |
| 593 | effect.uniform("pos") = float2{0, 1}; |
| 594 | effect.test(0xFFFF0000); |
| 595 | |
| 596 | effect.uniform("pos") = float2{1, 1}; |
| 597 | effect.test(0xFFFFFFFF); |
| 598 | |
| 599 | // Sampling a color filter |
| 600 | effect.build("uniform colorFilter child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 601 | "half4 main(half4 s, half4 d) { return filter(child, half4(1)); }"); |
John Stiles | 8e51bad | 2021-07-22 10:13:30 -0400 | [diff] [blame] | 602 | effect.child("child") = SkColorFilters::Blend(0xFF012345, SkBlendMode::kSrc); |
| 603 | effect.test(0xFF452301); |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 604 | |
| 605 | // Sampling a built-in blender |
| 606 | surface->getCanvas()->drawPaint(rgbwPaint); |
| 607 | effect.build("uniform blender child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 608 | "half4 main(half4 s, half4 d) { return blend(child, s, d); }"); |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 609 | effect.child("child") = SkBlender::Mode(SkBlendMode::kPlus); |
| 610 | effect.test({0xFF4523FF, 0xFF45FF01, 0xFFFF2301, 0xFFFFFFFF}, |
| 611 | [](SkCanvas*, SkPaint* paint) { paint->setColor(0xFF012345); }); |
| 612 | |
| 613 | // Sampling a runtime-effect blender |
| 614 | surface->getCanvas()->drawPaint(rgbwPaint); |
| 615 | effect.build("uniform blender child;" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 616 | "half4 main(half4 s, half4 d) { return blend(child, s, d); }"); |
John Stiles | ce9a5c9 | 2021-07-30 11:20:19 -0400 | [diff] [blame] | 617 | effect.child("child") = SkBlenders::Arithmetic(0, 1, 1, 0, /*enforcePremul=*/false); |
| 618 | effect.test({0xFF4523FF, 0xFF45FF01, 0xFFFF2301, 0xFFFFFFFF}, |
| 619 | [](SkCanvas*, SkPaint* paint) { paint->setColor(0xFF012345); }); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | DEF_TEST(SkRuntimeEffect_Blender_CPU, r) { |
John Stiles | 9a2aa97 | 2021-06-18 10:15:35 -0400 | [diff] [blame] | 623 | test_RuntimeEffect_Blenders(r, /*rContext=*/nullptr); |
John Stiles | 9b170c6 | 2021-06-18 10:14:14 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU, r, ctxInfo) { |
| 627 | test_RuntimeEffect_Blenders(r, ctxInfo.directContext()); |
| 628 | } |
| 629 | |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 630 | DEF_TEST(SkRuntimeShaderBuilderReuse, r) { |
| 631 | const char* kSource = R"( |
| 632 | uniform half x; |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 633 | half4 main(float2 p) { return half4(x); } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 634 | )"; |
| 635 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 636 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect; |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 637 | REPORTER_ASSERT(r, effect); |
| 638 | |
| 639 | // Test passes if this sequence doesn't assert. skbug.com/10667 |
| 640 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 641 | b.uniform("x") = 0.0f; |
John Stiles | 4912824 | 2021-06-16 22:37:15 -0400 | [diff] [blame] | 642 | auto shader_0 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false); |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 643 | |
| 644 | b.uniform("x") = 1.0f; |
John Stiles | 4912824 | 2021-06-16 22:37:15 -0400 | [diff] [blame] | 645 | auto shader_1 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true); |
| 646 | } |
| 647 | |
| 648 | DEF_TEST(SkRuntimeBlendBuilderReuse, r) { |
| 649 | const char* kSource = R"( |
| 650 | uniform half x; |
| 651 | half4 main(half4 s, half4 d) { return half4(x); } |
| 652 | )"; |
| 653 | |
| 654 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForBlender(SkString(kSource)).effect; |
| 655 | REPORTER_ASSERT(r, effect); |
| 656 | |
| 657 | // We should be able to construct multiple SkBlenders in a row without asserting. |
| 658 | SkRuntimeBlendBuilder b(std::move(effect)); |
| 659 | for (float x = 0.0f; x <= 2.0f; x += 2.0f) { |
| 660 | b.uniform("x") = x; |
| 661 | sk_sp<SkBlender> blender = b.makeBlender(); |
| 662 | } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 663 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 664 | |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 665 | DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) { |
| 666 | const char* kSource = R"( |
| 667 | uniform half x; |
| 668 | uniform vec2 offset; |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 669 | half4 main(float2 p) { return half4(x); } |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 670 | )"; |
| 671 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 672 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect; |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 673 | REPORTER_ASSERT(r, effect); |
| 674 | |
| 675 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 676 | |
| 677 | // Test passes if this sequence doesn't assert. |
| 678 | float x = 1.0f; |
| 679 | REPORTER_ASSERT(r, b.uniform("x").set(&x, 1)); |
| 680 | |
| 681 | // add extra value to ensure that set doesn't try to use sizeof(array) |
| 682 | float origin[] = { 2.0f, 3.0f, 4.0f }; |
| 683 | REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2)); |
| 684 | |
| 685 | #ifndef SK_DEBUG |
| 686 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1)); |
| 687 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3)); |
| 688 | #endif |
| 689 | |
John Stiles | 4912824 | 2021-06-16 22:37:15 -0400 | [diff] [blame] | 690 | auto shader = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false); |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 691 | } |
| 692 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 693 | DEF_TEST(SkRuntimeEffectThreaded, r) { |
| 694 | // SkRuntimeEffect uses a single compiler instance, but it's mutex locked. |
| 695 | // This tests that we can safely use it from more than one thread, and also |
| 696 | // that programs don't refer to shared structures owned by the compiler. |
| 697 | // skbug.com/10589 |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 698 | static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }"; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 699 | |
| 700 | std::thread threads[16]; |
| 701 | for (auto& thread : threads) { |
| 702 | thread = std::thread([r]() { |
Brian Osman | 77046a7 | 2021-07-20 13:16:57 -0400 | [diff] [blame] | 703 | SkRuntimeEffect::Options options; |
| 704 | SkRuntimeEffectPriv::EnableFragCoord(&options); |
| 705 | auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource), options); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 706 | REPORTER_ASSERT(r, effect); |
| 707 | }); |
| 708 | } |
| 709 | |
| 710 | for (auto& thread : threads) { |
| 711 | thread.join(); |
| 712 | } |
| 713 | } |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 714 | |
| 715 | DEF_TEST(SkRuntimeColorFilterSingleColor, r) { |
| 716 | // Test runtime colorfilters support filterColor4f(). |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 717 | auto [effect, err] = |
| 718 | SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"}); |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 719 | REPORTER_ASSERT(r, effect); |
| 720 | REPORTER_ASSERT(r, err.isEmpty()); |
| 721 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 722 | sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 723 | REPORTER_ASSERT(r, cf); |
| 724 | |
| 725 | SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0}, |
| 726 | sk_srgb_singleton(), sk_srgb_singleton()); |
| 727 | REPORTER_ASSERT(r, c.fR == 0.0625f); |
| 728 | REPORTER_ASSERT(r, c.fG == 0.25f); |
| 729 | REPORTER_ASSERT(r, c.fB == 0.5625f); |
| 730 | REPORTER_ASSERT(r, c.fA == 1.0f); |
| 731 | } |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 732 | |
| 733 | static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) { |
| 734 | // 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] | 735 | auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString( |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 736 | "uniform shader paint;" |
| 737 | "struct S { half4 rgba; };" |
| 738 | "void process(inout S s) { s.rgba.rgb *= 0.5; }" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 739 | "half4 main(float2 p) { S s; s.rgba = shade(paint, p); process(s); return s.rgba; }" |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 740 | )); |
| 741 | REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str()); |
| 742 | sk_sp<SkShader> nullChild = nullptr; |
| 743 | sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild, |
| 744 | /*childCount=*/1, /*localMatrix=*/nullptr, |
| 745 | /*isOpaque=*/false); |
| 746 | |
| 747 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 748 | sk_sp<SkSurface> surface = rContext |
| 749 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 750 | : SkSurface::MakeRaster(info); |
| 751 | REPORTER_ASSERT(r, surface); |
| 752 | |
| 753 | TestEffect effect(r, surface); |
| 754 | effect.build( |
| 755 | "uniform shader child;" |
| 756 | "struct S { float2 coord; };" |
| 757 | "void process(inout S s) { s.coord = s.coord.yx; }" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 758 | "half4 main(float2 p) { S s; s.coord = p; process(s); return shade(child, s.coord); " |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 759 | "}"); |
| 760 | effect.child("child") = child; |
| 761 | effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) { |
| 762 | paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f}); |
| 763 | }); |
| 764 | } |
| 765 | |
| 766 | DEF_TEST(SkRuntimeStructNameReuse, r) { |
| 767 | test_RuntimeEffectStructNameReuse(r, nullptr); |
| 768 | } |
| 769 | |
| 770 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) { |
| 771 | test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext()); |
| 772 | } |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 773 | |
| 774 | DEF_TEST(SkRuntimeColorFilterFlags, r) { |
| 775 | { // Here's a non-trivial filter that doesn't change alpha. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 776 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 777 | "half4 main(half4 color) { return color + half4(1,1,1,0); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 778 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 779 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 780 | REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged()); |
| 781 | } |
| 782 | |
| 783 | { // Here's one that definitely changes alpha. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 784 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 785 | "half4 main(half4 color) { return color + half4(0,0,0,4); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 786 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 787 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 788 | REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged()); |
| 789 | } |
| 790 | } |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 791 | |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 792 | DEF_TEST(SkRuntimeShaderSampleCoords, r) { |
| 793 | // This test verifies that we detect calls to sample where the coords are the same as those |
| 794 | // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough" |
| 795 | // sampling. This optimization is implemented very conservatively. |
| 796 | // |
| 797 | // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect |
| 798 | // FP, depending on how the coords parameter to main is used. |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 799 | |
| 800 | auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) { |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 801 | auto [effect, err] = |
| 802 | SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src)); |
| 803 | REPORTER_ASSERT(r, effect); |
| 804 | |
| 805 | auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 }); |
Brian Osman | 171fba7 | 2021-06-16 17:10:21 -0400 | [diff] [blame] | 806 | auto fp = GrSkSLFP::Make(effect, "test_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone, |
| 807 | "child", std::move(child)); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 808 | REPORTER_ASSERT(r, fp); |
| 809 | |
Brian Salomon | 66b500a | 2021-08-02 12:37:14 -0400 | [diff] [blame] | 810 | REPORTER_ASSERT(r, fp->childProcessor(0)->sampleUsage().isExplicit() == expectExplicit); |
| 811 | REPORTER_ASSERT(r, fp->usesSampleCoords() == expectReferencesSampleCoords); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 812 | }; |
| 813 | |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 814 | // Cases where our optimization is valid, and works: |
| 815 | |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 816 | // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call |
| 817 | // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main. |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 818 | test("half4 main(float2 xy) { return shade(child, xy); }", false, false); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 819 | // Sample with passed-in coords, read (but don't write) sample coords elsewhere |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 820 | test("half4 main(float2 xy) { return shade(child, xy) + sin(xy.x); }", false, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 821 | |
| 822 | // Cases where our optimization is not valid, and does not happen: |
| 823 | |
| 824 | // Sampling with values completely unrelated to passed-in coords |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 825 | test("half4 main(float2 xy) { return shade(child, float2(0, 0)); }", true, false); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 826 | // Use of expression involving passed in coords |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 827 | test("half4 main(float2 xy) { return shade(child, xy * 0.5); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 828 | // Use of coords after modification |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 829 | test("half4 main(float2 xy) { xy *= 2; return shade(child, xy); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 830 | // Use of coords after modification via out-param call |
| 831 | test("void adjust(inout float2 xy) { xy *= 2; }" |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 832 | "half4 main(float2 xy) { adjust(xy); return shade(child, xy); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 833 | |
| 834 | // There should (must) not be any false-positive cases. There are false-negatives. |
| 835 | // In all of these cases, our optimization would be valid, but does not happen: |
| 836 | |
| 837 | // Direct use of passed-in coords, modified after use |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 838 | test("half4 main(float2 xy) { half4 c = shade(child, xy); xy *= 2; return c; }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 839 | // Passed-in coords copied to a temp variable |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 840 | test("half4 main(float2 xy) { float2 p = xy; return shade(child, p); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 841 | // Use of coords passed to helper function |
Brian Osman | 293497e | 2021-08-24 14:08:50 -0400 | [diff] [blame^] | 842 | test("half4 helper(float2 xy) { return shade(child, xy); }" |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 843 | "half4 main(float2 xy) { return helper(xy); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 844 | } |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 845 | |
| 846 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) { |
| 847 | struct FpAndKey { |
| 848 | std::unique_ptr<GrFragmentProcessor> fp; |
| 849 | SkTArray<uint32_t, true> key; |
| 850 | }; |
| 851 | |
Brian Osman | 5946589 | 2021-06-18 09:15:44 -0400 | [diff] [blame] | 852 | // Constant color, but with a similar option to GrFragmentProcessor::OverrideInput |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 853 | // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform |
| 854 | auto make_color_fp = [&](SkPMColor4f color, bool specialize) { |
| 855 | auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"( |
| 856 | uniform half4 color; |
| 857 | half4 main(float2 xy) { return color; } |
| 858 | )"); |
| 859 | FpAndKey result; |
Brian Osman | b2cb817 | 2021-06-15 14:36:17 -0400 | [diff] [blame] | 860 | result.fp = GrSkSLFP::Make(std::move(effect), "color_fp", /*inputFP=*/nullptr, |
Brian Osman | 171fba7 | 2021-06-16 17:10:21 -0400 | [diff] [blame] | 861 | GrSkSLFP::OptFlags::kNone, |
Brian Osman | b2cb817 | 2021-06-15 14:36:17 -0400 | [diff] [blame] | 862 | "color", GrSkSLFP::SpecializeIf(specialize, color)); |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 863 | GrProcessorKeyBuilder builder(&result.key); |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 864 | result.fp->addToKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(), &builder); |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 865 | builder.flush(); |
| 866 | return result; |
| 867 | }; |
| 868 | |
| 869 | FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false), |
| 870 | uGreen = make_color_fp({0, 1, 0, 1}, false), |
| 871 | sRed = make_color_fp({1, 0, 0, 1}, true), |
| 872 | sGreen = make_color_fp({0, 1, 0, 1}, true); |
| 873 | |
| 874 | // uRed and uGreen should have the same key - they just have different uniforms |
| 875 | SkASSERT(uRed.key == uGreen.key); |
| 876 | // sRed and sGreen should have keys that are different from the uniform case, and each other |
| 877 | SkASSERT(sRed.key != uRed.key); |
| 878 | SkASSERT(sGreen.key != uRed.key); |
| 879 | SkASSERT(sRed.key != sGreen.key); |
| 880 | } |