blob: 4c98b499b019675ce459902bc9a1b1e08eb813a6 [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"
John Stilesd80f9662021-02-03 11:42:32 -050021#include "src/sksl/SkSLDefines.h" // for kDefaultInlineThreshold
John Stiles232dd2b2021-01-25 10:57:47 -050022#include "tests/Test.h"
23#include "tools/Resources.h"
24#include "tools/ToolUtils.h"
25
26static const SkRect kRect = SkRect::MakeWH(1, 1);
27
John Stilesb41d5bb2021-01-26 16:28:12 -050028template <typename T>
29static void set_uniform(SkRuntimeShaderBuilder* builder, const char* name, const T& value) {
30 SkRuntimeShaderBuilder::BuilderUniform uniform = builder->uniform(name);
31 if (uniform.fVar) {
32 uniform = value;
33 }
34}
35
John Stilesd80f9662021-02-03 11:42:32 -050036static void test_one_permutation(skiatest::Reporter* r,
37 SkSurface* surface,
38 const char* testFile,
39 const char* permutationSuffix,
40 const SkRuntimeEffect::Options& options) {
John Stiles232dd2b2021-01-25 10:57:47 -050041 SkString resourcePath = SkStringPrintf("sksl/%s", testFile);
42 sk_sp<SkData> shaderData = GetResourceAsData(resourcePath.c_str());
43 if (!shaderData) {
John Stilesd80f9662021-02-03 11:42:32 -050044 ERRORF(r, "%s%s: Unable to load file", testFile, permutationSuffix);
John Stiles232dd2b2021-01-25 10:57:47 -050045 return;
46 }
47
48 SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
John Stilesd80f9662021-02-03 11:42:32 -050049 SkRuntimeEffect::Result result = SkRuntimeEffect::Make(shaderString, options);
50 if (!result.effect) {
51 ERRORF(r, "%s%s: %s", testFile, permutationSuffix, result.errorText.c_str());
John Stiles232dd2b2021-01-25 10:57:47 -050052 return;
53 }
54
John Stilesd80f9662021-02-03 11:42:32 -050055 SkRuntimeShaderBuilder builder(result.effect);
John Stilese1658b52021-01-29 11:44:13 -050056 set_uniform(&builder, "colorBlack", SkV4{0, 0, 0, 1});
57 set_uniform(&builder, "colorRed", SkV4{1, 0, 0, 1});
58 set_uniform(&builder, "colorGreen", SkV4{0, 1, 0, 1});
59 set_uniform(&builder, "colorBlue", SkV4{0, 0, 1, 1});
60 set_uniform(&builder, "colorWhite", SkV4{1, 1, 1, 1});
61 set_uniform(&builder, "testInputs", SkV4{-1.25, 0, 0.75, 2.25});
John Stiles01cdf012021-02-10 09:53:41 -050062 set_uniform(&builder, "testMatrix2x2", std::array<float,4>{1, 2,
63 3, 4});
64 set_uniform(&builder, "testMatrix3x3", std::array<float,9>{1, 2, 3,
65 4, 5, 6,
66 7, 8, 9});
John Stilese1658b52021-01-29 11:44:13 -050067 set_uniform(&builder, "unknownInput", 1.0f);
John Stilesb41d5bb2021-01-26 16:28:12 -050068
John Stiles232dd2b2021-01-25 10:57:47 -050069 sk_sp<SkShader> shader = builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
70 if (!shader) {
John Stilesd80f9662021-02-03 11:42:32 -050071 ERRORF(r, "%s%s: Unable to build shader", testFile, permutationSuffix);
John Stiles232dd2b2021-01-25 10:57:47 -050072 return;
73 }
74
75 SkPaint paintShader;
76 paintShader.setShader(shader);
77 surface->getCanvas()->drawRect(kRect, paintShader);
78
79 SkBitmap bitmap;
80 REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
81 REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
82 /*srcX=*/0, /*srcY=*/0));
83
84 SkColor color = bitmap.getColor(0, 0);
85 REPORTER_ASSERT(r, color == SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00),
86 "Expected: solid green. Actual: A=%02X R=%02X G=%02X B=%02X.",
87 SkColorGetA(color), SkColorGetR(color), SkColorGetG(color), SkColorGetB(color));
88}
89
John Stilesd80f9662021-02-03 11:42:32 -050090static void test_permutations(skiatest::Reporter* r, SkSurface* surface, const char* testFile) {
91 SkRuntimeEffect::Options options;
92 options.inlineThreshold = 0;
93 test_one_permutation(r, surface, testFile, " (NoInline)", options);
94
95 options.inlineThreshold = SkSL::kDefaultInlineThreshold;
96 test_one_permutation(r, surface, testFile, "", options);
97}
98
John Stiles232dd2b2021-01-25 10:57:47 -050099static void test_cpu(skiatest::Reporter* r, const char* testFile) {
100 const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
101 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
102
John Stilesd80f9662021-02-03 11:42:32 -0500103 test_permutations(r, surface.get(), testFile);
John Stiles232dd2b2021-01-25 10:57:47 -0500104}
105
106static void test_gpu(skiatest::Reporter* r, GrDirectContext* ctx, const char* testFile) {
107 const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
108 sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
109
John Stilesd80f9662021-02-03 11:42:32 -0500110 test_permutations(r, surface.get(), testFile);
John Stiles232dd2b2021-01-25 10:57:47 -0500111}
112
John Stilesb4418502021-02-04 10:57:08 -0500113#define SKSL_TEST_CPU(name, path) \
John Stiles232dd2b2021-01-25 10:57:47 -0500114 DEF_TEST(name ## _CPU, r) { \
115 test_cpu(r, path); \
John Stilesb4418502021-02-04 10:57:08 -0500116 }
117#define SKSL_TEST_GPU(name, path) \
John Stiles232dd2b2021-01-25 10:57:47 -0500118 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name ## _GPU, r, ctxInfo) { \
119 test_gpu(r, ctxInfo.directContext(), path); \
120 }
John Stilesb4418502021-02-04 10:57:08 -0500121#define SKSL_TEST(name, path) SKSL_TEST_CPU(name, path) SKSL_TEST_GPU(name, path)
John Stiles232dd2b2021-01-25 10:57:47 -0500122
John Stiles34c098d2021-02-09 10:38:59 -0500123SKSL_TEST(SkSLAssignmentOps, "folding/AssignmentOps.sksl")
John Stilesd4374782021-02-05 09:31:21 -0500124SKSL_TEST(SkSLBoolFolding, "folding/BoolFolding.sksl")
125SKSL_TEST(SkSLIntFoldingES2, "folding/IntFoldingES2.sksl")
126SKSL_TEST(SkSLFloatFolding, "folding/FloatFolding.sksl")
127SKSL_TEST(SkSLMatrixFoldingES2, "folding/MatrixFoldingES2.sksl")
128SKSL_TEST(SkSLShortCircuitBoolFolding, "folding/ShortCircuitBoolFolding.sksl")
129SKSL_TEST(SkSLVectorScalarFolding, "folding/VectorScalarFolding.sksl")
130SKSL_TEST(SkSLVectorVectorFolding, "folding/VectorVectorFolding.sksl")
John Stiles232dd2b2021-01-25 10:57:47 -0500131
John Stilesd4374782021-02-05 09:31:21 -0500132SKSL_TEST(SkSLIntrinsicAbsFloat, "intrinsics/AbsFloat.sksl")
133SKSL_TEST(SkSLIntrinsicCeil, "intrinsics/Ceil.sksl")
134SKSL_TEST(SkSLIntrinsicClampFloat, "intrinsics/ClampFloat.sksl")
135SKSL_TEST(SkSLIntrinsicMaxFloat, "intrinsics/MaxFloat.sksl")
136SKSL_TEST(SkSLIntrinsicMinFloat, "intrinsics/MinFloat.sksl")
137SKSL_TEST(SkSLIntrinsicMixFloat, "intrinsics/MixFloat.sksl")
138SKSL_TEST(SkSLIntrinsicSignFloat, "intrinsics/SignFloat.sksl")
John Stilese1658b52021-01-29 11:44:13 -0500139
John Stilesd4374782021-02-05 09:31:21 -0500140SKSL_TEST(SkSLArrayTypes, "shared/ArrayTypes.sksl")
141SKSL_TEST(SkSLAssignment, "shared/Assignment.sksl")
142SKSL_TEST(SkSLCastsRoundTowardZero, "shared/CastsRoundTowardZero.sksl")
143SKSL_TEST(SkSLCommaMixedTypes, "shared/CommaMixedTypes.sksl")
144SKSL_TEST(SkSLCommaSideEffects, "shared/CommaSideEffects.sksl")
145SKSL_TEST(SkSLConstantIf, "shared/ConstantIf.sksl")
146SKSL_TEST(SkSLConstVariableComparison, "shared/ConstVariableComparison.sksl")
147SKSL_TEST(SkSLDeadIfStatement, "shared/DeadIfStatement.sksl")
148SKSL_TEST(SkSLDeadStripFunctions, "shared/DeadStripFunctions.sksl")
149SKSL_TEST(SkSLDependentInitializers, "shared/DependentInitializers.sksl")
150SKSL_TEST(SkSLEmptyBlocksES2, "shared/EmptyBlocksES2.sksl")
151SKSL_TEST(SkSLForLoopControlFlow, "shared/ForLoopControlFlow.sksl")
152SKSL_TEST(SkSLFunctionArgTypeMatch, "shared/FunctionArgTypeMatch.sksl")
153SKSL_TEST(SkSLFunctionReturnTypeMatch, "shared/FunctionReturnTypeMatch.sksl")
154SKSL_TEST(SkSLFunctions, "shared/Functions.sksl")
155SKSL_TEST(SkSLGeometricIntrinsics, "shared/GeometricIntrinsics.sksl")
156SKSL_TEST(SkSLHelloWorld, "shared/HelloWorld.sksl")
157SKSL_TEST(SkSLHex, "shared/Hex.sksl")
158SKSL_TEST(SkSLMatrices, "shared/Matrices.sksl")
159SKSL_TEST(SkSLMultipleAssignments, "shared/MultipleAssignments.sksl")
160SKSL_TEST(SkSLNegatedVectorLiteral, "shared/NegatedVectorLiteral.sksl")
161SKSL_TEST(SkSLNumberCasts, "shared/NumberCasts.sksl")
162SKSL_TEST(SkSLOperatorsES2, "shared/OperatorsES2.sksl")
163SKSL_TEST(SkSLOutParams, "shared/OutParams.sksl")
164SKSL_TEST(SkSLOutParamsTricky, "shared/OutParamsTricky.sksl")
165SKSL_TEST(SkSLResizeMatrix, "shared/ResizeMatrix.sksl")
166SKSL_TEST(SkSLScalarConversionConstructorsES2, "shared/ScalarConversionConstructorsES2.sksl")
167SKSL_TEST(SkSLStackingVectorCasts, "shared/StackingVectorCasts.sksl")
168SKSL_TEST(SkSLStaticIf, "shared/StaticIf.sksl")
169SKSL_TEST(SkSLSwizzleBoolConstants, "shared/SwizzleBoolConstants.sksl")
John Stilesc2c1b0c2021-02-05 16:24:03 -0500170SKSL_TEST(SkSLSwizzleByConstantIndex, "shared/SwizzleByConstantIndex.sksl")
171SKSL_TEST(SkSLSwizzleConstants, "shared/SwizzleConstants.sksl")
172SKSL_TEST(SkSLSwizzleLTRB, "shared/SwizzleLTRB.sksl")
173SKSL_TEST(SkSLSwizzleOpt, "shared/SwizzleOpt.sksl")
174SKSL_TEST(SkSLSwizzleScalar, "shared/SwizzleScalar.sksl")
John Stilesecd7c222021-02-08 14:20:28 -0500175SKSL_TEST(SkSLTernaryAsLValueEntirelyFoldable, "shared/TernaryAsLValueEntirelyFoldable.sksl")
176SKSL_TEST(SkSLTernaryAsLValueFoldableTest, "shared/TernaryAsLValueFoldableTest.sksl")
177SKSL_TEST(SkSLUnaryPositiveNegative, "shared/UnaryPositiveNegative.sksl")
178SKSL_TEST(SkSLUnusedVariables, "shared/UnusedVariables.sksl")
179SKSL_TEST(SkSLVectorConstructors, "shared/VectorConstructors.sksl")
John Stiles7b928972021-01-27 09:56:04 -0500180
John Stiles232dd2b2021-01-25 10:57:47 -0500181/*
John Stilesecd20362021-02-03 17:44:47 -0500182// Incompatible with Runtime Effects because calling a function before its definition is disallowed.
183// (This was done to prevent recursion, as required by ES2.)
184SKSL_TEST(SkSLFunctionPrototype, "shared/FunctionPrototype.sksl")
John Stilese1658b52021-01-29 11:44:13 -0500185*/
186
187/*
Brian Osmanbf7b4b82021-01-27 13:53:43 -0500188TODO(skia:10939): enable this test when Runtime Effects supports structs in function signatures
189SKSL_TEST(SkSLStructsInFunctions, "shared/StructsInFunctions.sksl")
190*/
191
192/*
John Stiles232dd2b2021-01-25 10:57:47 -0500193TODO(skia:11209): enable these tests when Runtime Effects have support for ES3
194
John Stilesd4374782021-02-05 09:31:21 -0500195SKSL_TEST(SkSLIntFoldingES3, "folding/IntFoldingES3.sksl")
196SKSL_TEST(SkSLMatrixFoldingES3, "folding/MatrixFoldingES3.sksl")
John Stiles7b928972021-01-27 09:56:04 -0500197
John Stilesd4374782021-02-05 09:31:21 -0500198SKSL_TEST(SkSLIntrinsicAbsInt, "intrinsics/AbsInt.sksl")
199SKSL_TEST(SkSLIntrinsicClampInt, "intrinsics/ClampInt.sksl")
200SKSL_TEST(SkSLIntrinsicMaxInt, "intrinsics/MaxInt.sksl")
201SKSL_TEST(SkSLIntrinsicMinInt, "intrinsics/MinInt.sksl")
202SKSL_TEST(SkSLIntrinsicMixBool, "intrinsics/MixBool.sksl")
203SKSL_TEST(SkSLIntrinsicSignInt, "intrinsics/SignInt.sksl")
John Stilese1658b52021-01-29 11:44:13 -0500204
John Stilesd4374782021-02-05 09:31:21 -0500205SKSL_TEST(SkSLArrayConstructors, "shared/ArrayConstructors.sksl")
206SKSL_TEST(SkSLDeadLoopVariable, "shared/DeadLoopVariable.sksl")
207SKSL_TEST(SkSLDoWhileControlFlow, "shared/DoWhileControlFlow.sksl")
208SKSL_TEST(SkSLEmptyBlocksES3, "shared/EmptyBlocksES3.sksl")
209SKSL_TEST(SkSLHexUnsigned, "shared/HexUnsigned.sksl")
210SKSL_TEST(SkSLMatricesNonsquare, "shared/MatricesNonsquare.sksl")
211SKSL_TEST(SkSLOperatorsES3, "shared/OperatorsES3.sksl")
212SKSL_TEST(SkSLResizeMatrixNonsquare, "shared/ResizeMatrixNonsquare.sksl")
213SKSL_TEST(SkSLScalarConversionConstructorsES3, "shared/ScalarConversionConstructorsES3.sksl")
John Stilesc2c1b0c2021-02-05 16:24:03 -0500214SKSL_TEST(SkSLSwizzleByIndex, "shared/SwizzleByIndex.sksl")
John Stilesd4374782021-02-05 09:31:21 -0500215SKSL_TEST(SkSLWhileLoopControlFlow, "shared/WhileLoopControlFlow.sksl")
John Stiles232dd2b2021-01-25 10:57:47 -0500216*/