epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #ifndef GrGLProgram_DEFINED |
| 11 | #define GrGLProgram_DEFINED |
| 12 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 13 | #include "GrDrawState.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 14 | #include "GrGLInterface.h" |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 15 | #include "GrStringBuilder.h" |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 16 | #include "GrGpu.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 18 | #include "SkXfermode.h" |
| 19 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 20 | class GrBinHashKeyBuilder; |
junov@google.com | d31cbc4 | 2011-05-17 17:01:17 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 22 | struct ShaderCodeSegments; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 24 | // optionally compile the experimental GS code. Set to GR_DEBUG |
| 25 | // so that debug build bots will execute the code. |
| 26 | #define GR_GL_EXPERIMENTAL_GS GR_DEBUG |
| 27 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * This class manages a GPU program and records per-program information. |
| 30 | * We can specify the attribute locations so that they are constant |
| 31 | * across our shaders. But the driver determines the uniform locations |
| 32 | * at link time. We don't need to remember the sampler uniform location |
| 33 | * because we will bind a texture slot to it and never change it |
| 34 | * Uniforms are program-local so we can't rely on fHWState to hold the |
| 35 | * previous uniform state after a program change. |
| 36 | */ |
| 37 | class GrGLProgram { |
| 38 | public: |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 39 | // Limited set of GLSL versions we build shaders for. Caller should round |
| 40 | // down the GLSL version to one of these enums. |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 41 | enum GLSLVersion { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Desktop GLSL 1.10 and ES2 shading lang (based on desktop GLSL 1.20) |
| 44 | */ |
| 45 | k110_GLSLVersion, |
| 46 | /** |
| 47 | * Desktop GLSL 1.30 |
| 48 | */ |
| 49 | k130_GLSLVersion, |
| 50 | /** |
| 51 | * Dekstop GLSL 1.50 |
| 52 | */ |
| 53 | k150_GLSLVersion, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 56 | class CachedData; |
| 57 | |
| 58 | GrGLProgram(); |
| 59 | ~GrGLProgram(); |
| 60 | |
| 61 | /** |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 62 | * This is the heavy initilization routine for building a GLProgram. |
| 63 | * The result of heavy init is not stored in datamembers of GrGLProgam, |
| 64 | * but in a separate cacheable container. |
| 65 | */ |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 66 | bool genProgram(const GrGLInterface* gl, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 67 | GLSLVersion glslVersion, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 68 | CachedData* programData) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 69 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 70 | /** |
| 71 | * The shader may modify the blend coeffecients. Params are in/out |
| 72 | */ |
| 73 | void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
| 74 | |
| 75 | /** |
bsalomon@google.com | b5b5eaf | 2011-10-19 13:25:46 +0000 | [diff] [blame] | 76 | * Attribute indices. These should not overlap. Matrices consume 3 slots. |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 77 | */ |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 78 | static int PositionAttributeIdx() { return 0; } |
| 79 | static int TexCoordAttributeIdx(int tcIdx) { return 1 + tcIdx; } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 80 | static int ColorAttributeIdx() { return 1 + GrDrawState::kMaxTexCoords; } |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 81 | static int CoverageAttributeIdx() { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 82 | return 2 + GrDrawState::kMaxTexCoords; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 83 | } |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 84 | static int EdgeAttributeIdx() { return 3 + GrDrawState::kMaxTexCoords; } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 85 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 86 | static int ViewMatrixAttributeIdx() { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 87 | return 4 + GrDrawState::kMaxTexCoords; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 88 | } |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 89 | static int TextureMatrixAttributeIdx(int stage) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 90 | return 7 + GrDrawState::kMaxTexCoords + 3 * stage; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 93 | public: |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 94 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 95 | // Parameters that affect code generation |
| 96 | // These structs should be kept compact; they are the input to an |
| 97 | // expensive hash key generator. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 98 | struct ProgramDesc { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 99 | ProgramDesc() { |
| 100 | // since we use this as part of a key we can't have any unitialized |
| 101 | // padding |
| 102 | memset(this, 0, sizeof(ProgramDesc)); |
| 103 | } |
| 104 | |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 105 | enum OutputPM { |
| 106 | // PM-color OR color with no alpha channel |
| 107 | kYes_OutputPM, |
| 108 | // nonPM-color with alpha channel |
| 109 | kNo_OutputPM, |
| 110 | |
| 111 | kOutputPMCnt |
| 112 | }; |
| 113 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 114 | struct StageDesc { |
| 115 | enum OptFlagBits { |
| 116 | kNoPerspective_OptFlagBit = 1 << 0, |
| 117 | kIdentityMatrix_OptFlagBit = 1 << 1, |
| 118 | kCustomTextureDomain_OptFlagBit = 1 << 2, |
| 119 | kIsEnabled_OptFlagBit = 1 << 7 |
| 120 | }; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 121 | enum FetchMode { |
| 122 | kSingle_FetchMode, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 123 | k2x2_FetchMode, |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 124 | kConvolution_FetchMode, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 125 | |
| 126 | kFetchModeCnt, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 127 | }; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 128 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 129 | Flags set based on a src texture's pixel config. The operations |
| 130 | described are performed after reading a texel. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 131 | */ |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 132 | enum InConfigFlags { |
| 133 | kNone_InConfigFlag = 0x0, |
| 134 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 135 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 136 | Swap the R and B channels. This is incompatible with |
| 137 | kSmearAlpha. It is prefereable to perform the swizzle outside |
| 138 | the shader using GL_ARB_texture_swizzle if possible rather |
| 139 | than setting this flag. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 140 | */ |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 141 | kSwapRAndB_InConfigFlag = 0x1, |
| 142 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 143 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 144 | Smear alpha across all four channels. This is incompatible with |
| 145 | kSwapRAndB and kPremul. It is prefereable to perform the |
| 146 | smear outside the shader using GL_ARB_texture_swizzle if |
| 147 | possible rather than setting this flag. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 148 | */ |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 149 | kSmearAlpha_InConfigFlag = 0x2, |
| 150 | |
| 151 | /** |
| 152 | Multiply r,g,b by a after texture reads. This flag incompatible |
| 153 | with kSmearAlpha and may only be used with FetchMode kSingle. |
| 154 | */ |
| 155 | kMulRGBByAlpha_InConfigFlag = 0x4, |
| 156 | |
| 157 | kDummyInConfigFlag, |
| 158 | kInConfigBitMask = (kDummyInConfigFlag-1) | |
| 159 | (kDummyInConfigFlag-2) |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 160 | }; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 161 | enum CoordMapping { |
| 162 | kIdentity_CoordMapping, |
| 163 | kRadialGradient_CoordMapping, |
| 164 | kSweepGradient_CoordMapping, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 165 | kRadial2Gradient_CoordMapping, |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 166 | // need different shader computation when quadratic |
| 167 | // eq describing the gradient degenerates to a linear eq. |
| 168 | kRadial2GradientDegenerate_CoordMapping, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 169 | kCoordMappingCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 170 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 171 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 172 | uint8_t fOptFlags; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 173 | uint8_t fInConfigFlags; // bitfield of InConfigFlags values |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 174 | uint8_t fFetchMode; // casts to enum FetchMode |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 175 | uint8_t fCoordMapping; // casts to enum CoordMapping |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 176 | uint8_t fKernelWidth; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 178 | GR_STATIC_ASSERT((InConfigFlags)(uint8_t)kInConfigBitMask == |
| 179 | kInConfigBitMask); |
| 180 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 181 | inline bool isEnabled() const { |
bsalomon@google.com | c2c9b97 | 2011-10-03 13:17:22 +0000 | [diff] [blame] | 182 | return SkToBool(fOptFlags & kIsEnabled_OptFlagBit); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 183 | } |
| 184 | inline void setEnabled(bool newValue) { |
| 185 | if (newValue) { |
| 186 | fOptFlags |= kIsEnabled_OptFlagBit; |
| 187 | } else { |
| 188 | fOptFlags &= ~kIsEnabled_OptFlagBit; |
| 189 | } |
| 190 | } |
| 191 | }; |
| 192 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 193 | // Specifies where the intitial color comes from before the stages are |
| 194 | // applied. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 195 | enum ColorInput { |
| 196 | kSolidWhite_ColorInput, |
| 197 | kTransBlack_ColorInput, |
| 198 | kAttribute_ColorInput, |
| 199 | kUniform_ColorInput, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 200 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 201 | kColorInputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 202 | }; |
| 203 | // 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] | 204 | // used as a per-pixel blend coeffecient. This controls whether a |
| 205 | // secondary source is output and what value it holds. |
| 206 | enum DualSrcOutput { |
| 207 | kNone_DualSrcOutput, |
| 208 | kCoverage_DualSrcOutput, |
| 209 | kCoverageISA_DualSrcOutput, |
| 210 | kCoverageISC_DualSrcOutput, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 211 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 212 | kDualSrcOutputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 213 | }; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 214 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 215 | GrDrawState::VertexEdgeType fVertexEdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 216 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 217 | // stripped of bits that don't affect prog generation |
| 218 | GrVertexLayout fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 219 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 220 | StageDesc fStages[GrDrawState::kNumStages]; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 222 | // To enable experimental geometry shader code (not for use in |
| 223 | // production) |
| 224 | #if GR_GL_EXPERIMENTAL_GS |
| 225 | bool fExperimentalGS; |
| 226 | #endif |
| 227 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 228 | uint8_t fColorInput; // casts to enum ColorInput |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 229 | uint8_t fOutputPM; // cases to enum OutputPM |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 230 | uint8_t fDualSrcOutput; // casts to enum DualSrcOutput |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 231 | int8_t fFirstCoverageStage; |
| 232 | SkBool8 fEmitsPointSize; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 233 | SkBool8 fEdgeAAConcave; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 234 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 235 | int8_t fEdgeAANumEdges; |
| 236 | uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 237 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 238 | } fProgramDesc; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 239 | GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 240 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 241 | // for code readability |
| 242 | typedef ProgramDesc::StageDesc StageDesc; |
| 243 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 244 | private: |
| 245 | |
| 246 | const ProgramDesc& getDesc() { return fProgramDesc; } |
| 247 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 248 | public: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 249 | enum { |
| 250 | kUnusedUniform = -1, |
| 251 | kSetAsAttribute = 1000, |
| 252 | }; |
| 253 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 254 | struct StageUniLocations { |
| 255 | GrGLint fTextureMatrixUni; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 256 | GrGLint fNormalizedTexelSizeUni; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 257 | GrGLint fSamplerUni; |
| 258 | GrGLint fRadial2Uni; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 259 | GrGLint fTexDomUni; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 260 | GrGLint fKernelUni; |
| 261 | GrGLint fImageIncrementUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 262 | void reset() { |
| 263 | fTextureMatrixUni = kUnusedUniform; |
| 264 | fNormalizedTexelSizeUni = kUnusedUniform; |
| 265 | fSamplerUni = kUnusedUniform; |
| 266 | fRadial2Uni = kUnusedUniform; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 267 | fTexDomUni = kUnusedUniform; |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 268 | fKernelUni = kUnusedUniform; |
| 269 | fImageIncrementUni = kUnusedUniform; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 270 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | struct UniLocations { |
| 274 | GrGLint fViewMatrixUni; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 275 | GrGLint fColorUni; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 276 | GrGLint fEdgesUni; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 277 | GrGLint fColorFilterUni; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 278 | StageUniLocations fStages[GrDrawState::kNumStages]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 279 | void reset() { |
| 280 | fViewMatrixUni = kUnusedUniform; |
| 281 | fColorUni = kUnusedUniform; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 282 | fEdgesUni = kUnusedUniform; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 283 | fColorFilterUni = kUnusedUniform; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 284 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 285 | fStages[s].reset(); |
| 286 | } |
| 287 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
| 290 | class CachedData : public ::GrNoncopyable { |
| 291 | public: |
| 292 | CachedData() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | ~CachedData() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void copyAndTakeOwnership(CachedData& other) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 299 | memcpy(this, &other, sizeof(*this)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 300 | } |
| 301 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 302 | public: |
| 303 | |
| 304 | // IDs |
| 305 | GrGLuint fVShaderID; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 306 | GrGLuint fGShaderID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 307 | GrGLuint fFShaderID; |
| 308 | GrGLuint fProgramID; |
| 309 | // shader uniform locations (-1 if shader doesn't use them) |
| 310 | UniLocations fUniLocations; |
| 311 | |
| 312 | GrMatrix fViewMatrix; |
| 313 | |
| 314 | // these reflect the current values of uniforms |
| 315 | // (GL uniform values travel with program) |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 316 | GrColor fColor; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 317 | GrColor fColorFilterColor; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 318 | GrMatrix fTextureMatrices[GrDrawState::kNumStages]; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 319 | // width and height used for normalized texel size |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 320 | int fTextureWidth[GrDrawState::kNumStages]; |
| 321 | int fTextureHeight[GrDrawState::kNumStages]; |
| 322 | GrScalar fRadial2CenterX1[GrDrawState::kNumStages]; |
| 323 | GrScalar fRadial2Radius0[GrDrawState::kNumStages]; |
| 324 | bool fRadial2PosRoot[GrDrawState::kNumStages]; |
| 325 | GrRect fTextureDomain[GrDrawState::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 326 | |
| 327 | private: |
| 328 | enum Constants { |
| 329 | kUniLocationPreAllocSize = 8 |
| 330 | }; |
| 331 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 332 | }; // CachedData |
| 333 | |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 334 | enum Constants { |
| 335 | kProgramKeySize = sizeof(ProgramDesc) |
| 336 | }; |
| 337 | |
| 338 | // Provide an opaque ProgramDesc |
| 339 | const uint32_t* keyData() const{ |
| 340 | return reinterpret_cast<const uint32_t*>(&fProgramDesc); |
| 341 | } |
| 342 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 343 | private: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 344 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 345 | // Determines which uniforms will need to be bound. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 346 | void genStageCode(const GrGLInterface* gl, |
| 347 | int stageNum, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 348 | const ProgramDesc::StageDesc& desc, |
| 349 | const char* fsInColor, // NULL means no incoming color |
| 350 | const char* fsOutColor, |
| 351 | const char* vsInCoord, |
| 352 | ShaderCodeSegments* segments, |
| 353 | StageUniLocations* locations) const; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 354 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 355 | void genGeometryShader(const GrGLInterface* gl, |
| 356 | GLSLVersion glslVersion, |
| 357 | ShaderCodeSegments* segments) const; |
| 358 | |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 359 | // generates code to compute coverage based on edge AA. |
| 360 | void genEdgeCoverage(const GrGLInterface* gl, |
| 361 | GrVertexLayout layout, |
| 362 | CachedData* programData, |
| 363 | GrStringBuilder* coverageVar, |
| 364 | ShaderCodeSegments* segments) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 366 | static bool CompileShaders(const GrGLInterface* gl, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 367 | GLSLVersion glslVersion, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 368 | const ShaderCodeSegments& segments, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 369 | CachedData* programData); |
| 370 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 371 | // Compiles a GL shader, returns shader ID or 0 if failed |
| 372 | // params have same meaning as glShaderSource |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 373 | static GrGLuint CompileShader(const GrGLInterface* gl, |
| 374 | GrGLenum type, int stringCnt, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 375 | const char** strings, |
| 376 | int* stringLengths); |
| 377 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 378 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and |
| 379 | // links the program |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 380 | bool bindOutputsAttribsAndLinkProgram( |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 381 | const GrGLInterface* gl, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 382 | GrStringBuilder texCoordAttrNames[GrDrawState::kMaxTexCoords], |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 383 | bool bindColorOut, |
| 384 | bool bindDualSrcOut, |
| 385 | CachedData* programData) const; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 386 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 387 | // Binds uniforms; initializes cache to invalid values. |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 388 | void getUniformLocationsAndInitCache(const GrGLInterface* gl, |
| 389 | CachedData* programData) const; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 390 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 391 | friend class GrGpuGLShaders; |
| 392 | }; |
| 393 | |
| 394 | #endif |