From fbf660d4eca3510cd19ce3d4b1a6e3213e373372 Mon Sep 17 00:00:00 2001 From: Sergey Lipskiy Date: Mon, 8 Sep 2014 14:46:42 +0700 Subject: [PATCH] Refactor: Make DepthBufferList singleton. --- DepthBuffer.cpp | 109 ++++++++++++++++++++++++++---------------------- DepthBuffer.h | 18 +++++++- FrameBuffer.cpp | 64 ++++++++++++++-------------- 3 files changed, 107 insertions(+), 84 deletions(-) diff --git a/DepthBuffer.cpp b/DepthBuffer.cpp index 0575fa87..ed7982a7 100644 --- a/DepthBuffer.cpp +++ b/DepthBuffer.cpp @@ -8,61 +8,63 @@ #include "Config.h" #include "Debug.h" -DepthBufferInfo depthBuffer; const GLuint ZlutImageUnit = 0; const GLuint TlutImageUnit = 1; const GLuint depthImageUnit = 2; void DepthBuffer_Init() { - depthBuffer.current = NULL; - depthBuffer.top = NULL; - depthBuffer.bottom = NULL; - depthBuffer.numBuffers = 0; + DepthBufferList & dbList = depthBufferList(); + dbList.current = NULL; + dbList.top = NULL; + dbList.bottom = NULL; + dbList.numBuffers = 0; } void DepthBuffer_RemoveBottom() { - DepthBuffer *newBottom = depthBuffer.bottom->higher; + DepthBufferList & dbList = depthBufferList(); + DepthBuffer *newBottom = dbList.bottom->higher; - if (depthBuffer.bottom == depthBuffer.top) - depthBuffer.top = NULL; + if (dbList.bottom == dbList.top) + dbList.top = NULL; - if (depthBuffer.bottom->renderbuf != 0) - glDeleteRenderbuffers(1, &depthBuffer.bottom->renderbuf); - if (depthBuffer.bottom->depth_texture != NULL) - textureCache().removeFrameBufferTexture(depthBuffer.bottom->depth_texture); - free( depthBuffer.bottom ); + if (dbList.bottom->renderbuf != 0) + glDeleteRenderbuffers(1, &dbList.bottom->renderbuf); + if (dbList.bottom->depth_texture != NULL) + textureCache().removeFrameBufferTexture(dbList.bottom->depth_texture); + free( dbList.bottom ); - depthBuffer.bottom = newBottom; + dbList.bottom = newBottom; - if (depthBuffer.bottom != NULL) - depthBuffer.bottom->lower = NULL; + if (dbList.bottom != NULL) + dbList.bottom->lower = NULL; - depthBuffer.numBuffers--; + dbList.numBuffers--; } void DepthBuffer_Remove( DepthBuffer *buffer ) { - if ((buffer == depthBuffer.bottom) && - (buffer == depthBuffer.top)) + DepthBufferList & dbList = depthBufferList(); + if ((buffer == dbList.bottom) && + (buffer == dbList.top)) { - depthBuffer.top = NULL; - depthBuffer.bottom = NULL; + dbList.top = NULL; + dbList.bottom = NULL; } - else if (buffer == depthBuffer.bottom) + else if (buffer == dbList.bottom) { - depthBuffer.bottom = buffer->higher; + dbList.bottom = buffer->higher; - if (depthBuffer.bottom) - depthBuffer.bottom->lower = NULL; + if (dbList.bottom) + dbList.bottom->lower = NULL; } - else if (buffer == depthBuffer.top) + else if (buffer == dbList.top) { - depthBuffer.top = buffer->lower; + dbList.top = buffer->lower; - if (depthBuffer.top) - depthBuffer.top->higher = NULL; + if (dbList.top) + dbList.top->higher = NULL; } else { @@ -78,12 +80,12 @@ void DepthBuffer_Remove( DepthBuffer *buffer ) textureCache().removeFrameBufferTexture(buffer->depth_texture); free( buffer ); - depthBuffer.numBuffers--; + dbList.numBuffers--; } void DepthBuffer_RemoveBuffer( u32 address ) { - DepthBuffer *current = depthBuffer.bottom; + DepthBuffer *current = depthBufferList().bottom; while (current != NULL) { @@ -98,35 +100,37 @@ void DepthBuffer_RemoveBuffer( u32 address ) DepthBuffer *DepthBuffer_AddTop() { + DepthBufferList & dbList = depthBufferList(); DepthBuffer *newtop = (DepthBuffer*)malloc( sizeof( DepthBuffer ) ); - newtop->lower = depthBuffer.top; + newtop->lower = dbList.top; newtop->higher = NULL; newtop->renderbuf = 0; newtop->fbo = 0; - if (depthBuffer.top) - depthBuffer.top->higher = newtop; + if (dbList.top) + dbList.top->higher = newtop; - if (!depthBuffer.bottom) - depthBuffer.bottom = newtop; + if (!dbList.bottom) + dbList.bottom = newtop; - depthBuffer.top = newtop; + dbList.top = newtop; - depthBuffer.numBuffers++; + dbList.numBuffers++; return newtop; } void DepthBuffer_MoveToTop( DepthBuffer *newtop ) { - if (newtop == depthBuffer.top) + DepthBufferList & dbList = depthBufferList(); + if (newtop == dbList.top) return; - if (newtop == depthBuffer.bottom) + if (newtop == dbList.bottom) { - depthBuffer.bottom = newtop->higher; - depthBuffer.bottom->lower = NULL; + dbList.bottom = newtop->higher; + dbList.bottom->lower = NULL; } else { @@ -135,26 +139,28 @@ void DepthBuffer_MoveToTop( DepthBuffer *newtop ) } newtop->higher = NULL; - newtop->lower = depthBuffer.top; - depthBuffer.top->higher = newtop; - depthBuffer.top = newtop; + newtop->lower = dbList.top; + dbList.top->higher = newtop; + dbList.top = newtop; } void DepthBuffer_Destroy() { - while (depthBuffer.bottom) + DepthBufferList & dbList = depthBufferList(); + while (dbList.bottom) DepthBuffer_RemoveBottom(); - depthBuffer.top = depthBuffer.bottom = depthBuffer.current = NULL; + dbList.top = dbList.bottom = dbList.current = NULL; } void DepthBuffer_SetBuffer( u32 address ) { + DepthBufferList & dbList = depthBufferList(); FrameBuffer * pFrameBuffer = frameBufferList().findBuffer(address); if (pFrameBuffer == NULL) pFrameBuffer = frameBufferList().getCurrent(); - DepthBuffer *current = depthBuffer.top; + DepthBuffer *current = dbList.top; // Search through saved depth buffers while (current != NULL) @@ -203,12 +209,13 @@ void DepthBuffer_SetBuffer( u32 address ) #endif } - depthBuffer.current = current; + dbList.current = current; } DepthBuffer *DepthBuffer_FindBuffer( u32 address ) { - DepthBuffer *current = depthBuffer.top; + DepthBufferList & dbList = depthBufferList(); + DepthBuffer *current = dbList.top; while (current) { @@ -224,7 +231,7 @@ void DepthBuffer_ClearBuffer() { #ifndef GLES2 if (!OGL.bImageTexture) return; - DepthBuffer *current = depthBuffer.top; + DepthBuffer *current = depthBufferList().top; if (current == NULL || current->fbo == 0) return; float color[4] = {1.0f, 1.0f, 0.0f, 1.0f}; diff --git a/DepthBuffer.h b/DepthBuffer.h index 0de74eb7..a11113d6 100644 --- a/DepthBuffer.h +++ b/DepthBuffer.h @@ -14,13 +14,27 @@ struct DepthBuffer GLuint fbo; }; -struct DepthBufferInfo +struct DepthBufferList { DepthBuffer *top, *bottom, *current; int numBuffers; + + static DepthBufferList & get() + { + static DepthBufferList depthBufferList; + return depthBufferList; + } + +private: + DepthBufferList() : current(NULL) {} + DepthBufferList(const FrameBufferList &); }; -extern DepthBufferInfo depthBuffer; +inline +DepthBufferList & depthBufferList() +{ + return DepthBufferList::get(); +} extern const GLuint ZlutImageUnit; extern const GLuint TlutImageUnit; diff --git a/FrameBuffer.cpp b/FrameBuffer.cpp index d3cbdebb..2332876f 100644 --- a/FrameBuffer.cpp +++ b/FrameBuffer.cpp @@ -276,56 +276,58 @@ void FrameBufferList::removeBuffer(u32 _address ) void FrameBufferList::_initDepthTexture() { #ifndef GLES2 - depthBuffer.top->depth_texture = textureCache().addFrameBufferTexture(); + DepthBufferList & dbList = depthBufferList(); + dbList.top->depth_texture = textureCache().addFrameBufferTexture(); - depthBuffer.top->depth_texture->width = (u32)(m_pCurrent->m_width * OGL.scaleX); - depthBuffer.top->depth_texture->height = (u32)(m_pCurrent->m_height * OGL.scaleY); - depthBuffer.top->depth_texture->format = 0; - depthBuffer.top->depth_texture->size = 2; - depthBuffer.top->depth_texture->clampS = 1; - depthBuffer.top->depth_texture->clampT = 1; - depthBuffer.top->depth_texture->address = m_pCurrent->m_startAddress; - depthBuffer.top->depth_texture->clampWidth = m_pCurrent->m_width; - depthBuffer.top->depth_texture->clampHeight = m_pCurrent->m_height; - depthBuffer.top->depth_texture->frameBufferTexture = TRUE; - depthBuffer.top->depth_texture->maskS = 0; - depthBuffer.top->depth_texture->maskT = 0; - depthBuffer.top->depth_texture->mirrorS = 0; - depthBuffer.top->depth_texture->mirrorT = 0; - depthBuffer.top->depth_texture->realWidth = (u32)pow2( depthBuffer.top->depth_texture->width ); - depthBuffer.top->depth_texture->realHeight = (u32)pow2( depthBuffer.top->depth_texture->height ); - depthBuffer.top->depth_texture->textureBytes = depthBuffer.top->depth_texture->realWidth * depthBuffer.top->depth_texture->realHeight * 2; - textureCache().addFrameBufferTextureSize(depthBuffer.top->depth_texture->textureBytes); + dbList.top->depth_texture->width = (u32)(m_pCurrent->m_width * OGL.scaleX); + dbList.top->depth_texture->height = (u32)(m_pCurrent->m_height * OGL.scaleY); + dbList.top->depth_texture->format = 0; + dbList.top->depth_texture->size = 2; + dbList.top->depth_texture->clampS = 1; + dbList.top->depth_texture->clampT = 1; + dbList.top->depth_texture->address = m_pCurrent->m_startAddress; + dbList.top->depth_texture->clampWidth = m_pCurrent->m_width; + dbList.top->depth_texture->clampHeight = m_pCurrent->m_height; + dbList.top->depth_texture->frameBufferTexture = TRUE; + dbList.top->depth_texture->maskS = 0; + dbList.top->depth_texture->maskT = 0; + dbList.top->depth_texture->mirrorS = 0; + dbList.top->depth_texture->mirrorT = 0; + dbList.top->depth_texture->realWidth = (u32)pow2( dbList.top->depth_texture->width ); + dbList.top->depth_texture->realHeight = (u32)pow2( dbList.top->depth_texture->height ); + dbList.top->depth_texture->textureBytes = dbList.top->depth_texture->realWidth * dbList.top->depth_texture->realHeight * 2; + textureCache().addFrameBufferTextureSize(dbList.top->depth_texture->textureBytes); - glBindTexture( GL_TEXTURE_2D, depthBuffer.top->depth_texture->glName ); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, depthBuffer.top->depth_texture->realWidth, depthBuffer.top->depth_texture->realHeight, 0, GL_RGBA, GL_FLOAT, NULL); + glBindTexture( GL_TEXTURE_2D, dbList.top->depth_texture->glName ); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, dbList.top->depth_texture->realWidth, dbList.top->depth_texture->realHeight, 0, GL_RGBA, GL_FLOAT, NULL); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glBindTexture( GL_TEXTURE_2D, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); - glGenFramebuffers(1, &depthBuffer.top->fbo); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, depthBuffer.top->fbo); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, depthBuffer.top->depth_texture->glName, 0); + glGenFramebuffers(1, &dbList.top->fbo); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dbList.top->fbo); + glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, dbList.top->depth_texture->glName, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_pCurrent->m_FBO); - m_pCurrent->m_pDepthBuffer = depthBuffer.top; + m_pCurrent->m_pDepthBuffer = dbList.top; DepthBuffer_ClearBuffer(); #else - depthBuffer.top->depth_texture = NULL; + dbList.top->depth_texture = NULL; #endif // GLES2 } void FrameBufferList::attachDepthBuffer() { - if (m_pCurrent != NULL && m_pCurrent->m_FBO > 0 && depthBuffer.top != NULL && depthBuffer.top->renderbuf > 0) { - if (depthBuffer.top->depth_texture == NULL) + DepthBufferList & dbList = depthBufferList(); + if (m_pCurrent != NULL && m_pCurrent->m_FBO > 0 && dbList.top != NULL && dbList.top->renderbuf > 0) { + if (dbList.top->depth_texture == NULL) _initDepthTexture(); - m_pCurrent->m_pDepthBuffer = depthBuffer.top; - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer.top->renderbuf); + m_pCurrent->m_pDepthBuffer = dbList.top; + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dbList.top->renderbuf); #ifndef GLES2 GLuint attachments[2] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT }; glDrawBuffers(2, attachments); - glBindImageTexture(depthImageUnit, depthBuffer.top->depth_texture->glName, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); + glBindImageTexture(depthImageUnit, dbList.top->depth_texture->glName, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F); #endif assert(checkFBO()); } else if (m_pCurrent != NULL) {