joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 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 | |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 8 | #ifndef GrGLSLFragmentShaderBuilder_DEFINED |
| 9 | #define GrGLSLFragmentShaderBuilder_DEFINED |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 11 | #include "GrBlend.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 12 | #include "GrGLSLShaderBuilder.h" |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 13 | #include "GrProcessor.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 14 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 15 | class GrRenderTarget; |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 16 | class GrGLSLVarying; |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 17 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 18 | /* |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 19 | * This base class encapsulates the common functionality which all processors use to build fragment |
| 20 | * shaders. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 21 | */ |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 22 | class GrGLSLFragmentBuilder : public GrGLSLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 23 | public: |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 24 | GrGLSLFragmentBuilder(GrGLSLProgramBuilder* program) : INHERITED(program) {} |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 25 | virtual ~GrGLSLFragmentBuilder() {} |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 26 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
| 29 | * if code is added that uses one of these features without calling enableFeature() |
| 30 | */ |
| 31 | enum GLSLFeature { |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 32 | kPixelLocalStorage_GLSLFeature = kLastGLSLPrivateFeature + 1, |
cdalton | 4a98cdb | 2016-03-01 12:12:20 -0800 | [diff] [blame] | 33 | kMultisampleInterpolation_GLSLFeature |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * If the feature is supported then true is returned and any necessary #extension declarations |
| 38 | * are added to the shaders. If the feature is not supported then false will be returned. |
| 39 | */ |
| 40 | virtual bool enableFeature(GLSLFeature) = 0; |
| 41 | |
| 42 | /** |
| 43 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 44 | * the fragment shader. The passed in coordinates must either be of type kVec2f or kVec3f. If |
| 45 | * the coordinates are 3-dimensional, it a perspective divide into is emitted into the |
| 46 | * fragment shader (xy / z) to convert them to 2D. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 47 | */ |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 48 | virtual SkString ensureCoords2D(const GrShaderVar&) = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 49 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 50 | // TODO: remove this method. |
ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 51 | void declAppendf(const char* fmt, ...); |
| 52 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 53 | private: |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 54 | typedef GrGLSLShaderBuilder INHERITED; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /* |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 58 | * This class is used by fragment processors to build their fragment code. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 59 | */ |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 60 | class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 61 | public: |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 62 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
| 63 | GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 64 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 65 | enum Coordinates { |
| 66 | kSkiaDevice_Coordinates, |
| 67 | kGLSLWindow_Coordinates, |
| 68 | |
| 69 | kLast_Coordinates = kGLSLWindow_Coordinates |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * Appends the offset from the center of the pixel to a specified sample. |
| 74 | * |
| 75 | * @param sampleIdx GLSL expression of the sample index. |
| 76 | * @param Coordinates Coordinate space in which to emit the offset. |
| 77 | * |
| 78 | * A processor must call setWillUseSampleLocations in its constructor before using this method. |
| 79 | */ |
| 80 | virtual void appendOffsetToSample(const char* sampleIdx, Coordinates) = 0; |
| 81 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 82 | /** |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 83 | * Subtracts sample coverage from the fragment. Any sample whose corresponding bit is not found |
| 84 | * in the mask will not be written out to the framebuffer. |
| 85 | * |
| 86 | * @param mask int that contains the sample mask. Bit N corresponds to the Nth sample. |
| 87 | * @param invert perform a bit-wise NOT on the provided mask before applying it? |
| 88 | * |
| 89 | * Requires GLSL support for sample variables. |
| 90 | */ |
| 91 | virtual void maskSampleCoverage(const char* mask, bool invert = false) = 0; |
| 92 | |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 93 | /** Returns a variable name that represents a vector to the nearest edge of the shape, in source |
| 94 | space coordinates. */ |
| 95 | virtual const char* distanceVectorName() const = 0; |
| 96 | |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 97 | /** |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 98 | * Fragment procs with child procs should call these functions before/after calling emitCode |
| 99 | * on a child proc. |
| 100 | */ |
| 101 | virtual void onBeforeChildProcEmitCode() = 0; |
| 102 | virtual void onAfterChildProcEmitCode() = 0; |
| 103 | |
| 104 | virtual const SkString& getMangleString() const = 0; |
| 105 | }; |
| 106 | |
| 107 | /* |
| 108 | * This class is used by primitive processors to build their fragment code. |
| 109 | */ |
| 110 | class GrGLSLPPFragmentBuilder : public GrGLSLFPFragmentBuilder { |
| 111 | public: |
| 112 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
| 113 | GrGLSLPPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Overrides the fragment's sample coverage. The provided mask determines which samples will now |
| 117 | * be written out to the framebuffer. Note that this mask can be reduced by a future call to |
| 118 | * maskSampleCoverage. |
| 119 | * |
| 120 | * If a primitive processor uses this method, it must guarantee that every codepath through the |
| 121 | * shader overrides the sample mask at some point. |
| 122 | * |
| 123 | * @param mask int that contains the new coverage mask. Bit N corresponds to the Nth sample. |
| 124 | * |
| 125 | * Requires NV_sample_mask_override_coverage. |
| 126 | */ |
| 127 | virtual void overrideSampleCoverage(const char* mask) = 0; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * This class is used by Xfer processors to build their fragment code. |
| 132 | */ |
| 133 | class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder { |
| 134 | public: |
| 135 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
| 136 | GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} |
| 137 | |
| 138 | virtual bool hasCustomColorOutput() const = 0; |
| 139 | virtual bool hasSecondaryOutput() const = 0; |
| 140 | |
| 141 | /** Returns the variable name that holds the color of the destination pixel. This may be nullptr |
| 142 | * if no effect advertised that it will read the destination. */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 143 | virtual const char* dstColor() = 0; |
| 144 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 145 | /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with |
| 146 | this shader. It is only legal to call this method with an advanced blend equation, and only |
| 147 | if these equations are supported. */ |
| 148 | virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 151 | /* |
| 152 | * This class implements the various fragment builder interfaces. |
| 153 | */ |
| 154 | class GrGLSLFragmentShaderBuilder : public GrGLSLPPFragmentBuilder, public GrGLSLXPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 155 | public: |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 156 | /** Returns a nonzero key for a surface's origin. This should only be called if a processor will |
| 157 | use the fragment position and/or sample locations. */ |
| 158 | static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 159 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 160 | GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 161 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 162 | // Shared GrGLSLFragmentBuilder interface. |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 163 | bool enableFeature(GLSLFeature) override; |
bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 164 | virtual SkString ensureCoords2D(const GrShaderVar&) override; |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 165 | const char* distanceVectorName() const override; |
joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 166 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 167 | // GrGLSLFPFragmentBuilder interface. |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 168 | void appendOffsetToSample(const char* sampleIdx, Coordinates) override; |
cdalton | 33ad701 | 2016-02-22 07:55:44 -0800 | [diff] [blame] | 169 | void maskSampleCoverage(const char* mask, bool invert = false) override; |
| 170 | void overrideSampleCoverage(const char* mask) override; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 171 | const SkString& getMangleString() const override { return fMangleString; } |
| 172 | void onBeforeChildProcEmitCode() override; |
| 173 | void onAfterChildProcEmitCode() override; |
| 174 | |
| 175 | // GrGLSLXPFragmentBuilder interface. |
| 176 | bool hasCustomColorOutput() const override { return fHasCustomColorOutput; } |
| 177 | bool hasSecondaryOutput() const override { return fHasSecondaryOutput; } |
| 178 | const char* dstColor() override; |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 179 | void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; |
| 180 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 181 | private: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 182 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 183 | void enableCustomOutput(); |
| 184 | void enableSecondaryOutput(); |
| 185 | const char* getPrimaryColorOutputName() const; |
| 186 | const char* getSecondaryColorOutputName() const; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 187 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 188 | #ifdef SK_DEBUG |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 189 | // As GLSLProcessors emit code, there are some conditions we need to verify. We use the below |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 190 | // state to track this. The reset call is called per processor emitted. |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 191 | GrProcessor::RequiredFeatures usedProcessorFeatures() const { return fUsedProcessorFeatures; } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 192 | bool hasReadDstColor() const { return fHasReadDstColor; } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 193 | void resetVerification() { |
| 194 | fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 195 | fHasReadDstColor = false; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 196 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 197 | #endif |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 198 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 199 | static const char* DeclaredColorOutputName() { return "sk_FragColor"; } |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 200 | static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; } |
| 201 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 202 | GrSurfaceOrigin getSurfaceOrigin() const; |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 203 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 204 | void onFinalize() override; |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 205 | void defineSampleOffsetArray(const char* name, const SkMatrix&); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 206 | |
egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 207 | static const char* kDstColorName; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 208 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 209 | /* |
| 210 | * State that tracks which child proc in the proc tree is currently emitting code. This is |
| 211 | * used to update the fMangleString, which is used to mangle the names of uniforms and functions |
| 212 | * emitted by the proc. fSubstageIndices is a stack: its count indicates how many levels deep |
| 213 | * we are in the tree, and its second-to-last value is the index of the child proc at that |
| 214 | * level which is currently emitting code. For example, if fSubstageIndices = [3, 1, 2, 0], that |
| 215 | * means we're currently emitting code for the base proc's 3rd child's 1st child's 2nd child. |
| 216 | */ |
| 217 | SkTArray<int> fSubstageIndices; |
| 218 | |
| 219 | /* |
| 220 | * The mangle string is used to mangle the names of uniforms/functions emitted by the child |
| 221 | * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle |
| 222 | * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0], |
| 223 | * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will |
| 224 | * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's |
| 225 | * 1st child's 2nd child". |
| 226 | */ |
| 227 | SkString fMangleString; |
| 228 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 229 | bool fSetupFragPosition; |
| 230 | bool fHasCustomColorOutput; |
| 231 | int fCustomColorOutputIndex; |
| 232 | bool fHasSecondaryOutput; |
| 233 | uint8_t fUsedSampleOffsetArrays; |
| 234 | bool fHasInitializedSampleMask; |
dvonbeck | 9b03e7b | 2016-08-01 11:01:56 -0700 | [diff] [blame] | 235 | SkString fDistanceVectorOutput; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 236 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 237 | #ifdef SK_DEBUG |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 238 | // some state to verify shaders and effects are consistent, this is reset between effects by |
| 239 | // the program creator |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 240 | GrProcessor::RequiredFeatures fUsedProcessorFeatures; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 241 | bool fHasReadDstColor; |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 242 | #endif |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 243 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 244 | friend class GrGLSLProgramBuilder; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 245 | friend class GrGLProgramBuilder; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | #endif |