blob: 5e889ac6ee89ccbbaa84bf9922ee009d4144f6cc [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 Osman70ae91f2021-06-10 14:27:53 -040020#include "src/gpu/GrDirectContextPriv.h"
Brian Osman4d571112021-04-27 09:10:10 -040021#include "src/gpu/GrFragmentProcessor.h"
Brian Osman443b5da2021-06-04 16:52:21 -040022#include "src/gpu/effects/GrSkSLFP.h"
Brian Osman088913a2019-12-19 15:44:56 -050023#include "tests/Test.h"
24
Brian Osmanf72dedd2020-01-08 13:19:58 -050025#include <algorithm>
Brian Osman8e2ef022020-09-30 13:26:43 -040026#include <thread>
Brian Osmanf72dedd2020-01-08 13:19:58 -050027
John Stiles7ce17512021-01-12 18:39:02 -050028void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) {
Brian Osman56ed7da2021-04-21 15:57:27 -040029 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
John Stiles7ce17512021-01-12 18:39:02 -050030 REPORTER_ASSERT(r, !effect);
31 REPORTER_ASSERT(r, errorText.contains(expected),
32 "Expected error message to contain \"%s\". Actual message: \"%s\"",
33 expected, errorText.c_str());
34};
Brian Osman088913a2019-12-19 15:44:56 -050035
Brian Osman56ed7da2021-04-21 15:57:27 -040036#define EMPTY_MAIN "half4 main(float2 p) { return half4(0); }"
Brian Osman24c18522020-11-10 16:36:01 -050037
John Stiles7ce17512021-01-12 18:39:02 -050038DEF_TEST(SkRuntimeEffectInvalid_FPOnly, r) {
Brian Osman3adc0912021-05-18 10:43:08 -040039 // Features that are only allowed in .fp files (key, in uniform, ctype, when).
Brian Osman088913a2019-12-19 15:44:56 -050040 // Ensure that these fail, and the error messages contain the relevant keyword.
John Stiles7ce17512021-01-12 18:39:02 -050041 test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key");
42 test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform");
43 test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype");
44 test_invalid_effect(r, "in bool Flag; "
45 "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when");
John Stiles7ce17512021-01-12 18:39:02 -050046}
Brian Osman088913a2019-12-19 15:44:56 -050047
John Stiles7ce17512021-01-12 18:39:02 -050048DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) {
Brian Osmand18967c2021-04-01 09:56:07 -040049 // Runtime SkSL supports a limited set of uniform types. No bool, for example:
John Stiles7ce17512021-01-12 18:39:02 -050050 test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform");
John Stiles7ce17512021-01-12 18:39:02 -050051}
Brian Osman088913a2019-12-19 15:44:56 -050052
John Stiles7ce17512021-01-12 18:39:02 -050053DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) {
Brian Osmana4b91692020-08-10 14:26:16 -040054 // 'in' variables aren't allowed at all:
John Stiles7ce17512021-01-12 18:39:02 -050055 test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'");
56 test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'");
57 test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'");
58 test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'");
59}
Brian Osman8783b782020-01-06 11:13:45 -050060
John Stiles7ce17512021-01-12 18:39:02 -050061DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) {
Brian Osman56ed7da2021-04-21 15:57:27 -040062 test_invalid_effect(r, "half4 missing(); half4 main(float2 p) { return missing(); }",
John Stiles7ce17512021-01-12 18:39:02 -050063 "undefined function");
64}
Brian Osman182c92e2020-07-20 15:18:33 -040065
John Stiles7ce17512021-01-12 18:39:02 -050066DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) {
Brian Osman182c92e2020-07-20 15:18:33 -040067 // Shouldn't be possible to create an SkRuntimeEffect without "main"
John Stiles7ce17512021-01-12 18:39:02 -050068 test_invalid_effect(r, "", "main");
69}
Brian Osman82329002020-07-21 09:39:27 -040070
John Stiles7ce17512021-01-12 18:39:02 -050071DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) {
Brian Osmanb06301e2020-11-06 11:45:36 -050072 // sk_Caps is an internal system. It should not be visible to runtime effects
Brian Osman56ed7da2021-04-21 15:57:27 -040073 test_invalid_effect(
74 r,
75 "half4 main(float2 p) { return sk_Caps.integerSupport ? half4(1) : half4(0); }",
76 "unknown identifier 'sk_Caps'");
John Stiles7ce17512021-01-12 18:39:02 -050077}
Brian Osmanb06301e2020-11-06 11:45:36 -050078
John Stiles65d7ab22021-04-28 15:14:09 -040079DEF_TEST(SkRuntimeEffectCanDisableES2Restrictions, r) {
80 auto test_valid_es3 = [](skiatest::Reporter* r, const char* sksl) {
81 SkRuntimeEffect::Options opt;
82 opt.enforceES2Restrictions = false;
83 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl), opt);
84 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
85 };
86
87 test_invalid_effect(r, "float f[2] = float[2](0, 1);" EMPTY_MAIN, "construction of array type");
88 test_valid_es3 (r, "float f[2] = float[2](0, 1);" EMPTY_MAIN);
89}
90
Brian Osmanaf4e2332021-04-20 12:00:10 -040091DEF_TEST(SkRuntimeEffectForColorFilter, r) {
92 // Tests that the color filter factory rejects or accepts certain SkSL constructs
93 auto test_valid = [r](const char* sksl) {
94 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -040095 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -040096 };
97
98 auto test_invalid = [r](const char* sksl, const char* expected) {
99 auto [effect, errorText] = SkRuntimeEffect::MakeForColorFilter(SkString(sksl));
100 REPORTER_ASSERT(r, !effect);
101 REPORTER_ASSERT(r,
102 errorText.contains(expected),
103 "Expected error message to contain \"%s\". Actual message: \"%s\"",
104 expected,
105 errorText.c_str());
106 };
107
108 // Color filters must use the 'half4 main(half4)' signature. Either color can be float4/vec4
109 test_valid("half4 main(half4 c) { return c; }");
110 test_valid("float4 main(half4 c) { return c; }");
111 test_valid("half4 main(float4 c) { return c; }");
112 test_valid("float4 main(float4 c) { return c; }");
113 test_valid("vec4 main(half4 c) { return c; }");
114 test_valid("half4 main(vec4 c) { return c; }");
115 test_valid("vec4 main(vec4 c) { return c; }");
116
117 // Invalid return types
118 test_invalid("void main(half4 c) {}", "'main' must return");
119 test_invalid("half3 main(half4 c) { return c.rgb; }", "'main' must return");
120
121 // Invalid argument types (some are valid as shaders, but not color filters)
122 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
123 test_invalid("half4 main(float2 p) { return half4(1); }", "'main' parameter");
124 test_invalid("half4 main(float2 p, half4 c) { return c; }", "'main' parameter");
125
126 // sk_FragCoord should not be available
127 test_invalid("half4 main(half4 c) { return sk_FragCoord.xy01; }", "unknown identifier");
128
129 // Sampling a child shader requires that we pass explicit coords
130 test_valid("uniform shader child;"
131 "half4 main(half4 c) { return sample(child, c.rg); }");
Brian Osmanc9125aa2021-04-21 09:57:19 -0400132 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
133 test_invalid("uniform shader child;"
134 "half4 main(half4 c) { return sample(child, c.rg, c); }",
135 "no match for sample(shader, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400136
Brian Osmanc9125aa2021-04-21 09:57:19 -0400137 // Shader with just a color
138 test_invalid("uniform shader child;"
139 "half4 main(half4 c) { return sample(child, c); }",
140 "no match for sample(shader, half4)");
141 // Coords and color in a differet order
142 test_invalid("uniform shader child;"
143 "half4 main(half4 c) { return sample(child, c, c.rg); }",
144 "no match for sample(shader, half4, half2)");
145
146 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400147 test_invalid(
148 "uniform shader child;"
149 "half4 main(half4 c) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400150 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400151 test_invalid(
152 "uniform shader child;"
153 "half4 main(half4 c) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400154 "no match for sample(shader, float3x3)");
155
156 // Sampling a colorFilter requires a color. No other signatures are valid.
157 test_valid("uniform colorFilter child;"
158 "half4 main(half4 c) { return sample(child, c); }");
159
160 test_invalid("uniform colorFilter child;"
161 "half4 main(half4 c) { return sample(child); }",
162 "sample(colorFilter)");
163 test_invalid("uniform colorFilter child;"
164 "half4 main(half4 c) { return sample(child, c.rg); }",
165 "sample(colorFilter, half2)");
166 test_invalid("uniform colorFilter child;"
167 "half4 main(half4 c) { return sample(child, c.rg, c); }",
168 "sample(colorFilter, half2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400169}
170
171DEF_TEST(SkRuntimeEffectForShader, r) {
172 // Tests that the shader factory rejects or accepts certain SkSL constructs
173 auto test_valid = [r](const char* sksl) {
174 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
John Stiles65d7ab22021-04-28 15:14:09 -0400175 REPORTER_ASSERT(r, effect, "%s", errorText.c_str());
Brian Osmanaf4e2332021-04-20 12:00:10 -0400176 };
177
178 auto test_invalid = [r](const char* sksl, const char* expected) {
179 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(sksl));
180 REPORTER_ASSERT(r, !effect);
181 REPORTER_ASSERT(r,
182 errorText.contains(expected),
183 "Expected error message to contain \"%s\". Actual message: \"%s\"",
184 expected,
185 errorText.c_str());
186 };
187
188 // Shaders must use either the 'half4 main(float2)' or 'half4 main(float2, half4)' signature
189 // Either color can be half4/float4/vec4, but the coords must be float2/vec2
190 test_valid("half4 main(float2 p) { return p.xyxy; }");
191 test_valid("float4 main(float2 p) { return p.xyxy; }");
192 test_valid("vec4 main(float2 p) { return p.xyxy; }");
193 test_valid("half4 main(vec2 p) { return p.xyxy; }");
194 test_valid("vec4 main(vec2 p) { return p.xyxy; }");
195 test_valid("half4 main(float2 p, half4 c) { return c; }");
196 test_valid("half4 main(float2 p, float4 c) { return c; }");
197 test_valid("half4 main(float2 p, vec4 c) { return c; }");
198 test_valid("float4 main(float2 p, half4 c) { return c; }");
199 test_valid("vec4 main(float2 p, half4 c) { return c; }");
200 test_valid("vec4 main(vec2 p, vec4 c) { return c; }");
201
202 // Invalid return types
203 test_invalid("void main(float2 p) {}", "'main' must return");
204 test_invalid("half3 main(float2 p) { return p.xy1; }", "'main' must return");
205
206 // Invalid argument types (some are valid as color filters, but not shaders)
207 test_invalid("half4 main() { return half4(1); }", "'main' parameter");
208 test_invalid("half4 main(half4 c) { return c; }", "'main' parameter");
209
210 // sk_FragCoord should be available
211 test_valid("half4 main(float2 p) { return sk_FragCoord.xy01; }");
212
213 // Sampling a child shader requires that we pass explicit coords
214 test_valid("uniform shader child;"
215 "half4 main(float2 p) { return sample(child, p); }");
216
Brian Osmanc9125aa2021-04-21 09:57:19 -0400217 // Trying to pass a color as well. (Works internally with FPs, but not in runtime effects).
218 test_invalid("uniform shader child;"
219 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
220 "no match for sample(shader, float2, half4)");
221
222 // Shader with just a color
223 test_invalid("uniform shader child;"
224 "half4 main(float2 p, half4 c) { return sample(child, c); }",
225 "no match for sample(shader, half4)");
226 // Coords and color in a different order
227 test_invalid("uniform shader child;"
228 "half4 main(float2 p, half4 c) { return sample(child, c, p); }",
229 "no match for sample(shader, half4, float2)");
230
231 // Older variants that are no longer allowed
Brian Osmanaf4e2332021-04-20 12:00:10 -0400232 test_invalid(
233 "uniform shader child;"
234 "half4 main(float2 p) { return sample(child); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400235 "no match for sample(shader)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400236 test_invalid(
237 "uniform shader child;"
238 "half4 main(float2 p) { return sample(child, float3x3(1)); }",
Brian Osmanc9125aa2021-04-21 09:57:19 -0400239 "no match for sample(shader, float3x3)");
240
241 // Sampling a colorFilter requires a color. No other signatures are valid.
242 test_valid("uniform colorFilter child;"
243 "half4 main(float2 p, half4 c) { return sample(child, c); }");
244
245 test_invalid("uniform colorFilter child;"
246 "half4 main(float2 p) { return sample(child); }",
247 "sample(colorFilter)");
248 test_invalid("uniform colorFilter child;"
249 "half4 main(float2 p) { return sample(child, p); }",
250 "sample(colorFilter, float2)");
251 test_invalid("uniform colorFilter child;"
252 "half4 main(float2 p, half4 c) { return sample(child, p, c); }",
253 "sample(colorFilter, float2, half4)");
Brian Osmanaf4e2332021-04-20 12:00:10 -0400254}
255
Brian Osmanf72dedd2020-01-08 13:19:58 -0500256class TestEffect {
257public:
Brian Osman62419612020-07-22 10:19:02 -0400258 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
259 : fReporter(r), fSurface(std::move(surface)) {}
260
Brian Osman33316412020-11-06 10:42:51 -0500261 void build(const char* src) {
Brian Osman56ed7da2021-04-21 15:57:27 -0400262 auto [effect, errorText] = SkRuntimeEffect::MakeForShader(SkString(src));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500263 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400264 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500265 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
266 return;
267 }
Brian Osmand9bde072020-04-15 14:18:13 -0400268 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500269 }
270
Brian Osmana4b91692020-08-10 14:26:16 -0400271 SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) {
272 return fBuilder->uniform(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500273 }
Brian Osman62419612020-07-22 10:19:02 -0400274 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
275 return fBuilder->child(name);
276 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500277
Brian Osman62419612020-07-22 10:19:02 -0400278 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
279
280 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
281 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -0400282 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500283 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400284 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500285 return;
286 }
287
Brian Osman62419612020-07-22 10:19:02 -0400288 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500289 SkPaint paint;
290 paint.setShader(std::move(shader));
291 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500292
Brian Osman62419612020-07-22 10:19:02 -0400293 canvas->save();
294 if (preTestCallback) {
295 preTestCallback(canvas, &paint);
296 }
297 canvas->drawPaint(paint);
298 canvas->restore();
299
300 GrColor actual[4];
301 SkImageInfo info = fSurface->imageInfo();
302 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
303 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500304 return;
305 }
306
Brian Osman62419612020-07-22 10:19:02 -0400307 GrColor expected[4] = {TL, TR, BL, BR};
John Stilesc1c3c6d2020-08-15 23:22:53 -0400308 if (0 != memcmp(actual, expected, sizeof(actual))) {
Brian Osman62419612020-07-22 10:19:02 -0400309 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500310 SkStringPrintf("\n"
311 "Expected: [ %08x %08x %08x %08x ]\n"
312 "Got : [ %08x %08x %08x %08x ]\n"
313 "SkSL:\n%s\n",
314 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400315 actual[3], fBuilder->effect()->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500316 }
317 }
318
Brian Osman62419612020-07-22 10:19:02 -0400319 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
320 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500321 }
322
323private:
Brian Osman62419612020-07-22 10:19:02 -0400324 skiatest::Reporter* fReporter;
325 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400326 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500327};
328
Brian Osman62419612020-07-22 10:19:02 -0400329// Produces a 2x2 bitmap shader, with opaque colors:
330// [ Red, Green ]
331// [ Blue, White ]
332static sk_sp<SkShader> make_RGBW_shader() {
333 SkBitmap bmp;
334 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
335 SkIRect topLeft = SkIRect::MakeWH(1, 1);
336 bmp.pixmap().erase(SK_ColorRED, topLeft);
337 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
338 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
339 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
Mike Reedb41bd152020-12-12 11:18:31 -0500340 return bmp.makeShader(SkSamplingOptions());
Brian Osman62419612020-07-22 10:19:02 -0400341}
342
Robert Phillipse94b4e12020-07-23 13:54:35 -0400343static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500344 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400345 sk_sp<SkSurface> surface = rContext
346 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
347 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500348 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400349 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500350
Brian Osman504032e2020-01-10 10:05:24 -0500351 using float4 = std::array<float, 4>;
Brian Osmand18967c2021-04-01 09:56:07 -0400352 using int4 = std::array<int, 4>;
Brian Osman504032e2020-01-10 10:05:24 -0500353
Brian Osman62419612020-07-22 10:19:02 -0400354 // Local coords
Brian Osman33316412020-11-06 10:42:51 -0500355 effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }");
Brian Osman62419612020-07-22 10:19:02 -0400356 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500357
Brian Osman62419612020-07-22 10:19:02 -0400358 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
Brian Osman56ed7da2021-04-21 15:57:27 -0400359 effect.build("uniform float4 gColor; half4 main(float2 p) { return half4(gColor); }");
Brian Osmana4b91692020-08-10 14:26:16 -0400360 effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osman62419612020-07-22 10:19:02 -0400361 effect.test(0xFFBF4000);
Brian Osmana4b91692020-08-10 14:26:16 -0400362 effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f };
Brian Osman62419612020-07-22 10:19:02 -0400363 effect.test(0x7F00007F); // Tests that we clamp to valid premul
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400364
Brian Osmand18967c2021-04-01 09:56:07 -0400365 // Same, with integer uniforms
Brian Osman56ed7da2021-04-21 15:57:27 -0400366 effect.build("uniform int4 gColor; half4 main(float2 p) { return half4(gColor) / 255.0; }");
Brian Osmand18967c2021-04-01 09:56:07 -0400367 effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF };
368 effect.test(0xFFBF4000);
369 effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F };
370 effect.test(0x7F00007F); // Tests that we clamp to valid premul
371
Brian Osman62419612020-07-22 10:19:02 -0400372 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
373 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
374 // make sure we're not saturating unexpectedly.
Brian Osman56ed7da2021-04-21 15:57:27 -0400375 effect.build(
376 "half4 main(float2 p) { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }");
Brian Osman62419612020-07-22 10:19:02 -0400377 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
378 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400379
Brian Osman0acb5b52020-09-02 13:45:47 -0400380 // Runtime effects should use relaxed precision rules by default
Brian Osman33316412020-11-06 10:42:51 -0500381 effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
Brian Osman0acb5b52020-09-02 13:45:47 -0400382 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
383
Brian Osman33316412020-11-06 10:42:51 -0500384 // ... and support *returning* float4 (aka vec4), not just half4
385 effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
386 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
387 effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }");
Brian Osmanf1319c32020-10-13 09:34:23 -0400388 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
389
Brian Osmanb4ce9442020-11-11 09:18:02 -0500390 // Mutating coords should work. (skbug.com/10918)
391 effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }");
392 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
393 effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }"
394 "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }");
395 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
396
Brian Osmanb5f0f522020-07-23 13:28:14 -0400397 //
398 // Sampling children
399 //
400
Brian Osman62419612020-07-22 10:19:02 -0400401 // Sampling a null child should return the paint color
Brian Osman33316412020-11-06 10:42:51 -0500402 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400403 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400404 effect.child("child") = nullptr;
405 effect.test(0xFF00FFFF,
406 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
407
408 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
409
Brian Osman56ed7da2021-04-21 15:57:27 -0400410 // Sampling a simple child at our coordinates
Brian Osman33316412020-11-06 10:42:51 -0500411 effect.build("uniform shader child;"
Brian Osman56ed7da2021-04-21 15:57:27 -0400412 "half4 main(float2 p) { return sample(child, p); }");
Brian Osman62419612020-07-22 10:19:02 -0400413 effect.child("child") = rgbwShader;
414 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
415
416 // Sampling with explicit coordinates (reflecting about the diagonal)
Brian Osman33316412020-11-06 10:42:51 -0500417 effect.build("uniform shader child;"
418 "half4 main(float2 p) { return sample(child, p.yx); }");
Brian Osman62419612020-07-22 10:19:02 -0400419 effect.child("child") = rgbwShader;
420 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
421
Brian Osmanb5f0f522020-07-23 13:28:14 -0400422 //
423 // Helper functions
424 //
425
426 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
Brian Osman33316412020-11-06 10:42:51 -0500427 effect.build("float2 helper(float2 x) { return x + 1; }"
428 "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }");
Brian Osmanb5f0f522020-07-23 13:28:14 -0400429 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500430}
431
432DEF_TEST(SkRuntimeEffectSimple, r) {
433 test_RuntimeEffect_Shaders(r, nullptr);
434}
435
436DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400437 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500438}
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400439
440DEF_TEST(SkRuntimeShaderBuilderReuse, r) {
441 const char* kSource = R"(
442 uniform half x;
Brian Osman56ed7da2021-04-21 15:57:27 -0400443 half4 main(float2 p) { return half4(x); }
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400444 )";
445
Brian Osman56ed7da2021-04-21 15:57:27 -0400446 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Brian Osmanb6bd0d22020-08-27 10:51:22 -0400447 REPORTER_ASSERT(r, effect);
448
449 // Test passes if this sequence doesn't assert. skbug.com/10667
450 SkRuntimeShaderBuilder b(std::move(effect));
451 b.uniform("x") = 0.0f;
452 auto shader_0 = b.makeShader(nullptr, false);
453
454 b.uniform("x") = 1.0f;
455 auto shader_1 = b.makeShader(nullptr, true);
456}
Brian Osman8e2ef022020-09-30 13:26:43 -0400457
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500458DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) {
459 const char* kSource = R"(
460 uniform half x;
461 uniform vec2 offset;
Brian Osman56ed7da2021-04-21 15:57:27 -0400462 half4 main(float2 p) { return half4(x); }
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500463 )";
464
Brian Osman56ed7da2021-04-21 15:57:27 -0400465 sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::MakeForShader(SkString(kSource)).effect;
Derek Sollenberger9e1cedd2021-01-14 08:30:52 -0500466 REPORTER_ASSERT(r, effect);
467
468 SkRuntimeShaderBuilder b(std::move(effect));
469
470 // Test passes if this sequence doesn't assert.
471 float x = 1.0f;
472 REPORTER_ASSERT(r, b.uniform("x").set(&x, 1));
473
474 // add extra value to ensure that set doesn't try to use sizeof(array)
475 float origin[] = { 2.0f, 3.0f, 4.0f };
476 REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2));
477
478#ifndef SK_DEBUG
479 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1));
480 REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3));
481#endif
482
483
484 auto shader = b.makeShader(nullptr, false);
485}
486
Brian Osman8e2ef022020-09-30 13:26:43 -0400487DEF_TEST(SkRuntimeEffectThreaded, r) {
488 // SkRuntimeEffect uses a single compiler instance, but it's mutex locked.
489 // This tests that we can safely use it from more than one thread, and also
490 // that programs don't refer to shared structures owned by the compiler.
491 // skbug.com/10589
Brian Osman56ed7da2021-04-21 15:57:27 -0400492 static constexpr char kSource[] = "half4 main(float2 p) { return sk_FragCoord.xyxy; }";
Brian Osman8e2ef022020-09-30 13:26:43 -0400493
494 std::thread threads[16];
495 for (auto& thread : threads) {
496 thread = std::thread([r]() {
Brian Osman56ed7da2021-04-21 15:57:27 -0400497 auto [effect, error] = SkRuntimeEffect::MakeForShader(SkString(kSource));
Brian Osman8e2ef022020-09-30 13:26:43 -0400498 REPORTER_ASSERT(r, effect);
499 });
500 }
501
502 for (auto& thread : threads) {
503 thread.join();
504 }
505}
Mike Klein827f8c02021-02-06 09:13:01 -0600506
507DEF_TEST(SkRuntimeColorFilterSingleColor, r) {
508 // Test runtime colorfilters support filterColor4f().
Brian Osman56ed7da2021-04-21 15:57:27 -0400509 auto [effect, err] =
510 SkRuntimeEffect::MakeForColorFilter(SkString{"half4 main(half4 c) { return c*c; }"});
Mike Klein827f8c02021-02-06 09:13:01 -0600511 REPORTER_ASSERT(r, effect);
512 REPORTER_ASSERT(r, err.isEmpty());
513
Brian Osman56ed7da2021-04-21 15:57:27 -0400514 sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty());
Mike Klein827f8c02021-02-06 09:13:01 -0600515 REPORTER_ASSERT(r, cf);
516
517 SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0},
518 sk_srgb_singleton(), sk_srgb_singleton());
519 REPORTER_ASSERT(r, c.fR == 0.0625f);
520 REPORTER_ASSERT(r, c.fG == 0.25f);
521 REPORTER_ASSERT(r, c.fB == 0.5625f);
522 REPORTER_ASSERT(r, c.fA == 1.0f);
523}
Brian Osman8e756f32021-02-10 10:19:27 -0500524
525static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) {
526 // Test that two different runtime effects can reuse struct names in a single paint operation
Brian Osman56ed7da2021-04-21 15:57:27 -0400527 auto [childEffect, err] = SkRuntimeEffect::MakeForShader(SkString(
Brian Osman8e756f32021-02-10 10:19:27 -0500528 "uniform shader paint;"
529 "struct S { half4 rgba; };"
530 "void process(inout S s) { s.rgba.rgb *= 0.5; }"
Brian Osman56ed7da2021-04-21 15:57:27 -0400531 "half4 main(float2 p) { S s; s.rgba = sample(paint, p); process(s); return s.rgba; }"
Brian Osman8e756f32021-02-10 10:19:27 -0500532 ));
533 REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str());
534 sk_sp<SkShader> nullChild = nullptr;
535 sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild,
536 /*childCount=*/1, /*localMatrix=*/nullptr,
537 /*isOpaque=*/false);
538
539 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
540 sk_sp<SkSurface> surface = rContext
541 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
542 : SkSurface::MakeRaster(info);
543 REPORTER_ASSERT(r, surface);
544
545 TestEffect effect(r, surface);
546 effect.build(
547 "uniform shader child;"
548 "struct S { float2 coord; };"
549 "void process(inout S s) { s.coord = s.coord.yx; }"
550 "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); "
551 "}");
552 effect.child("child") = child;
553 effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) {
554 paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f});
555 });
556}
557
558DEF_TEST(SkRuntimeStructNameReuse, r) {
559 test_RuntimeEffectStructNameReuse(r, nullptr);
560}
561
562DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) {
563 test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext());
564}
Mike Kleine0d9b862021-02-16 12:00:29 -0600565
566DEF_TEST(SkRuntimeColorFilterFlags, r) {
567 { // Here's a non-trivial filter that doesn't change alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400568 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400569 "half4 main(half4 color) { return color + half4(1,1,1,0); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600570 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400571 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600572 REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged());
573 }
574
575 { // Here's one that definitely changes alpha.
Brian Osman56ed7da2021-04-21 15:57:27 -0400576 auto [effect, err] = SkRuntimeEffect::MakeForColorFilter(SkString{
Brian Osmancdee1202021-04-14 09:36:49 -0400577 "half4 main(half4 color) { return color + half4(0,0,0,4); }"});
Mike Kleine0d9b862021-02-16 12:00:29 -0600578 REPORTER_ASSERT(r, effect && err.isEmpty());
Brian Osmancdee1202021-04-14 09:36:49 -0400579 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
Mike Kleine0d9b862021-02-16 12:00:29 -0600580 REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged());
581 }
582}
Brian Osman4d571112021-04-27 09:10:10 -0400583
Brian Osman70ae4c82021-05-21 16:23:06 -0400584DEF_TEST(SkRuntimeShaderSampleCoords, r) {
585 // This test verifies that we detect calls to sample where the coords are the same as those
586 // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough"
587 // sampling. This optimization is implemented very conservatively.
588 //
589 // It also checks that we correctly set the "referencesSampleCoords" bit on the runtime effect
590 // FP, depending on how the coords parameter to main is used.
Brian Osman70ae4c82021-05-21 16:23:06 -0400591
592 auto test = [&](const char* src, bool expectExplicit, bool expectReferencesSampleCoords) {
Brian Osman4d571112021-04-27 09:10:10 -0400593 auto [effect, err] =
594 SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src));
595 REPORTER_ASSERT(r, effect);
596
597 auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 });
Brian Osman443b5da2021-06-04 16:52:21 -0400598 auto fp = GrSkSLFP::Make(effect, "test_fp", "child", std::move(child));
Brian Osman4d571112021-04-27 09:10:10 -0400599 REPORTER_ASSERT(r, fp);
600
601 REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit);
Brian Osman70ae4c82021-05-21 16:23:06 -0400602 REPORTER_ASSERT(r, fp->referencesSampleCoords() == expectReferencesSampleCoords);
Brian Osman4d571112021-04-27 09:10:10 -0400603 };
604
Brian Osman4d571112021-04-27 09:10:10 -0400605 // Cases where our optimization is valid, and works:
606
Brian Osman8cdf28f2021-05-24 09:52:39 -0400607 // Direct use of passed-in coords. Here, the only use of sample coords is for a sample call
608 // converted to passthrough, so referenceSampleCoords is *false*, despite appearing in main.
609 test("half4 main(float2 xy) { return sample(child, xy); }", false, false);
Brian Osman4d571112021-04-27 09:10:10 -0400610 // Sample with passed-in coords, read (but don't write) sample coords elsewhere
Brian Osman70ae4c82021-05-21 16:23:06 -0400611 test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false, true);
Brian Osman4d571112021-04-27 09:10:10 -0400612
613 // Cases where our optimization is not valid, and does not happen:
614
615 // Sampling with values completely unrelated to passed-in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400616 test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true, false);
Brian Osman4d571112021-04-27 09:10:10 -0400617 // Use of expression involving passed in coords
Brian Osman70ae4c82021-05-21 16:23:06 -0400618 test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400619 // Use of coords after modification
Brian Osman70ae4c82021-05-21 16:23:06 -0400620 test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400621 // Use of coords after modification via out-param call
622 test("void adjust(inout float2 xy) { xy *= 2; }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400623 "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400624
625 // There should (must) not be any false-positive cases. There are false-negatives.
626 // In all of these cases, our optimization would be valid, but does not happen:
627
628 // Direct use of passed-in coords, modified after use
Brian Osman70ae4c82021-05-21 16:23:06 -0400629 test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400630 // Passed-in coords copied to a temp variable
Brian Osman70ae4c82021-05-21 16:23:06 -0400631 test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400632 // Use of coords passed to helper function
633 test("half4 helper(float2 xy) { return sample(child, xy); }"
Brian Osman70ae4c82021-05-21 16:23:06 -0400634 "half4 main(float2 xy) { return helper(xy); }", true, true);
Brian Osman4d571112021-04-27 09:10:10 -0400635}
Brian Osman70ae91f2021-06-10 14:27:53 -0400636
637DEF_GPUTEST_FOR_ALL_CONTEXTS(GrSkSLFP_Specialized, r, ctxInfo) {
638 struct FpAndKey {
639 std::unique_ptr<GrFragmentProcessor> fp;
640 SkTArray<uint32_t, true> key;
641 };
642
643 // Constant color, but with a similar option to GrOverrideInputFragmentProcessor
644 // specialize decides if the color is inserted in the SkSL as a literal, or left as a uniform
645 auto make_color_fp = [&](SkPMColor4f color, bool specialize) {
646 auto effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader, R"(
647 uniform half4 color;
648 half4 main(float2 xy) { return color; }
649 )");
650 FpAndKey result;
651 result.fp = GrSkSLFP::Make(
652 std::move(effect), "color_fp", "color", GrSkSLFP::SpecializeIf(specialize, color));
653 GrProcessorKeyBuilder builder(&result.key);
654 result.fp->getGLSLProcessorKey(*ctxInfo.directContext()->priv().caps()->shaderCaps(),
655 &builder);
656 builder.flush();
657 return result;
658 };
659
660 FpAndKey uRed = make_color_fp({1, 0, 0, 1}, false),
661 uGreen = make_color_fp({0, 1, 0, 1}, false),
662 sRed = make_color_fp({1, 0, 0, 1}, true),
663 sGreen = make_color_fp({0, 1, 0, 1}, true);
664
665 // uRed and uGreen should have the same key - they just have different uniforms
666 SkASSERT(uRed.key == uGreen.key);
667 // sRed and sGreen should have keys that are different from the uniform case, and each other
668 SkASSERT(sRed.key != uRed.key);
669 SkASSERT(sGreen.key != uRed.key);
670 SkASSERT(sRed.key != sGreen.key);
671}