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