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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/gpu/GrTexture.h" |
| 11 | #include "include/private/GrContext_Base.h" |
| 12 | #include "src/gpu/GrBaseContextPriv.h" |
| 13 | #include "src/sksl/SkSLUtil.h" |
Robert Phillips | 1efecea | 2019-02-15 16:58:40 -0500 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLFragmentProcessor.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 17 | #include "src/gpu/glsl/GrGLSLProgramBuilder.h" |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 18 | |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 19 | GrSkSLFPFactory::GrSkSLFPFactory(const char* name, const GrShaderCaps* shaderCaps, const char* sksl, |
| 20 | SkSL::Program::Kind kind) |
| 21 | : fKind(kind) |
| 22 | , fName(name) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 23 | SkSL::Program::Settings settings; |
| 24 | settings.fCaps = shaderCaps; |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 25 | fBaseProgram = fCompiler.convertProgram(fKind, SkSL::String(sksl), settings); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 26 | if (fCompiler.errorCount()) { |
| 27 | SkDebugf("%s\n", fCompiler.errorText().c_str()); |
| 28 | } |
| 29 | SkASSERT(fBaseProgram); |
| 30 | SkASSERT(!fCompiler.errorCount()); |
| 31 | for (const auto& e : *fBaseProgram) { |
| 32 | if (e.fKind == SkSL::ProgramElement::kVar_Kind) { |
| 33 | SkSL::VarDeclarations& v = (SkSL::VarDeclarations&) e; |
| 34 | for (const auto& varStatement : v.fVars) { |
| 35 | const SkSL::Variable& var = *((SkSL::VarDeclaration&) *varStatement).fVar; |
| 36 | if (var.fModifiers.fFlags & SkSL::Modifiers::kIn_Flag) { |
| 37 | fInputVars.push_back(&var); |
| 38 | } |
| 39 | if (var.fModifiers.fLayout.fKey) { |
| 40 | fKeyVars.push_back(&var); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | const SkSL::Program* GrSkSLFPFactory::getSpecialization(const SkSL::String& key, const void* inputs, |
| 48 | size_t inputSize) { |
| 49 | const auto& found = fSpecializations.find(key); |
| 50 | if (found != fSpecializations.end()) { |
| 51 | return found->second.get(); |
| 52 | } |
| 53 | |
| 54 | std::unordered_map<SkSL::String, SkSL::Program::Settings::Value> inputMap; |
| 55 | size_t offset = 0; |
| 56 | for (const auto& v : fInputVars) { |
| 57 | SkSL::String name(v->fName); |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 58 | if (&v->fType == fCompiler.context().fInt_Type.get() || |
| 59 | &v->fType == fCompiler.context().fShort_Type.get()) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 60 | offset = SkAlign4(offset); |
| 61 | int32_t v = *(int32_t*) (((uint8_t*) inputs) + offset); |
| 62 | inputMap.insert(std::make_pair(name, SkSL::Program::Settings::Value(v))); |
| 63 | offset += sizeof(int32_t); |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 64 | } else if (&v->fType == fCompiler.context().fFloat_Type.get() || |
| 65 | &v->fType == fCompiler.context().fHalf_Type.get()) { |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 66 | offset = SkAlign4(offset); |
| 67 | float v = *(float*) (((uint8_t*) inputs) + offset); |
| 68 | inputMap.insert(std::make_pair(name, SkSL::Program::Settings::Value(v))); |
| 69 | offset += sizeof(float); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 70 | } else if (&v->fType == fCompiler.context().fBool_Type.get()) { |
| 71 | bool v = *(((bool*) inputs) + offset); |
| 72 | inputMap.insert(std::make_pair(name, SkSL::Program::Settings::Value(v))); |
| 73 | offset += sizeof(bool); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 74 | } else if (&v->fType == fCompiler.context().fFloat4_Type.get() || |
| 75 | &v->fType == fCompiler.context().fHalf4_Type.get()) { |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 76 | offset = SkAlign4(offset) + sizeof(float) * 4; |
| 77 | } else if (&v->fType == fCompiler.context().fFragmentProcessor_Type.get()) { |
| 78 | // do nothing |
| 79 | } else { |
| 80 | printf("can't handle input var: %s\n", SkSL::String(v->fType.fName).c_str()); |
| 81 | SkASSERT(false); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 82 | } |
| 83 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 84 | |
| 85 | std::unique_ptr<SkSL::Program> specialized = fCompiler.specialize(*fBaseProgram, inputMap); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 86 | bool optimized = fCompiler.optimize(*specialized); |
| 87 | if (!optimized) { |
| 88 | SkDebugf("%s\n", fCompiler.errorText().c_str()); |
| 89 | SkASSERT(false); |
| 90 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 91 | const SkSL::Program* result = specialized.get(); |
| 92 | fSpecializations.insert(std::make_pair(key, std::move(specialized))); |
| 93 | return result; |
| 94 | } |
| 95 | |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 96 | static SkSL::Layout::CType get_ctype(const SkSL::Context& context, const SkSL::Variable& v) { |
| 97 | SkSL::Layout::CType result = v.fModifiers.fLayout.fCType; |
| 98 | if (result == SkSL::Layout::CType::kDefault) { |
| 99 | if (&v.fType == context.fFloat_Type.get()) { |
| 100 | result = SkSL::Layout::CType::kFloat; |
| 101 | } else if (&v.fType == context.fFloat4_Type.get()) { |
| 102 | result = SkSL::Layout::CType::kSkRect; |
| 103 | } else if (&v.fType == context.fHalf4_Type.get()) { |
| 104 | result = SkSL::Layout::CType::kSkPMColor; |
| 105 | } else if (&v.fType == context.fInt_Type.get()) { |
| 106 | result = SkSL::Layout::CType::kInt32; |
| 107 | } else if (&v.fType == context.fBool_Type.get()) { |
| 108 | result = SkSL::Layout::CType::kBool; |
| 109 | } else { |
| 110 | return SkSL::Layout::CType::kDefault; |
| 111 | } |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 116 | class GrGLSLSkSLFP : public GrGLSLFragmentProcessor { |
| 117 | public: |
| 118 | GrGLSLSkSLFP(const SkSL::Context* context, const std::vector<const SkSL::Variable*>* inputVars, |
| 119 | SkSL::String glsl, std::vector<SkSL::Compiler::FormatArg> formatArgs) |
| 120 | : fContext(*context) |
| 121 | , fInputVars(*inputVars) |
| 122 | , fGLSL(glsl) |
| 123 | , fFormatArgs(formatArgs) {} |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 124 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 125 | GrSLType uniformType(const SkSL::Type& type) { |
| 126 | if (type == *fContext.fFloat_Type) { |
| 127 | return kFloat_GrSLType; |
| 128 | } else if (type == *fContext.fHalf_Type) { |
| 129 | return kHalf_GrSLType; |
| 130 | } else if (type == *fContext.fFloat2_Type) { |
| 131 | return kFloat2_GrSLType; |
| 132 | } else if (type == *fContext.fHalf2_Type) { |
| 133 | return kHalf2_GrSLType; |
| 134 | } else if (type == *fContext.fFloat4_Type) { |
| 135 | return kFloat4_GrSLType; |
| 136 | } else if (type == *fContext.fHalf4_Type) { |
| 137 | return kHalf4_GrSLType; |
| 138 | } else if (type == *fContext.fFloat4x4_Type) { |
| 139 | return kFloat4x4_GrSLType; |
| 140 | } else if (type == *fContext.fHalf4x4_Type) { |
| 141 | return kHalf4x4_GrSLType; |
| 142 | } else if (type == *fContext.fBool_Type) { |
| 143 | return kBool_GrSLType; |
| 144 | } else if (type == *fContext.fInt_Type) { |
| 145 | return kInt_GrSLType; |
| 146 | } |
| 147 | printf("%s\n", SkSL::String(type.fName).c_str()); |
| 148 | SK_ABORT("unsupported uniform type"); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 149 | return kFloat_GrSLType; |
| 150 | } |
| 151 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 152 | void emitCode(EmitArgs& args) override { |
| 153 | for (const auto& v : fInputVars) { |
| 154 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag && v->fType != |
| 155 | *fContext.fFragmentProcessor_Type) { |
| 156 | fUniformHandles.push_back(args.fUniformHandler->addUniform( |
| 157 | kFragment_GrShaderFlag, |
| 158 | this->uniformType(v->fType), |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 159 | SkSL::String(v->fName).c_str())); |
| 160 | } |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 161 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 162 | std::vector<SkString> childNames; |
| 163 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 164 | childNames.push_back(SkStringPrintf("_child%d", i)); |
| 165 | this->emitChild(i, &childNames[i], args); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 166 | } |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame^] | 167 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 168 | int substringStartIndex = 0; |
| 169 | int formatArgIndex = 0; |
| 170 | for (size_t i = 0; i < fGLSL.length(); ++i) { |
| 171 | char c = fGLSL[i]; |
| 172 | if (c == '%') { |
| 173 | fragBuilder->codeAppend(fGLSL.c_str() + substringStartIndex, |
| 174 | i - substringStartIndex); |
| 175 | ++i; |
| 176 | c = fGLSL[i]; |
| 177 | switch (c) { |
| 178 | case 's': { |
| 179 | SkSL::Compiler::FormatArg& arg = fFormatArgs[formatArgIndex++]; |
| 180 | switch (arg.fKind) { |
| 181 | case SkSL::Compiler::FormatArg::Kind::kInput: |
| 182 | fragBuilder->codeAppend(args.fInputColor); |
| 183 | break; |
| 184 | case SkSL::Compiler::FormatArg::Kind::kOutput: |
| 185 | fragBuilder->codeAppend(args.fOutputColor); |
| 186 | break; |
| 187 | case SkSL::Compiler::FormatArg::Kind::kUniform: |
| 188 | fragBuilder->codeAppend(args.fUniformHandler->getUniformCStr( |
| 189 | fUniformHandles[arg.fIndex])); |
| 190 | break; |
| 191 | case SkSL::Compiler::FormatArg::Kind::kChildProcessor: |
| 192 | fragBuilder->codeAppend(childNames[arg.fIndex].c_str()); |
| 193 | break; |
| 194 | } |
| 195 | break; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 196 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 197 | default: |
| 198 | fragBuilder->codeAppendf("%c", c); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 199 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 200 | substringStartIndex = i + 1; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 201 | } |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 202 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 203 | fragBuilder->codeAppend(fGLSL.c_str() + substringStartIndex, |
| 204 | fGLSL.length() - substringStartIndex); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 205 | } |
| 206 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 207 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 208 | const GrFragmentProcessor& _proc) override { |
| 209 | size_t uniformIndex = 0; |
| 210 | size_t offset = 0; |
| 211 | const GrSkSLFP& outer = _proc.cast<GrSkSLFP>(); |
| 212 | char* inputs = (char*) outer.fInputs.get(); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 213 | for (const auto& v : outer.fFactory->fInputVars) { |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 214 | switch (get_ctype(fContext, *v)) { |
| 215 | case SkSL::Layout::CType::kSkPMColor: { |
| 216 | float f1 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 217 | float f2 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 218 | float f3 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 219 | float f4 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 220 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 221 | pdman.set4f(fUniformHandles[uniformIndex++], f1, f2, f3, f4); |
| 222 | } |
| 223 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 224 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 225 | case SkSL::Layout::CType::kSkRect: { |
| 226 | offset = SkAlign4(offset); |
| 227 | float f1 = *(float*) (inputs + offset); |
| 228 | offset += sizeof(float); |
| 229 | float f2 = *(float*) (inputs + offset); |
| 230 | offset += sizeof(float); |
| 231 | float f3 = *(float*) (inputs + offset); |
| 232 | offset += sizeof(float); |
| 233 | float f4 = *(float*) (inputs + offset); |
| 234 | offset += sizeof(float); |
| 235 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 236 | pdman.set4f(fUniformHandles[uniformIndex++], f1, f2, f3, f4); |
| 237 | } |
| 238 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 239 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 240 | case SkSL::Layout::CType::kInt32: { |
| 241 | int32_t i = *(int32_t*) (inputs + offset); |
| 242 | offset += sizeof(int32_t); |
| 243 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 244 | pdman.set1i(fUniformHandles[uniformIndex++], i); |
| 245 | } |
| 246 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 247 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 248 | case SkSL::Layout::CType::kFloat: { |
| 249 | float f = *(float*) (inputs + offset); |
| 250 | offset += sizeof(float); |
| 251 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 252 | pdman.set1f(fUniformHandles[uniformIndex++], f); |
| 253 | } |
| 254 | break; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 255 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 256 | case SkSL::Layout::CType::kBool: |
| 257 | SkASSERT(!(v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag)); |
| 258 | ++offset; |
| 259 | break; |
| 260 | default: |
| 261 | SkASSERT(&v->fType == fContext.fFragmentProcessor_Type.get()); |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame] | 262 | } |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame] | 263 | } |
| 264 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 265 | |
| 266 | const SkSL::Context& fContext; |
| 267 | const std::vector<const SkSL::Variable*>& fInputVars; |
| 268 | // nearly-finished GLSL; still contains printf-style "%s" format tokens |
| 269 | const SkSL::String fGLSL; |
| 270 | std::vector<SkSL::Compiler::FormatArg> fFormatArgs; |
| 271 | std::vector<UniformHandle> fUniformHandles; |
| 272 | }; |
| 273 | |
Robert Phillips | 1efecea | 2019-02-15 16:58:40 -0500 | [diff] [blame] | 274 | std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, int index, const char* name, |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 275 | const char* sksl, const void* inputs, |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 276 | size_t inputSize, SkSL::Program::Kind kind) { |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 277 | return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(), |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 278 | context->priv().caps()->shaderCaps(), |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 279 | kind, index, name, sksl, SkString(), |
| 280 | inputs, inputSize)); |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, int index, const char* name, |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 284 | SkString sksl, const void* inputs, size_t inputSize, |
| 285 | SkSL::Program::Kind kind) { |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 286 | return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(), |
| 287 | context->priv().caps()->shaderCaps(), |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 288 | kind, index, name, nullptr, std::move(sksl), |
| 289 | inputs, inputSize)); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 292 | GrSkSLFP::GrSkSLFP(sk_sp<GrSkSLFPFactoryCache> factoryCache, const GrShaderCaps* shaderCaps, |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 293 | SkSL::Program::Kind kind, int index, const char* name, const char* sksl, |
| 294 | SkString skslString, const void* inputs, size_t inputSize) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 295 | : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags) |
| 296 | , fFactoryCache(factoryCache) |
| 297 | , fShaderCaps(sk_ref_sp(shaderCaps)) |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 298 | , fKind(kind) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 299 | , fIndex(index) |
| 300 | , fName(name) |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 301 | , fSkSLString(skslString) |
| 302 | , fSkSL(sksl ? sksl : fSkSLString.c_str()) |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 303 | , fInputs(new int8_t[inputSize]) |
| 304 | , fInputSize(inputSize) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 305 | if (fInputSize) { |
| 306 | memcpy(fInputs.get(), inputs, inputSize); |
| 307 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 308 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 309 | |
| 310 | GrSkSLFP::GrSkSLFP(const GrSkSLFP& other) |
| 311 | : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags) |
| 312 | , fFactoryCache(other.fFactoryCache) |
| 313 | , fShaderCaps(other.fShaderCaps) |
| 314 | , fFactory(other.fFactory) |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 315 | , fKind(other.fKind) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 316 | , fIndex(other.fIndex) |
| 317 | , fName(other.fName) |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 318 | , fSkSLString(other.fSkSLString) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 319 | , fSkSL(other.fSkSL) |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 320 | , fInputs(new int8_t[other.fInputSize]) |
| 321 | , fInputSize(other.fInputSize) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 322 | if (fInputSize) { |
| 323 | memcpy(fInputs.get(), other.fInputs.get(), fInputSize); |
| 324 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 325 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 326 | |
| 327 | const char* GrSkSLFP::name() const { |
| 328 | return fName; |
| 329 | } |
| 330 | |
| 331 | void GrSkSLFP::createFactory() const { |
| 332 | if (!fFactory) { |
| 333 | fFactory = fFactoryCache->get(fIndex); |
| 334 | if (!fFactory) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 335 | fFactory = sk_sp<GrSkSLFPFactory>(new GrSkSLFPFactory(fName, fShaderCaps.get(), fSkSL, |
| 336 | fKind)); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 337 | fFactoryCache->set(fIndex, fFactory); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 342 | void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) { |
| 343 | this->registerChildProcessor(std::move(child)); |
| 344 | } |
| 345 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 346 | GrGLSLFragmentProcessor* GrSkSLFP::onCreateGLSLInstance() const { |
| 347 | this->createFactory(); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 348 | const SkSL::Program* specialized = fFactory->getSpecialization(fKey, fInputs.get(), fInputSize); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 349 | SkSL::String glsl; |
| 350 | std::vector<SkSL::Compiler::FormatArg> formatArgs; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 351 | if (!fFactory->fCompiler.toPipelineStage(*specialized, &glsl, &formatArgs)) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 352 | printf("%s\n", fFactory->fCompiler.errorText().c_str()); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 353 | SkASSERT(false); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 354 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 355 | return new GrGLSLSkSLFP(specialized->fContext.get(), &fFactory->fInputVars, glsl, formatArgs); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void GrSkSLFP::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 359 | GrProcessorKeyBuilder* b) const { |
| 360 | this->createFactory(); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 361 | b->add32(fIndex); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 362 | size_t offset = 0; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 363 | char* inputs = (char*) fInputs.get(); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 364 | const SkSL::Context& context = fFactory->fCompiler.context(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 365 | for (const auto& v : fFactory->fInputVars) { |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 366 | if (&v->fType == context.fFragmentProcessor_Type.get()) { |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 367 | continue; |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 368 | } |
| 369 | switch (get_ctype(context, *v)) { |
| 370 | case SkSL::Layout::CType::kBool: |
| 371 | if (v->fModifiers.fLayout.fKey) { |
| 372 | fKey += inputs[offset]; |
| 373 | b->add32(inputs[offset]); |
| 374 | } |
| 375 | ++offset; |
| 376 | break; |
| 377 | case SkSL::Layout::CType::kInt32: { |
| 378 | offset = SkAlign4(offset); |
| 379 | if (v->fModifiers.fLayout.fKey) { |
| 380 | fKey += inputs[offset + 0]; |
| 381 | fKey += inputs[offset + 1]; |
| 382 | fKey += inputs[offset + 2]; |
| 383 | fKey += inputs[offset + 3]; |
| 384 | b->add32(*(int32_t*) (inputs + offset)); |
| 385 | } |
| 386 | offset += sizeof(int32_t); |
| 387 | break; |
| 388 | } |
| 389 | case SkSL::Layout::CType::kFloat: { |
| 390 | offset = SkAlign4(offset); |
| 391 | if (v->fModifiers.fLayout.fKey) { |
| 392 | fKey += inputs[offset + 0]; |
| 393 | fKey += inputs[offset + 1]; |
| 394 | fKey += inputs[offset + 2]; |
| 395 | fKey += inputs[offset + 3]; |
| 396 | b->add32(*(float*) (inputs + offset)); |
| 397 | } |
| 398 | offset += sizeof(float); |
| 399 | break; |
| 400 | } |
| 401 | case SkSL::Layout::CType::kSkPMColor: // fall through |
| 402 | case SkSL::Layout::CType::kSkRect: |
| 403 | if (v->fModifiers.fLayout.fKey) { |
| 404 | for (size_t i = 0; i < sizeof(float) * 4; ++i) { |
| 405 | fKey += inputs[offset + i]; |
| 406 | } |
| 407 | b->add32(*(int32_t*) (inputs + offset)); |
| 408 | offset += sizeof(float); |
| 409 | b->add32(*(int32_t*) (inputs + offset)); |
| 410 | offset += sizeof(float); |
| 411 | b->add32(*(int32_t*) (inputs + offset)); |
| 412 | offset += sizeof(float); |
| 413 | b->add32(*(int32_t*) (inputs + offset)); |
| 414 | offset += sizeof(float); |
| 415 | } else { |
| 416 | offset += sizeof(float) * 4; |
| 417 | } |
| 418 | break; |
| 419 | default: |
| 420 | // unsupported input var type |
| 421 | printf("%s\n", SkSL::String(v->fType.fName).c_str()); |
| 422 | SkASSERT(false); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 423 | } |
| 424 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | bool GrSkSLFP::onIsEqual(const GrFragmentProcessor& other) const { |
| 428 | const GrSkSLFP& sk = other.cast<GrSkSLFP>(); |
| 429 | SkASSERT(fIndex != sk.fIndex || fInputSize == sk.fInputSize); |
| 430 | return fIndex == sk.fIndex && |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 431 | !memcmp(fInputs.get(), sk.fInputs.get(), fInputSize); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | std::unique_ptr<GrFragmentProcessor> GrSkSLFP::clone() const { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 435 | std::unique_ptr<GrSkSLFP> result(new GrSkSLFP(*this)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 436 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 437 | result->registerChildProcessor(this->childProcessor(i).clone()); |
| 438 | } |
| 439 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // We have to do a bit of manual refcounting in the cache methods below. Ideally, we could just |
| 443 | // define fFactories to contain sk_sp<GrSkSLFPFactory> rather than GrSkSLFPFactory*, but that would |
| 444 | // require GrContext to include GrSkSLFP, which creates much bigger headaches than a few manual |
| 445 | // refcounts. |
| 446 | |
| 447 | sk_sp<GrSkSLFPFactory> GrSkSLFPFactoryCache::get(int index) { |
| 448 | if (index >= (int) fFactories.size()) { |
| 449 | return nullptr; |
| 450 | } |
| 451 | GrSkSLFPFactory* result = fFactories[index]; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 452 | SkSafeRef(result); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 453 | return sk_sp<GrSkSLFPFactory>(result); |
| 454 | } |
| 455 | |
| 456 | void GrSkSLFPFactoryCache::set(int index, sk_sp<GrSkSLFPFactory> factory) { |
| 457 | while (index >= (int) fFactories.size()) { |
| 458 | fFactories.emplace_back(); |
| 459 | } |
| 460 | factory->ref(); |
| 461 | SkASSERT(!fFactories[index]); |
| 462 | fFactories[index] = factory.get(); |
| 463 | } |
| 464 | |
| 465 | GrSkSLFPFactoryCache::~GrSkSLFPFactoryCache() { |
| 466 | for (GrSkSLFPFactory* factory : fFactories) { |
| 467 | if (factory) { |
| 468 | factory->unref(); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSkSLFP); |
| 474 | |
| 475 | #if GR_TEST_UTILS |
| 476 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 477 | #include "include/effects/SkArithmeticImageFilter.h" |
| 478 | #include "include/gpu/GrContext.h" |
| 479 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 480 | |
| 481 | extern const char* SKSL_ARITHMETIC_SRC; |
| 482 | extern const char* SKSL_DITHER_SRC; |
| 483 | extern const char* SKSL_OVERDRAW_SRC; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 484 | |
| 485 | using Value = SkSL::Program::Settings::Value; |
| 486 | |
| 487 | std::unique_ptr<GrFragmentProcessor> GrSkSLFP::TestCreate(GrProcessorTestData* d) { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 488 | int type = d->fRandom->nextULessThan(3); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 489 | switch (type) { |
| 490 | case 0: { |
| 491 | static int ditherIndex = NewIndex(); |
| 492 | int rangeType = d->fRandom->nextULessThan(3); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 493 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), ditherIndex, "Dither", |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 494 | SKSL_DITHER_SRC, &rangeType, |
| 495 | sizeof(rangeType)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 496 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
| 497 | } |
| 498 | case 1: { |
| 499 | static int arithmeticIndex = NewIndex(); |
| 500 | ArithmeticFPInputs inputs; |
| 501 | inputs.k[0] = d->fRandom->nextF(); |
| 502 | inputs.k[1] = d->fRandom->nextF(); |
| 503 | inputs.k[2] = d->fRandom->nextF(); |
| 504 | inputs.k[3] = d->fRandom->nextF(); |
| 505 | inputs.enforcePMColor = d->fRandom->nextBool(); |
| 506 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), arithmeticIndex, |
| 507 | "Arithmetic", SKSL_ARITHMETIC_SRC, |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 508 | &inputs, sizeof(inputs)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 509 | result->addChild(GrConstColorProcessor::Make( |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 510 | SK_PMColor4fWHITE, |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 511 | GrConstColorProcessor::InputMode::kIgnore)); |
| 512 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 513 | } |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 514 | case 2: { |
| 515 | static int overdrawIndex = NewIndex(); |
| 516 | SkPMColor inputs[6]; |
| 517 | for (int i = 0; i < 6; ++i) { |
| 518 | inputs[i] = d->fRandom->nextU(); |
| 519 | } |
| 520 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), overdrawIndex, |
| 521 | "Overdraw", SKSL_OVERDRAW_SRC, |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 522 | &inputs, sizeof(inputs)); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 523 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
| 524 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 525 | } |
| 526 | SK_ABORT("unreachable"); |
| 527 | return nullptr; |
| 528 | } |
| 529 | |
| 530 | #endif |