2014-10-15 17:50:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GrFragmentProcessor_DEFINED
|
|
|
|
#define GrFragmentProcessor_DEFINED
|
|
|
|
|
|
|
|
#include "GrProcessor.h"
|
|
|
|
|
|
|
|
class GrCoordTransform;
|
2015-11-13 14:54:19 +00:00
|
|
|
class GrGLSLFragmentProcessor;
|
2015-08-29 01:46:56 +00:00
|
|
|
class GrInvariantOutput;
|
2016-09-20 16:12:47 +00:00
|
|
|
class GrPipeline;
|
2014-12-04 19:35:33 +00:00
|
|
|
class GrProcessorKeyBuilder;
|
2016-11-29 18:43:05 +00:00
|
|
|
class GrShaderCaps;
|
2014-10-15 17:50:36 +00:00
|
|
|
|
|
|
|
/** Provides custom fragment shader code. Fragment processors receive an input color (vec4f) and
|
|
|
|
produce an output color. They may reference textures and uniforms. They may use
|
|
|
|
GrCoordTransforms to receive a transformation of the local coordinates that map from local space
|
|
|
|
to the fragment being processed.
|
|
|
|
*/
|
|
|
|
class GrFragmentProcessor : public GrProcessor {
|
|
|
|
public:
|
2015-09-22 13:41:59 +00:00
|
|
|
/**
|
|
|
|
* In many instances (e.g. SkShader::asFragmentProcessor() implementations) it is desirable to
|
|
|
|
* only consider the input color's alpha. However, there is a competing desire to have reusable
|
|
|
|
* GrFragmentProcessor subclasses that can be used in other scenarios where the entire input
|
|
|
|
* color is considered. This function exists to filter the input color and pass it to a FP. It
|
|
|
|
* does so by returning a parent FP that multiplies the passed in FPs output by the parent's
|
|
|
|
* input alpha. The passed in FP will not receive an input color.
|
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<GrFragmentProcessor> MulOutputByInputAlpha(sk_sp<GrFragmentProcessor>);
|
2015-09-28 13:26:28 +00:00
|
|
|
|
|
|
|
/**
|
2017-01-26 16:25:12 +00:00
|
|
|
* This assumes that the input color to the returned processor will be unpremul and that the
|
|
|
|
* passed processor (which becomes the returned processor's child) produces a premul output.
|
|
|
|
* The result of the returned processor is a premul of its input color modulated by the child
|
|
|
|
* processor's premul output.
|
2015-09-28 13:26:28 +00:00
|
|
|
*/
|
2017-01-26 16:25:12 +00:00
|
|
|
static sk_sp<GrFragmentProcessor> MakeInputPremulAndMulByOutput(sk_sp<GrFragmentProcessor>);
|
2015-09-28 13:26:28 +00:00
|
|
|
|
|
|
|
/**
|
2015-09-29 13:38:55 +00:00
|
|
|
* Returns a parent fragment processor that adopts the passed fragment processor as a child.
|
|
|
|
* The parent will ignore its input color and instead feed the passed in color as input to the
|
|
|
|
* child.
|
2015-09-28 13:26:28 +00:00
|
|
|
*/
|
2016-09-08 16:33:50 +00:00
|
|
|
static sk_sp<GrFragmentProcessor> OverrideInput(sk_sp<GrFragmentProcessor>, GrColor4f);
|
2015-09-22 13:41:59 +00:00
|
|
|
|
2016-07-20 18:20:30 +00:00
|
|
|
/**
|
|
|
|
* Returns a fragment processor that premuls the input before calling the passed in fragment
|
|
|
|
* processor.
|
|
|
|
*/
|
|
|
|
static sk_sp<GrFragmentProcessor> PremulInput(sk_sp<GrFragmentProcessor>);
|
|
|
|
|
2015-09-29 13:38:55 +00:00
|
|
|
/**
|
|
|
|
* Returns a fragment processor that runs the passed in array of fragment processors in a
|
|
|
|
* series. The original input is passed to the first, the first's output is passed to the
|
|
|
|
* second, etc. The output of the returned processor is the output of the last processor of the
|
|
|
|
* series.
|
2016-06-09 15:01:03 +00:00
|
|
|
*
|
|
|
|
* The array elements with be moved.
|
2015-09-29 13:38:55 +00:00
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
static sk_sp<GrFragmentProcessor> RunInSeries(sk_sp<GrFragmentProcessor>*, int cnt);
|
2015-09-29 13:38:55 +00:00
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
~GrFragmentProcessor() override;
|
|
|
|
|
2015-11-13 19:57:27 +00:00
|
|
|
GrGLSLFragmentProcessor* createGLSLInstance() const;
|
2014-12-04 19:35:33 +00:00
|
|
|
|
2016-11-29 18:43:05 +00:00
|
|
|
void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const {
|
2015-11-13 19:57:27 +00:00
|
|
|
this->onGetGLSLProcessorKey(caps, b);
|
2015-08-04 14:59:37 +00:00
|
|
|
for (int i = 0; i < fChildProcessors.count(); ++i) {
|
2015-11-13 19:57:27 +00:00
|
|
|
fChildProcessors[i]->getGLSLProcessorKey(caps, b);
|
2015-08-04 14:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 16:12:47 +00:00
|
|
|
int numCoordTransforms() const { return fCoordTransforms.count(); }
|
2014-10-15 17:50:36 +00:00
|
|
|
|
|
|
|
/** Returns the coordinate transformation at index. index must be valid according to
|
|
|
|
numTransforms(). */
|
|
|
|
const GrCoordTransform& coordTransform(int index) const { return *fCoordTransforms[index]; }
|
|
|
|
|
2015-01-13 23:02:10 +00:00
|
|
|
const SkTArray<const GrCoordTransform*, true>& coordTransforms() const {
|
|
|
|
return fCoordTransforms;
|
|
|
|
}
|
|
|
|
|
2015-08-04 14:59:37 +00:00
|
|
|
int numChildProcessors() const { return fChildProcessors.count(); }
|
|
|
|
|
2015-08-27 13:30:17 +00:00
|
|
|
const GrFragmentProcessor& childProcessor(int index) const { return *fChildProcessors[index]; }
|
2015-08-04 14:59:37 +00:00
|
|
|
|
2014-12-19 21:45:20 +00:00
|
|
|
/** Do any of the coordtransforms for this processor require local coords? */
|
2017-01-27 15:59:27 +00:00
|
|
|
bool usesLocalCoords() const { return SkToBool(fFlags & kUsesLocalCoords_Flag); }
|
2014-12-19 21:45:20 +00:00
|
|
|
|
2016-08-01 18:01:56 +00:00
|
|
|
/** Does this FP need a vector to the nearest edge? */
|
2017-01-27 15:59:27 +00:00
|
|
|
bool usesDistanceVectorField() const {
|
|
|
|
return SkToBool(fFlags & kUsesDistanceVectorField_Flag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the processor's output is a modulation of its input color or alpha with a computed
|
|
|
|
* color or alpha in the 0..1 range. If true and the blend mode allows it we may fold coverage
|
|
|
|
* into the first color fragment processor's input.
|
|
|
|
*/
|
|
|
|
bool modulatesInput() const { return SkToBool(fFlags & kModulatesInput_OptimizationFlag); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this is true then all opaque input colors to the processor produce opaque output colors.
|
|
|
|
*/
|
|
|
|
bool preservesOpaqueInput() const {
|
|
|
|
return SkToBool(fFlags & kPreservesOpaqueInput_OptimizationFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether given a constant input color the processor produces a constant output color
|
|
|
|
* (for all fragments). If true outputColor will contain the constant color produces for
|
|
|
|
* inputColor.
|
|
|
|
*/
|
|
|
|
bool hasConstantOutputForConstantInput(GrColor4f inputColor, GrColor4f* outputColor) const {
|
|
|
|
if (fFlags & kConstantOutputForConstantInput_OptimizationFlag) {
|
|
|
|
*outputColor = this->constantOutputForConstantInput(inputColor);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool hasConstantOutputForConstantInput() const {
|
|
|
|
return SkToBool(fFlags & kConstantOutputForConstantInput_OptimizationFlag);
|
|
|
|
}
|
2016-08-01 18:01:56 +00:00
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
/** Returns true if this and other processor conservatively draw identically. It can only return
|
|
|
|
true when the two processor are of the same subclass (i.e. they return the same object from
|
2014-10-15 17:50:36 +00:00
|
|
|
from getFactory()).
|
|
|
|
|
2014-12-04 19:35:33 +00:00
|
|
|
A return value of true from isEqual() should not be used to test whether the processor would
|
2015-11-13 19:57:27 +00:00
|
|
|
generate the same shader code. To test for identical code generation use getGLSLProcessorKey
|
|
|
|
*/
|
2016-09-12 15:55:38 +00:00
|
|
|
bool isEqual(const GrFragmentProcessor& that) const;
|
2014-10-15 17:50:36 +00:00
|
|
|
|
2014-12-11 23:44:02 +00:00
|
|
|
/**
|
|
|
|
* This function is used to perform optimizations. When called the invarientOuput param
|
|
|
|
* indicate whether the input components to this processor in the FS will have known values.
|
|
|
|
* In inout the validFlags member is a bitfield of GrColorComponentFlags. The isSingleComponent
|
|
|
|
* member indicates whether the input will be 1 or 4 bytes. The function updates the members of
|
|
|
|
* inout to indicate known values of its output. A component of the color member only has
|
|
|
|
* meaning if the corresponding bit in validFlags is set.
|
|
|
|
*/
|
2015-08-29 01:46:56 +00:00
|
|
|
void computeInvariantOutput(GrInvariantOutput* inout) const {
|
|
|
|
this->onComputeInvariantOutput(inout);
|
|
|
|
}
|
2014-12-11 23:44:02 +00:00
|
|
|
|
2016-09-20 16:12:47 +00:00
|
|
|
/**
|
|
|
|
* Pre-order traversal of a FP hierarchy, or of the forest of FPs in a GrPipeline. In the latter
|
|
|
|
* case the tree rooted at each FP in the GrPipeline is visited successively.
|
2016-09-26 13:55:02 +00:00
|
|
|
*/
|
2016-09-20 16:12:47 +00:00
|
|
|
class Iter : public SkNoncopyable {
|
|
|
|
public:
|
|
|
|
explicit Iter(const GrFragmentProcessor* fp) { fFPStack.push_back(fp); }
|
|
|
|
explicit Iter(const GrPipeline& pipeline);
|
|
|
|
const GrFragmentProcessor* next();
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkSTArray<4, const GrFragmentProcessor*, true> fFPStack;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-09-26 13:55:02 +00:00
|
|
|
* Iterates over all the Ts owned by a GrFragmentProcessor and its children or over all the Ts
|
|
|
|
* owned by the forest of GrFragmentProcessors in a GrPipeline. FPs are visited in the same
|
|
|
|
* order as Iter and each of an FP's Ts are visited in order.
|
2016-09-20 16:12:47 +00:00
|
|
|
*/
|
2016-09-26 13:55:02 +00:00
|
|
|
template <typename T, typename BASE,
|
|
|
|
int (BASE::*COUNT)() const,
|
|
|
|
const T& (BASE::*GET)(int) const>
|
|
|
|
class FPItemIter : public SkNoncopyable {
|
2016-09-20 16:12:47 +00:00
|
|
|
public:
|
2016-09-26 13:55:02 +00:00
|
|
|
explicit FPItemIter(const GrFragmentProcessor* fp)
|
|
|
|
: fCurrFP(nullptr)
|
|
|
|
, fCTIdx(0)
|
|
|
|
, fFPIter(fp) {
|
|
|
|
fCurrFP = fFPIter.next();
|
|
|
|
}
|
|
|
|
explicit FPItemIter(const GrPipeline& pipeline)
|
2016-09-20 16:12:47 +00:00
|
|
|
: fCurrFP(nullptr)
|
|
|
|
, fCTIdx(0)
|
|
|
|
, fFPIter(pipeline) {
|
|
|
|
fCurrFP = fFPIter.next();
|
|
|
|
}
|
2016-09-26 13:55:02 +00:00
|
|
|
|
|
|
|
const T* next() {
|
|
|
|
if (!fCurrFP) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
while (fCTIdx == (fCurrFP->*COUNT)()) {
|
|
|
|
fCTIdx = 0;
|
|
|
|
fCurrFP = fFPIter.next();
|
|
|
|
if (!fCurrFP) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &(fCurrFP->*GET)(fCTIdx++);
|
|
|
|
}
|
2016-09-20 16:12:47 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const GrFragmentProcessor* fCurrFP;
|
|
|
|
int fCTIdx;
|
|
|
|
GrFragmentProcessor::Iter fFPIter;
|
|
|
|
};
|
|
|
|
|
2016-09-26 13:55:02 +00:00
|
|
|
using CoordTransformIter = FPItemIter<GrCoordTransform,
|
|
|
|
GrFragmentProcessor,
|
|
|
|
&GrFragmentProcessor::numCoordTransforms,
|
|
|
|
&GrFragmentProcessor::coordTransform>;
|
|
|
|
|
2016-11-17 16:38:22 +00:00
|
|
|
using TextureAccessIter = FPItemIter<TextureSampler,
|
2016-09-26 13:55:02 +00:00
|
|
|
GrProcessor,
|
2016-11-17 16:38:22 +00:00
|
|
|
&GrProcessor::numTextureSamplers,
|
|
|
|
&GrProcessor::textureSampler>;
|
2016-09-26 13:55:02 +00:00
|
|
|
|
2014-10-15 17:50:36 +00:00
|
|
|
protected:
|
2017-01-27 15:59:27 +00:00
|
|
|
enum OptimizationFlags : uint32_t {
|
|
|
|
kNone_OptimizationFlags,
|
|
|
|
kModulatesInput_OptimizationFlag = 0x1,
|
|
|
|
kPreservesOpaqueInput_OptimizationFlag = 0x2,
|
|
|
|
kConstantOutputForConstantInput_OptimizationFlag = 0x4,
|
|
|
|
kAll_OptimizationFlags = kModulatesInput_OptimizationFlag |
|
|
|
|
kPreservesOpaqueInput_OptimizationFlag |
|
|
|
|
kConstantOutputForConstantInput_OptimizationFlag
|
|
|
|
};
|
|
|
|
GR_DECL_BITFIELD_OPS_FRIENDS(OptimizationFlags)
|
|
|
|
|
|
|
|
GrFragmentProcessor(OptimizationFlags optimizationFlags) : fFlags(optimizationFlags) {
|
|
|
|
SkASSERT((fFlags & ~kAll_OptimizationFlags) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptimizationFlags optimizationFlags() const {
|
|
|
|
return static_cast<OptimizationFlags>(kAll_OptimizationFlags & fFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This allows one subclass to access another subclass's implementation of
|
|
|
|
* constantOutputForConstantInput. It must only be called when
|
|
|
|
* hasConstantOutputForConstantInput() is known to be true.
|
|
|
|
*/
|
|
|
|
static GrColor4f ConstantOutputForConstantInput(const GrFragmentProcessor& fp,
|
|
|
|
GrColor4f input) {
|
|
|
|
SkASSERT(fp.hasConstantOutputForConstantInput());
|
|
|
|
return fp.constantOutputForConstantInput(input);
|
|
|
|
}
|
|
|
|
|
2014-10-15 17:50:36 +00:00
|
|
|
/**
|
|
|
|
* Fragment Processor subclasses call this from their constructor to register coordinate
|
2014-10-16 02:06:21 +00:00
|
|
|
* transformations. Coord transforms provide a mechanism for a processor to receive coordinates
|
|
|
|
* in their FS code. The matrix expresses a transformation from local space. For a given
|
|
|
|
* fragment the matrix will be applied to the local coordinate that maps to the fragment.
|
|
|
|
*
|
|
|
|
* When the transformation has perspective, the transformed coordinates will have
|
2015-08-19 18:05:39 +00:00
|
|
|
* 3 components. Otherwise they'll have 2.
|
2014-10-16 02:06:21 +00:00
|
|
|
*
|
|
|
|
* This must only be called from the constructor because GrProcessors are immutable. The
|
|
|
|
* processor subclass manages the lifetime of the transformations (this function only stores a
|
2015-08-19 18:05:39 +00:00
|
|
|
* pointer). The GrCoordTransform is typically a member field of the GrProcessor subclass.
|
2014-10-16 02:06:21 +00:00
|
|
|
*
|
|
|
|
* A processor subclass that has multiple methods of construction should always add its coord
|
|
|
|
* transforms in a consistent order. The non-virtual implementation of isEqual() automatically
|
|
|
|
* compares transforms and will assume they line up across the two processor instances.
|
2014-10-15 17:50:36 +00:00
|
|
|
*/
|
|
|
|
void addCoordTransform(const GrCoordTransform*);
|
|
|
|
|
2015-08-04 14:59:37 +00:00
|
|
|
/**
|
2015-08-12 16:40:47 +00:00
|
|
|
* FragmentProcessor subclasses call this from their constructor to register any child
|
2015-08-19 15:23:12 +00:00
|
|
|
* FragmentProcessors they have. This must be called AFTER all texture accesses and coord
|
|
|
|
* transforms have been added.
|
2015-08-04 14:59:37 +00:00
|
|
|
* This is for processors whose shader code will be composed of nested processors whose output
|
|
|
|
* colors will be combined somehow to produce its output color. Registering these child
|
2015-08-12 16:40:47 +00:00
|
|
|
* processors will allow the ProgramBuilder to automatically handle their transformed coords and
|
|
|
|
* texture accesses and mangle their uniform and output color names.
|
2015-08-04 14:59:37 +00:00
|
|
|
*/
|
2016-06-09 15:01:03 +00:00
|
|
|
int registerChildProcessor(sk_sp<GrFragmentProcessor> child);
|
2015-08-04 14:59:37 +00:00
|
|
|
|
2014-12-11 23:44:02 +00:00
|
|
|
/**
|
|
|
|
* Subclass implements this to support getConstantColorComponents(...).
|
2015-08-18 14:39:33 +00:00
|
|
|
*
|
|
|
|
* Note: it's up to the subclass implementation to do any recursive call to compute the child
|
|
|
|
* procs' output invariants; computeInvariantOutput will not be recursive.
|
2014-12-11 23:44:02 +00:00
|
|
|
*/
|
|
|
|
virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const = 0;
|
|
|
|
|
2017-01-27 15:59:27 +00:00
|
|
|
/**
|
|
|
|
* Sub-classes should call this in their constructors if they need access to a distance
|
2016-08-01 18:01:56 +00:00
|
|
|
* vector field to the nearest edge
|
|
|
|
*/
|
2017-01-27 15:59:27 +00:00
|
|
|
void setWillUseDistanceVectorField() { fFlags |= kUsesDistanceVectorField_Flag; }
|
2016-08-01 18:01:56 +00:00
|
|
|
|
2014-10-15 17:50:36 +00:00
|
|
|
private:
|
2015-08-27 23:43:48 +00:00
|
|
|
void notifyRefCntIsZero() const final;
|
|
|
|
|
2017-01-27 15:59:27 +00:00
|
|
|
virtual GrColor4f constantOutputForConstantInput(GrColor4f /* inputColor */) const {
|
|
|
|
SkFAIL("Subclass must override this if advertising this optimization.");
|
|
|
|
return GrColor4f::TransparentBlack();
|
|
|
|
}
|
|
|
|
|
2015-08-18 18:29:31 +00:00
|
|
|
/** Returns a new instance of the appropriate *GL* implementation class
|
|
|
|
for the given GrFragmentProcessor; caller is responsible for deleting
|
|
|
|
the object. */
|
2015-11-13 19:57:27 +00:00
|
|
|
virtual GrGLSLFragmentProcessor* onCreateGLSLInstance() const = 0;
|
2015-08-18 18:29:31 +00:00
|
|
|
|
2015-08-04 14:59:37 +00:00
|
|
|
/** Implemented using GLFragmentProcessor::GenKey as described in this class's comment. */
|
2016-11-29 18:43:05 +00:00
|
|
|
virtual void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const = 0;
|
2015-08-04 14:59:37 +00:00
|
|
|
|
2014-10-16 02:06:21 +00:00
|
|
|
/**
|
|
|
|
* Subclass implements this to support isEqual(). It will only be called if it is known that
|
|
|
|
* the two processors are of the same subclass (i.e. they return the same object from
|
|
|
|
* getFactory()). The processor subclass should not compare its coord transforms as that will
|
|
|
|
* be performed automatically in the non-virtual isEqual().
|
|
|
|
*/
|
|
|
|
virtual bool onIsEqual(const GrFragmentProcessor&) const = 0;
|
|
|
|
|
|
|
|
bool hasSameTransforms(const GrFragmentProcessor&) const;
|
2014-10-15 17:50:36 +00:00
|
|
|
|
2017-01-27 15:59:27 +00:00
|
|
|
enum PrivateFlags {
|
|
|
|
kFirstPrivateFlag = kAll_OptimizationFlags + 1,
|
|
|
|
kUsesLocalCoords_Flag = kFirstPrivateFlag,
|
|
|
|
kUsesDistanceVectorField_Flag = kFirstPrivateFlag << 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable uint32_t fFlags = 0;
|
2015-08-12 16:40:47 +00:00
|
|
|
|
2016-09-20 16:12:47 +00:00
|
|
|
SkSTArray<4, const GrCoordTransform*, true> fCoordTransforms;
|
|
|
|
|
2016-06-09 15:01:03 +00:00
|
|
|
/**
|
|
|
|
* This is not SkSTArray<1, sk_sp<GrFragmentProcessor>> because this class holds strong
|
|
|
|
* references until notifyRefCntIsZero and then it holds pending executions.
|
|
|
|
*/
|
2017-01-27 15:59:27 +00:00
|
|
|
SkSTArray<1, GrFragmentProcessor*, true> fChildProcessors;
|
2014-10-15 17:50:36 +00:00
|
|
|
|
|
|
|
typedef GrProcessor INHERITED;
|
|
|
|
};
|
|
|
|
|
2017-01-27 15:59:27 +00:00
|
|
|
GR_MAKE_BITFIELD_OPS(GrFragmentProcessor::OptimizationFlags)
|
|
|
|
|
2014-10-15 17:50:36 +00:00
|
|
|
#endif
|