blob: 79b05e7c014c1fd65ff76c11f72026f379069289 [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_FPOnly, r) {
Brian Osman3adc0912021-05-18 10:43:08 -040041 // Features that are only allowed in .fp files (key, in uniform, ctype, when).
Brian Osman088913a2019-12-19 15:44:56 -050042 // Ensure that these fail, and the error messages contain the relevant keyword.
John Stiles7ce17512021-01-12 18:39:02 -050043 test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key");
44 test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform");
45 test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype");
46 test_invalid_effect(r, "in bool Flag; "
47 "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when");
John Stiles7ce17512021-01-12 18:39:02 -050048}
Brian Osman088913a2019-12-19 15:44:56 -050049
John Stiles7ce17512021-01-12 18:39:02 -050050DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) {
Brian Osmand18967c2021-04-01 09:56:07 -040051 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
John Stiles7ce17512021-01-12 18:39:02 -050052 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
John Stiles7ce17512021-01-12 18:39:02 -050053}
Brian Osman088913a2019-12-19 15:44:56 -050054
John Stiles7ce17512021-01-12 18:39:02 -050055DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
Brian Osmana4b91692020-08-10 14:26:16 -040056 // 'in' variables aren't allowed at all:
John Stiles7ce17512021-01-12 18:39:02 -050057 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
58 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
59 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
60 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
61}
Brian Osman8783b782020-01-06 11:13:45 -050062
John Stiles7ce17512021-01-12 18:39:02 -050063DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
Brian Osman56ed7da2021-04-21 15:57:27 -040064 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
John Stiles7ce17512021-01-12 18:39:02 -050065 "undefined function");
66}
Brian Osman182c92e2020-07-20 15:18:33 -040067
John Stiles7ce17512021-01-12 18:39:02 -050068DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
Brian Osman182c92e2020-07-20 15:18:33 -040069 // Shouldn't be possible to create an SkRuntimeEffect without "main"
John Stiles7ce17512021-01-12 18:39:02 -050070 test_invalid_effect(r, "", "main");
71}
Brian Osman82329002020-07-21 09:39:27 -040072
John Stiles7ce17512021-01-12 18:39:02 -050073DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
Brian Osmanb06301e2020-11-06 11:45:36 -050074 // sk_Caps is an internal system. It should not be visible to runtime effects
Brian Osman56ed7da2021-04-21 15:57:27 -040075 test_invalid_effect(
76 r,
77 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
78 "unknown identifier 'sk_Caps'");
John Stiles7ce17512021-01-12 18:39:02 -050079}
Brian Osmanb06301e2020-11-06 11:45:36 -050080
John Stiles65d7ab22021-04-28 15:14:09 -040081DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
82 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
Brian Osmane9ab3912021-07-02 10:17:45 -040083 SkRuntimeEffect::Options opt = SkRuntimeEffectPriv::ES3Options();
John Stiles65d7ab22021-04-28 15:14:09 -040084 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
85 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
86 };
87
88 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
89 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
90}
91
Brian Osmanaf4e2332021-04-20 12:00:10 -040092DEF_TEST(SkRuntimeEffectForColorFilter, r) {
93 // Tests that the color filter factory rejects or accepts certain SkSL constructs
94 auto test_valid = [r](const char* sksl) {
95 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -040096 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -040097 };
98
99 auto test_invalid = [r](const char* sksl, const char* expected) {
100 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
101 REPORTER_ASSERT(r, !effect);
102 REPORTER_ASSERT(r,
103 errorText.contains(expected),
104 "Expected error message to contain \"%s\". Actual message: \"%s\"",
105 expected,
106 errorText.c_str());
107 };
108
109 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
110 test_valid("half4 main(half4 c) { return c; }");
111 test_valid("float4 main(half4 c) { return c; }");
112 test_valid("half4 main(float4 c) { return c; }");
113 test_valid("float4 main(float4 c) { return c; }");
114 test_valid("vec4 main(half4 c) { return c; }");
115 test_valid("half4 main(vec4 c) { return c; }");
116 test_valid("vec4 main(vec4 c) { return c; }");
117
118 // Invalid return types
119 test_invalid("void main(half4 c) {}", "'main' must return");
120 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
121
122 // Invalid argument types (some are valid as shaders, but not color filters)
123 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
124 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
125 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
126
127 // sk_FragCoord should not be available
128 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
129
130 // Sampling a child shader requires that we pass explicit coords
131 test_valid("uniform shader child;"
132 "half4 main(half4 c) { return sample(child, c.rg); }");
Brian Osmanc9125aa2021-04-21 09:57:19 -0400133 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
134 test_invalid("uniform shader child;"
135 "half4 main(half4 c) { return sample(child, c.rg, c); }",
136 "no match for sample(shader, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400137
Brian Osmanc9125aa2021-04-21 09:57:19 -0400138 // Shader with just a color
139 test_invalid("uniform shader child;"
140 "half4 main(half4 c) { return sample(child, c); }",
141 "no match for sample(shader, half4)");
John Stiles93003912021-06-16 11:34:37 -0400142 // Coords and color in a different order
Brian Osmanc9125aa2021-04-21 09:57:19 -0400143 test_invalid("uniform shader child;"
144 "half4 main(half4 c) { return sample(child, c, c.rg); }",
145 "no match for sample(shader, half4, half2)");
146
147 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400148 test_invalid(
149 "uniform shader child;"
150 "half4 main(half4 c) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400151 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400152 test_invalid(
153 "uniform shader child;"
154 "half4 main(half4 c) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400155 "no match for sample(shader, float3x3)");
156
157 // Sampling a colorFilter requires a color. No other signatures are valid.
158 test_valid("uniform colorFilter child;"
159 "half4 main(half4 c) { return sample(child, c); }");
160
161 test_invalid("uniform colorFilter child;"
162 "half4 main(half4 c) { return sample(child); }",
163 "sample(colorFilter)");
164 test_invalid("uniform colorFilter child;"
165 "half4 main(half4 c) { return sample(child, c.rg); }",
166 "sample(colorFilter, half2)");
167 test_invalid("uniform colorFilter child;"
168 "half4 main(half4 c) { return sample(child, c.rg, c); }",
169 "sample(colorFilter, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400170}
171
John Stiles93003912021-06-16 11:34:37 -0400172DEF_TEST(SkRuntimeEffectForBlender, r) {
173 // Tests that the blender factory rejects or accepts certain SkSL constructs
174 auto test_valid = [r](const char* sksl) {
175 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
176 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
177 };
178
179 auto test_invalid = [r](const char* sksl, const char* expected) {
180 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(sksl));
181 REPORTER_ASSERT(r, !effect);
182 REPORTER_ASSERT(r,
183 errorText.contains(expected),
184 "Expected error message to contain \"%s\". Actual message: \"%s\"",
185 expected,
186 errorText.c_str());
187 };
188
189 // Color filters must use the 'half4 main(half4, half4)' signature. Any mixture of
190 // float4/vec4/half4 is allowed.
191 test_valid("half4 main(half4 s, half4 d) { return s; }");
192 test_valid("float4 main(float4 s, float4 d) { return d; }");
193 test_valid("float4 main(half4 s, float4 d) { return s; }");
194 test_valid("half4 main(float4 s, half4 d) { return d; }");
195 test_valid("vec4 main(half4 s, half4 d) { return s; }");
196 test_valid("half4 main(vec4 s, vec4 d) { return d; }");
197 test_valid("vec4 main(vec4 s, vec4 d) { return s; }");
198
199 // Invalid return types
200 test_invalid("void main(half4 s, half4 d) {}", "'main' must return");
201 test_invalid("half3 main(half4 s, half4 d) { return s.rgb; }", "'main' must return");
202
203 // Invalid argument types (some are valid as shaders/color filters)
204 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
205 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
206 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
207 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
208 test_invalid("half4 main(float2 p, half4 a, half4 b) { return a; }", "'main' parameter");
209 test_invalid("half4 main(half4 a, half4 b, half4 c) { return a; }", "'main' parameter");
210
211 // sk_FragCoord should not be available
212 test_invalid("half4 main(half4 s, half4 d) { return sk_FragCoord.xy01; }",
213 "unknown identifier");
214
215 // Child shaders are currently unsupported in blends
216 test_invalid("uniform shader sh; half4 main(half4 s, half4 d) { return s; }",
217 "'shader' is not allowed in runtime blend");
218 test_invalid("uniform shader sh; half4 main(half4 s, half4 d) { return sample(sh, s.rg); }",
219 "unknown identifier 'sample'");
220}
221
Brian Osmanaf4e2332021-04-20 12:00:10 -0400222DEF_TEST(SkRuntimeEffectForShader, r) {
223 // Tests that the shader factory rejects or accepts certain SkSL constructs
Brian Osman77046a72021-07-20 13:16:57 -0400224 auto test_valid = [r](const char* sksl, SkRuntimeEffect::Options options = {}) {
225 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), options);
John Stiles65d7ab22021-04-28 15:14:09 -0400226 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400227 };
228
Brian Osman77046a72021-07-20 13:16:57 -0400229 auto test_invalid = [r](const char* sksl,
230 const char* expected,
231 SkRuntimeEffect::Options options = {}) {
Brian Osmanaf4e2332021-04-20 12:00:10 -0400232 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
233 REPORTER_ASSERT(r, !effect);
234 REPORTER_ASSERT(r,
235 errorText.contains(expected),
236 "Expected error message to contain \"%s\". Actual message: \"%s\"",
237 expected,
238 errorText.c_str());
239 };
240
241 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
242 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
243 test_valid("half4 main(float2 p) { return p.xyxy; }");
244 test_valid("float4 main(float2 p) { return p.xyxy; }");
245 test_valid("vec4 main(float2 p) { return p.xyxy; }");
246 test_valid("half4 main(vec2 p) { return p.xyxy; }");
247 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
248 test_valid("half4 main(float2 p, half4 c) { return c; }");
249 test_valid("half4 main(float2 p, float4 c) { return c; }");
250 test_valid("half4 main(float2 p, vec4 c) { return c; }");
251 test_valid("float4 main(float2 p, half4 c) { return c; }");
252 test_valid("vec4 main(float2 p, half4 c) { return c; }");
253 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
254
255 // Invalid return types
256 test_invalid("void main(float2 p) {}", "'main' must return");
257 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
258
259 // Invalid argument types (some are valid as color filters, but not shaders)
260 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
261 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
262
Brian Osman77046a72021-07-20 13:16:57 -0400263 // sk_FragCoord should be available, but only if we've enabled it via Options
264 test_invalid("half4 main(float2 p) { return sk_FragCoord.xy01; }",
265 "unknown identifier 'sk_FragCoord'");
266
267 SkRuntimeEffect::Options optionsWithFragCoord;
268 SkRuntimeEffectPriv::EnableFragCoord(&optionsWithFragCoord);
269 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }", optionsWithFragCoord);
Brian Osmanaf4e2332021-04-20 12:00:10 -0400270
271 // Sampling a child shader requires that we pass explicit coords
272 test_valid("uniform shader child;"
273 "half4 main(float2 p) { return sample(child, p); }");
274
Brian Osmanc9125aa2021-04-21 09:57:19 -0400275 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
276 test_invalid("uniform shader child;"
277 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
278 "no match for sample(shader, float2, half4)");
279
280 // Shader with just a color
281 test_invalid("uniform shader child;"
282 "half4 main(float2 p, half4 c) { return sample(child, c); }",
283 "no match for sample(shader, half4)");
284 // Coords and color in a different order
285 test_invalid("uniform shader child;"
286 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
287 "no match for sample(shader, half4, float2)");
288
289 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400290 test_invalid(
291 "uniform shader child;"
292 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400293 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400294 test_invalid(
295 "uniform shader child;"
296 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400297 "no match for sample(shader, float3x3)");
298
299 // Sampling a colorFilter requires a color. No other signatures are valid.
300 test_valid("uniform colorFilter child;"
301 "half4 main(float2 p, half4 c) { return sample(child, c); }");
302
303 test_invalid("uniform colorFilter child;"
304 "half4 main(float2 p) { return sample(child); }",
305 "sample(colorFilter)");
306 test_invalid("uniform colorFilter child;"
307 "half4 main(float2 p) { return sample(child, p); }",
308 "sample(colorFilter, float2)");
309 test_invalid("uniform colorFilter child;"
310 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
311 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400312}
313
John Stiles9b170c62021-06-18 10:14:14 -0400314using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
315
316void paint_canvas(SkCanvas* canvas, SkPaint* paint, const PreTestFn& preTestCallback) {
317 canvas->save();
318 if (preTestCallback) {
319 preTestCallback(canvas, paint);
320 }
321 canvas->drawPaint(*paint);
322 canvas->restore();
323}
324
325static void verify_2x2_surface_results(skiatest::Reporter* r,
326 const SkRuntimeEffect* effect,
327 SkSurface* surface,
328 std::array<GrColor, 4> expected) {
329 std::array<GrColor, 4> actual;
330 SkImageInfo info = surface->imageInfo();
331 if (!surface->readPixels(info, actual.data(), info.minRowBytes(), /*srcX=*/0, /*srcY=*/0)) {
332 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
333 return;
334 }
335
336 if (actual != expected) {
337 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
338 SkStringPrintf("\n"
339 "Expected: [ %08x %08x %08x %08x ]\n"
340 "Got : [ %08x %08x %08x %08x ]\n"
341 "SkSL:\n%s\n",
342 expected[0], expected[1], expected[2], expected[3],
343 actual[0], actual[1], actual[2], actual[3],
344 effect->source().c_str()));
345 }
346}
347
Brian Osmanf72dedd2020-01-08 13:19:58 -0500348class TestEffect {
349public:
Brian Osman62419612020-07-22 10:19:02 -0400350 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
351 : fReporter(r), fSurface(std::move(surface)) {}
352
Brian Osman33316412020-11-06 10:42:51 -0500353 void build(const char* src) {
Brian Osman77046a72021-07-20 13:16:57 -0400354 SkRuntimeEffect::Options options;
355 SkRuntimeEffectPriv::EnableFragCoord(&options);
356 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src), options);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500357 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400358 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500359 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
360 return;
361 }
Brian Osmand9bde072020-04-15 14:18:13 -0400362 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500363 }
364
Brian Osmana4b91692020-08-10 14:26:16 -0400365 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
366 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500367 }
John Stiles9b170c62021-06-18 10:14:14 -0400368
Brian Osman62419612020-07-22 10:19:02 -0400369 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
370 return fBuilder->child(name);
371 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500372
John Stiles9b170c62021-06-18 10:14:14 -0400373 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
John Stiles49128242021-06-16 22:37:15 -0400374 auto shader = fBuilder->makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500375 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400376 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500377 return;
378 }
379
Brian Osman62419612020-07-22 10:19:02 -0400380 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500381 SkPaint paint;
382 paint.setShader(std::move(shader));
383 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500384
John Stiles9b170c62021-06-18 10:14:14 -0400385 paint_canvas(canvas, &paint, preTestCallback);
Brian Osman62419612020-07-22 10:19:02 -0400386
John Stiles9b170c62021-06-18 10:14:14 -0400387 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500388 }
389
Brian Osman62419612020-07-22 10:19:02 -0400390 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
John Stiles9b170c62021-06-18 10:14:14 -0400391 this->test({expected, expected, expected, expected}, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500392 }
393
394private:
Brian Osman62419612020-07-22 10:19:02 -0400395 skiatest::Reporter* fReporter;
396 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400397 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500398};
399
John Stiles9b170c62021-06-18 10:14:14 -0400400class TestBlend {
401public:
402 TestBlend(skiatest::Reporter* r, sk_sp<SkSurface> surface)
403 : fReporter(r), fSurface(std::move(surface)) {}
404
405 void build(const char* src) {
406 auto [effect, errorText] = SkRuntimeEffect::MakeForBlender(SkString(src));
407 if (!effect) {
408 REPORT_FAILURE(fReporter, "effect",
409 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
410 return;
411 }
412 fBuilder.init(std::move(effect));
413 }
414
415 SkRuntimeBlendBuilder::BuilderUniform uniform(const char* name) {
416 return fBuilder->uniform(name);
417 }
418
419 void test(std::array<GrColor, 4> expected, PreTestFn preTestCallback = nullptr) {
420 auto blender = fBuilder->makeBlender();
421 if (!blender) {
422 REPORT_FAILURE(fReporter, "blender", SkString("Effect didn't produce a blender"));
423 return;
424 }
425
426 SkCanvas* canvas = fSurface->getCanvas();
427 SkPaint paint;
Mike Reed3037d9f2021-07-06 11:29:45 -0400428 paint.setBlender(std::move(blender));
John Stiles9b170c62021-06-18 10:14:14 -0400429 paint.setColor(SK_ColorGRAY);
430
431 paint_canvas(canvas, &paint, preTestCallback);
432
433 verify_2x2_surface_results(fReporter, fBuilder->effect(), fSurface.get(), expected);
434 }
435
436 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
437 this->test({expected, expected, expected, expected}, preTestCallback);
438 }
439
440private:
441 skiatest::Reporter* fReporter;
442 sk_sp<SkSurface> fSurface;
443 SkTLazy<SkRuntimeBlendBuilder> fBuilder;
444};
445
Brian Osman62419612020-07-22 10:19:02 -0400446// Produces a 2x2 bitmap shader, with opaque colors:
447// [ Red, Green ]
448// [ Blue, White ]
449static sk_sp<SkShader> make_RGBW_shader() {
450 SkBitmap bmp;
451 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
452 SkIRect topLeft = SkIRect::MakeWH(1, 1);
453 bmp.pixmap().erase(SK_ColorRED, topLeft);
454 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
455 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
456 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500457 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400458}
459
Robert Phillipse94b4e12020-07-23 13:54:35 -0400460static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500461 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400462 sk_sp<SkSurface> surface = rContext
463 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
464 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500465 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400466 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500467
Brian Osman504032e2020-01-10 10:05:24 -0500468 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400469 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500470
Brian Osman62419612020-07-22 10:19:02 -0400471 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500472 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400473 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf72dedd2020-01-08 13:19:58 -0500474
Brian Osman62419612020-07-22 10:19:02 -0400475 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400476 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400477 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400478 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400479 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400480 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400481
Brian Osmand18967c2021-04-01 09:56:07 -0400482 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400483 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400484 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
485 effect.test(0xFFBF4000);
486 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
487 effect.test(0x7F00007F); // Tests that we clamp to valid premul
488
Brian Osman62419612020-07-22 10:19:02 -0400489 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
490 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
491 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400492 effect.build(
493 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400494 effect.test({0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F},
Brian Osman62419612020-07-22 10:19:02 -0400495 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400496
Brian Osman0acb5b52020-09-02 13:45:47 -0400497 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500498 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400499 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman0acb5b52020-09-02 13:45:47 -0400500
Brian Osman33316412020-11-06 10:42:51 -0500501 // ... and support *returning* float4 (aka vec4), not just half4
502 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400503 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osman33316412020-11-06 10:42:51 -0500504 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400505 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanf1319c32020-10-13 09:34:23 -0400506
Brian Osmanb4ce9442020-11-11 09:18:02 -0500507 // Mutating coords should work. (skbug.com/10918)
508 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400509 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500510 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
511 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
John Stiles9b170c62021-06-18 10:14:14 -0400512 effect.test({0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF});
Brian Osmanb4ce9442020-11-11 09:18:02 -0500513
Brian Osmanb5f0f522020-07-23 13:28:14 -0400514 //
515 // Sampling children
516 //
517
Brian Osman62419612020-07-22 10:19:02 -0400518 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500519 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400520 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400521 effect.child("child") = nullptr;
522 effect.test(0xFF00FFFF,
523 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
524
525 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
526
Brian Osman56ed7da2021-04-21 15:57:27 -0400527 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500528 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400529 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400530 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400531 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400532
533 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500534 effect.build("uniform shader child;"
535 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400536 effect.child("child") = rgbwShader;
John Stiles9b170c62021-06-18 10:14:14 -0400537 effect.test({0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF});
Brian Osman62419612020-07-22 10:19:02 -0400538
Brian Osmanb5f0f522020-07-23 13:28:14 -0400539 //
540 // Helper functions
541 //
542
543 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500544 effect.build("float2 helper(float2 x) { return x + 1; }"
545 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400546 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500547}
548
549DEF_TEST(SkRuntimeEffectSimple, r) {
550 test_RuntimeEffect_Shaders(r, nullptr);
551}
552
553DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400554 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500555}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400556
John Stiles9b170c62021-06-18 10:14:14 -0400557static void test_RuntimeEffect_Blenders(skiatest::Reporter* r, GrRecordingContext* rContext) {
558 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
559 sk_sp<SkSurface> surface = rContext
560 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
561 : SkSurface::MakeRaster(info);
562 REPORTER_ASSERT(r, surface);
563 TestBlend effect(r, surface);
564
565 using float4 = std::array<float, 4>;
566 using int4 = std::array<int, 4>;
567
568 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
569 effect.build("uniform float4 gColor; half4 main(half4 s, half4 d) { return half4(gColor); }");
570 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
571 effect.test(0xFFBF4000);
572 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
573 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
574
575 // Same, with integer uniforms
576 effect.build("uniform int4 gColor;"
577 "half4 main(half4 s, half4 d) { return half4(gColor) / 255.0; }");
578 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
579 effect.test(0xFFBF4000);
580 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
581 effect.test(0x7F0000FF); // Unlike SkShaders, we don't clamp here
582
583 // Verify that mutating the source and destination colors is allowed
584 effect.build("half4 main(half4 s, half4 d) { s += d; d += s; return half4(1); }");
585 effect.test(0xFFFFFFFF);
586
587 // Verify that we can write out the source color (ignoring the dest color)
588 // This is equivalent to the kSrc blend mode.
589 effect.build("half4 main(half4 s, half4 d) { return s; }");
590 effect.test(0xFF888888);
591
592 // Fill the destination with a variety of colors (using the RGBW shader)
593 SkPaint paint;
594 paint.setShader(make_RGBW_shader());
595 paint.setBlendMode(SkBlendMode::kSrc);
596 surface->getCanvas()->drawPaint(paint);
597
598 // Verify that we can read back the dest color exactly as-is (ignoring the source color)
599 // This is equivalent to the kDst blend mode.
600 effect.build("half4 main(half4 s, half4 d) { return d; }");
601 effect.test({0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF});
602
603 // Verify that we can invert the destination color (including the alpha channel).
604 // The expected outputs are the exact inverse of the previous test.
605 effect.build("half4 main(half4 s, half4 d) { return half4(1) - d; }");
606 effect.test({0x00FFFF00, 0x00FF00FF, 0x0000FFFF, 0x00000000});
607
608 // Verify that color values are clamped to 0 and 1.
609 effect.build("half4 main(half4 s, half4 d) { return half4(-1); }");
610 effect.test(0x00000000);
611 effect.build("half4 main(half4 s, half4 d) { return half4(2); }");
612 effect.test(0xFFFFFFFF);
613}
614
615DEF_TEST(SkRuntimeEffect_Blender_CPU, r) {
John Stiles9a2aa972021-06-18 10:15:35 -0400616 test_RuntimeEffect_Blenders(r, /*rContext=*/nullptr);
John Stiles9b170c62021-06-18 10:14:14 -0400617}
618
619DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffect_Blender_GPU, r, ctxInfo) {
620 test_RuntimeEffect_Blenders(r, ctxInfo.directContext());
621}
622
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400623DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
624 const char* kSource = R"(
625 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400626 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400627 )";
628
Brian Osman56ed7da2021-04-21 15:57:27 -0400629 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400630 REPORTER_ASSERT(r, effect);
631
632 // Test passes if this sequence doesn't assert. skbug.com/10667
633 SkRuntimeShaderBuilder b(std::move(effect));
634 b.uniform("x") = 0.0f;
John Stiles49128242021-06-16 22:37:15 -0400635 auto shader_0 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400636
637 b.uniform("x") = 1.0f;
John Stiles49128242021-06-16 22:37:15 -0400638 auto shader_1 = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
639}
640
641DEF_TEST(SkRuntimeBlendBuilderReuse, r) {
642 const char* kSource = R"(
643 uniform half x;
644 half4 main(half4 s, half4 d) { return half4(x); }
645 )";
646
647 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForBlender(SkString(kSource)).effect;
648 REPORTER_ASSERT(r, effect);
649
650 // We should be able to construct multiple SkBlenders in a row without asserting.
651 SkRuntimeBlendBuilder b(std::move(effect));
652 for (float x = 0.0f; x <= 2.0f; x += 2.0f) {
653 b.uniform("x") = x;
654 sk_sp<SkBlender> blender = b.makeBlender();
655 }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400656}
Brian Osman8e2ef022020-09-30 13:26:43 -0400657
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500658DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
659 const char* kSource = R"(
660 uniform half x;
661 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400662 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500663 )";
664
Brian Osman56ed7da2021-04-21 15:57:27 -0400665 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500666 REPORTER_ASSERT(r, effect);
667
668 SkRuntimeShaderBuilder b(std::move(effect));
669
670 // Test passes if this sequence doesn't assert.
671 float x = 1.0f;
672 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
673
674 // add extra value to ensure that set doesn't try to use sizeof(array)
675 float origin[] = { 2.0f, 3.0f, 4.0f };
676 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
677
678#ifndef SK_DEBUG
679 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
680 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
681#endif
682
John Stiles49128242021-06-16 22:37:15 -0400683 auto shader = b.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/false);
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500684}
685
Brian Osman8e2ef022020-09-30 13:26:43 -0400686DEF_TEST(SkRuntimeEffectThreaded, r) {
687 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
688 // This tests that we can safely use it from more than one thread, and also
689 // that programs don't refer to shared structures owned by the compiler.
690 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400691 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400692
693 std::thread threads[16];
694 for (auto& thread : threads) {
695 thread = std::thread([r]() {
Brian Osman77046a72021-07-20 13:16:57 -0400696 SkRuntimeEffect::Options options;
697 SkRuntimeEffectPriv::EnableFragCoord(&options);
698 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource), options);
Brian Osman8e2ef022020-09-30 13:26:43 -0400699 REPORTER_ASSERT(r, effect);
700 });
701 }
702
703 for (auto& thread : threads) {
704 thread.join();
705 }
706}
Mike Klein827f8c02021-02-06 09:13:01 -0600707
708DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
709 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400710 auto [effect, err] =
711 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600712 REPORTER_ASSERT(r, effect);
713 REPORTER_ASSERT(r, err.isEmpty());
714
Brian Osman56ed7da2021-04-21 15:57:27 -0400715 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600716 REPORTER_ASSERT(r, cf);
717
718 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
719 sk_srgb_singleton(), sk_srgb_singleton());
720 REPORTER_ASSERT(r, c.fR == 0.0625f);
721 REPORTER_ASSERT(r, c.fG == 0.25f);
722 REPORTER_ASSERT(r, c.fB == 0.5625f);
723 REPORTER_ASSERT(r, c.fA == 1.0f);
724}
Brian Osman8e756f32021-02-10 10:19:27 -0500725
726static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
727 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400728 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500729 "uniform shader paint;"
730 "struct S { half4 rgba; };"
731 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400732 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500733 ));
734 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
735 sk_sp<SkShader> nullChild = nullptr;
736 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
737 /*childCount=*/1, /*localMatrix=*/nullptr,
738 /*isOpaque=*/false);
739
740 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
741 sk_sp<SkSurface> surface = rContext
742 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
743 : SkSurface::MakeRaster(info);
744 REPORTER_ASSERT(r, surface);
745
746 TestEffect effect(r, surface);
747 effect.build(
748 "uniform shader child;"
749 "struct S { float2 coord; };"
750 "void process(inout S s) { s.coord = s.coord.yx; }"
751 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
752 "}");
753 effect.child("child") = child;
754 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
755 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
756 });
757}
758
759DEF_TEST(SkRuntimeStructNameReuse, r) {
760 test_RuntimeEffectStructNameReuse(r, nullptr);
761}
762
763DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
764 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
765}
Mike Kleine0d9b862021-02-16 12:00:29 -0600766
767DEF_TEST(SkRuntimeColorFilterFlags, r) {
768 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400769 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400770 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600771 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400772 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600773 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
774 }
775
776 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400777 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400778 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600779 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400780 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600781 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
782 }
783}
Brian Osman4d571112021-04-27 09:10:10 -0400784
Brian Osman70ae4c82021-05-21 16:23:06 -0400785DEF_TEST(SkRuntimeShaderSampleCoords, r) {
786 // This test verifies that we detect calls to sample where the coords are the same as those
787 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
788 // sampling. This optimization is implemented very conservatively.
789 //
790 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
791 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400792
793 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400794 auto [effect, err] =
795 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
796 REPORTER_ASSERT(r, effect);
797
798 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
Brian Osman171fba72021-06-16 17:10:21 -0400799 auto fp = GrSkSLFP::Make(effect, "test_fp", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone,
800 "child", std::move(child));
Brian Osman4d571112021-04-27 09:10:10 -0400801 REPORTER_ASSERT(r, fp);
802
803 REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit);
Brian Osman70ae4c82021-05-21 16:23:06 -0400804 REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400805 };
806
Brian Osman4d571112021-04-27 09:10:10 -0400807 // Cases where our optimization is valid, and works:
808
Brian Osman8cdf28f2021-05-24 09:52:39 -0400809 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
810 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
811 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400812 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400813 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400814
815 // Cases where our optimization is not valid, and does not happen:
816
817 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400818 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400819 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400820 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400821 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400822 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400823 // Use of coords after modification via out-param call
824 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400825 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400826
827 // There should (must) not be any false-positive cases. There are false-negatives.
828 // In all of these cases, our optimization would be valid, but does not happen:
829
830 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400831 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400832 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400833 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400834 // Use of coords passed to helper function
835 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400836 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400837}
Brian Osman70ae91f2021-06-10 14:27:53 -0400838
839DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) {
840 struct FpAndKey {
841 std::unique_ptr<GrFragmentProcessor> fp;
842 SkTArray<uint32_t, true> key;
843 };
844
Brian Osman59465892021-06-18 09:15:44 -0400845 // Constant color, but with a similar option to GrFragmentProcessor::OverrideInput
Brian Osman70ae91f2021-06-10 14:27:53 -0400846 // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform
847 auto make_color_fp = [&](SkPMColor4f color, bool specialize) {
848 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
849 uniform half4 color;
850 half4 main(float2 xy) { return color; }
851 )");
852 FpAndKey result;
Brian Osmanb2cb8172021-06-15 14:36:17 -0400853 result.fp = GrSkSLFP::Make(std::move(effect), "color_fp", /*inputFP=*/nullptr,
Brian Osman171fba72021-06-16 17:10:21 -0400854 GrSkSLFP::OptFlags::kNone,
Brian Osmanb2cb8172021-06-15 14:36:17 -0400855 "color", GrSkSLFP::SpecializeIf(specialize, color));
Brian Osman70ae91f2021-06-10 14:27:53 -0400856 GrProcessorKeyBuilder builder(&result.key);
857 result.fp->getGLSLProcessorKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(),
858 &builder);
859 builder.flush();
860 return result;
861 };
862
863 FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false),
864 uGreen = make_color_fp({0, 1, 0, 1}, false),
865 sRed = make_color_fp({1, 0, 0, 1}, true),
866 sGreen = make_color_fp({0, 1, 0, 1}, true);
867
868 // uRed and uGreen should have the same key - they just have different uniforms
869 SkASSERT(uRed.key == uGreen.key);
870 // sRed and sGreen should have keys that are different from the uniform case, and each other
871 SkASSERT(sRed.key != uRed.key);
872 SkASSERT(sGreen.key != uRed.key);
873 SkASSERT(sRed.key != sGreen.key);
874}