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 | } |
| 150 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 151 | void emitCode(EmitArgs& args) override { |
| 152 | for (const auto& v : fInputVars) { |
| 153 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag && v->fType != |
| 154 | *fContext.fFragmentProcessor_Type) { |
| 155 | fUniformHandles.push_back(args.fUniformHandler->addUniform( |
| 156 | kFragment_GrShaderFlag, |
| 157 | this->uniformType(v->fType), |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 158 | SkSL::String(v->fName).c_str())); |
| 159 | } |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 160 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 161 | std::vector<SkString> childNames; |
| 162 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 163 | childNames.push_back(SkStringPrintf("_child%d", i)); |
Ethan Nicholas | c6dce5a | 2019-07-24 16:51:36 -0400 | [diff] [blame] | 164 | this->invokeChild(i, &childNames[i], args); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 165 | } |
Ethan Nicholas | 6ad5289 | 2019-05-03 13:13:42 +0000 | [diff] [blame] | 166 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 167 | int substringStartIndex = 0; |
| 168 | int formatArgIndex = 0; |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 169 | SkString coords = args.fTransformedCoords.count() |
Ethan Nicholas | d4efe68 | 2019-08-29 16:10:13 -0400 | [diff] [blame] | 170 | ? fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint) |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 171 | : SkString("sk_FragCoord"); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 172 | for (size_t i = 0; i < fGLSL.length(); ++i) { |
| 173 | char c = fGLSL[i]; |
| 174 | if (c == '%') { |
| 175 | fragBuilder->codeAppend(fGLSL.c_str() + substringStartIndex, |
| 176 | i - substringStartIndex); |
| 177 | ++i; |
| 178 | c = fGLSL[i]; |
| 179 | switch (c) { |
| 180 | case 's': { |
| 181 | SkSL::Compiler::FormatArg& arg = fFormatArgs[formatArgIndex++]; |
| 182 | switch (arg.fKind) { |
| 183 | case SkSL::Compiler::FormatArg::Kind::kInput: |
| 184 | fragBuilder->codeAppend(args.fInputColor); |
| 185 | break; |
| 186 | case SkSL::Compiler::FormatArg::Kind::kOutput: |
| 187 | fragBuilder->codeAppend(args.fOutputColor); |
| 188 | break; |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 189 | case SkSL::Compiler::FormatArg::Kind::kCoordX: |
| 190 | fragBuilder->codeAppendf("%s.x", coords.c_str()); |
| 191 | break; |
| 192 | case SkSL::Compiler::FormatArg::Kind::kCoordY: |
| 193 | fragBuilder->codeAppendf("%s.y", coords.c_str()); |
| 194 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 195 | case SkSL::Compiler::FormatArg::Kind::kUniform: |
| 196 | fragBuilder->codeAppend(args.fUniformHandler->getUniformCStr( |
| 197 | fUniformHandles[arg.fIndex])); |
| 198 | break; |
| 199 | case SkSL::Compiler::FormatArg::Kind::kChildProcessor: |
| 200 | fragBuilder->codeAppend(childNames[arg.fIndex].c_str()); |
| 201 | break; |
| 202 | } |
| 203 | break; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 204 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 205 | default: |
| 206 | fragBuilder->codeAppendf("%c", c); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 207 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 208 | substringStartIndex = i + 1; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 209 | } |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 210 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 211 | fragBuilder->codeAppend(fGLSL.c_str() + substringStartIndex, |
| 212 | fGLSL.length() - substringStartIndex); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 215 | void onSetData(const GrGLSLProgramDataManager& pdman, |
| 216 | const GrFragmentProcessor& _proc) override { |
| 217 | size_t uniformIndex = 0; |
| 218 | size_t offset = 0; |
| 219 | const GrSkSLFP& outer = _proc.cast<GrSkSLFP>(); |
| 220 | char* inputs = (char*) outer.fInputs.get(); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 221 | for (const auto& v : outer.fFactory->fInputVars) { |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 222 | switch (get_ctype(fContext, *v)) { |
| 223 | case SkSL::Layout::CType::kSkPMColor: { |
| 224 | float f1 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 225 | float f2 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 226 | float f3 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 227 | float f4 = ((uint8_t*) inputs)[offset++] / 255.0; |
| 228 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 229 | pdman.set4f(fUniformHandles[uniformIndex++], f1, f2, f3, f4); |
| 230 | } |
| 231 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 232 | } |
Ethan Nicholas | 0f78174 | 2019-07-17 09:57:11 -0400 | [diff] [blame] | 233 | case SkSL::Layout::CType::kSkPMColor4f: |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 234 | case SkSL::Layout::CType::kSkRect: { |
| 235 | offset = SkAlign4(offset); |
| 236 | float f1 = *(float*) (inputs + offset); |
| 237 | offset += sizeof(float); |
| 238 | float f2 = *(float*) (inputs + offset); |
| 239 | offset += sizeof(float); |
| 240 | float f3 = *(float*) (inputs + offset); |
| 241 | offset += sizeof(float); |
| 242 | float f4 = *(float*) (inputs + offset); |
| 243 | offset += sizeof(float); |
| 244 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 245 | pdman.set4f(fUniformHandles[uniformIndex++], f1, f2, f3, f4); |
| 246 | } |
| 247 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 248 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 249 | case SkSL::Layout::CType::kInt32: { |
| 250 | int32_t i = *(int32_t*) (inputs + offset); |
| 251 | offset += sizeof(int32_t); |
| 252 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 253 | pdman.set1i(fUniformHandles[uniformIndex++], i); |
| 254 | } |
| 255 | break; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 256 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 257 | case SkSL::Layout::CType::kFloat: { |
| 258 | float f = *(float*) (inputs + offset); |
| 259 | offset += sizeof(float); |
| 260 | if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) { |
| 261 | pdman.set1f(fUniformHandles[uniformIndex++], f); |
| 262 | } |
| 263 | break; |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 264 | } |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 265 | case SkSL::Layout::CType::kBool: |
| 266 | SkASSERT(!(v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag)); |
| 267 | ++offset; |
| 268 | break; |
| 269 | default: |
| 270 | SkASSERT(&v->fType == fContext.fFragmentProcessor_Type.get()); |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame] | 271 | } |
Ethan Nicholas | 222e275 | 2018-10-11 11:21:34 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 274 | |
| 275 | const SkSL::Context& fContext; |
| 276 | const std::vector<const SkSL::Variable*>& fInputVars; |
| 277 | // nearly-finished GLSL; still contains printf-style "%s" format tokens |
| 278 | const SkSL::String fGLSL; |
| 279 | std::vector<SkSL::Compiler::FormatArg> fFormatArgs; |
| 280 | std::vector<UniformHandle> fUniformHandles; |
| 281 | }; |
| 282 | |
Robert Phillips | 1efecea | 2019-02-15 16:58:40 -0500 | [diff] [blame] | 283 | 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] | 284 | const char* sksl, const void* inputs, |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 285 | size_t inputSize, SkSL::Program::Kind kind, |
| 286 | const SkMatrix* matrix) { |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 287 | return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(), |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 288 | context->priv().caps()->shaderCaps(), |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 289 | kind, index, name, sksl, SkString(), |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 290 | inputs, inputSize, matrix)); |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | 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] | 294 | SkString sksl, const void* inputs, size_t inputSize, |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 295 | SkSL::Program::Kind kind, const SkMatrix* matrix) { |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 296 | return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(), |
| 297 | context->priv().caps()->shaderCaps(), |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 298 | kind, index, name, nullptr, std::move(sksl), |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 299 | inputs, inputSize, matrix)); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 302 | GrSkSLFP::GrSkSLFP(sk_sp<GrSkSLFPFactoryCache> factoryCache, const GrShaderCaps* shaderCaps, |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 303 | SkSL::Program::Kind kind, int index, const char* name, const char* sksl, |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 304 | SkString skslString, const void* inputs, size_t inputSize, |
| 305 | const SkMatrix* matrix) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 306 | : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags) |
| 307 | , fFactoryCache(factoryCache) |
| 308 | , fShaderCaps(sk_ref_sp(shaderCaps)) |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 309 | , fKind(kind) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 310 | , fIndex(index) |
| 311 | , fName(name) |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 312 | , fSkSLString(skslString) |
| 313 | , fSkSL(sksl ? sksl : fSkSLString.c_str()) |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 314 | , fInputs(new int8_t[inputSize]) |
| 315 | , fInputSize(inputSize) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 316 | if (fInputSize) { |
| 317 | memcpy(fInputs.get(), inputs, inputSize); |
| 318 | } |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 319 | if (matrix) { |
| 320 | fCoordTransform = GrCoordTransform(*matrix); |
| 321 | this->addCoordTransform(&fCoordTransform); |
| 322 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 323 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 324 | |
| 325 | GrSkSLFP::GrSkSLFP(const GrSkSLFP& other) |
| 326 | : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags) |
| 327 | , fFactoryCache(other.fFactoryCache) |
| 328 | , fShaderCaps(other.fShaderCaps) |
| 329 | , fFactory(other.fFactory) |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 330 | , fKind(other.fKind) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 331 | , fIndex(other.fIndex) |
| 332 | , fName(other.fName) |
Ethan Nicholas | a70693b | 2019-03-04 13:07:36 -0500 | [diff] [blame] | 333 | , fSkSLString(other.fSkSLString) |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 334 | , fSkSL(other.fSkSL) |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 335 | , fInputs(new int8_t[other.fInputSize]) |
| 336 | , fInputSize(other.fInputSize) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 337 | if (fInputSize) { |
| 338 | memcpy(fInputs.get(), other.fInputs.get(), fInputSize); |
| 339 | } |
Brian Osman | 3b1e4c2 | 2019-08-12 15:12:14 -0400 | [diff] [blame] | 340 | if (other.numCoordTransforms()) { |
| 341 | fCoordTransform = other.fCoordTransform; |
| 342 | this->addCoordTransform(&fCoordTransform); |
| 343 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 344 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 345 | |
| 346 | const char* GrSkSLFP::name() const { |
| 347 | return fName; |
| 348 | } |
| 349 | |
| 350 | void GrSkSLFP::createFactory() const { |
| 351 | if (!fFactory) { |
| 352 | fFactory = fFactoryCache->get(fIndex); |
| 353 | if (!fFactory) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 354 | fFactory = sk_sp<GrSkSLFPFactory>(new GrSkSLFPFactory(fName, fShaderCaps.get(), fSkSL, |
| 355 | fKind)); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 356 | fFactoryCache->set(fIndex, fFactory); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 361 | void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) { |
| 362 | this->registerChildProcessor(std::move(child)); |
| 363 | } |
| 364 | |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 365 | GrGLSLFragmentProcessor* GrSkSLFP::onCreateGLSLInstance() const { |
| 366 | this->createFactory(); |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 367 | const SkSL::Program* specialized = fFactory->getSpecialization(fKey, fInputs.get(), fInputSize); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 368 | SkSL::String glsl; |
| 369 | std::vector<SkSL::Compiler::FormatArg> formatArgs; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 370 | if (!fFactory->fCompiler.toPipelineStage(*specialized, &glsl, &formatArgs)) { |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 371 | printf("%s\n", fFactory->fCompiler.errorText().c_str()); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 372 | SkASSERT(false); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 373 | } |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 374 | return new GrGLSLSkSLFP(specialized->fContext.get(), &fFactory->fInputVars, glsl, formatArgs); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | void GrSkSLFP::onGetGLSLProcessorKey(const GrShaderCaps& caps, |
| 378 | GrProcessorKeyBuilder* b) const { |
| 379 | this->createFactory(); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 380 | b->add32(fIndex); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 381 | size_t offset = 0; |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 382 | char* inputs = (char*) fInputs.get(); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 383 | const SkSL::Context& context = fFactory->fCompiler.context(); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 384 | for (const auto& v : fFactory->fInputVars) { |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 385 | if (&v->fType == context.fFragmentProcessor_Type.get()) { |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 386 | continue; |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 387 | } |
| 388 | switch (get_ctype(context, *v)) { |
| 389 | case SkSL::Layout::CType::kBool: |
| 390 | if (v->fModifiers.fLayout.fKey) { |
| 391 | fKey += inputs[offset]; |
| 392 | b->add32(inputs[offset]); |
| 393 | } |
| 394 | ++offset; |
| 395 | break; |
| 396 | case SkSL::Layout::CType::kInt32: { |
| 397 | offset = SkAlign4(offset); |
| 398 | if (v->fModifiers.fLayout.fKey) { |
| 399 | fKey += inputs[offset + 0]; |
| 400 | fKey += inputs[offset + 1]; |
| 401 | fKey += inputs[offset + 2]; |
| 402 | fKey += inputs[offset + 3]; |
| 403 | b->add32(*(int32_t*) (inputs + offset)); |
| 404 | } |
| 405 | offset += sizeof(int32_t); |
| 406 | break; |
| 407 | } |
| 408 | case SkSL::Layout::CType::kFloat: { |
| 409 | offset = SkAlign4(offset); |
| 410 | if (v->fModifiers.fLayout.fKey) { |
| 411 | fKey += inputs[offset + 0]; |
| 412 | fKey += inputs[offset + 1]; |
| 413 | fKey += inputs[offset + 2]; |
| 414 | fKey += inputs[offset + 3]; |
| 415 | b->add32(*(float*) (inputs + offset)); |
| 416 | } |
| 417 | offset += sizeof(float); |
| 418 | break; |
| 419 | } |
Ethan Nicholas | 0f78174 | 2019-07-17 09:57:11 -0400 | [diff] [blame] | 420 | case SkSL::Layout::CType::kSkPMColor: |
| 421 | case SkSL::Layout::CType::kSkPMColor4f: |
Ethan Nicholas | c1c686b | 2019-04-02 17:30:23 -0400 | [diff] [blame] | 422 | case SkSL::Layout::CType::kSkRect: |
| 423 | if (v->fModifiers.fLayout.fKey) { |
| 424 | for (size_t i = 0; i < sizeof(float) * 4; ++i) { |
| 425 | fKey += inputs[offset + i]; |
| 426 | } |
| 427 | b->add32(*(int32_t*) (inputs + offset)); |
| 428 | offset += sizeof(float); |
| 429 | b->add32(*(int32_t*) (inputs + offset)); |
| 430 | offset += sizeof(float); |
| 431 | b->add32(*(int32_t*) (inputs + offset)); |
| 432 | offset += sizeof(float); |
| 433 | b->add32(*(int32_t*) (inputs + offset)); |
| 434 | offset += sizeof(float); |
| 435 | } else { |
| 436 | offset += sizeof(float) * 4; |
| 437 | } |
| 438 | break; |
| 439 | default: |
| 440 | // unsupported input var type |
| 441 | printf("%s\n", SkSL::String(v->fType.fName).c_str()); |
| 442 | SkASSERT(false); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 443 | } |
| 444 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | bool GrSkSLFP::onIsEqual(const GrFragmentProcessor& other) const { |
| 448 | const GrSkSLFP& sk = other.cast<GrSkSLFP>(); |
| 449 | SkASSERT(fIndex != sk.fIndex || fInputSize == sk.fInputSize); |
| 450 | return fIndex == sk.fIndex && |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 451 | !memcmp(fInputs.get(), sk.fInputs.get(), fInputSize); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | std::unique_ptr<GrFragmentProcessor> GrSkSLFP::clone() const { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 455 | std::unique_ptr<GrSkSLFP> result(new GrSkSLFP(*this)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 456 | for (int i = 0; i < this->numChildProcessors(); ++i) { |
| 457 | result->registerChildProcessor(this->childProcessor(i).clone()); |
| 458 | } |
| 459 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | // We have to do a bit of manual refcounting in the cache methods below. Ideally, we could just |
| 463 | // define fFactories to contain sk_sp<GrSkSLFPFactory> rather than GrSkSLFPFactory*, but that would |
| 464 | // require GrContext to include GrSkSLFP, which creates much bigger headaches than a few manual |
| 465 | // refcounts. |
| 466 | |
| 467 | sk_sp<GrSkSLFPFactory> GrSkSLFPFactoryCache::get(int index) { |
| 468 | if (index >= (int) fFactories.size()) { |
| 469 | return nullptr; |
| 470 | } |
| 471 | GrSkSLFPFactory* result = fFactories[index]; |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 472 | SkSafeRef(result); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 473 | return sk_sp<GrSkSLFPFactory>(result); |
| 474 | } |
| 475 | |
| 476 | void GrSkSLFPFactoryCache::set(int index, sk_sp<GrSkSLFPFactory> factory) { |
| 477 | while (index >= (int) fFactories.size()) { |
| 478 | fFactories.emplace_back(); |
| 479 | } |
| 480 | factory->ref(); |
| 481 | SkASSERT(!fFactories[index]); |
| 482 | fFactories[index] = factory.get(); |
| 483 | } |
| 484 | |
| 485 | GrSkSLFPFactoryCache::~GrSkSLFPFactoryCache() { |
| 486 | for (GrSkSLFPFactory* factory : fFactories) { |
| 487 | if (factory) { |
| 488 | factory->unref(); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSkSLFP); |
| 494 | |
| 495 | #if GR_TEST_UTILS |
| 496 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 497 | #include "include/effects/SkArithmeticImageFilter.h" |
| 498 | #include "include/gpu/GrContext.h" |
| 499 | #include "src/gpu/effects/generated/GrConstColorProcessor.h" |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 500 | |
| 501 | extern const char* SKSL_ARITHMETIC_SRC; |
| 502 | extern const char* SKSL_DITHER_SRC; |
| 503 | extern const char* SKSL_OVERDRAW_SRC; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 504 | |
| 505 | using Value = SkSL::Program::Settings::Value; |
| 506 | |
| 507 | std::unique_ptr<GrFragmentProcessor> GrSkSLFP::TestCreate(GrProcessorTestData* d) { |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 508 | int type = d->fRandom->nextULessThan(3); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 509 | switch (type) { |
| 510 | case 0: { |
| 511 | static int ditherIndex = NewIndex(); |
| 512 | int rangeType = d->fRandom->nextULessThan(3); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 513 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), ditherIndex, "Dither", |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 514 | SKSL_DITHER_SRC, &rangeType, |
| 515 | sizeof(rangeType)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 516 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
| 517 | } |
| 518 | case 1: { |
| 519 | static int arithmeticIndex = NewIndex(); |
| 520 | ArithmeticFPInputs inputs; |
| 521 | inputs.k[0] = d->fRandom->nextF(); |
| 522 | inputs.k[1] = d->fRandom->nextF(); |
| 523 | inputs.k[2] = d->fRandom->nextF(); |
| 524 | inputs.k[3] = d->fRandom->nextF(); |
| 525 | inputs.enforcePMColor = d->fRandom->nextBool(); |
| 526 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), arithmeticIndex, |
| 527 | "Arithmetic", SKSL_ARITHMETIC_SRC, |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 528 | &inputs, sizeof(inputs)); |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 529 | result->addChild(GrConstColorProcessor::Make( |
Brian Osman | f28e55d | 2018-10-03 16:35:54 -0400 | [diff] [blame] | 530 | SK_PMColor4fWHITE, |
Ethan Nicholas | ce00811 | 2018-08-30 09:19:50 -0400 | [diff] [blame] | 531 | GrConstColorProcessor::InputMode::kIgnore)); |
| 532 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 533 | } |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 534 | case 2: { |
| 535 | static int overdrawIndex = NewIndex(); |
| 536 | SkPMColor inputs[6]; |
| 537 | for (int i = 0; i < 6; ++i) { |
| 538 | inputs[i] = d->fRandom->nextU(); |
| 539 | } |
| 540 | std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), overdrawIndex, |
| 541 | "Overdraw", SKSL_OVERDRAW_SRC, |
Ethan Nicholas | eace935 | 2018-10-15 20:09:54 +0000 | [diff] [blame] | 542 | &inputs, sizeof(inputs)); |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 543 | return std::unique_ptr<GrFragmentProcessor>(result.release()); |
| 544 | } |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 545 | } |
| 546 | SK_ABORT("unreachable"); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | #endif |