| 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/DrawList.h" |
| 8 |
|
| 9 |
#include "include/core/SkTypes.h" |
| 10 |
#include "include/gpu/graphite/Recorder.h" |
| 11 |
#include "src/core/SkTraceEvent.h" |
| 12 |
#include "src/gpu/graphite/DrawPass.h" |
| 13 |
#include "src/gpu/graphite/DrawWriter.h" |
| 14 |
#include "src/gpu/graphite/KeyContext.h" |
| 15 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 16 |
#include "src/gpu/graphite/Renderer.h" |
| 17 |
#include "src/gpu/graphite/geom/Geometry.h" |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
|
| 21 |
void DrawList::recordDraw(const Renderer* renderer, |
| 22 |
const Transform& localToDevice, |
| 23 |
const Geometry& geometry, |
| 24 |
const Clip& clip, |
| 25 |
DrawOrder ordering, |
| 26 |
UniquePaintParamsID paintID, |
| 27 |
SkEnumBitMask<DstUsage> dstUsage, |
| 28 |
BarrierType barrierBeforeDraws, |
| 29 |
PipelineDataGatherer* gatherer, |
| 30 |
const StrokeStyle* stroke) { |
| 31 |
SkASSERT(localToDevice.valid()); |
| 32 |
SkASSERT(!geometry.isEmpty() && !clip.drawBounds().isEmptyNegativeOrNaN()); |
| 33 |
SkASSERT(!(renderer->depthStencilFlags() & DepthStencilFlags::kStencil) || |
| 34 |
ordering.stencilIndex() != DrawOrder::kUnassigned); |
| 35 |
|
| 36 |
// TODO: Add validation that the renderer's expected shape type and stroke params match provided |
| 37 |
const Draw& draw = fDraws.emplace_back(renderer, |
| 38 |
this->deduplicateTransform(localToDevice), |
| 39 |
geometry, |
| 40 |
clip, |
| 41 |
ordering, |
| 42 |
barrierBeforeDraws, |
| 43 |
stroke); |
| 44 |
|
| 45 |
fRenderStepCount += renderer->numRenderSteps(); |
| 46 |
// Create a sort key for every render step in this draw |
| 47 |
for (int stepIndex = 0; stepIndex < draw.renderer()->numRenderSteps(); ++stepIndex) { |
| 48 |
const RenderStep* const step = draw.renderer()->steps()[stepIndex]; |
| 49 |
gatherer->markOffsetAndAlign(step->performsShading(), step->uniformAlignment()); |
| 50 |
|
| 51 |
GraphicsPipelineCache::Index pipelineIndex = fPipelineCache.insert( |
| 52 |
{ step->renderStepID(), step->performsShading() ? |
| 53 |
paintID : UniquePaintParamsID::Invalid()}); |
| 54 |
|
| 55 |
step->writeUniformsAndTextures(draw.drawParams(), gatherer); |
| 56 |
|
| 57 |
auto [combinedUniforms, combinedTextures] = |
| 58 |
gatherer->endCombinedData(step->performsShading()); |
| 59 |
|
| 60 |
UniformDataCache::Index uniformIndex = combinedUniforms ? |
| 61 |
fUniformDataCache.insert(combinedUniforms) : UniformDataCache::kInvalidIndex; |
| 62 |
TextureDataCache::Index textureBindingIndex = combinedTextures ? |
| 63 |
fTextureDataCache.insert(combinedTextures) : TextureDataCache::kInvalidIndex; |
| 64 |
|
| 65 |
fSortKeys.push_back({&draw, stepIndex, pipelineIndex, uniformIndex, textureBindingIndex}); |
| 66 |
gatherer->rewindForRenderStep(); |
| 67 |
} |
| 68 |
|
| 69 |
fPassBounds.join(clip.drawBounds()); |
| 70 |
fRequiresMSAA |= renderer->requiresMSAA(); |
| 71 |
fDepthStencilFlags |= renderer->depthStencilFlags(); |
| 72 |
if (dstUsage & DstUsage::kDstReadRequired) { |
| 73 |
// For paints that read from the dst, update the bounds. It may later be determined that the |
| 74 |
// DstReadStrategy does not require them, but they are inexpensive to track. |
| 75 |
fDstReadBounds.join(clip.drawBounds()); |
| 76 |
} |
| 77 |
|
| 78 |
#if defined(SK_DEBUG) |
| 79 |
if (geometry.isCoverageMaskShape()) { |
| 80 |
fCoverageMaskShapeDrawCount++; |
| 81 |
} |
| 82 |
#endif |
| 83 |
} |
| 84 |
|
| 85 |
std::unique_ptr<DrawPass> DrawList::snapDrawPass(Recorder* recorder, |
| 86 |
sk_sp<TextureProxy> target, |
| 87 |
const SkImageInfo& targetInfo, |
| 88 |
DstReadStrategy dstReadStrategy) { |
| 89 |
// NOTE: This assert is here to ensure SortKey is as tightly packed as possible. Any change to |
| 90 |
// its size should be done with care and good reason. The performance of sorting the keys is |
| 91 |
// heavily tied to the total size. |
| 92 |
// |
| 93 |
// At 24 bytes (current), sorting is about 30% slower than if SortKey could be packed into just |
| 94 |
// 16 bytes. There are several ways this could be done if necessary: |
| 95 |
// - Restricting the max draw count to 16k (14-bits) and only using a single index to refer to |
| 96 |
// the uniform data => 8 bytes of key, 8 bytes of pointer. |
| 97 |
// - Restrict the max draw count to 32k (15-bits), use a single uniform index, and steal the |
| 98 |
// 4 low bits from the Draw* pointer since it's 16 byte aligned. |
| 99 |
// - Compact the Draw* to an index into the original collection, although that has extra |
| 100 |
// indirection and does not work as well with SkTBlockList. |
| 101 |
// In pseudo tests, manipulating the pointer or having to mask out indices was about 15% slower |
| 102 |
// than an 8 byte key and unmodified pointer. |
| 103 |
static_assert(sizeof(SortKey) == SkAlignTo(16 + sizeof(void*), alignof(SortKey))); |
| 104 |
|
| 105 |
// TODO: Explore sorting algorithms; in all likelihood this will be mostly sorted already, so |
| 106 |
// algorithms that approach O(n) in that condition may be favorable. Alternatively, could |
| 107 |
// explore radix sort that is always O(n). Brief testing suggested std::sort was faster than |
| 108 |
// std::stable_sort and SkTQSort on my [ml]'s Windows desktop. Also worth considering in-place |
| 109 |
// vs. algorithms that require an extra O(n) storage. |
| 110 |
// TODO: It's not strictly necessary, but would a stable sort be useful or just end up hiding |
| 111 |
// bugs in the DrawOrder determination code? |
| 112 |
std::sort(fSortKeys.begin(), fSortKeys.end()); |
| 113 |
|
| 114 |
TRACE_EVENT1("skia.gpu", TRACE_FUNC, "draw count", fDraws.count()); |
| 115 |
|
| 116 |
// The DrawList is converted directly into the DrawPass' data structures, but once the DrawPass |
| 117 |
// is returned from Make(), it is considered immutable. |
| 118 |
std::unique_ptr<DrawPass> drawPass(new DrawPass(target, {fLoadOp, StoreOp::kStore}, fClearColor, |
| 119 |
recorder->priv().refFloatStorageManager())); |
| 120 |
|
| 121 |
DrawBufferManager* bufferMgr = recorder->priv().drawBufferManager(); |
| 122 |
DrawWriter drawWriter(&drawPass->fCommandList, bufferMgr); |
| 123 |
GraphicsPipelineCache::Index lastPipeline = GraphicsPipelineCache::kInvalidIndex; |
| 124 |
const SkIRect targetBounds = SkIRect::MakeSize(targetInfo.dimensions()); |
| 125 |
SkIRect lastScissor = targetBounds; |
| 126 |
|
| 127 |
SkASSERT(drawPass->fTarget->isFullyLazy() || |
| 128 |
SkIRect::MakeSize(drawPass->fTarget->dimensions()).contains(lastScissor)); |
| 129 |
drawPass->fCommandList.setScissor(lastScissor); |
| 130 |
|
| 131 |
const Caps* caps = recorder->priv().caps(); |
| 132 |
const bool useStorageBuffers = caps->storageBufferSupport(); |
| 133 |
UniformTracker uniformTracker(useStorageBuffers); |
| 134 |
|
| 135 |
// TODO(b/372953722): Remove this forced binding command behavior once dst copies are always |
| 136 |
// bound separately from the rest of the textures. |
| 137 |
const bool rebindTexturesOnPipelineChange = dstReadStrategy == DstReadStrategy::kTextureCopy; |
| 138 |
// Keep track of the prior draw's PaintOrder. If the current draw requires barriers and there |
| 139 |
// is no pipeline or state change, then we must compare the current and prior draw's PaintOrders |
| 140 |
// to determine if the draws overlap. If they do, we must inject a flush between them such that |
| 141 |
// the barrier addition and draw commands are ordered correctly. |
| 142 |
CompressedPaintersOrder priorDrawPaintOrder {}; |
| 143 |
|
| 144 |
// Accumulate rough pixel area touched by each pipeline as we iterate the SortKeys |
| 145 |
drawPass->fPipelineDrawAreas.push_back_n(fPipelineCache.count(), 0.f); |
| 146 |
|
| 147 |
TextureTracker textureBindingTracker(&fTextureDataCache); |
| 148 |
for (const DrawList::SortKey& key : fSortKeys) { |
| 149 |
const DrawList::Draw& draw = key.draw(); |
| 150 |
const RenderStep& renderStep = key.renderStep(); |
| 151 |
|
| 152 |
const bool pipelineChange = key.pipelineIndex() != lastPipeline; |
| 153 |
drawPass->fPipelineDrawAreas[key.pipelineIndex()] += |
| 154 |
draw.drawParams().drawBounds().area(); |
| 155 |
|
| 156 |
const bool uniformBindingChange = uniformTracker.writeUniforms( |
| 157 |
fUniformDataCache, bufferMgr, key.uniformIndex()); |
| 158 |
|
| 159 |
// TODO(b/372953722): The Dawn and Vulkan CommandBuffer implementations currently append any |
| 160 |
// dst copy to the texture bind group/descriptor set automatically when processing a |
| 161 |
// BindTexturesAndSamplers call because they use a single group to contain all textures. |
| 162 |
// However, from the DrawPass POV, we can run into the scenario where two pipelines have the |
| 163 |
// same textures+samplers except one requires a dst-copy and the other does not. In this |
| 164 |
// case we wouldn't necessarily insert a new command when the pipeline changed and then |
| 165 |
// end up with layout validation errors. |
| 166 |
const bool textureBindingsChange = textureBindingTracker.setCurrentTextureBindings( |
| 167 |
key.textureBindingIndex()) || |
| 168 |
(rebindTexturesOnPipelineChange && pipelineChange && |
| 169 |
key.textureBindingIndex() != TextureDataCache::kInvalidIndex); |
| 170 |
|
| 171 |
std::optional<SkIRect> newScissor = |
| 172 |
renderStep.getScissor(draw.drawParams(), lastScissor, targetBounds); |
| 173 |
|
| 174 |
const bool stateChange = uniformBindingChange || textureBindingsChange || |
| 175 |
newScissor.has_value(); |
| 176 |
|
| 177 |
// Update DrawWriter *before* we actually change any state so that accumulated draws from |
| 178 |
// the previous state use the proper state. |
| 179 |
if (pipelineChange) { |
| 180 |
drawWriter.newPipelineState(renderStep.primitiveType(), |
| 181 |
renderStep.staticDataStride(), |
| 182 |
renderStep.appendDataStride(), |
| 183 |
renderStep.getRenderStateFlags(), |
| 184 |
draw.barrierBeforeDraws()); |
| 185 |
} else if (stateChange) { |
| 186 |
drawWriter.newDynamicState(); |
| 187 |
} else if (draw.barrierBeforeDraws() != BarrierType::kNone && |
| 188 |
priorDrawPaintOrder != draw.drawParams().order().paintOrder()) { |
| 189 |
// Even if there is no pipeline or state change, we must consider whether a |
| 190 |
// DrawPassCommand to add barriers must be inserted before any draw commands. If so, |
| 191 |
// then determine if the current and prior draws overlap (ie, their PaintOrders are |
| 192 |
// unequal). If so, perform a flush() to make sure the draw and add barrier commands are |
| 193 |
// appended to the command list in the proper order. |
| 194 |
drawWriter.flush(); |
| 195 |
} |
| 196 |
|
| 197 |
// Make state changes before accumulating new draw data |
| 198 |
if (pipelineChange) { |
| 199 |
drawPass->fCommandList.bindGraphicsPipeline(key.pipelineIndex()); |
| 200 |
lastPipeline = key.pipelineIndex(); |
| 201 |
} |
| 202 |
if (stateChange) { |
| 203 |
if (uniformBindingChange) { |
| 204 |
uniformTracker.bindUniforms(UniformSlot::kCombinedUniforms, &drawPass->fCommandList); |
| 205 |
} |
| 206 |
if (textureBindingsChange) { |
| 207 |
textureBindingTracker.bindTextures(&drawPass->fCommandList); |
| 208 |
} |
| 209 |
if (newScissor.has_value()) { |
| 210 |
drawPass->fCommandList.setScissor(*newScissor); |
| 211 |
lastScissor = *newScissor; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
uint32_t uniformSsboIndex = useStorageBuffers ? uniformTracker.ssboIndex() : 0; |
| 216 |
renderStep.writeVertices(&drawWriter, draw.drawParams(), uniformSsboIndex); |
| 217 |
|
| 218 |
if (bufferMgr->hasMappingFailed()) { |
| 219 |
SKGPU_LOG_W("Failed to write necessary vertex/instance data for DrawPass, dropping!"); |
| 220 |
return nullptr; |
| 221 |
} |
| 222 |
|
| 223 |
// Update priorDrawPaintOrder value before iterating to analyze the next draw. |
| 224 |
priorDrawPaintOrder = draw.drawParams().order().paintOrder(); |
| 225 |
} |
| 226 |
// Finish recording draw calls for any collected data still pending at end of the loop |
| 227 |
drawWriter.flush(); |
| 228 |
|
| 229 |
drawPass->fBounds = fPassBounds.roundOut().asSkIRect(); |
| 230 |
drawPass->fPipelineDescs = fPipelineCache.detach(); |
| 231 |
drawPass->fSampledTextures = fTextureDataCache.detachTextures(); |
| 232 |
|
| 233 |
TRACE_COUNTER1("skia.gpu", "# pipelines", drawPass->fPipelineDescs.size()); |
| 234 |
TRACE_COUNTER1("skia.gpu", "# textures", drawPass->fSampledTextures.size()); |
| 235 |
TRACE_COUNTER1("skia.gpu", "# commands", drawPass->fCommandList.count()); |
| 236 |
|
| 237 |
this->reset(LoadOp::kLoad); |
| 238 |
|
| 239 |
return drawPass; |
| 240 |
} |
| 241 |
|
| 242 |
void DrawList::reset(LoadOp op, SkColor4f clearColor) { |
| 243 |
fDraws.reset(); |
| 244 |
fSortKeys.clear(); |
| 245 |
DrawListBase::reset(op, clearColor); |
| 246 |
} |
| 247 |
|
| 248 |
} // namespace skgpu::graphite |