GrCCCoverageProcessor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "src/gpu/ccpr/GrCCCoverageProcessor.h"
  8. #include "src/core/SkMakeUnique.h"
  9. #include "src/gpu/GrGpuCommandBuffer.h"
  10. #include "src/gpu/GrOpFlushState.h"
  11. #include "src/gpu/ccpr/GrCCConicShader.h"
  12. #include "src/gpu/ccpr/GrCCCubicShader.h"
  13. #include "src/gpu/ccpr/GrCCQuadraticShader.h"
  14. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  15. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  16. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  17. class GrCCCoverageProcessor::TriangleShader : public GrCCCoverageProcessor::Shader {
  18. void onEmitVaryings(
  19. GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
  20. const char* position, const char* coverage, const char* cornerCoverage,
  21. const char* /*wind*/) override {
  22. if (!cornerCoverage) {
  23. fCoverages.reset(kHalf_GrSLType, scope);
  24. varyingHandler->addVarying("coverage", &fCoverages);
  25. code->appendf("%s = %s;", OutName(fCoverages), coverage);
  26. } else {
  27. fCoverages.reset(kHalf3_GrSLType, scope);
  28. varyingHandler->addVarying("coverages", &fCoverages);
  29. code->appendf("%s = half3(%s, %s);", OutName(fCoverages), coverage, cornerCoverage);
  30. }
  31. }
  32. void emitFragmentCoverageCode(
  33. GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const override {
  34. if (kHalf_GrSLType == fCoverages.type()) {
  35. f->codeAppendf("%s = %s;", outputCoverage, fCoverages.fsIn());
  36. } else {
  37. f->codeAppendf("%s = %s.z * %s.y + %s.x;",
  38. outputCoverage, fCoverages.fsIn(), fCoverages.fsIn(), fCoverages.fsIn());
  39. }
  40. }
  41. void emitSampleMaskCode(GrGLSLFPFragmentBuilder*) const override { return; }
  42. GrGLSLVarying fCoverages;
  43. };
  44. void GrCCCoverageProcessor::Shader::CalcWind(const GrCCCoverageProcessor& proc,
  45. GrGLSLVertexGeoBuilder* s, const char* pts,
  46. const char* outputWind) {
  47. if (3 == proc.numInputPoints()) {
  48. s->codeAppendf("float2 a = %s[0] - %s[1], "
  49. "b = %s[0] - %s[2];", pts, pts, pts, pts);
  50. } else {
  51. // All inputs are convex, so it's sufficient to just average the middle two input points.
  52. SkASSERT(4 == proc.numInputPoints());
  53. s->codeAppendf("float2 p12 = (%s[1] + %s[2]) * .5;", pts, pts);
  54. s->codeAppendf("float2 a = %s[0] - p12, "
  55. "b = %s[0] - %s[3];", pts, pts, pts);
  56. }
  57. s->codeAppend ("float area_x2 = determinant(float2x2(a, b));");
  58. if (proc.isTriangles()) {
  59. // We cull extremely thin triangles by zeroing wind. When a triangle gets too thin it's
  60. // possible for FP round-off error to actually give us the wrong winding direction, causing
  61. // rendering artifacts. The criteria we choose is "height <~ 1/1024". So we drop a triangle
  62. // if the max effect it can have on any single pixel is <~ 1/1024, or 1/4 of a bit in 8888.
  63. s->codeAppend ("float2 bbox_size = max(abs(a), abs(b));");
  64. s->codeAppend ("float basewidth = max(bbox_size.x + bbox_size.y, 1);");
  65. s->codeAppendf("%s = (abs(area_x2 * 1024) > basewidth) ? sign(half(area_x2)) : 0;",
  66. outputWind);
  67. } else {
  68. // We already converted nearly-flat curves to lines on the CPU, so no need to worry about
  69. // thin curve hulls at this point.
  70. s->codeAppendf("%s = sign(half(area_x2));", outputWind);
  71. }
  72. }
  73. void GrCCCoverageProcessor::Shader::CalcEdgeCoverageAtBloatVertex(GrGLSLVertexGeoBuilder* s,
  74. const char* leftPt,
  75. const char* rightPt,
  76. const char* rasterVertexDir,
  77. const char* outputCoverage) {
  78. // Here we find an edge's coverage at one corner of a conservative raster bloat box whose center
  79. // falls on the edge in question. (A bloat box is axis-aligned and the size of one pixel.) We
  80. // always set up coverage so it is -1 at the outermost corner, 0 at the innermost, and -.5 at
  81. // the center. Interpolated, these coverage values convert jagged conservative raster edges into
  82. // smooth antialiased edges.
  83. //
  84. // d1 == (P + sign(n) * bloat) dot n (Distance at the bloat box vertex whose
  85. // == P dot n + (abs(n.x) + abs(n.y)) * bloatSize coverage=-1, where the bloat box is
  86. // centered on P.)
  87. //
  88. // d0 == (P - sign(n) * bloat) dot n (Distance at the bloat box vertex whose
  89. // == P dot n - (abs(n.x) + abs(n.y)) * bloatSize coverage=0, where the bloat box is
  90. // centered on P.)
  91. //
  92. // d == (P + rasterVertexDir * bloatSize) dot n (Distance at the bloat box vertex whose
  93. // == P dot n + (rasterVertexDir dot n) * bloatSize coverage we wish to calculate.)
  94. //
  95. // coverage == -(d - d0) / (d1 - d0) (coverage=-1 at d=d1; coverage=0 at d=d0)
  96. //
  97. // == (rasterVertexDir dot n) / (abs(n.x) + abs(n.y)) * -.5 - .5
  98. //
  99. s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
  100. rightPt, leftPt, leftPt, rightPt);
  101. s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
  102. s->codeAppendf("float t = dot(%s, n);", rasterVertexDir);
  103. // The below conditional guarantees we get exactly 1 on the divide when nwidth=t (in case the
  104. // GPU divides by multiplying by the reciprocal?) It also guards against NaN when nwidth=0.
  105. s->codeAppendf("%s = half(abs(t) != nwidth ? t / nwidth : sign(t)) * -.5 - .5;",
  106. outputCoverage);
  107. }
  108. void GrCCCoverageProcessor::Shader::CalcEdgeCoveragesAtBloatVertices(GrGLSLVertexGeoBuilder* s,
  109. const char* leftPt,
  110. const char* rightPt,
  111. const char* bloatDir1,
  112. const char* bloatDir2,
  113. const char* outputCoverages) {
  114. // See comments in CalcEdgeCoverageAtBloatVertex.
  115. s->codeAppendf("float2 n = float2(%s.y - %s.y, %s.x - %s.x);",
  116. rightPt, leftPt, leftPt, rightPt);
  117. s->codeAppend ("float nwidth = abs(n.x) + abs(n.y);");
  118. s->codeAppendf("float2 t = n * float2x2(%s, %s);", bloatDir1, bloatDir2);
  119. s->codeAppendf("for (int i = 0; i < 2; ++i) {");
  120. s->codeAppendf( "%s[i] = half(abs(t[i]) != nwidth ? t[i] / nwidth : sign(t[i])) * -.5 - .5;",
  121. outputCoverages);
  122. s->codeAppendf("}");
  123. }
  124. void GrCCCoverageProcessor::Shader::CalcCornerAttenuation(GrGLSLVertexGeoBuilder* s,
  125. const char* leftDir, const char* rightDir,
  126. const char* outputAttenuation) {
  127. // obtuseness = cos(corner_angle) if corner_angle > 90 degrees
  128. // 0 if corner_angle <= 90 degrees
  129. //
  130. // NOTE: leftDir and rightDir are normalized and point in the same direction the path was
  131. // defined with, i.e., leftDir points into the corner and rightDir points away from the corner.
  132. s->codeAppendf("half obtuseness = max(half(dot(%s, %s)), 0);", leftDir, rightDir);
  133. // axis_alignedness = 1 - tan(angle_to_nearest_axis_from_corner_bisector)
  134. // (i.e., 1 when the corner bisector is aligned with the x- or y-axis
  135. // 0 when the corner bisector falls on a 45 degree angle
  136. // 0..1 when the corner bisector falls somewhere in between
  137. s->codeAppendf("half2 abs_bisect_maybe_transpose = abs((0 == obtuseness) ? half2(%s - %s) : "
  138. "half2(%s + %s));",
  139. leftDir, rightDir, leftDir, rightDir);
  140. s->codeAppend ("half axis_alignedness = "
  141. "1 - min(abs_bisect_maybe_transpose.y, abs_bisect_maybe_transpose.x) / "
  142. "max(abs_bisect_maybe_transpose.x, abs_bisect_maybe_transpose.y);");
  143. // ninety_degreesness = sin^2(corner_angle)
  144. // sin^2 just because... it's always positive and the results looked better than plain sine... ?
  145. s->codeAppendf("half ninety_degreesness = determinant(half2x2(%s, %s));", leftDir, rightDir);
  146. s->codeAppend ("ninety_degreesness = ninety_degreesness * ninety_degreesness;");
  147. // The below formula is not smart. It was just arrived at by considering the following
  148. // observations:
  149. //
  150. // 1. 90-degree, axis-aligned corners have full attenuation along the bisector.
  151. // (i.e. coverage = 1 - distance_to_corner^2)
  152. // (i.e. outputAttenuation = 0)
  153. //
  154. // 2. 180-degree corners always have zero attenuation.
  155. // (i.e. coverage = 1 - distance_to_corner)
  156. // (i.e. outputAttenuation = 1)
  157. //
  158. // 3. 90-degree corners whose bisector falls on a 45 degree angle also do not attenuate.
  159. // (i.e. outputAttenuation = 1)
  160. s->codeAppendf("%s = max(obtuseness, axis_alignedness * ninety_degreesness);",
  161. outputAttenuation);
  162. }
  163. GrGLSLPrimitiveProcessor* GrCCCoverageProcessor::createGLSLInstance(const GrShaderCaps&) const {
  164. std::unique_ptr<Shader> shader;
  165. switch (fPrimitiveType) {
  166. case PrimitiveType::kTriangles:
  167. case PrimitiveType::kWeightedTriangles:
  168. shader = skstd::make_unique<TriangleShader>();
  169. break;
  170. case PrimitiveType::kQuadratics:
  171. shader = skstd::make_unique<GrCCQuadraticShader>();
  172. break;
  173. case PrimitiveType::kCubics:
  174. shader = skstd::make_unique<GrCCCubicShader>();
  175. break;
  176. case PrimitiveType::kConics:
  177. shader = skstd::make_unique<GrCCConicShader>();
  178. break;
  179. }
  180. return this->onCreateGLSLInstance(std::move(shader));
  181. }
  182. void GrCCCoverageProcessor::draw(
  183. GrOpFlushState* flushState, const GrPipeline& pipeline, const SkIRect scissorRects[],
  184. const GrMesh meshes[], int meshCount, const SkRect& drawBounds) const {
  185. GrPipeline::DynamicStateArrays dynamicStateArrays;
  186. dynamicStateArrays.fScissorRects = scissorRects;
  187. GrGpuRTCommandBuffer* cmdBuff = flushState->rtCommandBuffer();
  188. cmdBuff->draw(*this, pipeline, nullptr, &dynamicStateArrays, meshes, meshCount, drawBounds);
  189. }