blob: cd8803eb4da64a75cf171a28b57cf39a8b7d787c [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 Nicholas0f781742019-07-17 09:57:11 -0400225 case SkSL::Layout::CType::kSkPMColor4f:
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400226 case SkSL::Layout::CType::kSkRect: {
227 offset = SkAlign4(offset);
228 float f1 = *(float*) (inputs + offset);
229 offset += sizeof(float);
230 float f2 = *(float*) (inputs + offset);
231 offset += sizeof(float);
232 float f3 = *(float*) (inputs + offset);
233 offset += sizeof(float);
234 float f4 = *(float*) (inputs + offset);
235 offset += sizeof(float);
236 if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) {
237 pdman.set4f(fUniformHandles[uniformIndex++], f1, f2, f3, f4);
238 }
239 break;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000240 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400241 case SkSL::Layout::CType::kInt32: {
242 int32_t i = *(int32_t*) (inputs + offset);
243 offset += sizeof(int32_t);
244 if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) {
245 pdman.set1i(fUniformHandles[uniformIndex++], i);
246 }
247 break;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000248 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400249 case SkSL::Layout::CType::kFloat: {
250 float f = *(float*) (inputs + offset);
251 offset += sizeof(float);
252 if (v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag) {
253 pdman.set1f(fUniformHandles[uniformIndex++], f);
254 }
255 break;
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500256 }
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400257 case SkSL::Layout::CType::kBool:
258 SkASSERT(!(v->fModifiers.fFlags & SkSL::Modifiers::kUniform_Flag));
259 ++offset;
260 break;
261 default:
262 SkASSERT(&v->fType == fContext.fFragmentProcessor_Type.get());
Ethan Nicholas222e2752018-10-11 11:21:34 -0400263 }
Ethan Nicholas222e2752018-10-11 11:21:34 -0400264 }
265 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000266
267 const SkSL::Context& fContext;
268 const std::vector<const SkSL::Variable*>& fInputVars;
269 // nearly-finished GLSL; still contains printf-style "%s" format tokens
270 const SkSL::String fGLSL;
271 std::vector<SkSL::Compiler::FormatArg> fFormatArgs;
272 std::vector<UniformHandle> fUniformHandles;
273};
274
Robert Phillips1efecea2019-02-15 16:58:40 -0500275std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, int index, const char* name,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000276 const char* sksl, const void* inputs,
Ethan Nicholas0d997662019-04-08 09:46:01 -0400277 size_t inputSize, SkSL::Program::Kind kind) {
Robert Phillipsbb606772019-02-04 17:50:57 -0500278 return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(),
Robert Phillips9da87e02019-02-04 13:26:26 -0500279 context->priv().caps()->shaderCaps(),
Ethan Nicholas0d997662019-04-08 09:46:01 -0400280 kind, index, name, sksl, SkString(),
281 inputs, inputSize));
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500282}
283
284std::unique_ptr<GrSkSLFP> GrSkSLFP::Make(GrContext_Base* context, int index, const char* name,
Ethan Nicholas0d997662019-04-08 09:46:01 -0400285 SkString sksl, const void* inputs, size_t inputSize,
286 SkSL::Program::Kind kind) {
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500287 return std::unique_ptr<GrSkSLFP>(new GrSkSLFP(context->priv().fpFactoryCache(),
288 context->priv().caps()->shaderCaps(),
Ethan Nicholas0d997662019-04-08 09:46:01 -0400289 kind, index, name, nullptr, std::move(sksl),
290 inputs, inputSize));
Ethan Nicholas00543112018-07-31 09:44:36 -0400291}
292
Ethan Nicholas00543112018-07-31 09:44:36 -0400293GrSkSLFP::GrSkSLFP(sk_sp<GrSkSLFPFactoryCache> factoryCache, const GrShaderCaps* shaderCaps,
Ethan Nicholas0d997662019-04-08 09:46:01 -0400294 SkSL::Program::Kind kind, int index, const char* name, const char* sksl,
295 SkString skslString, const void* inputs, size_t inputSize)
Ethan Nicholas00543112018-07-31 09:44:36 -0400296 : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags)
297 , fFactoryCache(factoryCache)
298 , fShaderCaps(sk_ref_sp(shaderCaps))
Ethan Nicholas0d997662019-04-08 09:46:01 -0400299 , fKind(kind)
Ethan Nicholas00543112018-07-31 09:44:36 -0400300 , fIndex(index)
301 , fName(name)
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500302 , fSkSLString(skslString)
303 , fSkSL(sksl ? sksl : fSkSLString.c_str())
Ethan Nicholaseace9352018-10-15 20:09:54 +0000304 , fInputs(new int8_t[inputSize])
305 , fInputSize(inputSize) {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400306 if (fInputSize) {
307 memcpy(fInputs.get(), inputs, inputSize);
308 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000309}
Ethan Nicholas00543112018-07-31 09:44:36 -0400310
311GrSkSLFP::GrSkSLFP(const GrSkSLFP& other)
312 : INHERITED(kGrSkSLFP_ClassID, kNone_OptimizationFlags)
313 , fFactoryCache(other.fFactoryCache)
314 , fShaderCaps(other.fShaderCaps)
315 , fFactory(other.fFactory)
Ethan Nicholas0d997662019-04-08 09:46:01 -0400316 , fKind(other.fKind)
Ethan Nicholas00543112018-07-31 09:44:36 -0400317 , fIndex(other.fIndex)
318 , fName(other.fName)
Ethan Nicholasa70693b2019-03-04 13:07:36 -0500319 , fSkSLString(other.fSkSLString)
Ethan Nicholas00543112018-07-31 09:44:36 -0400320 , fSkSL(other.fSkSL)
Ethan Nicholaseace9352018-10-15 20:09:54 +0000321 , fInputs(new int8_t[other.fInputSize])
322 , fInputSize(other.fInputSize) {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400323 if (fInputSize) {
324 memcpy(fInputs.get(), other.fInputs.get(), fInputSize);
325 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000326}
Ethan Nicholas00543112018-07-31 09:44:36 -0400327
328const char* GrSkSLFP::name() const {
329 return fName;
330}
331
332void GrSkSLFP::createFactory() const {
333 if (!fFactory) {
334 fFactory = fFactoryCache->get(fIndex);
335 if (!fFactory) {
Ethan Nicholas0d997662019-04-08 09:46:01 -0400336 fFactory = sk_sp<GrSkSLFPFactory>(new GrSkSLFPFactory(fName, fShaderCaps.get(), fSkSL,
337 fKind));
Ethan Nicholas00543112018-07-31 09:44:36 -0400338 fFactoryCache->set(fIndex, fFactory);
339 }
340 }
341}
342
Ethan Nicholasce008112018-08-30 09:19:50 -0400343void GrSkSLFP::addChild(std::unique_ptr<GrFragmentProcessor> child) {
344 this->registerChildProcessor(std::move(child));
345}
346
Ethan Nicholas00543112018-07-31 09:44:36 -0400347GrGLSLFragmentProcessor* GrSkSLFP::onCreateGLSLInstance() const {
348 this->createFactory();
Ethan Nicholaseace9352018-10-15 20:09:54 +0000349 const SkSL::Program* specialized = fFactory->getSpecialization(fKey, fInputs.get(), fInputSize);
Ethan Nicholas00543112018-07-31 09:44:36 -0400350 SkSL::String glsl;
351 std::vector<SkSL::Compiler::FormatArg> formatArgs;
Ethan Nicholasce008112018-08-30 09:19:50 -0400352 if (!fFactory->fCompiler.toPipelineStage(*specialized, &glsl, &formatArgs)) {
Ethan Nicholas00543112018-07-31 09:44:36 -0400353 printf("%s\n", fFactory->fCompiler.errorText().c_str());
Ethan Nicholasce008112018-08-30 09:19:50 -0400354 SkASSERT(false);
Ethan Nicholas00543112018-07-31 09:44:36 -0400355 }
Ethan Nicholaseace9352018-10-15 20:09:54 +0000356 return new GrGLSLSkSLFP(specialized->fContext.get(), &fFactory->fInputVars, glsl, formatArgs);
Ethan Nicholas00543112018-07-31 09:44:36 -0400357}
358
359void GrSkSLFP::onGetGLSLProcessorKey(const GrShaderCaps& caps,
360 GrProcessorKeyBuilder* b) const {
361 this->createFactory();
Ethan Nicholas0d997662019-04-08 09:46:01 -0400362 b->add32(fIndex);
Ethan Nicholas00543112018-07-31 09:44:36 -0400363 size_t offset = 0;
Ethan Nicholaseace9352018-10-15 20:09:54 +0000364 char* inputs = (char*) fInputs.get();
Ethan Nicholasce008112018-08-30 09:19:50 -0400365 const SkSL::Context& context = fFactory->fCompiler.context();
Ethan Nicholas00543112018-07-31 09:44:36 -0400366 for (const auto& v : fFactory->fInputVars) {
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400367 if (&v->fType == context.fFragmentProcessor_Type.get()) {
Ethan Nicholasce008112018-08-30 09:19:50 -0400368 continue;
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400369 }
370 switch (get_ctype(context, *v)) {
371 case SkSL::Layout::CType::kBool:
372 if (v->fModifiers.fLayout.fKey) {
373 fKey += inputs[offset];
374 b->add32(inputs[offset]);
375 }
376 ++offset;
377 break;
378 case SkSL::Layout::CType::kInt32: {
379 offset = SkAlign4(offset);
380 if (v->fModifiers.fLayout.fKey) {
381 fKey += inputs[offset + 0];
382 fKey += inputs[offset + 1];
383 fKey += inputs[offset + 2];
384 fKey += inputs[offset + 3];
385 b->add32(*(int32_t*) (inputs + offset));
386 }
387 offset += sizeof(int32_t);
388 break;
389 }
390 case SkSL::Layout::CType::kFloat: {
391 offset = SkAlign4(offset);
392 if (v->fModifiers.fLayout.fKey) {
393 fKey += inputs[offset + 0];
394 fKey += inputs[offset + 1];
395 fKey += inputs[offset + 2];
396 fKey += inputs[offset + 3];
397 b->add32(*(float*) (inputs + offset));
398 }
399 offset += sizeof(float);
400 break;
401 }
Ethan Nicholas0f781742019-07-17 09:57:11 -0400402 case SkSL::Layout::CType::kSkPMColor:
403 case SkSL::Layout::CType::kSkPMColor4f:
Ethan Nicholasc1c686b2019-04-02 17:30:23 -0400404 case SkSL::Layout::CType::kSkRect:
405 if (v->fModifiers.fLayout.fKey) {
406 for (size_t i = 0; i < sizeof(float) * 4; ++i) {
407 fKey += inputs[offset + i];
408 }
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 b->add32(*(int32_t*) (inputs + offset));
416 offset += sizeof(float);
417 } else {
418 offset += sizeof(float) * 4;
419 }
420 break;
421 default:
422 // unsupported input var type
423 printf("%s\n", SkSL::String(v->fType.fName).c_str());
424 SkASSERT(false);
Ethan Nicholas00543112018-07-31 09:44:36 -0400425 }
426 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400427}
428
429bool GrSkSLFP::onIsEqual(const GrFragmentProcessor& other) const {
430 const GrSkSLFP& sk = other.cast<GrSkSLFP>();
431 SkASSERT(fIndex != sk.fIndex || fInputSize == sk.fInputSize);
432 return fIndex == sk.fIndex &&
Ethan Nicholaseace9352018-10-15 20:09:54 +0000433 !memcmp(fInputs.get(), sk.fInputs.get(), fInputSize);
Ethan Nicholas00543112018-07-31 09:44:36 -0400434}
435
436std::unique_ptr<GrFragmentProcessor> GrSkSLFP::clone() const {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000437 std::unique_ptr<GrSkSLFP> result(new GrSkSLFP(*this));
Ethan Nicholasce008112018-08-30 09:19:50 -0400438 for (int i = 0; i < this->numChildProcessors(); ++i) {
439 result->registerChildProcessor(this->childProcessor(i).clone());
440 }
441 return std::unique_ptr<GrFragmentProcessor>(result.release());
Ethan Nicholas00543112018-07-31 09:44:36 -0400442}
443
444// We have to do a bit of manual refcounting in the cache methods below. Ideally, we could just
445// define fFactories to contain sk_sp<GrSkSLFPFactory> rather than GrSkSLFPFactory*, but that would
446// require GrContext to include GrSkSLFP, which creates much bigger headaches than a few manual
447// refcounts.
448
449sk_sp<GrSkSLFPFactory> GrSkSLFPFactoryCache::get(int index) {
450 if (index >= (int) fFactories.size()) {
451 return nullptr;
452 }
453 GrSkSLFPFactory* result = fFactories[index];
Ethan Nicholasce008112018-08-30 09:19:50 -0400454 SkSafeRef(result);
Ethan Nicholas00543112018-07-31 09:44:36 -0400455 return sk_sp<GrSkSLFPFactory>(result);
456}
457
458void GrSkSLFPFactoryCache::set(int index, sk_sp<GrSkSLFPFactory> factory) {
459 while (index >= (int) fFactories.size()) {
460 fFactories.emplace_back();
461 }
462 factory->ref();
463 SkASSERT(!fFactories[index]);
464 fFactories[index] = factory.get();
465}
466
467GrSkSLFPFactoryCache::~GrSkSLFPFactoryCache() {
468 for (GrSkSLFPFactory* factory : fFactories) {
469 if (factory) {
470 factory->unref();
471 }
472 }
473}
474
475GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrSkSLFP);
476
477#if GR_TEST_UTILS
478
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500479#include "include/effects/SkArithmeticImageFilter.h"
480#include "include/gpu/GrContext.h"
481#include "src/gpu/effects/generated/GrConstColorProcessor.h"
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400482
483extern const char* SKSL_ARITHMETIC_SRC;
484extern const char* SKSL_DITHER_SRC;
485extern const char* SKSL_OVERDRAW_SRC;
Ethan Nicholas00543112018-07-31 09:44:36 -0400486
487using Value = SkSL::Program::Settings::Value;
488
489std::unique_ptr<GrFragmentProcessor> GrSkSLFP::TestCreate(GrProcessorTestData* d) {
Ethan Nicholaseace9352018-10-15 20:09:54 +0000490 int type = d->fRandom->nextULessThan(3);
Ethan Nicholas00543112018-07-31 09:44:36 -0400491 switch (type) {
492 case 0: {
493 static int ditherIndex = NewIndex();
494 int rangeType = d->fRandom->nextULessThan(3);
Ethan Nicholasce008112018-08-30 09:19:50 -0400495 std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), ditherIndex, "Dither",
Ethan Nicholaseace9352018-10-15 20:09:54 +0000496 SKSL_DITHER_SRC, &rangeType,
497 sizeof(rangeType));
Ethan Nicholasce008112018-08-30 09:19:50 -0400498 return std::unique_ptr<GrFragmentProcessor>(result.release());
499 }
500 case 1: {
501 static int arithmeticIndex = NewIndex();
502 ArithmeticFPInputs inputs;
503 inputs.k[0] = d->fRandom->nextF();
504 inputs.k[1] = d->fRandom->nextF();
505 inputs.k[2] = d->fRandom->nextF();
506 inputs.k[3] = d->fRandom->nextF();
507 inputs.enforcePMColor = d->fRandom->nextBool();
508 std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), arithmeticIndex,
509 "Arithmetic", SKSL_ARITHMETIC_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000510 &inputs, sizeof(inputs));
Ethan Nicholasce008112018-08-30 09:19:50 -0400511 result->addChild(GrConstColorProcessor::Make(
Brian Osmanf28e55d2018-10-03 16:35:54 -0400512 SK_PMColor4fWHITE,
Ethan Nicholasce008112018-08-30 09:19:50 -0400513 GrConstColorProcessor::InputMode::kIgnore));
514 return std::unique_ptr<GrFragmentProcessor>(result.release());
Ethan Nicholas00543112018-07-31 09:44:36 -0400515 }
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400516 case 2: {
517 static int overdrawIndex = NewIndex();
518 SkPMColor inputs[6];
519 for (int i = 0; i < 6; ++i) {
520 inputs[i] = d->fRandom->nextU();
521 }
522 std::unique_ptr<GrSkSLFP> result = GrSkSLFP::Make(d->context(), overdrawIndex,
523 "Overdraw", SKSL_OVERDRAW_SRC,
Ethan Nicholaseace9352018-10-15 20:09:54 +0000524 &inputs, sizeof(inputs));
Ethan Nicholas78aceb22018-08-31 16:13:58 -0400525 return std::unique_ptr<GrFragmentProcessor>(result.release());
526 }
Ethan Nicholas00543112018-07-31 09:44:36 -0400527 }
528 SK_ABORT("unreachable");
529 return nullptr;
530}
531
532#endif