blob: a808aa8dda7ccf397006158b719f2f835b1eef92 [file] [log] [blame]
junov@google.comf93e7172011-03-31 21:26:24 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.comf93e7172011-03-31 21:26:24 +00006 */
7
bsalomon@google.com5739d2c2012-05-31 15:07:19 +00008#include "GrGpuGL.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
joshualitt47bb3822014-10-07 16:43:25 -070010#include "builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070011#include "GrProcessor.h"
12#include "GrGLProcessor.h"
egdaniel8a4c1032014-09-16 07:18:54 -070013#include "GrGLPathRendering.h"
egdaniel170f90b2014-09-16 12:54:40 -070014#include "GrOptDrawState.h"
15#include "SkRTConf.h"
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000016#include "SkTSearch.h"
junov@google.comf93e7172011-03-31 21:26:24 +000017
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000018#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000019SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false,
20 "Display program cache usage.");
jvanverth@google.coma2f4b152013-09-16 20:00:46 +000021#endif
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000022
kkinnunen7510b222014-07-30 00:04:16 -070023typedef GrGLProgramDataManager::UniformHandle UniformHandle;
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000024
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000025struct GrGpuGL::ProgramCache::Entry {
26 SK_DECLARE_INST_COUNT_ROOT(Entry);
27 Entry() : fProgram(NULL), fLRUStamp(0) {}
28
29 SkAutoTUnref<GrGLProgram> fProgram;
30 unsigned int fLRUStamp;
31};
32
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000033struct GrGpuGL::ProgramCache::ProgDescLess {
joshualitt79f8fae2014-10-28 17:59:26 -070034 bool operator() (const GrProgramDesc& desc, const Entry* entry) {
bsalomon49f085d2014-09-05 13:34:00 -070035 SkASSERT(entry->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -070036 return GrProgramDesc::Less(desc, entry->fProgram->getDesc());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000037 }
38
joshualitt79f8fae2014-10-28 17:59:26 -070039 bool operator() (const Entry* entry, const GrProgramDesc& desc) {
bsalomon49f085d2014-09-05 13:34:00 -070040 SkASSERT(entry->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -070041 return GrProgramDesc::Less(entry->fProgram->getDesc(), desc);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000042 }
43};
junov@google.comf93e7172011-03-31 21:26:24 +000044
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000045GrGpuGL::ProgramCache::ProgramCache(GrGpuGL* gpu)
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000046 : fCount(0)
47 , fCurrLRUStamp(0)
commit-bot@chromium.org9188a152013-09-05 18:28:24 +000048 , fGpu(gpu)
jvanverth@google.com94878772013-03-12 16:00:54 +000049#ifdef PROGRAM_CACHE_STATS
50 , fTotalRequests(0)
51 , fCacheMisses(0)
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000052 , fHashMisses(0)
jvanverth@google.com94878772013-03-12 16:00:54 +000053#endif
54{
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000055 for (int i = 0; i < 1 << kHashBits; ++i) {
56 fHashTable[i] = NULL;
57 }
jvanverth@google.com94878772013-03-12 16:00:54 +000058}
59
60GrGpuGL::ProgramCache::~ProgramCache() {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000061 for (int i = 0; i < fCount; ++i){
62 SkDELETE(fEntries[i]);
63 }
jvanverth@google.com94878772013-03-12 16:00:54 +000064 // dump stats
65#ifdef PROGRAM_CACHE_STATS
jvanverth@google.com5c9b6fa2013-09-16 19:40:31 +000066 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.com94878772013-03-12 16:00:54 +000077#endif
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000078}
junov@google.comf93e7172011-03-31 21:26:24 +000079
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000080void GrGpuGL::ProgramCache::abandon() {
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000081 for (int i = 0; i < fCount; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -070082 SkASSERT(fEntries[i]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000083 fEntries[i]->fProgram->abandon();
bsalomon944bcf02014-07-29 08:01:52 -070084 SkDELETE(fEntries[i]);
bsalomon@google.comecb60aa2012-07-18 13:20:29 +000085 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +000086 fCount = 0;
87}
88
joshualitt79f8fae2014-10-28 17:59:26 -070089int GrGpuGL::ProgramCache::search(const GrProgramDesc& desc) const {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000090 ProgDescLess less;
91 return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less);
92}
93
joshualittdafa4d02014-12-04 08:59:10 -080094GrGLProgram* GrGpuGL::ProgramCache::getProgram(const GrOptDrawState& optState) {
jvanverth@google.com94878772013-03-12 16:00:54 +000095#ifdef PROGRAM_CACHE_STATS
96 ++fTotalRequests;
97#endif
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +000098
bsalomon@google.com2db3ded2013-05-22 14:34:04 +000099 Entry* entry = NULL;
100
joshualitt79f8fae2014-10-28 17:59:26 -0700101 uint32_t hashIdx = optState.programDesc().getChecksum();
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000102 hashIdx ^= hashIdx >> 16;
103 if (kHashBits <= 8) {
104 hashIdx ^= hashIdx >> 8;
105 }
106 hashIdx &=((1 << kHashBits) - 1);
107 Entry* hashedEntry = fHashTable[hashIdx];
joshualitt79f8fae2014-10-28 17:59:26 -0700108 if (hashedEntry && hashedEntry->fProgram->getDesc() == optState.programDesc()) {
bsalomon49f085d2014-09-05 13:34:00 -0700109 SkASSERT(hashedEntry->fProgram);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000110 entry = hashedEntry;
111 }
112
113 int entryIdx;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000114 if (NULL == entry) {
joshualitt79f8fae2014-10-28 17:59:26 -0700115 entryIdx = this->search(optState.programDesc());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000116 if (entryIdx >= 0) {
117 entry = fEntries[entryIdx];
118#ifdef PROGRAM_CACHE_STATS
119 ++fHashMisses;
120#endif
121 }
122 }
123
124 if (NULL == entry) {
125 // We have a cache miss
jvanverth@google.com94878772013-03-12 16:00:54 +0000126#ifdef PROGRAM_CACHE_STATS
127 ++fCacheMisses;
128#endif
joshualittdafa4d02014-12-04 08:59:10 -0800129 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(optState, fGpu);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000130 if (NULL == program) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000131 return NULL;
132 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000133 int purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000134 if (fCount < kMaxEntries) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000135 entry = SkNEW(Entry);
136 purgeIdx = fCount++;
137 fEntries[purgeIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000138 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000139 SkASSERT(fCount == kMaxEntries);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000140 purgeIdx = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000141 for (int i = 1; i < kMaxEntries; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000142 if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) {
143 purgeIdx = i;
junov@google.comf93e7172011-03-31 21:26:24 +0000144 }
junov@google.comf93e7172011-03-31 21:26:24 +0000145 }
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000146 entry = fEntries[purgeIdx];
147 int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1);
148 if (fHashTable[purgedHashIdx] == entry) {
149 fHashTable[purgedHashIdx] = NULL;
150 }
junov@google.comf93e7172011-03-31 21:26:24 +0000151 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000152 SkASSERT(fEntries[purgeIdx] == entry);
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000153 entry->fProgram.reset(program);
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000154 // We need to shift fEntries around so that the entry currently at purgeIdx is placed
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000155 // just before the entry at ~entryIdx (in order to keep fEntries sorted by descriptor).
156 entryIdx = ~entryIdx;
157 if (entryIdx < purgeIdx) {
158 // Let E and P be the entries at index entryIdx and purgeIdx, respectively.
159 // If the entries array looks like this:
160 // aaaaEbbbbbPccccc
161 // we rearrange it to look like this:
162 // aaaaPEbbbbbccccc
163 size_t copySize = (purgeIdx - entryIdx) * sizeof(Entry*);
164 memmove(fEntries + entryIdx + 1, fEntries + entryIdx, copySize);
165 fEntries[entryIdx] = entry;
166 } else if (purgeIdx < entryIdx) {
167 // If the entries array looks like this:
168 // aaaaPbbbbbEccccc
169 // we rearrange it to look like this:
170 // aaaabbbbbPEccccc
171 size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*);
172 memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize);
173 fEntries[entryIdx - 1] = entry;
174 }
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000175#ifdef SK_DEBUG
bsalomon49f085d2014-09-05 13:34:00 -0700176 SkASSERT(fEntries[0]->fProgram.get());
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000177 for (int i = 0; i < fCount - 1; ++i) {
bsalomon49f085d2014-09-05 13:34:00 -0700178 SkASSERT(fEntries[i + 1]->fProgram.get());
joshualitt79f8fae2014-10-28 17:59:26 -0700179 const GrProgramDesc& a = fEntries[i]->fProgram->getDesc();
180 const GrProgramDesc& b = fEntries[i + 1]->fProgram->getDesc();
181 SkASSERT(GrProgramDesc::Less(a, b));
182 SkASSERT(!GrProgramDesc::Less(b, a));
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000183 }
184#endif
junov@google.comf93e7172011-03-31 21:26:24 +0000185 }
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000186
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000187 fHashTable[hashIdx] = entry;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000188 entry->fLRUStamp = fCurrLRUStamp;
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000189
190 if (SK_MaxU32 == fCurrLRUStamp) {
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000191 // wrap around! just trash our LRU, one time hit.
192 for (int i = 0; i < fCount; ++i) {
bsalomon@google.com2db3ded2013-05-22 14:34:04 +0000193 fEntries[i]->fLRUStamp = 0;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000194 }
195 }
196 ++fCurrLRUStamp;
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000197 return entry->fProgram;
bsalomon@google.comc1d2a582012-06-01 15:08:19 +0000198}
junov@google.comf93e7172011-03-31 21:26:24 +0000199
bsalomon@google.com1e257a52011-07-06 19:52:16 +0000200////////////////////////////////////////////////////////////////////////////////
201
bsalomon@google.com0b77d682011-08-19 13:28:54 +0000202#define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
203
joshualittdafa4d02014-12-04 08:59:10 -0800204bool GrGpuGL::flushGraphicsState(const GrOptDrawState& optState) {
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000205 // GrGpu::setupClipAndFlushState should have already checked this and bailed if not true.
joshualittd53a8272014-11-10 16:03:14 -0800206 SkASSERT(optState.getRenderTarget());
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000207
joshualittdafa4d02014-12-04 08:59:10 -0800208 if (kStencilPath_DrawType == optState.drawType()) {
joshualittd53a8272014-11-10 16:03:14 -0800209 const GrRenderTarget* rt = optState.getRenderTarget();
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000210 SkISize size;
211 size.set(rt->width(), rt->height());
joshualittd53a8272014-11-10 16:03:14 -0800212 this->glPathRendering()->setProjectionMatrix(optState.getViewMatrix(), size, rt->origin());
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000213 } else {
joshualittd53a8272014-11-10 16:03:14 -0800214 this->flushMiscFixedFunctionState(optState);
bsalomon@google.comc96cb3a2012-06-04 19:31:00 +0000215
joshualittd53a8272014-11-10 16:03:14 -0800216 GrBlendCoeff srcCoeff = optState.getSrcBlendCoeff();
217 GrBlendCoeff dstCoeff = optState.getDstBlendCoeff();
egdaniel170f90b2014-09-16 12:54:40 -0700218
joshualittdafa4d02014-12-04 08:59:10 -0800219 fCurrentProgram.reset(fProgramCache->getProgram(optState));
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000220 if (NULL == fCurrentProgram.get()) {
mtklein@google.com330313a2013-08-22 15:37:26 +0000221 SkDEBUGFAIL("Failed to create program!");
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000222 return false;
223 }
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000224
bsalomon@google.com9ba4fa62012-07-16 17:36:28 +0000225 fCurrentProgram.get()->ref();
junov@google.comf93e7172011-03-31 21:26:24 +0000226
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000227 GrGLuint programID = fCurrentProgram->programID();
228 if (fHWProgramID != programID) {
229 GL_CALL(UseProgram(programID));
230 fHWProgramID = programID;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000231 }
bsalomon@google.com6a51dcb2013-02-13 16:03:51 +0000232
joshualittdafa4d02014-12-04 08:59:10 -0800233 this->flushBlend(optState, kDrawLines_DrawType == optState.drawType(), srcCoeff, dstCoeff);
junov@google.comf93e7172011-03-31 21:26:24 +0000234
joshualittdafa4d02014-12-04 08:59:10 -0800235 fCurrentProgram->setData(optState);
junov@google.comf93e7172011-03-31 21:26:24 +0000236 }
bsalomonb0bd4f62014-09-03 07:19:50 -0700237
joshualittd53a8272014-11-10 16:03:14 -0800238 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(optState.getRenderTarget());
joshualittdafa4d02014-12-04 08:59:10 -0800239 this->flushStencil(optState.getStencil(), optState.drawType());
joshualitt54e0c122014-11-19 09:38:51 -0800240 this->flushScissor(optState.getScissorState(), glRT->getViewport(), glRT->origin());
joshualittdafa4d02014-12-04 08:59:10 -0800241 this->flushAAState(optState);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000242
bsalomon@google.com4c883782012-06-04 19:05:11 +0000243 // This must come after textures are flushed because a texture may need
244 // to be msaa-resolved (which will modify bound FBO state).
joshualitt2c93efe2014-11-06 12:57:13 -0800245 this->flushRenderTarget(glRT, NULL);
bsalomon@google.com4c883782012-06-04 19:05:11 +0000246
junov@google.comf93e7172011-03-31 21:26:24 +0000247 return true;
248}
249
joshualittd53a8272014-11-10 16:03:14 -0800250void GrGpuGL::setupGeometry(const GrOptDrawState& optState,
251 const GrDrawTarget::DrawInfo& info,
252 size_t* indexOffsetInBytes) {
bsalomon@google.com6918d482013-03-07 19:09:11 +0000253 GrGLVertexBuffer* vbuf;
joshualitt7eb8c7b2014-11-18 14:24:27 -0800254 vbuf = (GrGLVertexBuffer*) info.vertexBuffer();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000255
bsalomon49f085d2014-09-05 13:34:00 -0700256 SkASSERT(vbuf);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000257 SkASSERT(!vbuf->isMapped());
bsalomon@google.com6918d482013-03-07 19:09:11 +0000258
259 GrGLIndexBuffer* ibuf = NULL;
260 if (info.isIndexed()) {
bsalomon49f085d2014-09-05 13:34:00 -0700261 SkASSERT(indexOffsetInBytes);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000262
joshualittf4e5e332014-11-07 12:58:46 -0800263 *indexOffsetInBytes = 0;
joshualitt7eb8c7b2014-11-18 14:24:27 -0800264 ibuf = (GrGLIndexBuffer*)info.indexBuffer();
bsalomon@google.com6918d482013-03-07 19:09:11 +0000265
bsalomon49f085d2014-09-05 13:34:00 -0700266 SkASSERT(ibuf);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000267 SkASSERT(!ibuf->isMapped());
bsalomon@google.com6918d482013-03-07 19:09:11 +0000268 *indexOffsetInBytes += ibuf->baseOffset();
269 }
270 GrGLAttribArrayState* attribState =
271 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
junov@google.comf93e7172011-03-31 21:26:24 +0000272
commit-bot@chromium.org0a6fe712014-04-23 19:26:26 +0000273 if (fCurrentProgram->hasVertexShader()) {
joshualitt2dd1ae02014-12-03 06:24:10 -0800274 const GrGeometryProcessor* gp = optState.getGeometryProcessor();
egdaniel02cafcc2014-07-21 11:37:28 -0700275
joshualitt2dd1ae02014-12-03 06:24:10 -0800276 GrGLsizei stride = static_cast<GrGLsizei>(gp->getVertexStride());
277
278 size_t vertexOffsetInBytes = stride * info.startVertex();
279
280 vertexOffsetInBytes += vbuf->baseOffset();
281
282 const SkTArray<GrGeometryProcessor::GrAttribute, true>& attribs = gp->getAttribs();
283 int vaCount = attribs.count();
284 uint32_t usedAttribArraysMask = 0;
285 size_t offset = 0;
286
287 for (int attribIndex = 0; attribIndex < vaCount; attribIndex++) {
288 usedAttribArraysMask |= (1 << attribIndex);
289 GrVertexAttribType attribType = attribs[attribIndex].fType;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000290 attribState->set(this,
joshualitt2dd1ae02014-12-03 06:24:10 -0800291 attribIndex,
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000292 vbuf,
293 GrGLAttribTypeToLayout(attribType).fCount,
294 GrGLAttribTypeToLayout(attribType).fType,
295 GrGLAttribTypeToLayout(attribType).fNormalized,
296 stride,
joshualitt2dd1ae02014-12-03 06:24:10 -0800297 reinterpret_cast<GrGLvoid*>(vertexOffsetInBytes + offset));
298 offset += attribs[attribIndex].fOffset;
commit-bot@chromium.org6b30e452013-10-04 20:02:53 +0000299 }
commit-bot@chromium.org6ebfbf92014-02-24 12:05:02 +0000300 attribState->disableUnusedArrays(this, usedAttribArraysMask);
bsalomon@google.com6918d482013-03-07 19:09:11 +0000301 }
junov@google.comf93e7172011-03-31 21:26:24 +0000302}
joshualitt79f8fae2014-10-28 17:59:26 -0700303
304void GrGpuGL::buildProgramDesc(const GrOptDrawState& optState,
305 const GrProgramDesc::DescInfo& descInfo,
306 GrGpu::DrawType drawType,
joshualitt79f8fae2014-10-28 17:59:26 -0700307 GrProgramDesc* desc) {
joshualitt9176e2c2014-11-20 07:28:52 -0800308 if (!GrGLProgramDescBuilder::Build(optState, descInfo, drawType, this, desc)) {
joshualitt79f8fae2014-10-28 17:59:26 -0700309 SkDEBUGFAIL("Failed to generate GL program descriptor");
310 }
311}