GrCCConicShader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2018 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/GrCCConicShader.h"
  8. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  9. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  10. void GrCCConicShader::emitSetupCode(
  11. GrGLSLVertexGeoBuilder* s, const char* pts, const char** outHull4) const {
  12. // K is distance from the line P2 -> P0. L is distance from the line P0 -> P1, scaled by 2w.
  13. // M is distance from the line P1 -> P2, scaled by 2w. We do this in a space where P1=0.
  14. s->declareGlobal(fKLMMatrix);
  15. s->codeAppendf("float x0 = %s[0].x - %s[1].x, x2 = %s[2].x - %s[1].x;", pts, pts, pts, pts);
  16. s->codeAppendf("float y0 = %s[0].y - %s[1].y, y2 = %s[2].y - %s[1].y;", pts, pts, pts, pts);
  17. s->codeAppendf("float w = %s[3].x;", pts);
  18. s->codeAppendf("%s = float3x3(y2 - y0, x0 - x2, x2*y0 - x0*y2, "
  19. "2*w * float2(+y0, -x0), 0, "
  20. "2*w * float2(-y2, +x2), 0);", fKLMMatrix.c_str());
  21. s->declareGlobal(fControlPoint);
  22. s->codeAppendf("%s = %s[1];", fControlPoint.c_str(), pts);
  23. // Scale KLM by the inverse Manhattan width of K, and make sure K is positive. This allows K to
  24. // double as the flat opposite edge AA. kwidth will not be 0 because we cull degenerate conics
  25. // on the CPU.
  26. s->codeAppendf("float kwidth = 2*bloat * (abs(%s[0].x) + abs(%s[0].y)) * sign(%s[0].z);",
  27. fKLMMatrix.c_str(), fKLMMatrix.c_str(), fKLMMatrix.c_str());
  28. s->codeAppendf("%s *= 1/kwidth;", fKLMMatrix.c_str());
  29. if (outHull4) {
  30. // Clip the conic triangle by the tangent line at maximum height. Conics have the nice
  31. // property that maximum height always occurs at T=.5. This is a simple application for
  32. // De Casteljau's algorithm.
  33. s->codeAppendf("float2 p1w = %s[1]*w;", pts);
  34. s->codeAppend ("float r = 1 / (1 + w);");
  35. s->codeAppend ("float2 conic_hull[4];");
  36. s->codeAppendf("conic_hull[0] = %s[0];", pts);
  37. s->codeAppendf("conic_hull[1] = (%s[0] + p1w) * r;", pts);
  38. s->codeAppendf("conic_hull[2] = (p1w + %s[2]) * r;", pts);
  39. s->codeAppendf("conic_hull[3] = %s[2];", pts);
  40. *outHull4 = "conic_hull";
  41. }
  42. }
  43. void GrCCConicShader::onEmitVaryings(
  44. GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
  45. const char* position, const char* coverage, const char* cornerCoverage, const char* wind) {
  46. code->appendf("float3 klm = float3(%s - %s, 1) * %s;",
  47. position, fControlPoint.c_str(), fKLMMatrix.c_str());
  48. if (coverage) {
  49. fKLM_fWind.reset(kFloat4_GrSLType, scope);
  50. varyingHandler->addVarying("klm_and_wind", &fKLM_fWind);
  51. code->appendf("%s.w = %s;", OutName(fKLM_fWind), wind);
  52. } else {
  53. fKLM_fWind.reset(kFloat3_GrSLType, scope);
  54. varyingHandler->addVarying("klm", &fKLM_fWind);
  55. }
  56. code->appendf("%s.xyz = klm;", OutName(fKLM_fWind));
  57. fGrad_fCorner.reset(cornerCoverage ? kFloat4_GrSLType : kFloat2_GrSLType, scope);
  58. varyingHandler->addVarying((cornerCoverage) ? "grad_and_corner" : "grad", &fGrad_fCorner);
  59. code->appendf("%s.xy = 2*bloat * (float3x2(%s) * float3(2*klm[0], -klm[2], -klm[1]));",
  60. OutName(fGrad_fCorner), fKLMMatrix.c_str());
  61. if (cornerCoverage) {
  62. SkASSERT(coverage);
  63. code->appendf("half hull_coverage;");
  64. this->calcHullCoverage(code, "klm", OutName(fGrad_fCorner), "hull_coverage");
  65. code->appendf("%s.zw = half2(hull_coverage, 1) * %s;",
  66. OutName(fGrad_fCorner), cornerCoverage);
  67. }
  68. }
  69. void GrCCConicShader::emitFragmentCoverageCode(
  70. GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const {
  71. this->calcHullCoverage(&AccessCodeString(f), fKLM_fWind.fsIn(), fGrad_fCorner.fsIn(),
  72. outputCoverage);
  73. f->codeAppendf("%s *= half(%s.w);", outputCoverage, fKLM_fWind.fsIn()); // Wind.
  74. if (kFloat4_GrSLType == fGrad_fCorner.type()) {
  75. f->codeAppendf("%s = fma(half(%s.z), half(%s.w), %s);", // Attenuated corner coverage.
  76. outputCoverage, fGrad_fCorner.fsIn(), fGrad_fCorner.fsIn(),
  77. outputCoverage);
  78. }
  79. }
  80. void GrCCConicShader::calcHullCoverage(SkString* code, const char* klm, const char* grad,
  81. const char* outputCoverage) const {
  82. code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klm, klm, klm);
  83. code->append ("float f = k*k - l*m;");
  84. code->appendf("float fwidth = abs(%s.x) + abs(%s.y);", grad, grad);
  85. code->appendf("float curve_coverage = min(0.5 - f/fwidth, 1);");
  86. // K doubles as the flat opposite edge's AA.
  87. code->append ("float edge_coverage = min(k - 0.5, 0);");
  88. // Total hull coverage.
  89. code->appendf("%s = max(half(curve_coverage + edge_coverage), 0);", outputCoverage);
  90. }
  91. void GrCCConicShader::emitSampleMaskCode(GrGLSLFPFragmentBuilder* f) const {
  92. f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;",
  93. fKLM_fWind.fsIn(), fKLM_fWind.fsIn(), fKLM_fWind.fsIn());
  94. f->codeAppendf("float f = k*k - l*m;");
  95. f->codeAppendf("float2 grad = %s;", fGrad_fCorner.fsIn());
  96. f->applyFnToMultisampleMask("f", "grad", GrGLSLFPFragmentBuilder::ScopeFlags::kTopLevel);
  97. }