tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 GrGLShaderBuilder_DEFINED |
| 9 | #define GrGLShaderBuilder_DEFINED |
| 10 | |
| 11 | #include "GrAllocator.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 12 | #include "GrBackendEffectFactory.h" |
bsalomon@google.com | b8eb2e8 | 2013-03-28 13:46:42 +0000 | [diff] [blame] | 13 | #include "GrColor.h" |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 14 | #include "GrEffect.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 15 | #include "SkTypes.h" |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 16 | #include "gl/GrGLProgramEffects.h" |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 17 | #include "gl/GrGLSL.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 18 | #include "gl/GrGLUniformManager.h" |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 22 | class GrGLContextInfo; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 23 | class GrEffectStage; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 24 | class GrGLProgramDesc; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 25 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 26 | /** |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 27 | Contains all the incremental state of a shader as it is being built,as well as helpers to |
| 28 | manipulate that state. |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 29 | */ |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 30 | class GrGLShaderBuilder { |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 31 | public: |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 32 | typedef GrTAllocator<GrGLShaderVar> VarArray; |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 33 | typedef GrBackendEffectFactory::EffectKey EffectKey; |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 34 | typedef GrGLProgramEffects::TextureSampler TextureSampler; |
| 35 | typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 36 | typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 38 | enum ShaderVisibility { |
| 39 | kVertex_Visibility = 0x1, |
| 40 | kGeometry_Visibility = 0x2, |
| 41 | kFragment_Visibility = 0x4, |
bsalomon@google.com | eb715c8 | 2012-07-11 15:03:31 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 44 | GrGLShaderBuilder(GrGpuGL*, GrGLUniformManager&, const GrGLProgramDesc&); |
| 45 | virtual ~GrGLShaderBuilder() {} |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 46 | |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 47 | /** |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 48 | * Use of these features may require a GLSL extension to be enabled. Shaders may not compile |
| 49 | * if code is added that uses one of these features without calling enableFeature() |
| 50 | */ |
| 51 | enum GLSLFeature { |
| 52 | kStandardDerivatives_GLSLFeature = 0, |
| 53 | |
| 54 | kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * If the feature is supported then true is returned and any necessary #extension declarations |
| 59 | * are added to the shaders. If the feature is not supported then false will be returned. |
| 60 | */ |
| 61 | bool enableFeature(GLSLFeature); |
| 62 | |
| 63 | /** |
commit-bot@chromium.org | 5a02cb4 | 2013-08-30 20:17:31 +0000 | [diff] [blame] | 64 | * Called by GrGLEffects to add code the fragment shader. |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 65 | */ |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 66 | void fsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 67 | va_list args; |
| 68 | va_start(args, format); |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 69 | fFSCode.appendf(format, args); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 70 | va_end(args); |
| 71 | } |
| 72 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 73 | void fsCodeAppend(const char* str) { fFSCode.append(str); } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 75 | /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or |
| 76 | Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle |
| 77 | order of the result depends on the GrTextureAccess associated with the TextureSampler. */ |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 78 | void appendTextureLookup(SkString* out, |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 79 | const TextureSampler&, |
bsalomon@google.com | dbe49f7 | 2012-11-05 16:36:02 +0000 | [diff] [blame] | 80 | const char* coordName, |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 81 | GrSLType coordType = kVec2f_GrSLType) const; |
tomhudson@google.com | 5259814 | 2012-05-24 17:44:30 +0000 | [diff] [blame] | 82 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 83 | /** Version of above that appends the result to the fragment shader code instead.*/ |
| 84 | void fsAppendTextureLookup(const TextureSampler&, |
| 85 | const char* coordName, |
| 86 | GrSLType coordType = kVec2f_GrSLType); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 87 | |
| 88 | |
bsalomon@google.com | 2d8edaf | 2012-09-07 14:47:31 +0000 | [diff] [blame] | 89 | /** Does the work of appendTextureLookup and modulates the result by modulation. The result is |
| 90 | always a vec4. modulation and the swizzle specified by TextureSampler must both be vec4 or |
| 91 | float. If modulation is "" or NULL it this function acts as though appendTextureLookup were |
| 92 | called. */ |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 93 | void fsAppendTextureLookupAndModulate(const char* modulation, |
| 94 | const TextureSampler&, |
| 95 | const char* coordName, |
| 96 | GrSLType coordType = kVec2f_GrSLType); |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 98 | /** Emits a helper function outside of main() in the fragment shader. */ |
| 99 | void fsEmitFunction(GrSLType returnType, |
| 100 | const char* name, |
| 101 | int argCnt, |
| 102 | const GrGLShaderVar* args, |
| 103 | const char* body, |
| 104 | SkString* outName); |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 105 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 106 | typedef uint8_t DstReadKey; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 107 | typedef uint8_t FragPosKey; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 108 | |
| 109 | /** Returns a key for adding code to read the copy-of-dst color in service of effects that |
| 110 | require reading the dst. It must not return 0 because 0 indicates that there is no dst |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 111 | copy read at all (in which case this function should not be called). */ |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 112 | static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 113 | |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 114 | /** Returns a key for reading the fragment location. This should only be called if there is an |
| 115 | effect that will requires the fragment position. If the fragment position is not required, |
| 116 | the key is 0. */ |
| 117 | static FragPosKey KeyForFragmentPosition(const GrRenderTarget* dst, const GrGLCaps&); |
| 118 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 119 | /** If texture swizzling is available using tex parameters then it is preferred over mangling |
| 120 | the generated shader code. This potentially allows greater reuse of cached shaders. */ |
| 121 | static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCaps& caps); |
| 122 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 123 | /** Add a uniform variable to the current program, that has visibility in one or more shaders. |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 124 | visibility is a bitfield of ShaderVisibility values indicating from which shaders the |
| 125 | uniform should be accessible. At least one bit must be set. Geometry shader uniforms are not |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 126 | supported at this time. The actual uniform name will be mangled. If outName is not NULL then |
| 127 | it will refer to the final uniform name after return. Use the addUniformArray variant to add |
| 128 | an array of uniforms. |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 129 | */ |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 130 | GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, |
| 131 | GrSLType type, |
| 132 | const char* name, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 133 | const char** outName = NULL) { |
| 134 | return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNonArray, outName); |
| 135 | } |
| 136 | GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, |
| 137 | GrSLType type, |
| 138 | const char* name, |
| 139 | int arrayCount, |
| 140 | const char** outName = NULL); |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 141 | |
commit-bot@chromium.org | 7425c12 | 2013-08-14 18:14:19 +0000 | [diff] [blame] | 142 | const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle u) const { |
| 143 | return fUniformManager.getBuilderUniform(fUniforms, u).fVariable; |
| 144 | } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 145 | |
| 146 | /** |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 147 | * Shortcut for getUniformVariable(u).c_str() |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 148 | */ |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 149 | const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 150 | return this->getUniformVariable(u).c_str(); |
| 151 | } |
tomhudson@google.com | 242ed6f | 2012-05-30 17:38:57 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 153 | /** |
| 154 | * This returns a variable name to access the 2D, perspective correct version of the coords in |
| 155 | * the fragment shader. If the coordinates at index are 3-dimensional, it immediately emits a |
| 156 | * perspective divide into the fragment shader (xy / z) to convert them to 2D. |
| 157 | */ |
| 158 | SkString ensureFSCoords2D(const TransformedCoordsArray&, int index); |
| 159 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 160 | /** Returns a variable name that represents the position of the fragment in the FS. The position |
| 161 | is in device space (e.g. 0,0 is the top left and pixel centers are at half-integers). */ |
| 162 | const char* fragmentPosition(); |
| 163 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 164 | /** Returns the color of the destination pixel. This may be NULL if no effect advertised |
| 165 | that it will read the destination. */ |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 166 | const char* dstColor(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 167 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 168 | /** |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 169 | * Interfaces used by GrGLProgram. |
bsalomon@google.com | 34cccde | 2013-01-04 18:34:30 +0000 | [diff] [blame] | 170 | */ |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 171 | const GrGLSLExpr4& getInputColor() const { |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 172 | return fInputColor; |
| 173 | } |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 174 | const GrGLSLExpr4& getInputCoverage() const { |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 175 | return fInputCoverage; |
| 176 | } |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 178 | /** |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 179 | * Adds code for effects and returns a GrGLProgramEffects* object. The caller is responsible for |
| 180 | * deleting it when finished. effectStages contains the effects to add. effectKeys[i] is the key |
| 181 | * generated from effectStages[i]. inOutFSColor specifies the input color to the first stage and |
commit-bot@chromium.org | 824c346 | 2013-10-10 06:30:18 +0000 | [diff] [blame] | 182 | * is updated to be the output color of the last stage. |
| 183 | * The handles to texture samplers for effectStage[i] are added to |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 184 | * effectSamplerHandles[i]. |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 185 | */ |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 186 | virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effectStages[], |
| 187 | const EffectKey effectKeys[], |
| 188 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 189 | GrGLSLExpr4* inOutFSColor) = 0; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 190 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 191 | const char* getColorOutputName() const; |
| 192 | const char* enableSecondaryOutput(); |
| 193 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 194 | GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHeightUniform; } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 195 | GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { |
| 196 | return fDstCopyTopLeftUniform; |
| 197 | } |
| 198 | GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { |
| 199 | return fDstCopyScaleUniform; |
| 200 | } |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 201 | GrGLUniformManager::UniformHandle getColorUniform() const { return fColorUniform; } |
| 202 | GrGLUniformManager::UniformHandle getCoverageUniform() const { return fCoverageUniform; } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 203 | GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { |
commit-bot@chromium.org | 3390b9a | 2013-10-03 15:17:58 +0000 | [diff] [blame] | 204 | return fDstCopySamplerUniform; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 205 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 206 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 207 | bool finish(GrGLuint* outProgramId); |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 208 | |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 209 | const GrGLContextInfo& ctxInfo() const; |
robertphillips@google.com | 13f181f | 2013-03-02 12:02:08 +0000 | [diff] [blame] | 210 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 211 | protected: |
| 212 | GrGpuGL* gpu() const { return fGpu; } |
| 213 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 214 | void setInputColor(const GrGLSLExpr4& inputColor) { fInputColor = inputColor; } |
| 215 | void setInputCoverage(const GrGLSLExpr4& inputCoverage) { fInputCoverage = inputCoverage; } |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 216 | |
| 217 | /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */ |
| 218 | GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } |
| 219 | |
| 220 | // Generates a name for a variable. The generated string will be name prefixed by the prefix |
| 221 | // char (unless the prefix is '\0'). It also mangles the name to be stage-specific if we're |
| 222 | // generating stage code. |
| 223 | void nameVariable(SkString* out, char prefix, const char* name); |
| 224 | |
| 225 | // Helper for emitEffects(). |
| 226 | void createAndEmitEffects(GrGLProgramEffectsBuilder*, |
| 227 | const GrEffectStage* effectStages[], |
| 228 | const EffectKey effectKeys[], |
| 229 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 230 | GrGLSLExpr4* inOutFSColor); |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 231 | |
| 232 | virtual bool compileAndAttachShaders(GrGLuint programId) const; |
| 233 | virtual void bindProgramLocations(GrGLuint programId) const; |
| 234 | |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 235 | void appendDecls(const VarArray&, SkString*) const; |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 236 | void appendUniformDecls(ShaderVisibility, SkString*) const; |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 237 | |
bsalomon@google.com | ad5e937 | 2012-07-11 18:11:27 +0000 | [diff] [blame] | 238 | private: |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 239 | class CodeStage : public SkNoncopyable { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 240 | public: |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 241 | CodeStage() : fNextIndex(0), fCurrentIndex(-1), fEffectStage(NULL) {} |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 242 | |
| 243 | bool inStageCode() const { |
| 244 | this->validate(); |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 245 | return NULL != fEffectStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 246 | } |
| 247 | |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 248 | const GrEffectStage* effectStage() const { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 249 | this->validate(); |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 250 | return fEffectStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | int stageIndex() const { |
skia.committer@gmail.com | 0f20a3f | 2013-05-10 07:01:04 +0000 | [diff] [blame] | 254 | this->validate(); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 255 | return fCurrentIndex; |
| 256 | } |
| 257 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 258 | class AutoStageRestore : public SkNoncopyable { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 259 | public: |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 260 | AutoStageRestore(CodeStage* codeStage, const GrEffectStage* newStage) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 261 | SkASSERT(NULL != codeStage); |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 262 | fSavedIndex = codeStage->fCurrentIndex; |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 263 | fSavedEffectStage = codeStage->fEffectStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 264 | |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 265 | if (NULL == newStage) { |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 266 | codeStage->fCurrentIndex = -1; |
| 267 | } else { |
| 268 | codeStage->fCurrentIndex = codeStage->fNextIndex++; |
| 269 | } |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 270 | codeStage->fEffectStage = newStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 271 | |
| 272 | fCodeStage = codeStage; |
| 273 | } |
| 274 | ~AutoStageRestore() { |
| 275 | fCodeStage->fCurrentIndex = fSavedIndex; |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 276 | fCodeStage->fEffectStage = fSavedEffectStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 277 | } |
| 278 | private: |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 279 | CodeStage* fCodeStage; |
| 280 | int fSavedIndex; |
| 281 | const GrEffectStage* fSavedEffectStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 282 | }; |
| 283 | private: |
commit-bot@chromium.org | a4acf12 | 2013-09-30 15:13:58 +0000 | [diff] [blame] | 284 | void validate() const { SkASSERT((NULL == fEffectStage) == (-1 == fCurrentIndex)); } |
| 285 | int fNextIndex; |
| 286 | int fCurrentIndex; |
| 287 | const GrEffectStage* fEffectStage; |
bsalomon@google.com | 504976e | 2013-05-09 13:45:02 +0000 | [diff] [blame] | 288 | } fCodeStage; |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 289 | |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 290 | /** |
| 291 | * Features that should only be enabled by GrGLShaderBuilder itself. |
| 292 | */ |
| 293 | enum GLSLPrivateFeature { |
bsalomon@google.com | 6b0cf02 | 2013-05-03 13:35:14 +0000 | [diff] [blame] | 294 | kFragCoordConventions_GLSLPrivateFeature = kLastGLSLFeature + 1, |
| 295 | kEXTShaderFramebufferFetch_GLSLPrivateFeature, |
| 296 | kNVShaderFramebufferFetch_GLSLPrivateFeature, |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 297 | }; |
| 298 | bool enablePrivateFeature(GLSLPrivateFeature); |
| 299 | |
commit-bot@chromium.org | 74a3a21 | 2013-08-30 19:43:59 +0000 | [diff] [blame] | 300 | // If we ever have VS/GS features we can expand this to take a bitmask of ShaderVisibility and |
| 301 | // track the enables separately for each shader. |
bsalomon@google.com | 42eff16 | 2013-04-02 12:50:49 +0000 | [diff] [blame] | 302 | void addFSFeature(uint32_t featureBit, const char* extensionName); |
| 303 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 304 | // Interpretation of DstReadKey when generating code |
| 305 | enum { |
| 306 | kNoDstRead_DstReadKey = 0, |
| 307 | kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 308 | kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only. |
| 309 | kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-left. |
| 310 | }; |
| 311 | |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 312 | enum { |
| 313 | kNoFragPosRead_FragPosKey = 0, // The fragment positition will not be needed. |
| 314 | kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to top-left. |
| 315 | kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to bottom-left. |
| 316 | }; |
| 317 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 318 | GrGpuGL* fGpu; |
| 319 | GrGLUniformManager& fUniformManager; |
| 320 | uint32_t fFSFeaturesAddedMask; |
| 321 | SkString fFSFunctions; |
| 322 | SkString fFSExtensions; |
| 323 | VarArray fFSInputs; |
| 324 | VarArray fFSOutputs; |
| 325 | GrGLUniformManager::BuilderUniformArray fUniforms; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 326 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 327 | SkString fFSCode; |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 328 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 329 | bool fSetupFragPosition; |
| 330 | GrGLUniformManager::UniformHandle fDstCopySamplerUniform; |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 331 | |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 332 | GrGLSLExpr4 fInputColor; |
| 333 | GrGLSLExpr4 fInputCoverage; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 334 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 335 | bool fHasCustomColorOutput; |
| 336 | bool fHasSecondaryOutput; |
commit-bot@chromium.org | 410552a | 2013-09-30 15:30:27 +0000 | [diff] [blame] | 337 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 338 | GrGLUniformManager::UniformHandle fRTHeightUniform; |
| 339 | GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; |
| 340 | GrGLUniformManager::UniformHandle fDstCopyScaleUniform; |
| 341 | GrGLUniformManager::UniformHandle fColorUniform; |
| 342 | GrGLUniformManager::UniformHandle fCoverageUniform; |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 343 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 344 | bool fTopLeftFragPosRead; |
| 345 | }; |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 346 | |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 347 | //////////////////////////////////////////////////////////////////////////////// |
| 348 | |
| 349 | class GrGLFullShaderBuilder : public GrGLShaderBuilder { |
| 350 | public: |
| 351 | GrGLFullShaderBuilder(GrGpuGL*, GrGLUniformManager&, const GrGLProgramDesc&); |
| 352 | |
| 353 | /** |
| 354 | * Called by GrGLEffects to add code to one of the shaders. |
| 355 | */ |
| 356 | void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 357 | va_list args; |
| 358 | va_start(args, format); |
| 359 | fVSCode.appendf(format, args); |
| 360 | va_end(args); |
| 361 | } |
| 362 | |
| 363 | void vsCodeAppend(const char* str) { fVSCode.append(str); } |
| 364 | |
| 365 | /** Add a vertex attribute to the current program that is passed in from the vertex data. |
| 366 | Returns false if the attribute was already there, true otherwise. */ |
| 367 | bool addAttribute(GrSLType type, const char* name); |
| 368 | |
| 369 | /** Add a varying variable to the current program to pass values between vertex and fragment |
| 370 | shaders. If the last two parameters are non-NULL, they are filled in with the name |
| 371 | generated. */ |
| 372 | void addVarying(GrSLType type, |
| 373 | const char* name, |
| 374 | const char** vsOutName = NULL, |
| 375 | const char** fsInName = NULL); |
| 376 | |
| 377 | /** Returns a vertex attribute that represents the vertex position in the VS. This is the |
| 378 | pre-matrix position and is commonly used by effects to compute texture coords via a matrix. |
| 379 | */ |
| 380 | const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } |
| 381 | |
| 382 | /** Returns a vertex attribute that represents the local coords in the VS. This may be the same |
| 383 | as positionAttribute() or it may not be. It depends upon whether the rendering code |
| 384 | specified explicit local coords or not in the GrDrawState. */ |
| 385 | const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar; } |
| 386 | |
| 387 | /** |
| 388 | * Are explicit local coordinates provided as input to the vertex shader. |
| 389 | */ |
| 390 | bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVar); } |
| 391 | |
| 392 | bool addEffectAttribute(int attributeIndex, GrSLType type, const SkString& name); |
| 393 | const SkString* getEffectAttributeName(int attributeIndex) const; |
| 394 | |
| 395 | virtual GrGLProgramEffects* createAndEmitEffects( |
| 396 | const GrEffectStage* effectStages[], |
| 397 | const EffectKey effectKeys[], |
| 398 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 399 | GrGLSLExpr4* inOutFSColor) SK_OVERRIDE; |
commit-bot@chromium.org | 261dc56 | 2013-10-04 15:42:56 +0000 | [diff] [blame] | 400 | |
| 401 | GrGLUniformManager::UniformHandle getViewMatrixUniform() const { |
| 402 | return fViewMatrixUniform; |
| 403 | } |
| 404 | |
| 405 | protected: |
| 406 | virtual bool compileAndAttachShaders(GrGLuint programId) const SK_OVERRIDE; |
| 407 | virtual void bindProgramLocations(GrGLuint programId) const SK_OVERRIDE; |
| 408 | |
| 409 | private: |
| 410 | const GrGLProgramDesc& fDesc; |
| 411 | VarArray fVSAttrs; |
| 412 | VarArray fVSOutputs; |
| 413 | VarArray fGSInputs; |
| 414 | VarArray fGSOutputs; |
| 415 | |
| 416 | SkString fVSCode; |
| 417 | |
| 418 | struct AttributePair { |
| 419 | void set(int index, const SkString& name) { |
| 420 | fIndex = index; fName = name; |
| 421 | } |
| 422 | int fIndex; |
| 423 | SkString fName; |
| 424 | }; |
| 425 | SkSTArray<10, AttributePair, true> fEffectAttributes; |
| 426 | |
| 427 | GrGLUniformManager::UniformHandle fViewMatrixUniform; |
| 428 | |
| 429 | GrGLShaderVar* fPositionVar; |
| 430 | GrGLShaderVar* fLocalCoordsVar; |
| 431 | |
| 432 | typedef GrGLShaderBuilder INHERITED; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 433 | }; |
| 434 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 435 | //////////////////////////////////////////////////////////////////////////////// |
| 436 | |
| 437 | class GrGLFragmentOnlyShaderBuilder : public GrGLShaderBuilder { |
| 438 | public: |
| 439 | GrGLFragmentOnlyShaderBuilder(GrGpuGL*, GrGLUniformManager&, const GrGLProgramDesc&); |
| 440 | |
| 441 | int getNumTexCoordSets() const { return fNumTexCoordSets; } |
commit-bot@chromium.org | 8e919ad | 2013-10-21 14:48:23 +0000 | [diff] [blame] | 442 | int addTexCoordSets(int count); |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 443 | |
| 444 | virtual GrGLProgramEffects* createAndEmitEffects( |
| 445 | const GrEffectStage* effectStages[], |
| 446 | const EffectKey effectKeys[], |
| 447 | int effectCnt, |
commit-bot@chromium.org | a34995e | 2013-10-23 05:42:03 +0000 | [diff] [blame] | 448 | GrGLSLExpr4* inOutFSColor) SK_OVERRIDE; |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 449 | |
| 450 | private: |
| 451 | int fNumTexCoordSets; |
| 452 | |
| 453 | typedef GrGLShaderBuilder INHERITED; |
| 454 | }; |
| 455 | |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 456 | #endif |