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 | 096b091 | 2019-08-14 16:56:13 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrBlend.h" |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrFragmentProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrProcessor.h" |
| 14 | #include "src/gpu/glsl/GrGLSLShaderBuilder.h" |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 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 class is used by fragment processors to build their fragment code. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 21 | */ |
Brian Salomon | ffd15ea | 2020-07-01 16:48:20 -0400 | [diff] [blame] | 22 | class GrGLSLFPFragmentBuilder : virtual public GrGLSLShaderBuilder { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 23 | public: |
Brian Salomon | ffd15ea | 2020-07-01 16:48:20 -0400 | [diff] [blame] | 24 | /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */ |
| 25 | GrGLSLFPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) { |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 26 | // Suppress unused warning error |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 27 | (void) fPadding; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 28 | } |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 29 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 30 | enum class ScopeFlags { |
| 31 | // Every fragment will always execute this code, and will do it exactly once. |
| 32 | kTopLevel = 0, |
| 33 | // Either all fragments in a given primitive, or none, will execute this code. |
| 34 | kInsidePerPrimitiveBranch = (1 << 0), |
| 35 | // Any given fragment may or may not execute this code. |
| 36 | kInsidePerPixelBranch = (1 << 1), |
| 37 | // This code will be executed more than once. |
| 38 | kInsideLoop = (1 << 2) |
Robert Phillips | 7f86192 | 2018-01-30 13:13:42 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 41 | virtual void forceHighPrecision() = 0; |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 42 | |
John Stiles | bb04e3d | 2021-06-04 12:09:11 -0400 | [diff] [blame] | 43 | /** Returns the variable name that holds the color of the destination pixel. This may be nullptr |
| 44 | * if no effect advertised that it will read the destination. */ |
| 45 | virtual const char* dstColor() = 0; |
| 46 | |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 47 | private: |
| 48 | // WARNING: LIke GrRenderTargetProxy, changes to this can cause issues in ASAN. This is caused |
Michael Ludwig | c1ed11d | 2021-08-24 11:49:55 -0400 | [diff] [blame] | 49 | // by GrGLSLProgramBuilder's SkTBlockLists requiring 16 byte alignment, but since |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 50 | // GrGLSLFragmentShaderBuilder has a virtual diamond hierarchy, ASAN requires all this pointers |
| 51 | // to start aligned, even though clang is already correctly offsetting the individual fields |
| 52 | // that require the larger alignment. In the current world, this extra padding is sufficient to |
| 53 | // correctly initialize GrGLSLXPFragmentBuilder second. |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 54 | char fPadding[4] = {}; |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
Brian Osman | 2421b99 | 2021-06-19 10:44:54 -0400 | [diff] [blame] | 57 | GR_MAKE_BITFIELD_CLASS_OPS(GrGLSLFPFragmentBuilder::ScopeFlags) |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 58 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 59 | /* |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 60 | * This class is used by Xfer processors to build their fragment code. |
| 61 | */ |
Brian Salomon | ffd15ea | 2020-07-01 16:48:20 -0400 | [diff] [blame] | 62 | class GrGLSLXPFragmentBuilder : virtual public GrGLSLShaderBuilder { |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 63 | public: |
Brian Salomon | ffd15ea | 2020-07-01 16:48:20 -0400 | [diff] [blame] | 64 | /** Appease the compiler; the derived class initializes GrGLSLShaderBuilder. */ |
| 65 | GrGLSLXPFragmentBuilder() : GrGLSLShaderBuilder(nullptr) {} |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 66 | |
| 67 | virtual bool hasCustomColorOutput() const = 0; |
| 68 | virtual bool hasSecondaryOutput() const = 0; |
| 69 | |
| 70 | /** Returns the variable name that holds the color of the destination pixel. This may be nullptr |
| 71 | * if no effect advertised that it will read the destination. */ |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 72 | virtual const char* dstColor() = 0; |
| 73 | |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 74 | /** Adds any necessary layout qualifiers in order to legalize the supplied blend equation with |
| 75 | this shader. It is only legal to call this method with an advanced blend equation, and only |
| 76 | if these equations are supported. */ |
| 77 | virtual void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) = 0; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 80 | /* |
| 81 | * This class implements the various fragment builder interfaces. |
| 82 | */ |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 83 | class GrGLSLFragmentShaderBuilder : public GrGLSLFPFragmentBuilder, public GrGLSLXPFragmentBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 84 | public: |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 85 | GrGLSLFragmentShaderBuilder(GrGLSLProgramBuilder* program); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 86 | |
John Stiles | bb04e3d | 2021-06-04 12:09:11 -0400 | [diff] [blame] | 87 | // Shared FP/XP interface. |
| 88 | const char* dstColor() override; |
| 89 | |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 90 | // GrGLSLFPFragmentBuilder interface. |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 91 | void forceHighPrecision() override { fForceHighPrecision = true; } |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 92 | |
| 93 | // GrGLSLXPFragmentBuilder interface. |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 94 | bool hasCustomColorOutput() const override { return SkToBool(fCustomColorOutput); } |
cdalton | 8528541 | 2016-02-18 12:37:07 -0800 | [diff] [blame] | 95 | bool hasSecondaryOutput() const override { return fHasSecondaryOutput; } |
cdalton | 8917d62 | 2015-05-06 13:40:21 -0700 | [diff] [blame] | 96 | void enableAdvancedBlendEquationIfNeeded(GrBlendEquation) override; |
| 97 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 98 | private: |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 99 | // Private public interface, used by GrGLProgramBuilder to build a fragment shader |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 100 | void enableCustomOutput(); |
| 101 | void enableSecondaryOutput(); |
| 102 | const char* getPrimaryColorOutputName() const; |
| 103 | const char* getSecondaryColorOutputName() const; |
Brian Salomon | dc09213 | 2018-04-04 10:14:16 -0400 | [diff] [blame] | 104 | bool primaryColorOutputIsInOut() const; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 105 | |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 106 | #ifdef SK_DEBUG |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 107 | // 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] | 108 | // state to track this. The reset call is called per processor emitted. |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 109 | bool fHasReadDstColorThisStage_DebugOnly = false; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 110 | |
| 111 | void debugOnly_resetPerStageVerification() { |
| 112 | fHasReadDstColorThisStage_DebugOnly = false; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 113 | } |
cdalton | 8733210 | 2016-02-26 12:22:02 -0800 | [diff] [blame] | 114 | #endif |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 115 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 116 | static const char* DeclaredColorOutputName() { return "sk_FragColor"; } |
egdaniel | 8dcdedc | 2015-11-11 06:27:20 -0800 | [diff] [blame] | 117 | static const char* DeclaredSecondaryColorOutputName() { return "fsSecondaryColorOut"; } |
| 118 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 119 | GrSurfaceOrigin getSurfaceOrigin() const; |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 120 | |
egdaniel | 574a4c1 | 2015-11-02 06:22:44 -0800 | [diff] [blame] | 121 | void onFinalize() override; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 122 | |
John Stiles | bb04e3d | 2021-06-04 12:09:11 -0400 | [diff] [blame] | 123 | static constexpr const char kDstColorName[] = "_dstColor"; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 124 | |
Michael Ludwig | 4519134 | 2020-03-24 12:29:39 -0400 | [diff] [blame] | 125 | GrShaderVar* fCustomColorOutput = nullptr; |
| 126 | |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 127 | bool fSetupFragPosition = false; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 128 | bool fHasSecondaryOutput = false; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 129 | bool fHasModifiedSampleMask = false; |
Chris Dalton | d7291ba | 2019-03-07 14:17:03 -0700 | [diff] [blame] | 130 | bool fForceHighPrecision = false; |
joshualitt | fe1233c | 2014-10-07 12:16:35 -0700 | [diff] [blame] | 131 | |
egdaniel | fa89632 | 2016-01-13 12:19:30 -0800 | [diff] [blame] | 132 | friend class GrGLSLProgramBuilder; |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 133 | friend class GrGLProgramBuilder; |
Ethan Nicholas | 8f352ce | 2021-03-17 14:12:20 -0400 | [diff] [blame] | 134 | friend class GrVkPipelineStateBuilder; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | #endif |