OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrDefaultGeoProcFactory.h" | 8 #include "GrDefaultGeoProcFactory.h" |
9 | 9 |
10 #include "GrInvariantOutput.h" | 10 #include "GrInvariantOutput.h" |
11 #include "gl/GrGLGeometryProcessor.h" | 11 #include "gl/GrGLGeometryProcessor.h" |
12 #include "gl/builders/GrGLProgramBuilder.h" | 12 #include "gl/builders/GrGLProgramBuilder.h" |
13 | 13 |
14 /* | 14 /* |
15 * The default Geometry Processor simply takes position and multiplies it by the
uniform view | 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 | 16 * matrix. It also leaves coverage untouched. Behind the scenes, we may add per
vertex color or |
17 * local coords. | 17 * local coords. |
18 */ | 18 */ |
19 typedef GrDefaultGeoProcFactory Flag; | 19 typedef GrDefaultGeoProcFactory Flag; |
20 | 20 |
21 class DefaultGeoProc : public GrGeometryProcessor { | 21 class DefaultGeoProc : public GrGeometryProcessor { |
22 public: | 22 public: |
23 static GrGeometryProcessor* Create(uint32_t gpTypeFlags, | 23 static GrGeometryProcessor* Create(uint32_t gpTypeFlags, |
24 GrColor color, | 24 GrColor color, |
25 const SkMatrix& viewMatrix, | 25 const SkMatrix& viewMatrix, |
26 const SkMatrix& localMatrix, | 26 const SkMatrix& localMatrix, |
27 bool localCoordsWillBeRead, | 27 bool usesLocalCoords, |
28 bool coverageWillBeIgnored, | 28 bool coverageIgnored, |
29 uint8_t coverage) { | 29 uint8_t coverage) { |
30 return SkNEW_ARGS(DefaultGeoProc, (gpTypeFlags, | 30 return SkNEW_ARGS(DefaultGeoProc, (gpTypeFlags, |
31 color, | 31 color, |
32 viewMatrix, | 32 viewMatrix, |
33 localMatrix, | 33 localMatrix, |
34 coverage, | 34 coverage, |
35 localCoordsWillBeRead, | 35 usesLocalCoords, |
36 coverageWillBeIgnored)); | 36 coverageIgnored)); |
37 } | 37 } |
38 | 38 |
39 const char* name() const override { return "DefaultGeometryProcessor"; } | 39 const char* name() const override { return "DefaultGeometryProcessor"; } |
40 | 40 |
41 const Attribute* inPosition() const { return fInPosition; } | 41 const Attribute* inPosition() const { return fInPosition; } |
42 const Attribute* inColor() const { return fInColor; } | 42 const Attribute* inColor() const { return fInColor; } |
43 const Attribute* inLocalCoords() const { return fInLocalCoords; } | 43 const Attribute* inLocalCoords() const { return fInLocalCoords; } |
44 const Attribute* inCoverage() const { return fInCoverage; } | 44 const Attribute* inCoverage() const { return fInCoverage; } |
45 GrColor color() const { return fColor; } | 45 GrColor color() const { return fColor; } |
46 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } | 46 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; } |
47 bool hasVertexColor() const { return SkToBool(fInColor); } | 47 bool hasVertexColor() const { return SkToBool(fInColor); } |
48 const SkMatrix& viewMatrix() const { return fViewMatrix; } | 48 const SkMatrix& viewMatrix() const { return fViewMatrix; } |
49 const SkMatrix& localMatrix() const { return fLocalMatrix; } | 49 const SkMatrix& localMatrix() const { return fLocalMatrix; } |
50 bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; } | 50 bool usesLocalCoords() const { return fUsesLocalCoords; } |
51 uint8_t coverage() const { return fCoverage; } | 51 uint8_t coverage() const { return fCoverage; } |
52 bool coverageWillBeIgnored() const { return fCoverageWillBeIgnored; } | 52 bool coverageIgnored() const { return fCoverageIgnored; } |
53 bool hasVertexCoverage() const { return SkToBool(fInCoverage); } | 53 bool hasVertexCoverage() const { return SkToBool(fInCoverage); } |
54 | 54 |
55 class GLProcessor : public GrGLGeometryProcessor { | 55 class GLProcessor : public GrGLGeometryProcessor { |
56 public: | 56 public: |
57 GLProcessor(const GrGeometryProcessor& gp, const GrBatchTracker&) | 57 GLProcessor(const GrGeometryProcessor& gp, const GrBatchTracker&) |
58 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), f
Coverage(0xff) {} | 58 : fViewMatrix(SkMatrix::InvalidMatrix()), fColor(GrColor_ILLEGAL), f
Coverage(0xff) {} |
59 | 59 |
60 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { | 60 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
61 const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>(); | 61 const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>(); |
62 GrGLGPBuilder* pb = args.fPB; | 62 GrGLGPBuilder* pb = args.fPB; |
(...skipping 20 matching lines...) Expand all Loading... |
83 // emit transforms with explicit local coords | 83 // emit transforms with explicit local coords |
84 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inLocalCoords(
)->fName, | 84 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inLocalCoords(
)->fName, |
85 gp.localMatrix(), args.fTransformsIn, args.
fTransformsOut); | 85 gp.localMatrix(), args.fTransformsIn, args.
fTransformsOut); |
86 } else { | 86 } else { |
87 // emit transforms with position | 87 // emit transforms with position |
88 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inPosition()->
fName, | 88 this->emitTransforms(pb, gpArgs->fPositionVar, gp.inPosition()->
fName, |
89 gp.localMatrix(), args.fTransformsIn, args.
fTransformsOut); | 89 gp.localMatrix(), args.fTransformsIn, args.
fTransformsOut); |
90 } | 90 } |
91 | 91 |
92 // Setup coverage as pass through | 92 // Setup coverage as pass through |
93 if (!gp.coverageWillBeIgnored()) { | 93 if (!gp.coverageIgnored()) { |
94 if (gp.hasVertexCoverage()) { | 94 if (gp.hasVertexCoverage()) { |
95 fs->codeAppendf("float alpha = 1.0;"); | 95 fs->codeAppendf("float alpha = 1.0;"); |
96 args.fPB->addPassThroughAttribute(gp.inCoverage(), "alpha"); | 96 args.fPB->addPassThroughAttribute(gp.inCoverage(), "alpha"); |
97 fs->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); | 97 fs->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
98 } else if (gp.coverage() == 0xff) { | 98 } else if (gp.coverage() == 0xff) { |
99 fs->codeAppendf("%s = vec4(1);", args.fOutputCoverage); | 99 fs->codeAppendf("%s = vec4(1);", args.fOutputCoverage); |
100 } else { | 100 } else { |
101 const char* fragCoverage; | 101 const char* fragCoverage; |
102 fCoverageUniform = pb->addUniform(GrGLProgramBuilder::kFragm
ent_Visibility, | 102 fCoverageUniform = pb->addUniform(GrGLProgramBuilder::kFragm
ent_Visibility, |
103 kFloat_GrSLType, | 103 kFloat_GrSLType, |
104 kDefault_GrSLPrecision, | 104 kDefault_GrSLPrecision, |
105 "Coverage", | 105 "Coverage", |
106 &fragCoverage); | 106 &fragCoverage); |
107 fs->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, frag
Coverage); | 107 fs->codeAppendf("%s = vec4(%s);", args.fOutputCoverage, frag
Coverage); |
108 } | 108 } |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 static inline void GenKey(const GrGeometryProcessor& gp, | 112 static inline void GenKey(const GrGeometryProcessor& gp, |
113 const GrBatchTracker& bt, | 113 const GrBatchTracker& bt, |
114 const GrGLSLCaps&, | 114 const GrGLSLCaps&, |
115 GrProcessorKeyBuilder* b) { | 115 GrProcessorKeyBuilder* b) { |
116 const DefaultGeoProc& def = gp.cast<DefaultGeoProc>(); | 116 const DefaultGeoProc& def = gp.cast<DefaultGeoProc>(); |
117 uint32_t key = def.fFlags; | 117 uint32_t key = def.fFlags; |
118 key |= def.colorIgnored() << 8; | 118 key |= def.colorIgnored() << 8; |
119 key |= def.coverageWillBeIgnored() << 9; | 119 key |= def.coverageIgnored() << 9; |
120 key |= def.hasVertexColor() << 10; | 120 key |= def.hasVertexColor() << 10; |
121 key |= def.hasVertexCoverage() << 11; | 121 key |= def.hasVertexCoverage() << 11; |
122 key |= def.coverage() == 0xff ? 0x1 << 12 : 0; | 122 key |= def.coverage() == 0xff ? 0x1 << 12 : 0; |
123 key |= def.localCoordsWillBeRead() && def.localMatrix().hasPerspecti
ve() ? 0x1 << 24 : | 123 key |= def.usesLocalCoords() && def.localMatrix().hasPerspective() ?
0x1 << 24 : 0x0; |
124
0x0; | |
125 key |= ComputePosKey(def.viewMatrix()) << 25; | 124 key |= ComputePosKey(def.viewMatrix()) << 25; |
126 b->add32(key); | 125 b->add32(key); |
127 } | 126 } |
128 | 127 |
129 virtual void setData(const GrGLProgramDataManager& pdman, | 128 virtual void setData(const GrGLProgramDataManager& pdman, |
130 const GrPrimitiveProcessor& gp, | 129 const GrPrimitiveProcessor& gp, |
131 const GrBatchTracker& bt) override { | 130 const GrBatchTracker& bt) override { |
132 const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>(); | 131 const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>(); |
133 | 132 |
134 if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.
viewMatrix())) { | 133 if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.
viewMatrix())) { |
135 fViewMatrix = dgp.viewMatrix(); | 134 fViewMatrix = dgp.viewMatrix(); |
136 GrGLfloat viewMatrix[3 * 3]; | 135 GrGLfloat viewMatrix[3 * 3]; |
137 GrGLGetMatrix<3>(viewMatrix, fViewMatrix); | 136 GrGLGetMatrix<3>(viewMatrix, fViewMatrix); |
138 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); | 137 pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
139 } | 138 } |
140 | 139 |
141 if (dgp.color() != fColor && !dgp.hasVertexColor()) { | 140 if (dgp.color() != fColor && !dgp.hasVertexColor()) { |
142 GrGLfloat c[4]; | 141 GrGLfloat c[4]; |
143 GrColorToRGBAFloat(dgp.color(), c); | 142 GrColorToRGBAFloat(dgp.color(), c); |
144 pdman.set4fv(fColorUniform, 1, c); | 143 pdman.set4fv(fColorUniform, 1, c); |
145 fColor = dgp.color(); | 144 fColor = dgp.color(); |
146 } | 145 } |
147 | 146 |
148 if (!dgp.coverageWillBeIgnored() && | 147 if (!dgp.coverageIgnored() && dgp.coverage() != fCoverage && !dgp.ha
sVertexCoverage()) { |
149 dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) { | |
150 pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverag
e())); | 148 pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverag
e())); |
151 fCoverage = dgp.coverage(); | 149 fCoverage = dgp.coverage(); |
152 } | 150 } |
153 } | 151 } |
154 | 152 |
155 void setTransformData(const GrPrimitiveProcessor& primProc, | 153 void setTransformData(const GrPrimitiveProcessor& primProc, |
156 const GrGLProgramDataManager& pdman, | 154 const GrGLProgramDataManager& pdman, |
157 int index, | 155 int index, |
158 const SkTArray<const GrCoordTransform*, true>& tra
nsforms) override { | 156 const SkTArray<const GrCoordTransform*, true>& tra
nsforms) override { |
159 this->setTransformDataHelper<DefaultGeoProc>(primProc, pdman, index,
transforms); | 157 this->setTransformDataHelper<DefaultGeoProc>(primProc, pdman, index,
transforms); |
(...skipping 20 matching lines...) Expand all Loading... |
180 const GrGLSLCaps&) const ov
erride { | 178 const GrGLSLCaps&) const ov
erride { |
181 return SkNEW_ARGS(GLProcessor, (*this, bt)); | 179 return SkNEW_ARGS(GLProcessor, (*this, bt)); |
182 } | 180 } |
183 | 181 |
184 private: | 182 private: |
185 DefaultGeoProc(uint32_t gpTypeFlags, | 183 DefaultGeoProc(uint32_t gpTypeFlags, |
186 GrColor color, | 184 GrColor color, |
187 const SkMatrix& viewMatrix, | 185 const SkMatrix& viewMatrix, |
188 const SkMatrix& localMatrix, | 186 const SkMatrix& localMatrix, |
189 uint8_t coverage, | 187 uint8_t coverage, |
190 bool localCoordsWillBeRead, | 188 bool usesLocalCoords, |
191 bool coverageWillBeIgnored) | 189 bool coverageIgnored) |
192 : fInPosition(NULL) | 190 : fInPosition(NULL) |
193 , fInColor(NULL) | 191 , fInColor(NULL) |
194 , fInLocalCoords(NULL) | 192 , fInLocalCoords(NULL) |
195 , fInCoverage(NULL) | 193 , fInCoverage(NULL) |
196 , fColor(color) | 194 , fColor(color) |
197 , fViewMatrix(viewMatrix) | 195 , fViewMatrix(viewMatrix) |
198 , fLocalMatrix(localMatrix) | 196 , fLocalMatrix(localMatrix) |
199 , fCoverage(coverage) | 197 , fCoverage(coverage) |
200 , fFlags(gpTypeFlags) | 198 , fFlags(gpTypeFlags) |
201 , fLocalCoordsWillBeRead(localCoordsWillBeRead) | 199 , fUsesLocalCoords(usesLocalCoords) |
202 , fCoverageWillBeIgnored(coverageWillBeIgnored) { | 200 , fCoverageIgnored(coverageIgnored) { |
203 this->initClassID<DefaultGeoProc>(); | 201 this->initClassID<DefaultGeoProc>(); |
204 bool hasColor = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kColor_G
PType); | 202 bool hasColor = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kColor_G
PType); |
205 bool hasLocalCoord = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kLo
calCoord_GPType); | 203 bool hasLocalCoord = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kLo
calCoord_GPType); |
206 bool hasCoverage = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kCove
rage_GPType); | 204 bool hasCoverage = SkToBool(gpTypeFlags & GrDefaultGeoProcFactory::kCove
rage_GPType); |
207 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVe
rtexAttribType, | 205 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVe
rtexAttribType, |
208 kHigh_GrSLPrecision)); | 206 kHigh_GrSLPrecision)); |
209 if (hasColor) { | 207 if (hasColor) { |
210 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVer
texAttribType)); | 208 fInColor = &this->addVertexAttrib(Attribute("inColor", kVec4ub_GrVer
texAttribType)); |
211 } | 209 } |
212 if (hasLocalCoord) { | 210 if (hasLocalCoord) { |
213 fInLocalCoords = &this->addVertexAttrib(Attribute("inLocalCoord", | 211 fInLocalCoords = &this->addVertexAttrib(Attribute("inLocalCoord", |
214 kVec2f_GrVertexAtt
ribType)); | 212 kVec2f_GrVertexAtt
ribType)); |
215 this->setHasLocalCoords(); | 213 this->setHasLocalCoords(); |
216 } | 214 } |
217 if (hasCoverage) { | 215 if (hasCoverage) { |
218 fInCoverage = &this->addVertexAttrib(Attribute("inCoverage", | 216 fInCoverage = &this->addVertexAttrib(Attribute("inCoverage", |
219 kFloat_GrVertexAttrib
Type)); | 217 kFloat_GrVertexAttr
ibType)); |
220 } | 218 } |
221 } | 219 } |
222 | 220 |
223 const Attribute* fInPosition; | 221 const Attribute* fInPosition; |
224 const Attribute* fInColor; | 222 const Attribute* fInColor; |
225 const Attribute* fInLocalCoords; | 223 const Attribute* fInLocalCoords; |
226 const Attribute* fInCoverage; | 224 const Attribute* fInCoverage; |
227 GrColor fColor; | 225 GrColor fColor; |
228 SkMatrix fViewMatrix; | 226 SkMatrix fViewMatrix; |
229 SkMatrix fLocalMatrix; | 227 SkMatrix fLocalMatrix; |
230 uint8_t fCoverage; | 228 uint8_t fCoverage; |
231 uint32_t fFlags; | 229 uint32_t fFlags; |
232 bool fLocalCoordsWillBeRead; | 230 bool fUsesLocalCoords; |
233 bool fCoverageWillBeIgnored; | 231 bool fCoverageIgnored; |
234 | 232 |
235 GR_DECLARE_GEOMETRY_PROCESSOR_TEST; | 233 GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
236 | 234 |
237 typedef GrGeometryProcessor INHERITED; | 235 typedef GrGeometryProcessor INHERITED; |
238 }; | 236 }; |
239 | 237 |
240 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); | 238 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); |
241 | 239 |
242 GrGeometryProcessor* DefaultGeoProc::TestCreate(SkRandom* random, | 240 GrGeometryProcessor* DefaultGeoProc::TestCreate(SkRandom* random, |
243 GrContext*, | 241 GrContext*, |
(...skipping 14 matching lines...) Expand all Loading... |
258 GrRandomColor(random), | 256 GrRandomColor(random), |
259 GrTest::TestMatrix(random), | 257 GrTest::TestMatrix(random), |
260 GrTest::TestMatrix(random), | 258 GrTest::TestMatrix(random), |
261 random->nextBool(), | 259 random->nextBool(), |
262 random->nextBool(), | 260 random->nextBool(), |
263 GrRandomCoverage(random)); | 261 GrRandomCoverage(random)); |
264 } | 262 } |
265 | 263 |
266 const GrGeometryProcessor* GrDefaultGeoProcFactory::Create(uint32_t gpTypeFlags, | 264 const GrGeometryProcessor* GrDefaultGeoProcFactory::Create(uint32_t gpTypeFlags, |
267 GrColor color, | 265 GrColor color, |
268 bool localCoordsWillB
eRead, | 266 bool usesLocalCoords, |
269 bool coverageWillBeIg
nored, | 267 bool coverageIgnored, |
270 const SkMatrix& viewM
atrix, | 268 const SkMatrix& viewM
atrix, |
271 const SkMatrix& local
Matrix, | 269 const SkMatrix& local
Matrix, |
272 uint8_t coverage) { | 270 uint8_t coverage) { |
273 return DefaultGeoProc::Create(gpTypeFlags, | 271 return DefaultGeoProc::Create(gpTypeFlags, |
274 color, | 272 color, |
275 viewMatrix, | 273 viewMatrix, |
276 localMatrix, | 274 localMatrix, |
277 localCoordsWillBeRead, | 275 usesLocalCoords, |
278 coverageWillBeIgnored, | 276 coverageIgnored, |
279 coverage); | 277 coverage); |
280 } | 278 } |
OLD | NEW |