blob: dea6c424f257d88383c7accc7f8a00379fe8d6b5 [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"
Brian Osmanf72dedd2020-01-08 13:19:58 -05009#include "include/core/SkCanvas.h"
Brian Osman92aac1e2020-08-05 16:48:58 -040010#include "include/core/SkColorFilter.h"
Brian Osman269b21c2020-08-06 12:15:53 -040011#include "include/core/SkData.h"
Brian Osmanf72dedd2020-01-08 13:19:58 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkSurface.h"
Brian Osmanee426f22020-01-02 11:55:24 -050014#include "include/effects/SkRuntimeEffect.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040015#include "include/gpu/GrDirectContext.h"
Brian Salomon5392c942021-03-30 16:14:37 -040016#include "src/core/SkColorSpacePriv.h"
Brian Osman4f009822021-04-28 09:41:06 -040017#include "src/core/SkRuntimeEffectPriv.h"
Brian Osmand9bde072020-04-15 14:18:13 -040018#include "src/core/SkTLazy.h"
Brian Osman62419612020-07-22 10:19:02 -040019#include "src/gpu/GrColor.h"
Brian Osman4d571112021-04-27 09:10:10 -040020#include "src/gpu/GrFragmentProcessor.h"
Brian Osman443b5da2021-06-04 16:52:21 -040021#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman088913a2019-12-19 15:44:56 -050022#include "tests/Test.h"
23
Brian Osmanf72dedd2020-01-08 13:19:58 -050024#include <algorithm>
Brian Osman8e2ef022020-09-30 13:26:43 -040025#include <thread>
Brian Osmanf72dedd2020-01-08 13:19:58 -050026
John Stiles7ce17512021-01-12 18:39:02 -050027void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) {
Brian Osman56ed7da2021-04-21 15:57:27 -040028 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
John Stiles7ce17512021-01-12 18:39:02 -050029 REPORTER_ASSERT(r, !effect);
30 REPORTER_ASSERT(r, errorText.contains(expected),
31 "Expected error message to contain \"%s\". Actual message: \"%s\"",
32 expected, errorText.c_str());
33};
Brian Osman088913a2019-12-19 15:44:56 -050034
Brian Osman56ed7da2021-04-21 15:57:27 -040035#define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }"
Brian Osman24c18522020-11-10 16:36:01 -050036
John Stiles7ce17512021-01-12 18:39:02 -050037DEF_TEST(SkRuntimeEffectInvalid_FPOnly, r) {
Brian Osman3adc0912021-05-18 10:43:08 -040038 // Features that are only allowed in .fp files (key, in uniform, ctype, when).
Brian Osman088913a2019-12-19 15:44:56 -050039 // Ensure that these fail, and the error messages contain the relevant keyword.
John Stiles7ce17512021-01-12 18:39:02 -050040 test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key");
41 test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform");
42 test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype");
43 test_invalid_effect(r, "in bool Flag; "
44 "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when");
John Stiles7ce17512021-01-12 18:39:02 -050045}
Brian Osman088913a2019-12-19 15:44:56 -050046
John Stiles7ce17512021-01-12 18:39:02 -050047DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) {
Brian Osmand18967c2021-04-01 09:56:07 -040048 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
John Stiles7ce17512021-01-12 18:39:02 -050049 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
John Stiles7ce17512021-01-12 18:39:02 -050050}
Brian Osman088913a2019-12-19 15:44:56 -050051
John Stiles7ce17512021-01-12 18:39:02 -050052DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
Brian Osmana4b91692020-08-10 14:26:16 -040053 // 'in' variables aren't allowed at all:
John Stiles7ce17512021-01-12 18:39:02 -050054 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
55 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
56 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
57 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
58}
Brian Osman8783b782020-01-06 11:13:45 -050059
John Stiles7ce17512021-01-12 18:39:02 -050060DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
Brian Osman56ed7da2021-04-21 15:57:27 -040061 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
John Stiles7ce17512021-01-12 18:39:02 -050062 "undefined function");
63}
Brian Osman182c92e2020-07-20 15:18:33 -040064
John Stiles7ce17512021-01-12 18:39:02 -050065DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
Brian Osman182c92e2020-07-20 15:18:33 -040066 // Shouldn't be possible to create an SkRuntimeEffect without "main"
John Stiles7ce17512021-01-12 18:39:02 -050067 test_invalid_effect(r, "", "main");
68}
Brian Osman82329002020-07-21 09:39:27 -040069
John Stiles7ce17512021-01-12 18:39:02 -050070DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
Brian Osmanb06301e2020-11-06 11:45:36 -050071 // sk_Caps is an internal system. It should not be visible to runtime effects
Brian Osman56ed7da2021-04-21 15:57:27 -040072 test_invalid_effect(
73 r,
74 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
75 "unknown identifier 'sk_Caps'");
John Stiles7ce17512021-01-12 18:39:02 -050076}
Brian Osmanb06301e2020-11-06 11:45:36 -050077
John Stiles65d7ab22021-04-28 15:14:09 -040078DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
79 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
80 SkRuntimeEffect::Options opt;
81 opt.enforceES2Restrictions = false;
82 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
83 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
84 };
85
86 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
87 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
88}
89
Brian Osmanaf4e2332021-04-20 12:00:10 -040090DEF_TEST(SkRuntimeEffectForColorFilter, r) {
91 // Tests that the color filter factory rejects or accepts certain SkSL constructs
92 auto test_valid = [r](const char* sksl) {
93 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -040094 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -040095 };
96
97 auto test_invalid = [r](const char* sksl, const char* expected) {
98 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
99 REPORTER_ASSERT(r, !effect);
100 REPORTER_ASSERT(r,
101 errorText.contains(expected),
102 "Expected error message to contain \"%s\". Actual message: \"%s\"",
103 expected,
104 errorText.c_str());
105 };
106
107 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
108 test_valid("half4 main(half4 c) { return c; }");
109 test_valid("float4 main(half4 c) { return c; }");
110 test_valid("half4 main(float4 c) { return c; }");
111 test_valid("float4 main(float4 c) { return c; }");
112 test_valid("vec4 main(half4 c) { return c; }");
113 test_valid("half4 main(vec4 c) { return c; }");
114 test_valid("vec4 main(vec4 c) { return c; }");
115
116 // Invalid return types
117 test_invalid("void main(half4 c) {}", "'main' must return");
118 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
119
120 // Invalid argument types (some are valid as shaders, but not color filters)
121 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
122 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
123 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
124
125 // sk_FragCoord should not be available
126 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
127
128 // Sampling a child shader requires that we pass explicit coords
129 test_valid("uniform shader child;"
130 "half4 main(half4 c) { return sample(child, c.rg); }");
Brian Osmanc9125aa2021-04-21 09:57:19 -0400131 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
132 test_invalid("uniform shader child;"
133 "half4 main(half4 c) { return sample(child, c.rg, c); }",
134 "no match for sample(shader, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400135
Brian Osmanc9125aa2021-04-21 09:57:19 -0400136 // Shader with just a color
137 test_invalid("uniform shader child;"
138 "half4 main(half4 c) { return sample(child, c); }",
139 "no match for sample(shader, half4)");
140 // Coords and color in a differet order
141 test_invalid("uniform shader child;"
142 "half4 main(half4 c) { return sample(child, c, c.rg); }",
143 "no match for sample(shader, half4, half2)");
144
145 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400146 test_invalid(
147 "uniform shader child;"
148 "half4 main(half4 c) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400149 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400150 test_invalid(
151 "uniform shader child;"
152 "half4 main(half4 c) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400153 "no match for sample(shader, float3x3)");
154
155 // Sampling a colorFilter requires a color. No other signatures are valid.
156 test_valid("uniform colorFilter child;"
157 "half4 main(half4 c) { return sample(child, c); }");
158
159 test_invalid("uniform colorFilter child;"
160 "half4 main(half4 c) { return sample(child); }",
161 "sample(colorFilter)");
162 test_invalid("uniform colorFilter child;"
163 "half4 main(half4 c) { return sample(child, c.rg); }",
164 "sample(colorFilter, half2)");
165 test_invalid("uniform colorFilter child;"
166 "half4 main(half4 c) { return sample(child, c.rg, c); }",
167 "sample(colorFilter, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400168}
169
170DEF_TEST(SkRuntimeEffectForShader, r) {
171 // Tests that the shader factory rejects or accepts certain SkSL constructs
172 auto test_valid = [r](const char* sksl) {
173 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -0400174 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400175 };
176
177 auto test_invalid = [r](const char* sksl, const char* expected) {
178 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
179 REPORTER_ASSERT(r, !effect);
180 REPORTER_ASSERT(r,
181 errorText.contains(expected),
182 "Expected error message to contain \"%s\". Actual message: \"%s\"",
183 expected,
184 errorText.c_str());
185 };
186
187 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
188 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
189 test_valid("half4 main(float2 p) { return p.xyxy; }");
190 test_valid("float4 main(float2 p) { return p.xyxy; }");
191 test_valid("vec4 main(float2 p) { return p.xyxy; }");
192 test_valid("half4 main(vec2 p) { return p.xyxy; }");
193 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
194 test_valid("half4 main(float2 p, half4 c) { return c; }");
195 test_valid("half4 main(float2 p, float4 c) { return c; }");
196 test_valid("half4 main(float2 p, vec4 c) { return c; }");
197 test_valid("float4 main(float2 p, half4 c) { return c; }");
198 test_valid("vec4 main(float2 p, half4 c) { return c; }");
199 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
200
201 // Invalid return types
202 test_invalid("void main(float2 p) {}", "'main' must return");
203 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
204
205 // Invalid argument types (some are valid as color filters, but not shaders)
206 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
207 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
208
209 // sk_FragCoord should be available
210 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }");
211
212 // Sampling a child shader requires that we pass explicit coords
213 test_valid("uniform shader child;"
214 "half4 main(float2 p) { return sample(child, p); }");
215
Brian Osmanc9125aa2021-04-21 09:57:19 -0400216 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
217 test_invalid("uniform shader child;"
218 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
219 "no match for sample(shader, float2, half4)");
220
221 // Shader with just a color
222 test_invalid("uniform shader child;"
223 "half4 main(float2 p, half4 c) { return sample(child, c); }",
224 "no match for sample(shader, half4)");
225 // Coords and color in a different order
226 test_invalid("uniform shader child;"
227 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
228 "no match for sample(shader, half4, float2)");
229
230 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400231 test_invalid(
232 "uniform shader child;"
233 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400234 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400235 test_invalid(
236 "uniform shader child;"
237 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400238 "no match for sample(shader, float3x3)");
239
240 // Sampling a colorFilter requires a color. No other signatures are valid.
241 test_valid("uniform colorFilter child;"
242 "half4 main(float2 p, half4 c) { return sample(child, c); }");
243
244 test_invalid("uniform colorFilter child;"
245 "half4 main(float2 p) { return sample(child); }",
246 "sample(colorFilter)");
247 test_invalid("uniform colorFilter child;"
248 "half4 main(float2 p) { return sample(child, p); }",
249 "sample(colorFilter, float2)");
250 test_invalid("uniform colorFilter child;"
251 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
252 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400253}
254
Brian Osmanf72dedd2020-01-08 13:19:58 -0500255class TestEffect {
256public:
Brian Osman62419612020-07-22 10:19:02 -0400257 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
258 : fReporter(r), fSurface(std::move(surface)) {}
259
Brian Osman33316412020-11-06 10:42:51 -0500260 void build(const char* src) {
Brian Osman56ed7da2021-04-21 15:57:27 -0400261 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500262 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400263 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500264 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
265 return;
266 }
Brian Osmand9bde072020-04-15 14:18:13 -0400267 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500268 }
269
Brian Osmana4b91692020-08-10 14:26:16 -0400270 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
271 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500272 }
Brian Osman62419612020-07-22 10:19:02 -0400273 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
274 return fBuilder->child(name);
275 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500276
Brian Osman62419612020-07-22 10:19:02 -0400277 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
278
279 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
280 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -0400281 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500282 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400283 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500284 return;
285 }
286
Brian Osman62419612020-07-22 10:19:02 -0400287 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500288 SkPaint paint;
289 paint.setShader(std::move(shader));
290 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500291
Brian Osman62419612020-07-22 10:19:02 -0400292 canvas->save();
293 if (preTestCallback) {
294 preTestCallback(canvas, &paint);
295 }
296 canvas->drawPaint(paint);
297 canvas->restore();
298
299 GrColor actual[4];
300 SkImageInfo info = fSurface->imageInfo();
301 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
302 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500303 return;
304 }
305
Brian Osman62419612020-07-22 10:19:02 -0400306 GrColor expected[4] = {TL, TR, BL, BR};
John Stilesc1c3c6d2020-08-15 23:22:53 -0400307 if (0 != memcmp(actual, expected, sizeof(actual))) {
Brian Osman62419612020-07-22 10:19:02 -0400308 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500309 SkStringPrintf("\n"
310 "Expected: [ %08x %08x %08x %08x ]\n"
311 "Got : [ %08x %08x %08x %08x ]\n"
312 "SkSL:\n%s\n",
313 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400314 actual[3], fBuilder->effect()->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500315 }
316 }
317
Brian Osman62419612020-07-22 10:19:02 -0400318 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
319 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500320 }
321
322private:
Brian Osman62419612020-07-22 10:19:02 -0400323 skiatest::Reporter* fReporter;
324 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400325 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500326};
327
Brian Osman62419612020-07-22 10:19:02 -0400328// Produces a 2x2 bitmap shader, with opaque colors:
329// [ Red, Green ]
330// [ Blue, White ]
331static sk_sp<SkShader> make_RGBW_shader() {
332 SkBitmap bmp;
333 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
334 SkIRect topLeft = SkIRect::MakeWH(1, 1);
335 bmp.pixmap().erase(SK_ColorRED, topLeft);
336 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
337 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
338 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500339 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400340}
341
Robert Phillipse94b4e12020-07-23 13:54:35 -0400342static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500343 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400344 sk_sp<SkSurface> surface = rContext
345 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
346 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500347 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400348 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500349
Brian Osman504032e2020-01-10 10:05:24 -0500350 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400351 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500352
Brian Osman62419612020-07-22 10:19:02 -0400353 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500354 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
Brian Osman62419612020-07-22 10:19:02 -0400355 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500356
Brian Osman62419612020-07-22 10:19:02 -0400357 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400358 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400359 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400360 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400361 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400362 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400363
Brian Osmand18967c2021-04-01 09:56:07 -0400364 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400365 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400366 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
367 effect.test(0xFFBF4000);
368 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
369 effect.test(0x7F00007F); // Tests that we clamp to valid premul
370
Brian Osman62419612020-07-22 10:19:02 -0400371 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
372 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
373 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400374 effect.build(
375 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
Brian Osman62419612020-07-22 10:19:02 -0400376 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
377 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400378
Brian Osman0acb5b52020-09-02 13:45:47 -0400379 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500380 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
Brian Osman0acb5b52020-09-02 13:45:47 -0400381 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
382
Brian Osman33316412020-11-06 10:42:51 -0500383 // ... and support *returning* float4 (aka vec4), not just half4
384 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
385 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
386 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
Brian Osmanf1319c32020-10-13 09:34:23 -0400387 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
388
Brian Osmanb4ce9442020-11-11 09:18:02 -0500389 // Mutating coords should work. (skbug.com/10918)
390 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
391 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
392 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
393 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
394 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
395
Brian Osmanb5f0f522020-07-23 13:28:14 -0400396 //
397 // Sampling children
398 //
399
Brian Osman62419612020-07-22 10:19:02 -0400400 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500401 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400402 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400403 effect.child("child") = nullptr;
404 effect.test(0xFF00FFFF,
405 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
406
407 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
408
Brian Osman56ed7da2021-04-21 15:57:27 -0400409 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500410 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400411 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400412 effect.child("child") = rgbwShader;
413 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
414
415 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500416 effect.build("uniform shader child;"
417 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400418 effect.child("child") = rgbwShader;
419 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
420
Brian Osmanb5f0f522020-07-23 13:28:14 -0400421 //
422 // Helper functions
423 //
424
425 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500426 effect.build("float2 helper(float2 x) { return x + 1; }"
427 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400428 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500429}
430
431DEF_TEST(SkRuntimeEffectSimple, r) {
432 test_RuntimeEffect_Shaders(r, nullptr);
433}
434
435DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400436 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500437}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400438
439DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
440 const char* kSource = R"(
441 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400442 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400443 )";
444
Brian Osman56ed7da2021-04-21 15:57:27 -0400445 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400446 REPORTER_ASSERT(r, effect);
447
448 // Test passes if this sequence doesn't assert. skbug.com/10667
449 SkRuntimeShaderBuilder b(std::move(effect));
450 b.uniform("x") = 0.0f;
451 auto shader_0 = b.makeShader(nullptr, false);
452
453 b.uniform("x") = 1.0f;
454 auto shader_1 = b.makeShader(nullptr, true);
455}
Brian Osman8e2ef022020-09-30 13:26:43 -0400456
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500457DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
458 const char* kSource = R"(
459 uniform half x;
460 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400461 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500462 )";
463
Brian Osman56ed7da2021-04-21 15:57:27 -0400464 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500465 REPORTER_ASSERT(r, effect);
466
467 SkRuntimeShaderBuilder b(std::move(effect));
468
469 // Test passes if this sequence doesn't assert.
470 float x = 1.0f;
471 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
472
473 // add extra value to ensure that set doesn't try to use sizeof(array)
474 float origin[] = { 2.0f, 3.0f, 4.0f };
475 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
476
477#ifndef SK_DEBUG
478 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
479 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
480#endif
481
482
483 auto shader = b.makeShader(nullptr, false);
484}
485
Brian Osman8e2ef022020-09-30 13:26:43 -0400486DEF_TEST(SkRuntimeEffectThreaded, r) {
487 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
488 // This tests that we can safely use it from more than one thread, and also
489 // that programs don't refer to shared structures owned by the compiler.
490 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400491 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400492
493 std::thread threads[16];
494 for (auto& thread : threads) {
495 thread = std::thread([r]() {
Brian Osman56ed7da2021-04-21 15:57:27 -0400496 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource));
Brian Osman8e2ef022020-09-30 13:26:43 -0400497 REPORTER_ASSERT(r, effect);
498 });
499 }
500
501 for (auto& thread : threads) {
502 thread.join();
503 }
504}
Mike Klein827f8c02021-02-06 09:13:01 -0600505
506DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
507 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400508 auto [effect, err] =
509 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600510 REPORTER_ASSERT(r, effect);
511 REPORTER_ASSERT(r, err.isEmpty());
512
Brian Osman56ed7da2021-04-21 15:57:27 -0400513 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600514 REPORTER_ASSERT(r, cf);
515
516 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
517 sk_srgb_singleton(), sk_srgb_singleton());
518 REPORTER_ASSERT(r, c.fR == 0.0625f);
519 REPORTER_ASSERT(r, c.fG == 0.25f);
520 REPORTER_ASSERT(r, c.fB == 0.5625f);
521 REPORTER_ASSERT(r, c.fA == 1.0f);
522}
Brian Osman8e756f32021-02-10 10:19:27 -0500523
524static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
525 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400526 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500527 "uniform shader paint;"
528 "struct S { half4 rgba; };"
529 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400530 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500531 ));
532 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
533 sk_sp<SkShader> nullChild = nullptr;
534 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
535 /*childCount=*/1, /*localMatrix=*/nullptr,
536 /*isOpaque=*/false);
537
538 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
539 sk_sp<SkSurface> surface = rContext
540 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
541 : SkSurface::MakeRaster(info);
542 REPORTER_ASSERT(r, surface);
543
544 TestEffect effect(r, surface);
545 effect.build(
546 "uniform shader child;"
547 "struct S { float2 coord; };"
548 "void process(inout S s) { s.coord = s.coord.yx; }"
549 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
550 "}");
551 effect.child("child") = child;
552 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
553 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
554 });
555}
556
557DEF_TEST(SkRuntimeStructNameReuse, r) {
558 test_RuntimeEffectStructNameReuse(r, nullptr);
559}
560
561DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
562 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
563}
Mike Kleine0d9b862021-02-16 12:00:29 -0600564
565DEF_TEST(SkRuntimeColorFilterFlags, r) {
566 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400567 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400568 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600569 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400570 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600571 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
572 }
573
574 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400575 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400576 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600577 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400578 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600579 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
580 }
581}
Brian Osman4d571112021-04-27 09:10:10 -0400582
Brian Osman70ae4c82021-05-21 16:23:06 -0400583DEF_TEST(SkRuntimeShaderSampleCoords, r) {
584 // This test verifies that we detect calls to sample where the coords are the same as those
585 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
586 // sampling. This optimization is implemented very conservatively.
587 //
588 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
589 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400590
591 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400592 auto [effect, err] =
593 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
594 REPORTER_ASSERT(r, effect);
595
596 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
Brian Osman443b5da2021-06-04 16:52:21 -0400597 auto fp = GrSkSLFP::Make(effect, "test_fp", "child", std::move(child));
Brian Osman4d571112021-04-27 09:10:10 -0400598 REPORTER_ASSERT(r, fp);
599
600 REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit);
Brian Osman70ae4c82021-05-21 16:23:06 -0400601 REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400602 };
603
Brian Osman4d571112021-04-27 09:10:10 -0400604 // Cases where our optimization is valid, and works:
605
Brian Osman8cdf28f2021-05-24 09:52:39 -0400606 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
607 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
608 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400609 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400610 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400611
612 // Cases where our optimization is not valid, and does not happen:
613
614 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400615 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400616 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400617 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400618 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400619 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400620 // Use of coords after modification via out-param call
621 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400622 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400623
624 // There should (must) not be any false-positive cases. There are false-negatives.
625 // In all of these cases, our optimization would be valid, but does not happen:
626
627 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400628 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400629 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400630 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400631 // Use of coords passed to helper function
632 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400633 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400634}