• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/private/SkSLDefines.h"  // for kDefaultInlineThreshold
21 #include "include/utils/SkRandom.h"
22 #include "src/gpu/GrCaps.h"
23 #include "src/gpu/GrDirectContextPriv.h"
24 #include "tests/Test.h"
25 #include "tools/Resources.h"
26 #include "tools/ToolUtils.h"
27 
28 static const SkRect kRect = SkRect::MakeWH(1, 1);
29 
30 template <typename T>
set_uniform(SkRuntimeShaderBuilder * builder,const char * name,const T & value)31 static void set_uniform(SkRuntimeShaderBuilder* builder, const char* name, const T& value) {
32     SkRuntimeShaderBuilder::BuilderUniform uniform = builder->uniform(name);
33     if (uniform.fVar) {
34         uniform = value;
35     }
36 }
37 
test_one_permutation(skiatest::Reporter * r,SkSurface * surface,const char * testFile,const char * permutationSuffix,const SkRuntimeEffect::Options & options)38 static void test_one_permutation(skiatest::Reporter* r,
39                                  SkSurface* surface,
40                                  const char* testFile,
41                                  const char* permutationSuffix,
42                                  const SkRuntimeEffect::Options& options) {
43     SkString resourcePath = SkStringPrintf("sksl/%s", testFile);
44     sk_sp<SkData> shaderData = GetResourceAsData(resourcePath.c_str());
45     if (!shaderData) {
46         ERRORF(r, "%s%s: Unable to load file", testFile, permutationSuffix);
47         return;
48     }
49 
50     SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()};
51     SkRuntimeEffect::Result result = SkRuntimeEffect::MakeForShader(shaderString, options);
52     if (!result.effect) {
53         ERRORF(r, "%s%s: %s", testFile, permutationSuffix, result.errorText.c_str());
54         return;
55     }
56 
57     SkRuntimeShaderBuilder builder(result.effect);
58     set_uniform(&builder, "colorBlack",       SkV4{0, 0, 0, 1});
59     set_uniform(&builder, "colorRed",         SkV4{1, 0, 0, 1});
60     set_uniform(&builder, "colorGreen",       SkV4{0, 1, 0, 1});
61     set_uniform(&builder, "colorBlue",        SkV4{0, 0, 1, 1});
62     set_uniform(&builder, "colorWhite",       SkV4{1, 1, 1, 1});
63     set_uniform(&builder, "testInputs",       SkV4{-1.25, 0, 0.75, 2.25});
64     set_uniform(&builder, "testMatrix2x2",    std::array<float,4>{1, 2,
65                                                                   3, 4});
66     set_uniform(&builder, "testMatrix3x3",    std::array<float,9>{1, 2, 3,
67                                                                   4, 5, 6,
68                                                                   7, 8, 9});
69     set_uniform(&builder, "unknownInput",     1.0f);
70     set_uniform(&builder, "testMatrix2x2",    std::array<float,4>{1, 2,
71                                                                   3, 4});
72     set_uniform(&builder, "testMatrix3x3",    std::array<float,9>{1, 2, 3,
73                                                                   4, 5, 6,
74                                                                   7, 8, 9});
75 
76     sk_sp<SkShader> shader = builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true);
77     if (!shader) {
78         ERRORF(r, "%s%s: Unable to build shader", testFile, permutationSuffix);
79         return;
80     }
81 
82     SkPaint paintShader;
83     paintShader.setShader(shader);
84     surface->getCanvas()->drawRect(kRect, paintShader);
85 
86     SkBitmap bitmap;
87     REPORTER_ASSERT(r, bitmap.tryAllocPixels(surface->imageInfo()));
88     REPORTER_ASSERT(r, surface->readPixels(bitmap.info(), bitmap.getPixels(), bitmap.rowBytes(),
89                                            /*srcX=*/0, /*srcY=*/0));
90 
91     SkColor color = bitmap.getColor(0, 0);
92     REPORTER_ASSERT(r, color == SkColorSetARGB(0xFF, 0x00, 0xFF, 0x00),
93                     "Expected: solid green. Actual: A=%02X R=%02X G=%02X B=%02X.",
94                     SkColorGetA(color), SkColorGetR(color), SkColorGetG(color), SkColorGetB(color));
95 }
96 
test_permutations(skiatest::Reporter * r,SkSurface * surface,const char * testFile,bool worksInES2)97 static void test_permutations(skiatest::Reporter* r,
98                               SkSurface* surface,
99                               const char* testFile,
100                               bool worksInES2) {
101     SkRuntimeEffect::Options options;
102     options.enforceES2Restrictions = worksInES2;
103     options.forceNoInline = false;
104     test_one_permutation(r, surface, testFile, "", options);
105 
106     options.forceNoInline = true;
107     test_one_permutation(r, surface, testFile, " (NoInline)", options);
108 }
109 
test_cpu(skiatest::Reporter * r,const char * testFile)110 static void test_cpu(skiatest::Reporter* r, const char* testFile) {
111     const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
112     sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
113 
114     test_permutations(r, surface.get(), testFile, /*worksInES2=*/true);
115 }
116 
test_gpu(skiatest::Reporter * r,GrDirectContext * ctx,const char * testFile)117 static void test_gpu(skiatest::Reporter* r, GrDirectContext* ctx, const char* testFile) {
118     const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
119     sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
120 
121     test_permutations(r, surface.get(), testFile, /*worksInES2=*/true);
122 }
123 
test_es3(skiatest::Reporter * r,GrDirectContext * ctx,const char * testFile)124 static void test_es3(skiatest::Reporter* r, GrDirectContext* ctx, const char* testFile) {
125     // We don't have an ES2 caps bit, so we check for integer support and derivatives support.
126     // Our ES2 bots should return false for these.
127     if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport() ||
128         !ctx->priv().caps()->shaderCaps()->integerSupport()) {
129         return;
130     }
131     // ES3-only tests never run on the CPU, because SkVM lacks support for many non-ES2 features.
132     const SkImageInfo info = SkImageInfo::MakeN32Premul(kRect.width(), kRect.height());
133     sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
134 
135     test_permutations(r, surface.get(), testFile, /*worksInES2=*/false);
136 }
137 
138 #define SKSL_TEST_CPU(name, path)                                   \
139     DEF_TEST(name ## _CPU, r) {                                     \
140         test_cpu(r, path);                                          \
141     }
142 #define SKSL_TEST_GPU(name, path)                                   \
143     DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name ## _GPU, r, ctxInfo) {  \
144         test_gpu(r, ctxInfo.directContext(), path);                 \
145     }
146 #define SKSL_TEST_ES3(name, path)                                   \
147     DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name ## _GPU, r, ctxInfo) {  \
148         test_es3(r, ctxInfo.directContext(), path);                 \
149     }
150 #define SKSL_TEST(name, path) SKSL_TEST_CPU(name, path) SKSL_TEST_GPU(name, path)
151 
152 SKSL_TEST(SkSLAssignmentOps,                   "folding/AssignmentOps.sksl")
153 SKSL_TEST(SkSLBoolFolding,                     "folding/BoolFolding.sksl")
154 SKSL_TEST(SkSLCastFolding,                     "folding/CastFolding.sksl")
155 SKSL_TEST(SkSLIntFoldingES2,                   "folding/IntFoldingES2.sksl")
156 SKSL_TEST_ES3(SkSLIntFoldingES3,               "folding/IntFoldingES3.sksl")
157 SKSL_TEST(SkSLFloatFolding,                    "folding/FloatFolding.sksl")
158 // skbug.com/11919: Fails on Nexus5/7, and Intel GPUs
159 SKSL_TEST_CPU(SkSLMatrixFoldingES2,            "folding/MatrixFoldingES2.sksl")
160 SKSL_TEST(SkSLSelfAssignment,                  "folding/SelfAssignment.sksl")
161 SKSL_TEST(SkSLShortCircuitBoolFolding,         "folding/ShortCircuitBoolFolding.sksl")
162 SKSL_TEST(SkSLSwizzleFolding,                  "folding/SwizzleFolding.sksl")
163 SKSL_TEST(SkSLVectorScalarFolding,             "folding/VectorScalarFolding.sksl")
164 SKSL_TEST(SkSLVectorVectorFolding,             "folding/VectorVectorFolding.sksl")
165 
166 SKSL_TEST(SkSLForBodyMustBeInlinedIntoAScope,     "inliner/ForBodyMustBeInlinedIntoAScope.sksl")
167 SKSL_TEST_ES3(SkSLForInitializerExpressionsCanBeInlined,
168          "inliner/ForInitializerExpressionsCanBeInlined.sksl")
169 SKSL_TEST(SkSLForWithoutReturnInsideCanBeInlined, "inliner/ForWithoutReturnInsideCanBeInlined.sksl")
170 SKSL_TEST(SkSLForWithReturnInsideCannotBeInlined, "inliner/ForWithReturnInsideCannotBeInlined.sksl")
171 SKSL_TEST(SkSLIfBodyMustBeInlinedIntoAScope,      "inliner/IfBodyMustBeInlinedIntoAScope.sksl")
172 SKSL_TEST(SkSLIfElseBodyMustBeInlinedIntoAScope,  "inliner/IfElseBodyMustBeInlinedIntoAScope.sksl")
173 SKSL_TEST(SkSLIfElseChainWithReturnsCanBeInlined, "inliner/IfElseChainWithReturnsCanBeInlined.sksl")
174 SKSL_TEST(SkSLIfTestCanBeInlined,                 "inliner/IfTestCanBeInlined.sksl")
175 SKSL_TEST(SkSLIfWithReturnsCanBeInlined,          "inliner/IfWithReturnsCanBeInlined.sksl")
176 SKSL_TEST(SkSLInlineKeywordOverridesThreshold,    "inliner/InlineKeywordOverridesThreshold.sksl")
177 SKSL_TEST(SkSLInlinerAvoidsVariableNameOverlap,   "inliner/InlinerAvoidsVariableNameOverlap.sksl")
178 SKSL_TEST(SkSLInlinerElidesTempVarForReturnsInsideBlock,
179      "inliner/InlinerElidesTempVarForReturnsInsideBlock.sksl")
180 SKSL_TEST(SkSLInlinerUsesTempVarForMultipleReturns,
181      "inliner/InlinerUsesTempVarForMultipleReturns.sksl")
182 SKSL_TEST(SkSLInlinerUsesTempVarForReturnsInsideBlockWithVar,
183      "inliner/InlinerUsesTempVarForReturnsInsideBlockWithVar.sksl")
184 SKSL_TEST(SkSLInlineThreshold,                    "inliner/InlineThreshold.sksl")
185 // skbug.com/11919: Fails on Adreno + Vulkan
186 SKSL_TEST_CPU(SkSLInlineWithInoutArgument,        "inliner/InlineWithInoutArgument.sksl")
187 SKSL_TEST(SkSLInlineWithModifiedArgument,         "inliner/InlineWithModifiedArgument.sksl")
188 SKSL_TEST(SkSLInlineWithNestedBigCalls,           "inliner/InlineWithNestedBigCalls.sksl")
189 SKSL_TEST(SkSLInlineWithUnmodifiedArgument,       "inliner/InlineWithUnmodifiedArgument.sksl")
190 SKSL_TEST(SkSLInlineWithUnnecessaryBlocks,        "inliner/InlineWithUnnecessaryBlocks.sksl")
191 SKSL_TEST(SkSLNoInline,                           "inliner/NoInline.sksl")
192 SKSL_TEST(SkSLShortCircuitEvaluationsCannotInlineRightHandSide,
193      "inliner/ShortCircuitEvaluationsCannotInlineRightHandSide.sksl")
194 SKSL_TEST(SkSLStructsCanBeInlinedSafely,          "inliner/StructsCanBeInlinedSafely.sksl")
195 SKSL_TEST(SkSLSwizzleCanBeInlinedDirectly,        "inliner/SwizzleCanBeInlinedDirectly.sksl")
196 SKSL_TEST(SkSLTernaryResultsCannotBeInlined,      "inliner/TernaryResultsCannotBeInlined.sksl")
197 SKSL_TEST(SkSLTernaryTestCanBeInlined,            "inliner/TernaryTestCanBeInlined.sksl")
198 SKSL_TEST(SkSLTrivialArgumentsInlineDirectly,     "inliner/TrivialArgumentsInlineDirectly.sksl")
199 SKSL_TEST_ES3(SkSLWhileBodyMustBeInlinedIntoAScope,
200          "inliner/WhileBodyMustBeInlinedIntoAScope.sksl")
201 SKSL_TEST_ES3(SkSLWhileTestCannotBeInlined,       "inliner/WhileTestCannotBeInlined.sksl")
202 
203 // TODO(skia:11052): SPIR-V does not yet honor `out` param semantics correctly
204 SKSL_TEST_CPU(SkSLInlinerHonorsGLSLOutParamSemantics,
205          "inliner/InlinerHonorsGLSLOutParamSemantics.sksl")
206 
207 SKSL_TEST(SkSLIntrinsicAbsFloat,               "intrinsics/AbsFloat.sksl")
208 SKSL_TEST(SkSLIntrinsicCeil,                   "intrinsics/Ceil.sksl")
209 SKSL_TEST(SkSLIntrinsicClampFloat,             "intrinsics/ClampFloat.sksl")
210 SKSL_TEST(SkSLIntrinsicMaxFloat,               "intrinsics/MaxFloat.sksl")
211 SKSL_TEST(SkSLIntrinsicMinFloat,               "intrinsics/MinFloat.sksl")
212 // skbug.com/11919: Fails on Adreno + Vulkan
213 SKSL_TEST_CPU(SkSLIntrinsicMixFloat,           "intrinsics/MixFloat.sksl")
214 SKSL_TEST(SkSLIntrinsicSignFloat,              "intrinsics/SignFloat.sksl")
215 
216 SKSL_TEST_ES3(SkSLArrayComparison,             "shared/ArrayComparison.sksl")
217 SKSL_TEST(SkSLArrayTypes,                      "shared/ArrayTypes.sksl")
218 SKSL_TEST(SkSLAssignment,                      "shared/Assignment.sksl")
219 SKSL_TEST(SkSLCastsRoundTowardZero,            "shared/CastsRoundTowardZero.sksl")
220 SKSL_TEST(SkSLCommaMixedTypes,                 "shared/CommaMixedTypes.sksl")
221 // This test causes the Adreno 330 driver to crash, and does not pass on Quadro P400 in wasm.
222 // The CPU test confirms that we can get it right, even if not all drivers do.
223 SKSL_TEST_CPU(SkSLCommaSideEffects,            "shared/CommaSideEffects.sksl")
224 SKSL_TEST(SkSLConstantIf,                      "shared/ConstantIf.sksl")
225 SKSL_TEST(SkSLConstVariableComparison,         "shared/ConstVariableComparison.sksl")
226 SKSL_TEST(SkSLDeadIfStatement,                 "shared/DeadIfStatement.sksl")
227 SKSL_TEST(SkSLDeadStripFunctions,              "shared/DeadStripFunctions.sksl")
228 SKSL_TEST(SkSLDependentInitializers,           "shared/DependentInitializers.sksl")
229 SKSL_TEST(SkSLEmptyBlocksES2,                  "shared/EmptyBlocksES2.sksl")
230 SKSL_TEST(SkSLForLoopControlFlow,              "shared/ForLoopControlFlow.sksl")
231 SKSL_TEST(SkSLFunctionArgTypeMatch,            "shared/FunctionArgTypeMatch.sksl")
232 SKSL_TEST(SkSLFunctionReturnTypeMatch,         "shared/FunctionReturnTypeMatch.sksl")
233 SKSL_TEST(SkSLFunctions,                       "shared/Functions.sksl")
234 SKSL_TEST(SkSLGeometricIntrinsics,             "shared/GeometricIntrinsics.sksl")
235 SKSL_TEST(SkSLHelloWorld,                      "shared/HelloWorld.sksl")
236 SKSL_TEST(SkSLHex,                             "shared/Hex.sksl")
237 SKSL_TEST(SkSLMatrices,                        "shared/Matrices.sksl")
238 SKSL_TEST(SkSLMatrixEquality,                  "shared/MatrixEquality.sksl")
239 SKSL_TEST(SkSLMultipleAssignments,             "shared/MultipleAssignments.sksl")
240 SKSL_TEST(SkSLNegatedVectorLiteral,            "shared/NegatedVectorLiteral.sksl")
241 SKSL_TEST(SkSLNumberCasts,                     "shared/NumberCasts.sksl")
242 SKSL_TEST(SkSLOperatorsES2,                    "shared/OperatorsES2.sksl")
243 
244 // skbug.com/11919: Fails on Adreno + Vulkan
245 SKSL_TEST_CPU(SkSLOutParams,                   "shared/OutParams.sksl")
246 SKSL_TEST_CPU(SkSLOutParamsNoInline,           "shared/OutParamsNoInline.sksl")
247 SKSL_TEST_CPU(SkSLOutParamsTricky,             "shared/OutParamsTricky.sksl")
248 
249 SKSL_TEST(SkSLResizeMatrix,                    "shared/ResizeMatrix.sksl")
250 SKSL_TEST(SkSLReturnsValueOnEveryPathES2,      "shared/ReturnsValueOnEveryPathES2.sksl")
251 SKSL_TEST(SkSLScalarConversionConstructorsES2, "shared/ScalarConversionConstructorsES2.sksl")
252 SKSL_TEST(SkSLStackingVectorCasts,             "shared/StackingVectorCasts.sksl")
253 SKSL_TEST(SkSLStaticIf,                        "shared/StaticIf.sksl")
254 SKSL_TEST(SkSLStructsInFunctions,              "shared/StructsInFunctions.sksl")
255 SKSL_TEST(SkSLSwizzleBoolConstants,            "shared/SwizzleBoolConstants.sksl")
256 SKSL_TEST(SkSLSwizzleByConstantIndex,          "shared/SwizzleByConstantIndex.sksl")
257 SKSL_TEST(SkSLSwizzleConstants,                "shared/SwizzleConstants.sksl")
258 SKSL_TEST(SkSLSwizzleLTRB,                     "shared/SwizzleLTRB.sksl")
259 SKSL_TEST(SkSLSwizzleOpt,                      "shared/SwizzleOpt.sksl")
260 SKSL_TEST(SkSLSwizzleScalar,                   "shared/SwizzleScalar.sksl")
261 SKSL_TEST(SkSLTernaryAsLValueEntirelyFoldable, "shared/TernaryAsLValueEntirelyFoldable.sksl")
262 SKSL_TEST(SkSLTernaryAsLValueFoldableTest,     "shared/TernaryAsLValueFoldableTest.sksl")
263 SKSL_TEST(SkSLTernaryExpression,               "shared/TernaryExpression.sksl")
264 SKSL_TEST(SkSLUnaryPositiveNegative,           "shared/UnaryPositiveNegative.sksl")
265 SKSL_TEST(SkSLUnusedVariables,                 "shared/UnusedVariables.sksl")
266 SKSL_TEST(SkSLVectorConstructors,              "shared/VectorConstructors.sksl")
267 // skbug.com/11919: Fails on Nexus5/7, and Intel GPUs
268 SKSL_TEST_CPU(SkSLVectorScalarMath,            "shared/VectorScalarMath.sksl")
269 
270 /*
271 // Incompatible with Runtime Effects because calling a function before its definition is disallowed.
272 // (This was done to prevent recursion, as required by ES2.)
273 SKSL_TEST(SkSLFunctionPrototype,               "shared/FunctionPrototype.sksl")
274 */
275 
276 /*
277 TODO(skia:11209): enable these tests when Runtime Effects have support for ES3
278 
279 SKSL_TEST(SkSLMatrixFoldingES3,                "folding/MatrixFoldingES3.sksl")
280 
281 SKSL_TEST(SkSLDoWhileBodyMustBeInlinedIntoAScope, "inliner/DoWhileBodyMustBeInlinedIntoAScope.sksl")
282 SKSL_TEST(SkSLDoWhileTestCannotBeInlined,         "inliner/DoWhileTestCannotBeInlined.sksl")
283 SKSL_TEST(SkSLEnumsCanBeInlinedSafely,         "inliner/EnumsCanBeInlinedSafely.sksl")
284 SKSL_TEST(SkSLStaticSwitch,                       "inliner/StaticSwitch.sksl")
285 
286 SKSL_TEST(SkSLIntrinsicAbsInt,                 "intrinsics/AbsInt.sksl")
287 SKSL_TEST(SkSLIntrinsicClampInt,               "intrinsics/ClampInt.sksl")
288 SKSL_TEST(SkSLIntrinsicMaxInt,                 "intrinsics/MaxInt.sksl")
289 SKSL_TEST(SkSLIntrinsicMinInt,                 "intrinsics/MinInt.sksl")
290 SKSL_TEST(SkSLIntrinsicMixBool,                "intrinsics/MixBool.sksl")
291 SKSL_TEST(SkSLIntrinsicSignInt,                "intrinsics/SignInt.sksl")
292 
293 SKSL_TEST(SkSLArrayConstructors,               "shared/ArrayConstructors.sksl")
294 SKSL_TEST(SkSLDeadLoopVariable,                "shared/DeadLoopVariable.sksl")
295 SKSL_TEST(SkSLDoWhileControlFlow,              "shared/DoWhileControlFlow.sksl")
296 SKSL_TEST(SkSLEmptyBlocksES3,                  "shared/EmptyBlocksES3.sksl")
297 SKSL_TEST(SkSLHexUnsigned,                     "shared/HexUnsigned.sksl")
298 SKSL_TEST(SkSLMatricesNonsquare,               "shared/MatricesNonsquare.sksl")
299 SKSL_TEST(SkSLOperatorsES3,                    "shared/OperatorsES3.sksl")
300 SKSL_TEST(SkSLResizeMatrixNonsquare,           "shared/ResizeMatrixNonsquare.sksl")
301 SKSL_TEST(SkSLReturnsValueOnEveryPathES3,      "shared/ReturnsValueOnEveryPathES3.sksl")
302 SKSL_TEST(SkSLScalarConversionConstructorsES3, "shared/ScalarConversionConstructorsES3.sksl")
303 SKSL_TEST(SkSLSwizzleByIndex,                  "shared/SwizzleByIndex.sksl")
304 SKSL_TEST(SkSLWhileLoopControlFlow,            "shared/WhileLoopControlFlow.sksl")
305 */
306