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 | dd182cb | 2012-02-10 21:01:00 +0000 | [diff] [blame] | 13 | #include "../GrDrawState.h" |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 14 | #include "GrGLContextInfo.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 15 | #include "GrGLSL.h" |
tomhudson@google.com | dd182cb | 2012-02-10 21:01:00 +0000 | [diff] [blame] | 16 | #include "../GrStringBuilder.h" |
| 17 | #include "../GrGpu.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 18 | |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 19 | #include "SkXfermode.h" |
| 20 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 21 | class GrBinHashKeyBuilder; |
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, |
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 | }; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 109 | enum FetchMode { |
| 110 | kSingle_FetchMode, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 111 | k2x2_FetchMode, |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 112 | kConvolution_FetchMode, |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame^] | 113 | kErode_FetchMode, |
| 114 | kDilate_FetchMode, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 115 | |
| 116 | kFetchModeCnt, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 117 | }; |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 118 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 119 | Flags set based on a src texture's pixel config. The operations |
| 120 | described are performed after reading a texel. |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 121 | */ |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 122 | enum InConfigFlags { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 123 | kNone_InConfigFlag = 0x0, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 124 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 125 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 126 | Swap the R and B channels. This is incompatible with |
| 127 | kSmearAlpha. It is prefereable to perform the swizzle outside |
| 128 | the shader using GL_ARB_texture_swizzle if possible rather |
| 129 | than setting this flag. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 130 | */ |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 131 | kSwapRAndB_InConfigFlag = 0x1, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 132 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 133 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 134 | Smear alpha across all four channels. This is incompatible with |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 135 | kSwapRAndB and kMulRGBByAlpha*. It is prefereable to perform |
| 136 | the smear outside the shader using GL_ARB_texture_swizzle if |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 137 | possible rather than setting this flag. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 138 | */ |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 139 | kSmearAlpha_InConfigFlag = 0x2, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | Multiply r,g,b by a after texture reads. This flag incompatible |
| 143 | with kSmearAlpha and may only be used with FetchMode kSingle. |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 144 | |
| 145 | It is assumed the src texture has 8bit color components. After |
| 146 | reading the texture one version rounds up to the next multiple |
| 147 | of 1/255.0 and the other rounds down. At most one of these |
| 148 | flags may be set. |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 149 | */ |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 150 | kMulRGBByAlpha_RoundUp_InConfigFlag = 0x4, |
| 151 | kMulRGBByAlpha_RoundDown_InConfigFlag = 0x8, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 152 | |
| 153 | kDummyInConfigFlag, |
| 154 | kInConfigBitMask = (kDummyInConfigFlag-1) | |
| 155 | (kDummyInConfigFlag-2) |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 156 | }; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 157 | enum CoordMapping { |
| 158 | kIdentity_CoordMapping, |
| 159 | kRadialGradient_CoordMapping, |
| 160 | kSweepGradient_CoordMapping, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 161 | kRadial2Gradient_CoordMapping, |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 162 | // need different shader computation when quadratic |
| 163 | // eq describing the gradient degenerates to a linear eq. |
| 164 | kRadial2GradientDegenerate_CoordMapping, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 165 | kCoordMappingCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 166 | }; |
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 | uint8_t fOptFlags; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 169 | uint8_t fInConfigFlags; // bitfield of InConfigFlags values |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 170 | uint8_t fFetchMode; // casts to enum FetchMode |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 171 | uint8_t fCoordMapping; // casts to enum CoordMapping |
senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 172 | uint8_t fKernelWidth; |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 174 | GR_STATIC_ASSERT((InConfigFlags)(uint8_t)kInConfigBitMask == |
| 175 | kInConfigBitMask); |
| 176 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 177 | inline bool isEnabled() const { |
bsalomon@google.com | c2c9b97 | 2011-10-03 13:17:22 +0000 | [diff] [blame] | 178 | return SkToBool(fOptFlags & kIsEnabled_OptFlagBit); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 179 | } |
| 180 | inline void setEnabled(bool newValue) { |
| 181 | if (newValue) { |
| 182 | fOptFlags |= kIsEnabled_OptFlagBit; |
| 183 | } else { |
| 184 | fOptFlags &= ~kIsEnabled_OptFlagBit; |
| 185 | } |
| 186 | } |
| 187 | }; |
| 188 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 189 | // Specifies where the intitial color comes from before the stages are |
| 190 | // applied. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 191 | enum ColorInput { |
| 192 | kSolidWhite_ColorInput, |
| 193 | kTransBlack_ColorInput, |
| 194 | kAttribute_ColorInput, |
| 195 | kUniform_ColorInput, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 196 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 197 | kColorInputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 198 | }; |
| 199 | // 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] | 200 | // used as a per-pixel blend coeffecient. This controls whether a |
| 201 | // secondary source is output and what value it holds. |
| 202 | enum DualSrcOutput { |
| 203 | kNone_DualSrcOutput, |
| 204 | kCoverage_DualSrcOutput, |
| 205 | kCoverageISA_DualSrcOutput, |
| 206 | kCoverageISC_DualSrcOutput, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 207 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 208 | kDualSrcOutputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 209 | }; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 210 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 211 | GrDrawState::VertexEdgeType fVertexEdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 212 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 213 | // stripped of bits that don't affect prog generation |
| 214 | GrVertexLayout fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 215 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 216 | StageDesc fStages[GrDrawState::kNumStages]; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 217 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 218 | // To enable experimental geometry shader code (not for use in |
| 219 | // production) |
| 220 | #if GR_GL_EXPERIMENTAL_GS |
| 221 | bool fExperimentalGS; |
| 222 | #endif |
| 223 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 224 | uint8_t fColorInput; // casts to enum ColorInput |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 225 | uint8_t fCoverageInput; // casts to enum CoverageInput |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 226 | uint8_t fOutputConfig; // casts to enum OutputConfig |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 227 | uint8_t fDualSrcOutput; // casts to enum DualSrcOutput |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 228 | int8_t fFirstCoverageStage; |
| 229 | SkBool8 fEmitsPointSize; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 230 | SkBool8 fEdgeAAConcave; |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 231 | SkBool8 fColorMatrixEnabled; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 232 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 233 | int8_t fEdgeAANumEdges; |
| 234 | uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 235 | int8_t fPadding[3]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 236 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 237 | } fProgramDesc; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 238 | GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 239 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 240 | // for code readability |
| 241 | typedef ProgramDesc::StageDesc StageDesc; |
| 242 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 243 | private: |
| 244 | |
| 245 | const ProgramDesc& getDesc() { return fProgramDesc; } |
senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 246 | const char* adjustInColor(const GrStringBuilder& inColor) const; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 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; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 276 | GrGLint fCoverageUni; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 277 | GrGLint fEdgesUni; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 278 | GrGLint fColorFilterUni; |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 279 | GrGLint fColorMatrixUni; |
| 280 | GrGLint fColorMatrixVecUni; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 281 | StageUniLocations fStages[GrDrawState::kNumStages]; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 282 | void reset() { |
| 283 | fViewMatrixUni = kUnusedUniform; |
| 284 | fColorUni = kUnusedUniform; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 285 | fCoverageUni = kUnusedUniform; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 286 | fEdgesUni = kUnusedUniform; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 287 | fColorFilterUni = kUnusedUniform; |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 288 | fColorMatrixUni = kUnusedUniform; |
| 289 | fColorMatrixVecUni = kUnusedUniform; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 290 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 291 | fStages[s].reset(); |
| 292 | } |
| 293 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | class CachedData : public ::GrNoncopyable { |
| 297 | public: |
| 298 | CachedData() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | ~CachedData() { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void copyAndTakeOwnership(CachedData& other) { |
bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 305 | memcpy(this, &other, sizeof(*this)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 306 | } |
| 307 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 308 | public: |
| 309 | |
| 310 | // IDs |
| 311 | GrGLuint fVShaderID; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 312 | GrGLuint fGShaderID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 313 | GrGLuint fFShaderID; |
| 314 | GrGLuint fProgramID; |
| 315 | // shader uniform locations (-1 if shader doesn't use them) |
| 316 | UniLocations fUniLocations; |
| 317 | |
| 318 | GrMatrix fViewMatrix; |
| 319 | |
| 320 | // these reflect the current values of uniforms |
| 321 | // (GL uniform values travel with program) |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 322 | GrColor fColor; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 323 | GrColor fCoverage; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 324 | GrColor fColorFilterColor; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 325 | GrMatrix fTextureMatrices[GrDrawState::kNumStages]; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 326 | // width and height used for normalized texel size |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 327 | int fTextureWidth[GrDrawState::kNumStages]; |
| 328 | int fTextureHeight[GrDrawState::kNumStages]; |
| 329 | GrScalar fRadial2CenterX1[GrDrawState::kNumStages]; |
| 330 | GrScalar fRadial2Radius0[GrDrawState::kNumStages]; |
| 331 | bool fRadial2PosRoot[GrDrawState::kNumStages]; |
| 332 | GrRect fTextureDomain[GrDrawState::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 333 | |
| 334 | private: |
| 335 | enum Constants { |
| 336 | kUniLocationPreAllocSize = 8 |
| 337 | }; |
| 338 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 339 | }; // CachedData |
| 340 | |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 341 | enum Constants { |
| 342 | kProgramKeySize = sizeof(ProgramDesc) |
| 343 | }; |
| 344 | |
| 345 | // Provide an opaque ProgramDesc |
| 346 | const uint32_t* keyData() const{ |
| 347 | return reinterpret_cast<const uint32_t*>(&fProgramDesc); |
| 348 | } |
| 349 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 350 | private: |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 351 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 352 | // Determines which uniforms will need to be bound. |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 353 | void genStageCode(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 354 | int stageNum, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 355 | const ProgramDesc::StageDesc& desc, |
| 356 | const char* fsInColor, // NULL means no incoming color |
| 357 | const char* fsOutColor, |
| 358 | const char* vsInCoord, |
| 359 | ShaderCodeSegments* segments, |
| 360 | StageUniLocations* locations) const; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 361 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 362 | void genGeometryShader(const GrGLContextInfo& gl, |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 363 | ShaderCodeSegments* segments) const; |
| 364 | |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 365 | // generates code to compute coverage based on edge AA. |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 366 | void genEdgeCoverage(const GrGLContextInfo& gl, |
bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 367 | GrVertexLayout layout, |
| 368 | CachedData* programData, |
| 369 | GrStringBuilder* coverageVar, |
| 370 | ShaderCodeSegments* segments) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 371 | |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 372 | static bool CompileShaders(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 373 | const ShaderCodeSegments& segments, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 374 | CachedData* programData); |
| 375 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 376 | // Compiles a GL shader, returns shader ID or 0 if failed |
| 377 | // params have same meaning as glShaderSource |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 378 | static GrGLuint CompileShader(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 379 | GrGLenum type, int stringCnt, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 380 | const char** strings, |
| 381 | int* stringLengths); |
| 382 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 383 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and |
| 384 | // links the program |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 385 | bool bindOutputsAttribsAndLinkProgram( |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 386 | const GrGLContextInfo& gl, |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 387 | GrStringBuilder texCoordAttrNames[GrDrawState::kMaxTexCoords], |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 388 | bool bindColorOut, |
| 389 | bool bindDualSrcOut, |
| 390 | CachedData* programData) const; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 391 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 392 | // Binds uniforms; initializes cache to invalid values. |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 393 | void getUniformLocationsAndInitCache(const GrGLContextInfo& gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 394 | CachedData* programData) const; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 395 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 396 | friend class GrGpuGLShaders; |
| 397 | }; |
| 398 | |
| 399 | #endif |