Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/effects/GrSkSLFP.h" |
Robert Phillips | 1efecea | 2019-02-15 16:58:40 -0500 | [diff] [blame] | 9 | |
Brian Osman | ee426f2 | 2020-01-02 11:55:24 -0500 | [diff] [blame] | 10 | #include "include/effects/SkRuntimeEffect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrTexture.h" |
| 12 | #include "include/private/GrContext_Base.h" |
| 13 | #include "src/gpu/GrBaseContextPriv.h" |
| 14 | #include "src/sksl/SkSLUtil.h" |
Robert Phillips | 1efecea | 2019-02-15 16:58:40 -0500 | [diff] [blame] | 15 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 17 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 18 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 19 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 20 | class GrGLSLSkSLFP : public GrGLSLFragmentProcessor { |
| 21 | public: |
Brian Osman | 300fe1d | 2020-01-23 15:42:43 -0500 | [diff] [blame] | 22 | GrGLSLSkSLFP(SkSL::PipelineStageArgs&& args) : fArgs(std::move(args)) {} |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 23 | |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 24 | SkSL::String expandFormatArgs(const SkSL::String& raw, |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 25 | const EmitArgs& args, |
| 26 | const std::vector<SkSL::Compiler::FormatArg> formatArgs, |
| 27 | const char* coordsName, |
| 28 | const std::vector<SkString>& childNames) { |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 29 | SkSL::String result; |
| 30 | int substringStartIndex = 0; |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 31 | int formatArgIndex = 0; |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 32 | for (size_t i = 0; i < raw.length(); ++i) { |
| 33 | char c = raw[i]; |
| 34 | if (c == '%') { |
| 35 | result += SkSL::StringFragment(raw.c_str() + substringStartIndex, |
| 36 | i - substringStartIndex); |
| 37 | ++i; |
| 38 | c = raw[i]; |
| 39 | switch (c) { |
| 40 | case 's': { |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 41 | const SkSL::Compiler::FormatArg& arg = formatArgs[formatArgIndex++]; |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 42 | switch (arg.fKind) { |
| 43 | case SkSL::Compiler::FormatArg::Kind::kInput: |
| 44 | result += args.fInputColor; |
| 45 | break; |
| 46 | case SkSL::Compiler::FormatArg::Kind::kOutput: |
| 47 | result += args.fOutputColor; |
| 48 | break; |
| 49 | case SkSL::Compiler::FormatArg::Kind::kCoordX: |
| 50 | result += coordsName; |
| 51 | result += ".x"; |
| 52 | break; |
| 53 | case SkSL::Compiler::FormatArg::Kind::kCoordY: |
| 54 | result += coordsName; |
| 55 | result += ".y"; |
| 56 | break; |
| 57 | case SkSL::Compiler::FormatArg::Kind::kUniform: |
| 58 | result += args.fUniformHandler->getUniformCStr( |
| 59 | fUniformHandles[arg.fIndex]); |
| 60 | break; |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 61 | case SkSL::Compiler::FormatArg::Kind::kChildProcessor: |
| 62 | result += childNames[arg.fIndex].c_str(); |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 63 | break; |
| 64 | case SkSL::Compiler::FormatArg::Kind::kFunctionName: |
| 65 | SkASSERT((int) fFunctionNames.size() > arg.fIndex); |
| 66 | result += fFunctionNames[arg.fIndex].c_str(); |
| 67 | break; |
| 68 | } |
| 69 | break; |
| 70 | } |
| 71 | default: |
| 72 | result += c; |
| 73 | } |
| 74 | substringStartIndex = i + 1; |
| 75 | } |
| 76 | } |
| 77 | result += SkSL::StringFragment(raw.c_str() + substringStartIndex, |
| 78 | raw.length() - substringStartIndex); |
| 79 | return result; |
| 80 | } |
| 81 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 82 | void emitCode(EmitArgs& args) override { |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 83 | const GrSkSLFP& fp = args.fFp.cast<GrSkSLFP>(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 84 | for (const auto& v : fp.fEffect->inputs()) { |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 85 | if (v.fQualifier == SkRuntimeEffect::Variable::Qualifier::kUniform) { |
| 86 | auto handle = args.fUniformHandler->addUniformArray(kFragment_GrShaderFlag, |
| 87 | v.fGPUType, |
| 88 | v.fName.c_str(), |
| 89 | v.isArray() ? v.fCount : 0); |
| 90 | fUniformHandles.push_back(handle); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 91 | } |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 92 | } |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 93 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Brian Osman | fd7fd96 | 2020-01-28 00:27:41 +0000 | [diff] [blame] | 94 | SkString coords = args.fTransformedCoords.count() |
| 95 | ? fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint) |
| 96 | : SkString("sk_FragCoord"); |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 97 | std::vector<SkString> childNames; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 98 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 99 | childNames.push_back(this->invokeChild(i, args)); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 100 | } |
Brian Osman | 300fe1d | 2020-01-23 15:42:43 -0500 | [diff] [blame] | 101 | for (const auto& f : fArgs.fFunctions) { |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 102 | fFunctionNames.emplace_back(); |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 103 | SkSL::String body = this->expandFormatArgs(f.fBody.c_str(), args, f.fFormatArgs, |
| 104 | coords.c_str(), childNames); |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 105 | fragBuilder->emitFunction(f.fReturnType, |
| 106 | f.fName.c_str(), |
| 107 | f.fParameters.size(), |
| 108 | f.fParameters.data(), |
| 109 | body.c_str(), |
| 110 | &fFunctionNames.back()); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 111 | } |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 112 | fragBuilder->codeAppend(this->expandFormatArgs(fArgs.fCode.c_str(), args, fArgs.fFormatArgs, |
| 113 | coords.c_str(), childNames).c_str()); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 116 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 117 | const GrFragmentProcessor& _proc) override { |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 118 | size_t uniIndex = 0; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 119 | const GrSkSLFP& outer = _proc.cast<GrSkSLFP>(); |
| 120 | char* inputs = (char*) outer.fInputs.get(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 121 | for (const auto& v : outer.fEffect->inputs()) { |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 122 | if (v.fQualifier != SkRuntimeEffect::Variable::Qualifier::kUniform) { |
| 123 | continue; |
| 124 | } |
| 125 | |
| 126 | const float* data = reinterpret_cast<const float*>(inputs + v.fOffset); |
| 127 | switch (v.fType) { |
| 128 | case SkRuntimeEffect::Variable::Type::kFloat: |
| 129 | pdman.set1fv(fUniformHandles[uniIndex++], v.fCount, data); |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 130 | break; |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 131 | case SkRuntimeEffect::Variable::Type::kFloat2: |
| 132 | pdman.set2fv(fUniformHandles[uniIndex++], v.fCount, data); |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 133 | break; |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 134 | case SkRuntimeEffect::Variable::Type::kFloat3: |
| 135 | pdman.set3fv(fUniformHandles[uniIndex++], v.fCount, data); |
Ethan Nicholas | fc671ad | 2019-11-21 11:14:11 -0500 | [diff] [blame] | 136 | break; |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 137 | case SkRuntimeEffect::Variable::Type::kFloat4: |
| 138 | pdman.set4fv(fUniformHandles[uniIndex++], v.fCount, data); |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 139 | break; |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 140 | case SkRuntimeEffect::Variable::Type::kFloat2x2: |
| 141 | pdman.setMatrix2fv(fUniformHandles[uniIndex++], v.fCount, data); |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 142 | break; |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 143 | case SkRuntimeEffect::Variable::Type::kFloat3x3: |
| 144 | pdman.setMatrix3fv(fUniformHandles[uniIndex++], v.fCount, data); |
| 145 | break; |
| 146 | case SkRuntimeEffect::Variable::Type::kFloat4x4: |
| 147 | pdman.setMatrix4fv(fUniformHandles[uniIndex++], v.fCount, data); |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 148 | break; |
| 149 | default: |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 150 | SkDEBUGFAIL("Unsupported uniform type"); |
| 151 | break; |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame] | 152 | } |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 155 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 156 | // nearly-finished GLSL; still contains printf-style "%s" format tokens |
Brian Osman | 300fe1d | 2020-01-23 15:42:43 -0500 | [diff] [blame] | 157 | SkSL::PipelineStageArgs fArgs; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 158 | std::vector<UniformHandle> fUniformHandles; |
Brian Osman | 2e29ab5 | 2019-09-20 12:19:11 -0400 | [diff] [blame] | 159 | std::vector<SkString> fFunctionNames; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 162 | std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, sk_sp<SkRuntimeEffect> effect, |
| 163 | const char* name, const void* inputs, size_t inputSize, |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 164 | const SkMatrix* matrix) { |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 165 | return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().caps()->refShaderCaps(), |
| 166 | std::move(effect), name, inputs, inputSize, |
| 167 | matrix)); |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 168 | } |
| 169 | |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 170 | GrSkSLFP::GrSkSLFP(sk_sp<const GrShaderCaps> shaderCaps, sk_sp<SkRuntimeEffect> effect, |
| 171 | const char* name, const void* inputs, size_t inputSize, const SkMatrix* matrix) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 172 | : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags) |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 173 | , fShaderCaps(std::move(shaderCaps)) |
| 174 | , fEffect(std::move(effect)) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 175 | , fName(name) |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 176 | , fInputs(new int8_t[inputSize]) |
| 177 | , fInputSize(inputSize) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 178 | if (fInputSize) { |
| 179 | memcpy(fInputs.get(), inputs, inputSize); |
| 180 | } |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 181 | if (matrix) { |
| 182 | fCoordTransform = GrCoordTransform(*matrix); |
Brian Osman | fd7fd96 | 2020-01-28 00:27:41 +0000 | [diff] [blame] | 183 | this->addCoordTransform(&fCoordTransform); |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 184 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 185 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 186 | |
| 187 | GrSkSLFP::GrSkSLFP(const GrSkSLFP& other) |
| 188 | : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags) |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 189 | , fShaderCaps(other.fShaderCaps) |
| 190 | , fEffect(other.fEffect) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 191 | , fName(other.fName) |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 192 | , fInputs(new int8_t[other.fInputSize]) |
| 193 | , fInputSize(other.fInputSize) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 194 | if (fInputSize) { |
| 195 | memcpy(fInputs.get(), other.fInputs.get(), fInputSize); |
| 196 | } |
Brian Osman | fd7fd96 | 2020-01-28 00:27:41 +0000 | [diff] [blame] | 197 | if (other.numCoordTransforms()) { |
| 198 | fCoordTransform = other.fCoordTransform; |
| 199 | this->addCoordTransform(&fCoordTransform); |
| 200 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 201 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 202 | |
| 203 | const char* GrSkSLFP::name() const { |
| 204 | return fName; |
| 205 | } |
| 206 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 207 | void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) { |
| 208 | this->registerChildProcessor(std::move(child)); |
| 209 | } |
| 210 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 211 | GrGLSLFragmentProcessor* GrSkSLFP::onCreateGLSLInstance() const { |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 212 | // Note: This is actually SkSL (again) but with inline format specifiers. |
Brian Osman | 107c666 | 2019-12-30 15:02:30 -0500 | [diff] [blame] | 213 | SkSL::PipelineStageArgs args; |
| 214 | SkAssertResult(fEffect->toPipelineStage(fInputs.get(), fShaderCaps.get(), &args)); |
Brian Osman | 300fe1d | 2020-01-23 15:42:43 -0500 | [diff] [blame] | 215 | return new GrGLSLSkSLFP(std::move(args)); |
Greg Daniel | f410794 | 2019-12-12 01:02:48 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 218 | void GrSkSLFP::onGetGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const { |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 219 | b->add32(fEffect->index()); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 220 | char* inputs = (char*) fInputs.get(); |
Brian Osman | f72dedd | 2020-01-08 13:19:58 -0500 | [diff] [blame] | 221 | for (const auto& v : fEffect->inputs()) { |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 222 | if (v.fQualifier != SkRuntimeEffect::Variable::Qualifier::kIn) { |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 223 | continue; |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 224 | } |
Brian Osman | d927bd2 | 2019-12-18 11:23:12 -0500 | [diff] [blame] | 225 | // 'in' arrays are not supported |
| 226 | SkASSERT(!v.isArray()); |
| 227 | switch (v.fType) { |
| 228 | case SkRuntimeEffect::Variable::Type::kBool: |
| 229 | b->add32(inputs[v.fOffset]); |
| 230 | break; |
| 231 | case SkRuntimeEffect::Variable::Type::kInt: |
| 232 | case SkRuntimeEffect::Variable::Type::kFloat: |
| 233 | b->add32(*(int32_t*)(inputs + v.fOffset)); |
| 234 | break; |
| 235 | default: |
| 236 | SkDEBUGFAIL("Unsupported input variable type"); |
| 237 | break; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool GrSkSLFP::onIsEqual(const GrFragmentProcessor& other) const { |
| 243 | const GrSkSLFP& sk = other.cast<GrSkSLFP>(); |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 244 | SkASSERT(fEffect->index() != sk.fEffect->index() || fInputSize == sk.fInputSize); |
| 245 | return fEffect->index() == sk.fEffect->index() && |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 246 | !memcmp(fInputs.get(), sk.fInputs.get(), fInputSize); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | std::unique_ptr<GrFragmentProcessor> GrSkSLFP::clone() const { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 250 | std::unique_ptr<GrSkSLFP> result(new GrSkSLFP(*this)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 251 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Brian Salomon | d4bf54e | 2020-01-27 17:50:36 +0000 | [diff] [blame] | 252 | result->registerChildProcessor(this->childProcessor(i).clone()); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 253 | } |
| 254 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 255 | } |
| 256 | |
Robert Phillips | 7f11fb5 | 2019-12-03 13:35:19 -0500 | [diff] [blame] | 257 | /**************************************************************************************************/ |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 258 | |
| 259 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSkSLFP); |
| 260 | |
| 261 | #if GR_TEST_UTILS |
| 262 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 263 | #include "include/effects/SkArithmeticImageFilter.h" |
| 264 | #include "include/gpu/GrContext.h" |
| 265 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 266 | |
| 267 | extern const char* SKSL_ARITHMETIC_SRC; |
| 268 | extern const char* SKSL_DITHER_SRC; |
| 269 | extern const char* SKSL_OVERDRAW_SRC; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 270 | |
| 271 | using Value = SkSL::Program::Settings::Value; |
| 272 | |
| 273 | std::unique_ptr<GrFragmentProcessor> GrSkSLFP::TestCreate(GrProcessorTestData* d) { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 274 | int type = d->fRandom->nextULessThan(3); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 275 | switch (type) { |
| 276 | case 0: { |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 277 | static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_DITHER_SRC))); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 278 | int rangeType = d->fRandom->nextULessThan(3); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 279 | auto result = GrSkSLFP::Make(d->context(), effect, "Dither", |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 280 | &rangeType, sizeof(rangeType)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 281 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
| 282 | } |
| 283 | case 1: { |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 284 | static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_ARITHMETIC_SRC))); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 285 | ArithmeticFPInputs inputs; |
| 286 | inputs.k[0] = d->fRandom->nextF(); |
| 287 | inputs.k[1] = d->fRandom->nextF(); |
| 288 | inputs.k[2] = d->fRandom->nextF(); |
| 289 | inputs.k[3] = d->fRandom->nextF(); |
| 290 | inputs.enforcePMColor = d->fRandom->nextBool(); |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 291 | auto result = GrSkSLFP::Make(d->context(), effect, "Arithmetic", |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 292 | &inputs, sizeof(inputs)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 293 | result->addChild(GrConstColorProcessor::Make( |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 294 | SK_PMColor4fWHITE, |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 295 | GrConstColorProcessor::InputMode::kIgnore)); |
| 296 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 297 | } |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 298 | case 2: { |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 299 | static auto effect = std::get<0>(SkRuntimeEffect::Make(SkString(SKSL_OVERDRAW_SRC))); |
Brian Osman | 9d10abe | 2019-12-13 16:31:40 -0500 | [diff] [blame] | 300 | SkColor4f inputs[6]; |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 301 | for (int i = 0; i < 6; ++i) { |
Brian Osman | 9d10abe | 2019-12-13 16:31:40 -0500 | [diff] [blame] | 302 | inputs[i] = SkColor4f::FromBytes_RGBA(d->fRandom->nextU()); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 303 | } |
Brian Osman | 088913a | 2019-12-19 15:44:56 -0500 | [diff] [blame] | 304 | auto result = GrSkSLFP::Make(d->context(), effect, "Overdraw", |
Brian Osman | 7b1678a | 2019-12-16 09:17:25 -0500 | [diff] [blame] | 305 | &inputs, sizeof(inputs)); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 306 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
| 307 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 308 | } |
| 309 | SK_ABORT("unreachable"); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | #endif |