blob: e28a2d90161f96385647cff46191106c6e74a665 [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 Osman088913a2019-12-19 15:44:56 -050021#include "tests/Test.h"
22
Brian Osmanf72dedd2020-01-08 13:19:58 -050023#include <algorithm>
Brian Osman8e2ef022020-09-30 13:26:43 -040024#include <thread>
Brian Osmanf72dedd2020-01-08 13:19:58 -050025
John Stiles7ce17512021-01-12 18:39:02 -050026void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) {
Brian Osman56ed7da2021-04-21 15:57:27 -040027 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
John Stiles7ce17512021-01-12 18:39:02 -050028 REPORTER_ASSERT(r, !effect);
29 REPORTER_ASSERT(r, errorText.contains(expected),
30 "Expected error message to contain \"%s\". Actual message: \"%s\"",
31 expected, errorText.c_str());
32};
Brian Osman088913a2019-12-19 15:44:56 -050033
Brian Osman56ed7da2021-04-21 15:57:27 -040034#define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }"
Brian Osman24c18522020-11-10 16:36:01 -050035
John Stiles7ce17512021-01-12 18:39:02 -050036DEF_TEST(SkRuntimeEffectInvalid_FPOnly, r) {
Brian Osman3adc0912021-05-18 10:43:08 -040037 // Features that are only allowed in .fp files (key, in uniform, ctype, when).
Brian Osman088913a2019-12-19 15:44:56 -050038 // Ensure that these fail, and the error messages contain the relevant keyword.
John Stiles7ce17512021-01-12 18:39:02 -050039 test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key");
40 test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform");
41 test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype");
42 test_invalid_effect(r, "in bool Flag; "
43 "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when");
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_LimitedUniformTypes, r) {
Brian Osmand18967c2021-04-01 09:56:07 -040047 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
John Stiles7ce17512021-01-12 18:39:02 -050048 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
John Stiles7ce17512021-01-12 18:39:02 -050049}
Brian Osman088913a2019-12-19 15:44:56 -050050
John Stiles7ce17512021-01-12 18:39:02 -050051DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
Brian Osmana4b91692020-08-10 14:26:16 -040052 // 'in' variables aren't allowed at all:
John Stiles7ce17512021-01-12 18:39:02 -050053 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
54 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
55 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
56 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
57}
Brian Osman8783b782020-01-06 11:13:45 -050058
John Stiles7ce17512021-01-12 18:39:02 -050059DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
Brian Osman56ed7da2021-04-21 15:57:27 -040060 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
John Stiles7ce17512021-01-12 18:39:02 -050061 "undefined function");
62}
Brian Osman182c92e2020-07-20 15:18:33 -040063
John Stiles7ce17512021-01-12 18:39:02 -050064DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
Brian Osman182c92e2020-07-20 15:18:33 -040065 // Shouldn't be possible to create an SkRuntimeEffect without "main"
John Stiles7ce17512021-01-12 18:39:02 -050066 test_invalid_effect(r, "", "main");
67}
Brian Osman82329002020-07-21 09:39:27 -040068
John Stiles7ce17512021-01-12 18:39:02 -050069DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
Brian Osmanb06301e2020-11-06 11:45:36 -050070 // sk_Caps is an internal system. It should not be visible to runtime effects
Brian Osman56ed7da2021-04-21 15:57:27 -040071 test_invalid_effect(
72 r,
73 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
74 "unknown identifier 'sk_Caps'");
John Stiles7ce17512021-01-12 18:39:02 -050075}
Brian Osmanb06301e2020-11-06 11:45:36 -050076
John Stiles65d7ab22021-04-28 15:14:09 -040077DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
78 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
79 SkRuntimeEffect::Options opt;
80 opt.enforceES2Restrictions = false;
81 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
82 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
83 };
84
85 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
86 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
87}
88
Brian Osmanaf4e2332021-04-20 12:00:10 -040089DEF_TEST(SkRuntimeEffectForColorFilter, r) {
90 // Tests that the color filter factory rejects or accepts certain SkSL constructs
91 auto test_valid = [r](const char* sksl) {
92 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -040093 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -040094 };
95
96 auto test_invalid = [r](const char* sksl, const char* expected) {
97 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
98 REPORTER_ASSERT(r, !effect);
99 REPORTER_ASSERT(r,
100 errorText.contains(expected),
101 "Expected error message to contain \"%s\". Actual message: \"%s\"",
102 expected,
103 errorText.c_str());
104 };
105
106 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
107 test_valid("half4 main(half4 c) { return c; }");
108 test_valid("float4 main(half4 c) { return c; }");
109 test_valid("half4 main(float4 c) { return c; }");
110 test_valid("float4 main(float4 c) { return c; }");
111 test_valid("vec4 main(half4 c) { return c; }");
112 test_valid("half4 main(vec4 c) { return c; }");
113 test_valid("vec4 main(vec4 c) { return c; }");
114
115 // Invalid return types
116 test_invalid("void main(half4 c) {}", "'main' must return");
117 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
118
119 // Invalid argument types (some are valid as shaders, but not color filters)
120 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
121 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
122 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
123
124 // sk_FragCoord should not be available
125 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
126
127 // Sampling a child shader requires that we pass explicit coords
128 test_valid("uniform shader child;"
129 "half4 main(half4 c) { return sample(child, c.rg); }");
Brian Osmanc9125aa2021-04-21 09:57:19 -0400130 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
131 test_invalid("uniform shader child;"
132 "half4 main(half4 c) { return sample(child, c.rg, c); }",
133 "no match for sample(shader, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400134
Brian Osmanc9125aa2021-04-21 09:57:19 -0400135 // Shader with just a color
136 test_invalid("uniform shader child;"
137 "half4 main(half4 c) { return sample(child, c); }",
138 "no match for sample(shader, half4)");
139 // Coords and color in a differet order
140 test_invalid("uniform shader child;"
141 "half4 main(half4 c) { return sample(child, c, c.rg); }",
142 "no match for sample(shader, half4, half2)");
143
144 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400145 test_invalid(
146 "uniform shader child;"
147 "half4 main(half4 c) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400148 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400149 test_invalid(
150 "uniform shader child;"
151 "half4 main(half4 c) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400152 "no match for sample(shader, float3x3)");
153
154 // Sampling a colorFilter requires a color. No other signatures are valid.
155 test_valid("uniform colorFilter child;"
156 "half4 main(half4 c) { return sample(child, c); }");
157
158 test_invalid("uniform colorFilter child;"
159 "half4 main(half4 c) { return sample(child); }",
160 "sample(colorFilter)");
161 test_invalid("uniform colorFilter child;"
162 "half4 main(half4 c) { return sample(child, c.rg); }",
163 "sample(colorFilter, half2)");
164 test_invalid("uniform colorFilter child;"
165 "half4 main(half4 c) { return sample(child, c.rg, c); }",
166 "sample(colorFilter, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400167}
168
169DEF_TEST(SkRuntimeEffectForShader, r) {
170 // Tests that the shader factory rejects or accepts certain SkSL constructs
171 auto test_valid = [r](const char* sksl) {
172 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -0400173 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400174 };
175
176 auto test_invalid = [r](const char* sksl, const char* expected) {
177 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
178 REPORTER_ASSERT(r, !effect);
179 REPORTER_ASSERT(r,
180 errorText.contains(expected),
181 "Expected error message to contain \"%s\". Actual message: \"%s\"",
182 expected,
183 errorText.c_str());
184 };
185
186 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
187 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
188 test_valid("half4 main(float2 p) { return p.xyxy; }");
189 test_valid("float4 main(float2 p) { return p.xyxy; }");
190 test_valid("vec4 main(float2 p) { return p.xyxy; }");
191 test_valid("half4 main(vec2 p) { return p.xyxy; }");
192 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
193 test_valid("half4 main(float2 p, half4 c) { return c; }");
194 test_valid("half4 main(float2 p, float4 c) { return c; }");
195 test_valid("half4 main(float2 p, vec4 c) { return c; }");
196 test_valid("float4 main(float2 p, half4 c) { return c; }");
197 test_valid("vec4 main(float2 p, half4 c) { return c; }");
198 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
199
200 // Invalid return types
201 test_invalid("void main(float2 p) {}", "'main' must return");
202 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
203
204 // Invalid argument types (some are valid as color filters, but not shaders)
205 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
206 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
207
208 // sk_FragCoord should be available
209 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }");
210
211 // Sampling a child shader requires that we pass explicit coords
212 test_valid("uniform shader child;"
213 "half4 main(float2 p) { return sample(child, p); }");
214
Brian Osmanc9125aa2021-04-21 09:57:19 -0400215 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
216 test_invalid("uniform shader child;"
217 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
218 "no match for sample(shader, float2, half4)");
219
220 // Shader with just a color
221 test_invalid("uniform shader child;"
222 "half4 main(float2 p, half4 c) { return sample(child, c); }",
223 "no match for sample(shader, half4)");
224 // Coords and color in a different order
225 test_invalid("uniform shader child;"
226 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
227 "no match for sample(shader, half4, float2)");
228
229 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400230 test_invalid(
231 "uniform shader child;"
232 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400233 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400234 test_invalid(
235 "uniform shader child;"
236 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400237 "no match for sample(shader, float3x3)");
238
239 // Sampling a colorFilter requires a color. No other signatures are valid.
240 test_valid("uniform colorFilter child;"
241 "half4 main(float2 p, half4 c) { return sample(child, c); }");
242
243 test_invalid("uniform colorFilter child;"
244 "half4 main(float2 p) { return sample(child); }",
245 "sample(colorFilter)");
246 test_invalid("uniform colorFilter child;"
247 "half4 main(float2 p) { return sample(child, p); }",
248 "sample(colorFilter, float2)");
249 test_invalid("uniform colorFilter child;"
250 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
251 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400252}
253
Brian Osmanf72dedd2020-01-08 13:19:58 -0500254class TestEffect {
255public:
Brian Osman62419612020-07-22 10:19:02 -0400256 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
257 : fReporter(r), fSurface(std::move(surface)) {}
258
Brian Osman33316412020-11-06 10:42:51 -0500259 void build(const char* src) {
Brian Osman56ed7da2021-04-21 15:57:27 -0400260 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500261 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400262 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500263 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
264 return;
265 }
Brian Osmand9bde072020-04-15 14:18:13 -0400266 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500267 }
268
Brian Osmana4b91692020-08-10 14:26:16 -0400269 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
270 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500271 }
Brian Osman62419612020-07-22 10:19:02 -0400272 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
273 return fBuilder->child(name);
274 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500275
Brian Osman62419612020-07-22 10:19:02 -0400276 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
277
278 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
279 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -0400280 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500281 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400282 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500283 return;
284 }
285
Brian Osman62419612020-07-22 10:19:02 -0400286 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500287 SkPaint paint;
288 paint.setShader(std::move(shader));
289 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500290
Brian Osman62419612020-07-22 10:19:02 -0400291 canvas->save();
292 if (preTestCallback) {
293 preTestCallback(canvas, &paint);
294 }
295 canvas->drawPaint(paint);
296 canvas->restore();
297
298 GrColor actual[4];
299 SkImageInfo info = fSurface->imageInfo();
300 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
301 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500302 return;
303 }
304
Brian Osman62419612020-07-22 10:19:02 -0400305 GrColor expected[4] = {TL, TR, BL, BR};
John Stilesc1c3c6d2020-08-15 23:22:53 -0400306 if (0 != memcmp(actual, expected, sizeof(actual))) {
Brian Osman62419612020-07-22 10:19:02 -0400307 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500308 SkStringPrintf("\n"
309 "Expected: [ %08x %08x %08x %08x ]\n"
310 "Got : [ %08x %08x %08x %08x ]\n"
311 "SkSL:\n%s\n",
312 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400313 actual[3], fBuilder->effect()->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500314 }
315 }
316
Brian Osman62419612020-07-22 10:19:02 -0400317 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
318 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500319 }
320
321private:
Brian Osman62419612020-07-22 10:19:02 -0400322 skiatest::Reporter* fReporter;
323 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400324 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500325};
326
Brian Osman62419612020-07-22 10:19:02 -0400327// Produces a 2x2 bitmap shader, with opaque colors:
328// [ Red, Green ]
329// [ Blue, White ]
330static sk_sp<SkShader> make_RGBW_shader() {
331 SkBitmap bmp;
332 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
333 SkIRect topLeft = SkIRect::MakeWH(1, 1);
334 bmp.pixmap().erase(SK_ColorRED, topLeft);
335 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
336 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
337 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500338 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400339}
340
Robert Phillipse94b4e12020-07-23 13:54:35 -0400341static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500342 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400343 sk_sp<SkSurface> surface = rContext
344 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
345 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500346 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400347 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500348
Brian Osman504032e2020-01-10 10:05:24 -0500349 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400350 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500351
Brian Osman62419612020-07-22 10:19:02 -0400352 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500353 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
Brian Osman62419612020-07-22 10:19:02 -0400354 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500355
Brian Osman62419612020-07-22 10:19:02 -0400356 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400357 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400358 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400359 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400360 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400361 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400362
Brian Osmand18967c2021-04-01 09:56:07 -0400363 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400364 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400365 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
366 effect.test(0xFFBF4000);
367 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
368 effect.test(0x7F00007F); // Tests that we clamp to valid premul
369
Brian Osman62419612020-07-22 10:19:02 -0400370 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
371 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
372 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400373 effect.build(
374 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
Brian Osman62419612020-07-22 10:19:02 -0400375 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
376 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400377
Brian Osman0acb5b52020-09-02 13:45:47 -0400378 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500379 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
Brian Osman0acb5b52020-09-02 13:45:47 -0400380 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
381
Brian Osman33316412020-11-06 10:42:51 -0500382 // ... and support *returning* float4 (aka vec4), not just half4
383 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
384 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
385 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
Brian Osmanf1319c32020-10-13 09:34:23 -0400386 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
387
Brian Osmanb4ce9442020-11-11 09:18:02 -0500388 // Mutating coords should work. (skbug.com/10918)
389 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
390 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
391 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
392 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
393 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
394
Brian Osmanb5f0f522020-07-23 13:28:14 -0400395 //
396 // Sampling children
397 //
398
Brian Osman62419612020-07-22 10:19:02 -0400399 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500400 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400401 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400402 effect.child("child") = nullptr;
403 effect.test(0xFF00FFFF,
404 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
405
406 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
407
Brian Osman56ed7da2021-04-21 15:57:27 -0400408 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500409 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400410 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400411 effect.child("child") = rgbwShader;
412 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
413
414 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500415 effect.build("uniform shader child;"
416 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400417 effect.child("child") = rgbwShader;
418 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
419
Brian Osmanb5f0f522020-07-23 13:28:14 -0400420 //
421 // Helper functions
422 //
423
424 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500425 effect.build("float2 helper(float2 x) { return x + 1; }"
426 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400427 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500428}
429
430DEF_TEST(SkRuntimeEffectSimple, r) {
431 test_RuntimeEffect_Shaders(r, nullptr);
432}
433
434DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400435 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500436}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400437
438DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
439 const char* kSource = R"(
440 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400441 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400442 )";
443
Brian Osman56ed7da2021-04-21 15:57:27 -0400444 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400445 REPORTER_ASSERT(r, effect);
446
447 // Test passes if this sequence doesn't assert. skbug.com/10667
448 SkRuntimeShaderBuilder b(std::move(effect));
449 b.uniform("x") = 0.0f;
450 auto shader_0 = b.makeShader(nullptr, false);
451
452 b.uniform("x") = 1.0f;
453 auto shader_1 = b.makeShader(nullptr, true);
454}
Brian Osman8e2ef022020-09-30 13:26:43 -0400455
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500456DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
457 const char* kSource = R"(
458 uniform half x;
459 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400460 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500461 )";
462
Brian Osman56ed7da2021-04-21 15:57:27 -0400463 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500464 REPORTER_ASSERT(r, effect);
465
466 SkRuntimeShaderBuilder b(std::move(effect));
467
468 // Test passes if this sequence doesn't assert.
469 float x = 1.0f;
470 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
471
472 // add extra value to ensure that set doesn't try to use sizeof(array)
473 float origin[] = { 2.0f, 3.0f, 4.0f };
474 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
475
476#ifndef SK_DEBUG
477 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
478 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
479#endif
480
481
482 auto shader = b.makeShader(nullptr, false);
483}
484
Brian Osman8e2ef022020-09-30 13:26:43 -0400485DEF_TEST(SkRuntimeEffectThreaded, r) {
486 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
487 // This tests that we can safely use it from more than one thread, and also
488 // that programs don't refer to shared structures owned by the compiler.
489 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400490 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400491
492 std::thread threads[16];
493 for (auto& thread : threads) {
494 thread = std::thread([r]() {
Brian Osman56ed7da2021-04-21 15:57:27 -0400495 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource));
Brian Osman8e2ef022020-09-30 13:26:43 -0400496 REPORTER_ASSERT(r, effect);
497 });
498 }
499
500 for (auto& thread : threads) {
501 thread.join();
502 }
503}
Mike Klein827f8c02021-02-06 09:13:01 -0600504
505DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
506 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400507 auto [effect, err] =
508 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600509 REPORTER_ASSERT(r, effect);
510 REPORTER_ASSERT(r, err.isEmpty());
511
Brian Osman56ed7da2021-04-21 15:57:27 -0400512 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600513 REPORTER_ASSERT(r, cf);
514
515 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
516 sk_srgb_singleton(), sk_srgb_singleton());
517 REPORTER_ASSERT(r, c.fR == 0.0625f);
518 REPORTER_ASSERT(r, c.fG == 0.25f);
519 REPORTER_ASSERT(r, c.fB == 0.5625f);
520 REPORTER_ASSERT(r, c.fA == 1.0f);
521}
Brian Osman8e756f32021-02-10 10:19:27 -0500522
523static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
524 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400525 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500526 "uniform shader paint;"
527 "struct S { half4 rgba; };"
528 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400529 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500530 ));
531 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
532 sk_sp<SkShader> nullChild = nullptr;
533 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
534 /*childCount=*/1, /*localMatrix=*/nullptr,
535 /*isOpaque=*/false);
536
537 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
538 sk_sp<SkSurface> surface = rContext
539 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
540 : SkSurface::MakeRaster(info);
541 REPORTER_ASSERT(r, surface);
542
543 TestEffect effect(r, surface);
544 effect.build(
545 "uniform shader child;"
546 "struct S { float2 coord; };"
547 "void process(inout S s) { s.coord = s.coord.yx; }"
548 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
549 "}");
550 effect.child("child") = child;
551 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
552 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
553 });
554}
555
556DEF_TEST(SkRuntimeStructNameReuse, r) {
557 test_RuntimeEffectStructNameReuse(r, nullptr);
558}
559
560DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
561 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
562}
Mike Kleine0d9b862021-02-16 12:00:29 -0600563
564DEF_TEST(SkRuntimeColorFilterFlags, r) {
565 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400566 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400567 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600568 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400569 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600570 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
571 }
572
573 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400574 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400575 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600576 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400577 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600578 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
579 }
580}
Brian Osman4d571112021-04-27 09:10:10 -0400581
Brian Osman70ae4c82021-05-21 16:23:06 -0400582DEF_TEST(SkRuntimeShaderSampleCoords, r) {
583 // This test verifies that we detect calls to sample where the coords are the same as those
584 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
585 // sampling. This optimization is implemented very conservatively.
586 //
587 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
588 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400589
590 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400591 auto [effect, err] =
592 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
593 REPORTER_ASSERT(r, effect);
594
595 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
596 auto fp = effect->makeFP(nullptr, &child, 1);
597 REPORTER_ASSERT(r, fp);
598
599 REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit);
Brian Osman70ae4c82021-05-21 16:23:06 -0400600 REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400601 };
602
Brian Osman4d571112021-04-27 09:10:10 -0400603 // Cases where our optimization is valid, and works:
604
Brian Osman8cdf28f2021-05-24 09:52:39 -0400605 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
606 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
607 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400608 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400609 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400610
611 // Cases where our optimization is not valid, and does not happen:
612
613 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400614 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400615 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400616 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400617 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400618 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400619 // Use of coords after modification via out-param call
620 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400621 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400622
623 // There should (must) not be any false-positive cases. There are false-negatives.
624 // In all of these cases, our optimization would be valid, but does not happen:
625
626 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400627 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400628 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400629 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400630 // Use of coords passed to helper function
631 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400632 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400633}