1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-07-04 10:03:36 +00:00

Refactor: Make DepthBufferList singleton.

This commit is contained in:
Sergey Lipskiy 2014-09-08 14:46:42 +07:00
parent 4f8c3f691d
commit fbf660d4ec
3 changed files with 107 additions and 84 deletions

View File

@ -8,61 +8,63 @@
#include "Config.h" #include "Config.h"
#include "Debug.h" #include "Debug.h"
DepthBufferInfo depthBuffer;
const GLuint ZlutImageUnit = 0; const GLuint ZlutImageUnit = 0;
const GLuint TlutImageUnit = 1; const GLuint TlutImageUnit = 1;
const GLuint depthImageUnit = 2; const GLuint depthImageUnit = 2;
void DepthBuffer_Init() void DepthBuffer_Init()
{ {
depthBuffer.current = NULL; DepthBufferList & dbList = depthBufferList();
depthBuffer.top = NULL; dbList.current = NULL;
depthBuffer.bottom = NULL; dbList.top = NULL;
depthBuffer.numBuffers = 0; dbList.bottom = NULL;
dbList.numBuffers = 0;
} }
void DepthBuffer_RemoveBottom() void DepthBuffer_RemoveBottom()
{ {
DepthBuffer *newBottom = depthBuffer.bottom->higher; DepthBufferList & dbList = depthBufferList();
DepthBuffer *newBottom = dbList.bottom->higher;
if (depthBuffer.bottom == depthBuffer.top) if (dbList.bottom == dbList.top)
depthBuffer.top = NULL; dbList.top = NULL;
if (depthBuffer.bottom->renderbuf != 0) if (dbList.bottom->renderbuf != 0)
glDeleteRenderbuffers(1, &depthBuffer.bottom->renderbuf); glDeleteRenderbuffers(1, &dbList.bottom->renderbuf);
if (depthBuffer.bottom->depth_texture != NULL) if (dbList.bottom->depth_texture != NULL)
textureCache().removeFrameBufferTexture(depthBuffer.bottom->depth_texture); textureCache().removeFrameBufferTexture(dbList.bottom->depth_texture);
free( depthBuffer.bottom ); free( dbList.bottom );
depthBuffer.bottom = newBottom; dbList.bottom = newBottom;
if (depthBuffer.bottom != NULL) if (dbList.bottom != NULL)
depthBuffer.bottom->lower = NULL; dbList.bottom->lower = NULL;
depthBuffer.numBuffers--; dbList.numBuffers--;
} }
void DepthBuffer_Remove( DepthBuffer *buffer ) void DepthBuffer_Remove( DepthBuffer *buffer )
{ {
if ((buffer == depthBuffer.bottom) && DepthBufferList & dbList = depthBufferList();
(buffer == depthBuffer.top)) if ((buffer == dbList.bottom) &&
(buffer == dbList.top))
{ {
depthBuffer.top = NULL; dbList.top = NULL;
depthBuffer.bottom = 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) if (dbList.bottom)
depthBuffer.bottom->lower = NULL; 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) if (dbList.top)
depthBuffer.top->higher = NULL; dbList.top->higher = NULL;
} }
else else
{ {
@ -78,12 +80,12 @@ void DepthBuffer_Remove( DepthBuffer *buffer )
textureCache().removeFrameBufferTexture(buffer->depth_texture); textureCache().removeFrameBufferTexture(buffer->depth_texture);
free( buffer ); free( buffer );
depthBuffer.numBuffers--; dbList.numBuffers--;
} }
void DepthBuffer_RemoveBuffer( u32 address ) void DepthBuffer_RemoveBuffer( u32 address )
{ {
DepthBuffer *current = depthBuffer.bottom; DepthBuffer *current = depthBufferList().bottom;
while (current != NULL) while (current != NULL)
{ {
@ -98,35 +100,37 @@ void DepthBuffer_RemoveBuffer( u32 address )
DepthBuffer *DepthBuffer_AddTop() DepthBuffer *DepthBuffer_AddTop()
{ {
DepthBufferList & dbList = depthBufferList();
DepthBuffer *newtop = (DepthBuffer*)malloc( sizeof( DepthBuffer ) ); DepthBuffer *newtop = (DepthBuffer*)malloc( sizeof( DepthBuffer ) );
newtop->lower = depthBuffer.top; newtop->lower = dbList.top;
newtop->higher = NULL; newtop->higher = NULL;
newtop->renderbuf = 0; newtop->renderbuf = 0;
newtop->fbo = 0; newtop->fbo = 0;
if (depthBuffer.top) if (dbList.top)
depthBuffer.top->higher = newtop; dbList.top->higher = newtop;
if (!depthBuffer.bottom) if (!dbList.bottom)
depthBuffer.bottom = newtop; dbList.bottom = newtop;
depthBuffer.top = newtop; dbList.top = newtop;
depthBuffer.numBuffers++; dbList.numBuffers++;
return newtop; return newtop;
} }
void DepthBuffer_MoveToTop( DepthBuffer *newtop ) void DepthBuffer_MoveToTop( DepthBuffer *newtop )
{ {
if (newtop == depthBuffer.top) DepthBufferList & dbList = depthBufferList();
if (newtop == dbList.top)
return; return;
if (newtop == depthBuffer.bottom) if (newtop == dbList.bottom)
{ {
depthBuffer.bottom = newtop->higher; dbList.bottom = newtop->higher;
depthBuffer.bottom->lower = NULL; dbList.bottom->lower = NULL;
} }
else else
{ {
@ -135,26 +139,28 @@ void DepthBuffer_MoveToTop( DepthBuffer *newtop )
} }
newtop->higher = NULL; newtop->higher = NULL;
newtop->lower = depthBuffer.top; newtop->lower = dbList.top;
depthBuffer.top->higher = newtop; dbList.top->higher = newtop;
depthBuffer.top = newtop; dbList.top = newtop;
} }
void DepthBuffer_Destroy() void DepthBuffer_Destroy()
{ {
while (depthBuffer.bottom) DepthBufferList & dbList = depthBufferList();
while (dbList.bottom)
DepthBuffer_RemoveBottom(); DepthBuffer_RemoveBottom();
depthBuffer.top = depthBuffer.bottom = depthBuffer.current = NULL; dbList.top = dbList.bottom = dbList.current = NULL;
} }
void DepthBuffer_SetBuffer( u32 address ) void DepthBuffer_SetBuffer( u32 address )
{ {
DepthBufferList & dbList = depthBufferList();
FrameBuffer * pFrameBuffer = frameBufferList().findBuffer(address); FrameBuffer * pFrameBuffer = frameBufferList().findBuffer(address);
if (pFrameBuffer == NULL) if (pFrameBuffer == NULL)
pFrameBuffer = frameBufferList().getCurrent(); pFrameBuffer = frameBufferList().getCurrent();
DepthBuffer *current = depthBuffer.top; DepthBuffer *current = dbList.top;
// Search through saved depth buffers // Search through saved depth buffers
while (current != NULL) while (current != NULL)
@ -203,12 +209,13 @@ void DepthBuffer_SetBuffer( u32 address )
#endif #endif
} }
depthBuffer.current = current; dbList.current = current;
} }
DepthBuffer *DepthBuffer_FindBuffer( u32 address ) DepthBuffer *DepthBuffer_FindBuffer( u32 address )
{ {
DepthBuffer *current = depthBuffer.top; DepthBufferList & dbList = depthBufferList();
DepthBuffer *current = dbList.top;
while (current) while (current)
{ {
@ -224,7 +231,7 @@ void DepthBuffer_ClearBuffer() {
#ifndef GLES2 #ifndef GLES2
if (!OGL.bImageTexture) if (!OGL.bImageTexture)
return; return;
DepthBuffer *current = depthBuffer.top; DepthBuffer *current = depthBufferList().top;
if (current == NULL || current->fbo == 0) if (current == NULL || current->fbo == 0)
return; return;
float color[4] = {1.0f, 1.0f, 0.0f, 1.0f}; float color[4] = {1.0f, 1.0f, 0.0f, 1.0f};

View File

@ -14,13 +14,27 @@ struct DepthBuffer
GLuint fbo; GLuint fbo;
}; };
struct DepthBufferInfo struct DepthBufferList
{ {
DepthBuffer *top, *bottom, *current; DepthBuffer *top, *bottom, *current;
int numBuffers; 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 ZlutImageUnit;
extern const GLuint TlutImageUnit; extern const GLuint TlutImageUnit;

View File

@ -276,56 +276,58 @@ void FrameBufferList::removeBuffer(u32 _address )
void FrameBufferList::_initDepthTexture() void FrameBufferList::_initDepthTexture()
{ {
#ifndef GLES2 #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); dbList.top->depth_texture->width = (u32)(m_pCurrent->m_width * OGL.scaleX);
depthBuffer.top->depth_texture->height = (u32)(m_pCurrent->m_height * OGL.scaleY); dbList.top->depth_texture->height = (u32)(m_pCurrent->m_height * OGL.scaleY);
depthBuffer.top->depth_texture->format = 0; dbList.top->depth_texture->format = 0;
depthBuffer.top->depth_texture->size = 2; dbList.top->depth_texture->size = 2;
depthBuffer.top->depth_texture->clampS = 1; dbList.top->depth_texture->clampS = 1;
depthBuffer.top->depth_texture->clampT = 1; dbList.top->depth_texture->clampT = 1;
depthBuffer.top->depth_texture->address = m_pCurrent->m_startAddress; dbList.top->depth_texture->address = m_pCurrent->m_startAddress;
depthBuffer.top->depth_texture->clampWidth = m_pCurrent->m_width; dbList.top->depth_texture->clampWidth = m_pCurrent->m_width;
depthBuffer.top->depth_texture->clampHeight = m_pCurrent->m_height; dbList.top->depth_texture->clampHeight = m_pCurrent->m_height;
depthBuffer.top->depth_texture->frameBufferTexture = TRUE; dbList.top->depth_texture->frameBufferTexture = TRUE;
depthBuffer.top->depth_texture->maskS = 0; dbList.top->depth_texture->maskS = 0;
depthBuffer.top->depth_texture->maskT = 0; dbList.top->depth_texture->maskT = 0;
depthBuffer.top->depth_texture->mirrorS = 0; dbList.top->depth_texture->mirrorS = 0;
depthBuffer.top->depth_texture->mirrorT = 0; dbList.top->depth_texture->mirrorT = 0;
depthBuffer.top->depth_texture->realWidth = (u32)pow2( depthBuffer.top->depth_texture->width ); dbList.top->depth_texture->realWidth = (u32)pow2( dbList.top->depth_texture->width );
depthBuffer.top->depth_texture->realHeight = (u32)pow2( depthBuffer.top->depth_texture->height ); dbList.top->depth_texture->realHeight = (u32)pow2( dbList.top->depth_texture->height );
depthBuffer.top->depth_texture->textureBytes = depthBuffer.top->depth_texture->realWidth * depthBuffer.top->depth_texture->realHeight * 2; dbList.top->depth_texture->textureBytes = dbList.top->depth_texture->realWidth * dbList.top->depth_texture->realHeight * 2;
textureCache().addFrameBufferTextureSize(depthBuffer.top->depth_texture->textureBytes); textureCache().addFrameBufferTextureSize(dbList.top->depth_texture->textureBytes);
glBindTexture( GL_TEXTURE_2D, depthBuffer.top->depth_texture->glName ); glBindTexture( GL_TEXTURE_2D, dbList.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); 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_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glBindTexture( GL_TEXTURE_2D, 0); glBindTexture( GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glGenFramebuffers(1, &depthBuffer.top->fbo); glGenFramebuffers(1, &dbList.top->fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, depthBuffer.top->fbo); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dbList.top->fbo);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, depthBuffer.top->depth_texture->glName, 0); 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, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_pCurrent->m_FBO); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_pCurrent->m_FBO);
m_pCurrent->m_pDepthBuffer = depthBuffer.top; m_pCurrent->m_pDepthBuffer = dbList.top;
DepthBuffer_ClearBuffer(); DepthBuffer_ClearBuffer();
#else #else
depthBuffer.top->depth_texture = NULL; dbList.top->depth_texture = NULL;
#endif // GLES2 #endif // GLES2
} }
void FrameBufferList::attachDepthBuffer() void FrameBufferList::attachDepthBuffer()
{ {
if (m_pCurrent != NULL && m_pCurrent->m_FBO > 0 && depthBuffer.top != NULL && depthBuffer.top->renderbuf > 0) { DepthBufferList & dbList = depthBufferList();
if (depthBuffer.top->depth_texture == NULL) if (m_pCurrent != NULL && m_pCurrent->m_FBO > 0 && dbList.top != NULL && dbList.top->renderbuf > 0) {
if (dbList.top->depth_texture == NULL)
_initDepthTexture(); _initDepthTexture();
m_pCurrent->m_pDepthBuffer = depthBuffer.top; m_pCurrent->m_pDepthBuffer = dbList.top;
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer.top->renderbuf); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dbList.top->renderbuf);
#ifndef GLES2 #ifndef GLES2
GLuint attachments[2] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT }; GLuint attachments[2] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT };
glDrawBuffers(2, attachments); 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 #endif
assert(checkFBO()); assert(checkFBO());
} else if (m_pCurrent != NULL) { } else if (m_pCurrent != NULL) {