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" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 22 | #include "GrGpu.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 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; |
junov@google.com | d31cbc4 | 2011-05-17 17:01:17 +0000 | [diff] [blame] | 27 | |
| 28 | struct ShaderCodeSegments { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 29 | GrStringBuilder fHeader; // VS+FS, GLSL version, etc |
junov@google.com | d31cbc4 | 2011-05-17 17:01:17 +0000 | [diff] [blame] | 30 | GrStringBuilder fVSUnis; |
| 31 | GrStringBuilder fVSAttrs; |
| 32 | GrStringBuilder fVaryings; |
| 33 | GrStringBuilder fFSUnis; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 34 | GrStringBuilder fFSOutputs; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 35 | GrStringBuilder fFSFunctions; |
junov@google.com | d31cbc4 | 2011-05-17 17:01:17 +0000 | [diff] [blame] | 36 | GrStringBuilder fVSCode; |
| 37 | GrStringBuilder fFSCode; |
| 38 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * This class manages a GPU program and records per-program information. |
| 42 | * We can specify the attribute locations so that they are constant |
| 43 | * across our shaders. But the driver determines the uniform locations |
| 44 | * at link time. We don't need to remember the sampler uniform location |
| 45 | * because we will bind a texture slot to it and never change it |
| 46 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 47 | * previous uniform state after a program change. |
| 48 | */ |
| 49 | class GrGLProgram { |
| 50 | public: |
| 51 | class CachedData; |
| 52 | |
| 53 | GrGLProgram(); |
| 54 | ~GrGLProgram(); |
| 55 | |
| 56 | /** |
| 57 | * Streams data that can uniquely identifies the generated |
| 58 | * gpu program into a key, for cache indexing purposes. |
| 59 | * |
| 60 | * @param key The key object to receive the key data |
| 61 | */ |
| 62 | void buildKey(GrBinHashKeyBuilder& key) const; |
| 63 | |
| 64 | /** |
| 65 | * This is the heavy initilization routine for building a GLProgram. |
| 66 | * The result of heavy init is not stored in datamembers of GrGLProgam, |
| 67 | * but in a separate cacheable container. |
| 68 | */ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 69 | bool genProgram(CachedData* programData) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 70 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 71 | /** |
| 72 | * The shader may modify the blend coeffecients. Params are in/out |
| 73 | */ |
| 74 | void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
| 75 | |
| 76 | /** |
| 77 | * Attribute indices |
| 78 | */ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 79 | static int PositionAttributeIdx() { return 0; } |
| 80 | static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; } |
| 81 | static int ColorAttributeIdx() { return 1 + GrDrawTarget::kMaxTexCoords; } |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 82 | static int ViewMatrixAttributeIdx() { |
| 83 | return 2 + GrDrawTarget::kMaxTexCoords; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 84 | } |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 85 | static int TextureMatrixAttributeIdx(int stage) { |
| 86 | return 5 + GrDrawTarget::kMaxTexCoords + 3 * stage; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 87 | } |
| 88 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 89 | private: |
| 90 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 91 | // Parameters that affect code generation |
| 92 | // These structs should be kept compact; they are the input to an |
| 93 | // expensive hash key generator. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 94 | struct ProgramDesc { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 95 | ProgramDesc() { |
| 96 | // since we use this as part of a key we can't have any unitialized |
| 97 | // padding |
| 98 | memset(this, 0, sizeof(ProgramDesc)); |
| 99 | } |
| 100 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 101 | struct StageDesc { |
| 102 | enum OptFlagBits { |
| 103 | kNoPerspective_OptFlagBit = 1 << 0, |
| 104 | kIdentityMatrix_OptFlagBit = 1 << 1, |
| 105 | kCustomTextureDomain_OptFlagBit = 1 << 2, |
| 106 | kIsEnabled_OptFlagBit = 1 << 7 |
| 107 | }; |
| 108 | enum Modulation { |
| 109 | kColor_Modulation, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 110 | kAlpha_Modulation, |
| 111 | |
| 112 | kModulationCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 113 | }; |
| 114 | enum FetchMode { |
| 115 | kSingle_FetchMode, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 116 | k2x2_FetchMode, |
| 117 | |
| 118 | kFetchModeCnt, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 119 | }; |
| 120 | enum CoordMapping { |
| 121 | kIdentity_CoordMapping, |
| 122 | kRadialGradient_CoordMapping, |
| 123 | kSweepGradient_CoordMapping, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 124 | kRadial2Gradient_CoordMapping, |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame^] | 125 | // need different shader computation when quadratic |
| 126 | // eq describing the gradient degenerates to a linear eq. |
| 127 | kRadial2GradientDegenerate_CoordMapping, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 128 | kCoordMappingCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 129 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 130 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 131 | uint8_t fOptFlags; |
| 132 | uint8_t fModulation; // casts to enum Modulation |
| 133 | uint8_t fFetchMode; // casts to enum FetchMode |
| 134 | uint8_t fCoordMapping; // casts to enum CoordMapping |
| 135 | |
| 136 | inline bool isEnabled() const { |
| 137 | return fOptFlags & kIsEnabled_OptFlagBit; |
| 138 | } |
| 139 | inline void setEnabled(bool newValue) { |
| 140 | if (newValue) { |
| 141 | fOptFlags |= kIsEnabled_OptFlagBit; |
| 142 | } else { |
| 143 | fOptFlags &= ~kIsEnabled_OptFlagBit; |
| 144 | } |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | enum ColorType { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 149 | kNone_ColorType = 0, |
| 150 | kAttribute_ColorType = 1, |
| 151 | kUniform_ColorType = 2, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 152 | }; |
| 153 | // Dual-src blending makes use of a secondary output color that can be |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 154 | // used as a per-pixel blend coeffecient. This controls whether a |
| 155 | // secondary source is output and what value it holds. |
| 156 | enum DualSrcOutput { |
| 157 | kNone_DualSrcOutput, |
| 158 | kCoverage_DualSrcOutput, |
| 159 | kCoverageISA_DualSrcOutput, |
| 160 | kCoverageISC_DualSrcOutput, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 161 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 162 | kDualSrcOutputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 163 | }; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 164 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 165 | // stripped of bits that don't affect prog generation |
| 166 | GrVertexLayout fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 167 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 168 | StageDesc fStages[GrDrawTarget::kNumStages]; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 169 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 170 | uint8_t fColorType; // casts to enum ColorType |
| 171 | uint8_t fDualSrcOutput; // casts to enum DualSrcOutput |
| 172 | int8_t fFirstCoverageStage; |
| 173 | SkBool8 fEmitsPointSize; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 174 | SkBool8 fEdgeAAConcave; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 175 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 176 | int8_t fEdgeAANumEdges; |
| 177 | uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 178 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 179 | uint8_t fPadTo32bLengthMultiple [1]; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 180 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 181 | } fProgramDesc; |
| 182 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 183 | const ProgramDesc& getDesc() { return fProgramDesc; } |
| 184 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame^] | 185 | // for code readability |
| 186 | typedef ProgramDesc::StageDesc StageDesc; |
| 187 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 188 | public: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 189 | enum { |
| 190 | kUnusedUniform = -1, |
| 191 | kSetAsAttribute = 1000, |
| 192 | }; |
| 193 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 194 | struct StageUniLocations { |
| 195 | GrGLint fTextureMatrixUni; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 196 | GrGLint fNormalizedTexelSizeUni; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 197 | GrGLint fSamplerUni; |
| 198 | GrGLint fRadial2Uni; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 199 | GrGLint fTexDomUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 200 | void reset() { |
| 201 | fTextureMatrixUni = kUnusedUniform; |
| 202 | fNormalizedTexelSizeUni = kUnusedUniform; |
| 203 | fSamplerUni = kUnusedUniform; |
| 204 | fRadial2Uni = kUnusedUniform; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 205 | fTexDomUni = kUnusedUniform; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 206 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | struct UniLocations { |
| 210 | GrGLint fViewMatrixUni; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 211 | GrGLint fColorUni; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 212 | GrGLint fEdgesUni; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 213 | GrGLint fColorFilterUni; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 214 | StageUniLocations fStages[GrDrawTarget::kNumStages]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 215 | void reset() { |
| 216 | fViewMatrixUni = kUnusedUniform; |
| 217 | fColorUni = kUnusedUniform; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 218 | fEdgesUni = kUnusedUniform; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 219 | fColorFilterUni = kUnusedUniform; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 220 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 221 | fStages[s].reset(); |
| 222 | } |
| 223 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | class CachedData : public ::GrNoncopyable { |
| 227 | public: |
| 228 | CachedData() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | ~CachedData() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void copyAndTakeOwnership(CachedData& other) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 235 | memcpy(this, &other, sizeof(*this)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 238 | public: |
| 239 | |
| 240 | // IDs |
| 241 | GrGLuint fVShaderID; |
| 242 | GrGLuint fFShaderID; |
| 243 | GrGLuint fProgramID; |
| 244 | // shader uniform locations (-1 if shader doesn't use them) |
| 245 | UniLocations fUniLocations; |
| 246 | |
| 247 | GrMatrix fViewMatrix; |
| 248 | |
| 249 | // these reflect the current values of uniforms |
| 250 | // (GL uniform values travel with program) |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 251 | GrColor fColor; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 252 | GrColor fColorFilterColor; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 253 | GrMatrix fTextureMatrices[GrDrawTarget::kNumStages]; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 254 | // width and height used for normalized texel size |
| 255 | int fTextureWidth[GrDrawTarget::kNumStages]; |
| 256 | int fTextureHeight[GrDrawTarget::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 257 | GrScalar fRadial2CenterX1[GrDrawTarget::kNumStages]; |
| 258 | GrScalar fRadial2Radius0[GrDrawTarget::kNumStages]; |
| 259 | bool fRadial2PosRoot[GrDrawTarget::kNumStages]; |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 260 | GrRect fTextureDomain[GrDrawTarget::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 261 | |
| 262 | private: |
| 263 | enum Constants { |
| 264 | kUniLocationPreAllocSize = 8 |
| 265 | }; |
| 266 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 267 | }; // CachedData |
| 268 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 269 | private: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 270 | enum { |
| 271 | kUseUniform = 2000 |
| 272 | }; |
| 273 | |
| 274 | // should set all fields in locations var to kUseUniform if the |
| 275 | // corresponding uniform is required for the program. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 276 | void genStageCode(int stageNum, |
| 277 | const ProgramDesc::StageDesc& desc, |
| 278 | const char* fsInColor, // NULL means no incoming color |
| 279 | const char* fsOutColor, |
| 280 | const char* vsInCoord, |
| 281 | ShaderCodeSegments* segments, |
| 282 | StageUniLocations* locations) const; |
| 283 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 284 | static bool CompileFSAndVS(const ShaderCodeSegments& segments, |
| 285 | CachedData* programData); |
| 286 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 287 | // Compiles a GL shader, returns shader ID or 0 if failed |
| 288 | // params have same meaning as glShaderSource |
| 289 | static GrGLuint CompileShader(GrGLenum type, int stringCnt, |
| 290 | const char** strings, |
| 291 | int* stringLengths); |
| 292 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 293 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and |
| 294 | // links the program |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 295 | bool bindOutputsAttribsAndLinkProgram( |
| 296 | GrStringBuilder texCoordAttrNames[GrDrawTarget::kMaxTexCoords], |
| 297 | bool bindColorOut, |
| 298 | bool bindDualSrcOut, |
| 299 | CachedData* programData) const; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 300 | |
| 301 | // Gets locations for all uniforms set to kUseUniform and initializes cache |
| 302 | // to invalid values. |
| 303 | void getUniformLocationsAndInitCache(CachedData* programData) const; |
| 304 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 305 | friend class GrGpuGLShaders; |
| 306 | }; |
| 307 | |
| 308 | #endif |