2015-01-14 18:49:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SkArithmeticMode_gpu.h"
|
|
|
|
|
|
|
|
#if SK_SUPPORT_GPU
|
|
|
|
#include "GrContext.h"
|
|
|
|
#include "GrFragmentProcessor.h"
|
|
|
|
#include "GrInvariantOutput.h"
|
|
|
|
#include "GrProcessor.h"
|
|
|
|
#include "GrTexture.h"
|
|
|
|
#include "gl/GrGLCaps.h"
|
2015-07-22 17:21:17 +00:00
|
|
|
#include "gl/GrGLFragmentProcessor.h"
|
2015-01-14 18:49:18 +00:00
|
|
|
#include "gl/GrGLProgramDataManager.h"
|
|
|
|
#include "gl/builders/GrGLProgramBuilder.h"
|
|
|
|
|
|
|
|
static const bool gUseUnpremul = false;
|
|
|
|
|
2015-04-29 18:54:42 +00:00
|
|
|
static void add_arithmetic_code(GrGLFragmentBuilder* fsBuilder,
|
2015-01-14 20:53:01 +00:00
|
|
|
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
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == inputColor) {
|
2015-01-14 20:53:01 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 18:49:18 +00:00
|
|
|
class GLArithmeticFP : public GrGLFragmentProcessor {
|
|
|
|
public:
|
2015-01-14 20:53:01 +00:00
|
|
|
GLArithmeticFP(const GrProcessor&)
|
|
|
|
: fEnforcePMColor(true) {
|
|
|
|
}
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
~GLArithmeticFP() override {}
|
2015-01-14 20:53:01 +00:00
|
|
|
|
2015-07-22 22:08:53 +00:00
|
|
|
void emitCode(EmitArgs& args) override {
|
|
|
|
GrGLFragmentBuilder* fsBuilder = args.fBuilder->getFragmentShaderBuilder();
|
2015-01-14 20:53:01 +00:00
|
|
|
fsBuilder->codeAppend("vec4 bgColor = ");
|
2015-07-22 22:08:53 +00:00
|
|
|
fsBuilder->appendTextureLookup(args.fSamplers[0], args.fCoords[0].c_str(),
|
|
|
|
args.fCoords[0].getType());
|
2015-01-14 20:53:01 +00:00
|
|
|
fsBuilder->codeAppendf(";");
|
|
|
|
const char* dstColor = "bgColor";
|
|
|
|
|
2015-07-22 22:08:53 +00:00
|
|
|
fKUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
|
2015-01-14 20:53:01 +00:00
|
|
|
kVec4f_GrSLType, kDefault_GrSLPrecision,
|
|
|
|
"k");
|
2015-07-22 22:08:53 +00:00
|
|
|
const char* kUni = args.fBuilder->getUniformCStr(fKUni);
|
2015-01-14 20:53:01 +00:00
|
|
|
|
2015-07-22 22:08:53 +00:00
|
|
|
add_arithmetic_code(fsBuilder, args.fInputColor, dstColor, args.fOutputColor, kUni,
|
|
|
|
fEnforcePMColor);
|
2015-01-14 20:53:01 +00:00
|
|
|
}
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-04-28 15:48:20 +00:00
|
|
|
static void GenKey(const GrProcessor& proc, const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) {
|
2015-01-14 20:53:01 +00:00
|
|
|
const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>();
|
|
|
|
uint32_t key = arith.enforcePMColor() ? 1 : 0;
|
|
|
|
b->add32(key);
|
|
|
|
}
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-08-18 18:29:31 +00:00
|
|
|
protected:
|
|
|
|
void onSetData(const GrGLProgramDataManager& pdman, const GrProcessor& proc) override {
|
|
|
|
const GrArithmeticFP& arith = proc.cast<GrArithmeticFP>();
|
|
|
|
pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4());
|
|
|
|
fEnforcePMColor = arith.enforcePMColor();
|
|
|
|
}
|
|
|
|
|
2015-01-14 18:49:18 +00:00
|
|
|
private:
|
|
|
|
GrGLProgramDataManager::UniformHandle fKUni;
|
|
|
|
bool fEnforcePMColor;
|
|
|
|
|
|
|
|
typedef GrGLFragmentProcessor INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-07-09 13:28:14 +00:00
|
|
|
GrArithmeticFP::GrArithmeticFP(GrProcessorDataManager*, float k1, float k2, float k3, float k4,
|
2015-01-14 18:49:18 +00:00
|
|
|
bool enforcePMColor, GrTexture* background)
|
|
|
|
: fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) {
|
|
|
|
this->initClassID<GrArithmeticFP>();
|
2015-01-14 20:53:01 +00:00
|
|
|
|
|
|
|
SkASSERT(background);
|
|
|
|
|
|
|
|
fBackgroundTransform.reset(kLocal_GrCoordSet, background,
|
|
|
|
GrTextureParams::kNone_FilterMode);
|
|
|
|
this->addCoordTransform(&fBackgroundTransform);
|
|
|
|
fBackgroundAccess.reset(background);
|
|
|
|
this->addTextureAccess(&fBackgroundAccess);
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-04 14:59:37 +00:00
|
|
|
void GrArithmeticFP::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
|
2015-01-14 18:49:18 +00:00
|
|
|
GLArithmeticFP::GenKey(*this, caps, b);
|
|
|
|
}
|
|
|
|
|
2015-08-18 18:29:31 +00:00
|
|
|
GrGLFragmentProcessor* GrArithmeticFP::onCreateGLInstance() const {
|
2015-08-26 20:07:48 +00:00
|
|
|
return new GLArithmeticFP(*this);
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GrArithmeticFP::onIsEqual(const GrFragmentProcessor& fpBase) const {
|
|
|
|
const GrArithmeticFP& fp = fpBase.cast<GrArithmeticFP>();
|
|
|
|
return fK1 == fp.fK1 &&
|
|
|
|
fK2 == fp.fK2 &&
|
|
|
|
fK3 == fp.fK3 &&
|
|
|
|
fK4 == fp.fK4 &&
|
|
|
|
fEnforcePMColor == fp.fEnforcePMColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GrArithmeticFP::onComputeInvariantOutput(GrInvariantOutput* inout) const {
|
|
|
|
// TODO: optimize this
|
|
|
|
inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-29 00:16:50 +00:00
|
|
|
GrFragmentProcessor* GrArithmeticFP::TestCreate(GrProcessorTestData* d) {
|
2015-07-08 21:26:19 +00:00
|
|
|
float k1 = d->fRandom->nextF();
|
|
|
|
float k2 = d->fRandom->nextF();
|
|
|
|
float k3 = d->fRandom->nextF();
|
|
|
|
float k4 = d->fRandom->nextF();
|
|
|
|
bool enforcePMColor = d->fRandom->nextBool();
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
return new GrArithmeticFP(d->fProcDataManager, k1, k2, k3, k4, enforcePMColor, d->fTextures[0]);
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:53:01 +00:00
|
|
|
GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticFP);
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-01-14 20:53:01 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Xfer Processor
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-02-09 15:51:00 +00:00
|
|
|
class ArithmeticXP : public GrXferProcessor {
|
|
|
|
public:
|
2015-06-08 22:11:04 +00:00
|
|
|
ArithmeticXP(const DstTexture*, bool hasMixedSamples,
|
|
|
|
float k1, float k2, float k3, float k4, bool enforcePMColor);
|
2015-02-09 15:51:00 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
const char* name() const override { return "Arithmetic"; }
|
2015-02-09 15:51:00 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
GrGLXferProcessor* createGLInstance() const override;
|
2015-02-09 15:51:00 +00:00
|
|
|
|
|
|
|
float k1() const { return fK1; }
|
|
|
|
float k2() const { return fK2; }
|
|
|
|
float k3() const { return fK3; }
|
|
|
|
float k4() const { return fK4; }
|
|
|
|
bool enforcePMColor() const { return fEnforcePMColor; }
|
|
|
|
|
|
|
|
private:
|
2015-05-10 15:45:18 +00:00
|
|
|
GrXferProcessor::OptFlags onGetOptimizations(const GrProcOptInfo& colorPOI,
|
|
|
|
const GrProcOptInfo& coveragePOI,
|
|
|
|
bool doesStencilWrite,
|
|
|
|
GrColor* overrideColor,
|
2015-05-19 16:29:46 +00:00
|
|
|
const GrCaps& caps) override;
|
2015-05-10 15:45:18 +00:00
|
|
|
|
2015-04-28 15:48:20 +00:00
|
|
|
void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
|
2015-02-09 15:51:00 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool onIsEqual(const GrXferProcessor& xpBase) const override {
|
2015-02-09 15:51:00 +00:00
|
|
|
const ArithmeticXP& xp = xpBase.cast<ArithmeticXP>();
|
|
|
|
if (fK1 != xp.fK1 ||
|
|
|
|
fK2 != xp.fK2 ||
|
|
|
|
fK3 != xp.fK3 ||
|
|
|
|
fK4 != xp.fK4 ||
|
|
|
|
fEnforcePMColor != xp.fEnforcePMColor) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
float fK1, fK2, fK3, fK4;
|
|
|
|
bool fEnforcePMColor;
|
|
|
|
|
|
|
|
typedef GrXferProcessor INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-01-14 20:53:01 +00:00
|
|
|
class GLArithmeticXP : public GrGLXferProcessor {
|
|
|
|
public:
|
|
|
|
GLArithmeticXP(const GrProcessor&)
|
|
|
|
: fEnforcePMColor(true) {
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
~GLArithmeticXP() override {}
|
2015-01-14 20:53:01 +00:00
|
|
|
|
2015-04-28 15:48:20 +00:00
|
|
|
static void GenKey(const GrProcessor& processor, const GrGLSLCaps& caps,
|
2015-02-06 15:02:37 +00:00
|
|
|
GrProcessorKeyBuilder* b) {
|
2015-02-09 15:51:00 +00:00
|
|
|
const ArithmeticXP& arith = processor.cast<ArithmeticXP>();
|
2015-02-06 15:02:37 +00:00
|
|
|
uint32_t key = arith.enforcePMColor() ? 1 : 0;
|
|
|
|
b->add32(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-06-08 19:14:44 +00:00
|
|
|
void emitBlendCodeForDstRead(GrGLXPBuilder* pb, const char* srcColor, const char* dstColor,
|
|
|
|
const char* outColor, const GrXferProcessor& proc) override {
|
|
|
|
GrGLXPFragmentBuilder* fsBuilder = pb->getFragmentShaderBuilder();
|
2015-01-14 20:53:01 +00:00
|
|
|
|
2015-06-08 19:14:44 +00:00
|
|
|
fKUni = pb->addUniform(GrGLProgramBuilder::kFragment_Visibility,
|
|
|
|
kVec4f_GrSLType, kDefault_GrSLPrecision,
|
|
|
|
"k");
|
|
|
|
const char* kUni = pb->getUniformCStr(fKUni);
|
2015-01-14 20:53:01 +00:00
|
|
|
|
2015-06-08 19:14:44 +00:00
|
|
|
add_arithmetic_code(fsBuilder, srcColor, dstColor, outColor, kUni, fEnforcePMColor);
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 15:02:37 +00:00
|
|
|
void onSetData(const GrGLProgramDataManager& pdman,
|
2015-03-26 01:17:31 +00:00
|
|
|
const GrXferProcessor& processor) override {
|
2015-02-09 15:51:00 +00:00
|
|
|
const ArithmeticXP& arith = processor.cast<ArithmeticXP>();
|
2015-01-14 20:53:01 +00:00
|
|
|
pdman.set4f(fKUni, arith.k1(), arith.k2(), arith.k3(), arith.k4());
|
|
|
|
fEnforcePMColor = arith.enforcePMColor();
|
|
|
|
};
|
|
|
|
|
|
|
|
GrGLProgramDataManager::UniformHandle fKUni;
|
|
|
|
bool fEnforcePMColor;
|
|
|
|
|
|
|
|
typedef GrGLXferProcessor INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-06-08 22:11:04 +00:00
|
|
|
ArithmeticXP::ArithmeticXP(const DstTexture* dstTexture, bool hasMixedSamples,
|
|
|
|
float k1, float k2, float k3, float k4, bool enforcePMColor)
|
|
|
|
: INHERITED(dstTexture, true, hasMixedSamples)
|
2015-02-06 15:02:37 +00:00
|
|
|
, fK1(k1)
|
2015-01-14 20:53:01 +00:00
|
|
|
, fK2(k2)
|
|
|
|
, fK3(k3)
|
|
|
|
, fK4(k4)
|
|
|
|
, fEnforcePMColor(enforcePMColor) {
|
2015-02-09 15:51:00 +00:00
|
|
|
this->initClassID<ArithmeticXP>();
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 15:48:20 +00:00
|
|
|
void ArithmeticXP::onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
|
2015-01-14 20:53:01 +00:00
|
|
|
GLArithmeticXP::GenKey(*this, caps, b);
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
GrGLXferProcessor* ArithmeticXP::createGLInstance() const { return new GLArithmeticXP(*this); }
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-05-10 15:45:18 +00:00
|
|
|
GrXferProcessor::OptFlags ArithmeticXP::onGetOptimizations(const GrProcOptInfo& colorPOI,
|
|
|
|
const GrProcOptInfo& coveragePOI,
|
|
|
|
bool doesStencilWrite,
|
|
|
|
GrColor* overrideColor,
|
2015-05-19 16:29:46 +00:00
|
|
|
const GrCaps& caps) {
|
2015-07-08 18:26:37 +00:00
|
|
|
return GrXferProcessor::kNone_OptFlags;
|
2015-01-14 20:53:01 +00:00
|
|
|
}
|
2015-01-14 18:49:18 +00:00
|
|
|
|
2015-01-14 20:53:01 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
GrArithmeticXPFactory::GrArithmeticXPFactory(float k1, float k2, float k3, float k4,
|
|
|
|
bool enforcePMColor)
|
|
|
|
: fK1(k1), fK2(k2), fK3(k3), fK4(k4), fEnforcePMColor(enforcePMColor) {
|
2015-01-16 14:29:47 +00:00
|
|
|
this->initClassID<GrArithmeticXPFactory>();
|
2015-01-14 18:49:18 +00:00
|
|
|
}
|
|
|
|
|
2015-02-09 15:51:00 +00:00
|
|
|
GrXferProcessor*
|
2015-05-19 16:29:46 +00:00
|
|
|
GrArithmeticXPFactory::onCreateXferProcessor(const GrCaps& caps,
|
2015-02-17 19:15:47 +00:00
|
|
|
const GrProcOptInfo& colorPOI,
|
2015-02-09 15:51:00 +00:00
|
|
|
const GrProcOptInfo& coveragePOI,
|
2015-06-08 22:11:04 +00:00
|
|
|
bool hasMixedSamples,
|
2015-05-26 16:49:05 +00:00
|
|
|
const DstTexture* dstTexture) const {
|
2015-08-26 20:07:48 +00:00
|
|
|
return new ArithmeticXP(dstTexture, hasMixedSamples, fK1, fK2, fK3, fK4, fEnforcePMColor);
|
2015-02-09 15:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-02 17:43:39 +00:00
|
|
|
void GrArithmeticXPFactory::getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
|
|
|
|
InvariantBlendedColor* blendedColor) const {
|
|
|
|
blendedColor->fWillBlendWithDst = true;
|
|
|
|
|
|
|
|
// TODO: We could try to optimize this more. For example if 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).
|
|
|
|
blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
|
2015-01-14 20:53:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GR_DEFINE_XP_FACTORY_TEST(GrArithmeticXPFactory);
|
|
|
|
|
2015-08-29 00:16:50 +00:00
|
|
|
GrXPFactory* GrArithmeticXPFactory::TestCreate(GrProcessorTestData* d) {
|
2015-07-08 21:26:19 +00:00
|
|
|
float k1 = d->fRandom->nextF();
|
|
|
|
float k2 = d->fRandom->nextF();
|
|
|
|
float k3 = d->fRandom->nextF();
|
|
|
|
float k4 = d->fRandom->nextF();
|
|
|
|
bool enforcePMColor = d->fRandom->nextBool();
|
2015-01-14 20:53:01 +00:00
|
|
|
|
|
|
|
return GrArithmeticXPFactory::Create(k1, k2, k3, k4, enforcePMColor);
|
|
|
|
}
|
2015-01-14 18:49:18 +00:00
|
|
|
|
|
|
|
#endif
|