blob: 66e346b82fe675b47bad8a8223c3221b9b588bc4 [file] [log] [blame]
egdaniel9cb63402016-06-23 08:37:05 -07001/*
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 Daniel2d41d0d2019-08-26 11:08:51 -04008#include "src/gpu/GrOpsRenderPass.h"
egdaniel9cb63402016-06-23 08:37:05 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkRect.h"
11#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrContextPriv.h"
14#include "src/gpu/GrFixedClip.h"
15#include "src/gpu/GrGpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrPrimitiveProcessor.h"
Robert Phillips901aff02019-10-08 12:32:56 -040017#include "src/gpu/GrProgramInfo.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040018#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrRenderTargetPriv.h"
Chris Daltoneb694b72020-03-16 09:25:50 -060020#include "src/gpu/GrSimpleMesh.h"
Chris Dalton4ece96d2019-08-30 11:26:39 -060021#include "src/gpu/GrTexturePriv.h"
egdaniel9cb63402016-06-23 08:37:05 -070022
Chris Dalton2c3e1692020-03-20 12:32:55 -060023void GrOpsRenderPass::begin() {
24 fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured;
25#ifdef SK_DEBUG
26 fScissorStatus = DynamicStateStatus::kDisabled;
27 fTextureBindingStatus = DynamicStateStatus::kDisabled;
28 fHasIndexBuffer = false;
29 fInstanceBufferStatus = DynamicStateStatus::kDisabled;
30 fVertexBufferStatus = DynamicStateStatus::kDisabled;
31#endif
32 this->onBegin();
33}
34
35void GrOpsRenderPass::end() {
36 this->onEnd();
Chris Daltonb60c8a22020-03-31 17:24:22 -060037 this->resetActiveBuffers();
Chris Dalton2c3e1692020-03-20 12:32:55 -060038}
39
Greg Daniel2d41d0d2019-08-26 11:08:51 -040040void GrOpsRenderPass::clear(const GrFixedClip& clip, const SkPMColor4f& color) {
Robert Phillips4912d902018-04-27 12:09:35 -040041 SkASSERT(fRenderTarget);
Michael Ludwigc39d0c82019-01-15 10:03:43 -050042 // A clear at this level will always be a true clear, so make sure clears were not supposed to
43 // be redirected to draws instead
44 SkASSERT(!this->gpu()->caps()->performColorClearsAsDraws());
45 SkASSERT(!clip.scissorEnabled() || !this->gpu()->caps()->performPartialClearsAsDraws());
Chris Dalton4386ad12020-02-19 16:42:06 -070046 fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured;
Robert Phillips19e51dc2017-08-09 09:30:51 -040047 this->onClear(clip, color);
egdaniel9cb63402016-06-23 08:37:05 -070048}
49
Greg Daniel2d41d0d2019-08-26 11:08:51 -040050void GrOpsRenderPass::clearStencilClip(const GrFixedClip& clip, bool insideStencilMask) {
Michael Ludwigc39d0c82019-01-15 10:03:43 -050051 // As above, make sure the stencil clear wasn't supposed to be a draw rect with stencil settings
52 SkASSERT(!this->gpu()->caps()->performStencilClearsAsDraws());
Chris Dalton4386ad12020-02-19 16:42:06 -070053 fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured;
Robert Phillips19e51dc2017-08-09 09:30:51 -040054 this->onClearStencilClip(clip, insideStencilMask);
egdaniel9cb63402016-06-23 08:37:05 -070055}
56
Chris Dalton4386ad12020-02-19 16:42:06 -070057void GrOpsRenderPass::executeDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
58 fDrawPipelineStatus = DrawPipelineStatus::kNotConfigured;
59 this->onExecuteDrawable(std::move(drawable));
60}
Robert Phillips901aff02019-10-08 12:32:56 -040061
Chris Daltonaa0e45c2020-03-16 10:05:11 -060062void GrOpsRenderPass::bindPipeline(const GrProgramInfo& programInfo, const SkRect& drawBounds) {
Chris Daltonbca46e22017-05-15 11:03:26 -060063#ifdef SK_DEBUG
Chris Dalton56c09f02020-03-13 17:30:03 -060064 // Both the 'programInfo' and this renderPass have an origin. Since they come from the same
65 // place (i.e., the target renderTargetProxy) they had best agree.
66 SkASSERT(programInfo.origin() == fOrigin);
Chris Dalton4386ad12020-02-19 16:42:06 -070067 if (programInfo.primProc().hasInstanceAttributes()) {
Chris Daltona77cdee2020-04-03 14:50:43 -060068 SkASSERT(this->gpu()->caps()->drawInstancedSupport());
Chris Dalton4386ad12020-02-19 16:42:06 -070069 }
70 if (programInfo.pipeline().usesConservativeRaster()) {
71 SkASSERT(this->gpu()->caps()->conservativeRasterSupport());
72 // Conservative raster, by default, only supports triangles. Implementations can
73 // optionally indicate that they also support points and lines, but we don't currently
74 // query or track that info.
75 SkASSERT(GrIsPrimTypeTris(programInfo.primitiveType()));
76 }
77 if (programInfo.pipeline().isWireframe()) {
78 SkASSERT(this->gpu()->caps()->wireframeSupport());
79 }
Chris Dalton3d28b6a2020-04-13 11:12:28 -060080 if (this->gpu()->caps()->twoSidedStencilRefsAndMasksMustMatch() &&
81 programInfo.pipeline().isStencilEnabled()) {
82 const GrUserStencilSettings* stencil = programInfo.pipeline().getUserStencil();
83 if (stencil->isTwoSided(programInfo.pipeline().hasStencilClip())) {
84 SkASSERT(stencil->fCCWFace.fRef == stencil->fCWFace.fRef);
85 SkASSERT(stencil->fCCWFace.fTestMask == stencil->fCWFace.fTestMask);
86 SkASSERT(stencil->fCCWFace.fWriteMask == stencil->fCWFace.fWriteMask);
87 }
88 }
Chris Dalton4386ad12020-02-19 16:42:06 -070089 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
90 SkASSERT(this->gpu()->caps()->shaderCaps()->tessellationSupport());
91 }
Robert Phillips2d8a95e2019-10-10 12:50:22 -040092 programInfo.checkAllInstantiated();
93 programInfo.checkMSAAAndMIPSAreResolved();
Robert Phillips12c46292019-04-23 07:36:17 -040094#endif
Robert Phillipsa91e0b72017-05-01 13:12:20 -040095
Chris Daltonb60c8a22020-03-31 17:24:22 -060096 this->resetActiveBuffers();
97
Robert Phillips901aff02019-10-08 12:32:56 -040098 if (programInfo.primProc().numVertexAttributes() > this->gpu()->caps()->maxVertexAttributes()) {
Chris Dalton4386ad12020-02-19 16:42:06 -070099 fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind;
100 return;
egdaniel9cb63402016-06-23 08:37:05 -0700101 }
Chris Dalton4386ad12020-02-19 16:42:06 -0700102
103 if (!this->onBindPipeline(programInfo, drawBounds)) {
104 fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind;
105 return;
106 }
Robert Phillips901aff02019-10-08 12:32:56 -0400107
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600108#ifdef SK_DEBUG
Robert Phillips901aff02019-10-08 12:32:56 -0400109 GrProcessor::CustomFeatures processorFeatures = programInfo.requestedFeatures();
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600110 if (GrProcessor::CustomFeatures::kSampleLocations & processorFeatures) {
111 // Verify we always have the same sample pattern key, regardless of graphics state.
112 SkASSERT(this->gpu()->findOrAssignSamplePatternKey(fRenderTarget)
113 == fRenderTarget->renderTargetPriv().getSamplePatternKey());
114 }
Chris Dalton2e7ed262020-02-21 15:17:59 -0700115 fScissorStatus = (programInfo.pipeline().isScissorTestEnabled()) ?
116 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
117 bool hasTextures = (programInfo.primProc().numTextureSamplers() > 0);
118 if (!hasTextures) {
119 programInfo.pipeline().visitProxies([&hasTextures](GrSurfaceProxy*, GrMipMapped) {
120 hasTextures = true;
121 });
122 }
123 fTextureBindingStatus = (hasTextures) ?
124 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
Chris Daltonded43702020-03-02 11:45:27 -0700125 fHasIndexBuffer = false;
126 fInstanceBufferStatus = (programInfo.primProc().hasInstanceAttributes()) ?
127 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
128 fVertexBufferStatus = (programInfo.primProc().hasVertexAttributes()) ?
129 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600130#endif
Chris Dalton4386ad12020-02-19 16:42:06 -0700131
132 fDrawPipelineStatus = DrawPipelineStatus::kOk;
Chris Dalton2e7ed262020-02-21 15:17:59 -0700133 fXferBarrierType = programInfo.pipeline().xferBarrierType(fRenderTarget->asTexture(),
134 *this->gpu()->caps());
135}
136
137void GrOpsRenderPass::setScissorRect(const SkIRect& scissor) {
138 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
139 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
140 return;
141 }
142 SkASSERT(DynamicStateStatus::kDisabled != fScissorStatus);
143 this->onSetScissorRect(scissor);
144 SkDEBUGCODE(fScissorStatus = DynamicStateStatus::kConfigured);
145}
146
Chris Daltondb20afc2020-03-05 12:13:53 -0700147void GrOpsRenderPass::bindTextures(const GrPrimitiveProcessor& primProc,
148 const GrSurfaceProxy* const primProcTextures[],
149 const GrPipeline& pipeline) {
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700150#ifdef SK_DEBUG
151 SkASSERT((primProc.numTextureSamplers() > 0) == SkToBool(primProcTextures));
152 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600153 const auto& sampler = primProc.textureSampler(i);
154 const GrSurfaceProxy* proxy = primProcTextures[i];
155 SkASSERT(proxy);
156 SkASSERT(proxy->backendFormat() == sampler.backendFormat());
157 SkASSERT(proxy->backendFormat().textureType() == sampler.backendFormat().textureType());
158
159 const GrTexture* tex = proxy->peekTexture();
160 SkASSERT(tex);
161 if (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter() &&
162 (tex->width() != 1 || tex->height() != 1)) {
163 // There are some cases where we might be given a non-mipmapped texture with a mipmap
164 // filter. See skbug.com/7094.
165 SkASSERT(tex->texturePriv().mipMapped() != GrMipMapped::kYes ||
166 !tex->texturePriv().mipMapsAreDirty());
167 }
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700168 }
169#endif
170
Chris Dalton2e7ed262020-02-21 15:17:59 -0700171 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
172 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
173 return;
174 }
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700175
Chris Dalton2e7ed262020-02-21 15:17:59 -0700176 // Don't assert on fTextureBindingStatus. onBindTextures() just turns into a no-op when there
177 // aren't any textures, and it's hard to tell from the GrPipeline whether there are any. For
178 // many clients it is easier to just always call this method.
Chris Daltondb20afc2020-03-05 12:13:53 -0700179 if (!this->onBindTextures(primProc, primProcTextures, pipeline)) {
Chris Dalton2e7ed262020-02-21 15:17:59 -0700180 fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind;
181 return;
182 }
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700183
Chris Dalton2e7ed262020-02-21 15:17:59 -0700184 SkDEBUGCODE(fTextureBindingStatus = DynamicStateStatus::kConfigured);
Chris Dalton4386ad12020-02-19 16:42:06 -0700185}
186
Chris Daltonded43702020-03-02 11:45:27 -0700187void GrOpsRenderPass::bindBuffers(const GrBuffer* indexBuffer, const GrBuffer* instanceBuffer,
188 const GrBuffer* vertexBuffer, GrPrimitiveRestart primRestart) {
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
214 this->onBindBuffers(indexBuffer, instanceBuffer, vertexBuffer, primRestart);
215}
216
Chris Dalton33db9fc2020-02-25 18:52:12 -0700217bool GrOpsRenderPass::prepareToDraw() {
Chris Dalton4386ad12020-02-19 16:42:06 -0700218 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
219 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
220 this->gpu()->stats()->incNumFailedDraws();
Chris Dalton33db9fc2020-02-25 18:52:12 -0700221 return false;
Chris Dalton4386ad12020-02-19 16:42:06 -0700222 }
Chris Dalton2e7ed262020-02-21 15:17:59 -0700223 SkASSERT(DynamicStateStatus::kUninitialized != fScissorStatus);
224 SkASSERT(DynamicStateStatus::kUninitialized != fTextureBindingStatus);
Chris Dalton4386ad12020-02-19 16:42:06 -0700225
Chris Dalton2e7ed262020-02-21 15:17:59 -0700226 if (kNone_GrXferBarrierType != fXferBarrierType) {
227 this->gpu()->xferBarrier(fRenderTarget, fXferBarrierType);
Chris Dalton4386ad12020-02-19 16:42:06 -0700228 }
Chris Dalton33db9fc2020-02-25 18:52:12 -0700229 return true;
230}
231
Chris Daltonded43702020-03-02 11:45:27 -0700232void GrOpsRenderPass::draw(int vertexCount, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -0700233 if (!this->prepareToDraw()) {
234 return;
235 }
Chris Daltonded43702020-03-02 11:45:27 -0700236 SkASSERT(!fHasIndexBuffer);
237 SkASSERT(DynamicStateStatus::kConfigured != fInstanceBufferStatus);
238 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
239 this->onDraw(vertexCount, baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -0700240}
241
Chris Daltonded43702020-03-02 11:45:27 -0700242void GrOpsRenderPass::drawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue,
243 uint16_t maxIndexValue, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -0700244 if (!this->prepareToDraw()) {
245 return;
246 }
Chris Daltonded43702020-03-02 11:45:27 -0700247 SkASSERT(fHasIndexBuffer);
248 SkASSERT(DynamicStateStatus::kConfigured != fInstanceBufferStatus);
249 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
250 this->onDrawIndexed(indexCount, baseIndex, minIndexValue, maxIndexValue, baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -0700251}
252
Chris Daltonded43702020-03-02 11:45:27 -0700253void GrOpsRenderPass::drawInstanced(int instanceCount, int baseInstance, int vertexCount,
Chris Dalton33db9fc2020-02-25 18:52:12 -0700254 int baseVertex) {
Chris Daltona77cdee2020-04-03 14:50:43 -0600255 SkASSERT(this->gpu()->caps()->drawInstancedSupport());
Chris Dalton33db9fc2020-02-25 18:52:12 -0700256 if (!this->prepareToDraw()) {
257 return;
258 }
Chris Daltonded43702020-03-02 11:45:27 -0700259 SkASSERT(!fHasIndexBuffer);
260 SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus);
261 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
262 this->onDrawInstanced(instanceCount, baseInstance, vertexCount, baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -0700263}
264
Chris Daltonded43702020-03-02 11:45:27 -0700265void GrOpsRenderPass::drawIndexedInstanced(int indexCount, int baseIndex, int instanceCount,
266 int baseInstance, int baseVertex) {
Chris Daltona77cdee2020-04-03 14:50:43 -0600267 SkASSERT(this->gpu()->caps()->drawInstancedSupport());
Chris Dalton33db9fc2020-02-25 18:52:12 -0700268 if (!this->prepareToDraw()) {
269 return;
270 }
Chris Daltonded43702020-03-02 11:45:27 -0700271 SkASSERT(fHasIndexBuffer);
272 SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus);
273 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
274 this->onDrawIndexedInstanced(indexCount, baseIndex, instanceCount, baseInstance, baseVertex);
egdaniel9cb63402016-06-23 08:37:05 -0700275}
Chris Daltonbb768422020-03-12 12:13:29 -0600276
Chris Dalton03fdf6a2020-04-07 12:31:59 -0600277void GrOpsRenderPass::drawIndirect(const GrBuffer* drawIndirectBuffer, size_t bufferOffset,
278 int drawCount) {
279 SkASSERT(this->gpu()->caps()->drawInstancedSupport());
280 SkASSERT(drawIndirectBuffer->isCpuBuffer() ||
281 !static_cast<const GrGpuBuffer*>(drawIndirectBuffer)->isMapped());
282 if (!this->prepareToDraw()) {
283 return;
284 }
285 SkASSERT(!fHasIndexBuffer);
286 SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus);
287 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
288 if (!this->gpu()->caps()->nativeDrawIndirectSupport()) {
289 // Polyfill indirect draws with looping instanced calls.
290 SkASSERT(drawIndirectBuffer->isCpuBuffer());
291 auto cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer);
292 auto cmd = reinterpret_cast<const GrDrawIndirectCommand*>(
293 cpuIndirectBuffer->data() + bufferOffset);
294 auto end = cmd + drawCount;
295 for (; cmd != end; ++cmd) {
296 this->onDrawInstanced(cmd->fInstanceCount, cmd->fBaseInstance, cmd->fVertexCount,
297 cmd->fBaseVertex);
298 }
299 return;
300 }
301 this->onDrawIndirect(drawIndirectBuffer, bufferOffset, drawCount);
302}
303
304void GrOpsRenderPass::drawIndexedIndirect(const GrBuffer* drawIndirectBuffer, size_t bufferOffset,
305 int drawCount) {
306 SkASSERT(this->gpu()->caps()->drawInstancedSupport());
307 SkASSERT(drawIndirectBuffer->isCpuBuffer() ||
308 !static_cast<const GrGpuBuffer*>(drawIndirectBuffer)->isMapped());
309 if (!this->prepareToDraw()) {
310 return;
311 }
312 SkASSERT(fHasIndexBuffer);
313 SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus);
314 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
315 if (!this->gpu()->caps()->nativeDrawIndirectSupport() ||
316 this->gpu()->caps()->nativeDrawIndexedIndirectIsBroken()) {
317 // Polyfill indexedIndirect draws with looping indexedInstanced calls.
318 SkASSERT(drawIndirectBuffer->isCpuBuffer());
319 auto cpuIndirectBuffer = static_cast<const GrCpuBuffer*>(drawIndirectBuffer);
320 auto cmd = reinterpret_cast<const GrDrawIndexedIndirectCommand*>(
321 cpuIndirectBuffer->data() + bufferOffset);
322 auto end = cmd + drawCount;
323 for (; cmd != end; ++cmd) {
324 this->onDrawIndexedInstanced(cmd->fIndexCount, cmd->fBaseIndex, cmd->fInstanceCount,
325 cmd->fBaseInstance, cmd->fBaseVertex);
326 }
327 return;
328 }
329 this->onDrawIndexedIndirect(drawIndirectBuffer, bufferOffset, drawCount);
330}
331
Chris Daltonbb768422020-03-12 12:13:29 -0600332void GrOpsRenderPass::drawIndexPattern(int patternIndexCount, int patternRepeatCount,
333 int maxPatternRepetitionsInIndexBuffer,
334 int patternVertexCount, int baseVertex) {
335 int baseRepetition = 0;
336 while (baseRepetition < patternRepeatCount) {
337 int repeatCount = std::min(patternRepeatCount - baseRepetition,
338 maxPatternRepetitionsInIndexBuffer);
339 int drawIndexCount = repeatCount * patternIndexCount;
340 // A patterned index buffer must contain indices in the range [0..vertexCount].
341 int minIndexValue = 0;
342 int maxIndexValue = patternVertexCount * repeatCount - 1;
343 this->drawIndexed(drawIndexCount, 0, minIndexValue, maxIndexValue,
344 patternVertexCount * baseRepetition + baseVertex);
345 baseRepetition += repeatCount;
346 }
347}