Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Unified Diff: src/gpu/glsl/GrGLSLShaderBuilder.cpp

Issue 2180803005: Add color gamut xform helpers to GrGLSLShaderBuilder (Closed) Base URL: https://skia.googlesource.com/skia.git@xform-storage
Patch Set: Rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,

Powered by Google App Engine
This is Rietveld 408576698