blob: c44d4648269f4a82aed7a16fea4a7a613717a595 [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 Dalton8dfc70f2018-03-26 19:15:22 -060010#include "GrGpuCommandBuffer.h"
11#include "GrOpFlushState.h"
Chris Dalton6a3dbee2017-10-16 10:44:41 -060012#include "SkMakeUnique.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050013#include "ccpr/GrCCCubicShader.h"
14#include "ccpr/GrCCQuadraticShader.h"
Chris Daltonfe462ef2018-03-08 15:54:01 +000015#include "ccpr/GrCCTriangleShader.h"
Chris Dalton90e8fb12017-12-22 02:24:53 -070016#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060017#include "glsl/GrGLSLFragmentShaderBuilder.h"
Chris Dalton1fbdb612017-12-12 12:48:47 -070018#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton1a325d22017-07-14 15:17:41 -060019
Chris Dalton383a2ef2018-01-08 17:21:41 -050020void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc,
Chris Dalton60283612018-02-14 13:38:14 -070021 GrGLSLFPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050022 const char* skOutputColor,
23 const char* skOutputCoverage) const {
Chris Daltonfe462ef2018-03-08 15:54:01 +000024 f->codeAppendf("half coverage = 0;");
Chris Daltonbaf3e782018-03-08 15:55:58 +000025 this->onEmitFragmentCode(f, "coverage");
Chris Daltonf510e262018-01-30 16:42:37 -070026 f->codeAppendf("%s.a = coverage;", skOutputColor);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060027 f->codeAppendf("%s = half4(1);", skOutputCoverage);
Eric Borend6365e52017-10-16 12:31:14 +000028}
29
Chris Daltonbaf3e782018-03-08 15:55:58 +000030void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s,
31 const char* leftPt,
32 const char* rightPt,
33 const char* outputDistanceEquation) {
34 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
35 rightPt, leftPt, leftPt, rightPt);
36 s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
37 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we
38 // come up with here as long as it isn't NaN or Inf.
39 s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
40 s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt);
41}
42
Chris Dalton0a793812018-03-07 11:18:30 -070043void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
44 const char* leftPt,
45 const char* rightPt,
46 const char* rasterVertexDir,
47 const char* outputCoverage) {
48 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
49 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
50 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
51 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
52 // smooth antialiased edges.
53 //
54 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
55 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
56 // centered on P.)
57 //
58 // d0 == (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=0, where the bloat box is
60 // centered on P.)
61 //
62 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
63 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
64 //
65 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
66 //
67 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
68 //
69 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
70 rightPt, leftPt, leftPt, rightPt);
71 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
72 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
73 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
74 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
75 s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage);
76}
Chris Daltonbaf3e782018-03-08 15:55:58 +000077
Chris Dalton8738cf42018-03-09 11:57:40 -070078void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
79 const char* leftPt,
80 const char* rightPt,
81 const char* bloatDir1,
82 const char* bloatDir2,
83 const char* outputCoverages) {
84 // See comments in CalcEdgeCoverageAtBloatVertex.
85 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
86 rightPt, leftPt, leftPt, rightPt);
87 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
88 s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
89 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
90 s->codeAppendf( "%s[i] = (abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
91 outputCoverages);
92 s->codeAppendf("}");
93}
94
Chris Dalton04a1de52018-03-14 02:04:09 -060095void GrCCCoverageProcessor::Shader::CalcCornerCoverageAttenuation(GrGLSLVertexGeoBuilder* s,
96 const char* leftDir,
97 const char* rightDir,
98 const char* outputAttenuation) {
99 // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
100 // 0 if corner_angle <= 90 degrees
101 s->codeAppendf("half obtuseness = max(dot(%s, %s), 0);", leftDir, rightDir);
102
103 // axis_alignedness = 1 when the leftDir/rightDir bisector is aligned with the x- or y-axis
104 // 0 when the bisector falls on a 45 degree angle
105 // (i.e. 1 - tan(angle_to_nearest_axis))
106 s->codeAppendf("half2 abs_bisect = abs(%s - %s);", leftDir, rightDir);
107 s->codeAppend ("half axis_alignedness = 1 - min(abs_bisect.y, abs_bisect.x) / "
108 "max(abs_bisect.x, abs_bisect.y);");
109
110 // ninety_degreesness = sin^2(corner_angle)
111 // sin^2 just because... it's always positive and the results looked better than plain sine... ?
112 s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
113 s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
114
115 // The below formula is not smart. It was just arrived at by considering the following
116 // observations:
117 //
118 // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
119 // (i.e. coverage = 1 - distance_to_corner^2)
120 // (i.e. outputAttenuation = 0)
121 //
122 // 2. 180-degree corners always have zero attenuation.
123 // (i.e. coverage = 1 - distance_to_corner)
124 // (i.e. outputAttenuation = 1)
125 //
126 // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
127 // (i.e. outputAttenuation = 1)
128 s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
129 outputAttenuation);
130}
131
Chris Daltonbaf3e782018-03-08 15:55:58 +0000132void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&,
133 GrProcessorKeyBuilder* b) const {
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600134 int key = (int)fPrimitiveType << 3;
135 if (GSSubpass::kCorners == fGSSubpass) {
136 key |= 4;
137 }
Chris Daltonbaf3e782018-03-08 15:55:58 +0000138 if (WindMethod::kInstanceData == fWindMethod) {
139 key |= 2;
140 }
141 if (Impl::kVertexShader == fImpl) {
142 key |= 1;
143 }
144#ifdef SK_DEBUG
145 uint32_t bloatBits;
146 memcpy(&bloatBits, &fDebugBloat, 4);
147 b->add32(bloatBits);
148#endif
149 b->add32(key);
150}
151
152GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
153 std::unique_ptr<Shader> shader;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600154 switch (fPrimitiveType) {
155 case PrimitiveType::kTriangles:
Chris Dalton8738cf42018-03-09 11:57:40 -0700156 shader = skstd::make_unique<GrCCTriangleShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000157 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600158 case PrimitiveType::kQuadratics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600159 shader = skstd::make_unique<GrCCQuadraticShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000160 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600161 case PrimitiveType::kCubics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600162 shader = skstd::make_unique<GrCCCubicShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000163 break;
164 }
165 return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader))
166 : this->createVSImpl(std::move(shader));
167}
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600168
169void GrCCCoverageProcessor::draw(GrOpFlushState* flushState, const GrPipeline& pipeline,
170 const GrMesh meshes[],
171 const GrPipeline::DynamicState dynamicStates[], int meshCount,
172 const SkRect& drawBounds) const {
173 GrGpuRTCommandBuffer* cmdBuff = flushState->rtCommandBuffer();
174 cmdBuff->draw(pipeline, *this, meshes, dynamicStates, meshCount, drawBounds);
175
176 // Geometry shader backend draws primitives in two subpasses.
177 if (Impl::kGeometryShader == fImpl) {
178 SkASSERT(GSSubpass::kHulls == fGSSubpass);
179 GrCCCoverageProcessor cornerProc(*this, GSSubpass::kCorners);
180 cmdBuff->draw(pipeline, cornerProc, meshes, dynamicStates, meshCount, drawBounds);
181 }
182}