blob: 5271a20b836e4415f4ed373430eba4877f90bb7d [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 Osman088913a2019-12-19 15:44:56 -050064}
Brian Osmanf72dedd2020-01-08 13:19:58 -050065
Brian Osmanf72dedd2020-01-08 13:19:58 -050066class TestEffect {
67public:
Brian Osman62419612020-07-22 10:19:02 -040068 TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface)
69 : fReporter(r), fSurface(std::move(surface)) {}
70
71 void build(const char* header, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -050072 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osman62419612020-07-22 10:19:02 -040073 header, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050074 auto[effect, errorText] = SkRuntimeEffect::Make(src);
75 if (!effect) {
Brian Osman62419612020-07-22 10:19:02 -040076 REPORT_FAILURE(fReporter, "effect",
Brian Osmanf72dedd2020-01-08 13:19:58 -050077 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
78 return;
79 }
Brian Osmand9bde072020-04-15 14:18:13 -040080 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -050081 }
82
Brian Osman62419612020-07-22 10:19:02 -040083 SkRuntimeShaderBuilder::BuilderInput input(const char* name) {
Brian Osmand9bde072020-04-15 14:18:13 -040084 return fBuilder->input(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -050085 }
Brian Osman62419612020-07-22 10:19:02 -040086 SkRuntimeShaderBuilder::BuilderChild child(const char* name) {
87 return fBuilder->child(name);
88 }
Brian Osmanf72dedd2020-01-08 13:19:58 -050089
Brian Osman62419612020-07-22 10:19:02 -040090 using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>;
91
92 void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR,
93 PreTestFn preTestCallback = nullptr) {
Brian Osmand9bde072020-04-15 14:18:13 -040094 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -050095 if (!shader) {
Brian Osman62419612020-07-22 10:19:02 -040096 REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader"));
Brian Osmanf72dedd2020-01-08 13:19:58 -050097 return;
98 }
99
Brian Osman62419612020-07-22 10:19:02 -0400100 SkCanvas* canvas = fSurface->getCanvas();
Brian Osmanf72dedd2020-01-08 13:19:58 -0500101 SkPaint paint;
102 paint.setShader(std::move(shader));
103 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500104
Brian Osman62419612020-07-22 10:19:02 -0400105 canvas->save();
106 if (preTestCallback) {
107 preTestCallback(canvas, &paint);
108 }
109 canvas->drawPaint(paint);
110 canvas->restore();
111
112 GrColor actual[4];
113 SkImageInfo info = fSurface->imageInfo();
114 if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
115 REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed"));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500116 return;
117 }
118
Brian Osman62419612020-07-22 10:19:02 -0400119 GrColor expected[4] = {TL, TR, BL, BR};
Brian Osmanf72dedd2020-01-08 13:19:58 -0500120 if (memcmp(actual, expected, sizeof(actual)) != 0) {
Brian Osman62419612020-07-22 10:19:02 -0400121 REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations",
Brian Osmanf72dedd2020-01-08 13:19:58 -0500122 SkStringPrintf("\n"
123 "Expected: [ %08x %08x %08x %08x ]\n"
124 "Got : [ %08x %08x %08x %08x ]\n"
125 "SkSL:\n%s\n",
126 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmand9bde072020-04-15 14:18:13 -0400127 actual[3], fBuilder->fEffect->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500128 }
129 }
130
Brian Osman62419612020-07-22 10:19:02 -0400131 void test(GrColor expected, PreTestFn preTestCallback = nullptr) {
132 this->test(expected, expected, expected, expected, preTestCallback);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500133 }
134
135private:
Brian Osman62419612020-07-22 10:19:02 -0400136 skiatest::Reporter* fReporter;
137 sk_sp<SkSurface> fSurface;
Brian Osmand9bde072020-04-15 14:18:13 -0400138 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500139};
140
Brian Osman62419612020-07-22 10:19:02 -0400141// Produces a 2x2 bitmap shader, with opaque colors:
142// [ Red, Green ]
143// [ Blue, White ]
144static sk_sp<SkShader> make_RGBW_shader() {
145 SkBitmap bmp;
146 bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType));
147 SkIRect topLeft = SkIRect::MakeWH(1, 1);
148 bmp.pixmap().erase(SK_ColorRED, topLeft);
149 bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0));
150 bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1));
151 bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1));
152 return bmp.makeShader();
153}
154
Robert Phillipse94b4e12020-07-23 13:54:35 -0400155static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) {
Brian Osmanf72dedd2020-01-08 13:19:58 -0500156 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Robert Phillipse94b4e12020-07-23 13:54:35 -0400157 sk_sp<SkSurface> surface = rContext
158 ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info)
159 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500160 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400161 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500162
Brian Osman504032e2020-01-10 10:05:24 -0500163 using float4 = std::array<float, 4>;
164
Brian Osman62419612020-07-22 10:19:02 -0400165 // Local coords
166 effect.build("", "color = half4(half2(p - 0.5), 0, 1);");
167 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500168
Brian Osman62419612020-07-22 10:19:02 -0400169 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
170 effect.build("uniform float4 gColor;",
171 "color = half4(gColor);");
172 effect.input("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
173 effect.test(0xFFBF4000);
174 effect.input("gColor") = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
175 effect.test(0xFF0040BF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500176
Brian Osman62419612020-07-22 10:19:02 -0400177 // Indexing a uniform array with an 'in' integer
178 effect.build("in int flag; uniform half4 gColors[2];",
179 "color = gColors[flag];");
180 effect.input("gColors") = std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f},
181 float4{0.0f, 1.0f, 0.0f, 1.0f }};
182 effect.input("flag") = 0;
183 effect.test(0x7F00007F); // Tests that we clamp to valid premul
184 effect.input("flag") = 1;
185 effect.test(0xFF00FF00);
Brian Osman504032e2020-01-10 10:05:24 -0500186
Brian Osman62419612020-07-22 10:19:02 -0400187 // 'in' half (functionally a uniform, but handled very differently internally)
188 effect.build("in half c;",
189 "color = half4(c, c, c, 1);");
190 effect.input("c") = 0.498f;
191 effect.test(0xFF7F7F7F);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400192
Brian Osman62419612020-07-22 10:19:02 -0400193 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
194 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
195 // make sure we're not saturating unexpectedly.
196 effect.build("", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);");
197 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
198 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400199
Brian Osmanb5f0f522020-07-23 13:28:14 -0400200 //
201 // Sampling children
202 //
203
Brian Osman62419612020-07-22 10:19:02 -0400204 // Sampling a null child should return the paint color
205 effect.build("in shader child;",
206 "color = sample(child);");
207 effect.child("child") = nullptr;
208 effect.test(0xFF00FFFF,
209 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
210
211 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
212
213 // Sampling a simple child at our coordinates (implicitly)
214 effect.build("in shader child;",
215 "color = sample(child);");
216 effect.child("child") = rgbwShader;
217 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
218
219 // Sampling with explicit coordinates (reflecting about the diagonal)
220 effect.build("in shader child;",
221 "color = sample(child, p.yx);");
222 effect.child("child") = rgbwShader;
223 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
224
225 // Sampling with a matrix (again, reflecting about the diagonal)
226 effect.build("in shader child;",
227 "color = sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));");
228 effect.child("child") = rgbwShader;
229 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
Brian Osmanb5f0f522020-07-23 13:28:14 -0400230
231 //
232 // Helper functions
233 //
234
235 // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526):
236 effect.build("float2 helper(float2 x) { return x + 1; }",
237 "float2 v = helper(p);"
238 "color = half4(half2(v), 0, 1);");
239 effect.test(0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500240}
241
242DEF_TEST(SkRuntimeEffectSimple, r) {
243 test_RuntimeEffect_Shaders(r, nullptr);
244}
245
246DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400247 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500248}