blob: fad294ee95792360ec859c35e5499fe05034f6ff [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 Osmand9bde072020-04-15 14:18:13 -040016#include "src/core/SkTLazy.h"
Brian Osman62419612020-07-22 10:19:02 -040017#include "src/gpu/GrColor.h"
Brian Osman088913a2019-12-19 15:44:56 -050018#include "tests/Test.h"
19
Brian Osmanf72dedd2020-01-08 13:19:58 -050020#include <algorithm>
21
Brian Osman5f6b41e2020-03-09 11:53:24 -040022DEF_TEST(SkRuntimeEffectInvalid, r) {
23 auto test = [r](const char* hdr, const char* body, const char* expected) {
24 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
25 hdr, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050026 auto[effect, errorText] = SkRuntimeEffect::Make(src);
Brian Osman088913a2019-12-19 15:44:56 -050027 REPORTER_ASSERT(r, !effect);
28 REPORTER_ASSERT(r, errorText.contains(expected),
29 "Expected error message to contain \"%s\". Actual message: \"%s\"",
30 expected, errorText.c_str());
31 };
32
33 // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
34 // Ensure that these fail, and the error messages contain the relevant keyword.
Brian Osman5f6b41e2020-03-09 11:53:24 -040035 test("layout(key) in bool Input;", "", "key");
36 test("in uniform float Input;", "", "in uniform");
37 test("layout(ctype=SkRect) float4 Input;", "", "ctype");
38 test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when");
39 test("layout(tracked) uniform float Input;", "", "tracked");
Brian Osman088913a2019-12-19 15:44:56 -050040
Brian Osman82d49702019-12-30 13:44:01 -050041 // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
Brian Osman5f6b41e2020-03-09 11:53:24 -040042 test("uniform sampler2D s;", "", "sampler2D");
Brian Osman088913a2019-12-19 15:44:56 -050043
Brian Osman82d49702019-12-30 13:44:01 -050044 // 'in' variables can't be arrays
Brian Osman5f6b41e2020-03-09 11:53:24 -040045 test("in int Input[2];", "", "array");
Brian Osman8783b782020-01-06 11:13:45 -050046
47 // Type specific restrictions:
48
49 // 'bool', 'int' can't be 'uniform'
Brian Osman5f6b41e2020-03-09 11:53:24 -040050 test("uniform bool Input;", "", "'uniform'");
51 test("uniform int Input;", "", "'uniform'");
Brian Osman8783b782020-01-06 11:13:45 -050052
53 // vector and matrix types can't be 'in'
Brian Osman5f6b41e2020-03-09 11:53:24 -040054 test("in float2 Input;", "", "'in'");
55 test("in half3x3 Input;", "", "'in'");
56
Brian Osmane64ae862020-07-16 15:29:15 -040057 // 'marker' is only permitted on 'uniform' variables
58 test("layout(marker=local_to_world) in float4x4 localToWorld;", "", "'uniform'");
59 // 'marker' is only permitted on float4x4 variables
60 test("layout(marker=local_to_world) uniform float3x3 localToWorld;", "", "float4x4");
61
Brian Osman5f6b41e2020-03-09 11:53:24 -040062 test("half missing();", "color.r = missing();", "undefined function");
Brian Osman182c92e2020-07-20 15:18:33 -040063
64 // Shouldn't be possible to create an SkRuntimeEffect without "main"
65 test("//", "", "main");
Brian Osman82329002020-07-21 09:39:27 -040066
67 // Various places that shaders (fragmentProcessors) should not be allowed
68 test("",
69 "shader child;",
70 "must be global");
71 test("in shader child; half4 helper(shader fp) { return sample(fp); }",
72 "color = helper(child);",
73 "parameter");
74 test("in shader child; shader get_child() { return child; }",
75 "color = sample(get_child());",
76 "return");
77 test("in shader child;",
78 "color = sample(shader(child));",
79 "construct");
80 test("in shader child1; in shader child2;",
81 "color = sample(p.x > 10 ? child1 : child2);",
82 "expression");
Brian Osman088913a2019-12-19 15:44:56 -050083}
Brian Osmanf72dedd2020-01-08 13:19:58 -050084
Brian Osman92aac1e2020-08-05 16:48:58 -040085DEF_TEST(SkRuntimeEffectInvalidColorFilters, r) {
86 auto test = [r](const char* sksl) {
87 auto [effect, errorText] = SkRuntimeEffect::Make(SkString(sksl));
88 REPORTER_ASSERT(r, effect);
Brian Osman269b21c2020-08-06 12:15:53 -040089
90 sk_sp<SkData> inputs = SkData::MakeUninitialized(effect->inputSize());
91
92 REPORTER_ASSERT(r, effect->makeShader(inputs, nullptr, 0, nullptr, false));
93 REPORTER_ASSERT(r, !effect->makeColorFilter(inputs));
Brian Osman92aac1e2020-08-05 16:48:58 -040094 };
95
96 // Runtime effects that use sample coords or sk_FragCoord are valid shaders,
97 // but not valid color filters
98 test("void main(float2 p, inout half4 color) { color.rg = half2(p); }");
99 test("void main(float2 p, inout half4 color) { color.rg = half2(sk_FragCoord.xy); }");
Brian Osman269b21c2020-08-06 12:15:53 -0400100
101 // We also can't use layout(marker), which would give the runtime color filter CTM information
102 test("layout(marker=ctm) uniform float4x4 ctm;"
103 "void main(float2 p, inout half4 color) { color.r = half(ctm[0][0]); }");
Brian Osman92aac1e2020-08-05 16:48:58 -0400104}
105
Brian Osmanf72dedd2020-01-08 13:19:58 -0500106class TestEffect {
107public:
Brian Osman62419612020-07-22 10:19:02 -0400108 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
109 : fReporter(r), fSurface(std::move(surface)) {}
110
111 void build(const char* header, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -0500112 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osman62419612020-07-22 10:19:02 -0400113 header, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500114 auto[effect, errorText] = SkRuntimeEffect::Make(src);
115 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -0400116 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500117 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
118 return;
119 }
Brian Osmand9bde072020-04-15 14:18:13 -0400120 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500121 }
122
Brian Osman62419612020-07-22 10:19:02 -0400123 SkRuntimeShaderBuilder::BuilderInput input(const char* name) {
Brian Osmand9bde072020-04-15 14:18:13 -0400124 return fBuilder->input(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500125 }
Brian Osman62419612020-07-22 10:19:02 -0400126 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
127 return fBuilder->child(name);
128 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500129
Brian Osman62419612020-07-22 10:19:02 -0400130 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
131
132 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
133 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -0400134 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500135 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400136 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500137 return;
138 }
139
Brian Osman62419612020-07-22 10:19:02 -0400140 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500141 SkPaint paint;
142 paint.setShader(std::move(shader));
143 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500144
Brian Osman62419612020-07-22 10:19:02 -0400145 canvas->save();
146 if (preTestCallback) {
147 preTestCallback(canvas, &paint);
148 }
149 canvas->drawPaint(paint);
150 canvas->restore();
151
152 GrColor actual[4];
153 SkImageInfo info = fSurface->imageInfo();
154 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
155 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500156 return;
157 }
158
Brian Osman62419612020-07-22 10:19:02 -0400159 GrColor expected[4] = {TL, TR, BL, BR};
Brian Osmanf72dedd2020-01-08 13:19:58 -0500160 if (memcmp(actual, expected, sizeof(actual)) != 0) {
Brian Osman62419612020-07-22 10:19:02 -0400161 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500162 SkStringPrintf("\n"
163 "Expected: [ %08x %08x %08x %08x ]\n"
164 "Got : [ %08x %08x %08x %08x ]\n"
165 "SkSL:\n%s\n",
166 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmand9bde072020-04-15 14:18:13 -0400167 actual[3], fBuilder->fEffect->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500168 }
169 }
170
Brian Osman62419612020-07-22 10:19:02 -0400171 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
172 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500173 }
174
175private:
Brian Osman62419612020-07-22 10:19:02 -0400176 skiatest::Reporter* fReporter;
177 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400178 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500179};
180
Brian Osman62419612020-07-22 10:19:02 -0400181// Produces a 2x2 bitmap shader, with opaque colors:
182// [ Red, Green ]
183// [ Blue, White ]
184static sk_sp<SkShader> make_RGBW_shader() {
185 SkBitmap bmp;
186 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
187 SkIRect topLeft = SkIRect::MakeWH(1, 1);
188 bmp.pixmap().erase(SK_ColorRED, topLeft);
189 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
190 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
191 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
192 return bmp.makeShader();
193}
194
Robert Phillipse94b4e12020-07-23 13:54:35 -0400195static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500196 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400197 sk_sp<SkSurface> surface = rContext
198 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
199 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500200 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400201 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500202
Brian Osman504032e2020-01-10 10:05:24 -0500203 using float4 = std::array<float, 4>;
204
Brian Osman62419612020-07-22 10:19:02 -0400205 // Local coords
206 effect.build("", "color = half4(half2(p - 0.5), 0, 1);");
207 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500208
Brian Osman62419612020-07-22 10:19:02 -0400209 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
210 effect.build("uniform float4 gColor;",
211 "color = half4(gColor);");
212 effect.input("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
213 effect.test(0xFFBF4000);
214 effect.input("gColor") = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
215 effect.test(0xFF0040BF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500216
Brian Osman62419612020-07-22 10:19:02 -0400217 // Indexing a uniform array with an 'in' integer
218 effect.build("in int flag; uniform half4 gColors[2];",
219 "color = gColors[flag];");
220 effect.input("gColors") = std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f},
221 float4{0.0f, 1.0f, 0.0f, 1.0f }};
222 effect.input("flag") = 0;
223 effect.test(0x7F00007F); // Tests that we clamp to valid premul
224 effect.input("flag") = 1;
225 effect.test(0xFF00FF00);
Brian Osman504032e2020-01-10 10:05:24 -0500226
Brian Osman62419612020-07-22 10:19:02 -0400227 // 'in' half (functionally a uniform, but handled very differently internally)
228 effect.build("in half c;",
229 "color = half4(c, c, c, 1);");
230 effect.input("c") = 0.498f;
231 effect.test(0xFF7F7F7F);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400232
Brian Osman62419612020-07-22 10:19:02 -0400233 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
234 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
235 // make sure we're not saturating unexpectedly.
236 effect.build("", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);");
237 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
238 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400239
Brian Osmanb5f0f522020-07-23 13:28:14 -0400240 //
241 // Sampling children
242 //
243
Brian Osman62419612020-07-22 10:19:02 -0400244 // Sampling a null child should return the paint color
245 effect.build("in shader child;",
246 "color = sample(child);");
247 effect.child("child") = nullptr;
248 effect.test(0xFF00FFFF,
249 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
250
251 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
252
253 // Sampling a simple child at our coordinates (implicitly)
254 effect.build("in shader child;",
255 "color = sample(child);");
256 effect.child("child") = rgbwShader;
257 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
258
259 // Sampling with explicit coordinates (reflecting about the diagonal)
260 effect.build("in shader child;",
261 "color = sample(child, p.yx);");
262 effect.child("child") = rgbwShader;
263 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
264
265 // Sampling with a matrix (again, reflecting about the diagonal)
266 effect.build("in shader child;",
267 "color = sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));");
268 effect.child("child") = rgbwShader;
269 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
Brian Osmanb5f0f522020-07-23 13:28:14 -0400270
271 //
272 // Helper functions
273 //
274
275 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
276 effect.build("float2 helper(float2 x) { return x + 1; }",
277 "float2 v = helper(p);"
278 "color = half4(half2(v), 0, 1);");
279 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500280}
281
282DEF_TEST(SkRuntimeEffectSimple, r) {
283 test_RuntimeEffect_Shaders(r, nullptr);
284}
285
286DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400287 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500288}