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 | |
| 8 | #ifndef GrGLFragmentShaderBuilder_DEFINED |
| 9 | #define GrGLFragmentShaderBuilder_DEFINED |
| 10 | #include "GrGLShaderBuilder.h" |
| 11 | |
| 12 | class GrGLProgramBuilder; |
| 13 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame^] | 14 | /* |
| 15 | * This base class encapsulates the functionality which all GrProcessors are allowed to use in their |
| 16 | * fragment shader |
| 17 | */ |
| 18 | class GrGLProcessorFragmentShaderBuilder : public GrGLShaderBuilder { |
| 19 | public: |
| 20 | GrGLProcessorFragmentShaderBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 21 | virtual ~GrGLProcessorFragmentShaderBuilder() {} |
| 22 | /** |
| 23 | * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
| 24 | * if code is added that uses one of these features without calling enableFeature() |
| 25 | */ |
| 26 | enum GLSLFeature { |
| 27 | kStandardDerivatives_GLSLFeature = 0, |
| 28 | kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * If the feature is supported then true is returned and any necessary #extension declarations |
| 33 | * are added to the shaders. If the feature is not supported then false will be returned. |
| 34 | */ |
| 35 | virtual bool enableFeature(GLSLFeature) = 0; |
| 36 | |
| 37 | /** |
| 38 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
| 39 | * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a |
| 40 | * perspective divide into the fragment shader (xy / z) to convert them to 2D. |
| 41 | */ |
| 42 | virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords, |
| 43 | int index) = 0; |
| 44 | |
| 45 | |
| 46 | /** Returns a variable name that represents the position of the fragment in the FS. The position |
| 47 | is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */ |
| 48 | virtual const char* fragmentPosition() = 0; |
| 49 | |
| 50 | private: |
| 51 | typedef GrGLShaderBuilder INHERITED; |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * Fragment processor's, in addition to all of the above, may need to use dst color so they use |
| 56 | * this builder to create their shader |
| 57 | */ |
| 58 | class GrGLFragmentProcessorShaderBuilder : public GrGLProcessorFragmentShaderBuilder { |
| 59 | public: |
| 60 | GrGLFragmentProcessorShaderBuilder(GrGLProgramBuilder* program) : INHERITED(program) {} |
| 61 | /** Returns the variable name that holds the color of the destination pixel. This may be NULL if |
| 62 | no effect advertised that it will read the destination. */ |
| 63 | virtual const char* dstColor() = 0; |
| 64 | |
| 65 | private: |
| 66 | typedef GrGLProcessorFragmentShaderBuilder INHERITED; |
| 67 | }; |
| 68 | |
| 69 | class GrGLFragmentShaderBuilder : public GrGLFragmentProcessorShaderBuilder { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 70 | public: |
| 71 | typedef uint8_t DstReadKey; |
| 72 | typedef uint8_t FragPosKey; |
| 73 | |
| 74 | /** Returns a key for adding code to read the copy-of-dst color in service of effects that |
| 75 | require reading the dst. It must not return 0 because 0 indicates that there is no dst |
| 76 | copy read at all (in which case this function should not be called). */ |
| 77 | static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 78 | |
| 79 | /** Returns a key for reading the fragment location. This should only be called if there is an |
| 80 | effect that will requires the fragment position. If the fragment position is not required, |
| 81 | the key is 0. */ |
| 82 | static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&); |
| 83 | |
| 84 | GrGLFragmentShaderBuilder(GrGLProgramBuilder* program, const GrGLProgramDesc& desc); |
| 85 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame^] | 86 | virtual const char* dstColor() SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 87 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame^] | 88 | virtual bool enableFeature(GLSLFeature) SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 89 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame^] | 90 | virtual SkString ensureFSCoords2D(const GrGLProcessor::TransformedCoordsArray& coords, |
| 91 | int index) SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 92 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame^] | 93 | virtual const char* fragmentPosition() SK_OVERRIDE; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | /* |
| 97 | * An internal call for GrGLFullProgramBuilder to use to add varyings to the vertex shader |
| 98 | */ |
| 99 | void addVarying(GrSLType type, |
| 100 | const char* name, |
egdaniel | 6db9128 | 2014-09-02 08:02:38 -0700 | [diff] [blame] | 101 | const char** fsInName, |
| 102 | GrGLShaderVar::Precision fsPrecision = GrGLShaderVar::kDefault_Precision); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Private functions used by GrGLProgramBuilder for compilation |
| 106 | */ |
| 107 | void bindProgramLocations(GrGLuint programId); |
| 108 | bool compileAndAttachShaders(GrGLuint programId, SkTDArray<GrGLuint>* shaderIds) const; |
| 109 | void emitCodeBeforeEffects(); |
| 110 | void emitCodeAfterEffects(const GrGLSLExpr4& inputColor, const GrGLSLExpr4& inputCoverage); |
| 111 | |
| 112 | /** Enables using the secondary color output and returns the name of the var in which it is |
| 113 | to be stored */ |
| 114 | const char* enableSecondaryOutput(); |
| 115 | |
| 116 | /** Gets the name of the primary color output. */ |
| 117 | const char* getColorOutputName() const; |
| 118 | |
| 119 | /** |
| 120 | * Features that should only be enabled by GrGLFragmentShaderBuilder itself. |
| 121 | */ |
| 122 | enum GLSLPrivateFeature { |
| 123 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 124 | kLastGLSLPrivateFeature = kFragCoordConventions_GLSLPrivateFeature |
| 125 | }; |
| 126 | |
| 127 | // Interpretation of DstReadKey when generating code |
| 128 | enum { |
| 129 | kNoDstRead_DstReadKey = 0, |
| 130 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 131 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 132 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 133 | }; |
| 134 | |
| 135 | enum { |
| 136 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
| 137 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
| 138 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
| 139 | }; |
| 140 | |
| 141 | bool fHasCustomColorOutput; |
| 142 | bool fHasSecondaryOutput; |
| 143 | bool fSetupFragPosition; |
| 144 | bool fTopLeftFragPosRead; |
| 145 | |
| 146 | friend class GrGLProgramBuilder; |
| 147 | friend class GrGLFullProgramBuilder; |
| 148 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame^] | 149 | typedef GrGLFragmentProcessorShaderBuilder INHERITED; |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | #endif |