| 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/mtl/MtlCommandBuffer.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/BackendSemaphore.h" |
| 11 |
#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h" |
| 12 |
#include "src/gpu/graphite/ContextUtils.h" |
| 13 |
#include "src/gpu/graphite/Log.h" |
| 14 |
#include "src/gpu/graphite/PipelineData.h" |
| 15 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 16 |
#include "src/gpu/graphite/TextureFormat.h" |
| 17 |
#include "src/gpu/graphite/TextureProxy.h" |
| 18 |
#include "src/gpu/graphite/UniformManager.h" |
| 19 |
#include "src/gpu/graphite/compute/DispatchGroup.h" |
| 20 |
#include "src/gpu/graphite/mtl/MtlBlitCommandEncoder.h" |
| 21 |
#include "src/gpu/graphite/mtl/MtlBuffer.h" |
| 22 |
#include "src/gpu/graphite/mtl/MtlCaps.h" |
| 23 |
#include "src/gpu/graphite/mtl/MtlCommandBuffer.h" |
| 24 |
#include "src/gpu/graphite/mtl/MtlComputeCommandEncoder.h" |
| 25 |
#include "src/gpu/graphite/mtl/MtlComputePipeline.h" |
| 26 |
#include "src/gpu/graphite/mtl/MtlGraphicsPipeline.h" |
| 27 |
#include "src/gpu/graphite/mtl/MtlRenderCommandEncoder.h" |
| 28 |
#include "src/gpu/graphite/mtl/MtlSampler.h" |
| 29 |
#include "src/gpu/graphite/mtl/MtlSharedContext.h" |
| 30 |
#include "src/gpu/graphite/mtl/MtlTexture.h" |
| 31 |
#include "src/gpu/mtl/MtlUtilsPriv.h" |
| 32 |
|
| 33 |
namespace skgpu::graphite { |
| 34 |
|
| 35 |
std::unique_ptr<MtlCommandBuffer> MtlCommandBuffer::Make(id<MTLCommandQueue> queue, |
| 36 |
const MtlSharedContext* sharedContext, |
| 37 |
MtlResourceProvider* resourceProvider) { |
| 38 |
auto commandBuffer = std::unique_ptr<MtlCommandBuffer>( |
| 39 |
new MtlCommandBuffer(queue, sharedContext, resourceProvider)); |
| 40 |
if (!commandBuffer) { |
| 41 |
return nullptr; |
| 42 |
} |
| 43 |
if (!commandBuffer->createNewMTLCommandBuffer()) { |
| 44 |
return nullptr; |
| 45 |
} |
| 46 |
return commandBuffer; |
| 47 |
} |
| 48 |
|
| 49 |
MtlCommandBuffer::MtlCommandBuffer(id<MTLCommandQueue> queue, |
| 50 |
const MtlSharedContext* sharedContext, |
| 51 |
MtlResourceProvider* resourceProvider) |
| 52 |
: CommandBuffer(Protected::kNo) // Metal doesn't support protected memory |
| 53 |
, fQueue(queue) |
| 54 |
, fSharedContext(sharedContext) |
| 55 |
, fResourceProvider(resourceProvider) {} |
| 56 |
|
| 57 |
MtlCommandBuffer::~MtlCommandBuffer() { |
| 58 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 59 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 60 |
SkASSERT(!fActiveBlitCommandEncoder); |
| 61 |
} |
| 62 |
|
| 63 |
bool MtlCommandBuffer::setNewCommandBufferResources() { |
| 64 |
return this->createNewMTLCommandBuffer(); |
| 65 |
} |
| 66 |
|
| 67 |
bool MtlCommandBuffer::createNewMTLCommandBuffer() { |
| 68 |
SkASSERT(fCommandBuffer == nil); |
| 69 |
|
| 70 |
// Inserting a pool here so the autorelease occurs when we return and the |
| 71 |
// only remaining ref is the retain below. |
| 72 |
@autoreleasepool { |
| 73 |
if (@available(macOS 11.0, iOS 14.0, tvOS 14.0, *)) { |
| 74 |
sk_cfp<MTLCommandBufferDescriptor*> desc([[MTLCommandBufferDescriptor alloc] init]); |
| 75 |
(*desc).retainedReferences = NO; |
| 76 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 77 |
(*desc).errorOptions = MTLCommandBufferErrorOptionEncoderExecutionStatus; |
| 78 |
#endif |
| 79 |
// We add a retain here because the command buffer is set to autorelease (not alloc or copy) |
| 80 |
fCommandBuffer.reset([[fQueue commandBufferWithDescriptor:desc.get()] retain]); |
| 81 |
} else { |
| 82 |
// We add a retain here because the command buffer is set to autorelease (not alloc or copy) |
| 83 |
fCommandBuffer.reset([[fQueue commandBufferWithUnretainedReferences] retain]); |
| 84 |
} |
| 85 |
} |
| 86 |
return fCommandBuffer != nil; |
| 87 |
} |
| 88 |
|
| 89 |
bool MtlCommandBuffer::commit() { |
| 90 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 91 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 92 |
this->endBlitCommandEncoder(); |
| 93 |
[(*fCommandBuffer) commit]; |
| 94 |
|
| 95 |
if ((*fCommandBuffer).status == MTLCommandBufferStatusError) { |
| 96 |
NSString* description = (*fCommandBuffer).error.localizedDescription; |
| 97 |
const char* errorString = [description UTF8String]; |
| 98 |
SKGPU_LOG_E("Failure submitting command buffer: %s", errorString); |
| 99 |
} |
| 100 |
|
| 101 |
return ((*fCommandBuffer).status != MTLCommandBufferStatusError); |
| 102 |
} |
| 103 |
|
| 104 |
void MtlCommandBuffer::onResetCommandBuffer() { |
| 105 |
fCommandBuffer.reset(); |
| 106 |
fActiveRenderCommandEncoder.reset(); |
| 107 |
fActiveComputeCommandEncoder.reset(); |
| 108 |
fActiveBlitCommandEncoder.reset(); |
| 109 |
fCurrentIndexBuffer = nil; |
| 110 |
fCurrentIndexBufferOffset = 0; |
| 111 |
} |
| 112 |
|
| 113 |
void MtlCommandBuffer::addWaitSemaphores(size_t numWaitSemaphores, |
| 114 |
const BackendSemaphore* waitSemaphores) { |
| 115 |
if (!waitSemaphores) { |
| 116 |
SkASSERT(numWaitSemaphores == 0); |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
// Can only insert events with no active encoder |
| 121 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 122 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 123 |
this->endBlitCommandEncoder(); |
| 124 |
if (@available(macOS 10.14, iOS 12.0, tvOS 12.0, *)) { |
| 125 |
for (size_t i = 0; i < numWaitSemaphores; ++i) { |
| 126 |
auto semaphore = waitSemaphores[i]; |
| 127 |
if (semaphore.isValid() && semaphore.backend() == BackendApi::kMetal) { |
| 128 |
id<MTLEvent> mtlEvent = |
| 129 |
(__bridge id<MTLEvent>)BackendSemaphores::GetMtlEvent(semaphore); |
| 130 |
[(*fCommandBuffer) encodeWaitForEvent:mtlEvent |
| 131 |
value:BackendSemaphores::GetMtlValue(semaphore)]; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void MtlCommandBuffer::addSignalSemaphores(size_t numSignalSemaphores, |
| 138 |
const BackendSemaphore* signalSemaphores) { |
| 139 |
if (!signalSemaphores) { |
| 140 |
SkASSERT(numSignalSemaphores == 0); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
// Can only insert events with no active encoder |
| 145 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 146 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 147 |
this->endBlitCommandEncoder(); |
| 148 |
|
| 149 |
if (@available(macOS 10.14, iOS 12.0, tvOS 12.0, *)) { |
| 150 |
for (size_t i = 0; i < numSignalSemaphores; ++i) { |
| 151 |
auto semaphore = signalSemaphores[i]; |
| 152 |
if (semaphore.isValid() && semaphore.backend() == BackendApi::kMetal) { |
| 153 |
id<MTLEvent> mtlEvent = (__bridge id<MTLEvent>)BackendSemaphores::GetMtlEvent; |
| 154 |
[(*fCommandBuffer) encodeSignalEvent:mtlEvent |
| 155 |
value:BackendSemaphores::GetMtlValue(semaphore)]; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
bool MtlCommandBuffer::onAddRenderPass(const RenderPassDesc& renderPassDesc, |
| 162 |
SkIRect renderPassBounds, |
| 163 |
const Texture* colorTexture, |
| 164 |
const Texture* resolveTexture, |
| 165 |
const Texture* depthStencilTexture, |
| 166 |
SkIPoint resolveOffset, |
| 167 |
SkIRect viewport, |
| 168 |
const DrawPassList& drawPasses) { |
| 169 |
SkASSERT(resolveOffset.isZero()); |
| 170 |
if (!this->beginRenderPass(renderPassDesc, colorTexture, resolveTexture, depthStencilTexture)) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
this->setViewport(viewport.x(), viewport.y(), viewport.width(), viewport.height(), 0, 1); |
| 175 |
this->updateIntrinsicUniforms(viewport); |
| 176 |
|
| 177 |
for (const auto& drawPass : drawPasses) { |
| 178 |
if (!this->addDrawPass(drawPass.get())) SK_UNLIKELY { |
| 179 |
this->endRenderPass(); |
| 180 |
return false; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
this->endRenderPass(); |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
bool MtlCommandBuffer::onAddComputePass(DispatchGroupSpan groups) { |
| 189 |
this->beginComputePass(); |
| 190 |
for (const auto& group : groups) { |
| 191 |
group->addResourceRefs(this); |
| 192 |
for (const auto& dispatch : group->dispatches()) { |
| 193 |
this->bindComputePipeline(group->getPipeline(dispatch.fPipelineIndex)); |
| 194 |
for (const ResourceBinding& binding : dispatch.fBindings) { |
| 195 |
if (const BindBufferInfo* buffer = std::get_if<BindBufferInfo>(&binding.fResource)) { |
| 196 |
this->bindBuffer(buffer->fBuffer, buffer->fOffset, binding.fIndex); |
| 197 |
} else if (const TextureIndex* texIdx = |
| 198 |
std::get_if<TextureIndex>(&binding.fResource)) { |
| 199 |
SkASSERT(texIdx); |
| 200 |
this->bindTexture(group->getTexture(texIdx->fValue), binding.fIndex); |
| 201 |
} else { |
| 202 |
const SamplerIndex* samplerIdx = std::get_if<SamplerIndex>(&binding.fResource); |
| 203 |
SkASSERT(samplerIdx); |
| 204 |
this->bindSampler(group->getSampler(samplerIdx->fValue), binding.fIndex); |
| 205 |
} |
| 206 |
} |
| 207 |
SkASSERT(fActiveComputeCommandEncoder); |
| 208 |
for (const ComputeStep::WorkgroupBufferDesc& wgBuf : dispatch.fWorkgroupBuffers) { |
| 209 |
fActiveComputeCommandEncoder->setThreadgroupMemoryLength( |
| 210 |
SkAlignTo(wgBuf.size, 16u), |
| 211 |
wgBuf.index); |
| 212 |
} |
| 213 |
if (const WorkgroupSize* globalSize = |
| 214 |
std::get_if<WorkgroupSize>(&dispatch.fGlobalSizeOrIndirect)) { |
| 215 |
this->dispatchThreadgroups(*globalSize, dispatch.fLocalSize); |
| 216 |
} else { |
| 217 |
SkASSERT(std::holds_alternative<BindBufferInfo>(dispatch.fGlobalSizeOrIndirect)); |
| 218 |
const BindBufferInfo& indirect = |
| 219 |
*std::get_if<BindBufferInfo>(&dispatch.fGlobalSizeOrIndirect); |
| 220 |
this->dispatchThreadgroupsIndirect( |
| 221 |
dispatch.fLocalSize, indirect.fBuffer, indirect.fOffset); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
this->endComputePass(); |
| 226 |
return true; |
| 227 |
} |
| 228 |
|
| 229 |
bool MtlCommandBuffer::beginRenderPass(const RenderPassDesc& renderPassDesc, |
| 230 |
const Texture* colorTexture, |
| 231 |
const Texture* resolveTexture, |
| 232 |
const Texture* depthStencilTexture) { |
| 233 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 234 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 235 |
this->endBlitCommandEncoder(); |
| 236 |
|
| 237 |
const static MTLLoadAction mtlLoadAction[] { |
| 238 |
MTLLoadActionLoad, |
| 239 |
MTLLoadActionClear, |
| 240 |
MTLLoadActionDontCare |
| 241 |
}; |
| 242 |
static_assert((int)LoadOp::kLoad == 0); |
| 243 |
static_assert((int)LoadOp::kClear == 1); |
| 244 |
static_assert((int)LoadOp::kDiscard == 2); |
| 245 |
static_assert(std::size(mtlLoadAction) == kLoadOpCount); |
| 246 |
|
| 247 |
const static MTLStoreAction mtlStoreAction[] { |
| 248 |
MTLStoreActionStore, |
| 249 |
MTLStoreActionDontCare |
| 250 |
}; |
| 251 |
static_assert((int)StoreOp::kStore == 0); |
| 252 |
static_assert((int)StoreOp::kDiscard == 1); |
| 253 |
static_assert(std::size(mtlStoreAction) == kStoreOpCount); |
| 254 |
|
| 255 |
sk_cfp<MTLRenderPassDescriptor*> descriptor([[MTLRenderPassDescriptor alloc] init]); |
| 256 |
// Validate attachment descs and textures |
| 257 |
const auto& colorInfo = renderPassDesc.fColorAttachment; |
| 258 |
const auto& resolveInfo = renderPassDesc.fColorResolveAttachment; |
| 259 |
const auto& depthStencilInfo = renderPassDesc.fDepthStencilAttachment; |
| 260 |
SkASSERT(colorTexture ? colorInfo.isCompatible(colorTexture->textureInfo()) |
| 261 |
: colorInfo.fFormat == TextureFormat::kUnsupported); |
| 262 |
SkASSERT(resolveTexture ? resolveInfo.isCompatible(resolveTexture->textureInfo()) |
| 263 |
: resolveInfo.fFormat == TextureFormat::kUnsupported); |
| 264 |
SkASSERT(depthStencilTexture ? depthStencilInfo.isCompatible(depthStencilTexture->textureInfo()) |
| 265 |
: depthStencilInfo.fFormat == TextureFormat::kUnsupported); |
| 266 |
|
| 267 |
// Set up color attachment. |
| 268 |
bool loadMSAAFromResolve = false; |
| 269 |
if (colorTexture) { |
| 270 |
auto colorAttachment = (*descriptor).colorAttachments[0]; |
| 271 |
colorAttachment.texture = ((const MtlTexture*)colorTexture)->mtlTexture(); |
| 272 |
const std::array<float, 4>& clearColor = renderPassDesc.fClearColor; |
| 273 |
colorAttachment.clearColor = |
| 274 |
MTLClearColorMake(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); |
| 275 |
colorAttachment.loadAction = mtlLoadAction[static_cast<int>(colorInfo.fLoadOp)]; |
| 276 |
colorAttachment.storeAction = mtlStoreAction[static_cast<int>(colorInfo.fStoreOp)]; |
| 277 |
|
| 278 |
// Set up resolve attachment |
| 279 |
if (resolveTexture) { |
| 280 |
SkASSERT(resolveInfo.fStoreOp == StoreOp::kStore); |
| 281 |
|
| 282 |
colorAttachment.resolveTexture = ((const MtlTexture*)resolveTexture)->mtlTexture(); |
| 283 |
// Inclusion of a resolve texture implies the client wants to finish the |
| 284 |
// renderpass with a resolve. |
| 285 |
if (@available(macOS 10.12, iOS 10.0, tvOS 10.0, *)) { |
| 286 |
SkASSERT(colorAttachment.storeAction == MTLStoreActionDontCare); |
| 287 |
colorAttachment.storeAction = MTLStoreActionMultisampleResolve; |
| 288 |
} else { |
| 289 |
// We expect at least Metal 2 |
| 290 |
// TODO: Add error output |
| 291 |
SkASSERT(false); |
| 292 |
} |
| 293 |
// But it also means we have to load the resolve texture into the MSAA color attachment |
| 294 |
loadMSAAFromResolve = resolveInfo.fLoadOp == LoadOp::kLoad; |
| 295 |
// TODO: If the color resolve texture is read-only we can use a private (vs. memoryless) |
| 296 |
// msaa attachment that's coupled to the framebuffer and the StoreAndMultisampleResolve |
| 297 |
// action instead of loading as a draw. |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// Set up stencil/depth attachment |
| 302 |
if (depthStencilTexture) { |
| 303 |
id<MTLTexture> mtlTexture = ((const MtlTexture*)depthStencilTexture)->mtlTexture(); |
| 304 |
if (TextureFormatHasDepth(depthStencilInfo.fFormat)) { |
| 305 |
auto depthAttachment = (*descriptor).depthAttachment; |
| 306 |
depthAttachment.texture = mtlTexture; |
| 307 |
depthAttachment.clearDepth = renderPassDesc.fClearDepth; |
| 308 |
depthAttachment.loadAction = |
| 309 |
mtlLoadAction[static_cast<int>(depthStencilInfo.fLoadOp)]; |
| 310 |
depthAttachment.storeAction = |
| 311 |
mtlStoreAction[static_cast<int>(depthStencilInfo.fStoreOp)]; |
| 312 |
} |
| 313 |
if (TextureFormatHasStencil(depthStencilInfo.fFormat)) { |
| 314 |
auto stencilAttachment = (*descriptor).stencilAttachment; |
| 315 |
stencilAttachment.texture = mtlTexture; |
| 316 |
stencilAttachment.clearStencil = renderPassDesc.fClearStencil; |
| 317 |
stencilAttachment.loadAction = |
| 318 |
mtlLoadAction[static_cast<int>(depthStencilInfo.fLoadOp)]; |
| 319 |
stencilAttachment.storeAction = |
| 320 |
mtlStoreAction[static_cast<int>(depthStencilInfo.fStoreOp)]; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
fActiveRenderCommandEncoder = MtlRenderCommandEncoder::Make(fSharedContext, |
| 325 |
fCommandBuffer.get(), |
| 326 |
descriptor.get()); |
| 327 |
this->trackCommandBufferResource(fActiveRenderCommandEncoder); |
| 328 |
|
| 329 |
if (loadMSAAFromResolve) { |
| 330 |
// Manually load the contents of the resolve texture into the MSAA attachment as a draw, |
| 331 |
// so the actual load op for the MSAA attachment had better have been discard. |
| 332 |
SkASSERT(colorInfo.fLoadOp == LoadOp::kDiscard); |
| 333 |
auto loadPipeline = fResourceProvider->findOrCreateLoadMSAAPipeline(renderPassDesc); |
| 334 |
if (!loadPipeline) { |
| 335 |
SKGPU_LOG_E("Unable to create pipeline to load resolve texture into MSAA attachment"); |
| 336 |
return false; |
| 337 |
} |
| 338 |
this->bindGraphicsPipeline(loadPipeline.get()); |
| 339 |
// The load msaa pipeline takes no uniforms, no vertex/instance attributes and only uses |
| 340 |
// one texture that does not require a sampler. |
| 341 |
fActiveRenderCommandEncoder->setFragmentTexture( |
| 342 |
((const MtlTexture*) resolveTexture)->mtlTexture(), 0); |
| 343 |
this->draw(PrimitiveType::kTriangleStrip, 0, 4); |
| 344 |
} |
| 345 |
|
| 346 |
return true; |
| 347 |
} |
| 348 |
|
| 349 |
void MtlCommandBuffer::endRenderPass() { |
| 350 |
SkASSERT(fActiveRenderCommandEncoder); |
| 351 |
fActiveRenderCommandEncoder->endEncoding(); |
| 352 |
fActiveRenderCommandEncoder.reset(); |
| 353 |
fDrawIsOffscreen = false; |
| 354 |
} |
| 355 |
|
| 356 |
bool MtlCommandBuffer::addDrawPass(DrawPass* drawPass) { |
| 357 |
const SkIRect replayedBounds = drawPass->bounds().makeOffset(fReplayTranslation.x(), |
| 358 |
fReplayTranslation.y()); |
| 359 |
if (!SkIRect::Intersects(replayedBounds, fRenderPassBounds)) { |
| 360 |
// The entire DrawPass is offscreen given the replay translation so skip adding any |
| 361 |
// commands. When the DrawPass is partially offscreen individual draw commands will be |
| 362 |
// culled while preserving state changing commands. |
| 363 |
return true; |
| 364 |
} |
| 365 |
|
| 366 |
// If there is gradient data to bind, it must be done prior to draws. |
| 367 |
if (drawPass->floatStorageManager()->hasData()) { |
| 368 |
this->bindUniformBuffer(drawPass->floatStorageManager()->getBufferInfo(), |
| 369 |
UniformSlot::kGradient); |
| 370 |
} |
| 371 |
|
| 372 |
if (!drawPass->addResourceRefs(fResourceProvider, this)) SK_UNLIKELY { |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
for (auto[type, cmdPtr] : drawPass->commands()) { |
| 377 |
// Skip draw commands if they'd be offscreen. |
| 378 |
if (fDrawIsOffscreen) { |
| 379 |
switch (type) { |
| 380 |
case DrawPassCommands::Type::kDraw: |
| 381 |
case DrawPassCommands::Type::kDrawIndexed: |
| 382 |
case DrawPassCommands::Type::kDrawInstanced: |
| 383 |
case DrawPassCommands::Type::kDrawIndexedInstanced: |
| 384 |
continue; |
| 385 |
default: |
| 386 |
break; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
switch (type) { |
| 391 |
case DrawPassCommands::Type::kBindGraphicsPipeline: { |
| 392 |
auto bgp = static_cast<DrawPassCommands::BindGraphicsPipeline*>(cmdPtr); |
| 393 |
this->bindGraphicsPipeline(drawPass->getPipeline(bgp->fPipelineIndex)); |
| 394 |
break; |
| 395 |
} |
| 396 |
case DrawPassCommands::Type::kSetBlendConstants: { |
| 397 |
auto sbc = static_cast<DrawPassCommands::SetBlendConstants*>(cmdPtr); |
| 398 |
this->setBlendConstants(sbc->fBlendConstants); |
| 399 |
break; |
| 400 |
} |
| 401 |
case DrawPassCommands::Type::kBindUniformBuffer: { |
| 402 |
auto bub = static_cast<DrawPassCommands::BindUniformBuffer*>(cmdPtr); |
| 403 |
this->bindUniformBuffer(bub->fInfo, bub->fSlot); |
| 404 |
break; |
| 405 |
} |
| 406 |
case DrawPassCommands::Type::kBindStaticDataBuffer: { |
| 407 |
auto bdb = static_cast<DrawPassCommands::BindStaticDataBuffer*>(cmdPtr); |
| 408 |
this->bindInputBuffer(bdb->fStaticData.fBuffer, bdb->fStaticData.fOffset, |
| 409 |
MtlGraphicsPipeline::kStaticDataBufferIndex); |
| 410 |
break; |
| 411 |
} |
| 412 |
case DrawPassCommands::Type::kBindAppendDataBuffer: { |
| 413 |
auto bdb = static_cast<DrawPassCommands::BindAppendDataBuffer*>(cmdPtr); |
| 414 |
this->bindInputBuffer(bdb->fAppendData.fBuffer, bdb->fAppendData.fOffset, |
| 415 |
MtlGraphicsPipeline::kAppendDataBufferIndex); |
| 416 |
break; |
| 417 |
} |
| 418 |
case DrawPassCommands::Type::kBindIndexBuffer: { |
| 419 |
auto bdb = static_cast<DrawPassCommands::BindIndexBuffer*>(cmdPtr); |
| 420 |
this->bindIndexBuffer( |
| 421 |
bdb->fIndices.fBuffer, bdb->fIndices.fOffset); |
| 422 |
break; |
| 423 |
} |
| 424 |
case DrawPassCommands::Type::kBindIndirectBuffer: { |
| 425 |
auto bdb = static_cast<DrawPassCommands::BindIndirectBuffer*>(cmdPtr); |
| 426 |
this->bindIndirectBuffer( |
| 427 |
bdb->fIndirect.fBuffer, bdb->fIndirect.fOffset); |
| 428 |
break; |
| 429 |
} |
| 430 |
case DrawPassCommands::Type::kBindTexturesAndSamplers: { |
| 431 |
auto bts = static_cast<DrawPassCommands::BindTexturesAndSamplers*>(cmdPtr); |
| 432 |
for (int j = 0; j < bts->fNumTexSamplers; ++j) { |
| 433 |
// immutable samplers don't exist in metal |
| 434 |
SkASSERT(!bts->fSamplers[j].isImmutable()); |
| 435 |
this->bindTextureAndSampler(bts->fTextures[j]->texture(), |
| 436 |
fSharedContext->globalCache()->getDynamicSampler( |
| 437 |
bts->fSamplers[j]), |
| 438 |
j); |
| 439 |
} |
| 440 |
break; |
| 441 |
} |
| 442 |
case DrawPassCommands::Type::kSetScissor: { |
| 443 |
auto ss = static_cast<DrawPassCommands::SetScissor*>(cmdPtr); |
| 444 |
this->setScissor(ss->fScissor); |
| 445 |
break; |
| 446 |
} |
| 447 |
case DrawPassCommands::Type::kDraw: { |
| 448 |
auto draw = static_cast<DrawPassCommands::Draw*>(cmdPtr); |
| 449 |
this->draw(draw->fType, draw->fBaseVertex, draw->fVertexCount); |
| 450 |
break; |
| 451 |
} |
| 452 |
case DrawPassCommands::Type::kDrawIndexed: { |
| 453 |
auto draw = static_cast<DrawPassCommands::DrawIndexed*>(cmdPtr); |
| 454 |
this->drawIndexed(draw->fType, |
| 455 |
draw->fBaseIndex, |
| 456 |
draw->fIndexCount, |
| 457 |
draw->fBaseVertex); |
| 458 |
break; |
| 459 |
} |
| 460 |
case DrawPassCommands::Type::kDrawInstanced: { |
| 461 |
auto draw = static_cast<DrawPassCommands::DrawInstanced*>(cmdPtr); |
| 462 |
this->drawInstanced(draw->fType, |
| 463 |
draw->fBaseVertex, |
| 464 |
draw->fVertexCount, |
| 465 |
draw->fBaseInstance, |
| 466 |
draw->fInstanceCount); |
| 467 |
break; |
| 468 |
} |
| 469 |
case DrawPassCommands::Type::kDrawIndexedInstanced: { |
| 470 |
auto draw = static_cast<DrawPassCommands::DrawIndexedInstanced*>(cmdPtr); |
| 471 |
this->drawIndexedInstanced(draw->fType, |
| 472 |
draw->fBaseIndex, |
| 473 |
draw->fIndexCount, |
| 474 |
draw->fBaseVertex, |
| 475 |
draw->fBaseInstance, |
| 476 |
draw->fInstanceCount); |
| 477 |
break; |
| 478 |
} |
| 479 |
case DrawPassCommands::Type::kDrawIndirect: { |
| 480 |
auto draw = static_cast<DrawPassCommands::DrawIndirect*>(cmdPtr); |
| 481 |
this->drawIndirect(draw->fType); |
| 482 |
break; |
| 483 |
} |
| 484 |
case DrawPassCommands::Type::kDrawIndexedIndirect: { |
| 485 |
auto draw = static_cast<DrawPassCommands::DrawIndexedIndirect*>(cmdPtr); |
| 486 |
this->drawIndexedIndirect(draw->fType); |
| 487 |
break; |
| 488 |
} |
| 489 |
case DrawPassCommands::Type::kAddBarrier: { |
| 490 |
SKGPU_LOG_E("MtlCommandBuffer does not support the addition of barriers."); |
| 491 |
break; |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
return true; |
| 497 |
} |
| 498 |
|
| 499 |
MtlBlitCommandEncoder* MtlCommandBuffer::getBlitCommandEncoder() { |
| 500 |
if (fActiveBlitCommandEncoder) { |
| 501 |
return fActiveBlitCommandEncoder.get(); |
| 502 |
} |
| 503 |
|
| 504 |
fActiveBlitCommandEncoder = MtlBlitCommandEncoder::Make(fSharedContext, fCommandBuffer.get()); |
| 505 |
|
| 506 |
if (!fActiveBlitCommandEncoder) { |
| 507 |
return nullptr; |
| 508 |
} |
| 509 |
|
| 510 |
// We add the ref on the command buffer for the BlitCommandEncoder now so that we don't need |
| 511 |
// to add a ref for every copy we do. |
| 512 |
this->trackCommandBufferResource(fActiveBlitCommandEncoder); |
| 513 |
return fActiveBlitCommandEncoder.get(); |
| 514 |
} |
| 515 |
|
| 516 |
void MtlCommandBuffer::endBlitCommandEncoder() { |
| 517 |
if (fActiveBlitCommandEncoder) { |
| 518 |
fActiveBlitCommandEncoder->endEncoding(); |
| 519 |
fActiveBlitCommandEncoder.reset(); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
void MtlCommandBuffer::bindGraphicsPipeline(const GraphicsPipeline* graphicsPipeline) { |
| 524 |
SkASSERT(fActiveRenderCommandEncoder); |
| 525 |
|
| 526 |
auto mtlPipeline = static_cast<const MtlGraphicsPipeline*>(graphicsPipeline); |
| 527 |
auto pipelineState = mtlPipeline->mtlPipelineState(); |
| 528 |
fActiveRenderCommandEncoder->setRenderPipelineState(pipelineState); |
| 529 |
auto depthStencilState = mtlPipeline->mtlDepthStencilState(); |
| 530 |
fActiveRenderCommandEncoder->setDepthStencilState(depthStencilState); |
| 531 |
uint32_t stencilRefValue = mtlPipeline->stencilReferenceValue(); |
| 532 |
fActiveRenderCommandEncoder->setStencilReferenceValue(stencilRefValue); |
| 533 |
|
| 534 |
if (graphicsPipeline->dstReadStrategy() == DstReadStrategy::kTextureCopy) { |
| 535 |
// The last texture binding is reserved for the dstCopy texture, which is not included in |
| 536 |
// the list on each BindTexturesAndSamplers command. We can set it once now and any |
| 537 |
// subsequent BindTexturesAndSamplers commands in a DrawPass will set the other N-1. |
| 538 |
SkASSERT(fDstCopy.first && fDstCopy.second); |
| 539 |
const int textureIndex = graphicsPipeline->numFragTexturesAndSamplers() - 1; |
| 540 |
this->bindTextureAndSampler(fDstCopy.first, fDstCopy.second, textureIndex); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
void MtlCommandBuffer::bindUniformBuffer(const BindBufferInfo& info, UniformSlot slot) { |
| 545 |
SkASSERT(fActiveRenderCommandEncoder); |
| 546 |
|
| 547 |
id<MTLBuffer> mtlBuffer = info.fBuffer ? |
| 548 |
static_cast<const MtlBuffer*>(info.fBuffer)->mtlBuffer() : nullptr; |
| 549 |
|
| 550 |
unsigned int bufferIndex; |
| 551 |
switch(slot) { |
| 552 |
case UniformSlot::kCombinedUniforms: |
| 553 |
bufferIndex = MtlGraphicsPipeline::kCombinedUniformIndex; |
| 554 |
break; |
| 555 |
case UniformSlot::kGradient: |
| 556 |
bufferIndex = MtlGraphicsPipeline::kGradientBufferIndex; |
| 557 |
break; |
| 558 |
} |
| 559 |
|
| 560 |
fActiveRenderCommandEncoder->setVertexBuffer(mtlBuffer, info.fOffset, bufferIndex); |
| 561 |
fActiveRenderCommandEncoder->setFragmentBuffer(mtlBuffer, info.fOffset, bufferIndex); |
| 562 |
} |
| 563 |
|
| 564 |
void MtlCommandBuffer::bindInputBuffer(const Buffer* buffer, size_t offset, uint32_t bindingIndex) { |
| 565 |
SkASSERT(fActiveRenderCommandEncoder); |
| 566 |
if (buffer) { |
| 567 |
id<MTLBuffer> mtlBuffer = static_cast<const MtlBuffer*>(buffer)->mtlBuffer(); |
| 568 |
SkASSERT((offset & 0b11) == 0); |
| 569 |
fActiveRenderCommandEncoder->setVertexBuffer(mtlBuffer, offset, bindingIndex); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
void MtlCommandBuffer::bindIndexBuffer(const Buffer* indexBuffer, size_t offset) { |
| 574 |
if (indexBuffer) { |
| 575 |
fCurrentIndexBuffer = static_cast<const MtlBuffer*>(indexBuffer)->mtlBuffer(); |
| 576 |
fCurrentIndexBufferOffset = offset; |
| 577 |
} else { |
| 578 |
fCurrentIndexBuffer = nil; |
| 579 |
fCurrentIndexBufferOffset = 0; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
void MtlCommandBuffer::bindIndirectBuffer(const Buffer* indirectBuffer, size_t offset) { |
| 584 |
if (indirectBuffer) { |
| 585 |
fCurrentIndirectBuffer = static_cast<const MtlBuffer*>(indirectBuffer)->mtlBuffer(); |
| 586 |
fCurrentIndirectBufferOffset = offset; |
| 587 |
} else { |
| 588 |
fCurrentIndirectBuffer = nil; |
| 589 |
fCurrentIndirectBufferOffset = 0; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
void MtlCommandBuffer::bindTextureAndSampler(const Texture* texture, |
| 594 |
const Sampler* sampler, |
| 595 |
unsigned int bindIndex) { |
| 596 |
SkASSERT(texture && sampler); |
| 597 |
SkASSERT(fActiveRenderCommandEncoder); |
| 598 |
|
| 599 |
id<MTLTexture> mtlTexture = ((const MtlTexture*)texture)->mtlTexture(); |
| 600 |
id<MTLSamplerState> mtlSamplerState = ((const MtlSampler*)sampler)->mtlSamplerState(); |
| 601 |
fActiveRenderCommandEncoder->setFragmentTexture(mtlTexture, bindIndex); |
| 602 |
fActiveRenderCommandEncoder->setFragmentSamplerState(mtlSamplerState, bindIndex); |
| 603 |
} |
| 604 |
|
| 605 |
void MtlCommandBuffer::setScissor(const Scissor& scissor) { |
| 606 |
SkASSERT(fActiveRenderCommandEncoder); |
| 607 |
|
| 608 |
SkIRect rect = scissor.getRect(fReplayTranslation, fRenderPassBounds); |
| 609 |
fDrawIsOffscreen = rect.isEmpty(); |
| 610 |
|
| 611 |
fActiveRenderCommandEncoder->setScissorRect({ |
| 612 |
static_cast<unsigned int>(rect.x()), |
| 613 |
static_cast<unsigned int>(rect.y()), |
| 614 |
static_cast<unsigned int>(rect.width()), |
| 615 |
static_cast<unsigned int>(rect.height()), |
| 616 |
}); |
| 617 |
} |
| 618 |
|
| 619 |
void MtlCommandBuffer::setViewport(float x, float y, float width, float height, |
| 620 |
float minDepth, float maxDepth) { |
| 621 |
SkASSERT(fActiveRenderCommandEncoder); |
| 622 |
MTLViewport viewport = {x, |
| 623 |
y, |
| 624 |
width, |
| 625 |
height, |
| 626 |
minDepth, |
| 627 |
maxDepth}; |
| 628 |
fActiveRenderCommandEncoder->setViewport(viewport); |
| 629 |
} |
| 630 |
|
| 631 |
void MtlCommandBuffer::updateIntrinsicUniforms(SkIRect viewport) { |
| 632 |
UniformManager intrinsicValues{Layout::kMetal}; |
| 633 |
CollectIntrinsicUniforms(fSharedContext->caps(), viewport, fDstReadBounds, &intrinsicValues); |
| 634 |
SkSpan<const char> bytes = intrinsicValues.finish(); |
| 635 |
fActiveRenderCommandEncoder->setVertexBytes( |
| 636 |
bytes.data(), bytes.size_bytes(), MtlGraphicsPipeline::kIntrinsicUniformBufferIndex); |
| 637 |
fActiveRenderCommandEncoder->setFragmentBytes( |
| 638 |
bytes.data(), bytes.size_bytes(), MtlGraphicsPipeline::kIntrinsicUniformBufferIndex); |
| 639 |
} |
| 640 |
|
| 641 |
void MtlCommandBuffer::setBlendConstants(std::array<float, 4> blendConstants) { |
| 642 |
SkASSERT(fActiveRenderCommandEncoder); |
| 643 |
|
| 644 |
fActiveRenderCommandEncoder->setBlendColor(blendConstants); |
| 645 |
} |
| 646 |
|
| 647 |
static MTLPrimitiveType graphite_to_mtl_primitive(PrimitiveType primitiveType) { |
| 648 |
const static MTLPrimitiveType mtlPrimitiveType[] { |
| 649 |
MTLPrimitiveTypeTriangle, |
| 650 |
MTLPrimitiveTypeTriangleStrip, |
| 651 |
MTLPrimitiveTypePoint, |
| 652 |
}; |
| 653 |
static_assert((int)PrimitiveType::kTriangles == 0); |
| 654 |
static_assert((int)PrimitiveType::kTriangleStrip == 1); |
| 655 |
static_assert((int)PrimitiveType::kPoints == 2); |
| 656 |
|
| 657 |
SkASSERT(primitiveType <= PrimitiveType::kPoints); |
| 658 |
return mtlPrimitiveType[static_cast<int>(primitiveType)]; |
| 659 |
} |
| 660 |
|
| 661 |
void MtlCommandBuffer::draw(PrimitiveType type, |
| 662 |
unsigned int baseVertex, |
| 663 |
unsigned int vertexCount) { |
| 664 |
SkASSERT(fActiveRenderCommandEncoder); |
| 665 |
|
| 666 |
auto mtlPrimitiveType = graphite_to_mtl_primitive(type); |
| 667 |
|
| 668 |
fActiveRenderCommandEncoder->drawPrimitives(mtlPrimitiveType, baseVertex, vertexCount); |
| 669 |
} |
| 670 |
|
| 671 |
void MtlCommandBuffer::drawIndexed(PrimitiveType type, unsigned int baseIndex, |
| 672 |
unsigned int indexCount, unsigned int baseVertex) { |
| 673 |
SkASSERT(fActiveRenderCommandEncoder); |
| 674 |
|
| 675 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 676 |
auto mtlPrimitiveType = graphite_to_mtl_primitive(type); |
| 677 |
size_t indexOffset = fCurrentIndexBufferOffset + sizeof(uint16_t )* baseIndex; |
| 678 |
// Use the "instance" variant witha count of 1 so that we can pass in a base vertex |
| 679 |
// instead of rebinding a vertex buffer offset. |
| 680 |
fActiveRenderCommandEncoder->drawIndexedPrimitives(mtlPrimitiveType, indexCount, |
| 681 |
MTLIndexTypeUInt16, fCurrentIndexBuffer, |
| 682 |
indexOffset, 1, baseVertex, 0); |
| 683 |
|
| 684 |
} else { |
| 685 |
SKGPU_LOG_E("Skipping unsupported draw call."); |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
void MtlCommandBuffer::drawInstanced(PrimitiveType type, unsigned int baseVertex, |
| 690 |
unsigned int vertexCount, unsigned int baseInstance, |
| 691 |
unsigned int instanceCount) { |
| 692 |
SkASSERT(fActiveRenderCommandEncoder); |
| 693 |
|
| 694 |
auto mtlPrimitiveType = graphite_to_mtl_primitive(type); |
| 695 |
|
| 696 |
// This ordering is correct |
| 697 |
fActiveRenderCommandEncoder->drawPrimitives(mtlPrimitiveType, baseVertex, vertexCount, |
| 698 |
instanceCount, baseInstance); |
| 699 |
} |
| 700 |
|
| 701 |
void MtlCommandBuffer::drawIndexedInstanced(PrimitiveType type, |
| 702 |
unsigned int baseIndex, |
| 703 |
unsigned int indexCount, |
| 704 |
unsigned int baseVertex, |
| 705 |
unsigned int baseInstance, |
| 706 |
unsigned int instanceCount) { |
| 707 |
SkASSERT(fActiveRenderCommandEncoder); |
| 708 |
|
| 709 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 710 |
auto mtlPrimitiveType = graphite_to_mtl_primitive(type); |
| 711 |
size_t indexOffset = fCurrentIndexBufferOffset + sizeof(uint16_t) * baseIndex; |
| 712 |
fActiveRenderCommandEncoder->drawIndexedPrimitives(mtlPrimitiveType, indexCount, |
| 713 |
MTLIndexTypeUInt16, fCurrentIndexBuffer, |
| 714 |
indexOffset, instanceCount, |
| 715 |
baseVertex, baseInstance); |
| 716 |
} else { |
| 717 |
SKGPU_LOG_E("Skipping unsupported draw call."); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
void MtlCommandBuffer::drawIndirect(PrimitiveType type) { |
| 722 |
SkASSERT(fActiveRenderCommandEncoder); |
| 723 |
SkASSERT(fCurrentIndirectBuffer); |
| 724 |
|
| 725 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 726 |
auto mtlPrimitiveType = graphite_to_mtl_primitive(type); |
| 727 |
fActiveRenderCommandEncoder->drawPrimitives( |
| 728 |
mtlPrimitiveType, fCurrentIndirectBuffer, fCurrentIndirectBufferOffset); |
| 729 |
} else { |
| 730 |
SKGPU_LOG_E("Skipping unsupported draw call."); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
void MtlCommandBuffer::drawIndexedIndirect(PrimitiveType type) { |
| 735 |
SkASSERT(fActiveRenderCommandEncoder); |
| 736 |
SkASSERT(fCurrentIndirectBuffer); |
| 737 |
|
| 738 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 739 |
auto mtlPrimitiveType = graphite_to_mtl_primitive(type); |
| 740 |
fActiveRenderCommandEncoder->drawIndexedPrimitives(mtlPrimitiveType, |
| 741 |
MTLIndexTypeUInt32, |
| 742 |
fCurrentIndexBuffer, |
| 743 |
fCurrentIndexBufferOffset, |
| 744 |
fCurrentIndirectBuffer, |
| 745 |
fCurrentIndirectBufferOffset); |
| 746 |
} else { |
| 747 |
SKGPU_LOG_E("Skipping unsupported draw call."); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
void MtlCommandBuffer::beginComputePass() { |
| 752 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 753 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 754 |
this->endBlitCommandEncoder(); |
| 755 |
fActiveComputeCommandEncoder = MtlComputeCommandEncoder::Make(fSharedContext, |
| 756 |
fCommandBuffer.get()); |
| 757 |
} |
| 758 |
|
| 759 |
void MtlCommandBuffer::bindComputePipeline(const ComputePipeline* computePipeline) { |
| 760 |
SkASSERT(fActiveComputeCommandEncoder); |
| 761 |
|
| 762 |
auto mtlPipeline = static_cast<const MtlComputePipeline*>(computePipeline); |
| 763 |
fActiveComputeCommandEncoder->setComputePipelineState(mtlPipeline->mtlPipelineState()); |
| 764 |
} |
| 765 |
|
| 766 |
void MtlCommandBuffer::bindBuffer(const Buffer* buffer, unsigned int offset, unsigned int index) { |
| 767 |
SkASSERT(fActiveComputeCommandEncoder); |
| 768 |
|
| 769 |
id<MTLBuffer> mtlBuffer = buffer ? static_cast<const MtlBuffer*>(buffer)->mtlBuffer() : nil; |
| 770 |
fActiveComputeCommandEncoder->setBuffer(mtlBuffer, offset, index); |
| 771 |
} |
| 772 |
|
| 773 |
void MtlCommandBuffer::bindTexture(const Texture* texture, unsigned int index) { |
| 774 |
SkASSERT(fActiveComputeCommandEncoder); |
| 775 |
|
| 776 |
id<MTLTexture> mtlTexture = |
| 777 |
texture ? static_cast<const MtlTexture*>(texture)->mtlTexture() : nil; |
| 778 |
fActiveComputeCommandEncoder->setTexture(mtlTexture, index); |
| 779 |
} |
| 780 |
|
| 781 |
void MtlCommandBuffer::bindSampler(const Sampler* sampler, unsigned int index) { |
| 782 |
SkASSERT(fActiveComputeCommandEncoder); |
| 783 |
|
| 784 |
id<MTLSamplerState> mtlSamplerState = |
| 785 |
sampler ? static_cast<const MtlSampler*>(sampler)->mtlSamplerState() : nil; |
| 786 |
fActiveComputeCommandEncoder->setSamplerState(mtlSamplerState, index); |
| 787 |
} |
| 788 |
|
| 789 |
void MtlCommandBuffer::dispatchThreadgroups(const WorkgroupSize& globalSize, |
| 790 |
const WorkgroupSize& localSize) { |
| 791 |
SkASSERT(fActiveComputeCommandEncoder); |
| 792 |
fActiveComputeCommandEncoder->dispatchThreadgroups(globalSize, localSize); |
| 793 |
} |
| 794 |
|
| 795 |
void MtlCommandBuffer::dispatchThreadgroupsIndirect(const WorkgroupSize& localSize, |
| 796 |
const Buffer* indirectBuffer, |
| 797 |
size_t indirectBufferOffset) { |
| 798 |
SkASSERT(fActiveComputeCommandEncoder); |
| 799 |
|
| 800 |
id<MTLBuffer> mtlIndirectBuffer = static_cast<const MtlBuffer*>(indirectBuffer)->mtlBuffer(); |
| 801 |
fActiveComputeCommandEncoder->dispatchThreadgroupsWithIndirectBuffer( |
| 802 |
mtlIndirectBuffer, indirectBufferOffset, localSize); |
| 803 |
} |
| 804 |
|
| 805 |
void MtlCommandBuffer::endComputePass() { |
| 806 |
SkASSERT(fActiveComputeCommandEncoder); |
| 807 |
fActiveComputeCommandEncoder->endEncoding(); |
| 808 |
fActiveComputeCommandEncoder.reset(); |
| 809 |
} |
| 810 |
|
| 811 |
static bool check_max_blit_width(int widthInPixels) { |
| 812 |
if (widthInPixels > 32767) { |
| 813 |
SkASSERT(false); // surfaces should not be this wide anyway |
| 814 |
return false; |
| 815 |
} |
| 816 |
return true; |
| 817 |
} |
| 818 |
|
| 819 |
bool MtlCommandBuffer::onCopyBufferToBuffer(const Buffer* srcBuffer, |
| 820 |
size_t srcOffset, |
| 821 |
const Buffer* dstBuffer, |
| 822 |
size_t dstOffset, |
| 823 |
size_t size) { |
| 824 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 825 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 826 |
|
| 827 |
id<MTLBuffer> mtlSrcBuffer = static_cast<const MtlBuffer*>(srcBuffer)->mtlBuffer(); |
| 828 |
id<MTLBuffer> mtlDstBuffer = static_cast<const MtlBuffer*>(dstBuffer)->mtlBuffer(); |
| 829 |
|
| 830 |
MtlBlitCommandEncoder* blitCmdEncoder = this->getBlitCommandEncoder(); |
| 831 |
if (!blitCmdEncoder) { |
| 832 |
return false; |
| 833 |
} |
| 834 |
|
| 835 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 836 |
blitCmdEncoder->pushDebugGroup(@"copyBufferToBuffer"); |
| 837 |
#endif |
| 838 |
blitCmdEncoder->copyBufferToBuffer(mtlSrcBuffer, srcOffset, mtlDstBuffer, dstOffset, size); |
| 839 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 840 |
blitCmdEncoder->popDebugGroup(); |
| 841 |
#endif |
| 842 |
return true; |
| 843 |
} |
| 844 |
|
| 845 |
bool MtlCommandBuffer::onCopyTextureToBuffer(const Texture* texture, |
| 846 |
SkIRect srcRect, |
| 847 |
const Buffer* buffer, |
| 848 |
size_t bufferOffset, |
| 849 |
size_t bufferRowBytes) { |
| 850 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 851 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 852 |
|
| 853 |
if (!check_max_blit_width(srcRect.width())) { |
| 854 |
return false; |
| 855 |
} |
| 856 |
|
| 857 |
id<MTLTexture> mtlTexture = static_cast<const MtlTexture*>(texture)->mtlTexture(); |
| 858 |
id<MTLBuffer> mtlBuffer = static_cast<const MtlBuffer*>(buffer)->mtlBuffer(); |
| 859 |
|
| 860 |
MtlBlitCommandEncoder* blitCmdEncoder = this->getBlitCommandEncoder(); |
| 861 |
if (!blitCmdEncoder) { |
| 862 |
return false; |
| 863 |
} |
| 864 |
|
| 865 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 866 |
blitCmdEncoder->pushDebugGroup(@"copyTextureToBuffer"); |
| 867 |
#endif |
| 868 |
blitCmdEncoder->copyFromTexture(mtlTexture, srcRect, mtlBuffer, bufferOffset, bufferRowBytes); |
| 869 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 870 |
blitCmdEncoder->popDebugGroup(); |
| 871 |
#endif |
| 872 |
return true; |
| 873 |
} |
| 874 |
|
| 875 |
bool MtlCommandBuffer::onCopyBufferToTexture(const Buffer* buffer, |
| 876 |
const Texture* texture, |
| 877 |
const BufferTextureCopyData* copyData, |
| 878 |
int count) { |
| 879 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 880 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 881 |
|
| 882 |
id<MTLBuffer> mtlBuffer = static_cast<const MtlBuffer*>(buffer)->mtlBuffer(); |
| 883 |
id<MTLTexture> mtlTexture = static_cast<const MtlTexture*>(texture)->mtlTexture(); |
| 884 |
|
| 885 |
MtlBlitCommandEncoder* blitCmdEncoder = this->getBlitCommandEncoder(); |
| 886 |
if (!blitCmdEncoder) { |
| 887 |
return false; |
| 888 |
} |
| 889 |
|
| 890 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 891 |
blitCmdEncoder->pushDebugGroup(@"copyBufferToTexture"); |
| 892 |
#endif |
| 893 |
for (int i = 0; i < count; ++i) { |
| 894 |
if (!check_max_blit_width(copyData[i].fRect.width())) { |
| 895 |
return false; |
| 896 |
} |
| 897 |
|
| 898 |
blitCmdEncoder->copyFromBuffer(mtlBuffer, |
| 899 |
copyData[i].fBufferOffset, |
| 900 |
copyData[i].fBufferRowBytes, |
| 901 |
mtlTexture, |
| 902 |
copyData[i].fRect, |
| 903 |
copyData[i].fMipLevel); |
| 904 |
} |
| 905 |
|
| 906 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 907 |
blitCmdEncoder->popDebugGroup(); |
| 908 |
#endif |
| 909 |
return true; |
| 910 |
} |
| 911 |
|
| 912 |
bool MtlCommandBuffer::onCopyTextureToTexture(const Texture* src, |
| 913 |
SkIRect srcRect, |
| 914 |
const Texture* dst, |
| 915 |
SkIPoint dstPoint, |
| 916 |
int mipLevel) { |
| 917 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 918 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 919 |
|
| 920 |
id<MTLTexture> srcMtlTexture = static_cast<const MtlTexture*>(src)->mtlTexture(); |
| 921 |
id<MTLTexture> dstMtlTexture = static_cast<const MtlTexture*>(dst)->mtlTexture(); |
| 922 |
|
| 923 |
MtlBlitCommandEncoder* blitCmdEncoder = this->getBlitCommandEncoder(); |
| 924 |
if (!blitCmdEncoder) { |
| 925 |
return false; |
| 926 |
} |
| 927 |
|
| 928 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 929 |
blitCmdEncoder->pushDebugGroup(@"copyTextureToTexture"); |
| 930 |
#endif |
| 931 |
|
| 932 |
blitCmdEncoder->copyTextureToTexture(srcMtlTexture, srcRect, dstMtlTexture, dstPoint, mipLevel); |
| 933 |
|
| 934 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 935 |
blitCmdEncoder->popDebugGroup(); |
| 936 |
#endif |
| 937 |
return true; |
| 938 |
} |
| 939 |
|
| 940 |
bool MtlCommandBuffer::onSynchronizeBufferToCpu(const Buffer* buffer, bool* outDidResultInWork) { |
| 941 |
#ifdef SK_BUILD_FOR_MAC |
| 942 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 943 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 944 |
|
| 945 |
id<MTLBuffer> mtlBuffer = static_cast<const MtlBuffer*>(buffer)->mtlBuffer(); |
| 946 |
if ([mtlBuffer storageMode] != MTLStorageModeManaged) { |
| 947 |
*outDidResultInWork = false; |
| 948 |
return true; |
| 949 |
} |
| 950 |
|
| 951 |
MtlBlitCommandEncoder* blitCmdEncoder = this->getBlitCommandEncoder(); |
| 952 |
if (!blitCmdEncoder) { |
| 953 |
return false; |
| 954 |
} |
| 955 |
|
| 956 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 957 |
blitCmdEncoder->pushDebugGroup(@"synchronizeToCpu"); |
| 958 |
#endif |
| 959 |
blitCmdEncoder->synchronizeResource(mtlBuffer); |
| 960 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 961 |
blitCmdEncoder->popDebugGroup(); |
| 962 |
#endif |
| 963 |
|
| 964 |
*outDidResultInWork = true; |
| 965 |
return true; |
| 966 |
#else // SK_BUILD_FOR_MAC |
| 967 |
// Explicit synchronization is never necessary on builds that are not macOS since we never use |
| 968 |
// discrete GPUs with managed mode buffers outside of macOS. |
| 969 |
*outDidResultInWork = false; |
| 970 |
return true; |
| 971 |
#endif // SK_BUILD_FOR_MAC |
| 972 |
} |
| 973 |
|
| 974 |
bool MtlCommandBuffer::onClearBuffer(const Buffer* buffer, size_t offset, size_t size) { |
| 975 |
SkASSERT(!fActiveRenderCommandEncoder); |
| 976 |
SkASSERT(!fActiveComputeCommandEncoder); |
| 977 |
|
| 978 |
MtlBlitCommandEncoder* blitCmdEncoder = this->getBlitCommandEncoder(); |
| 979 |
if (!blitCmdEncoder) { |
| 980 |
return false; |
| 981 |
} |
| 982 |
|
| 983 |
id<MTLBuffer> mtlBuffer = static_cast<const MtlBuffer*>(buffer)->mtlBuffer(); |
| 984 |
blitCmdEncoder->fillBuffer(mtlBuffer, offset, size, 0); |
| 985 |
|
| 986 |
return true; |
| 987 |
} |
| 988 |
|
| 989 |
} // namespace skgpu::graphite |