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