blob: f8fe85d420a66ba68475593d4b3a092bcf439010 [file] [log] [blame]
Ethan Nicholas00543112018-07-31 09:44:36 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/effects/GrSkSLFP.h"
Robert Phillips1efecea2019-02-15 16:58:40 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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 Phillips1efecea2019-02-15 16:58:40 -050014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040018
Ethan Nicholas0d997662019-04-08 09:46:01 -040019GrSkSLFPFactory::GrSkSLFPFactory(const char* name, const GrShaderCaps* shaderCaps, const char* sksl,
20 SkSL::Program::Kind kind)
21 : fKind(kind)
22 , fName(name) {
Ethan Nicholas00543112018-07-31 09:44:36 -040023 SkSL::Program::Settings settings;
24 settings.fCaps = shaderCaps;
Ethan Nicholas0d997662019-04-08 09:46:01 -040025 fBaseProgram = fCompiler.convertProgram(fKind, SkSL::String(sksl), settings);
Ethan Nicholas00543112018-07-31 09:44:36 -040026 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
47const 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 Nicholasc1c686b2019-04-02 17:30:23 -040058 if (&v->fType == fCompiler.context().fInt_Type.get() ||
59 &v->fType == fCompiler.context().fShort_Type.get()) {
Ethan Nicholas00543112018-07-31 09:44:36 -040060 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 Nicholasc1c686b2019-04-02 17:30:23 -040064 } else if (&v->fType == fCompiler.context().fFloat_Type.get() ||
65 &v->fType == fCompiler.context().fHalf_Type.get()) {
Ethan Nicholasa70693b2019-03-04 13:07:36 -050066 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 Nicholasce008112018-08-30 09:19:50 -040070 } 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 Nicholas78aceb22018-08-31 16:13:58 -040074 } else if (&v->fType == fCompiler.context().fFloat4_Type.get() ||
75 &v->fType == fCompiler.context().fHalf4_Type.get()) {
Ethan Nicholasce008112018-08-30 09:19:50 -040076 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 Nicholas00543112018-07-31 09:44:36 -040082 }
83 }
Ethan Nicholas00543112018-07-31 09:44:36 -040084
85 std::unique_ptr<SkSL::Program> specialized = fCompiler.specialize(*fBaseProgram, inputMap);
Ethan Nicholas0d997662019-04-08 09:46:01 -040086 bool optimized = fCompiler.optimize(*specialized);
87 if (!optimized) {
88 SkDebugf("%s\n", fCompiler.errorText().c_str());
89 SkASSERT(false);
90 }
Ethan Nicholas00543112018-07-31 09:44:36 -040091 const SkSL::Program* result = specialized.get();
92 fSpecializations.insert(std::make_pair(key, std::move(specialized)));
93 return result;
94}
95
Ethan Nicholasc1c686b2019-04-02 17:30:23 -040096static 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 Nicholaseace9352018-10-15 20:09:54 +0000116class GrGLSLSkSLFP : public GrGLSLFragmentProcessor {
117public:
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 Nicholas00543112018-07-31 09:44:36 -0400124
Ethan Nicholaseace9352018-10-15 20:09:54 +0000125 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 Nicholasce008112018-08-30 09:19:50 -0400149 return kFloat_GrSLType;
150 }
151
Ethan Nicholaseace9352018-10-15 20:09:54 +0000152 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 Nicholaseace9352018-10-15 20:09:54 +0000159 SkSL::String(v->fName).c_str()));
160 }
Ethan Nicholasce008112018-08-30 09:19:50 -0400161 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000162 std::vector<SkString> childNames;
163 for (int i = 0; i < this->numChildProcessors(); ++i) {
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000164 childNames.push_back(SkStringPrintf("_child%d", i));
165 this->emitChild(i, &childNames[i], args);
Ethan Nicholaseace9352018-10-15 20:09:54 +0000166 }
Ethan Nicholas6ad52892019-05-03 13:13:42 +0000167 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000168 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 Nicholasce008112018-08-30 09:19:50 -0400196 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000197 default:
198 fragBuilder->codeAppendf("%c", c);
Ethan Nicholas00543112018-07-31 09:44:36 -0400199 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000200 substringStartIndex = i + 1;
Ethan Nicholas00543112018-07-31 09:44:36 -0400201 }
Ethan Nicholasce008112018-08-30 09:19:50 -0400202 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000203 fragBuilder->codeAppend(fGLSL.c_str() + substringStartIndex,
204 fGLSL.length() - substringStartIndex);
Ethan Nicholasce008112018-08-30 09:19:50 -0400205 }
206
Ethan Nicholaseace9352018-10-15 20:09:54 +0000207 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 Nicholaseace9352018-10-15 20:09:54 +0000213 for (const auto& v : outer.fFactory->fInputVars) {
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400214 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 Nicholaseace9352018-10-15 20:09:54 +0000224 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400225 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 Nicholaseace9352018-10-15 20:09:54 +0000239 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400240 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 Nicholaseace9352018-10-15 20:09:54 +0000247 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400248 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 Nicholasa70693b2019-03-04 13:07:36 -0500255 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400256 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 Nicholas222e2752018-10-11 11:21:34 -0400262 }
Ethan Nicholas222e2752018-10-11 11:21:34 -0400263 }
264 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000265
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 Phillips1efecea2019-02-15 16:58:40 -0500274std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, int index, const char* name,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000275 const char* sksl, const void* inputs,
Ethan Nicholas0d997662019-04-08 09:46:01 -0400276 size_t inputSize, SkSL::Program::Kind kind) {
Robert Phillipsbb606772019-02-04 17:50:57 -0500277 return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(),
Robert Phillips9da87e02019-02-04 13:26:26 -0500278 context->priv().caps()->shaderCaps(),
Ethan Nicholas0d997662019-04-08 09:46:01 -0400279 kind, index, name, sksl, SkString(),
280 inputs, inputSize));
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500281}
282
283std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, int index, const char* name,
Ethan Nicholas0d997662019-04-08 09:46:01 -0400284 SkString sksl, const void* inputs, size_t inputSize,
285 SkSL::Program::Kind kind) {
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500286 return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(),
287 context->priv().caps()->shaderCaps(),
Ethan Nicholas0d997662019-04-08 09:46:01 -0400288 kind, index, name, nullptr, std::move(sksl),
289 inputs, inputSize));
Ethan Nicholas00543112018-07-31 09:44:36 -0400290}
291
Ethan Nicholas00543112018-07-31 09:44:36 -0400292GrSkSLFP::GrSkSLFP(sk_sp<GrSkSLFPFactoryCache> factoryCache, const GrShaderCaps* shaderCaps,
Ethan Nicholas0d997662019-04-08 09:46:01 -0400293 SkSL::Program::Kind kind, int index, const char* name, const char* sksl,
294 SkString skslString, const void* inputs, size_t inputSize)
Ethan Nicholas00543112018-07-31 09:44:36 -0400295 : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags)
296 , fFactoryCache(factoryCache)
297 , fShaderCaps(sk_ref_sp(shaderCaps))
Ethan Nicholas0d997662019-04-08 09:46:01 -0400298 , fKind(kind)
Ethan Nicholas00543112018-07-31 09:44:36 -0400299 , fIndex(index)
300 , fName(name)
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500301 , fSkSLString(skslString)
302 , fSkSL(sksl ? sksl : fSkSLString.c_str())
Ethan Nicholaseace9352018-10-15 20:09:54 +0000303 , fInputs(new int8_t[inputSize])
304 , fInputSize(inputSize) {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400305 if (fInputSize) {
306 memcpy(fInputs.get(), inputs, inputSize);
307 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000308}
Ethan Nicholas00543112018-07-31 09:44:36 -0400309
310GrSkSLFP::GrSkSLFP(const GrSkSLFP& other)
311 : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags)
312 , fFactoryCache(other.fFactoryCache)
313 , fShaderCaps(other.fShaderCaps)
314 , fFactory(other.fFactory)
Ethan Nicholas0d997662019-04-08 09:46:01 -0400315 , fKind(other.fKind)
Ethan Nicholas00543112018-07-31 09:44:36 -0400316 , fIndex(other.fIndex)
317 , fName(other.fName)
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500318 , fSkSLString(other.fSkSLString)
Ethan Nicholas00543112018-07-31 09:44:36 -0400319 , fSkSL(other.fSkSL)
Ethan Nicholaseace9352018-10-15 20:09:54 +0000320 , fInputs(new int8_t[other.fInputSize])
321 , fInputSize(other.fInputSize) {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400322 if (fInputSize) {
323 memcpy(fInputs.get(), other.fInputs.get(), fInputSize);
324 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000325}
Ethan Nicholas00543112018-07-31 09:44:36 -0400326
327const char* GrSkSLFP::name() const {
328 return fName;
329}
330
331void GrSkSLFP::createFactory() const {
332 if (!fFactory) {
333 fFactory = fFactoryCache->get(fIndex);
334 if (!fFactory) {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400335 fFactory = sk_sp<GrSkSLFPFactory>(new GrSkSLFPFactory(fName, fShaderCaps.get(), fSkSL,
336 fKind));
Ethan Nicholas00543112018-07-31 09:44:36 -0400337 fFactoryCache->set(fIndex, fFactory);
338 }
339 }
340}
341
Ethan Nicholasce008112018-08-30 09:19:50 -0400342void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) {
343 this->registerChildProcessor(std::move(child));
344}
345
Ethan Nicholas00543112018-07-31 09:44:36 -0400346GrGLSLFragmentProcessor* GrSkSLFP::onCreateGLSLInstance() const {
347 this->createFactory();
Ethan Nicholaseace9352018-10-15 20:09:54 +0000348 const SkSL::Program* specialized = fFactory->getSpecialization(fKey, fInputs.get(), fInputSize);
Ethan Nicholas00543112018-07-31 09:44:36 -0400349 SkSL::String glsl;
350 std::vector<SkSL::Compiler::FormatArg> formatArgs;
Ethan Nicholasce008112018-08-30 09:19:50 -0400351 if (!fFactory->fCompiler.toPipelineStage(*specialized, &glsl, &formatArgs)) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400352 printf("%s\n", fFactory->fCompiler.errorText().c_str());
Ethan Nicholasce008112018-08-30 09:19:50 -0400353 SkASSERT(false);
Ethan Nicholas00543112018-07-31 09:44:36 -0400354 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000355 return new GrGLSLSkSLFP(specialized->fContext.get(), &fFactory->fInputVars, glsl, formatArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -0400356}
357
358void GrSkSLFP::onGetGLSLProcessorKey(const GrShaderCaps& caps,
359 GrProcessorKeyBuilder* b) const {
360 this->createFactory();
Ethan Nicholas0d997662019-04-08 09:46:01 -0400361 b->add32(fIndex);
Ethan Nicholas00543112018-07-31 09:44:36 -0400362 size_t offset = 0;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000363 char* inputs = (char*) fInputs.get();
Ethan Nicholasce008112018-08-30 09:19:50 -0400364 const SkSL::Context& context = fFactory->fCompiler.context();
Ethan Nicholas00543112018-07-31 09:44:36 -0400365 for (const auto& v : fFactory->fInputVars) {
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400366 if (&v->fType == context.fFragmentProcessor_Type.get()) {
Ethan Nicholasce008112018-08-30 09:19:50 -0400367 continue;
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400368 }
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 Nicholas00543112018-07-31 09:44:36 -0400423 }
424 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400425}
426
427bool 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 Nicholaseace9352018-10-15 20:09:54 +0000431 !memcmp(fInputs.get(), sk.fInputs.get(), fInputSize);
Ethan Nicholas00543112018-07-31 09:44:36 -0400432}
433
434std::unique_ptr<GrFragmentProcessor> GrSkSLFP::clone() const {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000435 std::unique_ptr<GrSkSLFP> result(new GrSkSLFP(*this));
Ethan Nicholasce008112018-08-30 09:19:50 -0400436 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 Nicholas00543112018-07-31 09:44:36 -0400440}
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
447sk_sp<GrSkSLFPFactory> GrSkSLFPFactoryCache::get(int index) {
448 if (index >= (int) fFactories.size()) {
449 return nullptr;
450 }
451 GrSkSLFPFactory* result = fFactories[index];
Ethan Nicholasce008112018-08-30 09:19:50 -0400452 SkSafeRef(result);
Ethan Nicholas00543112018-07-31 09:44:36 -0400453 return sk_sp<GrSkSLFPFactory>(result);
454}
455
456void 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
465GrSkSLFPFactoryCache::~GrSkSLFPFactoryCache() {
466 for (GrSkSLFPFactory* factory : fFactories) {
467 if (factory) {
468 factory->unref();
469 }
470 }
471}
472
473GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSkSLFP);
474
475#if GR_TEST_UTILS
476
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500477#include "include/effects/SkArithmeticImageFilter.h"
478#include "include/gpu/GrContext.h"
479#include "src/gpu/effects/generated/GrConstColorProcessor.h"
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400480
481extern const char* SKSL_ARITHMETIC_SRC;
482extern const char* SKSL_DITHER_SRC;
483extern const char* SKSL_OVERDRAW_SRC;
Ethan Nicholas00543112018-07-31 09:44:36 -0400484
485using Value = SkSL::Program::Settings::Value;
486
487std::unique_ptr<GrFragmentProcessor> GrSkSLFP::TestCreate(GrProcessorTestData* d) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000488 int type = d->fRandom->nextULessThan(3);
Ethan Nicholas00543112018-07-31 09:44:36 -0400489 switch (type) {
490 case 0: {
491 static int ditherIndex = NewIndex();
492 int rangeType = d->fRandom->nextULessThan(3);
Ethan Nicholasce008112018-08-30 09:19:50 -0400493 std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), ditherIndex, "Dither",
Ethan Nicholaseace9352018-10-15 20:09:54 +0000494 SKSL_DITHER_SRC, &rangeType,
495 sizeof(rangeType));
Ethan Nicholasce008112018-08-30 09:19:50 -0400496 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 Nicholaseace9352018-10-15 20:09:54 +0000508 &inputs, sizeof(inputs));
Ethan Nicholasce008112018-08-30 09:19:50 -0400509 result->addChild(GrConstColorProcessor::Make(
Brian Osmanf28e55d2018-10-03 16:35:54 -0400510 SK_PMColor4fWHITE,
Ethan Nicholasce008112018-08-30 09:19:50 -0400511 GrConstColorProcessor::InputMode::kIgnore));
512 return std::unique_ptr<GrFragmentProcessor>(result.release());
Ethan Nicholas00543112018-07-31 09:44:36 -0400513 }
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400514 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 Nicholaseace9352018-10-15 20:09:54 +0000522 &inputs, sizeof(inputs));
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400523 return std::unique_ptr<GrFragmentProcessor>(result.release());
524 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400525 }
526 SK_ABORT("unreachable");
527 return nullptr;
528}
529
530#endif