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 | |
| 10 | #include "GrXferProcessor.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 11 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 12 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLUniformHandler.h" |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 14 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 15 | void GrGLSLXferProcessor::emitCode(const EmitArgs& args) { |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 16 | if (!args.fXP.willReadDstColor()) { |
| 17 | this->emitOutputsForBlendState(args); |
| 18 | return; |
| 19 | } |
| 20 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 21 | GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 22 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 23 | const char* dstColor = fragBuilder->dstColor(); |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 24 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 25 | if (args.fXP.getDstTexture()) { |
| 26 | bool topDown = kTopLeft_GrSurfaceOrigin == args.fXP.getDstTexture()->origin(); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 27 | |
egdaniel | 56cf6dc | 2015-11-30 10:15:58 -0800 | [diff] [blame] | 28 | if (args.fInputCoverage) { |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 29 | // 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 |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 31 | fragBuilder->codeAppendf("if (all(lessThanEqual(%s, vec4(0)))) {" |
| 32 | " discard;" |
| 33 | "}", args.fInputCoverage); |
egdaniel | c19cdc2 | 2015-05-10 08:45:18 -0700 | [diff] [blame] | 34 | } |
| 35 | |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 36 | const char* dstTopLeftName; |
| 37 | const char* dstCoordScaleName; |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 38 | |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame^] | 39 | fDstTopLeftUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 40 | kVec2f_GrSLType, |
| 41 | kDefault_GrSLPrecision, |
| 42 | "DstTextureUpperLeft", |
| 43 | &dstTopLeftName); |
cdalton | 5e58cee | 2016-02-11 12:49:47 -0800 | [diff] [blame^] | 44 | fDstScaleUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 45 | kVec2f_GrSLType, |
| 46 | kDefault_GrSLPrecision, |
| 47 | "DstTextureCoordScale", |
| 48 | &dstCoordScaleName); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 49 | const char* fragPos = fragBuilder->fragmentPosition(); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 50 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 51 | fragBuilder->codeAppend("// Read color from copy of the destination.\n"); |
| 52 | fragBuilder->codeAppendf("vec2 _dstTexCoord = (%s.xy - %s) * %s;", |
| 53 | fragPos, dstTopLeftName, dstCoordScaleName); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 54 | |
| 55 | if (!topDown) { |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 56 | fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;"); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 57 | } |
| 58 | |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 59 | fragBuilder->codeAppendf("vec4 %s = ", dstColor); |
| 60 | fragBuilder->appendTextureLookup(args.fSamplers[0], "_dstTexCoord", kVec2f_GrSLType); |
| 61 | fragBuilder->codeAppend(";"); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 62 | } |
| 63 | |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 64 | this->emitBlendCodeForDstRead(fragBuilder, |
| 65 | uniformHandler, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 66 | args.fInputColor, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 67 | args.fInputCoverage, |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 68 | dstColor, |
| 69 | args.fOutputPrimary, |
egdaniel | f34b293 | 2015-12-01 13:54:06 -0800 | [diff] [blame] | 70 | args.fOutputSecondary, |
cdalton | edbb31f | 2015-06-08 12:14:44 -0700 | [diff] [blame] | 71 | args.fXP); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 72 | } |
| 73 | |
egdaniel | fa4cc8b | 2015-11-13 08:34:52 -0800 | [diff] [blame] | 74 | void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrXferProcessor& xp) { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 75 | if (xp.getDstTexture()) { |
| 76 | if (fDstTopLeftUni.isValid()) { |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 77 | pdm.set2f(fDstTopLeftUni, static_cast<float>(xp.dstTextureOffset().fX), |
| 78 | static_cast<float>(xp.dstTextureOffset().fY)); |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 79 | pdm.set2f(fDstScaleUni, 1.f / xp.getDstTexture()->width(), |
| 80 | 1.f / xp.getDstTexture()->height()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 81 | } else { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 82 | SkASSERT(!fDstScaleUni.isValid()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 83 | } |
| 84 | } else { |
bsalomon | 6a44c6a | 2015-05-26 09:49:05 -0700 | [diff] [blame] | 85 | SkASSERT(!fDstTopLeftUni.isValid()); |
| 86 | SkASSERT(!fDstScaleUni.isValid()); |
bsalomon | 50785a3 | 2015-02-06 07:02:37 -0800 | [diff] [blame] | 87 | } |
| 88 | this->onSetData(pdm, xp); |
| 89 | } |
| 90 | |
robertphillips | 727b7d2 | 2016-01-26 12:07:13 -0800 | [diff] [blame] | 91 | void 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 | } |