junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef GrGLProgram_DEFINED |
| 18 | #define GrGLProgram_DEFINED |
| 19 | |
| 20 | #include "GrGLInterface.h" |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 21 | #include "GrStringBuilder.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 22 | #include "GrDrawTarget.h" |
| 23 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 24 | #include "SkXfermode.h" |
| 25 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 26 | class GrBinHashKeyBuilder; |
| 27 | class GrGLEffect; |
| 28 | struct ShaderCodeSegments; |
| 29 | |
| 30 | /** |
| 31 | * This class manages a GPU program and records per-program information. |
| 32 | * We can specify the attribute locations so that they are constant |
| 33 | * across our shaders. But the driver determines the uniform locations |
| 34 | * at link time. We don't need to remember the sampler uniform location |
| 35 | * because we will bind a texture slot to it and never change it |
| 36 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 37 | * previous uniform state after a program change. |
| 38 | */ |
| 39 | class GrGLProgram { |
| 40 | public: |
| 41 | class CachedData; |
| 42 | |
| 43 | GrGLProgram(); |
| 44 | ~GrGLProgram(); |
| 45 | |
| 46 | /** |
| 47 | * Streams data that can uniquely identifies the generated |
| 48 | * gpu program into a key, for cache indexing purposes. |
| 49 | * |
| 50 | * @param key The key object to receive the key data |
| 51 | */ |
| 52 | void buildKey(GrBinHashKeyBuilder& key) const; |
| 53 | |
| 54 | /** |
| 55 | * This is the heavy initilization routine for building a GLProgram. |
| 56 | * The result of heavy init is not stored in datamembers of GrGLProgam, |
| 57 | * but in a separate cacheable container. |
| 58 | */ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 59 | bool genProgram(CachedData* programData) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Routine that is called before rendering. Sets-up all the state and |
| 63 | * other initializations required for the Gpu Program to run. |
| 64 | */ |
| 65 | bool doGLSetup(GrPrimitiveType type, CachedData* programData) const; |
| 66 | |
| 67 | /** |
| 68 | * Routine that is called after rendering. Performs state restoration. |
| 69 | * May perform secondary render passes. |
| 70 | */ |
| 71 | void doGLPost() const; |
| 72 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 73 | static int PositionAttributeIdx() { return 0; } |
| 74 | static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; } |
| 75 | static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; } |
| 76 | static int ViewMatrixAttributeIdx() { |
| 77 | return 2 + GrDrawTarget::kMaxTexCoords; |
| 78 | } |
| 79 | static int TextureMatrixAttributeIdx(int stage) { |
| 80 | return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage; |
| 81 | } |
| 82 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 83 | private: |
| 84 | |
| 85 | //Parameters that affect code generation |
| 86 | struct ProgramDesc { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 87 | ProgramDesc() { |
| 88 | // since we use this as part of a key we can't have any unitialized |
| 89 | // padding |
| 90 | memset(this, 0, sizeof(ProgramDesc)); |
| 91 | } |
| 92 | |
| 93 | // stripped of bits that don't affect prog generation |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 94 | GrVertexLayout fVertexLayout; |
| 95 | |
| 96 | enum { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 97 | kNone_ColorType = 0, |
| 98 | kAttribute_ColorType = 1, |
| 99 | kUniform_ColorType = 2, |
| 100 | } fColorType; |
| 101 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 102 | int fFirstCoverageStage; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 103 | bool fEmitsPointSize; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 104 | bool fUsesEdgeAA; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 105 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 106 | SkXfermode::Mode fColorFilterXfermode; |
| 107 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 108 | struct StageDesc { |
| 109 | enum OptFlagBits { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 110 | kNoPerspective_OptFlagBit = 1 << 0, |
| 111 | kIdentityMatrix_OptFlagBit = 1 << 1, |
| 112 | kCustomTextureDomain_OptFlagBit = 1 << 2 |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 115 | unsigned fOptFlags; |
| 116 | bool fEnabled; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 117 | |
| 118 | enum Modulation { |
| 119 | kColor_Modulation, |
| 120 | kAlpha_Modulation |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 121 | } fModulation; |
| 122 | |
| 123 | enum FetchMode { |
| 124 | kSingle_FetchMode, |
| 125 | k2x2_FetchMode |
| 126 | } fFetchMode; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 127 | |
| 128 | enum CoordMapping { |
| 129 | kIdentity_CoordMapping, |
| 130 | kRadialGradient_CoordMapping, |
| 131 | kSweepGradient_CoordMapping, |
| 132 | kRadial2Gradient_CoordMapping |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 133 | } fCoordMapping; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 134 | } fStages[GrDrawTarget::kNumStages]; |
| 135 | } fProgramDesc; |
| 136 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 137 | const ProgramDesc& getDesc() { return fProgramDesc; } |
| 138 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 139 | public: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 140 | enum { |
| 141 | kUnusedUniform = -1, |
| 142 | kSetAsAttribute = 1000, |
| 143 | }; |
| 144 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 145 | struct StageUniLocations { |
| 146 | GrGLint fTextureMatrixUni; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 147 | GrGLint fNormalizedTexelSizeUni; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 148 | GrGLint fSamplerUni; |
| 149 | GrGLint fRadial2Uni; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 150 | GrGLint fTexDomUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 151 | void reset() { |
| 152 | fTextureMatrixUni = kUnusedUniform; |
| 153 | fNormalizedTexelSizeUni = kUnusedUniform; |
| 154 | fSamplerUni = kUnusedUniform; |
| 155 | fRadial2Uni = kUnusedUniform; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 156 | fTexDomUni = kUnusedUniform; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 157 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | struct UniLocations { |
| 161 | GrGLint fViewMatrixUni; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 162 | GrGLint fColorUni; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 163 | GrGLint fEdgesUni; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 164 | GrGLint fColorFilterUni; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 165 | StageUniLocations fStages[GrDrawTarget::kNumStages]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 166 | void reset() { |
| 167 | fViewMatrixUni = kUnusedUniform; |
| 168 | fColorUni = kUnusedUniform; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 169 | fEdgesUni = kUnusedUniform; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 170 | fColorFilterUni = kUnusedUniform; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 171 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 172 | fStages[s].reset(); |
| 173 | } |
| 174 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | class CachedData : public ::GrNoncopyable { |
| 178 | public: |
| 179 | CachedData() { |
| 180 | GR_DEBUGCODE(fEffectUniCount = 0;) |
| 181 | fEffectUniLocationsExtended = NULL; |
| 182 | } |
| 183 | |
| 184 | ~CachedData() { |
| 185 | GrFree(fEffectUniLocationsExtended); |
| 186 | } |
| 187 | |
| 188 | void copyAndTakeOwnership(CachedData& other) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 189 | memcpy(this, &other, sizeof(*this)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 190 | other.fEffectUniLocationsExtended = NULL; // ownership transfer |
| 191 | GR_DEBUGCODE(other.fEffectUniCount = 0;) |
| 192 | } |
| 193 | |
| 194 | void setEffectUniformCount(size_t effectUniforms) { |
| 195 | GR_DEBUGCODE(fEffectUniCount = effectUniforms;) |
| 196 | GrFree(fEffectUniLocationsExtended); |
| 197 | if (effectUniforms > kUniLocationPreAllocSize) { |
| 198 | fEffectUniLocationsExtended = (GrGLint*)GrMalloc(sizeof(GrGLint)*(effectUniforms-kUniLocationPreAllocSize)); |
| 199 | } else { |
| 200 | fEffectUniLocationsExtended = NULL; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | GrGLint& effectUniLocation(size_t index) { |
| 205 | GrAssert(index < fEffectUniCount); |
| 206 | return (index < kUniLocationPreAllocSize) ? |
| 207 | fEffectUniLocations[index] : |
| 208 | fEffectUniLocationsExtended[index - kUniLocationPreAllocSize]; |
| 209 | } |
| 210 | |
| 211 | public: |
| 212 | |
| 213 | // IDs |
| 214 | GrGLuint fVShaderID; |
| 215 | GrGLuint fFShaderID; |
| 216 | GrGLuint fProgramID; |
| 217 | // shader uniform locations (-1 if shader doesn't use them) |
| 218 | UniLocations fUniLocations; |
| 219 | |
| 220 | GrMatrix fViewMatrix; |
| 221 | |
| 222 | // these reflect the current values of uniforms |
| 223 | // (GL uniform values travel with program) |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 224 | GrColor fColor; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 225 | GrColor fColorFilterColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 226 | GrMatrix fTextureMatrices[GrDrawTarget::kNumStages]; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 227 | // width and height used for normalized texel size |
| 228 | int fTextureWidth[GrDrawTarget::kNumStages]; |
| 229 | int fTextureHeight[GrDrawTarget::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 230 | GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages]; |
| 231 | GrScalar fRadial2Radius0[GrDrawTarget::kNumStages]; |
| 232 | bool fRadial2PosRoot[GrDrawTarget::kNumStages]; |
| 233 | |
| 234 | private: |
| 235 | enum Constants { |
| 236 | kUniLocationPreAllocSize = 8 |
| 237 | }; |
| 238 | |
| 239 | GrGLint fEffectUniLocations[kUniLocationPreAllocSize]; |
| 240 | GrGLint* fEffectUniLocationsExtended; |
| 241 | GR_DEBUGCODE(size_t fEffectUniCount;) |
| 242 | }; // CachedData |
| 243 | |
| 244 | GrGLEffect* fStageEffects[GrDrawTarget::kNumStages]; |
| 245 | |
| 246 | private: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 247 | enum { |
| 248 | kUseUniform = 2000 |
| 249 | }; |
| 250 | |
| 251 | // should set all fields in locations var to kUseUniform if the |
| 252 | // corresponding uniform is required for the program. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 253 | void genStageCode(int stageNum, |
| 254 | const ProgramDesc::StageDesc& desc, |
| 255 | const char* fsInColor, // NULL means no incoming color |
| 256 | const char* fsOutColor, |
| 257 | const char* vsInCoord, |
| 258 | ShaderCodeSegments* segments, |
| 259 | StageUniLocations* locations) const; |
| 260 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 261 | static bool CompileFSAndVS(const ShaderCodeSegments& segments, |
| 262 | CachedData* programData); |
| 263 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 264 | // Compiles a GL shader, returns shader ID or 0 if failed |
| 265 | // params have same meaning as glShaderSource |
| 266 | static GrGLuint CompileShader(GrGLenum type, int stringCnt, |
| 267 | const char** strings, |
| 268 | int* stringLengths); |
| 269 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 270 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and |
| 271 | // links the program |
| 272 | bool bindAttribsAndLinkProgram(GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords], |
| 273 | CachedData* programData) const; |
| 274 | |
| 275 | // Gets locations for all uniforms set to kUseUniform and initializes cache |
| 276 | // to invalid values. |
| 277 | void getUniformLocationsAndInitCache(CachedData* programData) const; |
| 278 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 279 | friend class GrGpuGLShaders; |
| 280 | }; |
| 281 | |
| 282 | #endif |