blob: d5409e4e2df27cae08226100d12fc1d6b61a2b22 [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"
John Stilesce9a5c92021-07-30 11:20:19 -040015#include "include/effects/SkBlenders.h"
Brian Osmanee426f22020-01-02 11:55:24 -050016#include "include/effects/SkRuntimeEffect.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040017#include "include/gpu/GrDirectContext.h"
Brian Salomon5392c942021-03-30 16:14:37 -040018#include "src/core/SkColorSpacePriv.h"
Brian Osman4f009822021-04-28 09:41:06 -040019#include "src/core/SkRuntimeEffectPriv.h"
Brian Osmand9bde072020-04-15 14:18:13 -040020#include "src/core/SkTLazy.h"
Robert Phillipscc44feb2021-07-06 12:21:37 -040021#include "src/gpu/GrCaps.h"
Brian Osman62419612020-07-22 10:19:02 -040022#include "src/gpu/GrColor.h"
Brian Osman70ae91f2021-06-10 14:27:53 -040023#include "src/gpu/GrDirectContextPriv.h"
Brian Osman4d571112021-04-27 09:10:10 -040024#include "src/gpu/GrFragmentProcessor.h"
Brian Osman443b5da2021-06-04 16:52:21 -040025#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman088913a2019-12-19 15:44:56 -050026#include "tests/Test.h"
27
Brian Osmanf72dedd2020-01-08 13:19:58 -050028#include <algorithm>
Brian Osman8e2ef022020-09-30 13:26:43 -040029#include <thread>
Brian Osmanf72dedd2020-01-08 13:19:58 -050030
John Stiles7ce17512021-01-12 18:39:02 -050031void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) {
Brian Osman56ed7da2021-04-21 15:57:27 -040032 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
John Stiles7ce17512021-01-12 18:39:02 -050033 REPORTER_ASSERT(r, !effect);
34 REPORTER_ASSERT(r, errorText.contains(expected),
35 "Expected error message to contain \"%s\". Actual message: \"%s\"",
36 expected, errorText.c_str());
37};
Brian Osman088913a2019-12-19 15:44:56 -050038
Brian Osman56ed7da2021-04-21 15:57:27 -040039#define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }"
Brian Osman24c18522020-11-10 16:36:01 -050040
John Stiles7ce17512021-01-12 18:39:02 -050041DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) {
Brian Osmand18967c2021-04-01 09:56:07 -040042 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
John Stiles7ce17512021-01-12 18:39:02 -050043 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
John Stiles7ce17512021-01-12 18:39:02 -050044}
Brian Osman088913a2019-12-19 15:44:56 -050045
John Stiles7ce17512021-01-12 18:39:02 -050046DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
Brian Osmana4b91692020-08-10 14:26:16 -040047 // 'in' variables aren't allowed at all:
John Stiles7ce17512021-01-12 18:39:02 -050048 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
49 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
50 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
51 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
52}
Brian Osman8783b782020-01-06 11:13:45 -050053
John Stiles7ce17512021-01-12 18:39:02 -050054DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
Brian Osman56ed7da2021-04-21 15:57:27 -040055 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
Brian Osman7da06572021-07-21 15:19:34 -040056 "function 'half4 missing()' is not defined");
John Stiles7ce17512021-01-12 18:39:02 -050057}
Brian Osman182c92e2020-07-20 15:18:33 -040058
John Stiles7ce17512021-01-12 18:39:02 -050059DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
Brian Osman182c92e2020-07-20 15:18:33 -040060 // Shouldn't be possible to create an SkRuntimeEffect without "main"
John Stiles7ce17512021-01-12 18:39:02 -050061 test_invalid_effect(r, "", "main");
62}
Brian Osman82329002020-07-21 09:39:27 -040063
John Stiles7ce17512021-01-12 18:39:02 -050064DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
Brian Osmanb06301e2020-11-06 11:45:36 -050065 // sk_Caps is an internal system. It should not be visible to runtime effects
Brian Osman56ed7da2021-04-21 15:57:27 -040066 test_invalid_effect(
67 r,
68 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
69 "unknown identifier 'sk_Caps'");
John Stiles7ce17512021-01-12 18:39:02 -050070}
Brian Osmanb06301e2020-11-06 11:45:36 -050071
John Stiles65d7ab22021-04-28 15:14:09 -040072DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
73 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
Brian Osmane9ab3912021-07-02 10:17:45 -040074 SkRuntimeEffect::Options opt = SkRuntimeEffectPriv::ES3Options();
John Stiles65d7ab22021-04-28 15:14:09 -040075 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
76 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
77 };
78
79 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
80 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
81}
82
Brian Osmanaf4e2332021-04-20 12:00:10 -040083DEF_TEST(SkRuntimeEffectForColorFilter, r) {
84 // Tests that the color filter factory rejects or accepts certain SkSL constructs
85 auto test_valid = [r](const char* sksl) {
86 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -040087 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -040088 };
89
90 auto test_invalid = [r](const char* sksl, const char* expected) {
91 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
92 REPORTER_ASSERT(r, !effect);
93 REPORTER_ASSERT(r,
94 errorText.contains(expected),
95 "Expected error message to contain \"%s\". Actual message: \"%s\"",
96 expected,
97 errorText.c_str());
98 };
99
100 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
101 test_valid("half4 main(half4 c) { return c; }");
102 test_valid("float4 main(half4 c) { return c; }");
103 test_valid("half4 main(float4 c) { return c; }");
104 test_valid("float4 main(float4 c) { return c; }");
105 test_valid("vec4 main(half4 c) { return c; }");
106 test_valid("half4 main(vec4 c) { return c; }");
107 test_valid("vec4 main(vec4 c) { return c; }");
108
109 // Invalid return types
110 test_invalid("void main(half4 c) {}", "'main' must return");
111 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
112
113 // Invalid argument types (some are valid as shaders, but not color filters)
114 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
115 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
116 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
117
118 // sk_FragCoord should not be available
119 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
120
121 // Sampling a child shader requires that we pass explicit coords
122 test_valid("uniform shader child;"
123 "half4 main(half4 c) { return sample(child, c.rg); }");
Brian Osmanc9125aa2021-04-21 09:57:19 -0400124 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
125 test_invalid("uniform shader child;"
126 "half4 main(half4 c) { return sample(child, c.rg, c); }",
127 "no match for sample(shader, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400128
Brian Osmanc9125aa2021-04-21 09:57:19 -0400129 // Shader with just a color
130 test_invalid("uniform shader child;"
131 "half4 main(half4 c) { return sample(child, c); }",
132 "no match for sample(shader, half4)");
John Stiles93003912021-06-16 11:34:37 -0400133 // Coords and color in a different order
Brian Osmanc9125aa2021-04-21 09:57:19 -0400134 test_invalid("uniform shader child;"
135 "half4 main(half4 c) { return sample(child, c, c.rg); }",
136 "no match for sample(shader, half4, half2)");
137
138 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400139 test_invalid(
140 "uniform shader child;"
141 "half4 main(half4 c) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400142 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400143 test_invalid(
144 "uniform shader child;"
145 "half4 main(half4 c) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400146 "no match for sample(shader, float3x3)");
147
148 // Sampling a colorFilter requires a color. No other signatures are valid.
149 test_valid("uniform colorFilter child;"
150 "half4 main(half4 c) { return sample(child, c); }");
151
152 test_invalid("uniform colorFilter child;"
153 "half4 main(half4 c) { return sample(child); }",
154 "sample(colorFilter)");
155 test_invalid("uniform colorFilter child;"
156 "half4 main(half4 c) { return sample(child, c.rg); }",
157 "sample(colorFilter, half2)");
158 test_invalid("uniform colorFilter child;"
159 "half4 main(half4 c) { return sample(child, c.rg, c); }",
160 "sample(colorFilter, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400161}
162
John Stiles93003912021-06-16 11:34:37 -0400163DEF_TEST(SkRuntimeEffectForBlender, r) {
164 // Tests that the blender factory rejects or accepts certain SkSL constructs
165 auto test_valid = [r](const char* sksl) {
166 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
167 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
168 };
169
170 auto test_invalid = [r](const char* sksl, const char* expected) {
171 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
172 REPORTER_ASSERT(r, !effect);
173 REPORTER_ASSERT(r,
174 errorText.contains(expected),
175 "Expected error message to contain \"%s\". Actual message: \"%s\"",
176 expected,
177 errorText.c_str());
178 };
179
John Stiles8e51bad2021-07-22 10:13:30 -0400180 // Blenders must use the 'half4 main(half4, half4)' signature. Any mixture of float4/vec4/half4
181 // is allowed.
John Stiles93003912021-06-16 11:34:37 -0400182 test_valid("half4 main(half4 s, half4 d) { return s; }");
183 test_valid("float4 main(float4 s, float4 d) { return d; }");
184 test_valid("float4 main(half4 s, float4 d) { return s; }");
185 test_valid("half4 main(float4 s, half4 d) { return d; }");
186 test_valid("vec4 main(half4 s, half4 d) { return s; }");
187 test_valid("half4 main(vec4 s, vec4 d) { return d; }");
188 test_valid("vec4 main(vec4 s, vec4 d) { return s; }");
189
190 // Invalid return types
191 test_invalid("void main(half4 s, half4 d) {}", "'main' must return");
192 test_invalid("half3 main(half4 s, half4 d) { return s.rgb; }", "'main' must return");
193
194 // Invalid argument types (some are valid as shaders/color filters)
195 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
196 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
197 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
198 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
199 test_invalid("half4 main(float2 p, half4 a, half4 b) { return a; }", "'main' parameter");
200 test_invalid("half4 main(half4 a, half4 b, half4 c) { return a; }", "'main' parameter");
201
202 // sk_FragCoord should not be available
203 test_invalid("half4 main(half4 s, half4 d) { return sk_FragCoord.xy01; }",
204 "unknown identifier");
205
John Stiles8e51bad2021-07-22 10:13:30 -0400206 // Sampling a child shader requires that we pass explicit coords
207 test_valid("uniform shader child;"
208 "half4 main(half4 s, half4 d) { return sample(child, s.rg); }");
209 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
210 test_invalid("uniform shader child;"
211 "half4 main(half4 s, half4 d) { return sample(child, s.rg, d); }",
212 "no match for sample(shader, half2, half4)");
213
214 // Shader with just a color
215 test_invalid("uniform shader child;"
216 "half4 main(half4 s, half4 d) { return sample(child, s); }",
217 "no match for sample(shader, half4)");
218 // Coords and color in a different order
219 test_invalid("uniform shader child;"
220 "half4 main(half4 s, half4 d) { return sample(child, s, d.rg); }",
221 "no match for sample(shader, half4, half2)");
222
223 // Older variants that are no longer allowed
224 test_invalid("uniform shader child;"
225 "half4 main(half4 s, half4 d) { return sample(child); }",
226 "no match for sample(shader)");
227 test_invalid("uniform shader child;"
228 "half4 main(half4 s, half4 d) { return sample(child, float3x3(1)); }",
229 "no match for sample(shader, float3x3)");
230
231 // Sampling a colorFilter requires a color. No other signatures are valid.
232 test_valid("uniform colorFilter child;"
233 "half4 main(half4 s, half4 d) { return sample(child, d); }");
234
235 test_invalid("uniform colorFilter child;"
236 "half4 main(half4 s, half4 d) { return sample(child); }",
237 "sample(colorFilter)");
238 test_invalid("uniform colorFilter child;"
239 "half4 main(half4 s, half4 d) { return sample(child, d.rg); }",
240 "sample(colorFilter, half2)");
241 test_invalid("uniform colorFilter child;"
242 "half4 main(half4 s, half4 d) { return sample(child, d.rg, s); }",
243 "sample(colorFilter, half2, half4)");
John Stiles93003912021-06-16 11:34:37 -0400244}
245
Brian Osmanaf4e2332021-04-20 12:00:10 -0400246DEF_TEST(SkRuntimeEffectForShader, r) {
247 // Tests that the shader factory rejects or accepts certain SkSL constructs
Brian Osman77046a72021-07-20 13:16:57 -0400248 auto test_valid = [r](const char* sksl, SkRuntimeEffect::Options options = {}) {
249 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), options);
John Stiles65d7ab22021-04-28 15:14:09 -0400250 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400251 };
252
Brian Osman77046a72021-07-20 13:16:57 -0400253 auto test_invalid = [r](const char* sksl,
254 const char* expected,
255 SkRuntimeEffect::Options options = {}) {
Brian Osmanaf4e2332021-04-20 12:00:10 -0400256 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
257 REPORTER_ASSERT(r, !effect);
258 REPORTER_ASSERT(r,
259 errorText.contains(expected),
260 "Expected error message to contain \"%s\". Actual message: \"%s\"",
261 expected,
262 errorText.c_str());
263 };
264
265 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
266 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
267 test_valid("half4 main(float2 p) { return p.xyxy; }");
268 test_valid("float4 main(float2 p) { return p.xyxy; }");
269 test_valid("vec4 main(float2 p) { return p.xyxy; }");
270 test_valid("half4 main(vec2 p) { return p.xyxy; }");
271 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
272 test_valid("half4 main(float2 p, half4 c) { return c; }");
273 test_valid("half4 main(float2 p, float4 c) { return c; }");
274 test_valid("half4 main(float2 p, vec4 c) { return c; }");
275 test_valid("float4 main(float2 p, half4 c) { return c; }");
276 test_valid("vec4 main(float2 p, half4 c) { return c; }");
277 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
278
279 // Invalid return types
280 test_invalid("void main(float2 p) {}", "'main' must return");
281 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
282
283 // Invalid argument types (some are valid as color filters, but not shaders)
284 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
285 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
286
Brian Osman77046a72021-07-20 13:16:57 -0400287 // sk_FragCoord should be available, but only if we've enabled it via Options
288 test_invalid("half4 main(float2 p) { return sk_FragCoord.xy01; }",
289 "unknown identifier 'sk_FragCoord'");
290
291 SkRuntimeEffect::Options optionsWithFragCoord;
292 SkRuntimeEffectPriv::EnableFragCoord(&optionsWithFragCoord);
293 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }", optionsWithFragCoord);
Brian Osmanaf4e2332021-04-20 12:00:10 -0400294
295 // Sampling a child shader requires that we pass explicit coords
296 test_valid("uniform shader child;"
297 "half4 main(float2 p) { return sample(child, p); }");
298
Brian Osmanc9125aa2021-04-21 09:57:19 -0400299 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
300 test_invalid("uniform shader child;"
301 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
302 "no match for sample(shader, float2, half4)");
303
304 // Shader with just a color
305 test_invalid("uniform shader child;"
306 "half4 main(float2 p, half4 c) { return sample(child, c); }",
307 "no match for sample(shader, half4)");
308 // Coords and color in a different order
309 test_invalid("uniform shader child;"
310 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
311 "no match for sample(shader, half4, float2)");
312
313 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400314 test_invalid(
315 "uniform shader child;"
316 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400317 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400318 test_invalid(
319 "uniform shader child;"
320 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400321 "no match for sample(shader, float3x3)");
322
323 // Sampling a colorFilter requires a color. No other signatures are valid.
324 test_valid("uniform colorFilter child;"
325 "half4 main(float2 p, half4 c) { return sample(child, c); }");
326
327 test_invalid("uniform colorFilter child;"
328 "half4 main(float2 p) { return sample(child); }",
329 "sample(colorFilter)");
330 test_invalid("uniform colorFilter child;"
331 "half4 main(float2 p) { return sample(child, p); }",
332 "sample(colorFilter, float2)");
333 test_invalid("uniform colorFilter child;"
334 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
335 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400336}
337
John Stiles9b170c62021-06-18 10:14:14 -0400338using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
339
340void paint_canvas(SkCanvas* canvas, SkPaint* paint, const PreTestFn& preTestCallback) {
341 canvas->save();
342 if (preTestCallback) {
343 preTestCallback(canvas, paint);
344 }
345 canvas->drawPaint(*paint);
346 canvas->restore();
347}
348
349static void verify_2x2_surface_results(skiatest::Reporter* r,
350 const SkRuntimeEffect* effect,
351 SkSurface* surface,
352 std::array<GrColor, 4> expected) {
353 std::array<GrColor, 4> actual;
354 SkImageInfo info = surface->imageInfo();
355 if (!surface->readPixels(info, actual.data(), info.minRowBytes(), /*srcX=*/0, /*srcY=*/0)) {
356 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
357 return;
358 }
359
360 if (actual != expected) {
361 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
362 SkStringPrintf("\n"
363 "Expected: [ %08x %08x %08x %08x ]\n"
364 "Got : [ %08x %08x %08x %08x ]\n"
365 "SkSL:\n%s\n",
366 expected[0], expected[1], expected[2], expected[3],
367 actual[0], actual[1], actual[2], actual[3],
368 effect->source().c_str()));
369 }
370}
371
Brian Osmanf72dedd2020-01-08 13:19:58 -0500372class TestEffect {
373public:
Brian Osman62419612020-07-22 10:19:02 -0400374 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
375 : fReporter(r), fSurface(std::move(surface)) {}
376
Brian Osman33316412020-11-06 10:42:51 -0500377 void build(const char* src) {
Brian Osman77046a72021-07-20 13:16:57 -0400378 SkRuntimeEffect::Options options;
379 SkRuntimeEffectPriv::EnableFragCoord(&options);
380 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src), options);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500381 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400382 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500383 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
384 return;
385 }
Brian Osmand9bde072020-04-15 14:18:13 -0400386 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500387 }
388
Brian Osmana4b91692020-08-10 14:26:16 -0400389 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
390 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500391 }
John Stiles9b170c62021-06-18 10:14:14 -0400392
Brian Osman62419612020-07-22 10:19:02 -0400393 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
394 return fBuilder->child(name);
395 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500396
John Stiles9b170c62021-06-18 10:14:14 -0400397 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
John Stiles49128242021-06-16 22:37:15 -0400398 auto shader = fBuilder->makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500399 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400400 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500401 return;
402 }
403
Brian Osman62419612020-07-22 10:19:02 -0400404 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500405 SkPaint paint;
406 paint.setShader(std::move(shader));
407 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500408
John Stiles9b170c62021-06-18 10:14:14 -0400409 paint_canvas(canvas, &paint, preTestCallback);
Brian Osman62419612020-07-22 10:19:02 -0400410
John Stiles9b170c62021-06-18 10:14:14 -0400411 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500412 }
413
Brian Osman62419612020-07-22 10:19:02 -0400414 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
John Stiles9b170c62021-06-18 10:14:14 -0400415 this->test({expected, expected, expected, expected}, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500416 }
417
418private:
Brian Osman62419612020-07-22 10:19:02 -0400419 skiatest::Reporter* fReporter;
420 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400421 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500422};
423
John Stiles9b170c62021-06-18 10:14:14 -0400424class TestBlend {
425public:
426 TestBlend(skiatest::Reporter* r, sk_sp<SkSurface> surface)
427 : fReporter(r), fSurface(std::move(surface)) {}
428
429 void build(const char* src) {
430 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(src));
431 if (!effect) {
432 REPORT_FAILURE(fReporter, "effect",
433 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
434 return;
435 }
436 fBuilder.init(std::move(effect));
437 }
438
439 SkRuntimeBlendBuilder::BuilderUniform uniform(const char* name) {
440 return fBuilder->uniform(name);
441 }
442
John Stiles8e51bad2021-07-22 10:13:30 -0400443 SkRuntimeBlendBuilder::BuilderChild child(const char* name) {
444 return fBuilder->child(name);
445 }
446
John Stiles9b170c62021-06-18 10:14:14 -0400447 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
448 auto blender = fBuilder->makeBlender();
449 if (!blender) {
450 REPORT_FAILURE(fReporter, "blender", SkString("Effect didn't produce a blender"));
451 return;
452 }
453
454 SkCanvas* canvas = fSurface->getCanvas();
455 SkPaint paint;
Mike Reed3037d9f2021-07-06 11:29:45 -0400456 paint.setBlender(std::move(blender));
John Stiles9b170c62021-06-18 10:14:14 -0400457 paint.setColor(SK_ColorGRAY);
458
459 paint_canvas(canvas, &paint, preTestCallback);
460
461 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
462 }
463
464 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
465 this->test({expected, expected, expected, expected}, preTestCallback);
466 }
467
468private:
469 skiatest::Reporter* fReporter;
470 sk_sp<SkSurface> fSurface;
471 SkTLazy<SkRuntimeBlendBuilder> fBuilder;
472};
473
Brian Osman62419612020-07-22 10:19:02 -0400474// Produces a 2x2 bitmap shader, with opaque colors:
475// [ Red, Green ]
476// [ Blue, White ]
477static sk_sp<SkShader> make_RGBW_shader() {
478 SkBitmap bmp;
479 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
480 SkIRect topLeft = SkIRect::MakeWH(1, 1);
481 bmp.pixmap().erase(SK_ColorRED, topLeft);
482 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
483 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
484 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500485 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400486}
487
Robert Phillipse94b4e12020-07-23 13:54:35 -0400488static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500489 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400490 sk_sp<SkSurface> surface = rContext
491 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
492 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500493 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400494 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500495
Brian Osman504032e2020-01-10 10:05:24 -0500496 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400497 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500498
Brian Osman62419612020-07-22 10:19:02 -0400499 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500500 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400501 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf72dedd2020-01-08 13:19:58 -0500502
Brian Osman62419612020-07-22 10:19:02 -0400503 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400504 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400505 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400506 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400507 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400508 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400509
Brian Osmand18967c2021-04-01 09:56:07 -0400510 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400511 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400512 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
513 effect.test(0xFFBF4000);
514 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
515 effect.test(0x7F00007F); // Tests that we clamp to valid premul
516
Brian Osman62419612020-07-22 10:19:02 -0400517 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
518 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
519 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400520 effect.build(
521 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400522 effect.test({0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F},
Brian Osman62419612020-07-22 10:19:02 -0400523 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400524
Brian Osman0acb5b52020-09-02 13:45:47 -0400525 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500526 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400527 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman0acb5b52020-09-02 13:45:47 -0400528
Brian Osman33316412020-11-06 10:42:51 -0500529 // ... and support *returning* float4 (aka vec4), not just half4
530 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400531 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman33316412020-11-06 10:42:51 -0500532 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400533 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf1319c32020-10-13 09:34:23 -0400534
Brian Osmanb4ce9442020-11-11 09:18:02 -0500535 // Mutating coords should work. (skbug.com/10918)
536 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400537 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500538 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
539 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400540 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500541
Brian Osmanb5f0f522020-07-23 13:28:14 -0400542 //
543 // Sampling children
544 //
545
Brian Osman62419612020-07-22 10:19:02 -0400546 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500547 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400548 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400549 effect.child("child") = nullptr;
550 effect.test(0xFF00FFFF,
551 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
552
553 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
554
Brian Osman56ed7da2021-04-21 15:57:27 -0400555 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500556 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400557 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400558 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400559 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400560
561 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500562 effect.build("uniform shader child;"
563 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400564 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400565 effect.test({0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400566
Brian Osmanb5f0f522020-07-23 13:28:14 -0400567 //
568 // Helper functions
569 //
570
571 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500572 effect.build("float2 helper(float2 x) { return x + 1; }"
573 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400574 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500575}
576
577DEF_TEST(SkRuntimeEffectSimple, r) {
578 test_RuntimeEffect_Shaders(r, nullptr);
579}
580
581DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400582 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500583}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400584
John Stiles9b170c62021-06-18 10:14:14 -0400585static void test_RuntimeEffect_Blenders(skiatest::Reporter* r, GrRecordingContext* rContext) {
586 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
587 sk_sp<SkSurface> surface = rContext
588 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
589 : SkSurface::MakeRaster(info);
590 REPORTER_ASSERT(r, surface);
591 TestBlend effect(r, surface);
592
John Stiles8e51bad2021-07-22 10:13:30 -0400593 using float2 = std::array<float, 2>;
John Stiles9b170c62021-06-18 10:14:14 -0400594 using float4 = std::array<float, 4>;
595 using int4 = std::array<int, 4>;
596
597 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
598 effect.build("uniform float4 gColor; half4 main(half4 s, half4 d) { return half4(gColor); }");
599 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
600 effect.test(0xFFBF4000);
601 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
602 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
603
604 // Same, with integer uniforms
605 effect.build("uniform int4 gColor;"
606 "half4 main(half4 s, half4 d) { return half4(gColor) / 255.0; }");
607 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
608 effect.test(0xFFBF4000);
609 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
610 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
611
612 // Verify that mutating the source and destination colors is allowed
613 effect.build("half4 main(half4 s, half4 d) { s += d; d += s; return half4(1); }");
614 effect.test(0xFFFFFFFF);
615
616 // Verify that we can write out the source color (ignoring the dest color)
617 // This is equivalent to the kSrc blend mode.
618 effect.build("half4 main(half4 s, half4 d) { return s; }");
619 effect.test(0xFF888888);
620
621 // Fill the destination with a variety of colors (using the RGBW shader)
John Stilesce9a5c92021-07-30 11:20:19 -0400622 SkPaint rgbwPaint;
623 rgbwPaint.setShader(make_RGBW_shader());
624 rgbwPaint.setBlendMode(SkBlendMode::kSrc);
625 surface->getCanvas()->drawPaint(rgbwPaint);
John Stiles9b170c62021-06-18 10:14:14 -0400626
627 // Verify that we can read back the dest color exactly as-is (ignoring the source color)
628 // This is equivalent to the kDst blend mode.
629 effect.build("half4 main(half4 s, half4 d) { return d; }");
630 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
631
632 // Verify that we can invert the destination color (including the alpha channel).
633 // The expected outputs are the exact inverse of the previous test.
634 effect.build("half4 main(half4 s, half4 d) { return half4(1) - d; }");
635 effect.test({0x00FFFF00, 0x00FF00FF, 0x0000FFFF, 0x00000000});
636
637 // Verify that color values are clamped to 0 and 1.
638 effect.build("half4 main(half4 s, half4 d) { return half4(-1); }");
639 effect.test(0x00000000);
640 effect.build("half4 main(half4 s, half4 d) { return half4(2); }");
641 effect.test(0xFFFFFFFF);
John Stiles8e51bad2021-07-22 10:13:30 -0400642
643 //
644 // Sampling children
645 //
646
John Stilesce9a5c92021-07-30 11:20:19 -0400647 // Sampling a null shader/color filter should return the paint color.
John Stiles8e51bad2021-07-22 10:13:30 -0400648 effect.build("uniform shader child;"
649 "half4 main(half4 s, half4 d) { return sample(child, s.rg); }");
650 effect.child("child") = nullptr;
651 effect.test(0xFF00FFFF,
652 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
653
John Stilesce9a5c92021-07-30 11:20:19 -0400654 effect.build("uniform colorFilter child;"
655 "half4 main(half4 s, half4 d) { return sample(child, s); }");
656 effect.child("child") = nullptr;
657 effect.test(0xFF00FFFF,
658 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
659
660 // Sampling a null blender should do a src-over blend. Draw 50% black over RGBW to verify this.
661 surface->getCanvas()->drawPaint(rgbwPaint);
662 effect.build("uniform blender child;"
663 "half4 main(half4 s, half4 d) { return sample(child, s, d); }");
664 effect.child("child") = nullptr;
665 effect.test({0xFF000080, 0xFF008000, 0xFF800000, 0xFF808080},
666 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({0.0f, 0.0f, 0.0f, 0.497f}); });
667
John Stiles8e51bad2021-07-22 10:13:30 -0400668 // Sampling a shader at various coordinates
669 effect.build("uniform shader child;"
670 "uniform half2 pos;"
671 "half4 main(half4 s, half4 d) { return sample(child, pos); }");
672 effect.child("child") = make_RGBW_shader();
673 effect.uniform("pos") = float2{0, 0};
674 effect.test(0xFF0000FF);
675
676 effect.uniform("pos") = float2{1, 0};
677 effect.test(0xFF00FF00);
678
679 effect.uniform("pos") = float2{0, 1};
680 effect.test(0xFFFF0000);
681
682 effect.uniform("pos") = float2{1, 1};
683 effect.test(0xFFFFFFFF);
684
685 // Sampling a color filter
686 effect.build("uniform colorFilter child;"
687 "half4 main(half4 s, half4 d) { return sample(child, half4(1)); }");
688 effect.child("child") = SkColorFilters::Blend(0xFF012345, SkBlendMode::kSrc);
689 effect.test(0xFF452301);
John Stilesce9a5c92021-07-30 11:20:19 -0400690
691 // Sampling a built-in blender
692 surface->getCanvas()->drawPaint(rgbwPaint);
693 effect.build("uniform blender child;"
694 "half4 main(half4 s, half4 d) { return sample(child, s, d); }");
695 effect.child("child") = SkBlender::Mode(SkBlendMode::kPlus);
696 effect.test({0xFF4523FF, 0xFF45FF01, 0xFFFF2301, 0xFFFFFFFF},
697 [](SkCanvas*, SkPaint* paint) { paint->setColor(0xFF012345); });
698
699 // Sampling a runtime-effect blender
700 surface->getCanvas()->drawPaint(rgbwPaint);
701 effect.build("uniform blender child;"
702 "half4 main(half4 s, half4 d) { return sample(child, s, d); }");
703 effect.child("child") = SkBlenders::Arithmetic(0, 1, 1, 0, /*enforcePremul=*/false);
704 effect.test({0xFF4523FF, 0xFF45FF01, 0xFFFF2301, 0xFFFFFFFF},
705 [](SkCanvas*, SkPaint* paint) { paint->setColor(0xFF012345); });
John Stiles9b170c62021-06-18 10:14:14 -0400706}
707
708DEF_TEST(SkRuntimeEffect_Blender_CPU, r) {
John Stiles9a2aa972021-06-18 10:15:35 -0400709 test_RuntimeEffect_Blenders(r, /*rContext=*/nullptr);
John Stiles9b170c62021-06-18 10:14:14 -0400710}
711
712DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU, r, ctxInfo) {
713 test_RuntimeEffect_Blenders(r, ctxInfo.directContext());
714}
715
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400716DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
717 const char* kSource = R"(
718 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400719 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400720 )";
721
Brian Osman56ed7da2021-04-21 15:57:27 -0400722 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400723 REPORTER_ASSERT(r, effect);
724
725 // Test passes if this sequence doesn't assert. skbug.com/10667
726 SkRuntimeShaderBuilder b(std::move(effect));
727 b.uniform("x") = 0.0f;
John Stiles49128242021-06-16 22:37:15 -0400728 auto shader_0 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400729
730 b.uniform("x") = 1.0f;
John Stiles49128242021-06-16 22:37:15 -0400731 auto shader_1 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
732}
733
734DEF_TEST(SkRuntimeBlendBuilderReuse, r) {
735 const char* kSource = R"(
736 uniform half x;
737 half4 main(half4 s, half4 d) { return half4(x); }
738 )";
739
740 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForBlender(SkString(kSource)).effect;
741 REPORTER_ASSERT(r, effect);
742
743 // We should be able to construct multiple SkBlenders in a row without asserting.
744 SkRuntimeBlendBuilder b(std::move(effect));
745 for (float x = 0.0f; x <= 2.0f; x += 2.0f) {
746 b.uniform("x") = x;
747 sk_sp<SkBlender> blender = b.makeBlender();
748 }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400749}
Brian Osman8e2ef022020-09-30 13:26:43 -0400750
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500751DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
752 const char* kSource = R"(
753 uniform half x;
754 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400755 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500756 )";
757
Brian Osman56ed7da2021-04-21 15:57:27 -0400758 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500759 REPORTER_ASSERT(r, effect);
760
761 SkRuntimeShaderBuilder b(std::move(effect));
762
763 // Test passes if this sequence doesn't assert.
764 float x = 1.0f;
765 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
766
767 // add extra value to ensure that set doesn't try to use sizeof(array)
768 float origin[] = { 2.0f, 3.0f, 4.0f };
769 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
770
771#ifndef SK_DEBUG
772 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
773 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
774#endif
775
John Stiles49128242021-06-16 22:37:15 -0400776 auto shader = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500777}
778
Brian Osman8e2ef022020-09-30 13:26:43 -0400779DEF_TEST(SkRuntimeEffectThreaded, r) {
780 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
781 // This tests that we can safely use it from more than one thread, and also
782 // that programs don't refer to shared structures owned by the compiler.
783 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400784 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400785
786 std::thread threads[16];
787 for (auto& thread : threads) {
788 thread = std::thread([r]() {
Brian Osman77046a72021-07-20 13:16:57 -0400789 SkRuntimeEffect::Options options;
790 SkRuntimeEffectPriv::EnableFragCoord(&options);
791 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource), options);
Brian Osman8e2ef022020-09-30 13:26:43 -0400792 REPORTER_ASSERT(r, effect);
793 });
794 }
795
796 for (auto& thread : threads) {
797 thread.join();
798 }
799}
Mike Klein827f8c02021-02-06 09:13:01 -0600800
801DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
802 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400803 auto [effect, err] =
804 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600805 REPORTER_ASSERT(r, effect);
806 REPORTER_ASSERT(r, err.isEmpty());
807
Brian Osman56ed7da2021-04-21 15:57:27 -0400808 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600809 REPORTER_ASSERT(r, cf);
810
811 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
812 sk_srgb_singleton(), sk_srgb_singleton());
813 REPORTER_ASSERT(r, c.fR == 0.0625f);
814 REPORTER_ASSERT(r, c.fG == 0.25f);
815 REPORTER_ASSERT(r, c.fB == 0.5625f);
816 REPORTER_ASSERT(r, c.fA == 1.0f);
817}
Brian Osman8e756f32021-02-10 10:19:27 -0500818
819static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
820 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400821 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500822 "uniform shader paint;"
823 "struct S { half4 rgba; };"
824 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400825 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500826 ));
827 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
828 sk_sp<SkShader> nullChild = nullptr;
829 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
830 /*childCount=*/1, /*localMatrix=*/nullptr,
831 /*isOpaque=*/false);
832
833 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
834 sk_sp<SkSurface> surface = rContext
835 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
836 : SkSurface::MakeRaster(info);
837 REPORTER_ASSERT(r, surface);
838
839 TestEffect effect(r, surface);
840 effect.build(
841 "uniform shader child;"
842 "struct S { float2 coord; };"
843 "void process(inout S s) { s.coord = s.coord.yx; }"
844 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
845 "}");
846 effect.child("child") = child;
847 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
848 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
849 });
850}
851
852DEF_TEST(SkRuntimeStructNameReuse, r) {
853 test_RuntimeEffectStructNameReuse(r, nullptr);
854}
855
856DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
857 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
858}
Mike Kleine0d9b862021-02-16 12:00:29 -0600859
860DEF_TEST(SkRuntimeColorFilterFlags, r) {
861 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400862 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400863 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600864 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400865 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600866 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
867 }
868
869 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400870 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400871 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600872 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400873 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600874 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
875 }
876}
Brian Osman4d571112021-04-27 09:10:10 -0400877
Brian Osman70ae4c82021-05-21 16:23:06 -0400878DEF_TEST(SkRuntimeShaderSampleCoords, r) {
879 // This test verifies that we detect calls to sample where the coords are the same as those
880 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
881 // sampling. This optimization is implemented very conservatively.
882 //
883 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
884 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400885
886 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400887 auto [effect, err] =
888 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
889 REPORTER_ASSERT(r, effect);
890
891 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
Brian Osman171fba72021-06-16 17:10:21 -0400892 auto fp = GrSkSLFP::Make(effect, "test_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
893 "child", std::move(child));
Brian Osman4d571112021-04-27 09:10:10 -0400894 REPORTER_ASSERT(r, fp);
895
Brian Salomon66b500a2021-08-02 12:37:14 -0400896 REPORTER_ASSERT(r, fp->childProcessor(0)->sampleUsage().isExplicit() == expectExplicit);
897 REPORTER_ASSERT(r, fp->usesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400898 };
899
Brian Osman4d571112021-04-27 09:10:10 -0400900 // Cases where our optimization is valid, and works:
901
Brian Osman8cdf28f2021-05-24 09:52:39 -0400902 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
903 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
904 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400905 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400906 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400907
908 // Cases where our optimization is not valid, and does not happen:
909
910 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400911 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400912 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400913 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400914 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400915 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400916 // Use of coords after modification via out-param call
917 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400918 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400919
920 // There should (must) not be any false-positive cases. There are false-negatives.
921 // In all of these cases, our optimization would be valid, but does not happen:
922
923 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400924 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400925 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400926 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400927 // Use of coords passed to helper function
928 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400929 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400930}
Brian Osman70ae91f2021-06-10 14:27:53 -0400931
932DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) {
933 struct FpAndKey {
934 std::unique_ptr<GrFragmentProcessor> fp;
935 SkTArray<uint32_t, true> key;
936 };
937
Brian Osman59465892021-06-18 09:15:44 -0400938 // Constant color, but with a similar option to GrFragmentProcessor::OverrideInput
Brian Osman70ae91f2021-06-10 14:27:53 -0400939 // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform
940 auto make_color_fp = [&](SkPMColor4f color, bool specialize) {
941 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
942 uniform half4 color;
943 half4 main(float2 xy) { return color; }
944 )");
945 FpAndKey result;
Brian Osmanb2cb8172021-06-15 14:36:17 -0400946 result.fp = GrSkSLFP::Make(std::move(effect), "color_fp", /*inputFP=*/nullptr,
Brian Osman171fba72021-06-16 17:10:21 -0400947 GrSkSLFP::OptFlags::kNone,
Brian Osmanb2cb8172021-06-15 14:36:17 -0400948 "color", GrSkSLFP::SpecializeIf(specialize, color));
Brian Osman70ae91f2021-06-10 14:27:53 -0400949 GrProcessorKeyBuilder builder(&result.key);
950 result.fp->getGLSLProcessorKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(),
951 &builder);
952 builder.flush();
953 return result;
954 };
955
956 FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false),
957 uGreen = make_color_fp({0, 1, 0, 1}, false),
958 sRed = make_color_fp({1, 0, 0, 1}, true),
959 sGreen = make_color_fp({0, 1, 0, 1}, true);
960
961 // uRed and uGreen should have the same key - they just have different uniforms
962 SkASSERT(uRed.key == uGreen.key);
963 // sRed and sGreen should have keys that are different from the uniform case, and each other
964 SkASSERT(sRed.key != uRed.key);
965 SkASSERT(sGreen.key != uRed.key);
966 SkASSERT(sRed.key != sGreen.key);
967}