OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "gl/GrGLXferProcessor.h" | 8 #include "gl/GrGLXferProcessor.h" |
9 | 9 |
10 #include "GrXferProcessor.h" | 10 #include "GrXferProcessor.h" |
11 #include "gl/builders/GrGLFragmentShaderBuilder.h" | 11 #include "gl/builders/GrGLFragmentShaderBuilder.h" |
12 #include "gl/builders/GrGLProgramBuilder.h" | 12 #include "gl/builders/GrGLProgramBuilder.h" |
13 | 13 |
14 void GrGLXferProcessor::emitCode(const EmitArgs& args) { | 14 void GrGLXferProcessor::emitCode(const EmitArgs& args) { |
15 if (args.fXP.getDstTexture()) { | 15 if (args.fXP.willReadDstColor()) { |
16 bool topDown = kTopLeft_GrSurfaceOrigin == args.fXP.getDstTexture()->ori
gin(); | |
17 | |
18 GrGLXPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 16 GrGLXPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
19 | 17 |
20 if (args.fXP.readsCoverage()) { | 18 if (args.fXP.getDstTexture()) { |
21 // We don't think any shaders actually output negative coverage, but
just as a safety | 19 bool topDown = kTopLeft_GrSurfaceOrigin == args.fXP.getDstTexture()-
>origin(); |
22 // check for floating point precision errors we compare with <= here | 20 |
23 fsBuilder->codeAppendf("if (all(lessThanEqual(%s, vec4(0)))) {" | 21 if (args.fXP.readsCoverage()) { |
24 " discard;" | 22 // We don't think any shaders actually output negative coverage,
but just as a |
25 "}", args.fInputCoverage); | 23 // safety check for floating point precision errors we compare w
ith <= here |
| 24 fsBuilder->codeAppendf("if (all(lessThanEqual(%s, vec4(0)))) {" |
| 25 " discard;" |
| 26 "}", args.fInputCoverage); |
| 27 } |
| 28 |
| 29 const char* dstColor = fsBuilder->dstColor(); |
| 30 |
| 31 const char* dstTopLeftName; |
| 32 const char* dstCoordScaleName; |
| 33 |
| 34 fDstTopLeftUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_
Visibility, |
| 35 kVec2f_GrSLType, |
| 36 kDefault_GrSLPrecision, |
| 37 "DstTextureUpperLeft", |
| 38 &dstTopLeftName); |
| 39 fDstScaleUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Vi
sibility, |
| 40 kVec2f_GrSLType, |
| 41 kDefault_GrSLPrecision, |
| 42 "DstTextureCoordScale", |
| 43 &dstCoordScaleName); |
| 44 const char* fragPos = fsBuilder->fragmentPosition(); |
| 45 |
| 46 fsBuilder->codeAppend("// Read color from copy of the destination.\n
"); |
| 47 fsBuilder->codeAppendf("vec2 _dstTexCoord = (%s.xy - %s) * %s;", |
| 48 fragPos, dstTopLeftName, dstCoordScaleName); |
| 49 |
| 50 if (!topDown) { |
| 51 fsBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;"); |
| 52 } |
| 53 |
| 54 fsBuilder->codeAppendf("vec4 %s = ", dstColor); |
| 55 fsBuilder->appendTextureLookup(args.fSamplers[0], "_dstTexCoord", kV
ec2f_GrSLType); |
| 56 fsBuilder->codeAppend(";"); |
26 } | 57 } |
27 | 58 |
28 const char* dstColor = fsBuilder->dstColor(); | 59 if (args.fXP.dstReadUsesMixedSamples()) { |
29 | 60 // Output 1.0, which the hardware will modulate by sample-based cove
rage before blend. |
30 const char* dstTopLeftName; | 61 fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputSecondary); |
31 const char* dstCoordScaleName; | |
32 | |
33 fDstTopLeftUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visi
bility, | |
34 kVec2f_GrSLType, | |
35 kDefault_GrSLPrecision, | |
36 "DstTextureUpperLeft", | |
37 &dstTopLeftName); | |
38 fDstScaleUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibi
lity, | |
39 kVec2f_GrSLType, | |
40 kDefault_GrSLPrecision, | |
41 "DstTextureCoordScale", | |
42 &dstCoordScaleName); | |
43 const char* fragPos = fsBuilder->fragmentPosition(); | |
44 | |
45 fsBuilder->codeAppend("// Read color from copy of the destination.\n"); | |
46 fsBuilder->codeAppendf("vec2 _dstTexCoord = (%s.xy - %s) * %s;", | |
47 fragPos, dstTopLeftName, dstCoordScaleName); | |
48 | |
49 if (!topDown) { | |
50 fsBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;"); | |
51 } | 62 } |
52 | |
53 fsBuilder->codeAppendf("vec4 %s = ", dstColor); | |
54 fsBuilder->appendTextureLookup(args.fSamplers[0], "_dstTexCoord", kVec2f
_GrSLType); | |
55 fsBuilder->codeAppend(";"); | |
56 } | 63 } |
57 | 64 |
58 this->onEmitCode(args); | 65 this->onEmitCode(args); |
59 } | 66 } |
60 | 67 |
61 void GrGLXferProcessor::setData(const GrGLProgramDataManager& pdm, const GrXferP
rocessor& xp) { | 68 void GrGLXferProcessor::setData(const GrGLProgramDataManager& pdm, const GrXferP
rocessor& xp) { |
62 if (xp.getDstTexture()) { | 69 if (xp.getDstTexture()) { |
63 if (fDstTopLeftUni.isValid()) { | 70 if (fDstTopLeftUni.isValid()) { |
64 pdm.set2f(fDstTopLeftUni, static_cast<GrGLfloat>(xp.dstTextureOffset
().fX), | 71 pdm.set2f(fDstTopLeftUni, static_cast<GrGLfloat>(xp.dstTextureOffset
().fX), |
65 static_cast<GrGLfloat>(xp.dstTextureOffset().fY)); | 72 static_cast<GrGLfloat>(xp.dstTextureOffset().fY)); |
66 pdm.set2f(fDstScaleUni, 1.f / xp.getDstTexture()->width(), | 73 pdm.set2f(fDstScaleUni, 1.f / xp.getDstTexture()->width(), |
67 1.f / xp.getDstTexture()->height()); | 74 1.f / xp.getDstTexture()->height()); |
68 } else { | 75 } else { |
69 SkASSERT(!fDstScaleUni.isValid()); | 76 SkASSERT(!fDstScaleUni.isValid()); |
70 } | 77 } |
71 } else { | 78 } else { |
72 SkASSERT(!fDstTopLeftUni.isValid()); | 79 SkASSERT(!fDstTopLeftUni.isValid()); |
73 SkASSERT(!fDstScaleUni.isValid()); | 80 SkASSERT(!fDstScaleUni.isValid()); |
74 } | 81 } |
75 this->onSetData(pdm, xp); | 82 this->onSetData(pdm, xp); |
76 } | 83 } |
77 | 84 |
OLD | NEW |