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" |
bsalomon@google.com | 890e3b5 | 2012-06-01 19:01:37 +0000 | [diff] [blame] | 15 | #include "GrGLTexture.h" |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 16 | #include "GrGLUniformManager.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 18 | #include "SkString.h" |
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; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 22 | class GrGLProgramStage; |
tomhudson@google.com | f9ad886 | 2012-05-11 20:38:48 +0000 | [diff] [blame] | 23 | class GrGLShaderBuilder; |
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 | */ |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 38 | class GrGLProgram : public GrRefCnt { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 39 | public: |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 40 | SK_DECLARE_INST_COUNT(GrGLProgram) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 42 | struct Desc; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 44 | static GrGLProgram* Create(const GrGLContextInfo& gl, |
| 45 | const Desc& desc, |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 46 | const GrCustomStage** customStages); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 47 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 48 | virtual ~GrGLProgram(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 49 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 50 | /** Call to abandon GL objects owned by this program */ |
| 51 | void abandon(); |
| 52 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 53 | /** |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 54 | * The shader may modify the blend coeffecients. Params are in/out |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 55 | */ |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 56 | void overrideBlend(GrBlendCoeff* srcCoeff, GrBlendCoeff* dstCoeff) const; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 58 | const Desc& getDesc() { return fDesc; } |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 59 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 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 | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 78 | // Parameters that affect code generation |
| 79 | // These structs should be kept compact; they are the input to an |
| 80 | // expensive hash key generator. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 81 | struct Desc { |
| 82 | Desc() { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 83 | // since we use this as part of a key we can't have any unitialized |
| 84 | // padding |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 85 | memset(this, 0, sizeof(Desc)); |
| 86 | } |
| 87 | |
| 88 | // returns this as a uint32_t array to be used as a key in the program cache |
| 89 | const uint32_t* asKey() const { |
| 90 | return reinterpret_cast<const uint32_t*>(this); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 93 | enum OutputConfig { |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 94 | // PM-color OR color with no alpha channel |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 95 | kPremultiplied_OutputConfig, |
| 96 | // nonPM-color with alpha channel. Round components up after |
| 97 | // dividing by alpha. Assumes output is 8 bits for r, g, and b |
| 98 | kUnpremultiplied_RoundUp_OutputConfig, |
| 99 | // nonPM-color with alpha channel. Round components down after |
| 100 | // dividing by alpha. Assumes output is 8 bits for r, g, and b |
| 101 | kUnpremultiplied_RoundDown_OutputConfig, |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 102 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 103 | kOutputConfigCnt |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 106 | struct StageDesc { |
| 107 | enum OptFlagBits { |
| 108 | kNoPerspective_OptFlagBit = 1 << 0, |
| 109 | kIdentityMatrix_OptFlagBit = 1 << 1, |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 110 | kIsEnabled_OptFlagBit = 1 << 7 |
| 111 | }; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 112 | |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 113 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 114 | Flags set based on a src texture's pixel config. The operations |
| 115 | described are performed after reading a texel. |
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 | enum InConfigFlags { |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 118 | kNone_InConfigFlag = 0x00, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 119 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 120 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 121 | Swap the R and B channels. This is incompatible with |
| 122 | kSmearAlpha. It is prefereable to perform the swizzle outside |
| 123 | the shader using GL_ARB_texture_swizzle if possible rather |
| 124 | than setting this flag. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 125 | */ |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 126 | kSwapRAndB_InConfigFlag = 0x01, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 127 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 128 | /** |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 129 | Smear alpha across all four channels. This is incompatible with |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 130 | kSwapRAndB, kMulRGBByAlpha* and kSmearRed. It is prefereable |
| 131 | to perform the smear outside the shader using |
| 132 | GL_ARB_texture_swizzle if possible rather than setting this |
| 133 | flag. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 134 | */ |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 135 | kSmearAlpha_InConfigFlag = 0x02, |
| 136 | |
| 137 | /** |
| 138 | Smear the red channel across all four channels. This flag is |
| 139 | incompatible with kSwapRAndB, kMulRGBByAlpha*and kSmearAlpha. |
| 140 | It is preferable to use GL_ARB_texture_swizzle instead of this |
| 141 | flag. |
| 142 | */ |
| 143 | kSmearRed_InConfigFlag = 0x04, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | Multiply r,g,b by a after texture reads. This flag incompatible |
bsalomon@google.com | d2ae1fa | 2012-06-04 20:06:02 +0000 | [diff] [blame] | 147 | with kSmearAlpha. |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 148 | |
| 149 | It is assumed the src texture has 8bit color components. After |
| 150 | reading the texture one version rounds up to the next multiple |
| 151 | of 1/255.0 and the other rounds down. At most one of these |
| 152 | flags may be set. |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 153 | */ |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 154 | kMulRGBByAlpha_RoundUp_InConfigFlag = 0x08, |
| 155 | kMulRGBByAlpha_RoundDown_InConfigFlag = 0x10, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 156 | |
| 157 | kDummyInConfigFlag, |
| 158 | kInConfigBitMask = (kDummyInConfigFlag-1) | |
| 159 | (kDummyInConfigFlag-2) |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 160 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 161 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 162 | uint8_t fOptFlags; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 163 | uint8_t fInConfigFlags; // bitfield of InConfigFlags values |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 164 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 165 | /** Non-zero if user-supplied code will write the stage's |
| 166 | contribution to the fragment shader. */ |
twiz@google.com | a5e65ec | 2012-08-02 15:15:16 +0000 | [diff] [blame^] | 167 | GrProgramStageFactory::StageKey fCustomStageKey; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 168 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 169 | GR_STATIC_ASSERT((InConfigFlags)(uint8_t)kInConfigBitMask == |
| 170 | kInConfigBitMask); |
| 171 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 172 | inline bool isEnabled() const { |
bsalomon@google.com | c2c9b97 | 2011-10-03 13:17:22 +0000 | [diff] [blame] | 173 | return SkToBool(fOptFlags & kIsEnabled_OptFlagBit); |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 174 | } |
| 175 | inline void setEnabled(bool newValue) { |
| 176 | if (newValue) { |
| 177 | fOptFlags |= kIsEnabled_OptFlagBit; |
| 178 | } else { |
| 179 | fOptFlags &= ~kIsEnabled_OptFlagBit; |
| 180 | } |
| 181 | } |
| 182 | }; |
| 183 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 184 | // Specifies where the intitial color comes from before the stages are |
| 185 | // applied. |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 186 | enum ColorInput { |
| 187 | kSolidWhite_ColorInput, |
| 188 | kTransBlack_ColorInput, |
| 189 | kAttribute_ColorInput, |
| 190 | kUniform_ColorInput, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 191 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 192 | kColorInputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 193 | }; |
| 194 | // 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] | 195 | // used as a per-pixel blend coeffecient. This controls whether a |
| 196 | // secondary source is output and what value it holds. |
| 197 | enum DualSrcOutput { |
| 198 | kNone_DualSrcOutput, |
| 199 | kCoverage_DualSrcOutput, |
| 200 | kCoverageISA_DualSrcOutput, |
| 201 | kCoverageISC_DualSrcOutput, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 203 | kDualSrcOutputCnt |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 204 | }; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 205 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 206 | GrDrawState::VertexEdgeType fVertexEdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 207 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 208 | // stripped of bits that don't affect prog generation |
| 209 | GrVertexLayout fVertexLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 210 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 211 | StageDesc fStages[GrDrawState::kNumStages]; |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 212 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 213 | // To enable experimental geometry shader code (not for use in |
| 214 | // production) |
| 215 | #if GR_GL_EXPERIMENTAL_GS |
| 216 | bool fExperimentalGS; |
| 217 | #endif |
| 218 | |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 219 | uint8_t fColorInput; // casts to enum ColorInput |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 220 | uint8_t fCoverageInput; // casts to enum CoverageInput |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 221 | uint8_t fOutputConfig; // casts to enum OutputConfig |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 222 | uint8_t fDualSrcOutput; // casts to enum DualSrcOutput |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 223 | int8_t fFirstCoverageStage; |
| 224 | SkBool8 fEmitsPointSize; |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 225 | SkBool8 fColorMatrixEnabled; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 226 | |
tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 227 | uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 228 | int8_t fPadding[1]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 229 | }; |
| 230 | GR_STATIC_ASSERT(!(sizeof(Desc) % 4)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 231 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 232 | // for code readability |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 233 | typedef Desc::StageDesc StageDesc; |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 234 | |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 235 | private: |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 236 | GrGLProgram(const GrGLContextInfo& gl, |
| 237 | const Desc& desc, |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 238 | const GrCustomStage** customStages); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 239 | |
| 240 | bool succeeded() const { return 0 != fProgramID; } |
| 241 | |
| 242 | /** |
| 243 | * This is the heavy initilization routine for building a GLProgram. |
| 244 | */ |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 245 | bool genProgram(const GrCustomStage** customStages); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 247 | void genInputColor(GrGLShaderBuilder* builder, SkString* inColor); |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 248 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 249 | // Determines which uniforms will need to be bound. |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 250 | void genStageCode(int stageNum, |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 251 | const char* fsInColor, // NULL means no incoming color |
| 252 | const char* fsOutColor, |
| 253 | const char* vsInCoord, |
| 254 | GrGLShaderBuilder* builder); |
| 255 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 256 | void genGeometryShader(GrGLShaderBuilder* segments) const; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 257 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 258 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
| 259 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 260 | void genUniformCoverage(GrGLShaderBuilder* segments, SkString* inOutCoverage); |
| 261 | |
| 262 | // generates code to compute coverage based on edge AA. |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 263 | void genEdgeCoverage(SkString* coverageVar, GrGLShaderBuilder* builder) const; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 264 | |
| 265 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and links the program |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 266 | bool bindOutputsAttribsAndLinkProgram(SkString texCoordAttrNames[GrDrawState::kMaxTexCoords], |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 267 | bool bindColorOut, |
| 268 | bool bindDualSrcOut); |
| 269 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 270 | // Sets the texture units for samplers |
| 271 | void initSamplerUniforms(); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 272 | |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 273 | bool compileShaders(const GrGLShaderBuilder& builder); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 274 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 275 | const char* adjustInColor(const SkString& inColor) const; |
tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 276 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 277 | struct StageUniforms { |
| 278 | UniformHandle fTextureMatrixUni; |
bsalomon@google.com | 0982d35 | 2012-07-31 15:33:25 +0000 | [diff] [blame] | 279 | SkTArray<UniformHandle, true> fSamplerUniforms; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 280 | StageUniforms() { |
| 281 | fTextureMatrixUni = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 282 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 285 | struct Uniforms { |
| 286 | UniformHandle fViewMatrixUni; |
| 287 | UniformHandle fColorUni; |
| 288 | UniformHandle fCoverageUni; |
| 289 | UniformHandle fColorFilterUni; |
| 290 | UniformHandle fColorMatrixUni; |
| 291 | UniformHandle fColorMatrixVecUni; |
| 292 | StageUniforms fStages[GrDrawState::kNumStages]; |
| 293 | Uniforms() { |
| 294 | fViewMatrixUni = GrGLUniformManager::kInvalidUniformHandle; |
| 295 | fColorUni = GrGLUniformManager::kInvalidUniformHandle; |
| 296 | fCoverageUni = GrGLUniformManager::kInvalidUniformHandle; |
| 297 | fColorFilterUni = GrGLUniformManager::kInvalidUniformHandle; |
| 298 | fColorMatrixUni = GrGLUniformManager::kInvalidUniformHandle; |
| 299 | fColorMatrixVecUni = GrGLUniformManager::kInvalidUniformHandle; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 300 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 301 | }; |
| 302 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 303 | // IDs |
| 304 | GrGLuint fVShaderID; |
| 305 | GrGLuint fGShaderID; |
| 306 | GrGLuint fFShaderID; |
| 307 | GrGLuint fProgramID; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 308 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 309 | // The matrix sent to GL is determined by both the client's matrix and |
| 310 | // the size of the viewport. |
| 311 | GrMatrix fViewMatrix; |
| 312 | SkISize fViewportSize; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 313 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 314 | // these reflect the current values of uniforms |
| 315 | // (GL uniform values travel with program) |
| 316 | GrColor fColor; |
| 317 | GrColor fCoverage; |
| 318 | GrColor fColorFilterColor; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 319 | /// When it is sent to GL, the texture matrix will be flipped if the texture orientation |
| 320 | /// (below) requires. |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 321 | GrMatrix fTextureMatrices[GrDrawState::kNumStages]; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 322 | GrGLTexture::Orientation fTextureOrientation[GrDrawState::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 323 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 324 | GrGLProgramStage* fProgramStage[GrDrawState::kNumStages]; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 325 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 326 | Desc fDesc; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 327 | const GrGLContextInfo& fContextInfo; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 328 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 329 | GrGLUniformManager fUniformManager; |
| 330 | Uniforms fUniforms; |
| 331 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 332 | friend class GrGpuGL; // TODO: remove this by adding getters and moving functionality. |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 333 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 334 | typedef GrRefCnt INHERITED; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | #endif |