blob: 8e7249e9e48a37efcc4d43efa02198b662a449e6 [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);
Eric Borend6365e52017-10-16 12:31:14 +000026}
27
Chris Daltonbaf3e782018-03-08 15:55:58 +000028void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s,
29 const char* leftPt,
30 const char* rightPt,
31 const char* outputDistanceEquation) {
32 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
33 rightPt, leftPt, leftPt, rightPt);
34 s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
35 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we
36 // come up with here as long as it isn't NaN or Inf.
37 s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
38 s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt);
39}
40
Chris Dalton0a793812018-03-07 11:18:30 -070041void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
42 const char* leftPt,
43 const char* rightPt,
44 const char* rasterVertexDir,
45 const char* outputCoverage) {
46 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
47 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
48 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
49 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
50 // smooth antialiased edges.
51 //
52 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
53 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
54 // centered on P.)
55 //
56 // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose
57 // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is
58 // centered on P.)
59 //
60 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
61 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
62 //
63 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
64 //
65 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
66 //
67 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
68 rightPt, leftPt, leftPt, rightPt);
69 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
70 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
71 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
72 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
73 s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage);
74}
Chris Daltonbaf3e782018-03-08 15:55:58 +000075
Chris Dalton8738cf42018-03-09 11:57:40 -070076void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
77 const char* leftPt,
78 const char* rightPt,
79 const char* bloatDir1,
80 const char* bloatDir2,
81 const char* outputCoverages) {
82 // See comments in CalcEdgeCoverageAtBloatVertex.
83 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
84 rightPt, leftPt, leftPt, rightPt);
85 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
86 s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
87 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
88 s->codeAppendf( "%s[i] = (abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
89 outputCoverages);
90 s->codeAppendf("}");
91}
92
Chris Dalton04a1de52018-03-14 02:04:09 -060093void GrCCCoverageProcessor::Shader::CalcCornerCoverageAttenuation(GrGLSLVertexGeoBuilder* s,
94 const char* leftDir,
95 const char* rightDir,
96 const char* outputAttenuation) {
97 // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
98 // 0 if corner_angle <= 90 degrees
99 s->codeAppendf("half obtuseness = max(dot(%s, %s), 0);", leftDir, rightDir);
100
101 // axis_alignedness = 1 when the leftDir/rightDir bisector is aligned with the x- or y-axis
102 // 0 when the bisector falls on a 45 degree angle
103 // (i.e. 1 - tan(angle_to_nearest_axis))
104 s->codeAppendf("half2 abs_bisect = abs(%s - %s);", leftDir, rightDir);
105 s->codeAppend ("half axis_alignedness = 1 - min(abs_bisect.y, abs_bisect.x) / "
106 "max(abs_bisect.x, abs_bisect.y);");
107
108 // ninety_degreesness = sin^2(corner_angle)
109 // sin^2 just because... it's always positive and the results looked better than plain sine... ?
110 s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
111 s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
112
113 // The below formula is not smart. It was just arrived at by considering the following
114 // observations:
115 //
116 // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
117 // (i.e. coverage = 1 - distance_to_corner^2)
118 // (i.e. outputAttenuation = 0)
119 //
120 // 2. 180-degree corners always have zero attenuation.
121 // (i.e. coverage = 1 - distance_to_corner)
122 // (i.e. outputAttenuation = 1)
123 //
124 // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
125 // (i.e. outputAttenuation = 1)
126 s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
127 outputAttenuation);
128}
129
Chris Daltonbaf3e782018-03-08 15:55:58 +0000130void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&,
131 GrProcessorKeyBuilder* b) const {
132 int key = (int)fRenderPass << 2;
133 if (WindMethod::kInstanceData == fWindMethod) {
134 key |= 2;
135 }
136 if (Impl::kVertexShader == fImpl) {
137 key |= 1;
138 }
139#ifdef SK_DEBUG
140 uint32_t bloatBits;
141 memcpy(&bloatBits, &fDebugBloat, 4);
142 b->add32(bloatBits);
143#endif
144 b->add32(key);
145}
146
147GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
148 std::unique_ptr<Shader> shader;
149 switch (fRenderPass) {
150 case RenderPass::kTriangles:
Chris Daltonbaf3e782018-03-08 15:55:58 +0000151 case RenderPass::kTriangleCorners:
Chris Dalton8738cf42018-03-09 11:57:40 -0700152 shader = skstd::make_unique<GrCCTriangleShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000153 break;
154 case RenderPass::kQuadratics:
Chris Daltonbaf3e782018-03-08 15:55:58 +0000155 case RenderPass::kQuadraticCorners:
Chris Dalton21ba5512018-03-21 17:20:21 -0600156 shader = skstd::make_unique<GrCCQuadraticShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000157 break;
158 case RenderPass::kCubics:
Chris Daltonbaf3e782018-03-08 15:55:58 +0000159 case RenderPass::kCubicCorners:
Chris Dalton21ba5512018-03-21 17:20:21 -0600160 shader = skstd::make_unique<GrCCCubicShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000161 break;
162 }
163 return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader))
164 : this->createVSImpl(std::move(shader));
165}