blob: 9e2ebda7e03e9767d064976198b51b69d70f5cbf [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(); }",
Brian Osman7da06572021-07-21 15:19:34 -040055 "function 'half4 missing()' is not defined");
John Stiles7ce17512021-01-12 18:39:02 -050056}
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
John Stiles8e51bad2021-07-22 10:13:30 -0400179 // Blenders must use the 'half4 main(half4, half4)' signature. Any mixture of float4/vec4/half4
180 // is allowed.
John Stiles93003912021-06-16 11:34:37 -0400181 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
John Stiles8e51bad2021-07-22 10:13:30 -0400205 // Sampling a child shader requires that we pass explicit coords
206 test_valid("uniform shader child;"
207 "half4 main(half4 s, half4 d) { return sample(child, s.rg); }");
208 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
209 test_invalid("uniform shader child;"
210 "half4 main(half4 s, half4 d) { return sample(child, s.rg, d); }",
211 "no match for sample(shader, half2, half4)");
212
213 // Shader with just a color
214 test_invalid("uniform shader child;"
215 "half4 main(half4 s, half4 d) { return sample(child, s); }",
216 "no match for sample(shader, half4)");
217 // Coords and color in a different order
218 test_invalid("uniform shader child;"
219 "half4 main(half4 s, half4 d) { return sample(child, s, d.rg); }",
220 "no match for sample(shader, half4, half2)");
221
222 // Older variants that are no longer allowed
223 test_invalid("uniform shader child;"
224 "half4 main(half4 s, half4 d) { return sample(child); }",
225 "no match for sample(shader)");
226 test_invalid("uniform shader child;"
227 "half4 main(half4 s, half4 d) { return sample(child, float3x3(1)); }",
228 "no match for sample(shader, float3x3)");
229
230 // Sampling a colorFilter requires a color. No other signatures are valid.
231 test_valid("uniform colorFilter child;"
232 "half4 main(half4 s, half4 d) { return sample(child, d); }");
233
234 test_invalid("uniform colorFilter child;"
235 "half4 main(half4 s, half4 d) { return sample(child); }",
236 "sample(colorFilter)");
237 test_invalid("uniform colorFilter child;"
238 "half4 main(half4 s, half4 d) { return sample(child, d.rg); }",
239 "sample(colorFilter, half2)");
240 test_invalid("uniform colorFilter child;"
241 "half4 main(half4 s, half4 d) { return sample(child, d.rg, s); }",
242 "sample(colorFilter, half2, half4)");
John Stiles93003912021-06-16 11:34:37 -0400243}
244
Brian Osmanaf4e2332021-04-20 12:00:10 -0400245DEF_TEST(SkRuntimeEffectForShader, r) {
246 // Tests that the shader factory rejects or accepts certain SkSL constructs
Brian Osman77046a72021-07-20 13:16:57 -0400247 auto test_valid = [r](const char* sksl, SkRuntimeEffect::Options options = {}) {
248 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), options);
John Stiles65d7ab22021-04-28 15:14:09 -0400249 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400250 };
251
Brian Osman77046a72021-07-20 13:16:57 -0400252 auto test_invalid = [r](const char* sksl,
253 const char* expected,
254 SkRuntimeEffect::Options options = {}) {
Brian Osmanaf4e2332021-04-20 12:00:10 -0400255 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
256 REPORTER_ASSERT(r, !effect);
257 REPORTER_ASSERT(r,
258 errorText.contains(expected),
259 "Expected error message to contain \"%s\". Actual message: \"%s\"",
260 expected,
261 errorText.c_str());
262 };
263
264 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
265 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
266 test_valid("half4 main(float2 p) { return p.xyxy; }");
267 test_valid("float4 main(float2 p) { return p.xyxy; }");
268 test_valid("vec4 main(float2 p) { return p.xyxy; }");
269 test_valid("half4 main(vec2 p) { return p.xyxy; }");
270 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
271 test_valid("half4 main(float2 p, half4 c) { return c; }");
272 test_valid("half4 main(float2 p, float4 c) { return c; }");
273 test_valid("half4 main(float2 p, vec4 c) { return c; }");
274 test_valid("float4 main(float2 p, half4 c) { return c; }");
275 test_valid("vec4 main(float2 p, half4 c) { return c; }");
276 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
277
278 // Invalid return types
279 test_invalid("void main(float2 p) {}", "'main' must return");
280 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
281
282 // Invalid argument types (some are valid as color filters, but not shaders)
283 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
284 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
285
Brian Osman77046a72021-07-20 13:16:57 -0400286 // sk_FragCoord should be available, but only if we've enabled it via Options
287 test_invalid("half4 main(float2 p) { return sk_FragCoord.xy01; }",
288 "unknown identifier 'sk_FragCoord'");
289
290 SkRuntimeEffect::Options optionsWithFragCoord;
291 SkRuntimeEffectPriv::EnableFragCoord(&optionsWithFragCoord);
292 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }", optionsWithFragCoord);
Brian Osmanaf4e2332021-04-20 12:00:10 -0400293
294 // Sampling a child shader requires that we pass explicit coords
295 test_valid("uniform shader child;"
296 "half4 main(float2 p) { return sample(child, p); }");
297
Brian Osmanc9125aa2021-04-21 09:57:19 -0400298 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
299 test_invalid("uniform shader child;"
300 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
301 "no match for sample(shader, float2, half4)");
302
303 // Shader with just a color
304 test_invalid("uniform shader child;"
305 "half4 main(float2 p, half4 c) { return sample(child, c); }",
306 "no match for sample(shader, half4)");
307 // Coords and color in a different order
308 test_invalid("uniform shader child;"
309 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
310 "no match for sample(shader, half4, float2)");
311
312 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400313 test_invalid(
314 "uniform shader child;"
315 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400316 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400317 test_invalid(
318 "uniform shader child;"
319 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400320 "no match for sample(shader, float3x3)");
321
322 // Sampling a colorFilter requires a color. No other signatures are valid.
323 test_valid("uniform colorFilter child;"
324 "half4 main(float2 p, half4 c) { return sample(child, c); }");
325
326 test_invalid("uniform colorFilter child;"
327 "half4 main(float2 p) { return sample(child); }",
328 "sample(colorFilter)");
329 test_invalid("uniform colorFilter child;"
330 "half4 main(float2 p) { return sample(child, p); }",
331 "sample(colorFilter, float2)");
332 test_invalid("uniform colorFilter child;"
333 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
334 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400335}
336
John Stiles9b170c62021-06-18 10:14:14 -0400337using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
338
339void paint_canvas(SkCanvas* canvas, SkPaint* paint, const PreTestFn& preTestCallback) {
340 canvas->save();
341 if (preTestCallback) {
342 preTestCallback(canvas, paint);
343 }
344 canvas->drawPaint(*paint);
345 canvas->restore();
346}
347
348static void verify_2x2_surface_results(skiatest::Reporter* r,
349 const SkRuntimeEffect* effect,
350 SkSurface* surface,
351 std::array<GrColor, 4> expected) {
352 std::array<GrColor, 4> actual;
353 SkImageInfo info = surface->imageInfo();
354 if (!surface->readPixels(info, actual.data(), info.minRowBytes(), /*srcX=*/0, /*srcY=*/0)) {
355 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
356 return;
357 }
358
359 if (actual != expected) {
360 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
361 SkStringPrintf("\n"
362 "Expected: [ %08x %08x %08x %08x ]\n"
363 "Got : [ %08x %08x %08x %08x ]\n"
364 "SkSL:\n%s\n",
365 expected[0], expected[1], expected[2], expected[3],
366 actual[0], actual[1], actual[2], actual[3],
367 effect->source().c_str()));
368 }
369}
370
Brian Osmanf72dedd2020-01-08 13:19:58 -0500371class TestEffect {
372public:
Brian Osman62419612020-07-22 10:19:02 -0400373 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
374 : fReporter(r), fSurface(std::move(surface)) {}
375
Brian Osman33316412020-11-06 10:42:51 -0500376 void build(const char* src) {
Brian Osman77046a72021-07-20 13:16:57 -0400377 SkRuntimeEffect::Options options;
378 SkRuntimeEffectPriv::EnableFragCoord(&options);
379 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src), options);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500380 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400381 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500382 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
383 return;
384 }
Brian Osmand9bde072020-04-15 14:18:13 -0400385 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500386 }
387
Brian Osmana4b91692020-08-10 14:26:16 -0400388 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
389 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500390 }
John Stiles9b170c62021-06-18 10:14:14 -0400391
Brian Osman62419612020-07-22 10:19:02 -0400392 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
393 return fBuilder->child(name);
394 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500395
John Stiles9b170c62021-06-18 10:14:14 -0400396 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
John Stiles49128242021-06-16 22:37:15 -0400397 auto shader = fBuilder->makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500398 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400399 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500400 return;
401 }
402
Brian Osman62419612020-07-22 10:19:02 -0400403 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500404 SkPaint paint;
405 paint.setShader(std::move(shader));
406 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500407
John Stiles9b170c62021-06-18 10:14:14 -0400408 paint_canvas(canvas, &paint, preTestCallback);
Brian Osman62419612020-07-22 10:19:02 -0400409
John Stiles9b170c62021-06-18 10:14:14 -0400410 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500411 }
412
Brian Osman62419612020-07-22 10:19:02 -0400413 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
John Stiles9b170c62021-06-18 10:14:14 -0400414 this->test({expected, expected, expected, expected}, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500415 }
416
417private:
Brian Osman62419612020-07-22 10:19:02 -0400418 skiatest::Reporter* fReporter;
419 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400420 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500421};
422
John Stiles9b170c62021-06-18 10:14:14 -0400423class TestBlend {
424public:
425 TestBlend(skiatest::Reporter* r, sk_sp<SkSurface> surface)
426 : fReporter(r), fSurface(std::move(surface)) {}
427
428 void build(const char* src) {
429 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(src));
430 if (!effect) {
431 REPORT_FAILURE(fReporter, "effect",
432 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
433 return;
434 }
435 fBuilder.init(std::move(effect));
436 }
437
438 SkRuntimeBlendBuilder::BuilderUniform uniform(const char* name) {
439 return fBuilder->uniform(name);
440 }
441
John Stiles8e51bad2021-07-22 10:13:30 -0400442 SkRuntimeBlendBuilder::BuilderChild child(const char* name) {
443 return fBuilder->child(name);
444 }
445
John Stiles9b170c62021-06-18 10:14:14 -0400446 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
447 auto blender = fBuilder->makeBlender();
448 if (!blender) {
449 REPORT_FAILURE(fReporter, "blender", SkString("Effect didn't produce a blender"));
450 return;
451 }
452
453 SkCanvas* canvas = fSurface->getCanvas();
454 SkPaint paint;
Mike Reed3037d9f2021-07-06 11:29:45 -0400455 paint.setBlender(std::move(blender));
John Stiles9b170c62021-06-18 10:14:14 -0400456 paint.setColor(SK_ColorGRAY);
457
458 paint_canvas(canvas, &paint, preTestCallback);
459
460 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
461 }
462
463 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
464 this->test({expected, expected, expected, expected}, preTestCallback);
465 }
466
467private:
468 skiatest::Reporter* fReporter;
469 sk_sp<SkSurface> fSurface;
470 SkTLazy<SkRuntimeBlendBuilder> fBuilder;
471};
472
Brian Osman62419612020-07-22 10:19:02 -0400473// Produces a 2x2 bitmap shader, with opaque colors:
474// [ Red, Green ]
475// [ Blue, White ]
476static sk_sp<SkShader> make_RGBW_shader() {
477 SkBitmap bmp;
478 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
479 SkIRect topLeft = SkIRect::MakeWH(1, 1);
480 bmp.pixmap().erase(SK_ColorRED, topLeft);
481 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
482 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
483 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500484 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400485}
486
Robert Phillipse94b4e12020-07-23 13:54:35 -0400487static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500488 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400489 sk_sp<SkSurface> surface = rContext
490 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
491 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500492 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400493 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500494
Brian Osman504032e2020-01-10 10:05:24 -0500495 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400496 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500497
Brian Osman62419612020-07-22 10:19:02 -0400498 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500499 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400500 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf72dedd2020-01-08 13:19:58 -0500501
Brian Osman62419612020-07-22 10:19:02 -0400502 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400503 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400504 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400505 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400506 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400507 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400508
Brian Osmand18967c2021-04-01 09:56:07 -0400509 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400510 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400511 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
512 effect.test(0xFFBF4000);
513 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
514 effect.test(0x7F00007F); // Tests that we clamp to valid premul
515
Brian Osman62419612020-07-22 10:19:02 -0400516 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
517 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
518 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400519 effect.build(
520 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400521 effect.test({0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F},
Brian Osman62419612020-07-22 10:19:02 -0400522 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400523
Brian Osman0acb5b52020-09-02 13:45:47 -0400524 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500525 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400526 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman0acb5b52020-09-02 13:45:47 -0400527
Brian Osman33316412020-11-06 10:42:51 -0500528 // ... and support *returning* float4 (aka vec4), not just half4
529 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400530 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman33316412020-11-06 10:42:51 -0500531 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400532 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf1319c32020-10-13 09:34:23 -0400533
Brian Osmanb4ce9442020-11-11 09:18:02 -0500534 // Mutating coords should work. (skbug.com/10918)
535 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400536 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500537 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
538 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400539 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500540
Brian Osmanb5f0f522020-07-23 13:28:14 -0400541 //
542 // Sampling children
543 //
544
Brian Osman62419612020-07-22 10:19:02 -0400545 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500546 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400547 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400548 effect.child("child") = nullptr;
549 effect.test(0xFF00FFFF,
550 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
551
552 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
553
Brian Osman56ed7da2021-04-21 15:57:27 -0400554 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500555 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400556 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400557 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400558 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400559
560 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500561 effect.build("uniform shader child;"
562 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400563 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400564 effect.test({0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400565
Brian Osmanb5f0f522020-07-23 13:28:14 -0400566 //
567 // Helper functions
568 //
569
570 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500571 effect.build("float2 helper(float2 x) { return x + 1; }"
572 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400573 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500574}
575
576DEF_TEST(SkRuntimeEffectSimple, r) {
577 test_RuntimeEffect_Shaders(r, nullptr);
578}
579
580DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400581 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500582}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400583
John Stiles9b170c62021-06-18 10:14:14 -0400584static void test_RuntimeEffect_Blenders(skiatest::Reporter* r, GrRecordingContext* rContext) {
585 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
586 sk_sp<SkSurface> surface = rContext
587 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
588 : SkSurface::MakeRaster(info);
589 REPORTER_ASSERT(r, surface);
590 TestBlend effect(r, surface);
591
John Stiles8e51bad2021-07-22 10:13:30 -0400592 using float2 = std::array<float, 2>;
John Stiles9b170c62021-06-18 10:14:14 -0400593 using float4 = std::array<float, 4>;
594 using int4 = std::array<int, 4>;
595
596 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
597 effect.build("uniform float4 gColor; half4 main(half4 s, half4 d) { return half4(gColor); }");
598 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
599 effect.test(0xFFBF4000);
600 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
601 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
602
603 // Same, with integer uniforms
604 effect.build("uniform int4 gColor;"
605 "half4 main(half4 s, half4 d) { return half4(gColor) / 255.0; }");
606 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
607 effect.test(0xFFBF4000);
608 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
609 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
610
611 // Verify that mutating the source and destination colors is allowed
612 effect.build("half4 main(half4 s, half4 d) { s += d; d += s; return half4(1); }");
613 effect.test(0xFFFFFFFF);
614
615 // Verify that we can write out the source color (ignoring the dest color)
616 // This is equivalent to the kSrc blend mode.
617 effect.build("half4 main(half4 s, half4 d) { return s; }");
618 effect.test(0xFF888888);
619
620 // Fill the destination with a variety of colors (using the RGBW shader)
621 SkPaint paint;
622 paint.setShader(make_RGBW_shader());
623 paint.setBlendMode(SkBlendMode::kSrc);
624 surface->getCanvas()->drawPaint(paint);
625
626 // Verify that we can read back the dest color exactly as-is (ignoring the source color)
627 // This is equivalent to the kDst blend mode.
628 effect.build("half4 main(half4 s, half4 d) { return d; }");
629 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
630
631 // Verify that we can invert the destination color (including the alpha channel).
632 // The expected outputs are the exact inverse of the previous test.
633 effect.build("half4 main(half4 s, half4 d) { return half4(1) - d; }");
634 effect.test({0x00FFFF00, 0x00FF00FF, 0x0000FFFF, 0x00000000});
635
636 // Verify that color values are clamped to 0 and 1.
637 effect.build("half4 main(half4 s, half4 d) { return half4(-1); }");
638 effect.test(0x00000000);
639 effect.build("half4 main(half4 s, half4 d) { return half4(2); }");
640 effect.test(0xFFFFFFFF);
John Stiles8e51bad2021-07-22 10:13:30 -0400641
642 //
643 // Sampling children
644 //
645
646 // Sampling a null child should return the paint color
647 effect.build("uniform shader child;"
648 "half4 main(half4 s, half4 d) { return sample(child, s.rg); }");
649 effect.child("child") = nullptr;
650 effect.test(0xFF00FFFF,
651 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
652
653 // Sampling a shader at various coordinates
654 effect.build("uniform shader child;"
655 "uniform half2 pos;"
656 "half4 main(half4 s, half4 d) { return sample(child, pos); }");
657 effect.child("child") = make_RGBW_shader();
658 effect.uniform("pos") = float2{0, 0};
659 effect.test(0xFF0000FF);
660
661 effect.uniform("pos") = float2{1, 0};
662 effect.test(0xFF00FF00);
663
664 effect.uniform("pos") = float2{0, 1};
665 effect.test(0xFFFF0000);
666
667 effect.uniform("pos") = float2{1, 1};
668 effect.test(0xFFFFFFFF);
669
670 // Sampling a color filter
671 effect.build("uniform colorFilter child;"
672 "half4 main(half4 s, half4 d) { return sample(child, half4(1)); }");
673 effect.child("child") = SkColorFilters::Blend(0xFF012345, SkBlendMode::kSrc);
674 effect.test(0xFF452301);
John Stiles9b170c62021-06-18 10:14:14 -0400675}
676
677DEF_TEST(SkRuntimeEffect_Blender_CPU, r) {
John Stiles9a2aa972021-06-18 10:15:35 -0400678 test_RuntimeEffect_Blenders(r, /*rContext=*/nullptr);
John Stiles9b170c62021-06-18 10:14:14 -0400679}
680
681DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU, r, ctxInfo) {
682 test_RuntimeEffect_Blenders(r, ctxInfo.directContext());
683}
684
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400685DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
686 const char* kSource = R"(
687 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400688 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400689 )";
690
Brian Osman56ed7da2021-04-21 15:57:27 -0400691 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400692 REPORTER_ASSERT(r, effect);
693
694 // Test passes if this sequence doesn't assert. skbug.com/10667
695 SkRuntimeShaderBuilder b(std::move(effect));
696 b.uniform("x") = 0.0f;
John Stiles49128242021-06-16 22:37:15 -0400697 auto shader_0 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400698
699 b.uniform("x") = 1.0f;
John Stiles49128242021-06-16 22:37:15 -0400700 auto shader_1 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
701}
702
703DEF_TEST(SkRuntimeBlendBuilderReuse, r) {
704 const char* kSource = R"(
705 uniform half x;
706 half4 main(half4 s, half4 d) { return half4(x); }
707 )";
708
709 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForBlender(SkString(kSource)).effect;
710 REPORTER_ASSERT(r, effect);
711
712 // We should be able to construct multiple SkBlenders in a row without asserting.
713 SkRuntimeBlendBuilder b(std::move(effect));
714 for (float x = 0.0f; x <= 2.0f; x += 2.0f) {
715 b.uniform("x") = x;
716 sk_sp<SkBlender> blender = b.makeBlender();
717 }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400718}
Brian Osman8e2ef022020-09-30 13:26:43 -0400719
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500720DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
721 const char* kSource = R"(
722 uniform half x;
723 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400724 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500725 )";
726
Brian Osman56ed7da2021-04-21 15:57:27 -0400727 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500728 REPORTER_ASSERT(r, effect);
729
730 SkRuntimeShaderBuilder b(std::move(effect));
731
732 // Test passes if this sequence doesn't assert.
733 float x = 1.0f;
734 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
735
736 // add extra value to ensure that set doesn't try to use sizeof(array)
737 float origin[] = { 2.0f, 3.0f, 4.0f };
738 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
739
740#ifndef SK_DEBUG
741 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
742 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
743#endif
744
John Stiles49128242021-06-16 22:37:15 -0400745 auto shader = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500746}
747
Brian Osman8e2ef022020-09-30 13:26:43 -0400748DEF_TEST(SkRuntimeEffectThreaded, r) {
749 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
750 // This tests that we can safely use it from more than one thread, and also
751 // that programs don't refer to shared structures owned by the compiler.
752 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400753 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400754
755 std::thread threads[16];
756 for (auto& thread : threads) {
757 thread = std::thread([r]() {
Brian Osman77046a72021-07-20 13:16:57 -0400758 SkRuntimeEffect::Options options;
759 SkRuntimeEffectPriv::EnableFragCoord(&options);
760 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource), options);
Brian Osman8e2ef022020-09-30 13:26:43 -0400761 REPORTER_ASSERT(r, effect);
762 });
763 }
764
765 for (auto& thread : threads) {
766 thread.join();
767 }
768}
Mike Klein827f8c02021-02-06 09:13:01 -0600769
770DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
771 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400772 auto [effect, err] =
773 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600774 REPORTER_ASSERT(r, effect);
775 REPORTER_ASSERT(r, err.isEmpty());
776
Brian Osman56ed7da2021-04-21 15:57:27 -0400777 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600778 REPORTER_ASSERT(r, cf);
779
780 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
781 sk_srgb_singleton(), sk_srgb_singleton());
782 REPORTER_ASSERT(r, c.fR == 0.0625f);
783 REPORTER_ASSERT(r, c.fG == 0.25f);
784 REPORTER_ASSERT(r, c.fB == 0.5625f);
785 REPORTER_ASSERT(r, c.fA == 1.0f);
786}
Brian Osman8e756f32021-02-10 10:19:27 -0500787
788static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
789 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400790 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500791 "uniform shader paint;"
792 "struct S { half4 rgba; };"
793 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400794 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500795 ));
796 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
797 sk_sp<SkShader> nullChild = nullptr;
798 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
799 /*childCount=*/1, /*localMatrix=*/nullptr,
800 /*isOpaque=*/false);
801
802 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
803 sk_sp<SkSurface> surface = rContext
804 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
805 : SkSurface::MakeRaster(info);
806 REPORTER_ASSERT(r, surface);
807
808 TestEffect effect(r, surface);
809 effect.build(
810 "uniform shader child;"
811 "struct S { float2 coord; };"
812 "void process(inout S s) { s.coord = s.coord.yx; }"
813 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
814 "}");
815 effect.child("child") = child;
816 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
817 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
818 });
819}
820
821DEF_TEST(SkRuntimeStructNameReuse, r) {
822 test_RuntimeEffectStructNameReuse(r, nullptr);
823}
824
825DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
826 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
827}
Mike Kleine0d9b862021-02-16 12:00:29 -0600828
829DEF_TEST(SkRuntimeColorFilterFlags, r) {
830 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400831 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400832 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600833 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400834 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600835 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
836 }
837
838 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400839 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400840 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600841 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400842 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600843 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
844 }
845}
Brian Osman4d571112021-04-27 09:10:10 -0400846
Brian Osman70ae4c82021-05-21 16:23:06 -0400847DEF_TEST(SkRuntimeShaderSampleCoords, r) {
848 // This test verifies that we detect calls to sample where the coords are the same as those
849 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
850 // sampling. This optimization is implemented very conservatively.
851 //
852 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
853 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400854
855 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400856 auto [effect, err] =
857 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
858 REPORTER_ASSERT(r, effect);
859
860 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
Brian Osman171fba72021-06-16 17:10:21 -0400861 auto fp = GrSkSLFP::Make(effect, "test_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
862 "child", std::move(child));
Brian Osman4d571112021-04-27 09:10:10 -0400863 REPORTER_ASSERT(r, fp);
864
865 REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit);
Brian Osman70ae4c82021-05-21 16:23:06 -0400866 REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400867 };
868
Brian Osman4d571112021-04-27 09:10:10 -0400869 // Cases where our optimization is valid, and works:
870
Brian Osman8cdf28f2021-05-24 09:52:39 -0400871 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
872 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
873 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400874 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400875 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400876
877 // Cases where our optimization is not valid, and does not happen:
878
879 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400880 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400881 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400882 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400883 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400884 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400885 // Use of coords after modification via out-param call
886 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400887 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400888
889 // There should (must) not be any false-positive cases. There are false-negatives.
890 // In all of these cases, our optimization would be valid, but does not happen:
891
892 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400893 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400894 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400895 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400896 // Use of coords passed to helper function
897 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400898 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400899}
Brian Osman70ae91f2021-06-10 14:27:53 -0400900
901DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) {
902 struct FpAndKey {
903 std::unique_ptr<GrFragmentProcessor> fp;
904 SkTArray<uint32_t, true> key;
905 };
906
Brian Osman59465892021-06-18 09:15:44 -0400907 // Constant color, but with a similar option to GrFragmentProcessor::OverrideInput
Brian Osman70ae91f2021-06-10 14:27:53 -0400908 // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform
909 auto make_color_fp = [&](SkPMColor4f color, bool specialize) {
910 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
911 uniform half4 color;
912 half4 main(float2 xy) { return color; }
913 )");
914 FpAndKey result;
Brian Osmanb2cb8172021-06-15 14:36:17 -0400915 result.fp = GrSkSLFP::Make(std::move(effect), "color_fp", /*inputFP=*/nullptr,
Brian Osman171fba72021-06-16 17:10:21 -0400916 GrSkSLFP::OptFlags::kNone,
Brian Osmanb2cb8172021-06-15 14:36:17 -0400917 "color", GrSkSLFP::SpecializeIf(specialize, color));
Brian Osman70ae91f2021-06-10 14:27:53 -0400918 GrProcessorKeyBuilder builder(&result.key);
919 result.fp->getGLSLProcessorKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(),
920 &builder);
921 builder.flush();
922 return result;
923 };
924
925 FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false),
926 uGreen = make_color_fp({0, 1, 0, 1}, false),
927 sRed = make_color_fp({1, 0, 0, 1}, true),
928 sGreen = make_color_fp({0, 1, 0, 1}, true);
929
930 // uRed and uGreen should have the same key - they just have different uniforms
931 SkASSERT(uRed.key == uGreen.key);
932 // sRed and sGreen should have keys that are different from the uniform case, and each other
933 SkASSERT(sRed.key != uRed.key);
934 SkASSERT(sGreen.key != uRed.key);
935 SkASSERT(sRed.key != sGreen.key);
936}