Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
skgpu::graphite::DrawWriter Class Reference

#include <DrawWriter.h>

Classes

class  Appender
 
class  DynamicInstances
 
class  Instances
 
class  Vertices
 

Public Member Functions

 DrawWriter (DrawPassCommands::List *, DrawBufferManager *)
 
 DrawWriter (const DrawWriter &)=delete
 
 DrawWriter (DrawWriter &&)=delete
 
 ~DrawWriter ()
 
DrawBufferManagerbufferManager ()
 
void flush ()
 
void newDynamicState ()
 
void newPipelineState (PrimitiveType type, size_t vertexStride, size_t instanceStride)
 
void draw (BindBufferInfo vertices, unsigned int vertexCount)
 
void drawIndexed (BindBufferInfo vertices, BindBufferInfo indices, unsigned int indexCount)
 
void drawInstanced (BindBufferInfo vertices, unsigned int vertexCount, BindBufferInfo instances, unsigned int instanceCount)
 
void drawIndexedInstanced (BindBufferInfo vertices, BindBufferInfo indices, unsigned int indexCount, BindBufferInfo instances, unsigned int instanceCount)
 

Detailed Description

DrawWriter is a helper around recording draws (to a temporary buffer or directly to a CommandBuffer), particularly when the number of draws is not known ahead of time, or the vertex and instance data is computed at record time and does not have a known size.

To use, construct the DrawWriter with the current pipeline layout or call newPipelineState() on an existing DrawWriter and then bind that matching pipeline. When other dynamic state needs to change between draw calls, notify the DrawWriter using newDynamicState() before recording the modifications. See the listing below for how to append dynamic data or draw with existing buffers

CommandBuffer::draw(vertices)

CommandBuffer::drawIndexed(vertices, indices)

CommandBuffer::drawInstances(vertices, instances)

CommandBuffer::drawIndexedInstanced(vertices, indices, instances)

NOTE: DrawWriter automatically handles failures to find or create a GPU buffer or map it to be writable. All returned VertexWriters will have a non-null pointer to write to, even if it will be discarded due to GPU failure at Recorder::snap() time.

Definition at line 59 of file DrawWriter.h.

Constructor & Destructor Documentation

◆ DrawWriter() [1/3]

skgpu::graphite::DrawWriter::DrawWriter ( DrawPassCommands::List commandList,
DrawBufferManager bufferManager 
)

Definition at line 15 of file DrawWriter.cpp.

16 : fCommandList(commandList)
17 , fManager(bufferManager)
18 , fPrimitiveType(PrimitiveType::kTriangles)
19 , fVertexStride(0)
20 , fInstanceStride(0)
21 , fVertices()
22 , fIndices()
23 , fInstances()
24 , fTemplateCount(0)
25 , fPendingCount(0)
26 , fPendingBase(0)
27 , fPendingBufferBinds(true) {
28 SkASSERT(commandList && bufferManager);
29}
#define SkASSERT(cond)
Definition SkAssert.h:116
DrawBufferManager * bufferManager()
Definition DrawWriter.h:72

◆ DrawWriter() [2/3]

skgpu::graphite::DrawWriter::DrawWriter ( const DrawWriter )
delete

◆ DrawWriter() [3/3]

skgpu::graphite::DrawWriter::DrawWriter ( DrawWriter &&  )
delete

◆ ~DrawWriter()

skgpu::graphite::DrawWriter::~DrawWriter ( )
inline

Definition at line 70 of file DrawWriter.h.

70{ SkASSERT(fPendingCount == 0); }

Member Function Documentation

◆ bufferManager()

DrawBufferManager * skgpu::graphite::DrawWriter::bufferManager ( )
inline

Definition at line 72 of file DrawWriter.h.

72{ return fManager; }

◆ draw()

void skgpu::graphite::DrawWriter::draw ( BindBufferInfo  vertices,
unsigned int  vertexCount 
)
inline

Definition at line 176 of file DrawWriter.h.

176 {
177 this->bindAndFlush(vertices, {}, {}, 0, vertexCount);
178 }

◆ drawIndexed()

void skgpu::graphite::DrawWriter::drawIndexed ( BindBufferInfo  vertices,
BindBufferInfo  indices,
unsigned int  indexCount 
)
inline

