blob: 1d570dca06948569bfd31b7d7f00db3863f229e6 [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
Chris Dalton383a2ef2018-01-08 17:21:41 -05008#include "GrCCCoverageProcessor.h"
Chris Dalton1a325d22017-07-14 15:17:41 -06009
Chris Dalton6a3dbee2017-10-16 10:44:41 -060010#include "SkMakeUnique.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050011#include "ccpr/GrCCCubicShader.h"
12#include "ccpr/GrCCQuadraticShader.h"
Chris Daltonfe462ef2018-03-08 15:54:01 +000013#include "ccpr/GrCCTriangleShader.h"
Chris Dalton90e8fb12017-12-22 02:24:53 -070014#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060015#include "glsl/GrGLSLFragmentShaderBuilder.h"
Chris Dalton1fbdb612017-12-12 12:48:47 -070016#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017
Chris Dalton383a2ef2018-01-08 17:21:41 -050018void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc,
Chris Dalton60283612018-02-14 13:38:14 -070019 GrGLSLFPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050020 const char* skOutputColor,
21 const char* skOutputCoverage) const {
Chris Daltonfe462ef2018-03-08 15:54:01 +000022 f->codeAppendf("half coverage = 0;");
Chris Daltonbaf3e782018-03-08 15:55:58 +000023 this->onEmitFragmentCode(f, "coverage");
Chris Daltonf510e262018-01-30 16:42:37 -070024 f->codeAppendf("%s.a = coverage;", skOutputColor);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060025 f->codeAppendf("%s = half4(1);", skOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -060026#ifdef SK_DEBUG
Chris Daltona640c492017-09-11 22:04:03 -070027 if (proc.debugVisualizationsEnabled()) {
Chris Dalton8738cf42018-03-09 11:57:40 -070028 f->codeAppendf("%s = half4(-%s.a, %s.a, 0, abs(%s.a));",
29 skOutputColor, skOutputColor, skOutputColor, skOutputColor);
Eric Borend6365e52017-10-16 12:31:14 +000030 }
31#endif
Eric Borend6365e52017-10-16 12:31:14 +000032}
33
Chris Daltonbaf3e782018-03-08 15:55:58 +000034void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s,
35 const char* leftPt,
36 const char* rightPt,
37 const char* outputDistanceEquation) {
38 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
39 rightPt, leftPt, leftPt, rightPt);
40 s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
41 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we
42 // come up with here as long as it isn't NaN or Inf.
43 s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
44 s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt);
45}
46
Chris Dalton0a793812018-03-07 11:18:30 -070047void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
48 const char* leftPt,
49 const char* rightPt,
50 const char* rasterVertexDir,
51 const char* outputCoverage) {
52 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
53 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
54 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
55 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
56 // smooth antialiased edges.
57 //
58 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
59 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
60 // centered on P.)
61 //
62 // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose
63 // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is
64 // centered on P.)
65 //
66 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
67 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
68 //
69 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
70 //
71 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
72 //
73 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
74 rightPt, leftPt, leftPt, rightPt);
75 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
76 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
77 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
78 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
79 s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage);
80}
Chris Daltonbaf3e782018-03-08 15:55:58 +000081
Chris Dalton8738cf42018-03-09 11:57:40 -070082void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
83 const char* leftPt,
84 const char* rightPt,
85 const char* bloatDir1,
86 const char* bloatDir2,
87 const char* outputCoverages) {
88 // See comments in CalcEdgeCoverageAtBloatVertex.
89 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
90 rightPt, leftPt, leftPt, rightPt);
91 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
92 s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
93 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
94 s->codeAppendf( "%s[i] = (abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
95 outputCoverages);
96 s->codeAppendf("}");
97}
98
Chris Dalton04a1de52018-03-14 02:04:09 -060099void GrCCCoverageProcessor::Shader::CalcCornerCoverageAttenuation(GrGLSLVertexGeoBuilder* s,
100 const char* leftDir,
101 const char* rightDir,
102 const char* outputAttenuation) {
103 // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
104 // 0 if corner_angle <= 90 degrees
105 s->codeAppendf("half obtuseness = max(dot(%s, %s), 0);", leftDir, rightDir);
106
107 // axis_alignedness = 1 when the leftDir/rightDir bisector is aligned with the x- or y-axis
108 // 0 when the bisector falls on a 45 degree angle
109 // (i.e. 1 - tan(angle_to_nearest_axis))
110 s->codeAppendf("half2 abs_bisect = abs(%s - %s);", leftDir, rightDir);
111 s->codeAppend ("half axis_alignedness = 1 - min(abs_bisect.y, abs_bisect.x) / "
112 "max(abs_bisect.x, abs_bisect.y);");
113
114 // ninety_degreesness = sin^2(corner_angle)
115 // sin^2 just because... it's always positive and the results looked better than plain sine... ?
116 s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
117 s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
118
119 // The below formula is not smart. It was just arrived at by considering the following
120 // observations:
121 //
122 // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
123 // (i.e. coverage = 1 - distance_to_corner^2)
124 // (i.e. outputAttenuation = 0)
125 //
126 // 2. 180-degree corners always have zero attenuation.
127 // (i.e. coverage = 1 - distance_to_corner)
128 // (i.e. outputAttenuation = 1)
129 //
130 // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
131 // (i.e. outputAttenuation = 1)
132 s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
133 outputAttenuation);
134}
135
Chris Daltonbaf3e782018-03-08 15:55:58 +0000136int GrCCCoverageProcessor::Shader::DefineSoftSampleLocations(GrGLSLFPFragmentBuilder* f,
137 const char* samplesName) {
138 // Standard DX11 sample locations.
139#if defined(SK_BUILD_FOR_ANDROID) || defined(SK_BUILD_FOR_IOS)
140 f->defineConstant("float2[8]", samplesName, "float2[8]("
141 "float2(+1, -3)/16, float2(-1, +3)/16, float2(+5, +1)/16, float2(-3, -5)/16, "
142 "float2(-5, +5)/16, float2(-7, -1)/16, float2(+3, +7)/16, float2(+7, -7)/16."
143 ")");
144 return 8;
145#else
146 f->defineConstant("float2[16]", samplesName, "float2[16]("
147 "float2(+1, +1)/16, float2(-1, -3)/16, float2(-3, +2)/16, float2(+4, -1)/16, "
148 "float2(-5, -2)/16, float2(+2, +5)/16, float2(+5, +3)/16, float2(+3, -5)/16, "
149 "float2(-2, +6)/16, float2( 0, -7)/16, float2(-4, -6)/16, float2(-6, +4)/16, "
150 "float2(-8, 0)/16, float2(+7, -4)/16, float2(+6, +7)/16, float2(-7, -8)/16."
151 ")");
152 return 16;
153#endif
154}
155
156void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&,
157 GrProcessorKeyBuilder* b) const {
158 int key = (int)fRenderPass << 2;
159 if (WindMethod::kInstanceData == fWindMethod) {
160 key |= 2;
161 }
162 if (Impl::kVertexShader == fImpl) {
163 key |= 1;
164 }
165#ifdef SK_DEBUG
166 uint32_t bloatBits;
167 memcpy(&bloatBits, &fDebugBloat, 4);
168 b->add32(bloatBits);
169#endif
170 b->add32(key);
171}
172
173GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
174 std::unique_ptr<Shader> shader;
175 switch (fRenderPass) {
176 case RenderPass::kTriangles:
Chris Daltonbaf3e782018-03-08 15:55:58 +0000177 case RenderPass::kTriangleCorners:
Chris Dalton8738cf42018-03-09 11:57:40 -0700178 shader = skstd::make_unique<GrCCTriangleShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000179 break;
180 case RenderPass::kQuadratics:
181 shader = skstd::make_unique<GrCCQuadraticHullShader>();
182 break;
183 case RenderPass::kQuadraticCorners:
184 shader = skstd::make_unique<GrCCQuadraticCornerShader>();
185 break;
186 case RenderPass::kCubics:
187 shader = skstd::make_unique<GrCCCubicHullShader>();
188 break;
189 case RenderPass::kCubicCorners:
190 shader = skstd::make_unique<GrCCCubicCornerShader>();
191 break;
192 }
193 return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader))
194 : this->createVSImpl(std::move(shader));
195}