Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
| 8 | #include "src/gpu/tessellate/GrPathInnerTriangulateOp.h" |
| 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 | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 12 | #include "src/gpu/GrInnerFanTriangulator.h" |
| 13 | #include "src/gpu/GrOpFlushState.h" |
| 14 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrResourceProvider.h" |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 16 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrPathCurveTessellator.h" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame] | 19 | #include "src/gpu/tessellate/shaders/GrPathTessellationShader.h" |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 20 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 21 | using PathFlags = GrTessellationPathRenderer::PathFlags; |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 22 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | // Fills an array of convex hulls surrounding 4-point cubic or conic instances. This shader is used |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 26 | // for the "cover" pass after the curves have been fully stencilled. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 27 | class HullShader : public GrPathTessellationShader { |
| 28 | public: |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 29 | HullShader(const SkMatrix& viewMatrix, SkPMColor4f color, const GrShaderCaps& shaderCaps) |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 30 | : GrPathTessellationShader(kTessellate_HullShader_ClassID, |
| 31 | GrPrimitiveType::kTriangleStrip, 0, viewMatrix, color) { |
Chris Dalton | 50516f3 | 2021-07-18 22:26:27 -0600 | [diff] [blame] | 32 | fInstanceAttribs.emplace_back("p01", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 33 | fInstanceAttribs.emplace_back("p23", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 34 | if (!shaderCaps.infinitySupport()) { |
| 35 | // A conic curve is written out with p3=[w,Infinity], but GPUs that don't support |
| 36 | // infinity can't detect this. On these platforms we also write out an extra float with |
| 37 | // each patch that explicitly tells the shader what type of curve it is. |
| 38 | fInstanceAttribs.emplace_back("curveType", kFloat_GrVertexAttribType, kFloat_GrSLType); |
| 39 | } |
| 40 | this->setInstanceAttributes(fInstanceAttribs.data(), fInstanceAttribs.count()); |
| 41 | SkASSERT(fInstanceAttribs.count() <= kMaxInstanceAttribCount); |
| 42 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 43 | if (!shaderCaps.vertexIDSupport()) { |
| 44 | constexpr static Attribute kVertexIdxAttrib("vertexidx", kFloat_GrVertexAttribType, |
| 45 | kFloat_GrSLType); |
| 46 | this->setVertexAttributes(&kVertexIdxAttrib, 1); |
| 47 | } |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | private: |
Chris Dalton | b63711a | 2021-06-01 14:52:02 -0600 | [diff] [blame] | 51 | const char* name() const final { return "tessellate_HullShader"; } |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 52 | void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame^] | 53 | std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final; |
Chris Dalton | 50516f3 | 2021-07-18 22:26:27 -0600 | [diff] [blame] | 54 | |
| 55 | constexpr static int kMaxInstanceAttribCount = 3; |
| 56 | SkSTArray<kMaxInstanceAttribCount, Attribute> fInstanceAttribs; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 57 | }; |
| 58 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame^] | 59 | std::unique_ptr<GrGeometryProcessor::ProgramImpl> HullShader::makeProgramImpl( |
| 60 | const GrShaderCaps&) const { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 61 | class Impl : public GrPathTessellationShader::Impl { |
Chris Dalton | 6904303 | 2021-07-01 11:17:53 -0600 | [diff] [blame] | 62 | void emitVertexCode(const GrShaderCaps& shaderCaps, const GrPathTessellationShader&, |
| 63 | GrGLSLVertexBuilder* v, GrGPArgs* gpArgs) override { |
Chris Dalton | 50516f3 | 2021-07-18 22:26:27 -0600 | [diff] [blame] | 64 | if (shaderCaps.infinitySupport()) { |
| 65 | v->insertFunction(R"( |
| 66 | bool is_conic_curve() { return isinf(p23.w); } |
| 67 | bool is_non_triangular_conic_curve() { |
| 68 | // We consider a conic non-triangular as long as its weight isn't infinity. |
| 69 | // NOTE: "isinf == false" works on Mac Radeon GLSL; "!isinf" can get the wrong |
| 70 | // answer. |
| 71 | return isinf(p23.z) == false; |
| 72 | })"); |
| 73 | } else { |
| 74 | v->insertFunction(SkStringPrintf(R"( |
| 75 | bool is_conic_curve() { return curveType != %g; })", kCubicCurveType).c_str()); |
| 76 | v->insertFunction(SkStringPrintf(R"( |
| 77 | bool is_non_triangular_conic_curve() { |
| 78 | return curveType == %g; |
| 79 | })", kConicCurveType).c_str()); |
| 80 | } |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 81 | v->codeAppend(R"( |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 82 | float2 p0=p01.xy, p1=p01.zw, p2=p23.xy, p3=p23.zw; |
Chris Dalton | 50516f3 | 2021-07-18 22:26:27 -0600 | [diff] [blame] | 83 | if (is_conic_curve()) { |
| 84 | // Conics are 3 points, with the weight in p3. |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 85 | float w = p3.x; |
Chris Dalton | 50516f3 | 2021-07-18 22:26:27 -0600 | [diff] [blame] | 86 | p3 = p2; // Duplicate the endpoint for shared code that also runs on cubics. |
| 87 | if (is_non_triangular_conic_curve()) { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 88 | // Convert the points to a trapeziodal hull that circumcscribes the conic. |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 89 | float2 p1w = p1 * w; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 90 | float T = .51; // Bias outward a bit to ensure we cover the outermost samples. |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 91 | float2 c1 = mix(p0, p1w, T); |
| 92 | float2 c2 = mix(p2, p1w, T); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 93 | float iw = 1 / mix(1, w, T); |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 94 | p2 = c2 * iw; |
| 95 | p1 = c1 * iw; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | // Translate the points to v0..3 where v0=0. |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 100 | float2 v1 = p1 - p0; |
| 101 | float2 v2 = p2 - p0; |
| 102 | float2 v3 = p3 - p0; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 103 | |
| 104 | // Reorder the points so v2 bisects v1 and v3. |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 105 | if (sign(cross(v2, v1)) == sign(cross(v2, v3))) { |
| 106 | float2 tmp = p2; |
| 107 | if (sign(cross(v1, v2)) != sign(cross(v1, v3))) { |
| 108 | p2 = p1; // swap(p2, p1) |
| 109 | p1 = tmp; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 110 | } else { |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 111 | p2 = p3; // swap(p2, p3) |
| 112 | p3 = tmp; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 113 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 114 | })"); |
| 115 | |
Chris Dalton | 6904303 | 2021-07-01 11:17:53 -0600 | [diff] [blame] | 116 | if (shaderCaps.vertexIDSupport()) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 117 | // If we don't have sk_VertexID support then "vertexidx" already came in as a |
| 118 | // vertex attrib. |
| 119 | v->codeAppend(R"( |
| 120 | // sk_VertexID comes in fan order. Convert to strip order. |
| 121 | int vertexidx = sk_VertexID; |
| 122 | vertexidx ^= vertexidx >> 1;)"); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 123 | } |
| 124 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 125 | v->codeAppend(R"( |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 126 | // Find the "turn direction" of each corner and net turn direction. |
| 127 | float vertexdir = 0; |
| 128 | float netdir = 0; |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 129 | float2 prev, next; |
| 130 | float dir; |
| 131 | float2 localcoord; |
| 132 | float2 nextcoord;)"); |
| 133 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 134 | for (int i = 0; i < 4; ++i) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 135 | v->codeAppendf(R"( |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 136 | prev = p%i - p%i;)", i, (i + 3) % 4); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 137 | v->codeAppendf(R"( |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 138 | next = p%i - p%i;)", (i + 1) % 4, i); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 139 | v->codeAppendf(R"( |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 140 | dir = sign(cross(prev, next)); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 141 | if (vertexidx == %i) { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 142 | vertexdir = dir; |
Chris Dalton | 202441f | 2021-06-30 22:48:30 -0600 | [diff] [blame] | 143 | localcoord = p%i; |
| 144 | nextcoord = p%i; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 145 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 146 | netdir += dir;)", i, i, (i + 1) % 4); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 147 | } |
| 148 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 149 | v->codeAppend(R"( |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 150 | // Remove the non-convex vertex, if any. |
| 151 | if (vertexdir != sign(netdir)) { |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 152 | localcoord = nextcoord; |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 155 | float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;)"); |
| 156 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
| 157 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
| 158 | } |
| 159 | }; |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame^] | 160 | return std::make_unique<Impl>(); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace |
| 164 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 165 | void GrPathInnerTriangulateOp::visitProxies(const GrVisitProxyFunc& func) const { |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 166 | if (fPipelineForFills) { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 167 | fPipelineForFills->visitProxies(func); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 168 | } else { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 169 | fProcessors.visitProxies(func); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | GrDrawOp::FixedFunctionFlags GrPathInnerTriangulateOp::fixedFunctionFlags() const { |
| 174 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 175 | if (GrAAType::kNone != fAAType) { |
| 176 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 177 | } |
| 178 | return flags; |
| 179 | } |
| 180 | |
| 181 | GrProcessorSet::Analysis GrPathInnerTriangulateOp::finalize(const GrCaps& caps, |
| 182 | const GrAppliedClip* clip, |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 183 | GrClampType clampType) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 184 | return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps, |
| 185 | clampType, &fColor); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 188 | void GrPathInnerTriangulateOp::pushFanStencilProgram(const GrTessellationShader::ProgramArgs& args, |
| 189 | const GrPipeline* pipelineForStencils, |
| 190 | const GrUserStencilSettings* stencil) { |
| 191 | SkASSERT(pipelineForStencils); |
| 192 | auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(args.fArena, fViewMatrix, |
| 193 | SK_PMColor4fTRANSPARENT); |
| 194 | fFanPrograms.push_back(GrTessellationShader::MakeProgram(args, shader, pipelineForStencils, |
| 195 | stencil)); } |
| 196 | |
| 197 | void GrPathInnerTriangulateOp::pushFanFillProgram(const GrTessellationShader::ProgramArgs& args, |
| 198 | const GrUserStencilSettings* stencil) { |
| 199 | SkASSERT(fPipelineForFills); |
| 200 | auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(args.fArena, fViewMatrix, |
| 201 | fColor); |
| 202 | fFanPrograms.push_back(GrTessellationShader::MakeProgram(args, shader, fPipelineForFills, |
| 203 | stencil)); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 206 | void GrPathInnerTriangulateOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args, |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 207 | GrAppliedClip&& appliedClip) { |
| 208 | SkASSERT(!fFanTriangulator); |
| 209 | SkASSERT(!fFanPolys); |
| 210 | SkASSERT(!fPipelineForFills); |
| 211 | SkASSERT(!fTessellator); |
| 212 | SkASSERT(!fStencilCurvesProgram); |
| 213 | SkASSERT(fFanPrograms.empty()); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 214 | SkASSERT(!fCoverHullsProgram); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 215 | |
| 216 | if (fPath.countVerbs() <= 0) { |
| 217 | return; |
| 218 | } |
| 219 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 220 | // If using wireframe, we have to fall back on a standard Redbook "stencil then cover" algorithm |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 221 | // instead of bypassing the stencil buffer to fill the fan directly. |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 222 | bool forceRedbookStencilPass = (fPathFlags & (PathFlags::kStencilOnly | PathFlags::kWireframe)); |
| 223 | bool doFill = !(fPathFlags & PathFlags::kStencilOnly); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 224 | |
| 225 | bool isLinear; |
| 226 | fFanTriangulator = args.fArena->make<GrInnerFanTriangulator>(fPath, args.fArena); |
| 227 | fFanPolys = fFanTriangulator->pathToPolys(&fFanBreadcrumbs, &isLinear); |
| 228 | |
| 229 | // Create a pipeline for stencil passes if needed. |
| 230 | const GrPipeline* pipelineForStencils = nullptr; |
| 231 | if (forceRedbookStencilPass || !isLinear) { // Curves always get stencilled. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 232 | pipelineForStencils = GrPathTessellationShader::MakeStencilOnlyPipeline( |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame] | 233 | args, fAAType, fPathFlags, appliedClip.hardClip()); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Create a pipeline for fill passes if needed. |
| 237 | if (doFill) { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 238 | fPipelineForFills = GrTessellationShader::MakePipeline(args, fAAType, |
| 239 | std::move(appliedClip), |
| 240 | std::move(fProcessors)); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // Pass 1: Tessellate the outer curves into the stencil buffer. |
| 244 | if (!isLinear) { |
Chris Dalton | 26666bd | 2021-06-08 16:25:46 -0600 | [diff] [blame] | 245 | fTessellator = GrPathCurveTessellator::Make(args.fArena, fViewMatrix, |
| 246 | SK_PMColor4fTRANSPARENT, |
Chris Dalton | d2b8ba3 | 2021-06-09 00:12:59 -0600 | [diff] [blame] | 247 | GrPathCurveTessellator::DrawInnerFan::kNo, |
Chris Dalton | 198ac15 | 2021-06-09 13:49:43 -0600 | [diff] [blame] | 248 | fPath.countVerbs(), *pipelineForStencils, |
| 249 | *args.fCaps); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 250 | const GrUserStencilSettings* stencilPathSettings = |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 251 | GrPathTessellationShader::StencilPathSettings(GrFillRuleForSkPath(fPath)); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 252 | fStencilCurvesProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(), |
| 253 | pipelineForStencils, |
| 254 | stencilPathSettings); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Pass 2: Fill the path's inner fan with a stencil test against the curves. |
| 258 | if (fFanPolys) { |
| 259 | if (forceRedbookStencilPass) { |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 260 | // Use a standard Redbook "stencil then cover" algorithm instead of bypassing the |
| 261 | // stencil buffer to fill the fan directly. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 262 | const GrUserStencilSettings* stencilPathSettings = |
Chris Dalton | baae2dd | 2021-06-25 14:52:49 -0600 | [diff] [blame] | 263 | GrPathTessellationShader::StencilPathSettings(GrFillRuleForSkPath(fPath)); |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 264 | this->pushFanStencilProgram(args, pipelineForStencils, stencilPathSettings); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 265 | if (doFill) { |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 266 | this->pushFanFillProgram(args, |
| 267 | GrPathTessellationShader::TestAndResetStencilSettings()); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 268 | } |
| 269 | } else if (isLinear) { |
| 270 | // There are no outer curves! Ignore stencil and fill the path directly. |
| 271 | SkASSERT(!pipelineForStencils); |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 272 | this->pushFanFillProgram(args, &GrUserStencilSettings::kUnused); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 273 | } else if (!fPipelineForFills->hasStencilClip()) { |
| 274 | // These are a twist on the standard Redbook stencil settings that allow us to fill the |
| 275 | // inner polygon directly to the final render target. By the time these programs |
| 276 | // execute, the outer curves will already be stencilled in. So if the stencil value is |
| 277 | // zero, then it means the sample in question is not affected by any curves and we can |
| 278 | // fill it in directly. If the stencil value is nonzero, then we don't fill and instead |
| 279 | // continue the standard Redbook counting process. |
| 280 | constexpr static GrUserStencilSettings kFillOrIncrDecrStencil( |
| 281 | GrUserStencilSettings::StaticInitSeparate< |
| 282 | 0x0000, 0x0000, |
| 283 | GrUserStencilTest::kEqual, GrUserStencilTest::kEqual, |
| 284 | 0xffff, 0xffff, |
| 285 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 286 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 287 | 0xffff, 0xffff>()); |
| 288 | |
| 289 | constexpr static GrUserStencilSettings kFillOrInvertStencil( |
| 290 | GrUserStencilSettings::StaticInit< |
| 291 | 0x0000, |
| 292 | GrUserStencilTest::kEqual, |
| 293 | 0xffff, |
| 294 | GrUserStencilOp::kKeep, |
| 295 | // "Zero" instead of "Invert" because the fan only touches any given pixel once. |
| 296 | GrUserStencilOp::kZero, |
| 297 | 0xffff>()); |
| 298 | |
| 299 | auto* stencil = (fPath.getFillType() == SkPathFillType::kWinding) |
| 300 | ? &kFillOrIncrDecrStencil |
| 301 | : &kFillOrInvertStencil; |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 302 | this->pushFanFillProgram(args, stencil); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 303 | } else { |
| 304 | // This is the same idea as above, but we use two passes instead of one because there is |
| 305 | // a stencil clip. The stencil test isn't expressive enough to do the above tests and |
| 306 | // also check the clip bit in a single pass. |
| 307 | constexpr static GrUserStencilSettings kFillIfZeroAndInClip( |
| 308 | GrUserStencilSettings::StaticInit< |
| 309 | 0x0000, |
| 310 | GrUserStencilTest::kEqualIfInClip, |
| 311 | 0xffff, |
| 312 | GrUserStencilOp::kKeep, |
| 313 | GrUserStencilOp::kKeep, |
| 314 | 0xffff>()); |
| 315 | |
| 316 | constexpr static GrUserStencilSettings kIncrDecrStencilIfNonzero( |
| 317 | GrUserStencilSettings::StaticInitSeparate< |
| 318 | 0x0000, 0x0000, |
| 319 | // No need to check the clip because the previous stencil pass will have only |
| 320 | // written to samples already inside the clip. |
| 321 | GrUserStencilTest::kNotEqual, GrUserStencilTest::kNotEqual, |
| 322 | 0xffff, 0xffff, |
| 323 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 324 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 325 | 0xffff, 0xffff>()); |
| 326 | |
| 327 | constexpr static GrUserStencilSettings kInvertStencilIfNonZero( |
| 328 | GrUserStencilSettings::StaticInit< |
| 329 | 0x0000, |
| 330 | // No need to check the clip because the previous stencil pass will have only |
| 331 | // written to samples already inside the clip. |
| 332 | GrUserStencilTest::kNotEqual, |
| 333 | 0xffff, |
| 334 | // "Zero" instead of "Invert" because the fan only touches any given pixel once. |
| 335 | GrUserStencilOp::kZero, |
| 336 | GrUserStencilOp::kKeep, |
| 337 | 0xffff>()); |
| 338 | |
| 339 | // Pass 2a: Directly fill fan samples whose stencil values (from curves) are zero. |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 340 | this->pushFanFillProgram(args, &kFillIfZeroAndInClip); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 341 | |
| 342 | // Pass 2b: Redbook counting on fan samples whose stencil values (from curves) != 0. |
| 343 | auto* stencil = (fPath.getFillType() == SkPathFillType::kWinding) |
| 344 | ? &kIncrDecrStencilIfNonzero |
| 345 | : &kInvertStencilIfNonZero; |
Chris Dalton | a8c4de9 | 2021-07-26 21:46:28 +0000 | [diff] [blame] | 346 | this->pushFanStencilProgram(args, pipelineForStencils, stencil); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | // Pass 3: Draw convex hulls around each curve. |
| 351 | if (doFill && !isLinear) { |
| 352 | // By the time this program executes, every pixel will be filled in except the ones touched |
| 353 | // by curves. We issue a final cover pass over the curves by drawing their convex hulls. |
| 354 | // This will fill in any remaining samples and reset the stencil values back to zero. |
| 355 | SkASSERT(fTessellator); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 356 | auto* hullShader = args.fArena->make<HullShader>(fViewMatrix, fColor, |
| 357 | *args.fCaps->shaderCaps()); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 358 | fCoverHullsProgram = GrTessellationShader::MakeProgram( |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 359 | args, hullShader, fPipelineForFills, |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 360 | GrPathTessellationShader::TestAndResetStencilSettings()); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | void GrPathInnerTriangulateOp::onPrePrepare(GrRecordingContext* context, |
| 365 | const GrSurfaceProxyView& writeView, |
| 366 | GrAppliedClip* clip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 367 | const GrDstProxyView& dstProxyView, |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 368 | GrXferBarrierFlags renderPassXferBarriers, |
| 369 | GrLoadOp colorLoadOp) { |
| 370 | this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 371 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 372 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 373 | if (fStencilCurvesProgram) { |
| 374 | context->priv().recordProgramInfo(fStencilCurvesProgram); |
| 375 | } |
| 376 | for (const GrProgramInfo* fanProgram : fFanPrograms) { |
| 377 | context->priv().recordProgramInfo(fanProgram); |
| 378 | } |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 379 | if (fCoverHullsProgram) { |
| 380 | context->priv().recordProgramInfo(fCoverHullsProgram); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 384 | GR_DECLARE_STATIC_UNIQUE_KEY(gHullVertexBufferKey); |
| 385 | |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 386 | void GrPathInnerTriangulateOp::onPrepare(GrOpFlushState* flushState) { |
| 387 | if (!fFanTriangulator) { |
| 388 | this->prePreparePrograms({flushState->allocator(), flushState->writeView(), |
| 389 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 390 | flushState->colorLoadOp(), &flushState->caps()}, |
| 391 | flushState->detachAppliedClip()); |
| 392 | if (!fFanTriangulator) { |
| 393 | return; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | if (fFanPolys) { |
| 398 | GrEagerDynamicVertexAllocator alloc(flushState, &fFanBuffer, &fBaseFanVertex); |
| 399 | fFanVertexCount = fFanTriangulator->polysToTriangles(fFanPolys, &alloc, &fFanBreadcrumbs); |
| 400 | } |
| 401 | |
| 402 | if (fTessellator) { |
| 403 | // Must be called after polysToTriangles() in order for fFanBreadcrumbs to be complete. |
Chris Dalton | 17eaf62 | 2021-07-28 09:43:52 -0600 | [diff] [blame] | 404 | fTessellator->prepare(flushState, this->bounds(), {SkMatrix::I(), fPath}, |
| 405 | fPath.countVerbs(), &fFanBreadcrumbs); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 406 | } |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 407 | |
| 408 | if (!flushState->caps().shaderCaps()->vertexIDSupport()) { |
| 409 | constexpr static float kStripOrderIDs[4] = {0, 1, 3, 2}; |
| 410 | |
| 411 | GR_DEFINE_STATIC_UNIQUE_KEY(gHullVertexBufferKey); |
| 412 | |
| 413 | fHullVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer( |
| 414 | GrGpuBufferType::kVertex, sizeof(kStripOrderIDs), kStripOrderIDs, |
| 415 | gHullVertexBufferKey); |
| 416 | } |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void GrPathInnerTriangulateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
| 420 | if (fStencilCurvesProgram) { |
| 421 | SkASSERT(fTessellator); |
| 422 | flushState->bindPipelineAndScissorClip(*fStencilCurvesProgram, this->bounds()); |
| 423 | fTessellator->draw(flushState); |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 424 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 425 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 426 | } |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | for (const GrProgramInfo* fanProgram : fFanPrograms) { |
| 430 | SkASSERT(fFanBuffer); |
| 431 | flushState->bindPipelineAndScissorClip(*fanProgram, this->bounds()); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 432 | flushState->bindTextures(fanProgram->geomProc(), nullptr, fanProgram->pipeline()); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 433 | flushState->bindBuffers(nullptr, nullptr, fFanBuffer); |
| 434 | flushState->draw(fFanVertexCount, fBaseFanVertex); |
| 435 | } |
| 436 | |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 437 | if (fCoverHullsProgram) { |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 438 | SkASSERT(fTessellator); |
Chris Dalton | 031d76b | 2021-06-08 16:32:00 -0600 | [diff] [blame] | 439 | flushState->bindPipelineAndScissorClip(*fCoverHullsProgram, this->bounds()); |
| 440 | flushState->bindTextures(fCoverHullsProgram->geomProc(), nullptr, *fPipelineForFills); |
Chris Dalton | a05ccc3 | 2021-06-29 19:42:13 -0600 | [diff] [blame] | 441 | fTessellator->drawHullInstances(flushState, fHullVertexBufferIfNoIDSupport); |
Chris Dalton | ebb37e7 | 2021-01-27 17:59:45 -0700 | [diff] [blame] | 442 | } |
| 443 | } |