Definition at line 179 of file DrawWriter.h.

179 {
180 this->bindAndFlush(vertices, indices, {}, 0, indexCount);
181 }

◆ drawIndexedInstanced()

void skgpu::graphite::DrawWriter::drawIndexedInstanced ( BindBufferInfo  vertices,
BindBufferInfo  indices,
unsigned int  indexCount,
BindBufferInfo  instances,
unsigned int  instanceCount 
)
inline

Definition at line 187 of file DrawWriter.h.

189 {
190 SkASSERT(indexCount > 0);
191 this->bindAndFlush(vertices, indices, instances, indexCount, instanceCount);
192 }

◆ drawInstanced()

void skgpu::graphite::DrawWriter::drawInstanced ( BindBufferInfo  vertices,
unsigned int  vertexCount,
BindBufferInfo  instances,
unsigned int  instanceCount 
)
inline

Definition at line 182 of file DrawWriter.h.

183 {
184 SkASSERT(vertexCount > 0);
185 this->bindAndFlush(vertices, {}, instances, vertexCount, instanceCount);
186 }

◆ flush()

void skgpu::graphite::DrawWriter::flush ( )

Definition at line 78 of file DrawWriter.cpp.

78 {
79 // If nothing was appended, or the only appended data was through dynamic instances and the
80 // final vertex count per instance is 0 (-1 in the sign encoded field), nothing should be drawn.
81 if (fPendingCount == 0 || fTemplateCount == -1) {
82 return;
83 }
84 if (fPendingBufferBinds) {
85 fCommandList->bindDrawBuffers(fVertices, fInstances, fIndices, {});
86 fPendingBufferBinds = false;
87 }
88
89 if (fTemplateCount) {
90 // Instanced drawing
91 unsigned int realVertexCount;
92 if (fTemplateCount < 0) {
93 realVertexCount = -fTemplateCount - 1;
94 fTemplateCount = -1; // reset to re-accumulate max index account for next flush
95 } else {
96 realVertexCount = fTemplateCount;
97 }
98
99 if (fIndices) {
100 fCommandList->drawIndexedInstanced(fPrimitiveType, 0, realVertexCount, 0,
101 fPendingBase, fPendingCount);
102 } else {
103 fCommandList->drawInstanced(fPrimitiveType, 0, realVertexCount,
104 fPendingBase, fPendingCount);
105 }
106 } else {
107 SkASSERT(!fInstances);
108 if (fIndices) {
109 fCommandList->drawIndexed(fPrimitiveType, 0, fPendingCount, fPendingBase);
110 } else {
111 fCommandList->draw(fPrimitiveType, fPendingBase, fPendingCount);
112 }
113 }
114
115 fPendingBase += fPendingCount;
116 fPendingCount = 0;
117}
void draw(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount)
void drawIndexed(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount, unsigned int baseVertex)
void drawIndexedInstanced(PrimitiveType type, unsigned int baseIndex, unsigned int indexCount, unsigned int baseVertex, unsigned int baseInstance, unsigned int instanceCount)
void bindDrawBuffers(BindBufferInfo vertexAttribs, BindBufferInfo instanceAttribs, BindBufferInfo indices, BindBufferInfo indirect)
void drawInstanced(PrimitiveType type, unsigned int baseVertex, unsigned int vertexCount, unsigned int baseInstance, unsigned int instanceCount)

◆ newDynamicState()

void skgpu::graphite::DrawWriter::newDynamicState ( )
inline

Definition at line 77 of file DrawWriter.h.

77{ this->flush(); }

◆ newPipelineState()

void skgpu::graphite::DrawWriter::newPipelineState ( PrimitiveType  type,
size_t  vertexStride,
size_t  instanceStride 
)
inline

Definition at line 82 of file DrawWriter.h.

82 {
83 this->flush();
84 fPrimitiveType = type;
85 fVertexStride = vertexStride;
86 fInstanceStride = instanceStride;
87
88 // NOTE: resetting pending base is sufficient to redo bindings for vertex/instance data that
89 // is later appended but doesn't invalidate bindings for fixed buffers that might not need
90 // to change between pipelines.
91 fPendingBase = 0;
92 SkASSERT(fPendingCount == 0);
93 }

The documentation for this class was generated from the following files: