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