blob: 639a593220c68fbf56b099517084e373434597bc [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"
12#include "gl/builders/GrGLProgramBuilder.h"
joshualitt4973d9d2014-11-08 09:24:25 -080013
14/*
15 * The default Geometry Processor simply takes position and multiplies it by the uniform view
16 * matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or
17 * local coords.
18 */
19class DefaultGeoProc : public GrGeometryProcessor {
20public:
joshualitt8059eb92014-12-29 15:10:07 -080021 static GrGeometryProcessor* Create(uint32_t gpTypeFlags,
22 GrColor color,
23 const SkMatrix& viewMatrix,
24 const SkMatrix& localMatrix,
bsalomon7765a472015-07-08 11:26:37 -070025 bool localCoordsWillBeRead,
26 bool coverageWillBeIgnored,
joshualitt8059eb92014-12-29 15:10:07 -080027 uint8_t coverage) {
28 return SkNEW_ARGS(DefaultGeoProc, (gpTypeFlags,
29 color,
30 viewMatrix,
31 localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -070032 coverage,
bsalomon7765a472015-07-08 11:26:37 -070033 localCoordsWillBeRead,
34 coverageWillBeIgnored));
joshualitt4973d9d2014-11-08 09:24:25 -080035 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 const char* name() const override { return "DefaultGeometryProcessor"; }
joshualitt4973d9d2014-11-08 09:24:25 -080038
joshualitt71c92602015-01-14 08:12:47 -080039 const Attribute* inPosition() const { return fInPosition; }
40 const Attribute* inColor() const { return fInColor; }
41 const Attribute* inLocalCoords() const { return fInLocalCoords; }
42 const Attribute* inCoverage() const { return fInCoverage; }
joshualitt88c23fc2015-05-13 14:18:07 -070043 GrColor color() const { return fColor; }
joshualittb8c241a2015-05-19 08:23:30 -070044 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
45 bool hasVertexColor() const { return SkToBool(fInColor); }
joshualitte578a952015-05-14 10:09:13 -070046 const SkMatrix& viewMatrix() const { return fViewMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -070047 const SkMatrix& localMatrix() const { return fLocalMatrix; }
bsalomon7765a472015-07-08 11:26:37 -070048 bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; }
joshualitt9b989322014-12-15 14:16:27 -080049 uint8_t coverage() const { return fCoverage; }
bsalomon7765a472015-07-08 11:26:37 -070050 bool coverageWillBeIgnored() const { return fCoverageWillBeIgnored; }
joshualittb8c241a2015-05-19 08:23:30 -070051 bool hasVertexCoverage() const { return SkToBool(fInCoverage); }
joshualitt9b989322014-12-15 14:16:27 -080052
joshualitt4973d9d2014-11-08 09:24:25 -080053 class GLProcessor : public GrGLGeometryProcessor {
54 public:
joshualitt9b989322014-12-15 14:16:27 -080055 GLProcessor(const GrGeometryProcessor& gp, const GrBatchTracker&)
joshualitt5559ca22015-05-21 15:50:36 -070056 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), fCoverage(0xff) {}
joshualitt4973d9d2014-11-08 09:24:25 -080057
mtklein36352bf2015-03-25 18:17:31 -070058 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -080059 const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>();
joshualitt9b989322014-12-15 14:16:27 -080060 GrGLGPBuilder* pb = args.fPB;
robertphillips46d36f02015-01-18 08:14:14 -080061 GrGLVertexBuilder* vsBuilder = pb->getVertexShaderBuilder();
egdaniel29bee0f2015-04-29 11:54:42 -070062 GrGLFragmentBuilder* fs = args.fPB->getFragmentShaderBuilder();
joshualitt4973d9d2014-11-08 09:24:25 -080063
joshualittabb52a12015-01-13 15:02:10 -080064 // emit attributes
robertphillips46d36f02015-01-18 08:14:14 -080065 vsBuilder->emitAttributes(gp);
joshualitt2dd1ae02014-12-03 06:24:10 -080066
67 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -070068 if (!gp.colorIgnored()) {
69 if (gp.hasVertexColor()) {
70 pb->addPassThroughAttribute(gp.inColor(), args.fOutputColor);
71 } else {
72 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
73 }
74 }
75
joshualittabb52a12015-01-13 15:02:10 -080076 // Setup position
joshualitt5559ca22015-05-21 15:50:36 -070077 this->setupPosition(pb, gpArgs, gp.inPosition()->fName, gp.viewMatrix(),
78 &fViewMatrixUniform);
joshualitt4973d9d2014-11-08 09:24:25 -080079
joshualittabb52a12015-01-13 15:02:10 -080080 if (gp.inLocalCoords()) {
81 // emit transforms with explicit local coords
robertphillips46d36f02015-01-18 08:14:14 -080082 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inLocalCoords()->fName,
joshualittabb52a12015-01-13 15:02:10 -080083 gp.localMatrix(), args.fTransformsIn, args.fTransformsOut);
84 } else {
85 // emit transforms with position
robertphillips46d36f02015-01-18 08:14:14 -080086 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inPosition()->fName,
joshualittabb52a12015-01-13 15:02:10 -080087 gp.localMatrix(), args.fTransformsIn, args.fTransformsOut);
88 }
89
joshualitt2dd1ae02014-12-03 06:24:10 -080090 // Setup coverage as pass through
bsalomon7765a472015-07-08 11:26:37 -070091 if (!gp.coverageWillBeIgnored()) {
joshualittb8c241a2015-05-19 08:23:30 -070092 if (gp.hasVertexCoverage()) {
93 fs->codeAppendf("float alpha = 1.0;");
94 args.fPB->addPassThroughAttribute(gp.inCoverage(), "alpha");
95 fs->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
96 } else if (gp.coverage() == 0xff) {
97 fs->codeAppendf("%s = vec4(1);", args.fOutputCoverage);
98 } else {
99 const char* fragCoverage;
100 fCoverageUniform = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility,
101 kFloat_GrSLType,
102 kDefault_GrSLPrecision,
103 "Coverage",
104 &fragCoverage);
105 fs->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, fragCoverage);
106 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800107 }
joshualitt4973d9d2014-11-08 09:24:25 -0800108 }
109
joshualitt87f48d92014-12-04 10:41:40 -0800110 static inline void GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800111 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700112 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800113 GrProcessorKeyBuilder* b) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800114 const DefaultGeoProc& def = gp.cast<DefaultGeoProc>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800115 uint32_t key = def.fFlags;
joshualittb8c241a2015-05-19 08:23:30 -0700116 key |= def.colorIgnored() << 8;
bsalomon7765a472015-07-08 11:26:37 -0700117 key |= def.coverageWillBeIgnored() << 9;
joshualittb8c241a2015-05-19 08:23:30 -0700118 key |= def.hasVertexColor() << 10;
119 key |= def.hasVertexCoverage() << 11;
120 key |= def.coverage() == 0xff ? 0x1 << 12 : 0;
bsalomon7765a472015-07-08 11:26:37 -0700121 key |= def.localCoordsWillBeRead() && def.localMatrix().hasPerspective() ? 0x1 << 24 :
122 0x0;
joshualitte578a952015-05-14 10:09:13 -0700123 key |= ComputePosKey(def.viewMatrix()) << 25;
joshualitt8fc6c2d2014-12-22 15:27:05 -0800124 b->add32(key);
joshualitt2dd1ae02014-12-03 06:24:10 -0800125 }
joshualitt4973d9d2014-11-08 09:24:25 -0800126
joshualitt9b989322014-12-15 14:16:27 -0800127 virtual void setData(const GrGLProgramDataManager& pdman,
128 const GrPrimitiveProcessor& gp,
mtklein36352bf2015-03-25 18:17:31 -0700129 const GrBatchTracker& bt) override {
joshualitte578a952015-05-14 10:09:13 -0700130 const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>();
joshualitt5559ca22015-05-21 15:50:36 -0700131
132 if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.viewMatrix())) {
133 fViewMatrix = dgp.viewMatrix();
134 GrGLfloat viewMatrix[3 * 3];
135 GrGLGetMatrix<3>(viewMatrix, fViewMatrix);
136 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix);
137 }
joshualittee2af952014-12-30 09:04:15 -0800138
joshualittb8c241a2015-05-19 08:23:30 -0700139 if (dgp.color() != fColor && !dgp.hasVertexColor()) {
joshualitt9b989322014-12-15 14:16:27 -0800140 GrGLfloat c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700141 GrColorToRGBAFloat(dgp.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800142 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700143 fColor = dgp.color();
joshualitt9b989322014-12-15 14:16:27 -0800144 }
joshualittb8c241a2015-05-19 08:23:30 -0700145
bsalomon7765a472015-07-08 11:26:37 -0700146 if (!dgp.coverageWillBeIgnored() &&
147 dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) {
joshualittb8c241a2015-05-19 08:23:30 -0700148 pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverage()));
149 fCoverage = dgp.coverage();
joshualitt9b989322014-12-15 14:16:27 -0800150 }
151 }
joshualitt4973d9d2014-11-08 09:24:25 -0800152
joshualitte3ababe2015-05-15 07:56:07 -0700153 void setTransformData(const GrPrimitiveProcessor& primProc,
154 const GrGLProgramDataManager& pdman,
155 int index,
156 const SkTArray<const GrCoordTransform*, true>& transforms) override {
157 this->setTransformDataHelper<DefaultGeoProc>(primProc, pdman, index, transforms);
158 }
159
joshualitt4973d9d2014-11-08 09:24:25 -0800160 private:
joshualitt5559ca22015-05-21 15:50:36 -0700161 SkMatrix fViewMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800162 GrColor fColor;
163 uint8_t fCoverage;
joshualitt5559ca22015-05-21 15:50:36 -0700164 UniformHandle fViewMatrixUniform;
joshualitt9b989322014-12-15 14:16:27 -0800165 UniformHandle fColorUniform;
166 UniformHandle fCoverageUniform;
167
joshualitt4973d9d2014-11-08 09:24:25 -0800168 typedef GrGLGeometryProcessor INHERITED;
169 };
170
joshualitteb2a6762014-12-04 11:35:33 -0800171 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700172 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -0700173 GrProcessorKeyBuilder* b) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800174 GLProcessor::GenKey(*this, bt, caps, b);
175 }
176
joshualittabb52a12015-01-13 15:02:10 -0800177 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700178 const GrGLSLCaps&) const override {
joshualitteb2a6762014-12-04 11:35:33 -0800179 return SkNEW_ARGS(GLProcessor, (*this, bt));
180 }
181
joshualitt4973d9d2014-11-08 09:24:25 -0800182private:
joshualitt8059eb92014-12-29 15:10:07 -0800183 DefaultGeoProc(uint32_t gpTypeFlags,
184 GrColor color,
185 const SkMatrix& viewMatrix,
186 const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -0700187 uint8_t coverage,
bsalomon7765a472015-07-08 11:26:37 -0700188 bool localCoordsWillBeRead,
189 bool coverageWillBeIgnored)
joshualitte3ababe2015-05-15 07:56:07 -0700190 : fInPosition(NULL)
joshualitt2dd1ae02014-12-03 06:24:10 -0800191 , fInColor(NULL)
192 , fInLocalCoords(NULL)
193 , fInCoverage(NULL)
joshualitt88c23fc2015-05-13 14:18:07 -0700194 , fColor(color)
joshualitte578a952015-05-14 10:09:13 -0700195 , fViewMatrix(viewMatrix)
joshualitte3ababe2015-05-15 07:56:07 -0700196 , fLocalMatrix(localMatrix)
joshualitt9b989322014-12-15 14:16:27 -0800197 , fCoverage(coverage)
joshualittb8c241a2015-05-19 08:23:30 -0700198 , fFlags(gpTypeFlags)
bsalomon7765a472015-07-08 11:26:37 -0700199 , fLocalCoordsWillBeRead(localCoordsWillBeRead)
200 , fCoverageWillBeIgnored(coverageWillBeIgnored) {
joshualitteb2a6762014-12-04 11:35:33 -0800201 this->initClassID<DefaultGeoProc>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800202 bool hasColor = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kColor_GPType);
203 bool hasLocalCoord = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kLocalCoord_GPType);
204 bool hasCoverage = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kCoverage_GPType);
senorblancof2539d52015-05-20 14:03:42 -0700205 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType,
206 kHigh_GrSLPrecision));
joshualitt2dd1ae02014-12-03 06:24:10 -0800207 if (hasColor) {
joshualitt71c92602015-01-14 08:12:47 -0800208 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800209 }
210 if (hasLocalCoord) {
joshualitt71c92602015-01-14 08:12:47 -0800211 fInLocalCoords = &this->addVertexAttrib(Attribute("inLocalCoord",
joshualittb8c241a2015-05-19 08:23:30 -0700212 kVec2f_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800213 this->setHasLocalCoords();
214 }
215 if (hasCoverage) {
joshualitt71c92602015-01-14 08:12:47 -0800216 fInCoverage = &this->addVertexAttrib(Attribute("inCoverage",
bsalomon7765a472015-07-08 11:26:37 -0700217 kFloat_GrVertexAttribType));
joshualitt2dd1ae02014-12-03 06:24:10 -0800218 }
219 }
joshualitt4973d9d2014-11-08 09:24:25 -0800220
joshualitt71c92602015-01-14 08:12:47 -0800221 const Attribute* fInPosition;
222 const Attribute* fInColor;
223 const Attribute* fInLocalCoords;
224 const Attribute* fInCoverage;
joshualitt88c23fc2015-05-13 14:18:07 -0700225 GrColor fColor;
joshualitte578a952015-05-14 10:09:13 -0700226 SkMatrix fViewMatrix;
joshualitte3ababe2015-05-15 07:56:07 -0700227 SkMatrix fLocalMatrix;
joshualitt9b989322014-12-15 14:16:27 -0800228 uint8_t fCoverage;
joshualitt2dd1ae02014-12-03 06:24:10 -0800229 uint32_t fFlags;
bsalomon7765a472015-07-08 11:26:37 -0700230 bool fLocalCoordsWillBeRead;
231 bool fCoverageWillBeIgnored;
joshualitt4973d9d2014-11-08 09:24:25 -0800232
joshualitt2dd1ae02014-12-03 06:24:10 -0800233 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
joshualitt5478d422014-11-14 16:00:38 -0800234
joshualitt2e3b3e32014-12-09 13:31:14 -0800235 typedef GrGeometryProcessor INHERITED;
joshualitt4973d9d2014-11-08 09:24:25 -0800236};
237
238GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc);
239
joshualitt0067ff52015-07-08 14:26:19 -0700240GrGeometryProcessor* DefaultGeoProc::TestCreate(GrProcessorTestData* d) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800241 uint32_t flags = 0;
joshualitt0067ff52015-07-08 14:26:19 -0700242 if (d->fRandom->nextBool()) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800243 flags |= GrDefaultGeoProcFactory::kColor_GPType;
joshualitt4973d9d2014-11-08 09:24:25 -0800244 }
joshualitt0067ff52015-07-08 14:26:19 -0700245 if (d->fRandom->nextBool()) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800246 flags |= GrDefaultGeoProcFactory::kCoverage_GPType;
joshualitt4973d9d2014-11-08 09:24:25 -0800247 }
joshualitt0067ff52015-07-08 14:26:19 -0700248 if (d->fRandom->nextBool()) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800249 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
250 }
251
joshualitt8059eb92014-12-29 15:10:07 -0800252 return DefaultGeoProc::Create(flags,
joshualitt0067ff52015-07-08 14:26:19 -0700253 GrRandomColor(d->fRandom),
254 GrTest::TestMatrix(d->fRandom),
255 GrTest::TestMatrix(d->fRandom),
256 d->fRandom->nextBool(),
257 d->fRandom->nextBool(),
258 GrRandomCoverage(d->fRandom));
joshualitt4973d9d2014-11-08 09:24:25 -0800259}
260
joshualitte9d60952015-07-27 12:13:14 -0700261const GrGeometryProcessor* GrDefaultGeoProcFactory::Create(const Color& color,
262 const Coverage& coverage,
joshualitt0d986d82015-07-28 10:01:18 -0700263 const LocalCoords& localCoords,
264 const SkMatrix& viewMatrix) {
joshualitte9d60952015-07-27 12:13:14 -0700265 uint32_t flags = 0;
266 flags |= color.fType == Color::kAttribute_Type ? kColor_GPType : 0;
267 flags |= coverage.fType == Coverage::kAttribute_Type ? kCoverage_GPType : 0;
joshualitt0d986d82015-07-28 10:01:18 -0700268 flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoord_GPType : 0;
joshualitte9d60952015-07-27 12:13:14 -0700269
270 uint8_t inCoverage = coverage.fCoverage;
271 bool coverageWillBeIgnored = coverage.fType == Coverage::kNone_Type;
joshualitt0d986d82015-07-28 10:01:18 -0700272 bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type;
joshualitte9d60952015-07-27 12:13:14 -0700273
274 GrColor inColor = color.fColor;
275 return DefaultGeoProc::Create(flags,
276 inColor,
277 viewMatrix,
joshualitt0d986d82015-07-28 10:01:18 -0700278 localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(),
joshualitte9d60952015-07-27 12:13:14 -0700279 localCoordsWillBeRead,
280 coverageWillBeIgnored,
281 inCoverage);
282}
joshualitt0d986d82015-07-28 10:01:18 -0700283
284const GrGeometryProcessor* GrDefaultGeoProcFactory::CreateForDeviceSpace(
285 const Color& color,
286 const Coverage& coverage,
287 const LocalCoords& localCoords,
288 const SkMatrix& viewMatrix) {
joshualitt0d986d82015-07-28 10:01:18 -0700289 SkMatrix invert = SkMatrix::I();
joshualittdf0c5572015-08-03 11:35:28 -0700290 if (LocalCoords::kUnused_Type != localCoords.fType) {
291 SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType);
292 if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) {
293 SkDebugf("Could not invert\n");
294 return NULL;
295 }
joshualitt0d986d82015-07-28 10:01:18 -0700296
joshualittdf0c5572015-08-03 11:35:28 -0700297 if (localCoords.hasLocalMatrix()) {
298 invert.preConcat(*localCoords.fMatrix);
299 }
joshualitt0d986d82015-07-28 10:01:18 -0700300 }
301
302 LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert);
joshualittdf0c5572015-08-03 11:35:28 -0700303 return Create(color, coverage, inverted, SkMatrix::I());
joshualitt0d986d82015-07-28 10:01:18 -0700304}