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