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