blob: 8ea25d502701a9e0d1f199eb40d6feebde8da6ad [file] [log] [blame]
bsalomon16b99132015-08-13 14:55:50 -07001/*
2 * Copyright 2015 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrMeshDrawOp.h"
bsalomon16b99132015-08-13 14:55:50 -07009
Greg Daniel2d41d0d2019-08-26 11:08:51 -040010#include "src/gpu/GrOpFlushState.h"
11#include "src/gpu/GrOpsRenderPass.h"
Robert Phillips2669a7b2020-03-12 12:07:19 -040012#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040013#include "src/gpu/GrResourceProvider.h"
14
Brian Salomon7dc6e752017-11-02 11:34:51 -040015GrMeshDrawOp::GrMeshDrawOp(uint32_t classID) : INHERITED(classID) {}
bsalomon16b99132015-08-13 14:55:50 -070016
Brian Salomon29b60c92017-10-31 14:42:10 -040017void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
bsalomon75398562015-08-17 12:55:38 -070018
Robert Phillips71143952021-06-17 14:55:07 -040019void GrMeshDrawOp::createProgramInfo(GrMeshDrawTarget* target) {
Robert Phillips4133dc42020-03-11 15:55:55 -040020 this->createProgramInfo(&target->caps(),
21 target->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -040022 target->writeView(),
Chris Dalton6aaf00f2021-07-13 13:26:39 -060023 target->usesMSAASurface(),
Robert Phillips4133dc42020-03-11 15:55:55 -040024 target->detachAppliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -040025 target->dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -050026 target->renderPassBarriers(),
27 target->colorLoadOp());
Robert Phillips4133dc42020-03-11 15:55:55 -040028}
29
Robert Phillips1a82a4e2021-07-01 10:27:44 -040030bool GrMeshDrawOp::CombinedQuadCountWillOverflow(GrAAType aaType,
31 bool willBeUpgradedToAA,
32 int combinedQuadCount) {
33 bool willBeAA = (aaType == GrAAType::kCoverage) || willBeUpgradedToAA;
34
35 return combinedQuadCount > (willBeAA ? GrResourceProvider::MaxNumAAQuads()
36 : GrResourceProvider::MaxNumNonAAQuads());
37}
38
Robert Phillips2669a7b2020-03-12 12:07:19 -040039// This onPrepareDraws implementation assumes the derived Op only has a single programInfo -
40// which is the majority of the cases.
41void GrMeshDrawOp::onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -050042 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -040043 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -040044 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -050045 GrXferBarrierFlags renderPassXferBarriers,
46 GrLoadOp colorLoadOp) {
Robert Phillips2669a7b2020-03-12 12:07:19 -040047 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
48
Chris Dalton6aaf00f2021-07-13 13:26:39 -060049 // http://skbug.com/12201 -- DDL does not yet support DMSAA.
50 bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
51
Robert Phillips2669a7b2020-03-12 12:07:19 -040052 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -040053 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips2669a7b2020-03-12 12:07:19 -040054
Chris Dalton6aaf00f2021-07-13 13:26:39 -060055 this->createProgramInfo(context->priv().caps(), arena, writeView, usesMSAASurface,
Greg Daniel42dbca52020-11-20 10:22:43 -050056 std::move(appliedClip), dstProxyView, renderPassXferBarriers,
57 colorLoadOp);
Robert Phillips2669a7b2020-03-12 12:07:19 -040058
Robert Phillipscad8fba2020-03-20 15:39:29 -040059 // TODO: at this point we've created both the program info and desc in the recording context's
60 // arena. In the DDL case, it would be cool if 'recordProgramInfo' could return the
61 // pre-existing versions if the program has already been seen. We could then return the
62 // memory for the current copy to the arena.
Robert Phillips2669a7b2020-03-12 12:07:19 -040063 context->priv().recordProgramInfo(this->programInfo());
64}
65
Brian Salomonb9485722018-08-05 21:30:33 -040066//////////////////////////////////////////////////////////////////////////////
67
Robert Phillips71143952021-06-17 14:55:07 -040068GrMeshDrawOp::PatternHelper::PatternHelper(GrMeshDrawTarget* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050069 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000070 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040071 int repeatCount, int maxRepetitions) {
Brian Salomon12d22642019-01-29 14:38:50 -050072 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040073 indicesPerRepetition, repeatCount, maxRepetitions);
Brian Salomon7eae3e02018-08-07 14:02:38 +000074}
75
Robert Phillips71143952021-06-17 14:55:07 -040076void GrMeshDrawOp::PatternHelper::init(GrMeshDrawTarget* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050077 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000078 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040079 int repeatCount, int maxRepetitions) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000080 SkASSERT(target);
81 if (!indexBuffer) {
82 return;
83 }
Brian Salomon12d22642019-01-29 14:38:50 -050084 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000085 int firstVertex;
86 int vertexCount = verticesPerRepetition * repeatCount;
87 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
88 if (!fVertices) {
89 SkDebugf("Vertices could not be allocated for patterned rendering.");
90 return;
91 }
92 SkASSERT(vertexBuffer);
Chris Dalton79f336d2020-02-11 14:42:56 -070093 fMesh = target->allocMesh();
Robert Phillipscea290f2019-11-06 11:21:03 -050094 fPrimitiveType = primitiveType;
Robert Phillipsee08d522019-10-28 16:34:44 -040095
96 SkASSERT(maxRepetitions ==
97 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Chris Dalton37c7bdd2020-03-13 09:21:12 -060098 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
99 maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
100 firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000101}
102
Robert Phillips71143952021-06-17 14:55:07 -0400103void GrMeshDrawOp::PatternHelper::recordDraw(GrMeshDrawTarget* target,
104 const GrGeometryProcessor* gp) const {
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500105 target->recordDraw(gp, fMesh, 1, fPrimitiveType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700106}
107
108void GrMeshDrawOp::PatternHelper::recordDraw(
Robert Phillips71143952021-06-17 14:55:07 -0400109 GrMeshDrawTarget* target,
Robert Phillips6c59fe42020-02-27 09:30:37 -0500110 const GrGeometryProcessor* gp,
Chris Dalton304e14d2020-03-17 14:29:06 -0600111 const GrSurfaceProxy* const primProcProxies[]) const {
112 target->recordDraw(gp, fMesh, 1, primProcProxies, fPrimitiveType);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000113}
114
115//////////////////////////////////////////////////////////////////////////////
116
Robert Phillips71143952021-06-17 14:55:07 -0400117GrMeshDrawOp::QuadHelper::QuadHelper(GrMeshDrawTarget* target,
118 size_t vertexStride,
119 int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400120 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
121 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000122 SkDebugf("Could not get quad index buffer.");
123 return;
124 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400125 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
126 GrResourceProvider::NumVertsPerNonAAQuad(),
127 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
128 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000129}