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 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 10 | #include "GrEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 11 | #include "GrGLEffect.h" |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 12 | #include "SkRTConf.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 13 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 14 | |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 15 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 16 | SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false, |
| 17 | "Display program cache usage."); |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 18 | #endif |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 19 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 20 | typedef GrGLUniformManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 22 | struct GrGpuGL::ProgramCache::Entry { |
| 23 | SK_DECLARE_INST_COUNT_ROOT(Entry); |
| 24 | Entry() : fProgram(NULL), fLRUStamp(0) {} |
| 25 | |
| 26 | SkAutoTUnref<GrGLProgram> fProgram; |
| 27 | unsigned int fLRUStamp; |
| 28 | }; |
| 29 | |
| 30 | SK_DEFINE_INST_COUNT(GrGpuGL::ProgramCache::Entry); |
| 31 | |
| 32 | struct GrGpuGL::ProgramCache::ProgDescLess { |
| 33 | bool operator() (const GrGLProgramDesc& desc, const Entry* entry) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 34 | SkASSERT(NULL != entry->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 35 | return GrGLProgramDesc::Less(desc, entry->fProgram->getDesc()); |
| 36 | } |
| 37 | |
| 38 | bool operator() (const Entry* entry, const GrGLProgramDesc& desc) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 39 | SkASSERT(NULL != entry->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 40 | return GrGLProgramDesc::Less(entry->fProgram->getDesc(), desc); |
| 41 | } |
| 42 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 43 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 44 | GrGpuGL::ProgramCache::ProgramCache(GrGpuGL* gpu) |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 45 | : fCount(0) |
| 46 | , fCurrLRUStamp(0) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 47 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 48 | #ifdef PROGRAM_CACHE_STATS |
| 49 | , fTotalRequests(0) |
| 50 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 51 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 52 | #endif |
| 53 | { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 54 | for (int i = 0; i < 1 << kHashBits; ++i) { |
| 55 | fHashTable[i] = NULL; |
| 56 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | GrGpuGL::ProgramCache::~ProgramCache() { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 60 | for (int i = 0; i < fCount; ++i){ |
| 61 | SkDELETE(fEntries[i]); |
| 62 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 63 | // dump stats |
| 64 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 65 | if (c_DisplayCache) { |
| 66 | SkDebugf("--- Program Cache ---\n"); |
| 67 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 68 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 69 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 70 | 100.f * fCacheMisses / fTotalRequests : |
| 71 | 0.f); |
| 72 | int cacheHits = fTotalRequests - fCacheMisses; |
| 73 | SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f); |
| 74 | SkDebugf("---------------------\n"); |
| 75 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 76 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 77 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 78 | |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 79 | void GrGpuGL::ProgramCache::abandon() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 80 | for (int i = 0; i < fCount; ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 81 | SkASSERT(NULL != fEntries[i]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 82 | fEntries[i]->fProgram->abandon(); |
| 83 | fEntries[i]->fProgram.reset(NULL); |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 84 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 85 | fCount = 0; |
| 86 | } |
| 87 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 88 | int GrGpuGL::ProgramCache::search(const GrGLProgramDesc& desc) const { |
| 89 | ProgDescLess less; |
| 90 | return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); |
| 91 | } |
| 92 | |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 93 | GrGLProgram* GrGpuGL::ProgramCache::getProgram(const GrGLProgramDesc& desc, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 94 | const GrEffectStage* colorStages[], |
| 95 | const GrEffectStage* coverageStages[]) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 96 | #ifdef PROGRAM_CACHE_STATS |
| 97 | ++fTotalRequests; |
| 98 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 99 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 100 | Entry* entry = NULL; |
| 101 | |
| 102 | uint32_t hashIdx = desc.getChecksum(); |
| 103 | hashIdx ^= hashIdx >> 16; |
| 104 | if (kHashBits <= 8) { |
| 105 | hashIdx ^= hashIdx >> 8; |
| 106 | } |
| 107 | hashIdx &=((1 << kHashBits) - 1); |
| 108 | Entry* hashedEntry = fHashTable[hashIdx]; |
| 109 | if (NULL != hashedEntry && hashedEntry->fProgram->getDesc() == desc) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 110 | SkASSERT(NULL != hashedEntry->fProgram); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 111 | entry = hashedEntry; |
| 112 | } |
| 113 | |
| 114 | int entryIdx; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 115 | if (NULL == entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 116 | entryIdx = this->search(desc); |
| 117 | if (entryIdx >= 0) { |
| 118 | entry = fEntries[entryIdx]; |
| 119 | #ifdef PROGRAM_CACHE_STATS |
| 120 | ++fHashMisses; |
| 121 | #endif |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (NULL == entry) { |
| 126 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 127 | #ifdef PROGRAM_CACHE_STATS |
| 128 | ++fCacheMisses; |
| 129 | #endif |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 130 | GrGLProgram* program = GrGLProgram::Create(fGpu, desc, colorStages, coverageStages); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 131 | if (NULL == program) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 134 | int purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 135 | if (fCount < kMaxEntries) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 136 | entry = SkNEW(Entry); |
| 137 | purgeIdx = fCount++; |
| 138 | fEntries[purgeIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 139 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 140 | SkASSERT(fCount == kMaxEntries); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 141 | purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 142 | for (int i = 1; i < kMaxEntries; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 143 | if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { |
| 144 | purgeIdx = i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 145 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 146 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 147 | entry = fEntries[purgeIdx]; |
| 148 | int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); |
| 149 | if (fHashTable[purgedHashIdx] == entry) { |
| 150 | fHashTable[purgedHashIdx] = NULL; |
| 151 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 152 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 153 | SkASSERT(fEntries[purgeIdx] == entry); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 154 | entry->fProgram.reset(program); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 155 | // We need to shift fEntries around so that the entry currently at purgeIdx is placed |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 156 | // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). |
| 157 | entryIdx = ~entryIdx; |
| 158 | if (entryIdx < purgeIdx) { |
| 159 | // Let E and P be the entries at index entryIdx and purgeIdx, respectively. |
| 160 | // If the entries array looks like this: |
| 161 | // aaaaEbbbbbPccccc |
| 162 | // we rearrange it to look like this: |
| 163 | // aaaaPEbbbbbccccc |
| 164 | size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); |
| 165 | memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); |
| 166 | fEntries[entryIdx] = entry; |
| 167 | } else if (purgeIdx < entryIdx) { |
| 168 | // If the entries array looks like this: |
| 169 | // aaaaPbbbbbEccccc |
| 170 | // we rearrange it to look like this: |
| 171 | // aaaabbbbbPEccccc |
| 172 | size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); |
| 173 | memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); |
| 174 | fEntries[entryIdx - 1] = entry; |
| 175 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 176 | #ifdef SK_DEBUG |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 177 | SkASSERT(NULL != fEntries[0]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 178 | for (int i = 0; i < fCount - 1; ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 179 | SkASSERT(NULL != fEntries[i + 1]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 180 | const GrGLProgramDesc& a = fEntries[i]->fProgram->getDesc(); |
| 181 | const GrGLProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 182 | SkASSERT(GrGLProgramDesc::Less(a, b)); |
| 183 | SkASSERT(!GrGLProgramDesc::Less(b, a)); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 184 | } |
| 185 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 186 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 187 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 188 | fHashTable[hashIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 189 | entry->fLRUStamp = fCurrLRUStamp; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 190 | |
| 191 | if (SK_MaxU32 == fCurrLRUStamp) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 192 | // wrap around! just trash our LRU, one time hit. |
| 193 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 194 | fEntries[i]->fLRUStamp = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | ++fCurrLRUStamp; |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 198 | return entry->fProgram; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 199 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 200 | |
bsalomon@google.com | 1e257a5 | 2011-07-06 19:52:16 +0000 | [diff] [blame] | 201 | //////////////////////////////////////////////////////////////////////////////// |
| 202 | |
bsalomon@google.com | 5739d2c | 2012-05-31 15:07:19 +0000 | [diff] [blame] | 203 | void GrGpuGL::abandonResources(){ |
| 204 | INHERITED::abandonResources(); |
| 205 | fProgramCache->abandon(); |
| 206 | fHWProgramID = 0; |
| 207 | } |
| 208 | |
| 209 | //////////////////////////////////////////////////////////////////////////////// |
| 210 | |
bsalomon@google.com | 0b77d68 | 2011-08-19 13:28:54 +0000 | [diff] [blame] | 211 | #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X) |
| 212 | |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 213 | bool GrGpuGL::flushGraphicsState(DrawType type, const GrDeviceCoordTexture* dstCopy) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 214 | const GrDrawState& drawState = this->getDrawState(); |
| 215 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 216 | // GrGpu::setupClipAndFlushState should have already checked this and bailed if not true. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 217 | SkASSERT(NULL != drawState.getRenderTarget()); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 218 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 219 | if (kStencilPath_DrawType == type) { |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 220 | const GrRenderTarget* rt = this->getDrawState().getRenderTarget(); |
| 221 | SkISize size; |
| 222 | size.set(rt->width(), rt->height()); |
| 223 | this->setProjectionMatrix(drawState.getViewMatrix(), size, rt->origin()); |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 224 | } else { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 225 | this->flushMiscFixedFunctionState(); |
bsalomon@google.com | c96cb3a | 2012-06-04 19:31:00 +0000 | [diff] [blame] | 226 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 227 | GrBlendCoeff srcCoeff; |
| 228 | GrBlendCoeff dstCoeff; |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 229 | GrDrawState::BlendOptFlags blendOpts = drawState.getBlendOpts(false, &srcCoeff, &dstCoeff); |
| 230 | if (GrDrawState::kSkipDraw_BlendOptFlag & blendOpts) { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 231 | return false; |
| 232 | } |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 233 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 234 | SkSTArray<8, const GrEffectStage*, true> colorStages; |
| 235 | SkSTArray<8, const GrEffectStage*, true> coverageStages; |
bsalomon@google.com | 31ec798 | 2013-03-27 18:14:57 +0000 | [diff] [blame] | 236 | GrGLProgramDesc desc; |
| 237 | GrGLProgramDesc::Build(this->getDrawState(), |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 238 | kDrawPoints_DrawType == type, |
| 239 | blendOpts, |
| 240 | srcCoeff, |
| 241 | dstCoeff, |
| 242 | this, |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 243 | dstCopy, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 244 | &colorStages, |
| 245 | &coverageStages, |
bsalomon@google.com | 9120748 | 2013-02-12 21:45:24 +0000 | [diff] [blame] | 246 | &desc); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 247 | |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 248 | fCurrentProgram.reset(fProgramCache->getProgram(desc, |
| 249 | colorStages.begin(), |
| 250 | coverageStages.begin())); |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 251 | if (NULL == fCurrentProgram.get()) { |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 252 | SkDEBUGFAIL("Failed to create program!"); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 253 | return false; |
| 254 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 255 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 256 | SkASSERT(kDrawPath_DrawType != type || !fCurrentProgram->hasVertexShader()); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 257 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 258 | fCurrentProgram.get()->ref(); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 259 | |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 260 | GrGLuint programID = fCurrentProgram->programID(); |
| 261 | if (fHWProgramID != programID) { |
| 262 | GL_CALL(UseProgram(programID)); |
| 263 | fHWProgramID = programID; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 264 | } |
bsalomon@google.com | 6a51dcb | 2013-02-13 16:03:51 +0000 | [diff] [blame] | 265 | |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 266 | fCurrentProgram->overrideBlend(&srcCoeff, &dstCoeff); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 267 | this->flushBlend(kDrawLines_DrawType == type, srcCoeff, dstCoeff); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 268 | |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 269 | fCurrentProgram->setData(blendOpts, |
bsalomon@google.com | 2c84aa3 | 2013-06-06 20:28:57 +0000 | [diff] [blame] | 270 | colorStages.begin(), |
| 271 | coverageStages.begin(), |
| 272 | dstCopy, |
| 273 | &fSharedGLProgramState); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 274 | } |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 275 | this->flushStencil(type); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 276 | this->flushScissor(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 277 | this->flushAAState(type); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 278 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 279 | SkIRect* devRect = NULL; |
| 280 | SkIRect devClipBounds; |
robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 281 | if (drawState.isClipState()) { |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 282 | this->getClip()->getConservativeBounds(drawState.getRenderTarget(), &devClipBounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 283 | devRect = &devClipBounds; |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 284 | } |
| 285 | // This must come after textures are flushed because a texture may need |
| 286 | // to be msaa-resolved (which will modify bound FBO state). |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 287 | this->flushRenderTarget(devRect); |
bsalomon@google.com | 4c88378 | 2012-06-04 19:05:11 +0000 | [diff] [blame] | 288 | |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | |
bsalomon@google.com | 880b8fc | 2013-02-19 20:17:28 +0000 | [diff] [blame] | 292 | void GrGpuGL::setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes) { |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 293 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 294 | GrGLsizei stride = this->getDrawState().getVertexSize(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 295 | |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 296 | size_t vertexOffsetInBytes = stride * info.startVertex(); |
| 297 | |
| 298 | const GeometryPoolState& geoPoolState = this->getGeomPoolState(); |
| 299 | |
| 300 | GrGLVertexBuffer* vbuf; |
| 301 | switch (this->getGeomSrc().fVertexSrc) { |
| 302 | case kBuffer_GeometrySrcType: |
| 303 | vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer; |
| 304 | break; |
| 305 | case kArray_GeometrySrcType: |
| 306 | case kReserved_GeometrySrcType: |
| 307 | this->finalizeReservedVertices(); |
| 308 | vertexOffsetInBytes += geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize; |
| 309 | vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer; |
| 310 | break; |
| 311 | default: |
| 312 | vbuf = NULL; // suppress warning |
| 313 | GrCrash("Unknown geometry src type!"); |
| 314 | } |
| 315 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 316 | SkASSERT(NULL != vbuf); |
| 317 | SkASSERT(!vbuf->isLocked()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 318 | vertexOffsetInBytes += vbuf->baseOffset(); |
| 319 | |
| 320 | GrGLIndexBuffer* ibuf = NULL; |
| 321 | if (info.isIndexed()) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 322 | SkASSERT(NULL != indexOffsetInBytes); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 323 | |
| 324 | switch (this->getGeomSrc().fIndexSrc) { |
| 325 | case kBuffer_GeometrySrcType: |
| 326 | *indexOffsetInBytes = 0; |
| 327 | ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer; |
| 328 | break; |
| 329 | case kArray_GeometrySrcType: |
| 330 | case kReserved_GeometrySrcType: |
| 331 | this->finalizeReservedIndices(); |
| 332 | *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort); |
| 333 | ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer; |
| 334 | break; |
| 335 | default: |
| 336 | ibuf = NULL; // suppress warning |
| 337 | GrCrash("Unknown geometry src type!"); |
| 338 | } |
| 339 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 340 | SkASSERT(NULL != ibuf); |
| 341 | SkASSERT(!ibuf->isLocked()); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 342 | *indexOffsetInBytes += ibuf->baseOffset(); |
| 343 | } |
| 344 | GrGLAttribArrayState* attribState = |
| 345 | fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf); |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 346 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 347 | if (!fCurrentProgram->hasVertexShader()) { |
| 348 | int posIdx = this->getDrawState().positionAttributeIndex(); |
| 349 | const GrVertexAttrib* vertexArray = this->getDrawState().getVertexAttribs() + posIdx; |
| 350 | GrVertexAttribType vertexArrayType = vertexArray->fType; |
| 351 | SkASSERT(!GrGLAttribTypeToLayout(vertexArrayType).fNormalized); |
| 352 | SkASSERT(GrGLAttribTypeToLayout(vertexArrayType).fCount == 2); |
| 353 | attribState->setFixedFunctionVertexArray(this, |
| 354 | vbuf, |
| 355 | 2, |
| 356 | GrGLAttribTypeToLayout(vertexArrayType).fType, |
| 357 | stride, |
| 358 | reinterpret_cast<GrGLvoid*>( |
| 359 | vertexOffsetInBytes + vertexArray->fOffset)); |
| 360 | attribState->disableUnusedArrays(this, 0, true); |
| 361 | } else { |
| 362 | uint32_t usedAttribArraysMask = 0; |
| 363 | const GrVertexAttrib* vertexAttrib = this->getDrawState().getVertexAttribs(); |
| 364 | int vertexAttribCount = this->getDrawState().getVertexAttribCount(); |
| 365 | for (int vertexAttribIndex = 0; vertexAttribIndex < vertexAttribCount; |
| 366 | ++vertexAttribIndex, ++vertexAttrib) { |
robertphillips@google.com | af3a3b9 | 2013-02-28 23:08:28 +0000 | [diff] [blame] | 367 | |
commit-bot@chromium.org | 6b30e45 | 2013-10-04 20:02:53 +0000 | [diff] [blame] | 368 | usedAttribArraysMask |= (1 << vertexAttribIndex); |
| 369 | GrVertexAttribType attribType = vertexAttrib->fType; |
| 370 | attribState->set(this, |
| 371 | vertexAttribIndex, |
| 372 | vbuf, |
| 373 | GrGLAttribTypeToLayout(attribType).fCount, |
| 374 | GrGLAttribTypeToLayout(attribType).fType, |
| 375 | GrGLAttribTypeToLayout(attribType).fNormalized, |
| 376 | stride, |
| 377 | reinterpret_cast<GrGLvoid*>( |
| 378 | vertexOffsetInBytes + vertexAttrib->fOffset)); |
| 379 | } |
| 380 | |
| 381 | attribState->disableUnusedArrays(this, usedAttribArraysMask, false); |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 382 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 383 | } |