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