Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 1 | /* |
| 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 Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkSurface.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 13 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 14 | #include "include/gpu/GrDirectContext.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 15 | #include "src/core/SkTLazy.h" |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrColor.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 17 | #include "tests/Test.h" |
| 18 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 21 | DEF_TEST(SkRuntimeEffectInvalid, r) { |
| 22 | auto test = [r](const char* hdr, const char* body, const char* expected) { |
| 23 | SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }", |
| 24 | hdr, body); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 25 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 26 | REPORTER_ASSERT(r, !effect); |
| 27 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 28 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 29 | expected, errorText.c_str()); |
| 30 | }; |
| 31 | |
| 32 | // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked). |
| 33 | // Ensure that these fail, and the error messages contain the relevant keyword. |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 34 | test("layout(key) in bool Input;", "", "key"); |
| 35 | test("in uniform float Input;", "", "in uniform"); |
| 36 | test("layout(ctype=SkRect) float4 Input;", "", "ctype"); |
| 37 | test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when"); |
| 38 | test("layout(tracked) uniform float Input;", "", "tracked"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 39 | |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 40 | // Runtime SkSL supports a limited set of uniform types. No samplers, for example: |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 41 | test("uniform sampler2D s;", "", "sampler2D"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 42 | |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 43 | // 'in' variables can't be arrays |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 44 | test("in int Input[2];", "", "array"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 45 | |
| 46 | // Type specific restrictions: |
| 47 | |
| 48 | // 'bool', 'int' can't be 'uniform' |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 49 | test("uniform bool Input;", "", "'uniform'"); |
| 50 | test("uniform int Input;", "", "'uniform'"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 51 | |
| 52 | // vector and matrix types can't be 'in' |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 53 | test("in float2 Input;", "", "'in'"); |
| 54 | test("in half3x3 Input;", "", "'in'"); |
| 55 | |
Brian Osman | e64ae86 | 2020-07-16 15:29:15 -0400 | [diff] [blame] | 56 | // 'marker' is only permitted on 'uniform' variables |
| 57 | test("layout(marker=local_to_world) in float4x4 localToWorld;", "", "'uniform'"); |
| 58 | // 'marker' is only permitted on float4x4 variables |
| 59 | test("layout(marker=local_to_world) uniform float3x3 localToWorld;", "", "float4x4"); |
| 60 | |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 61 | test("half missing();", "color.r = missing();", "undefined function"); |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 62 | |
| 63 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
| 64 | test("//", "", "main"); |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 65 | |
| 66 | // Various places that shaders (fragmentProcessors) should not be allowed |
| 67 | test("", |
| 68 | "shader child;", |
| 69 | "must be global"); |
| 70 | test("in shader child; half4 helper(shader fp) { return sample(fp); }", |
| 71 | "color = helper(child);", |
| 72 | "parameter"); |
| 73 | test("in shader child; shader get_child() { return child; }", |
| 74 | "color = sample(get_child());", |
| 75 | "return"); |
| 76 | test("in shader child;", |
| 77 | "color = sample(shader(child));", |
| 78 | "construct"); |
| 79 | test("in shader child1; in shader child2;", |
| 80 | "color = sample(p.x > 10 ? child1 : child2);", |
| 81 | "expression"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 82 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 83 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 84 | DEF_TEST(SkRuntimeEffectInvalidColorFilters, r) { |
| 85 | auto test = [r](const char* sksl) { |
| 86 | auto [effect, errorText] = SkRuntimeEffect::Make(SkString(sksl)); |
| 87 | REPORTER_ASSERT(r, effect); |
| 88 | REPORTER_ASSERT(r, effect->makeShader(nullptr, nullptr, 0, nullptr, false)); |
| 89 | REPORTER_ASSERT(r, !effect->makeColorFilter(nullptr)); |
| 90 | }; |
| 91 | |
| 92 | // Runtime effects that use sample coords or sk_FragCoord are valid shaders, |
| 93 | // but not valid color filters |
| 94 | test("void main(float2 p, inout half4 color) { color.rg = half2(p); }"); |
| 95 | test("void main(float2 p, inout half4 color) { color.rg = half2(sk_FragCoord.xy); }"); |
| 96 | } |
| 97 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 98 | class TestEffect { |
| 99 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 100 | TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 101 | : fReporter(r), fSurface(std::move(surface)) {} |
| 102 | |
| 103 | void build(const char* header, const char* body) { |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 104 | SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }", |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 105 | header, body); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 106 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
| 107 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 108 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 109 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 110 | return; |
| 111 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 112 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 115 | SkRuntimeShaderBuilder::BuilderInput input(const char* name) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 116 | return fBuilder->input(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 117 | } |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 118 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 119 | return fBuilder->child(name); |
| 120 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 121 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 122 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 123 | |
| 124 | void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR, |
| 125 | PreTestFn preTestCallback = nullptr) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 126 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 127 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 128 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 132 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 133 | SkPaint paint; |
| 134 | paint.setShader(std::move(shader)); |
| 135 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 136 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 137 | canvas->save(); |
| 138 | if (preTestCallback) { |
| 139 | preTestCallback(canvas, &paint); |
| 140 | } |
| 141 | canvas->drawPaint(paint); |
| 142 | canvas->restore(); |
| 143 | |
| 144 | GrColor actual[4]; |
| 145 | SkImageInfo info = fSurface->imageInfo(); |
| 146 | if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 147 | REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 151 | GrColor expected[4] = {TL, TR, BL, BR}; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 152 | if (memcmp(actual, expected, sizeof(actual)) != 0) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 153 | REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 154 | SkStringPrintf("\n" |
| 155 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 156 | "Got : [ %08x %08x %08x %08x ]\n" |
| 157 | "SkSL:\n%s\n", |
| 158 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 159 | actual[3], fBuilder->fEffect->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 163 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 164 | this->test(expected, expected, expected, expected, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 168 | skiatest::Reporter* fReporter; |
| 169 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 170 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 171 | }; |
| 172 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 173 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 174 | // [ Red, Green ] |
| 175 | // [ Blue, White ] |
| 176 | static sk_sp<SkShader> make_RGBW_shader() { |
| 177 | SkBitmap bmp; |
| 178 | bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 179 | SkIRect topLeft = SkIRect::MakeWH(1, 1); |
| 180 | bmp.pixmap().erase(SK_ColorRED, topLeft); |
| 181 | bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0)); |
| 182 | bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1)); |
| 183 | bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1)); |
| 184 | return bmp.makeShader(); |
| 185 | } |
| 186 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 187 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 188 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 189 | sk_sp<SkSurface> surface = rContext |
| 190 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 191 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 192 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 193 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 194 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 195 | using float4 = std::array<float, 4>; |
| 196 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 197 | // Local coords |
| 198 | effect.build("", "color = half4(half2(p - 0.5), 0, 1);"); |
| 199 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 200 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 201 | // Use of a simple uniform. (Draw twice with two values to ensure it's updated). |
| 202 | effect.build("uniform float4 gColor;", |
| 203 | "color = half4(gColor);"); |
| 204 | effect.input("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
| 205 | effect.test(0xFFBF4000); |
| 206 | effect.input("gColor") = float4{ 0.75f, 0.25f, 0.0f, 1.0f }; |
| 207 | effect.test(0xFF0040BF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 208 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 209 | // Indexing a uniform array with an 'in' integer |
| 210 | effect.build("in int flag; uniform half4 gColors[2];", |
| 211 | "color = gColors[flag];"); |
| 212 | effect.input("gColors") = std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f}, |
| 213 | float4{0.0f, 1.0f, 0.0f, 1.0f }}; |
| 214 | effect.input("flag") = 0; |
| 215 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
| 216 | effect.input("flag") = 1; |
| 217 | effect.test(0xFF00FF00); |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 218 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 219 | // 'in' half (functionally a uniform, but handled very differently internally) |
| 220 | effect.build("in half c;", |
| 221 | "color = half4(c, c, c, 1);"); |
| 222 | effect.input("c") = 0.498f; |
| 223 | effect.test(0xFF7F7F7F); |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 224 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 225 | // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords. |
| 226 | // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to |
| 227 | // make sure we're not saturating unexpectedly. |
| 228 | effect.build("", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);"); |
| 229 | effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, |
| 230 | [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); }); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 231 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 232 | // |
| 233 | // Sampling children |
| 234 | // |
| 235 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 236 | // Sampling a null child should return the paint color |
| 237 | effect.build("in shader child;", |
| 238 | "color = sample(child);"); |
| 239 | effect.child("child") = nullptr; |
| 240 | effect.test(0xFF00FFFF, |
| 241 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 242 | |
| 243 | sk_sp<SkShader> rgbwShader = make_RGBW_shader(); |
| 244 | |
| 245 | // Sampling a simple child at our coordinates (implicitly) |
| 246 | effect.build("in shader child;", |
| 247 | "color = sample(child);"); |
| 248 | effect.child("child") = rgbwShader; |
| 249 | effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF); |
| 250 | |
| 251 | // Sampling with explicit coordinates (reflecting about the diagonal) |
| 252 | effect.build("in shader child;", |
| 253 | "color = sample(child, p.yx);"); |
| 254 | effect.child("child") = rgbwShader; |
| 255 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
| 256 | |
| 257 | // Sampling with a matrix (again, reflecting about the diagonal) |
| 258 | effect.build("in shader child;", |
| 259 | "color = sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1));"); |
| 260 | effect.child("child") = rgbwShader; |
| 261 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 262 | |
| 263 | // |
| 264 | // Helper functions |
| 265 | // |
| 266 | |
| 267 | // Test case for inlining in the pipeline-stage and fragment-shader passes (skbug.com/10526): |
| 268 | effect.build("float2 helper(float2 x) { return x + 1; }", |
| 269 | "float2 v = helper(p);" |
| 270 | "color = half4(half2(v), 0, 1);"); |
| 271 | effect.test(0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 275 | test_RuntimeEffect_Shaders(r, nullptr); |
| 276 | } |
| 277 | |
| 278 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 279 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 280 | } |