blob: 0e2e92361daea0840e396cc8a38a1ef62f7d05af [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 Phillips4133dc42020-03-11 15:55:55 -040019void GrMeshDrawOp::createProgramInfo(Target* target) {
20 this->createProgramInfo(&target->caps(),
21 target->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -040022 target->writeView(),
Robert Phillips4133dc42020-03-11 15:55:55 -040023 target->detachAppliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -040024 target->dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -050025 target->renderPassBarriers(),
26 target->colorLoadOp());
Robert Phillips4133dc42020-03-11 15:55:55 -040027}
28
Robert Phillips2669a7b2020-03-12 12:07:19 -040029// This onPrepareDraws implementation assumes the derived Op only has a single programInfo -
30// which is the majority of the cases.
31void GrMeshDrawOp::onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -050032 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -040033 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -040034 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -050035 GrXferBarrierFlags renderPassXferBarriers,
36 GrLoadOp colorLoadOp) {
Robert Phillips2669a7b2020-03-12 12:07:19 -040037 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
38
39 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -040040 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips2669a7b2020-03-12 12:07:19 -040041
Brian Salomon8afde5f2020-04-01 16:22:00 -040042 this->createProgramInfo(context->priv().caps(), arena, writeView,
Greg Daniel42dbca52020-11-20 10:22:43 -050043 std::move(appliedClip), dstProxyView, renderPassXferBarriers,
44 colorLoadOp);
Robert Phillips2669a7b2020-03-12 12:07:19 -040045
Robert Phillipscad8fba2020-03-20 15:39:29 -040046 // TODO: at this point we've created both the program info and desc in the recording context's
47 // arena. In the DDL case, it would be cool if 'recordProgramInfo' could return the
48 // pre-existing versions if the program has already been seen. We could then return the
49 // memory for the current copy to the arena.
Robert Phillips2669a7b2020-03-12 12:07:19 -040050 context->priv().recordProgramInfo(this->programInfo());
51}
52
Brian Salomonb9485722018-08-05 21:30:33 -040053//////////////////////////////////////////////////////////////////////////////
54
Brian Salomon7eae3e02018-08-07 14:02:38 +000055GrMeshDrawOp::PatternHelper::PatternHelper(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050056 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000057 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040058 int repeatCount, int maxRepetitions) {
Brian Salomon12d22642019-01-29 14:38:50 -050059 this->init(target, primitiveType, vertexStride, std::move(indexBuffer), verticesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040060 indicesPerRepetition, repeatCount, maxRepetitions);
Brian Salomon7eae3e02018-08-07 14:02:38 +000061}
62
63void GrMeshDrawOp::PatternHelper::init(Target* target, GrPrimitiveType primitiveType,
Brian Salomon12d22642019-01-29 14:38:50 -050064 size_t vertexStride, sk_sp<const GrBuffer> indexBuffer,
Brian Salomon7eae3e02018-08-07 14:02:38 +000065 int verticesPerRepetition, int indicesPerRepetition,
Robert Phillipsee08d522019-10-28 16:34:44 -040066 int repeatCount, int maxRepetitions) {
Brian Salomon7eae3e02018-08-07 14:02:38 +000067 SkASSERT(target);
68 if (!indexBuffer) {
69 return;
70 }
Brian Salomon12d22642019-01-29 14:38:50 -050071 sk_sp<const GrBuffer> vertexBuffer;
Brian Salomon7eae3e02018-08-07 14:02:38 +000072 int firstVertex;
73 int vertexCount = verticesPerRepetition * repeatCount;
74 fVertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
75 if (!fVertices) {
76 SkDebugf("Vertices could not be allocated for patterned rendering.");
77 return;
78 }
79 SkASSERT(vertexBuffer);
Chris Dalton79f336d2020-02-11 14:42:56 -070080 fMesh = target->allocMesh();
Robert Phillipscea290f2019-11-06 11:21:03 -050081 fPrimitiveType = primitiveType;
Robert Phillipsee08d522019-10-28 16:34:44 -040082
83 SkASSERT(maxRepetitions ==
84 static_cast<int>(indexBuffer->size() / (sizeof(uint16_t) * indicesPerRepetition)));
Chris Dalton37c7bdd2020-03-13 09:21:12 -060085 fMesh->setIndexedPatterned(std::move(indexBuffer), indicesPerRepetition, repeatCount,
86 maxRepetitions, std::move(vertexBuffer), verticesPerRepetition,
87 firstVertex);
Brian Salomon7eae3e02018-08-07 14:02:38 +000088}
89
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050090void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) const {
91 target->recordDraw(gp, fMesh, 1, fPrimitiveType);
Chris Dalton07cdcfc92019-02-26 11:13:22 -070092}
93
94void GrMeshDrawOp::PatternHelper::recordDraw(
Robert Phillips6c59fe42020-02-27 09:30:37 -050095 Target* target,
96 const GrGeometryProcessor* gp,
Chris Dalton304e14d2020-03-17 14:29:06 -060097 const GrSurfaceProxy* const primProcProxies[]) const {
98 target->recordDraw(gp, fMesh, 1, primProcProxies, fPrimitiveType);
Brian Salomon7eae3e02018-08-07 14:02:38 +000099}
100
101//////////////////////////////////////////////////////////////////////////////
102
103GrMeshDrawOp::QuadHelper::QuadHelper(Target* target, size_t vertexStride, int quadsToDraw) {
Robert Phillipsee08d522019-10-28 16:34:44 -0400104 sk_sp<const GrGpuBuffer> indexBuffer = target->resourceProvider()->refNonAAQuadIndexBuffer();
105 if (!indexBuffer) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000106 SkDebugf("Could not get quad index buffer.");
107 return;
108 }
Robert Phillipsee08d522019-10-28 16:34:44 -0400109 this->init(target, GrPrimitiveType::kTriangles, vertexStride, std::move(indexBuffer),
110 GrResourceProvider::NumVertsPerNonAAQuad(),
111 GrResourceProvider::NumIndicesPerNonAAQuad(), quadsToDraw,
112 GrResourceProvider::MaxNumNonAAQuads());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000113}