OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "GrDrawVerticesBatch.h" | 8 #include "GrDrawVerticesBatch.h" |
9 | 9 |
10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
11 #include "GrInvariantOutput.h" | 11 #include "GrInvariantOutput.h" |
12 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
13 | 13 |
14 static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, | 14 static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, |
15 bool hasColors, | |
16 int* colorOffset, | 15 int* colorOffset, |
17 int* texOffset, | 16 int* texOffset, |
18 GrColor color, | |
19 const SkMatrix& viewMatr
ix, | 17 const SkMatrix& viewMatr
ix, |
20 bool coverageIgnored) { | 18 bool coverageIgnored) { |
21 using namespace GrDefaultGeoProcFactory; | 19 using namespace GrDefaultGeoProcFactory; |
22 *texOffset = -1; | 20 *texOffset = -1; |
23 *colorOffset = -1; | 21 *colorOffset = -1; |
24 Color gpColor(color); | |
25 if (hasColors) { | |
26 gpColor.fType = Color::kAttribute_Type; | |
27 } | |
28 | 22 |
29 Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_
Type); | 23 Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_
Type); |
30 LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type : | 24 LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type : |
31 LocalCoords::kUsePosition_Type); | 25 LocalCoords::kUsePosition_Type); |
32 if (hasLocalCoords && hasColors) { | 26 *colorOffset = sizeof(SkPoint); |
33 *colorOffset = sizeof(SkPoint); | 27 if (hasLocalCoords) { |
34 *texOffset = sizeof(SkPoint) + sizeof(GrColor); | 28 *texOffset = sizeof(SkPoint) + sizeof(GrColor); |
35 } else if (hasLocalCoords) { | |
36 *texOffset = sizeof(SkPoint); | |
37 } else if (hasColors) { | |
38 *colorOffset = sizeof(SkPoint); | |
39 } | 29 } |
40 return GrDefaultGeoProcFactory::Create(gpColor, coverage, localCoords, viewM
atrix); | 30 return GrDefaultGeoProcFactory::Create(Color(Color::kAttribute_Type), |
| 31 coverage, localCoords, viewMatrix); |
41 } | 32 } |
42 | 33 |
43 GrDrawVerticesBatch::GrDrawVerticesBatch(const Geometry& geometry, GrPrimitiveTy
pe primitiveType, | 34 GrDrawVerticesBatch::GrDrawVerticesBatch(const Geometry& geometry, GrPrimitiveTy
pe primitiveType, |
44 const SkMatrix& viewMatrix, | 35 const SkMatrix& viewMatrix, |
45 const SkPoint* positions, int vertexCou
nt, | 36 const SkPoint* positions, int vertexCou
nt, |
46 const uint16_t* indices, int indexCount
, | 37 const uint16_t* indices, int indexCount
, |
47 const GrColor* colors, const SkPoint* l
ocalCoords, | 38 const GrColor* colors, const SkPoint* l
ocalCoords, |
48 const SkRect& bounds) | 39 const SkRect& bounds) |
49 : INHERITED(ClassID()) { | 40 : INHERITED(ClassID()) { |
50 SkASSERT(positions); | 41 SkASSERT(positions); |
51 | 42 |
52 fBatch.fViewMatrix = viewMatrix; | 43 fViewMatrix = viewMatrix; |
53 Geometry& installedGeo = fGeoData.push_back(geometry); | 44 Geometry& installedGeo = fGeoData.push_back(geometry); |
54 | 45 |
55 installedGeo.fPositions.append(vertexCount, positions); | 46 installedGeo.fPositions.append(vertexCount, positions); |
56 if (indices) { | 47 if (indices) { |
57 installedGeo.fIndices.append(indexCount, indices); | 48 installedGeo.fIndices.append(indexCount, indices); |
58 fBatch.fHasIndices = true; | |
59 } else { | |
60 fBatch.fHasIndices = false; | |
61 } | 49 } |
62 | 50 |
63 if (colors) { | 51 if (colors) { |
| 52 fVariableColor = true; |
64 installedGeo.fColors.append(vertexCount, colors); | 53 installedGeo.fColors.append(vertexCount, colors); |
65 fBatch.fHasColors = true; | |
66 } else { | 54 } else { |
67 fBatch.fHasColors = false; | 55 fVariableColor = false; |
68 } | 56 } |
69 | 57 |
70 if (localCoords) { | 58 if (localCoords) { |
71 installedGeo.fLocalCoords.append(vertexCount, localCoords); | 59 installedGeo.fLocalCoords.append(vertexCount, localCoords); |
72 fBatch.fHasLocalCoords = true; | |
73 } else { | |
74 fBatch.fHasLocalCoords = false; | |
75 } | 60 } |
76 fBatch.fVertexCount = vertexCount; | 61 fVertexCount = vertexCount; |
77 fBatch.fIndexCount = indexCount; | 62 fIndexCount = indexCount; |
78 fBatch.fPrimitiveType = primitiveType; | 63 fPrimitiveType = primitiveType; |
79 | 64 |
80 this->setBounds(bounds); | 65 this->setBounds(bounds); |
81 } | 66 } |
82 | 67 |
83 void GrDrawVerticesBatch::getInvariantOutputColor(GrInitInvariantOutput* out) co
nst { | 68 void GrDrawVerticesBatch::getInvariantOutputColor(GrInitInvariantOutput* out) co
nst { |
84 // When this is called on a batch, there is only one geometry bundle | 69 // When this is called on a batch, there is only one geometry bundle |
85 if (this->hasColors()) { | 70 if (fVariableColor) { |
86 out->setUnknownFourComponents(); | 71 out->setUnknownFourComponents(); |
87 } else { | 72 } else { |
88 out->setKnownFourComponents(fGeoData[0].fColor); | 73 out->setKnownFourComponents(fGeoData[0].fColor); |
89 } | 74 } |
90 } | 75 } |
91 | 76 |
92 void GrDrawVerticesBatch::getInvariantOutputCoverage(GrInitInvariantOutput* out)
const { | 77 void GrDrawVerticesBatch::getInvariantOutputCoverage(GrInitInvariantOutput* out)
const { |
93 out->setKnownSingleComponent(0xff); | 78 out->setKnownSingleComponent(0xff); |
94 } | 79 } |
95 | 80 |
96 void GrDrawVerticesBatch::initBatchTracker(const GrPipelineOptimizations& opt) { | 81 void GrDrawVerticesBatch::initBatchTracker(const GrPipelineOptimizations& opt) { |
97 // Handle any color overrides | 82 SkASSERT(fGeoData.count() == 1); |
98 if (!opt.readsColor()) { | 83 GrColor overrideColor; |
99 fGeoData[0].fColor = GrColor_ILLEGAL; | 84 if (opt.getOverrideColorIfSet(&overrideColor)) { |
| 85 fGeoData[0].fColor = overrideColor; |
| 86 fGeoData[0].fColors.reset(); |
| 87 fVariableColor = false; |
100 } | 88 } |
101 opt.getOverrideColorIfSet(&fGeoData[0].fColor); | 89 fCoverageIgnored = !opt.readsCoverage(); |
102 | 90 if (!opt.readsLocalCoords()) { |
103 // setup batch properties | 91 fGeoData[0].fLocalCoords.reset(); |
104 fBatch.fColorIgnored = !opt.readsColor(); | 92 } |
105 fBatch.fColor = fGeoData[0].fColor; | |
106 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); | |
107 fBatch.fCoverageIgnored = !opt.readsCoverage(); | |
108 } | 93 } |
109 | 94 |
110 void GrDrawVerticesBatch::onPrepareDraws(Target* target) { | 95 void GrDrawVerticesBatch::onPrepareDraws(Target* target) { |
| 96 bool hasLocalCoords = !fGeoData[0].fLocalCoords.isEmpty(); |
111 int colorOffset = -1, texOffset = -1; | 97 int colorOffset = -1, texOffset = -1; |
112 SkAutoTUnref<const GrGeometryProcessor> gp( | 98 SkAutoTUnref<const GrGeometryProcessor> gp( |
113 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &co
lorOffset, | 99 set_vertex_attributes(hasLocalCoords, &colorOffset, &texOffset, fViewMat
rix, |
114 &texOffset, this->color(), this->viewMatrix(), | 100 fCoverageIgnored)); |
115 this->coverageIgnored())); | |
116 | |
117 target->initDraw(gp, this->pipeline()); | 101 target->initDraw(gp, this->pipeline()); |
118 | 102 |
119 size_t vertexStride = gp->getVertexStride(); | 103 size_t vertexStride = gp->getVertexStride(); |
120 | 104 |
121 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(
SkPoint) : 0) | 105 SkASSERT(vertexStride == sizeof(SkPoint) + (hasLocalCoords ? sizeof(SkPoint)
: 0) |
122 + (this->hasColors() ? sizeof(GrCol
or) : 0)); | 106 + sizeof(GrColor)); |
123 | 107 |
124 int instanceCount = fGeoData.count(); | 108 int instanceCount = fGeoData.count(); |
125 | 109 |
126 const GrVertexBuffer* vertexBuffer; | 110 const GrVertexBuffer* vertexBuffer; |
127 int firstVertex; | 111 int firstVertex; |
128 | 112 |
129 void* verts = target->makeVertexSpace(vertexStride, this->vertexCount(), | 113 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuf
fer, &firstVertex); |
130 &vertexBuffer, &firstVertex); | |
131 | 114 |
132 if (!verts) { | 115 if (!verts) { |
133 SkDebugf("Could not allocate vertices\n"); | 116 SkDebugf("Could not allocate vertices\n"); |
134 return; | 117 return; |
135 } | 118 } |
136 | 119 |
137 const GrIndexBuffer* indexBuffer = nullptr; | 120 const GrIndexBuffer* indexBuffer = nullptr; |
138 int firstIndex = 0; | 121 int firstIndex = 0; |
139 | 122 |
140 uint16_t* indices = nullptr; | 123 uint16_t* indices = nullptr; |
141 if (this->hasIndices()) { | 124 if (!fGeoData[0].fIndices.isEmpty()) { |
142 indices = target->makeIndexSpace(this->indexCount(), &indexBuffer, &firs
tIndex); | 125 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex)
; |
143 | 126 |
144 if (!indices) { | 127 if (!indices) { |
145 SkDebugf("Could not allocate indices\n"); | 128 SkDebugf("Could not allocate indices\n"); |
146 return; | 129 return; |
147 } | 130 } |
148 } | 131 } |
149 | 132 |
150 int indexOffset = 0; | 133 int indexOffset = 0; |
151 int vertexOffset = 0; | 134 int vertexOffset = 0; |
152 for (int i = 0; i < instanceCount; i++) { | 135 for (int i = 0; i < instanceCount; i++) { |
153 const Geometry& args = fGeoData[i]; | 136 const Geometry& args = fGeoData[i]; |
154 | 137 |
155 // TODO we can actually cache this interleaved and then just memcopy | 138 // TODO we can actually cache this interleaved and then just memcopy |
156 if (this->hasIndices()) { | 139 if (indices) { |
157 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) { | 140 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) { |
158 *(indices + indexOffset) = args.fIndices[j] + vertexOffset; | 141 *(indices + indexOffset) = args.fIndices[j] + vertexOffset; |
159 } | 142 } |
160 } | 143 } |
161 | 144 |
162 for (int j = 0; j < args.fPositions.count(); ++j) { | 145 for (int j = 0; j < args.fPositions.count(); ++j) { |
163 *((SkPoint*)verts) = args.fPositions[j]; | 146 *((SkPoint*)verts) = args.fPositions[j]; |
164 if (this->hasColors()) { | 147 if (args.fColors.isEmpty()) { |
| 148 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColor; |
| 149 } else { |
165 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j]; | 150 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j]; |
166 } | 151 } |
167 if (this->hasLocalCoords()) { | 152 if (hasLocalCoords) { |
168 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j]; | 153 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j]; |
169 } | 154 } |
170 verts = (void*)((intptr_t)verts + vertexStride); | 155 verts = (void*)((intptr_t)verts + vertexStride); |
171 vertexOffset++; | 156 vertexOffset++; |
172 } | 157 } |
173 } | 158 } |
174 | 159 |
175 GrVertices vertices; | 160 GrVertices vertices; |
176 if (this->hasIndices()) { | 161 if (indices) { |
177 vertices.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, f
irstVertex, | 162 vertices.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, f
irstVertex, |
178 firstIndex, this->vertexCount(), this->indexCount()
); | 163 firstIndex, fVertexCount, fIndexCount); |
179 | 164 |
180 } else { | 165 } else { |
181 vertices.init(this->primitiveType(), vertexBuffer, firstVertex, this->ve
rtexCount()); | 166 vertices.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexC
ount); |
182 } | 167 } |
183 target->draw(vertices); | 168 target->draw(vertices); |
184 } | 169 } |
185 | 170 |
186 bool GrDrawVerticesBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) { | 171 bool GrDrawVerticesBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) { |
187 GrDrawVerticesBatch* that = t->cast<GrDrawVerticesBatch>(); | 172 GrDrawVerticesBatch* that = t->cast<GrDrawVerticesBatch>(); |
188 | 173 |
189 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeli
ne(), | 174 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeli
ne(), |
190 that->bounds(), caps)) { | 175 that->bounds(), caps)) { |
191 return false; | 176 return false; |
192 } | 177 } |
193 | 178 |
194 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primit
iveType()) { | 179 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primit
iveType()) { |
195 return false; | 180 return false; |
196 } | 181 } |
197 | 182 |
198 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); | |
199 | |
200 // We currently use a uniform viewmatrix for this batch | 183 // We currently use a uniform viewmatrix for this batch |
201 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) { | 184 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
202 return false; | 185 return false; |
203 } | 186 } |
204 | 187 |
205 if (this->hasColors() != that->hasColors()) { | 188 if (fGeoData[0].fIndices.isEmpty() != that->fGeoData[0].fIndices.isEmpty())
{ |
206 return false; | 189 return false; |
207 } | 190 } |
208 | 191 |
209 if (this->hasIndices() != that->hasIndices()) { | 192 if (fGeoData[0].fLocalCoords.isEmpty() != that->fGeoData[0].fLocalCoords.isE
mpty()) { |
210 return false; | 193 return false; |
211 } | 194 } |
212 | 195 |
213 if (this->hasLocalCoords() != that->hasLocalCoords()) { | 196 if (!fVariableColor) { |
214 return false; | 197 if (that->fVariableColor || that->fGeoData[0].fColor != fGeoData[0].fCol
or) { |
| 198 fVariableColor = true; |
| 199 } |
215 } | 200 } |
216 | 201 |
217 if (!this->hasColors() && this->color() != that->color()) { | |
218 return false; | |
219 } | |
220 | |
221 if (this->color() != that->color()) { | |
222 fBatch.fColor = GrColor_ILLEGAL; | |
223 } | |
224 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); | 202 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
225 fBatch.fVertexCount += that->vertexCount(); | 203 fVertexCount += that->fVertexCount; |
226 fBatch.fIndexCount += that->indexCount(); | 204 fIndexCount += that->fIndexCount; |
227 | 205 |
228 this->joinBounds(that->bounds()); | 206 this->joinBounds(that->bounds()); |
229 return true; | 207 return true; |
230 } | 208 } |
231 | 209 |
232 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 210 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
233 | 211 |
234 #ifdef GR_TEST_UTILS | 212 #ifdef GR_TEST_UTILS |
235 | 213 |
236 #include "GrBatchTest.h" | 214 #include "GrBatchTest.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 geometry.fColor = GrRandomColor(random); | 318 geometry.fColor = GrRandomColor(random); |
341 return GrDrawVerticesBatch::Create(geometry, type, viewMatrix, | 319 return GrDrawVerticesBatch::Create(geometry, type, viewMatrix, |
342 positions.begin(), vertexCount, | 320 positions.begin(), vertexCount, |
343 indices.begin(), hasIndices ? vertexCount
: 0, | 321 indices.begin(), hasIndices ? vertexCount
: 0, |
344 colors.begin(), | 322 colors.begin(), |
345 texCoords.begin(), | 323 texCoords.begin(), |
346 bounds); | 324 bounds); |
347 } | 325 } |
348 | 326 |
349 #endif | 327 #endif |
OLD | NEW |