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 | |
| 53 | test("half missing();", "color.r = missing();", "undefined function"); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 54 | } |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 55 | |
| 56 | // Our packing rules and unit test code here relies on this: |
| 57 | static_assert(sizeof(bool) == 1); |
| 58 | |
| 59 | class TestEffect { |
| 60 | public: |
| 61 | TestEffect(skiatest::Reporter* r, const char* hdr, const char* body) { |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 62 | SkString src = SkStringPrintf("%s void main(float2 p, inout half4 color) { %s }", |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 63 | hdr, body); |
| 64 | auto[effect, errorText] = SkRuntimeEffect::Make(src); |
| 65 | if (!effect) { |
| 66 | REPORT_FAILURE(r, "effect", |
| 67 | SkStringPrintf("Effect didn't compile: %s", errorText.c_str())); |
| 68 | return; |
| 69 | } |
| 70 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 71 | fBuilder.init(std::move(effect)); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 74 | SkRuntimeShaderBuilder::BuilderInput operator[](const char* name) { |
| 75 | return fBuilder->input(name); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, |
Brian Osman | 5aaaeea | 2020-06-22 14:26:03 -0400 | [diff] [blame] | 79 | 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] | 80 | auto shader = fBuilder->makeShader(nullptr, false); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 81 | if (!shader) { |
| 82 | REPORT_FAILURE(r, "shader", SkString("Effect didn't produce a shader")); |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | SkPaint paint; |
| 87 | paint.setShader(std::move(shader)); |
| 88 | paint.setBlendMode(SkBlendMode::kSrc); |
Brian Osman | 5aaaeea | 2020-06-22 14:26:03 -0400 | [diff] [blame] | 89 | surface->getCanvas()->rotate(rotate); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 90 | surface->getCanvas()->drawPaint(paint); |
| 91 | |
| 92 | uint32_t actual[4]; |
| 93 | SkImageInfo info = surface->imageInfo(); |
| 94 | if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 95 | REPORT_FAILURE(r, "readPixels", SkString("readPixels failed")); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | uint32_t expected[4] = {TL, TR, BL, BR}; |
| 100 | if (memcmp(actual, expected, sizeof(actual)) != 0) { |
| 101 | REPORT_FAILURE(r, "Runtime effect didn't match expectations", |
| 102 | SkStringPrintf("\n" |
| 103 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 104 | "Got : [ %08x %08x %08x %08x ]\n" |
| 105 | "SkSL:\n%s\n", |
| 106 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 107 | actual[3], fBuilder->fEffect->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) { |
| 112 | this->test(r, surface, expected, expected, expected, expected); |
| 113 | } |
| 114 | |
| 115 | private: |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 116 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) { |
| 120 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 121 | sk_sp<SkSurface> surface; |
| 122 | if (context) { |
| 123 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 124 | } else { |
| 125 | surface = SkSurface::MakeRaster(info); |
| 126 | } |
| 127 | REPORTER_ASSERT(r, surface); |
| 128 | |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 129 | TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);"); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 130 | xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 131 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 132 | using float4 = std::array<float, 4>; |
| 133 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 134 | // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping |
| 135 | TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);"); |
| 136 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 137 | uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 138 | uniformColor.test(r, surface, 0xFFBF4000); |
| 139 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 140 | uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f }; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 141 | uniformColor.test(r, surface, 0xFF0040BF); |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 142 | |
| 143 | TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];"); |
| 144 | pickColor["gColors"] = |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 145 | 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] | 146 | pickColor["flag"] = 0; |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 147 | pickColor.test(r, surface, 0x7F00007F); // Tests that we clamp to valid premul |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 148 | pickColor["flag"] = 1; |
| 149 | pickColor.test(r, surface, 0xFF00FF00); |
Michael Ludwig | 5e6b3cd | 2020-05-27 17:02:37 -0400 | [diff] [blame] | 150 | |
| 151 | TestEffect inlineColor(r, "in half c;", "color = half4(c, c, c, 1);"); |
| 152 | inlineColor["c"] = 0.498f; |
| 153 | inlineColor.test(r, surface, 0xFF7F7F7F); |
Michael Ludwig | 22534f2 | 2020-05-27 17:25:33 -0400 | [diff] [blame] | 154 | |
| 155 | // Test sk_FragCoord, which we output to color. Since the surface is 2x2, we should see |
| 156 | // (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] | 157 | TestEffect fragCoord(r, "", "color = half4(0.498 * (half2(sk_FragCoord.xy) - 0.5), 0, 1);"); |
| 158 | fragCoord.test(r, surface, 0xFF000000, 0xFF00007F, 0xFF007F00, 0xFF007F7F, 45.0f); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 162 | test_RuntimeEffect_Shaders(r, nullptr); |
| 163 | } |
| 164 | |
| 165 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 166 | test_RuntimeEffect_Shaders(r, ctxInfo.directContext()); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 167 | } |