Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkData.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkSurface.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 14 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 16 | #include "src/core/SkColorSpacePriv.h" |
Brian Osman | 4f00982 | 2021-04-28 09:41:06 -0400 | [diff] [blame] | 17 | #include "src/core/SkRuntimeEffectPriv.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 18 | #include "src/core/SkTLazy.h" |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrColor.h" |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 20 | #include "src/gpu/GrDirectContextPriv.h" |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrFragmentProcessor.h" |
Brian Osman | 443b5da | 2021-06-04 16:52:21 -0400 | [diff] [blame] | 22 | #include "src/gpu/effects/GrSkSLFP.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 23 | #include "tests/Test.h" |
| 24 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 25 | #include <algorithm> |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 26 | #include <thread> |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 27 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 28 | 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] | 29 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src)); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 30 | REPORTER_ASSERT(r, !effect); |
| 31 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 32 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 33 | expected, errorText.c_str()); |
| 34 | }; |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 35 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 36 | #define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }" |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 37 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 38 | DEF_TEST(SkRuntimeEffectInvalid_FPOnly, r) { |
Brian Osman | 3adc091 | 2021-05-18 10:43:08 -0400 | [diff] [blame] | 39 | // 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] | 40 | // Ensure that these fail, and the error messages contain the relevant keyword. |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 41 | test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key"); |
| 42 | test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform"); |
| 43 | test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype"); |
| 44 | test_invalid_effect(r, "in bool Flag; " |
| 45 | "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 46 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 47 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 48 | DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) { |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 49 | // 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] | 50 | test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 51 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 52 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 53 | DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) { |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 54 | // 'in' variables aren't allowed at all: |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 55 | test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'"); |
| 56 | test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'"); |
| 57 | test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'"); |
| 58 | test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'"); |
| 59 | } |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 60 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 61 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 62 | test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }", |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 63 | "undefined function"); |
| 64 | } |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 65 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 66 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) { |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 67 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 68 | test_invalid_effect(r, "", "main"); |
| 69 | } |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 70 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 71 | DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) { |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 72 | // 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] | 73 | test_invalid_effect( |
| 74 | r, |
| 75 | "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }", |
| 76 | "unknown identifier 'sk_Caps'"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 77 | } |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 78 | |
John Stiles | 65d7ab2 | 2021-04-28 15:14:09 -0400 | [diff] [blame] | 79 | DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) { |
| 80 | auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) { |
| 81 | SkRuntimeEffect::Options opt; |
| 82 | opt.enforceES2Restrictions = false; |
| 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 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 306 | class TestEffect { |
| 307 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 308 | TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 309 | : fReporter(r), fSurface(std::move(surface)) {} |
| 310 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 311 | void build(const char* src) { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 312 | auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 313 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 314 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 315 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 316 | return; |
| 317 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 318 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 319 | } |
| 320 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 321 | SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) { |
| 322 | return fBuilder->uniform(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 323 | } |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 324 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 325 | return fBuilder->child(name); |
| 326 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 327 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 328 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 329 | |
| 330 | void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR, |
| 331 | PreTestFn preTestCallback = nullptr) { |
John Stiles | c804b74 | 2021-06-16 20:59:25 +0000 | [diff] [blame] | 332 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 333 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 334 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 338 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 339 | SkPaint paint; |
| 340 | paint.setShader(std::move(shader)); |
| 341 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 342 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 343 | canvas->save(); |
| 344 | if (preTestCallback) { |
| 345 | preTestCallback(canvas, &paint); |
| 346 | } |
| 347 | canvas->drawPaint(paint); |
| 348 | canvas->restore(); |
| 349 | |
| 350 | GrColor actual[4]; |
| 351 | SkImageInfo info = fSurface->imageInfo(); |
| 352 | if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 353 | REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 354 | return; |
| 355 | } |
| 356 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 357 | GrColor expected[4] = {TL, TR, BL, BR}; |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 358 | if (0 != memcmp(actual, expected, sizeof(actual))) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 359 | REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 360 | SkStringPrintf("\n" |
| 361 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 362 | "Got : [ %08x %08x %08x %08x ]\n" |
| 363 | "SkSL:\n%s\n", |
| 364 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 365 | actual[3], fBuilder->effect()->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 369 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 370 | this->test(expected, expected, expected, expected, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 374 | skiatest::Reporter* fReporter; |
| 375 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 376 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 377 | }; |
| 378 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 379 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 380 | // [ Red, Green ] |
| 381 | // [ Blue, White ] |
| 382 | static sk_sp<SkShader> make_RGBW_shader() { |
| 383 | SkBitmap bmp; |
| 384 | bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 385 | SkIRect topLeft = SkIRect::MakeWH(1, 1); |
| 386 | bmp.pixmap().erase(SK_ColorRED, topLeft); |
| 387 | bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0)); |
| 388 | bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1)); |
| 389 | bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1)); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 390 | return bmp.makeShader(SkSamplingOptions()); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 393 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 394 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 395 | sk_sp<SkSurface> surface = rContext |
| 396 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 397 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 398 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 399 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 400 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 401 | using float4 = std::array<float, 4>; |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 402 | using int4 = std::array<int, 4>; |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 403 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 404 | // Local coords |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 405 | 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] | 406 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 407 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 408 | // 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] | 409 | effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 410 | effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 411 | effect.test(0xFFBF4000); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 412 | effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 413 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 414 | |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 415 | // Same, with integer uniforms |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 416 | 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] | 417 | effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF }; |
| 418 | effect.test(0xFFBF4000); |
| 419 | effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F }; |
| 420 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
| 421 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 422 | // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords. |
| 423 | // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to |
| 424 | // make sure we're not saturating unexpectedly. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 425 | effect.build( |
| 426 | "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] | 427 | effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, |
| 428 | [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); }); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 429 | |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 430 | // Runtime effects should use relaxed precision rules by default |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 431 | 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] | 432 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 433 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 434 | // ... and support *returning* float4 (aka vec4), not just half4 |
| 435 | effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
| 436 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 437 | 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] | 438 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 439 | |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 440 | // Mutating coords should work. (skbug.com/10918) |
| 441 | effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }"); |
| 442 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 443 | effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }" |
| 444 | "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }"); |
| 445 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 446 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 447 | // |
| 448 | // Sampling children |
| 449 | // |
| 450 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 451 | // Sampling a null child should return the paint color |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 452 | effect.build("uniform shader child;" |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 453 | "half4 main(float2 p) { return sample(child, p); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 454 | effect.child("child") = nullptr; |
| 455 | effect.test(0xFF00FFFF, |
| 456 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 457 | |
| 458 | sk_sp<SkShader> rgbwShader = make_RGBW_shader(); |
| 459 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 460 | // Sampling a simple child at our coordinates |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 461 | effect.build("uniform shader child;" |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 462 | "half4 main(float2 p) { return sample(child, p); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 463 | effect.child("child") = rgbwShader; |
| 464 | effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF); |
| 465 | |
| 466 | // Sampling with explicit coordinates (reflecting about the diagonal) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 467 | effect.build("uniform shader child;" |
| 468 | "half4 main(float2 p) { return sample(child, p.yx); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 469 | effect.child("child") = rgbwShader; |
| 470 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
| 471 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 472 | // |
| 473 | // Helper functions |
| 474 | // |
| 475 | |
| 476 | // 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] | 477 | effect.build("float2 helper(float2 x) { return x + 1; }" |
| 478 | "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] | 479 | effect.test(0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 483 | test_RuntimeEffect_Shaders(r, nullptr); |
| 484 | } |
| 485 | |
| 486 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 487 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 488 | } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 489 | |
| 490 | DEF_TEST(SkRuntimeShaderBuilderReuse, r) { |
| 491 | const char* kSource = R"( |
| 492 | uniform half x; |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 493 | half4 main(float2 p) { return half4(x); } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 494 | )"; |
| 495 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 496 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect; |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 497 | REPORTER_ASSERT(r, effect); |
| 498 | |
| 499 | // Test passes if this sequence doesn't assert. skbug.com/10667 |
| 500 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 501 | b.uniform("x") = 0.0f; |
John Stiles | c804b74 | 2021-06-16 20:59:25 +0000 | [diff] [blame] | 502 | auto shader_0 = b.makeShader(nullptr, false); |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 503 | |
| 504 | b.uniform("x") = 1.0f; |
John Stiles | c804b74 | 2021-06-16 20:59:25 +0000 | [diff] [blame] | 505 | auto shader_1 = b.makeShader(nullptr, true); |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 506 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 507 | |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 508 | DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) { |
| 509 | const char* kSource = R"( |
| 510 | uniform half x; |
| 511 | uniform vec2 offset; |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 512 | half4 main(float2 p) { return half4(x); } |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 513 | )"; |
| 514 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 515 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect; |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 516 | REPORTER_ASSERT(r, effect); |
| 517 | |
| 518 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 519 | |
| 520 | // Test passes if this sequence doesn't assert. |
| 521 | float x = 1.0f; |
| 522 | REPORTER_ASSERT(r, b.uniform("x").set(&x, 1)); |
| 523 | |
| 524 | // add extra value to ensure that set doesn't try to use sizeof(array) |
| 525 | float origin[] = { 2.0f, 3.0f, 4.0f }; |
| 526 | REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2)); |
| 527 | |
| 528 | #ifndef SK_DEBUG |
| 529 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1)); |
| 530 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3)); |
| 531 | #endif |
| 532 | |
John Stiles | c804b74 | 2021-06-16 20:59:25 +0000 | [diff] [blame] | 533 | |
| 534 | auto shader = b.makeShader(nullptr, false); |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 535 | } |
| 536 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 537 | DEF_TEST(SkRuntimeEffectThreaded, r) { |
| 538 | // SkRuntimeEffect uses a single compiler instance, but it's mutex locked. |
| 539 | // This tests that we can safely use it from more than one thread, and also |
| 540 | // that programs don't refer to shared structures owned by the compiler. |
| 541 | // skbug.com/10589 |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 542 | static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }"; |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 543 | |
| 544 | std::thread threads[16]; |
| 545 | for (auto& thread : threads) { |
| 546 | thread = std::thread([r]() { |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 547 | auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource)); |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 548 | REPORTER_ASSERT(r, effect); |
| 549 | }); |
| 550 | } |
| 551 | |
| 552 | for (auto& thread : threads) { |
| 553 | thread.join(); |
| 554 | } |
| 555 | } |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 556 | |
| 557 | DEF_TEST(SkRuntimeColorFilterSingleColor, r) { |
| 558 | // Test runtime colorfilters support filterColor4f(). |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 559 | auto [effect, err] = |
| 560 | SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"}); |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 561 | REPORTER_ASSERT(r, effect); |
| 562 | REPORTER_ASSERT(r, err.isEmpty()); |
| 563 | |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 564 | sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 565 | REPORTER_ASSERT(r, cf); |
| 566 | |
| 567 | SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0}, |
| 568 | sk_srgb_singleton(), sk_srgb_singleton()); |
| 569 | REPORTER_ASSERT(r, c.fR == 0.0625f); |
| 570 | REPORTER_ASSERT(r, c.fG == 0.25f); |
| 571 | REPORTER_ASSERT(r, c.fB == 0.5625f); |
| 572 | REPORTER_ASSERT(r, c.fA == 1.0f); |
| 573 | } |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 574 | |
| 575 | static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) { |
| 576 | // 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] | 577 | auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString( |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 578 | "uniform shader paint;" |
| 579 | "struct S { half4 rgba; };" |
| 580 | "void process(inout S s) { s.rgba.rgb *= 0.5; }" |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 581 | "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] | 582 | )); |
| 583 | REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str()); |
| 584 | sk_sp<SkShader> nullChild = nullptr; |
| 585 | sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild, |
| 586 | /*childCount=*/1, /*localMatrix=*/nullptr, |
| 587 | /*isOpaque=*/false); |
| 588 | |
| 589 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 590 | sk_sp<SkSurface> surface = rContext |
| 591 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 592 | : SkSurface::MakeRaster(info); |
| 593 | REPORTER_ASSERT(r, surface); |
| 594 | |
| 595 | TestEffect effect(r, surface); |
| 596 | effect.build( |
| 597 | "uniform shader child;" |
| 598 | "struct S { float2 coord; };" |
| 599 | "void process(inout S s) { s.coord = s.coord.yx; }" |
| 600 | "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); " |
| 601 | "}"); |
| 602 | effect.child("child") = child; |
| 603 | effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) { |
| 604 | paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f}); |
| 605 | }); |
| 606 | } |
| 607 | |
| 608 | DEF_TEST(SkRuntimeStructNameReuse, r) { |
| 609 | test_RuntimeEffectStructNameReuse(r, nullptr); |
| 610 | } |
| 611 | |
| 612 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) { |
| 613 | test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext()); |
| 614 | } |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 615 | |
| 616 | DEF_TEST(SkRuntimeColorFilterFlags, r) { |
| 617 | { // Here's a non-trivial filter that doesn't change alpha. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 618 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 619 | "half4 main(half4 color) { return color + half4(1,1,1,0); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 620 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 621 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 622 | REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged()); |
| 623 | } |
| 624 | |
| 625 | { // Here's one that definitely changes alpha. |
Brian Osman | 56ed7da | 2021-04-21 15:57:27 -0400 | [diff] [blame] | 626 | auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 627 | "half4 main(half4 color) { return color + half4(0,0,0,4); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 628 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 629 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 630 | REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged()); |
| 631 | } |
| 632 | } |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 633 | |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 634 | DEF_TEST(SkRuntimeShaderSampleCoords, r) { |
| 635 | // This test verifies that we detect calls to sample where the coords are the same as those |
| 636 | // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough" |
| 637 | // sampling. This optimization is implemented very conservatively. |
| 638 | // |
| 639 | // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect |
| 640 | // FP, depending on how the coords parameter to main is used. |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 641 | |
| 642 | auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) { |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 643 | auto [effect, err] = |
| 644 | SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src)); |
| 645 | REPORTER_ASSERT(r, effect); |
| 646 | |
| 647 | auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 }); |
Brian Osman | 443b5da | 2021-06-04 16:52:21 -0400 | [diff] [blame] | 648 | auto fp = GrSkSLFP::Make(effect, "test_fp", "child", std::move(child)); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 649 | REPORTER_ASSERT(r, fp); |
| 650 | |
| 651 | REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit); |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 652 | REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 653 | }; |
| 654 | |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 655 | // Cases where our optimization is valid, and works: |
| 656 | |
Brian Osman | 8cdf28f | 2021-05-24 09:52:39 -0400 | [diff] [blame] | 657 | // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call |
| 658 | // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main. |
| 659 | test("half4 main(float2 xy) { return sample(child, xy); }", false, false); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 660 | // 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] | 661 | 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] | 662 | |
| 663 | // Cases where our optimization is not valid, and does not happen: |
| 664 | |
| 665 | // Sampling with values completely unrelated to passed-in coords |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 666 | 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] | 667 | // Use of expression involving passed in coords |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 668 | 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] | 669 | // Use of coords after modification |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 670 | 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] | 671 | // Use of coords after modification via out-param call |
| 672 | test("void adjust(inout float2 xy) { xy *= 2; }" |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 673 | "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 674 | |
| 675 | // There should (must) not be any false-positive cases. There are false-negatives. |
| 676 | // In all of these cases, our optimization would be valid, but does not happen: |
| 677 | |
| 678 | // Direct use of passed-in coords, modified after use |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 679 | 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] | 680 | // Passed-in coords copied to a temp variable |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 681 | 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] | 682 | // Use of coords passed to helper function |
| 683 | test("half4 helper(float2 xy) { return sample(child, xy); }" |
Brian Osman | 70ae4c8 | 2021-05-21 16:23:06 -0400 | [diff] [blame] | 684 | "half4 main(float2 xy) { return helper(xy); }", true, true); |
Brian Osman | 4d57111 | 2021-04-27 09:10:10 -0400 | [diff] [blame] | 685 | } |
Brian Osman | 70ae91f | 2021-06-10 14:27:53 -0400 | [diff] [blame] | 686 | |
| 687 | DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) { |
| 688 | struct FpAndKey { |
| 689 | std::unique_ptr<GrFragmentProcessor> fp; |
| 690 | SkTArray<uint32_t, true> key; |
| 691 | }; |
| 692 | |
| 693 | // Constant color, but with a similar option to GrOverrideInputFragmentProcessor |
| 694 | // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform |
| 695 | auto make_color_fp = [&](SkPMColor4f color, bool specialize) { |
| 696 | auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"( |
| 697 | uniform half4 color; |
| 698 | half4 main(float2 xy) { return color; } |
| 699 | )"); |
| 700 | FpAndKey result; |
| 701 | result.fp = GrSkSLFP::Make( |
| 702 | std::move(effect), "color_fp", "color", GrSkSLFP::SpecializeIf(specialize, color)); |
| 703 | GrProcessorKeyBuilder builder(&result.key); |
| 704 | result.fp->getGLSLProcessorKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(), |
| 705 | &builder); |
| 706 | builder.flush(); |
| 707 | return result; |
| 708 | }; |
| 709 | |
| 710 | FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false), |
| 711 | uGreen = make_color_fp({0, 1, 0, 1}, false), |
| 712 | sRed = make_color_fp({1, 0, 0, 1}, true), |
| 713 | sGreen = make_color_fp({0, 1, 0, 1}, true); |
| 714 | |
| 715 | // uRed and uGreen should have the same key - they just have different uniforms |
| 716 | SkASSERT(uRed.key == uGreen.key); |
| 717 | // sRed and sGreen should have keys that are different from the uniform case, and each other |
| 718 | SkASSERT(sRed.key != uRed.key); |
| 719 | SkASSERT(sGreen.key != uRed.key); |
| 720 | SkASSERT(sRed.key != sGreen.key); |
| 721 | } |