Add makeVertexWriter helper to GrMeshDrawTarget

Adds makeVertexWriter[AtLeast] and makeIndexWriter[AtLeast] wrappers
around the vertex-space virtuals. Updates call sites of those functions
that immediately passed the returned pointer into a VertexWriter ctor
to call the new function.

This is simplification that will make changing the ctor of VertexWriter
easier (to come in another CL).

Change-Id: I8f4f1a9bed71e368828a42ed798fa6a93030a765
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/496776
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
This commit is contained in:
Michael Ludwig 2022-01-20 16:50:14 -05:00 committed by SkCQ
parent 418557a96a
commit 96860d3bba
15 changed files with 103 additions and 49 deletions

View File

@ -233,8 +233,8 @@ DEF_PATH_TESS_BENCH(middle_out_triangulation,
SkMatrix::I()) {
sk_sp<const GrBuffer> buffer;
int baseVertex;
VertexWriter vertexWriter = static_cast<SkPoint*>(fTarget->makeVertexSpace(
sizeof(SkPoint), kNumCubicsInChalkboard, &buffer, &baseVertex));
VertexWriter vertexWriter = fTarget->makeVertexWriter(
sizeof(SkPoint), kNumCubicsInChalkboard, &buffer, &baseVertex);
AffineMatrix m(gAlmostIdentity);
for (PathMiddleOutFanIter it(fPath); !it.done();) {
for (auto [p0, p1, p2] : it.nextStack()) {

View File

@ -22,7 +22,7 @@ public:
protected:
BufferWriter() = default;
BufferWriter(void* ptr) : fPtr(ptr) {}
explicit BufferWriter(void* ptr) : fPtr(ptr) {}
BufferWriter& operator=(const BufferWriter&) = delete;
BufferWriter& operator=(BufferWriter&& that) {
@ -47,7 +47,7 @@ struct VertexWriter : public BufferWriter {
inline constexpr static uint32_t kIEEE_32_infinity = 0x7f800000;
VertexWriter() = default;
VertexWriter(void* ptr) : BufferWriter(ptr) {}
explicit VertexWriter(void* ptr) : BufferWriter(ptr) {}
VertexWriter(const VertexWriter&) = delete;
VertexWriter(VertexWriter&& that) { *this = std::move(that); }
@ -65,7 +65,7 @@ struct VertexWriter : public BufferWriter {
void* ptr() const { return fPtr; }
VertexWriter makeOffset(ptrdiff_t offsetInBytes) const {
return {SkTAddOffset<void>(fPtr, offsetInBytes)};
return VertexWriter{SkTAddOffset<void>(fPtr, offsetInBytes)};
}
template <typename T>
@ -297,7 +297,8 @@ SK_MAYBE_UNUSED inline VertexWriter& operator<<(VertexWriter& w, const VertexCol
struct IndexWriter : public BufferWriter {
IndexWriter() = default;
IndexWriter(void* ptr) : BufferWriter(ptr) {}
explicit IndexWriter(void* ptr) : BufferWriter(ptr) {}
IndexWriter(const IndexWriter&) = delete;
IndexWriter(IndexWriter&& that) { *this = std::move(that); }
@ -308,7 +309,7 @@ struct IndexWriter : public BufferWriter {
}
IndexWriter makeAdvance(int numIndices) const {
return {SkTAddOffset<void>(fPtr, numIndices * sizeof(uint16_t))};
return IndexWriter{SkTAddOffset<void>(fPtr, numIndices * sizeof(uint16_t))};
}
void writeArray(const uint16_t* array, int count) {
@ -331,7 +332,8 @@ inline IndexWriter& operator<<(IndexWriter& w, int val) { return (w << SkTo<uint
struct UniformWriter : public BufferWriter {
UniformWriter() = default;
UniformWriter(void* ptr) : BufferWriter(ptr) {}
explicit UniformWriter(void* ptr) : BufferWriter(ptr) {}
UniformWriter(const UniformWriter&) = delete;
UniformWriter(UniformWriter&& that) { *this = std::move(that); }

View File

@ -7,8 +7,43 @@
#include "src/gpu/GrMeshDrawTarget.h"
#include "src/gpu/BufferWriter.h"
#include "src/gpu/GrResourceProvider.h"
uint32_t GrMeshDrawTarget::contextUniqueID() const {
return this->resourceProvider()->contextUniqueID();
}
template<typename W>
static W make_writer(void* p, int count, size_t elementSize) {
// TODO: This will do more when writers track their ending mark based on count*elementSize
return W{p};
}
skgpu::VertexWriter GrMeshDrawTarget::makeVertexWriter(
size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>* buffer, int* startVertex) {
void* p = this->makeVertexSpace(vertexSize, vertexCount, buffer, startVertex);
return make_writer<skgpu::VertexWriter>(p, vertexCount, vertexSize);
}
skgpu::IndexWriter GrMeshDrawTarget::makeIndexWriter(
int indexCount, sk_sp<const GrBuffer>* buffer, int* startIndex) {
void* p = this->makeIndexSpace(indexCount, buffer, startIndex);
return make_writer<skgpu::IndexWriter>(p, indexCount, sizeof(uint16_t));
}
skgpu::VertexWriter GrMeshDrawTarget::makeVertexWriterAtLeast(
size_t vertexSize, int minVertexCount, int fallbackVertexCount,
sk_sp<const GrBuffer>* buffer, int* startVertex, int* actualVertexCount) {
void* p = this->makeVertexSpaceAtLeast(vertexSize, minVertexCount, fallbackVertexCount,
buffer, startVertex, actualVertexCount);
return make_writer<skgpu::VertexWriter>(p, *actualVertexCount, vertexSize);
}
skgpu::IndexWriter GrMeshDrawTarget::makeIndexWriterAtLeast(
int minIndexCount, int fallbackIndexCount, sk_sp<const GrBuffer>* buffer,
int* startIndex, int* actualIndexCount) {
void* p = this->makeIndexSpaceAtLeast(minIndexCount, fallbackIndexCount, buffer,
startIndex, actualIndexCount);
return make_writer<skgpu::IndexWriter>(p, *actualIndexCount, sizeof(uint16_t));
}

View File

@ -15,7 +15,12 @@ class GrAtlasManager;
class GrStrikeCache;
class GrThreadSafeCache;
namespace skgpu { namespace v1 { class SmallPathAtlasMgr; }}
namespace skgpu {
namespace v1 { class SmallPathAtlasMgr; }
struct IndexWriter;
struct VertexWriter;
} // namespace skgpu
/*
* Abstract interface that supports creating vertices, indices, and meshes, as well as
@ -94,6 +99,17 @@ public:
sk_sp<const GrBuffer>*,
size_t* offsetInBytes) = 0;
/** Helpers for ops that only need to use the VertexWriter to fill the data directly. */
skgpu::VertexWriter makeVertexWriter(size_t vertexSize, int vertexCount,
sk_sp<const GrBuffer>*, int* startVertex);
skgpu::IndexWriter makeIndexWriter(int indexCount, sk_sp<const GrBuffer>*, int* startIndex);
skgpu::VertexWriter makeVertexWriterAtLeast(size_t vertexSize, int minVertexCount,
int fallbackVertexCount, sk_sp<const GrBuffer>*,
int* startVertex, int* actualVertexCount);
skgpu::IndexWriter makeIndexWriterAtLeast(int minIndexCount, int fallbackIndexCount,
sk_sp<const GrBuffer>*, int* startIndex,
int* actualIndexCount);
/** Helpers for ops which over-allocate and then return excess data to the pool. */
virtual void putBackIndices(int indices) = 0;
virtual void putBackVertices(int vertices, size_t vertexStride) = 0;

View File

@ -437,11 +437,11 @@ sk_sp<const GrGpuBuffer> GrResourceProvider::findOrMakeStaticBuffer(
buffer->resourcePriv().setUniqueKey(uniqueKey);
// Map the buffer. Use a staging buffer on the heap if mapping isn't supported.
skgpu::VertexWriter vertexWriter = buffer->map();
skgpu::VertexWriter vertexWriter{buffer->map()};
SkAutoTMalloc<char> stagingBuffer;
if (!vertexWriter) {
SkASSERT(!buffer->isMapped());
vertexWriter = stagingBuffer.reset(size);
vertexWriter = skgpu::VertexWriter{stagingBuffer.reset(size)};
}
initializeBufferFn(std::move(vertexWriter), size);

View File

@ -24,10 +24,10 @@ bool GrVertexChunkBuilder::allocChunk(int minCount) {
fCurrChunkVertexCount = 0;
GrVertexChunk* chunk = &fChunks->push_back();
int minAllocCount = std::max(minCount, fMinVerticesPerChunk);
fCurrChunkVertexWriter = {fTarget->makeVertexSpaceAtLeast(fStride, minAllocCount,
minAllocCount, &chunk->fBuffer,
&chunk->fBase,
&fCurrChunkVertexCapacity)};
fCurrChunkVertexWriter = fTarget->makeVertexWriterAtLeast(fStride, minAllocCount,
minAllocCount, &chunk->fBuffer,
&chunk->fBase,
&fCurrChunkVertexCapacity);
if (!fCurrChunkVertexWriter || !chunk->fBuffer || fCurrChunkVertexCapacity < minCount) {
SkDebugf("WARNING: Failed to allocate vertex buffer for GrVertexChunk.\n");
fChunks->pop_back();

View File

@ -54,7 +54,7 @@ public:
SkASSERT(count > 0);
if (fCurrChunkVertexCount + count > fCurrChunkVertexCapacity && !this->allocChunk(count)) {
SkDEBUGCODE(fLastAppendAmount = 0;)
return {nullptr};
return {};
}
SkASSERT(fCurrChunkVertexCount + count <= fCurrChunkVertexCapacity);
fCurrChunkVertexCount += count;

View File

@ -803,10 +803,10 @@ private:
sk_sp<const GrBuffer> vertexBuffer;
int firstVertex;
VertexWriter verts{target->makeVertexSpace(kVertexStride,
vertexCount,
&vertexBuffer,
&firstVertex)};
VertexWriter verts = target->makeVertexWriter(kVertexStride,
vertexCount,
&vertexBuffer,
&firstVertex);
if (!verts) {
SkDebugf("Could not allocate vertices\n");

View File

@ -192,7 +192,7 @@ void DrawAtlasPathOp::onPrepare(GrOpFlushState* flushState) {
SkASSERT(fProgram);
}
if (VertexWriter instanceWriter = flushState->makeVertexSpace(
if (VertexWriter instanceWriter = flushState->makeVertexWriter(
fProgram->geomProc().instanceStride(), fInstanceCount, &fInstanceBuffer,
&fBaseInstance)) {
for (const Instance* i = fHeadInstance; i; i = i->fNext) {

View File

@ -697,10 +697,10 @@ void CustomMeshOp::onPrepareDraws(GrMeshDrawTarget* target) {
size_t vertexStride = fSpecification->stride();
sk_sp<const GrBuffer> vertexBuffer;
int firstVertex = 0;
skgpu::VertexWriter verts{target->makeVertexSpace(vertexStride,
fVertexCount,
&vertexBuffer,
&firstVertex)};
skgpu::VertexWriter verts = target->makeVertexWriter(vertexStride,
fVertexCount,
&vertexBuffer,
&firstVertex);
if (!verts) {
SkDebugf("Could not allocate vertices.\n");
return;

View File

@ -520,9 +520,9 @@ void FillRRectOpImpl::onPrepareDraws(GrMeshDrawTarget* target) {
size_t instanceStride = fProgramInfo->geomProc().instanceStride();
if (VertexWriter instanceWrter = target->makeVertexSpace(instanceStride, fInstanceCount,
&fInstanceBuffer, &fBaseInstance)) {
SkDEBUGCODE(auto end = instanceWrter.makeOffset(instanceStride * fInstanceCount));
if (VertexWriter instanceWriter = target->makeVertexWriter(instanceStride, fInstanceCount,
&fInstanceBuffer, &fBaseInstance)) {
SkDEBUGCODE(auto end = instanceWriter.makeOffset(instanceStride * fInstanceCount));
for (Instance* i = fHeadInstance; i; i = i->fNext) {
auto [l, t, r, b] = i->fRRect.rect();
@ -539,14 +539,14 @@ void FillRRectOpImpl::onPrepareDraws(GrMeshDrawTarget* target) {
radiiX *= 2 / (r - l);
radiiY *= 2 / (b - t);
instanceWrter << m.getScaleX() << m.getSkewX() << m.getSkewY() << m.getScaleY()
<< m.getTranslateX() << m.getTranslateY()
<< radiiX << radiiY
<< VertexColor(i->fColor, fProcessorFlags & ProcessorFlags::kWideColor)
<< VertexWriter::If(fProcessorFlags & ProcessorFlags::kHasLocalCoords,
i->fLocalRect);
instanceWriter << m.getScaleX() << m.getSkewX() << m.getSkewY() << m.getScaleY()
<< m.getTranslateX() << m.getTranslateY()
<< radiiX << radiiY
<< VertexColor(i->fColor, fProcessorFlags & ProcessorFlags::kWideColor)
<< VertexWriter::If(fProcessorFlags & ProcessorFlags::kHasLocalCoords,
i->fLocalRect);
}
SkASSERT(instanceWrter == end);
SkASSERT(instanceWriter == end);
}
SKGPU_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);

View File

@ -1281,8 +1281,8 @@ private:
sk_sp<const GrBuffer> vertexBuffer;
int firstVertex;
VertexWriter vertices{target->makeVertexSpace(fProgramInfo->geomProc().vertexStride(),
fVertCount, &vertexBuffer, &firstVertex)};
VertexWriter vertices = target->makeVertexWriter(fProgramInfo->geomProc().vertexStride(),
fVertCount, &vertexBuffer, &firstVertex);
if (!vertices) {
SkDebugf("Could not allocate vertices\n");
return;
@ -1653,8 +1653,8 @@ private:
sk_sp<const GrBuffer> vertexBuffer;
int firstVertex;
VertexWriter vertices{target->makeVertexSpace(fProgramInfo->geomProc().vertexStride(),
fVertCount, &vertexBuffer, &firstVertex)};
VertexWriter vertices = target->makeVertexWriter(fProgramInfo->geomProc().vertexStride(),
fVertCount, &vertexBuffer, &firstVertex);
if (!vertices) {
SkDebugf("Could not allocate vertices\n");
return;
@ -2684,8 +2684,8 @@ private:
sk_sp<const GrBuffer> vertexBuffer;
int firstVertex;
VertexWriter verts{target->makeVertexSpace(fProgramInfo->geomProc().vertexStride(),
fVertCount, &vertexBuffer, &firstVertex)};
VertexWriter verts = target->makeVertexWriter(fProgramInfo->geomProc().vertexStride(),
fVertCount, &vertexBuffer, &firstVertex);
if (!verts) {
SkDebugf("Could not allocate vertices\n");
return;

View File

@ -244,7 +244,8 @@ void PathStencilCoverOp::onPrepare(GrOpFlushState* flushState) {
// edge count of n are fanned by strictly fewer triangles.
int maxTrianglesInFans = std::max(maxCombinedFanEdges - 2, 0);
int fanTriangleCount = 0;
if (VertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxTrianglesInFans * 3)) {
if (VertexWriter triangleVertexWriter =
VertexWriter{vertexAlloc.lock<SkPoint>(maxTrianglesInFans * 3)}) {
for (auto [pathMatrix, path, color] : *fPathDrawList) {
AffineMatrix m(pathMatrix);
for (PathMiddleOutFanIter it(path); !it.done();) {
@ -270,10 +271,10 @@ void PathStencilCoverOp::onPrepare(GrOpFlushState* flushState) {
if (fCoverBBoxProgram) {
size_t instanceStride = fCoverBBoxProgram->geomProc().instanceStride();
VertexWriter vertexWriter = flushState->makeVertexSpace(instanceStride,
fPathCount,
&fBBoxBuffer,
&fBBoxBaseInstance);
VertexWriter vertexWriter = flushState->makeVertexWriter(instanceStride,
fPathCount,
&fBBoxBuffer,
&fBBoxBaseInstance);
SkDEBUGCODE(int pathCount = 0;)
for (auto [pathMatrix, path, color] : *fPathDrawList) {
SkDEBUGCODE(auto end = vertexWriter.makeOffset(instanceStride));

View File

@ -211,9 +211,9 @@ private:
if (instanceCount > SK_MaxS32 / GrResourceProvider::NumVertsPerNonAAQuad()) {
return;
}
VertexWriter vertices{target->makeVertexSpace(
VertexWriter vertices = target->makeVertexWriter(
kVertexStride, GrResourceProvider::NumVertsPerNonAAQuad() * instanceCount,
&flushInfo.fVertexBuffer, &flushInfo.fVertexOffset)};
&flushInfo.fVertexBuffer, &flushInfo.fVertexOffset);
flushInfo.fIndexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
if (!vertices || !flushInfo.fIndexBuffer) {

View File

@ -142,7 +142,7 @@ private:
if (!fFallbackPatchStorage) {
fFallbackPatchStorage.reset(fChunker.stride());
}
vertexWriter = fFallbackPatchStorage.data();
vertexWriter = VertexWriter{fFallbackPatchStorage.data()};
}
return vertexWriter;
}