blob: 4d7db65d293020b0dd2433d15637d112836ca623 [file] [log] [blame]
John Stiles232dd2b2021-01-25 10:57:47 -05001/*
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
25static const SkRect kRect = SkRect::MakeWH(1, 1);
26
John Stilesb41d5bb2021-01-26 16:28:12 -050027template <typename T>
28static 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 Stiles232dd2b2021-01-25 10:57:47 -050035static 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 Stilesb41d5bb2021-01-26 16:28:12 -050051 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 Stiles232dd2b2021-01-25 10:57:47 -050058 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
79static 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
86static 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
101SKSL_TEST(SkSLBoolFolding, "folding/BoolFolding.sksl")
102SKSL_TEST(SkSLIntFoldingES2, "folding/IntFoldingES2.sksl")
John Stiles053f7852021-01-25 13:04:12 -0500103SKSL_TEST(SkSLFloatFolding, "folding/FloatFolding.sksl")
John Stiles232dd2b2021-01-25 10:57:47 -0500104SKSL_TEST(SkSLMatrixFoldingES2, "folding/MatrixFoldingES2.sksl")
105SKSL_TEST(SkSLShortCircuitBoolFolding, "folding/ShortCircuitBoolFolding.sksl")
106SKSL_TEST(SkSLVectorScalarFolding, "folding/VectorScalarFolding.sksl")
107SKSL_TEST(SkSLVectorVectorFolding, "folding/VectorVectorFolding.sksl")
108
John Stiles7b928972021-01-27 09:56:04 -0500109SKSL_TEST(SkSLForLoopControlFlow, "shared/ForLoopControlFlow.sksl")
110
John Stiles232dd2b2021-01-25 10:57:47 -0500111/*
Brian Osmanbf7b4b82021-01-27 13:53:43 -0500112TODO(skia:10939): enable this test when Runtime Effects supports structs in function signatures
113SKSL_TEST(SkSLStructsInFunctions, "shared/StructsInFunctions.sksl")
114*/
115
116/*
John Stiles232dd2b2021-01-25 10:57:47 -0500117TODO(skia:11209): enable these tests when Runtime Effects have support for ES3
118
119SKSL_TEST(SkSLIntFoldingES3, "folding/IntFoldingES3.sksl")
120SKSL_TEST(SkSLMatrixFoldingES3, "folding/MatrixFoldingES3.sksl")
John Stiles7b928972021-01-27 09:56:04 -0500121
122SKSL_TEST(SkSLDoWhileControlFlow, "shared/DoWhileControlFlow.sksl")
123SKSL_TEST(SkSLWhileLoopControlFlow, "shared/WhileLoopControlFlow.sksl")
John Stiles232dd2b2021-01-25 10:57:47 -0500124*/