blob: 012fce7f0dae8b095b81f47a3e97e5314aabbb3d [file] [log] [blame]
bsalomon50785a32015-02-06 07:02:37 -08001/*
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
egdanielfa4cc8b2015-11-13 08:34:52 -08008#include "glsl/GrGLSLXferProcessor.h"
bsalomon50785a32015-02-06 07:02:37 -08009
Brian Salomon94efbf52016-11-29 13:43:05 -050010#include "GrShaderCaps.h"
Robert Phillipsc9c06d42017-06-12 10:58:31 -040011#include "GrTexture.h"
bsalomon50785a32015-02-06 07:02:37 -080012#include "GrXferProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080013#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070014#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080015#include "glsl/GrGLSLUniformHandler.h"
bsalomon50785a32015-02-06 07:02:37 -080016
Greg Daniel6ebe4b92017-05-19 10:56:46 -040017// 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.
20static 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
egdanielfa4cc8b2015-11-13 08:34:52 -080030void GrGLSLXferProcessor::emitCode(const EmitArgs& args) {
cdaltonedbb31f2015-06-08 12:14:44 -070031 if (!args.fXP.willReadDstColor()) {
Greg Daniel6ebe4b92017-05-19 10:56:46 -040032 adjust_for_lcd_coverage(args.fXPFragBuilder, args.fInputCoverage, args.fXP);
cdaltonedbb31f2015-06-08 12:14:44 -070033 this->emitOutputsForBlendState(args);
34 return;
35 }
36
egdaniel4ca2e602015-11-18 08:01:26 -080037 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080038 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
egdaniel4ca2e602015-11-18 08:01:26 -080039 const char* dstColor = fragBuilder->dstColor();
cdaltonedbb31f2015-06-08 12:14:44 -070040
egdaniel138c2632016-08-17 10:59:00 -070041 bool needsLocalOutColor = false;
42
Brian Salomon18dfa982017-04-03 16:57:43 -040043 if (args.fDstTextureSamplerHandle.isValid()) {
44 bool flipY = kBottomLeft_GrSurfaceOrigin == args.fDstTextureOrigin;
bsalomon50785a32015-02-06 07:02:37 -080045
egdaniel56cf6dc2015-11-30 10:15:58 -080046 if (args.fInputCoverage) {
egdanielc19cdc22015-05-10 08:45:18 -070047 // We don't think any shaders actually output negative coverage, but just as a safety
Greg Daniel55923822017-05-22 16:34:34 -040048 // 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 Nicholasf7b88202017-09-18 14:10:39 -040055 fragBuilder->codeAppendf("if (all(lessThanEqual(%s.rgb, half3(0)))) {"
egdaniel4ca2e602015-11-18 08:01:26 -080056 " discard;"
57 "}", args.fInputCoverage);
egdanielc19cdc22015-05-10 08:45:18 -070058 }
59
bsalomon6a44c6a2015-05-26 09:49:05 -070060 const char* dstTopLeftName;
61 const char* dstCoordScaleName;
bsalomon50785a32015-02-06 07:02:37 -080062
cdalton5e58cee2016-02-11 12:49:47 -080063 fDstTopLeftUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040064 kHalf2_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080065 "DstTextureUpperLeft",
66 &dstTopLeftName);
cdalton5e58cee2016-02-11 12:49:47 -080067 fDstScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
Ethan Nicholasf7b88202017-09-18 14:10:39 -040068 kHalf2_GrSLType,
egdaniel7ea439b2015-12-03 09:20:44 -080069 "DstTextureCoordScale",
70 &dstCoordScaleName);
bsalomon50785a32015-02-06 07:02:37 -080071
egdaniel4ca2e602015-11-18 08:01:26 -080072 fragBuilder->codeAppend("// Read color from copy of the destination.\n");
Ethan Nicholase1f55022019-02-05 17:17:40 -050073 fragBuilder->codeAppendf("half2 _dstTexCoord = (half2(sk_FragCoord.xy) - %s) * %s;",
Ethan Nicholas38657112017-02-09 17:01:22 -050074 dstTopLeftName, dstCoordScaleName);
bsalomon50785a32015-02-06 07:02:37 -080075
Brian Salomon18dfa982017-04-03 16:57:43 -040076 if (flipY) {
egdaniel4ca2e602015-11-18 08:01:26 -080077 fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;");
bsalomon50785a32015-02-06 07:02:37 -080078 }
79
Ethan Nicholasf7b88202017-09-18 14:10:39 -040080 fragBuilder->codeAppendf("half4 %s = ", dstColor);
Brian Salomon18dfa982017-04-03 16:57:43 -040081 fragBuilder->appendTextureLookup(args.fDstTextureSamplerHandle, "_dstTexCoord",
Ethan Nicholasf7b88202017-09-18 14:10:39 -040082 kHalf2_GrSLType);
egdaniel4ca2e602015-11-18 08:01:26 -080083 fragBuilder->codeAppend(";");
egdaniel138c2632016-08-17 10:59:00 -070084 } else {
Brian Salomon1edc5b92016-11-29 13:43:46 -050085 needsLocalOutColor = args.fShaderCaps->requiresLocalOutputColorForFBFetch();
egdaniel138c2632016-08-17 10:59:00 -070086 }
87
88 const char* outColor = "_localColorOut";
89 if (!needsLocalOutColor) {
90 outColor = args.fOutputPrimary;
91 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -040092 fragBuilder->codeAppendf("half4 %s;", outColor);
bsalomon50785a32015-02-06 07:02:37 -080093 }
94
egdaniel7ea439b2015-12-03 09:20:44 -080095 this->emitBlendCodeForDstRead(fragBuilder,
96 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080097 args.fInputColor,
egdanielf34b2932015-12-01 13:54:06 -080098 args.fInputCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -080099 dstColor,
egdaniel138c2632016-08-17 10:59:00 -0700100 outColor,
egdanielf34b2932015-12-01 13:54:06 -0800101 args.fOutputSecondary,
cdaltonedbb31f2015-06-08 12:14:44 -0700102 args.fXP);
egdaniel138c2632016-08-17 10:59:00 -0700103 if (needsLocalOutColor) {
104 fragBuilder->codeAppendf("%s = %s;", args.fOutputPrimary, outColor);
105 }
bsalomon50785a32015-02-06 07:02:37 -0800106}
107
Brian Salomon18dfa982017-04-03 16:57:43 -0400108void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp,
109 const GrTexture* dstTexture, const SkIPoint& dstTextureOffset) {
110 if (dstTexture) {
bsalomon6a44c6a2015-05-26 09:49:05 -0700111 if (fDstTopLeftUni.isValid()) {
Brian Salomon18dfa982017-04-03 16:57:43 -0400112 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());
bsalomon50785a32015-02-06 07:02:37 -0800115 } else {
bsalomon6a44c6a2015-05-26 09:49:05 -0700116 SkASSERT(!fDstScaleUni.isValid());
bsalomon50785a32015-02-06 07:02:37 -0800117 }
118 } else {
bsalomon6a44c6a2015-05-26 09:49:05 -0700119 SkASSERT(!fDstTopLeftUni.isValid());
120 SkASSERT(!fDstScaleUni.isValid());
bsalomon50785a32015-02-06 07:02:37 -0800121 }
122 this->onSetData(pdm, xp);
123}
124
robertphillips727b7d22016-01-26 12:07:13 -0800125void 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 Danield547fb62017-05-19 14:11:06 -0400133 // 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());
robertphillips727b7d22016-01-26 12:07:13 -0800137 fragBuilder->codeAppendf("%s *= %s;", outColor, srcCoverage);
138 fragBuilder->codeAppendf("%s = %s;", outColorSecondary, srcCoverage);
139 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400140 fragBuilder->codeAppendf("%s = half4(1.0);", outColorSecondary);
robertphillips727b7d22016-01-26 12:07:13 -0800141 }
142 } else if (srcCoverage) {
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400143 if (proc.isLCD()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400144 fragBuilder->codeAppendf("half lerpRed = mix(%s.a, %s.a, %s.r);",
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400145 dstColor, outColor, srcCoverage);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400146 fragBuilder->codeAppendf("half lerpBlue = mix(%s.a, %s.a, %s.g);",
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400147 dstColor, outColor, srcCoverage);
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400148 fragBuilder->codeAppendf("half lerpGreen = mix(%s.a, %s.a, %s.b);",
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400149 dstColor, outColor, srcCoverage);
150 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400151 fragBuilder->codeAppendf("%s = %s * %s + (half4(1.0) - %s) * %s;",
robertphillips727b7d22016-01-26 12:07:13 -0800152 outColor, srcCoverage, outColor, srcCoverage, dstColor);
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400153 if (proc.isLCD()) {
154 fragBuilder->codeAppendf("%s.a = max(max(lerpRed, lerpBlue), lerpGreen);", outColor);
155 }
robertphillips727b7d22016-01-26 12:07:13 -0800156 }
157}
Greg Daniel6ebe4b92017-05-19 10:56:46 -0400158