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" |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 12 | #include "include/gpu/GrContext.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, |
| 79 | uint32_t TL, uint32_t TR, uint32_t BL, uint32_t BR) { |
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); |
| 89 | surface->getCanvas()->drawPaint(paint); |
| 90 | |
| 91 | uint32_t actual[4]; |
| 92 | SkImageInfo info = surface->imageInfo(); |
| 93 | if (!surface->readPixels(info, actual, info.minRowBytes(), 0, 0)) { |
| 94 | REPORT_FAILURE(r, "readPixels", SkString("readPixels failed")); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | uint32_t expected[4] = {TL, TR, BL, BR}; |
| 99 | if (memcmp(actual, expected, sizeof(actual)) != 0) { |
| 100 | REPORT_FAILURE(r, "Runtime effect didn't match expectations", |
| 101 | SkStringPrintf("\n" |
| 102 | "Expected: [ %08x %08x %08x %08x ]\n" |
| 103 | "Got : [ %08x %08x %08x %08x ]\n" |
| 104 | "SkSL:\n%s\n", |
| 105 | TL, TR, BL, BR, actual[0], actual[1], actual[2], |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 106 | actual[3], fBuilder->fEffect->source().c_str())); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | void test(skiatest::Reporter* r, sk_sp<SkSurface> surface, uint32_t expected) { |
| 111 | this->test(r, surface, expected, expected, expected, expected); |
| 112 | } |
| 113 | |
| 114 | private: |
Brian Osman | d9bde07 | 2020-04-15 14:18:13 -0400 | [diff] [blame] | 115 | SkTLazy<SkRuntimeShaderBuilder> fBuilder; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static void test_RuntimeEffect_Shaders(skiatest::Reporter* r, GrContext* context) { |
| 119 | SkImageInfo info = SkImageInfo::Make(2, 2, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
| 120 | sk_sp<SkSurface> surface; |
| 121 | if (context) { |
| 122 | surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info); |
| 123 | } else { |
| 124 | surface = SkSurface::MakeRaster(info); |
| 125 | } |
| 126 | REPORTER_ASSERT(r, surface); |
| 127 | |
Brian Osman | 7353dc5 | 2020-02-07 13:37:12 -0500 | [diff] [blame] | 128 | TestEffect xy(r, "", "color = half4(half2(p - 0.5), 0, 1);"); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 129 | xy.test(r, surface, 0xFF000000, 0xFF0000FF, 0xFF00FF00, 0xFF00FFFF); |
| 130 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 131 | using float4 = std::array<float, 4>; |
| 132 | |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 133 | // NOTE: For now, we always emit valid premul colors, until CPU and GPU agree on clamping |
| 134 | TestEffect uniformColor(r, "uniform float4 gColor;", "color = half4(gColor);"); |
| 135 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 136 | uniformColor["gColor"] = float4{ 0.0f, 0.25f, 0.75f, 1.0f }; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 137 | uniformColor.test(r, surface, 0xFFBF4000); |
| 138 | |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 139 | uniformColor["gColor"] = float4{ 0.75f, 0.25f, 0.0f, 1.0f }; |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 140 | uniformColor.test(r, surface, 0xFF0040BF); |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 141 | |
| 142 | TestEffect pickColor(r, "in int flag; uniform half4 gColors[2];", "color = gColors[flag];"); |
| 143 | pickColor["gColors"] = |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 144 | 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] | 145 | pickColor["flag"] = 0; |
Brian Osman | 6f5e940 | 2020-01-22 10:39:31 -0500 | [diff] [blame] | 146 | pickColor.test(r, surface, 0x7F00007F); // Tests that we clamp to valid premul |
Brian Osman | 504032e | 2020-01-10 10:05:24 -0500 | [diff] [blame] | 147 | pickColor["flag"] = 1; |
| 148 | pickColor.test(r, surface, 0xFF00FF00); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | DEF_TEST(SkRuntimeEffectSimple, r) { |
| 152 | test_RuntimeEffect_Shaders(r, nullptr); |
| 153 | } |
| 154 | |
| 155 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkRuntimeEffectSimple_GPU, r, ctxInfo) { |
| 156 | test_RuntimeEffect_Shaders(r, ctxInfo.grContext()); |
| 157 | } |