blob: 76ca8f562ea38bca76336881eafc783ce1b70a12 [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 Daltondf04ce22018-03-07 17:28:07 -070018void GrCCCoverageProcessor::getGLSLProcessorKey(const GrShaderCaps&,
19 GrProcessorKeyBuilder* b) const {
Chris Daltonfe462ef2018-03-08 15:54:01 +000020 int key = (int)fRenderPass << 2;
Chris Daltondf04ce22018-03-07 17:28:07 -070021 if (WindMethod::kInstanceData == fWindMethod) {
22 key |= 2;
23 }
24 if (Impl::kVertexShader == fImpl) {
25 key |= 1;
26 }
27#ifdef SK_DEBUG
28 uint32_t bloatBits;
29 memcpy(&bloatBits, &fDebugBloat, 4);
30 b->add32(bloatBits);
31#endif
32 b->add32(key);
33}
34
35GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
36 std::unique_ptr<Shader> shader;
37 switch (fRenderPass) {
38 case RenderPass::kTriangles:
Chris Daltonfe462ef2018-03-08 15:54:01 +000039 shader = skstd::make_unique<GrCCTriangleShader>();
40 break;
41 case RenderPass::kTriangleCorners:
42 shader = skstd::make_unique<GrCCTriangleCornerShader>();
Chris Daltondf04ce22018-03-07 17:28:07 -070043 break;
44 case RenderPass::kQuadratics:
45 shader = skstd::make_unique<GrCCQuadraticShader>();
46 break;
47 case RenderPass::kCubics:
48 shader = skstd::make_unique<GrCCCubicShader>();
49 break;
50 }
51 return Impl::kGeometryShader == fImpl ? this->createGSImpl(std::move(shader))
52 : this->createVSImpl(std::move(shader));
53}
54
Chris Dalton383a2ef2018-01-08 17:21:41 -050055void GrCCCoverageProcessor::Shader::emitFragmentCode(const GrCCCoverageProcessor& proc,
Chris Dalton60283612018-02-14 13:38:14 -070056 GrGLSLFPFragmentBuilder* f,
Chris Dalton383a2ef2018-01-08 17:21:41 -050057 const char* skOutputColor,
58 const char* skOutputCoverage) const {
Chris Daltonfe462ef2018-03-08 15:54:01 +000059 f->codeAppendf("half coverage = 0;");
Chris Daltondf04ce22018-03-07 17:28:07 -070060 this->onEmitFragmentCode(proc, f, "coverage");
Chris Daltonf510e262018-01-30 16:42:37 -070061 f->codeAppendf("%s.a = coverage;", skOutputColor);
Chris Dalton6a3dbee2017-10-16 10:44:41 -060062 f->codeAppendf("%s = half4(1);", skOutputCoverage);
Chris Dalton1a325d22017-07-14 15:17:41 -060063#ifdef SK_DEBUG
Chris Daltona640c492017-09-11 22:04:03 -070064 if (proc.debugVisualizationsEnabled()) {
Chris Daltondf04ce22018-03-07 17:28:07 -070065 f->codeAppendf("%s = half4(-%s.a, %s.a, 0, abs(%s.a));",
66 skOutputColor, skOutputColor, skOutputColor, skOutputColor);
Eric Borend6365e52017-10-16 12:31:14 +000067 }
68#endif
Eric Borend6365e52017-10-16 12:31:14 +000069}
70
Chris Dalton0a793812018-03-07 11:18:30 -070071void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
72 const char* leftPt,
73 const char* rightPt,
74 const char* rasterVertexDir,
75 const char* outputCoverage) {
76 // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
77 // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
78 // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
79 // the center. Interpolated, these coverage values convert jagged conservative raster edges into
80 // smooth antialiased edges.
81 //
82 // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
83 // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
84 // centered on P.)
85 //
86 // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose
87 // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is
88 // centered on P.)
89 //
90 // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
91 // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
92 //
93 // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
94 //
95 // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
96 //
97 s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
98 rightPt, leftPt, leftPt, rightPt);
99 s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
100 s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
101 // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
102 // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
103 s->codeAppendf("%s = (abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;", outputCoverage);
104}