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" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkSurface.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 12 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 14 | #include "src/core/SkTLazy.h" |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrColor.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 16 | #include "tests/Test.h" |
| 17 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 18 | #include <algorithm> |
| 19 | |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 20 | DEF_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 Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 24 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 25 | 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 Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 33 | 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 Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 38 | |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 39 | // 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] | 40 | test("uniform sampler2D s;", "", "sampler2D"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 41 | |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 42 | // 'in' variables can't be arrays |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 43 | test("in int Input[2];", "", "array"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 44 | |
| 45 | // Type specific restrictions: |
| 46 | |
| 47 | // 'bool', 'int' can't be 'uniform' |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 48 | test("uniform bool Input;", "", "'uniform'"); |
| 49 | test("uniform int Input;", "", "'uniform'"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 50 | |
| 51 | // vector and matrix types can't be 'in' |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 52 | test("in float2 Input;", "", "'in'"); |
| 53 | test("in half3x3 Input;", "", "'in'"); |
| 54 | |
Brian Osman | e64ae86 | 2020-07-16 15:29:15 -0400 | [diff] [blame] | 55 | // '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 Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 60 | test("half missing();", "color.r = missing();", "undefined function"); |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 61 | |
| 62 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
| 63 | test("//", "", "main"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 64 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 65 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 66 | class TestEffect { |
| 67 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 68 | 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 Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 72 | SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }", |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 73 | header, body); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 74 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
| 75 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 76 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 77 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 78 | return; |
| 79 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 80 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 83 | SkRuntimeShaderBuilder::BuilderInput input(const char* name) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 84 | return fBuilder->input(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 85 | } |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 86 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 87 | return fBuilder->child(name); |
| 88 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 89 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 90 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 91 | |
| 92 | void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR, |
| 93 | PreTestFn preTestCallback = nullptr) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 94 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 95 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 96 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 100 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 101 | SkPaint paint; |
| 102 | paint.setShader(std::move(shader)); |
| 103 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 104 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 105 | 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 Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 119 | GrColor expected[4] = {TL, TR, BL, BR}; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 120 | if (memcmp(actual, expected, sizeof(actual)) != 0) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 121 | REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 122 | 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 Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 127 | actual[3], fBuilder->fEffect->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 131 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 132 | this->test(expected, expected, expected, expected, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 136 | skiatest::Reporter* fReporter; |
| 137 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 138 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 141 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 142 | // [ Red, Green ] |
| 143 | // [ Blue, White ] |
| 144 | static 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 Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 155 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) { |
| 156 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 157 | sk_sp<SkSurface> surface = context ? SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info) |
| 158 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 159 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 160 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 161 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 162 | using float4 = std::array<float, 4>; |
| 163 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 164 | // Local coords |
| 165 | effect.build("", "color = half4(half2(p - 0.5), 0, 1);"); |
| 166 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 167 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 168 | // 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 Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 175 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 176 | // 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 Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 185 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 186 | // '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 Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 191 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 192 | // 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 Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 198 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 199 | // 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 Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 228 | test_RuntimeEffect_Shaders(r, nullptr); |
| 229 | } |
| 230 | |
| 231 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 232 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 233 | } |