blob: ca143d1edb6ddf51d34ffd29293ada6ac052aaeb [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
8#ifndef GrCCPRTriangleProcessor_DEFINED
9#define GrCCPRTriangleProcessor_DEFINED
10
11#include "ccpr/GrCCPRCoverageProcessor.h"
12
13/**
14 * This class renders the coverage of triangles.
15 *
16 * Triangles are rendered in three passes:
17 *
18 * Pass 1: Draw the triangle's conservative raster hull with a coverage of 1. (Conservative raster
19 * is drawn by considering 3 pixel size boxes, one centered at each vertex, and drawing the
20 * convex hull of those boxes.)
21 *
22 * Pass 2: Smooth the edges that were over-rendered during Pass 1. Draw the conservative raster of
23 * each edge (i.e. convex hull of two pixel-size boxes at the endpoints), interpolating from
24 * coverage=-1 on the outside edge to coverage=0 on the inside edge.
25 *
26 * Pass 3: Touch up the corner pixels to have the correct coverage.
27 */
28class GrCCPRTriangleProcessor : public GrCCPRCoverageProcessor::PrimitiveProcessor {
29public:
30 GrCCPRTriangleProcessor(CoverageType initialCoverage) : INHERITED(initialCoverage) {}
31
32 void onEmitVertexShader(const GrCCPRCoverageProcessor&, GrGLSLVertexBuilder*,
33 const TexelBufferHandle& pointsBuffer, const char* atlasOffset,
34 const char* rtAdjust, GrGPArgs*) const override;
35 void emitWind(GrGLSLGeometryBuilder*, const char* rtAdjust, const char* outputWind) const final;
36
37protected:
38 void defineInputVertices(GrGLSLGeometryBuilder*) const;
39
40private:
41 typedef GrCCPRCoverageProcessor::PrimitiveProcessor INHERITED;
42};
43
44class GrCCPRTriangleHullAndEdgeProcessor : public GrCCPRTriangleProcessor {
45public:
46 enum class GeometryType {
47 kHulls,
48 kEdges,
49 kHullsAndEdges
50 };
51
52 GrCCPRTriangleHullAndEdgeProcessor(GeometryType geometryType)
53 : INHERITED(GeometryType::kHulls == geometryType ?
54 CoverageType::kOne : CoverageType::kInterpolated)
55 , fGeometryType(geometryType) {}
56
57 void onEmitGeometryShader(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* wind,
58 const char* rtAdjust) const override;
59
60private:
61 const GeometryType fGeometryType;
62
63 typedef GrCCPRTriangleProcessor INHERITED;
64};
65
66/**
Chris Dalton71e37972017-09-18 22:23:53 -060067 * This pass fixes the corner pixels of a triangle. It touches up the simple distance-to-edge
68 * coverage analysis done previously so that it takes into account the region that is outside both
69 * edges at the same time.
Chris Dalton1a325d22017-07-14 15:17:41 -060070 */
71class GrCCPRTriangleCornerProcessor : public GrCCPRTriangleProcessor {
72public:
73 GrCCPRTriangleCornerProcessor()
74 : INHERITED(CoverageType::kShader)
Ethan Nicholas8aa45692017-09-20 11:24:15 -040075 , fAABoxMatrices("aa_box_matrices", kFloat2x2_GrSLType, 2)
76 , fAABoxTranslates("aa_box_translates", kFloat2_GrSLType, 2)
77 , fGeoShaderBisects("bisects", kFloat2_GrSLType, 2)
78 , fCornerLocationInAABoxes(kFloat2x2_GrSLType)
79 , fBisectInAABoxes(kFloat2x2_GrSLType) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060080
81 void resetVaryings(GrGLSLVaryingHandler* varyingHandler) override {
82 this->INHERITED::resetVaryings(varyingHandler);
Chris Dalton71e37972017-09-18 22:23:53 -060083 varyingHandler->addVarying("corner_location_in_aa_boxes", &fCornerLocationInAABoxes);
84 varyingHandler->addFlatVarying("bisect_in_aa_boxes", &fBisectInAABoxes);
Chris Dalton1a325d22017-07-14 15:17:41 -060085 }
86
Chris Dalton1a325d22017-07-14 15:17:41 -060087 void onEmitGeometryShader(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* wind,
88 const char* rtAdjust) const override;
89 void emitPerVertexGeometryCode(SkString* fnBody, const char* position, const char* coverage,
90 const char* wind) const override;
91 void emitShaderCoverage(GrGLSLFragmentBuilder*, const char* outputCoverage) const override;
92
93private:
Chris Dalton71e37972017-09-18 22:23:53 -060094 GrShaderVar fAABoxMatrices;
95 GrShaderVar fAABoxTranslates;
96 GrShaderVar fGeoShaderBisects;
97 GrGLSLGeoToFrag fCornerLocationInAABoxes;
98 GrGLSLGeoToFrag fBisectInAABoxes;
Chris Dalton1a325d22017-07-14 15:17:41 -060099
100 typedef GrCCPRTriangleProcessor INHERITED;
101};
102
103#endif