Index: src/gpu/glsl/GrGLSLShaderBuilder.cpp |
diff --git a/src/gpu/glsl/GrGLSLShaderBuilder.cpp b/src/gpu/glsl/GrGLSLShaderBuilder.cpp |
index 1e52a11013942636fb75495db4b1b8ebc4ff2527..8b004b265f862763ddfa25f4da146b6288ee8b43 100644 |
--- a/src/gpu/glsl/GrGLSLShaderBuilder.cpp |
+++ b/src/gpu/glsl/GrGLSLShaderBuilder.cpp |
@@ -8,6 +8,7 @@ |
#include "GrSwizzle.h" |
#include "glsl/GrGLSLShaderBuilder.h" |
#include "glsl/GrGLSLCaps.h" |
+#include "glsl/GrGLSLColorSpaceXformHelper.h" |
#include "glsl/GrGLSLShaderVar.h" |
#include "glsl/GrGLSLSampler.h" |
#include "glsl/GrGLSLProgramBuilder.h" |
@@ -98,17 +99,79 @@ void GrGLSLShaderBuilder::appendTextureLookup(SkString* out, |
void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle, |
const char* coordName, |
- GrSLType varyingType) { |
- this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingType); |
+ GrSLType varyingType, |
+ GrGLSLColorSpaceXformHelper* colorXformHelper) { |
+ if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
+ // With a color gamut transform, we need to wrap the lookup in another function call |
+ SkString lookup; |
+ this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
+ this->appendColorGamutXform(lookup.c_str(), colorXformHelper); |
+ } else { |
+ this->appendTextureLookup(&this->code(), samplerHandle, coordName, varyingType); |
+ } |
} |
-void GrGLSLShaderBuilder::appendTextureLookupAndModulate(const char* modulation, |
- SamplerHandle samplerHandle, |
- const char* coordName, |
- GrSLType varyingType) { |
+void GrGLSLShaderBuilder::appendTextureLookupAndModulate( |
+ const char* modulation, |
+ SamplerHandle samplerHandle, |
+ const char* coordName, |
+ GrSLType varyingType, |
+ GrGLSLColorSpaceXformHelper* colorXformHelper) { |
SkString lookup; |
this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType); |
- this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
+ if (colorXformHelper && colorXformHelper->getXformMatrix()) { |
+ SkString xform; |
+ this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper); |
+ this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str()); |
+ } else { |
+ this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(lookup)).c_str()); |
+ } |
+} |
+ |
+void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out, |
+ const char* srcColor, |
+ GrGLSLColorSpaceXformHelper* colorXformHelper) { |
+ // Our color is (r, g, b, a), but we want to multiply (r, g, b, 1) by our matrix, then |
+ // re-insert the original alpha. The supplied srcColor is likely to be of the form |
+ // "texture(...)", and we don't want to evaluate that twice. |
+ // |
+ // Worse: We can't do the transformation on premultiplied colors, so if the source is premul, |
+ // we need to unpremul, transform, then multiply again. Anyways, we wrap all of the work in a |
+ // function. |
+ static const GrGLSLShaderVar gColorGamutXformArgs[] = { |
+ GrGLSLShaderVar("color", kVec4f_GrSLType), |
+ GrGLSLShaderVar("xform", kMat44f_GrSLType), |
+ }; |
+ SkString functionBody; |
+ if (kPremul_SkAlphaType == colorXformHelper->alphaType()) { |
+ // Unpremultiply |
+ functionBody.append("\tfloat nonZeroAlpha = max(color.a, 0.00001);\n" |
+ "\tcolor.rgb = color.rgb / nonZeroAlpha;\n"); |
+ } |
+ // Gamut xform, clamp to destination gamut |
+ functionBody.append("\tcolor.rgb = clamp((vec4(color.rgb, 1.0) * xform).rgb, 0.0, 1.0);\n"); |
+ if (kPremul_SkAlphaType == colorXformHelper->alphaType()) { |
+ // Re-multiply by alpha |
+ functionBody.append("\tcolor.rgb = color.rgb * nonZeroAlpha;\n"); |
+ } |
+ functionBody.append("\treturn color;"); |
+ SkString colorGamutXformFuncName; |
+ this->emitFunction(kVec4f_GrSLType, |
+ "colorGamutXform", |
+ SK_ARRAY_COUNT(gColorGamutXformArgs), |
+ gColorGamutXformArgs, |
+ functionBody.c_str(), |
+ &colorGamutXformFuncName); |
+ |
+ out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor, |
+ colorXformHelper->getXformMatrix()); |
+} |
+ |
+void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor, |
+ GrGLSLColorSpaceXformHelper* colorXformHelper) { |
+ SkString xform; |
+ this->appendColorGamutXform(&xform, srcColor, colorXformHelper); |
+ this->codeAppend(xform.c_str()); |
} |
void GrGLSLShaderBuilder::appendTexelFetch(SkString* out, |