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 "GrSwizzle.h" | 8 #include "GrSwizzle.h" |
9 #include "glsl/GrGLSLShaderBuilder.h" | 9 #include "glsl/GrGLSLShaderBuilder.h" |
10 #include "glsl/GrGLSLCaps.h" | 10 #include "glsl/GrGLSLCaps.h" |
| 11 #include "glsl/GrGLSLColorSpaceXformHelper.h" |
11 #include "glsl/GrGLSLShaderVar.h" | 12 #include "glsl/GrGLSLShaderVar.h" |
12 #include "glsl/GrGLSLSampler.h" | 13 #include "glsl/GrGLSLSampler.h" |
13 #include "glsl/GrGLSLProgramBuilder.h" | 14 #include "glsl/GrGLSLProgramBuilder.h" |
14 | 15 |
15 GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program) | 16 GrGLSLShaderBuilder::GrGLSLShaderBuilder(GrGLSLProgramBuilder* program) |
16 : fProgramBuilder(program) | 17 : fProgramBuilder(program) |
17 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) | 18 , fInputs(GrGLSLProgramBuilder::kVarsPerBlock) |
18 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) | 19 , fOutputs(GrGLSLProgramBuilder::kVarsPerBlock) |
19 , fFeaturesAddedMask(0) | 20 , fFeaturesAddedMask(0) |
20 , fCodeIndex(kCode) | 21 , fCodeIndex(kCode) |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 GrGLSLTexture2DFunctionName(varyingType, samplerType, glslC
aps->generation()), | 92 GrGLSLTexture2DFunctionName(varyingType, samplerType, glslC
aps->generation()), |
92 sampler.getSamplerNameForTexture2D(), | 93 sampler.getSamplerNameForTexture2D(), |
93 coordName); | 94 coordName); |
94 } | 95 } |
95 | 96 |
96 this->appendTextureSwizzle(out, sampler.config()); | 97 this->appendTextureSwizzle(out, sampler.config()); |
97 } | 98 } |
98 | 99 |
99 void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, | 100 void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, |
100 const char* coordName, | 101 const char* coordName, |
101 GrSLType varyingType) { | 102 GrSLType varyingType, |
102 this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingTy
pe); | 103 GrGLSLColorSpaceXformHelper* color
XformHelper) { |
| 104 if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
| 105 // With a color gamut transform, we need to wrap the lookup in another f
unction call |
| 106 SkString lookup; |
| 107 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType
); |
| 108 this->appendColorGamutXform(lookup.c_str(), colorXformHelper); |
| 109 } else { |
| 110 this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyi
ngType); |
| 111 } |
103 } | 112 } |
104 | 113 |
105 void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, | 114 void GrGLSLShaderBuilder::appendTextureLookupAndModulate( |
106 SamplerHandle samplerHa
ndle, | 115 const char* modulation, |
107 const char* coordName, | 116 SamplerHandle samplerHandle, |
108 GrSLType varyingType) { | 117 const char* coordName, |
| 118 GrSLType varyingType, |
| 119 GrGLSLColorSpaceXformHelper*
colorXformHelper) { |
109 SkString lookup; | 120 SkString lookup; |
110 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); | 121 this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
111 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); | 122 if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
| 123 SkString xform; |
| 124 this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper); |
| 125 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str())
; |
| 126 } else { |
| 127 this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()
); |
| 128 } |
| 129 } |
| 130 |
| 131 void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out, |
| 132 const char* srcColor, |
| 133 GrGLSLColorSpaceXformHelper* col
orXformHelper) { |
| 134 // Our color is (r, g, b, a), but we want to multiply (r, g, b, 1) by our ma
trix, then |
| 135 // re-insert the original alpha. The supplied srcColor is likely to be of th
e form |
| 136 // "texture(...)", and we don't want to evaluate that twice. |
| 137 // |
| 138 // Worse: We can't do the transformation on premultiplied colors, so if the
source is premul, |
| 139 // we need to unpremul, transform, then multiply again. Anyways, we wrap all
of the work in a |
| 140 // function. |
| 141 static const GrGLSLShaderVar gColorGamutXformArgs[] = { |
| 142 GrGLSLShaderVar("color", kVec4f_GrSLType), |
| 143 GrGLSLShaderVar("xform", kMat44f_GrSLType), |
| 144 }; |
| 145 SkString functionBody; |
| 146 if (kPremul_SkAlphaType == colorXformHelper->alphaType()) { |
| 147 // Unpremultiply |
| 148 functionBody.append("\tfloat nonZeroAlpha = max(color.a, 0.00001);\n" |
| 149 "\tcolor.rgb = color.rgb / nonZeroAlpha;\n"); |
| 150 } |
| 151 // Gamut xform, clamp to destination gamut |
| 152 functionBody.append("\tcolor.rgb = clamp((vec4(color.rgb, 1.0) * xform).rgb,
0.0, 1.0);\n"); |
| 153 if (kPremul_SkAlphaType == colorXformHelper->alphaType()) { |
| 154 // Re-multiply by alpha |
| 155 functionBody.append("\tcolor.rgb = color.rgb * nonZeroAlpha;\n"); |
| 156 } |
| 157 functionBody.append("\treturn color;"); |
| 158 SkString colorGamutXformFuncName; |
| 159 this->emitFunction(kVec4f_GrSLType, |
| 160 "colorGamutXform", |
| 161 SK_ARRAY_COUNT(gColorGamutXformArgs), |
| 162 gColorGamutXformArgs, |
| 163 functionBody.c_str(), |
| 164 &colorGamutXformFuncName); |
| 165 |
| 166 out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor, |
| 167 colorXformHelper->getXformMatrix()); |
| 168 } |
| 169 |
| 170 void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor, |
| 171 GrGLSLColorSpaceXformHelper* col
orXformHelper) { |
| 172 SkString xform; |
| 173 this->appendColorGamutXform(&xform, srcColor, colorXformHelper); |
| 174 this->codeAppend(xform.c_str()); |
112 } | 175 } |
113 | 176 |
114 void GrGLSLShaderBuilder::appendTexelFetch(SkString* out, | 177 void GrGLSLShaderBuilder::appendTexelFetch(SkString* out, |
115 SamplerHandle samplerHandle, | 178 SamplerHandle samplerHandle, |
116 const char* coordExpr) const { | 179 const char* coordExpr) const { |
117 const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle); | 180 const GrGLSLSampler& sampler = fProgramBuilder->getSampler(samplerHandle); |
118 SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport()); | 181 SkASSERT(fProgramBuilder->glslCaps()->texelFetchSupport()); |
119 SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.type())); | 182 SkASSERT(GrSLTypeIsCombinedSamplerType(sampler.type())); |
120 | 183 |
121 out->appendf("texelFetch(%s, %s)", sampler.getSamplerNameForTexelFetch(), co
ordExpr); | 184 out->appendf("texelFetch(%s, %s)", sampler.getSamplerNameForTexelFetch(), co
ordExpr); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // append the 'footer' to code | 253 // append the 'footer' to code |
191 this->code().append("}"); | 254 this->code().append("}"); |
192 | 255 |
193 for (int i = 0; i <= fCodeIndex; i++) { | 256 for (int i = 0; i <= fCodeIndex; i++) { |
194 fCompilerStrings[i] = fShaderStrings[i].c_str(); | 257 fCompilerStrings[i] = fShaderStrings[i].c_str(); |
195 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); | 258 fCompilerStringLengths[i] = (int)fShaderStrings[i].size(); |
196 } | 259 } |
197 | 260 |
198 fFinalized = true; | 261 fFinalized = true; |
199 } | 262 } |
OLD | NEW |