blob: b3a5522ccedc459df1de05c8269c32ee6359e6fe [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"
Robert Phillips6d344c32020-07-06 10:56:46 -040012#include "include/gpu/GrDirectContext.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
Brian Osmane64ae862020-07-16 15:29:15 -040053 // 'marker' is only permitted on 'uniform' variables
54 test("layout(marker=local_to_world) in float4x4 localToWorld;", "", "'uniform'");
55 // 'marker' is only permitted on float4x4 variables
56 test("layout(marker=local_to_world) uniform float3x3 localToWorld;", "", "float4x4");
57
Brian Osman5f6b41e2020-03-09 11:53:24 -040058 test("half missing();", "color.r = missing();", "undefined function");
Brian Osman182c92e2020-07-20 15:18:33 -040059
60 // Shouldn't be possible to create an SkRuntimeEffect without "main"
61 test("//", "", "main");
Brian Osman088913a2019-12-19 15:44:56 -050062}
Brian Osmanf72dedd2020-01-08 13:19:58 -050063
Brian Osmanf72dedd2020-01-08 13:19:58 -050064class TestEffect {
65public:
66 TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) {
Brian Osman7353dc52020-02-07 13:37:12 -050067 SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }",
Brian Osmanf72dedd2020-01-08 13:19:58 -050068 hdr, body);
69 auto[effect, errorText] = SkRuntimeEffect::Make(src);
70 if (!effect) {
71 REPORT_FAILURE(r, "effect",
72 SkStringPrintf("Effect didn't compile: %s", errorText.c_str()));
73 return;
74 }
75
Brian Osmand9bde072020-04-15 14:18:13 -040076 fBuilder.init(std::move(effect));
Brian Osmanf72dedd2020-01-08 13:19:58 -050077 }
78
Brian Osmand9bde072020-04-15 14:18:13 -040079 SkRuntimeShaderBuilder::BuilderInput operator[](const char* name) {
80 return fBuilder->input(name);
Brian Osmanf72dedd2020-01-08 13:19:58 -050081 }
82
83 void test(skiatest::Reporter* r, sk_sp<SkSurface> surface,
Brian Osman5aaaeea2020-06-22 14:26:03 -040084 uint32_t TL, uint32_t TR, uint32_t BL, uint32_t BR, SkScalar rotate = 0.0f) {
Brian Osmand9bde072020-04-15 14:18:13 -040085 auto shader = fBuilder->makeShader(nullptr, false);
Brian Osmanf72dedd2020-01-08 13:19:58 -050086 if (!shader) {
87 REPORT_FAILURE(r, "shader", SkString("Effect didn't produce a shader"));
88 return;
89 }
90
91 SkPaint paint;
92 paint.setShader(std::move(shader));
93 paint.setBlendMode(SkBlendMode::kSrc);
Brian Osman5aaaeea2020-06-22 14:26:03 -040094 surface->getCanvas()->rotate(rotate);
Brian Osmanf72dedd2020-01-08 13:19:58 -050095 surface->getCanvas()->drawPaint(paint);
96
97 uint32_t actual[4];
98 SkImageInfo info = surface->imageInfo();
99 if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) {
100 REPORT_FAILURE(r, "readPixels", SkString("readPixels failed"));
101 return;
102 }
103
104 uint32_t expected[4] = {TL, TR, BL, BR};
105 if (memcmp(actual, expected, sizeof(actual)) != 0) {
106 REPORT_FAILURE(r, "Runtime effect didn't match expectations",
107 SkStringPrintf("\n"
108 "Expected: [ %08x %08x %08x %08x ]\n"
109 "Got : [ %08x %08x %08x %08x ]\n"
110 "SkSL:\n%s\n",
111 TL, TR, BL, BR, actual[0], actual[1], actual[2],
Brian Osmand9bde072020-04-15 14:18:13 -0400112 actual[3], fBuilder->fEffect->source().c_str()));
Brian Osmanf72dedd2020-01-08 13:19:58 -0500113 }
114 }
115
116 void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) {
117 this->test(r, surface, expected, expected, expected, expected);
118 }
119
120private:
Brian Osmand9bde072020-04-15 14:18:13 -0400121 SkTLazy<SkRuntimeShaderBuilder> fBuilder;
Brian Osmanf72dedd2020-01-08 13:19:58 -0500122};
123
124static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) {
125 SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
126 sk_sp<SkSurface> surface;
127 if (context) {
128 surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info);
129 } else {
130 surface = SkSurface::MakeRaster(info);
131 }
132 REPORTER_ASSERT(r, surface);
133
Brian Osman7353dc52020-02-07 13:37:12 -0500134 TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);");
Brian Osmanf72dedd2020-01-08 13:19:58 -0500135 xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF);
136
Brian Osman504032e2020-01-10 10:05:24 -0500137 using float4 = std::array<float, 4>;
138
Brian Osmanf72dedd2020-01-08 13:19:58 -0500139 // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping
140 TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);");
141
Brian Osman504032e2020-01-10 10:05:24 -0500142 uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f };
Brian Osmanf72dedd2020-01-08 13:19:58 -0500143 uniformColor.test(r, surface, 0xFFBF4000);
144
Brian Osman504032e2020-01-10 10:05:24 -0500145 uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f };
Brian Osmanf72dedd2020-01-08 13:19:58 -0500146 uniformColor.test(r, surface, 0xFF0040BF);
Brian Osman504032e2020-01-10 10:05:24 -0500147
148 TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];");
149 pickColor["gColors"] =
Brian Osman6f5e9402020-01-22 10:39:31 -0500150 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 -0500151 pickColor["flag"] = 0;
Brian Osman6f5e9402020-01-22 10:39:31 -0500152 pickColor.test(r, surface, 0x7F00007F); // Tests that we clamp to valid premul
Brian Osman504032e2020-01-10 10:05:24 -0500153 pickColor["flag"] = 1;
154 pickColor.test(r, surface, 0xFF00FF00);
Michael Ludwig5e6b3cd2020-05-27 17:02:37 -0400155
156 TestEffect inlineColor(r, "in half c;", "color = half4(c, c, c, 1);");
157 inlineColor["c"] = 0.498f;
158 inlineColor.test(r, surface, 0xFF7F7F7F);
Michael Ludwig22534f22020-05-27 17:25:33 -0400159
160 // Test sk_FragCoord, which we output to color. Since the surface is 2x2, we should see
161 // (0,0), (1,0), (0,1), (1,1), multiply by 0.498 to make sure we're not saturating unexpectedly.
Brian Osman5aaaeea2020-06-22 14:26:03 -0400162 TestEffect fragCoord(r, "", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);");
163 fragCoord.test(r, surface, 0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, 45.0f);
Brian Osmanf72dedd2020-01-08 13:19:58 -0500164}
165
166DEF_TEST(SkRuntimeEffectSimple, r) {
167 test_RuntimeEffect_Shaders(r, nullptr);
168}
169
170DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400171 test_RuntimeEffect_Shaders(r, ctxInfo.directContext());
Brian Osmanf72dedd2020-01-08 13:19:58 -0500172}