| 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 | /** |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 28 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 29 | * the fragment shader. The passed in coordinates must either be of type kHalf2 or kHalf3. If |
| bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 30 | * the coordinates are 3-dimensional, it a perspective divide into is emitted into the |
| 31 | * fragment shader (xy / z) to convert them to 2D. |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 32 | */ |
| bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 33 | virtual SkString ensureCoords2D(const GrShaderVar&) = 0; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 34 | |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 35 | // TODO: remove this method. |
| ethannicholas | 2279325 | 2016-01-30 09:59:10 -0800 | [diff] [blame] | 36 | void declAppendf(const char* fmt, ...); |
| 37 | |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 38 | private: |
| egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 39 | typedef GrGLSLShaderBuilder INHERITED; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /* |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 43 | * This class is used by fragment processors to build their fragment code. |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 44 | */ |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 45 | class GrGLSLFPFragmentBuilder : virtual public GrGLSLFragmentBuilder { |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 46 | public: |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 47 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
| 48 | GrGLSLFPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 49 | |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 50 | enum Coordinates { |
| 51 | kSkiaDevice_Coordinates, |
| 52 | kGLSLWindow_Coordinates, |
| 53 | |
| 54 | kLast_Coordinates = kGLSLWindow_Coordinates |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * Appends the offset from the center of the pixel to a specified sample. |
| 59 | * |
| 60 | * @param sampleIdx GLSL expression of the sample index. |
| 61 | * @param Coordinates Coordinate space in which to emit the offset. |
| 62 | * |
| 63 | * A processor must call setWillUseSampleLocations in its constructor before using this method. |
| 64 | */ |
| 65 | virtual void appendOffsetToSample(const char* sampleIdx, Coordinates) = 0; |
| 66 | |
| 67 | /** |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 68 | * Fragment procs with child procs should call these functions before/after calling emitCode |
| 69 | * on a child proc. |
| 70 | */ |
| 71 | virtual void onBeforeChildProcEmitCode() = 0; |
| 72 | virtual void onAfterChildProcEmitCode() = 0; |
| 73 | |
| 74 | virtual const SkString& getMangleString() const = 0; |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 75 | |
| 76 | virtual void forceHighPrecision() = 0; |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | /* |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 80 | * This class is used by Xfer processors to build their fragment code. |
| 81 | */ |
| 82 | class GrGLSLXPFragmentBuilder : virtual public GrGLSLFragmentBuilder { |
| 83 | public: |
| 84 | /** Appease the compiler; the derived class initializes GrGLSLFragmentBuilder. */ |
| 85 | GrGLSLXPFragmentBuilder() : GrGLSLFragmentBuilder(nullptr) {} |
| 86 | |
| 87 | virtual bool hasCustomColorOutput() const = 0; |
| 88 | virtual bool hasSecondaryOutput() const = 0; |
| 89 | |
| 90 | /** Returns the variable name that holds the color of the destination pixel. This may be nullptr |
| 91 | * if no effect advertised that it will read the destination. */ |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 92 | virtual const char* dstColor() = 0; |
| 93 | |
| cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 94 | /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with |
| 95 | this shader. It is only legal to call this method with an advanced blend equation, and only |
| 96 | if these equations are supported. */ |
| 97 | virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; |
| joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 100 | /* |
| 101 | * This class implements the various fragment builder interfaces. |
| 102 | */ |
| Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 103 | class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder { |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 104 | public: |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 105 | /** Returns a nonzero key for a surface's origin. This should only be called if a processor will |
| 106 | use the fragment position and/or sample locations. */ |
| 107 | static uint8_t KeyForSurfaceOrigin(GrSurfaceOrigin); |
| 108 | |
| cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 109 | GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 110 | |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 111 | // Shared GrGLSLFragmentBuilder interface. |
| bsalomon | 1a1aa93 | 2016-09-12 09:30:36 -0700 | [diff] [blame] | 112 | virtual SkString ensureCoords2D(const GrShaderVar&) override; |
| joshualitt | db0d3ca | 2014-10-07 12:42:26 -0700 | [diff] [blame] | 113 | |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 114 | // GrGLSLFPFragmentBuilder interface. |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 115 | void appendOffsetToSample(const char* sampleIdx, Coordinates) override; |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 116 | const SkString& getMangleString() const override { return fMangleString; } |
| 117 | void onBeforeChildProcEmitCode() override; |
| 118 | void onAfterChildProcEmitCode() override; |
| Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 119 | void forceHighPrecision() override { fForceHighPrecision = true; } |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 120 | |
| 121 | // GrGLSLXPFragmentBuilder interface. |
| 122 | bool hasCustomColorOutput() const override { return fHasCustomColorOutput; } |
| 123 | bool hasSecondaryOutput() const override { return fHasSecondaryOutput; } |
| 124 | const char* dstColor() override; |
| cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 125 | void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; |
| 126 | |
| joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 127 | private: |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 128 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 129 | void enableCustomOutput(); |
| 130 | void enableSecondaryOutput(); |
| 131 | const char* getPrimaryColorOutputName() const; |
| 132 | const char* getSecondaryColorOutputName() const; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 133 | |
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 134 | #ifdef SK_DEBUG |
| egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 135 | // 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] | 136 | // state to track this. The reset call is called per processor emitted. |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 137 | GrProcessor::RequiredFeatures usedProcessorFeatures() const { return fUsedProcessorFeatures; } |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 138 | bool hasReadDstColor() const { return fHasReadDstColor; } |
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 139 | void resetVerification() { |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 140 | fUsedProcessorFeatures = GrProcessor::kNone_RequiredFeatures; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 141 | fHasReadDstColor = false; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 142 | } |
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 143 | #endif |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 144 | |
| ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 145 | static const char* DeclaredColorOutputName() { return "sk_FragColor"; } |
| egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 146 | static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; } |
| 147 | |
| cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 148 | GrSurfaceOrigin getSurfaceOrigin() const; |
| joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 149 | |
| egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 150 | void onFinalize() override; |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 151 | void defineSampleOffsetArray(const char* name, const SkMatrix&); |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 152 | |
| egdaniel | 138c263 | 2016-08-17 10:59:00 -0700 | [diff] [blame] | 153 | static const char* kDstColorName; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 154 | |
| cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 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 | /* |
| 166 | * The mangle string is used to mangle the names of uniforms/functions emitted by the child |
| 167 | * procs so no duplicate uniforms/functions appear in the generated shader program. The mangle |
| 168 | * string is simply based on fSubstageIndices. For example, if fSubstageIndices = [3, 1, 2, 0], |
| 169 | * then the manglestring will be "_c3_c1_c2", and any uniform/function emitted by that proc will |
| 170 | * have "_c3_c1_c2" appended to its name, which can be interpreted as "base proc's 3rd child's |
| 171 | * 1st child's 2nd child". |
| 172 | */ |
| 173 | SkString fMangleString; |
| 174 | |
| Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 175 | bool fSetupFragPosition; |
| 176 | bool fHasCustomColorOutput; |
| 177 | int fCustomColorOutputIndex; |
| 178 | bool fHasSecondaryOutput; |
| 179 | uint8_t fUsedSampleOffsetArrays; |
| 180 | bool fForceHighPrecision; |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 181 | |
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 182 | #ifdef SK_DEBUG |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 183 | // some state to verify shaders and effects are consistent, this is reset between effects by |
| 184 | // the program creator |
| Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 185 | GrProcessor::RequiredFeatures fUsedProcessorFeatures; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 186 | bool fHasReadDstColor; |
| cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 187 | #endif |
| joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 188 | |
| egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 189 | friend class GrGLSLProgramBuilder; |
| joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 190 | friend class GrGLProgramBuilder; |
| joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | #endif |