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