egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrOpsRenderPass.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkRect.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrCaps.h" |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrCpuBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrGpu.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrPrimitiveProcessor.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrProgramInfo.h" |
Brian Salomon | 201cdbb | 2019-08-14 17:00:30 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrRenderTarget.h" |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 17 | #include "src/gpu/GrScissorState.h" |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 18 | #include "src/gpu/GrSimpleMesh.h" |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrTexture.h" |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 20 | |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 21 | void GrOpsRenderPass::begin() { |
| 22 | fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured; |
| 23 | #ifdef SK_DEBUG |
| 24 | fScissorStatus = DynamicStateStatus::kDisabled; |
| 25 | fTextureBindingStatus = DynamicStateStatus::kDisabled; |
| 26 | fHasIndexBuffer = false; |
| 27 | fInstanceBufferStatus = DynamicStateStatus::kDisabled; |
| 28 | fVertexBufferStatus = DynamicStateStatus::kDisabled; |
| 29 | #endif |
| 30 | this->onBegin(); |
| 31 | } |
| 32 | |
| 33 | void GrOpsRenderPass::end() { |
| 34 | this->onEnd(); |
Chris Dalton | b60c8a2 | 2020-03-31 17:24:22 -0600 | [diff] [blame] | 35 | this->resetActiveBuffers(); |
Chris Dalton | 2c3e169 | 2020-03-20 12:32:55 -0600 | [diff] [blame] | 36 | } |
| 37 | |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 38 | void GrOpsRenderPass::clear(const GrScissorState& scissor, const SkPMColor4f& color) { |
Robert Phillips | 4912d90 | 2018-04-27 12:09:35 -0400 | [diff] [blame] | 39 | SkASSERT(fRenderTarget); |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 40 | // A clear at this level will always be a true clear, so make sure clears were not supposed to |
| 41 | // be redirected to draws instead |
| 42 | SkASSERT(!this->gpu()->caps()->performColorClearsAsDraws()); |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 43 | SkASSERT(!scissor.enabled() || !this->gpu()->caps()->performPartialClearsAsDraws()); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 44 | fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured; |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 45 | this->onClear(scissor, color); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 48 | void GrOpsRenderPass::clearStencilClip(const GrScissorState& scissor, bool insideStencilMask) { |
Michael Ludwig | c39d0c8 | 2019-01-15 10:03:43 -0500 | [diff] [blame] | 49 | // As above, make sure the stencil clear wasn't supposed to be a draw rect with stencil settings |
| 50 | SkASSERT(!this->gpu()->caps()->performStencilClearsAsDraws()); |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 51 | SkASSERT(!scissor.enabled() || !this->gpu()->caps()->performPartialClearsAsDraws()); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 52 | fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured; |
Michael Ludwig | 1e63279 | 2020-05-21 12:45:31 -0400 | [diff] [blame] | 53 | this->onClearStencilClip(scissor, insideStencilMask); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 56 | void GrOpsRenderPass::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) { |
| 57 | fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured; |
| 58 | this->onExecuteDrawable(std::move(drawable)); |
| 59 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 60 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 61 | void GrOpsRenderPass::bindPipeline(const GrProgramInfo& programInfo, const SkRect& drawBounds) { |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 62 | #ifdef SK_DEBUG |
Chris Dalton | 56c09f0 | 2020-03-13 17:30:03 -0600 | [diff] [blame] | 63 | // Both the 'programInfo' and this renderPass have an origin. Since they come from the same |
| 64 | // place (i.e., the target renderTargetProxy) they had best agree. |
| 65 | SkASSERT(programInfo.origin() == fOrigin); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 66 | if (programInfo.primProc().hasInstanceAttributes()) { |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 67 | SkASSERT(this->gpu()->caps()->drawInstancedSupport()); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 68 | } |
| 69 | if (programInfo.pipeline().usesConservativeRaster()) { |
| 70 | SkASSERT(this->gpu()->caps()->conservativeRasterSupport()); |
| 71 | // Conservative raster, by default, only supports triangles. Implementations can |
| 72 | // optionally indicate that they also support points and lines, but we don't currently |
| 73 | // query or track that info. |
| 74 | SkASSERT(GrIsPrimTypeTris(programInfo.primitiveType())); |
| 75 | } |
| 76 | if (programInfo.pipeline().isWireframe()) { |
| 77 | SkASSERT(this->gpu()->caps()->wireframeSupport()); |
| 78 | } |
Chris Dalton | 3d28b6a | 2020-04-13 11:12:28 -0600 | [diff] [blame] | 79 | if (this->gpu()->caps()->twoSidedStencilRefsAndMasksMustMatch() && |
| 80 | programInfo.pipeline().isStencilEnabled()) { |
| 81 | const GrUserStencilSettings* stencil = programInfo.pipeline().getUserStencil(); |
| 82 | if (stencil->isTwoSided(programInfo.pipeline().hasStencilClip())) { |
| 83 | SkASSERT(stencil->fCCWFace.fRef == stencil->fCWFace.fRef); |
| 84 | SkASSERT(stencil->fCCWFace.fTestMask == stencil->fCWFace.fTestMask); |
| 85 | SkASSERT(stencil->fCCWFace.fWriteMask == stencil->fCWFace.fWriteMask); |
| 86 | } |
| 87 | } |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 88 | if (GrPrimitiveType::kPatches == programInfo.primitiveType()) { |
| 89 | SkASSERT(this->gpu()->caps()->shaderCaps()->tessellationSupport()); |
| 90 | } |
Robert Phillips | 2d8a95e | 2019-10-10 12:50:22 -0400 | [diff] [blame] | 91 | programInfo.checkAllInstantiated(); |
| 92 | programInfo.checkMSAAAndMIPSAreResolved(); |
Robert Phillips | 12c4629 | 2019-04-23 07:36:17 -0400 | [diff] [blame] | 93 | #endif |
Robert Phillips | a91e0b7 | 2017-05-01 13:12:20 -0400 | [diff] [blame] | 94 | |
Chris Dalton | b60c8a2 | 2020-03-31 17:24:22 -0600 | [diff] [blame] | 95 | this->resetActiveBuffers(); |
| 96 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 97 | if (programInfo.primProc().numVertexAttributes() > this->gpu()->caps()->maxVertexAttributes()) { |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 98 | fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind; |
| 99 | return; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 100 | } |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 101 | |
| 102 | if (!this->onBindPipeline(programInfo, drawBounds)) { |
| 103 | fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind; |
| 104 | return; |
| 105 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 106 | |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 107 | #ifdef SK_DEBUG |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 108 | GrProcessor::CustomFeatures processorFeatures = programInfo.requestedFeatures(); |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 109 | if (GrProcessor::CustomFeatures::kSampleLocations & processorFeatures) { |
| 110 | // Verify we always have the same sample pattern key, regardless of graphics state. |
Brian Salomon | f7f5433 | 2020-07-28 09:23:35 -0400 | [diff] [blame^] | 111 | SkASSERT(this->gpu()->findOrAssignSamplePatternKey(fRenderTarget) == |
| 112 | fRenderTarget->getSamplePatternKey()); |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 113 | } |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 114 | fScissorStatus = (programInfo.pipeline().isScissorTestEnabled()) ? |
| 115 | DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled; |
| 116 | bool hasTextures = (programInfo.primProc().numTextureSamplers() > 0); |
| 117 | if (!hasTextures) { |
Brian Salomon | 7e67dca | 2020-07-21 09:27:25 -0400 | [diff] [blame] | 118 | programInfo.pipeline().visitProxies([&hasTextures](GrSurfaceProxy*, GrMipmapped) { |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 119 | hasTextures = true; |
| 120 | }); |
| 121 | } |
| 122 | fTextureBindingStatus = (hasTextures) ? |
| 123 | DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled; |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 124 | fHasIndexBuffer = false; |
| 125 | fInstanceBufferStatus = (programInfo.primProc().hasInstanceAttributes()) ? |
| 126 | DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled; |
| 127 | fVertexBufferStatus = (programInfo.primProc().hasVertexAttributes()) ? |
| 128 | DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled; |
Chris Dalton | 8c4cafd | 2019-04-15 19:14:36 -0600 | [diff] [blame] | 129 | #endif |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 130 | |
| 131 | fDrawPipelineStatus = DrawPipelineStatus::kOk; |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 132 | fXferBarrierType = programInfo.pipeline().xferBarrierType(fRenderTarget->asTexture(), |
| 133 | *this->gpu()->caps()); |
| 134 | } |
| 135 | |
| 136 | void GrOpsRenderPass::setScissorRect(const SkIRect& scissor) { |
| 137 | if (DrawPipelineStatus::kOk != fDrawPipelineStatus) { |
| 138 | SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus); |
| 139 | return; |
| 140 | } |
| 141 | SkASSERT(DynamicStateStatus::kDisabled != fScissorStatus); |
| 142 | this->onSetScissorRect(scissor); |
| 143 | SkDEBUGCODE(fScissorStatus = DynamicStateStatus::kConfigured); |
| 144 | } |
| 145 | |
Chris Dalton | db20afc | 2020-03-05 12:13:53 -0700 | [diff] [blame] | 146 | void GrOpsRenderPass::bindTextures(const GrPrimitiveProcessor& primProc, |
| 147 | const GrSurfaceProxy* const primProcTextures[], |
| 148 | const GrPipeline& pipeline) { |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 149 | #ifdef SK_DEBUG |
| 150 | SkASSERT((primProc.numTextureSamplers() > 0) == SkToBool(primProcTextures)); |
| 151 | for (int i = 0; i < primProc.numTextureSamplers(); ++i) { |
Chris Dalton | 3bf2f3a | 2020-03-17 11:48:23 -0600 | [diff] [blame] | 152 | const auto& sampler = primProc.textureSampler(i); |
| 153 | const GrSurfaceProxy* proxy = primProcTextures[i]; |
| 154 | SkASSERT(proxy); |
| 155 | SkASSERT(proxy->backendFormat() == sampler.backendFormat()); |
| 156 | SkASSERT(proxy->backendFormat().textureType() == sampler.backendFormat().textureType()); |
| 157 | |
| 158 | const GrTexture* tex = proxy->peekTexture(); |
| 159 | SkASSERT(tex); |
Brian Salomon | e69b9ef | 2020-07-22 11:18:06 -0400 | [diff] [blame] | 160 | if (sampler.samplerState().mipmapped() == GrMipmapped::kYes && |
Chris Dalton | 3bf2f3a | 2020-03-17 11:48:23 -0600 | [diff] [blame] | 161 | (tex->width() != 1 || tex->height() != 1)) { |
| 162 | // There are some cases where we might be given a non-mipmapped texture with a mipmap |
| 163 | // filter. See skbug.com/7094. |
Brian Salomon | 4cfae3b | 2020-07-23 10:33:24 -0400 | [diff] [blame] | 164 | SkASSERT(tex->mipmapped() != GrMipmapped::kYes || !tex->mipmapsAreDirty()); |
Chris Dalton | 3bf2f3a | 2020-03-17 11:48:23 -0600 | [diff] [blame] | 165 | } |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 166 | } |
| 167 | #endif |
| 168 | |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 169 | if (DrawPipelineStatus::kOk != fDrawPipelineStatus) { |
| 170 | SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus); |
| 171 | return; |
| 172 | } |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 173 | |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 174 | // Don't assert on fTextureBindingStatus. onBindTextures() just turns into a no-op when there |
| 175 | // aren't any textures, and it's hard to tell from the GrPipeline whether there are any. For |
| 176 | // many clients it is easier to just always call this method. |
Chris Dalton | db20afc | 2020-03-05 12:13:53 -0700 | [diff] [blame] | 177 | if (!this->onBindTextures(primProc, primProcTextures, pipeline)) { |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 178 | fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind; |
| 179 | return; |
| 180 | } |
Chris Dalton | 1b1b0d5 | 2020-03-03 12:00:59 -0700 | [diff] [blame] | 181 | |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 182 | SkDEBUGCODE(fTextureBindingStatus = DynamicStateStatus::kConfigured); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 185 | void GrOpsRenderPass::bindBuffers(sk_sp<const GrBuffer> indexBuffer, |
| 186 | sk_sp<const GrBuffer> instanceBuffer, |
| 187 | sk_sp<const GrBuffer> vertexBuffer, |
| 188 | GrPrimitiveRestart primRestart) { |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 189 | if (DrawPipelineStatus::kOk != fDrawPipelineStatus) { |
| 190 | SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | #ifdef SK_DEBUG |
| 195 | if (indexBuffer) { |
| 196 | fHasIndexBuffer = true; |
| 197 | } |
| 198 | |
| 199 | SkASSERT((DynamicStateStatus::kDisabled == fInstanceBufferStatus) != SkToBool(instanceBuffer)); |
| 200 | if (instanceBuffer) { |
| 201 | fInstanceBufferStatus = DynamicStateStatus::kConfigured; |
| 202 | } |
| 203 | |
| 204 | SkASSERT((DynamicStateStatus::kDisabled == fVertexBufferStatus) != SkToBool(vertexBuffer)); |
| 205 | if (vertexBuffer) { |
| 206 | fVertexBufferStatus = DynamicStateStatus::kConfigured; |
| 207 | } |
| 208 | |
| 209 | if (GrPrimitiveRestart::kYes == primRestart) { |
| 210 | SkASSERT(this->gpu()->caps()->usePrimitiveRestart()); |
| 211 | } |
| 212 | #endif |
| 213 | |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 214 | this->onBindBuffers(std::move(indexBuffer), std::move(instanceBuffer), std::move(vertexBuffer), |
| 215 | primRestart); |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 218 | bool GrOpsRenderPass::prepareToDraw() { |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 219 | if (DrawPipelineStatus::kOk != fDrawPipelineStatus) { |
| 220 | SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus); |
| 221 | this->gpu()->stats()->incNumFailedDraws(); |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 222 | return false; |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 223 | } |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 224 | SkASSERT(DynamicStateStatus::kUninitialized != fScissorStatus); |
| 225 | SkASSERT(DynamicStateStatus::kUninitialized != fTextureBindingStatus); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 226 | |
Chris Dalton | 2e7ed26 | 2020-02-21 15:17:59 -0700 | [diff] [blame] | 227 | if (kNone_GrXferBarrierType != fXferBarrierType) { |
| 228 | this->gpu()->xferBarrier(fRenderTarget, fXferBarrierType); |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 229 | } |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 233 | void GrOpsRenderPass::draw(int vertexCount, int baseVertex) { |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 234 | if (!this->prepareToDraw()) { |
| 235 | return; |
| 236 | } |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 237 | SkASSERT(!fHasIndexBuffer); |
| 238 | SkASSERT(DynamicStateStatus::kConfigured != fInstanceBufferStatus); |
| 239 | SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus); |
| 240 | this->onDraw(vertexCount, baseVertex); |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 243 | void GrOpsRenderPass::drawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue, |
| 244 | uint16_t maxIndexValue, int baseVertex) { |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 245 | if (!this->prepareToDraw()) { |
| 246 | return; |
| 247 | } |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 248 | SkASSERT(fHasIndexBuffer); |
| 249 | SkASSERT(DynamicStateStatus::kConfigured != fInstanceBufferStatus); |
| 250 | SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus); |
| 251 | this->onDrawIndexed(indexCount, baseIndex, minIndexValue, maxIndexValue, baseVertex); |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 254 | void GrOpsRenderPass::drawInstanced(int instanceCount, int baseInstance, int vertexCount, |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 255 | int baseVertex) { |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 256 | SkASSERT(this->gpu()->caps()->drawInstancedSupport()); |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 257 | if (!this->prepareToDraw()) { |
| 258 | return; |
| 259 | } |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 260 | SkASSERT(!fHasIndexBuffer); |
| 261 | SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus); |
| 262 | SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus); |
| 263 | this->onDrawInstanced(instanceCount, baseInstance, vertexCount, baseVertex); |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 266 | void GrOpsRenderPass::drawIndexedInstanced(int indexCount, int baseIndex, int instanceCount, |
| 267 | int baseInstance, int baseVertex) { |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 268 | SkASSERT(this->gpu()->caps()->drawInstancedSupport()); |
Chris Dalton | 33db9fc | 2020-02-25 18:52:12 -0700 | [diff] [blame] | 269 | if (!this->prepareToDraw()) { |
| 270 | return; |
| 271 | } |
Chris Dalton | ded4370 | 2020-03-02 11:45:27 -0700 | [diff] [blame] | 272 | SkASSERT(fHasIndexBuffer); |
| 273 | SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus); |
| 274 | SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus); |
| 275 | this->onDrawIndexedInstanced(indexCount, baseIndex, instanceCount, baseInstance, baseVertex); |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 276 | } |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 277 | |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 278 | void GrOpsRenderPass::drawIndirect(const GrBuffer* drawIndirectBuffer, size_t bufferOffset, |
| 279 | int drawCount) { |
| 280 | SkASSERT(this->gpu()->caps()->drawInstancedSupport()); |
| 281 | SkASSERT(drawIndirectBuffer->isCpuBuffer() || |
| 282 | !static_cast<const GrGpuBuffer*>(drawIndirectBuffer)->isMapped()); |
| 283 | if (!this->prepareToDraw()) { |
| 284 | return; |
| 285 | } |
| 286 | SkASSERT(!fHasIndexBuffer); |
| 287 | SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus); |
| 288 | SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus); |
| 289 | if (!this->gpu()->caps()->nativeDrawIndirectSupport()) { |
| 290 | // Polyfill indirect draws with looping instanced calls. |
| 291 | SkASSERT(drawIndirectBuffer->isCpuBuffer()); |
| 292 | auto cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer); |
| 293 | auto cmd = reinterpret_cast<const GrDrawIndirectCommand*>( |
| 294 | cpuIndirectBuffer->data() + bufferOffset); |
| 295 | auto end = cmd + drawCount; |
| 296 | for (; cmd != end; ++cmd) { |
| 297 | this->onDrawInstanced(cmd->fInstanceCount, cmd->fBaseInstance, cmd->fVertexCount, |
| 298 | cmd->fBaseVertex); |
| 299 | } |
| 300 | return; |
| 301 | } |
| 302 | this->onDrawIndirect(drawIndirectBuffer, bufferOffset, drawCount); |
| 303 | } |
| 304 | |
| 305 | void GrOpsRenderPass::drawIndexedIndirect(const GrBuffer* drawIndirectBuffer, size_t bufferOffset, |
| 306 | int drawCount) { |
| 307 | SkASSERT(this->gpu()->caps()->drawInstancedSupport()); |
| 308 | SkASSERT(drawIndirectBuffer->isCpuBuffer() || |
| 309 | !static_cast<const GrGpuBuffer*>(drawIndirectBuffer)->isMapped()); |
| 310 | if (!this->prepareToDraw()) { |
| 311 | return; |
| 312 | } |
| 313 | SkASSERT(fHasIndexBuffer); |
| 314 | SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus); |
| 315 | SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus); |
| 316 | if (!this->gpu()->caps()->nativeDrawIndirectSupport() || |
| 317 | this->gpu()->caps()->nativeDrawIndexedIndirectIsBroken()) { |
| 318 | // Polyfill indexedIndirect draws with looping indexedInstanced calls. |
| 319 | SkASSERT(drawIndirectBuffer->isCpuBuffer()); |
| 320 | auto cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer); |
| 321 | auto cmd = reinterpret_cast<const GrDrawIndexedIndirectCommand*>( |
| 322 | cpuIndirectBuffer->data() + bufferOffset); |
| 323 | auto end = cmd + drawCount; |
| 324 | for (; cmd != end; ++cmd) { |
| 325 | this->onDrawIndexedInstanced(cmd->fIndexCount, cmd->fBaseIndex, cmd->fInstanceCount, |
| 326 | cmd->fBaseInstance, cmd->fBaseVertex); |
| 327 | } |
| 328 | return; |
| 329 | } |
| 330 | this->onDrawIndexedIndirect(drawIndirectBuffer, bufferOffset, drawCount); |
| 331 | } |
| 332 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 333 | void GrOpsRenderPass::drawIndexPattern(int patternIndexCount, int patternRepeatCount, |
| 334 | int maxPatternRepetitionsInIndexBuffer, |
| 335 | int patternVertexCount, int baseVertex) { |
| 336 | int baseRepetition = 0; |
| 337 | while (baseRepetition < patternRepeatCount) { |
| 338 | int repeatCount = std::min(patternRepeatCount - baseRepetition, |
| 339 | maxPatternRepetitionsInIndexBuffer); |
| 340 | int drawIndexCount = repeatCount * patternIndexCount; |
| 341 | // A patterned index buffer must contain indices in the range [0..vertexCount]. |
| 342 | int minIndexValue = 0; |
| 343 | int maxIndexValue = patternVertexCount * repeatCount - 1; |
| 344 | this->drawIndexed(drawIndexCount, 0, minIndexValue, maxIndexValue, |
| 345 | patternVertexCount * baseRepetition + baseVertex); |
| 346 | baseRepetition += repeatCount; |
| 347 | } |
| 348 | } |