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