blob: 0ebe63f67bd3ed9e65be75c446af57ac2091e2e9 [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 Osmanf72dedd2020-01-08 13:19:58 -050011#include "include/core/SkPaint.h"
12#include "include/core/SkSurface.h"
Brian Osmanee426f22020-01-02 11:55:24 -050013#include "include/effects/SkRuntimeEffect.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040014#include "include/gpu/GrDirectContext.h"
Brian Osmand9bde072020-04-15 14:18:13 -040015#include "src/core/SkTLazy.h"
Brian Osman62419612020-07-22 10:19:02 -040016#include "src/gpu/GrColor.h"
Brian Osman088913a2019-12-19 15:44:56 -050017#include "tests/Test.h"
18
Brian Osmanf72dedd2020-01-08 13:19:58 -050019#include <algorithm>
20
Brian Osman5f6b41e2020-03-09 11:53:24 -040021DEF_TEST(SkRuntimeEffectInvalid, r) {
22 auto test = [r](const char* hdr, const char* body, const char* expected) {
23 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
24 hdr, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050025 auto[effect, errorText] = SkRuntimeEffect::Make(src);
Brian Osman088913a2019-12-19 15:44:56 -050026 REPORTER_ASSERT(r, !effect);
27 REPORTER_ASSERT(r, errorText.contains(expected),
28 "Expected error message to contain \"%s\". Actual message: \"%s\"",
29 expected, errorText.c_str());
30 };
31
32 // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
33 // Ensure that these fail, and the error messages contain the relevant keyword.
Brian Osman5f6b41e2020-03-09 11:53:24 -040034 test("layout(key) in bool Input;", "", "key");
35 test("in uniform float Input;", "", "in uniform");
36 test("layout(ctype=SkRect) float4 Input;", "", "ctype");
37 test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when");
38 test("layout(tracked) uniform float Input;", "", "tracked");
Brian Osman088913a2019-12-19 15:44:56 -050039
Brian Osman82d49702019-12-30 13:44:01 -050040 // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
Brian Osman5f6b41e2020-03-09 11:53:24 -040041 test("uniform sampler2D s;", "", "sampler2D");
Brian Osman088913a2019-12-19 15:44:56 -050042
Brian Osman82d49702019-12-30 13:44:01 -050043 // 'in' variables can't be arrays
Brian Osman5f6b41e2020-03-09 11:53:24 -040044 test("in int Input[2];", "", "array");
Brian Osman8783b782020-01-06 11:13:45 -050045
46 // Type specific restrictions:
47
48 // 'bool', 'int' can't be 'uniform'
Brian Osman5f6b41e2020-03-09 11:53:24 -040049 test("uniform bool Input;", "", "'uniform'");
50 test("uniform int Input;", "", "'uniform'");
Brian Osman8783b782020-01-06 11:13:45 -050051
52 // vector and matrix types can't be 'in'
Brian Osman5f6b41e2020-03-09 11:53:24 -040053 test("in float2 Input;", "", "'in'");
54 test("in half3x3 Input;", "", "'in'");
55
Brian Osmane64ae862020-07-16 15:29:15 -040056 // 'marker' is only permitted on 'uniform' variables
57 test("layout(marker=local_to_world) in float4x4 localToWorld;", "", "'uniform'");
58 // 'marker' is only permitted on float4x4 variables
59 test("layout(marker=local_to_world) uniform float3x3 localToWorld;", "", "float4x4");
60
Brian Osman5f6b41e2020-03-09 11:53:24 -040061 test("half missing();", "color.r = missing();", "undefined function");
Brian Osman182c92e2020-07-20 15:18:33 -040062
63 // Shouldn't be possible to create an SkRuntimeEffect without "main"
64 test("//", "", "main");
Brian Osman82329002020-07-21 09:39:27 -040065
66 // Various places that shaders (fragmentProcessors) should not be allowed
67 test("",
68 "shader child;",
69 "must be global");
70 test("in shader child; half4 helper(shader fp) { return sample(fp); }",
71 "color = helper(child);",
72 "parameter");
73 test("in shader child; shader get_child() { return child; }",
74 "color = sample(get_child());",
75 "return");
76 test("in shader child;",
77 "color = sample(shader(child));",
78 "construct");
79 test("in shader child1; in shader child2;",
80 "color = sample(p.x > 10 ? child1 : child2);",
81 "expression");
Brian Osman088913a2019-12-19 15:44:56 -050082}
Brian Osmanf72dedd2020-01-08 13:19:58 -050083
Brian Osman92aac1e2020-08-05 16:48:58 -040084DEF_TEST(SkRuntimeEffectInvalidColorFilters, r) {
85 auto test = [r](const char* sksl) {
86 auto [effect, errorText] = SkRuntimeEffect::Make(SkString(sksl));
87 REPORTER_ASSERT(r, effect);
88 REPORTER_ASSERT(r, effect->makeShader(nullptr, nullptr, 0, nullptr, false));
89 REPORTER_ASSERT(r, !effect->makeColorFilter(nullptr));
90 };
91
92 // Runtime effects that use sample coords or sk_FragCoord are valid shaders,
93 // but not valid color filters
94 test("void main(float2 p, inout half4 color) { color.rg = half2(p); }");
95 test("void main(float2 p, inout half4 color) { color.rg = half2(sk_FragCoord.xy); }");
96}
97
Brian Osmanf72dedd2020-01-08 13:19:58 -050098class TestEffect {
99public:
Brian Osman62419612020-07-22 10:19:02 -0400100 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
101 : fReporter(r), fSurface(std::move(surface)) {}
102
103 void build(const char* header, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -0500104 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osman62419612020-07-22 10:19:02 -0400105 header, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500106 auto[effect, errorText] = SkRuntimeEffect::Make(src);
107 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400108 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500109 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
110 return;
111 }
Brian Osmand9bde072020-04-15 14:18:13 -0400112 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500113 }
114
Brian Osman62419612020-07-22 10:19:02 -0400115 SkRuntimeShaderBuilder::BuilderInput input(const char* name) {
Brian Osmand9bde072020-04-15 14:18:13 -0400116 return fBuilder->input(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500117 }
Brian Osman62419612020-07-22 10:19:02 -0400118 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
119 return fBuilder->child(name);
120 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500121
Brian Osman62419612020-07-22 10:19:02 -0400122 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
123
124 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
125 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -0400126 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500127 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400128 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500129 return;
130 }
131
Brian Osman62419612020-07-22 10:19:02 -0400132 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500133 SkPaint paint;
134 paint.setShader(std::move(shader));
135 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500136
Brian Osman62419612020-07-22 10:19:02 -0400137 canvas->save();
138 if (preTestCallback) {
139 preTestCallback(canvas, &paint);
140 }
141 canvas->drawPaint(paint);
142 canvas->restore();
143
144 GrColor actual[4];
145 SkImageInfo info = fSurface->imageInfo();
146 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
147 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500148 return;
149 }
150
Brian Osman62419612020-07-22 10:19:02 -0400151 GrColor expected[4] = {TL, TR, BL, BR};
Brian Osmanf72dedd2020-01-08 13:19:58 -0500152 if (memcmp(actual, expected, sizeof(actual)) != 0) {
Brian Osman62419612020-07-22 10:19:02 -0400153 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500154 SkStringPrintf("\n"
155 "Expected: [ %08x %08x %08x %08x ]\n"
156 "Got : [ %08x %08x %08x %08x ]\n"
157 "SkSL:\n%s\n",
158 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmand9bde072020-04-15 14:18:13 -0400159 actual[3], fBuilder->fEffect->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500160 }
161 }
162
Brian Osman62419612020-07-22 10:19:02 -0400163 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
164 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500165 }
166
167private:
Brian Osman62419612020-07-22 10:19:02 -0400168 skiatest::Reporter* fReporter;
169 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400170 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500171};
172
Brian Osman62419612020-07-22 10:19:02 -0400173// Produces a 2x2 bitmap shader, with opaque colors:
174// [ Red, Green ]
175// [ Blue, White ]
176static sk_sp<SkShader> make_RGBW_shader() {
177 SkBitmap bmp;
178 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
179 SkIRect topLeft = SkIRect::MakeWH(1, 1);
180 bmp.pixmap().erase(SK_ColorRED, topLeft);
181 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
182 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
183 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
184 return bmp.makeShader();
185}
186
Robert Phillipse94b4e12020-07-23 13:54:35 -0400187static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500188 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400189 sk_sp<SkSurface> surface = rContext
190 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
191 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500192 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400193 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500194
Brian Osman504032e2020-01-10 10:05:24 -0500195 using float4 = std::array<float, 4>;
196
Brian Osman62419612020-07-22 10:19:02 -0400197 // Local coords
198 effect.build("", "color = half4(half2(p - 0.5), 0, 1);");
199 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500200
Brian Osman62419612020-07-22 10:19:02 -0400201 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
202 effect.build("uniform float4 gColor;",
203 "color = half4(gColor);");
204 effect.input("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
205 effect.test(0xFFBF4000);
206 effect.input("gColor") = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
207 effect.test(0xFF0040BF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500208
Brian Osman62419612020-07-22 10:19:02 -0400209 // Indexing a uniform array with an 'in' integer
210 effect.build("in int flag; uniform half4 gColors[2];",
211 "color = gColors[flag];");
212 effect.input("gColors") = std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f},
213 float4{0.0f, 1.0f, 0.0f, 1.0f }};
214 effect.input("flag") = 0;
215 effect.test(0x7F00007F); // Tests that we clamp to valid premul
216 effect.input("flag") = 1;
217 effect.test(0xFF00FF00);
Brian Osman504032e2020-01-10 10:05:24 -0500218
Brian Osman62419612020-07-22 10:19:02 -0400219 // 'in' half (functionally a uniform, but handled very differently internally)
220 effect.build("in half c;",
221 "color = half4(c, c, c, 1);");
222 effect.input("c") = 0.498f;
223 effect.test(0xFF7F7F7F);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400224
Brian Osman62419612020-07-22 10:19:02 -0400225 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
226 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
227 // make sure we're not saturating unexpectedly.
228 effect.build("", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);");
229 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
230 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400231
Brian Osmanb5f0f522020-07-23 13:28:14 -0400232 //
233 // Sampling children
234 //
235
Brian Osman62419612020-07-22 10:19:02 -0400236 // Sampling a null child should return the paint color
237 effect.build("in shader child;",
238 "color = sample(child);");
239 effect.child("child") = nullptr;
240 effect.test(0xFF00FFFF,
241 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
242
243 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
244
245 // Sampling a simple child at our coordinates (implicitly)
246 effect.build("in shader child;",
247 "color = sample(child);");
248 effect.child("child") = rgbwShader;
249 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
250
251 // Sampling with explicit coordinates (reflecting about the diagonal)
252 effect.build("in shader child;",
253 "color = sample(child, p.yx);");
254 effect.child("child") = rgbwShader;
255 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
256
257 // Sampling with a matrix (again, reflecting about the diagonal)
258 effect.build("in shader child;",
259 "color = sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));");
260 effect.child("child") = rgbwShader;
261 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
Brian Osmanb5f0f522020-07-23 13:28:14 -0400262
263 //
264 // Helper functions
265 //
266
267 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
268 effect.build("float2 helper(float2 x) { return x + 1; }",
269 "float2 v = helper(p);"
270 "color = half4(half2(v), 0, 1);");
271 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500272}
273
274DEF_TEST(SkRuntimeEffectSimple, r) {
275 test_RuntimeEffect_Shaders(r, nullptr);
276}
277
278DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400279 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500280}