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