GrGLSLXferProcessor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2014 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/glsl/GrGLSLXferProcessor.h"
  8. #include "include/gpu/GrTexture.h"
  9. #include "src/gpu/GrShaderCaps.h"
  10. #include "src/gpu/GrXferProcessor.h"
  11. #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
  12. #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
  13. #include "src/gpu/glsl/GrGLSLUniformHandler.h"
  14. // This is only called for cases where we are doing LCD coverage and not using in shader blending.
  15. // For these cases we assume the the src alpha is 1, thus we can just use the max for the alpha
  16. // coverage since src alpha will always be greater than or equal to dst alpha.
  17. static void adjust_for_lcd_coverage(GrGLSLXPFragmentBuilder* fragBuilder,
  18. const char* srcCoverage,
  19. const GrXferProcessor& proc) {
  20. if (srcCoverage && proc.isLCD()) {
  21. fragBuilder->codeAppendf("%s.a = max(max(%s.r, %s.g), %s.b);",
  22. srcCoverage, srcCoverage, srcCoverage, srcCoverage);
  23. }
  24. }
  25. void GrGLSLXferProcessor::emitCode(const EmitArgs& args) {
  26. if (!args.fXP.willReadDstColor()) {
  27. adjust_for_lcd_coverage(args.fXPFragBuilder, args.fInputCoverage, args.fXP);
  28. this->emitOutputsForBlendState(args);
  29. } else {
  30. GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
  31. GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
  32. const char* dstColor = fragBuilder->dstColor();
  33. bool needsLocalOutColor = false;
  34. if (args.fDstTextureSamplerHandle.isValid()) {
  35. bool flipY = kBottomLeft_GrSurfaceOrigin == args.fDstTextureOrigin;
  36. if (args.fInputCoverage) {
  37. // We don't think any shaders actually output negative coverage, but just as a
  38. // safety check for floating point precision errors we compare with <= here. We just
  39. // check the rgb values of the coverage since the alpha may not have been set when
  40. // using lcd. If we are using single channel coverage alpha will equal to rgb
  41. // anyways.
  42. //
  43. // The discard here also helps for batching text draws together which need to read
  44. // from a dst copy for blends. Though this only helps the case where the outer
  45. // bounding boxes of each letter overlap and not two actually parts of the text.
  46. fragBuilder->codeAppendf("if (all(lessThanEqual(%s.rgb, half3(0)))) {"
  47. " discard;"
  48. "}", args.fInputCoverage);
  49. }
  50. const char* dstTopLeftName;
  51. const char* dstCoordScaleName;
  52. fDstTopLeftUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
  53. kHalf2_GrSLType,
  54. "DstTextureUpperLeft",
  55. &dstTopLeftName);
  56. fDstScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
  57. kHalf2_GrSLType,
  58. "DstTextureCoordScale",
  59. &dstCoordScaleName);
  60. fragBuilder->codeAppend("// Read color from copy of the destination.\n");
  61. fragBuilder->codeAppendf("half2 _dstTexCoord = (half2(sk_FragCoord.xy) - %s) * %s;",
  62. dstTopLeftName, dstCoordScaleName);
  63. if (flipY) {
  64. fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;");
  65. }
  66. fragBuilder->codeAppendf("half4 %s = ", dstColor);
  67. fragBuilder->appendTextureLookup(args.fDstTextureSamplerHandle, "_dstTexCoord",
  68. kHalf2_GrSLType);
  69. fragBuilder->codeAppend(";");
  70. } else {
  71. needsLocalOutColor = args.fShaderCaps->requiresLocalOutputColorForFBFetch();
  72. }
  73. const char* outColor = "_localColorOut";
  74. if (!needsLocalOutColor) {
  75. outColor = args.fOutputPrimary;
  76. } else {
  77. fragBuilder->codeAppendf("half4 %s;", outColor);
  78. }
  79. this->emitBlendCodeForDstRead(fragBuilder,
  80. uniformHandler,
  81. args.fInputColor,
  82. args.fInputCoverage,
  83. dstColor,
  84. outColor,
  85. args.fOutputSecondary,
  86. args.fXP);
  87. if (needsLocalOutColor) {
  88. fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, outColor);
  89. }
  90. }
  91. // Swizzle the fragment shader outputs if necessary.
  92. this->emitOutputSwizzle(
  93. args.fXPFragBuilder, args.fOutputSwizzle, args.fOutputPrimary, args.fOutputSecondary);
  94. }
  95. void GrGLSLXferProcessor::emitOutputSwizzle(
  96. GrGLSLXPFragmentBuilder* x, const GrSwizzle& swizzle, const char* outColor,
  97. const char* outColorSecondary) const {
  98. if (GrSwizzle::RGBA() != swizzle) {
  99. x->codeAppendf("%s = %s.%s;", outColor, outColor, swizzle.c_str());
  100. if (outColorSecondary) {
  101. x->codeAppendf("%s = %s.%s;", outColorSecondary, outColorSecondary, swizzle.c_str());
  102. }
  103. }
  104. }
  105. void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp,
  106. const GrTexture* dstTexture, const SkIPoint& dstTextureOffset) {
  107. if (dstTexture) {
  108. if (fDstTopLeftUni.isValid()) {
  109. pdm.set2f(fDstTopLeftUni, static_cast<float>(dstTextureOffset.fX),
  110. static_cast<float>(dstTextureOffset.fY));
  111. pdm.set2f(fDstScaleUni, 1.f / dstTexture->width(), 1.f / dstTexture->height());
  112. } else {
  113. SkASSERT(!fDstScaleUni.isValid());
  114. }
  115. } else {
  116. SkASSERT(!fDstTopLeftUni.isValid());
  117. SkASSERT(!fDstScaleUni.isValid());
  118. }
  119. this->onSetData(pdm, xp);
  120. }
  121. void GrGLSLXferProcessor::DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
  122. const char* srcCoverage,
  123. const char* dstColor,
  124. const char* outColor,
  125. const char* outColorSecondary,
  126. const GrXferProcessor& proc) {
  127. if (proc.dstReadUsesMixedSamples()) {
  128. if (srcCoverage) {
  129. // TODO: Once we are no longer using legacy mesh ops, it will not be possible to even
  130. // create a mixed sample with lcd so we can uncomment the below assert. In practice
  131. // today this never happens except for GLPrograms test which can make one. skia:6661
  132. // SkASSERT(!proc.isLCD());
  133. fragBuilder->codeAppendf("%s *= %s;", outColor, srcCoverage);
  134. fragBuilder->codeAppendf("%s = %s;", outColorSecondary, srcCoverage);
  135. } else {
  136. fragBuilder->codeAppendf("%s = half4(1.0);", outColorSecondary);
  137. }
  138. } else if (srcCoverage) {
  139. if (proc.isLCD()) {
  140. fragBuilder->codeAppendf("half lerpRed = mix(%s.a, %s.a, %s.r);",
  141. dstColor, outColor, srcCoverage);
  142. fragBuilder->codeAppendf("half lerpBlue = mix(%s.a, %s.a, %s.g);",
  143. dstColor, outColor, srcCoverage);
  144. fragBuilder->codeAppendf("half lerpGreen = mix(%s.a, %s.a, %s.b);",
  145. dstColor, outColor, srcCoverage);
  146. }
  147. fragBuilder->codeAppendf("%s = %s * %s + (half4(1.0) - %s) * %s;",
  148. outColor, srcCoverage, outColor, srcCoverage, dstColor);
  149. if (proc.isLCD()) {
  150. fragBuilder->codeAppendf("%s.a = max(max(lerpRed, lerpBlue), lerpGreen);", outColor);
  151. }
  152. }
  153. }