| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 10 | #ifndef GrGLProgram_DEFINED |
| 11 | #define GrGLProgram_DEFINED |
| 12 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 13 | #include "GrDrawState.h" |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 14 | #include "GrGLInterface.h" |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 15 | #include "GrGLSL.h" |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 16 | #include "GrStringBuilder.h" |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 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 | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 51 | bool genProgram(const GrGLInterface* gl, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 52 | GrGLSLGeneration glslVersion, |
| 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 | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 90 | enum OutputPM { |
| 91 | // PM-color OR color with no alpha channel |
| 92 | kYes_OutputPM, |
| 93 | // nonPM-color with alpha channel |
| 94 | kNo_OutputPM, |
| 95 | |
| 96 | kOutputPMCnt |
| 97 | }; |
| 98 | |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 99 | struct StageDesc { |
| 100 | enum OptFlagBits { |
| 101 | kNoPerspective_OptFlagBit = 1 << 0, |
| 102 | kIdentityMatrix_OptFlagBit = 1 << 1, |
| 103 | kCustomTextureDomain_OptFlagBit = 1 << 2, |
| 104 | kIsEnabled_OptFlagBit = 1 << 7 |
| 105 | }; |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 106 | enum FetchMode { |
| 107 | kSingle_FetchMode, |
| bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 108 | k2x2_FetchMode, |
| senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 109 | kConvolution_FetchMode, |
| bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 110 | |
| 111 | kFetchModeCnt, |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +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 { |
| 118 | kNone_InConfigFlag = 0x0, |
| 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 | */ |
| bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 126 | kSwapRAndB_InConfigFlag = 0x1, |
| 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 |
| 130 | kSwapRAndB and kPremul. It is prefereable to perform the |
| 131 | smear outside the shader using GL_ARB_texture_swizzle if |
| 132 | possible rather than setting this flag. |
| 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 | kSmearAlpha_InConfigFlag = 0x2, |
| 135 | |
| 136 | /** |
| 137 | Multiply r,g,b by a after texture reads. This flag incompatible |
| 138 | with kSmearAlpha and may only be used with FetchMode kSingle. |
| 139 | */ |
| 140 | kMulRGBByAlpha_InConfigFlag = 0x4, |
| 141 | |
| 142 | kDummyInConfigFlag, |
| 143 | kInConfigBitMask = (kDummyInConfigFlag-1) | |
| 144 | (kDummyInConfigFlag-2) |
| bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 145 | }; |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 146 | enum CoordMapping { |
| 147 | kIdentity_CoordMapping, |
| 148 | kRadialGradient_CoordMapping, |
| 149 | kSweepGradient_CoordMapping, |
| bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 150 | kRadial2Gradient_CoordMapping, |
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 151 | // need different shader computation when quadratic |
| 152 | // eq describing the gradient degenerates to a linear eq. |
| 153 | kRadial2GradientDegenerate_CoordMapping, |
| bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 154 | kCoordMappingCnt |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 155 | }; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 156 | |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 157 | uint8_t fOptFlags; |
| bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 158 | uint8_t fInConfigFlags; // bitfield of InConfigFlags values |
| bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 159 | uint8_t fFetchMode; // casts to enum FetchMode |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 160 | uint8_t fCoordMapping; // casts to enum CoordMapping |
| senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 161 | uint8_t fKernelWidth; |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 162 | |
| bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 163 | GR_STATIC_ASSERT((InConfigFlags)(uint8_t)kInConfigBitMask == |
| 164 | kInConfigBitMask); |
| 165 | |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 166 | inline bool isEnabled() const { |
| bsalomon@google.com | c2c9b97 | 2011-10-03 13:17:22 +0000 | [diff] [blame] | 167 | return SkToBool(fOptFlags & kIsEnabled_OptFlagBit); |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 168 | } |
| 169 | inline void setEnabled(bool newValue) { |
| 170 | if (newValue) { |
| 171 | fOptFlags |= kIsEnabled_OptFlagBit; |
| 172 | } else { |
| 173 | fOptFlags &= ~kIsEnabled_OptFlagBit; |
| 174 | } |
| 175 | } |
| 176 | }; |
| 177 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 178 | // Specifies where the intitial color comes from before the stages are |
| 179 | // applied. |
| bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 180 | enum ColorInput { |
| 181 | kSolidWhite_ColorInput, |
| 182 | kTransBlack_ColorInput, |
| 183 | kAttribute_ColorInput, |
| 184 | kUniform_ColorInput, |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 185 | |
| bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 186 | kColorInputCnt |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 187 | }; |
| 188 | // 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] | 189 | // used as a per-pixel blend coeffecient. This controls whether a |
| 190 | // secondary source is output and what value it holds. |
| 191 | enum DualSrcOutput { |
| 192 | kNone_DualSrcOutput, |
| 193 | kCoverage_DualSrcOutput, |
| 194 | kCoverageISA_DualSrcOutput, |
| 195 | kCoverageISC_DualSrcOutput, |
| bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 196 | |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 197 | kDualSrcOutputCnt |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 198 | }; |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 199 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 200 | GrDrawState::VertexEdgeType fVertexEdgeType; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 201 | |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 202 | // stripped of bits that don't affect prog generation |
| 203 | GrVertexLayout fVertexLayout; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 204 | |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 205 | StageDesc fStages[GrDrawState::kNumStages]; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 206 | |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 207 | // To enable experimental geometry shader code (not for use in |
| 208 | // production) |
| 209 | #if GR_GL_EXPERIMENTAL_GS |
| 210 | bool fExperimentalGS; |
| 211 | #endif |
| 212 | |
| bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 213 | uint8_t fColorInput; // casts to enum ColorInput |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame^] | 214 | uint8_t fCoverageInput; // casts to enum CoverageInput |
| bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 215 | uint8_t fOutputPM; // cases to enum OutputPM |
| bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 216 | uint8_t fDualSrcOutput; // casts to enum DualSrcOutput |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 217 | int8_t fFirstCoverageStage; |
| 218 | SkBool8 fEmitsPointSize; |
| senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 219 | SkBool8 fEdgeAAConcave; |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 220 | SkBool8 fColorMatrixEnabled; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 221 | |
| tomhudson@google.com | 0d83172 | 2011-06-02 15:37:14 +0000 | [diff] [blame] | 222 | int8_t fEdgeAANumEdges; |
| 223 | uint8_t fColorFilterXfermode; // casts to enum SkXfermode::Mode |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 224 | int8_t fPadding[3]; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 225 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 226 | } fProgramDesc; |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 227 | GR_STATIC_ASSERT(!(sizeof(ProgramDesc) % 4)); |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 228 | |
| bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 229 | // for code readability |
| 230 | typedef ProgramDesc::StageDesc StageDesc; |
| 231 | |
| tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 232 | private: |
| 233 | |
| 234 | const ProgramDesc& getDesc() { return fProgramDesc; } |
| senorblanco@chromium.org | b3a39b5 | 2012-01-05 18:28:56 +0000 | [diff] [blame] | 235 | const char* adjustInColor(const GrStringBuilder& inColor) const; |
| tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 236 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 237 | public: |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 238 | enum { |
| 239 | kUnusedUniform = -1, |
| 240 | kSetAsAttribute = 1000, |
| 241 | }; |
| 242 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 243 | struct StageUniLocations { |
| 244 | GrGLint fTextureMatrixUni; |
| bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 245 | GrGLint fNormalizedTexelSizeUni; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 246 | GrGLint fSamplerUni; |
| 247 | GrGLint fRadial2Uni; |
| junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 248 | GrGLint fTexDomUni; |
| senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 249 | GrGLint fKernelUni; |
| 250 | GrGLint fImageIncrementUni; |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 251 | void reset() { |
| 252 | fTextureMatrixUni = kUnusedUniform; |
| 253 | fNormalizedTexelSizeUni = kUnusedUniform; |
| 254 | fSamplerUni = kUnusedUniform; |
| 255 | fRadial2Uni = kUnusedUniform; |
| junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 256 | fTexDomUni = kUnusedUniform; |
| senorblanco@chromium.org | 027de5f | 2011-07-08 18:03:33 +0000 | [diff] [blame] | 257 | fKernelUni = kUnusedUniform; |
| 258 | fImageIncrementUni = kUnusedUniform; |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 259 | } |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | struct UniLocations { |
| 263 | GrGLint fViewMatrixUni; |
| bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 264 | GrGLint fColorUni; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame^] | 265 | GrGLint fCoverageUni; |
| senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 266 | GrGLint fEdgesUni; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 267 | GrGLint fColorFilterUni; |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 268 | GrGLint fColorMatrixUni; |
| 269 | GrGLint fColorMatrixVecUni; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 270 | StageUniLocations fStages[GrDrawState::kNumStages]; |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 271 | void reset() { |
| 272 | fViewMatrixUni = kUnusedUniform; |
| 273 | fColorUni = kUnusedUniform; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame^] | 274 | fCoverageUni = kUnusedUniform; |
| senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 275 | fEdgesUni = kUnusedUniform; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 276 | fColorFilterUni = kUnusedUniform; |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 277 | fColorMatrixUni = kUnusedUniform; |
| 278 | fColorMatrixVecUni = kUnusedUniform; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 279 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 280 | fStages[s].reset(); |
| 281 | } |
| 282 | } |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | class CachedData : public ::GrNoncopyable { |
| 286 | public: |
| 287 | CachedData() { |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | ~CachedData() { |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void copyAndTakeOwnership(CachedData& other) { |
| bsalomon@google.com | 2d9ddf9 | 2011-05-11 16:52:59 +0000 | [diff] [blame] | 294 | memcpy(this, &other, sizeof(*this)); |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 297 | public: |
| 298 | |
| 299 | // IDs |
| 300 | GrGLuint fVShaderID; |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 301 | GrGLuint fGShaderID; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 302 | GrGLuint fFShaderID; |
| 303 | GrGLuint fProgramID; |
| 304 | // shader uniform locations (-1 if shader doesn't use them) |
| 305 | UniLocations fUniLocations; |
| 306 | |
| 307 | GrMatrix fViewMatrix; |
| 308 | |
| 309 | // these reflect the current values of uniforms |
| 310 | // (GL uniform values travel with program) |
| bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 311 | GrColor fColor; |
| bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame^] | 312 | GrColor fCoverage; |
| Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 313 | GrColor fColorFilterColor; |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 314 | GrMatrix fTextureMatrices[GrDrawState::kNumStages]; |
| bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 315 | // width and height used for normalized texel size |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 316 | int fTextureWidth[GrDrawState::kNumStages]; |
| 317 | int fTextureHeight[GrDrawState::kNumStages]; |
| 318 | GrScalar fRadial2CenterX1[GrDrawState::kNumStages]; |
| 319 | GrScalar fRadial2Radius0[GrDrawState::kNumStages]; |
| 320 | bool fRadial2PosRoot[GrDrawState::kNumStages]; |
| 321 | GrRect fTextureDomain[GrDrawState::kNumStages]; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 322 | |
| 323 | private: |
| 324 | enum Constants { |
| 325 | kUniLocationPreAllocSize = 8 |
| 326 | }; |
| 327 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 328 | }; // CachedData |
| 329 | |
| junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 330 | enum Constants { |
| 331 | kProgramKeySize = sizeof(ProgramDesc) |
| 332 | }; |
| 333 | |
| 334 | // Provide an opaque ProgramDesc |
| 335 | const uint32_t* keyData() const{ |
| 336 | return reinterpret_cast<const uint32_t*>(&fProgramDesc); |
| 337 | } |
| 338 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 339 | private: |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 340 | |
| tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 341 | // Determines which uniforms will need to be bound. |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 342 | void genStageCode(const GrGLInterface* gl, |
| 343 | int stageNum, |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 344 | const ProgramDesc::StageDesc& desc, |
| 345 | const char* fsInColor, // NULL means no incoming color |
| 346 | const char* fsOutColor, |
| 347 | const char* vsInCoord, |
| 348 | ShaderCodeSegments* segments, |
| 349 | StageUniLocations* locations) const; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 350 | |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 351 | void genGeometryShader(const GrGLInterface* gl, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 352 | GrGLSLGeneration glslVersion, |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 353 | ShaderCodeSegments* segments) const; |
| 354 | |
| bsalomon@google.com | 6610567 | 2011-09-15 15:12:00 +0000 | [diff] [blame] | 355 | // generates code to compute coverage based on edge AA. |
| 356 | void genEdgeCoverage(const GrGLInterface* gl, |
| 357 | GrVertexLayout layout, |
| 358 | CachedData* programData, |
| 359 | GrStringBuilder* coverageVar, |
| 360 | ShaderCodeSegments* segments) const; |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 361 | |
| bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 362 | static bool CompileShaders(const GrGLInterface* gl, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 363 | GrGLSLGeneration glslVersion, |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 364 | const ShaderCodeSegments& segments, |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 365 | CachedData* programData); |
| 366 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 367 | // Compiles a GL shader, returns shader ID or 0 if failed |
| 368 | // params have same meaning as glShaderSource |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 369 | static GrGLuint CompileShader(const GrGLInterface* gl, |
| 370 | GrGLenum type, int stringCnt, |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 371 | const char** strings, |
| 372 | int* stringLengths); |
| 373 | |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 374 | // Creates a GL program ID, binds shader attributes to GL vertex attrs, and |
| 375 | // links the program |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 376 | bool bindOutputsAttribsAndLinkProgram( |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 377 | const GrGLInterface* gl, |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 378 | GrStringBuilder texCoordAttrNames[GrDrawState::kMaxTexCoords], |
| bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 379 | bool bindColorOut, |
| 380 | bool bindDualSrcOut, |
| 381 | CachedData* programData) const; |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 382 | |
| tomhudson@google.com | 2a2e3ef | 2011-10-25 19:51:09 +0000 | [diff] [blame] | 383 | // Binds uniforms; initializes cache to invalid values. |
| bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 384 | void getUniformLocationsAndInitCache(const GrGLInterface* gl, |
| 385 | CachedData* programData) const; |
| bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 386 | |
| junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 387 | friend class GrGpuGLShaders; |
| 388 | }; |
| 389 | |
| 390 | #endif |