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 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 8 | #include "GrGpuGL.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 10 | #include "effects/GrConvolutionEffect.h" |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 11 | #include "effects/GrMorphologyEffect.h" |
| 12 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 13 | #include "GrCustomStage.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 14 | #include "GrGLProgramStage.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 15 | #include "GrGLSL.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 16 | #include "GrGpuVertex.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | #include "GrNoncopyable.h" |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 18 | #include "GrProgramStageFactory.h" |
| 19 | #include "GrRandom.h" |
| 20 | #include "GrStringBuilder.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 21 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 22 | #define SKIP_CACHE_CHECK true |
| 23 | #define GR_UINT32_MAX static_cast<uint32_t>(-1) |
| 24 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 25 | void GrGpuGL::ProgramCache::Entry::copyAndTakeOwnership(Entry& entry) { |
| 26 | fProgramData.copyAndTakeOwnership(entry.fProgramData); |
| 27 | fKey = entry.fKey; // ownership transfer |
| 28 | fLRUStamp = entry.fLRUStamp; |
| 29 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 31 | GrGpuGL::ProgramCache::ProgramCache(const GrGLContextInfo& gl) |
| 32 | : fCount(0) |
| 33 | , fCurrLRUStamp(0) |
| 34 | , fGL(gl) { |
| 35 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 36 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 37 | GrGpuGL::ProgramCache::~ProgramCache() { |
| 38 | for (int i = 0; i < fCount; ++i) { |
| 39 | GrGpuGL::DeleteProgram(fGL.interface(), |
| 40 | &fEntries[i].fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 41 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 42 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 44 | void GrGpuGL::ProgramCache::abandon() { |
| 45 | fCount = 0; |
| 46 | } |
| 47 | |
| 48 | void GrGpuGL::ProgramCache::invalidateViewMatrices() { |
| 49 | for (int i = 0; i < fCount; ++i) { |
| 50 | // set to illegal matrix |
| 51 | fEntries[i].fProgramData.fViewMatrix = GrMatrix::InvalidMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 52 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 53 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 55 | GrGLProgram::CachedData* GrGpuGL::ProgramCache::getProgramData( |
| 56 | const GrGLProgram& desc, |
| 57 | GrCustomStage** stages) { |
| 58 | Entry newEntry; |
| 59 | newEntry.fKey.setKeyData(desc.keyData()); |
junov@google.com | f7c00f6 | 2011-08-18 18:15:16 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 61 | Entry* entry = fHashCache.find(newEntry.fKey); |
| 62 | if (NULL == entry) { |
| 63 | if (!desc.genProgram(fGL, stages, &newEntry.fProgramData)) { |
| 64 | return NULL; |
| 65 | } |
| 66 | if (fCount < kMaxEntries) { |
| 67 | entry = fEntries + fCount; |
| 68 | ++fCount; |
| 69 | } else { |
| 70 | GrAssert(kMaxEntries == fCount); |
| 71 | entry = fEntries; |
| 72 | for (int i = 1; i < kMaxEntries; ++i) { |
| 73 | if (fEntries[i].fLRUStamp < entry->fLRUStamp) { |
| 74 | entry = fEntries + i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 75 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 76 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 77 | fHashCache.remove(entry->fKey, entry); |
| 78 | GrGpuGL::DeleteProgram(fGL.interface(), |
| 79 | &entry->fProgramData); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 80 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 81 | entry->copyAndTakeOwnership(newEntry); |
| 82 | fHashCache.insert(entry->fKey, entry); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 83 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame^] | 84 | |
| 85 | entry->fLRUStamp = fCurrLRUStamp; |
| 86 | if (GR_UINT32_MAX == fCurrLRUStamp) { |
| 87 | // wrap around! just trash our LRU, one time hit. |
| 88 | for (int i = 0; i < fCount; ++i) { |
| 89 | fEntries[i].fLRUStamp = 0; |
| 90 | } |
| 91 | } |
| 92 | ++fCurrLRUStamp; |
| 93 | return &entry->fProgramData; |
| 94 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 95 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 96 | void GrGpuGL::DeleteProgram(const GrGLInterface* gl, |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 97 | CachedData* programData) { |
| 98 | GR_GL_CALL(gl, DeleteShader(programData->fVShaderID)); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 99 | if (programData->fGShaderID) { |
| 100 | GR_GL_CALL(gl, DeleteShader(programData->fGShaderID)); |
| 101 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 102 | GR_GL_CALL(gl, DeleteShader(programData->fFShaderID)); |
| 103 | GR_GL_CALL(gl, DeleteProgram(programData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 104 | GR_DEBUGCODE(memset(programData, 0, sizeof(*programData));) |
| 105 | } |
| 106 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 107 | //////////////////////////////////////////////////////////////////////////////// |
| 108 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 109 | void GrGpuGL::abandonResources(){ |
| 110 | INHERITED::abandonResources(); |
| 111 | fProgramCache->abandon(); |
| 112 | fHWProgramID = 0; |
| 113 | } |
| 114 | |
| 115 | //////////////////////////////////////////////////////////////////////////////// |
| 116 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 117 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 118 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 119 | namespace { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 120 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 121 | // GrRandoms nextU() values have patterns in the low bits |
| 122 | // So using nextU() % array_count might never take some values. |
| 123 | int random_int(GrRandom* r, int count) { |
| 124 | return (int)(r->nextF() * count); |
| 125 | } |
| 126 | |
| 127 | // min is inclusive, max is exclusive |
| 128 | int random_int(GrRandom* r, int min, int max) { |
| 129 | return (int)(r->nextF() * (max-min)) + min; |
| 130 | } |
| 131 | |
| 132 | bool random_bool(GrRandom* r) { |
| 133 | return r->nextF() > .5f; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 134 | } |
| 135 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 136 | typedef GrGLProgram::StageDesc StageDesc; |
| 137 | // TODO: Effects should be able to register themselves for inclusion in the |
| 138 | // randomly generated shaders. They should be able to configure themselves |
| 139 | // randomly. |
| 140 | GrCustomStage* create_random_effect(StageDesc* stageDesc, |
| 141 | GrRandom* random) { |
| 142 | enum EffectType { |
| 143 | kConvolution_EffectType, |
| 144 | kErode_EffectType, |
| 145 | kDilate_EffectType, |
| 146 | |
| 147 | kEffectCount |
| 148 | }; |
| 149 | |
| 150 | // TODO: Remove this when generator doesn't apply this non-custom-stage |
| 151 | // notion to custom stages automatically. |
| 152 | static const uint32_t kMulByAlphaMask = |
| 153 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag | |
| 154 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
| 155 | |
| 156 | static const Gr1DKernelEffect::Direction gKernelDirections[] = { |
| 157 | Gr1DKernelEffect::kX_Direction, |
| 158 | Gr1DKernelEffect::kY_Direction |
| 159 | }; |
| 160 | |
| 161 | // TODO: When matrices are property of the custom-stage then remove the |
| 162 | // no-persp flag code below. |
| 163 | int effect = random_int(random, kEffectCount); |
| 164 | switch (effect) { |
| 165 | case kConvolution_EffectType: { |
| 166 | int direction = random_int(random, 2); |
| 167 | int kernelRadius = random_int(random, 1, 4); |
| 168 | float kernel[GrConvolutionEffect::kMaxKernelWidth]; |
| 169 | for (int i = 0; i < GrConvolutionEffect::kMaxKernelWidth; i++) { |
| 170 | kernel[i] = random->nextF(); |
| 171 | } |
| 172 | // does not work with perspective or mul-by-alpha-mask |
| 173 | stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 174 | stageDesc->fInConfigFlags &= ~kMulByAlphaMask; |
| 175 | return new GrConvolutionEffect(gKernelDirections[direction], |
| 176 | kernelRadius, |
| 177 | kernel); |
| 178 | } |
| 179 | case kErode_EffectType: { |
| 180 | int direction = random_int(random, 2); |
| 181 | int kernelRadius = random_int(random, 1, 4); |
| 182 | // does not work with perspective or mul-by-alpha-mask |
| 183 | stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 184 | stageDesc->fInConfigFlags &= ~kMulByAlphaMask; |
| 185 | return new GrMorphologyEffect(gKernelDirections[direction], |
| 186 | kernelRadius, |
| 187 | GrContext::kErode_MorphologyType); |
| 188 | } |
| 189 | case kDilate_EffectType: { |
| 190 | int direction = random_int(random, 2); |
| 191 | int kernelRadius = random_int(random, 1, 4); |
| 192 | // does not work with perspective or mul-by-alpha-mask |
| 193 | stageDesc->fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 194 | stageDesc->fInConfigFlags &= ~kMulByAlphaMask; |
| 195 | return new GrMorphologyEffect(gKernelDirections[direction], |
| 196 | kernelRadius, |
| 197 | GrContext::kDilate_MorphologyType); |
| 198 | } |
| 199 | default: |
| 200 | GrCrash("Unexpected custom effect type"); |
| 201 | } |
| 202 | return NULL; |
| 203 | } |
| 204 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 205 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 206 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 207 | bool GrGpuGL::programUnitTest() { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 208 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 209 | GrGLSLGeneration glslGeneration = |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 210 | GrGetGLSLGeneration(this->glBinding(), this->glInterface()); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 211 | static const int STAGE_OPTS[] = { |
| 212 | 0, |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 213 | StageDesc::kNoPerspective_OptFlagBit, |
| 214 | StageDesc::kIdentity_CoordMapping |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 215 | }; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 216 | static const int IN_CONFIG_FLAGS[] = { |
| 217 | StageDesc::kNone_InConfigFlag, |
| 218 | StageDesc::kSwapRAndB_InConfigFlag, |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 219 | StageDesc::kSwapRAndB_InConfigFlag | |
| 220 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag, |
| 221 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 222 | StageDesc::kSmearAlpha_InConfigFlag, |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 223 | StageDesc::kSmearRed_InConfigFlag, |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 224 | }; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 225 | GrGLProgram program; |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 226 | ProgramDesc& pdesc = program.fProgramDesc; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 227 | |
| 228 | static const int NUM_TESTS = 512; |
| 229 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 230 | GrRandom random; |
| 231 | for (int t = 0; t < NUM_TESTS; ++t) { |
| 232 | |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 233 | #if 0 |
| 234 | GrPrintf("\nTest Program %d\n-------------\n", t); |
| 235 | static const int stop = -1; |
| 236 | if (t == stop) { |
| 237 | int breakpointhere = 9; |
| 238 | } |
| 239 | #endif |
| 240 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 241 | pdesc.fVertexLayout = 0; |
| 242 | pdesc.fEmitsPointSize = random.nextF() > .5f; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 243 | pdesc.fColorInput = random_int(&random, ProgramDesc::kColorInputCnt); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 244 | pdesc.fCoverageInput = random_int(&random, ProgramDesc::kColorInputCnt); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 245 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 246 | pdesc.fColorFilterXfermode = random_int(&random, SkXfermode::kCoeffModesCnt); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 247 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 248 | pdesc.fFirstCoverageStage = random_int(&random, GrDrawState::kNumStages); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 249 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 250 | pdesc.fVertexLayout |= random_bool(&random) ? |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 251 | GrDrawTarget::kCoverage_VertexLayoutBit : |
| 252 | 0; |
| 253 | |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 254 | #if GR_GL_EXPERIMENTAL_GS |
bsalomon@google.com | 6f92f18 | 2011-09-29 15:05:48 +0000 | [diff] [blame] | 255 | pdesc.fExperimentalGS = this->getCaps().fGeometryShaderSupport && |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 256 | random_bool(&random); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 257 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 258 | pdesc.fOutputConfig = random_int(&random, ProgramDesc::kOutputConfigCnt); |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 259 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 260 | bool edgeAA = random_bool(&random); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 261 | if (edgeAA) { |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 262 | pdesc.fVertexLayout |= GrDrawTarget::kEdge_VertexLayoutBit; |
| 263 | if (this->getCaps().fShaderDerivativeSupport) { |
| 264 | pdesc.fVertexEdgeType = (GrDrawState::VertexEdgeType) random_int(&random, GrDrawState::kVertexEdgeTypeCnt); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 265 | } else { |
bsalomon@google.com | 7ffe681 | 2012-05-11 17:32:43 +0000 | [diff] [blame] | 266 | pdesc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 267 | } |
| 268 | } else { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 269 | } |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 270 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 271 | pdesc.fColorMatrixEnabled = random_bool(&random); |
| 272 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 273 | if (this->getCaps().fDualSourceBlendingSupport) { |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 274 | pdesc.fDualSrcOutput = random_int(&random, ProgramDesc::kDualSrcOutputCnt); |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 275 | } else { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 276 | pdesc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 277 | } |
| 278 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 279 | SkAutoTUnref<GrCustomStage> customStages[GrDrawState::kNumStages]; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 280 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 281 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 282 | // enable the stage? |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 283 | if (random_bool(&random)) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 284 | // use separate tex coords? |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 285 | if (random_bool(&random)) { |
| 286 | int t = random_int(&random, GrDrawState::kMaxTexCoords); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 287 | pdesc.fVertexLayout |= StageTexCoordVertexLayoutBit(s, t); |
| 288 | } else { |
| 289 | pdesc.fVertexLayout |= StagePosAsTexCoordVertexLayoutBit(s); |
| 290 | } |
| 291 | } |
| 292 | // use text-formatted verts? |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 293 | if (random_bool(&random)) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 294 | pdesc.fVertexLayout |= kTextFormat_VertexLayoutBit; |
| 295 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 296 | StageDesc& stage = pdesc.fStages[s]; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 297 | |
| 298 | stage.fCustomStageKey = 0; |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 299 | |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 300 | stage.fOptFlags = STAGE_OPTS[random_int(&random, GR_ARRAY_COUNT(STAGE_OPTS))]; |
| 301 | stage.fInConfigFlags = IN_CONFIG_FLAGS[random_int(&random, GR_ARRAY_COUNT(IN_CONFIG_FLAGS))]; |
| 302 | stage.fCoordMapping = random_int(&random, StageDesc::kCoordMappingCnt); |
| 303 | stage.fFetchMode = random_int(&random, StageDesc::kFetchModeCnt); |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 304 | stage.setEnabled(VertexUsesStage(s, pdesc.fVertexLayout)); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 305 | static const uint32_t kMulByAlphaMask = |
| 306 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag | |
| 307 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 308 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 309 | if (StageDesc::k2x2_FetchMode == stage.fFetchMode) { |
| 310 | stage.fInConfigFlags &= ~kMulByAlphaMask; |
bsalomon@google.com | 74b9871 | 2011-11-11 19:46:16 +0000 | [diff] [blame] | 311 | } |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 312 | |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 313 | bool useCustomEffect = random_bool(&random); |
| 314 | if (useCustomEffect) { |
| 315 | customStages[s].reset(create_random_effect(&stage, &random)); |
| 316 | if (NULL != customStages[s]) { |
| 317 | stage.fCustomStageKey = |
| 318 | customStages[s]->getFactory().glStageKey(*customStages[s]); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 319 | } |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 320 | } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 321 | } |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 322 | CachedData cachedData; |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 323 | GR_STATIC_ASSERT(sizeof(customStages) == |
| 324 | GrDrawState::kNumStages * sizeof(GrCustomStage*)); |
| 325 | GrCustomStage** stages = reinterpret_cast<GrCustomStage**>(&customStages); |
| 326 | if (!program.genProgram(this->glContextInfo(), stages, &cachedData)) { |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 327 | return false; |
| 328 | } |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 329 | DeleteProgram(this->glInterface(), &cachedData); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 330 | } |
bsalomon@google.com | a8e686e | 2011-08-16 15:45:58 +0000 | [diff] [blame] | 331 | return true; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 332 | } |
| 333 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 334 | void GrGpuGL::flushViewMatrix() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 335 | const GrMatrix& vm = this->getDrawState().getViewMatrix(); |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 336 | if (!fProgramData->fViewMatrix.cheapEqualTo(vm)) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 337 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 338 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
| 339 | GrAssert(NULL != rt); |
| 340 | GrMatrix m; |
| 341 | m.setAll( |
| 342 | GrIntToScalar(2) / rt->width(), 0, -GR_Scalar1, |
| 343 | 0,-GrIntToScalar(2) / rt->height(), GR_Scalar1, |
| 344 | 0, 0, GrMatrix::I()[8]); |
| 345 | m.setConcat(m, vm); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 346 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 347 | // ES doesn't allow you to pass true to the transpose param, |
| 348 | // so do our own transpose |
| 349 | GrGLfloat mt[] = { |
| 350 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 351 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 352 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 353 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 354 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 355 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 356 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 357 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 358 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
| 359 | }; |
| 360 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 361 | GrAssert(GrGLProgram::kUnusedUniform != |
| 362 | fProgramData->fUniLocations.fViewMatrixUni); |
| 363 | GL_CALL(UniformMatrix3fv(fProgramData->fUniLocations.fViewMatrixUni, |
| 364 | 1, false, mt)); |
| 365 | fProgramData->fViewMatrix = vm; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 366 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 367 | } |
| 368 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 369 | void GrGpuGL::flushTextureDomain(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 370 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTexDomUni; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 371 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 372 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 373 | const GrRect &texDom = drawState.getSampler(s).getTextureDomain(); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 374 | |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 375 | if (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
| 376 | fProgramData->fTextureDomain[s] != texDom) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 377 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 378 | fProgramData->fTextureDomain[s] = texDom; |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 379 | |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 380 | float values[4] = { |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 381 | GrScalarToFloat(texDom.left()), |
| 382 | GrScalarToFloat(texDom.top()), |
| 383 | GrScalarToFloat(texDom.right()), |
| 384 | GrScalarToFloat(texDom.bottom()) |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 385 | }; |
| 386 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 387 | const GrGLTexture* texture = |
| 388 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 389 | GrGLTexture::Orientation orientation = texture->orientation(); |
| 390 | |
| 391 | // vertical flip if necessary |
| 392 | if (GrGLTexture::kBottomUp_Orientation == orientation) { |
| 393 | values[1] = 1.0f - values[1]; |
| 394 | values[3] = 1.0f - values[3]; |
twiz@google.com | 76b8274 | 2011-06-02 20:30:02 +0000 | [diff] [blame] | 395 | // The top and bottom were just flipped, so correct the ordering |
| 396 | // of elements so that values = (l, t, r, b). |
| 397 | SkTSwap(values[1], values[3]); |
junov@google.com | 2f83940 | 2011-05-24 15:13:01 +0000 | [diff] [blame] | 398 | } |
| 399 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 400 | GL_CALL(Uniform4fv(uni, 1, values)); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 401 | } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 405 | void GrGpuGL::flushTextureMatrix(int s) { |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 406 | const GrGLint& uni = fProgramData->fUniLocations.fStages[s].fTextureMatrixUni; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 407 | const GrDrawState& drawState = this->getDrawState(); |
| 408 | const GrGLTexture* texture = |
| 409 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 410 | if (NULL != texture) { |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 411 | const GrMatrix& hwMatrix = fProgramData->fTextureMatrices[s]; |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 412 | const GrMatrix& samplerMatrix = drawState.getSampler(s).getMatrix(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 413 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 414 | (((1 << s) & fDirtyFlags.fTextureChangedMask) || |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 415 | !hwMatrix.cheapEqualTo(samplerMatrix))) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 416 | |
bsalomon@google.com | 8fe84b5 | 2012-03-26 15:24:27 +0000 | [diff] [blame] | 417 | GrMatrix m = samplerMatrix; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 418 | GrSamplerState::SampleMode mode = |
| 419 | drawState.getSampler(s).getSampleMode(); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 420 | AdjustTextureMatrix(texture, mode, &m); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 421 | |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 422 | // ES doesn't allow you to pass true to the transpose param, |
| 423 | // so do our own transpose |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 424 | GrGLfloat mt[] = { |
| 425 | GrScalarToFloat(m[GrMatrix::kMScaleX]), |
| 426 | GrScalarToFloat(m[GrMatrix::kMSkewY]), |
| 427 | GrScalarToFloat(m[GrMatrix::kMPersp0]), |
| 428 | GrScalarToFloat(m[GrMatrix::kMSkewX]), |
| 429 | GrScalarToFloat(m[GrMatrix::kMScaleY]), |
| 430 | GrScalarToFloat(m[GrMatrix::kMPersp1]), |
| 431 | GrScalarToFloat(m[GrMatrix::kMTransX]), |
| 432 | GrScalarToFloat(m[GrMatrix::kMTransY]), |
| 433 | GrScalarToFloat(m[GrMatrix::kMPersp2]) |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 434 | }; |
bsalomon@google.com | 27a4dc4 | 2011-05-16 13:14:03 +0000 | [diff] [blame] | 435 | |
bsalomon@google.com | 341767c | 2012-05-11 20:47:39 +0000 | [diff] [blame] | 436 | GL_CALL(UniformMatrix3fv(uni, 1, false, mt)); |
| 437 | fProgramData->fTextureMatrices[s] = samplerMatrix; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 440 | } |
| 441 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 442 | void GrGpuGL::flushRadial2(int s) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 443 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 444 | const int &uni = fProgramData->fUniLocations.fStages[s].fRadial2Uni; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 445 | const GrSamplerState& sampler = this->getDrawState().getSampler(s); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 446 | if (GrGLProgram::kUnusedUniform != uni && |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 447 | (fProgramData->fRadial2CenterX1[s] != sampler.getRadial2CenterX1() || |
| 448 | fProgramData->fRadial2Radius0[s] != sampler.getRadial2Radius0() || |
| 449 | fProgramData->fRadial2PosRoot[s] != sampler.isRadial2PosRoot())) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 450 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 451 | GrScalar centerX1 = sampler.getRadial2CenterX1(); |
| 452 | GrScalar radius0 = sampler.getRadial2Radius0(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 453 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 454 | GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 455 | |
bsalomon@google.com | 22c5dea | 2011-07-07 14:38:03 +0000 | [diff] [blame] | 456 | // when were in the degenerate (linear) case the second |
| 457 | // value will be INF but the program doesn't read it. (We |
| 458 | // use the same 6 uniforms even though we don't need them |
| 459 | // all in the linear case just to keep the code complexity |
| 460 | // down). |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 461 | float values[6] = { |
| 462 | GrScalarToFloat(a), |
bsalomon@google.com | 2cdfade | 2011-11-23 16:53:42 +0000 | [diff] [blame] | 463 | 1 / (2.f * GrScalarToFloat(a)), |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 464 | GrScalarToFloat(centerX1), |
| 465 | GrScalarToFloat(radius0), |
| 466 | GrScalarToFloat(GrMul(radius0, radius0)), |
| 467 | sampler.isRadial2PosRoot() ? 1.f : -1.f |
| 468 | }; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 469 | GL_CALL(Uniform1fv(uni, 6, values)); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 470 | fProgramData->fRadial2CenterX1[s] = sampler.getRadial2CenterX1(); |
| 471 | fProgramData->fRadial2Radius0[s] = sampler.getRadial2Radius0(); |
| 472 | fProgramData->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); |
| 473 | } |
| 474 | } |
| 475 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 476 | void GrGpuGL::flushTexelSize(int s) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 477 | const int& uni = fProgramData->fUniLocations.fStages[s].fNormalizedTexelSizeUni; |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 478 | if (GrGLProgram::kUnusedUniform != uni) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 479 | const GrGLTexture* texture = |
| 480 | static_cast<const GrGLTexture*>(this->getDrawState().getTexture(s)); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 481 | if (texture->width() != fProgramData->fTextureWidth[s] || |
| 482 | texture->height() != fProgramData->fTextureHeight[s]) { |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 483 | |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 484 | float texelSize[] = {1.f / texture->width(), |
| 485 | 1.f / texture->height()}; |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 486 | GL_CALL(Uniform2fv(uni, 1, texelSize)); |
bsalomon@google.com | 9962108 | 2011-11-15 16:47:16 +0000 | [diff] [blame] | 487 | fProgramData->fTextureWidth[s] = texture->width(); |
| 488 | fProgramData->fTextureHeight[s] = texture->height(); |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 491 | } |
| 492 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 493 | void GrGpuGL::flushColorMatrix() { |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 494 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 495 | int matrixUni = fProgramData->fUniLocations.fColorMatrixUni; |
| 496 | int vecUni = fProgramData->fUniLocations.fColorMatrixVecUni; |
| 497 | if (GrGLProgram::kUnusedUniform != matrixUni |
| 498 | && GrGLProgram::kUnusedUniform != vecUni) { |
| 499 | const float* m = this->getDrawState().getColorMatrix(); |
| 500 | GrGLfloat mt[] = { |
| 501 | m[0], m[5], m[10], m[15], |
| 502 | m[1], m[6], m[11], m[16], |
| 503 | m[2], m[7], m[12], m[17], |
| 504 | m[3], m[8], m[13], m[18], |
| 505 | }; |
| 506 | static float scale = 1.0f / 255.0f; |
| 507 | GrGLfloat vec[] = { |
| 508 | m[4] * scale, m[9] * scale, m[14] * scale, m[19] * scale, |
| 509 | }; |
| 510 | GL_CALL(UniformMatrix4fv(matrixUni, 1, false, mt)); |
| 511 | GL_CALL(Uniform4fv(vecUni, 1, vec)); |
| 512 | } |
| 513 | } |
| 514 | |
Scroggo | 01b87ec | 2011-05-11 18:05:38 +0000 | [diff] [blame] | 515 | static const float ONE_OVER_255 = 1.f / 255.f; |
| 516 | |
| 517 | #define GR_COLOR_TO_VEC4(color) {\ |
| 518 | GrColorUnpackR(color) * ONE_OVER_255,\ |
| 519 | GrColorUnpackG(color) * ONE_OVER_255,\ |
| 520 | GrColorUnpackB(color) * ONE_OVER_255,\ |
| 521 | GrColorUnpackA(color) * ONE_OVER_255 \ |
| 522 | } |
| 523 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 524 | void GrGpuGL::flushColor(GrColor color) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 525 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 526 | const GrDrawState& drawState = this->getDrawState(); |
| 527 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 528 | if (this->getVertexLayout() & kColor_VertexLayoutBit) { |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 529 | // color will be specified per-vertex as an attribute |
| 530 | // invalidate the const vertex attrib color |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 531 | fHWConstAttribColor = GrColor_ILLEGAL; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 532 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 533 | switch (desc.fColorInput) { |
| 534 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 535 | if (fHWConstAttribColor != color) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 536 | // OpenGL ES only supports the float varieties of |
| 537 | // glVertexAttrib |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 538 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 539 | GL_CALL(VertexAttrib4fv(GrGLProgram::ColorAttributeIdx(), |
| 540 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 541 | fHWConstAttribColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 542 | } |
| 543 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 544 | case ProgramDesc::kUniform_ColorInput: |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 545 | if (fProgramData->fColor != color) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 546 | // OpenGL ES doesn't support unsigned byte varieties of |
| 547 | // glUniform |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 548 | float c[] = GR_COLOR_TO_VEC4(color); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 549 | GrAssert(GrGLProgram::kUnusedUniform != |
| 550 | fProgramData->fUniLocations.fColorUni); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 551 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorUni, |
| 552 | 1, c)); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 553 | fProgramData->fColor = color; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 554 | } |
| 555 | break; |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 556 | case ProgramDesc::kSolidWhite_ColorInput: |
| 557 | case ProgramDesc::kTransBlack_ColorInput: |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 558 | break; |
| 559 | default: |
| 560 | GrCrash("Unknown color type."); |
| 561 | } |
| 562 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 563 | if (fProgramData->fUniLocations.fColorFilterUni |
| 564 | != GrGLProgram::kUnusedUniform |
| 565 | && fProgramData->fColorFilterColor |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 566 | != drawState.getColorFilterColor()) { |
| 567 | float c[] = GR_COLOR_TO_VEC4(drawState.getColorFilterColor()); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 568 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fColorFilterUni, 1, c)); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 569 | fProgramData->fColorFilterColor = drawState.getColorFilterColor(); |
Scroggo | 97c88c2 | 2011-05-11 14:05:25 +0000 | [diff] [blame] | 570 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 571 | } |
| 572 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 573 | void GrGpuGL::flushCoverage(GrColor coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 574 | const ProgramDesc& desc = fCurrentProgram.getDesc(); |
| 575 | const GrDrawState& drawState = this->getDrawState(); |
| 576 | |
| 577 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 578 | if (this->getVertexLayout() & kCoverage_VertexLayoutBit) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 579 | // coverage will be specified per-vertex as an attribute |
| 580 | // invalidate the const vertex attrib coverage |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 581 | fHWConstAttribCoverage = GrColor_ILLEGAL; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 582 | } else { |
| 583 | switch (desc.fCoverageInput) { |
| 584 | case ProgramDesc::kAttribute_ColorInput: |
bsalomon@google.com | 255fa16 | 2012-05-21 21:44:59 +0000 | [diff] [blame] | 585 | if (fHWConstAttribCoverage != coverage) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 586 | // OpenGL ES only supports the float varieties of |
| 587 | // glVertexAttrib |
| 588 | float c[] = GR_COLOR_TO_VEC4(coverage); |
| 589 | GL_CALL(VertexAttrib4fv(GrGLProgram::CoverageAttributeIdx(), |
| 590 | c)); |
bsalomon@google.com | 8d49d93 | 2012-05-21 21:40:12 +0000 | [diff] [blame] | 591 | fHWConstAttribCoverage = coverage; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 592 | } |
| 593 | break; |
| 594 | case ProgramDesc::kUniform_ColorInput: |
| 595 | if (fProgramData->fCoverage != coverage) { |
| 596 | // OpenGL ES doesn't support unsigned byte varieties of |
| 597 | // glUniform |
| 598 | float c[] = GR_COLOR_TO_VEC4(coverage); |
| 599 | GrAssert(GrGLProgram::kUnusedUniform != |
| 600 | fProgramData->fUniLocations.fCoverageUni); |
| 601 | GL_CALL(Uniform4fv(fProgramData->fUniLocations.fCoverageUni, |
| 602 | 1, c)); |
| 603 | fProgramData->fCoverage = coverage; |
| 604 | } |
| 605 | break; |
| 606 | case ProgramDesc::kSolidWhite_ColorInput: |
| 607 | case ProgramDesc::kTransBlack_ColorInput: |
| 608 | break; |
| 609 | default: |
| 610 | GrCrash("Unknown coverage type."); |
| 611 | } |
| 612 | } |
| 613 | } |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 614 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 615 | bool GrGpuGL::flushGraphicsState(GrPrimitiveType type) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 616 | if (!flushGLStateCommon(type)) { |
| 617 | return false; |
| 618 | } |
| 619 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 620 | const GrDrawState& drawState = this->getDrawState(); |
| 621 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 622 | if (fDirtyFlags.fRenderTargetChanged) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 623 | // we assume all shader matrices may be wrong after viewport changes |
| 624 | fProgramCache->invalidateViewMatrices(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 625 | } |
| 626 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 627 | GrBlendCoeff srcCoeff; |
| 628 | GrBlendCoeff dstCoeff; |
| 629 | BlendOptFlags blendOpts = this->getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 630 | if (kSkipDraw_BlendOptFlag & blendOpts) { |
| 631 | return false; |
| 632 | } |
| 633 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 634 | GrCustomStage* customStages [GrDrawState::kNumStages]; |
| 635 | this->buildProgram(type, blendOpts, dstCoeff, customStages); |
| 636 | fProgramData = fProgramCache->getProgramData(fCurrentProgram, |
| 637 | customStages); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 638 | if (NULL == fProgramData) { |
| 639 | GrAssert(!"Failed to create program!"); |
| 640 | return false; |
| 641 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 642 | |
| 643 | if (fHWProgramID != fProgramData->fProgramID) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 644 | GL_CALL(UseProgram(fProgramData->fProgramID)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 645 | fHWProgramID = fProgramData->fProgramID; |
| 646 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 647 | fCurrentProgram.overrideBlend(&srcCoeff, &dstCoeff); |
| 648 | this->flushBlend(type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 649 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 650 | GrColor color; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 651 | GrColor coverage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 652 | if (blendOpts & kEmitTransBlack_BlendOptFlag) { |
| 653 | color = 0; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 654 | coverage = 0; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 655 | } else if (blendOpts & kEmitCoverage_BlendOptFlag) { |
| 656 | color = 0xffffffff; |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 657 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 658 | } else { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 659 | color = drawState.getColor(); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 660 | coverage = drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 661 | } |
| 662 | this->flushColor(color); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 663 | this->flushCoverage(coverage); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 664 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 665 | this->flushViewMatrix(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 666 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 667 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 668 | if (this->isStageEnabled(s)) { |
| 669 | this->flushTextureMatrix(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 670 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 671 | this->flushRadial2(s); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 672 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 673 | this->flushTexelSize(s); |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 674 | |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 675 | this->flushTextureDomain(s); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 676 | |
| 677 | if (NULL != fProgramData->fCustomStage[s]) { |
| 678 | const GrSamplerState& sampler = |
| 679 | this->getDrawState().getSampler(s); |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 680 | const GrGLTexture* texture = |
| 681 | static_cast<const GrGLTexture*>( |
| 682 | this->getDrawState().getTexture(s)); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 683 | fProgramData->fCustomStage[s]->setData( |
tomhudson@google.com | 6a820b6 | 2012-05-24 15:10:14 +0000 | [diff] [blame] | 684 | this->glInterface(), *texture, |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 685 | *sampler.getCustomStage(), s); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 686 | } |
bsalomon@google.com | 40d9293 | 2011-12-13 18:40:47 +0000 | [diff] [blame] | 687 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 688 | } |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 689 | this->flushColorMatrix(); |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 690 | resetDirtyFlags(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 691 | return true; |
| 692 | } |
| 693 | |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 694 | #if GR_TEXT_SCALAR_IS_USHORT |
| 695 | #define TEXT_COORDS_GL_TYPE GR_GL_UNSIGNED_SHORT |
| 696 | #define TEXT_COORDS_ARE_NORMALIZED 1 |
| 697 | #elif GR_TEXT_SCALAR_IS_FLOAT |
| 698 | #define TEXT_COORDS_GL_TYPE GR_GL_FLOAT |
| 699 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 700 | #elif GR_TEXT_SCALAR_IS_FIXED |
| 701 | #define TEXT_COORDS_GL_TYPE GR_GL_FIXED |
| 702 | #define TEXT_COORDS_ARE_NORMALIZED 0 |
| 703 | #else |
| 704 | #error "unknown GR_TEXT_SCALAR type" |
| 705 | #endif |
| 706 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 707 | void GrGpuGL::setupGeometry(int* startVertex, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 708 | int* startIndex, |
| 709 | int vertexCount, |
| 710 | int indexCount) { |
| 711 | |
| 712 | int newColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 713 | int newCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 714 | int newTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 715 | int newEdgeOffset; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 716 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 717 | GrVertexLayout currLayout = this->getVertexLayout(); |
| 718 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 719 | GrGLsizei newStride = VertexSizeAndOffsetsByIdx( |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 720 | currLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 721 | newTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 722 | &newColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 723 | &newCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 724 | &newEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 725 | int oldColorOffset; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 726 | int oldCoverageOffset; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 727 | int oldTexCoordOffsets[GrDrawState::kMaxTexCoords]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 728 | int oldEdgeOffset; |
| 729 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 730 | GrGLsizei oldStride = VertexSizeAndOffsetsByIdx( |
| 731 | fHWGeometryState.fVertexLayout, |
| 732 | oldTexCoordOffsets, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 733 | &oldColorOffset, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 734 | &oldCoverageOffset, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 735 | &oldEdgeOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 736 | bool indexed = NULL != startIndex; |
| 737 | |
| 738 | int extraVertexOffset; |
| 739 | int extraIndexOffset; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 740 | this->setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 741 | |
| 742 | GrGLenum scalarType; |
| 743 | bool texCoordNorm; |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 744 | if (currLayout & kTextFormat_VertexLayoutBit) { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 745 | scalarType = TEXT_COORDS_GL_TYPE; |
| 746 | texCoordNorm = SkToBool(TEXT_COORDS_ARE_NORMALIZED); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 747 | } else { |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 748 | GR_STATIC_ASSERT(GR_SCALAR_IS_FLOAT); |
| 749 | scalarType = GR_GL_FLOAT; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 750 | texCoordNorm = false; |
| 751 | } |
| 752 | |
| 753 | size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; |
| 754 | *startVertex = 0; |
| 755 | if (indexed) { |
| 756 | *startIndex += extraIndexOffset; |
| 757 | } |
| 758 | |
| 759 | // all the Pointers must be set if any of these are true |
| 760 | bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || |
| 761 | vertexOffset != fHWGeometryState.fVertexOffset || |
| 762 | newStride != oldStride; |
| 763 | |
| 764 | // position and tex coord offsets change if above conditions are true |
| 765 | // or the type/normalization changed based on text vs nontext type coords. |
| 766 | bool posAndTexChange = allOffsetsChange || |
bsalomon@google.com | 2717d56 | 2012-05-07 19:10:52 +0000 | [diff] [blame] | 767 | (((TEXT_COORDS_GL_TYPE != GR_GL_FLOAT) || TEXT_COORDS_ARE_NORMALIZED) && |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 768 | (kTextFormat_VertexLayoutBit & |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 769 | (fHWGeometryState.fVertexLayout ^ currLayout))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 770 | |
| 771 | if (posAndTexChange) { |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 772 | int idx = GrGLProgram::PositionAttributeIdx(); |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 773 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, false, newStride, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 774 | (GrGLvoid*)vertexOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 775 | fHWGeometryState.fVertexOffset = vertexOffset; |
| 776 | } |
| 777 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 778 | for (int t = 0; t < GrDrawState::kMaxTexCoords; ++t) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 779 | if (newTexCoordOffsets[t] > 0) { |
| 780 | GrGLvoid* texCoordOffset = (GrGLvoid*)(vertexOffset + newTexCoordOffsets[t]); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 781 | int idx = GrGLProgram::TexCoordAttributeIdx(t); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 782 | if (oldTexCoordOffsets[t] <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 783 | GL_CALL(EnableVertexAttribArray(idx)); |
| 784 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 785 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 786 | } else if (posAndTexChange || |
| 787 | newTexCoordOffsets[t] != oldTexCoordOffsets[t]) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 788 | GL_CALL(VertexAttribPointer(idx, 2, scalarType, texCoordNorm, |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 789 | newStride, texCoordOffset)); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 790 | } |
| 791 | } else if (oldTexCoordOffsets[t] > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 792 | GL_CALL(DisableVertexAttribArray(GrGLProgram::TexCoordAttributeIdx(t))); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
| 796 | if (newColorOffset > 0) { |
| 797 | GrGLvoid* colorOffset = (int8_t*)(vertexOffset + newColorOffset); |
bsalomon@google.com | 9196130 | 2011-05-09 18:39:58 +0000 | [diff] [blame] | 798 | int idx = GrGLProgram::ColorAttributeIdx(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 799 | if (oldColorOffset <= 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 800 | GL_CALL(EnableVertexAttribArray(idx)); |
| 801 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 802 | true, newStride, colorOffset)); |
| 803 | } else if (allOffsetsChange || newColorOffset != oldColorOffset) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 804 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 805 | true, newStride, colorOffset)); |
| 806 | } |
| 807 | } else if (oldColorOffset > 0) { |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 808 | GL_CALL(DisableVertexAttribArray(GrGLProgram::ColorAttributeIdx())); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 809 | } |
| 810 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 811 | if (newCoverageOffset > 0) { |
bsalomon@google.com | e10f6fd | 2011-10-11 20:15:26 +0000 | [diff] [blame] | 812 | GrGLvoid* coverageOffset = (int8_t*)(vertexOffset + newCoverageOffset); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 813 | int idx = GrGLProgram::CoverageAttributeIdx(); |
| 814 | if (oldCoverageOffset <= 0) { |
| 815 | GL_CALL(EnableVertexAttribArray(idx)); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 816 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 817 | true, newStride, coverageOffset)); |
| 818 | } else if (allOffsetsChange || newCoverageOffset != oldCoverageOffset) { |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 819 | GL_CALL(VertexAttribPointer(idx, 4, GR_GL_UNSIGNED_BYTE, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 820 | true, newStride, coverageOffset)); |
| 821 | } |
| 822 | } else if (oldCoverageOffset > 0) { |
| 823 | GL_CALL(DisableVertexAttribArray(GrGLProgram::CoverageAttributeIdx())); |
| 824 | } |
| 825 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 826 | if (newEdgeOffset > 0) { |
| 827 | GrGLvoid* edgeOffset = (int8_t*)(vertexOffset + newEdgeOffset); |
| 828 | int idx = GrGLProgram::EdgeAttributeIdx(); |
| 829 | if (oldEdgeOffset <= 0) { |
| 830 | GL_CALL(EnableVertexAttribArray(idx)); |
| 831 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 832 | false, newStride, edgeOffset)); |
| 833 | } else if (allOffsetsChange || newEdgeOffset != oldEdgeOffset) { |
| 834 | GL_CALL(VertexAttribPointer(idx, 4, scalarType, |
| 835 | false, newStride, edgeOffset)); |
| 836 | } |
| 837 | } else if (oldEdgeOffset > 0) { |
| 838 | GL_CALL(DisableVertexAttribArray(GrGLProgram::EdgeAttributeIdx())); |
| 839 | } |
| 840 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 841 | fHWGeometryState.fVertexLayout = currLayout; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 842 | fHWGeometryState.fArrayPtrsDirty = false; |
| 843 | } |
| 844 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 845 | namespace { |
| 846 | |
| 847 | void setup_custom_stage(GrGLProgram::ProgramDesc::StageDesc* stage, |
| 848 | const GrSamplerState& sampler, |
| 849 | GrCustomStage** customStages, |
| 850 | GrGLProgram* program, int index) { |
| 851 | GrCustomStage* customStage = sampler.getCustomStage(); |
| 852 | if (customStage) { |
bsalomon@google.com | ae4f96a | 2012-05-18 19:54:48 +0000 | [diff] [blame] | 853 | const GrProgramStageFactory& factory = customStage->getFactory(); |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 854 | stage->fCustomStageKey = factory.glStageKey(*customStage); |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 855 | customStages[index] = customStage; |
| 856 | } else { |
| 857 | stage->fCustomStageKey = 0; |
| 858 | customStages[index] = NULL; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | } |
| 863 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 864 | void GrGpuGL::buildProgram(GrPrimitiveType type, |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 865 | BlendOptFlags blendOpts, |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 866 | GrBlendCoeff dstCoeff, |
| 867 | GrCustomStage** customStages) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 868 | ProgramDesc& desc = fCurrentProgram.fProgramDesc; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 869 | const GrDrawState& drawState = this->getDrawState(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 870 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 871 | // This should already have been caught |
| 872 | GrAssert(!(kSkipDraw_BlendOptFlag & blendOpts)); |
| 873 | |
| 874 | bool skipCoverage = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 875 | |
| 876 | bool skipColor = SkToBool(blendOpts & (kEmitTransBlack_BlendOptFlag | |
| 877 | kEmitCoverage_BlendOptFlag)); |
| 878 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 879 | // The descriptor is used as a cache key. Thus when a field of the |
| 880 | // descriptor will not affect program generation (because of the vertex |
| 881 | // layout in use or other descriptor field settings) it should be set |
| 882 | // to a canonical value to avoid duplicate programs with different keys. |
| 883 | |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 884 | // Must initialize all fields or cache will have false negatives! |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 885 | desc.fVertexLayout = this->getVertexLayout(); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 886 | |
| 887 | desc.fEmitsPointSize = kPoints_PrimitiveType == type; |
| 888 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 889 | bool requiresAttributeColors = |
| 890 | !skipColor && SkToBool(desc.fVertexLayout & kColor_VertexLayoutBit); |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 891 | bool requiresAttributeCoverage = |
| 892 | !skipCoverage && SkToBool(desc.fVertexLayout & |
| 893 | kCoverage_VertexLayoutBit); |
| 894 | |
| 895 | // fColorInput/fCoverageInput records how colors are specified for the. |
| 896 | // program. So we strip the bits from the layout to avoid false negatives |
| 897 | // when searching for an existing program in the cache. |
| 898 | desc.fVertexLayout &= ~(kColor_VertexLayoutBit | kCoverage_VertexLayoutBit); |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 899 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 900 | desc.fColorFilterXfermode = skipColor ? |
| 901 | SkXfermode::kDst_Mode : |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 902 | drawState.getColorFilterMode(); |
bsalomon@google.com | f2d9155 | 2011-05-16 20:56:06 +0000 | [diff] [blame] | 903 | |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 904 | desc.fColorMatrixEnabled = drawState.isStateFlagEnabled(GrDrawState::kColorMatrix_StateBit); |
| 905 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 906 | // no reason to do edge aa or look at per-vertex coverage if coverage is |
| 907 | // ignored |
| 908 | if (skipCoverage) { |
| 909 | desc.fVertexLayout &= ~(kEdge_VertexLayoutBit | |
| 910 | kCoverage_VertexLayoutBit); |
| 911 | } |
| 912 | |
| 913 | bool colorIsTransBlack = SkToBool(blendOpts & kEmitTransBlack_BlendOptFlag); |
| 914 | bool colorIsSolidWhite = (blendOpts & kEmitCoverage_BlendOptFlag) || |
| 915 | (!requiresAttributeColors && |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 916 | 0xffffffff == drawState.getColor()); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 917 | if (GR_AGGRESSIVE_SHADER_OPTS && colorIsTransBlack) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 918 | desc.fColorInput = ProgramDesc::kTransBlack_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 919 | } else if (GR_AGGRESSIVE_SHADER_OPTS && colorIsSolidWhite) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 920 | desc.fColorInput = ProgramDesc::kSolidWhite_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 921 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeColors) { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 922 | desc.fColorInput = ProgramDesc::kUniform_ColorInput; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 923 | } else { |
bsalomon@google.com | 85b505b | 2011-11-07 14:56:51 +0000 | [diff] [blame] | 924 | desc.fColorInput = ProgramDesc::kAttribute_ColorInput; |
bsalomon@google.com | 4be283f | 2011-04-19 21:15:09 +0000 | [diff] [blame] | 925 | } |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 926 | |
| 927 | bool covIsSolidWhite = !requiresAttributeCoverage && |
| 928 | 0xffffffff == drawState.getCoverage(); |
| 929 | |
| 930 | if (skipCoverage) { |
| 931 | desc.fCoverageInput = ProgramDesc::kTransBlack_ColorInput; |
| 932 | } else if (covIsSolidWhite) { |
| 933 | desc.fCoverageInput = ProgramDesc::kSolidWhite_ColorInput; |
| 934 | } else if (GR_GL_NO_CONSTANT_ATTRIBUTES && !requiresAttributeCoverage) { |
| 935 | desc.fCoverageInput = ProgramDesc::kUniform_ColorInput; |
| 936 | } else { |
| 937 | desc.fCoverageInput = ProgramDesc::kAttribute_ColorInput; |
| 938 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 939 | |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 940 | int lastEnabledStage = -1; |
| 941 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 942 | if (!skipCoverage && (desc.fVertexLayout & |
| 943 | GrDrawTarget::kEdge_VertexLayoutBit)) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 944 | desc.fVertexEdgeType = drawState.getVertexEdgeType(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 945 | } else { |
| 946 | // use canonical value when not set to avoid cache misses |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 947 | desc.fVertexEdgeType = GrDrawState::kHairLine_EdgeType; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 948 | } |
| 949 | |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 950 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 951 | StageDesc& stage = desc.fStages[s]; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 952 | |
| 953 | stage.fOptFlags = 0; |
| 954 | stage.setEnabled(this->isStageEnabled(s)); |
| 955 | |
| 956 | bool skip = s < drawState.getFirstCoverageStage() ? skipColor : |
| 957 | skipCoverage; |
| 958 | |
| 959 | if (!skip && stage.isEnabled()) { |
| 960 | lastEnabledStage = s; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 961 | const GrGLTexture* texture = |
| 962 | static_cast<const GrGLTexture*>(drawState.getTexture(s)); |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 963 | GrAssert(NULL != texture); |
| 964 | const GrSamplerState& sampler = drawState.getSampler(s); |
| 965 | // we matrix to invert when orientation is TopDown, so make sure |
| 966 | // we aren't in that case before flagging as identity. |
| 967 | if (TextureMatrixIsIdentity(texture, sampler)) { |
| 968 | stage.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit; |
| 969 | } else if (!sampler.getMatrix().hasPerspective()) { |
| 970 | stage.fOptFlags |= StageDesc::kNoPerspective_OptFlagBit; |
| 971 | } |
| 972 | switch (sampler.getSampleMode()) { |
| 973 | case GrSamplerState::kNormal_SampleMode: |
| 974 | stage.fCoordMapping = StageDesc::kIdentity_CoordMapping; |
| 975 | break; |
| 976 | case GrSamplerState::kRadial_SampleMode: |
| 977 | stage.fCoordMapping = StageDesc::kRadialGradient_CoordMapping; |
| 978 | break; |
| 979 | case GrSamplerState::kRadial2_SampleMode: |
| 980 | if (sampler.radial2IsDegenerate()) { |
| 981 | stage.fCoordMapping = |
| 982 | StageDesc::kRadial2GradientDegenerate_CoordMapping; |
| 983 | } else { |
| 984 | stage.fCoordMapping = |
| 985 | StageDesc::kRadial2Gradient_CoordMapping; |
| 986 | } |
| 987 | break; |
| 988 | case GrSamplerState::kSweep_SampleMode: |
| 989 | stage.fCoordMapping = StageDesc::kSweepGradient_CoordMapping; |
| 990 | break; |
| 991 | default: |
| 992 | GrCrash("Unexpected sample mode!"); |
| 993 | break; |
| 994 | } |
| 995 | |
| 996 | switch (sampler.getFilter()) { |
| 997 | // these both can use a regular texture2D() |
| 998 | case GrSamplerState::kNearest_Filter: |
| 999 | case GrSamplerState::kBilinear_Filter: |
| 1000 | stage.fFetchMode = StageDesc::kSingle_FetchMode; |
| 1001 | break; |
| 1002 | // performs 4 texture2D()s |
| 1003 | case GrSamplerState::k4x4Downsample_Filter: |
| 1004 | stage.fFetchMode = StageDesc::k2x2_FetchMode; |
| 1005 | break; |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1006 | default: |
| 1007 | GrCrash("Unexpected filter!"); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | if (sampler.hasTextureDomain()) { |
| 1012 | GrAssert(GrSamplerState::kClamp_WrapMode == |
| 1013 | sampler.getWrapX() && |
| 1014 | GrSamplerState::kClamp_WrapMode == |
| 1015 | sampler.getWrapY()); |
| 1016 | stage.fOptFlags |= StageDesc::kCustomTextureDomain_OptFlagBit; |
| 1017 | } |
| 1018 | |
| 1019 | stage.fInConfigFlags = 0; |
bsalomon@google.com | f7fa806 | 2012-02-14 14:09:57 +0000 | [diff] [blame] | 1020 | if (!this->glCaps().textureSwizzleSupport()) { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1021 | if (GrPixelConfigIsAlphaOnly(texture->config())) { |
| 1022 | // if we don't have texture swizzle support then |
robertphillips@google.com | 443e5a5 | 2012-04-30 20:01:21 +0000 | [diff] [blame] | 1023 | // the shader must smear the single channel after |
| 1024 | // reading the texture |
| 1025 | if (this->glCaps().textureRedSupport()) { |
| 1026 | // we can use R8 textures so use kSmearRed |
| 1027 | stage.fInConfigFlags |= |
| 1028 | StageDesc::kSmearRed_InConfigFlag; |
| 1029 | } else { |
| 1030 | // we can use A8 textures so use kSmearAlpha |
| 1031 | stage.fInConfigFlags |= |
| 1032 | StageDesc::kSmearAlpha_InConfigFlag; |
| 1033 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1034 | } else if (sampler.swapsRAndB()) { |
| 1035 | stage.fInConfigFlags |= StageDesc::kSwapRAndB_InConfigFlag; |
| 1036 | } |
| 1037 | } |
| 1038 | if (GrPixelConfigIsUnpremultiplied(texture->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1039 | // The shader generator assumes that color channels are bytes |
| 1040 | // when rounding. |
| 1041 | GrAssert(4 == GrBytesPerPixel(texture->config())); |
| 1042 | if (kUpOnWrite_DownOnRead_UnpremulConversion == |
| 1043 | fUnpremulConversion) { |
| 1044 | stage.fInConfigFlags |= |
| 1045 | StageDesc::kMulRGBByAlpha_RoundDown_InConfigFlag; |
| 1046 | } else { |
| 1047 | stage.fInConfigFlags |= |
| 1048 | StageDesc::kMulRGBByAlpha_RoundUp_InConfigFlag; |
| 1049 | } |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1052 | setup_custom_stage(&stage, sampler, customStages, |
| 1053 | &fCurrentProgram, s); |
| 1054 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1055 | } else { |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1056 | stage.fOptFlags = 0; |
| 1057 | stage.fCoordMapping = (StageDesc::CoordMapping) 0; |
| 1058 | stage.fInConfigFlags = 0; |
| 1059 | stage.fFetchMode = (StageDesc::FetchMode) 0; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 1060 | stage.fCustomStageKey = 0; |
| 1061 | customStages[s] = NULL; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1064 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1065 | if (GrPixelConfigIsUnpremultiplied(drawState.getRenderTarget()->config())) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1066 | // The shader generator assumes that color channels are bytes |
| 1067 | // when rounding. |
| 1068 | GrAssert(4 == GrBytesPerPixel(drawState.getRenderTarget()->config())); |
| 1069 | if (kUpOnWrite_DownOnRead_UnpremulConversion == fUnpremulConversion) { |
| 1070 | desc.fOutputConfig = |
| 1071 | ProgramDesc::kUnpremultiplied_RoundUp_OutputConfig; |
| 1072 | } else { |
| 1073 | desc.fOutputConfig = |
| 1074 | ProgramDesc::kUnpremultiplied_RoundDown_OutputConfig; |
| 1075 | } |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1076 | } else { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1077 | desc.fOutputConfig = ProgramDesc::kPremultiplied_OutputConfig; |
bsalomon@google.com | c436499 | 2011-11-07 15:54:49 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1080 | desc.fDualSrcOutput = ProgramDesc::kNone_DualSrcOutput; |
bsalomon@google.com | edfe1aa | 2011-09-29 14:40:26 +0000 | [diff] [blame] | 1081 | |
| 1082 | // currently the experimental GS will only work with triangle prims |
| 1083 | // (and it doesn't do anything other than pass through values from |
| 1084 | // the VS to the FS anyway). |
| 1085 | #if 0 && GR_GL_EXPERIMENTAL_GS |
| 1086 | desc.fExperimentalGS = this->getCaps().fGeometryShaderSupport; |
| 1087 | #endif |
| 1088 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1089 | // we want to avoid generating programs with different "first cov stage" |
| 1090 | // values when they would compute the same result. |
| 1091 | // We set field in the desc to kNumStages when either there are no |
| 1092 | // coverage stages or the distinction between coverage and color is |
| 1093 | // immaterial. |
bsalomon@google.com | 88939ae | 2011-12-14 15:58:11 +0000 | [diff] [blame] | 1094 | int firstCoverageStage = GrDrawState::kNumStages; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 1095 | desc.fFirstCoverageStage = GrDrawState::kNumStages; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1096 | bool hasCoverage = drawState.getFirstCoverageStage() <= lastEnabledStage; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1097 | if (hasCoverage) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 1098 | firstCoverageStage = drawState.getFirstCoverageStage(); |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | // other coverage inputs |
| 1102 | if (!hasCoverage) { |
| 1103 | hasCoverage = |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 1104 | requiresAttributeCoverage || |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1105 | (desc.fVertexLayout & GrDrawTarget::kEdge_VertexLayoutBit); |
| 1106 | } |
| 1107 | |
| 1108 | if (hasCoverage) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1109 | // color filter is applied between color/coverage computation |
| 1110 | if (SkXfermode::kDst_Mode != desc.fColorFilterXfermode) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1111 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1114 | if (this->getCaps().fDualSourceBlendingSupport && |
| 1115 | !(blendOpts & (kEmitCoverage_BlendOptFlag | |
| 1116 | kCoverageAsAlpha_BlendOptFlag))) { |
| 1117 | if (kZero_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1118 | // write the coverage value to second color |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1119 | desc.fDualSrcOutput = ProgramDesc::kCoverage_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1120 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1121 | } else if (kSA_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1122 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 1123 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1124 | desc.fDualSrcOutput = ProgramDesc::kCoverageISA_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1125 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 1126 | } else if (kSC_BlendCoeff == dstCoeff) { |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1127 | // SA dst coeff becomes 1-(1-SA)*coverage when dst is partially |
| 1128 | // cover |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 1129 | desc.fDualSrcOutput = ProgramDesc::kCoverageISC_DualSrcOutput; |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 1130 | desc.fFirstCoverageStage = firstCoverageStage; |
bsalomon@google.com | 271cffc | 2011-05-20 14:13:56 +0000 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 1134 | } |