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