blob: c4b62be525c600055a62e1d178b7cd0394a6d6fc [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
10#include "GrXferProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080011#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070012#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080013#include "glsl/GrGLSLUniformHandler.h"
bsalomon50785a32015-02-06 07:02:37 -080014
egdanielfa4cc8b2015-11-13 08:34:52 -080015void GrGLSLXferProcessor::emitCode(const EmitArgs& args) {
cdaltonedbb31f2015-06-08 12:14:44 -070016 if (!args.fXP.willReadDstColor()) {
17 this->emitOutputsForBlendState(args);
18 return;
19 }
20
egdaniel4ca2e602015-11-18 08:01:26 -080021 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder;
egdaniel7ea439b2015-12-03 09:20:44 -080022 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
egdaniel4ca2e602015-11-18 08:01:26 -080023 const char* dstColor = fragBuilder->dstColor();
cdaltonedbb31f2015-06-08 12:14:44 -070024
bsalomon6a44c6a2015-05-26 09:49:05 -070025 if (args.fXP.getDstTexture()) {
26 bool topDown = kTopLeft_GrSurfaceOrigin == args.fXP.getDstTexture()->origin();
bsalomon50785a32015-02-06 07:02:37 -080027
egdaniel56cf6dc2015-11-30 10:15:58 -080028 if (args.fInputCoverage) {
egdanielc19cdc22015-05-10 08:45:18 -070029 // We don't think any shaders actually output negative coverage, but just as a safety
30 // check for floating point precision errors we compare with <= here
egdaniel4ca2e602015-11-18 08:01:26 -080031 fragBuilder->codeAppendf("if (all(lessThanEqual(%s, vec4(0)))) {"
32 " discard;"
33 "}", args.fInputCoverage);
egdanielc19cdc22015-05-10 08:45:18 -070034 }
35
bsalomon6a44c6a2015-05-26 09:49:05 -070036 const char* dstTopLeftName;
37 const char* dstCoordScaleName;
bsalomon50785a32015-02-06 07:02:37 -080038
cdalton5e58cee2016-02-11 12:49:47 -080039 fDstTopLeftUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080040 kVec2f_GrSLType,
41 kDefault_GrSLPrecision,
42 "DstTextureUpperLeft",
43 &dstTopLeftName);
cdalton5e58cee2016-02-11 12:49:47 -080044 fDstScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
egdaniel7ea439b2015-12-03 09:20:44 -080045 kVec2f_GrSLType,
46 kDefault_GrSLPrecision,
47 "DstTextureCoordScale",
48 &dstCoordScaleName);
egdaniel4ca2e602015-11-18 08:01:26 -080049 const char* fragPos = fragBuilder->fragmentPosition();
bsalomon50785a32015-02-06 07:02:37 -080050
egdaniel4ca2e602015-11-18 08:01:26 -080051 fragBuilder->codeAppend("// Read color from copy of the destination.\n");
52 fragBuilder->codeAppendf("vec2 _dstTexCoord = (%s.xy - %s) * %s;",
53 fragPos, dstTopLeftName, dstCoordScaleName);
bsalomon50785a32015-02-06 07:02:37 -080054
55 if (!topDown) {
egdaniel4ca2e602015-11-18 08:01:26 -080056 fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;");
bsalomon50785a32015-02-06 07:02:37 -080057 }
58
egdaniel4ca2e602015-11-18 08:01:26 -080059 fragBuilder->codeAppendf("vec4 %s = ", dstColor);
60 fragBuilder->appendTextureLookup(args.fSamplers[0], "_dstTexCoord", kVec2f_GrSLType);
61 fragBuilder->codeAppend(";");
bsalomon50785a32015-02-06 07:02:37 -080062 }
63
egdaniel7ea439b2015-12-03 09:20:44 -080064 this->emitBlendCodeForDstRead(fragBuilder,
65 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -080066 args.fInputColor,
egdanielf34b2932015-12-01 13:54:06 -080067 args.fInputCoverage,
egdaniel4ca2e602015-11-18 08:01:26 -080068 dstColor,
69 args.fOutputPrimary,
egdanielf34b2932015-12-01 13:54:06 -080070 args.fOutputSecondary,
cdaltonedbb31f2015-06-08 12:14:44 -070071 args.fXP);
bsalomon50785a32015-02-06 07:02:37 -080072}
73
egdanielfa4cc8b2015-11-13 08:34:52 -080074void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp) {
bsalomon6a44c6a2015-05-26 09:49:05 -070075 if (xp.getDstTexture()) {
76 if (fDstTopLeftUni.isValid()) {
egdaniel018fb622015-10-28 07:26:40 -070077 pdm.set2f(fDstTopLeftUni, static_cast<float>(xp.dstTextureOffset().fX),
78 static_cast<float>(xp.dstTextureOffset().fY));
bsalomon6a44c6a2015-05-26 09:49:05 -070079 pdm.set2f(fDstScaleUni, 1.f / xp.getDstTexture()->width(),
80 1.f / xp.getDstTexture()->height());
bsalomon50785a32015-02-06 07:02:37 -080081 } else {
bsalomon6a44c6a2015-05-26 09:49:05 -070082 SkASSERT(!fDstScaleUni.isValid());
bsalomon50785a32015-02-06 07:02:37 -080083 }
84 } else {
bsalomon6a44c6a2015-05-26 09:49:05 -070085 SkASSERT(!fDstTopLeftUni.isValid());
86 SkASSERT(!fDstScaleUni.isValid());
bsalomon50785a32015-02-06 07:02:37 -080087 }
88 this->onSetData(pdm, xp);
89}
90
robertphillips727b7d22016-01-26 12:07:13 -080091void GrGLSLXferProcessor::DefaultCoverageModulation(GrGLSLXPFragmentBuilder* fragBuilder,
92 const char* srcCoverage,
93 const char* dstColor,
94 const char* outColor,
95 const char* outColorSecondary,
96 const GrXferProcessor& proc) {
97 if (proc.dstReadUsesMixedSamples()) {
98 if (srcCoverage) {
99 fragBuilder->codeAppendf("%s *= %s;", outColor, srcCoverage);
100 fragBuilder->codeAppendf("%s = %s;", outColorSecondary, srcCoverage);
101 } else {
102 fragBuilder->codeAppendf("%s = vec4(1.0);", outColorSecondary);
103 }
104 } else if (srcCoverage) {
105 fragBuilder->codeAppendf("%s = %s * %s + (vec4(1.0) - %s) * %s;",
106 outColor, srcCoverage, outColor, srcCoverage, dstColor);
107 }
108}