John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | |
| 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkData.h" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkSize.h" |
| 15 | #include "include/core/SkString.h" |
| 16 | #include "include/core/SkSurface.h" |
| 17 | #include "include/effects/SkGradientShader.h" |
| 18 | #include "include/effects/SkImageFilters.h" |
| 19 | #include "include/effects/SkRuntimeEffect.h" |
| 20 | #include "include/utils/SkRandom.h" |
| 21 | #include "tests/Test.h" |
| 22 | #include "tools/Resources.h" |
| 23 | #include "tools/ToolUtils.h" |
| 24 | |
| 25 | static const SkRect kRect = SkRect::MakeWH(1, 1); |
| 26 | |
John Stiles | b41d5bb | 2021-01-26 16:28:12 -0500 | [diff] [blame] | 27 | template <typename T> |
| 28 | static void set_uniform(SkRuntimeShaderBuilder* builder, const char* name, const T& value) { |
| 29 | SkRuntimeShaderBuilder::BuilderUniform uniform = builder->uniform(name); |
| 30 | if (uniform.fVar) { |
| 31 | uniform = value; |
| 32 | } |
| 33 | } |
| 34 | |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 35 | static void test(skiatest::Reporter* r, SkSurface* surface, const char* testFile) { |
| 36 | SkString resourcePath = SkStringPrintf("sksl/%s", testFile); |
| 37 | sk_sp<SkData> shaderData = GetResourceAsData(resourcePath.c_str()); |
| 38 | if (!shaderData) { |
| 39 | ERRORF(r, "%s: Unable to load file", testFile); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()}; |
| 44 | auto [effect, error] = SkRuntimeEffect::Make(shaderString); |
| 45 | if (!effect) { |
| 46 | ERRORF(r, "%s: %s", testFile, error.c_str()); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | SkRuntimeShaderBuilder builder(effect); |
John Stiles | b41d5bb | 2021-01-26 16:28:12 -0500 | [diff] [blame] | 51 | set_uniform(&builder, "colorBlack", SkV4{0, 0, 0, 1}); |
| 52 | set_uniform(&builder, "colorRed", SkV4{1, 0, 0, 1}); |
| 53 | set_uniform(&builder, "colorGreen", SkV4{0, 1, 0, 1}); |
| 54 | set_uniform(&builder, "colorBlue", SkV4{0, 0, 1, 1}); |
| 55 | set_uniform(&builder, "colorWhite", SkV4{1, 1, 1, 1}); |
| 56 | set_uniform(&builder, "unknownInput", 1.0f); |
| 57 | |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 58 | sk_sp<SkShader> shader = builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true); |
| 59 | if (!shader) { |
| 60 | ERRORF(r, "%s: Unable to build shader", testFile); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | SkPaint paintShader; |
| 65 | paintShader.setShader(shader); |
| 66 | surface->getCanvas()->drawRect(kRect, paintShader); |
| 67 | |
| 68 | SkBitmap bitmap; |
| 69 | REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo())); |
| 70 | REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(), |
| 71 | /*srcX=*/0, /*srcY=*/0)); |
| 72 | |
| 73 | SkColor color = bitmap.getColor(0, 0); |
| 74 | REPORTER_ASSERT(r, color == SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00), |
| 75 | "Expected: solid green. Actual: A=%02X R=%02X G=%02X B=%02X.", |
| 76 | SkColorGetA(color), SkColorGetR(color), SkColorGetG(color), SkColorGetB(color)); |
| 77 | } |
| 78 | |
| 79 | static void test_cpu(skiatest::Reporter* r, const char* testFile) { |
| 80 | const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height()); |
| 81 | sk_sp<SkSurface> surface(SkSurface::MakeRaster(info)); |
| 82 | |
| 83 | test(r, surface.get(), testFile); |
| 84 | } |
| 85 | |
| 86 | static void test_gpu(skiatest::Reporter* r, GrDirectContext* ctx, const char* testFile) { |
| 87 | const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height()); |
| 88 | sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info)); |
| 89 | |
| 90 | test(r, surface.get(), testFile); |
| 91 | } |
| 92 | |
| 93 | #define SKSL_TEST(name, path) \ |
| 94 | DEF_TEST(name ## _CPU, r) { \ |
| 95 | test_cpu(r, path); \ |
| 96 | } \ |
| 97 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name ## _GPU, r, ctxInfo) { \ |
| 98 | test_gpu(r, ctxInfo.directContext(), path); \ |
| 99 | } |
| 100 | |
| 101 | SKSL_TEST(SkSLBoolFolding, "folding/BoolFolding.sksl") |
| 102 | SKSL_TEST(SkSLIntFoldingES2, "folding/IntFoldingES2.sksl") |
John Stiles | 053f785 | 2021-01-25 13:04:12 -0500 | [diff] [blame] | 103 | SKSL_TEST(SkSLFloatFolding, "folding/FloatFolding.sksl") |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 104 | SKSL_TEST(SkSLMatrixFoldingES2, "folding/MatrixFoldingES2.sksl") |
| 105 | SKSL_TEST(SkSLShortCircuitBoolFolding, "folding/ShortCircuitBoolFolding.sksl") |
| 106 | SKSL_TEST(SkSLVectorScalarFolding, "folding/VectorScalarFolding.sksl") |
| 107 | SKSL_TEST(SkSLVectorVectorFolding, "folding/VectorVectorFolding.sksl") |
| 108 | |
John Stiles | 7b92897 | 2021-01-27 09:56:04 -0500 | [diff] [blame] | 109 | SKSL_TEST(SkSLForLoopControlFlow, "shared/ForLoopControlFlow.sksl") |
| 110 | |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 111 | /* |
Brian Osman | bf7b4b8 | 2021-01-27 13:53:43 -0500 | [diff] [blame^] | 112 | TODO(skia:10939): enable this test when Runtime Effects supports structs in function signatures |
| 113 | SKSL_TEST(SkSLStructsInFunctions, "shared/StructsInFunctions.sksl") |
| 114 | */ |
| 115 | |
| 116 | /* |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 117 | TODO(skia:11209): enable these tests when Runtime Effects have support for ES3 |
| 118 | |
| 119 | SKSL_TEST(SkSLIntFoldingES3, "folding/IntFoldingES3.sksl") |
| 120 | SKSL_TEST(SkSLMatrixFoldingES3, "folding/MatrixFoldingES3.sksl") |
John Stiles | 7b92897 | 2021-01-27 09:56:04 -0500 | [diff] [blame] | 121 | |
| 122 | SKSL_TEST(SkSLDoWhileControlFlow, "shared/DoWhileControlFlow.sksl") |
| 123 | SKSL_TEST(SkSLWhileLoopControlFlow, "shared/WhileLoopControlFlow.sksl") |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 124 | */ |