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