Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 28 | class GrCCPRTriangleProcessor : public GrCCPRCoverageProcessor::PrimitiveProcessor { |
| 29 | public: |
| 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 | |
| 37 | protected: |
| 38 | void defineInputVertices(GrGLSLGeometryBuilder*) const; |
| 39 | |
| 40 | private: |
| 41 | typedef GrCCPRCoverageProcessor::PrimitiveProcessor INHERITED; |
| 42 | }; |
| 43 | |
| 44 | class GrCCPRTriangleHullAndEdgeProcessor : public GrCCPRTriangleProcessor { |
| 45 | public: |
| 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 | |
| 60 | private: |
| 61 | const GeometryType fGeometryType; |
| 62 | |
| 63 | typedef GrCCPRTriangleProcessor INHERITED; |
| 64 | }; |
| 65 | |
| 66 | /** |
Chris Dalton | 71e3797 | 2017-09-18 22:23:53 -0600 | [diff] [blame] | 67 | * 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 Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 70 | */ |
| 71 | class GrCCPRTriangleCornerProcessor : public GrCCPRTriangleProcessor { |
| 72 | public: |
| 73 | GrCCPRTriangleCornerProcessor() |
| 74 | : INHERITED(CoverageType::kShader) |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 75 | , 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 Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 80 | |
| 81 | void resetVaryings(GrGLSLVaryingHandler* varyingHandler) override { |
| 82 | this->INHERITED::resetVaryings(varyingHandler); |
Chris Dalton | 71e3797 | 2017-09-18 22:23:53 -0600 | [diff] [blame] | 83 | varyingHandler->addVarying("corner_location_in_aa_boxes", &fCornerLocationInAABoxes); |
| 84 | varyingHandler->addFlatVarying("bisect_in_aa_boxes", &fBisectInAABoxes); |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 85 | } |
| 86 | |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 87 | 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 | |
| 93 | private: |
Chris Dalton | 71e3797 | 2017-09-18 22:23:53 -0600 | [diff] [blame] | 94 | GrShaderVar fAABoxMatrices; |
| 95 | GrShaderVar fAABoxTranslates; |
| 96 | GrShaderVar fGeoShaderBisects; |
| 97 | GrGLSLGeoToFrag fCornerLocationInAABoxes; |
| 98 | GrGLSLGeoToFrag fBisectInAABoxes; |
Chris Dalton | 1a325d2 | 2017-07-14 15:17:41 -0600 | [diff] [blame] | 99 | |
| 100 | typedef GrCCPRTriangleProcessor INHERITED; |
| 101 | }; |
| 102 | |
| 103 | #endif |