blob: 9075e529e57a6327374daf0fb38a8a5531c91819 [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"
11#include "src/gpu/GrGpuCommandBuffer.h"
12#include "src/gpu/GrOpFlushState.h"
13#include "src/gpu/ccpr/GrCCConicShader.h"
14#include "src/gpu/ccpr/GrCCCubicShader.h"
15#include "src/gpu/ccpr/GrCCQuadraticShader.h"
16#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
17#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
18#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060019
Chris Dalton4c239342018-04-05 18:43:40 -060020class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader {
Chris Dalton84d36cd2019-04-17 14:47:17 -060021 void onEmitVaryings(
22 GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
23 const char* position, const char* coverage, const char* cornerCoverage,
24 const char* /*wind*/) override {
Chris Dalton4c239342018-04-05 18:43:40 -060025 if (!cornerCoverage) {
26 fCoverages.reset(kHalf_GrSLType, scope);
27 varyingHandler->addVarying("coverage", &fCoverages);
28 code->appendf("%s = %s;", OutName(fCoverages), coverage);
29 } else {
30 fCoverages.reset(kHalf3_GrSLType, scope);
31 varyingHandler->addVarying("coverages", &fCoverages);
32 code->appendf("%s = half3(%s, %s);", OutName(fCoverages), coverage, cornerCoverage);
33 }
34 }
35
36 void onEmitFragmentCode(GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override {
37 if (kHalf_GrSLType == fCoverages.type()) {
38 f->codeAppendf("%s = %s;", outputCoverage, fCoverages.fsIn());
39 } else {
40 f->codeAppendf("%s = %s.z * %s.y + %s.x;",
41 outputCoverage, fCoverages.fsIn(), fCoverages.fsIn(), fCoverages.fsIn());
42 }
43 }
44
45 GrGLSLVarying fCoverages;
46};
Eric Borend6365e52017-10-16 12:31:14 +000047
Chris Dalton6f5e77a2018-04-23 21:14:42 -060048void GrCCCoverageProcessor::Shader::CalcWind(const GrCCCoverageProcessor& proc,
49 GrGLSLVertexGeoBuilder* s, const char* pts,
50 const char* outputWind) {
51 if (3 == proc.numInputPoints()) {
52 s->codeAppendf("float2 a = %s[0] - %s[1], "
53 "b = %s[0] - %s[2];", pts, pts, pts, pts);
54 } else {
55 // All inputs are convex, so it's sufficient to just average the middle two input points.
56 SkASSERT(4 == proc.numInputPoints());
57 s->codeAppendf("float2 p12 = (%s[1] + %s[2]) * .5;", pts, pts);
58 s->codeAppendf("float2 a = %s[0] - p12, "
59 "b = %s[0] - %s[3];", pts, pts, pts);
60 }
61
62 s->codeAppend ("float area_x2 = determinant(float2x2(a, b));");
63 if (proc.isTriangles()) {
64 // We cull extremely thin triangles by zeroing wind. When a triangle gets too thin it's
65 // possible for FP round-off error to actually give us the wrong winding direction, causing
66 // rendering artifacts. The criteria we choose is "height <~ 1/1024". So we drop a triangle
67 // if the max effect it can have on any single pixel is <~ 1/1024, or 1/4 of a bit in 8888.
68 s->codeAppend ("float2 bbox_size = max(abs(a), abs(b));");
69 s->codeAppend ("float basewidth = max(bbox_size.x + bbox_size.y, 1);");
Ethan Nicholase1f55022019-02-05 17:17:40 -050070 s->codeAppendf("%s = (abs(area_x2 * 1024) > basewidth) ? sign(half(area_x2)) : 0;",
71 outputWind);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060072 } else {
73 // We already converted nearly-flat curves to lines on the CPU, so no need to worry about
74 // thin curve hulls at this point.
Ethan Nicholase1f55022019-02-05 17:17:40 -050075 s->codeAppendf("%s = sign(half(area_x2));", outputWind);
Chris Dalton6f5e77a2018-04-23 21:14:42 -060076 }
77}
78
Chris Dalton0a793812018-03-07 11:18:30 -070079void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
80 const char* leftPt,
81 const char* rightPt,
82 const char* rasterVertexDir,
83 const char* outputCoverage) {
84 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
85 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
86 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
87 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
88 // smooth antialiased edges.
89 //
90 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
91 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
92 // centered on P.)
93 //
94 // d0 == (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=0, where the bloat box is
96 // centered on P.)
97 //
98 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
99 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
100 //
101 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
102 //
103 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
104 //
105 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
106 rightPt, leftPt, leftPt, rightPt);
107 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
108 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
109 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
110 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500111 s->codeAppendf("%s = half(abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;",
112 outputCoverage);
Chris Dalton0a793812018-03-07 11:18:30 -0700113}
Chris Daltonbaf3e782018-03-08 15:55:58 +0000114
Chris Dalton8738cf42018-03-09 11:57:40 -0700115void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
116 const char* leftPt,
117 const char* rightPt,
118 const char* bloatDir1,
119 const char* bloatDir2,
120 const char* outputCoverages) {
121 // See comments in CalcEdgeCoverageAtBloatVertex.
122 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
123 rightPt, leftPt, leftPt, rightPt);
124 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
125 s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
126 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
Ethan Nicholase1f55022019-02-05 17:17:40 -0500127 s->codeAppendf( "%s[i] = half(abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
Chris Dalton8738cf42018-03-09 11:57:40 -0700128 outputCoverages);
129 s->codeAppendf("}");
130}
131
Chris Dalton4c239342018-04-05 18:43:40 -0600132void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s,
133 const char* leftDir, const char* rightDir,
134 const char* outputAttenuation) {
Chris Dalton04a1de52018-03-14 02:04:09 -0600135 // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
136 // 0 if corner_angle <= 90 degrees
Chris Daltonb7e03712018-10-04 15:23:13 -0600137 //
138 // NOTE: leftDir and rightDir are normalized and point in the same direction the path was
139 // defined with, i.e., leftDir points into the corner and rightDir points away from the corner.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500140 s->codeAppendf("half obtuseness = max(half(dot(%s, %s)), 0);", leftDir, rightDir);
Chris Dalton04a1de52018-03-14 02:04:09 -0600141
Chris Daltonb7e03712018-10-04 15:23:13 -0600142 // axis_alignedness = 1 - tan(angle_to_nearest_axis_from_corner_bisector)
143 // (i.e., 1 when the corner bisector is aligned with the x- or y-axis
144 // 0 when the corner bisector falls on a 45 degree angle
145 // 0..1 when the corner bisector falls somewhere in between
Ethan Nicholase1f55022019-02-05 17:17:40 -0500146 s->codeAppendf("half2 abs_bisect_maybe_transpose = abs((0 == obtuseness) ? half2(%s - %s) : "
147 "half2(%s + %s));",
Chris Daltonb7e03712018-10-04 15:23:13 -0600148 leftDir, rightDir, leftDir, rightDir);
149 s->codeAppend ("half axis_alignedness = "
150 "1 - min(abs_bisect_maybe_transpose.y, abs_bisect_maybe_transpose.x) / "
151 "max(abs_bisect_maybe_transpose.x, abs_bisect_maybe_transpose.y);");
Chris Dalton04a1de52018-03-14 02:04:09 -0600152
153 // ninety_degreesness = sin^2(corner_angle)
154 // sin^2 just because... it's always positive and the results looked better than plain sine... ?
155 s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
156 s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
157
158 // The below formula is not smart. It was just arrived at by considering the following
159 // observations:
160 //
161 // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
162 // (i.e. coverage = 1 - distance_to_corner^2)
163 // (i.e. outputAttenuation = 0)
164 //
165 // 2. 180-degree corners always have zero attenuation.
166 // (i.e. coverage = 1 - distance_to_corner)
167 // (i.e. outputAttenuation = 1)
168 //
169 // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
170 // (i.e. outputAttenuation = 1)
171 s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
172 outputAttenuation);
173}
174
Chris Daltonbaf3e782018-03-08 15:55:58 +0000175GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
176 std::unique_ptr<Shader> shader;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600177 switch (fPrimitiveType) {
178 case PrimitiveType::kTriangles:
Chris Dalton703b4762018-04-06 16:11:48 -0600179 case PrimitiveType::kWeightedTriangles:
Chris Dalton4c239342018-04-05 18:43:40 -0600180 shader = skstd::make_unique<TriangleShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000181 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600182 case PrimitiveType::kQuadratics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600183 shader = skstd::make_unique<GrCCQuadraticShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000184 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600185 case PrimitiveType::kCubics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600186 shader = skstd::make_unique<GrCCCubicShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000187 break;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600188 case PrimitiveType::kConics:
189 shader = skstd::make_unique<GrCCConicShader>();
190 break;
Chris Daltonbaf3e782018-03-08 15:55:58 +0000191 }
Chris Dalton2c5e0112019-03-29 13:14:18 -0500192 return this->onCreateGLSLInstance(std::move(shader));
Chris Daltonbaf3e782018-03-08 15:55:58 +0000193}
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600194
Chris Dalton2c5e0112019-03-29 13:14:18 -0500195void GrCCCoverageProcessor::Shader::emitFragmentCode(
196 const GrCCCoverageProcessor& proc, GrGLSLFPFragmentBuilder* f, const char* skOutputColor,
197 const char* skOutputCoverage) const {
Chris Dalton4c239342018-04-05 18:43:40 -0600198 f->codeAppendf("half coverage = 0;");
199 this->onEmitFragmentCode(f, "coverage");
200 f->codeAppendf("%s.a = coverage;", skOutputColor);
201 f->codeAppendf("%s = half4(1);", skOutputCoverage);
202}
203
Chris Dalton2c5e0112019-03-29 13:14:18 -0500204void GrCCCoverageProcessor::draw(
205 GrOpFlushState* flushState, const GrPipeline& pipeline, const SkIRect scissorRects[],
206 const GrMesh meshes[], int meshCount, const SkRect& drawBounds) const {
Brian Salomon49348902018-06-26 09:12:38 -0400207 GrPipeline::DynamicStateArrays dynamicStateArrays;
208 dynamicStateArrays.fScissorRects = scissorRects;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600209 GrGpuRTCommandBuffer* cmdBuff = flushState->rtCommandBuffer();
Brian Salomon49348902018-06-26 09:12:38 -0400210 cmdBuff->draw(*this, pipeline, nullptr, &dynamicStateArrays, meshes, meshCount, drawBounds);
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600211}