OLD | NEW |
(Empty) | |
| 1 /* |
| 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 #ifndef GrTInstanceBatch_DEFINED |
| 9 #define GrTInstanceBatch_DEFINED |
| 10 |
| 11 #include "GrVertexBatch.h" |
| 12 |
| 13 #include "GrBatchFlushState.h" |
| 14 |
| 15 /** |
| 16 * GrTInstanceBatch is an optional template to help with writing batches |
| 17 * To use this template, The 'Impl' must define the following statics: |
| 18 * A Geometry struct |
| 19 * |
| 20 * static const int kVertsPerInstance |
| 21 * static const int kIndicesPerInstance |
| 22 * |
| 23 * const char* Name() |
| 24 * |
| 25 * bool CanCombine(const Geometry& mine, const Geometry& theirs, |
| 26 * const GrPipelineOptimizations&) |
| 27 * |
| 28 * const GrGeometryProcessor* CreateGP(const Geometry& seedGeometry, |
| 29 * const GrPipelineOptimizations& opts) |
| 30 * |
| 31 * const GrIndexBuffer* GetIndexBuffer(GrResourceProvider*) |
| 32 * |
| 33 * Tesselate(intptr_t vertices, size_t vertexStride, const Geometry& geo, |
| 34 * const GrPipelineOptimizations& opts) |
| 35 */ |
| 36 template <typename Impl> |
| 37 class GrTInstanceBatch : public GrVertexBatch { |
| 38 public: |
| 39 typedef typename Impl::Geometry Geometry; |
| 40 |
| 41 static GrTInstanceBatch* Create() { |
| 42 return SkNEW(GrTInstanceBatch); |
| 43 } |
| 44 |
| 45 const char* name() const override { return Impl::Name(); } |
| 46 |
| 47 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
| 48 // When this is called on a batch, there is only one geometry bundle |
| 49 out->setKnownFourComponents(fGeoData[0].fColor); |
| 50 } |
| 51 |
| 52 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
| 53 out->setUnknownSingleComponent(); |
| 54 } |
| 55 |
| 56 void initBatchTracker(const GrPipelineOptimizations& opt) override { |
| 57 opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
| 58 fOpts = opt; |
| 59 } |
| 60 |
| 61 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
| 62 |
| 63 // to avoid even the initial copy of the struct, we have a getter for the fi
rst item which |
| 64 // is used to seed the batch with its initial geometry. After seeding, the
client should call |
| 65 // init() so the Batch can initialize itself |
| 66 Geometry* geometry() { return &fGeoData[0]; } |
| 67 void init() { |
| 68 const Geometry& geo = fGeoData[0]; |
| 69 this->setBounds(geo.fDevRect); |
| 70 } |
| 71 |
| 72 private: |
| 73 GrTInstanceBatch() { |
| 74 this->initClassID<GrTInstanceBatch<Impl>>(); |
| 75 |
| 76 // Push back an initial geometry |
| 77 fGeoData.push_back(); |
| 78 } |
| 79 |
| 80 void onPrepareDraws(Target* target) override { |
| 81 SkAutoTUnref<const GrGeometryProcessor> gp(Impl::CreateGP(this->seedGeom
etry(), fOpts)); |
| 82 if (!gp) { |
| 83 SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 84 return; |
| 85 } |
| 86 |
| 87 target->initDraw(gp, this->pipeline()); |
| 88 |
| 89 size_t vertexStride = gp->getVertexStride(); |
| 90 int instanceCount = fGeoData.count(); |
| 91 |
| 92 SkAutoTUnref<const GrIndexBuffer> indexBuffer( |
| 93 Impl::GetIndexBuffer(target->resourceProvider())); |
| 94 InstancedHelper helper; |
| 95 void* vertices = helper.init(target, kTriangles_GrPrimitiveType, vertexS
tride, |
| 96 indexBuffer, Impl::kVertsPerInstance, |
| 97 Impl::kIndicesPerInstance, instanceCount); |
| 98 if (!vertices || !indexBuffer) { |
| 99 SkDebugf("Could not allocate vertices\n"); |
| 100 return; |
| 101 } |
| 102 |
| 103 for (int i = 0; i < instanceCount; i++) { |
| 104 intptr_t verts = reinterpret_cast<intptr_t>(vertices) + |
| 105 i * Impl::kVertsPerInstance * vertexStride; |
| 106 Impl::Tesselate(verts, vertexStride, fGeoData[i], fOpts); |
| 107 } |
| 108 helper.recordDraw(target); |
| 109 } |
| 110 |
| 111 const Geometry& seedGeometry() const { return fGeoData[0]; } |
| 112 |
| 113 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override { |
| 114 GrTInstanceBatch* that = t->cast<GrTInstanceBatch>(); |
| 115 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi
peline(), |
| 116 that->bounds(), caps)) { |
| 117 return false; |
| 118 } |
| 119 |
| 120 if (!Impl::CanCombine(this->seedGeometry(), that->seedGeometry(), fOpts)
) { |
| 121 return false; |
| 122 } |
| 123 |
| 124 // In the event of two batches, one who can tweak, one who cannot, we ju
st fall back to |
| 125 // not tweaking |
| 126 if (fOpts.canTweakAlphaForCoverage() && !that->fOpts.canTweakAlphaForCov
erage()) { |
| 127 fOpts = that->fOpts; |
| 128 } |
| 129 |
| 130 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin())
; |
| 131 this->joinBounds(that->bounds()); |
| 132 return true; |
| 133 } |
| 134 |
| 135 GrPipelineOptimizations fOpts; |
| 136 SkSTArray<1, Geometry, true> fGeoData; |
| 137 }; |
| 138 |
| 139 #endif |
OLD | NEW |