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 | |
jvanverth | 39edf76 | 2014-12-22 11:44:19 -0800 | [diff] [blame] | 8 | #include "GrGLGpu.h" |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
joshualitt | 47bb382 | 2014-10-07 16:43:25 -0700 | [diff] [blame] | 10 | #include "builders/GrGLProgramBuilder.h" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 11 | #include "GrProcessor.h" |
egdaniel | 8a4c103 | 2014-09-16 07:18:54 -0700 | [diff] [blame] | 12 | #include "GrGLPathRendering.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 14 | #include "glsl/GrGLSLProgramDataManager.h" |
egdaniel | 170f90b | 2014-09-16 12:54:40 -0700 | [diff] [blame] | 15 | #include "SkRTConf.h" |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 16 | #include "SkTSearch.h" |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 17 | |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 18 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 19 | SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false, |
| 20 | "Display program cache usage."); |
jvanverth@google.com | a2f4b15 | 2013-09-16 20:00:46 +0000 | [diff] [blame] | 21 | #endif |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 22 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 23 | typedef GrGLSLProgramDataManager::UniformHandle UniformHandle; |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 24 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 25 | struct GrGLGpu::ProgramCache::Entry { |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 26 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | Entry() : fProgram(nullptr), fLRUStamp(0) {} |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 28 | |
| 29 | SkAutoTUnref<GrGLProgram> fProgram; |
| 30 | unsigned int fLRUStamp; |
| 31 | }; |
| 32 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 33 | struct GrGLGpu::ProgramCache::ProgDescLess { |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 34 | bool operator() (const GrProgramDesc& desc, const Entry* entry) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 35 | SkASSERT(entry->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 36 | return GrProgramDesc::Less(desc, entry->fProgram->getDesc()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 37 | } |
| 38 | |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 39 | bool operator() (const Entry* entry, const GrProgramDesc& desc) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 40 | SkASSERT(entry->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 41 | return GrProgramDesc::Less(entry->fProgram->getDesc(), desc); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 42 | } |
| 43 | }; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 44 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 45 | GrGLGpu::ProgramCache::ProgramCache(GrGLGpu* gpu) |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 46 | : fCount(0) |
| 47 | , fCurrLRUStamp(0) |
commit-bot@chromium.org | 9188a15 | 2013-09-05 18:28:24 +0000 | [diff] [blame] | 48 | , fGpu(gpu) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 49 | #ifdef PROGRAM_CACHE_STATS |
| 50 | , fTotalRequests(0) |
| 51 | , fCacheMisses(0) |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 52 | , fHashMisses(0) |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 53 | #endif |
| 54 | { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 55 | for (int i = 0; i < 1 << kHashBits; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | fHashTable[i] = nullptr; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 57 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 58 | } |
| 59 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 60 | GrGLGpu::ProgramCache::~ProgramCache() { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 61 | for (int i = 0; i < fCount; ++i){ |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 62 | delete fEntries[i]; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 63 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 64 | // dump stats |
| 65 | #ifdef PROGRAM_CACHE_STATS |
jvanverth@google.com | 5c9b6fa | 2013-09-16 19:40:31 +0000 | [diff] [blame] | 66 | if (c_DisplayCache) { |
| 67 | SkDebugf("--- Program Cache ---\n"); |
| 68 | SkDebugf("Total requests: %d\n", fTotalRequests); |
| 69 | SkDebugf("Cache misses: %d\n", fCacheMisses); |
| 70 | SkDebugf("Cache miss %%: %f\n", (fTotalRequests > 0) ? |
| 71 | 100.f * fCacheMisses / fTotalRequests : |
| 72 | 0.f); |
| 73 | int cacheHits = fTotalRequests - fCacheMisses; |
| 74 | SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f); |
| 75 | SkDebugf("---------------------\n"); |
| 76 | } |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 77 | #endif |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 78 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 79 | |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 80 | void GrGLGpu::ProgramCache::reset() { |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 81 | for (int i = 0; i < fCount; ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 82 | SkASSERT(fEntries[i]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 83 | fEntries[i]->fProgram->abandon(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 84 | delete fEntries[i]; |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 85 | fEntries[i] = nullptr; |
bsalomon@google.com | ecb60aa | 2012-07-18 13:20:29 +0000 | [diff] [blame] | 86 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 87 | fCount = 0; |
joshualitt | 8fd844f | 2015-12-02 13:36:47 -0800 | [diff] [blame] | 88 | |
| 89 | // zero out hash table |
| 90 | for (int i = 0; i < 1 << kHashBits; i++) { |
| 91 | fHashTable[i] = nullptr; |
| 92 | } |
| 93 | |
| 94 | fCurrLRUStamp = 0; |
| 95 | #ifdef PROGRAM_CACHE_STATS |
| 96 | fTotalRequests = 0; |
| 97 | fCacheMisses = 0; |
| 98 | fHashMisses = 0; |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | void GrGLGpu::ProgramCache::abandon() { |
| 103 | this->reset(); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
bsalomon | 861e103 | 2014-12-16 07:33:49 -0800 | [diff] [blame] | 106 | int GrGLGpu::ProgramCache::search(const GrProgramDesc& desc) const { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 107 | ProgDescLess less; |
| 108 | return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less); |
| 109 | } |
| 110 | |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 111 | GrGLProgram* GrGLGpu::ProgramCache::refProgram(const DrawArgs& args) { |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 112 | #ifdef PROGRAM_CACHE_STATS |
| 113 | ++fTotalRequests; |
| 114 | #endif |
bsalomon@google.com | 9ba4fa6 | 2012-07-16 17:36:28 +0000 | [diff] [blame] | 115 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 116 | Entry* entry = nullptr; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 117 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 118 | uint32_t hashIdx = args.fDesc->getChecksum(); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 119 | hashIdx ^= hashIdx >> 16; |
| 120 | if (kHashBits <= 8) { |
| 121 | hashIdx ^= hashIdx >> 8; |
| 122 | } |
| 123 | hashIdx &=((1 << kHashBits) - 1); |
| 124 | Entry* hashedEntry = fHashTable[hashIdx]; |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 125 | if (hashedEntry && hashedEntry->fProgram->getDesc() == *args.fDesc) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 126 | SkASSERT(hashedEntry->fProgram); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 127 | entry = hashedEntry; |
| 128 | } |
| 129 | |
| 130 | int entryIdx; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 131 | if (nullptr == entry) { |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 132 | entryIdx = this->search(*args.fDesc); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 133 | if (entryIdx >= 0) { |
| 134 | entry = fEntries[entryIdx]; |
| 135 | #ifdef PROGRAM_CACHE_STATS |
| 136 | ++fHashMisses; |
| 137 | #endif |
| 138 | } |
| 139 | } |
| 140 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 141 | if (nullptr == entry) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 142 | // We have a cache miss |
jvanverth@google.com | 9487877 | 2013-03-12 16:00:54 +0000 | [diff] [blame] | 143 | #ifdef PROGRAM_CACHE_STATS |
| 144 | ++fCacheMisses; |
| 145 | #endif |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 146 | GrGLProgram* program = GrGLProgramBuilder::CreateProgram(args, fGpu); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 147 | if (nullptr == program) { |
| 148 | return nullptr; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 149 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 150 | int purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 151 | if (fCount < kMaxEntries) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 152 | entry = new Entry; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 153 | purgeIdx = fCount++; |
| 154 | fEntries[purgeIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 155 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 156 | SkASSERT(fCount == kMaxEntries); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 157 | purgeIdx = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 158 | for (int i = 1; i < kMaxEntries; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 159 | if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) { |
| 160 | purgeIdx = i; |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 161 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 162 | } |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 163 | entry = fEntries[purgeIdx]; |
| 164 | int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1); |
| 165 | if (fHashTable[purgedHashIdx] == entry) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 166 | fHashTable[purgedHashIdx] = nullptr; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 167 | } |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 168 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 169 | SkASSERT(fEntries[purgeIdx] == entry); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 170 | entry->fProgram.reset(program); |
skia.committer@gmail.com | 2d816ad | 2013-05-23 07:01:22 +0000 | [diff] [blame] | 171 | // 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] | 172 | // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor). |
| 173 | entryIdx = ~entryIdx; |
| 174 | if (entryIdx < purgeIdx) { |
| 175 | // Let E and P be the entries at index entryIdx and purgeIdx, respectively. |
| 176 | // If the entries array looks like this: |
| 177 | // aaaaEbbbbbPccccc |
| 178 | // we rearrange it to look like this: |
| 179 | // aaaaPEbbbbbccccc |
| 180 | size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*); |
| 181 | memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize); |
| 182 | fEntries[entryIdx] = entry; |
| 183 | } else if (purgeIdx < entryIdx) { |
| 184 | // If the entries array looks like this: |
| 185 | // aaaaPbbbbbEccccc |
| 186 | // we rearrange it to look like this: |
| 187 | // aaaabbbbbPEccccc |
| 188 | size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*); |
| 189 | memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize); |
| 190 | fEntries[entryIdx - 1] = entry; |
| 191 | } |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 192 | #ifdef SK_DEBUG |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 193 | SkASSERT(fEntries[0]->fProgram.get()); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 194 | for (int i = 0; i < fCount - 1; ++i) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 195 | SkASSERT(fEntries[i + 1]->fProgram.get()); |
joshualitt | 79f8fae | 2014-10-28 17:59:26 -0700 | [diff] [blame] | 196 | const GrProgramDesc& a = fEntries[i]->fProgram->getDesc(); |
| 197 | const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc(); |
| 198 | SkASSERT(GrProgramDesc::Less(a, b)); |
| 199 | SkASSERT(!GrProgramDesc::Less(b, a)); |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 200 | } |
| 201 | #endif |
junov@google.com | f93e717 | 2011-03-31 21:26:24 +0000 | [diff] [blame] | 202 | } |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 203 | |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 204 | fHashTable[hashIdx] = entry; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 205 | entry->fLRUStamp = fCurrLRUStamp; |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 206 | |
| 207 | if (SK_MaxU32 == fCurrLRUStamp) { |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 208 | // wrap around! just trash our LRU, one time hit. |
| 209 | for (int i = 0; i < fCount; ++i) { |
bsalomon@google.com | 2db3ded | 2013-05-22 14:34:04 +0000 | [diff] [blame] | 210 | fEntries[i]->fLRUStamp = 0; |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | ++fCurrLRUStamp; |
bsalomon | 6df8640 | 2015-06-01 10:41:49 -0700 | [diff] [blame] | 214 | return SkRef(entry->fProgram.get()); |
bsalomon@google.com | c1d2a58 | 2012-06-01 15:08:19 +0000 | [diff] [blame] | 215 | } |