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 | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPaint.h" |
| 10 | #include "include/core/SkSurface.h" |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 11 | #include "include/effects/SkRuntimeEffect.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 13 | #include "src/core/SkTLazy.h" |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 14 | #include "tests/Test.h" |
| 15 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 18 | DEF_TEST(SkRuntimeEffectInvalid, r) { |
| 19 | auto test = [r](const char* hdr, const char* body, const char* expected) { |
| 20 | SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }", |
| 21 | hdr, body); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 22 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 23 | REPORTER_ASSERT(r, !effect); |
| 24 | REPORTER_ASSERT(r, errorText.contains(expected), |
| 25 | "Expected error message to contain \"%s\". Actual message: \"%s\"", |
| 26 | expected, errorText.c_str()); |
| 27 | }; |
| 28 | |
| 29 | // Features that are only allowed in .fp files (key, in uniform, ctype, when, tracked). |
| 30 | // Ensure that these fail, and the error messages contain the relevant keyword. |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 31 | test("layout(key) in bool Input;", "", "key"); |
| 32 | test("in uniform float Input;", "", "in uniform"); |
| 33 | test("layout(ctype=SkRect) float4 Input;", "", "ctype"); |
| 34 | test("in bool Flag; layout(when=Flag) uniform float Input;", "", "when"); |
| 35 | test("layout(tracked) uniform float Input;", "", "tracked"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 36 | |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 37 | // Runtime SkSL supports a limited set of uniform types. No samplers, for example: |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 38 | test("uniform sampler2D s;", "", "sampler2D"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 39 | |
Brian Osman | 82d4970 | 2019-12-30 13:44:01 -0500 | [diff] [blame] | 40 | // 'in' variables can't be arrays |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 41 | test("in int Input[2];", "", "array"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 42 | |
| 43 | // Type specific restrictions: |
| 44 | |
| 45 | // 'bool', 'int' can't be 'uniform' |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 46 | test("uniform bool Input;", "", "'uniform'"); |
| 47 | test("uniform int Input;", "", "'uniform'"); |
Brian Osman | 8783b78 | 2020-01-06 11:13:45 -0500 | [diff] [blame] | 48 | |
| 49 | // vector and matrix types can't be 'in' |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 50 | test("in float2 Input;", "", "'in'"); |
| 51 | test("in half3x3 Input;", "", "'in'"); |
| 52 | |
Brian Osman | e64ae86 | 2020-07-16 15:29:15 -0400 | [diff] [blame] | 53 | // 'marker' is only permitted on 'uniform' variables |
| 54 | test("layout(marker=local_to_world) in float4x4 localToWorld;", "", "'uniform'"); |
| 55 | // 'marker' is only permitted on float4x4 variables |
| 56 | test("layout(marker=local_to_world) uniform float3x3 localToWorld;", "", "float4x4"); |
| 57 | |
Brian Osman | 5f6b41e | 2020-03-09 11:53:24 -0400 | [diff] [blame] | 58 | test("half missing();", "color.r = missing();", "undefined function"); |
Brian Osman | 182c92e | 2020-07-20 15:18:33 -0400 | [diff] [blame] | 59 | |
| 60 | // Shouldn't be possible to create an SkRuntimeEffect without "main" |
| 61 | test("//", "", "main"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 62 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 63 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 64 | class TestEffect { |
| 65 | public: |
| 66 | TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) { |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 67 | SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 68 | hdr, body); |
| 69 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
| 70 | if (!effect) { |
| 71 | REPORT_FAILURE(r, "effect", |
| 72 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 73 | return; |
| 74 | } |
| 75 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 76 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 79 | SkRuntimeShaderBuilder::BuilderInput operator[](const char* name) { |
| 80 | return fBuilder->input(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, |
Brian Osman | 5aaaeea | 2020-06-22 14:26:03 -0400 | [diff] [blame] | 84 | uint32_t TL, uint32_t TR, uint32_t BL, uint32_t BR, SkScalar rotate = 0.0f) { |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 85 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 86 | if (!shader) { |
| 87 | REPORT_FAILURE(r, "shader", SkString("Effect didn't produce a shader")); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | SkPaint paint; |
| 92 | paint.setShader(std::move(shader)); |
| 93 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | 5aaaeea | 2020-06-22 14:26:03 -0400 | [diff] [blame] | 94 | surface->getCanvas()->rotate(rotate); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 95 | surface->getCanvas()->drawPaint(paint); |
| 96 | |
| 97 | uint32_t actual[4]; |
| 98 | SkImageInfo info = surface->imageInfo(); |
| 99 | if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 100 | REPORT_FAILURE(r, "readPixels", SkString("readPixels failed")); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | uint32_t expected[4] = {TL, TR, BL, BR}; |
| 105 | if (memcmp(actual, expected, sizeof(actual)) != 0) { |
| 106 | REPORT_FAILURE(r, "Runtime effect didn't match expectations", |
| 107 | SkStringPrintf("\n" |
| 108 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 109 | "Got : [ %08x %08x %08x %08x ]\n" |
| 110 | "SkSL:\n%s\n", |
| 111 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 112 | actual[3], fBuilder->fEffect->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) { |
| 117 | this->test(r, surface, expected, expected, expected, expected); |
| 118 | } |
| 119 | |
| 120 | private: |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 121 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) { |
| 125 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 126 | sk_sp<SkSurface> surface; |
| 127 | if (context) { |
| 128 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 129 | } else { |
| 130 | surface = SkSurface::MakeRaster(info); |
| 131 | } |
| 132 | REPORTER_ASSERT(r, surface); |
| 133 | |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 134 | TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);"); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 135 | xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 136 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 137 | using float4 = std::array<float, 4>; |
| 138 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 139 | // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping |
| 140 | TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);"); |
| 141 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 142 | uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 143 | uniformColor.test(r, surface, 0xFFBF4000); |
| 144 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 145 | uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f }; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 146 | uniformColor.test(r, surface, 0xFF0040BF); |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 147 | |
| 148 | TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];"); |
| 149 | pickColor["gColors"] = |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 150 | std::array<float4, 2>{float4{1.0f, 0.0f, 0.0f, 0.498f}, float4{0.0f, 1.0f, 0.0f, 1.0f}}; |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 151 | pickColor["flag"] = 0; |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 152 | pickColor.test(r, surface, 0x7F00007F); // Tests that we clamp to valid premul |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 153 | pickColor["flag"] = 1; |
| 154 | pickColor.test(r, surface, 0xFF00FF00); |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 155 | |
| 156 | TestEffect inlineColor(r, "in half c;", "color = half4(c, c, c, 1);"); |
| 157 | inlineColor["c"] = 0.498f; |
| 158 | inlineColor.test(r, surface, 0xFF7F7F7F); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 159 | |
| 160 | // Test sk_FragCoord, which we output to color. Since the surface is 2x2, we should see |
| 161 | // (0,0), (1,0), (0,1), (1,1), multiply by 0.498 to make sure we're not saturating unexpectedly. |
Brian Osman | 5aaaeea | 2020-06-22 14:26:03 -0400 | [diff] [blame] | 162 | TestEffect fragCoord(r, "", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);"); |
| 163 | fragCoord.test(r, surface, 0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, 45.0f); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 167 | test_RuntimeEffect_Shaders(r, nullptr); |
| 168 | } |
| 169 | |
| 170 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 171 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 172 | } |