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 | |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 23 | DEF_TEST(SkRuntimeEffectInvalid, r) { |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 24 | auto test = [r](const char* src, const char* expected) { |
| 25 | auto[effect, errorText] = SkRuntimeEffect::Make(SkString(src)); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 26 | REPORTER_ASSERT(r, !effect); |
| 27 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 28 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 29 | expected, errorText.c_str()); |
| 30 | }; |
| 31 | |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 32 | #define EMPTY_MAIN "half4 main() { return half4(0); }" |
| 33 | |
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. |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 36 | test("layout(key) in bool Input;" EMPTY_MAIN, "key"); |
| 37 | test("in uniform float Input;" EMPTY_MAIN, "in uniform"); |
| 38 | test("layout(ctype=SkRect) float4 Input;" EMPTY_MAIN, "ctype"); |
| 39 | test("in bool Flag; layout(when=Flag) uniform float Input;" EMPTY_MAIN, "when"); |
| 40 | test("layout(tracked) uniform float Input;" EMPTY_MAIN, "tracked"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 41 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 42 | // GLSL types like sampler2D and texture2D are not allowed anywhere: |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 43 | test("uniform sampler2D s;" EMPTY_MAIN, "no type named 'sampler2D'"); |
| 44 | test("uniform texture2D s;" EMPTY_MAIN, "no type named 'texture2D'"); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 45 | |
| 46 | // Runtime SkSL supports a limited set of uniform types. No bool, or int, for example: |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 47 | test("uniform bool b;" EMPTY_MAIN, "uniform"); |
| 48 | test("uniform int i;" EMPTY_MAIN, "uniform"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 49 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 50 | // 'in' variables aren't allowed at all: |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 51 | test("in bool b;" EMPTY_MAIN, "'in'"); |
| 52 | test("in float f;" EMPTY_MAIN, "'in'"); |
| 53 | test("in float2 v;" EMPTY_MAIN, "'in'"); |
| 54 | test("in half3x3 m;" EMPTY_MAIN, "'in'"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 55 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 56 | // 'marker' is only permitted on float4x4 uniforms |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 57 | test("layout(marker=local_to_world) uniform float3x3 localToWorld;" EMPTY_MAIN, "float4x4"); |
Brian Osman | e64ae86 | 2020-07-16 15:29:15 -0400 | [diff] [blame] | 58 | |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 59 | test("half4 missing(); half4 main() { return missing(); }", "undefined function"); |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 60 | |
| 61 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 62 | test("", "main"); |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 63 | |
| 64 | // Various places that shaders (fragmentProcessors) should not be allowed |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 65 | test("half4 main() { shader child; return sample(child); }", |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 66 | "must be global"); |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 67 | test("uniform shader child; half4 helper(shader fp) { return sample(fp); }" |
| 68 | "half4 main() { return helper(child); }", |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 69 | "parameter"); |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 70 | test("uniform shader child; shader get_child() { return child; }" |
| 71 | "half4 main() { return sample(get_child()); }", |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 72 | "return"); |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 73 | test("uniform shader child;" |
| 74 | "half4 main() { return sample(shader(child)); }", |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 75 | "construct"); |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 76 | test("uniform shader child1; uniform shader child2;" |
| 77 | "half4 main(float2 p) { return sample(p.x > 10 ? child1 : child2); }", |
Brian Osman | 8232900 | 2020-07-21 09:39:27 -0400 | [diff] [blame] | 78 | "expression"); |
Brian Osman | e12939f | 2020-08-12 11:36:56 -0400 | [diff] [blame] | 79 | |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 80 | // sk_Caps is an internal system. It should not be visible to runtime effects |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 81 | test("half4 main() { return sk_Caps.integerSupport ? half4(1) : half4(0); }", |
| 82 | "unknown identifier 'sk_Caps'"); |
Brian Osman | b06301e | 2020-11-06 11:45:36 -0500 | [diff] [blame] | 83 | |
Brian Osman | e12939f | 2020-08-12 11:36:56 -0400 | [diff] [blame] | 84 | // Errors that aren't caught until later in the compilation process (during optimize()) |
Brian Osman | 24c1852 | 2020-11-10 16:36:01 -0500 | [diff] [blame] | 85 | test("half4 main() { return half4(1); return half4(0); }", "unreachable"); |
| 86 | test("half4 badFunc() {}" |
| 87 | "half4 main() { return badFunc(); }", |
| 88 | "without returning"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 89 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 90 | |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 91 | DEF_TEST(SkRuntimeEffectInvalidColorFilters, r) { |
| 92 | auto test = [r](const char* sksl) { |
| 93 | auto [effect, errorText] = SkRuntimeEffect::Make(SkString(sksl)); |
| 94 | REPORTER_ASSERT(r, effect); |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 95 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 96 | sk_sp<SkData> uniforms = SkData::MakeUninitialized(effect->uniformSize()); |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 97 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 98 | REPORTER_ASSERT(r, effect->makeShader(uniforms, nullptr, 0, nullptr, false)); |
| 99 | REPORTER_ASSERT(r, !effect->makeColorFilter(uniforms)); |
Brian Osman | 92aac1e | 2020-08-05 16:48:58 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | // Runtime effects that use sample coords or sk_FragCoord are valid shaders, |
| 103 | // but not valid color filters |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 104 | test("half4 main(float2 p) { return half2(p).xy01; }"); |
| 105 | test("half4 main(float2 p) { return half2(sk_FragCoord.xy).xy01; }"); |
Brian Osman | 269b21c | 2020-08-06 12:15:53 -0400 | [diff] [blame] | 106 | |
| 107 | // We also can't use layout(marker), which would give the runtime color filter CTM information |
| 108 | test("layout(marker=ctm) uniform float4x4 ctm;" |
Brian Osman | 767f444 | 2020-08-13 16:59:48 -0400 | [diff] [blame] | 109 | "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] | 110 | } |
| 111 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 112 | class TestEffect { |
| 113 | public: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 114 | TestEffect(skiatest::Reporter* r, sk_sp<SkSurface> surface) |
| 115 | : fReporter(r), fSurface(std::move(surface)) {} |
| 116 | |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 117 | void build(const char* src) { |
| 118 | auto[effect, errorText] = SkRuntimeEffect::Make(SkString(src)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 119 | if (!effect) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 120 | REPORT_FAILURE(fReporter, "effect", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 121 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 122 | return; |
| 123 | } |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 124 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 127 | SkRuntimeShaderBuilder::BuilderUniform uniform(const char* name) { |
| 128 | return fBuilder->uniform(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 129 | } |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 130 | SkRuntimeShaderBuilder::BuilderChild child(const char* name) { |
| 131 | return fBuilder->child(name); |
| 132 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 133 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 134 | using PreTestFn = std::function<void(SkCanvas*, SkPaint*)>; |
| 135 | |
| 136 | void test(GrColor TL, GrColor TR, GrColor BL, GrColor BR, |
| 137 | PreTestFn preTestCallback = nullptr) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 138 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 139 | if (!shader) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 140 | REPORT_FAILURE(fReporter, "shader", SkString("Effect didn't produce a shader")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 144 | SkCanvas* canvas = fSurface->getCanvas(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 145 | SkPaint paint; |
| 146 | paint.setShader(std::move(shader)); |
| 147 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 148 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 149 | canvas->save(); |
| 150 | if (preTestCallback) { |
| 151 | preTestCallback(canvas, &paint); |
| 152 | } |
| 153 | canvas->drawPaint(paint); |
| 154 | canvas->restore(); |
| 155 | |
| 156 | GrColor actual[4]; |
| 157 | SkImageInfo info = fSurface->imageInfo(); |
| 158 | if (!fSurface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 159 | REPORT_FAILURE(fReporter, "readPixels", SkString("readPixels failed")); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 160 | return; |
| 161 | } |
| 162 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 163 | GrColor expected[4] = {TL, TR, BL, BR}; |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 164 | if (0 != memcmp(actual, expected, sizeof(actual))) { |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 165 | REPORT_FAILURE(fReporter, "Runtime effect didn't match expectations", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 166 | SkStringPrintf("\n" |
| 167 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 168 | "Got : [ %08x %08x %08x %08x ]\n" |
| 169 | "SkSL:\n%s\n", |
| 170 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 171 | actual[3], fBuilder->effect()->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 175 | void test(GrColor expected, PreTestFn preTestCallback = nullptr) { |
| 176 | this->test(expected, expected, expected, expected, preTestCallback); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private: |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 180 | skiatest::Reporter* fReporter; |
| 181 | sk_sp<SkSurface> fSurface; |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 182 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 183 | }; |
| 184 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 185 | // Produces a 2x2 bitmap shader, with opaque colors: |
| 186 | // [ Red, Green ] |
| 187 | // [ Blue, White ] |
| 188 | static sk_sp<SkShader> make_RGBW_shader() { |
| 189 | SkBitmap bmp; |
| 190 | bmp.allocPixels(SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType)); |
| 191 | SkIRect topLeft = SkIRect::MakeWH(1, 1); |
| 192 | bmp.pixmap().erase(SK_ColorRED, topLeft); |
| 193 | bmp.pixmap().erase(SK_ColorGREEN, topLeft.makeOffset(1, 0)); |
| 194 | bmp.pixmap().erase(SK_ColorBLUE, topLeft.makeOffset(0, 1)); |
| 195 | bmp.pixmap().erase(SK_ColorWHITE, topLeft.makeOffset(1, 1)); |
| 196 | return bmp.makeShader(); |
| 197 | } |
| 198 | |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 199 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrRecordingContext* rContext) { |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 200 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
Robert Phillips | e94b4e1 | 2020-07-23 13:54:35 -0400 | [diff] [blame] | 201 | sk_sp<SkSurface> surface = rContext |
| 202 | ? SkSurface::MakeRenderTarget(rContext, SkBudgeted::kNo, info) |
| 203 | : SkSurface::MakeRaster(info); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 204 | REPORTER_ASSERT(r, surface); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 205 | TestEffect effect(r, surface); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 206 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 207 | using float4 = std::array<float, 4>; |
| 208 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 209 | // Local coords |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 210 | 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] | 211 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 212 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 213 | // 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] | 214 | effect.build("uniform float4 gColor; half4 main() { return half4(gColor); }"); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 215 | effect.uniform("gColor") = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 216 | effect.test(0xFFBF4000); |
Brian Osman | a4b9169 | 2020-08-10 14:26:16 -0400 | [diff] [blame] | 217 | effect.uniform("gColor") = float4{ 1.0f, 0.0f, 0.0f, 0.498f }; |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 218 | effect.test(0x7F00007F); // Tests that we clamp to valid premul |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 219 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 220 | // Test sk_FragCoord (device coords). Rotate the canvas to be sure we're seeing device coords. |
| 221 | // Since the surface is 2x2, we should see (0,0), (1,0), (0,1), (1,1). Multiply by 0.498 to |
| 222 | // make sure we're not saturating unexpectedly. |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 223 | 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] | 224 | effect.test(0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, |
| 225 | [](SkCanvas* canvas, SkPaint*) { canvas->rotate(45.0f); }); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 226 | |
Brian Osman | 0acb5b5 | 2020-09-02 13:45:47 -0400 | [diff] [blame] | 227 | // Runtime effects should use relaxed precision rules by default |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 228 | 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] | 229 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 230 | |
Brian Osman | f1319c3 | 2020-10-13 09:34:23 -0400 | [diff] [blame] | 231 | // ... and support GLSL type names |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 232 | effect.build("half4 main(float2 p) { return vec4(p - 0.5, 0, 1); }"); |
| 233 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 234 | |
| 235 | // ... and support *returning* float4 (aka vec4), not just half4 |
| 236 | effect.build("float4 main(float2 p) { return float4(p - 0.5, 0, 1); }"); |
| 237 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 238 | 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] | 239 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 240 | |
Brian Osman | b4ce944 | 2020-11-11 09:18:02 -0500 | [diff] [blame] | 241 | // Mutating coords should work. (skbug.com/10918) |
| 242 | effect.build("vec4 main(vec2 p) { p -= 0.5; return vec4(p, 0, 1); }"); |
| 243 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 244 | effect.build("void moveCoords(inout vec2 p) { p -= 0.5; }" |
| 245 | "vec4 main(vec2 p) { moveCoords(p); return vec4(p, 0, 1); }"); |
| 246 | effect.test(0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 247 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 248 | // |
| 249 | // Sampling children |
| 250 | // |
| 251 | |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 252 | // Sampling a null child should return the paint color |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 253 | effect.build("uniform shader child;" |
| 254 | "half4 main() { return sample(child); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 255 | effect.child("child") = nullptr; |
| 256 | effect.test(0xFF00FFFF, |
| 257 | [](SkCanvas*, SkPaint* paint) { paint->setColor4f({1.0f, 1.0f, 0.0f, 1.0f}); }); |
| 258 | |
| 259 | sk_sp<SkShader> rgbwShader = make_RGBW_shader(); |
| 260 | |
| 261 | // Sampling a simple child at our coordinates (implicitly) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 262 | effect.build("uniform shader child;" |
| 263 | "half4 main() { return sample(child); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 264 | effect.child("child") = rgbwShader; |
| 265 | effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF); |
| 266 | |
| 267 | // Sampling with explicit coordinates (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(float2 p) { return sample(child, p.yx); }"); |
Brian Osman | 6241961 | 2020-07-22 10:19:02 -0400 | [diff] [blame] | 270 | effect.child("child") = rgbwShader; |
| 271 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
| 272 | |
| 273 | // Sampling with a matrix (again, reflecting about the diagonal) |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 274 | effect.build("uniform shader child;" |
| 275 | "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] | 276 | effect.child("child") = rgbwShader; |
| 277 | effect.test(0xFF0000FF, 0xFFFF0000, 0xFF00FF00, 0xFFFFFFFF); |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 278 | |
Brian Osman | 91292e9 | 2020-11-04 15:40:50 -0500 | [diff] [blame] | 279 | // Legacy behavior - shaders can be declared 'in' rather than 'uniform' |
Brian Osman | 3331641 | 2020-11-06 10:42:51 -0500 | [diff] [blame] | 280 | effect.build("in shader child;" |
| 281 | "half4 main() { return sample(child); }"); |
Brian Osman | 91292e9 | 2020-11-04 15:40:50 -0500 | [diff] [blame] | 282 | effect.child("child") = rgbwShader; |
| 283 | effect.test(0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF); |
| 284 | |
Brian Osman | b5f0f52 | 2020-07-23 13:28:14 -0400 | [diff] [blame] | 285 | // |
| 286 | // Helper functions |
| 287 | // |
| 288 | |
| 289 | // 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] | 290 | effect.build("float2 helper(float2 x) { return x + 1; }" |
| 291 | "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] | 292 | effect.test(0xFF00FFFF); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 296 | test_RuntimeEffect_Shaders(r, nullptr); |
| 297 | } |
| 298 | |
| 299 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 300 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 301 | } |
Brian Osman | b6bd0d2 | 2020-08-27 10:51:22 -0400 | [diff] [blame] | 302 | |
| 303 | DEF_TEST(SkRuntimeShaderBuilderReuse, r) { |
| 304 | const char* kSource = R"( |
| 305 | uniform half x; |
| 306 | half4 main() { return half4(x); } |
| 307 | )"; |
| 308 | |
| 309 | sk_sp<SkRuntimeEffect> effect = std::get<0>(SkRuntimeEffect::Make(SkString(kSource))); |
| 310 | REPORTER_ASSERT(r, effect); |
| 311 | |
| 312 | // Test passes if this sequence doesn't assert. skbug.com/10667 |
| 313 | SkRuntimeShaderBuilder b(std::move(effect)); |
| 314 | b.uniform("x") = 0.0f; |
| 315 | auto shader_0 = b.makeShader(nullptr, false); |
| 316 | |
| 317 | b.uniform("x") = 1.0f; |
| 318 | auto shader_1 = b.makeShader(nullptr, true); |
| 319 | } |
Brian Osman | 8e2ef02 | 2020-09-30 13:26:43 -0400 | [diff] [blame] | 320 | |
| 321 | DEF_TEST(SkRuntimeEffectThreaded, r) { |
| 322 | // SkRuntimeEffect uses a single compiler instance, but it's mutex locked. |
| 323 | // This tests that we can safely use it from more than one thread, and also |
| 324 | // that programs don't refer to shared structures owned by the compiler. |
| 325 | // skbug.com/10589 |
| 326 | static constexpr char kSource[] = "half4 main() { return sk_FragCoord.xyxy; }"; |
| 327 | |
| 328 | std::thread threads[16]; |
| 329 | for (auto& thread : threads) { |
| 330 | thread = std::thread([r]() { |
| 331 | auto [effect, error] = SkRuntimeEffect::Make(SkString(kSource)); |
| 332 | REPORTER_ASSERT(r, effect); |
| 333 | }); |
| 334 | } |
| 335 | |
| 336 | for (auto& thread : threads) { |
| 337 | thread.join(); |
| 338 | } |
| 339 | } |