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