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