blob: 486a9f14dddafb406b8fc9d03bb9f15c2f5cfb04 [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()) {
68 SkASSERT(this->gpu()->caps()->instanceAttribSupport());
69 }
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 }
80 if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
81 SkASSERT(this->gpu()->caps()->shaderCaps()->tessellationSupport());
82 }
Robert Phillips2d8a95e2019-10-10 12:50:22 -040083 programInfo.checkAllInstantiated();
84 programInfo.checkMSAAAndMIPSAreResolved();
Robert Phillips12c46292019-04-23 07:36:17 -040085#endif
Robert Phillipsa91e0b72017-05-01 13:12:20 -040086
Chris Daltonb60c8a22020-03-31 17:24:22 -060087 this->resetActiveBuffers();
88
Robert Phillips901aff02019-10-08 12:32:56 -040089 if (programInfo.primProc().numVertexAttributes() > this->gpu()->caps()->maxVertexAttributes()) {
Chris Dalton4386ad12020-02-19 16:42:06 -070090 fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind;
91 return;
egdaniel9cb63402016-06-23 08:37:05 -070092 }
Chris Dalton4386ad12020-02-19 16:42:06 -070093
94 if (!this->onBindPipeline(programInfo, drawBounds)) {
95 fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind;
96 return;
97 }
Robert Phillips901aff02019-10-08 12:32:56 -040098
Chris Dalton8c4cafd2019-04-15 19:14:36 -060099#ifdef SK_DEBUG
Robert Phillips901aff02019-10-08 12:32:56 -0400100 GrProcessor::CustomFeatures processorFeatures = programInfo.requestedFeatures();
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600101 if (GrProcessor::CustomFeatures::kSampleLocations & processorFeatures) {
102 // Verify we always have the same sample pattern key, regardless of graphics state.
103 SkASSERT(this->gpu()->findOrAssignSamplePatternKey(fRenderTarget)
104 == fRenderTarget->renderTargetPriv().getSamplePatternKey());
105 }
Chris Dalton2e7ed262020-02-21 15:17:59 -0700106 fScissorStatus = (programInfo.pipeline().isScissorTestEnabled()) ?
107 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
108 bool hasTextures = (programInfo.primProc().numTextureSamplers() > 0);
109 if (!hasTextures) {
110 programInfo.pipeline().visitProxies([&hasTextures](GrSurfaceProxy*, GrMipMapped) {
111 hasTextures = true;
112 });
113 }
114 fTextureBindingStatus = (hasTextures) ?
115 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
Chris Daltonded43702020-03-02 11:45:27 -0700116 fHasIndexBuffer = false;
117 fInstanceBufferStatus = (programInfo.primProc().hasInstanceAttributes()) ?
118 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
119 fVertexBufferStatus = (programInfo.primProc().hasVertexAttributes()) ?
120 DynamicStateStatus::kUninitialized : DynamicStateStatus::kDisabled;
Chris Dalton8c4cafd2019-04-15 19:14:36 -0600121#endif
Chris Dalton4386ad12020-02-19 16:42:06 -0700122
123 fDrawPipelineStatus = DrawPipelineStatus::kOk;
Chris Dalton2e7ed262020-02-21 15:17:59 -0700124 fXferBarrierType = programInfo.pipeline().xferBarrierType(fRenderTarget->asTexture(),
125 *this->gpu()->caps());
126}
127
128void GrOpsRenderPass::setScissorRect(const SkIRect& scissor) {
129 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
130 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
131 return;
132 }
133 SkASSERT(DynamicStateStatus::kDisabled != fScissorStatus);
134 this->onSetScissorRect(scissor);
135 SkDEBUGCODE(fScissorStatus = DynamicStateStatus::kConfigured);
136}
137
Chris Daltondb20afc2020-03-05 12:13:53 -0700138void GrOpsRenderPass::bindTextures(const GrPrimitiveProcessor& primProc,
139 const GrSurfaceProxy* const primProcTextures[],
140 const GrPipeline& pipeline) {
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700141#ifdef SK_DEBUG
142 SkASSERT((primProc.numTextureSamplers() > 0) == SkToBool(primProcTextures));
143 for (int i = 0; i < primProc.numTextureSamplers(); ++i) {
Chris Dalton3bf2f3a2020-03-17 11:48:23 -0600144 const auto& sampler = primProc.textureSampler(i);
145 const GrSurfaceProxy* proxy = primProcTextures[i];
146 SkASSERT(proxy);
147 SkASSERT(proxy->backendFormat() == sampler.backendFormat());
148 SkASSERT(proxy->backendFormat().textureType() == sampler.backendFormat().textureType());
149
150 const GrTexture* tex = proxy->peekTexture();
151 SkASSERT(tex);
152 if (GrSamplerState::Filter::kMipMap == sampler.samplerState().filter() &&
153 (tex->width() != 1 || tex->height() != 1)) {
154 // There are some cases where we might be given a non-mipmapped texture with a mipmap
155 // filter. See skbug.com/7094.
156 SkASSERT(tex->texturePriv().mipMapped() != GrMipMapped::kYes ||
157 !tex->texturePriv().mipMapsAreDirty());
158 }
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700159 }
160#endif
161
Chris Dalton2e7ed262020-02-21 15:17:59 -0700162 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
163 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
164 return;
165 }
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700166
Chris Dalton2e7ed262020-02-21 15:17:59 -0700167 // Don't assert on fTextureBindingStatus. onBindTextures() just turns into a no-op when there
168 // aren't any textures, and it's hard to tell from the GrPipeline whether there are any. For
169 // many clients it is easier to just always call this method.
Chris Daltondb20afc2020-03-05 12:13:53 -0700170 if (!this->onBindTextures(primProc, primProcTextures, pipeline)) {
Chris Dalton2e7ed262020-02-21 15:17:59 -0700171 fDrawPipelineStatus = DrawPipelineStatus::kFailedToBind;
172 return;
173 }
Chris Dalton1b1b0d52020-03-03 12:00:59 -0700174
Chris Dalton2e7ed262020-02-21 15:17:59 -0700175 SkDEBUGCODE(fTextureBindingStatus = DynamicStateStatus::kConfigured);
Chris Dalton4386ad12020-02-19 16:42:06 -0700176}
177
Chris Daltonded43702020-03-02 11:45:27 -0700178void GrOpsRenderPass::bindBuffers(const GrBuffer* indexBuffer, const GrBuffer* instanceBuffer,
179 const GrBuffer* vertexBuffer, GrPrimitiveRestart primRestart) {
180 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
181 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
182 return;
183 }
184
185#ifdef SK_DEBUG
186 if (indexBuffer) {
187 fHasIndexBuffer = true;
188 }
189
190 SkASSERT((DynamicStateStatus::kDisabled == fInstanceBufferStatus) != SkToBool(instanceBuffer));
191 if (instanceBuffer) {
192 fInstanceBufferStatus = DynamicStateStatus::kConfigured;
193 }
194
195 SkASSERT((DynamicStateStatus::kDisabled == fVertexBufferStatus) != SkToBool(vertexBuffer));
196 if (vertexBuffer) {
197 fVertexBufferStatus = DynamicStateStatus::kConfigured;
198 }
199
200 if (GrPrimitiveRestart::kYes == primRestart) {
201 SkASSERT(this->gpu()->caps()->usePrimitiveRestart());
202 }
203#endif
204
205 this->onBindBuffers(indexBuffer, instanceBuffer, vertexBuffer, primRestart);
206}
207
Chris Dalton33db9fc2020-02-25 18:52:12 -0700208bool GrOpsRenderPass::prepareToDraw() {
Chris Dalton4386ad12020-02-19 16:42:06 -0700209 if (DrawPipelineStatus::kOk != fDrawPipelineStatus) {
210 SkASSERT(DrawPipelineStatus::kNotConfigured != fDrawPipelineStatus);
211 this->gpu()->stats()->incNumFailedDraws();
Chris Dalton33db9fc2020-02-25 18:52:12 -0700212 return false;
Chris Dalton4386ad12020-02-19 16:42:06 -0700213 }
Chris Dalton2e7ed262020-02-21 15:17:59 -0700214 SkASSERT(DynamicStateStatus::kUninitialized != fScissorStatus);
215 SkASSERT(DynamicStateStatus::kUninitialized != fTextureBindingStatus);
Chris Dalton4386ad12020-02-19 16:42:06 -0700216
Chris Dalton2e7ed262020-02-21 15:17:59 -0700217 if (kNone_GrXferBarrierType != fXferBarrierType) {
218 this->gpu()->xferBarrier(fRenderTarget, fXferBarrierType);
Chris Dalton4386ad12020-02-19 16:42:06 -0700219 }
Chris Dalton33db9fc2020-02-25 18:52:12 -0700220 return true;
221}
222
Chris Daltonded43702020-03-02 11:45:27 -0700223void GrOpsRenderPass::draw(int vertexCount, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -0700224 if (!this->prepareToDraw()) {
225 return;
226 }
Chris Daltonded43702020-03-02 11:45:27 -0700227 SkASSERT(!fHasIndexBuffer);
228 SkASSERT(DynamicStateStatus::kConfigured != fInstanceBufferStatus);
229 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
230 this->onDraw(vertexCount, baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -0700231}
232
Chris Daltonded43702020-03-02 11:45:27 -0700233void GrOpsRenderPass::drawIndexed(int indexCount, int baseIndex, uint16_t minIndexValue,
234 uint16_t maxIndexValue, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -0700235 if (!this->prepareToDraw()) {
236 return;
237 }
Chris Daltonded43702020-03-02 11:45:27 -0700238 SkASSERT(fHasIndexBuffer);
239 SkASSERT(DynamicStateStatus::kConfigured != fInstanceBufferStatus);
240 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
241 this->onDrawIndexed(indexCount, baseIndex, minIndexValue, maxIndexValue, baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -0700242}
243
Chris Daltonded43702020-03-02 11:45:27 -0700244void GrOpsRenderPass::drawInstanced(int instanceCount, int baseInstance, int vertexCount,
Chris Dalton33db9fc2020-02-25 18:52:12 -0700245 int baseVertex) {
246 if (!this->prepareToDraw()) {
247 return;
248 }
Chris Daltonded43702020-03-02 11:45:27 -0700249 SkASSERT(!fHasIndexBuffer);
250 SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus);
251 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
252 this->onDrawInstanced(instanceCount, baseInstance, vertexCount, baseVertex);
Chris Dalton33db9fc2020-02-25 18:52:12 -0700253}
254
Chris Daltonded43702020-03-02 11:45:27 -0700255void GrOpsRenderPass::drawIndexedInstanced(int indexCount, int baseIndex, int instanceCount,
256 int baseInstance, int baseVertex) {
Chris Dalton33db9fc2020-02-25 18:52:12 -0700257 if (!this->prepareToDraw()) {
258 return;
259 }
Chris Daltonded43702020-03-02 11:45:27 -0700260 SkASSERT(fHasIndexBuffer);
261 SkASSERT(DynamicStateStatus::kUninitialized != fInstanceBufferStatus);
262 SkASSERT(DynamicStateStatus::kUninitialized != fVertexBufferStatus);
263 this->onDrawIndexedInstanced(indexCount, baseIndex, instanceCount, baseInstance, baseVertex);
egdaniel9cb63402016-06-23 08:37:05 -0700264}
Chris Daltonbb768422020-03-12 12:13:29 -0600265
266void GrOpsRenderPass::drawIndexPattern(int patternIndexCount, int patternRepeatCount,
267 int maxPatternRepetitionsInIndexBuffer,
268 int patternVertexCount, int baseVertex) {
269 int baseRepetition = 0;
270 while (baseRepetition < patternRepeatCount) {
271 int repeatCount = std::min(patternRepeatCount - baseRepetition,
272 maxPatternRepetitionsInIndexBuffer);
273 int drawIndexCount = repeatCount * patternIndexCount;
274 // A patterned index buffer must contain indices in the range [0..vertexCount].
275 int minIndexValue = 0;
276 int maxIndexValue = patternVertexCount * repeatCount - 1;
277 this->drawIndexed(drawIndexCount, 0, minIndexValue, maxIndexValue,
278 patternVertexCount * baseRepetition + baseVertex);
279 baseRepetition += repeatCount;
280 }
281}