GrGLSLFragmentShaderBuilder.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2014 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrGLSLFragmentShaderBuilder_DEFINED
  8. #define GrGLSLFragmentShaderBuilder_DEFINED
  9. #include "include/gpu/GrBlend.h"
  10. #include "src/gpu/GrProcessor.h"
  11. #include "src/gpu/glsl/GrGLSLShaderBuilder.h"
  12. class GrRenderTarget;
  13. class GrGLSLVarying;
  14. /*
  15. * This base class encapsulates the common functionality which all processors use to build fragment
  16. * shaders.
  17. */
  18. class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder {
  19. public:
  20. GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {}
  21. virtual ~GrGLSLFragmentBuilder() {}
  22. /**
  23. * This returns a variable name to access the 2D, perspective correct version of the coords in
  24. * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If
  25. * the coordinates are 3-dimensional, it a perspective divide into is emitted into the
  26. * fragment shader (xy / z) to convert them to 2D.
  27. */
  28. virtual SkString ensureCoords2D(const GrShaderVar&) = 0;
  29. // TODO: remove this method.
  30. void declAppendf(const char* fmt, ...);
  31. private:
  32. typedef GrGLSLShaderBuilder INHERITED;
  33. };
  34. /*
  35. * This class is used by fragment processors to build their fragment code.
  36. */
  37. class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
  38. public:
  39. /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
  40. GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
  41. /**
  42. * Returns the variable name that holds the array of sample offsets from pixel center to each
  43. * sample location. Before this is called, a processor must have advertised that it will use
  44. * CustomFeatures::kSampleLocations.
  45. */
  46. virtual const char* sampleOffsets() = 0;
  47. enum class ScopeFlags {
  48. // Every fragment will always execute this code, and will do it exactly once.
  49. kTopLevel = 0,
  50. // Either all fragments in a given primitive, or none, will execute this code.
  51. kInsidePerPrimitiveBranch = (1 << 0),
  52. // Any given fragment may or may not execute this code.
  53. kInsidePerPixelBranch = (1 << 1),
  54. // This code will be executed more than once.
  55. kInsideLoop = (1 << 2)
  56. };
  57. /**
  58. * Subtracts multisample coverage by AND-ing the sample mask with the provided "mask".
  59. * Sample N corresponds to bit "1 << N".
  60. *
  61. * If the given scope is "kTopLevel" and the sample mask has not yet been modified, this method
  62. * assigns the sample mask in place rather than pre-initializing it to ~0 then AND-ing it.
  63. *
  64. * Requires MSAA and GLSL support for sample variables.
  65. */
  66. virtual void maskOffMultisampleCoverage(const char* mask, ScopeFlags) = 0;
  67. /**
  68. * Turns off coverage at each sample where the implicit function fn > 0.
  69. *
  70. * The provided "fn" value represents the implicit function at pixel center. We then approximate
  71. * the implicit at each sample by riding the gradient, "grad", linearly from pixel center to
  72. * each sample location.
  73. *
  74. * If "grad" is null, we approximate the gradient using HW derivatives.
  75. *
  76. * Requires MSAA and GLSL support for sample variables. Also requires HW derivatives if not
  77. * providing a gradient.
  78. */
  79. virtual void applyFnToMultisampleMask(const char* fn, const char* grad, ScopeFlags) = 0;
  80. /**
  81. * Fragment procs with child procs should call these functions before/after calling emitCode
  82. * on a child proc.
  83. */
  84. virtual void onBeforeChildProcEmitCode() = 0;
  85. virtual void onAfterChildProcEmitCode() = 0;
  86. virtual const SkString& getMangleString() const = 0;
  87. virtual void forceHighPrecision() = 0;
  88. };
  89. GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags);
  90. /*
  91. * This class is used by Xfer processors to build their fragment code.
  92. */
  93. class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder {
  94. public:
  95. /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */
  96. GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {}
  97. virtual bool hasCustomColorOutput() const = 0;
  98. virtual bool hasSecondaryOutput() const = 0;
  99. /** Returns the variable name that holds the color of the destination pixel. This may be nullptr
  100. * if no effect advertised that it will read the destination. */
  101. virtual const char* dstColor() = 0;
  102. /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with
  103. this shader. It is only legal to call this method with an advanced blend equation, and only
  104. if these equations are supported. */
  105. virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0;
  106. };
  107. /*
  108. * This class implements the various fragment builder interfaces.
  109. */
  110. class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder {
  111. public:
  112. /** Returns a nonzero key for a surface's origin. This should only be called if a processor will
  113. use the fragment position and/or sample locations. */
  114. static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin);
  115. GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program);
  116. // Shared GrGLSLFragmentBuilder interface.
  117. virtual SkString ensureCoords2D(const GrShaderVar&) override;
  118. // GrGLSLFPFragmentBuilder interface.
  119. const char* sampleOffsets() override;
  120. void maskOffMultisampleCoverage(const char* mask, ScopeFlags) override;
  121. void applyFnToMultisampleMask(const char* fn, const char* grad, ScopeFlags) override;
  122. const SkString& getMangleString() const override { return fMangleString; }
  123. void onBeforeChildProcEmitCode() override;
  124. void onAfterChildProcEmitCode() override;
  125. void forceHighPrecision() override { fForceHighPrecision = true; }
  126. // GrGLSLXPFragmentBuilder interface.
  127. bool hasCustomColorOutput() const override { return fHasCustomColorOutput; }
  128. bool hasSecondaryOutput() const override { return fHasSecondaryOutput; }
  129. const char* dstColor() override;
  130. void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override;
  131. private:
  132. using CustomFeatures = GrProcessor::CustomFeatures;
  133. // Private public interface, used by GrGLProgramBuilder to build a fragment shader
  134. void enableCustomOutput();
  135. void enableSecondaryOutput();
  136. const char* getPrimaryColorOutputName() const;
  137. const char* getSecondaryColorOutputName() const;
  138. bool primaryColorOutputIsInOut() const;
  139. #ifdef SK_DEBUG
  140. // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below
  141. // state to track this. The reset call is called per processor emitted.
  142. bool fHasReadDstColorThisStage_DebugOnly = false;
  143. CustomFeatures fUsedProcessorFeaturesThisStage_DebugOnly = CustomFeatures::kNone;
  144. CustomFeatures fUsedProcessorFeaturesAllStages_DebugOnly = CustomFeatures::kNone;
  145. void debugOnly_resetPerStageVerification() {
  146. fHasReadDstColorThisStage_DebugOnly = false;
  147. fUsedProcessorFeaturesThisStage_DebugOnly = CustomFeatures::kNone;
  148. }
  149. #endif
  150. static const char* DeclaredColorOutputName() { return "sk_FragColor"; }
  151. static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; }
  152. GrSurfaceOrigin getSurfaceOrigin() const;
  153. void onFinalize() override;
  154. static const char* kDstColorName;
  155. /*
  156. * State that tracks which child proc in the proc tree is currently emitting code. This is
  157. * used to update the fMangleString, which is used to mangle the names of uniforms and functions
  158. * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep
  159. * we are in the tree, and its second-to-last value is the index of the child proc at that
  160. * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that
  161. * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child.
  162. */
  163. SkTArray<int> fSubstageIndices;
  164. /*
  165. * The mangle string is used to mangle the names of uniforms/functions emitted by the child
  166. * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle
  167. * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0],
  168. * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will
  169. * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's
  170. * 1st child's 2nd child".
  171. */
  172. SkString fMangleString;
  173. bool fSetupFragPosition = false;
  174. bool fHasCustomColorOutput = false;
  175. int fCustomColorOutputIndex = -1;
  176. bool fHasSecondaryOutput = false;
  177. bool fHasModifiedSampleMask = false;
  178. bool fForceHighPrecision = false;
  179. friend class GrGLSLProgramBuilder;
  180. friend class GrGLProgramBuilder;
  181. };
  182. #endif