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