blob: 156e4b349e4ad8a937758db7d29be890d0479593 [file] [log] [blame]
joshualitt2771b562015-08-07 12:46:26 -07001/*
2 * Copyright 2015 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 "GrDrawVerticesBatch.h"
9
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualitt2771b562015-08-07 12:46:26 -070011#include "GrInvariantOutput.h"
12#include "GrDefaultGeoProcFactory.h"
13
14static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
joshualitt2771b562015-08-07 12:46:26 -070015 int* colorOffset,
16 int* texOffset,
joshualitt2771b562015-08-07 12:46:26 -070017 const SkMatrix& viewMatrix,
18 bool coverageIgnored) {
19 using namespace GrDefaultGeoProcFactory;
20 *texOffset = -1;
21 *colorOffset = -1;
joshualitt2771b562015-08-07 12:46:26 -070022
23 Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_Type);
24 LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type :
25 LocalCoords::kUsePosition_Type);
bsalomon14eaaa62015-09-24 07:01:26 -070026 *colorOffset = sizeof(SkPoint);
27 if (hasLocalCoords) {
joshualitt2771b562015-08-07 12:46:26 -070028 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
joshualitt2771b562015-08-07 12:46:26 -070029 }
bsalomon14eaaa62015-09-24 07:01:26 -070030 return GrDefaultGeoProcFactory::Create(Color(Color::kAttribute_Type),
31 coverage, localCoords, viewMatrix);
joshualitt2771b562015-08-07 12:46:26 -070032}
33
34GrDrawVerticesBatch::GrDrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
35 const SkMatrix& viewMatrix,
36 const SkPoint* positions, int vertexCount,
37 const uint16_t* indices, int indexCount,
38 const GrColor* colors, const SkPoint* localCoords,
reed1b55a962015-09-17 20:16:13 -070039 const SkRect& bounds)
40 : INHERITED(ClassID()) {
joshualitt2771b562015-08-07 12:46:26 -070041 SkASSERT(positions);
42
bsalomon14eaaa62015-09-24 07:01:26 -070043 fViewMatrix = viewMatrix;
joshualitt2771b562015-08-07 12:46:26 -070044 Geometry& installedGeo = fGeoData.push_back(geometry);
45
46 installedGeo.fPositions.append(vertexCount, positions);
47 if (indices) {
48 installedGeo.fIndices.append(indexCount, indices);
joshualitt2771b562015-08-07 12:46:26 -070049 }
50
51 if (colors) {
bsalomon14eaaa62015-09-24 07:01:26 -070052 fVariableColor = true;
joshualitt2771b562015-08-07 12:46:26 -070053 installedGeo.fColors.append(vertexCount, colors);
joshualitt2771b562015-08-07 12:46:26 -070054 } else {
bsalomon14eaaa62015-09-24 07:01:26 -070055 fVariableColor = false;
joshualitt2771b562015-08-07 12:46:26 -070056 }
57
58 if (localCoords) {
59 installedGeo.fLocalCoords.append(vertexCount, localCoords);
joshualitt2771b562015-08-07 12:46:26 -070060 }
bsalomon14eaaa62015-09-24 07:01:26 -070061 fVertexCount = vertexCount;
62 fIndexCount = indexCount;
63 fPrimitiveType = primitiveType;
joshualitt2771b562015-08-07 12:46:26 -070064
65 this->setBounds(bounds);
66}
67
ethannicholasff210322015-11-24 12:10:10 -080068void GrDrawVerticesBatch::computePipelineOptimizations(GrInitInvariantOutput* color,
69 GrInitInvariantOutput* coverage,
70 GrBatchToXPOverrides* overrides) const {
joshualitt2771b562015-08-07 12:46:26 -070071 // When this is called on a batch, there is only one geometry bundle
bsalomon14eaaa62015-09-24 07:01:26 -070072 if (fVariableColor) {
ethannicholasff210322015-11-24 12:10:10 -080073 color->setUnknownFourComponents();
joshualitt2771b562015-08-07 12:46:26 -070074 } else {
ethannicholasff210322015-11-24 12:10:10 -080075 color->setKnownFourComponents(fGeoData[0].fColor);
joshualitt2771b562015-08-07 12:46:26 -070076 }
ethannicholasff210322015-11-24 12:10:10 -080077 coverage->setKnownSingleComponent(0xff);
joshualitt2771b562015-08-07 12:46:26 -070078}
79
ethannicholasff210322015-11-24 12:10:10 -080080void GrDrawVerticesBatch::initBatchTracker(const GrXPOverridesForBatch& overrides) {
bsalomon14eaaa62015-09-24 07:01:26 -070081 SkASSERT(fGeoData.count() == 1);
82 GrColor overrideColor;
ethannicholasff210322015-11-24 12:10:10 -080083 if (overrides.getOverrideColorIfSet(&overrideColor)) {
bsalomon14eaaa62015-09-24 07:01:26 -070084 fGeoData[0].fColor = overrideColor;
85 fGeoData[0].fColors.reset();
86 fVariableColor = false;
joshualitt2771b562015-08-07 12:46:26 -070087 }
ethannicholasff210322015-11-24 12:10:10 -080088 fCoverageIgnored = !overrides.readsCoverage();
89 if (!overrides.readsLocalCoords()) {
bsalomon14eaaa62015-09-24 07:01:26 -070090 fGeoData[0].fLocalCoords.reset();
91 }
joshualitt2771b562015-08-07 12:46:26 -070092}
93
joshualitt144c3c82015-11-30 12:30:13 -080094void GrDrawVerticesBatch::onPrepareDraws(Target* target) const {
bsalomon14eaaa62015-09-24 07:01:26 -070095 bool hasLocalCoords = !fGeoData[0].fLocalCoords.isEmpty();
joshualitt2771b562015-08-07 12:46:26 -070096 int colorOffset = -1, texOffset = -1;
97 SkAutoTUnref<const GrGeometryProcessor> gp(
bsalomon14eaaa62015-09-24 07:01:26 -070098 set_vertex_attributes(hasLocalCoords, &colorOffset, &texOffset, fViewMatrix,
99 fCoverageIgnored));
bsalomon75398562015-08-17 12:55:38 -0700100 target->initDraw(gp, this->pipeline());
joshualitt2771b562015-08-07 12:46:26 -0700101
102 size_t vertexStride = gp->getVertexStride();
103
bsalomon14eaaa62015-09-24 07:01:26 -0700104 SkASSERT(vertexStride == sizeof(SkPoint) + (hasLocalCoords ? sizeof(SkPoint) : 0)
105 + sizeof(GrColor));
joshualitt2771b562015-08-07 12:46:26 -0700106
107 int instanceCount = fGeoData.count();
108
109 const GrVertexBuffer* vertexBuffer;
110 int firstVertex;
111
bsalomon14eaaa62015-09-24 07:01:26 -0700112 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700113
114 if (!verts) {
115 SkDebugf("Could not allocate vertices\n");
116 return;
117 }
118
halcanary96fcdcc2015-08-27 07:41:13 -0700119 const GrIndexBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700120 int firstIndex = 0;
121
halcanary96fcdcc2015-08-27 07:41:13 -0700122 uint16_t* indices = nullptr;
bsalomon14eaaa62015-09-24 07:01:26 -0700123 if (!fGeoData[0].fIndices.isEmpty()) {
124 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700125
126 if (!indices) {
127 SkDebugf("Could not allocate indices\n");
128 return;
129 }
130 }
131
132 int indexOffset = 0;
133 int vertexOffset = 0;
134 for (int i = 0; i < instanceCount; i++) {
135 const Geometry& args = fGeoData[i];
136
137 // TODO we can actually cache this interleaved and then just memcopy
bsalomon14eaaa62015-09-24 07:01:26 -0700138 if (indices) {
joshualitt2771b562015-08-07 12:46:26 -0700139 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
140 *(indices + indexOffset) = args.fIndices[j] + vertexOffset;
141 }
142 }
143
144 for (int j = 0; j < args.fPositions.count(); ++j) {
145 *((SkPoint*)verts) = args.fPositions[j];
bsalomon14eaaa62015-09-24 07:01:26 -0700146 if (args.fColors.isEmpty()) {
147 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColor;
148 } else {
joshualitt2771b562015-08-07 12:46:26 -0700149 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j];
150 }
bsalomon14eaaa62015-09-24 07:01:26 -0700151 if (hasLocalCoords) {
joshualitt2771b562015-08-07 12:46:26 -0700152 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j];
153 }
154 verts = (void*)((intptr_t)verts + vertexStride);
155 vertexOffset++;
156 }
157 }
158
159 GrVertices vertices;
bsalomon14eaaa62015-09-24 07:01:26 -0700160 if (indices) {
joshualitt2771b562015-08-07 12:46:26 -0700161 vertices.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex,
bsalomon14eaaa62015-09-24 07:01:26 -0700162 firstIndex, fVertexCount, fIndexCount);
joshualitt2771b562015-08-07 12:46:26 -0700163
164 } else {
bsalomon14eaaa62015-09-24 07:01:26 -0700165 vertices.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700166 }
bsalomon75398562015-08-17 12:55:38 -0700167 target->draw(vertices);
joshualitt2771b562015-08-07 12:46:26 -0700168}
169
bsalomoncb02b382015-08-12 11:14:50 -0700170bool GrDrawVerticesBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
bsalomonabd30f52015-08-13 13:34:48 -0700171 GrDrawVerticesBatch* that = t->cast<GrDrawVerticesBatch>();
172
173 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
174 that->bounds(), caps)) {
joshualitt2771b562015-08-07 12:46:26 -0700175 return false;
176 }
177
joshualitt2771b562015-08-07 12:46:26 -0700178 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
179 return false;
180 }
181
joshualitt2771b562015-08-07 12:46:26 -0700182 // We currently use a uniform viewmatrix for this batch
bsalomon14eaaa62015-09-24 07:01:26 -0700183 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
joshualitt2771b562015-08-07 12:46:26 -0700184 return false;
185 }
186
bsalomon14eaaa62015-09-24 07:01:26 -0700187 if (fGeoData[0].fIndices.isEmpty() != that->fGeoData[0].fIndices.isEmpty()) {
joshualitt2771b562015-08-07 12:46:26 -0700188 return false;
189 }
190
bsalomon14eaaa62015-09-24 07:01:26 -0700191 if (fGeoData[0].fLocalCoords.isEmpty() != that->fGeoData[0].fLocalCoords.isEmpty()) {
joshualitt2771b562015-08-07 12:46:26 -0700192 return false;
193 }
194
bsalomon14eaaa62015-09-24 07:01:26 -0700195 if (!fVariableColor) {
196 if (that->fVariableColor || that->fGeoData[0].fColor != fGeoData[0].fColor) {
197 fVariableColor = true;
198 }
joshualitt2771b562015-08-07 12:46:26 -0700199 }
200
joshualitt2771b562015-08-07 12:46:26 -0700201 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700202 fVertexCount += that->fVertexCount;
203 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700204
205 this->joinBounds(that->bounds());
206 return true;
207}
208
209///////////////////////////////////////////////////////////////////////////////////////////////////
210
211#ifdef GR_TEST_UTILS
212
213#include "GrBatchTest.h"
214
215static uint32_t seed_vertices(GrPrimitiveType type) {
216 switch (type) {
217 case kTriangles_GrPrimitiveType:
218 case kTriangleStrip_GrPrimitiveType:
219 case kTriangleFan_GrPrimitiveType:
220 return 3;
221 case kPoints_GrPrimitiveType:
222 return 1;
223 case kLines_GrPrimitiveType:
224 case kLineStrip_GrPrimitiveType:
225 return 2;
226 }
227 SkFAIL("Incomplete switch\n");
228 return 0;
229}
230
231static uint32_t primitive_vertices(GrPrimitiveType type) {
232 switch (type) {
233 case kTriangles_GrPrimitiveType:
234 return 3;
235 case kLines_GrPrimitiveType:
236 return 2;
237 case kTriangleStrip_GrPrimitiveType:
238 case kTriangleFan_GrPrimitiveType:
239 case kPoints_GrPrimitiveType:
240 case kLineStrip_GrPrimitiveType:
241 return 1;
242 }
243 SkFAIL("Incomplete switch\n");
244 return 0;
245}
246
247static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
248 SkPoint p;
249 p.fX = random->nextRangeScalar(min, max);
250 p.fY = random->nextRangeScalar(min, max);
251 return p;
252}
253
254static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
255 SkRandom* random,
256 SkTArray<SkPoint>* positions,
257 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
258 SkTArray<GrColor>* colors, bool hasColors,
259 SkTArray<uint16_t>* indices, bool hasIndices) {
260 for (uint32_t v = 0; v < count; v++) {
261 positions->push_back(random_point(random, min, max));
262 if (hasTexCoords) {
263 texCoords->push_back(random_point(random, min, max));
264 }
265 if (hasColors) {
266 colors->push_back(GrRandomColor(random));
267 }
268 if (hasIndices) {
269 SkASSERT(maxVertex <= SK_MaxU16);
270 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
271 }
272 }
273}
274
bsalomonabd30f52015-08-13 13:34:48 -0700275DRAW_BATCH_TEST_DEFINE(VerticesBatch) {
joshualitt2771b562015-08-07 12:46:26 -0700276 GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1));
277 uint32_t primitiveCount = random->nextRangeU(1, 100);
278
279 // TODO make 'sensible' indexbuffers
280 SkTArray<SkPoint> positions;
281 SkTArray<SkPoint> texCoords;
282 SkTArray<GrColor> colors;
283 SkTArray<uint16_t> indices;
284
285 bool hasTexCoords = random->nextBool();
286 bool hasIndices = random->nextBool();
287 bool hasColors = random->nextBool();
288
289 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
290
291 static const SkScalar kMinVertExtent = -100.f;
292 static const SkScalar kMaxVertExtent = 100.f;
293 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
294 random,
295 &positions,
296 &texCoords, hasTexCoords,
297 &colors, hasColors,
298 &indices, hasIndices);
299
300 for (uint32_t i = 1; i < primitiveCount; i++) {
301 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
302 random,
303 &positions,
304 &texCoords, hasTexCoords,
305 &colors, hasColors,
306 &indices, hasIndices);
307 }
308
309 SkMatrix viewMatrix = GrTest::TestMatrix(random);
310 SkRect bounds;
311 SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexCount);
312 SkASSERT(result);
313
314 viewMatrix.mapRect(&bounds);
315
316 GrDrawVerticesBatch::Geometry geometry;
317 geometry.fColor = GrRandomColor(random);
318 return GrDrawVerticesBatch::Create(geometry, type, viewMatrix,
319 positions.begin(), vertexCount,
320 indices.begin(), hasIndices ? vertexCount : 0,
321 colors.begin(),
322 texCoords.begin(),
323 bounds);
324}
325
326#endif