blob: 12ea05a74744579e7c9a11f01c21c72d28c41622 [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
halcanary9d524f22016-03-29 09:03:52 -070068void GrDrawVerticesBatch::computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080069 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));
joshualitt2771b562015-08-07 12:46:26 -0700100 size_t vertexStride = gp->getVertexStride();
101
bsalomon14eaaa62015-09-24 07:01:26 -0700102 SkASSERT(vertexStride == sizeof(SkPoint) + (hasLocalCoords ? sizeof(SkPoint) : 0)
103 + sizeof(GrColor));
joshualitt2771b562015-08-07 12:46:26 -0700104
105 int instanceCount = fGeoData.count();
106
cdalton397536c2016-03-25 12:15:03 -0700107 const GrBuffer* vertexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700108 int firstVertex;
109
bsalomon14eaaa62015-09-24 07:01:26 -0700110 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700111
112 if (!verts) {
113 SkDebugf("Could not allocate vertices\n");
114 return;
115 }
116
cdalton397536c2016-03-25 12:15:03 -0700117 const GrBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700118 int firstIndex = 0;
119
halcanary96fcdcc2015-08-27 07:41:13 -0700120 uint16_t* indices = nullptr;
bsalomon14eaaa62015-09-24 07:01:26 -0700121 if (!fGeoData[0].fIndices.isEmpty()) {
122 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700123
124 if (!indices) {
125 SkDebugf("Could not allocate indices\n");
126 return;
127 }
128 }
129
130 int indexOffset = 0;
131 int vertexOffset = 0;
132 for (int i = 0; i < instanceCount; i++) {
133 const Geometry& args = fGeoData[i];
134
135 // TODO we can actually cache this interleaved and then just memcopy
bsalomon14eaaa62015-09-24 07:01:26 -0700136 if (indices) {
joshualitt2771b562015-08-07 12:46:26 -0700137 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
138 *(indices + indexOffset) = args.fIndices[j] + vertexOffset;
139 }
140 }
141
142 for (int j = 0; j < args.fPositions.count(); ++j) {
143 *((SkPoint*)verts) = args.fPositions[j];
bsalomon14eaaa62015-09-24 07:01:26 -0700144 if (args.fColors.isEmpty()) {
145 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColor;
146 } else {
joshualitt2771b562015-08-07 12:46:26 -0700147 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j];
148 }
bsalomon14eaaa62015-09-24 07:01:26 -0700149 if (hasLocalCoords) {
joshualitt2771b562015-08-07 12:46:26 -0700150 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j];
151 }
152 verts = (void*)((intptr_t)verts + vertexStride);
153 vertexOffset++;
154 }
155 }
156
egdaniel0e1853c2016-03-17 11:35:45 -0700157 GrMesh mesh;
bsalomon14eaaa62015-09-24 07:01:26 -0700158 if (indices) {
egdaniel0e1853c2016-03-17 11:35:45 -0700159 mesh.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex,
160 firstIndex, fVertexCount, fIndexCount);
joshualitt2771b562015-08-07 12:46:26 -0700161
162 } else {
egdaniel0e1853c2016-03-17 11:35:45 -0700163 mesh.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700164 }
bsalomon342bfc22016-04-01 06:06:20 -0700165 target->draw(gp, mesh);
joshualitt2771b562015-08-07 12:46:26 -0700166}
167
bsalomoncb02b382015-08-12 11:14:50 -0700168bool GrDrawVerticesBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
bsalomonabd30f52015-08-13 13:34:48 -0700169 GrDrawVerticesBatch* that = t->cast<GrDrawVerticesBatch>();
170
171 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
172 that->bounds(), caps)) {
joshualitt2771b562015-08-07 12:46:26 -0700173 return false;
174 }
175
joshualitt2771b562015-08-07 12:46:26 -0700176 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
177 return false;
178 }
179
joshualitt2771b562015-08-07 12:46:26 -0700180 // We currently use a uniform viewmatrix for this batch
bsalomon14eaaa62015-09-24 07:01:26 -0700181 if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) {
joshualitt2771b562015-08-07 12:46:26 -0700182 return false;
183 }
184
bsalomon14eaaa62015-09-24 07:01:26 -0700185 if (fGeoData[0].fIndices.isEmpty() != that->fGeoData[0].fIndices.isEmpty()) {
joshualitt2771b562015-08-07 12:46:26 -0700186 return false;
187 }
188
bsalomon14eaaa62015-09-24 07:01:26 -0700189 if (fGeoData[0].fLocalCoords.isEmpty() != that->fGeoData[0].fLocalCoords.isEmpty()) {
joshualitt2771b562015-08-07 12:46:26 -0700190 return false;
191 }
192
bsalomon14eaaa62015-09-24 07:01:26 -0700193 if (!fVariableColor) {
194 if (that->fVariableColor || that->fGeoData[0].fColor != fGeoData[0].fColor) {
195 fVariableColor = true;
196 }
joshualitt2771b562015-08-07 12:46:26 -0700197 }
198
joshualitt2771b562015-08-07 12:46:26 -0700199 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700200 fVertexCount += that->fVertexCount;
201 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700202
203 this->joinBounds(that->bounds());
204 return true;
205}
206
207///////////////////////////////////////////////////////////////////////////////////////////////////
208
209#ifdef GR_TEST_UTILS
210
211#include "GrBatchTest.h"
212
213static uint32_t seed_vertices(GrPrimitiveType type) {
214 switch (type) {
215 case kTriangles_GrPrimitiveType:
216 case kTriangleStrip_GrPrimitiveType:
217 case kTriangleFan_GrPrimitiveType:
218 return 3;
219 case kPoints_GrPrimitiveType:
220 return 1;
221 case kLines_GrPrimitiveType:
222 case kLineStrip_GrPrimitiveType:
223 return 2;
224 }
225 SkFAIL("Incomplete switch\n");
226 return 0;
227}
228
229static uint32_t primitive_vertices(GrPrimitiveType type) {
230 switch (type) {
231 case kTriangles_GrPrimitiveType:
232 return 3;
233 case kLines_GrPrimitiveType:
234 return 2;
235 case kTriangleStrip_GrPrimitiveType:
236 case kTriangleFan_GrPrimitiveType:
237 case kPoints_GrPrimitiveType:
238 case kLineStrip_GrPrimitiveType:
239 return 1;
240 }
241 SkFAIL("Incomplete switch\n");
242 return 0;
243}
244
245static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
246 SkPoint p;
247 p.fX = random->nextRangeScalar(min, max);
248 p.fY = random->nextRangeScalar(min, max);
249 return p;
250}
251
252static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
253 SkRandom* random,
254 SkTArray<SkPoint>* positions,
255 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
256 SkTArray<GrColor>* colors, bool hasColors,
257 SkTArray<uint16_t>* indices, bool hasIndices) {
258 for (uint32_t v = 0; v < count; v++) {
259 positions->push_back(random_point(random, min, max));
260 if (hasTexCoords) {
261 texCoords->push_back(random_point(random, min, max));
262 }
263 if (hasColors) {
264 colors->push_back(GrRandomColor(random));
265 }
266 if (hasIndices) {
267 SkASSERT(maxVertex <= SK_MaxU16);
268 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
269 }
270 }
271}
272
bsalomonabd30f52015-08-13 13:34:48 -0700273DRAW_BATCH_TEST_DEFINE(VerticesBatch) {
joshualitt2771b562015-08-07 12:46:26 -0700274 GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1));
275 uint32_t primitiveCount = random->nextRangeU(1, 100);
276
277 // TODO make 'sensible' indexbuffers
278 SkTArray<SkPoint> positions;
279 SkTArray<SkPoint> texCoords;
280 SkTArray<GrColor> colors;
281 SkTArray<uint16_t> indices;
282
283 bool hasTexCoords = random->nextBool();
284 bool hasIndices = random->nextBool();
285 bool hasColors = random->nextBool();
286
287 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
288
289 static const SkScalar kMinVertExtent = -100.f;
290 static const SkScalar kMaxVertExtent = 100.f;
291 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
292 random,
293 &positions,
294 &texCoords, hasTexCoords,
295 &colors, hasColors,
296 &indices, hasIndices);
297
298 for (uint32_t i = 1; i < primitiveCount; i++) {
299 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
300 random,
301 &positions,
302 &texCoords, hasTexCoords,
303 &colors, hasColors,
304 &indices, hasIndices);
305 }
306
307 SkMatrix viewMatrix = GrTest::TestMatrix(random);
308 SkRect bounds;
309 SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexCount);
310 SkASSERT(result);
311
312 viewMatrix.mapRect(&bounds);
313
314 GrDrawVerticesBatch::Geometry geometry;
315 geometry.fColor = GrRandomColor(random);
316 return GrDrawVerticesBatch::Create(geometry, type, viewMatrix,
317 positions.begin(), vertexCount,
318 indices.begin(), hasIndices ? vertexCount : 0,
319 colors.begin(),
320 texCoords.begin(),
321 bounds);
322}
323
324#endif