Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 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 | |
Robert Phillips | e453fa0 | 2021-08-19 14:57:05 -0400 | [diff] [blame] | 8 | #include "src/gpu/ops/PathStencilCoverOp.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 11 | #include "src/gpu/GrGpu.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 12 | #include "src/gpu/GrOpFlushState.h" |
| 13 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrResourceProvider.h" |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 16 | #include "src/gpu/glsl/GrGLSLVarying.h" |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 17 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Robert Phillips | 06273bc | 2021-08-11 15:43:50 -0400 | [diff] [blame] | 18 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrPathCurveTessellator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrPathWedgeTessellator.h" |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame] | 22 | #include "src/gpu/tessellate/shaders/GrPathTessellationShader.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 23 | |
Robert Phillips | 832f3fb | 2021-08-18 13:05:15 -0400 | [diff] [blame] | 24 | using PathFlags = GrTessellationPathFlags; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 25 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | // Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme |
| 29 | // edges of the path. |
| 30 | // NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix. |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 31 | class BoundingBoxShader : public GrGeometryProcessor { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 32 | public: |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 33 | BoundingBoxShader(SkPMColor4f color, const GrShaderCaps& shaderCaps) |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 34 | : GrGeometryProcessor(kTessellate_BoundingBoxShader_ClassID) |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 35 | , fColor(color) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 36 | if (!shaderCaps.vertexIDSupport()) { |
| 37 | constexpr static Attribute kUnitCoordAttrib("unitCoord", kFloat2_GrVertexAttribType, |
| 38 | kFloat2_GrSLType); |
| 39 | this->setVertexAttributes(&kUnitCoordAttrib, 1); |
| 40 | } |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 41 | constexpr static Attribute kInstanceAttribs[] = { |
| 42 | {"matrix2d", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 43 | {"translate", kFloat2_GrVertexAttribType, kFloat2_GrSLType}, |
| 44 | {"pathBounds", kFloat4_GrVertexAttribType, kFloat4_GrSLType} |
| 45 | }; |
| 46 | this->setInstanceAttributes(kInstanceAttribs, SK_ARRAY_COUNT(kInstanceAttribs)); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | private: |
Chris Dalton | b63711a | 2021-06-01 14:52:02 -0600 | [diff] [blame] | 50 | const char* name() const final { return "tessellate_BoundingBoxShader"; } |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 51 | void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 52 | std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final; |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 53 | |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 54 | const SkPMColor4f fColor; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 55 | }; |
| 56 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 57 | std::unique_ptr<GrGeometryProcessor::ProgramImpl> BoundingBoxShader::makeProgramImpl( |
| 58 | const GrShaderCaps&) const { |
| 59 | class Impl : public ProgramImpl { |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 60 | public: |
| 61 | void setData(const GrGLSLProgramDataManager& pdman, |
| 62 | const GrShaderCaps&, |
| 63 | const GrGeometryProcessor& gp) override { |
| 64 | const SkPMColor4f& color = gp.cast<BoundingBoxShader>().fColor; |
| 65 | pdman.set4f(fColorUniform, color.fR, color.fG, color.fB, color.fA); |
| 66 | } |
| 67 | |
| 68 | private: |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 69 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 70 | args.fVaryingHandler->emitAttributes(args.fGeomProc); |
| 71 | |
| 72 | // Vertex shader. |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 73 | if (args.fShaderCaps->vertexIDSupport()) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 74 | // If we don't have sk_VertexID support then "unitCoord" already came in as a vertex |
| 75 | // attrib. |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 76 | args.fVertBuilder->codeAppend(R"( |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 77 | float2 unitCoord = float2(sk_VertexID & 1, sk_VertexID >> 1);)"); |
| 78 | } |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 79 | args.fVertBuilder->codeAppend(R"( |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 80 | // Bloat the bounding box by 1/4px to be certain we will reset every stencil value. |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 81 | float2x2 M_ = inverse(float2x2(matrix2d)); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 82 | float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25; |
| 83 | |
| 84 | // Find the vertex position. |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 85 | float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, unitCoord); |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 86 | float2 vertexpos = float2x2(matrix2d) * localcoord + translate;)"); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 87 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
| 88 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 89 | |
| 90 | // Fragment shader. |
| 91 | const char* color; |
| 92 | fColorUniform = args.fUniformHandler->addUniform(nullptr, kFragment_GrShaderFlag, |
| 93 | kHalf4_GrSLType, "color", &color); |
| 94 | args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor, color); |
| 95 | args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 96 | } |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 97 | |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 98 | GrGLSLUniformHandler::UniformHandle fColorUniform; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 99 | }; |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 100 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 101 | return std::make_unique<Impl>(); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 102 | } |
| 103 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 104 | } // anonymous namespace |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 105 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 106 | namespace skgpu::v1 { |
| 107 | |
| 108 | void PathStencilCoverOp::visitProxies(const GrVisitProxyFunc& func) const { |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 109 | if (fCoverBBoxProgram) { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 110 | fCoverBBoxProgram->pipeline().visitProxies(func); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 111 | } else { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 112 | fProcessors.visitProxies(func); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 116 | GrDrawOp::FixedFunctionFlags PathStencilCoverOp::fixedFunctionFlags() const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 117 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 118 | if (fAAType != GrAAType::kNone) { |
| 119 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 120 | } |
| 121 | return flags; |
| 122 | } |
| 123 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 124 | GrProcessorSet::Analysis PathStencilCoverOp::finalize(const GrCaps& caps, |
| 125 | const GrAppliedClip* clip, |
| 126 | GrClampType clampType) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 127 | return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps, |
| 128 | clampType, &fColor); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 131 | void PathStencilCoverOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args, |
| 132 | GrAppliedClip&& appliedClip) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 133 | SkASSERT(!fTessellator); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 134 | SkASSERT(!fStencilFanProgram); |
| 135 | SkASSERT(!fStencilPathProgram); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 136 | SkASSERT(!fCoverBBoxProgram); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 137 | |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 138 | // We transform paths on the CPU. This allows for better batching. |
| 139 | const SkMatrix& shaderMatrix = SkMatrix::I(); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 140 | const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline( |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 141 | args, fAAType, fPathFlags, appliedClip.hardClip()); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 142 | const GrUserStencilSettings* stencilSettings = GrPathTessellationShader::StencilPathSettings( |
| 143 | GrFillRuleForPathFillType(this->pathFillType())); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 144 | |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 145 | if (fTotalCombinedPathVerbCnt > 50 && |
| 146 | this->bounds().height() * this->bounds().width() > 256 * 256) { |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 147 | // Large complex paths do better with a dedicated triangle shader for the inner fan. |
| 148 | // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us |
| 149 | // to make sure it has an efficient middle-out topology. |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 150 | auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(args.fArena, |
| 151 | shaderMatrix, |
| 152 | SK_PMColor4fTRANSPARENT); |
| 153 | fStencilFanProgram = GrTessellationShader::MakeProgram(args, |
| 154 | shader, |
| 155 | stencilPipeline, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 156 | stencilSettings); |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 157 | fTessellator = GrPathCurveTessellator::Make(args.fArena, |
| 158 | shaderMatrix, |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 159 | SK_PMColor4fTRANSPARENT, |
| 160 | GrPathCurveTessellator::DrawInnerFan::kNo, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 161 | fTotalCombinedPathVerbCnt, |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 162 | *stencilPipeline, |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 163 | *args.fCaps); |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 164 | } else { |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 165 | fTessellator = GrPathWedgeTessellator::Make(args.fArena, |
| 166 | shaderMatrix, |
| 167 | SK_PMColor4fTRANSPARENT, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 168 | fTotalCombinedPathVerbCnt, |
Chris Dalton | 6966981 | 2021-07-27 10:00:12 -0600 | [diff] [blame] | 169 | *stencilPipeline, |
| 170 | *args.fCaps); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 171 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 172 | fStencilPathProgram = GrTessellationShader::MakeProgram(args, |
| 173 | fTessellator->shader(), |
| 174 | stencilPipeline, |
| 175 | stencilSettings); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 176 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 177 | if (!(fPathFlags & PathFlags::kStencilOnly)) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 178 | // Create a program that draws a bounding box over the path and fills its stencil coverage |
| 179 | // into the color buffer. |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 180 | auto* bboxShader = args.fArena->make<BoundingBoxShader>(fColor, *args.fCaps->shaderCaps()); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 181 | auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType, |
| 182 | std::move(appliedClip), |
| 183 | std::move(fProcessors)); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 184 | auto* bboxStencil = GrPathTessellationShader::TestAndResetStencilSettings( |
| 185 | SkPathFillType_IsInverse(this->pathFillType())); |
Chris Dalton | 8aec124 | 2021-07-19 11:10:45 -0600 | [diff] [blame] | 186 | fCoverBBoxProgram = GrSimpleMeshDrawOpHelper::CreateProgramInfo( |
| 187 | args.fArena, |
| 188 | bboxPipeline, |
| 189 | args.fWriteView, |
| 190 | bboxShader, |
| 191 | GrPrimitiveType::kTriangleStrip, |
| 192 | args.fXferBarrierFlags, |
| 193 | args.fColorLoadOp, |
| 194 | bboxStencil); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 198 | void PathStencilCoverOp::onPrePrepare(GrRecordingContext* context, |
| 199 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 200 | const GrDstProxyView& dstProxyView, |
| 201 | GrXferBarrierFlags renderPassXferBarriers, |
| 202 | GrLoadOp colorLoadOp) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 203 | this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 204 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 205 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 206 | if (fStencilFanProgram) { |
| 207 | context->priv().recordProgramInfo(fStencilFanProgram); |
| 208 | } |
| 209 | if (fStencilPathProgram) { |
| 210 | context->priv().recordProgramInfo(fStencilPathProgram); |
| 211 | } |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 212 | if (fCoverBBoxProgram) { |
| 213 | context->priv().recordProgramInfo(fCoverBBoxProgram); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 217 | GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 218 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 219 | void PathStencilCoverOp::onPrepare(GrOpFlushState* flushState) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 220 | if (!fTessellator) { |
| 221 | this->prePreparePrograms({flushState->allocator(), flushState->writeView(), |
| 222 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 223 | flushState->colorLoadOp(), &flushState->caps()}, |
| 224 | flushState->detachAppliedClip()); |
| 225 | if (!fTessellator) { |
| 226 | return; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (fStencilFanProgram) { |
| 231 | // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a |
| 232 | // middle-out topology. |
| 233 | GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 234 | int maxCombinedFanEdges = |
| 235 | GrPathTessellator::MaxCombinedFanEdgesInPathDrawList(fTotalCombinedPathVerbCnt); |
| 236 | // A single n-sided polygon is fanned by n-2 triangles. Multiple polygons with a combined |
| 237 | // edge count of n are fanned by strictly fewer triangles. |
| 238 | int maxTrianglesInFans = std::max(maxCombinedFanEdges - 2, 0); |
| 239 | GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxTrianglesInFans * 3); |
| 240 | int fanTriangleCount = 0; |
| 241 | for (auto [pathMatrix, path] : *fPathDrawList) { |
| 242 | int numTrianglesWritten; |
| 243 | triangleVertexWriter = GrMiddleOutPolygonTriangulator::WritePathInnerFan( |
| 244 | std::move(triangleVertexWriter), |
| 245 | 0, |
| 246 | 0, |
| 247 | pathMatrix, |
| 248 | path, |
| 249 | &numTrianglesWritten); |
| 250 | fanTriangleCount += numTrianglesWritten; |
| 251 | } |
| 252 | SkASSERT(fanTriangleCount <= maxTrianglesInFans); |
| 253 | fFanVertexCount = fanTriangleCount * 3; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 254 | vertexAlloc.unlock(fFanVertexCount); |
| 255 | } |
| 256 | |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 257 | fTessellator->prepare(flushState, this->bounds(), *fPathDrawList, fTotalCombinedPathVerbCnt); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 258 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 259 | if (fCoverBBoxProgram) { |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 260 | size_t instanceStride = fCoverBBoxProgram->geomProc().instanceStride(); |
| 261 | GrVertexWriter vertexWriter = flushState->makeVertexSpace( |
| 262 | instanceStride, |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 263 | fPathCount, |
Chris Dalton | e909e1e | 2021-07-27 13:23:18 -0600 | [diff] [blame] | 264 | &fBBoxBuffer, |
| 265 | &fBBoxBaseInstance); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 266 | SkDEBUGCODE(int pathCount = 0;) |
| 267 | for (auto [pathMatrix, path] : *fPathDrawList) { |
| 268 | SkDEBUGCODE(auto end = vertexWriter.makeOffset(instanceStride)); |
| 269 | vertexWriter.write(pathMatrix.getScaleX(), |
| 270 | pathMatrix.getSkewY(), |
| 271 | pathMatrix.getSkewX(), |
| 272 | pathMatrix.getScaleY(), |
| 273 | pathMatrix.getTranslateX(), |
| 274 | pathMatrix.getTranslateY()); |
| 275 | if (path.isInverseFillType()) { |
| 276 | // Fill the entire backing store to make sure we clear every stencil value back to |
| 277 | // 0. If there is a scissor it will have already clipped the stencil draw. |
| 278 | auto rtBounds = |
| 279 | flushState->writeView().asRenderTargetProxy()->backingStoreBoundsRect(); |
| 280 | SkASSERT(rtBounds == fOriginalDrawBounds); |
| 281 | SkRect pathSpaceRTBounds; |
| 282 | if (SkMatrixPriv::InverseMapRect(pathMatrix, &pathSpaceRTBounds, rtBounds)) { |
| 283 | vertexWriter.write(pathSpaceRTBounds); |
| 284 | } else { |
| 285 | vertexWriter.write(path.getBounds()); |
| 286 | } |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 287 | } else { |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 288 | vertexWriter.write(path.getBounds()); |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 289 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 290 | SkASSERT(vertexWriter == end); |
| 291 | SkDEBUGCODE(++pathCount;) |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 292 | } |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 293 | SkASSERT(pathCount == fPathCount); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 294 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 295 | |
| 296 | if (!flushState->caps().shaderCaps()->vertexIDSupport()) { |
| 297 | constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}}; |
| 298 | |
| 299 | GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey); |
| 300 | |
| 301 | fBBoxVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer( |
| 302 | GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey); |
| 303 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 306 | void PathStencilCoverOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 307 | if (!fTessellator) { |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | // Stencil the inner fan, if any. |
| 312 | if (fFanVertexCount > 0) { |
| 313 | SkASSERT(fStencilFanProgram); |
| 314 | SkASSERT(fFanBuffer); |
| 315 | flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds()); |
| 316 | flushState->bindBuffers(nullptr, nullptr, fFanBuffer); |
| 317 | flushState->draw(fFanVertexCount, fFanBaseVertex); |
| 318 | } |
| 319 | |
| 320 | // Stencil the rest of the path. |
| 321 | SkASSERT(fStencilPathProgram); |
| 322 | flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds()); |
| 323 | fTessellator->draw(flushState); |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 324 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 325 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 326 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 327 | |
| 328 | // Fill in the bounding box (if not in stencil-only mode). |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 329 | if (fCoverBBoxProgram) { |
| 330 | flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds()); |
| 331 | flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr, |
| 332 | fCoverBBoxProgram->pipeline()); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 333 | flushState->bindBuffers(nullptr, fBBoxBuffer, fBBoxVertexBufferIfNoIDSupport); |
Chris Dalton | 8a1bbaa | 2021-07-28 14:43:07 -0600 | [diff] [blame] | 334 | flushState->drawInstanced(fPathCount, fBBoxBaseInstance, 4, 0); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 335 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 336 | } |
Robert Phillips | 62bd633 | 2021-08-26 09:56:55 -0400 | [diff] [blame^] | 337 | |
| 338 | } // namespace skgpu::v1 |