GrTwoPointConicalGradientLayout.fp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. // Equivalent to SkTwoPointConicalGradient::Type
  8. enum class Type {
  9. kRadial, kStrip, kFocal
  10. };
  11. in half4x4 gradientMatrix;
  12. layout(key) in Type type;
  13. layout(key) in bool isRadiusIncreasing;
  14. // Focal-specific optimizations
  15. layout(key) in bool isFocalOnCircle;
  16. layout(key) in bool isWellBehaved;
  17. layout(key) in bool isSwapped;
  18. layout(key) in bool isNativelyFocal;
  19. // focalParams is interpreted differently depending on if type is focal or degenerate when
  20. // degenerate, focalParams = (r0, r0^2), so strips will use .y and kRadial will use .x when focal,
  21. // focalParams = (1/r1, focalX = r0/(r0-r1)) The correct parameters are calculated once in Make for
  22. // each FP
  23. layout(tracked) in uniform half2 focalParams;
  24. @coordTransform {
  25. gradientMatrix
  26. }
  27. void main() {
  28. // p typed as a float2 is intentional; while a half2 is adequate for most normal cases in the
  29. // two point conic gradient's coordinate system, when the gradient is composed with a local
  30. // perspective matrix, certain out-of-bounds regions become ill behaved on mobile devices.
  31. // On desktops, they are properly clamped after the fact, but on many Adreno GPUs the
  32. // calculations of t and x_t below overflow and produce an incorrect interpolant (which then
  33. // renders the wrong border color sporadically). Increasing precition alleviates that issue.
  34. float2 p = sk_TransformedCoords2D[0];
  35. float t = -1;
  36. half v = 1; // validation flag, set to negative to discard fragment later
  37. @switch(type) {
  38. case Type::kStrip: {
  39. half r0_2 = focalParams.y;
  40. t = r0_2 - p.y * p.y;
  41. if (t >= 0) {
  42. t = p.x + sqrt(t);
  43. } else {
  44. v = -1;
  45. }
  46. }
  47. break;
  48. case Type::kRadial: {
  49. half r0 = focalParams.x;
  50. @if(isRadiusIncreasing) {
  51. t = length(p) - r0;
  52. } else {
  53. t = -length(p) - r0;
  54. }
  55. }
  56. break;
  57. case Type::kFocal: {
  58. half invR1 = focalParams.x;
  59. half fx = focalParams.y;
  60. float x_t = -1;
  61. @if (isFocalOnCircle) {
  62. x_t = dot(p, p) / p.x;
  63. } else if (isWellBehaved) {
  64. x_t = length(p) - p.x * invR1;
  65. } else {
  66. float temp = p.x * p.x - p.y * p.y;
  67. // Only do sqrt if temp >= 0; this is significantly slower than checking temp >= 0
  68. // in the if statement that checks r(t) >= 0. But GPU may break if we sqrt a
  69. // negative float. (Although I havevn't observed that on any devices so far, and the
  70. // old approach also does sqrt negative value without a check.) If the performance
  71. // is really critical, maybe we should just compute the area where temp and x_t are
  72. // always valid and drop all these ifs.
  73. if (temp >= 0) {
  74. @if(isSwapped || !isRadiusIncreasing) {
  75. x_t = -sqrt(temp) - p.x * invR1;
  76. } else {
  77. x_t = sqrt(temp) - p.x * invR1;
  78. }
  79. }
  80. }
  81. // The final calculation of t from x_t has lots of static optimizations but only do them
  82. // when x_t is positive (which can be assumed true if isWellBehaved is true)
  83. @if (!isWellBehaved) {
  84. // This will still calculate t even though it will be ignored later in the pipeline
  85. // to avoid a branch
  86. if (x_t <= 0.0) {
  87. v = -1;
  88. }
  89. }
  90. @if (isRadiusIncreasing) {
  91. @if (isNativelyFocal) {
  92. t = x_t;
  93. } else {
  94. t = x_t + fx;
  95. }
  96. } else {
  97. @if (isNativelyFocal) {
  98. t = -x_t;
  99. } else {
  100. t = -x_t + fx;
  101. }
  102. }
  103. @if(isSwapped) {
  104. t = 1 - t;
  105. }
  106. }
  107. break;
  108. }
  109. sk_OutColor = half4(half(t), v, 0, 0);
  110. }
  111. //////////////////////////////////////////////////////////////////////////////
  112. @header {
  113. #include "src/gpu/gradients/GrGradientShader.h"
  114. #include "src/shaders/gradients/SkTwoPointConicalGradient.h"
  115. }
  116. // The 2 point conical gradient can reject a pixel so it does change opacity
  117. // even if the input was opaque, so disable that optimization
  118. @optimizationFlags {
  119. kNone_OptimizationFlags
  120. }
  121. @make {
  122. static std::unique_ptr<GrFragmentProcessor> Make(const SkTwoPointConicalGradient& gradient,
  123. const GrFPArgs& args);
  124. }
  125. @cppEnd {
  126. // .fp files do not let you reference outside enum definitions, so we have to explicitly map
  127. // between the two compatible enum defs
  128. GrTwoPointConicalGradientLayout::Type convert_type(
  129. SkTwoPointConicalGradient::Type type) {
  130. switch(type) {
  131. case SkTwoPointConicalGradient::Type::kRadial:
  132. return GrTwoPointConicalGradientLayout::Type::kRadial;
  133. case SkTwoPointConicalGradient::Type::kStrip:
  134. return GrTwoPointConicalGradientLayout::Type::kStrip;
  135. case SkTwoPointConicalGradient::Type::kFocal:
  136. return GrTwoPointConicalGradientLayout::Type::kFocal;
  137. }
  138. SkDEBUGFAIL("Should not be reachable");
  139. return GrTwoPointConicalGradientLayout::Type::kRadial;
  140. }
  141. std::unique_ptr<GrFragmentProcessor> GrTwoPointConicalGradientLayout::Make(
  142. const SkTwoPointConicalGradient& grad, const GrFPArgs& args) {
  143. GrTwoPointConicalGradientLayout::Type grType = convert_type(grad.getType());
  144. // The focalData struct is only valid if isFocal is true
  145. const SkTwoPointConicalGradient::FocalData& focalData = grad.getFocalData();
  146. bool isFocal = grType == Type::kFocal;
  147. // Calculate optimization switches from gradient specification
  148. bool isFocalOnCircle = isFocal && focalData.isFocalOnCircle();
  149. bool isWellBehaved = isFocal && focalData.isWellBehaved();
  150. bool isSwapped = isFocal && focalData.isSwapped();
  151. bool isNativelyFocal = isFocal && focalData.isNativelyFocal();
  152. // Type-specific calculations: isRadiusIncreasing, focalParams, and the gradient matrix.
  153. // However, all types start with the total inverse local matrix calculated from the shader
  154. // and args
  155. bool isRadiusIncreasing;
  156. SkPoint focalParams; // really just a 2D tuple
  157. SkMatrix matrix;
  158. // Initialize the base matrix
  159. if (!grad.totalLocalMatrix(args.fPreLocalMatrix, args.fPostLocalMatrix)->invert(&matrix)) {
  160. return nullptr;
  161. }
  162. if (isFocal) {
  163. isRadiusIncreasing = (1 - focalData.fFocalX) > 0;
  164. focalParams.set(1.0 / focalData.fR1, focalData.fFocalX);
  165. matrix.postConcat(grad.getGradientMatrix());
  166. } else if (grType == Type::kRadial) {
  167. SkScalar dr = grad.getDiffRadius();
  168. isRadiusIncreasing = dr >= 0;
  169. SkScalar r0 = grad.getStartRadius() / dr;
  170. focalParams.set(r0, r0 * r0);
  171. // GPU radial matrix is different from the original matrix, since we map the diff radius
  172. // to have |dr| = 1, so manually compute the final gradient matrix here.
  173. // Map center to (0, 0)
  174. matrix.postTranslate(-grad.getStartCenter().fX, -grad.getStartCenter().fY);
  175. // scale |diffRadius| to 1
  176. matrix.postScale(1 / dr, 1 / dr);
  177. } else { // kStrip
  178. isRadiusIncreasing = false; // kStrip doesn't use this flag
  179. SkScalar r0 = grad.getStartRadius() / grad.getCenterX1();
  180. focalParams.set(r0, r0 * r0);
  181. matrix.postConcat(grad.getGradientMatrix());
  182. }
  183. return std::unique_ptr<GrFragmentProcessor>(new GrTwoPointConicalGradientLayout(
  184. matrix, grType, isRadiusIncreasing, isFocalOnCircle, isWellBehaved,
  185. isSwapped, isNativelyFocal, focalParams));
  186. }
  187. }
  188. //////////////////////////////////////////////////////////////////////////////
  189. @test(d) {
  190. SkScalar scale = GrGradientShader::RandomParams::kGradientScale;
  191. SkScalar offset = scale / 32.0f;
  192. SkPoint center1 = {d->fRandom->nextRangeScalar(0.0f, scale),
  193. d->fRandom->nextRangeScalar(0.0f, scale)};
  194. SkPoint center2 = {d->fRandom->nextRangeScalar(0.0f, scale),
  195. d->fRandom->nextRangeScalar(0.0f, scale)};
  196. SkScalar radius1 = d->fRandom->nextRangeScalar(0.0f, scale);
  197. SkScalar radius2 = d->fRandom->nextRangeScalar(0.0f, scale);
  198. constexpr int kTestTypeMask = (1 << 2) - 1,
  199. kTestNativelyFocalBit = (1 << 2),
  200. kTestFocalOnCircleBit = (1 << 3),
  201. kTestSwappedBit = (1 << 4);
  202. // We won't treat isWellDefined and isRadiusIncreasing specially because they
  203. // should have high probability to be turned on and off as we're getting random
  204. // radii and centers.
  205. int mask = d->fRandom->nextU();
  206. int type = mask & kTestTypeMask;
  207. if (type == static_cast<int>(Type::kRadial)) {
  208. center2 = center1;
  209. // Make sure that the radii are different
  210. if (SkScalarNearlyZero(radius1 - radius2)) {
  211. radius2 += offset;
  212. }
  213. } else if (type == static_cast<int>(Type::kStrip)) {
  214. radius1 = SkTMax(radius1, .1f); // Make sure that the radius is non-zero
  215. radius2 = radius1;
  216. // Make sure that the centers are different
  217. if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
  218. center2.fX += offset;
  219. }
  220. } else { // kFocal_Type
  221. // Make sure that the centers are different
  222. if (SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
  223. center2.fX += offset;
  224. }
  225. if (kTestNativelyFocalBit & mask) {
  226. radius1 = 0;
  227. }
  228. if (kTestFocalOnCircleBit & mask) {
  229. radius2 = radius1 + SkPoint::Distance(center1, center2);
  230. }
  231. if (kTestSwappedBit & mask) {
  232. std::swap(radius1, radius2);
  233. radius2 = 0;
  234. }
  235. // Make sure that the radii are different
  236. if (SkScalarNearlyZero(radius1 - radius2)) {
  237. radius2 += offset;
  238. }
  239. }
  240. if (SkScalarNearlyZero(radius1 - radius2) &&
  241. SkScalarNearlyZero(SkPoint::Distance(center1, center2))) {
  242. radius2 += offset; // make sure that we're not degenerated
  243. }
  244. GrGradientShader::RandomParams params(d->fRandom);
  245. auto shader = params.fUseColors4f ?
  246. SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2,
  247. params.fColors4f, params.fColorSpace, params.fStops,
  248. params.fColorCount, params.fTileMode) :
  249. SkGradientShader::MakeTwoPointConical(center1, radius1, center2, radius2,
  250. params.fColors, params.fStops,
  251. params.fColorCount, params.fTileMode);
  252. GrTest::TestAsFPArgs asFPArgs(d);
  253. std::unique_ptr<GrFragmentProcessor> fp = as_SB(shader)->asFragmentProcessor(asFPArgs.args());
  254. GrAlwaysAssert(fp);
  255. return fp;
  256. }