blob: 66dacd3ff667ccdcc65a3a1285993bb72ff2eb0a [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 Osmanf72dedd2020-01-08 13:19:58 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkSurface.h"
Brian Osmanee426f22020-01-02 11:55:24 -050011#include "include/effects/SkRuntimeEffect.h"
Brian Osmanf72dedd2020-01-08 13:19:58 -050012#include "include/gpu/GrContext.h"
Brian Osmand9bde072020-04-15 14:18:13 -040013#include "src/core/SkTLazy.h"
Brian Osman088913a2019-12-19 15:44:56 -050014#include "tests/Test.h"
15
Brian Osmanf72dedd2020-01-08 13:19:58 -050016#include <algorithm>
17
Brian Osman5f6b41e2020-03-09 11:53:24 -040018DEF_TEST(SkRuntimeEffectInvalid, r) {
19 auto test = [r](const char* hdr, const char* body, const char* expected) {
20 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
21 hdr, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050022 auto[effect, errorText] = SkRuntimeEffect::Make(src);
Brian Osman088913a2019-12-19 15:44:56 -050023 REPORTER_ASSERT(r, !effect);
24 REPORTER_ASSERT(r, errorText.contains(expected),
25 "Expected error message to contain \"%s\". Actual message: \"%s\"",
26 expected, errorText.c_str());
27 };
28
29 // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
30 // Ensure that these fail, and the error messages contain the relevant keyword.
Brian Osman5f6b41e2020-03-09 11:53:24 -040031 test("layout(key) in bool Input;", "", "key");
32 test("in uniform float Input;", "", "in uniform");
33 test("layout(ctype=SkRect) float4 Input;", "", "ctype");
34 test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when");
35 test("layout(tracked) uniform float Input;", "", "tracked");
Brian Osman088913a2019-12-19 15:44:56 -050036
Brian Osman82d49702019-12-30 13:44:01 -050037 // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
Brian Osman5f6b41e2020-03-09 11:53:24 -040038 test("uniform sampler2D s;", "", "sampler2D");
Brian Osman088913a2019-12-19 15:44:56 -050039
Brian Osman82d49702019-12-30 13:44:01 -050040 // 'in' variables can't be arrays
Brian Osman5f6b41e2020-03-09 11:53:24 -040041 test("in int Input[2];", "", "array");
Brian Osman8783b782020-01-06 11:13:45 -050042
43 // Type specific restrictions:
44
45 // 'bool', 'int' can't be 'uniform'
Brian Osman5f6b41e2020-03-09 11:53:24 -040046 test("uniform bool Input;", "", "'uniform'");
47 test("uniform int Input;", "", "'uniform'");
Brian Osman8783b782020-01-06 11:13:45 -050048
49 // vector and matrix types can't be 'in'
Brian Osman5f6b41e2020-03-09 11:53:24 -040050 test("in float2 Input;", "", "'in'");
51 test("in half3x3 Input;", "", "'in'");
52
53 test("half missing();", "color.r = missing();", "undefined function");
Brian Osman088913a2019-12-19 15:44:56 -050054}
Brian Osmanf72dedd2020-01-08 13:19:58 -050055
56// Our packing rules and unit test code here relies on this:
57static_assert(sizeof(bool) == 1);
58
59class TestEffect {
60public:
61 TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -050062 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osmanf72dedd2020-01-08 13:19:58 -050063 hdr, body);
64 auto[effect, errorText] = SkRuntimeEffect::Make(src);
65 if (!effect) {
66 REPORT_FAILURE(r, "effect",
67 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
68 return;
69 }
70
Brian Osmand9bde072020-04-15 14:18:13 -040071 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -050072 }
73
Brian Osmand9bde072020-04-15 14:18:13 -040074 SkRuntimeShaderBuilder::BuilderInput operator[](const char* name) {
75 return fBuilder->input(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -050076 }
77
78 void test(skiatest::Reporter* r, sk_sp<SkSurface> surface,
79 uint32_t TL, uint32_t TR, uint32_t BL, uint32_t BR) {
Brian Osmand9bde072020-04-15 14:18:13 -040080 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -050081 if (!shader) {
82 REPORT_FAILURE(r, "shader", SkString("Effect didn't produce a shader"));
83 return;
84 }
85
86 SkPaint paint;
87 paint.setShader(std::move(shader));
88 paint.setBlendMode(SkBlendMode::kSrc);
89 surface->getCanvas()->drawPaint(paint);
90
91 uint32_t actual[4];
92 SkImageInfo info = surface->imageInfo();
93 if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
94 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
95 return;
96 }
97
98 uint32_t expected[4] = {TL, TR, BL, BR};
99 if (memcmp(actual, expected, sizeof(actual)) != 0) {
100 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
101 SkStringPrintf("\n"
102 "Expected: [ %08x %08x %08x %08x ]\n"
103 "Got : [ %08x %08x %08x %08x ]\n"
104 "SkSL:\n%s\n",
105 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmand9bde072020-04-15 14:18:13 -0400106 actual[3], fBuilder->fEffect->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500107 }
108 }
109
110 void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) {
111 this->test(r, surface, expected, expected, expected, expected);
112 }
113
114private:
Brian Osmand9bde072020-04-15 14:18:13 -0400115 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500116};
117
118static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) {
119 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
120 sk_sp<SkSurface> surface;
121 if (context) {
122 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
123 } else {
124 surface = SkSurface::MakeRaster(info);
125 }
126 REPORTER_ASSERT(r, surface);
127
Brian Osman7353dc52020-02-07 13:37:12 -0500128 TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);");
Brian Osmanf72dedd2020-01-08 13:19:58 -0500129 xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
130
Brian Osman504032e2020-01-10 10:05:24 -0500131 using float4 = std::array<float, 4>;
132
Brian Osmanf72dedd2020-01-08 13:19:58 -0500133 // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping
134 TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);");
135
Brian Osman504032e2020-01-10 10:05:24 -0500136 uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osmanf72dedd2020-01-08 13:19:58 -0500137 uniformColor.test(r, surface, 0xFFBF4000);
138
Brian Osman504032e2020-01-10 10:05:24 -0500139 uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
Brian Osmanf72dedd2020-01-08 13:19:58 -0500140 uniformColor.test(r, surface, 0xFF0040BF);
Brian Osman504032e2020-01-10 10:05:24 -0500141
142 TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];");
143 pickColor["gColors"] =
Brian Osman6f5e9402020-01-22 10:39:31 -0500144 std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f}, float4{0.0f, 1.0f, 0.0f, 1.0f}};
Brian Osman504032e2020-01-10 10:05:24 -0500145 pickColor["flag"] = 0;
Brian Osman6f5e9402020-01-22 10:39:31 -0500146 pickColor.test(r, surface, 0x7F00007F); // Tests that we clamp to valid premul
Brian Osman504032e2020-01-10 10:05:24 -0500147 pickColor["flag"] = 1;
148 pickColor.test(r, surface, 0xFF00FF00);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400149
150 TestEffect inlineColor(r, "in half c;", "color = half4(c, c, c, 1);");
151 inlineColor["c"] = 0.498f;
152 inlineColor.test(r, surface, 0xFF7F7F7F);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500153}
154
155DEF_TEST(SkRuntimeEffectSimple, r) {
156 test_RuntimeEffect_Shaders(r, nullptr);
157}
158
159DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
160 test_RuntimeEffect_Shaders(r, ctxInfo.grContext());
161}