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