GrCCCoverageProcessor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. #ifndef GrCCCoverageProcessor_DEFINED
  8. #define GrCCCoverageProcessor_DEFINED
  9. #include "include/private/SkNx.h"
  10. #include "src/gpu/GrCaps.h"
  11. #include "src/gpu/GrGeometryProcessor.h"
  12. #include "src/gpu/GrPipeline.h"
  13. #include "src/gpu/GrShaderCaps.h"
  14. #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
  15. #include "src/gpu/glsl/GrGLSLVarying.h"
  16. class GrGLSLFPFragmentBuilder;
  17. class GrGLSLVertexGeoBuilder;
  18. class GrMesh;
  19. class GrOpFlushState;
  20. /**
  21. * This is the geometry processor for the simple convex primitive shapes (triangles and closed,
  22. * convex bezier curves) from which ccpr paths are composed. The output is a single-channel alpha
  23. * value, positive for clockwise shapes and negative for counter-clockwise, that indicates coverage.
  24. *
  25. * The caller is responsible to draw all primitives as produced by GrCCGeometry into a cleared,
  26. * floating point, alpha-only render target using SkBlendMode::kPlus. Once all of a path's
  27. * primitives have been drawn, the render target contains a composite coverage count that can then
  28. * be used to draw the path (see GrCCPathProcessor).
  29. *
  30. * To draw primitives, use appendMesh() and draw() (defined below).
  31. */
  32. class GrCCCoverageProcessor : public GrGeometryProcessor {
  33. public:
  34. enum class PrimitiveType {
  35. kTriangles,
  36. kWeightedTriangles, // Triangles (from the tessellator) whose winding magnitude > 1.
  37. kQuadratics,
  38. kCubics,
  39. kConics
  40. };
  41. static const char* PrimitiveTypeName(PrimitiveType);
  42. // Defines a single primitive shape with 3 input points (i.e. Triangles and Quadratics).
  43. // X,Y point values are transposed.
  44. struct TriPointInstance {
  45. float fValues[6];
  46. enum class Ordering : bool {
  47. kXYTransposed,
  48. kXYInterleaved,
  49. };
  50. void set(const SkPoint[3], const Sk2f& translate, Ordering);
  51. void set(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& translate, Ordering);
  52. void set(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering);
  53. };
  54. // Defines a single primitive shape with 4 input points, or 3 input points plus a "weight"
  55. // parameter duplicated in both lanes of the 4th input (i.e. Cubics, Conics, and Triangles with
  56. // a weighted winding number). X,Y point values are transposed.
  57. struct QuadPointInstance {
  58. float fX[4];
  59. float fY[4];
  60. void set(const SkPoint[4], float dx, float dy);
  61. void setW(const SkPoint[3], const Sk2f& trans, float w);
  62. void setW(const SkPoint&, const SkPoint&, const SkPoint&, const Sk2f& trans, float w);
  63. void setW(const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& trans, float w);
  64. };
  65. virtual void reset(PrimitiveType, GrResourceProvider*) = 0;
  66. PrimitiveType primitiveType() const { return fPrimitiveType; }
  67. // Number of bezier points for curves, or 3 for triangles.
  68. int numInputPoints() const { return PrimitiveType::kCubics == fPrimitiveType ? 4 : 3; }
  69. bool isTriangles() const {
  70. return PrimitiveType::kTriangles == fPrimitiveType ||
  71. PrimitiveType::kWeightedTriangles == fPrimitiveType;
  72. }
  73. int hasInputWeight() const {
  74. return PrimitiveType::kWeightedTriangles == fPrimitiveType ||
  75. PrimitiveType::kConics == fPrimitiveType;
  76. }
  77. // GrPrimitiveProcessor overrides.
  78. const char* name() const override { return PrimitiveTypeName(fPrimitiveType); }
  79. #ifdef SK_DEBUG
  80. SkString dumpInfo() const override {
  81. return SkStringPrintf("%s\n%s", this->name(), this->INHERITED::dumpInfo().c_str());
  82. }
  83. #endif
  84. void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override {
  85. SkDEBUGCODE(this->getDebugBloatKey(b));
  86. b->add32((int)fPrimitiveType);
  87. }
  88. GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
  89. #ifdef SK_DEBUG
  90. // Increases the 1/2 pixel AA bloat by a factor of debugBloat.
  91. void enableDebugBloat(float debugBloat) { fDebugBloat = debugBloat; }
  92. bool debugBloatEnabled() const { return fDebugBloat > 0; }
  93. float debugBloat() const { SkASSERT(this->debugBloatEnabled()); return fDebugBloat; }
  94. void getDebugBloatKey(GrProcessorKeyBuilder* b) const {
  95. uint32_t bloatBits;
  96. memcpy(&bloatBits, &fDebugBloat, 4);
  97. b->add32(bloatBits);
  98. }
  99. #endif
  100. // Appends a GrMesh that will draw the provided instances. The instanceBuffer must be an array
  101. // of either TriPointInstance or QuadPointInstance, depending on this processor's RendererPass,
  102. // with coordinates in the desired shape's final atlas-space position.
  103. virtual void appendMesh(sk_sp<const GrGpuBuffer> instanceBuffer, int instanceCount,
  104. int baseInstance, SkTArray<GrMesh>* out) const = 0;
  105. virtual void draw(GrOpFlushState*, const GrPipeline&, const SkIRect scissorRects[],
  106. const GrMesh[], int meshCount, const SkRect& drawBounds) const;
  107. // The Shader provides code to calculate each pixel's coverage in a RenderPass. It also
  108. // provides details about shape-specific geometry.
  109. class Shader {
  110. public:
  111. // Returns true if the Impl should not calculate the coverage argument for emitVaryings().
  112. // If true, then "coverage" will have a signed magnitude of 1.
  113. virtual bool calculatesOwnEdgeCoverage() const { return false; }
  114. // Called before generating geometry. Subclasses may set up internal member variables during
  115. // this time that will be needed during onEmitVaryings (e.g. transformation matrices).
  116. //
  117. // If the 'outHull4' parameter is provided, and there are not 4 input points, the subclass
  118. // is required to fill it with the name of a 4-point hull around which the Impl can generate
  119. // its geometry. If it is left unchanged, the Impl will use the regular input points.
  120. virtual void emitSetupCode(
  121. GrGLSLVertexGeoBuilder*, const char* pts, const char** outHull4 = nullptr) const {
  122. SkASSERT(!outHull4);
  123. }
  124. void emitVaryings(
  125. GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
  126. const char* position, const char* coverage, const char* cornerCoverage,
  127. const char* wind) {
  128. SkASSERT(GrGLSLVarying::Scope::kVertToGeo != scope);
  129. this->onEmitVaryings(
  130. varyingHandler, scope, code, position, coverage, cornerCoverage, wind);
  131. }
  132. // Writes the signed coverage value at the current pixel to "outputCoverage".
  133. virtual void emitFragmentCoverageCode(
  134. GrGLSLFPFragmentBuilder*, const char* outputCoverage) const = 0;
  135. // Assigns the built-in sample mask at the current pixel.
  136. virtual void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const = 0;
  137. // Calculates the winding direction of the input points (+1, -1, or 0). Wind for extremely
  138. // thin triangles gets rounded to zero.
  139. static void CalcWind(const GrCCCoverageProcessor&, GrGLSLVertexGeoBuilder*, const char* pts,
  140. const char* outputWind);
  141. // Calculates an edge's coverage at a conservative raster vertex. The edge is defined by two
  142. // clockwise-ordered points, 'leftPt' and 'rightPt'. 'rasterVertexDir' is a pair of +/-1
  143. // values that point in the direction of conservative raster bloat, starting from an
  144. // endpoint.
  145. //
  146. // Coverage values ramp from -1 (completely outside the edge) to 0 (completely inside).
  147. static void CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder*, const char* leftPt,
  148. const char* rightPt, const char* rasterVertexDir,
  149. const char* outputCoverage);
  150. // Calculates an edge's coverage at two conservative raster vertices.
  151. // (See CalcEdgeCoverageAtBloatVertex).
  152. static void CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder*, const char* leftPt,
  153. const char* rightPt, const char* bloatDir1,
  154. const char* bloatDir2,
  155. const char* outputCoverages);
  156. // Corner boxes require an additional "attenuation" varying that is multiplied by the
  157. // regular (linearly-interpolated) coverage. This function calculates the attenuation value
  158. // to use in the single, outermost vertex. The remaining three vertices of the corner box
  159. // all use an attenuation value of 1.
  160. static void CalcCornerAttenuation(GrGLSLVertexGeoBuilder*, const char* leftDir,
  161. const char* rightDir, const char* outputAttenuation);
  162. virtual ~Shader() {}
  163. protected:
  164. // Here the subclass adds its internal varyings to the handler and produces code to
  165. // initialize those varyings from a given position and coverage values.
  166. //
  167. // NOTE: the coverage values are signed appropriately for wind.
  168. // 'coverage' will only be +1 or -1 on curves.
  169. virtual void onEmitVaryings(
  170. GrGLSLVaryingHandler*, GrGLSLVarying::Scope, SkString* code, const char* position,
  171. const char* coverage, const char* cornerCoverage, const char* wind) = 0;
  172. // Returns the name of a Shader's internal varying at the point where where its value is
  173. // assigned. This is intended to work whether called for a vertex or a geometry shader.
  174. const char* OutName(const GrGLSLVarying& varying) const {
  175. using Scope = GrGLSLVarying::Scope;
  176. SkASSERT(Scope::kVertToGeo != varying.scope());
  177. return Scope::kGeoToFrag == varying.scope() ? varying.gsOut() : varying.vsOut();
  178. }
  179. // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
  180. inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
  181. };
  182. protected:
  183. // Slightly undershoot a bloat radius of 0.5 so vertices that fall on integer boundaries don't
  184. // accidentally bleed into neighbor pixels.
  185. static constexpr float kAABloatRadius = 0.491111f;
  186. GrCCCoverageProcessor(ClassID classID) : INHERITED(classID) {}
  187. virtual GrGLSLPrimitiveProcessor* onCreateGLSLInstance(std::unique_ptr<Shader>) const = 0;
  188. // Our friendship with GrGLSLShaderBuilder does not propagate to subclasses.
  189. inline static SkString& AccessCodeString(GrGLSLShaderBuilder* s) { return s->code(); }
  190. PrimitiveType fPrimitiveType;
  191. SkDEBUGCODE(float fDebugBloat = 0);
  192. class TriangleShader;
  193. typedef GrGeometryProcessor INHERITED;
  194. };
  195. inline const char* GrCCCoverageProcessor::PrimitiveTypeName(PrimitiveType type) {
  196. switch (type) {
  197. case PrimitiveType::kTriangles: return "kTriangles";
  198. case PrimitiveType::kWeightedTriangles: return "kWeightedTriangles";
  199. case PrimitiveType::kQuadratics: return "kQuadratics";
  200. case PrimitiveType::kCubics: return "kCubics";
  201. case PrimitiveType::kConics: return "kConics";
  202. }
  203. SK_ABORT("Invalid PrimitiveType");
  204. return "";
  205. }
  206. inline void GrCCCoverageProcessor::TriPointInstance::set(
  207. const SkPoint p[3], const Sk2f& translate, Ordering ordering) {
  208. this->set(p[0], p[1], p[2], translate, ordering);
  209. }
  210. inline void GrCCCoverageProcessor::TriPointInstance::set(
  211. const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, const Sk2f& translate,
  212. Ordering ordering) {
  213. Sk2f P0 = Sk2f::Load(&p0);
  214. Sk2f P1 = Sk2f::Load(&p1);
  215. Sk2f P2 = Sk2f::Load(&p2);
  216. this->set(P0, P1, P2, translate, ordering);
  217. }
  218. inline void GrCCCoverageProcessor::TriPointInstance::set(
  219. const Sk2f& P0, const Sk2f& P1, const Sk2f& P2, const Sk2f& translate, Ordering ordering) {
  220. if (Ordering::kXYTransposed == ordering) {
  221. Sk2f::Store3(fValues, P0 + translate, P1 + translate, P2 + translate);
  222. } else {
  223. (P0 + translate).store(fValues);
  224. (P1 + translate).store(fValues + 2);
  225. (P2 + translate).store(fValues + 4);
  226. }
  227. }
  228. inline void GrCCCoverageProcessor::QuadPointInstance::set(const SkPoint p[4], float dx, float dy) {
  229. Sk4f X,Y;
  230. Sk4f::Load2(p, &X, &Y);
  231. (X + dx).store(&fX);
  232. (Y + dy).store(&fY);
  233. }
  234. inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint p[3], const Sk2f& trans,
  235. float w) {
  236. this->setW(p[0], p[1], p[2], trans, w);
  237. }
  238. inline void GrCCCoverageProcessor::QuadPointInstance::setW(const SkPoint& p0, const SkPoint& p1,
  239. const SkPoint& p2, const Sk2f& trans,
  240. float w) {
  241. Sk2f P0 = Sk2f::Load(&p0);
  242. Sk2f P1 = Sk2f::Load(&p1);
  243. Sk2f P2 = Sk2f::Load(&p2);
  244. this->setW(P0, P1, P2, trans, w);
  245. }
  246. inline void GrCCCoverageProcessor::QuadPointInstance::setW(const Sk2f& P0, const Sk2f& P1,
  247. const Sk2f& P2, const Sk2f& trans,
  248. float w) {
  249. Sk2f W = Sk2f(w);
  250. Sk2f::Store4(this, P0 + trans, P1 + trans, P2 + trans, W);
  251. }
  252. #endif