blob: 5237ba1cfd43594a2194b5ce4852c77c75314c50 [file] [log] [blame]
jvanverth50530632015-04-27 10:36:27 -07001/*
2* Copyright 2013 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
joshualitt8072caa2015-02-12 14:20:52 -08008#include "GrPathProcessor.h"
9
joshualitt8072caa2015-02-12 14:20:52 -080010#include "gl/GrGLGpu.h"
jvanverthcba99b82015-06-24 06:59:57 -070011#include "glsl/GrGLSLCaps.h"
egdaniel2d721d32015-11-11 13:06:05 -080012#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel7dc4bd02015-10-29 07:57:01 -070013#include "glsl/GrGLSLProcessorTypes.h"
egdaniel7ea439b2015-12-03 09:20:44 -080014#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080015#include "glsl/GrGLSLVarying.h"
jvanverthcba99b82015-06-24 06:59:57 -070016
egdaniele659a582015-11-13 09:55:43 -080017class GrGLPathProcessor : public GrGLSLPrimitiveProcessor {
joshualitt102081a2015-09-11 11:52:17 -070018public:
19 GrGLPathProcessor() : fColor(GrColor_ILLEGAL) {}
20
21 static void GenKey(const GrPathProcessor& pathProc,
22 const GrGLSLCaps&,
23 GrProcessorKeyBuilder* b) {
ethannicholasff210322015-11-24 12:10:10 -080024 b->add32(SkToInt(pathProc.overrides().readsColor()) |
cdaltonc08f1962016-02-12 12:14:06 -080025 (SkToInt(pathProc.overrides().readsCoverage()) << 1) |
26 (SkToInt(pathProc.viewMatrix().hasPerspective()) << 2));
joshualitt102081a2015-09-11 11:52:17 -070027 }
28
29 void emitCode(EmitArgs& args) override {
cdalton85285412016-02-18 12:37:07 -080030 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt102081a2015-09-11 11:52:17 -070031 const GrPathProcessor& pathProc = args.fGP.cast<GrPathProcessor>();
32
cdaltonc08f1962016-02-12 12:14:06 -080033 if (!pathProc.viewMatrix().hasPerspective()) {
34 args.fVaryingHandler->setNoPerspective();
35 }
36
joshualitt102081a2015-09-11 11:52:17 -070037 // emit transforms
egdaniel0eafe792015-11-20 14:01:22 -080038 this->emitTransforms(args.fVaryingHandler, args.fTransformsIn, args.fTransformsOut);
joshualitt102081a2015-09-11 11:52:17 -070039
40 // Setup uniform color
ethannicholasff210322015-11-24 12:10:10 -080041 if (pathProc.overrides().readsColor()) {
joshualitt102081a2015-09-11 11:52:17 -070042 const char* stagedLocalVarName;
cdalton5e58cee2016-02-11 12:49:47 -080043 fColorUniform = args.fUniformHandler->addUniform(kFragment_GrShaderFlag,
44 kVec4f_GrSLType,
45 kDefault_GrSLPrecision,
46 "Color",
47 &stagedLocalVarName);
egdaniel4ca2e602015-11-18 08:01:26 -080048 fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, stagedLocalVarName);
joshualitt102081a2015-09-11 11:52:17 -070049 }
50
51 // setup constant solid coverage
ethannicholasff210322015-11-24 12:10:10 -080052 if (pathProc.overrides().readsCoverage()) {
egdaniel4ca2e602015-11-18 08:01:26 -080053 fragBuilder->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
joshualitt102081a2015-09-11 11:52:17 -070054 }
55 }
56
egdaniel0eafe792015-11-20 14:01:22 -080057 void emitTransforms(GrGLSLVaryingHandler* varyingHandler,
58 const TransformsIn& tin,
59 TransformsOut* tout) {
joshualitt102081a2015-09-11 11:52:17 -070060 tout->push_back_n(tin.count());
61 fInstalledTransforms.push_back_n(tin.count());
62 for (int i = 0; i < tin.count(); i++) {
63 const ProcCoords& coordTransforms = tin[i];
64 fInstalledTransforms[i].push_back_n(coordTransforms.count());
65 for (int t = 0; t < coordTransforms.count(); t++) {
66 GrSLType varyingType =
67 coordTransforms[t]->getMatrix().hasPerspective() ? kVec3f_GrSLType :
68 kVec2f_GrSLType;
69
70 SkString strVaryingName("MatrixCoord");
71 strVaryingName.appendf("_%i_%i", i, t);
egdaniel8dcdedc2015-11-11 06:27:20 -080072 GrGLSLVertToFrag v(varyingType);
egdaniel0eafe792015-11-20 14:01:22 -080073 GrGLVaryingHandler* glVaryingHandler = (GrGLVaryingHandler*) varyingHandler;
joshualitt102081a2015-09-11 11:52:17 -070074 fInstalledTransforms[i][t].fHandle =
egdaniel0eafe792015-11-20 14:01:22 -080075 glVaryingHandler->addPathProcessingVarying(strVaryingName.c_str(),
76 &v).toIndex();
joshualitt102081a2015-09-11 11:52:17 -070077 fInstalledTransforms[i][t].fType = varyingType;
78
bungeman85dc3592016-02-09 11:32:56 -080079 (*tout)[i].emplace_back(SkString(v.fsIn()), varyingType);
joshualitt102081a2015-09-11 11:52:17 -070080 }
81 }
82 }
83
egdaniel018fb622015-10-28 07:26:40 -070084 void setData(const GrGLSLProgramDataManager& pd,
85 const GrPrimitiveProcessor& primProc) override {
joshualitt102081a2015-09-11 11:52:17 -070086 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
ethannicholasff210322015-11-24 12:10:10 -080087 if (pathProc.overrides().readsColor() && pathProc.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -070088 float c[4];
joshualitt102081a2015-09-11 11:52:17 -070089 GrColorToRGBAFloat(pathProc.color(), c);
90 pd.set4fv(fColorUniform, 1, c);
91 fColor = pathProc.color();
92 }
93 }
94
95 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -070096 const GrGLSLProgramDataManager& pdman,
joshualitt102081a2015-09-11 11:52:17 -070097 int index,
98 const SkTArray<const GrCoordTransform*, true>& coordTransforms) override {
99 const GrPathProcessor& pathProc = primProc.cast<GrPathProcessor>();
egdanielfe8a8392016-05-09 10:22:19 -0700100 SkSTArray<2, VaryingTransform, true>& transforms = fInstalledTransforms[index];
joshualitt102081a2015-09-11 11:52:17 -0700101 int numTransforms = transforms.count();
102 for (int t = 0; t < numTransforms; ++t) {
103 SkASSERT(transforms[t].fHandle.isValid());
104 const SkMatrix& transform = GetTransformMatrix(pathProc.localMatrix(),
105 *coordTransforms[t]);
106 if (transforms[t].fCurrentValue.cheapEqualTo(transform)) {
107 continue;
108 }
109 transforms[t].fCurrentValue = transform;
110
111 SkASSERT(transforms[t].fType == kVec2f_GrSLType ||
112 transforms[t].fType == kVec3f_GrSLType);
113 unsigned components = transforms[t].fType == kVec2f_GrSLType ? 2 : 3;
114 pdman.setPathFragmentInputTransform(transforms[t].fHandle, components, transform);
115 }
116 }
117
118private:
egdanielfe8a8392016-05-09 10:22:19 -0700119 typedef GrGLSLProgramDataManager::VaryingHandle VaryingHandle;
120 struct VaryingTransform : public Transform {
121 VaryingTransform() : Transform() {}
122 VaryingHandle fHandle;
123 };
124
125 SkSTArray<8, SkSTArray<2, VaryingTransform, true> > fInstalledTransforms;
126
joshualitt102081a2015-09-11 11:52:17 -0700127 UniformHandle fColorUniform;
128 GrColor fColor;
129
egdaniele659a582015-11-13 09:55:43 -0800130 typedef GrGLSLPrimitiveProcessor INHERITED;
joshualitt102081a2015-09-11 11:52:17 -0700131};
132
joshualitt8072caa2015-02-12 14:20:52 -0800133GrPathProcessor::GrPathProcessor(GrColor color,
ethannicholasff210322015-11-24 12:10:10 -0800134 const GrXPOverridesForBatch& overrides,
joshualitt8072caa2015-02-12 14:20:52 -0800135 const SkMatrix& viewMatrix,
136 const SkMatrix& localMatrix)
ethannicholas22793252016-01-30 09:59:10 -0800137 : fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700138 , fViewMatrix(viewMatrix)
joshualittf2384692015-09-10 11:00:51 -0700139 , fLocalMatrix(localMatrix)
ethannicholasff210322015-11-24 12:10:10 -0800140 , fOverrides(overrides) {
joshualitt8072caa2015-02-12 14:20:52 -0800141 this->initClassID<GrPathProcessor>();
142}
143
egdaniel57d3b032015-11-13 11:57:27 -0800144void GrPathProcessor::getGLSLProcessorKey(const GrGLSLCaps& caps,
145 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700146 GrGLPathProcessor::GenKey(*this, caps, b);
joshualitt8072caa2015-02-12 14:20:52 -0800147}
148
egdaniel57d3b032015-11-13 11:57:27 -0800149GrGLSLPrimitiveProcessor* GrPathProcessor::createGLSLInstance(const GrGLSLCaps& caps) const {
jvanverth50530632015-04-27 10:36:27 -0700150 SkASSERT(caps.pathRenderingSupport());
joshualitt465283c2015-09-11 08:19:35 -0700151 return new GrGLPathProcessor();
joshualitt8072caa2015-02-12 14:20:52 -0800152}