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