| 1 |
/* |
| 2 |
* Copyright 2021 Google LLC. |
| 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 |
#include "src/gpu/ganesh/ops/PathTessellateOp.h" |
| 8 |
#include <limits> |
| 9 |
|
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 12 |
#include "src/gpu/ganesh/GrAppliedClip.h" |
| 13 |
#include "src/gpu/ganesh/GrCaps.h" |
| 14 |
#include "src/gpu/ganesh/GrOpFlushState.h" |
| 15 |
#include "src/gpu/ganesh/GrPipeline.h" |
| 16 |
#include "src/gpu/ganesh/GrProcessorAnalysis.h" |
| 17 |
#include "src/gpu/ganesh/GrProgramInfo.h" |
| 18 |
#include "src/gpu/ganesh/GrRecordingContextPriv.h" |
| 19 |
#include "src/gpu/ganesh/GrRenderTargetProxy.h" |
| 20 |
#include "src/gpu/ganesh/GrShaderCaps.h" |
| 21 |
#include "src/gpu/ganesh/GrSurfaceProxyView.h" |
| 22 |
#include "src/gpu/ganesh/tessellate/GrPathTessellationShader.h" |
| 23 |
|
| 24 |
namespace skgpu::ganesh { |
| 25 |
|
| 26 |
void PathTessellateOp::visitProxies(const GrVisitProxyFunc& func) const { |
| 27 |
if (fTessellationProgram) { |
| 28 |
fTessellationProgram->pipeline().visitProxies(func); |
| 29 |
} else { |
| 30 |
fProcessors.visitProxies(func); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
GrProcessorSet::Analysis PathTessellateOp::finalize(const GrCaps& caps, |
| 35 |
const GrAppliedClip* clip, |
| 36 |
GrClampType clampType) { |
| 37 |
auto analysis = fProcessors.finalize(this->headDraw().fColor, |
| 38 |
GrProcessorAnalysisCoverage::kNone, |
| 39 |
clip, |
| 40 |
nullptr, |
| 41 |
caps, |
| 42 |
clampType, |
| 43 |
&this->headDraw().fColor); |
| 44 |
if (!analysis.usesLocalCoords()) { |
| 45 |
// Since we don't need local coords, we can transform on CPU instead of in the shader. This |
| 46 |
// gives us better batching potential. |
| 47 |
this->headDraw().fPathMatrix = fShaderMatrix; |
| 48 |
fShaderMatrix = SkMatrix::I(); |
| 49 |
} |
| 50 |
return analysis; |
| 51 |
} |
| 52 |
|
| 53 |
GrDrawOp::CombineResult PathTessellateOp::onCombineIfPossible(GrOp* grOp, |
| 54 |
SkArenaAlloc*, |
| 55 |
const GrCaps&) { |
| 56 |
auto* op = grOp->cast<PathTessellateOp>(); |
| 57 |
bool verbCountOverflow = std::numeric_limits<int>::max() - fTotalCombinedPathVerbCnt < |
| 58 |
op->fTotalCombinedPathVerbCnt; |
| 59 |
bool canMerge = fAAType == op->fAAType && |
| 60 |
fStencil == op->fStencil && |
| 61 |
fProcessors == op->fProcessors && |
| 62 |
fShaderMatrix == op->fShaderMatrix && |
| 63 |
!verbCountOverflow; |
| 64 |
if (canMerge) { |
| 65 |
fTotalCombinedPathVerbCnt += op->fTotalCombinedPathVerbCnt; |
| 66 |
fPatchAttribs |= op->fPatchAttribs; |
| 67 |
|
| 68 |
if (!(fPatchAttribs & PatchAttribs::kColor) && |
| 69 |
this->headDraw().fColor != op->headDraw().fColor) { |
| 70 |
// Color is no longer uniform. Move it into patch attribs. |
| 71 |
fPatchAttribs |= PatchAttribs::kColor; |
| 72 |
} |
| 73 |
|
| 74 |
*fPathDrawTail = op->fPathDrawList; |
| 75 |
fPathDrawTail = op->fPathDrawTail; |
| 76 |
return CombineResult::kMerged; |
| 77 |
} |
| 78 |
|
| 79 |
return CombineResult::kCannotCombine; |
| 80 |
} |
| 81 |
|
| 82 |
void PathTessellateOp::prepareTessellator(const GrTessellationShader::ProgramArgs& args, |
| 83 |
GrAppliedClip&& appliedClip) { |
| 84 |
SkASSERT(!fTessellator); |
| 85 |
SkASSERT(!fTessellationProgram); |
| 86 |
auto* pipeline = GrTessellationShader::MakePipeline(args, fAAType, std::move(appliedClip), |
| 87 |
std::move(fProcessors)); |
| 88 |
fTessellator = PathWedgeTessellator::Make(args.fArena, |
| 89 |
args.fCaps->shaderCaps()->fInfinitySupport, |
| 90 |
fPatchAttribs); |
| 91 |
auto* tessShader = GrPathTessellationShader::Make(*args.fCaps->shaderCaps(), |
| 92 |
args.fArena, |
| 93 |
fShaderMatrix, |
| 94 |
this->headDraw().fColor, |
| 95 |
fTessellator->patchAttribs()); |
| 96 |
fTessellationProgram = GrTessellationShader::MakeProgram(args, tessShader, pipeline, fStencil); |
| 97 |
} |
| 98 |
|
| 99 |
void PathTessellateOp::onPrePrepare(GrRecordingContext* context, |
| 100 |
const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 101 |
const GrDstProxyView& dstProxyView, |
| 102 |
GrXferBarrierFlags renderPassXferBarriers, |
| 103 |
GrLoadOp colorLoadOp) { |
| 104 |
// DMSAA is not supported on DDL. |
| 105 |
bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1; |
| 106 |
this->prepareTessellator({context->priv().recordTimeAllocator(), writeView, usesMSAASurface, |
| 107 |
&dstProxyView, renderPassXferBarriers, colorLoadOp, |
| 108 |
context->priv().caps()}, |
| 109 |
(clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 110 |
SkASSERT(fTessellationProgram); |
| 111 |
context->priv().recordProgramInfo(fTessellationProgram); |
| 112 |
} |
| 113 |
|
| 114 |
void PathTessellateOp::onPrepare(GrOpFlushState* flushState) { |
| 115 |
if (!fTessellator) { |
| 116 |
this->prepareTessellator({flushState->allocator(), flushState->writeView(), |
| 117 |
flushState->usesMSAASurface(), &flushState->dstProxyView(), |
| 118 |
flushState->renderPassBarriers(), flushState->colorLoadOp(), |
| 119 |
&flushState->caps()}, flushState->detachAppliedClip()); |
| 120 |
SkASSERT(fTessellator); |
| 121 |
} |
| 122 |
fTessellator->prepare(flushState, |
| 123 |
fShaderMatrix, |
| 124 |
*fPathDrawList, |
| 125 |
fTotalCombinedPathVerbCnt); |
| 126 |
} |
| 127 |
|
| 128 |
void PathTessellateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
| 129 |
SkASSERT(fTessellator); |
| 130 |
SkASSERT(fTessellationProgram); |
| 131 |
flushState->bindPipelineAndScissorClip(*fTessellationProgram, this->bounds()); |
| 132 |
flushState->bindTextures(fTessellationProgram->geomProc(), nullptr, |
| 133 |
fTessellationProgram->pipeline()); |
| 134 |
fTessellator->draw(flushState); |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace skgpu::ganesh |