Add more specialized fragment builders
Adds specialized fragment builders for primitive and fragment
processors. Removes fragment-specific functionality from the Xfer
fragment builder.
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1709153002
Review URL: https://codereview.chromium.org/1709153002
diff --git a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
index a437a19..4dab6f2 100644
--- a/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
+++ b/src/gpu/glsl/GrGLSLFragmentShaderBuilder.h
@@ -16,17 +16,14 @@
class GrGLSLVarying;
/*
- * This base class encapsulates the functionality which the GP uses to build fragment shaders
+ * This base class encapsulates the common functionality which all processors use to build fragment
+ * shaders.
*/
class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder {
public:
- GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program)
- : INHERITED(program)
- , fHasCustomColorOutput(false)
- , fHasSecondaryOutput(false) {
- fSubstageIndices.push_back(0);
- }
+ GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
virtual ~GrGLSLFragmentBuilder() {}
+
/**
* Use of these features may require a GLSL extension to be enabled. Shaders may not compile
* if code is added that uses one of these features without calling enableFeature()
@@ -54,74 +51,65 @@
is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */
virtual const char* fragmentPosition() = 0;
- /**
- * Fragment procs with child procs should call these functions before/after calling emitCode
- * on a child proc.
- */
- void onBeforeChildProcEmitCode();
- void onAfterChildProcEmitCode();
-
- const SkString& getMangleString() const { return fMangleString; }
-
- bool hasCustomColorOutput() const { return fHasCustomColorOutput; }
- bool hasSecondaryOutput() const { return fHasSecondaryOutput; }
-
+ // TODO: remove this method.
void declAppendf(const char* fmt, ...);
-protected:
- bool fHasCustomColorOutput;
- bool fHasSecondaryOutput;
-
private:
- /*
- * State that tracks which child proc in the proc tree is currently emitting code. This is
- * used to update the fMangleString, which is used to mangle the names of uniforms and functions
- * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
- * we are in the tree, and its second-to-last value is the index of the child proc at that
- * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
- * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
- */
- SkTArray<int> fSubstageIndices;
-
- /*
- * The mangle string is used to mangle the names of uniforms/functions emitted by the child
- * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
- * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
- * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
- * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
- * 1st child's 2nd child".
- */
- SkString fMangleString;
-
- friend class GrGLPathProcessor;
-
typedef GrGLSLShaderBuilder INHERITED;
};
/*
- * Fragment processor's, in addition to all of the above, may need to use dst color so they use
- * this builder to create their shader. Because this is the only shader builder the FP sees, we
- * just call it FPShaderBuilder
+ * This class is used by fragment processors to build their fragment code.
*/
-class GrGLSLXPFragmentBuilder : public GrGLSLFragmentBuilder {
+class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
public:
- GrGLSLXPFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
+ /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
+ GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
- /** Returns the variable name that holds the color of the destination pixel. This may be nullptr if
- no effect advertised that it will read the destination. */
+ /**
+ * Fragment procs with child procs should call these functions before/after calling emitCode
+ * on a child proc.
+ */
+ virtual void onBeforeChildProcEmitCode() = 0;
+ virtual void onAfterChildProcEmitCode() = 0;
+
+ virtual const SkString& getMangleString() const = 0;
+};
+
+/*
+ * This class is used by primitive processors to build their fragment code.
+ */
+class GrGLSLPPFragmentBuilder : public GrGLSLFPFragmentBuilder {
+public:
+ /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
+ GrGLSLPPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
+};
+
+/*
+ * This class is used by Xfer processors to build their fragment code.
+ */
+class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
+public:
+ /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
+ GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
+
+ virtual bool hasCustomColorOutput() const = 0;
+ virtual bool hasSecondaryOutput() const = 0;
+
+ /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
+ * if no effect advertised that it will read the destination. */
virtual const char* dstColor() = 0;
/** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
this shader. It is only legal to call this method with an advanced blend equation, and only
if these equations are supported. */
virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
-
-private:
- typedef GrGLSLFragmentBuilder INHERITED;
};
-// TODO rename to Fragment Builder
-class GrGLSLFragmentShaderBuilder : public GrGLSLXPFragmentBuilder {
+/*
+ * This class implements the various fragment builder interfaces.
+ */
+class GrGLSLFragmentShaderBuilder : public GrGLSLPPFragmentBuilder, public GrGLSLXPFragmentBuilder {
public:
typedef uint8_t FragPosKey;
@@ -132,13 +120,21 @@
GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program, uint8_t fragPosKey);
- // true public interface, defined explicitly in the abstract interfaces above
+ // Shared GrGLSLFragmentBuilder interface.
bool enableFeature(GLSLFeature) override;
virtual SkString ensureFSCoords2D(const GrGLSLTransformedCoordsArray& coords,
int index) override;
const char* fragmentPosition() override;
- const char* dstColor() override;
+ // GrGLSLFPFragmentBuilder interface.
+ const SkString& getMangleString() const override { return fMangleString; }
+ void onBeforeChildProcEmitCode() override;
+ void onAfterChildProcEmitCode() override;
+
+ // GrGLSLXPFragmentBuilder interface.
+ bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
+ bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
+ const char* dstColor() override;
void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
private:
@@ -176,9 +172,31 @@
static const char* kDstTextureColorName;
+ /*
+ * State that tracks which child proc in the proc tree is currently emitting code. This is
+ * used to update the fMangleString, which is used to mangle the names of uniforms and functions
+ * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
+ * we are in the tree, and its second-to-last value is the index of the child proc at that
+ * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
+ * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
+ */
+ SkTArray<int> fSubstageIndices;
+
+ /*
+ * The mangle string is used to mangle the names of uniforms/functions emitted by the child
+ * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
+ * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
+ * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
+ * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
+ * 1st child's 2nd child".
+ */
+ SkString fMangleString;
+
bool fSetupFragPosition;
bool fTopLeftFragPosRead;
+ bool fHasCustomColorOutput;
int fCustomColorOutputIndex;
+ bool fHasSecondaryOutput;
// some state to verify shaders and effects are consistent, this is reset between effects by
// the program creator
@@ -187,8 +205,6 @@
friend class GrGLSLProgramBuilder;
friend class GrGLProgramBuilder;
-
- typedef GrGLSLXPFragmentBuilder INHERITED;
};
#endif