blob: c36075e6fc9547c08c8692ac65c9d4b4a04ed879 [file] [log] [blame]
Chris Dalton1a325d22017-07-14 15:17:41 -06001/*
2 * Copyright 2017 Google Inc.
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ccpr/GrCCCoverageProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkMakeUnique.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040012#include "src/gpu/GrOpsRenderPass.h"
Robert Phillips901aff02019-10-08 12:32:56 -040013#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/ccpr/GrCCConicShader.h"
15#include "src/gpu/ccpr/GrCCCubicShader.h"
16#include "src/gpu/ccpr/GrCCQuadraticShader.h"
17#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
18#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
19#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060020
Chris Dalton4c239342018-04-05 18:43:40 -060021class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader {
Chris Dalton84d36cd2019-04-17 14:47:17 -060022 void onEmitVaryings(
23 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
24 const char* position, const char* coverage, const char* cornerCoverage,
25 const char* /*wind*/) override {
Chris Dalton4c239342018-04-05 18:43:40 -060026 if (!cornerCoverage) {
27 fCoverages.reset(kHalf_GrSLType, scope);
28 varyingHandler->addVarying("coverage", &fCoverages);
29 code->appendf("%s = %s;", OutName(fCoverages), coverage);
30 } else {
31 fCoverages.reset(kHalf3_GrSLType, scope);
32 varyingHandler->addVarying("coverages", &fCoverages);
33 code->appendf("%s = half3(%s, %s);", OutName(fCoverages), coverage, cornerCoverage);
34 }
35 }
36
Chris Daltonc3318f02019-07-19 14:20:53 -060037 void emitFragmentCoverageCode(
38 GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override {
Chris Dalton4c239342018-04-05 18:43:40 -060039 if (kHalf_GrSLType == fCoverages.type()) {
40 f->codeAppendf("%s = %s;", outputCoverage, fCoverages.fsIn());
41 } else {
42 f->codeAppendf("%s = %s.z * %s.y + %s.x;",
43 outputCoverage, fCoverages.fsIn(), fCoverages.fsIn(), fCoverages.fsIn());
44 }
45 }
46
Chris Daltonc3318f02019-07-19 14:20:53 -060047 void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const override { return; }
48
Chris Dalton4c239342018-04-05 18:43:40 -060049 GrGLSLVarying fCoverages;
50};
Eric Borend6365e52017-10-16 12:31:14 +000051
Chris Dalton6f5e77a2018-04-23 21:14:42 -060052void GrCCCoverageProcessor::Shader::CalcWind(const GrCCCoverageProcessor& proc,
53 GrGLSLVertexGeoBuilder* s, const char* pts,
54 const char* outputWind) {
55 if (3 == proc.numInputPoints()) {
56 s->codeAppendf("float2 a = %s[0] - %s[1], "
57 "b = %s[0] - %s[2];", pts, pts, pts, pts);
58 } else {
59 // All inputs are convex, so it's sufficient to just average the middle two input points.
60 SkASSERT(4 == proc.numInputPoints());
61 s->codeAppendf("float2 p12 = (%s[1] + %s[2]) * .5;", pts, pts);
62 s->codeAppendf("float2 a = %s[0] - p12, "
63 "b = %s[0] - %s[3];", pts, pts, pts);
64 }
65
66 s->codeAppend ("float area_x2 = determinant(float2x2(a, b));");
67 if (proc.isTriangles()) {
68 // We cull extremely thin triangles by zeroing wind. When a triangle gets too thin it's
69 // possible for FP round-off error to actually give us the wrong winding direction, causing
70 // rendering artifacts. The criteria we choose is "height <~ 1/1024". So we drop a triangle
71 // if the max effect it can have on any single pixel is <~ 1/1024, or 1/4 of a bit in 8888.
72 s->codeAppend ("float2 bbox_size = max(abs(a), abs(b));");
73 s->codeAppend ("float basewidth = max(bbox_size.x + bbox_size.y, 1);");
Ethan Nicholase1f55022019-02-05 17:17:40 -050074 s->codeAppendf("%s = (abs(area_x2 * 1024) > basewidth) ? sign(half(area_x2)) : 0;",
75 outputWind);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060076 } else {
77 // We already converted nearly-flat curves to lines on the CPU, so no need to worry about
78 // thin curve hulls at this point.
Ethan Nicholase1f55022019-02-05 17:17:40 -050079 s->codeAppendf("%s = sign(half(area_x2));", outputWind);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060080 }
81}
82
Chris Dalton0a793812018-03-07 11:18:30 -070083void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
84 const char* leftPt,
85 const char* rightPt,
86 const char* rasterVertexDir,
87 const char* outputCoverage) {
88 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
89 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
90 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
91 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
92 // smooth antialiased edges.
93 //
94 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
95 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
96 // centered on P.)
97 //
98 // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose
99 // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is
100 // centered on P.)
101 //
102 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
103 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
104 //
105 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
106 //
107 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
108 //
109 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
110 rightPt, leftPt, leftPt, rightPt);
111 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
112 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
113 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
114 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500115 s->codeAppendf("%s = half(abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;",
116 outputCoverage);
Chris Dalton0a793812018-03-07 11:18:30 -0700117}
Chris Daltonbaf3e782018-03-08 15:55:58 +0000118
Chris Dalton8738cf42018-03-09 11:57:40 -0700119void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
120 const char* leftPt,
121 const char* rightPt,
122 const char* bloatDir1,
123 const char* bloatDir2,
124 const char* outputCoverages) {
125 // See comments in CalcEdgeCoverageAtBloatVertex.
126 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
127 rightPt, leftPt, leftPt, rightPt);
128 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
129 s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
130 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500131 s->codeAppendf( "%s[i] = half(abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
Chris Dalton8738cf42018-03-09 11:57:40 -0700132 outputCoverages);
133 s->codeAppendf("}");
134}
135
Chris Dalton4c239342018-04-05 18:43:40 -0600136void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s,
137 const char* leftDir, const char* rightDir,
138 const char* outputAttenuation) {
Chris Dalton04a1de52018-03-14 02:04:09 -0600139 // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
140 // 0 if corner_angle <= 90 degrees
Chris Daltonb7e03712018-10-04 15:23:13 -0600141 //
142 // NOTE: leftDir and rightDir are normalized and point in the same direction the path was
143 // defined with, i.e., leftDir points into the corner and rightDir points away from the corner.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500144 s->codeAppendf("half obtuseness = max(half(dot(%s, %s)), 0);", leftDir, rightDir);
Chris Dalton04a1de52018-03-14 02:04:09 -0600145
Chris Daltonb7e03712018-10-04 15:23:13 -0600146 // axis_alignedness = 1 - tan(angle_to_nearest_axis_from_corner_bisector)
147 // (i.e., 1 when the corner bisector is aligned with the x- or y-axis
148 // 0 when the corner bisector falls on a 45 degree angle
149 // 0..1 when the corner bisector falls somewhere in between
Ethan Nicholase1f55022019-02-05 17:17:40 -0500150 s->codeAppendf("half2 abs_bisect_maybe_transpose = abs((0 == obtuseness) ? half2(%s - %s) : "
151 "half2(%s + %s));",
Chris Daltonb7e03712018-10-04 15:23:13 -0600152 leftDir, rightDir, leftDir, rightDir);
153 s->codeAppend ("half axis_alignedness = "
154 "1 - min(abs_bisect_maybe_transpose.y, abs_bisect_maybe_transpose.x) / "
155 "max(abs_bisect_maybe_transpose.x, abs_bisect_maybe_transpose.y);");
Chris Dalton04a1de52018-03-14 02:04:09 -0600156
157 // ninety_degreesness = sin^2(corner_angle)
158 // sin^2 just because... it's always positive and the results looked better than plain sine... ?
159 s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
160 s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
161
162 // The below formula is not smart. It was just arrived at by considering the following
163 // observations:
164 //
165 // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
166 // (i.e. coverage = 1 - distance_to_corner^2)
167 // (i.e. outputAttenuation = 0)
168 //
169 // 2. 180-degree corners always have zero attenuation.
170 // (i.e. coverage = 1 - distance_to_corner)
171 // (i.e. outputAttenuation = 1)
172 //
173 // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
174 // (i.e. outputAttenuation = 1)
175 s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
176 outputAttenuation);
177}
178
Chris Daltonbaf3e782018-03-08 15:55:58 +0000179GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
180 std::unique_ptr<Shader> shader;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600181 switch (fPrimitiveType) {
182 case PrimitiveType::kTriangles:
Chris Dalton703b4762018-04-06 16:11:48 -0600183 case PrimitiveType::kWeightedTriangles:
Chris Dalton4c239342018-04-05 18:43:40 -0600184 shader = skstd::make_unique<TriangleShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000185 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600186 case PrimitiveType::kQuadratics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600187 shader = skstd::make_unique<GrCCQuadraticShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000188 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600189 case PrimitiveType::kCubics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600190 shader = skstd::make_unique<GrCCCubicShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000191 break;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600192 case PrimitiveType::kConics:
193 shader = skstd::make_unique<GrCCConicShader>();
194 break;
Chris Daltonbaf3e782018-03-08 15:55:58 +0000195 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500196 return this->onCreateGLSLInstance(std::move(shader));
Chris Daltonbaf3e782018-03-08 15:55:58 +0000197}
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600198
Chris Dalton2c5e0112019-03-29 13:14:18 -0500199void GrCCCoverageProcessor::draw(
200 GrOpFlushState* flushState, const GrPipeline& pipeline, const SkIRect scissorRects[],
201 const GrMesh meshes[], int meshCount, const SkRect& drawBounds) const {
Brian Salomon49348902018-06-26 09:12:38 -0400202 GrPipeline::DynamicStateArrays dynamicStateArrays;
203 dynamicStateArrays.fScissorRects = scissorRects;
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400204 GrOpsRenderPass* renderPass = flushState->opsRenderPass();
Robert Phillips901aff02019-10-08 12:32:56 -0400205
206 GrProgramInfo programInfo(flushState->drawOpArgs().numSamples(),
207 flushState->drawOpArgs().origin(),
208 pipeline,
209 *this,
210 nullptr,
211 &dynamicStateArrays);
212
213
214 renderPass->draw(programInfo, meshes, meshCount, drawBounds);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600215}