| 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 |
|
| 8 |
#include "src/gpu/graphite/task/RenderPassTask.h" |
| 9 |
|
| 10 |
#include "include/core/SkPoint.h" |
| 11 |
#include "include/core/SkSize.h" |
| 12 |
#include "include/core/SkSpan.h" |
| 13 |
#include "include/gpu/graphite/Context.h" |
| 14 |
#include "include/gpu/graphite/TextureInfo.h" |
| 15 |
#include "include/private/base/SkAssert.h" |
| 16 |
#include "src/gpu/SkBackingFit.h" |
| 17 |
#include "src/gpu/graphite/Caps.h" |
| 18 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 19 |
#include "src/gpu/graphite/ContextPriv.h" |
| 20 |
#include "src/gpu/graphite/DrawPass.h" |
| 21 |
#include "src/gpu/graphite/Log.h" |
| 22 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 23 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 24 |
#include "src/gpu/graphite/ScratchResourceManager.h" |
| 25 |
#include "src/gpu/graphite/Texture.h" |
| 26 |
#include "src/gpu/graphite/TextureFormat.h" |
| 27 |
#include "src/gpu/graphite/TextureProxy.h" |
| 28 |
|
| 29 |
#include <tuple> |
| 30 |
#include <utility> |
| 31 |
|
| 32 |
namespace skgpu::graphite { |
| 33 |
|
| 34 |
class GraphicsPipeline; |
| 35 |
|
| 36 |
namespace { |
| 37 |
|
| 38 |
// Get the required MSAA size for the render pass. |
| 39 |
// In some scenarios, the MSAA size can be smaller than the target texture. As long as it is big |
| 40 |
// enough to contain the draws' bounds. |
| 41 |
std::pair<SkISize, SkIPoint> get_msaa_size_and_resolve_offset(const SkISize& targetSize, |
| 42 |
const SkIRect& drawBounds, |
| 43 |
const Caps& caps, |
| 44 |
LoadOp loadOp) { |
| 45 |
if (caps.differentResolveAttachmentSizeSupport()) { |
| 46 |
// If possible, use approx size that can fit all draws. This reduces the MSAA texture size |
| 47 |
// and also reuses the textures better. |
| 48 |
// Note: we don't do this if loadOp=Clear because it's supposed to update the whole target |
| 49 |
// texture. |
| 50 |
auto smallEnoughBounds = drawBounds; |
| 51 |
if (loadOp != LoadOp::kClear && !smallEnoughBounds.isEmpty() && |
| 52 |
smallEnoughBounds.intersect(SkIRect::MakeSize(targetSize))) { |
| 53 |
SkIPoint resolveOffset = smallEnoughBounds.topLeft(); |
| 54 |
return {GetApproxSize(smallEnoughBounds.size()), resolveOffset}; |
| 55 |
} else { |
| 56 |
return {GetApproxSize(targetSize), {0, 0}}; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
return {targetSize, {0, 0}}; |
| 61 |
} |
| 62 |
|
| 63 |
} // anonymous namespace |
| 64 |
|
| 65 |
sk_sp<RenderPassTask> RenderPassTask::Make(DrawPassList passes, |
| 66 |
const RenderPassDesc& desc, |
| 67 |
sk_sp<TextureProxy> target, |
| 68 |
sk_sp<TextureProxy> dstCopy, |
| 69 |
SkIRect dstReadBounds) { |
| 70 |
// For now we have one DrawPass per RenderPassTask |
| 71 |
SkASSERT(passes.size() == 1); |
| 72 |
// If we have a dst copy texture, ensure it is big enough to cover the copy bounds that |
| 73 |
// will be sampled. |
| 74 |
SkASSERT(!dstCopy || (dstCopy->dimensions().width() >= dstReadBounds.width() && |
| 75 |
dstCopy->dimensions().height() >= dstReadBounds.height())); |
| 76 |
if (!target) { |
| 77 |
return nullptr; |
| 78 |
} |
| 79 |
|
| 80 |
if (desc.fColorResolveAttachment.fFormat != TextureFormat::kUnsupported) { |
| 81 |
// The resolve attachment must match `target`, since that is what's resolved to. |
| 82 |
SkASSERT(desc.fColorResolveAttachment.isCompatible(target->textureInfo())); |
| 83 |
// The resolve attachment should be single sampled and not depth/stencil |
| 84 |
SkASSERT(desc.fColorResolveAttachment.fSampleCount == SampleCount::k1); |
| 85 |
SkASSERT(!TextureFormatIsDepthOrStencil(desc.fColorResolveAttachment.fFormat)); |
| 86 |
// If there's a resolve attachment, the color attachment should have the same format and |
| 87 |
// more samples than the resolve. |
| 88 |
SkASSERT(desc.fColorAttachment.fFormat == desc.fColorResolveAttachment.fFormat); |
| 89 |
SkASSERT(desc.fColorAttachment.fSampleCount > SampleCount::k1); |
| 90 |
// The render pass's sample count must match the color attachment's sample count |
| 91 |
SkASSERT(desc.fSampleCount == desc.fColorAttachment.fSampleCount); |
| 92 |
} else { |
| 93 |
// The color attachment must match `target`, as it will be used to render directly into. |
| 94 |
SkASSERT(desc.fColorAttachment.isCompatible(target->textureInfo())); |
| 95 |
// The render pass's sample count must match or the color attachment's must be 1 and |
| 96 |
// the render pass has a higher sample count for msaa-render-to-single-sampled extensions. |
| 97 |
SkASSERT(desc.fColorAttachment.fSampleCount == desc.fSampleCount || |
| 98 |
(desc.fColorAttachment.fSampleCount == SampleCount::k1 && |
| 99 |
desc.fSampleCount > SampleCount::k1)); |
| 100 |
} |
| 101 |
|
| 102 |
if (desc.fDepthStencilAttachment.fFormat != TextureFormat::kUnsupported) { |
| 103 |
// The sample count for any depth/stencil buffer must match the render pass. |
| 104 |
SkASSERT(TextureFormatIsDepthOrStencil(desc.fDepthStencilAttachment.fFormat)); |
| 105 |
SkASSERT(desc.fDepthStencilAttachment.fSampleCount == desc.fSampleCount); |
| 106 |
} |
| 107 |
|
| 108 |
return sk_sp<RenderPassTask>(new RenderPassTask(std::move(passes), |
| 109 |
desc, |
| 110 |
std::move(target), |
| 111 |
std::move(dstCopy), |
| 112 |
dstReadBounds)); |
| 113 |
} |
| 114 |
|
| 115 |
RenderPassTask::RenderPassTask(DrawPassList passes, |
| 116 |
const RenderPassDesc& desc, |
| 117 |
sk_sp<TextureProxy> target, |
| 118 |
sk_sp<TextureProxy> dstCopy, |
| 119 |
SkIRect dstReadBounds) |
| 120 |
: fDrawPasses(std::move(passes)) |
| 121 |
, fRenderPassDesc(desc) |
| 122 |
, fTarget(std::move(target)) |
| 123 |
, fDstCopy(std::move(dstCopy)) |
| 124 |
, fDstReadBounds(dstReadBounds) {} |
| 125 |
|
| 126 |
RenderPassTask::~RenderPassTask() = default; |
| 127 |
|
| 128 |
Task::Status RenderPassTask::prepareResources(ResourceProvider* resourceProvider, |
| 129 |
ScratchResourceManager* scratchManager, |
| 130 |
sk_sp<const RuntimeEffectDictionary> runtimeDict) { |
| 131 |
SkASSERT(fTarget); |
| 132 |
|
| 133 |
bool instantiated; |
| 134 |
if (scratchManager->pendingReadCount(fTarget.get()) == 0) { |
| 135 |
// TODO(b/389908339, b/338976898): If there are no pending reads on a scratch texture |
| 136 |
// instantiation request, it means that the scratch Device was caught by a |
| 137 |
// Recorder::flushTrackedDevices() event but hasn't actually been restored to its parent. In |
| 138 |
// this case, the eventual read of the surface will be in another Recording and it can't be |
| 139 |
// allocated as a true scratch resource. |
| 140 |
// |
| 141 |
// Without pending reads, DrawTask does not track its lifecycle to return the scratch |
| 142 |
// resource, so we need to match that and instantiate with a regular non-shareable resource. |
| 143 |
instantiated = TextureProxy::InstantiateIfNotLazy(resourceProvider, fTarget.get()); |
| 144 |
} else { |
| 145 |
instantiated = TextureProxy::InstantiateIfNotLazy(scratchManager, fTarget.get()); |
| 146 |
} |
| 147 |
if (!instantiated) { |
| 148 |
SKGPU_LOG_W("Failed to instantiate RenderPassTask target. Will not create renderpass!"); |
| 149 |
SKGPU_LOG_W("Dimensions are (%d, %d).", |
| 150 |
fTarget->dimensions().width(), fTarget->dimensions().height()); |
| 151 |
return Status::kFail; |
| 152 |
} |
| 153 |
|
| 154 |
// Assuming one draw pass per renderpasstask for now |
| 155 |
SkASSERT(fDrawPasses.size() == 1); |
| 156 |
for (const auto& drawPass: fDrawPasses) { |
| 157 |
if (!drawPass->prepareResources(resourceProvider, runtimeDict, fRenderPassDesc)) { |
| 158 |
return Status::kFail; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// Once all internal resources have been prepared and instantiated, reclaim any pending returns |
| 163 |
// from the scratch manager, since at the equivalent point in the task graph's addCommands() |
| 164 |
// phase, the renderpass will have sampled from any scratch textures and their contents no |
| 165 |
// longer have to be preserved. |
| 166 |
scratchManager->notifyResourcesConsumed(); |
| 167 |
return Status::kSuccess; |
| 168 |
} |
| 169 |
|
| 170 |
Task::Status RenderPassTask::addCommands(Context* context, |
| 171 |
CommandBuffer* commandBuffer, |
| 172 |
ReplayTargetData replayData) { |
| 173 |
// TBD: Expose the surfaces that will need to be attached within the renderpass? |
| 174 |
|
| 175 |
// Instantiate the target |
| 176 |
SkASSERT(fTarget && fTarget->isInstantiated()); |
| 177 |
SkASSERT(!fDstCopy || fDstCopy->isInstantiated()); |
| 178 |
|
| 179 |
// Assuming one draw pass per renderpasstask for now |
| 180 |
SkASSERT(fDrawPasses.size() == 1); |
| 181 |
const auto& drawBounds = fDrawPasses[0]->bounds(); |
| 182 |
|
| 183 |
// Only apply the replay translation and clip if we're drawing to the final replay target. |
| 184 |
SkIVector replayTranslation = {0, 0}; |
| 185 |
SkIRect replayClip = SkIRect::MakeEmpty(); |
| 186 |
if (fTarget->texture() == replayData.fTarget) { |
| 187 |
replayTranslation = replayData.fTranslation; |
| 188 |
replayClip = replayData.fClip; |
| 189 |
} |
| 190 |
|
| 191 |
// We don't instantiate the MSAA or DS attachments in prepareResources because we want to use |
| 192 |
// the discardable attachments from the Context. |
| 193 |
ResourceProvider* resourceProvider = context->priv().resourceProvider(); |
| 194 |
sk_sp<Texture> colorAttachment; |
| 195 |
sk_sp<Texture> resolveAttachment; |
| 196 |
SkIPoint resolveOffset = SkIPoint::Make(0, 0); |
| 197 |
if (fRenderPassDesc.fColorResolveAttachment.fFormat != TextureFormat::kUnsupported) { |
| 198 |
// We always make color msaa attachments shareable. Between any render pass we discard |
| 199 |
// the values of the MSAA texture. Thus it is safe to be used by multiple different render |
| 200 |
// passes without worry of stomping on each other's data. CommandBuffer::addRenderPass is |
| 201 |
// responsible for loading this attachment with the resolve target's original contents. |
| 202 |
TextureInfo colorInfo = context->priv().caps()->getDefaultAttachmentTextureInfo( |
| 203 |
fRenderPassDesc.fColorAttachment, fTarget->isProtected(), Discardable::kYes); |
| 204 |
|
| 205 |
SkISize msaaSize; |
| 206 |
std::tie(msaaSize, resolveOffset) = |
| 207 |
get_msaa_size_and_resolve_offset(fTarget->dimensions(), |
| 208 |
drawBounds.makeOffset(replayTranslation), |
| 209 |
*context->priv().caps(), |
| 210 |
fRenderPassDesc.fColorAttachment.fLoadOp); |
| 211 |
colorAttachment = resourceProvider->findOrCreateShareableTexture( |
| 212 |
msaaSize, colorInfo, "DiscardableMSAAAttachment"); |
| 213 |
if (!colorAttachment) { |
| 214 |
SKGPU_LOG_W("Could not get Color attachment for RenderPassTask"); |
| 215 |
return Status::kFail; |
| 216 |
} |
| 217 |
resolveAttachment = fTarget->refTexture(); |
| 218 |
} else { |
| 219 |
colorAttachment = fTarget->refTexture(); |
| 220 |
} |
| 221 |
|
| 222 |
sk_sp<Texture> depthStencilAttachment; |
| 223 |
if (fRenderPassDesc.fDepthStencilAttachment.fFormat != TextureFormat::kUnsupported) { |
| 224 |
// We always make depth and stencil attachments shareable. Between any render pass the |
| 225 |
// values are reset. Thus it is safe to be used by multiple different render passes without |
| 226 |
// worry of stomping on each other's data. |
| 227 |
TextureInfo dsInfo = context->priv().caps()->getDefaultAttachmentTextureInfo( |
| 228 |
fRenderPassDesc.fDepthStencilAttachment, fTarget->isProtected(), Discardable::kYes); |
| 229 |
SkISize dimensions = context->priv().caps()->getDepthAttachmentDimensions( |
| 230 |
colorAttachment->textureInfo(), colorAttachment->dimensions()); |
| 231 |
|
| 232 |
depthStencilAttachment = resourceProvider->findOrCreateShareableTexture( |
| 233 |
dimensions, dsInfo, "DepthStencilAttachment"); |
| 234 |
if (!depthStencilAttachment) { |
| 235 |
SKGPU_LOG_W("Could not get DepthStencil attachment for RenderPassTask"); |
| 236 |
return Status::kFail; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
// The clip set here will intersect with the render target bounds, and then any scissor set |
| 241 |
// during this render pass. If there is no intersection between the clip and the render target |
| 242 |
// bounds, we can skip this entire render pass. |
| 243 |
// Note: if the MSAA texture is allocated smaller than the target texture, we need to apply an |
| 244 |
// additional translation (-resolveOffset) so that the draws' bounds' top left corner |
| 245 |
// will be at (0, 0) on the MSAA texture |
| 246 |
const SkIRect renderTargetBounds = SkIRect::MakeSize(colorAttachment->dimensions()); |
| 247 |
if (!commandBuffer->setReplayTranslationAndClip( |
| 248 |
replayTranslation - resolveOffset, replayClip, renderTargetBounds)) { |
| 249 |
return Status::kSuccess; |
| 250 |
} |
| 251 |
|
| 252 |
// TODO(b/313629288) we always pass in the render target's dimensions as the viewport here. |
| 253 |
// Using the dimensions of the logical device that we're drawing to could reduce flakiness in |
| 254 |
// rendering. |
| 255 |
if (commandBuffer->addRenderPass(fRenderPassDesc, |
| 256 |
std::move(colorAttachment), |
| 257 |
std::move(resolveAttachment), |
| 258 |
std::move(depthStencilAttachment), |
| 259 |
fDstCopy ? fDstCopy->texture() : nullptr, |
| 260 |
fDstReadBounds, |
| 261 |
resolveOffset, |
| 262 |
fTarget->dimensions(), |
| 263 |
fDrawPasses)) { |
| 264 |
return Status::kSuccess; |
| 265 |
} else { |
| 266 |
return Status::kFail; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
bool RenderPassTask::visitPipelines(const std::function<bool(const GraphicsPipeline*)>& visitor) { |
| 271 |
for (const std::unique_ptr<DrawPass>& pass : fDrawPasses) { |
| 272 |
for (const sk_sp<GraphicsPipeline>& pipeline : pass->pipelines()) { |
| 273 |
if (!visitor(pipeline.get())) { |
| 274 |
return false; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
bool RenderPassTask::visitProxies(const std::function<bool(const TextureProxy*)>& visitor, |
| 283 |
bool readsOnly) { |
| 284 |
for (const std::unique_ptr<DrawPass>& pass : fDrawPasses) { |
| 285 |
for (const sk_sp<TextureProxy>& proxy : pass->sampledTextures()) { |
| 286 |
if (!visitor(proxy.get())) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (fDstCopy && !visitor(fDstCopy.get())) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
// Skip visiting the target if we're only visiting read textures |
| 296 |
if (!readsOnly && fTarget && !visitor(fTarget.get())) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
} // namespace skgpu::graphite |