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 "glsl/GrGLSLXferProcessor.h" | 8 #include "glsl/GrGLSLXferProcessor.h" |
9 | 9 |
10 #include "GrXferProcessor.h" | 10 #include "GrXferProcessor.h" |
11 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 11 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
12 #include "glsl/GrGLSLProgramBuilder.h" | 12 #include "glsl/GrGLSLProgramBuilder.h" |
13 #include "glsl/GrGLSLProgramDataManager.h" | 13 #include "glsl/GrGLSLProgramDataManager.h" |
14 | 14 |
15 void GrGLSLXferProcessor::emitCode(const EmitArgs& args) { | 15 void GrGLSLXferProcessor::emitCode(const EmitArgs& args) { |
16 if (!args.fXP.willReadDstColor()) { | 16 if (!args.fXP.willReadDstColor()) { |
17 this->emitOutputsForBlendState(args); | 17 this->emitOutputsForBlendState(args); |
18 return; | 18 return; |
19 } | 19 } |
20 | 20 |
21 GrGLSLXPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); | 21 GrGLSLXPFragmentBuilder* fragBuilder = args.fXPFragBuilder; |
22 const char* dstColor = fsBuilder->dstColor(); | 22 const char* dstColor = fragBuilder->dstColor(); |
23 | 23 |
24 if (args.fXP.getDstTexture()) { | 24 if (args.fXP.getDstTexture()) { |
25 bool topDown = kTopLeft_GrSurfaceOrigin == args.fXP.getDstTexture()->ori
gin(); | 25 bool topDown = kTopLeft_GrSurfaceOrigin == args.fXP.getDstTexture()->ori
gin(); |
26 | 26 |
27 if (args.fXP.readsCoverage()) { | 27 if (args.fXP.readsCoverage()) { |
28 // We don't think any shaders actually output negative coverage, but
just as a safety | 28 // We don't think any shaders actually output negative coverage, but
just as a safety |
29 // check for floating point precision errors we compare with <= here | 29 // check for floating point precision errors we compare with <= here |
30 fsBuilder->codeAppendf("if (all(lessThanEqual(%s, vec4(0)))) {" | 30 fragBuilder->codeAppendf("if (all(lessThanEqual(%s, vec4(0)))) {" |
31 " discard;" | 31 " discard;" |
32 "}", args.fInputCoverage); | 32 "}", args.fInputCoverage); |
33 } | 33 } |
34 | 34 |
35 const char* dstTopLeftName; | 35 const char* dstTopLeftName; |
36 const char* dstCoordScaleName; | 36 const char* dstCoordScaleName; |
37 | 37 |
38 fDstTopLeftUni = args.fPB->addUniform(GrGLSLProgramBuilder::kFragment_Vi
sibility, | 38 fDstTopLeftUni = args.fPB->addUniform(GrGLSLProgramBuilder::kFragment_Vi
sibility, |
39 kVec2f_GrSLType, | 39 kVec2f_GrSLType, |
40 kDefault_GrSLPrecision, | 40 kDefault_GrSLPrecision, |
41 "DstTextureUpperLeft", | 41 "DstTextureUpperLeft", |
42 &dstTopLeftName); | 42 &dstTopLeftName); |
43 fDstScaleUni = args.fPB->addUniform(GrGLSLProgramBuilder::kFragment_Visi
bility, | 43 fDstScaleUni = args.fPB->addUniform(GrGLSLProgramBuilder::kFragment_Visi
bility, |
44 kVec2f_GrSLType, | 44 kVec2f_GrSLType, |
45 kDefault_GrSLPrecision, | 45 kDefault_GrSLPrecision, |
46 "DstTextureCoordScale", | 46 "DstTextureCoordScale", |
47 &dstCoordScaleName); | 47 &dstCoordScaleName); |
48 const char* fragPos = fsBuilder->fragmentPosition(); | 48 const char* fragPos = fragBuilder->fragmentPosition(); |
49 | 49 |
50 fsBuilder->codeAppend("// Read color from copy of the destination.\n"); | 50 fragBuilder->codeAppend("// Read color from copy of the destination.\n")
; |
51 fsBuilder->codeAppendf("vec2 _dstTexCoord = (%s.xy - %s) * %s;", | 51 fragBuilder->codeAppendf("vec2 _dstTexCoord = (%s.xy - %s) * %s;", |
52 fragPos, dstTopLeftName, dstCoordScaleName); | 52 fragPos, dstTopLeftName, dstCoordScaleName); |
53 | 53 |
54 if (!topDown) { | 54 if (!topDown) { |
55 fsBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;"); | 55 fragBuilder->codeAppend("_dstTexCoord.y = 1.0 - _dstTexCoord.y;"); |
56 } | 56 } |
57 | 57 |
58 fsBuilder->codeAppendf("vec4 %s = ", dstColor); | 58 fragBuilder->codeAppendf("vec4 %s = ", dstColor); |
59 fsBuilder->appendTextureLookup(args.fSamplers[0], "_dstTexCoord", kVec2f
_GrSLType); | 59 fragBuilder->appendTextureLookup(args.fSamplers[0], "_dstTexCoord", kVec
2f_GrSLType); |
60 fsBuilder->codeAppend(";"); | 60 fragBuilder->codeAppend(";"); |
61 } | 61 } |
62 | 62 |
63 this->emitBlendCodeForDstRead(args.fPB, args.fInputColor, dstColor, args.fOu
tputPrimary, | 63 this->emitBlendCodeForDstRead(args.fPB, |
| 64 fragBuilder, |
| 65 args.fInputColor, |
| 66 dstColor, |
| 67 args.fOutputPrimary, |
64 args.fXP); | 68 args.fXP); |
65 | 69 |
66 // Apply coverage. | 70 // Apply coverage. |
67 if (args.fXP.dstReadUsesMixedSamples()) { | 71 if (args.fXP.dstReadUsesMixedSamples()) { |
68 if (args.fXP.readsCoverage()) { | 72 if (args.fXP.readsCoverage()) { |
69 fsBuilder->codeAppendf("%s *= %s;", args.fOutputPrimary, args.fInput
Coverage); | 73 fragBuilder->codeAppendf("%s *= %s;", args.fOutputPrimary, args.fInp
utCoverage); |
70 fsBuilder->codeAppendf("%s = %s;", args.fOutputSecondary, args.fInpu
tCoverage); | 74 fragBuilder->codeAppendf("%s = %s;", args.fOutputSecondary, args.fIn
putCoverage); |
71 } else { | 75 } else { |
72 fsBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputSecondary); | 76 fragBuilder->codeAppendf("%s = vec4(1.0);", args.fOutputSecondary); |
73 } | 77 } |
74 } else if (args.fXP.readsCoverage()) { | 78 } else if (args.fXP.readsCoverage()) { |
75 fsBuilder->codeAppendf("%s = %s * %s + (vec4(1.0) - %s) * %s;", | 79 fragBuilder->codeAppendf("%s = %s * %s + (vec4(1.0) - %s) * %s;", |
76 args.fOutputPrimary, args.fInputCoverage, | 80 args.fOutputPrimary, args.fInputCoverage, |
77 args.fOutputPrimary, args.fInputCoverage, dstColo
r); | 81 args.fOutputPrimary, args.fInputCoverage, dstCo
lor); |
78 } | 82 } |
79 } | 83 } |
80 | 84 |
81 void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrX
ferProcessor& xp) { | 85 void GrGLSLXferProcessor::setData(const GrGLSLProgramDataManager& pdm, const GrX
ferProcessor& xp) { |
82 if (xp.getDstTexture()) { | 86 if (xp.getDstTexture()) { |
83 if (fDstTopLeftUni.isValid()) { | 87 if (fDstTopLeftUni.isValid()) { |
84 pdm.set2f(fDstTopLeftUni, static_cast<float>(xp.dstTextureOffset().f
X), | 88 pdm.set2f(fDstTopLeftUni, static_cast<float>(xp.dstTextureOffset().f
X), |
85 static_cast<float>(xp.dstTextureOffset().fY)); | 89 static_cast<float>(xp.dstTextureOffset().fY)); |
86 pdm.set2f(fDstScaleUni, 1.f / xp.getDstTexture()->width(), | 90 pdm.set2f(fDstScaleUni, 1.f / xp.getDstTexture()->width(), |
87 1.f / xp.getDstTexture()->height()); | 91 1.f / xp.getDstTexture()->height()); |
88 } else { | 92 } else { |
89 SkASSERT(!fDstScaleUni.isValid()); | 93 SkASSERT(!fDstScaleUni.isValid()); |
90 } | 94 } |
91 } else { | 95 } else { |
92 SkASSERT(!fDstTopLeftUni.isValid()); | 96 SkASSERT(!fDstTopLeftUni.isValid()); |
93 SkASSERT(!fDstScaleUni.isValid()); | 97 SkASSERT(!fDstScaleUni.isValid()); |
94 } | 98 } |
95 this->onSetData(pdm, xp); | 99 this->onSetData(pdm, xp); |
96 } | 100 } |
97 | 101 |
OLD | NEW |