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" |
John Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 21 | #include "src/sksl/SkSLDefines.h" // for kDefaultInlineThreshold |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 22 | #include "tests/Test.h" |
| 23 | #include "tools/Resources.h" |
| 24 | #include "tools/ToolUtils.h" |
| 25 | |
| 26 | static const SkRect kRect = SkRect::MakeWH(1, 1); |
| 27 | |
John Stiles | b41d5bb | 2021-01-26 16:28:12 -0500 | [diff] [blame] | 28 | template <typename T> |
| 29 | static 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 Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 36 | static void test_one_permutation(skiatest::Reporter* r, |
| 37 | SkSurface* surface, |
| 38 | const char* testFile, |
| 39 | const char* permutationSuffix, |
| 40 | const SkRuntimeEffect::Options& options) { |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 41 | SkString resourcePath = SkStringPrintf("sksl/%s", testFile); |
| 42 | sk_sp<SkData> shaderData = GetResourceAsData(resourcePath.c_str()); |
| 43 | if (!shaderData) { |
John Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 44 | ERRORF(r, "%s%s: Unable to load file", testFile, permutationSuffix); |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | |
| 48 | SkString shaderString{reinterpret_cast<const char*>(shaderData->bytes()), shaderData->size()}; |
John Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 49 | SkRuntimeEffect::Result result = SkRuntimeEffect::Make(shaderString, options); |
| 50 | if (!result.effect) { |
| 51 | ERRORF(r, "%s%s: %s", testFile, permutationSuffix, result.errorText.c_str()); |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
John Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 55 | SkRuntimeShaderBuilder builder(result.effect); |
John Stiles | e1658b5 | 2021-01-29 11:44:13 -0500 | [diff] [blame] | 56 | 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 Stiles | 01cdf01 | 2021-02-10 09:53:41 -0500 | [diff] [blame] | 62 | 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 Stiles | e1658b5 | 2021-01-29 11:44:13 -0500 | [diff] [blame] | 67 | set_uniform(&builder, "unknownInput", 1.0f); |
John Stiles | b41d5bb | 2021-01-26 16:28:12 -0500 | [diff] [blame] | 68 | |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 69 | sk_sp<SkShader> shader = builder.makeShader(/*localMatrix=*/nullptr, /*isOpaque=*/true); |
| 70 | if (!shader) { |
John Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 71 | ERRORF(r, "%s%s: Unable to build shader", testFile, permutationSuffix); |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 72 | 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 Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 90 | static 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 Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 99 | static 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 Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 103 | test_permutations(r, surface.get(), testFile); |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static 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 Stiles | d80f966 | 2021-02-03 11:42:32 -0500 | [diff] [blame] | 110 | test_permutations(r, surface.get(), testFile); |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 111 | } |
| 112 | |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 113 | #define SKSL_TEST_CPU(name, path) \ |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 114 | DEF_TEST(name ## _CPU, r) { \ |
| 115 | test_cpu(r, path); \ |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 116 | } |
| 117 | #define SKSL_TEST_GPU(name, path) \ |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 118 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(name ## _GPU, r, ctxInfo) { \ |
| 119 | test_gpu(r, ctxInfo.directContext(), path); \ |
| 120 | } |
John Stiles | b441850 | 2021-02-04 10:57:08 -0500 | [diff] [blame] | 121 | #define SKSL_TEST(name, path) SKSL_TEST_CPU(name, path) SKSL_TEST_GPU(name, path) |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 122 | |
John Stiles | 34c098d | 2021-02-09 10:38:59 -0500 | [diff] [blame] | 123 | SKSL_TEST(SkSLAssignmentOps, "folding/AssignmentOps.sksl") |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 124 | SKSL_TEST(SkSLBoolFolding, "folding/BoolFolding.sksl") |
| 125 | SKSL_TEST(SkSLIntFoldingES2, "folding/IntFoldingES2.sksl") |
| 126 | SKSL_TEST(SkSLFloatFolding, "folding/FloatFolding.sksl") |
| 127 | SKSL_TEST(SkSLMatrixFoldingES2, "folding/MatrixFoldingES2.sksl") |
| 128 | SKSL_TEST(SkSLShortCircuitBoolFolding, "folding/ShortCircuitBoolFolding.sksl") |
| 129 | SKSL_TEST(SkSLVectorScalarFolding, "folding/VectorScalarFolding.sksl") |
| 130 | SKSL_TEST(SkSLVectorVectorFolding, "folding/VectorVectorFolding.sksl") |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 131 | |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 132 | SKSL_TEST(SkSLIntrinsicAbsFloat, "intrinsics/AbsFloat.sksl") |
| 133 | SKSL_TEST(SkSLIntrinsicCeil, "intrinsics/Ceil.sksl") |
| 134 | SKSL_TEST(SkSLIntrinsicClampFloat, "intrinsics/ClampFloat.sksl") |
| 135 | SKSL_TEST(SkSLIntrinsicMaxFloat, "intrinsics/MaxFloat.sksl") |
| 136 | SKSL_TEST(SkSLIntrinsicMinFloat, "intrinsics/MinFloat.sksl") |
| 137 | SKSL_TEST(SkSLIntrinsicMixFloat, "intrinsics/MixFloat.sksl") |
| 138 | SKSL_TEST(SkSLIntrinsicSignFloat, "intrinsics/SignFloat.sksl") |
John Stiles | e1658b5 | 2021-01-29 11:44:13 -0500 | [diff] [blame] | 139 | |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 140 | SKSL_TEST(SkSLArrayTypes, "shared/ArrayTypes.sksl") |
| 141 | SKSL_TEST(SkSLAssignment, "shared/Assignment.sksl") |
| 142 | SKSL_TEST(SkSLCastsRoundTowardZero, "shared/CastsRoundTowardZero.sksl") |
| 143 | SKSL_TEST(SkSLCommaMixedTypes, "shared/CommaMixedTypes.sksl") |
| 144 | SKSL_TEST(SkSLCommaSideEffects, "shared/CommaSideEffects.sksl") |
| 145 | SKSL_TEST(SkSLConstantIf, "shared/ConstantIf.sksl") |
| 146 | SKSL_TEST(SkSLConstVariableComparison, "shared/ConstVariableComparison.sksl") |
| 147 | SKSL_TEST(SkSLDeadIfStatement, "shared/DeadIfStatement.sksl") |
| 148 | SKSL_TEST(SkSLDeadStripFunctions, "shared/DeadStripFunctions.sksl") |
| 149 | SKSL_TEST(SkSLDependentInitializers, "shared/DependentInitializers.sksl") |
| 150 | SKSL_TEST(SkSLEmptyBlocksES2, "shared/EmptyBlocksES2.sksl") |
| 151 | SKSL_TEST(SkSLForLoopControlFlow, "shared/ForLoopControlFlow.sksl") |
| 152 | SKSL_TEST(SkSLFunctionArgTypeMatch, "shared/FunctionArgTypeMatch.sksl") |
| 153 | SKSL_TEST(SkSLFunctionReturnTypeMatch, "shared/FunctionReturnTypeMatch.sksl") |
| 154 | SKSL_TEST(SkSLFunctions, "shared/Functions.sksl") |
| 155 | SKSL_TEST(SkSLGeometricIntrinsics, "shared/GeometricIntrinsics.sksl") |
| 156 | SKSL_TEST(SkSLHelloWorld, "shared/HelloWorld.sksl") |
| 157 | SKSL_TEST(SkSLHex, "shared/Hex.sksl") |
| 158 | SKSL_TEST(SkSLMatrices, "shared/Matrices.sksl") |
| 159 | SKSL_TEST(SkSLMultipleAssignments, "shared/MultipleAssignments.sksl") |
| 160 | SKSL_TEST(SkSLNegatedVectorLiteral, "shared/NegatedVectorLiteral.sksl") |
| 161 | SKSL_TEST(SkSLNumberCasts, "shared/NumberCasts.sksl") |
| 162 | SKSL_TEST(SkSLOperatorsES2, "shared/OperatorsES2.sksl") |
| 163 | SKSL_TEST(SkSLOutParams, "shared/OutParams.sksl") |
| 164 | SKSL_TEST(SkSLOutParamsTricky, "shared/OutParamsTricky.sksl") |
| 165 | SKSL_TEST(SkSLResizeMatrix, "shared/ResizeMatrix.sksl") |
| 166 | SKSL_TEST(SkSLScalarConversionConstructorsES2, "shared/ScalarConversionConstructorsES2.sksl") |
| 167 | SKSL_TEST(SkSLStackingVectorCasts, "shared/StackingVectorCasts.sksl") |
| 168 | SKSL_TEST(SkSLStaticIf, "shared/StaticIf.sksl") |
| 169 | SKSL_TEST(SkSLSwizzleBoolConstants, "shared/SwizzleBoolConstants.sksl") |
John Stiles | c2c1b0c | 2021-02-05 16:24:03 -0500 | [diff] [blame] | 170 | SKSL_TEST(SkSLSwizzleByConstantIndex, "shared/SwizzleByConstantIndex.sksl") |
| 171 | SKSL_TEST(SkSLSwizzleConstants, "shared/SwizzleConstants.sksl") |
| 172 | SKSL_TEST(SkSLSwizzleLTRB, "shared/SwizzleLTRB.sksl") |
| 173 | SKSL_TEST(SkSLSwizzleOpt, "shared/SwizzleOpt.sksl") |
| 174 | SKSL_TEST(SkSLSwizzleScalar, "shared/SwizzleScalar.sksl") |
John Stiles | ecd7c22 | 2021-02-08 14:20:28 -0500 | [diff] [blame] | 175 | SKSL_TEST(SkSLTernaryAsLValueEntirelyFoldable, "shared/TernaryAsLValueEntirelyFoldable.sksl") |
| 176 | SKSL_TEST(SkSLTernaryAsLValueFoldableTest, "shared/TernaryAsLValueFoldableTest.sksl") |
| 177 | SKSL_TEST(SkSLUnaryPositiveNegative, "shared/UnaryPositiveNegative.sksl") |
| 178 | SKSL_TEST(SkSLUnusedVariables, "shared/UnusedVariables.sksl") |
| 179 | SKSL_TEST(SkSLVectorConstructors, "shared/VectorConstructors.sksl") |
John Stiles | 7b92897 | 2021-01-27 09:56:04 -0500 | [diff] [blame] | 180 | |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 181 | /* |
John Stiles | ecd2036 | 2021-02-03 17:44:47 -0500 | [diff] [blame] | 182 | // Incompatible with Runtime Effects because calling a function before its definition is disallowed. |
| 183 | // (This was done to prevent recursion, as required by ES2.) |
| 184 | SKSL_TEST(SkSLFunctionPrototype, "shared/FunctionPrototype.sksl") |
John Stiles | e1658b5 | 2021-01-29 11:44:13 -0500 | [diff] [blame] | 185 | */ |
| 186 | |
| 187 | /* |
Brian Osman | bf7b4b8 | 2021-01-27 13:53:43 -0500 | [diff] [blame] | 188 | TODO(skia:10939): enable this test when Runtime Effects supports structs in function signatures |
| 189 | SKSL_TEST(SkSLStructsInFunctions, "shared/StructsInFunctions.sksl") |
| 190 | */ |
| 191 | |
| 192 | /* |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 193 | TODO(skia:11209): enable these tests when Runtime Effects have support for ES3 |
| 194 | |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 195 | SKSL_TEST(SkSLIntFoldingES3, "folding/IntFoldingES3.sksl") |
| 196 | SKSL_TEST(SkSLMatrixFoldingES3, "folding/MatrixFoldingES3.sksl") |
John Stiles | 7b92897 | 2021-01-27 09:56:04 -0500 | [diff] [blame] | 197 | |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 198 | SKSL_TEST(SkSLIntrinsicAbsInt, "intrinsics/AbsInt.sksl") |
| 199 | SKSL_TEST(SkSLIntrinsicClampInt, "intrinsics/ClampInt.sksl") |
| 200 | SKSL_TEST(SkSLIntrinsicMaxInt, "intrinsics/MaxInt.sksl") |
| 201 | SKSL_TEST(SkSLIntrinsicMinInt, "intrinsics/MinInt.sksl") |
| 202 | SKSL_TEST(SkSLIntrinsicMixBool, "intrinsics/MixBool.sksl") |
| 203 | SKSL_TEST(SkSLIntrinsicSignInt, "intrinsics/SignInt.sksl") |
John Stiles | e1658b5 | 2021-01-29 11:44:13 -0500 | [diff] [blame] | 204 | |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 205 | SKSL_TEST(SkSLArrayConstructors, "shared/ArrayConstructors.sksl") |
| 206 | SKSL_TEST(SkSLDeadLoopVariable, "shared/DeadLoopVariable.sksl") |
| 207 | SKSL_TEST(SkSLDoWhileControlFlow, "shared/DoWhileControlFlow.sksl") |
| 208 | SKSL_TEST(SkSLEmptyBlocksES3, "shared/EmptyBlocksES3.sksl") |
| 209 | SKSL_TEST(SkSLHexUnsigned, "shared/HexUnsigned.sksl") |
| 210 | SKSL_TEST(SkSLMatricesNonsquare, "shared/MatricesNonsquare.sksl") |
| 211 | SKSL_TEST(SkSLOperatorsES3, "shared/OperatorsES3.sksl") |
| 212 | SKSL_TEST(SkSLResizeMatrixNonsquare, "shared/ResizeMatrixNonsquare.sksl") |
| 213 | SKSL_TEST(SkSLScalarConversionConstructorsES3, "shared/ScalarConversionConstructorsES3.sksl") |
John Stiles | c2c1b0c | 2021-02-05 16:24:03 -0500 | [diff] [blame] | 214 | SKSL_TEST(SkSLSwizzleByIndex, "shared/SwizzleByIndex.sksl") |
John Stiles | d437478 | 2021-02-05 09:31:21 -0500 | [diff] [blame] | 215 | SKSL_TEST(SkSLWhileLoopControlFlow, "shared/WhileLoopControlFlow.sksl") |
John Stiles | 232dd2b | 2021-01-25 10:57:47 -0500 | [diff] [blame] | 216 | */ |