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 | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 11 | #include "include/core/SkData.h" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkSurface.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 14 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrDirectContext.h" |
Brian Salomon | 5392c94 | 2021-03-30 16:14:37 -0400 | [diff] [blame] | 16 | #include "src/core/SkColorSpacePriv.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 17 | #include "src/core/SkTLazy.h" |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrColor.h" |
Brian Osman | cc3d2d2 | 2021-04-16 14:53:24 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrFragmentProcessor.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 20 | #include "tests/Test.h" |
| 21 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 22 | #include <algorithm> |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 23 | #include <thread> |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 24 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 25 | void test_invalid_effect(skiatest::Reporter* r, const char* src, const char* expected) { |
| 26 | auto [effect, errorText] = SkRuntimeEffect::Make(SkString(src)); |
| 27 | REPORTER_ASSERT(r, !effect); |
| 28 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 29 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 30 | expected, errorText.c_str()); |
| 31 | }; |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 32 | |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 33 | #define EMPTY_MAIN "half4 main() { return half4(0); }" |
| 34 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 35 | DEF_TEST(SkRuntimeEffectInvalid_FPOnly, r) { |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 36 | // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked). |
| 37 | // Ensure that these fail, and the error messages contain the relevant keyword. |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 38 | test_invalid_effect(r, "layout(key) in bool Input;" EMPTY_MAIN, "key"); |
| 39 | test_invalid_effect(r, "in uniform float Input;" EMPTY_MAIN, "in uniform"); |
| 40 | test_invalid_effect(r, "layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype"); |
| 41 | test_invalid_effect(r, "in bool Flag; " |
| 42 | "layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when"); |
| 43 | test_invalid_effect(r, "layout(tracked) uniform float Input;" EMPTY_MAIN, "tracked"); |
| 44 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 45 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 46 | DEF_TEST(SkRuntimeEffectInvalid_LimitedUniformTypes, r) { |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 47 | // Runtime SkSL supports a limited set of uniform types. No bool, for example: |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 48 | test_invalid_effect(r, "uniform bool b;" EMPTY_MAIN, "uniform"); |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 49 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 50 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 51 | DEF_TEST(SkRuntimeEffectInvalid_NoInVariables, r) { |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 52 | // 'in' variables aren't allowed at all: |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 53 | test_invalid_effect(r, "in bool b;" EMPTY_MAIN, "'in'"); |
| 54 | test_invalid_effect(r, "in float f;" EMPTY_MAIN, "'in'"); |
| 55 | test_invalid_effect(r, "in float2 v;" EMPTY_MAIN, "'in'"); |
| 56 | test_invalid_effect(r, "in half3x3 m;" EMPTY_MAIN, "'in'"); |
| 57 | } |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 58 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 59 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedFunction, r) { |
| 60 | test_invalid_effect(r, "half4 missing(); half4 main() { return missing(); }", |
| 61 | "undefined function"); |
| 62 | } |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 63 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 64 | DEF_TEST(SkRuntimeEffectInvalid_UndefinedMain, r) { |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 65 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 66 | test_invalid_effect(r, "", "main"); |
| 67 | } |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 68 | |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 69 | DEF_TEST(SkRuntimeEffectInvalid_SkCapsDisallowed, r) { |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 70 | // sk_Caps is an internal system. It should not be visible to runtime effects |
John Stiles | 7ce1751 | 2021-01-12 18:39:02 -0500 | [diff] [blame] | 71 | test_invalid_effect(r, "half4 main() { return sk_Caps.integerSupport ? half4(1) : half4(0); }", |
| 72 | "unknown identifier 'sk_Caps'"); |
| 73 | } |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 74 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 75 | DEF_TEST(SkRuntimeEffectInvalidColorFilters, r) { |
| 76 | auto test = [r](const char* sksl) { |
| 77 | auto [effect, errorText] = SkRuntimeEffect::Make(SkString(sksl)); |
| 78 | REPORTER_ASSERT(r, effect); |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 79 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 80 | sk_sp<SkData> uniforms = SkData::MakeUninitialized(effect->uniformSize()); |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 81 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 82 | REPORTER_ASSERT(r, effect->makeShader(uniforms, nullptr, 0, nullptr, false)); |
| 83 | REPORTER_ASSERT(r, !effect->makeColorFilter(uniforms)); |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | // Runtime effects that use sample coords or sk_FragCoord are valid shaders, |
| 87 | // but not valid color filters |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 88 | test("half4 main(float2 p) { return half2(p).xy01; }"); |
| 89 | test("half4 main(float2 p) { return half2(sk_FragCoord.xy).xy01; }"); |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 92 | class TestEffect { |
| 93 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 94 | TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 95 | : fReporter(r), fSurface(std::move(surface)) {} |
| 96 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 97 | void build(const char* src) { |
John Stiles | 1f19ce2 | 2021-02-11 17:40:03 -0500 | [diff] [blame] | 98 | auto [effect, errorText] = SkRuntimeEffect::Make(SkString(src)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 99 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 100 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 101 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 102 | return; |
| 103 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 104 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 107 | SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) { |
| 108 | return fBuilder->uniform(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 109 | } |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 110 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 111 | return fBuilder->child(name); |
| 112 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 113 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 114 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 115 | |
| 116 | void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR, |
| 117 | PreTestFn preTestCallback = nullptr) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 118 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 119 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 120 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 124 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 125 | SkPaint paint; |
| 126 | paint.setShader(std::move(shader)); |
| 127 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 128 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 129 | canvas->save(); |
| 130 | if (preTestCallback) { |
| 131 | preTestCallback(canvas, &paint); |
| 132 | } |
| 133 | canvas->drawPaint(paint); |
| 134 | canvas->restore(); |
| 135 | |
| 136 | GrColor actual[4]; |
| 137 | SkImageInfo info = fSurface->imageInfo(); |
| 138 | if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 139 | REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 143 | GrColor expected[4] = {TL, TR, BL, BR}; |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 144 | if (0 != memcmp(actual, expected, sizeof(actual))) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 145 | REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 146 | SkStringPrintf("\n" |
| 147 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 148 | "Got : [ %08x %08x %08x %08x ]\n" |
| 149 | "SkSL:\n%s\n", |
| 150 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 151 | actual[3], fBuilder->effect()->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 155 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 156 | this->test(expected, expected, expected, expected, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 160 | skiatest::Reporter* fReporter; |
| 161 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 162 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 163 | }; |
| 164 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 165 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 166 | // [ Red, Green ] |
| 167 | // [ Blue, White ] |
| 168 | static sk_sp<SkShader> make_RGBW_shader() { |
| 169 | SkBitmap bmp; |
| 170 | bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 171 | SkIRect topLeft = SkIRect::MakeWH(1, 1); |
| 172 | bmp.pixmap().erase(SK_ColorRED, topLeft); |
| 173 | bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0)); |
| 174 | bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1)); |
| 175 | bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1)); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 176 | return bmp.makeShader(SkSamplingOptions()); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 179 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 180 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 181 | sk_sp<SkSurface> surface = rContext |
| 182 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 183 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 184 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 185 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 186 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 187 | using float4 = std::array<float, 4>; |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 188 | using int4 = std::array<int, 4>; |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 189 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 190 | // Local coords |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 191 | effect.build("half4 main(float2 p) { return half4(half2(p - 0.5), 0, 1); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 192 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 193 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 194 | // Use of a simple uniform. (Draw twice with two values to ensure it's updated). |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 195 | effect.build("uniform float4 gColor; half4 main() { return half4(gColor); }"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 196 | effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 197 | effect.test(0xFFBF4000); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 198 | effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 199 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 200 | |
Brian Osman | d18967c | 2021-04-01 09:56:07 -0400 | [diff] [blame] | 201 | // Same, with integer uniforms |
| 202 | effect.build("uniform int4 gColor; half4 main() { return half4(gColor) / 255.0; }"); |
| 203 | effect.uniform("gColor") = int4{ 0x00, 0x40, 0xBF, 0xFF }; |
| 204 | effect.test(0xFFBF4000); |
| 205 | effect.uniform("gColor") = int4{ 0xFF, 0x00, 0x00, 0x7F }; |
| 206 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
| 207 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 208 | // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords. |
| 209 | // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to |
| 210 | // make sure we're not saturating unexpectedly. |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 211 | effect.build("half4 main() { return half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 212 | effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, |
| 213 | [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); }); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 214 | |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 215 | // Runtime effects should use relaxed precision rules by default |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 216 | effect.build("half4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 217 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 218 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 219 | // ... and support *returning* float4 (aka vec4), not just half4 |
| 220 | effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
| 221 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 222 | effect.build("vec4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 223 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 224 | |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 225 | // Mutating coords should work. (skbug.com/10918) |
| 226 | effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }"); |
| 227 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 228 | effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }" |
| 229 | "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }"); |
| 230 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 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 |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 237 | effect.build("uniform shader child;" |
| 238 | "half4 main() { return sample(child); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 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) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 246 | effect.build("uniform shader child;" |
| 247 | "half4 main() { return sample(child); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 248 | effect.child("child") = rgbwShader; |
| 249 | effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF); |
| 250 | |
| 251 | // Sampling with explicit coordinates (reflecting about the diagonal) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 252 | effect.build("uniform shader child;" |
| 253 | "half4 main(float2 p) { return sample(child, p.yx); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 254 | effect.child("child") = rgbwShader; |
| 255 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
| 256 | |
| 257 | // Sampling with a matrix (again, reflecting about the diagonal) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 258 | effect.build("uniform shader child;" |
| 259 | "half4 main() { return sample(child, float3x3(0, 1, 0, 1, 0, 0, 0, 0, 1)); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 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): |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 268 | effect.build("float2 helper(float2 x) { return x + 1; }" |
| 269 | "half4 main(float2 p) { float2 v = helper(p); return half4(half2(v), 0, 1); }"); |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 270 | effect.test(0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 274 | test_RuntimeEffect_Shaders(r, nullptr); |
| 275 | } |
| 276 | |
| 277 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 278 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 279 | } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 280 | |
| 281 | DEF_TEST(SkRuntimeShaderBuilderReuse, r) { |
| 282 | const char* kSource = R"( |
| 283 | uniform half x; |
| 284 | half4 main() { return half4(x); } |
| 285 | )"; |
| 286 | |
John Stiles | 1f19ce2 | 2021-02-11 17:40:03 -0500 | [diff] [blame] | 287 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::Make(SkString(kSource)).effect; |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 288 | REPORTER_ASSERT(r, effect); |
| 289 | |
| 290 | // Test passes if this sequence doesn't assert. skbug.com/10667 |
| 291 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 292 | b.uniform("x") = 0.0f; |
| 293 | auto shader_0 = b.makeShader(nullptr, false); |
| 294 | |
| 295 | b.uniform("x") = 1.0f; |
| 296 | auto shader_1 = b.makeShader(nullptr, true); |
| 297 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 298 | |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 299 | DEF_TEST(SkRuntimeShaderBuilderSetUniforms, r) { |
| 300 | const char* kSource = R"( |
| 301 | uniform half x; |
| 302 | uniform vec2 offset; |
| 303 | half4 main() { return half4(x); } |
| 304 | )"; |
| 305 | |
John Stiles | 1f19ce2 | 2021-02-11 17:40:03 -0500 | [diff] [blame] | 306 | sk_sp<SkRuntimeEffect> effect = SkRuntimeEffect::Make(SkString(kSource)).effect; |
Derek Sollenberger | 9e1cedd | 2021-01-14 08:30:52 -0500 | [diff] [blame] | 307 | REPORTER_ASSERT(r, effect); |
| 308 | |
| 309 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 310 | |
| 311 | // Test passes if this sequence doesn't assert. |
| 312 | float x = 1.0f; |
| 313 | REPORTER_ASSERT(r, b.uniform("x").set(&x, 1)); |
| 314 | |
| 315 | // add extra value to ensure that set doesn't try to use sizeof(array) |
| 316 | float origin[] = { 2.0f, 3.0f, 4.0f }; |
| 317 | REPORTER_ASSERT(r, b.uniform("offset").set<float>(origin, 2)); |
| 318 | |
| 319 | #ifndef SK_DEBUG |
| 320 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 1)); |
| 321 | REPORTER_ASSERT(r, !b.uniform("offset").set<float>(origin, 3)); |
| 322 | #endif |
| 323 | |
| 324 | |
| 325 | auto shader = b.makeShader(nullptr, false); |
| 326 | } |
| 327 | |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 328 | DEF_TEST(SkRuntimeEffectThreaded, r) { |
| 329 | // SkRuntimeEffect uses a single compiler instance, but it's mutex locked. |
| 330 | // This tests that we can safely use it from more than one thread, and also |
| 331 | // that programs don't refer to shared structures owned by the compiler. |
| 332 | // skbug.com/10589 |
| 333 | static constexpr char kSource[] = "half4 main() { return sk_FragCoord.xyxy; }"; |
| 334 | |
| 335 | std::thread threads[16]; |
| 336 | for (auto& thread : threads) { |
| 337 | thread = std::thread([r]() { |
| 338 | auto [effect, error] = SkRuntimeEffect::Make(SkString(kSource)); |
| 339 | REPORTER_ASSERT(r, effect); |
| 340 | }); |
| 341 | } |
| 342 | |
| 343 | for (auto& thread : threads) { |
| 344 | thread.join(); |
| 345 | } |
| 346 | } |
Mike Klein | 827f8c0 | 2021-02-06 09:13:01 -0600 | [diff] [blame] | 347 | |
| 348 | DEF_TEST(SkRuntimeColorFilterSingleColor, r) { |
| 349 | // Test runtime colorfilters support filterColor4f(). |
| 350 | auto [effect, err] = SkRuntimeEffect::Make(SkString{ |
| 351 | "uniform shader input; half4 main() { half4 c = sample(input); return c*c; }"}); |
| 352 | REPORTER_ASSERT(r, effect); |
| 353 | REPORTER_ASSERT(r, err.isEmpty()); |
| 354 | |
| 355 | sk_sp<SkColorFilter> input = nullptr; |
| 356 | sk_sp<SkColorFilter> cf = effect->makeColorFilter(SkData::MakeEmpty(), &input, 1); |
| 357 | REPORTER_ASSERT(r, cf); |
| 358 | |
| 359 | SkColor4f c = cf->filterColor4f({0.25, 0.5, 0.75, 1.0}, |
| 360 | sk_srgb_singleton(), sk_srgb_singleton()); |
| 361 | REPORTER_ASSERT(r, c.fR == 0.0625f); |
| 362 | REPORTER_ASSERT(r, c.fG == 0.25f); |
| 363 | REPORTER_ASSERT(r, c.fB == 0.5625f); |
| 364 | REPORTER_ASSERT(r, c.fA == 1.0f); |
| 365 | } |
Brian Osman | 8e756f3 | 2021-02-10 10:19:27 -0500 | [diff] [blame] | 366 | |
| 367 | static void test_RuntimeEffectStructNameReuse(skiatest::Reporter* r, GrRecordingContext* rContext) { |
| 368 | // Test that two different runtime effects can reuse struct names in a single paint operation |
| 369 | auto [childEffect, err] = SkRuntimeEffect::Make(SkString( |
| 370 | "uniform shader paint;" |
| 371 | "struct S { half4 rgba; };" |
| 372 | "void process(inout S s) { s.rgba.rgb *= 0.5; }" |
| 373 | "half4 main() { S s; s.rgba = sample(paint); process(s); return s.rgba; }" |
| 374 | )); |
| 375 | REPORTER_ASSERT(r, childEffect, "%s\n", err.c_str()); |
| 376 | sk_sp<SkShader> nullChild = nullptr; |
| 377 | sk_sp<SkShader> child = childEffect->makeShader(/*uniforms=*/nullptr, &nullChild, |
| 378 | /*childCount=*/1, /*localMatrix=*/nullptr, |
| 379 | /*isOpaque=*/false); |
| 380 | |
| 381 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 382 | sk_sp<SkSurface> surface = rContext |
| 383 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 384 | : SkSurface::MakeRaster(info); |
| 385 | REPORTER_ASSERT(r, surface); |
| 386 | |
| 387 | TestEffect effect(r, surface); |
| 388 | effect.build( |
| 389 | "uniform shader child;" |
| 390 | "struct S { float2 coord; };" |
| 391 | "void process(inout S s) { s.coord = s.coord.yx; }" |
| 392 | "half4 main(float2 p) { S s; s.coord = p; process(s); return sample(child, s.coord); " |
| 393 | "}"); |
| 394 | effect.child("child") = child; |
| 395 | effect.test(0xFF00407F, [](SkCanvas*, SkPaint* paint) { |
| 396 | paint->setColor4f({0.99608f, 0.50196f, 0.0f, 1.0f}); |
| 397 | }); |
| 398 | } |
| 399 | |
| 400 | DEF_TEST(SkRuntimeStructNameReuse, r) { |
| 401 | test_RuntimeEffectStructNameReuse(r, nullptr); |
| 402 | } |
| 403 | |
| 404 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeStructNameReuse_GPU, r, ctxInfo) { |
| 405 | test_RuntimeEffectStructNameReuse(r, ctxInfo.directContext()); |
| 406 | } |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 407 | |
| 408 | DEF_TEST(SkRuntimeColorFilterFlags, r) { |
| 409 | { // Here's a non-trivial filter that doesn't change alpha. |
| 410 | auto [effect, err] = SkRuntimeEffect::Make(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 411 | "half4 main(half4 color) { return color + half4(1,1,1,0); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 412 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 413 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 414 | REPORTER_ASSERT(r, filter && filter->isAlphaUnchanged()); |
| 415 | } |
| 416 | |
| 417 | { // Here's one that definitely changes alpha. |
| 418 | auto [effect, err] = SkRuntimeEffect::Make(SkString{ |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 419 | "half4 main(half4 color) { return color + half4(0,0,0,4); }"}); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 420 | REPORTER_ASSERT(r, effect && err.isEmpty()); |
Brian Osman | cdee120 | 2021-04-14 09:36:49 -0400 | [diff] [blame] | 421 | sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty()); |
Mike Klein | e0d9b86 | 2021-02-16 12:00:29 -0600 | [diff] [blame] | 422 | REPORTER_ASSERT(r, filter && !filter->isAlphaUnchanged()); |
| 423 | } |
| 424 | } |
Brian Osman | cc3d2d2 | 2021-04-16 14:53:24 -0400 | [diff] [blame] | 425 | |
| 426 | DEF_TEST(SkRuntimeShaderSampleUsage, r) { |
| 427 | auto test = [&](const char* src, bool expectExplicit) { |
| 428 | auto [effect, err] = |
| 429 | SkRuntimeEffect::MakeForShader(SkStringPrintf("uniform shader child; %s", src)); |
| 430 | REPORTER_ASSERT(r, effect); |
| 431 | |
| 432 | auto child = GrFragmentProcessor::MakeColor({ 1, 1, 1, 1 }); |
| 433 | auto fp = effect->makeFP(nullptr, &child, 1); |
| 434 | REPORTER_ASSERT(r, fp); |
| 435 | |
| 436 | REPORTER_ASSERT(r, fp->childProcessor(0)->isSampledWithExplicitCoords() == expectExplicit); |
| 437 | }; |
| 438 | |
| 439 | // This test verifies that we detect calls to sample where the coords are the same as those |
| 440 | // passed to main. In those cases, it's safe to turn the "explicit" sampling into "passthrough" |
| 441 | // sampling. This optimization is implemented very conservatively. |
| 442 | |
| 443 | // Cases where our optimization is valid, and works: |
| 444 | |
| 445 | // Direct use of passed-in coords |
| 446 | test("half4 main(float2 xy) { return sample(child, xy); }", false); |
| 447 | // Sample with passed-in coords, read (but don't write) sample coords elsewhere |
| 448 | test("half4 main(float2 xy) { return sample(child, xy) + sin(xy.x); }", false); |
| 449 | |
| 450 | // Cases where our optimization is not valid, and does not happen: |
| 451 | |
| 452 | // Sampling with values completely unrelated to passed-in coords |
| 453 | test("half4 main(float2 xy) { return sample(child, float2(0, 0)); }", true); |
| 454 | // Use of expression involving passed in coords |
| 455 | test("half4 main(float2 xy) { return sample(child, xy * 0.5); }", true); |
| 456 | // Use of coords after modification |
| 457 | test("half4 main(float2 xy) { xy *= 2; return sample(child, xy); }", true); |
| 458 | // Use of coords after modification via out-param call |
| 459 | test("void adjust(inout float2 xy) { xy *= 2; }" |
| 460 | "half4 main(float2 xy) { adjust(xy); return sample(child, xy); }", true); |
| 461 | |
| 462 | // There should (must) not be any false-positive cases. There are false-negatives. |
| 463 | // In all of these cases, our optimization would be valid, but does not happen: |
| 464 | |
| 465 | // Direct use of passed-in coords, modified after use |
| 466 | test("half4 main(float2 xy) { half4 c = sample(child, xy); xy *= 2; return c; }", true); |
| 467 | // Passed-in coords copied to a temp variable |
| 468 | test("half4 main(float2 xy) { float2 p = xy; return sample(child, p); }", true); |
| 469 | // Use of coords passed to helper function |
| 470 | test("half4 helper(float2 xy) { return sample(child, xy); }" |
| 471 | "half4 main(float2 xy) { return helper(xy); }", true); |
| 472 | } |