blob: d38db27a19fc4ba4f5de99813dcdb80724d52864 [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 Dalton9f2dab02018-04-18 14:07:03 -060013#include "ccpr/GrCCConicShader.h"
Chris Dalton383a2ef2018-01-08 17:21:41 -050014#include "ccpr/GrCCCubicShader.h"
15#include "ccpr/GrCCQuadraticShader.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 Dalton4c239342018-04-05 18:43:40 -060020class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader {
21 void onEmitVaryings(GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope,
22 SkString* code, const char* position, const char* coverage,
23 const char* cornerCoverage) override {
24 if (!cornerCoverage) {
25 fCoverages.reset(kHalf_GrSLType, scope);
26 varyingHandler->addVarying("coverage", &fCoverages);
27 code->appendf("%s = %s;", OutName(fCoverages), coverage);
28 } else {
29 fCoverages.reset(kHalf3_GrSLType, scope);
30 varyingHandler->addVarying("coverages", &fCoverages);
31 code->appendf("%s = half3(%s, %s);", OutName(fCoverages), coverage, cornerCoverage);
32 }
33 }
34
35 void onEmitFragmentCode(GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override {
36 if (kHalf_GrSLType == fCoverages.type()) {
37 f->codeAppendf("%s = %s;", outputCoverage, fCoverages.fsIn());
38 } else {
39 f->codeAppendf("%s = %s.z * %s.y + %s.x;",
40 outputCoverage, fCoverages.fsIn(), fCoverages.fsIn(), fCoverages.fsIn());
41 }
42 }
43
44 GrGLSLVarying fCoverages;
45};
Eric Borend6365e52017-10-16 12:31:14 +000046
Chris Daltonbaf3e782018-03-08 15:55:58 +000047void GrCCCoverageProcessor::Shader::EmitEdgeDistanceEquation(GrGLSLVertexGeoBuilder* s,
48 const char* leftPt,
49 const char* rightPt,
50 const char* outputDistanceEquation) {
51 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
52 rightPt, leftPt, leftPt, rightPt);
53 s->codeAppend ("float nwidth = (abs(n.x) + abs(n.y)) * (bloat * 2);");
54 // When nwidth=0, wind must also be 0 (and coverage * wind = 0). So it doesn't matter what we
55 // come up with here as long as it isn't NaN or Inf.
56 s->codeAppend ("n /= (0 != nwidth) ? nwidth : 1;");
57 s->codeAppendf("%s = float3(-n, dot(n, %s) - .5);", outputDistanceEquation, leftPt);
58}
59
Chris Dalton0a793812018-03-07 11:18:30 -070060void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
61 const char* leftPt,
62 const char* rightPt,
63 const char* rasterVertexDir,
64 const char* outputCoverage) {
65 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
66 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
67 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
68 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
69 // smooth antialiased edges.
70 //
71 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
72 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
73 // centered on P.)
74 //
75 // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose
76 // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is
77 // centered on P.)
78 //
79 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
80 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
81 //
82 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
83 //
84 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
85 //
86 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
87 rightPt, leftPt, leftPt, rightPt);
88 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
89 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
90 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
91 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
92 s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage);
93}
Chris Daltonbaf3e782018-03-08 15:55:58 +000094
Chris Dalton8738cf42018-03-09 11:57:40 -070095void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
96 const char* leftPt,
97 const char* rightPt,
98 const char* bloatDir1,
99 const char* bloatDir2,
100 const char* outputCoverages) {
101 // See comments in CalcEdgeCoverageAtBloatVertex.
102 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
103 rightPt, leftPt, leftPt, rightPt);
104 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
105 s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
106 s->codeAppendf("for (int i = 0; i < 2; ++i) {");
107 s->codeAppendf( "%s[i] = (abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
108 outputCoverages);
109 s->codeAppendf("}");
110}
111
Chris Dalton4c239342018-04-05 18:43:40 -0600112void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s,
113 const char* leftDir, const char* rightDir,
114 const char* outputAttenuation) {
Chris Dalton04a1de52018-03-14 02:04:09 -0600115 // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
116 // 0 if corner_angle <= 90 degrees
117 s->codeAppendf("half obtuseness = max(dot(%s, %s), 0);", leftDir, rightDir);
118
119 // axis_alignedness = 1 when the leftDir/rightDir bisector is aligned with the x- or y-axis
120 // 0 when the bisector falls on a 45 degree angle
121 // (i.e. 1 - tan(angle_to_nearest_axis))
122 s->codeAppendf("half2 abs_bisect = abs(%s - %s);", leftDir, rightDir);
123 s->codeAppend ("half axis_alignedness = 1 - min(abs_bisect.y, abs_bisect.x) / "
124 "max(abs_bisect.x, abs_bisect.y);");
125
126 // ninety_degreesness = sin^2(corner_angle)
127 // sin^2 just because... it's always positive and the results looked better than plain sine... ?
128 s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
129 s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
130
131 // The below formula is not smart. It was just arrived at by considering the following
132 // observations:
133 //
134 // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
135 // (i.e. coverage = 1 - distance_to_corner^2)
136 // (i.e. outputAttenuation = 0)
137 //
138 // 2. 180-degree corners always have zero attenuation.
139 // (i.e. coverage = 1 - distance_to_corner)
140 // (i.e. outputAttenuation = 1)
141 //
142 // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
143 // (i.e. outputAttenuation = 1)
144 s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
145 outputAttenuation);
146}
147
Chris Daltonbaf3e782018-03-08 15:55:58 +0000148void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&,
149 GrProcessorKeyBuilder* b) const {
Chris Dalton703b4762018-04-06 16:11:48 -0600150 int key = (int)fPrimitiveType << 2;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600151 if (GSSubpass::kCorners == fGSSubpass) {
Chris Daltonbaf3e782018-03-08 15:55:58 +0000152 key |= 2;
153 }
154 if (Impl::kVertexShader == fImpl) {
155 key |= 1;
156 }
157#ifdef SK_DEBUG
158 uint32_t bloatBits;
159 memcpy(&bloatBits, &fDebugBloat, 4);
160 b->add32(bloatBits);
161#endif
162 b->add32(key);
163}
164
165GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
166 std::unique_ptr<Shader> shader;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600167 switch (fPrimitiveType) {
168 case PrimitiveType::kTriangles:
Chris Dalton703b4762018-04-06 16:11:48 -0600169 case PrimitiveType::kWeightedTriangles:
Chris Dalton4c239342018-04-05 18:43:40 -0600170 shader = skstd::make_unique<TriangleShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000171 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600172 case PrimitiveType::kQuadratics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600173 shader = skstd::make_unique<GrCCQuadraticShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000174 break;
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600175 case PrimitiveType::kCubics:
Chris Dalton21ba5512018-03-21 17:20:21 -0600176 shader = skstd::make_unique<GrCCCubicShader>();
Chris Daltonbaf3e782018-03-08 15:55:58 +0000177 break;
Chris Dalton9f2dab02018-04-18 14:07:03 -0600178 case PrimitiveType::kConics:
179 shader = skstd::make_unique<GrCCConicShader>();
180 break;
Chris Daltonbaf3e782018-03-08 15:55:58 +0000181 }
182 return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader))
183 : this->createVSImpl(std::move(shader));
184}
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600185
Chris Dalton4c239342018-04-05 18:43:40 -0600186void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc,
187 GrGLSLFPFragmentBuilder* f,
188 const char* skOutputColor,
189 const char* skOutputCoverage) const {
190 f->codeAppendf("half coverage = 0;");
191 this->onEmitFragmentCode(f, "coverage");
192 f->codeAppendf("%s.a = coverage;", skOutputColor);
193 f->codeAppendf("%s = half4(1);", skOutputCoverage);
194}
195
Chris Dalton8dfc70f2018-03-26 19:15:22 -0600196void GrCCCoverageProcessor::draw(GrOpFlushState* flushState, const GrPipeline& pipeline,
197 const GrMesh meshes[],
198 const GrPipeline::DynamicState dynamicStates[], int meshCount,
199 const SkRect& drawBounds) const {
200 GrGpuRTCommandBuffer* cmdBuff = flushState->rtCommandBuffer();
201 cmdBuff->draw(pipeline, *this, meshes, dynamicStates, meshCount, drawBounds);
202
203 // Geometry shader backend draws primitives in two subpasses.
204 if (Impl::kGeometryShader == fImpl) {
205 SkASSERT(GSSubpass::kHulls == fGSSubpass);
206 GrCCCoverageProcessor cornerProc(*this, GSSubpass::kCorners);
207 cmdBuff->draw(pipeline, cornerProc, meshes, dynamicStates, meshCount, drawBounds);
208 }
209}