blob: 20ca3f25477b86adede414ab186eca90c292c0b1 [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 GrCCPRCubicProcessor_DEFINED
9#define GrCCPRCubicProcessor_DEFINED
10
11#include "ccpr/GrCCPRCoverageProcessor.h"
12
13class GrGLSLGeometryBuilder;
14
15/**
16 * This class renders the coverage of convex closed cubic segments using the techniques outlined in
17 * "Resolution Independent Curve Rendering using Programmable Graphics Hardware" by Charles Loop and
18 * Jim Blinn:
19 *
20 * https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
21 *
Chris Dalton7f578bf2017-09-05 16:46:48 -060022 * The provided curves must be convex, monotonic with respect to the vector of their closing edge
23 * [P3 - P0], and must not contain or be near any inflection points or loop intersections.
24 * (Use GrCCPRGeometry.)
Chris Dalton1a325d22017-07-14 15:17:41 -060025 */
26class GrCCPRCubicProcessor : public GrCCPRCoverageProcessor::PrimitiveProcessor {
27public:
Chris Dalton7f578bf2017-09-05 16:46:48 -060028 enum class CubicType {
Chris Dalton1a325d22017-07-14 15:17:41 -060029 kSerpentine,
30 kLoop
31 };
32
Chris Dalton7f578bf2017-09-05 16:46:48 -060033 GrCCPRCubicProcessor(CubicType cubicType)
Chris Dalton1a325d22017-07-14 15:17:41 -060034 : INHERITED(CoverageType::kShader)
Chris Dalton7f578bf2017-09-05 16:46:48 -060035 , fCubicType(cubicType)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040036 , fKLMMatrix("klm_matrix", kHighFloat3x3_GrSLType, GrShaderVar::kNonArray,
Brian Salomon1d816b92017-08-17 11:07:59 -040037 kHigh_GrSLPrecision)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040038 , fKLMDerivatives("klm_derivatives", kHighFloat2_GrSLType, 3, kHigh_GrSLPrecision)
39 , fEdgeDistanceEquation("edge_distance_equation", kHighFloat3_GrSLType,
Chris Dalton7f578bf2017-09-05 16:46:48 -060040 GrShaderVar::kNonArray, kHigh_GrSLPrecision)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040041 , fKLMD(kHighFloat4_GrSLType) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060042
43 void resetVaryings(GrGLSLVaryingHandler* varyingHandler) override {
Chris Dalton7f578bf2017-09-05 16:46:48 -060044 varyingHandler->addVarying("klmd", &fKLMD, kHigh_GrSLPrecision);
Chris Dalton1a325d22017-07-14 15:17:41 -060045 }
46
47 void onEmitVertexShader(const GrCCPRCoverageProcessor&, GrGLSLVertexBuilder*,
48 const TexelBufferHandle& pointsBuffer, const char* atlasOffset,
49 const char* rtAdjust, GrGPArgs*) const override;
50 void emitWind(GrGLSLGeometryBuilder*, const char* rtAdjust, const char* outputWind) const final;
51 void onEmitGeometryShader(GrGLSLGeometryBuilder*, const char* emitVertexFn, const char* wind,
52 const char* rtAdjust) const final;
Chris Dalton7f578bf2017-09-05 16:46:48 -060053 void emitPerVertexGeometryCode(SkString* fnBody, const char* position, const char* coverage,
54 const char* wind) const final;
Chris Dalton1a325d22017-07-14 15:17:41 -060055
56protected:
57 virtual void emitCubicGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn,
58 const char* wind, const char* rtAdjust) const = 0;
Chris Dalton7f578bf2017-09-05 16:46:48 -060059 virtual void onEmitPerVertexGeometryCode(SkString* fnBody) const = 0;
Chris Dalton1a325d22017-07-14 15:17:41 -060060
Chris Dalton7f578bf2017-09-05 16:46:48 -060061 const CubicType fCubicType;
Chris Dalton1a325d22017-07-14 15:17:41 -060062 GrShaderVar fKLMMatrix;
63 GrShaderVar fKLMDerivatives;
Chris Dalton7f578bf2017-09-05 16:46:48 -060064 GrShaderVar fEdgeDistanceEquation;
65 GrGLSLGeoToFrag fKLMD;
Chris Dalton1a325d22017-07-14 15:17:41 -060066
67 typedef GrCCPRCoverageProcessor::PrimitiveProcessor INHERITED;
68};
69
Chris Dalton7f578bf2017-09-05 16:46:48 -060070class GrCCPRCubicHullProcessor : public GrCCPRCubicProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -060071public:
Chris Dalton7f578bf2017-09-05 16:46:48 -060072 GrCCPRCubicHullProcessor(CubicType cubicType)
73 : INHERITED(cubicType)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040074 , fGradMatrix(kHighFloat2x2_GrSLType) {}
Chris Dalton1a325d22017-07-14 15:17:41 -060075
76 void resetVaryings(GrGLSLVaryingHandler* varyingHandler) override {
77 this->INHERITED::resetVaryings(varyingHandler);
Chris Dalton1a325d22017-07-14 15:17:41 -060078 varyingHandler->addVarying("grad_matrix", &fGradMatrix, kHigh_GrSLPrecision);
79 }
80
81 void emitCubicGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn,
82 const char* wind, const char* rtAdjust) const override;
Chris Dalton7f578bf2017-09-05 16:46:48 -060083 void onEmitPerVertexGeometryCode(SkString* fnBody) const override;
Chris Dalton1a325d22017-07-14 15:17:41 -060084 void emitShaderCoverage(GrGLSLFragmentBuilder*, const char* outputCoverage) const override;
85
86protected:
Chris Dalton1a325d22017-07-14 15:17:41 -060087 GrGLSLGeoToFrag fGradMatrix;
88
89 typedef GrCCPRCubicProcessor INHERITED;
90};
91
Chris Dalton7f578bf2017-09-05 16:46:48 -060092class GrCCPRCubicCornerProcessor : public GrCCPRCubicProcessor {
Chris Dalton1a325d22017-07-14 15:17:41 -060093public:
Chris Dalton7f578bf2017-09-05 16:46:48 -060094 GrCCPRCubicCornerProcessor(CubicType cubicType)
95 : INHERITED(cubicType)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040096 , fEdgeDistanceDerivatives("edge_distance_derivatives", kHighFloat2_GrSLType,
Brian Salomon1d816b92017-08-17 11:07:59 -040097 GrShaderVar::kNonArray, kHigh_GrSLPrecision)
Ethan Nicholasf7b88202017-09-18 14:10:39 -040098 , fdKLMDdx(kHighFloat4_GrSLType)
99 , fdKLMDdy(kHighFloat4_GrSLType) {}
Chris Dalton1a325d22017-07-14 15:17:41 -0600100
101 void resetVaryings(GrGLSLVaryingHandler* varyingHandler) override {
102 this->INHERITED::resetVaryings(varyingHandler);
Chris Dalton1a325d22017-07-14 15:17:41 -0600103 varyingHandler->addFlatVarying("dklmddx", &fdKLMDdx, kHigh_GrSLPrecision);
104 varyingHandler->addFlatVarying("dklmddy", &fdKLMDdy, kHigh_GrSLPrecision);
Chris Dalton1a325d22017-07-14 15:17:41 -0600105 }
106
107 void emitCubicGeometry(GrGLSLGeometryBuilder*, const char* emitVertexFn,
108 const char* wind, const char* rtAdjust) const override;
Chris Dalton7f578bf2017-09-05 16:46:48 -0600109 void onEmitPerVertexGeometryCode(SkString* fnBody) const override;
Chris Dalton1a325d22017-07-14 15:17:41 -0600110 void emitShaderCoverage(GrGLSLFragmentBuilder*, const char* outputCoverage) const override;
111
112protected:
Chris Dalton1a325d22017-07-14 15:17:41 -0600113 GrShaderVar fEdgeDistanceDerivatives;
Chris Dalton1a325d22017-07-14 15:17:41 -0600114 GrGLSLGeoToFrag fdKLMDdx;
115 GrGLSLGeoToFrag fdKLMDdy;
Chris Dalton1a325d22017-07-14 15:17:41 -0600116
117 typedef GrCCPRCubicProcessor INHERITED;
118};
119
120#endif