blob: 84e0f9a6c105efe7eb9eff41d50800c4ef9facee [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"
10#include "include/core/SkPaint.h"
11#include "include/core/SkSurface.h"
Brian Osmanee426f22020-01-02 11:55:24 -050012#include "include/effects/SkRuntimeEffect.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040013#include "include/gpu/GrDirectContext.h"
Brian Osmand9bde072020-04-15 14:18:13 -040014#include "src/core/SkTLazy.h"
Brian Osman62419612020-07-22 10:19:02 -040015#include "src/gpu/GrColor.h"
Brian Osman088913a2019-12-19 15:44:56 -050016#include "tests/Test.h"
17
Brian Osmanf72dedd2020-01-08 13:19:58 -050018#include <algorithm>
19
Brian Osman5f6b41e2020-03-09 11:53:24 -040020DEF_TEST(SkRuntimeEffectInvalid, r) {
21 auto test = [r](const char* hdr, const char* body, const char* expected) {
22 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
23 hdr, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050024 auto[effect, errorText] = SkRuntimeEffect::Make(src);
Brian Osman088913a2019-12-19 15:44:56 -050025 REPORTER_ASSERT(r, !effect);
26 REPORTER_ASSERT(r, errorText.contains(expected),
27 "Expected error message to contain \"%s\". Actual message: \"%s\"",
28 expected, errorText.c_str());
29 };
30
31 // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
32 // Ensure that these fail, and the error messages contain the relevant keyword.
Brian Osman5f6b41e2020-03-09 11:53:24 -040033 test("layout(key) in bool Input;", "", "key");
34 test("in uniform float Input;", "", "in uniform");
35 test("layout(ctype=SkRect) float4 Input;", "", "ctype");
36 test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when");
37 test("layout(tracked) uniform float Input;", "", "tracked");
Brian Osman088913a2019-12-19 15:44:56 -050038
Brian Osman82d49702019-12-30 13:44:01 -050039 // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
Brian Osman5f6b41e2020-03-09 11:53:24 -040040 test("uniform sampler2D s;", "", "sampler2D");
Brian Osman088913a2019-12-19 15:44:56 -050041
Brian Osman82d49702019-12-30 13:44:01 -050042 // 'in' variables can't be arrays
Brian Osman5f6b41e2020-03-09 11:53:24 -040043 test("in int Input[2];", "", "array");
Brian Osman8783b782020-01-06 11:13:45 -050044
45 // Type specific restrictions:
46
47 // 'bool', 'int' can't be 'uniform'
Brian Osman5f6b41e2020-03-09 11:53:24 -040048 test("uniform bool Input;", "", "'uniform'");
49 test("uniform int Input;", "", "'uniform'");
Brian Osman8783b782020-01-06 11:13:45 -050050
51 // vector and matrix types can't be 'in'
Brian Osman5f6b41e2020-03-09 11:53:24 -040052 test("in float2 Input;", "", "'in'");
53 test("in half3x3 Input;", "", "'in'");
54
Brian Osmane64ae862020-07-16 15:29:15 -040055 // 'marker' is only permitted on 'uniform' variables
56 test("layout(marker=local_to_world) in float4x4 localToWorld;", "", "'uniform'");
57 // 'marker' is only permitted on float4x4 variables
58 test("layout(marker=local_to_world) uniform float3x3 localToWorld;", "", "float4x4");
59
Brian Osman5f6b41e2020-03-09 11:53:24 -040060 test("half missing();", "color.r = missing();", "undefined function");
Brian Osman182c92e2020-07-20 15:18:33 -040061
62 // Shouldn't be possible to create an SkRuntimeEffect without "main"
63 test("//", "", "main");
Brian Osman82329002020-07-21 09:39:27 -040064
65 // Various places that shaders (fragmentProcessors) should not be allowed
66 test("",
67 "shader child;",
68 "must be global");
69 test("in shader child; half4 helper(shader fp) { return sample(fp); }",
70 "color = helper(child);",
71 "parameter");
72 test("in shader child; shader get_child() { return child; }",
73 "color = sample(get_child());",
74 "return");
75 test("in shader child;",
76 "color = sample(shader(child));",
77 "construct");
78 test("in shader child1; in shader child2;",
79 "color = sample(p.x > 10 ? child1 : child2);",
80 "expression");
Brian Osman088913a2019-12-19 15:44:56 -050081}
Brian Osmanf72dedd2020-01-08 13:19:58 -050082
Brian Osmanf72dedd2020-01-08 13:19:58 -050083class TestEffect {
84public:
Brian Osman62419612020-07-22 10:19:02 -040085 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
86 : fReporter(r), fSurface(std::move(surface)) {}
87
88 void build(const char* header, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -050089 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osman62419612020-07-22 10:19:02 -040090 header, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050091 auto[effect, errorText] = SkRuntimeEffect::Make(src);
92 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -040093 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -050094 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
95 return;
96 }
Brian Osmand9bde072020-04-15 14:18:13 -040097 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -050098 }
99
Brian Osman62419612020-07-22 10:19:02 -0400100 SkRuntimeShaderBuilder::BuilderInput input(const char* name) {
Brian Osmand9bde072020-04-15 14:18:13 -0400101 return fBuilder->input(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500102 }
Brian Osman62419612020-07-22 10:19:02 -0400103 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
104 return fBuilder->child(name);
105 }
Brian Osmanf72dedd2020-01-08 13:19:58 -0500106
Brian Osman62419612020-07-22 10:19:02 -0400107 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
108
109 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
110 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -0400111 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500112 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -0400113 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500114 return;
115 }
116
Brian Osman62419612020-07-22 10:19:02 -0400117 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500118 SkPaint paint;
119 paint.setShader(std::move(shader));
120 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500121
Brian Osman62419612020-07-22 10:19:02 -0400122 canvas->save();
123 if (preTestCallback) {
124 preTestCallback(canvas, &paint);
125 }
126 canvas->drawPaint(paint);
127 canvas->restore();
128
129 GrColor actual[4];
130 SkImageInfo info = fSurface->imageInfo();
131 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
132 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500133 return;
134 }
135
Brian Osman62419612020-07-22 10:19:02 -0400136 GrColor expected[4] = {TL, TR, BL, BR};
Brian Osmanf72dedd2020-01-08 13:19:58 -0500137 if (memcmp(actual, expected, sizeof(actual)) != 0) {
Brian Osman62419612020-07-22 10:19:02 -0400138 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500139 SkStringPrintf("\n"
140 "Expected: [ %08x %08x %08x %08x ]\n"
141 "Got : [ %08x %08x %08x %08x ]\n"
142 "SkSL:\n%s\n",
143 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmand9bde072020-04-15 14:18:13 -0400144 actual[3], fBuilder->fEffect->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500145 }
146 }
147
Brian Osman62419612020-07-22 10:19:02 -0400148 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
149 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500150 }
151
152private:
Brian Osman62419612020-07-22 10:19:02 -0400153 skiatest::Reporter* fReporter;
154 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400155 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500156};
157
Brian Osman62419612020-07-22 10:19:02 -0400158// Produces a 2x2 bitmap shader, with opaque colors:
159// [ Red, Green ]
160// [ Blue, White ]
161static sk_sp<SkShader> make_RGBW_shader() {
162 SkBitmap bmp;
163 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
164 SkIRect topLeft = SkIRect::MakeWH(1, 1);
165 bmp.pixmap().erase(SK_ColorRED, topLeft);
166 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
167 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
168 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
169 return bmp.makeShader();
170}
171
Robert Phillipse94b4e12020-07-23 13:54:35 -0400172static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500173 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400174 sk_sp<SkSurface> surface = rContext
175 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
176 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500177 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400178 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500179
Brian Osman504032e2020-01-10 10:05:24 -0500180 using float4 = std::array<float, 4>;
181
Brian Osman62419612020-07-22 10:19:02 -0400182 // Local coords
183 effect.build("", "color = half4(half2(p - 0.5), 0, 1);");
184 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500185
Brian Osman62419612020-07-22 10:19:02 -0400186 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
187 effect.build("uniform float4 gColor;",
188 "color = half4(gColor);");
189 effect.input("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
190 effect.test(0xFFBF4000);
191 effect.input("gColor") = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
192 effect.test(0xFF0040BF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500193
Brian Osman62419612020-07-22 10:19:02 -0400194 // Indexing a uniform array with an 'in' integer
195 effect.build("in int flag; uniform half4 gColors[2];",
196 "color = gColors[flag];");
197 effect.input("gColors") = std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f},
198 float4{0.0f, 1.0f, 0.0f, 1.0f }};
199 effect.input("flag") = 0;
200 effect.test(0x7F00007F); // Tests that we clamp to valid premul
201 effect.input("flag") = 1;
202 effect.test(0xFF00FF00);
Brian Osman504032e2020-01-10 10:05:24 -0500203
Brian Osman62419612020-07-22 10:19:02 -0400204 // 'in' half (functionally a uniform, but handled very differently internally)
205 effect.build("in half c;",
206 "color = half4(c, c, c, 1);");
207 effect.input("c") = 0.498f;
208 effect.test(0xFF7F7F7F);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400209
Brian Osman62419612020-07-22 10:19:02 -0400210 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
211 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
212 // make sure we're not saturating unexpectedly.
213 effect.build("", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);");
214 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
215 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400216
Brian Osmanb5f0f522020-07-23 13:28:14 -0400217 //
218 // Sampling children
219 //
220
Brian Osman62419612020-07-22 10:19:02 -0400221 // Sampling a null child should return the paint color
222 effect.build("in shader child;",
223 "color = sample(child);");
224 effect.child("child") = nullptr;
225 effect.test(0xFF00FFFF,
226 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
227
228 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
229
230 // Sampling a simple child at our coordinates (implicitly)
231 effect.build("in shader child;",
232 "color = sample(child);");
233 effect.child("child") = rgbwShader;
234 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
235
236 // Sampling with explicit coordinates (reflecting about the diagonal)
237 effect.build("in shader child;",
238 "color = sample(child, p.yx);");
239 effect.child("child") = rgbwShader;
240 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
241
242 // Sampling with a matrix (again, reflecting about the diagonal)
243 effect.build("in shader child;",
244 "color = sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));");
245 effect.child("child") = rgbwShader;
246 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
Brian Osmanb5f0f522020-07-23 13:28:14 -0400247
248 //
249 // Helper functions
250 //
251
252 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
253 effect.build("float2 helper(float2 x) { return x + 1; }",
254 "float2 v = helper(p);"
255 "color = half4(half2(v), 0, 1);");
256 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500257}
258
259DEF_TEST(SkRuntimeEffectSimple, r) {
260 test_RuntimeEffect_Shaders(r, nullptr);
261}
262
263DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400264 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500265}