| Index: src/effects/SkArithmeticMode_gpu.cpp
|
| diff --git a/src/effects/SkArithmeticMode_gpu.cpp b/src/effects/SkArithmeticMode_gpu.cpp
|
| index 6ccd63ba747e17c52177a4d9de5de10f126d953c..3209fb9214d4520394ed661209648b3bb65a75f7 100644
|
| --- a/src/effects/SkArithmeticMode_gpu.cpp
|
| +++ b/src/effects/SkArithmeticMode_gpu.cpp
|
| @@ -20,21 +20,77 @@
|
|
|
| static const bool gUseUnpremul = false;
|
|
|
| +static void add_arithmetic_code(GrGLFPFragmentBuilder* fsBuilder,
|
| + const char* inputColor,
|
| + const char* dstColor,
|
| + const char* outputColor,
|
| + const char* kUni,
|
| + bool enforcePMColor) {
|
| + // We don't try to optimize for this case at all
|
| + if (NULL == inputColor) {
|
| + fsBuilder->codeAppend("const vec4 src = vec4(1);");
|
| + } else {
|
| + fsBuilder->codeAppendf("vec4 src = %s;", inputColor);
|
| + if (gUseUnpremul) {
|
| + fsBuilder->codeAppend("src.rgb = clamp(src.rgb / src.a, 0.0, 1.0);");
|
| + }
|
| + }
|
| +
|
| + fsBuilder->codeAppendf("vec4 dst = %s;", dstColor);
|
| + if (gUseUnpremul) {
|
| + fsBuilder->codeAppend("dst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);");
|
| + }
|
| +
|
| + fsBuilder->codeAppendf("%s = %s.x * src * dst + %s.y * src + %s.z * dst + %s.w;",
|
| + outputColor, kUni, kUni, kUni, kUni);
|
| + fsBuilder->codeAppendf("%s = clamp(%s, 0.0, 1.0);\n", outputColor, outputColor);
|
| + if (gUseUnpremul) {
|
| + fsBuilder->codeAppendf("%s.rgb *= %s.a;", outputColor, outputColor);
|
| + } else if (enforcePMColor) {
|
| + fsBuilder->codeAppendf("%s.rgb = min(%s.rgb, %s.a);",
|
| + outputColor, outputColor, outputColor);
|
| + }
|
| +}
|
| +
|
| class GLArithmeticFP : public GrGLFragmentProcessor {
|
| public:
|
| - GLArithmeticFP(const GrProcessor&);
|
| - virtual ~GLArithmeticFP();
|
| + GLArithmeticFP(const GrProcessor&)
|
| + : fEnforcePMColor(true) {
|
| + }
|
|
|
| - virtual void emitCode(GrGLFPBuilder*,
|
| - const GrFragmentProcessor&,
|
| - const char* outputColor,
|
| - const char* inputColor,
|
| - const TransformedCoordsArray&,
|
| - const TextureSamplerArray&) SK_OVERRIDE;
|
| + ~GLArithmeticFP() SK_OVERRIDE {}
|
| +
|
| + void emitCode(GrGLFPBuilder* builder,
|
| + const GrFragmentProcessor& fp,
|
| + const char* outputColor,
|
| + const char* inputColor,
|
| + const TransformedCoordsArray& coords,
|
| + const TextureSamplerArray& samplers) SK_OVERRIDE {
|
| + GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
|
| + fsBuilder->codeAppend("vec4 bgColor = ");
|
| + fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
|
| + fsBuilder->codeAppendf(";");
|
| + const char* dstColor = "bgColor";
|
| +
|
| + fKUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
|
| + kVec4f_GrSLType, kDefault_GrSLPrecision,
|
| + "k");
|
| + const char* kUni = builder->getUniformCStr(fKUni);
|
| +
|
| + add_arithmetic_code(fsBuilder, inputColor, dstColor, outputColor, kUni, fEnforcePMColor);
|
| + }
|
|
|
| - virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
|
| + void setData(const GrGLProgramDataManager& pdman, const GrProcessor& proc) SK_OVERRIDE {
|
| + const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>();
|
| + pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4());
|
| + fEnforcePMColor = arith.enforcePMColor();
|
| + }
|
|
|
| - static void GenKey(const GrProcessor&, const GrGLCaps& caps, GrProcessorKeyBuilder* b);
|
| + static void GenKey(const GrProcessor& proc, const GrGLCaps& caps, GrProcessorKeyBuilder* b) {
|
| + const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>();
|
| + uint32_t key = arith.enforcePMColor() ? 1 : 0;
|
| + b->add32(key);
|
| + }
|
|
|
| private:
|
| GrGLProgramDataManager::UniformHandle fKUni;
|
| @@ -49,15 +105,14 @@ GrArithmeticFP::GrArithmeticFP(float k1, float k2, float k3, float k4,
|
| bool enforcePMColor, GrTexture* background)
|
| : fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) {
|
| this->initClassID<GrArithmeticFP>();
|
| - if (background) {
|
| - fBackgroundTransform.reset(kLocal_GrCoordSet, background,
|
| - GrTextureParams::kNone_FilterMode);
|
| - this->addCoordTransform(&fBackgroundTransform);
|
| - fBackgroundAccess.reset(background);
|
| - this->addTextureAccess(&fBackgroundAccess);
|
| - } else {
|
| - this->setWillReadDstColor();
|
| - }
|
| +
|
| + SkASSERT(background);
|
| +
|
| + fBackgroundTransform.reset(kLocal_GrCoordSet, background,
|
| + GrTextureParams::kNone_FilterMode);
|
| + this->addCoordTransform(&fBackgroundTransform);
|
| + fBackgroundAccess.reset(background);
|
| + this->addTextureAccess(&fBackgroundAccess);
|
| }
|
|
|
| void GrArithmeticFP::getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const {
|
| @@ -84,91 +139,132 @@ void GrArithmeticFP::onComputeInvariantOutput(GrInvariantOutput* inout) const {
|
|
|
| ///////////////////////////////////////////////////////////////////////////////
|
|
|
| -GLArithmeticFP::GLArithmeticFP(const GrProcessor&)
|
| - : fEnforcePMColor(true) {
|
| -}
|
| +GrFragmentProcessor* GrArithmeticFP::TestCreate(SkRandom* rand,
|
| + GrContext*,
|
| + const GrDrawTargetCaps&,
|
| + GrTexture* textures[]) {
|
| + float k1 = rand->nextF();
|
| + float k2 = rand->nextF();
|
| + float k3 = rand->nextF();
|
| + float k4 = rand->nextF();
|
| + bool enforcePMColor = rand->nextBool();
|
|
|
| -GLArithmeticFP::~GLArithmeticFP() {
|
| + return SkNEW_ARGS(GrArithmeticFP, (k1, k2, k3, k4, enforcePMColor, textures[0]));
|
| }
|
|
|
| -void GLArithmeticFP::emitCode(GrGLFPBuilder* builder,
|
| - const GrFragmentProcessor& fp,
|
| - const char* outputColor,
|
| - const char* inputColor,
|
| - const TransformedCoordsArray& coords,
|
| - const TextureSamplerArray& samplers) {
|
| -
|
| - GrTexture* backgroundTex = fp.cast<GrArithmeticFP>().backgroundTexture();
|
| - GrGLFPFragmentBuilder* fsBuilder = builder->getFragmentShaderBuilder();
|
| - const char* dstColor;
|
| - if (backgroundTex) {
|
| - fsBuilder->codeAppend("\t\tvec4 bgColor = ");
|
| - fsBuilder->appendTextureLookup(samplers[0], coords[0].c_str(), coords[0].getType());
|
| - fsBuilder->codeAppendf(";\n");
|
| - dstColor = "bgColor";
|
| - } else {
|
| - dstColor = fsBuilder->dstColor();
|
| - }
|
| +GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticFP);
|
|
|
| - SkASSERT(dstColor);
|
| - fKUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
|
| - kVec4f_GrSLType, kDefault_GrSLPrecision,
|
| - "k");
|
| - const char* kUni = builder->getUniformCStr(fKUni);
|
| +///////////////////////////////////////////////////////////////////////////////
|
| +// Xfer Processor
|
| +///////////////////////////////////////////////////////////////////////////////
|
|
|
| - // We don't try to optimize for this case at all
|
| - if (NULL == inputColor) {
|
| - fsBuilder->codeAppendf("\t\tconst vec4 src = vec4(1);\n");
|
| - } else {
|
| - fsBuilder->codeAppendf("\t\tvec4 src = %s;\n", inputColor);
|
| - if (gUseUnpremul) {
|
| - fsBuilder->codeAppendf("\t\tsrc.rgb = clamp(src.rgb / src.a, 0.0, 1.0);\n");
|
| - }
|
| +class GLArithmeticXP : public GrGLXferProcessor {
|
| +public:
|
| + GLArithmeticXP(const GrProcessor&)
|
| + : fEnforcePMColor(true) {
|
| }
|
|
|
| - fsBuilder->codeAppendf("\t\tvec4 dst = %s;\n", dstColor);
|
| - if (gUseUnpremul) {
|
| - fsBuilder->codeAppendf("\t\tdst.rgb = clamp(dst.rgb / dst.a, 0.0, 1.0);\n");
|
| + ~GLArithmeticXP() SK_OVERRIDE {}
|
| +
|
| + void emitCode(const EmitArgs& args) SK_OVERRIDE {
|
| + GrGLFPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
|
| +
|
| + const char* dstColor = fsBuilder->dstColor();
|
| +
|
| + fKUni = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
|
| + kVec4f_GrSLType, kDefault_GrSLPrecision,
|
| + "k");
|
| + const char* kUni = args.fPB->getUniformCStr(fKUni);
|
| +
|
| + add_arithmetic_code(fsBuilder, args.fInputColor, dstColor, args.fOutputPrimary, kUni,
|
| + fEnforcePMColor);
|
| +
|
| + fsBuilder->codeAppendf("%s = %s * %s + (vec4(1.0) - %s) * %s;",
|
| + args.fOutputPrimary, args.fOutputPrimary, args.fInputCoverage,
|
| + args.fInputCoverage, dstColor);
|
| }
|
|
|
| - fsBuilder->codeAppendf("\t\t%s = %s.x * src * dst + %s.y * src + %s.z * dst + %s.w;\n", outputColor, kUni, kUni, kUni, kUni);
|
| - fsBuilder->codeAppendf("\t\t%s = clamp(%s, 0.0, 1.0);\n", outputColor, outputColor);
|
| - if (gUseUnpremul) {
|
| - fsBuilder->codeAppendf("\t\t%s.rgb *= %s.a;\n", outputColor, outputColor);
|
| - } else if (fEnforcePMColor) {
|
| - fsBuilder->codeAppendf("\t\t%s.rgb = min(%s.rgb, %s.a);\n", outputColor, outputColor, outputColor);
|
| + void setData(const GrGLProgramDataManager& pdman,
|
| + const GrXferProcessor& processor) SK_OVERRIDE {
|
| + const GrArithmeticXP& arith = processor.cast<GrArithmeticXP>();
|
| + pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4());
|
| + fEnforcePMColor = arith.enforcePMColor();
|
| + };
|
| +
|
| + static void GenKey(const GrProcessor& processor, const GrGLCaps& caps,
|
| + GrProcessorKeyBuilder* b) {
|
| + const GrArithmeticXP& arith = processor.cast<GrArithmeticXP>();
|
| + uint32_t key = arith.enforcePMColor() ? 1 : 0;
|
| + b->add32(key);
|
| }
|
| +
|
| +private:
|
| + GrGLProgramDataManager::UniformHandle fKUni;
|
| + bool fEnforcePMColor;
|
| +
|
| + typedef GrGLXferProcessor INHERITED;
|
| +};
|
| +
|
| +///////////////////////////////////////////////////////////////////////////////
|
| +
|
| +GrArithmeticXP::GrArithmeticXP(float k1, float k2, float k3, float k4, bool enforcePMColor)
|
| + : fK1(k1)
|
| + , fK2(k2)
|
| + , fK3(k3)
|
| + , fK4(k4)
|
| + , fEnforcePMColor(enforcePMColor) {
|
| + this->initClassID<GrPorterDuffXferProcessor>();
|
| + this->setWillReadDstColor();
|
| }
|
|
|
| -void GLArithmeticFP::setData(const GrGLProgramDataManager& pdman, const GrProcessor& processor) {
|
| - const GrArithmeticFP& arith = processor.cast<GrArithmeticFP>();
|
| - pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4());
|
| - fEnforcePMColor = arith.enforcePMColor();
|
| +void GrArithmeticXP::getGLProcessorKey(const GrGLCaps& caps, GrProcessorKeyBuilder* b) const {
|
| + GLArithmeticXP::GenKey(*this, caps, b);
|
| }
|
|
|
| -void GLArithmeticFP::GenKey(const GrProcessor& processor, const GrGLCaps&,
|
| - GrProcessorKeyBuilder* b) {
|
| - const GrArithmeticFP& arith = processor.cast<GrArithmeticFP>();
|
| - uint32_t key = arith.enforcePMColor() ? 1 : 0;
|
| - if (arith.backgroundTexture()) {
|
| - key |= 2;
|
| - }
|
| - b->add32(key);
|
| +GrGLXferProcessor* GrArithmeticXP::createGLInstance() const {
|
| + return SkNEW_ARGS(GLArithmeticXP, (*this));
|
| }
|
|
|
| -GrFragmentProcessor* GrArithmeticFP::TestCreate(SkRandom* rand,
|
| - GrContext*,
|
| - const GrDrawTargetCaps&,
|
| - GrTexture*[]) {
|
| - float k1 = rand->nextF();
|
| - float k2 = rand->nextF();
|
| - float k3 = rand->nextF();
|
| - float k4 = rand->nextF();
|
| - bool enforcePMColor = rand->nextBool();
|
| +GrXferProcessor::OptFlags GrArithmeticXP::getOptimizations(const GrProcOptInfo& colorPOI,
|
| + const GrProcOptInfo& coveragePOI,
|
| + bool doesStencilWrite,
|
| + GrColor* overrideColor,
|
| + const GrDrawTargetCaps& caps) {
|
| + return GrXferProcessor::kNone_Opt;
|
| +}
|
|
|
| - return SkNEW_ARGS(GrArithmeticFP, (k1, k2, k3, k4, enforcePMColor, NULL));
|
| +///////////////////////////////////////////////////////////////////////////////
|
| +
|
| +GrArithmeticXPFactory::GrArithmeticXPFactory(float k1, float k2, float k3, float k4,
|
| + bool enforcePMColor)
|
| + : fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) {
|
| + this->initClassID<GrArithmeticXPFactory>();
|
| }
|
|
|
| -GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticFP);
|
| +void GrArithmeticXPFactory::getInvariantOutput(const GrProcOptInfo& colorPOI,
|
| + const GrProcOptInfo& coveragePOI,
|
| + GrXPFactory::InvariantOutput* output) const {
|
| + output->fWillBlendWithDst = true;
|
| +
|
| + // TODO: We could try to optimize this more. For example if we have solid coverage and fK1 and
|
| + // fK3 are zero, then we won't be blending the color with dst at all so we can know what the
|
| + // output color is (up to the valid color components passed in).
|
| + output->fBlendedColorFlags = 0;
|
| +}
|
| +
|
| +GR_DEFINE_XP_FACTORY_TEST(GrArithmeticXPFactory);
|
| +
|
| +GrXPFactory* GrArithmeticXPFactory::TestCreate(SkRandom* random,
|
| + GrContext*,
|
| + const GrDrawTargetCaps&,
|
| + GrTexture*[]) {
|
| + float k1 = random->nextF();
|
| + float k2 = random->nextF();
|
| + float k3 = random->nextF();
|
| + float k4 = random->nextF();
|
| + bool enforcePMColor = random->nextBool();
|
| +
|
| + return GrArithmeticXPFactory::Create(k1, k2, k3, k4, enforcePMColor);
|
| +}
|
|
|
| #endif
|
|
|