| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
#include "src/gpu/graphite/DrawWriter.h" |
| 8 |
|
| 9 |
#include "src/gpu/graphite/Buffer.h" |
| 10 |
#include "src/gpu/graphite/DrawCommands.h" |
| 11 |
|
| 12 |
namespace skgpu::graphite { |
| 13 |
|
| 14 |
DrawWriter::DrawWriter(DrawPassCommands::List* commandList, DrawBufferManager* bufferManager) |
| 15 |
: fCommandList(commandList) |
| 16 |
, fManager(bufferManager) |
| 17 |
, fRenderState(RenderStateFlags::kNone) |
| 18 |
, fPrimitiveType(PrimitiveType::kTriangles) |
| 19 |
, fStaticStride(0) |
| 20 |
, fAppendStride(0) |
| 21 |
, fAppend({}) |
| 22 |
, fStatic({}) |
| 23 |
, fIndices({}) |
| 24 |
, fBoundAppend({}) |
| 25 |
, fBoundStatic({}) |
| 26 |
, fBoundIndices({}) |
| 27 |
, fTemplateCount(0) |
| 28 |
, fPendingCount(0) { |
| 29 |
SkASSERT(commandList && bufferManager); |
| 30 |
} |
| 31 |
|
| 32 |
void DrawWriter::flushInternal(BindBufferInfo appendData) { |
| 33 |
// Skip flush if no items appended, or dynamic instances resolved to zero count. |
| 34 |
if (fPendingCount == 0 || |
| 35 |
((fRenderState & RenderStateFlags::kAppendDynamicInstances) && fTemplateCount == 0)) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
// How much to advance fAppend.fOffset when the flush is completed |
| 40 |
uint32_t advanceOffset = fPendingCount; |
| 41 |
|
| 42 |
// ARM hardware (b/399631317): Unreferenced vertices in sequential indexes of 4 will be |
| 43 |
// speculatively executed. To work around this, we pad the buffer by requesting additional |
| 44 |
// space, and then ensure valid, minimally deleterious data by memsetting the padding to zero. |
| 45 |
if (fRenderState & RenderStateFlags::kAppendVertices) { |
| 46 |
const uint32_t countDiff = (SkAlign4(fPendingCount) - fPendingCount); |
| 47 |
if (countDiff) { |
| 48 |
BufferWriter zWriter = fCurrentBuffer.appendMappedWithStride(countDiff); |
| 49 |
SkASSERT(zWriter); // The buffer should have been sized to hold an aligned total |
| 50 |
zWriter.zeroBytes(countDiff * fAppendStride); |
| 51 |
advanceOffset += countDiff; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Calculate base offsets from buffer info for the draw commands. |
| 56 |
// - If a valid bufferFoo exists and matches the current stride, use pendingBaseFoo as a |
| 57 |
// pseudo-alias for offset and reset the offset and size before assigning to boundBufferFoo. |
| 58 |
// - If a valid bufferFoo does not exist, or is not stride aligned, then draw from the start of |
| 59 |
// the offset with pendingBaseFoo = 0 and assign the current buffer. |
| 60 |
auto bind = [](const BindBufferInfo& buffer, uint32_t stride, BindBufferInfo& boundBuffer, |
| 61 |
uint32_t& pendingBase) -> bool { |
| 62 |
bool shouldBind = false; |
| 63 |
if (buffer.fBuffer) { |
| 64 |
BindBufferInfo newBinding = buffer; |
| 65 |
if (buffer.fOffset % stride == 0) { |
| 66 |
pendingBase = buffer.fOffset / stride; |
| 67 |
newBinding = {buffer.fBuffer, 0, SkTo<uint32_t>(buffer.fBuffer->size())}; |
| 68 |
} |
| 69 |
shouldBind = boundBuffer != newBinding; |
| 70 |
boundBuffer = newBinding; |
| 71 |
} |
| 72 |
return shouldBind; |
| 73 |
}; |
| 74 |
|
| 75 |
uint32_t pendingBaseAppend = 0; |
| 76 |
uint32_t pendingBaseStatic = 0; |
| 77 |
uint32_t pendingBaseIndices = 0; |
| 78 |
if (bind(appendData, fAppendStride, fBoundAppend, pendingBaseAppend)) { |
| 79 |
fCommandList->bindAppendDataBuffer(fBoundAppend); |
| 80 |
} |
| 81 |
if (bind(fStatic, fStaticStride, fBoundStatic, pendingBaseStatic)) { |
| 82 |
fCommandList->bindStaticDataBuffer(fBoundStatic); |
| 83 |
} |
| 84 |
if (bind(fIndices, sizeof(uint16_t), fBoundIndices, pendingBaseIndices)) { |
| 85 |
fCommandList->bindIndexBuffer(fBoundIndices); |
| 86 |
} |
| 87 |
|
| 88 |
// Before any draw commands are added, check if the DrawWriter has an assigned barrier type |
| 89 |
// to issue prior to draw calls. |
| 90 |
if (fBarrierToIssueBeforeDraws != BarrierType::kNone) { |
| 91 |
fCommandList->addBarrier(fBarrierToIssueBeforeDraws); |
| 92 |
} |
| 93 |
|
| 94 |
// Issue the appropriate draw call (instanced vs. non-instanced) based on the current |
| 95 |
// fTemplateCount. Because of the initial AppendDynamicInstances && fTemplateCount check, any |
| 96 |
// DynamicInstance render step must have valid (non-zero) templateCount data at this point. |
| 97 |
if (fTemplateCount) { |
| 98 |
SkASSERT((pendingBaseAppend + fPendingCount)*fAppendStride <= fBoundAppend.fSize); |
| 99 |
if (fIndices) { |
| 100 |
// It's not possible to validate that the indices stored in fIndices access only valid |
| 101 |
// data within fVertices. Simply validate that fIndices holds enough data for the |
| 102 |
// vertex count that's drawn. |
| 103 |
SkASSERT(fTemplateCount*sizeof(uint16_t) <= fIndices.fSize); |
| 104 |
fCommandList->drawIndexedInstanced(fPrimitiveType, |
| 105 |
pendingBaseIndices, |
| 106 |
fTemplateCount, |
| 107 |
pendingBaseStatic, |
| 108 |
pendingBaseAppend, |
| 109 |
fPendingCount); |
| 110 |
|
| 111 |
} else { |
| 112 |
SkASSERT(fTemplateCount*fStaticStride <= fStatic.fSize); |
| 113 |
fCommandList->drawInstanced(fPrimitiveType, pendingBaseStatic, fTemplateCount, |
| 114 |
pendingBaseAppend, fPendingCount); |
| 115 |
} |
| 116 |
|
| 117 |
// Clear instancing template state after the draw is recorded for non-Fixed RenderState |
| 118 |
if (fRenderState & RenderStateFlags::kAppendDynamicInstances) { |
| 119 |
fTemplateCount = 0; |
| 120 |
} |
| 121 |
} else { |
| 122 |
// Issue non-instanced draw call (indexed or non-indexed). |
| 123 |
if (fIndices) { |
| 124 |
// As before, just validate there is sufficient index data |
| 125 |
SkASSERT(fPendingCount*sizeof(uint16_t) <= fIndices.fSize); |
| 126 |
fCommandList->drawIndexed(fPrimitiveType, |
| 127 |
pendingBaseIndices, |
| 128 |
fPendingCount, |
| 129 |
pendingBaseAppend); |
| 130 |
} else { |
| 131 |
SkASSERT((pendingBaseAppend + fPendingCount)*fStaticStride <= fBoundAppend.fSize); |
| 132 |
fCommandList->draw(fPrimitiveType, pendingBaseAppend, fPendingCount); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// Mark all appended items as drawn, advance base offset which is normally set as part of a new |
| 137 |
// pipeline state or flushing for a new buffer during appending data. But if we flushed because |
| 138 |
// of other dynamic state changes, there might not be interaction with the BufferManager. |
| 139 |
fAppend.fOffset += advanceOffset * fAppendStride; |
| 140 |
fPendingCount = 0; |
| 141 |
} |
| 142 |
|
| 143 |
} // namespace skgpu::graphite |