Begin instanced rendering for simple shapes
Adds a module that performs instanced rendering and starts using it for a select subset of draws on Mac GL platforms. The instance processor can currently handle rects, ovals, round rects, and double round rects. It can generalize shapes as round rects in order to improve batching. The instance processor also employs new drawing algorithms, irrespective of instanced rendering, that improve GPU-side performance (e.g. sample mask, different triangle layouts, etc.). This change only scratches the surface of instanced rendering. The majority of draws still only have one instance. Future work may include: * Passing coord transforms through the texel buffer. * Sending FP uniforms through instanced vertex attribs. * Using instanced rendering for more draws (stencil writes, drawAtlas, etc.). * Adding more shapes to the instance processor’s repertoire. * Batching draws that have mismatched scissors (analyzing draw bounds, inserting clip planes, etc.). * Bindless textures. * Uber shaders. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2066993003 Committed: https://skia.googlesource.com/skia/+/42eafa4bc00354b132ad114d22ed6b95d8849891 Review-Url: https://codereview.chromium.org/2066993003
This commit is contained in:
parent
d5f6e9a759
commit
a7f29640f6
@ -248,6 +248,9 @@ private:
|
||||
typedef Benchmark INHERITED;
|
||||
};
|
||||
|
||||
#if ENABLE_COMMAND_LINE_SHAPES_BENCH
|
||||
DEF_BENCH(return new ShapesBench;)
|
||||
#else
|
||||
// Small primitives (CPU bound, in theory):
|
||||
DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kNone_ShapesType,
|
||||
10000, SkISize::Make(32, 32), false);)
|
||||
@ -282,7 +285,4 @@ DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kRe
|
||||
50, SkISize::Make(500, 500), false);)
|
||||
DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kRRect_ShapesType,
|
||||
50, SkISize::Make(500, 500), false);)
|
||||
|
||||
#if ENABLE_COMMAND_LINE_SHAPES_BENCH
|
||||
DEF_BENCH(return new ShapesBench;)
|
||||
#endif
|
||||
|
@ -60,6 +60,7 @@
|
||||
|
||||
# Private includes
|
||||
'<(skia_include_path)/private/GrAuditTrail.h',
|
||||
'<(skia_include_path)/private/GrInstancedPipelineInfo.h',
|
||||
'<(skia_include_path)/private/GrSingleOwner.h',
|
||||
'<(skia_include_path)/private/GrRenderTargetProxy.h',
|
||||
'<(skia_include_path)/private/GrSurfaceProxy.h',
|
||||
@ -308,6 +309,14 @@
|
||||
'<(skia_src_path)/gpu/effects/GrYUVEffect.cpp',
|
||||
'<(skia_src_path)/gpu/effects/GrYUVEffect.h',
|
||||
|
||||
'<(skia_src_path)/gpu/instanced/InstancedRendering.cpp',
|
||||
'<(skia_src_path)/gpu/instanced/InstancedRendering.h',
|
||||
'<(skia_src_path)/gpu/instanced/InstancedRenderingTypes.h',
|
||||
'<(skia_src_path)/gpu/instanced/InstanceProcessor.cpp',
|
||||
'<(skia_src_path)/gpu/instanced/InstanceProcessor.h',
|
||||
'<(skia_src_path)/gpu/instanced/GLInstancedRendering.cpp',
|
||||
'<(skia_src_path)/gpu/instanced/GLInstancedRendering.h',
|
||||
|
||||
# text
|
||||
'<(skia_src_path)/gpu/text/GrAtlasTextBlob.cpp',
|
||||
'<(skia_src_path)/gpu/text/GrAtlasTextBlob_regenInBatch.cpp',
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include "SkRefCnt.h"
|
||||
#include "SkRegion.h"
|
||||
#include "SkSurfaceProps.h"
|
||||
#include "../private/GrInstancedPipelineInfo.h"
|
||||
#include "../private/GrSingleOwner.h"
|
||||
|
||||
class GrAtlasTextContext;
|
||||
@ -346,6 +347,7 @@ private:
|
||||
GrDrawTarget* fDrawTarget;
|
||||
SkAutoTDelete<GrAtlasTextContext> fAtlasTextContext;
|
||||
GrContext* fContext;
|
||||
GrInstancedPipelineInfo fInstancedPipelineInfo;
|
||||
|
||||
SkSurfaceProps fSurfaceProps;
|
||||
GrAuditTrail* fAuditTrail;
|
||||
|
49
include/private/GrInstancedPipelineInfo.h
Normal file
49
include/private/GrInstancedPipelineInfo.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef GrGrInstancedPipelineInfo_DEFINED
|
||||
#define GrGrInstancedPipelineInfo_DEFINED
|
||||
|
||||
#include "GrRenderTarget.h"
|
||||
|
||||
/**
|
||||
* Provides info about the pipeline that GrInstancedRendering needs in order to select appropriate
|
||||
* drawing algorithms.
|
||||
*/
|
||||
struct GrInstancedPipelineInfo {
|
||||
GrInstancedPipelineInfo(const GrRenderTarget* rt)
|
||||
: fIsMultisampled(rt->isStencilBufferMultisampled()),
|
||||
fIsMixedSampled(rt->hasMixedSamples()),
|
||||
fIsRenderingToFloat(GrPixelConfigIsFloatingPoint(rt->desc().fConfig)),
|
||||
fColorDisabled(false),
|
||||
fDrawingShapeToStencil(false),
|
||||
fCanDiscard(false) {
|
||||
}
|
||||
|
||||
bool canUseCoverageAA() const {
|
||||
return !fIsMultisampled || (fIsMixedSampled && !fDrawingShapeToStencil);
|
||||
}
|
||||
|
||||
bool fIsMultisampled : 1;
|
||||
bool fIsMixedSampled : 1;
|
||||
bool fIsRenderingToFloat : 1;
|
||||
bool fColorDisabled : 1;
|
||||
/**
|
||||
* Indicates that the instanced renderer should take extra precautions to ensure the shape gets
|
||||
* drawn correctly to the stencil buffer (e.g. no coverage AA). NOTE: this does not mean a
|
||||
* stencil test is or is not active.
|
||||
*/
|
||||
bool fDrawingShapeToStencil : 1;
|
||||
/**
|
||||
* Indicates that the instanced renderer can use processors with discard instructions. This
|
||||
* should not be set if the shader will use derivatives, automatic mipmap LOD, or other features
|
||||
* that depend on neighboring pixels. Some draws will fail to create if this is not set.
|
||||
*/
|
||||
bool fCanDiscard : 1;
|
||||
};
|
||||
|
||||
#endif
|
@ -25,6 +25,8 @@
|
||||
|
||||
#include "effects/GrRRectEffect.h"
|
||||
|
||||
#include "instanced/InstancedRendering.h"
|
||||
|
||||
#include "text/GrAtlasTextContext.h"
|
||||
#include "text/GrStencilAndCoverTextContext.h"
|
||||
|
||||
@ -41,6 +43,8 @@
|
||||
#define RETURN_FALSE_IF_ABANDONED_PRIV if (fDrawContext->fDrawingManager->wasAbandoned()) { return false; }
|
||||
#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
|
||||
|
||||
using gr_instanced::InstancedRendering;
|
||||
|
||||
class AutoCheckFlush {
|
||||
public:
|
||||
AutoCheckFlush(GrDrawingManager* drawingManager) : fDrawingManager(drawingManager) {
|
||||
@ -70,6 +74,7 @@ GrDrawContext::GrDrawContext(GrContext* context,
|
||||
, fRenderTarget(std::move(rt))
|
||||
, fDrawTarget(SkSafeRef(fRenderTarget->getLastDrawTarget()))
|
||||
, fContext(context)
|
||||
, fInstancedPipelineInfo(fRenderTarget.get())
|
||||
, fSurfaceProps(SkSurfacePropsCopyOrDefault(surfaceProps))
|
||||
, fAuditTrail(auditTrail)
|
||||
#ifdef SK_DEBUG
|
||||
@ -273,23 +278,29 @@ GrDrawBatch* GrDrawContext::getFillRectBatch(const GrPaint& paint,
|
||||
const SkMatrix& viewMatrix,
|
||||
const SkRect& rect,
|
||||
bool* useHWAA) {
|
||||
if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) {
|
||||
if (GrDrawBatch* batch = ir->recordRect(rect, viewMatrix, paint.getColor(),
|
||||
paint.isAntiAlias(), fInstancedPipelineInfo,
|
||||
useHWAA)) {
|
||||
return batch;
|
||||
}
|
||||
}
|
||||
|
||||
GrDrawBatch* batch = nullptr;
|
||||
if (should_apply_coverage_aa(paint, fRenderTarget.get(), useHWAA)) {
|
||||
// The fill path can handle rotation but not skew.
|
||||
if (view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
|
||||
SkRect devBoundRect;
|
||||
viewMatrix.mapRect(&devBoundRect, rect);
|
||||
batch = GrRectBatchFactory::CreateAAFill(paint.getColor(), viewMatrix,
|
||||
rect, devBoundRect);
|
||||
return GrRectBatchFactory::CreateAAFill(paint.getColor(), viewMatrix,
|
||||
rect, devBoundRect);
|
||||
}
|
||||
} else {
|
||||
// filled BW rect
|
||||
batch = GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rect,
|
||||
nullptr, nullptr);
|
||||
return GrRectBatchFactory::CreateNonAAFill(paint.getColor(), viewMatrix, rect,
|
||||
nullptr, nullptr);
|
||||
}
|
||||
|
||||
return batch;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void GrDrawContext::drawRect(const GrClip& clip,
|
||||
@ -502,9 +513,19 @@ void GrDrawContext::fillRectToRect(const GrClip& clip,
|
||||
GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::fillRectToRect");
|
||||
|
||||
AutoCheckFlush acf(fDrawingManager);
|
||||
|
||||
bool useHWAA;
|
||||
SkAutoTUnref<GrDrawBatch> batch;
|
||||
bool useHWAA;
|
||||
|
||||
if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) {
|
||||
batch.reset(ir->recordRect(rectToDraw, viewMatrix, paint.getColor(), localRect,
|
||||
paint.isAntiAlias(), fInstancedPipelineInfo, &useHWAA));
|
||||
if (batch) {
|
||||
GrPipelineBuilder pipelineBuilder(paint, useHWAA);
|
||||
this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA) &&
|
||||
view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
|
||||
batch.reset(GrAAFillRectBatch::CreateWithLocalRect(paint.getColor(), viewMatrix, rectToDraw,
|
||||
@ -531,9 +552,19 @@ void GrDrawContext::fillRectWithLocalMatrix(const GrClip& clip,
|
||||
GR_AUDIT_TRAIL_AUTO_FRAME(fAuditTrail, "GrDrawContext::fillRectWithLocalMatrix");
|
||||
|
||||
AutoCheckFlush acf(fDrawingManager);
|
||||
|
||||
bool useHWAA;
|
||||
SkAutoTUnref<GrDrawBatch> batch;
|
||||
bool useHWAA;
|
||||
|
||||
if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) {
|
||||
batch.reset(ir->recordRect(rectToDraw, viewMatrix, paint.getColor(), localMatrix,
|
||||
paint.isAntiAlias(), fInstancedPipelineInfo, &useHWAA));
|
||||
if (batch) {
|
||||
GrPipelineBuilder pipelineBuilder(paint, useHWAA);
|
||||
this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA) &&
|
||||
view_matrix_ok_for_aa_fill_rect(viewMatrix)) {
|
||||
batch.reset(GrAAFillRectBatch::Create(paint.getColor(), viewMatrix, localMatrix,
|
||||
@ -630,13 +661,25 @@ void GrDrawContext::drawRRect(const GrClip& clip,
|
||||
}
|
||||
|
||||
SkASSERT(!style.pathEffect()); // this should've been devolved to a path in SkGpuDevice
|
||||
const SkStrokeRec stroke = style.strokeRec();
|
||||
AutoCheckFlush acf(fDrawingManager);
|
||||
|
||||
AutoCheckFlush acf(fDrawingManager);
|
||||
const SkStrokeRec stroke = style.strokeRec();
|
||||
bool useHWAA;
|
||||
|
||||
if (this->getDrawTarget()->instancedRendering() && stroke.isFillStyle()) {
|
||||
InstancedRendering* ir = this->getDrawTarget()->instancedRendering();
|
||||
SkAutoTUnref<GrDrawBatch> batch(ir->recordRRect(rrect, viewMatrix, paint.getColor(),
|
||||
paint.isAntiAlias(), fInstancedPipelineInfo,
|
||||
&useHWAA));
|
||||
if (batch) {
|
||||
GrPipelineBuilder pipelineBuilder(paint, useHWAA);
|
||||
this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) {
|
||||
GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
|
||||
|
||||
SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateRRectBatch(paint.getColor(),
|
||||
viewMatrix,
|
||||
rrect,
|
||||
@ -663,6 +706,18 @@ bool GrDrawContext::drawFilledDRRect(const GrClip& clip,
|
||||
SkASSERT(!origInner.isEmpty());
|
||||
SkASSERT(!origOuter.isEmpty());
|
||||
|
||||
if (InstancedRendering* ir = this->getDrawTarget()->instancedRendering()) {
|
||||
bool useHWAA;
|
||||
SkAutoTUnref<GrDrawBatch> batch(ir->recordDRRect(origOuter, origInner, viewMatrix,
|
||||
paintIn.getColor(), paintIn.isAntiAlias(),
|
||||
fInstancedPipelineInfo, &useHWAA));
|
||||
if (batch) {
|
||||
GrPipelineBuilder pipelineBuilder(paintIn, useHWAA);
|
||||
this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool applyAA = paintIn.isAntiAlias() && !fRenderTarget->isUnifiedMultisampled();
|
||||
|
||||
GrPrimitiveEdgeType innerEdgeType = applyAA ? kInverseFillAA_GrProcessorEdgeType :
|
||||
@ -761,6 +816,19 @@ void GrDrawContext::drawOval(const GrClip& clip,
|
||||
AutoCheckFlush acf(fDrawingManager);
|
||||
const SkStrokeRec& stroke = style.strokeRec();
|
||||
bool useHWAA;
|
||||
|
||||
if (this->getDrawTarget()->instancedRendering() && stroke.isFillStyle()) {
|
||||
InstancedRendering* ir = this->getDrawTarget()->instancedRendering();
|
||||
SkAutoTUnref<GrDrawBatch> batch(ir->recordOval(oval, viewMatrix, paint.getColor(),
|
||||
paint.isAntiAlias(), fInstancedPipelineInfo,
|
||||
&useHWAA));
|
||||
if (batch) {
|
||||
GrPipelineBuilder pipelineBuilder(paint, useHWAA);
|
||||
this->getDrawTarget()->drawBatch(pipelineBuilder, this, clip, batch);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (should_apply_coverage_aa(paint, fRenderTarget.get(), &useHWAA)) {
|
||||
GrShaderCaps* shaderCaps = fContext->caps()->shaderCaps();
|
||||
SkAutoTUnref<GrDrawBatch> batch(GrOvalRenderer::CreateOvalBatch(paint.getColor(),
|
||||
|
@ -9,6 +9,7 @@
|
||||
#define GrDrawContextPriv_DEFINED
|
||||
|
||||
#include "GrDrawContext.h"
|
||||
#include "GrDrawTarget.h"
|
||||
#include "GrPathRendering.h"
|
||||
|
||||
class GrFixedClip;
|
||||
@ -20,6 +21,10 @@ struct GrUserStencilSettings;
|
||||
data members or virtual methods. */
|
||||
class GrDrawContextPriv {
|
||||
public:
|
||||
gr_instanced::InstancedRendering* accessInstancedRendering() const {
|
||||
return fDrawContext->getDrawTarget()->instancedRendering();
|
||||
}
|
||||
|
||||
void clearStencilClip(const SkIRect& rect, bool insideClip);
|
||||
|
||||
void stencilRect(const GrFixedClip& clip,
|
||||
|
@ -33,6 +33,8 @@
|
||||
#include "batches/GrRectBatchFactory.h"
|
||||
#include "batches/GrStencilPathBatch.h"
|
||||
|
||||
#include "instanced/InstancedRendering.h"
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Experimentally we have found that most batching occurs within the first 10 comparisons.
|
||||
@ -45,7 +47,8 @@ GrDrawTarget::GrDrawTarget(GrRenderTarget* rt, GrGpu* gpu, GrResourceProvider* r
|
||||
, fResourceProvider(resourceProvider)
|
||||
, fAuditTrail(auditTrail)
|
||||
, fFlags(0)
|
||||
, fRenderTarget(rt) {
|
||||
, fRenderTarget(rt)
|
||||
, fInstancedRendering(fGpu->createInstancedRenderingIfSupported()) {
|
||||
// TODO: Stop extracting the context (currently needed by GrClipMaskManager)
|
||||
fContext = fGpu->getContext();
|
||||
|
||||
@ -201,6 +204,10 @@ void GrDrawTarget::prepareBatches(GrBatchFlushState* flushState) {
|
||||
fBatches[i]->prepare(flushState);
|
||||
}
|
||||
}
|
||||
|
||||
if (fInstancedRendering) {
|
||||
fInstancedRendering->beginFlush(flushState->resourceProvider());
|
||||
}
|
||||
}
|
||||
|
||||
void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
|
||||
@ -269,6 +276,9 @@ void GrDrawTarget::drawBatches(GrBatchFlushState* flushState) {
|
||||
|
||||
void GrDrawTarget::reset() {
|
||||
fBatches.reset();
|
||||
if (fInstancedRendering) {
|
||||
fInstancedRendering->endFlush();
|
||||
}
|
||||
}
|
||||
|
||||
void GrDrawTarget::drawBatch(const GrPipelineBuilder& pipelineBuilder,
|
||||
|
@ -144,6 +144,11 @@ public:
|
||||
const SkIRect& srcRect,
|
||||
const SkIPoint& dstPoint);
|
||||
|
||||
/**
|
||||
* Gets the shape rendering object if it is supported on this platform.
|
||||
*/
|
||||
gr_instanced::InstancedRendering* instancedRendering() const { return fInstancedRendering; }
|
||||
|
||||
private:
|
||||
friend class GrDrawingManager; // for resetFlag & TopoSortTraits
|
||||
friend class GrDrawContextPriv; // for clearStencilClip
|
||||
@ -209,24 +214,26 @@ private:
|
||||
// Used only by drawContextPriv.
|
||||
void clearStencilClip(const SkIRect&, bool insideClip, GrRenderTarget*);
|
||||
|
||||
SkSTArray<256, SkAutoTUnref<GrBatch>, true> fBatches;
|
||||
SkSTArray<256, SkAutoTUnref<GrBatch>, true> fBatches;
|
||||
// The context is only in service of the clip mask manager, remove once CMM doesn't need this.
|
||||
GrContext* fContext;
|
||||
GrGpu* fGpu;
|
||||
GrResourceProvider* fResourceProvider;
|
||||
GrAuditTrail* fAuditTrail;
|
||||
GrContext* fContext;
|
||||
GrGpu* fGpu;
|
||||
GrResourceProvider* fResourceProvider;
|
||||
GrAuditTrail* fAuditTrail;
|
||||
|
||||
SkDEBUGCODE(int fDebugID;)
|
||||
uint32_t fFlags;
|
||||
SkDEBUGCODE(int fDebugID;)
|
||||
uint32_t fFlags;
|
||||
|
||||
// 'this' drawTarget relies on the output of the drawTargets in 'fDependencies'
|
||||
SkTDArray<GrDrawTarget*> fDependencies;
|
||||
GrRenderTarget* fRenderTarget;
|
||||
SkTDArray<GrDrawTarget*> fDependencies;
|
||||
GrRenderTarget* fRenderTarget;
|
||||
|
||||
bool fClipBatchToBounds;
|
||||
bool fDrawBatchBounds;
|
||||
int fMaxBatchLookback;
|
||||
int fMaxBatchLookahead;
|
||||
bool fClipBatchToBounds;
|
||||
bool fDrawBatchBounds;
|
||||
int fMaxBatchLookback;
|
||||
int fMaxBatchLookahead;
|
||||
|
||||
SkAutoTDelete<gr_instanced::InstancedRendering> fInstancedRendering;
|
||||
|
||||
typedef SkRefCnt INHERITED;
|
||||
};
|
||||
|
@ -13,9 +13,13 @@
|
||||
#include "GrSoftwarePathRenderer.h"
|
||||
#include "SkTTopoSort.h"
|
||||
|
||||
#include "instanced/InstancedRendering.h"
|
||||
|
||||
#include "text/GrAtlasTextContext.h"
|
||||
#include "text/GrStencilAndCoverTextContext.h"
|
||||
|
||||
using gr_instanced::InstancedRendering;
|
||||
|
||||
void GrDrawingManager::cleanup() {
|
||||
for (int i = 0; i < fDrawTargets.count(); ++i) {
|
||||
fDrawTargets[i]->makeClosed(); // no drawTarget should receive a new command after this
|
||||
@ -40,6 +44,11 @@ GrDrawingManager::~GrDrawingManager() {
|
||||
|
||||
void GrDrawingManager::abandon() {
|
||||
fAbandoned = true;
|
||||
for (int i = 0; i < fDrawTargets.count(); ++i) {
|
||||
if (InstancedRendering* ir = fDrawTargets[i]->instancedRendering()) {
|
||||
ir->resetGpuResources(InstancedRendering::ResetType::kAbandon);
|
||||
}
|
||||
}
|
||||
this->cleanup();
|
||||
}
|
||||
|
||||
@ -48,6 +57,11 @@ void GrDrawingManager::freeGpuResources() {
|
||||
delete fPathRendererChain;
|
||||
fPathRendererChain = nullptr;
|
||||
SkSafeSetNull(fSoftwarePathRenderer);
|
||||
for (int i = 0; i < fDrawTargets.count(); ++i) {
|
||||
if (InstancedRendering* ir = fDrawTargets[i]->instancedRendering()) {
|
||||
ir->resetGpuResources(InstancedRendering::ResetType::kDestroy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GrDrawingManager::reset() {
|
||||
|
@ -39,6 +39,8 @@ class GrStencilSettings;
|
||||
class GrSurface;
|
||||
class GrTexture;
|
||||
|
||||
namespace gr_instanced { class InstancedRendering; }
|
||||
|
||||
class GrGpu : public SkRefCnt {
|
||||
public:
|
||||
/**
|
||||
@ -147,6 +149,13 @@ public:
|
||||
GrBuffer* createBuffer(size_t size, GrBufferType intendedType, GrAccessPattern accessPattern,
|
||||
const void* data = nullptr);
|
||||
|
||||
/**
|
||||
* Creates an instanced rendering object if it is supported on this platform.
|
||||
*/
|
||||
virtual gr_instanced::InstancedRendering* createInstancedRenderingIfSupported() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves MSAA.
|
||||
*/
|
||||
@ -591,6 +600,7 @@ private:
|
||||
GrContext* fContext;
|
||||
|
||||
friend class GrPathRendering;
|
||||
friend class gr_instanced::InstancedRendering;
|
||||
typedef SkRefCnt INHERITED;
|
||||
};
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "glsl/GrGLSL.h"
|
||||
#include "glsl/GrGLSLCaps.h"
|
||||
#include "glsl/GrGLSLPLSPathRendering.h"
|
||||
#include "instanced/GLInstancedRendering.h"
|
||||
#include "SkMipMap.h"
|
||||
#include "SkPixmap.h"
|
||||
#include "SkStrokeRec.h"
|
||||
@ -46,6 +47,8 @@
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
using gr_instanced::InstancedRendering;
|
||||
using gr_instanced::GLInstancedRendering;
|
||||
|
||||
static const GrGLenum gXfermodeEquation2Blend[] = {
|
||||
// Basic OpenGL blend equations.
|
||||
@ -475,6 +478,10 @@ void GrGLGpu::disconnect(DisconnectType type) {
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
InstancedRendering* GrGLGpu::createInstancedRenderingIfSupported() {
|
||||
return GLInstancedRendering::CreateIfSupported(this);
|
||||
}
|
||||
|
||||
void GrGLGpu::onResetContext(uint32_t resetBits) {
|
||||
// we don't use the zb at all
|
||||
if (resetBits & kMisc_GrGLBackendState) {
|
||||
|
@ -28,6 +28,8 @@ class GrPipeline;
|
||||
class GrNonInstancedMesh;
|
||||
class GrSwizzle;
|
||||
|
||||
namespace gr_instanced { class GLInstancedRendering; }
|
||||
|
||||
#ifdef SK_DEBUG
|
||||
#define PROGRAM_CACHE_STATS
|
||||
#endif
|
||||
@ -54,6 +56,8 @@ public:
|
||||
return static_cast<GrGLPathRendering*>(pathRendering());
|
||||
}
|
||||
|
||||
gr_instanced::InstancedRendering* createInstancedRenderingIfSupported() override;
|
||||
|
||||
// Used by GrGLProgram to configure OpenGL state.
|
||||
void bindTexture(int unitIdx, const GrTextureParams& params, bool allowSRGBInputs,
|
||||
GrGLTexture* texture);
|
||||
@ -595,6 +599,7 @@ private:
|
||||
|
||||
typedef GrGpu INHERITED;
|
||||
friend class GrGLPathRendering; // For accessing setTextureUnit.
|
||||
friend class gr_instanced::GLInstancedRendering; // For accessing flushGLState.
|
||||
};
|
||||
|
||||
#endif
|
||||
|
301
src/gpu/instanced/GLInstancedRendering.cpp
Normal file
301
src/gpu/instanced/GLInstancedRendering.cpp
Normal file
@ -0,0 +1,301 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "GLInstancedRendering.h"
|
||||
|
||||
#include "GrResourceProvider.h"
|
||||
#include "gl/GrGLGpu.h"
|
||||
#include "instanced/InstanceProcessor.h"
|
||||
|
||||
#define GL_CALL(X) GR_GL_CALL(this->glGpu()->glInterface(), X)
|
||||
|
||||
namespace gr_instanced {
|
||||
|
||||
class GLInstancedRendering::GLBatch : public InstancedRendering::Batch {
|
||||
public:
|
||||
DEFINE_BATCH_CLASS_ID
|
||||
|
||||
GLBatch(GLInstancedRendering* instRendering) : INHERITED(ClassID(), instRendering) {}
|
||||
int numGLCommands() const { return 1 + fNumChangesInGeometry; }
|
||||
|
||||
private:
|
||||
int fEmulatedBaseInstance;
|
||||
int fGLDrawCmdsIdx;
|
||||
|
||||
friend class GLInstancedRendering;
|
||||
|
||||
typedef Batch INHERITED;
|
||||
};
|
||||
|
||||
GLInstancedRendering* GLInstancedRendering::CreateIfSupported(GrGLGpu* gpu) {
|
||||
#ifndef SK_BUILD_FOR_MAC
|
||||
// Only whitelisting on Mac for now. Once we've been able to work through the various issues on
|
||||
// other platforms we can enable more generally.
|
||||
return nullptr;
|
||||
#endif
|
||||
const GrGLCaps& glCaps = gpu->glCaps();
|
||||
AntialiasMode lastSupportedAAMode;
|
||||
if (!glCaps.vertexArrayObjectSupport() ||
|
||||
!glCaps.drawIndirectSupport() ||
|
||||
!InstanceProcessor::IsSupported(*glCaps.glslCaps(), glCaps, &lastSupportedAAMode)) {
|
||||
return nullptr;
|
||||
}
|
||||
return new GLInstancedRendering(gpu, lastSupportedAAMode);
|
||||
}
|
||||
|
||||
GLInstancedRendering::GLInstancedRendering(GrGLGpu* gpu, AntialiasMode lastSupportedAAMode)
|
||||
: INHERITED(gpu, lastSupportedAAMode, gpu->glCaps().canDrawIndirectToFloat()),
|
||||
fVertexArrayID(0),
|
||||
fGLDrawCmdsInfo(0),
|
||||
fInstanceAttribsBufferUniqueId(SK_InvalidUniqueID) {
|
||||
}
|
||||
|
||||
GLInstancedRendering::~GLInstancedRendering() {
|
||||
if (fVertexArrayID) {
|
||||
GL_CALL(DeleteVertexArrays(1, &fVertexArrayID));
|
||||
this->glGpu()->notifyVertexArrayDelete(fVertexArrayID);
|
||||
}
|
||||
}
|
||||
|
||||
inline GrGLGpu* GLInstancedRendering::glGpu() const {
|
||||
return static_cast<GrGLGpu*>(this->gpu());
|
||||
}
|
||||
|
||||
InstancedRendering::Batch* GLInstancedRendering::createBatch() {
|
||||
return new GLBatch(this);
|
||||
}
|
||||
|
||||
void GLInstancedRendering::onBeginFlush(GrResourceProvider* rp) {
|
||||
// Count what there is to draw.
|
||||
BatchList::Iter iter;
|
||||
iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart);
|
||||
int numGLInstances = 0;
|
||||
int numGLDrawCmds = 0;
|
||||
while (Batch* b = iter.get()) {
|
||||
GLBatch* batch = static_cast<GLBatch*>(b);
|
||||
iter.next();
|
||||
|
||||
numGLInstances += batch->fNumDraws;
|
||||
numGLDrawCmds += batch->numGLCommands();
|
||||
}
|
||||
if (!numGLDrawCmds) {
|
||||
return;
|
||||
}
|
||||
SkASSERT(numGLInstances);
|
||||
|
||||
// Lazily create a vertex array object.
|
||||
if (!fVertexArrayID) {
|
||||
GL_CALL(GenVertexArrays(1, &fVertexArrayID));
|
||||
if (!fVertexArrayID) {
|
||||
return;
|
||||
}
|
||||
this->glGpu()->bindVertexArray(fVertexArrayID);
|
||||
|
||||
// Attach our index buffer to the vertex array.
|
||||
GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER,
|
||||
static_cast<const GrGLBuffer*>(this->indexBuffer())->bufferID()));
|
||||
|
||||
// Set up the non-instanced attribs.
|
||||
this->glGpu()->bindBuffer(kVertex_GrBufferType,
|
||||
static_cast<const GrGLBuffer*>(this->vertexBuffer()));
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeCoords));
|
||||
GL_CALL(VertexAttribPointer((int)Attrib::kShapeCoords, 2, GR_GL_FLOAT, GR_GL_FALSE,
|
||||
sizeof(ShapeVertex), (void*) offsetof(ShapeVertex, fX)));
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kVertexAttrs));
|
||||
GL_CALL(VertexAttribIPointer((int)Attrib::kVertexAttrs, 1, GR_GL_INT, sizeof(ShapeVertex),
|
||||
(void*) offsetof(ShapeVertex, fAttrs)));
|
||||
|
||||
SkASSERT(SK_InvalidUniqueID == fInstanceAttribsBufferUniqueId);
|
||||
}
|
||||
|
||||
// Create and map instance and draw-indirect buffers.
|
||||
SkASSERT(!fInstanceBuffer);
|
||||
fInstanceBuffer.reset(static_cast<GrGLBuffer*>(
|
||||
rp->createBuffer(sizeof(Instance) * numGLInstances, kVertex_GrBufferType,
|
||||
kDynamic_GrAccessPattern, GrResourceProvider::kNoPendingIO_Flag)));
|
||||
if (!fInstanceBuffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
SkASSERT(!fDrawIndirectBuffer);
|
||||
fDrawIndirectBuffer.reset(static_cast<GrGLBuffer*>(
|
||||
rp->createBuffer(sizeof(GrGLDrawElementsIndirectCommand) * numGLDrawCmds,
|
||||
kDrawIndirect_GrBufferType, kDynamic_GrAccessPattern,
|
||||
GrResourceProvider::kNoPendingIO_Flag)));
|
||||
if (!fDrawIndirectBuffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
Instance* glMappedInstances = static_cast<Instance*>(fInstanceBuffer->map());
|
||||
int glInstancesIdx = 0;
|
||||
|
||||
auto* glMappedCmds = static_cast<GrGLDrawElementsIndirectCommand*>(fDrawIndirectBuffer->map());
|
||||
int glDrawCmdsIdx = 0;
|
||||
|
||||
bool baseInstanceSupport = this->glGpu()->glCaps().baseInstanceSupport();
|
||||
|
||||
if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) {
|
||||
fGLDrawCmdsInfo.reset(numGLDrawCmds);
|
||||
}
|
||||
|
||||
// Generate the instance and draw-indirect buffer contents based on the tracked batches.
|
||||
iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart);
|
||||
while (Batch* b = iter.get()) {
|
||||
GLBatch* batch = static_cast<GLBatch*>(b);
|
||||
iter.next();
|
||||
|
||||
batch->fEmulatedBaseInstance = baseInstanceSupport ? 0 : glInstancesIdx;
|
||||
batch->fGLDrawCmdsIdx = glDrawCmdsIdx;
|
||||
|
||||
const Batch::Draw* draw = batch->fHeadDraw;
|
||||
SkASSERT(draw);
|
||||
do {
|
||||
int instanceCount = 0;
|
||||
IndexRange geometry = draw->fGeometry;
|
||||
SkASSERT(!geometry.isEmpty());
|
||||
|
||||
do {
|
||||
glMappedInstances[glInstancesIdx + instanceCount++] = draw->fInstance;
|
||||
draw = draw->fNext;
|
||||
} while (draw && draw->fGeometry == geometry);
|
||||
|
||||
GrGLDrawElementsIndirectCommand& glCmd = glMappedCmds[glDrawCmdsIdx];
|
||||
glCmd.fCount = geometry.fCount;
|
||||
glCmd.fInstanceCount = instanceCount;
|
||||
glCmd.fFirstIndex = geometry.fStart;
|
||||
glCmd.fBaseVertex = 0;
|
||||
glCmd.fBaseInstance = baseInstanceSupport ? glInstancesIdx : 0;
|
||||
|
||||
if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) {
|
||||
fGLDrawCmdsInfo[glDrawCmdsIdx].fInstanceCount = instanceCount;
|
||||
#if GR_GL_LOG_INSTANCED_BATCHES
|
||||
fGLDrawCmdsInfo[glDrawCmdsIdx].fGeometry = geometry;
|
||||
#endif
|
||||
}
|
||||
|
||||
glInstancesIdx += instanceCount;
|
||||
++glDrawCmdsIdx;
|
||||
} while (draw);
|
||||
}
|
||||
|
||||
SkASSERT(glDrawCmdsIdx == numGLDrawCmds);
|
||||
fDrawIndirectBuffer->unmap();
|
||||
|
||||
SkASSERT(glInstancesIdx == numGLInstances);
|
||||
fInstanceBuffer->unmap();
|
||||
}
|
||||
|
||||
void GLInstancedRendering::onDraw(const GrPipeline& pipeline, const InstanceProcessor& instProc,
|
||||
const Batch* baseBatch) {
|
||||
if (!fDrawIndirectBuffer) {
|
||||
return; // beginFlush was not successful.
|
||||
}
|
||||
if (!this->glGpu()->flushGLState(pipeline, instProc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this->glGpu()->bindBuffer(kDrawIndirect_GrBufferType, fDrawIndirectBuffer.get());
|
||||
|
||||
const GrGLCaps& glCaps = this->glGpu()->glCaps();
|
||||
const GLBatch* batch = static_cast<const GLBatch*>(baseBatch);
|
||||
int numCommands = batch->numGLCommands();
|
||||
|
||||
#if GR_GL_LOG_INSTANCED_BATCHES
|
||||
SkASSERT(fGLDrawCmdsInfo);
|
||||
SkDebugf("Instanced batch: [");
|
||||
for (int i = 0; i < numCommands; ++i) {
|
||||
int glCmdIdx = batch->fGLDrawCmdsIdx + i;
|
||||
SkDebugf("%s%i * %s", (i ? ", " : ""), fGLDrawCmdsInfo[glCmdIdx].fInstanceCount,
|
||||
InstanceProcessor::GetNameOfIndexRange(fGLDrawCmdsInfo[glCmdIdx].fGeometry));
|
||||
}
|
||||
SkDebugf("]\n");
|
||||
#else
|
||||
SkASSERT(SkToBool(fGLDrawCmdsInfo) == !glCaps.baseInstanceSupport());
|
||||
#endif
|
||||
|
||||
if (1 == numCommands || !glCaps.baseInstanceSupport() || !glCaps.multiDrawIndirectSupport()) {
|
||||
int emulatedBaseInstance = batch->fEmulatedBaseInstance;
|
||||
for (int i = 0; i < numCommands; ++i) {
|
||||
int glCmdIdx = batch->fGLDrawCmdsIdx + i;
|
||||
this->flushInstanceAttribs(emulatedBaseInstance);
|
||||
GL_CALL(DrawElementsIndirect(GR_GL_TRIANGLES, GR_GL_UNSIGNED_BYTE,
|
||||
(GrGLDrawElementsIndirectCommand*) nullptr + glCmdIdx));
|
||||
if (!glCaps.baseInstanceSupport()) {
|
||||
emulatedBaseInstance += fGLDrawCmdsInfo[glCmdIdx].fInstanceCount;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
int glCmdsIdx = batch->fGLDrawCmdsIdx;
|
||||
this->flushInstanceAttribs(batch->fEmulatedBaseInstance);
|
||||
GL_CALL(MultiDrawElementsIndirect(GR_GL_TRIANGLES, GR_GL_UNSIGNED_BYTE,
|
||||
(GrGLDrawElementsIndirectCommand*) nullptr + glCmdsIdx,
|
||||
numCommands, 0));
|
||||
}
|
||||
}
|
||||
|
||||
void GLInstancedRendering::flushInstanceAttribs(int baseInstance) {
|
||||
SkASSERT(fVertexArrayID);
|
||||
this->glGpu()->bindVertexArray(fVertexArrayID);
|
||||
|
||||
SkASSERT(fInstanceBuffer);
|
||||
if (fInstanceAttribsBufferUniqueId != fInstanceBuffer->getUniqueID() ||
|
||||
fInstanceAttribsBaseInstance != baseInstance) {
|
||||
Instance* offsetInBuffer = (Instance*) nullptr + baseInstance;
|
||||
|
||||
this->glGpu()->bindBuffer(kVertex_GrBufferType, fInstanceBuffer.get());
|
||||
|
||||
// Info attrib.
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kInstanceInfo));
|
||||
GL_CALL(VertexAttribIPointer((int)Attrib::kInstanceInfo, 1, GR_GL_UNSIGNED_INT,
|
||||
sizeof(Instance), &offsetInBuffer->fInfo));
|
||||
GL_CALL(VertexAttribDivisor((int)Attrib::kInstanceInfo, 1));
|
||||
|
||||
// Shape matrix attrib.
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeMatrixX));
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeMatrixY));
|
||||
GL_CALL(VertexAttribPointer((int)Attrib::kShapeMatrixX, 3, GR_GL_FLOAT, GR_GL_FALSE,
|
||||
sizeof(Instance), &offsetInBuffer->fShapeMatrix2x3[0]));
|
||||
GL_CALL(VertexAttribPointer((int)Attrib::kShapeMatrixY, 3, GR_GL_FLOAT, GR_GL_FALSE,
|
||||
sizeof(Instance), &offsetInBuffer->fShapeMatrix2x3[3]));
|
||||
GL_CALL(VertexAttribDivisor((int)Attrib::kShapeMatrixX, 1));
|
||||
GL_CALL(VertexAttribDivisor((int)Attrib::kShapeMatrixY, 1));
|
||||
|
||||
// Color attrib.
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kColor));
|
||||
GL_CALL(VertexAttribPointer((int)Attrib::kColor, 4, GR_GL_UNSIGNED_BYTE, GR_GL_TRUE,
|
||||
sizeof(Instance), &offsetInBuffer->fColor));
|
||||
GL_CALL(VertexAttribDivisor((int)Attrib::kColor, 1));
|
||||
|
||||
// Local rect attrib.
|
||||
GL_CALL(EnableVertexAttribArray((int)Attrib::kLocalRect));
|
||||
GL_CALL(VertexAttribPointer((int)Attrib::kLocalRect, 4, GR_GL_FLOAT, GR_GL_FALSE,
|
||||
sizeof(Instance), &offsetInBuffer->fLocalRect));
|
||||
GL_CALL(VertexAttribDivisor((int)Attrib::kLocalRect, 1));
|
||||
|
||||
fInstanceAttribsBufferUniqueId = fInstanceBuffer->getUniqueID();
|
||||
fInstanceAttribsBaseInstance = baseInstance;
|
||||
}
|
||||
}
|
||||
|
||||
void GLInstancedRendering::onEndFlush() {
|
||||
fInstanceBuffer.reset();
|
||||
fDrawIndirectBuffer.reset();
|
||||
fGLDrawCmdsInfo.reset(0);
|
||||
}
|
||||
|
||||
void GLInstancedRendering::onResetGpuResources(ResetType resetType) {
|
||||
if (fVertexArrayID && ResetType::kDestroy == resetType) {
|
||||
GL_CALL(DeleteVertexArrays(1, &fVertexArrayID));
|
||||
this->glGpu()->notifyVertexArrayDelete(fVertexArrayID);
|
||||
}
|
||||
fVertexArrayID = 0;
|
||||
fInstanceBuffer.reset();
|
||||
fDrawIndirectBuffer.reset();
|
||||
fInstanceAttribsBufferUniqueId = SK_InvalidUniqueID;
|
||||
}
|
||||
|
||||
}
|
60
src/gpu/instanced/GLInstancedRendering.h
Normal file
60
src/gpu/instanced/GLInstancedRendering.h
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef gr_instanced_GLInstancedRendering_DEFINED
|
||||
#define gr_instanced_GLInstancedRendering_DEFINED
|
||||
|
||||
#include "gl/GrGLBuffer.h"
|
||||
#include "instanced/InstancedRendering.h"
|
||||
|
||||
class GrGLGpu;
|
||||
|
||||
#define GR_GL_LOG_INSTANCED_BATCHES 0
|
||||
|
||||
namespace gr_instanced {
|
||||
|
||||
class GLInstancedRendering final : public InstancedRendering {
|
||||
public:
|
||||
static GLInstancedRendering* CreateIfSupported(GrGLGpu*);
|
||||
~GLInstancedRendering() override;
|
||||
|
||||
private:
|
||||
GLInstancedRendering(GrGLGpu*, AntialiasMode lastSupportedAAMode);
|
||||
|
||||
GrGLGpu* glGpu() const;
|
||||
|
||||
Batch* createBatch() override;
|
||||
|
||||
void onBeginFlush(GrResourceProvider*) override;
|
||||
void onDraw(const GrPipeline&, const InstanceProcessor&, const Batch*) override;
|
||||
void onEndFlush() override;
|
||||
void onResetGpuResources(ResetType) override;
|
||||
|
||||
void flushInstanceAttribs(int baseInstance);
|
||||
|
||||
struct GLDrawCmdInfo {
|
||||
int fInstanceCount;
|
||||
#if GR_GL_LOG_INSTANCED_BATCHES
|
||||
IndexRange fGeometry;
|
||||
#endif
|
||||
};
|
||||
|
||||
GrGLuint fVertexArrayID;
|
||||
SkAutoTUnref<GrGLBuffer> fInstanceBuffer;
|
||||
SkAutoTUnref<GrGLBuffer> fDrawIndirectBuffer;
|
||||
SkAutoSTMalloc<1024, GLDrawCmdInfo> fGLDrawCmdsInfo;
|
||||
uint32_t fInstanceAttribsBufferUniqueId;
|
||||
int fInstanceAttribsBaseInstance;
|
||||
|
||||
class GLBatch;
|
||||
|
||||
typedef InstancedRendering INHERITED;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
2096
src/gpu/instanced/InstanceProcessor.cpp
Normal file
2096
src/gpu/instanced/InstanceProcessor.cpp
Normal file
File diff suppressed because it is too large
Load Diff
63
src/gpu/instanced/InstanceProcessor.h
Normal file
63
src/gpu/instanced/InstanceProcessor.h
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef gr_instanced_InstanceProcessor_DEFINED
|
||||
#define gr_instanced_InstanceProcessor_DEFINED
|
||||
|
||||
#include "GrBufferAccess.h"
|
||||
#include "GrGeometryProcessor.h"
|
||||
#include "instanced/InstancedRenderingTypes.h"
|
||||
|
||||
namespace gr_instanced {
|
||||
|
||||
/**
|
||||
* This class provides a GP implementation that uses instanced rendering. Is sends geometry in as
|
||||
* basic, pre-baked canonical shapes, and uses instanced vertex attribs to control how these shapes
|
||||
* are transformed and drawn. MSAA is accomplished with the sample mask rather than finely
|
||||
* tesselated geometry.
|
||||
*/
|
||||
class InstanceProcessor : public GrGeometryProcessor {
|
||||
public:
|
||||
static bool IsSupported(const GrGLSLCaps&, const GrCaps&, AntialiasMode* lastSupportedAAMode);
|
||||
|
||||
InstanceProcessor(BatchInfo, GrBuffer* paramsBuffer);
|
||||
|
||||
const char* name() const override { return "Instance Processor"; }
|
||||
BatchInfo batchInfo() const { return fBatchInfo; }
|
||||
|
||||
void getGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder* b) const override {
|
||||
b->add32(fBatchInfo.fData);
|
||||
}
|
||||
GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
|
||||
|
||||
/**
|
||||
* Returns a buffer of ShapeVertex that defines the canonical instanced geometry.
|
||||
*/
|
||||
static const GrBuffer* SK_WARN_UNUSED_RESULT FindOrCreateVertexBuffer(GrGpu*);
|
||||
|
||||
/**
|
||||
* Returns a buffer of 8-bit indices for the canonical instanced geometry. The client can call
|
||||
* GetIndexRangeForXXX to know which indices to use for a specific shape.
|
||||
*/
|
||||
static const GrBuffer* SK_WARN_UNUSED_RESULT FindOrCreateIndex8Buffer(GrGpu*);
|
||||
|
||||
static IndexRange GetIndexRangeForRect(AntialiasMode);
|
||||
static IndexRange GetIndexRangeForOval(AntialiasMode, const SkRect& devBounds);
|
||||
static IndexRange GetIndexRangeForRRect(AntialiasMode);
|
||||
|
||||
static const char* GetNameOfIndexRange(IndexRange);
|
||||
|
||||
private:
|
||||
const BatchInfo fBatchInfo;
|
||||
GrBufferAccess fParamsAccess;
|
||||
|
||||
typedef GrGeometryProcessor INHERITED;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
488
src/gpu/instanced/InstancedRendering.cpp
Normal file
488
src/gpu/instanced/InstancedRendering.cpp
Normal file
@ -0,0 +1,488 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "InstancedRendering.h"
|
||||
|
||||
#include "GrBatchFlushState.h"
|
||||
#include "GrPipeline.h"
|
||||
#include "GrResourceProvider.h"
|
||||
#include "instanced/InstanceProcessor.h"
|
||||
|
||||
namespace gr_instanced {
|
||||
|
||||
InstancedRendering::InstancedRendering(GrGpu* gpu, AntialiasMode lastSupportedAAMode,
|
||||
bool canRenderToFloat)
|
||||
: fGpu(SkRef(gpu)),
|
||||
fLastSupportedAAMode(lastSupportedAAMode),
|
||||
fCanRenderToFloat(canRenderToFloat),
|
||||
fState(State::kRecordingDraws),
|
||||
fDrawPool(1024 * sizeof(Batch::Draw), 1024 * sizeof(Batch::Draw)) {
|
||||
}
|
||||
|
||||
GrDrawBatch* InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix,
|
||||
GrColor color, bool antialias,
|
||||
const GrInstancedPipelineInfo& info, bool* useHWAA) {
|
||||
return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, antialias, info,
|
||||
useHWAA);
|
||||
}
|
||||
|
||||
GrDrawBatch* InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix,
|
||||
GrColor color, const SkRect& localRect, bool antialias,
|
||||
const GrInstancedPipelineInfo& info, bool* useHWAA) {
|
||||
return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, localRect, antialias, info,
|
||||
useHWAA);
|
||||
}
|
||||
|
||||
GrDrawBatch* InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix,
|
||||
GrColor color, const SkMatrix& localMatrix,
|
||||
bool antialias, const GrInstancedPipelineInfo& info,
|
||||
bool* useHWAA) {
|
||||
if (localMatrix.hasPerspective()) {
|
||||
return nullptr; // Perspective is not yet supported in the local matrix.
|
||||
}
|
||||
if (Batch* batch = this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, antialias,
|
||||
info, useHWAA)) {
|
||||
batch->getSingleInstance().fInfo |= kLocalMatrix_InfoFlag;
|
||||
batch->appendParamsTexel(localMatrix.getScaleX(), localMatrix.getSkewX(),
|
||||
localMatrix.getTranslateX());
|
||||
batch->appendParamsTexel(localMatrix.getSkewY(), localMatrix.getScaleY(),
|
||||
localMatrix.getTranslateY());
|
||||
batch->fInfo.fHasLocalMatrix = true;
|
||||
return batch;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GrDrawBatch* InstancedRendering::recordOval(const SkRect& oval, const SkMatrix& viewMatrix,
|
||||
GrColor color, bool antialias,
|
||||
const GrInstancedPipelineInfo& info, bool* useHWAA) {
|
||||
return this->recordShape(ShapeType::kOval, oval, viewMatrix, color, oval, antialias, info,
|
||||
useHWAA);
|
||||
}
|
||||
|
||||
GrDrawBatch* InstancedRendering::recordRRect(const SkRRect& rrect, const SkMatrix& viewMatrix,
|
||||
GrColor color, bool antialias,
|
||||
const GrInstancedPipelineInfo& info, bool* useHWAA) {
|
||||
if (Batch* batch = this->recordShape(GetRRectShapeType(rrect), rrect.rect(), viewMatrix, color,
|
||||
rrect.rect(), antialias, info, useHWAA)) {
|
||||
batch->appendRRectParams(rrect);
|
||||
return batch;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
GrDrawBatch* InstancedRendering::recordDRRect(const SkRRect& outer, const SkRRect& inner,
|
||||
const SkMatrix& viewMatrix, GrColor color,
|
||||
bool antialias, const GrInstancedPipelineInfo& info,
|
||||
bool* useHWAA) {
|
||||
if (inner.getType() > SkRRect::kSimple_Type) {
|
||||
return nullptr; // Complex inner round rects are not yet supported.
|
||||
}
|
||||
if (SkRRect::kEmpty_Type == inner.getType()) {
|
||||
return this->recordRRect(outer, viewMatrix, color, antialias, info, useHWAA);
|
||||
}
|
||||
if (Batch* batch = this->recordShape(GetRRectShapeType(outer), outer.rect(), viewMatrix, color,
|
||||
outer.rect(), antialias, info, useHWAA)) {
|
||||
batch->appendRRectParams(outer);
|
||||
ShapeType innerShapeType = GetRRectShapeType(inner);
|
||||
batch->fInfo.fInnerShapeTypes |= GetShapeFlag(innerShapeType);
|
||||
batch->getSingleInstance().fInfo |= ((int)innerShapeType << kInnerShapeType_InfoBit);
|
||||
batch->appendParamsTexel(inner.rect().asScalars(), 4);
|
||||
batch->appendRRectParams(inner);
|
||||
return batch;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
InstancedRendering::Batch* InstancedRendering::recordShape(ShapeType type, const SkRect& bounds,
|
||||
const SkMatrix& viewMatrix,
|
||||
GrColor color, const SkRect& localRect,
|
||||
bool antialias,
|
||||
const GrInstancedPipelineInfo& info,
|
||||
bool* useHWAA) {
|
||||
SkASSERT(State::kRecordingDraws == fState);
|
||||
|
||||
if (info.fIsRenderingToFloat && !fCanRenderToFloat) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AntialiasMode antialiasMode;
|
||||
if (!this->selectAntialiasMode(viewMatrix, antialias, info, useHWAA, &antialiasMode)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Batch* batch = this->createBatch();
|
||||
batch->fInfo.fAntialiasMode = antialiasMode;
|
||||
batch->fInfo.fShapeTypes = GetShapeFlag(type);
|
||||
batch->fInfo.fCannotDiscard = !info.fCanDiscard;
|
||||
|
||||
Instance& instance = batch->getSingleInstance();
|
||||
instance.fInfo = (int)type << kShapeType_InfoBit;
|
||||
|
||||
// The instanced shape renderer draws rectangles of [-1, -1, +1, +1], so we find the matrix that
|
||||
// will map this rectangle to the same device coordinates as "viewMatrix * bounds".
|
||||
float sx = 0.5f * bounds.width();
|
||||
float sy = 0.5f * bounds.height();
|
||||
float tx = sx + bounds.fLeft;
|
||||
float ty = sy + bounds.fTop;
|
||||
if (!viewMatrix.hasPerspective()) {
|
||||
float* m = instance.fShapeMatrix2x3;
|
||||
m[0] = viewMatrix.getScaleX() * sx;
|
||||
m[1] = viewMatrix.getSkewX() * sy;
|
||||
m[2] = viewMatrix.getTranslateX() +
|
||||
viewMatrix.getScaleX() * tx + viewMatrix.getSkewX() * ty;
|
||||
|
||||
m[3] = viewMatrix.getSkewY() * sx;
|
||||
m[4] = viewMatrix.getScaleY() * sy;
|
||||
m[5] = viewMatrix.getTranslateY() +
|
||||
viewMatrix.getSkewY() * tx + viewMatrix.getScaleY() * ty;
|
||||
|
||||
// Since 'm' is a 2x3 matrix that maps the rect [-1, +1] into the shape's device-space quad,
|
||||
// it's quite simple to find the bounding rectangle:
|
||||
float devBoundsHalfWidth = fabsf(m[0]) + fabsf(m[1]);
|
||||
float devBoundsHalfHeight = fabsf(m[3]) + fabsf(m[4]);
|
||||
batch->fBounds.fLeft = m[2] - devBoundsHalfWidth;
|
||||
batch->fBounds.fRight = m[2] + devBoundsHalfWidth;
|
||||
batch->fBounds.fTop = m[5] - devBoundsHalfHeight;
|
||||
batch->fBounds.fBottom = m[5] + devBoundsHalfHeight;
|
||||
|
||||
// TODO: Is this worth the CPU overhead?
|
||||
batch->fInfo.fNonSquare =
|
||||
fabsf(devBoundsHalfHeight - devBoundsHalfWidth) > 0.5f || // Early out.
|
||||
fabs(m[0] * m[3] + m[1] * m[4]) > 1e-3f || // Skew?
|
||||
fabs(m[0] * m[0] + m[1] * m[1] - m[3] * m[3] - m[4] * m[4]) > 1e-2f; // Diff. lengths?
|
||||
} else {
|
||||
SkMatrix shapeMatrix(viewMatrix);
|
||||
shapeMatrix.preTranslate(tx, ty);
|
||||
shapeMatrix.preScale(sx, sy);
|
||||
instance.fInfo |= kPerspective_InfoFlag;
|
||||
|
||||
float* m = instance.fShapeMatrix2x3;
|
||||
m[0] = SkScalarToFloat(shapeMatrix.getScaleX());
|
||||
m[1] = SkScalarToFloat(shapeMatrix.getSkewX());
|
||||
m[2] = SkScalarToFloat(shapeMatrix.getTranslateX());
|
||||
m[3] = SkScalarToFloat(shapeMatrix.getSkewY());
|
||||
m[4] = SkScalarToFloat(shapeMatrix.getScaleY());
|
||||
m[5] = SkScalarToFloat(shapeMatrix.getTranslateY());
|
||||
|
||||
// Send the perspective column as a param.
|
||||
batch->appendParamsTexel(shapeMatrix[SkMatrix::kMPersp0], shapeMatrix[SkMatrix::kMPersp1],
|
||||
shapeMatrix[SkMatrix::kMPersp2]);
|
||||
batch->fInfo.fHasPerspective = true;
|
||||
|
||||
viewMatrix.mapRect(&batch->fBounds, bounds);
|
||||
|
||||
batch->fInfo.fNonSquare = true;
|
||||
}
|
||||
|
||||
instance.fColor = color;
|
||||
|
||||
const float* rectAsFloats = localRect.asScalars(); // Ensure SkScalar == float.
|
||||
memcpy(&instance.fLocalRect, rectAsFloats, 4 * sizeof(float));
|
||||
|
||||
batch->fPixelLoad = batch->fBounds.height() * batch->fBounds.width();
|
||||
return batch;
|
||||
}
|
||||
|
||||
inline bool InstancedRendering::selectAntialiasMode(const SkMatrix& viewMatrix, bool antialias,
|
||||
const GrInstancedPipelineInfo& info,
|
||||
bool* useHWAA, AntialiasMode* antialiasMode) {
|
||||
SkASSERT(!info.fColorDisabled || info.fDrawingShapeToStencil);
|
||||
SkASSERT(!info.fIsMixedSampled || info.fIsMultisampled);
|
||||
|
||||
if (!info.fIsMultisampled || fGpu->caps()->multisampleDisableSupport()) {
|
||||
SkASSERT(fLastSupportedAAMode >= AntialiasMode::kCoverage);
|
||||
if (!antialias) {
|
||||
if (info.fDrawingShapeToStencil && !info.fCanDiscard) {
|
||||
// We can't draw to the stencil buffer without discard (or sample mask if MSAA).
|
||||
return false;
|
||||
}
|
||||
*antialiasMode = AntialiasMode::kNone;
|
||||
*useHWAA = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (info.canUseCoverageAA() && viewMatrix.preservesRightAngles()) {
|
||||
*antialiasMode = AntialiasMode::kCoverage;
|
||||
*useHWAA = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (info.fIsMultisampled && fLastSupportedAAMode >= AntialiasMode::kMSAA) {
|
||||
if (!info.fIsMixedSampled || info.fColorDisabled) {
|
||||
*antialiasMode = AntialiasMode::kMSAA;
|
||||
*useHWAA = true;
|
||||
return true;
|
||||
}
|
||||
if (fLastSupportedAAMode >= AntialiasMode::kMixedSamples) {
|
||||
*antialiasMode = AntialiasMode::kMixedSamples;
|
||||
*useHWAA = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
InstancedRendering::Batch::Batch(uint32_t classID, InstancedRendering* ir)
|
||||
: INHERITED(classID),
|
||||
fInstancedRendering(ir),
|
||||
fIsTracked(false),
|
||||
fNumDraws(1),
|
||||
fNumChangesInGeometry(0) {
|
||||
fHeadDraw = fTailDraw = (Draw*)fInstancedRendering->fDrawPool.allocate(sizeof(Draw));
|
||||
#ifdef SK_DEBUG
|
||||
fHeadDraw->fGeometry = {-1, 0};
|
||||
#endif
|
||||
fHeadDraw->fNext = nullptr;
|
||||
}
|
||||
|
||||
InstancedRendering::Batch::~Batch() {
|
||||
if (fIsTracked) {
|
||||
fInstancedRendering->fTrackedBatches.remove(this);
|
||||
}
|
||||
|
||||
Draw* draw = fHeadDraw;
|
||||
while (draw) {
|
||||
Draw* next = draw->fNext;
|
||||
fInstancedRendering->fDrawPool.release(draw);
|
||||
draw = next;
|
||||
}
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::appendRRectParams(const SkRRect& rrect) {
|
||||
SkASSERT(!fIsTracked);
|
||||
switch (rrect.getType()) {
|
||||
case SkRRect::kSimple_Type: {
|
||||
const SkVector& radii = rrect.getSimpleRadii();
|
||||
this->appendParamsTexel(radii.x(), radii.y(), rrect.width(), rrect.height());
|
||||
return;
|
||||
}
|
||||
case SkRRect::kNinePatch_Type: {
|
||||
float twoOverW = 2 / rrect.width();
|
||||
float twoOverH = 2 / rrect.height();
|
||||
const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
|
||||
const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
|
||||
this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBR.x() * twoOverW,
|
||||
radiiTL.y() * twoOverH, radiiBR.y() * twoOverH);
|
||||
return;
|
||||
}
|
||||
case SkRRect::kComplex_Type: {
|
||||
/**
|
||||
* The x and y radii of each arc are stored in separate vectors,
|
||||
* in the following order:
|
||||
*
|
||||
* __x1 _ _ _ x3__
|
||||
* y1 | | y2
|
||||
*
|
||||
* | |
|
||||
*
|
||||
* y3 |__ _ _ _ __| y4
|
||||
* x2 x4
|
||||
*
|
||||
*/
|
||||
float twoOverW = 2 / rrect.width();
|
||||
float twoOverH = 2 / rrect.height();
|
||||
const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
|
||||
const SkVector& radiiTR = rrect.radii(SkRRect::kUpperRight_Corner);
|
||||
const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
|
||||
const SkVector& radiiBL = rrect.radii(SkRRect::kLowerLeft_Corner);
|
||||
this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBL.x() * twoOverW,
|
||||
radiiTR.x() * twoOverW, radiiBR.x() * twoOverW);
|
||||
this->appendParamsTexel(radiiTL.y() * twoOverH, radiiTR.y() * twoOverH,
|
||||
radiiBL.y() * twoOverH, radiiBR.y() * twoOverH);
|
||||
return;
|
||||
}
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::appendParamsTexel(const SkScalar* vals, int count) {
|
||||
SkASSERT(!fIsTracked);
|
||||
SkASSERT(count <= 4 && count >= 0);
|
||||
const float* valsAsFloats = vals; // Ensure SkScalar == float.
|
||||
memcpy(&fParams.push_back(), valsAsFloats, count * sizeof(float));
|
||||
fInfo.fHasParams = true;
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w) {
|
||||
SkASSERT(!fIsTracked);
|
||||
ParamsTexel& texel = fParams.push_back();
|
||||
texel.fX = SkScalarToFloat(x);
|
||||
texel.fY = SkScalarToFloat(y);
|
||||
texel.fZ = SkScalarToFloat(z);
|
||||
texel.fW = SkScalarToFloat(w);
|
||||
fInfo.fHasParams = true;
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z) {
|
||||
SkASSERT(!fIsTracked);
|
||||
ParamsTexel& texel = fParams.push_back();
|
||||
texel.fX = SkScalarToFloat(x);
|
||||
texel.fY = SkScalarToFloat(y);
|
||||
texel.fZ = SkScalarToFloat(z);
|
||||
fInfo.fHasParams = true;
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::computePipelineOptimizations(GrInitInvariantOutput* color,
|
||||
GrInitInvariantOutput* coverage,
|
||||
GrBatchToXPOverrides* overrides) const {
|
||||
color->setKnownFourComponents(this->getSingleInstance().fColor);
|
||||
|
||||
if (AntialiasMode::kCoverage == fInfo.fAntialiasMode ||
|
||||
(AntialiasMode::kNone == fInfo.fAntialiasMode &&
|
||||
!fInfo.isSimpleRects() && fInfo.fCannotDiscard)) {
|
||||
coverage->setUnknownSingleComponent();
|
||||
} else {
|
||||
coverage->setKnownSingleComponent(255);
|
||||
}
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::initBatchTracker(const GrXPOverridesForBatch& overrides) {
|
||||
Draw& draw = this->getSingleDraw(); // This will assert if we have > 1 command.
|
||||
SkASSERT(draw.fGeometry.isEmpty());
|
||||
SkASSERT(SkIsPow2(fInfo.fShapeTypes));
|
||||
SkASSERT(!fIsTracked);
|
||||
|
||||
if (kRect_ShapeFlag == fInfo.fShapeTypes) {
|
||||
draw.fGeometry = InstanceProcessor::GetIndexRangeForRect(fInfo.fAntialiasMode);
|
||||
} else if (kOval_ShapeFlag == fInfo.fShapeTypes) {
|
||||
draw.fGeometry = InstanceProcessor::GetIndexRangeForOval(fInfo.fAntialiasMode, fBounds);
|
||||
} else {
|
||||
draw.fGeometry = InstanceProcessor::GetIndexRangeForRRect(fInfo.fAntialiasMode);
|
||||
}
|
||||
|
||||
if (!fParams.empty()) {
|
||||
SkASSERT(fInstancedRendering->fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
|
||||
this->getSingleInstance().fInfo |= fInstancedRendering->fParams.count();
|
||||
fInstancedRendering->fParams.push_back_n(fParams.count(), fParams.begin());
|
||||
}
|
||||
|
||||
GrColor overrideColor;
|
||||
if (overrides.getOverrideColorIfSet(&overrideColor)) {
|
||||
SkASSERT(State::kRecordingDraws == fInstancedRendering->fState);
|
||||
this->getSingleInstance().fColor = overrideColor;
|
||||
}
|
||||
fInfo.fUsesLocalCoords = overrides.readsLocalCoords();
|
||||
fInfo.fCannotTweakAlphaForCoverage = !overrides.canTweakAlphaForCoverage();
|
||||
|
||||
fInstancedRendering->fTrackedBatches.addToTail(this);
|
||||
fIsTracked = true;
|
||||
}
|
||||
|
||||
bool InstancedRendering::Batch::onCombineIfPossible(GrBatch* other, const GrCaps& caps) {
|
||||
Batch* that = static_cast<Batch*>(other);
|
||||
SkASSERT(fInstancedRendering == that->fInstancedRendering);
|
||||
SkASSERT(fTailDraw);
|
||||
SkASSERT(that->fTailDraw);
|
||||
|
||||
if (!BatchInfo::CanCombine(fInfo, that->fInfo) ||
|
||||
!GrPipeline::CanCombine(*this->pipeline(), this->bounds(),
|
||||
*that->pipeline(), that->bounds(), caps)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BatchInfo combinedInfo = fInfo | that->fInfo;
|
||||
if (!combinedInfo.isSimpleRects()) {
|
||||
// This threshold was chosen with the "shapes_mixed" bench on a MacBook with Intel graphics.
|
||||
// There seems to be a wide range where it doesn't matter if we combine or not. What matters
|
||||
// is that the itty bitty rects combine with other shapes and the giant ones don't.
|
||||
constexpr SkScalar kMaxPixelsToGeneralizeRects = 256 * 256;
|
||||
if (fInfo.isSimpleRects() && fPixelLoad > kMaxPixelsToGeneralizeRects) {
|
||||
return false;
|
||||
}
|
||||
if (that->fInfo.isSimpleRects() && that->fPixelLoad > kMaxPixelsToGeneralizeRects) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
fBounds.join(that->fBounds);
|
||||
fInfo = combinedInfo;
|
||||
fPixelLoad += that->fPixelLoad;
|
||||
|
||||
// Adopt the other batch's draws.
|
||||
fNumDraws += that->fNumDraws;
|
||||
fNumChangesInGeometry += that->fNumChangesInGeometry;
|
||||
if (fTailDraw->fGeometry != that->fHeadDraw->fGeometry) {
|
||||
++fNumChangesInGeometry;
|
||||
}
|
||||
fTailDraw->fNext = that->fHeadDraw;
|
||||
fTailDraw = that->fTailDraw;
|
||||
|
||||
that->fHeadDraw = that->fTailDraw = nullptr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void InstancedRendering::beginFlush(GrResourceProvider* rp) {
|
||||
SkASSERT(State::kRecordingDraws == fState);
|
||||
fState = State::kFlushing;
|
||||
|
||||
if (fTrackedBatches.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!fVertexBuffer) {
|
||||
fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu));
|
||||
if (!fVertexBuffer) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fIndexBuffer) {
|
||||
fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu));
|
||||
if (!fIndexBuffer) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fParams.empty()) {
|
||||
fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
|
||||
kTexel_GrBufferType, kDynamic_GrAccessPattern,
|
||||
GrResourceProvider::kNoPendingIO_Flag,
|
||||
fParams.begin()));
|
||||
if (!fParamsBuffer) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this->onBeginFlush(rp);
|
||||
}
|
||||
|
||||
void InstancedRendering::Batch::onDraw(GrBatchFlushState* state) {
|
||||
SkASSERT(State::kFlushing == fInstancedRendering->fState);
|
||||
SkASSERT(state->gpu() == fInstancedRendering->gpu());
|
||||
|
||||
state->gpu()->handleDirtyContext();
|
||||
if (GrXferBarrierType barrierType = this->pipeline()->xferBarrierType(*state->gpu()->caps())) {
|
||||
state->gpu()->xferBarrier(this->pipeline()->getRenderTarget(), barrierType);
|
||||
}
|
||||
|
||||
InstanceProcessor instProc(fInfo, fInstancedRendering->fParamsBuffer);
|
||||
fInstancedRendering->onDraw(*this->pipeline(), instProc, this);
|
||||
}
|
||||
|
||||
void InstancedRendering::endFlush() {
|
||||
// The caller is expected to delete all tracked batches (i.e. batches whose initBatchTracker
|
||||
// method has been called) before ending the flush.
|
||||
SkASSERT(fTrackedBatches.isEmpty());
|
||||
fParams.reset();
|
||||
fParamsBuffer.reset();
|
||||
this->onEndFlush();
|
||||
fState = State::kRecordingDraws;
|
||||
// Hold on to the shape coords and index buffers.
|
||||
}
|
||||
|
||||
void InstancedRendering::resetGpuResources(ResetType resetType) {
|
||||
fVertexBuffer.reset();
|
||||
fIndexBuffer.reset();
|
||||
fParamsBuffer.reset();
|
||||
this->onResetGpuResources(resetType);
|
||||
}
|
||||
|
||||
}
|
187
src/gpu/instanced/InstancedRendering.h
Normal file
187
src/gpu/instanced/InstancedRendering.h
Normal file
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef gr_instanced_InstancedRendering_DEFINED
|
||||
#define gr_instanced_InstancedRendering_DEFINED
|
||||
|
||||
#include "GrMemoryPool.h"
|
||||
#include "SkTInternalLList.h"
|
||||
#include "batches/GrDrawBatch.h"
|
||||
#include "instanced/InstancedRenderingTypes.h"
|
||||
#include "../private/GrInstancedPipelineInfo.h"
|
||||
|
||||
class GrResourceProvider;
|
||||
|
||||
namespace gr_instanced {
|
||||
|
||||
class InstanceProcessor;
|
||||
|
||||
/**
|
||||
* This class serves as a centralized clearinghouse for instanced rendering. It accumulates data for
|
||||
* instanced draws into one location, and creates special batches that pull from this data. The
|
||||
* nature of instanced rendering allows these batches to combine well and render efficiently.
|
||||
*
|
||||
* During a flush, this class assembles the accumulated draw data into a single vertex and texel
|
||||
* buffer, and its subclass draws the batches using backend-specific instanced rendering APIs.
|
||||
*
|
||||
* This class is responsible for the CPU side of instanced rendering. Shaders are implemented by
|
||||
* InstanceProcessor.
|
||||
*/
|
||||
class InstancedRendering : public SkNoncopyable {
|
||||
public:
|
||||
virtual ~InstancedRendering() { SkASSERT(State::kRecordingDraws == fState); }
|
||||
|
||||
GrGpu* gpu() const { return fGpu; }
|
||||
|
||||
/**
|
||||
* These methods make a new record internally for an instanced draw, and return a batch that is
|
||||
* effectively just an index to that record. The returned batch is not self-contained, but
|
||||
* rather relies on this class to handle the rendering. The client must call beginFlush() on
|
||||
* this class before attempting to flush batches returned by it. It is invalid to record new
|
||||
* draws between beginFlush() and endFlush().
|
||||
*/
|
||||
GrDrawBatch* SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, GrColor,
|
||||
bool antialias, const GrInstancedPipelineInfo&,
|
||||
bool* useHWAA);
|
||||
|
||||
GrDrawBatch* SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, GrColor,
|
||||
const SkRect& localRect, bool antialias,
|
||||
const GrInstancedPipelineInfo&, bool* useHWAA);
|
||||
|
||||
GrDrawBatch* SK_WARN_UNUSED_RESULT recordRect(const SkRect&, const SkMatrix&, GrColor,
|
||||
const SkMatrix& localMatrix, bool antialias,
|
||||
const GrInstancedPipelineInfo&, bool* useHWAA);
|
||||
|
||||
GrDrawBatch* SK_WARN_UNUSED_RESULT recordOval(const SkRect&, const SkMatrix&, GrColor,
|
||||
bool antialias, const GrInstancedPipelineInfo&,
|
||||
bool* useHWAA);
|
||||
|
||||
GrDrawBatch* SK_WARN_UNUSED_RESULT recordRRect(const SkRRect&, const SkMatrix&, GrColor,
|
||||
bool antialias, const GrInstancedPipelineInfo&,
|
||||
bool* useHWAA);
|
||||
|
||||
GrDrawBatch* SK_WARN_UNUSED_RESULT recordDRRect(const SkRRect& outer, const SkRRect& inner,
|
||||
const SkMatrix&, GrColor, bool antialias,
|
||||
const GrInstancedPipelineInfo&, bool* useHWAA);
|
||||
|
||||
/**
|
||||
* Compiles all recorded draws into GPU buffers and allows the client to begin flushing the
|
||||
* batches created by this class.
|
||||
*/
|
||||
void beginFlush(GrResourceProvider*);
|
||||
|
||||
/**
|
||||
* Called once the batches created previously by this class have all been released. Allows the
|
||||
* client to begin recording draws again.
|
||||
*/
|
||||
void endFlush();
|
||||
|
||||
enum class ResetType : bool {
|
||||
kDestroy,
|
||||
kAbandon
|
||||
};
|
||||
|
||||
/**
|
||||
* Resets all GPU resources, including those that are held long term. They will be lazily
|
||||
* reinitialized if the class begins to be used again.
|
||||
*/
|
||||
void resetGpuResources(ResetType);
|
||||
|
||||
protected:
|
||||
class Batch : public GrDrawBatch {
|
||||
public:
|
||||
SK_DECLARE_INTERNAL_LLIST_INTERFACE(Batch);
|
||||
|
||||
~Batch() override;
|
||||
const char* name() const override { return "Instanced Batch"; }
|
||||
|
||||
struct Draw {
|
||||
Instance fInstance;
|
||||
IndexRange fGeometry;
|
||||
Draw* fNext;
|
||||
};
|
||||
|
||||
Draw& getSingleDraw() const { SkASSERT(fHeadDraw && !fHeadDraw->fNext); return *fHeadDraw; }
|
||||
Instance& getSingleInstance() const { return this->getSingleDraw().fInstance; }
|
||||
|
||||
void appendRRectParams(const SkRRect&);
|
||||
void appendParamsTexel(const SkScalar* vals, int count);
|
||||
void appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w);
|
||||
void appendParamsTexel(SkScalar x, SkScalar y, SkScalar z);
|
||||
|
||||
protected:
|
||||
Batch(uint32_t classID, InstancedRendering* ir);
|
||||
|
||||
void initBatchTracker(const GrXPOverridesForBatch&) override;
|
||||
bool onCombineIfPossible(GrBatch* other, const GrCaps& caps) override;
|
||||
|
||||
void computePipelineOptimizations(GrInitInvariantOutput* color,
|
||||
GrInitInvariantOutput* coverage,
|
||||
GrBatchToXPOverrides*) const override;
|
||||
|
||||
void onPrepare(GrBatchFlushState*) override {}
|
||||
void onDraw(GrBatchFlushState*) override;
|
||||
|
||||
InstancedRendering* const fInstancedRendering;
|
||||
BatchInfo fInfo;
|
||||
SkScalar fPixelLoad;
|
||||
SkSTArray<5, ParamsTexel, true> fParams;
|
||||
bool fIsTracked;
|
||||
int fNumDraws;
|
||||
int fNumChangesInGeometry;
|
||||
Draw* fHeadDraw;
|
||||
Draw* fTailDraw;
|
||||
|
||||
typedef GrDrawBatch INHERITED;
|
||||
|
||||
friend class InstancedRendering;
|
||||
};
|
||||
|
||||
typedef SkTInternalLList<Batch> BatchList;
|
||||
|
||||
InstancedRendering(GrGpu* gpu, AntialiasMode lastSupportedAAMode, bool canRenderToFloat);
|
||||
|
||||
const BatchList& trackedBatches() const { return fTrackedBatches; }
|
||||
const GrBuffer* vertexBuffer() const { SkASSERT(fVertexBuffer); return fVertexBuffer; }
|
||||
const GrBuffer* indexBuffer() const { SkASSERT(fIndexBuffer); return fIndexBuffer; }
|
||||
|
||||
virtual void onBeginFlush(GrResourceProvider*) = 0;
|
||||
virtual void onDraw(const GrPipeline&, const InstanceProcessor&, const Batch*) = 0;
|
||||
virtual void onEndFlush() = 0;
|
||||
virtual void onResetGpuResources(ResetType) = 0;
|
||||
|
||||
private:
|
||||
enum class State : bool {
|
||||
kRecordingDraws,
|
||||
kFlushing
|
||||
};
|
||||
|
||||
Batch* SK_WARN_UNUSED_RESULT recordShape(ShapeType, const SkRect& bounds,
|
||||
const SkMatrix& viewMatrix, GrColor,
|
||||
const SkRect& localRect, bool antialias,
|
||||
const GrInstancedPipelineInfo&, bool* requireHWAA);
|
||||
|
||||
bool selectAntialiasMode(const SkMatrix& viewMatrix, bool antialias,
|
||||
const GrInstancedPipelineInfo&, bool* useHWAA, AntialiasMode*);
|
||||
|
||||
virtual Batch* createBatch() = 0;
|
||||
|
||||
const SkAutoTUnref<GrGpu> fGpu;
|
||||
const AntialiasMode fLastSupportedAAMode;
|
||||
const bool fCanRenderToFloat;
|
||||
State fState;
|
||||
GrMemoryPool fDrawPool;
|
||||
SkSTArray<1024, ParamsTexel, true> fParams;
|
||||
BatchList fTrackedBatches;
|
||||
SkAutoTUnref<const GrBuffer> fVertexBuffer;
|
||||
SkAutoTUnref<const GrBuffer> fIndexBuffer;
|
||||
SkAutoTUnref<GrBuffer> fParamsBuffer;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
192
src/gpu/instanced/InstancedRenderingTypes.h
Normal file
192
src/gpu/instanced/InstancedRenderingTypes.h
Normal file
@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright 2016 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#ifndef gr_instanced_InstancedRenderingTypes_DEFINED
|
||||
#define gr_instanced_InstancedRenderingTypes_DEFINED
|
||||
|
||||
#include "GrTypes.h"
|
||||
#include "SkRRect.h"
|
||||
|
||||
namespace gr_instanced {
|
||||
|
||||
/**
|
||||
* Per-vertex data. These values get fed into normal vertex attribs.
|
||||
*/
|
||||
struct ShapeVertex {
|
||||
float fX, fY; //!< Shape coordinates.
|
||||
int32_t fAttrs; //!< Shape-specific vertex attributes, if needed.
|
||||
};
|
||||
|
||||
/**
|
||||
* Per-instance data. These values get fed into instanced vertex attribs.
|
||||
*/
|
||||
struct Instance {
|
||||
uint32_t fInfo; //!< Packed info about the instance. See InfoBits.
|
||||
float fShapeMatrix2x3[6]; //!< Maps canonical shape coords -> device space coords.
|
||||
uint32_t fColor; //!< Color to be written out by the primitive processor.
|
||||
float fLocalRect[4]; //!< Local coords rect that spans [-1, +1] in shape coords.
|
||||
};
|
||||
|
||||
enum class Attrib : uint8_t {
|
||||
kShapeCoords,
|
||||
kVertexAttrs,
|
||||
kInstanceInfo,
|
||||
kShapeMatrixX,
|
||||
kShapeMatrixY,
|
||||
kColor,
|
||||
kLocalRect
|
||||
};
|
||||
constexpr int kNumAttribs = 1 + (int)Attrib::kLocalRect;
|
||||
|
||||
enum class AntialiasMode : uint8_t {
|
||||
kNone,
|
||||
kCoverage,
|
||||
kMSAA,
|
||||
kMixedSamples
|
||||
};
|
||||
constexpr int kNumAntialiasModes = 1 + (int)AntialiasMode::kMixedSamples;
|
||||
|
||||
enum class ShapeType : uint8_t {
|
||||
kRect,
|
||||
kOval,
|
||||
kSimpleRRect,
|
||||
kNinePatch,
|
||||
kComplexRRect
|
||||
};
|
||||
constexpr int kNumShapeTypes = 1 + (int)ShapeType::kComplexRRect;
|
||||
|
||||
inline static ShapeType GetRRectShapeType(const SkRRect& rrect) {
|
||||
SkASSERT(rrect.getType() >= SkRRect::kRect_Type &&
|
||||
rrect.getType() <= SkRRect::kComplex_Type);
|
||||
return static_cast<ShapeType>(rrect.getType() - 1);
|
||||
|
||||
GR_STATIC_ASSERT((int)ShapeType::kRect == SkRRect::kRect_Type - 1);
|
||||
GR_STATIC_ASSERT((int)ShapeType::kOval == SkRRect::kOval_Type - 1);
|
||||
GR_STATIC_ASSERT((int)ShapeType::kSimpleRRect == SkRRect::kSimple_Type - 1);
|
||||
GR_STATIC_ASSERT((int)ShapeType::kNinePatch == SkRRect::kNinePatch_Type - 1);
|
||||
GR_STATIC_ASSERT((int)ShapeType::kComplexRRect == SkRRect::kComplex_Type - 1);
|
||||
GR_STATIC_ASSERT(kNumShapeTypes == SkRRect::kComplex_Type);
|
||||
}
|
||||
|
||||
enum ShapeFlag {
|
||||
kRect_ShapeFlag = (1 << (int)ShapeType::kRect),
|
||||
kOval_ShapeFlag = (1 << (int)ShapeType::kOval),
|
||||
kSimpleRRect_ShapeFlag = (1 << (int)ShapeType::kSimpleRRect),
|
||||
kNinePatch_ShapeFlag = (1 << (int)ShapeType::kNinePatch),
|
||||
kComplexRRect_ShapeFlag = (1 << (int)ShapeType::kComplexRRect),
|
||||
|
||||
kRRect_ShapesMask = kSimpleRRect_ShapeFlag | kNinePatch_ShapeFlag | kComplexRRect_ShapeFlag
|
||||
};
|
||||
|
||||
constexpr uint8_t GetShapeFlag(ShapeType type) { return 1 << (int)type; }
|
||||
|
||||
/**
|
||||
* Defines what data is stored at which bits in the fInfo field of the instanced data.
|
||||
*/
|
||||
enum InfoBits {
|
||||
kShapeType_InfoBit = 29,
|
||||
kInnerShapeType_InfoBit = 27,
|
||||
kPerspective_InfoBit = 26,
|
||||
kLocalMatrix_InfoBit = 25,
|
||||
kParamsIdx_InfoBit = 0
|
||||
};
|
||||
|
||||
enum InfoMasks {
|
||||
kShapeType_InfoMask = 0u - (1 << kShapeType_InfoBit),
|
||||
kInnerShapeType_InfoMask = (1 << kShapeType_InfoBit) - (1 << kInnerShapeType_InfoBit),
|
||||
kPerspective_InfoFlag = (1 << kPerspective_InfoBit),
|
||||
kLocalMatrix_InfoFlag = (1 << kLocalMatrix_InfoBit),
|
||||
kParamsIdx_InfoMask = (1 << kLocalMatrix_InfoBit) - 1
|
||||
};
|
||||
|
||||
GR_STATIC_ASSERT((kNumShapeTypes - 1) <= (uint32_t)kShapeType_InfoMask >> kShapeType_InfoBit);
|
||||
GR_STATIC_ASSERT((int)ShapeType::kSimpleRRect <=
|
||||
kInnerShapeType_InfoMask >> kInnerShapeType_InfoBit);
|
||||
|
||||
/**
|
||||
* Additional parameters required by some instances (e.g. round rect radii, perspective column,
|
||||
* local matrix). These are accessed via texel buffer.
|
||||
*/
|
||||
struct ParamsTexel {
|
||||
float fX, fY, fZ, fW;
|
||||
};
|
||||
|
||||
GR_STATIC_ASSERT(0 == offsetof(ParamsTexel, fX));
|
||||
GR_STATIC_ASSERT(4 * 4 == sizeof(ParamsTexel));
|
||||
|
||||
/**
|
||||
* Tracks all information needed in order to draw a batch of instances. This struct also serves
|
||||
* as an all-in-one shader key for the batch.
|
||||
*/
|
||||
struct BatchInfo {
|
||||
BatchInfo() : fData(0) {}
|
||||
explicit BatchInfo(uint32_t data) : fData(data) {}
|
||||
|
||||
static bool CanCombine(const BatchInfo& a, const BatchInfo& b);
|
||||
|
||||
bool isSimpleRects() const {
|
||||
return !((fShapeTypes & ~kRect_ShapeFlag) | fInnerShapeTypes);
|
||||
}
|
||||
|
||||
union {
|
||||
struct {
|
||||
AntialiasMode fAntialiasMode;
|
||||
uint8_t fShapeTypes;
|
||||
uint8_t fInnerShapeTypes;
|
||||
bool fHasPerspective : 1;
|
||||
bool fHasLocalMatrix : 1;
|
||||
bool fHasParams : 1;
|
||||
bool fNonSquare : 1;
|
||||
bool fUsesLocalCoords : 1;
|
||||
bool fCannotTweakAlphaForCoverage : 1;
|
||||
bool fCannotDiscard : 1;
|
||||
};
|
||||
uint32_t fData;
|
||||
};
|
||||
};
|
||||
|
||||
inline bool BatchInfo::CanCombine(const BatchInfo& a, const BatchInfo& b) {
|
||||
if (a.fAntialiasMode != b.fAntialiasMode) {
|
||||
return false;
|
||||
}
|
||||
if (SkToBool(a.fInnerShapeTypes) != SkToBool(b.fInnerShapeTypes)) {
|
||||
// GrInstanceProcessor can't currently combine draws with and without inner shapes.
|
||||
return false;
|
||||
}
|
||||
if (a.fCannotDiscard != b.fCannotDiscard) {
|
||||
// For stencil draws, the use of discard can be a requirement.
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline BatchInfo operator|(const BatchInfo& a, const BatchInfo& b) {
|
||||
SkASSERT(BatchInfo::CanCombine(a, b));
|
||||
return BatchInfo(a.fData | b.fData);
|
||||
}
|
||||
|
||||
// This is required since all the data must fit into 32 bits of a shader key.
|
||||
GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(BatchInfo));
|
||||
GR_STATIC_ASSERT(kNumShapeTypes <= 8);
|
||||
|
||||
struct IndexRange {
|
||||
bool operator ==(const IndexRange& that) const {
|
||||
SkASSERT(fStart != that.fStart || fCount == that.fCount);
|
||||
return fStart == that.fStart;
|
||||
}
|
||||
bool operator !=(const IndexRange& that) const { return !(*this == that); }
|
||||
|
||||
bool isEmpty() const { return fCount <= 0; }
|
||||
int end() { return fStart + fCount; }
|
||||
|
||||
int16_t fStart;
|
||||
int16_t fCount;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user