GrCCCubicShader.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/GrCCCubicShader.h"
  8. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  9. #include "src/gpu/glsl/GrGLSLProgramBuilder.h"
  10. #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
  11. using Shader = GrCCCoverageProcessor::Shader;
  12. void GrCCCubicShader::emitSetupCode(
  13. GrGLSLVertexGeoBuilder* s, const char* pts, const char** /*outHull4*/) const {
  14. // Find the cubic's power basis coefficients.
  15. s->codeAppendf("float2x4 C = float4x4(-1, 3, -3, 1, "
  16. " 3, -6, 3, 0, "
  17. "-3, 3, 0, 0, "
  18. " 1, 0, 0, 0) * transpose(%s);", pts);
  19. // Find the cubic's inflection function.
  20. s->codeAppend ("float D3 = +determinant(float2x2(C[0].yz, C[1].yz));");
  21. s->codeAppend ("float D2 = -determinant(float2x2(C[0].xz, C[1].xz));");
  22. s->codeAppend ("float D1 = +determinant(float2x2(C));");
  23. // Shift the exponents in D so the largest magnitude falls somewhere in 1..2. This protects us
  24. // from overflow while solving for roots and KLM functionals.
  25. s->codeAppend ("float Dmax = max(max(abs(D1), abs(D2)), abs(D3));");
  26. s->codeAppend ("float norm;");
  27. if (s->getProgramBuilder()->shaderCaps()->fpManipulationSupport()) {
  28. s->codeAppend ("int exp;");
  29. s->codeAppend ("frexp(Dmax, exp);");
  30. s->codeAppend ("norm = ldexp(1, 1 - exp);");
  31. } else {
  32. s->codeAppend ("norm = 1/Dmax;"); // Dmax will not be 0 because we cull line cubics on CPU.
  33. }
  34. s->codeAppend ("D3 *= norm;");
  35. s->codeAppend ("D2 *= norm;");
  36. s->codeAppend ("D1 *= norm;");
  37. // Calculate the KLM matrix.
  38. s->declareGlobal(fKLMMatrix);
  39. s->codeAppend ("float discr = 3*D2*D2 - 4*D1*D3;");
  40. s->codeAppend ("float x = discr >= 0 ? 3 : 1;");
  41. s->codeAppend ("float q = sqrt(x * abs(discr));");
  42. s->codeAppend ("q = x*D2 + (D2 >= 0 ? q : -q);");
  43. s->codeAppend ("float2 l, m;");
  44. s->codeAppend ("l.ts = float2(q, 2*x * D1);");
  45. s->codeAppend ("m.ts = float2(2, q) * (discr >= 0 ? float2(D3, 1) "
  46. ": float2(D2*D2 - D3*D1, D1));");
  47. s->codeAppend ("float4 K;");
  48. s->codeAppend ("float4 lm = l.sstt * m.stst;");
  49. s->codeAppend ("K = float4(0, lm.x, -lm.y - lm.z, lm.w);");
  50. s->codeAppend ("float4 L, M;");
  51. s->codeAppend ("lm.yz += 2*lm.zy;");
  52. s->codeAppend ("L = float4(-1,x,-x,1) * l.sstt * (discr >= 0 ? l.ssst * l.sttt : lm);");
  53. s->codeAppend ("M = float4(-1,x,-x,1) * m.sstt * (discr >= 0 ? m.ssst * m.sttt : lm.xzyw);");
  54. s->codeAppend ("int middlerow = abs(D2) > abs(D1) ? 2 : 1;");
  55. s->codeAppend ("float3x3 CI = inverse(float3x3(C[0][0], C[0][middlerow], C[0][3], "
  56. "C[1][0], C[1][middlerow], C[1][3], "
  57. " 0, 0, 1));");
  58. s->codeAppendf("%s = CI * float3x3(K[0], K[middlerow], K[3], "
  59. "L[0], L[middlerow], L[3], "
  60. "M[0], M[middlerow], M[3]);", fKLMMatrix.c_str());
  61. // Evaluate the cubic at T=.5 for a mid-ish point.
  62. s->codeAppendf("float2 midpoint = %s * float4(.125, .375, .375, .125);", pts);
  63. // Orient the KLM matrix so L & M are both positive on the side of the curve we wish to fill.
  64. s->codeAppendf("float2 orientation = sign(float3(midpoint, 1) * float2x3(%s[1], %s[2]));",
  65. fKLMMatrix.c_str(), fKLMMatrix.c_str());
  66. s->codeAppendf("%s *= float3x3(orientation[0] * orientation[1], 0, 0, "
  67. "0, orientation[0], 0, "
  68. "0, 0, orientation[1]);", fKLMMatrix.c_str());
  69. }
  70. void GrCCCubicShader::onEmitVaryings(
  71. GrGLSLVaryingHandler* varyingHandler, GrGLSLVarying::Scope scope, SkString* code,
  72. const char* position, const char* coverage, const char* cornerCoverage, const char* wind) {
  73. code->appendf("float3 klm = float3(%s, 1) * %s;", position, fKLMMatrix.c_str());
  74. if (coverage) {
  75. fKLM_fEdge.reset(kFloat4_GrSLType, scope);
  76. varyingHandler->addVarying("klm_and_edge", &fKLM_fEdge);
  77. // Give L&M both the same sign as wind, in order to pass this value to the fragment shader.
  78. // (Cubics are pre-chopped such that L&M do not change sign within any individual segment.)
  79. code->appendf("%s.xyz = klm * float3(1, %s, %s);", OutName(fKLM_fEdge), wind, wind);
  80. // Flat edge opposite the curve.
  81. code->appendf("%s.w = %s;", OutName(fKLM_fEdge), coverage);
  82. } else {
  83. fKLM_fEdge.reset(kFloat3_GrSLType, scope);
  84. varyingHandler->addVarying("klm", &fKLM_fEdge);
  85. code->appendf("%s = klm;", OutName(fKLM_fEdge));
  86. }
  87. fGradMatrix.reset(kFloat4_GrSLType, scope);
  88. varyingHandler->addVarying("grad_matrix", &fGradMatrix);
  89. code->appendf("%s.xy = 2*bloat * 3 * klm[0] * %s[0].xy;",
  90. OutName(fGradMatrix), fKLMMatrix.c_str());
  91. code->appendf("%s.zw = -2*bloat * (klm[1] * %s[2].xy + klm[2] * %s[1].xy);",
  92. OutName(fGradMatrix), fKLMMatrix.c_str(), fKLMMatrix.c_str());
  93. if (cornerCoverage) {
  94. SkASSERT(coverage);
  95. code->appendf("half hull_coverage; {");
  96. this->calcHullCoverage(code, OutName(fKLM_fEdge), OutName(fGradMatrix), "hull_coverage");
  97. code->appendf("}");
  98. fCornerCoverage.reset(kHalf2_GrSLType, scope);
  99. varyingHandler->addVarying("corner_coverage", &fCornerCoverage);
  100. code->appendf("%s = half2(hull_coverage, 1) * %s;",
  101. OutName(fCornerCoverage), cornerCoverage);
  102. }
  103. }
  104. void GrCCCubicShader::emitFragmentCoverageCode(
  105. GrGLSLFPFragmentBuilder* f, const char* outputCoverage) const {
  106. this->calcHullCoverage(
  107. &AccessCodeString(f), fKLM_fEdge.fsIn(), fGradMatrix.fsIn(), outputCoverage);
  108. // Wind is the sign of both L and/or M. Take the sign of whichever has the larger magnitude.
  109. // (In reality, either would be fine because we chop cubics with more than a half pixel of
  110. // padding around the L & M lines, so neither should approach zero.)
  111. f->codeAppend ("half wind = sign(half(l + m));");
  112. f->codeAppendf("%s *= wind;", outputCoverage);
  113. if (fCornerCoverage.fsIn()) {
  114. f->codeAppendf("%s = %s.x * %s.y + %s;", // Attenuated corner coverage.
  115. outputCoverage, fCornerCoverage.fsIn(), fCornerCoverage.fsIn(),
  116. outputCoverage);
  117. }
  118. }
  119. void GrCCCubicShader::calcHullCoverage(SkString* code, const char* klmAndEdge,
  120. const char* gradMatrix, const char* outputCoverage) const {
  121. code->appendf("float k = %s.x, l = %s.y, m = %s.z;", klmAndEdge, klmAndEdge, klmAndEdge);
  122. code->append ("float f = k*k*k - l*m;");
  123. code->appendf("float2 grad = %s.xy * k + %s.zw;", gradMatrix, gradMatrix);
  124. code->append ("float fwidth = abs(grad.x) + abs(grad.y);");
  125. code->appendf("float curve_coverage = min(0.5 - f/fwidth, 1);");
  126. // Flat edge opposite the curve.
  127. code->appendf("float edge_coverage = min(%s.w, 0);", klmAndEdge);
  128. // Total hull coverage.
  129. code->appendf("%s = max(half(curve_coverage + edge_coverage), 0);", outputCoverage);
  130. }
  131. void GrCCCubicShader::emitSampleMaskCode(GrGLSLFPFragmentBuilder* f) const {
  132. f->codeAppendf("float k = %s.x, l = %s.y, m = %s.z;",
  133. fKLM_fEdge.fsIn(), fKLM_fEdge.fsIn(), fKLM_fEdge.fsIn());
  134. f->codeAppendf("float f = k*k*k - l*m;");
  135. f->codeAppendf("float2x2 grad_matrix = float2x2(%s);", fGradMatrix.fsIn());
  136. f->codeAppendf("float2 grad = grad_matrix * float2(k, 1);");
  137. f->applyFnToMultisampleMask("f", "grad", GrGLSLFPFragmentBuilder::ScopeFlags::kTopLevel);
  138. }