| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 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/ganesh/mtl/GrMtlOpsRenderPass.h" |
| 9 |
|
| 10 |
#include "src/gpu/ganesh/GrBackendUtils.h" |
| 11 |
#include "src/gpu/ganesh/GrColor.h" |
| 12 |
#include "src/gpu/ganesh/GrNativeRect.h" |
| 13 |
#include "src/gpu/ganesh/GrRenderTarget.h" |
| 14 |
#include "src/gpu/ganesh/mtl/GrMtlCommandBuffer.h" |
| 15 |
#include "src/gpu/ganesh/mtl/GrMtlPipelineState.h" |
| 16 |
#include "src/gpu/ganesh/mtl/GrMtlPipelineStateBuilder.h" |
| 17 |
#include "src/gpu/ganesh/mtl/GrMtlRenderCommandEncoder.h" |
| 18 |
#include "src/gpu/ganesh/mtl/GrMtlRenderTarget.h" |
| 19 |
#include "src/gpu/ganesh/mtl/GrMtlTexture.h" |
| 20 |
|
| 21 |
#if !__has_feature(objc_arc) |
| 22 |
#error This file must be compiled with Arc. Use -fobjc-arc flag |
| 23 |
#endif |
| 24 |
|
| 25 |
GR_NORETAIN_BEGIN |
| 26 |
|
| 27 |
GrMtlOpsRenderPass::GrMtlOpsRenderPass(GrMtlGpu* gpu, GrRenderTarget* rt, |
| 28 |
sk_sp<GrMtlFramebuffer> framebuffer, GrSurfaceOrigin origin, |
| 29 |
const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
| 30 |
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo) |
| 31 |
: INHERITED(rt, origin) |
| 32 |
, fGpu(gpu) |
| 33 |
, fFramebuffer(std::move(framebuffer)) { |
| 34 |
this->setupRenderPass(colorInfo, stencilInfo); |
| 35 |
} |
| 36 |
|
| 37 |
GrMtlOpsRenderPass::~GrMtlOpsRenderPass() { |
| 38 |
} |
| 39 |
|
| 40 |
void GrMtlOpsRenderPass::submit() { |
| 41 |
if (!fFramebuffer) { |
| 42 |
return; |
| 43 |
} |
| 44 |
SkIRect iBounds; |
| 45 |
fBounds.roundOut(&iBounds); |
| 46 |
fGpu->submitIndirectCommandBuffer(fRenderTarget, fOrigin, &iBounds); |
| 47 |
fActiveRenderCmdEncoder = nullptr; |
| 48 |
} |
| 49 |
|
| 50 |
static MTLPrimitiveType gr_to_mtl_primitive(GrPrimitiveType primitiveType) { |
| 51 |
const static MTLPrimitiveType mtlPrimitiveType[] { |
| 52 |
MTLPrimitiveTypeTriangle, |
| 53 |
MTLPrimitiveTypeTriangleStrip, |
| 54 |
MTLPrimitiveTypePoint, |
| 55 |
MTLPrimitiveTypeLine, |
| 56 |
MTLPrimitiveTypeLineStrip |
| 57 |
}; |
| 58 |
static_assert((int)GrPrimitiveType::kTriangles == 0); |
| 59 |
static_assert((int)GrPrimitiveType::kTriangleStrip == 1); |
| 60 |
static_assert((int)GrPrimitiveType::kPoints == 2); |
| 61 |
static_assert((int)GrPrimitiveType::kLines == 3); |
| 62 |
static_assert((int)GrPrimitiveType::kLineStrip == 4); |
| 63 |
|
| 64 |
SkASSERT(primitiveType <= GrPrimitiveType::kLineStrip); |
| 65 |
return mtlPrimitiveType[static_cast<int>(primitiveType)]; |
| 66 |
} |
| 67 |
|
| 68 |
bool GrMtlOpsRenderPass::onBindPipeline(const GrProgramInfo& programInfo, |
| 69 |
const SkRect& drawBounds) { |
| 70 |
const GrMtlCaps& caps = fGpu->mtlCaps(); |
| 71 |
GrProgramDesc programDesc = caps.makeDesc(fRenderTarget, programInfo, |
| 72 |
GrCaps::ProgramDescOverrideFlags::kNone); |
| 73 |
if (!programDesc.isValid()) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
fActivePipelineState = fGpu->resourceProvider().findOrCreateCompatiblePipelineState( |
| 78 |
programDesc, programInfo); |
| 79 |
if (!fActivePipelineState) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
fActivePipelineState->setData(fFramebuffer.get(), programInfo); |
| 84 |
fCurrentVertexStride = programInfo.geomProc().vertexStride(); |
| 85 |
|
| 86 |
if (!fActiveRenderCmdEncoder) { |
| 87 |
this->setupRenderCommandEncoder(fActivePipelineState); |
| 88 |
if (!fActiveRenderCmdEncoder) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
fGpu->commandBuffer()->addGrSurface( |
| 92 |
sk_ref_sp<GrMtlAttachment>(fFramebuffer->colorAttachment())); |
| 93 |
} |
| 94 |
|
| 95 |
fActiveRenderCmdEncoder->setRenderPipelineState( |
| 96 |
fActivePipelineState->pipeline()->mtlPipelineState()); |
| 97 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 98 |
if (!fDebugGroupActive) { |
| 99 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 100 |
fDebugGroupActive = true; |
| 101 |
} |
| 102 |
#endif |
| 103 |
fActivePipelineState->setDrawState(fActiveRenderCmdEncoder, |
| 104 |
programInfo.pipeline().writeSwizzle(), |
| 105 |
programInfo.pipeline().getXferProcessor()); |
| 106 |
if (this->gpu()->caps()->wireframeMode() || programInfo.pipeline().isWireframe()) { |
| 107 |
fActiveRenderCmdEncoder->setTriangleFillMode(MTLTriangleFillModeLines); |
| 108 |
} else { |
| 109 |
fActiveRenderCmdEncoder->setTriangleFillMode(MTLTriangleFillModeFill); |
| 110 |
} |
| 111 |
|
| 112 |
if (!programInfo.pipeline().isScissorTestEnabled()) { |
| 113 |
// "Disable" scissor by setting it to the full pipeline bounds. |
| 114 |
SkISize dimensions = fFramebuffer->colorAttachment()->dimensions(); |
| 115 |
GrMtlPipelineState::SetDynamicScissorRectState(fActiveRenderCmdEncoder, |
| 116 |
dimensions, fOrigin, |
| 117 |
SkIRect::MakeWH(dimensions.width(), |
| 118 |
dimensions.height())); |
| 119 |
} |
| 120 |
|
| 121 |
fActivePrimitiveType = gr_to_mtl_primitive(programInfo.primitiveType()); |
| 122 |
fBounds.join(drawBounds); |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
void GrMtlOpsRenderPass::onSetScissorRect(const SkIRect& scissor) { |
| 127 |
SkASSERT(fActivePipelineState); |
| 128 |
SkASSERT(fActiveRenderCmdEncoder); |
| 129 |
GrMtlPipelineState::SetDynamicScissorRectState(fActiveRenderCmdEncoder, |
| 130 |
fFramebuffer->colorAttachment()->dimensions(), |
| 131 |
fOrigin, scissor); |
| 132 |
} |
| 133 |
|
| 134 |
bool GrMtlOpsRenderPass::onBindTextures(const GrGeometryProcessor& geomProc, |
| 135 |
const GrSurfaceProxy* const geomProcTextures[], |
| 136 |
const GrPipeline& pipeline) { |
| 137 |
SkASSERT(fActivePipelineState); |
| 138 |
SkASSERT(fActiveRenderCmdEncoder); |
| 139 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 140 |
if (!fDebugGroupActive) { |
| 141 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 142 |
fDebugGroupActive = true; |
| 143 |
} |
| 144 |
#endif |
| 145 |
fActivePipelineState->setTextures(geomProc, pipeline, geomProcTextures); |
| 146 |
fActivePipelineState->bindTextures(fActiveRenderCmdEncoder); |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
void GrMtlOpsRenderPass::onClear(const GrScissorState& scissor, std::array<float, 4> color) { |
| 151 |
// Partial clears are not supported |
| 152 |
SkASSERT(!scissor.enabled()); |
| 153 |
|
| 154 |
// Ideally we should never end up here since all clears should either be done as draws or |
| 155 |
// load ops in metal. However, if a client inserts a wait op we need to handle it. |
| 156 |
auto colorAttachment = fRenderPassDesc.colorAttachments[0]; |
| 157 |
colorAttachment.clearColor = MTLClearColorMake(color[0], color[1], color[2], color[3]); |
| 158 |
colorAttachment.loadAction = MTLLoadActionClear; |
| 159 |
if (!this->setupResolve()) { |
| 160 |
this->setupRenderCommandEncoder(nullptr); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
void GrMtlOpsRenderPass::onClearStencilClip(const GrScissorState& scissor, bool insideStencilMask) { |
| 165 |
// Partial clears are not supported |
| 166 |
SkASSERT(!scissor.enabled()); |
| 167 |
|
| 168 |
GrAttachment* sb = fFramebuffer->stencilAttachment(); |
| 169 |
// this should only be called internally when we know we have a |
| 170 |
// stencil buffer. |
| 171 |
SkASSERT(sb); |
| 172 |
int stencilBitCount = GrBackendFormatStencilBits(sb->backendFormat()); |
| 173 |
|
| 174 |
// The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 175 |
// during this clear. Thus we will clear the entire stencil to the desired value. |
| 176 |
auto stencilAttachment = fRenderPassDesc.stencilAttachment; |
| 177 |
if (insideStencilMask) { |
| 178 |
stencilAttachment.clearStencil = (1 << (stencilBitCount - 1)); |
| 179 |
} else { |
| 180 |
stencilAttachment.clearStencil = 0; |
| 181 |
} |
| 182 |
|
| 183 |
stencilAttachment.loadAction = MTLLoadActionClear; |
| 184 |
if (!this->setupResolve()) { |
| 185 |
this->setupRenderCommandEncoder(nullptr); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| •189 |
bool GrMtlOpsRenderPass::inlineUpload(GrOpFlushState* state, GrDeferredTextureUploadFn& upload) { |
| 190 |
state->doUpload(upload); |
| 191 |
|
| 192 |
// If the previous renderCommandEncoder did a resolve without an MSAA store |
| 193 |
// (e.g., if the color attachment is memoryless) we need to copy the contents of |
| 194 |
// the resolve attachment to the MSAA attachment at this point. |
| 195 |
if (!this->setupResolve()) { |
| 196 |
// If setting up for the resolve didn't create an encoder, it's probably reasonable to |
| 197 |
// create a new encoder at this point, though maybe not necessary. |
| 198 |
this->setupRenderCommandEncoder(nullptr); |
| 199 |
} |
| •200 |
|
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
void GrMtlOpsRenderPass::initRenderState(GrMtlRenderCommandEncoder* encoder) { |
| 205 |
if (!encoder) { |
| 206 |
return; |
| 207 |
} |
| 208 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 209 |
encoder->pushDebugGroup(@"initRenderState"); |
| 210 |
#endif |
| 211 |
encoder->setFrontFacingWinding(MTLWindingCounterClockwise); |
| 212 |
SkISize colorAttachmentDimensions = fFramebuffer->colorAttachment()->dimensions(); |
| 213 |
// Strictly speaking we shouldn't have to set this, as the default viewport is the size of |
| 214 |
// the drawable used to generate the renderCommandEncoder -- but just in case. |
| 215 |
MTLViewport viewport = { 0.0, 0.0, |
| 216 |
(double) colorAttachmentDimensions.width(), |
| 217 |
(double) colorAttachmentDimensions.height(), |
| 218 |
0.0, 1.0 }; |
| 219 |
encoder->setViewport(viewport); |
| 220 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 221 |
encoder->popDebugGroup(); |
| 222 |
#endif |
| 223 |
} |
| 224 |
|
| 225 |
void GrMtlOpsRenderPass::setupRenderPass( |
| 226 |
const GrOpsRenderPass::LoadAndStoreInfo& colorInfo, |
| 227 |
const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo) { |
| 228 |
const static MTLLoadAction mtlLoadAction[] { |
| 229 |
MTLLoadActionLoad, |
| 230 |
MTLLoadActionClear, |
| 231 |
MTLLoadActionDontCare |
| 232 |
}; |
| 233 |
static_assert((int)GrLoadOp::kLoad == 0); |
| 234 |
static_assert((int)GrLoadOp::kClear == 1); |
| 235 |
static_assert((int)GrLoadOp::kDiscard == 2); |
| 236 |
SkASSERT(colorInfo.fLoadOp <= GrLoadOp::kDiscard); |
| 237 |
SkASSERT(stencilInfo.fLoadOp <= GrLoadOp::kDiscard); |
| 238 |
|
| 239 |
const static MTLStoreAction mtlStoreAction[] { |
| 240 |
MTLStoreActionStore, |
| 241 |
MTLStoreActionDontCare |
| 242 |
}; |
| 243 |
static_assert((int)GrStoreOp::kStore == 0); |
| 244 |
static_assert((int)GrStoreOp::kDiscard == 1); |
| 245 |
SkASSERT(colorInfo.fStoreOp <= GrStoreOp::kDiscard); |
| 246 |
SkASSERT(stencilInfo.fStoreOp <= GrStoreOp::kDiscard); |
| 247 |
|
| 248 |
fRenderPassDesc = [MTLRenderPassDescriptor new]; |
| 249 |
auto colorAttachment = fRenderPassDesc.colorAttachments[0]; |
| 250 |
auto color = fFramebuffer->colorAttachment(); |
| 251 |
colorAttachment.texture = color->mtlTexture(); |
| 252 |
const std::array<float, 4>& clearColor = colorInfo.fClearColor; |
| 253 |
colorAttachment.clearColor = |
| 254 |
MTLClearColorMake(clearColor[0], clearColor[1], clearColor[2], clearColor[3]); |
| 255 |
colorAttachment.loadAction = mtlLoadAction[static_cast<int>(colorInfo.fLoadOp)]; |
| 256 |
colorAttachment.storeAction = mtlStoreAction[static_cast<int>(colorInfo.fStoreOp)]; |
| 257 |
|
| 258 |
auto stencil = fFramebuffer->stencilAttachment(); |
| 259 |
auto mtlStencil = fRenderPassDesc.stencilAttachment; |
| 260 |
if (stencil) { |
| 261 |
mtlStencil.texture = stencil->mtlTexture(); |
| 262 |
} |
| 263 |
mtlStencil.clearStencil = 0; |
| 264 |
mtlStencil.loadAction = mtlLoadAction[static_cast<int>(stencilInfo.fLoadOp)]; |
| 265 |
mtlStencil.storeAction = mtlStoreAction[static_cast<int>(stencilInfo.fStoreOp)]; |
| 266 |
|
| 267 |
if (!this->setupResolve()) { |
| 268 |
// Manage initial clears |
| 269 |
if (colorInfo.fLoadOp == GrLoadOp::kClear || stencilInfo.fLoadOp == GrLoadOp::kClear) { |
| 270 |
fBounds = SkRect::MakeWH(color->dimensions().width(), |
| 271 |
color->dimensions().height()); |
| 272 |
this->setupRenderCommandEncoder(nullptr); |
| 273 |
} else { |
| 274 |
fBounds.setEmpty(); |
| 275 |
// For now, we lazily create the renderCommandEncoder because we may have no draws, |
| 276 |
// and an empty renderCommandEncoder can still produce output. This can cause issues |
| 277 |
// when we clear a texture upon creation -- we'll subsequently discard the contents. |
| 278 |
// This can be removed when that ordering is fixed. |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
bool GrMtlOpsRenderPass::setupResolve() { |
| 284 |
fActiveRenderCmdEncoder = nullptr; |
| 285 |
auto resolve = fFramebuffer->resolveAttachment(); |
| 286 |
if (resolve) { |
| 287 |
auto colorAttachment = fRenderPassDesc.colorAttachments[0]; |
| 288 |
colorAttachment.resolveTexture = resolve->mtlTexture(); |
| 289 |
// TODO: For framebufferOnly attachments we should do StoreAndMultisampleResolve if |
| 290 |
// the storeAction is Store. But for the moment they don't take this path. |
| 291 |
colorAttachment.storeAction = MTLStoreActionMultisampleResolve; |
| 292 |
if (colorAttachment.loadAction == MTLLoadActionLoad) { |
| 293 |
auto color = fFramebuffer->colorAttachment(); |
| 294 |
auto dimensions = color->dimensions(); |
| 295 |
// for now use the full bounds |
| 296 |
auto nativeBounds = GrNativeRect::MakeIRectRelativeTo( |
| 297 |
fOrigin, dimensions.height(), SkIRect::MakeSize(dimensions)); |
| 298 |
fActiveRenderCmdEncoder = |
| 299 |
fGpu->loadMSAAFromResolve(color, resolve, nativeBounds, |
| 300 |
fRenderPassDesc.stencilAttachment); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
return (fActiveRenderCmdEncoder != nullptr); |
| 305 |
} |
| 306 |
|
| 307 |
void GrMtlOpsRenderPass::setupRenderCommandEncoder(GrMtlPipelineState* pipelineState) { |
| 308 |
fActiveRenderCmdEncoder = |
| 309 |
fGpu->commandBuffer()->getRenderCommandEncoder(fRenderPassDesc, pipelineState, this); |
| 310 |
// Any future RenderCommandEncoders we create for this OpsRenderPass should load, |
| 311 |
// unless onClear or onClearStencilClip are explicitly called. |
| 312 |
auto colorAttachment = fRenderPassDesc.colorAttachments[0]; |
| 313 |
colorAttachment.loadAction = MTLLoadActionLoad; |
| 314 |
auto stencilAttachment = fRenderPassDesc.stencilAttachment; |
| 315 |
stencilAttachment.loadAction = MTLLoadActionLoad; |
| 316 |
} |
| 317 |
|
| 318 |
void GrMtlOpsRenderPass::onBindBuffers(sk_sp<const GrBuffer> indexBuffer, |
| 319 |
sk_sp<const GrBuffer> instanceBuffer, |
| 320 |
sk_sp<const GrBuffer> vertexBuffer, |
| 321 |
GrPrimitiveRestart primRestart) { |
| 322 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 323 |
if (!fDebugGroupActive) { |
| 324 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 325 |
fDebugGroupActive = true; |
| 326 |
} |
| 327 |
#endif |
| 328 |
SkASSERT(GrPrimitiveRestart::kNo == primRestart); |
| 329 |
int inputBufferIndex = 0; |
| 330 |
if (vertexBuffer) { |
| 331 |
SkASSERT(!vertexBuffer->isCpuBuffer()); |
| 332 |
SkASSERT(!static_cast<const GrGpuBuffer*>(vertexBuffer.get())->isMapped()); |
| 333 |
fActiveVertexBuffer = std::move(vertexBuffer); |
| 334 |
fGpu->commandBuffer()->addGrBuffer(fActiveVertexBuffer); |
| 335 |
++inputBufferIndex; |
| 336 |
} |
| 337 |
if (instanceBuffer) { |
| 338 |
SkASSERT(!instanceBuffer->isCpuBuffer()); |
| 339 |
SkASSERT(!static_cast<const GrGpuBuffer*>(instanceBuffer.get())->isMapped()); |
| 340 |
this->setVertexBuffer(fActiveRenderCmdEncoder, instanceBuffer.get(), 0, inputBufferIndex++); |
| 341 |
fActiveInstanceBuffer = std::move(instanceBuffer); |
| 342 |
fGpu->commandBuffer()->addGrBuffer(fActiveInstanceBuffer); |
| 343 |
} |
| 344 |
if (indexBuffer) { |
| 345 |
SkASSERT(!indexBuffer->isCpuBuffer()); |
| 346 |
SkASSERT(!static_cast<const GrGpuBuffer*>(indexBuffer.get())->isMapped()); |
| 347 |
fActiveIndexBuffer = std::move(indexBuffer); |
| 348 |
fGpu->commandBuffer()->addGrBuffer(fActiveIndexBuffer); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
void GrMtlOpsRenderPass::onDraw(int vertexCount, int baseVertex) { |
| 353 |
SkASSERT(fActivePipelineState); |
| 354 |
SkASSERT(nil != fActiveRenderCmdEncoder); |
| 355 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 356 |
if (!fDebugGroupActive) { |
| 357 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 358 |
fDebugGroupActive = true; |
| 359 |
} |
| 360 |
#endif |
| 361 |
this->setVertexBuffer(fActiveRenderCmdEncoder, fActiveVertexBuffer.get(), 0, 0); |
| 362 |
|
| 363 |
fActiveRenderCmdEncoder->drawPrimitives(fActivePrimitiveType, baseVertex, vertexCount); |
| 364 |
fGpu->stats()->incNumDraws(); |
| 365 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 366 |
SkASSERT(fDebugGroupActive); |
| 367 |
fActiveRenderCmdEncoder->popDebugGroup(); |
| 368 |
fDebugGroupActive = false; |
| 369 |
#endif |
| 370 |
} |
| 371 |
|
| 372 |
void GrMtlOpsRenderPass::onDrawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue, |
| 373 |
uint16_t maxIndexValue, int baseVertex) { |
| 374 |
SkASSERT(fActivePipelineState); |
| 375 |
SkASSERT(nil != fActiveRenderCmdEncoder); |
| 376 |
SkASSERT(fActiveIndexBuffer); |
| 377 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 378 |
if (!fDebugGroupActive) { |
| 379 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 380 |
fDebugGroupActive = true; |
| 381 |
} |
| 382 |
#endif |
| 383 |
this->setVertexBuffer(fActiveRenderCmdEncoder, fActiveVertexBuffer.get(), |
| 384 |
fCurrentVertexStride * baseVertex, 0); |
| 385 |
|
| 386 |
auto mtlIndexBuffer = static_cast<const GrMtlBuffer*>(fActiveIndexBuffer.get()); |
| 387 |
size_t indexOffset = sizeof(uint16_t) * baseIndex; |
| 388 |
id<MTLBuffer> indexBuffer = mtlIndexBuffer->mtlBuffer(); |
| 389 |
fActiveRenderCmdEncoder->drawIndexedPrimitives(fActivePrimitiveType, indexCount, |
| 390 |
MTLIndexTypeUInt16, indexBuffer, indexOffset); |
| 391 |
fGpu->stats()->incNumDraws(); |
| 392 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 393 |
SkASSERT(fDebugGroupActive); |
| 394 |
fActiveRenderCmdEncoder->popDebugGroup(); |
| 395 |
fDebugGroupActive = false; |
| 396 |
#endif |
| 397 |
} |
| 398 |
|
| 399 |
void GrMtlOpsRenderPass::onDrawInstanced(int instanceCount, int baseInstance, int vertexCount, |
| 400 |
int baseVertex) { |
| 401 |
SkASSERT(fActivePipelineState); |
| 402 |
SkASSERT(nil != fActiveRenderCmdEncoder); |
| 403 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 404 |
if (!fDebugGroupActive) { |
| 405 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 406 |
fDebugGroupActive = true; |
| 407 |
} |
| 408 |
#endif |
| 409 |
this->setVertexBuffer(fActiveRenderCmdEncoder, fActiveVertexBuffer.get(), 0, 0); |
| 410 |
|
| 411 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 412 |
fActiveRenderCmdEncoder->drawPrimitives(fActivePrimitiveType, baseVertex, vertexCount, |
| 413 |
instanceCount, baseInstance); |
| 414 |
} else { |
| 415 |
SkASSERT(false); |
| 416 |
} |
| 417 |
fGpu->stats()->incNumDraws(); |
| 418 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 419 |
SkASSERT(fDebugGroupActive); |
| 420 |
fActiveRenderCmdEncoder->popDebugGroup(); |
| 421 |
fDebugGroupActive = false; |
| 422 |
#endif |
| 423 |
} |
| 424 |
|
| 425 |
void GrMtlOpsRenderPass::onDrawIndexedInstanced( |
| 426 |
int indexCount, int baseIndex, int instanceCount, int baseInstance, int baseVertex) { |
| 427 |
SkASSERT(fActivePipelineState); |
| 428 |
SkASSERT(nil != fActiveRenderCmdEncoder); |
| 429 |
SkASSERT(fActiveIndexBuffer); |
| 430 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 431 |
if (!fDebugGroupActive) { |
| 432 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 433 |
fDebugGroupActive = true; |
| 434 |
} |
| 435 |
#endif |
| 436 |
this->setVertexBuffer(fActiveRenderCmdEncoder, fActiveVertexBuffer.get(), 0, 0); |
| 437 |
|
| 438 |
auto mtlIndexBuffer = static_cast<const GrMtlBuffer*>(fActiveIndexBuffer.get()); |
| 439 |
size_t indexOffset = sizeof(uint16_t) * baseIndex; |
| 440 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 441 |
fActiveRenderCmdEncoder->drawIndexedPrimitives(fActivePrimitiveType, indexCount, |
| 442 |
MTLIndexTypeUInt16, |
| 443 |
mtlIndexBuffer->mtlBuffer(), indexOffset, |
| 444 |
instanceCount, baseVertex, baseInstance); |
| 445 |
} else { |
| 446 |
SkASSERT(false); |
| 447 |
} |
| 448 |
fGpu->stats()->incNumDraws(); |
| 449 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 450 |
SkASSERT(fDebugGroupActive); |
| 451 |
fActiveRenderCmdEncoder->popDebugGroup(); |
| 452 |
fDebugGroupActive = false; |
| 453 |
#endif |
| 454 |
} |
| 455 |
|
| 456 |
void GrMtlOpsRenderPass::onDrawIndirect(const GrBuffer* drawIndirectBuffer, |
| 457 |
size_t bufferOffset, |
| 458 |
int drawCount) { |
| 459 |
SkASSERT(fGpu->caps()->nativeDrawIndirectSupport()); |
| 460 |
SkASSERT(fActivePipelineState); |
| 461 |
SkASSERT(nil != fActiveRenderCmdEncoder); |
| 462 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 463 |
if (!fDebugGroupActive) { |
| 464 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 465 |
fDebugGroupActive = true; |
| 466 |
} |
| 467 |
#endif |
| 468 |
this->setVertexBuffer(fActiveRenderCmdEncoder, fActiveVertexBuffer.get(), 0, 0); |
| 469 |
|
| 470 |
auto mtlIndirectBuffer = static_cast<const GrMtlBuffer*>(drawIndirectBuffer); |
| 471 |
const size_t stride = sizeof(GrDrawIndirectCommand); |
| 472 |
while (drawCount >= 1) { |
| 473 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 474 |
fActiveRenderCmdEncoder->drawPrimitives(fActivePrimitiveType, |
| 475 |
mtlIndirectBuffer->mtlBuffer(), bufferOffset); |
| 476 |
} else { |
| 477 |
SkASSERT(false); |
| 478 |
} |
| 479 |
drawCount--; |
| 480 |
bufferOffset += stride; |
| 481 |
fGpu->stats()->incNumDraws(); |
| 482 |
} |
| 483 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 484 |
SkASSERT(fDebugGroupActive); |
| 485 |
fActiveRenderCmdEncoder->popDebugGroup(); |
| 486 |
fDebugGroupActive = false; |
| 487 |
#endif |
| 488 |
} |
| 489 |
|
| 490 |
void GrMtlOpsRenderPass::onDrawIndexedIndirect(const GrBuffer* drawIndirectBuffer, |
| 491 |
size_t bufferOffset, |
| 492 |
int drawCount) { |
| 493 |
SkASSERT(fGpu->caps()->nativeDrawIndirectSupport()); |
| 494 |
SkASSERT(fActivePipelineState); |
| 495 |
SkASSERT(nil != fActiveRenderCmdEncoder); |
| 496 |
SkASSERT(fActiveIndexBuffer); |
| 497 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 498 |
if (!fDebugGroupActive) { |
| 499 |
fActiveRenderCmdEncoder->pushDebugGroup(@"bindAndDraw"); |
| 500 |
fDebugGroupActive = true; |
| 501 |
} |
| 502 |
#endif |
| 503 |
this->setVertexBuffer(fActiveRenderCmdEncoder, fActiveVertexBuffer.get(), 0, 0); |
| 504 |
|
| 505 |
auto mtlIndexBuffer = static_cast<const GrMtlBuffer*>(fActiveIndexBuffer.get()); |
| 506 |
auto mtlIndirectBuffer = static_cast<const GrMtlBuffer*>(drawIndirectBuffer); |
| 507 |
size_t indexOffset = 0; |
| 508 |
|
| 509 |
const size_t stride = sizeof(GrDrawIndexedIndirectCommand); |
| 510 |
while (drawCount >= 1) { |
| 511 |
if (@available(macOS 10.11, iOS 9.0, tvOS 9.0, *)) { |
| 512 |
fActiveRenderCmdEncoder->drawIndexedPrimitives(fActivePrimitiveType, |
| 513 |
MTLIndexTypeUInt16, |
| 514 |
mtlIndexBuffer->mtlBuffer(), |
| 515 |
indexOffset, |
| 516 |
mtlIndirectBuffer->mtlBuffer(), |
| 517 |
bufferOffset); |
| 518 |
} else { |
| 519 |
SkASSERT(false); |
| 520 |
} |
| 521 |
drawCount--; |
| 522 |
bufferOffset += stride; |
| 523 |
fGpu->stats()->incNumDraws(); |
| 524 |
} |
| 525 |
#ifdef SK_ENABLE_MTL_DEBUG_INFO |
| 526 |
SkASSERT(fDebugGroupActive); |
| 527 |
fActiveRenderCmdEncoder->popDebugGroup(); |
| 528 |
fDebugGroupActive = false; |
| 529 |
#endif |
| 530 |
} |
| 531 |
|
| 532 |
void GrMtlOpsRenderPass::setVertexBuffer(GrMtlRenderCommandEncoder* encoder, |
| 533 |
const GrBuffer* buffer, |
| 534 |
size_t vertexOffset, |
| 535 |
size_t inputBufferIndex) { |
| 536 |
if (!buffer) { |
| 537 |
return; |
| 538 |
} |
| 539 |
|
| 540 |
// point after the uniforms |
| 541 |
constexpr static int kFirstBufferBindingIdx = GrMtlUniformHandler::kLastUniformBinding + 1; |
| 542 |
int index = inputBufferIndex + kFirstBufferBindingIdx; |
| 543 |
SkASSERT(index < 4); |
| 544 |
auto mtlBuffer = static_cast<const GrMtlBuffer*>(buffer); |
| 545 |
id<MTLBuffer> mtlVertexBuffer = mtlBuffer->mtlBuffer(); |
| 546 |
SkASSERT(mtlVertexBuffer); |
| 547 |
size_t offset = vertexOffset; |
| 548 |
encoder->setVertexBuffer(mtlVertexBuffer, offset, index); |
| 549 |
} |
| 550 |
|
| 551 |
GR_NORETAIN_END |