2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "builders/GrGLProgramBuilder.h"
11 #include "GrProcessor.h"
12 #include "GrGLProcessor.h"
13 #include "GrGLPathRendering.h"
14 #include "GrOptDrawState.h"
16 #include "SkTSearch.h"
18 #ifdef PROGRAM_CACHE_STATS
19 SK_CONF_DECLARE(bool, c_DisplayCache, "gpu.displayCache", false,
20 "Display program cache usage.");
23 typedef GrGLProgramDataManager::UniformHandle UniformHandle;
25 struct GrGpuGL::ProgramCache::Entry {
26 SK_DECLARE_INST_COUNT_ROOT(Entry);
27 Entry() : fProgram(NULL), fLRUStamp(0) {}
29 SkAutoTUnref<GrGLProgram> fProgram;
30 unsigned int fLRUStamp;
33 struct GrGpuGL::ProgramCache::ProgDescLess {
34 bool operator() (const GrProgramDesc& desc, const Entry* entry) {
35 SkASSERT(entry->fProgram.get());
36 return GrProgramDesc::Less(desc, entry->fProgram->getDesc());
39 bool operator() (const Entry* entry, const GrProgramDesc& desc) {
40 SkASSERT(entry->fProgram.get());
41 return GrProgramDesc::Less(entry->fProgram->getDesc(), desc);
45 GrGpuGL::ProgramCache::ProgramCache(GrGpuGL* gpu)
49 #ifdef PROGRAM_CACHE_STATS
55 for (int i = 0; i < 1 << kHashBits; ++i) {
60 GrGpuGL::ProgramCache::~ProgramCache() {
61 for (int i = 0; i < fCount; ++i){
62 SkDELETE(fEntries[i]);
65 #ifdef PROGRAM_CACHE_STATS
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 :
73 int cacheHits = fTotalRequests - fCacheMisses;
74 SkDebugf("Hash miss %%: %f\n", (cacheHits > 0) ? 100.f * fHashMisses / cacheHits : 0.f);
75 SkDebugf("---------------------\n");
80 void GrGpuGL::ProgramCache::abandon() {
81 for (int i = 0; i < fCount; ++i) {
82 SkASSERT(fEntries[i]->fProgram.get());
83 fEntries[i]->fProgram->abandon();
84 SkDELETE(fEntries[i]);
89 int GrGpuGL::ProgramCache::search(const GrProgramDesc& desc) const {
91 return SkTSearch(fEntries, fCount, desc, sizeof(Entry*), less);
94 GrGLProgram* GrGpuGL::ProgramCache::getProgram(const GrOptDrawState& optState, DrawType type) {
95 #ifdef PROGRAM_CACHE_STATS
101 uint32_t hashIdx = optState.programDesc().getChecksum();
102 hashIdx ^= hashIdx >> 16;
103 if (kHashBits <= 8) {
104 hashIdx ^= hashIdx >> 8;
106 hashIdx &=((1 << kHashBits) - 1);
107 Entry* hashedEntry = fHashTable[hashIdx];
108 if (hashedEntry && hashedEntry->fProgram->getDesc() == optState.programDesc()) {
109 SkASSERT(hashedEntry->fProgram);
115 entryIdx = this->search(optState.programDesc());
117 entry = fEntries[entryIdx];
118 #ifdef PROGRAM_CACHE_STATS
125 // We have a cache miss
126 #ifdef PROGRAM_CACHE_STATS
129 GrGLProgram* program = GrGLProgramBuilder::CreateProgram(optState, type, fGpu);
130 if (NULL == program) {
134 if (fCount < kMaxEntries) {
135 entry = SkNEW(Entry);
137 fEntries[purgeIdx] = entry;
139 SkASSERT(fCount == kMaxEntries);
141 for (int i = 1; i < kMaxEntries; ++i) {
142 if (fEntries[i]->fLRUStamp < fEntries[purgeIdx]->fLRUStamp) {
146 entry = fEntries[purgeIdx];
147 int purgedHashIdx = entry->fProgram->getDesc().getChecksum() & ((1 << kHashBits) - 1);
148 if (fHashTable[purgedHashIdx] == entry) {
149 fHashTable[purgedHashIdx] = NULL;
152 SkASSERT(fEntries[purgeIdx] == entry);
153 entry->fProgram.reset(program);
154 // We need to shift fEntries around so that the entry currently at purgeIdx is placed
155 // 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:
161 // we rearrange it to look like this:
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:
169 // we rearrange it to look like this:
171 size_t copySize = (entryIdx - purgeIdx - 1) * sizeof(Entry*);
172 memmove(fEntries + purgeIdx, fEntries + purgeIdx + 1, copySize);
173 fEntries[entryIdx - 1] = entry;
176 SkASSERT(fEntries[0]->fProgram.get());
177 for (int i = 0; i < fCount - 1; ++i) {
178 SkASSERT(fEntries[i + 1]->fProgram.get());
179 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));
187 fHashTable[hashIdx] = entry;
188 entry->fLRUStamp = fCurrLRUStamp;
190 if (SK_MaxU32 == fCurrLRUStamp) {
191 // wrap around! just trash our LRU, one time hit.
192 for (int i = 0; i < fCount; ++i) {
193 fEntries[i]->fLRUStamp = 0;
197 return entry->fProgram;
200 ////////////////////////////////////////////////////////////////////////////////
202 #define GL_CALL(X) GR_GL_CALL(this->glInterface(), X)
204 bool GrGpuGL::flushGraphicsState(DrawType type,
205 const GrClipMaskManager::ScissorState& scissorState,
206 const GrDeviceCoordTexture* dstCopy) {
207 SkAutoTUnref<GrOptDrawState> optState(GrOptDrawState::Create(this->getDrawState(),
216 // GrGpu::setupClipAndFlushState should have already checked this and bailed if not true.
217 SkASSERT(optState->getRenderTarget());
219 if (kStencilPath_DrawType == type) {
220 const GrRenderTarget* rt = optState->getRenderTarget();
222 size.set(rt->width(), rt->height());
223 this->glPathRendering()->setProjectionMatrix(optState->getViewMatrix(), size, rt->origin());
225 this->flushMiscFixedFunctionState(*optState.get());
227 GrBlendCoeff srcCoeff = optState->getSrcBlendCoeff();
228 GrBlendCoeff dstCoeff = optState->getDstBlendCoeff();
230 // In these blend coeff's we end up drawing nothing so we can skip draw all together
231 if (kZero_GrBlendCoeff == srcCoeff && kOne_GrBlendCoeff == dstCoeff &&
232 !optState->getStencil().doesWrite()) {
236 fCurrentProgram.reset(fProgramCache->getProgram(*optState.get(), type));
237 if (NULL == fCurrentProgram.get()) {
238 SkDEBUGFAIL("Failed to create program!");
242 fCurrentProgram.get()->ref();
244 GrGLuint programID = fCurrentProgram->programID();
245 if (fHWProgramID != programID) {
246 GL_CALL(UseProgram(programID));
247 fHWProgramID = programID;
250 this->flushBlend(*optState.get(), kDrawLines_DrawType == type, srcCoeff, dstCoeff);
252 fCurrentProgram->setData(*optState.get(), type, dstCopy);
255 GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(optState->getRenderTarget());
256 this->flushStencil(optState->getStencil(), type);
257 this->flushScissor(scissorState, glRT->getViewport(), glRT->origin());
258 this->flushAAState(*optState.get(), type);
260 SkIRect* devRect = NULL;
261 SkIRect devClipBounds;
262 if (optState->isClipState()) {
263 this->getClip()->getConservativeBounds(optState->getRenderTarget(), &devClipBounds);
264 devRect = &devClipBounds;
266 // This must come after textures are flushed because a texture may need
267 // to be msaa-resolved (which will modify bound FBO state).
268 this->flushRenderTarget(glRT, devRect);
273 void GrGpuGL::setupGeometry(const DrawInfo& info, size_t* indexOffsetInBytes) {
274 SkAutoTUnref<GrOptDrawState> optState(
275 GrOptDrawState::Create(this->getDrawState(), this, info.getDstCopy(),
276 PrimTypeToDrawType(info.primitiveType())));
278 // If the optState would is NULL it should have been caught in flushGraphicsState before getting
282 GrGLsizei stride = static_cast<GrGLsizei>(optState->getVertexStride());
284 size_t vertexOffsetInBytes = stride * info.startVertex();
286 const GeometryPoolState& geoPoolState = this->getGeomPoolState();
288 GrGLVertexBuffer* vbuf;
289 switch (this->getGeomSrc().fVertexSrc) {
290 case kBuffer_GeometrySrcType:
291 vbuf = (GrGLVertexBuffer*) this->getGeomSrc().fVertexBuffer;
293 case kReserved_GeometrySrcType:
294 this->finalizeReservedVertices();
295 vertexOffsetInBytes += geoPoolState.fPoolStartVertex * this->getGeomSrc().fVertexSize;
296 vbuf = (GrGLVertexBuffer*) geoPoolState.fPoolVertexBuffer;
299 vbuf = NULL; // suppress warning
300 SkFAIL("Unknown geometry src type!");
304 SkASSERT(!vbuf->isMapped());
305 vertexOffsetInBytes += vbuf->baseOffset();
307 GrGLIndexBuffer* ibuf = NULL;
308 if (info.isIndexed()) {
309 SkASSERT(indexOffsetInBytes);
311 switch (this->getGeomSrc().fIndexSrc) {
312 case kBuffer_GeometrySrcType:
313 *indexOffsetInBytes = 0;
314 ibuf = (GrGLIndexBuffer*)this->getGeomSrc().fIndexBuffer;
316 case kReserved_GeometrySrcType:
317 this->finalizeReservedIndices();
318 *indexOffsetInBytes = geoPoolState.fPoolStartIndex * sizeof(GrGLushort);
319 ibuf = (GrGLIndexBuffer*) geoPoolState.fPoolIndexBuffer;
322 ibuf = NULL; // suppress warning
323 SkFAIL("Unknown geometry src type!");
327 SkASSERT(!ibuf->isMapped());
328 *indexOffsetInBytes += ibuf->baseOffset();
330 GrGLAttribArrayState* attribState =
331 fHWGeometryState.bindArrayAndBuffersToDraw(this, vbuf, ibuf);
333 if (fCurrentProgram->hasVertexShader()) {
334 int vertexAttribCount = optState->getVertexAttribCount();
335 uint32_t usedAttribArraysMask = 0;
336 const GrVertexAttrib* vertexAttrib = optState->getVertexAttribs();
338 for (int vertexAttribIndex = 0; vertexAttribIndex < vertexAttribCount;
339 ++vertexAttribIndex, ++vertexAttrib) {
340 usedAttribArraysMask |= (1 << vertexAttribIndex);
341 GrVertexAttribType attribType = vertexAttrib->fType;
342 attribState->set(this,
345 GrGLAttribTypeToLayout(attribType).fCount,
346 GrGLAttribTypeToLayout(attribType).fType,
347 GrGLAttribTypeToLayout(attribType).fNormalized,
349 reinterpret_cast<GrGLvoid*>(
350 vertexOffsetInBytes + vertexAttrib->fOffset));
352 attribState->disableUnusedArrays(this, usedAttribArraysMask);
356 void GrGpuGL::buildProgramDesc(const GrOptDrawState& optState,
357 const GrProgramDesc::DescInfo& descInfo,
358 GrGpu::DrawType drawType,
359 const GrDeviceCoordTexture* dstCopy,
360 GrProgramDesc* desc) {
361 if (!GrGLProgramDescBuilder::Build(optState, descInfo, drawType, this, dstCopy, desc)) {
362 SkDEBUGFAIL("Failed to generate GL program descriptor");