Refactor GrColorSpaceXformHelper
Nonlinear blending mode is going to (sometimes) require additional uniforms and shader code for color space transformation. This change just alters the usage of the helper struct so that we can hide any new logic (without having to change all the FPs that use it). BUG=skia: Change-Id: I913478a387973f5bad5aa09a29f85d21daacab94 Reviewed-on: https://skia-review.googlesource.com/9414 Reviewed-by: Robert Phillips <robertphillips@google.com> Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
parent
e353e81a6f
commit
c624d9d212
@ -79,7 +79,7 @@ protected:
|
||||
private:
|
||||
GrGLSLProgramDataManager::UniformHandle fInnerThresholdVar;
|
||||
GrGLSLProgramDataManager::UniformHandle fOuterThresholdVar;
|
||||
GrGLSLProgramDataManager::UniformHandle fColorSpaceXformVar;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
typedef GrGLSLFragmentProcessor INHERITED;
|
||||
};
|
||||
|
||||
@ -94,8 +94,7 @@ void GrGLAlphaThresholdFragmentProcessor::emitCode(EmitArgs& args) {
|
||||
|
||||
const GrAlphaThresholdFragmentProcessor& atfp =
|
||||
args.fFp.cast<GrAlphaThresholdFragmentProcessor>();
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, atfp.colorSpaceXform(),
|
||||
&fColorSpaceXformVar);
|
||||
fColorSpaceHelper.emitCode(uniformHandler, atfp.colorSpaceXform());
|
||||
|
||||
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
||||
SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
|
||||
@ -105,7 +104,7 @@ void GrGLAlphaThresholdFragmentProcessor::emitCode(EmitArgs& args) {
|
||||
fragBuilder->codeAppendf("vec2 mask_coord = %s;", maskCoords2D.c_str());
|
||||
fragBuilder->codeAppend("vec4 input_color = ");
|
||||
fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coord", kVec2f_GrSLType,
|
||||
&colorSpaceHelper);
|
||||
&fColorSpaceHelper);
|
||||
fragBuilder->codeAppend(";");
|
||||
fragBuilder->codeAppend("vec4 mask_color = ");
|
||||
fragBuilder->appendTextureLookup(args.fTexSamplers[1], "mask_coord");
|
||||
@ -140,7 +139,7 @@ void GrGLAlphaThresholdFragmentProcessor::onSetData(const GrGLSLProgramDataManag
|
||||
pdman.set1f(fInnerThresholdVar, atfp.innerThreshold());
|
||||
pdman.set1f(fOuterThresholdVar, atfp.outerThreshold());
|
||||
if (SkToBool(atfp.colorSpaceXform())) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformVar, atfp.colorSpaceXform()->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, atfp.colorSpaceXform());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -467,7 +467,7 @@ private:
|
||||
typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
|
||||
|
||||
UniformHandle fScaleUni;
|
||||
UniformHandle fColorSpaceXformUni;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
GrTextureDomain::GLDomain fGLDomain;
|
||||
|
||||
typedef GrGLSLFragmentProcessor INHERITED;
|
||||
@ -571,9 +571,7 @@ void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) {
|
||||
// a number smaller than that to approximate 0, but
|
||||
// leave room for 32-bit float GPU rounding errors.
|
||||
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler,
|
||||
displacementMap.colorSpaceXform(),
|
||||
&fColorSpaceXformUni);
|
||||
fColorSpaceHelper.emitCode(args.fUniformHandler, displacementMap.colorSpaceXform());
|
||||
|
||||
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
||||
fragBuilder->codeAppendf("\t\tvec4 %s = ", dColor);
|
||||
@ -634,7 +632,7 @@ void GrGLDisplacementMapEffect::emitCode(EmitArgs& args) {
|
||||
SkString(cCoords),
|
||||
args.fTexSamplers[1],
|
||||
nullptr,
|
||||
&colorSpaceHelper);
|
||||
&fColorSpaceHelper);
|
||||
fragBuilder->codeAppend(";\n");
|
||||
}
|
||||
|
||||
@ -649,7 +647,7 @@ void GrGLDisplacementMapEffect::onSetData(const GrGLSLProgramDataManager& pdman,
|
||||
SkScalarToFloat(scaleY) : SkScalarToFloat(-scaleY));
|
||||
fGLDomain.setData(pdman, displacementMap.domain(), colorTex);
|
||||
if (SkToBool(displacementMap.colorSpaceXform())) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformUni, displacementMap.colorSpaceXform()->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, displacementMap.colorSpaceXform());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -143,7 +143,7 @@ private:
|
||||
UniformHandle fInvZoomVar;
|
||||
UniformHandle fInvInsetVar;
|
||||
UniformHandle fBoundsVar;
|
||||
UniformHandle fColorSpaceXformVar;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
|
||||
typedef GrGLSLFragmentProcessor INHERITED;
|
||||
};
|
||||
@ -164,8 +164,7 @@ void GrGLMagnifierEffect::emitCode(EmitArgs& args) {
|
||||
"Bounds");
|
||||
|
||||
const GrMagnifierEffect& zoom = args.fFp.cast<GrMagnifierEffect>();
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, zoom.colorSpaceXform(),
|
||||
&fColorSpaceXformVar);
|
||||
fColorSpaceHelper.emitCode(uniformHandler, zoom.colorSpaceXform());
|
||||
|
||||
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
||||
SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
|
||||
@ -194,7 +193,7 @@ void GrGLMagnifierEffect::emitCode(EmitArgs& args) {
|
||||
fragBuilder->codeAppend("\t\tvec2 mix_coord = mix(coord, zoom_coord, weight);\n");
|
||||
fragBuilder->codeAppend("\t\tvec4 output_color = ");
|
||||
fragBuilder->appendTextureLookup(args.fTexSamplers[0], "mix_coord", kVec2f_GrSLType,
|
||||
&colorSpaceHelper);
|
||||
&fColorSpaceHelper);
|
||||
fragBuilder->codeAppend(";\n");
|
||||
|
||||
fragBuilder->codeAppendf("\t\t%s = output_color;", args.fOutputColor);
|
||||
@ -239,7 +238,7 @@ void GrGLMagnifierEffect::onSetData(const GrGLSLProgramDataManager& pdman,
|
||||
}
|
||||
|
||||
if (SkToBool(zoom.colorSpaceXform())) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformVar, zoom.colorSpaceXform()->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, zoom.colorSpaceXform());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1345,7 +1345,7 @@ void GrGradientEffect::GLSLProcessor::onSetData(const GrGLSLProgramDataManager&
|
||||
fCachedYCoord = yCoord;
|
||||
}
|
||||
if (SkToBool(e.fColorSpaceXform)) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformUni, e.fColorSpaceXform->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, e.fColorSpaceXform.get());
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -1583,15 +1583,14 @@ void GrGradientEffect::GLSLProcessor::emitColor(GrGLSLFPFragmentBuilder* fragBui
|
||||
}
|
||||
|
||||
case kTexture_ColorType: {
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, ge.fColorSpaceXform.get(),
|
||||
&fColorSpaceXformUni);
|
||||
fColorSpaceHelper.emitCode(uniformHandler, ge.fColorSpaceXform.get());
|
||||
|
||||
const char* fsyuni = uniformHandler->getUniformCStr(fFSYUni);
|
||||
|
||||
fragBuilder->codeAppendf("vec2 coord = vec2(%s, %s);", gradientTValue, fsyuni);
|
||||
fragBuilder->codeAppendf("%s = ", outputColor);
|
||||
fragBuilder->appendTextureLookupAndModulate(inputColor, texSamplers[0], "coord",
|
||||
kVec2f_GrSLType, &colorSpaceHelper);
|
||||
kVec2f_GrSLType, &fColorSpaceHelper);
|
||||
fragBuilder->codeAppend(";");
|
||||
|
||||
break;
|
||||
|
@ -295,6 +295,7 @@ static inline int next_dither_toggle(int toggle) {
|
||||
#include "GrColorSpaceXform.h"
|
||||
#include "GrCoordTransform.h"
|
||||
#include "GrFragmentProcessor.h"
|
||||
#include "glsl/GrGLSLColorSpaceXformHelper.h"
|
||||
#include "glsl/GrGLSLFragmentProcessor.h"
|
||||
#include "glsl/GrGLSLProgramDataManager.h"
|
||||
|
||||
@ -520,7 +521,7 @@ private:
|
||||
GrGLSLProgramDataManager::UniformHandle fColorsUni;
|
||||
GrGLSLProgramDataManager::UniformHandle fHardStopT;
|
||||
GrGLSLProgramDataManager::UniformHandle fFSYUni;
|
||||
GrGLSLProgramDataManager::UniformHandle fColorSpaceXformUni;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
|
||||
typedef GrGLSLFragmentProcessor INHERITED;
|
||||
};
|
||||
|
@ -32,7 +32,7 @@ private:
|
||||
typedef GrGLSLProgramDataManager::UniformHandle UniformHandle;
|
||||
|
||||
UniformHandle fImageIncrementUni;
|
||||
UniformHandle fColorSpaceXformUni;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
GrTextureDomain::GLDomain fDomain;
|
||||
|
||||
typedef GrGLSLFragmentProcessor INHERITED;
|
||||
@ -48,8 +48,7 @@ void GrGLBicubicEffect::emitCode(EmitArgs& args) {
|
||||
|
||||
const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
|
||||
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(uniformHandler, bicubicEffect.colorSpaceXform(),
|
||||
&fColorSpaceXformUni);
|
||||
fColorSpaceHelper.emitCode(uniformHandler, bicubicEffect.colorSpaceXform());
|
||||
|
||||
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
||||
SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
|
||||
@ -107,9 +106,9 @@ void GrGLBicubicEffect::emitCode(EmitArgs& args) {
|
||||
y);
|
||||
}
|
||||
SkString bicubicColor("(wy.x * s0 + wy.y * s1 + wy.z * s2 + wy.w * s3)");
|
||||
if (colorSpaceHelper.getXformMatrix()) {
|
||||
if (fColorSpaceHelper.isValid()) {
|
||||
SkString xformedColor;
|
||||
fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &colorSpaceHelper);
|
||||
fragBuilder->appendColorGamutXform(&xformedColor, bicubicColor.c_str(), &fColorSpaceHelper);
|
||||
bicubicColor.swap(xformedColor);
|
||||
}
|
||||
fragBuilder->codeAppendf("%s = %s;",
|
||||
@ -127,7 +126,7 @@ void GrGLBicubicEffect::onSetData(const GrGLSLProgramDataManager& pdman,
|
||||
pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
|
||||
fDomain.setData(pdman, bicubicEffect.domain(), texture);
|
||||
if (SkToBool(bicubicEffect.colorSpaceXform())) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformUni, bicubicEffect.colorSpaceXform()->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, bicubicEffect.colorSpaceXform());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,9 +42,7 @@ class GrGLSimpleTextureEffect : public GrGLSLFragmentProcessor {
|
||||
public:
|
||||
void emitCode(EmitArgs& args) override {
|
||||
const GrSimpleTextureEffect& textureEffect = args.fFp.cast<GrSimpleTextureEffect>();
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler,
|
||||
textureEffect.colorSpaceXform(),
|
||||
&fColorSpaceXformUni);
|
||||
fColorSpaceHelper.emitCode(args.fUniformHandler, textureEffect.colorSpaceXform());
|
||||
|
||||
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
||||
fragBuilder->codeAppendf("%s = ", args.fOutputColor);
|
||||
@ -52,7 +50,7 @@ public:
|
||||
args.fTexSamplers[0],
|
||||
args.fTransformedCoords[0].c_str(),
|
||||
args.fTransformedCoords[0].getType(),
|
||||
&colorSpaceHelper);
|
||||
&fColorSpaceHelper);
|
||||
fragBuilder->codeAppend(";");
|
||||
}
|
||||
|
||||
@ -66,14 +64,14 @@ protected:
|
||||
void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& processor) override {
|
||||
const GrSimpleTextureEffect& textureEffect = processor.cast<GrSimpleTextureEffect>();
|
||||
if (SkToBool(textureEffect.colorSpaceXform())) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformUni, textureEffect.colorSpaceXform()->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, textureEffect.colorSpaceXform());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
typedef GrGLSLFragmentProcessor INHERITED;
|
||||
|
||||
UniformHandle fColorSpaceXformUni;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -321,9 +321,7 @@ GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const {
|
||||
GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
|
||||
SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0]);
|
||||
|
||||
GrGLSLColorSpaceXformHelper colorSpaceHelper(args.fUniformHandler,
|
||||
tde.colorSpaceXform(),
|
||||
&fColorSpaceXformUni);
|
||||
fColorSpaceHelper.emitCode(args.fUniformHandler, tde.colorSpaceXform());
|
||||
fGLDomain.sampleTexture(fragBuilder,
|
||||
args.fUniformHandler,
|
||||
args.fShaderCaps,
|
||||
@ -332,7 +330,7 @@ GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const {
|
||||
coords2D,
|
||||
args.fTexSamplers[0],
|
||||
args.fInputColor,
|
||||
&colorSpaceHelper);
|
||||
&fColorSpaceHelper);
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -341,13 +339,13 @@ GrGLSLFragmentProcessor* GrTextureDomainEffect::onCreateGLSLInstance() const {
|
||||
const GrTextureDomain& domain = tde.fTextureDomain;
|
||||
fGLDomain.setData(pdman, domain, tde.textureSampler(0).texture());
|
||||
if (SkToBool(tde.colorSpaceXform())) {
|
||||
pdman.setSkMatrix44(fColorSpaceXformUni, tde.colorSpaceXform()->srcToDst());
|
||||
fColorSpaceHelper.setData(pdman, tde.colorSpaceXform());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
GrTextureDomain::GLDomain fGLDomain;
|
||||
UniformHandle fColorSpaceXformUni;
|
||||
GrGLSLColorSpaceXformHelper fColorSpaceHelper;
|
||||
};
|
||||
|
||||
return new GLSLProcessor;
|
||||
|
@ -12,29 +12,33 @@
|
||||
#include "GrGLSLUniformHandler.h"
|
||||
|
||||
/**
|
||||
* Stack helper class to assist with using GrColorSpaceXform within an FP's emitCode function.
|
||||
* This injects the uniform declaration, and stores the information needed to generate correct
|
||||
* gamut-transformation shader code.
|
||||
* Helper class to assist with using GrColorSpaceXform within an FP. This manages all of the
|
||||
* uniforms needed, and can be passed to shader builder functions to automatically generate the
|
||||
* correct color space transformation code.
|
||||
*/
|
||||
class GrGLSLColorSpaceXformHelper : public SkNoncopyable {
|
||||
public:
|
||||
GrGLSLColorSpaceXformHelper(GrGLSLUniformHandler* uniformHandler,
|
||||
GrColorSpaceXform* colorSpaceXform,
|
||||
GrGLSLProgramDataManager::UniformHandle* handle) {
|
||||
SkASSERT(uniformHandler && handle);
|
||||
GrGLSLColorSpaceXformHelper() : fValid(false) {}
|
||||
|
||||
void emitCode(GrGLSLUniformHandler* uniformHandler, GrColorSpaceXform* colorSpaceXform) {
|
||||
SkASSERT(uniformHandler);
|
||||
if (colorSpaceXform) {
|
||||
*handle = uniformHandler->addUniform(kFragment_GrShaderFlag, kMat44f_GrSLType,
|
||||
kDefault_GrSLPrecision, "ColorXform",
|
||||
&fXformMatrix);
|
||||
} else {
|
||||
fXformMatrix = nullptr;
|
||||
fGamutXformVar = uniformHandler->addUniform(kFragment_GrShaderFlag, kMat44f_GrSLType,
|
||||
kDefault_GrSLPrecision, "ColorXform");
|
||||
fValid = true;
|
||||
}
|
||||
}
|
||||
|
||||
const char* getXformMatrix() const { return fXformMatrix; }
|
||||
void setData(const GrGLSLProgramDataManager& pdman, GrColorSpaceXform* colorSpaceXform) {
|
||||
pdman.setSkMatrix44(fGamutXformVar, colorSpaceXform->srcToDst());
|
||||
}
|
||||
|
||||
bool isValid() const { return fValid; }
|
||||
GrGLSLProgramDataManager::UniformHandle const gamutXformUniform() { return fGamutXformVar; }
|
||||
|
||||
private:
|
||||
const char* fXformMatrix;
|
||||
GrGLSLProgramDataManager::UniformHandle fGamutXformVar;
|
||||
bool fValid;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -91,7 +91,7 @@ void GrGLSLShaderBuilder::appendTextureLookup(SamplerHandle samplerHandle,
|
||||
const char* coordName,
|
||||
GrSLType varyingType,
|
||||
GrGLSLColorSpaceXformHelper* colorXformHelper) {
|
||||
if (colorXformHelper && colorXformHelper->getXformMatrix()) {
|
||||
if (colorXformHelper && colorXformHelper->isValid()) {
|
||||
// With a color gamut transform, we need to wrap the lookup in another function call
|
||||
SkString lookup;
|
||||
this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
|
||||
@ -109,7 +109,7 @@ void GrGLSLShaderBuilder::appendTextureLookupAndModulate(
|
||||
GrGLSLColorSpaceXformHelper* colorXformHelper) {
|
||||
SkString lookup;
|
||||
this->appendTextureLookup(&lookup, samplerHandle, coordName, varyingType);
|
||||
if (colorXformHelper && colorXformHelper->getXformMatrix()) {
|
||||
if (colorXformHelper && colorXformHelper->isValid()) {
|
||||
SkString xform;
|
||||
this->appendColorGamutXform(&xform, lookup.c_str(), colorXformHelper);
|
||||
this->codeAppend((GrGLSLExpr4(modulation) * GrGLSLExpr4(xform)).c_str());
|
||||
@ -141,8 +141,9 @@ void GrGLSLShaderBuilder::appendColorGamutXform(SkString* out,
|
||||
functionBody.c_str(),
|
||||
&colorGamutXformFuncName);
|
||||
|
||||
GrGLSLUniformHandler* uniformHandler = fProgramBuilder->uniformHandler();
|
||||
out->appendf("%s(%s, %s)", colorGamutXformFuncName.c_str(), srcColor,
|
||||
colorXformHelper->getXformMatrix());
|
||||
uniformHandler->getUniformCStr(colorXformHelper->gamutXformUniform()));
|
||||
}
|
||||
|
||||
void GrGLSLShaderBuilder::appendColorGamutXform(const char* srcColor,
|
||||
|
Loading…
Reference in New Issue
Block a user