blob: b1cedd2c2d15f52db39e5a02053ad019e6f9ce64 [file] [log] [blame]
joshualitt4973d9d2014-11-08 09:24:25 -08001/*
2 * Copyright 2014 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
8#include "GrDefaultGeoProcFactory.h"
9
egdaniel605dd0f2014-11-12 08:35:25 -080010#include "GrInvariantOutput.h"
joshualitt2dd1ae02014-12-03 06:24:10 -080011#include "gl/GrGLGeometryProcessor.h"
egdanielf5294392015-10-21 07:14:17 -070012#include "gl/GrGLUtil.h"
joshualitt2dd1ae02014-12-03 06:24:10 -080013#include "gl/builders/GrGLProgramBuilder.h"
joshualitt4973d9d2014-11-08 09:24:25 -080014
15/*
16 * The default Geometry Processor simply takes position and multiplies it by the uniform view
17 * matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or
18 * local coords.
19 */
joshualittb2aa7cb2015-08-05 11:05:22 -070020
21enum GPFlag {
22 kColor_GPFlag = 0x1,
23 kLocalCoord_GPFlag = 0x2,
24 kCoverage_GPFlag= 0x4,
25 kTransformedLocalCoord_GPFlag = 0x8,
26};
27
joshualitt4973d9d2014-11-08 09:24:25 -080028class DefaultGeoProc : public GrGeometryProcessor {
29public:
joshualitt8059eb92014-12-29 15:10:07 -080030 static GrGeometryProcessor* Create(uint32_t gpTypeFlags,
31 GrColor color,
32 const SkMatrix& viewMatrix,
33 const SkMatrix& localMatrix,
bsalomon7765a472015-07-08 11:26:37 -070034 bool localCoordsWillBeRead,
35 bool coverageWillBeIgnored,
joshualitt8059eb92014-12-29 15:10:07 -080036 uint8_t coverage) {
halcanary385fe4d2015-08-26 13:07:48 -070037 return new DefaultGeoProc(gpTypeFlags, color, viewMatrix, localMatrix, coverage,
38 localCoordsWillBeRead, coverageWillBeIgnored);
joshualitt4973d9d2014-11-08 09:24:25 -080039 }
40
mtklein36352bf2015-03-25 18:17:31 -070041 const char* name() const override { return "DefaultGeometryProcessor"; }
joshualitt4973d9d2014-11-08 09:24:25 -080042
joshualitt71c92602015-01-14 08:12:47 -080043 const Attribute* inPosition() const { return fInPosition; }
44 const Attribute* inColor() const { return fInColor; }
45 const Attribute* inLocalCoords() const { return fInLocalCoords; }
46 const Attribute* inCoverage() const { return fInCoverage; }
joshualitt88c23fc2015-05-13 14:18:07 -070047 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -070048 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
49 bool hasVertexColor() const { return SkToBool(fInColor); }
joshualitte578a952015-05-14 10:09:13 -070050 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -070051 const SkMatrix& localMatrix() const { return fLocalMatrix; }
bsalomon7765a472015-07-08 11:26:37 -070052 bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; }
joshualitt9b989322014-12-15 14:16:27 -080053 uint8_t coverage() const { return fCoverage; }
bsalomon7765a472015-07-08 11:26:37 -070054 bool coverageWillBeIgnored() const { return fCoverageWillBeIgnored; }
joshualittb8c241a2015-05-19 08:23:30 -070055 bool hasVertexCoverage() const { return SkToBool(fInCoverage); }
joshualitt9b989322014-12-15 14:16:27 -080056
joshualitt4973d9d2014-11-08 09:24:25 -080057 class GLProcessor : public GrGLGeometryProcessor {
58 public:
joshualitt465283c2015-09-11 08:19:35 -070059 GLProcessor()
joshualitt5559ca22015-05-21 15:50:36 -070060 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverage(0xff) {}
joshualitt4973d9d2014-11-08 09:24:25 -080061
mtklein36352bf2015-03-25 18:17:31 -070062 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -080063 const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>();
egdaniel8dcdedc2015-11-11 06:27:20 -080064 GrGLSLGPBuilder* pb = args.fPB;
robertphillips46d36f02015-01-18 08:14:14 -080065 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
egdaniel29bee0f2015-04-29 11:54:42 -070066 GrGLFragmentBuilder* fs = args.fPB->getFragmentShaderBuilder();
joshualitt4973d9d2014-11-08 09:24:25 -080067
joshualittabb52a12015-01-13 15:02:10 -080068 // emit attributes
robertphillips46d36f02015-01-18 08:14:14 -080069 vsBuilder->emitAttributes(gp);
joshualitt2dd1ae02014-12-03 06:24:10 -080070
71 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070072 if (!gp.colorIgnored()) {
73 if (gp.hasVertexColor()) {
74 pb->addPassThroughAttribute(gp.inColor(), args.fOutputColor);
75 } else {
76 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
77 }
78 }
79
joshualittabb52a12015-01-13 15:02:10 -080080 // Setup position
joshualitt5559ca22015-05-21 15:50:36 -070081 this->setupPosition(pb, gpArgs, gp.inPosition()->fName, gp.viewMatrix(),
82 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -080083
joshualittb2aa7cb2015-08-05 11:05:22 -070084 if (gp.hasExplicitLocalCoords()) {
joshualittabb52a12015-01-13 15:02:10 -080085 // emit transforms with explicit local coords
robertphillips46d36f02015-01-18 08:14:14 -080086 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inLocalCoords()->fName,
joshualittabb52a12015-01-13 15:02:10 -080087 gp.localMatrix(), args.fTransformsIn, args.fTransformsOut);
joshualittb2aa7cb2015-08-05 11:05:22 -070088 } else if(gp.hasTransformedLocalCoords()) {
89 // transforms have already been applied to vertex attributes on the cpu
90 this->emitTransforms(pb, gp.inLocalCoords()->fName,
91 args.fTransformsIn, args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -080092 } else {
93 // emit transforms with position
robertphillips46d36f02015-01-18 08:14:14 -080094 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -080095 gp.localMatrix(), args.fTransformsIn, args.fTransformsOut);
96 }
97
joshualitt2dd1ae02014-12-03 06:24:10 -080098 // Setup coverage as pass through
bsalomon7765a472015-07-08 11:26:37 -070099 if (!gp.coverageWillBeIgnored()) {
joshualittb8c241a2015-05-19 08:23:30 -0700100 if (gp.hasVertexCoverage()) {
101 fs->codeAppendf("float alpha = 1.0;");
102 args.fPB->addPassThroughAttribute(gp.inCoverage(), "alpha");
103 fs->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
104 } else if (gp.coverage() == 0xff) {
105 fs->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
106 } else {
107 const char* fragCoverage;
108 fCoverageUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility,
109 kFloat_GrSLType,
110 kDefault_GrSLPrecision,
111 "Coverage",
112 &fragCoverage);
113 fs->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, fragCoverage);
114 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800115 }
joshualitt4973d9d2014-11-08 09:24:25 -0800116 }
117
joshualitt87f48d92014-12-04 10:41:40 -0800118 static inline void GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700119 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800120 GrProcessorKeyBuilder* b) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800121 const DefaultGeoProc& def = gp.cast<DefaultGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800122 uint32_t key = def.fFlags;
joshualittb8c241a2015-05-19 08:23:30 -0700123 key |= def.colorIgnored() << 8;
bsalomon7765a472015-07-08 11:26:37 -0700124 key |= def.coverageWillBeIgnored() << 9;
joshualittb8c241a2015-05-19 08:23:30 -0700125 key |= def.hasVertexColor() << 10;
126 key |= def.hasVertexCoverage() << 11;
127 key |= def.coverage() == 0xff ? 0x1 << 12 : 0;
bsalomon7765a472015-07-08 11:26:37 -0700128 key |= def.localCoordsWillBeRead() && def.localMatrix().hasPerspective() ? 0x1 << 24 :
129 0x0;
joshualitte578a952015-05-14 10:09:13 -0700130 key |= ComputePosKey(def.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800131 b->add32(key);
joshualitt2dd1ae02014-12-03 06:24:10 -0800132 }
joshualitt4973d9d2014-11-08 09:24:25 -0800133
egdaniel018fb622015-10-28 07:26:40 -0700134 void setData(const GrGLSLProgramDataManager& pdman,
135 const GrPrimitiveProcessor& gp) override {
joshualitte578a952015-05-14 10:09:13 -0700136 const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700137
138 if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.viewMatrix())) {
139 fViewMatrix = dgp.viewMatrix();
egdaniel018fb622015-10-28 07:26:40 -0700140 float viewMatrix[3 * 3];
joshualitt5559ca22015-05-21 15:50:36 -0700141 GrGLGetMatrix<3>(viewMatrix, fViewMatrix);
142 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
143 }
joshualittee2af952014-12-30 09:04:15 -0800144
joshualittb8c241a2015-05-19 08:23:30 -0700145 if (dgp.color() != fColor && !dgp.hasVertexColor()) {
egdaniel018fb622015-10-28 07:26:40 -0700146 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700147 GrColorToRGBAFloat(dgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800148 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700149 fColor = dgp.color();
joshualitt9b989322014-12-15 14:16:27 -0800150 }
joshualittb8c241a2015-05-19 08:23:30 -0700151
bsalomon7765a472015-07-08 11:26:37 -0700152 if (!dgp.coverageWillBeIgnored() &&
153 dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) {
joshualittb8c241a2015-05-19 08:23:30 -0700154 pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverage()));
155 fCoverage = dgp.coverage();
joshualitt9b989322014-12-15 14:16:27 -0800156 }
157 }
joshualitt4973d9d2014-11-08 09:24:25 -0800158
joshualitte3ababe2015-05-15 07:56:07 -0700159 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700160 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700161 int index,
162 const SkTArray<const GrCoordTransform*, true>& transforms) override {
163 this->setTransformDataHelper<DefaultGeoProc>(primProc, pdman, index, transforms);
164 }
165
joshualitt4973d9d2014-11-08 09:24:25 -0800166 private:
joshualitt5559ca22015-05-21 15:50:36 -0700167 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800168 GrColor fColor;
169 uint8_t fCoverage;
joshualitt5559ca22015-05-21 15:50:36 -0700170 UniformHandle fViewMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800171 UniformHandle fColorUniform;
172 UniformHandle fCoverageUniform;
173
joshualitt4973d9d2014-11-08 09:24:25 -0800174 typedef GrGLGeometryProcessor INHERITED;
175 };
176
joshualitt465283c2015-09-11 08:19:35 -0700177 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
178 GLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800179 }
180
joshualitt465283c2015-09-11 08:19:35 -0700181 GrGLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override {
182 return new GLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800183 }
184
joshualitt4973d9d2014-11-08 09:24:25 -0800185private:
joshualitt8059eb92014-12-29 15:10:07 -0800186 DefaultGeoProc(uint32_t gpTypeFlags,
187 GrColor color,
188 const SkMatrix& viewMatrix,
189 const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -0700190 uint8_t coverage,
bsalomon7765a472015-07-08 11:26:37 -0700191 bool localCoordsWillBeRead,
192 bool coverageWillBeIgnored)
halcanary96fcdcc2015-08-27 07:41:13 -0700193 : fInPosition(nullptr)
194 , fInColor(nullptr)
195 , fInLocalCoords(nullptr)
196 , fInCoverage(nullptr)
joshualitt88c23fc2015-05-13 14:18:07 -0700197 , fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700198 , fViewMatrix(viewMatrix)
joshualitte3ababe2015-05-15 07:56:07 -0700199 , fLocalMatrix(localMatrix)
joshualitt9b989322014-12-15 14:16:27 -0800200 , fCoverage(coverage)
joshualittb8c241a2015-05-19 08:23:30 -0700201 , fFlags(gpTypeFlags)
bsalomon7765a472015-07-08 11:26:37 -0700202 , fLocalCoordsWillBeRead(localCoordsWillBeRead)
203 , fCoverageWillBeIgnored(coverageWillBeIgnored) {
joshualitteb2a6762014-12-04 11:35:33 -0800204 this->initClassID<DefaultGeoProc>();
joshualittb2aa7cb2015-08-05 11:05:22 -0700205 bool hasColor = SkToBool(gpTypeFlags & kColor_GPFlag);
206 bool hasExplicitLocalCoords = SkToBool(gpTypeFlags & kLocalCoord_GPFlag);
207 bool hasTransformedLocalCoords = SkToBool(gpTypeFlags & kTransformedLocalCoord_GPFlag);
208 bool hasLocalCoord = hasExplicitLocalCoords || hasTransformedLocalCoords;
209 bool hasCoverage = SkToBool(gpTypeFlags & kCoverage_GPFlag);
senorblancof2539d52015-05-20 14:03:42 -0700210 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
211 kHigh_GrSLPrecision));
joshualitt2dd1ae02014-12-03 06:24:10 -0800212 if (hasColor) {
joshualitt71c92602015-01-14 08:12:47 -0800213 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800214 }
215 if (hasLocalCoord) {
joshualitt71c92602015-01-14 08:12:47 -0800216 fInLocalCoords = &this->addVertexAttrib(Attribute("inLocalCoord",
joshualittb8c241a2015-05-19 08:23:30 -0700217 kVec2f_GrVertexAttribType));
joshualittb2aa7cb2015-08-05 11:05:22 -0700218 if (hasExplicitLocalCoords) {
219 this->setHasExplicitLocalCoords();
220 } else {
221 SkASSERT(hasTransformedLocalCoords);
222 this->setHasTransformedLocalCoords();
223 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800224 }
225 if (hasCoverage) {
joshualitt71c92602015-01-14 08:12:47 -0800226 fInCoverage = &this->addVertexAttrib(Attribute("inCoverage",
bsalomon7765a472015-07-08 11:26:37 -0700227 kFloat_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800228 }
229 }
joshualitt4973d9d2014-11-08 09:24:25 -0800230
joshualitt71c92602015-01-14 08:12:47 -0800231 const Attribute* fInPosition;
232 const Attribute* fInColor;
233 const Attribute* fInLocalCoords;
234 const Attribute* fInCoverage;
joshualitt88c23fc2015-05-13 14:18:07 -0700235 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700236 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700237 SkMatrix fLocalMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800238 uint8_t fCoverage;
joshualitt2dd1ae02014-12-03 06:24:10 -0800239 uint32_t fFlags;
bsalomon7765a472015-07-08 11:26:37 -0700240 bool fLocalCoordsWillBeRead;
241 bool fCoverageWillBeIgnored;
joshualitt4973d9d2014-11-08 09:24:25 -0800242
joshualitt2dd1ae02014-12-03 06:24:10 -0800243 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
joshualitt5478d422014-11-14 16:00:38 -0800244
joshualitt2e3b3e32014-12-09 13:31:14 -0800245 typedef GrGeometryProcessor INHERITED;
joshualitt4973d9d2014-11-08 09:24:25 -0800246};
247
248GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc);
249
bsalomonc21b09e2015-08-28 18:46:56 -0700250const GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800251 uint32_t flags = 0;
joshualitt0067ff52015-07-08 14:26:19 -0700252 if (d->fRandom->nextBool()) {
joshualittb2aa7cb2015-08-05 11:05:22 -0700253 flags |= kColor_GPFlag;
joshualitt4973d9d2014-11-08 09:24:25 -0800254 }
joshualitt0067ff52015-07-08 14:26:19 -0700255 if (d->fRandom->nextBool()) {
joshualittb2aa7cb2015-08-05 11:05:22 -0700256 flags |= kCoverage_GPFlag;
joshualitt4973d9d2014-11-08 09:24:25 -0800257 }
joshualitt0067ff52015-07-08 14:26:19 -0700258 if (d->fRandom->nextBool()) {
joshualittb2aa7cb2015-08-05 11:05:22 -0700259 flags |= kLocalCoord_GPFlag;
260 }
261 if (d->fRandom->nextBool()) {
262 flags |= kTransformedLocalCoord_GPFlag;
joshualitt2dd1ae02014-12-03 06:24:10 -0800263 }
264
joshualitt8059eb92014-12-29 15:10:07 -0800265 return DefaultGeoProc::Create(flags,
joshualitt0067ff52015-07-08 14:26:19 -0700266 GrRandomColor(d->fRandom),
267 GrTest::TestMatrix(d->fRandom),
268 GrTest::TestMatrix(d->fRandom),
269 d->fRandom->nextBool(),
270 d->fRandom->nextBool(),
271 GrRandomCoverage(d->fRandom));
joshualitt4973d9d2014-11-08 09:24:25 -0800272}
273
joshualitte9d60952015-07-27 12:13:14 -0700274const GrGeometryProcessor* GrDefaultGeoProcFactory::Create(const Color& color,
275 const Coverage& coverage,
joshualitt0d986d82015-07-28 10:01:18 -0700276 const LocalCoords& localCoords,
277 const SkMatrix& viewMatrix) {
joshualitte9d60952015-07-27 12:13:14 -0700278 uint32_t flags = 0;
joshualittb2aa7cb2015-08-05 11:05:22 -0700279 flags |= color.fType == Color::kAttribute_Type ? kColor_GPFlag : 0;
280 flags |= coverage.fType == Coverage::kAttribute_Type ? kCoverage_GPFlag : 0;
281 flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoord_GPFlag : 0;
282 flags |= localCoords.fType == LocalCoords::kHasTransformed_Type ?
283 kTransformedLocalCoord_GPFlag : 0;
joshualitte9d60952015-07-27 12:13:14 -0700284
285 uint8_t inCoverage = coverage.fCoverage;
286 bool coverageWillBeIgnored = coverage.fType == Coverage::kNone_Type;
joshualitt0d986d82015-07-28 10:01:18 -0700287 bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type;
joshualitte9d60952015-07-27 12:13:14 -0700288
289 GrColor inColor = color.fColor;
290 return DefaultGeoProc::Create(flags,
291 inColor,
292 viewMatrix,
joshualitt0d986d82015-07-28 10:01:18 -0700293 localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(),
joshualitte9d60952015-07-27 12:13:14 -0700294 localCoordsWillBeRead,
295 coverageWillBeIgnored,
296 inCoverage);
297}
joshualitt0d986d82015-07-28 10:01:18 -0700298
299const GrGeometryProcessor* GrDefaultGeoProcFactory::CreateForDeviceSpace(
300 const Color& color,
301 const Coverage& coverage,
302 const LocalCoords& localCoords,
303 const SkMatrix& viewMatrix) {
joshualitt0d986d82015-07-28 10:01:18 -0700304 SkMatrix invert = SkMatrix::I();
joshualittdf0c5572015-08-03 11:35:28 -0700305 if (LocalCoords::kUnused_Type != localCoords.fType) {
306 SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType);
307 if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) {
308 SkDebugf("Could not invert\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700309 return nullptr;
joshualittdf0c5572015-08-03 11:35:28 -0700310 }
joshualitt0d986d82015-07-28 10:01:18 -0700311
joshualittdf0c5572015-08-03 11:35:28 -0700312 if (localCoords.hasLocalMatrix()) {
313 invert.preConcat(*localCoords.fMatrix);
314 }
joshualitt0d986d82015-07-28 10:01:18 -0700315 }
316
317 LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert);
joshualittdf0c5572015-08-03 11:35:28 -0700318 return Create(color, coverage, inverted, SkMatrix::I());
joshualitt0d986d82015-07-28 10:01:18 -0700319}