blob: 9ec8234dc842cfea1cd93e4f6d36ad415b8c82e8 [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
Brian Osmanf72dedd2020-01-08 13:19:58 -0500155static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) {
156 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
Brian Osman62419612020-07-22 10:19:02 -0400157 sk_sp<SkSurface> surface = context ? SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)
158 : SkSurface::MakeRaster(info);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500159 REPORTER_ASSERT(r, surface);
Brian Osman62419612020-07-22 10:19:02 -0400160 TestEffect effect(r, surface);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500161
Brian Osman504032e2020-01-10 10:05:24 -0500162 using float4 = std::array<float, 4>;
163
Brian Osman62419612020-07-22 10:19:02 -0400164 // Local coords
165 effect.build("", "color = half4(half2(p - 0.5), 0, 1);");
166 effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500167
Brian Osman62419612020-07-22 10:19:02 -0400168 // Use of a simple uniform. (Draw twice with two values to ensure it's updated).
169 effect.build("uniform float4 gColor;",
170 "color = half4(gColor);");
171 effect.input("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
172 effect.test(0xFFBF4000);
173 effect.input("gColor") = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
174 effect.test(0xFF0040BF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500175
Brian Osman62419612020-07-22 10:19:02 -0400176 // Indexing a uniform array with an 'in' integer
177 effect.build("in int flag; uniform half4 gColors[2];",
178 "color = gColors[flag];");
179 effect.input("gColors") = std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f},
180 float4{0.0f, 1.0f, 0.0f, 1.0f }};
181 effect.input("flag") = 0;
182 effect.test(0x7F00007F); // Tests that we clamp to valid premul
183 effect.input("flag") = 1;
184 effect.test(0xFF00FF00);
Brian Osman504032e2020-01-10 10:05:24 -0500185
Brian Osman62419612020-07-22 10:19:02 -0400186 // 'in' half (functionally a uniform, but handled very differently internally)
187 effect.build("in half c;",
188 "color = half4(c, c, c, 1);");
189 effect.input("c") = 0.498f;
190 effect.test(0xFF7F7F7F);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400191
Brian Osman62419612020-07-22 10:19:02 -0400192 // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords.
193 // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to
194 // make sure we're not saturating unexpectedly.
195 effect.build("", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);");
196 effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F,
197 [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); });
Michael Ludwig22534f22020-05-27 17:25:33 -0400198
Brian Osman62419612020-07-22 10:19:02 -0400199 // Sampling a null child should return the paint color
200 effect.build("in shader child;",
201 "color = sample(child);");
202 effect.child("child") = nullptr;
203 effect.test(0xFF00FFFF,
204 [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); });
205
206 sk_sp<SkShader> rgbwShader = make_RGBW_shader();
207
208 // Sampling a simple child at our coordinates (implicitly)
209 effect.build("in shader child;",
210 "color = sample(child);");
211 effect.child("child") = rgbwShader;
212 effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF);
213
214 // Sampling with explicit coordinates (reflecting about the diagonal)
215 effect.build("in shader child;",
216 "color = sample(child, p.yx);");
217 effect.child("child") = rgbwShader;
218 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
219
220 // Sampling with a matrix (again, reflecting about the diagonal)
221 effect.build("in shader child;",
222 "color = sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));");
223 effect.child("child") = rgbwShader;
224 effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500225}
226
227DEF_TEST(SkRuntimeEffectSimple, r) {
228 test_RuntimeEffect_Shaders(r, nullptr);
229}
230
231DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400232 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500233}