mirror of
https://github.com/AquariaOSE/Aquaria.git
synced 2025-08-30 18:25:36 +00:00
Changes to rendering logic, incl water surface flicker fix, plus other fixes.
This commit changes a bunch of internal rendering logic to use FBOs in a way that doesn't violate the GL spec. The water surface FBO's output texture was bound for reading while at the same time rendering the water surface back into the same FBO! Depending on the card/driver/load/zoom factor/moon phase, this could lead to water surface flickering, chessboard effects, and other visual glitches. In order to fix this an extra FBO is needed. In theory this is a simple fix but in practice this is the Aquaria codebase and everything is more complicated than it has any right to be. Couple other things: - FBOs no longer have a depth renderbuffer. Aquaria never uses the depth buffer for anything, so this can go to save some memory. Also remove renderbuffer GL function pointers. - Make FBOs multi-"paged". This is supposedly more efficient on desktop GL, if glDrawBuffer() supports GL_COLOR_ATTACHMENTn. This is currently checked via the presence of glDrawBuffersARB(). - Main core FBO now has 2 pages becaus it's needed for the water surface. The same 2 pages are later used by the after effect manager to ping-pong postprocessing shaders. Remove private after effect FBO. TODO: - There's still a bug in the one-fbo-multiple-binding-points code path. -> for now glDrawBuffersARB must be NULL to work properly.
This commit is contained in:
parent
dd7ab0448f
commit
09edbf49fd
19 changed files with 608 additions and 390 deletions
|
@ -24,15 +24,47 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|||
|
||||
//WARNING: FrameBuffer objects have to have reloadDevice/unloadDevice called manually!
|
||||
|
||||
|
||||
struct FBOStack
|
||||
{
|
||||
const FrameBuffer *fbo;
|
||||
unsigned page;
|
||||
};
|
||||
|
||||
static FBOStack s_fbostack[4]; // first entry is always unused
|
||||
static size_t s_stackpos = 0;
|
||||
|
||||
|
||||
static bool isOnTop(const FrameBuffer *fbo)
|
||||
{
|
||||
return s_fbostack[s_stackpos].fbo == fbo;
|
||||
}
|
||||
|
||||
static bool isInStack(const FrameBuffer *fbo, unsigned page)
|
||||
{
|
||||
for(size_t i = 1; i <= s_stackpos; ++i) // first entry is always NULL
|
||||
if(s_fbostack[i].fbo == fbo && s_fbostack[i].page == page)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
FrameBuffer::FrameBuffer()
|
||||
{
|
||||
inited = false;
|
||||
texw = 0;
|
||||
texh = 0;
|
||||
g_frameBuffer = 0;
|
||||
g_depthRenderBuffer = 0;
|
||||
g_dynamicTextureID = 0;
|
||||
viewportW = 0;
|
||||
viewportH = 0;
|
||||
_curpage = 0;
|
||||
_w = _h = 0;
|
||||
_numpages = 0;
|
||||
_numfbos = 0;
|
||||
for(size_t i = 0; i < Countof(_texs); ++i)
|
||||
{
|
||||
_fbos[i] = 0;
|
||||
_texs[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
FrameBuffer::~FrameBuffer()
|
||||
|
@ -50,10 +82,18 @@ float FrameBuffer::getHeightP() const
|
|||
return (float)core->getWindowHeight()/(float)texh;
|
||||
}
|
||||
|
||||
bool FrameBuffer::init(int width, int height, bool fitToScreen)
|
||||
bool FrameBuffer::getCurrentPage() const
|
||||
{
|
||||
assert(_curpage);
|
||||
return _curpage - 1;
|
||||
}
|
||||
|
||||
bool FrameBuffer::init(int width, int height, unsigned pages)
|
||||
{
|
||||
assert(pages && pages < Countof(_texs));
|
||||
_w = width;
|
||||
_h = height;
|
||||
_numpages = pages;
|
||||
|
||||
if (width == -1)
|
||||
width = core->width;
|
||||
|
@ -61,13 +101,12 @@ bool FrameBuffer::init(int width, int height, bool fitToScreen)
|
|||
if (height == -1)
|
||||
height = core->height;
|
||||
|
||||
if (fitToScreen)
|
||||
{
|
||||
sizePowerOf2Texture(width);
|
||||
sizePowerOf2Texture(height);
|
||||
}
|
||||
viewportW = width;
|
||||
viewportH = height;
|
||||
|
||||
sizePowerOf2Texture(width);
|
||||
sizePowerOf2Texture(height);
|
||||
|
||||
_fitToScreen = fitToScreen;
|
||||
if (width == 0 || height == 0)
|
||||
return false;
|
||||
|
||||
|
@ -78,12 +117,8 @@ bool FrameBuffer::init(int width, int height, bool fitToScreen)
|
|||
os << "Loading EXT_framebuffer_object (" << texw << ", " << texh << ")";
|
||||
debugLog(os.str());
|
||||
|
||||
if( !glIsRenderbufferEXT || !glBindRenderbufferEXT || !glDeleteRenderbuffersEXT ||
|
||||
!glGenRenderbuffersEXT || !glRenderbufferStorageEXT || !glGetRenderbufferParameterivEXT ||
|
||||
!glIsFramebufferEXT || !glBindFramebufferEXT || !glDeleteFramebuffersEXT ||
|
||||
!glGenFramebuffersEXT || !glCheckFramebufferStatusEXT || !glFramebufferTexture1DEXT ||
|
||||
!glFramebufferTexture2DEXT || !glFramebufferTexture3DEXT || !glFramebufferRenderbufferEXT||
|
||||
!glGetFramebufferAttachmentParameterivEXT)
|
||||
if( !glIsFramebufferEXT || !glBindFramebufferEXT || !glDeleteFramebuffersEXT ||
|
||||
!glGenFramebuffersEXT || !glCheckFramebufferStatusEXT || !glFramebufferTexture2DEXT)
|
||||
{
|
||||
debugLog("One or more EXT_framebuffer_object functions were not found");
|
||||
return false;
|
||||
|
@ -91,58 +126,58 @@ bool FrameBuffer::init(int width, int height, bool fitToScreen)
|
|||
|
||||
unloadDevice();
|
||||
|
||||
//
|
||||
// Create a frame-buffer object and a render-buffer object...
|
||||
//
|
||||
// If glDrawBuffersARB() is present, we can attach multiple textures per FBO
|
||||
// and switch between them as render targets. More efficient than switching FBOs.
|
||||
_numfbos = glDrawBuffersARB ? 1 : pages;
|
||||
|
||||
glGenFramebuffersEXT( 1, &g_frameBuffer );
|
||||
glGenRenderbuffersEXT( 1, &g_depthRenderBuffer );
|
||||
glGenFramebuffersEXT(_numfbos, &_fbos[0]);
|
||||
for(unsigned i = 0; i < _numfbos; ++i)
|
||||
if(!_fbos[i])
|
||||
return false;
|
||||
|
||||
// Initialize the render-buffer for usage as a depth buffer.
|
||||
// We don't really need this to render things into the frame-buffer object,
|
||||
// but without it the geometry will not be sorted properly.
|
||||
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, g_depthRenderBuffer );
|
||||
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, width, height );
|
||||
if(_numfbos == 1)
|
||||
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, _fbos[0] );
|
||||
|
||||
glGenTextures( pages, &_texs[0] );
|
||||
|
||||
glGenTextures( 1, &g_dynamicTextureID );
|
||||
for(unsigned i = 0; i < pages; ++i)
|
||||
{
|
||||
unsigned attach = GL_COLOR_ATTACHMENT0_EXT;
|
||||
if(_numfbos > 1)
|
||||
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, _fbos[i] );
|
||||
else
|
||||
attach += i;
|
||||
|
||||
glBindTexture( GL_TEXTURE_2D, g_dynamicTextureID );
|
||||
// GL_LINEAR
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
||||
glBindTexture(GL_TEXTURE_2D, _texs[i]);
|
||||
// GL_LINEAR
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
|
||||
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
|
||||
width, height,
|
||||
0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
|
||||
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
|
||||
width, height,
|
||||
0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
||||
|
||||
|
||||
// Put together
|
||||
|
||||
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_frameBuffer );
|
||||
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, g_dynamicTextureID, 0 );
|
||||
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, g_depthRenderBuffer );
|
||||
|
||||
//
|
||||
// Check for errors...
|
||||
//
|
||||
|
||||
GLenum status = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
|
||||
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, attach, GL_TEXTURE_2D, _texs[i], 0);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
||||
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
|
||||
|
||||
switch( status )
|
||||
for(unsigned i = 0; i < _numfbos; ++i)
|
||||
{
|
||||
case GL_FRAMEBUFFER_COMPLETE_EXT:
|
||||
break;
|
||||
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, _fbos[i] );
|
||||
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
||||
|
||||
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
||||
debugLog("GL_FRAMEBUFFER_UNSUPPORTED_EXT!");
|
||||
default:
|
||||
unloadDevice();
|
||||
return false;
|
||||
switch( status )
|
||||
{
|
||||
case GL_FRAMEBUFFER_COMPLETE_EXT:
|
||||
break;
|
||||
|
||||
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
||||
debugLog("GL_FRAMEBUFFER_UNSUPPORTED_EXT!");
|
||||
default:
|
||||
unloadDevice();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
debugLog("Done");
|
||||
|
@ -161,58 +196,107 @@ void FrameBuffer::unloadDevice()
|
|||
return;
|
||||
}
|
||||
|
||||
if (g_frameBuffer)
|
||||
{
|
||||
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, 0 );
|
||||
for(size_t i = 0; i < Countof(_texs); ++i)
|
||||
if (_fbos[i])
|
||||
{
|
||||
debugLog("frameBuffer handle present, deleting");
|
||||
glDeleteFramebuffersEXT(1, &_fbos[i]);
|
||||
_fbos[i] = 0;
|
||||
}
|
||||
|
||||
debugLog("frameBuffer handle present, deleting");
|
||||
glDeleteFramebuffersEXT(1, &g_frameBuffer);
|
||||
g_frameBuffer = 0;
|
||||
}
|
||||
for(size_t i = 0; i < Countof(_texs); ++i)
|
||||
if (_texs[i])
|
||||
{
|
||||
debugLog("delete framebuffer texture");
|
||||
glDeleteTextures(1, &_texs[i]);
|
||||
_texs[i] = 0;
|
||||
}
|
||||
|
||||
if (g_dynamicTextureID)
|
||||
{
|
||||
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, 0 );
|
||||
|
||||
debugLog("dynamic texture ID handle present, deleting");
|
||||
glDeleteTextures(1, &g_dynamicTextureID);
|
||||
g_dynamicTextureID = 0;
|
||||
}
|
||||
|
||||
if (g_depthRenderBuffer)
|
||||
{
|
||||
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, 0 );
|
||||
|
||||
debugLog("depth render buffer handle present, deleting");
|
||||
glDeleteRenderbuffersEXT(1, &g_depthRenderBuffer);
|
||||
g_depthRenderBuffer = 0;
|
||||
}
|
||||
|
||||
debugLog("done");
|
||||
}
|
||||
|
||||
void FrameBuffer::reloadDevice()
|
||||
{
|
||||
if(!_numpages)
|
||||
return;
|
||||
debugLog("frameBuffer::reloadDevice");
|
||||
init(_w, _h, _fitToScreen);
|
||||
init(_w, _h, _numpages);
|
||||
}
|
||||
|
||||
void FrameBuffer::startCapture() const
|
||||
void FrameBuffer::_bind(unsigned page) const
|
||||
{
|
||||
assert(page < _numpages);
|
||||
_curpage = page + 1;
|
||||
if(glDrawBuffersARB)
|
||||
{
|
||||
assert(_numfbos == 1);
|
||||
if(!isOnTop(this))
|
||||
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[0]);
|
||||
GLenum buf = GL_COLOR_ATTACHMENT0_EXT + page;
|
||||
glDrawBuffersARB(1, &buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(_fbos[page]);
|
||||
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, _fbos[page]);
|
||||
}
|
||||
|
||||
glViewport(0,0,viewportW,viewportH);
|
||||
}
|
||||
|
||||
void FrameBuffer::pushCapture(unsigned page) const
|
||||
{
|
||||
assert(inited);
|
||||
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, g_frameBuffer );
|
||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||
|
||||
_bind(page);
|
||||
|
||||
size_t idx = ++s_stackpos;
|
||||
assert(idx < Countof(s_fbostack));
|
||||
s_fbostack[idx].fbo = this;
|
||||
s_fbostack[idx].page = page;
|
||||
}
|
||||
|
||||
void FrameBuffer::endCapture() const
|
||||
unsigned FrameBuffer::popCapture() const
|
||||
{
|
||||
assert(inited);
|
||||
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
|
||||
assert(inited && s_stackpos && isOnTop(this));
|
||||
const unsigned page = s_fbostack[s_stackpos].page;
|
||||
|
||||
FBOStack prev = s_fbostack[--s_stackpos];
|
||||
if(prev.fbo)
|
||||
prev.fbo->_bind(prev.page);
|
||||
else
|
||||
{
|
||||
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
|
||||
if(glDrawBuffersARB)
|
||||
glDrawBuffer(GL_BACK);
|
||||
glViewport(0, 0, core->width, core->height);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
void FrameBuffer::bindTexture() const
|
||||
void FrameBuffer::replaceCapture(unsigned page) const
|
||||
{
|
||||
assert(inited);
|
||||
glBindTexture( GL_TEXTURE_2D, g_dynamicTextureID );
|
||||
assert(inited && s_stackpos);
|
||||
|
||||
_bind(page);
|
||||
s_fbostack[s_stackpos].fbo = this;
|
||||
s_fbostack[s_stackpos].page = page;
|
||||
}
|
||||
|
||||
unsigned FrameBuffer::getTextureID(unsigned page) const
|
||||
{
|
||||
assert(inited && page < _numpages);
|
||||
return _texs[page];
|
||||
}
|
||||
|
||||
void FrameBuffer::bindTexture(unsigned page) const
|
||||
{
|
||||
// Technically it's enough that this texture isn't the one currently rendered to,
|
||||
// but because we don't know when the topmost FBO is popped (and subsequently
|
||||
// writing to the texture we're now about to read from) let's be extra safe
|
||||
// and generalize this to: Textures part of the FBO stack are forbidden to bind.
|
||||
assert(!isInStack(this, page));
|
||||
unsigned tex = getTextureID(page);
|
||||
glBindTexture(GL_TEXTURE_2D, tex);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue