blob: ef4f7adf8e715861e63b3cddeaad870ca179a071 [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 Osman088913a2019-12-19 15:44:56 -050013#include "tests/Test.h"
14
Brian Osmanf72dedd2020-01-08 13:19:58 -050015#include <algorithm>
16
Brian Osman5f6b41e2020-03-09 11:53:24 -040017DEF_TEST(SkRuntimeEffectInvalid, r) {
18 auto test = [r](const char* hdr, const char* body, const char* expected) {
19 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
20 hdr, body);
Brian Osmanf72dedd2020-01-08 13:19:58 -050021 auto[effect, errorText] = SkRuntimeEffect::Make(src);
Brian Osman088913a2019-12-19 15:44:56 -050022 REPORTER_ASSERT(r, !effect);
23 REPORTER_ASSERT(r, errorText.contains(expected),
24 "Expected error message to contain \"%s\". Actual message: \"%s\"",
25 expected, errorText.c_str());
26 };
27
28 // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked).
29 // Ensure that these fail, and the error messages contain the relevant keyword.
Brian Osman5f6b41e2020-03-09 11:53:24 -040030 test("layout(key) in bool Input;", "", "key");
31 test("in uniform float Input;", "", "in uniform");
32 test("layout(ctype=SkRect) float4 Input;", "", "ctype");
33 test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when");
34 test("layout(tracked) uniform float Input;", "", "tracked");
Brian Osman088913a2019-12-19 15:44:56 -050035
Brian Osman82d49702019-12-30 13:44:01 -050036 // Runtime SkSL supports a limited set of uniform types. No samplers, for example:
Brian Osman5f6b41e2020-03-09 11:53:24 -040037 test("uniform sampler2D s;", "", "sampler2D");
Brian Osman088913a2019-12-19 15:44:56 -050038
Brian Osman82d49702019-12-30 13:44:01 -050039 // 'in' variables can't be arrays
Brian Osman5f6b41e2020-03-09 11:53:24 -040040 test("in int Input[2];", "", "array");
Brian Osman8783b782020-01-06 11:13:45 -050041
42 // Type specific restrictions:
43
44 // 'bool', 'int' can't be 'uniform'
Brian Osman5f6b41e2020-03-09 11:53:24 -040045 test("uniform bool Input;", "", "'uniform'");
46 test("uniform int Input;", "", "'uniform'");
Brian Osman8783b782020-01-06 11:13:45 -050047
48 // vector and matrix types can't be 'in'
Brian Osman5f6b41e2020-03-09 11:53:24 -040049 test("in float2 Input;", "", "'in'");
50 test("in half3x3 Input;", "", "'in'");
51
52 test("half missing();", "color.r = missing();", "undefined function");
Brian Osman088913a2019-12-19 15:44:56 -050053}
Brian Osmanf72dedd2020-01-08 13:19:58 -050054
55// Our packing rules and unit test code here relies on this:
56static_assert(sizeof(bool) == 1);
57
58class TestEffect {
59public:
60 TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -050061 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osmanf72dedd2020-01-08 13:19:58 -050062 hdr, body);
63 auto[effect, errorText] = SkRuntimeEffect::Make(src);
64 if (!effect) {
65 REPORT_FAILURE(r, "effect",
66 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
67 return;
68 }
69
70 fEffect = std::move(effect);
71 fInputs = SkData::MakeUninitialized(fEffect->inputSize());
72 }
73
74 struct InputVar {
75 template <typename T> InputVar& operator=(const T& val) {
76 SkASSERT(sizeof(T) == fVar.sizeInBytes());
77 memcpy(SkTAddOffset<void>(fOwner->fInputs->writable_data(), fVar.fOffset), &val,
78 sizeof(T));
79 return *this;
80 }
81 TestEffect* fOwner;
82 const SkRuntimeEffect::Variable& fVar;
83 };
84
85 InputVar operator[](const char* name) {
86 auto input = std::find_if(fEffect->inputs().begin(), fEffect->inputs().end(),
87 [name](const auto& v) { return v.fName.equals(name); });
88 SkASSERT(input != fEffect->inputs().end());
89 return {this, *input};
90 }
91
92 void test(skiatest::Reporter* r, sk_sp<SkSurface> surface,
93 uint32_t TL, uint32_t TR, uint32_t BL, uint32_t BR) {
94 if (!fEffect) { return; }
95
96 auto shader = fEffect->makeShader(fInputs, nullptr, 0, nullptr, false);
97 if (!shader) {
98 REPORT_FAILURE(r, "shader", SkString("Effect didn't produce a shader"));
99 return;
100 }
101
102 SkPaint paint;
103 paint.setShader(std::move(shader));
104 paint.setBlendMode(SkBlendMode::kSrc);
105 surface->getCanvas()->drawPaint(paint);
106
107 uint32_t actual[4];
108 SkImageInfo info = surface->imageInfo();
109 if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
110 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
111 return;
112 }
113
114 uint32_t expected[4] = {TL, TR, BL, BR};
115 if (memcmp(actual, expected, sizeof(actual)) != 0) {
116 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
117 SkStringPrintf("\n"
118 "Expected: [ %08x %08x %08x %08x ]\n"
119 "Got : [ %08x %08x %08x %08x ]\n"
120 "SkSL:\n%s\n",
121 TL, TR, BL, BR, actual[0], actual[1], actual[2],
122 actual[3], fEffect->source().c_str()));
123 }
124 }
125
126 void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) {
127 this->test(r, surface, expected, expected, expected, expected);
128 }
129
130private:
131 sk_sp<SkRuntimeEffect> fEffect;
132 sk_sp<SkData> fInputs;
133};
134
135static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) {
136 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
137 sk_sp<SkSurface> surface;
138 if (context) {
139 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
140 } else {
141 surface = SkSurface::MakeRaster(info);
142 }
143 REPORTER_ASSERT(r, surface);
144
Brian Osman7353dc52020-02-07 13:37:12 -0500145 TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);");
Brian Osmanf72dedd2020-01-08 13:19:58 -0500146 xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
147
Brian Osman504032e2020-01-10 10:05:24 -0500148 using float4 = std::array<float, 4>;
149
Brian Osmanf72dedd2020-01-08 13:19:58 -0500150 // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping
151 TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);");
152
Brian Osman504032e2020-01-10 10:05:24 -0500153 uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osmanf72dedd2020-01-08 13:19:58 -0500154 uniformColor.test(r, surface, 0xFFBF4000);
155
Brian Osman504032e2020-01-10 10:05:24 -0500156 uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
Brian Osmanf72dedd2020-01-08 13:19:58 -0500157 uniformColor.test(r, surface, 0xFF0040BF);
Brian Osman504032e2020-01-10 10:05:24 -0500158
159 TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];");
160 pickColor["gColors"] =
Brian Osman6f5e9402020-01-22 10:39:31 -0500161 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 -0500162 pickColor["flag"] = 0;
Brian Osman6f5e9402020-01-22 10:39:31 -0500163 pickColor.test(r, surface, 0x7F00007F); // Tests that we clamp to valid premul
Brian Osman504032e2020-01-10 10:05:24 -0500164 pickColor["flag"] = 1;
165 pickColor.test(r, surface, 0xFF00FF00);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500166}
167
168DEF_TEST(SkRuntimeEffectSimple, r) {
169 test_RuntimeEffect_Shaders(r, nullptr);
170}
171
172DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
173 test_RuntimeEffect_Shaders(r, ctxInfo.grContext());
174}