blob: 205520e6eb76667b48452d15d5f236936a3d6208 [file] [log] [blame]
Brian Osman088913a2019-12-19 15:44:56 -05001/*
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 Osman62419612020-07-22 10:19:02 -04008#include "include/core/SkBitmap.h"
John Stiles49128242021-06-16 22:37:15 -04009#include "include/core/SkBlender.h"
Brian Osmanf72dedd2020-01-08 13:19:58 -050010#include "include/core/SkCanvas.h"
Brian Osman92aac1e2020-08-05 16:48:58 -040011#include "include/core/SkColorFilter.h"
Brian Osman269b21c2020-08-06 12:15:53 -040012#include "include/core/SkData.h"
Brian Osmanf72dedd2020-01-08 13:19:58 -050013#include "include/core/SkPaint.h"
14#include "include/core/SkSurface.h"
Brian Osmanee426f22020-01-02 11:55:24 -050015#include "include/effects/SkRuntimeEffect.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040016#include "include/gpu/GrDirectContext.h"
Brian Salomon5392c942021-03-30 16:14:37 -040017#include "src/core/SkColorSpacePriv.h"
Brian Osman4f009822021-04-28 09:41:06 -040018#include "src/core/SkRuntimeEffectPriv.h"
Brian Osmand9bde072020-04-15 14:18:13 -040019#include "src/core/SkTLazy.h"
Robert Phillipscc44feb2021-07-06 12:21:37 -040020#include "src/gpu/GrCaps.h"
Brian Osman62419612020-07-22 10:19:02 -040021#include "src/gpu/GrColor.h"
Brian Osman70ae91f2021-06-10 14:27:53 -040022#include "src/gpu/GrDirectContextPriv.h"
Brian Osman4d571112021-04-27 09:10:10 -040023#include "src/gpu/GrFragmentProcessor.h"
Brian Osman443b5da2021-06-04 16:52:21 -040024#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman088913a2019-12-19 15:44:56 -050025#include "tests/Test.h"
26
Brian Osmanf72dedd2020-01-08 13:19:58 -050027#include <algorithm>
Brian Osman8e2ef022020-09-30 13:26:43 -040028#include <thread>
Brian Osmanf72dedd2020-01-08 13:19:58 -050029
John Stiles7ce17512021-01-12 18:39:02 -050030void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) {
Brian Osman56ed7da2021-04-21 15:57:27 -040031 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
John Stiles7ce17512021-01-12 18:39:02 -050032 REPORTER_ASSERT(r, !effect);
33 REPORTER_ASSERT(r, errorText.contains(expected),
34 "Expected error message to contain \"%s\". Actual message: \"%s\"",
35 expected, errorText.c_str());
36};
Brian Osman088913a2019-12-19 15:44:56 -050037
Brian Osman56ed7da2021-04-21 15:57:27 -040038#define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }"
Brian Osman24c18522020-11-10 16:36:01 -050039
John Stiles7ce17512021-01-12 18:39:02 -050040DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) {
Brian Osmand18967c2021-04-01 09:56:07 -040041 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
John Stiles7ce17512021-01-12 18:39:02 -050042 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
John Stiles7ce17512021-01-12 18:39:02 -050043}
Brian Osman088913a2019-12-19 15:44:56 -050044
John Stiles7ce17512021-01-12 18:39:02 -050045DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
Brian Osmana4b91692020-08-10 14:26:16 -040046 // 'in' variables aren't allowed at all:
John Stiles7ce17512021-01-12 18:39:02 -050047 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
48 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
49 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
50 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
51}
Brian Osman8783b782020-01-06 11:13:45 -050052
John Stiles7ce17512021-01-12 18:39:02 -050053DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
Brian Osman56ed7da2021-04-21 15:57:27 -040054 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
John Stiles7ce17512021-01-12 18:39:02 -050055 "undefined function");
56}
Brian Osman182c92e2020-07-20 15:18:33 -040057
John Stiles7ce17512021-01-12 18:39:02 -050058DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
Brian Osman182c92e2020-07-20 15:18:33 -040059 // Shouldn't be possible to create an SkRuntimeEffect without "main"
John Stiles7ce17512021-01-12 18:39:02 -050060 test_invalid_effect(r, "", "main");
61}
Brian Osman82329002020-07-21 09:39:27 -040062
John Stiles7ce17512021-01-12 18:39:02 -050063DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
Brian Osmanb06301e2020-11-06 11:45:36 -050064 // sk_Caps is an internal system. It should not be visible to runtime effects
Brian Osman56ed7da2021-04-21 15:57:27 -040065 test_invalid_effect(
66 r,
67 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
68 "unknown identifier 'sk_Caps'");
John Stiles7ce17512021-01-12 18:39:02 -050069}
Brian Osmanb06301e2020-11-06 11:45:36 -050070
John Stiles65d7ab22021-04-28 15:14:09 -040071DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
72 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
Brian Osmane9ab3912021-07-02 10:17:45 -040073 SkRuntimeEffect::Options opt = SkRuntimeEffectPriv::ES3Options();
John Stiles65d7ab22021-04-28 15:14:09 -040074 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
75 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
76 };
77
78 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
79 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
80}
81
Brian Osmanaf4e2332021-04-20 12:00:10 -040082DEF_TEST(SkRuntimeEffectForColorFilter, r) {
83 // Tests that the color filter factory rejects or accepts certain SkSL constructs
84 auto test_valid = [r](const char* sksl) {
85 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -040086 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -040087 };
88
89 auto test_invalid = [r](const char* sksl, const char* expected) {
90 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
91 REPORTER_ASSERT(r, !effect);
92 REPORTER_ASSERT(r,
93 errorText.contains(expected),
94 "Expected error message to contain \"%s\". Actual message: \"%s\"",
95 expected,
96 errorText.c_str());
97 };
98
99 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
100 test_valid("half4 main(half4 c) { return c; }");
101 test_valid("float4 main(half4 c) { return c; }");
102 test_valid("half4 main(float4 c) { return c; }");
103 test_valid("float4 main(float4 c) { return c; }");
104 test_valid("vec4 main(half4 c) { return c; }");
105 test_valid("half4 main(vec4 c) { return c; }");
106 test_valid("vec4 main(vec4 c) { return c; }");
107
108 // Invalid return types
109 test_invalid("void main(half4 c) {}", "'main' must return");
110 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
111
112 // Invalid argument types (some are valid as shaders, but not color filters)
113 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
114 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
115 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
116
117 // sk_FragCoord should not be available
118 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
119
120 // Sampling a child shader requires that we pass explicit coords
121 test_valid("uniform shader child;"
122 "half4 main(half4 c) { return sample(child, c.rg); }");
Brian Osmanc9125aa2021-04-21 09:57:19 -0400123 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
124 test_invalid("uniform shader child;"
125 "half4 main(half4 c) { return sample(child, c.rg, c); }",
126 "no match for sample(shader, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400127
Brian Osmanc9125aa2021-04-21 09:57:19 -0400128 // Shader with just a color
129 test_invalid("uniform shader child;"
130 "half4 main(half4 c) { return sample(child, c); }",
131 "no match for sample(shader, half4)");
John Stiles93003912021-06-16 11:34:37 -0400132 // Coords and color in a different order
Brian Osmanc9125aa2021-04-21 09:57:19 -0400133 test_invalid("uniform shader child;"
134 "half4 main(half4 c) { return sample(child, c, c.rg); }",
135 "no match for sample(shader, half4, half2)");
136
137 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400138 test_invalid(
139 "uniform shader child;"
140 "half4 main(half4 c) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400141 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400142 test_invalid(
143 "uniform shader child;"
144 "half4 main(half4 c) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400145 "no match for sample(shader, float3x3)");
146
147 // Sampling a colorFilter requires a color. No other signatures are valid.
148 test_valid("uniform colorFilter child;"
149 "half4 main(half4 c) { return sample(child, c); }");
150
151 test_invalid("uniform colorFilter child;"
152 "half4 main(half4 c) { return sample(child); }",
153 "sample(colorFilter)");
154 test_invalid("uniform colorFilter child;"
155 "half4 main(half4 c) { return sample(child, c.rg); }",
156 "sample(colorFilter, half2)");
157 test_invalid("uniform colorFilter child;"
158 "half4 main(half4 c) { return sample(child, c.rg, c); }",
159 "sample(colorFilter, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400160}
161
John Stiles93003912021-06-16 11:34:37 -0400162DEF_TEST(SkRuntimeEffectForBlender, r) {
163 // Tests that the blender factory rejects or accepts certain SkSL constructs
164 auto test_valid = [r](const char* sksl) {
165 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
166 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
167 };
168
169 auto test_invalid = [r](const char* sksl, const char* expected) {
170 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
171 REPORTER_ASSERT(r, !effect);
172 REPORTER_ASSERT(r,
173 errorText.contains(expected),
174 "Expected error message to contain \"%s\". Actual message: \"%s\"",
175 expected,
176 errorText.c_str());
177 };
178
179 // Color filters must use the 'half4 main(half4, half4)' signature. Any mixture of
180 // float4/vec4/half4 is allowed.
181 test_valid("half4 main(half4 s, half4 d) { return s; }");
182 test_valid("float4 main(float4 s, float4 d) { return d; }");
183 test_valid("float4 main(half4 s, float4 d) { return s; }");
184 test_valid("half4 main(float4 s, half4 d) { return d; }");
185 test_valid("vec4 main(half4 s, half4 d) { return s; }");
186 test_valid("half4 main(vec4 s, vec4 d) { return d; }");
187 test_valid("vec4 main(vec4 s, vec4 d) { return s; }");
188
189 // Invalid return types
190 test_invalid("void main(half4 s, half4 d) {}", "'main' must return");
191 test_invalid("half3 main(half4 s, half4 d) { return s.rgb; }", "'main' must return");
192
193 // Invalid argument types (some are valid as shaders/color filters)
194 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
195 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
196 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
197 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
198 test_invalid("half4 main(float2 p, half4 a, half4 b) { return a; }", "'main' parameter");
199 test_invalid("half4 main(half4 a, half4 b, half4 c) { return a; }", "'main' parameter");
200
201 // sk_FragCoord should not be available
202 test_invalid("half4 main(half4 s, half4 d) { return sk_FragCoord.xy01; }",
203 "unknown identifier");
204
205 // Child shaders are currently unsupported in blends
206 test_invalid("uniform shader sh; half4 main(half4 s, half4 d) { return s; }",
207 "'shader' is not allowed in runtime blend");
208 test_invalid("uniform shader sh; half4 main(half4 s, half4 d) { return sample(sh, s.rg); }",
209 "unknown identifier 'sample'");
210}
211
Brian Osmanaf4e2332021-04-20 12:00:10 -0400212DEF_TEST(SkRuntimeEffectForShader, r) {
213 // Tests that the shader factory rejects or accepts certain SkSL constructs
Brian Osman77046a72021-07-20 13:16:57 -0400214 auto test_valid = [r](const char* sksl, SkRuntimeEffect::Options options = {}) {
215 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), options);
John Stiles65d7ab22021-04-28 15:14:09 -0400216 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400217 };
218
Brian Osman77046a72021-07-20 13:16:57 -0400219 auto test_invalid = [r](const char* sksl,
220 const char* expected,
221 SkRuntimeEffect::Options options = {}) {
Brian Osmanaf4e2332021-04-20 12:00:10 -0400222 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
223 REPORTER_ASSERT(r, !effect);
224 REPORTER_ASSERT(r,
225 errorText.contains(expected),
226 "Expected error message to contain \"%s\". Actual message: \"%s\"",
227 expected,
228 errorText.c_str());
229 };
230
231 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
232 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
233 test_valid("half4 main(float2 p) { return p.xyxy; }");
234 test_valid("float4 main(float2 p) { return p.xyxy; }");
235 test_valid("vec4 main(float2 p) { return p.xyxy; }");
236 test_valid("half4 main(vec2 p) { return p.xyxy; }");
237 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
238 test_valid("half4 main(float2 p, half4 c) { return c; }");
239 test_valid("half4 main(float2 p, float4 c) { return c; }");
240 test_valid("half4 main(float2 p, vec4 c) { return c; }");
241 test_valid("float4 main(float2 p, half4 c) { return c; }");
242 test_valid("vec4 main(float2 p, half4 c) { return c; }");
243 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
244
245 // Invalid return types
246 test_invalid("void main(float2 p) {}", "'main' must return");
247 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
248
249 // Invalid argument types (some are valid as color filters, but not shaders)
250 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
251 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
252
Brian Osman77046a72021-07-20 13:16:57 -0400253 // sk_FragCoord should be available, but only if we've enabled it via Options
254 test_invalid("half4 main(float2 p) { return sk_FragCoord.xy01; }",
255 "unknown identifier 'sk_FragCoord'");
256
257 SkRuntimeEffect::Options optionsWithFragCoord;
258 SkRuntimeEffectPriv::EnableFragCoord(&optionsWithFragCoord);
259 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }", optionsWithFragCoord);
Brian Osmanaf4e2332021-04-20 12:00:10 -0400260
261 // Sampling a child shader requires that we pass explicit coords
262 test_valid("uniform shader child;"
263 "half4 main(float2 p) { return sample(child, p); }");
264
Brian Osmanc9125aa2021-04-21 09:57:19 -0400265 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
266 test_invalid("uniform shader child;"
267 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
268 "no match for sample(shader, float2, half4)");
269
270 // Shader with just a color
271 test_invalid("uniform shader child;"
272 "half4 main(float2 p, half4 c) { return sample(child, c); }",
273 "no match for sample(shader, half4)");
274 // Coords and color in a different order
275 test_invalid("uniform shader child;"
276 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
277 "no match for sample(shader, half4, float2)");
278
279 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400280 test_invalid(
281 "uniform shader child;"
282 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400283 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400284 test_invalid(
285 "uniform shader child;"
286 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400287 "no match for sample(shader, float3x3)");
288
289 // Sampling a colorFilter requires a color. No other signatures are valid.
290 test_valid("uniform colorFilter child;"
291 "half4 main(float2 p, half4 c) { return sample(child, c); }");
292
293 test_invalid("uniform colorFilter child;"
294 "half4 main(float2 p) { return sample(child); }",
295 "sample(colorFilter)");
296 test_invalid("uniform colorFilter child;"
297 "half4 main(float2 p) { return sample(child, p); }",
298 "sample(colorFilter, float2)");
299 test_invalid("uniform colorFilter child;"
300 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
301 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400302}
303
John Stiles9b170c62021-06-18 10:14:14 -0400304using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
305
306void paint_canvas(SkCanvas* canvas, SkPaint* paint, const PreTestFn& preTestCallback) {
307 canvas->save();
308 if (preTestCallback) {
309 preTestCallback(canvas, paint);
310 }
311 canvas->drawPaint(*paint);
312 canvas->restore();
313}
314
315static void verify_2x2_surface_results(skiatest::Reporter* r,
316 const SkRuntimeEffect* effect,
317 SkSurface* surface,
318 std::array<GrColor, 4> expected) {
319 std::array<GrColor, 4> actual;
320 SkImageInfo info = surface->imageInfo();
321 if (!surface->readPixels(info, actual.data(), info.minRowBytes(), /*srcX=*/0, /*srcY=*/0)) {
322 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
323 return;
324 }
325
326 if (actual != expected) {
327 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
328 SkStringPrintf("\n"
329 "Expected: [ %08x %08x %08x %08x ]\n"
330 "Got : [ %08x %08x %08x %08x ]\n"
331 "SkSL:\n%s\n",
332 expected[0], expected[1], expected[2], expected[3],
333 actual[0], actual[1], actual[2], actual[3],
334 effect->source().c_str()));
335 }
336}
337
Brian Osmanf72dedd2020-01-08 13:19:58 -0500338class TestEffect {
339public:
Brian Osman62419612020-07-22 10:19:02 -0400340 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
341 : fReporter(r), fSurface(std::move(surface)) {}
342
Brian Osman33316412020-11-06 10:42:51 -0500343 void build(const char* src) {
Brian Osman77046a72021-07-20 13:16:57 -0400344 SkRuntimeEffect::Options options;
345 SkRuntimeEffectPriv::EnableFragCoord(&options);
346 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src), options);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500347 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400348 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500349 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
350 return;
351 }
Brian Osmand9bde072020-04-15 14:18:13 -0400352 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500353 }
354
Brian Osmana4b91692020-08-10 14:26:16 -0400355 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
356 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500357 }
John Stiles9b170c62021-06-18 10:14:14 -0400358
Brian Osman62419612020-07-22 10:19:02 -0400359 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
360 return fBuilder->child(name);
361 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500362
John Stiles9b170c62021-06-18 10:14:14 -0400363 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
John Stiles49128242021-06-16 22:37:15 -0400364 auto shader = fBuilder->makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500365 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400366 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500367 return;
368 }
369
Brian Osman62419612020-07-22 10:19:02 -0400370 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500371 SkPaint paint;
372 paint.setShader(std::move(shader));
373 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500374
John Stiles9b170c62021-06-18 10:14:14 -0400375 paint_canvas(canvas, &paint, preTestCallback);
Brian Osman62419612020-07-22 10:19:02 -0400376
John Stiles9b170c62021-06-18 10:14:14 -0400377 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500378 }
379
Brian Osman62419612020-07-22 10:19:02 -0400380 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
John Stiles9b170c62021-06-18 10:14:14 -0400381 this->test({expected, expected, expected, expected}, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500382 }
383
384private:
Brian Osman62419612020-07-22 10:19:02 -0400385 skiatest::Reporter* fReporter;
386 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400387 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500388};
389
John Stiles9b170c62021-06-18 10:14:14 -0400390class TestBlend {
391public:
392 TestBlend(skiatest::Reporter* r, sk_sp<SkSurface> surface)
393 : fReporter(r), fSurface(std::move(surface)) {}
394
395 void build(const char* src) {
396 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(src));
397 if (!effect) {
398 REPORT_FAILURE(fReporter, "effect",
399 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
400 return;
401 }
402 fBuilder.init(std::move(effect));
403 }
404
405 SkRuntimeBlendBuilder::BuilderUniform uniform(const char* name) {
406 return fBuilder->uniform(name);
407 }
408
409 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
410 auto blender = fBuilder->makeBlender();
411 if (!blender) {
412 REPORT_FAILURE(fReporter, "blender", SkString("Effect didn't produce a blender"));
413 return;
414 }
415
416 SkCanvas* canvas = fSurface->getCanvas();
417 SkPaint paint;
Mike Reed3037d9f2021-07-06 11:29:45 -0400418 paint.setBlender(std::move(blender));
John Stiles9b170c62021-06-18 10:14:14 -0400419 paint.setColor(SK_ColorGRAY);
420
421 paint_canvas(canvas, &paint, preTestCallback);
422
423 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
424 }
425
426 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
427 this->test({expected, expected, expected, expected}, preTestCallback);
428 }
429
430private:
431 skiatest::Reporter* fReporter;
432 sk_sp<SkSurface> fSurface;
433 SkTLazy<SkRuntimeBlendBuilder> fBuilder;
434};
435
Brian Osman62419612020-07-22 10:19:02 -0400436// Produces a 2x2 bitmap shader, with opaque colors:
437// [ Red, Green ]
438// [ Blue, White ]
439static sk_sp<SkShader> make_RGBW_shader() {
440 SkBitmap bmp;
441 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
442 SkIRect topLeft = SkIRect::MakeWH(1, 1);
443 bmp.pixmap().erase(SK_ColorRED, topLeft);
444 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
445 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
446 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500447 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400448}
449
Robert Phillipse94b4e12020-07-23 13:54:35 -0400450static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500451 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400452 sk_sp<SkSurface> surface = rContext
453 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
454 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500455 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400456 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500457
Brian Osman504032e2020-01-10 10:05:24 -0500458 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400459 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500460
Brian Osman62419612020-07-22 10:19:02 -0400461 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500462 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400463 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf72dedd2020-01-08 13:19:58 -0500464
Brian Osman62419612020-07-22 10:19:02 -0400465 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400466 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400467 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400468 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400469 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400470 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400471
Brian Osmand18967c2021-04-01 09:56:07 -0400472 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400473 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400474 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
475 effect.test(0xFFBF4000);
476 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
477 effect.test(0x7F00007F); // Tests that we clamp to valid premul
478
Brian Osman62419612020-07-22 10:19:02 -0400479 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
480 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
481 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400482 effect.build(
483 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400484 effect.test({0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F},
Brian Osman62419612020-07-22 10:19:02 -0400485 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400486
Brian Osman0acb5b52020-09-02 13:45:47 -0400487 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500488 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400489 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman0acb5b52020-09-02 13:45:47 -0400490
Brian Osman33316412020-11-06 10:42:51 -0500491 // ... and support *returning* float4 (aka vec4), not just half4
492 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400493 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman33316412020-11-06 10:42:51 -0500494 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400495 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf1319c32020-10-13 09:34:23 -0400496
Brian Osmanb4ce9442020-11-11 09:18:02 -0500497 // Mutating coords should work. (skbug.com/10918)
498 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400499 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500500 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
501 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400502 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500503
Brian Osmanb5f0f522020-07-23 13:28:14 -0400504 //
505 // Sampling children
506 //
507
Brian Osman62419612020-07-22 10:19:02 -0400508 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500509 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400510 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400511 effect.child("child") = nullptr;
512 effect.test(0xFF00FFFF,
513 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
514
515 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
516
Brian Osman56ed7da2021-04-21 15:57:27 -0400517 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500518 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400519 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400520 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400521 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400522
523 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500524 effect.build("uniform shader child;"
525 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400526 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400527 effect.test({0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400528
Brian Osmanb5f0f522020-07-23 13:28:14 -0400529 //
530 // Helper functions
531 //
532
533 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500534 effect.build("float2 helper(float2 x) { return x + 1; }"
535 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400536 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500537}
538
539DEF_TEST(SkRuntimeEffectSimple, r) {
540 test_RuntimeEffect_Shaders(r, nullptr);
541}
542
543DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400544 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500545}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400546
John Stiles9b170c62021-06-18 10:14:14 -0400547static void test_RuntimeEffect_Blenders(skiatest::Reporter* r, GrRecordingContext* rContext) {
548 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
549 sk_sp<SkSurface> surface = rContext
550 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
551 : SkSurface::MakeRaster(info);
552 REPORTER_ASSERT(r, surface);
553 TestBlend effect(r, surface);
554
555 using float4 = std::array<float, 4>;
556 using int4 = std::array<int, 4>;
557
558 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
559 effect.build("uniform float4 gColor; half4 main(half4 s, half4 d) { return half4(gColor); }");
560 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
561 effect.test(0xFFBF4000);
562 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
563 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
564
565 // Same, with integer uniforms
566 effect.build("uniform int4 gColor;"
567 "half4 main(half4 s, half4 d) { return half4(gColor) / 255.0; }");
568 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
569 effect.test(0xFFBF4000);
570 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
571 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
572
573 // Verify that mutating the source and destination colors is allowed
574 effect.build("half4 main(half4 s, half4 d) { s += d; d += s; return half4(1); }");
575 effect.test(0xFFFFFFFF);
576
577 // Verify that we can write out the source color (ignoring the dest color)
578 // This is equivalent to the kSrc blend mode.
579 effect.build("half4 main(half4 s, half4 d) { return s; }");
580 effect.test(0xFF888888);
581
582 // Fill the destination with a variety of colors (using the RGBW shader)
583 SkPaint paint;
584 paint.setShader(make_RGBW_shader());
585 paint.setBlendMode(SkBlendMode::kSrc);
586 surface->getCanvas()->drawPaint(paint);
587
588 // Verify that we can read back the dest color exactly as-is (ignoring the source color)
589 // This is equivalent to the kDst blend mode.
590 effect.build("half4 main(half4 s, half4 d) { return d; }");
591 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
592
593 // Verify that we can invert the destination color (including the alpha channel).
594 // The expected outputs are the exact inverse of the previous test.
595 effect.build("half4 main(half4 s, half4 d) { return half4(1) - d; }");
596 effect.test({0x00FFFF00, 0x00FF00FF, 0x0000FFFF, 0x00000000});
597
598 // Verify that color values are clamped to 0 and 1.
599 effect.build("half4 main(half4 s, half4 d) { return half4(-1); }");
600 effect.test(0x00000000);
601 effect.build("half4 main(half4 s, half4 d) { return half4(2); }");
602 effect.test(0xFFFFFFFF);
603}
604
605DEF_TEST(SkRuntimeEffect_Blender_CPU, r) {
John Stiles9a2aa972021-06-18 10:15:35 -0400606 test_RuntimeEffect_Blenders(r, /*rContext=*/nullptr);
John Stiles9b170c62021-06-18 10:14:14 -0400607}
608
609DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU, r, ctxInfo) {
610 test_RuntimeEffect_Blenders(r, ctxInfo.directContext());
611}
612
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400613DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
614 const char* kSource = R"(
615 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400616 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400617 )";
618
Brian Osman56ed7da2021-04-21 15:57:27 -0400619 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400620 REPORTER_ASSERT(r, effect);
621
622 // Test passes if this sequence doesn't assert. skbug.com/10667
623 SkRuntimeShaderBuilder b(std::move(effect));
624 b.uniform("x") = 0.0f;
John Stiles49128242021-06-16 22:37:15 -0400625 auto shader_0 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400626
627 b.uniform("x") = 1.0f;
John Stiles49128242021-06-16 22:37:15 -0400628 auto shader_1 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
629}
630
631DEF_TEST(SkRuntimeBlendBuilderReuse, r) {
632 const char* kSource = R"(
633 uniform half x;
634 half4 main(half4 s, half4 d) { return half4(x); }
635 )";
636
637 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForBlender(SkString(kSource)).effect;
638 REPORTER_ASSERT(r, effect);
639
640 // We should be able to construct multiple SkBlenders in a row without asserting.
641 SkRuntimeBlendBuilder b(std::move(effect));
642 for (float x = 0.0f; x <= 2.0f; x += 2.0f) {
643 b.uniform("x") = x;
644 sk_sp<SkBlender> blender = b.makeBlender();
645 }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400646}
Brian Osman8e2ef022020-09-30 13:26:43 -0400647
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500648DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
649 const char* kSource = R"(
650 uniform half x;
651 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400652 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500653 )";
654
Brian Osman56ed7da2021-04-21 15:57:27 -0400655 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500656 REPORTER_ASSERT(r, effect);
657
658 SkRuntimeShaderBuilder b(std::move(effect));
659
660 // Test passes if this sequence doesn't assert.
661 float x = 1.0f;
662 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
663
664 // add extra value to ensure that set doesn't try to use sizeof(array)
665 float origin[] = { 2.0f, 3.0f, 4.0f };
666 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
667
668#ifndef SK_DEBUG
669 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
670 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
671#endif
672
John Stiles49128242021-06-16 22:37:15 -0400673 auto shader = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500674}
675
Brian Osman8e2ef022020-09-30 13:26:43 -0400676DEF_TEST(SkRuntimeEffectThreaded, r) {
677 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
678 // This tests that we can safely use it from more than one thread, and also
679 // that programs don't refer to shared structures owned by the compiler.
680 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400681 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400682
683 std::thread threads[16];
684 for (auto& thread : threads) {
685 thread = std::thread([r]() {
Brian Osman77046a72021-07-20 13:16:57 -0400686 SkRuntimeEffect::Options options;
687 SkRuntimeEffectPriv::EnableFragCoord(&options);
688 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource), options);
Brian Osman8e2ef022020-09-30 13:26:43 -0400689 REPORTER_ASSERT(r, effect);
690 });
691 }
692
693 for (auto& thread : threads) {
694 thread.join();
695 }
696}
Mike Klein827f8c02021-02-06 09:13:01 -0600697
698DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
699 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400700 auto [effect, err] =
701 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600702 REPORTER_ASSERT(r, effect);
703 REPORTER_ASSERT(r, err.isEmpty());
704
Brian Osman56ed7da2021-04-21 15:57:27 -0400705 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600706 REPORTER_ASSERT(r, cf);
707
708 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
709 sk_srgb_singleton(), sk_srgb_singleton());
710 REPORTER_ASSERT(r, c.fR == 0.0625f);
711 REPORTER_ASSERT(r, c.fG == 0.25f);
712 REPORTER_ASSERT(r, c.fB == 0.5625f);
713 REPORTER_ASSERT(r, c.fA == 1.0f);
714}
Brian Osman8e756f32021-02-10 10:19:27 -0500715
716static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
717 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400718 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500719 "uniform shader paint;"
720 "struct S { half4 rgba; };"
721 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400722 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500723 ));
724 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
725 sk_sp<SkShader> nullChild = nullptr;
726 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
727 /*childCount=*/1, /*localMatrix=*/nullptr,
728 /*isOpaque=*/false);
729
730 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
731 sk_sp<SkSurface> surface = rContext
732 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
733 : SkSurface::MakeRaster(info);
734 REPORTER_ASSERT(r, surface);
735
736 TestEffect effect(r, surface);
737 effect.build(
738 "uniform shader child;"
739 "struct S { float2 coord; };"
740 "void process(inout S s) { s.coord = s.coord.yx; }"
741 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
742 "}");
743 effect.child("child") = child;
744 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
745 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
746 });
747}
748
749DEF_TEST(SkRuntimeStructNameReuse, r) {
750 test_RuntimeEffectStructNameReuse(r, nullptr);
751}
752
753DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
754 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
755}
Mike Kleine0d9b862021-02-16 12:00:29 -0600756
757DEF_TEST(SkRuntimeColorFilterFlags, r) {
758 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400759 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400760 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600761 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400762 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600763 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
764 }
765
766 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400767 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400768 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600769 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400770 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600771 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
772 }
773}
Brian Osman4d571112021-04-27 09:10:10 -0400774
Brian Osman70ae4c82021-05-21 16:23:06 -0400775DEF_TEST(SkRuntimeShaderSampleCoords, r) {
776 // This test verifies that we detect calls to sample where the coords are the same as those
777 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
778 // sampling. This optimization is implemented very conservatively.
779 //
780 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
781 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400782
783 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400784 auto [effect, err] =
785 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
786 REPORTER_ASSERT(r, effect);
787
788 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
Brian Osman171fba72021-06-16 17:10:21 -0400789 auto fp = GrSkSLFP::Make(effect, "test_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
790 "child", std::move(child));
Brian Osman4d571112021-04-27 09:10:10 -0400791 REPORTER_ASSERT(r, fp);
792
793 REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit);
Brian Osman70ae4c82021-05-21 16:23:06 -0400794 REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400795 };
796
Brian Osman4d571112021-04-27 09:10:10 -0400797 // Cases where our optimization is valid, and works:
798
Brian Osman8cdf28f2021-05-24 09:52:39 -0400799 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
800 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
801 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400802 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400803 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400804
805 // Cases where our optimization is not valid, and does not happen:
806
807 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400808 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400809 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400810 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400811 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400812 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400813 // Use of coords after modification via out-param call
814 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400815 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400816
817 // There should (must) not be any false-positive cases. There are false-negatives.
818 // In all of these cases, our optimization would be valid, but does not happen:
819
820 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400821 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400822 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400823 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400824 // Use of coords passed to helper function
825 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400826 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400827}
Brian Osman70ae91f2021-06-10 14:27:53 -0400828
829DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) {
830 struct FpAndKey {
831 std::unique_ptr<GrFragmentProcessor> fp;
832 SkTArray<uint32_t, true> key;
833 };
834
Brian Osman59465892021-06-18 09:15:44 -0400835 // Constant color, but with a similar option to GrFragmentProcessor::OverrideInput
Brian Osman70ae91f2021-06-10 14:27:53 -0400836 // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform
837 auto make_color_fp = [&](SkPMColor4f color, bool specialize) {
838 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
839 uniform half4 color;
840 half4 main(float2 xy) { return color; }
841 )");
842 FpAndKey result;
Brian Osmanb2cb8172021-06-15 14:36:17 -0400843 result.fp = GrSkSLFP::Make(std::move(effect), "color_fp", /*inputFP=*/nullptr,
Brian Osman171fba72021-06-16 17:10:21 -0400844 GrSkSLFP::OptFlags::kNone,
Brian Osmanb2cb8172021-06-15 14:36:17 -0400845 "color", GrSkSLFP::SpecializeIf(specialize, color));
Brian Osman70ae91f2021-06-10 14:27:53 -0400846 GrProcessorKeyBuilder builder(&result.key);
847 result.fp->getGLSLProcessorKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(),
848 &builder);
849 builder.flush();
850 return result;
851 };
852
853 FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false),
854 uGreen = make_color_fp({0, 1, 0, 1}, false),
855 sRed = make_color_fp({1, 0, 0, 1}, true),
856 sGreen = make_color_fp({0, 1, 0, 1}, true);
857
858 // uRed and uGreen should have the same key - they just have different uniforms
859 SkASSERT(uRed.key == uGreen.key);
860 // sRed and sGreen should have keys that are different from the uniform case, and each other
861 SkASSERT(sRed.key != uRed.key);
862 SkASSERT(sGreen.key != uRed.key);
863 SkASSERT(sRed.key != sGreen.key);
864}