Jump to content
The Dark Mod Forums

Recommended Posts

Posted

Thanks m8 :) feeling better though i might not be as fast as i once was ;) but that was to be expected.

Getting to old for burning the midnight oil, but then again im one of the oldest (active) original members from the quake community hehe.

  • Like 1
  • 2 weeks later...
Posted

work in progress.

 

 

 

/*
===========================================================================
Copyright © 2006 Kirk Barnes
Copyright © 2006-2008 Robert Beckebans <trebor_7@users.sourceforge.net>

This file is part of XreaL source code.

XreaL source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

XreaL source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XreaL source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_fbo.c
#include "tr_local.h"

idFrameBuffer FrameBufferCache;

/*
==============
R_ListFBOMem_f
==============
*/
static void R_ListFBOMem_f( const idCmdArgs &args ) {
FrameBufferCache.FBO_List();
}

/*
============
FBO_Create
============
*/
FBO_t *idFrameBuffer::FBO_Create( const char *name, int width, int height ) {
FBO_t *fbo;
if( strlen( name ) >= MAX_PATH ) {
common->Error( "FBO_Create: \"%s\" is too long", name );
}
if( width <= 0 || width > glConfig.maxRenderbufferSize ) {
common->Error( "FBO_Create: bad uploadWidth %i", width );
}
if( height <= 0 || height > glConfig.maxRenderbufferSize ) {
common->Error( "FBO_Create: bad uploadHeight %i", height );
}
if( globalImages->numActiveFrameBufferImages == MAX_FRAMEBUFFERS ) {
common->Error( "FBO_Create: MAX_FBAMEBUFFERS hit" );
}
for( int index = 0; index < globalImages->numActiveFrameBufferImages; index++ ) {
fbo = globalImages->frameBufferObjects[index] = Mem_Alloc( sizeof( *fbo ) );
strncpy( fbo->name, name, sizeof( fbo->name ) );
fbo->index = index; // should this be incremented in a for loop ?
fbo->width = width;
fbo->height = height;
glGenFramebuffersEXT( 1, &fbo->frameBuffer );
}
return fbo;
}

/*
============
FBO_CreateBuffer
============
*/
void idFrameBuffer::FBO_CreateBuffer( FBO_t *fbo, int format, int index, int multisample ) {
uint32_t *pRenderBuffer;
GLenum attachment;
bool absent;
switch( format ) {
case GL_RGB:
case GL_RGBA:
case GL_RGB8:
case GL_RGBA8:
case GL_RGB16F_ARB:
case GL_RGBA16F_ARB:
case GL_RGB32F_ARB:
case GL_RGBA32F_ARB:
fbo->colorFormat = format;
pRenderBuffer = &fbo->colorBuffers[index];
attachment = GL_COLOR_ATTACHMENT0_EXT + index;
break;
case GL_DEPTH_COMPONENT:
case GL_DEPTH_COMPONENT16_ARB:
case GL_DEPTH_COMPONENT24_ARB:
case GL_DEPTH_COMPONENT32_ARB:
fbo->depthFormat = format;
pRenderBuffer = &fbo->depthBuffer;
attachment = GL_DEPTH_ATTACHMENT_EXT;
break;
case GL_STENCIL_INDEX:
case GL_STENCIL_INDEX1_EXT:
case GL_STENCIL_INDEX4_EXT:
case GL_STENCIL_INDEX8_EXT:
case GL_STENCIL_INDEX16_EXT:
fbo->stencilFormat = format;
pRenderBuffer = &fbo->stencilBuffer;
attachment = GL_STENCIL_ATTACHMENT_EXT;
break;
case GL_DEPTH_STENCIL_EXT:
case GL_DEPTH24_STENCIL8_EXT:
fbo->packedDepthStencilFormat = format;
pRenderBuffer = &fbo->packedDepthStencilBuffer;
attachment = 0; // special for stencil and depth
break;
default:
common->Printf( "FBO_CreateBuffer: invalid format %d\n", format );
return;
}
absent = *pRenderBuffer == 0;
if( absent ) {
glGenRenderbuffersEXT( 1, pRenderBuffer );
}
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, *pRenderBuffer );
if( multisample && glConfig.framebufferMultisample ) {
glRenderbufferStorageMultisampleEXT( GL_RENDERBUFFER_EXT, multisample, format, fbo->width, fbo->height );
} else {
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, format, fbo->width, fbo->height );
}
if( absent ) {
if( attachment == 0 ) {
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, *pRenderBuffer );
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, *pRenderBuffer );
} else {
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, *pRenderBuffer );
}
}
}

/*
=================
FBO_AttachTexture1D
=================
*/
void idFrameBuffer::FBO_AttachTexture1D( int texId, int index ) {
if( index < 0 || index >= glConfig.maxColorAttachments ) {
common->Printf( "R_AttachFBOTexture1D: invalid attachment index %i\n", index );
return;
}
glFramebufferTexture1DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_1D, texId, 0 );
}

/*
=================
FBO_AttachTexture2D
=================
*/
void idFrameBuffer::FBO_AttachTexture2D( int target, int texId, int index ) {
if( target != GL_TEXTURE_2D && ( target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB || target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB ) ) {
common->Printf( "R_AttachFBOTexture2D: invalid target %i\n", target );
return;
}
if( index < 0 || index >= glConfig.maxColorAttachments ) {
common->Printf( "R_AttachFBOTexture2D: invalid attachment index %i\n", index );
return;
}
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, target, texId, 0 );
}

/*
=================
FBO_AttachTexture3D
=================
*/
void idFrameBuffer::FBO_AttachTexture3D( int texId, int index, int zOffset ) {
if( index < 0 || index >= glConfig.maxColorAttachments ) {
common->Printf( "R_AttachFBOTexture3D: invalid attachment index %i\n", index );
return;
}
glFramebufferTexture3DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_3D_EXT, texId, 0, zOffset );
}

/*
=================
FBO_AttachTextureDepth
=================
*/
void idFrameBuffer::FBO_AttachTextureDepth( int texId ) {
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texId, 0 );
}

/*
=================
FBO_AttachTexturePackedDepthStencil
=================
*/
void idFrameBuffer::FBO_AttachTexturePackedDepthStencil( int texId ) {
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texId, 0 );
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, texId, 0 );
}

/*
============
FBO_AttachTextureImage
============
*/
void idFrameBuffer::FBO_AttachTextureImage( idImage *img, int index ) {
if( !backEnd.glState.currentFBO ) {
common->Printf( "FBO: attempted to attach a texture image with no FBO bound!\n" );
return;
}
FBO_AttachTexture2D( GL_TEXTURE_2D, img->texnum, index );
backEnd.glState.currentFBO->colorImage[index] = img;
}

/*
============
FBO_Bind
============
*/
void idFrameBuffer::FBO_Bind( FBO_t *fbo ) {
if( backEnd.glState.currentFBO == fbo ) {
return;
}
if( !fbo ) {
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
backEnd.glState.currentFBO = NULL;
return;
}
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo->frameBuffer );
backEnd.glState.currentFBO = fbo;
}

/*
=============
FBO_CheckFrameBuffer
=============
*/
bool idFrameBuffer::FBO_CheckFrameBuffer( const FBO_t *fbo ) {
int code;
int id;
glGetIntegerv( GL_FRAMEBUFFER_BINDING_EXT, &id );
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo->frameBuffer );
code = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
if( code == GL_FRAMEBUFFER_COMPLETE_EXT ) {
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, id );
return true;
}
// an error occured
switch( code ) {
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Unsupported framebuffer format\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete attachment\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, missing attachment\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, attached images must have same dimensions\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, attached images must have same format\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, missing draw buffer\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, missing read buffer\n", fbo->name );
break;
default:
common->Warning( "FBO_CheckFrameBuffer: (%s) unknown error 0x%X\n", fbo->name, code );
break;
}
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, id );
return false;
}

/*
============
FBO_Init
============
*/
void idFrameBuffer::FBO_Init( void ) {
int i;
int hdrFormat, multisample;
cmdSystem->AddCommand( "ListFBOMem", R_ListFBOMem_f, CMD_FL_RENDERER, "lists Objects Allocated in FrameBufferCache Cache" );
common->Printf( "------- FBO_Init -------\n" );
if( !glConfig.framebufferObject ) {
return;
}
globalImages->numActiveFrameBufferImages = 0;
GL_CheckErrors();
hdrFormat = GL_RGBA8;
if( r_hdr->integer && glConfig.framebufferObject && glConfig.textureFloat ) {
hdrFormat = GL_RGBA16F_ARB;
}
glGetIntegerv( GL_MAX_SAMPLES_EXT, &multisample );
if( r_ext_framebuffer_multisample->integer < multisample ) {
multisample = r_ext_framebuffer_multisample->integer;
}
if( multisample < 2 || !glConfig.framebufferBlit ) {
multisample = 0;
}
if( multisample != r_ext_framebuffer_multisample->integer ) {
ri.Cvar_SetValue( "r_ext_framebuffer_multisample", ( float )multisample );
}
// only create a render FBO if we need to resolve MSAA or do HDR
// otherwise just render straight to the screen (tr.renderFbo = NULL)
if( multisample && glConfig.framebufferMultisample ) {
globalImages->renderFbo = FBO_Create( "_render", globalImages->renderDepthImage->uploadWidth, globalImages->renderDepthImage->uploadHeight );
FBO_Bind( globalImages->renderFbo );
FBO_CreateBuffer( globalImages->renderFbo, hdrFormat, 0, multisample );
FBO_CreateBuffer( globalImages->renderFbo, GL_DEPTH_COMPONENT24_ARB, 0, multisample );
FBO_CheckFrameBuffer( globalImages->renderFbo );
globalImages->msaaResolveFbo = FBO_Create( "_msaaResolve", globalImages->renderDepthImage->uploadWidth, globalImages->renderDepthImage->uploadHeight );
FBO_Bind( globalImages->msaaResolveFbo );
FBO_AttachTextureImage( globalImages->renderImage, 0 );
FBO_AttachTextureDepth( globalImages->renderDepthImage->texnum );
FBO_CheckFrameBuffer( globalImages->msaaResolveFbo );
} else if( r_hdr->integer ) {
globalImages->renderFbo = FBO_Create( "_render", globalImages->renderDepthImage->uploadWidth, globalImages->renderDepthImage->uploadHeight );
FBO_Bind( globalImages->renderFbo );
FBO_AttachTextureImage( globalImages->renderImage, 0 );
FBO_AttachTextureDepth( globalImages->renderDepthImage->texnum );
FBO_CheckFrameBuffer( globalImages->renderFbo );
}
// clear render buffer
// this fixes the corrupt screen bug with r_hdr 1 on older hardware
if( globalImages->renderFbo ) {
FBO_Bind( globalImages->renderFbo );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
FBO_Bind( NULL );
}
if( r_drawSunRays->integer ) {
globalImages->sunRaysFbo = FBO_Create( "_sunRays", globalImages->renderDepthImage->uploadWidth, globalImages->renderDepthImage->uploadHeight );
FBO_Bind( globalImages->sunRaysFbo );
FBO_AttachTextureImage( globalImages->sunRaysImage, 0 );
FBO_AttachTextureDepth( globalImages->renderDepthImage->texnum );
FBO_CheckFrameBuffer( globalImages->sunRaysFbo );
}
// FIXME: Don't use separate color/depth buffers for a shadow buffer
if( MAX_DRAWN_PSHADOWS && globalImages->pshadowMaps[0] ) {
for( i = 0; i < MAX_DRAWN_PSHADOWS; i++ ) {
globalImages->pshadowFbos = FBO_Create( va( "_shadowmap%d", i ), globalImages->pshadowMaps->uploadWidth, globalImages->pshadowMaps->uploadHeight );
FBO_Bind( globalImages->pshadowFbos );
FBO_AttachTextureImage( globalImages->pshadowMaps, 0 );
FBO_CreateBuffer( globalImages->pshadowFbos, GL_DEPTH_COMPONENT24_ARB, 0, 0 );
FBO_CheckFrameBuffer( globalImages->pshadowFbos );
}
}
if( globalImages->sunShadowDepthImage[0] ) {
for( i = 0; i < 4; i++ ) {
globalImages->sunShadowFbo = FBO_Create( "_sunshadowmap", globalImages->sunShadowDepthImage->uploadWidth, globalImages->sunShadowDepthImage->uploadHeight );
FBO_Bind( globalImages->sunShadowFbo );
glDrawBuffer( GL_NONE );
glReadBuffer( GL_NONE );
FBO_AttachTextureDepth( globalImages->sunShadowDepthImage->texnum );
FBO_CheckFrameBuffer( globalImages->sunShadowFbo );
}
globalImages->screenShadowFbo = FBO_Create( "_screenshadow", globalImages->screenShadowImage->uploadWidth, globalImages->screenShadowImage->uploadHeight );
FBO_Bind( globalImages->screenShadowFbo );
FBO_AttachTextureImage( globalImages->screenShadowImage, 0 );
FBO_CheckFrameBuffer( globalImages->screenShadowFbo );
}
for( i = 0; i < 2; i++ ) {
globalImages->textureScratchFbo = FBO_Create( va( "_texturescratch%d", i ), globalImages->textureScratchImage->uploadWidth, globalImages->textureScratchImage->uploadHeight );
FBO_Bind( globalImages->textureScratchFbo );
FBO_AttachTextureImage( globalImages->textureScratchImage, 0 );
FBO_CheckFrameBuffer( globalImages->textureScratchFbo );
}
{
globalImages->calcLevelsFbo = FBO_Create( "_calclevels", globalImages->calcLevelsImage->uploadWidth, globalImages->calcLevelsImage->uploadHeight );
FBO_Bind( globalImages->calcLevelsFbo );
FBO_AttachTextureImage( globalImages->calcLevelsImage, 0 );
FBO_CheckFrameBuffer( globalImages->calcLevelsFbo );
}
{
globalImages->targetLevelsFbo = FBO_Create( "_targetlevels", globalImages->targetLevelsImage->uploadWidth, globalImages->targetLevelsImage->uploadHeight );
FBO_Bind( globalImages->targetLevelsFbo );
FBO_AttachTextureImage( globalImages->targetLevelsImage, 0 );
FBO_CheckFrameBuffer( globalImages->targetLevelsFbo );
}
for( i = 0; i < 2; i++ ) {
globalImages->quarterFbo = FBO_Create( va( "_quarter%d", i ), globalImages->quarterImage->uploadWidth, globalImages->quarterImage->uploadHeight );
FBO_Bind( globalImages->quarterFbo );
FBO_AttachTextureImage( globalImages->quarterImage, 0 );
FBO_CheckFrameBuffer( globalImages->quarterFbo );
}
if( r_ssao->integer ) {
globalImages->hdrDepthFbo = FBO_Create( "_hdrDepth", globalImages->hdrDepthImage->uploadWidth, globalImages->hdrDepthImage->uploadHeight );
FBO_Bind( globalImages->hdrDepthFbo );
FBO_AttachTextureImage( globalImages->hdrDepthImage, 0 );
FBO_CheckFrameBuffer( globalImages->hdrDepthFbo );
globalImages->screenSsaoFbo = FBO_Create( "_screenssao", globalImages->screenSsaoImage->uploadWidth, globalImages->screenSsaoImage->uploadHeight );
FBO_Bind( globalImages->screenSsaoFbo );
FBO_AttachTextureImage( globalImages->screenSsaoImage, 0 );
FBO_CheckFrameBuffer( globalImages->screenSsaoFbo );
}
if( globalImages->renderCubeImage ) {
globalImages->renderCubeFbo = FBO_Create( "_renderCubeFbo", globalImages->renderCubeImage->uploadWidth, globalImages->renderCubeImage->uploadHeight );
FBO_Bind( globalImages->renderCubeFbo );
FBO_AttachTexture2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, globalImages->renderCubeImage->texnum, 0 );
backEnd.glState.currentFBO->colorImage[0] = globalImages->renderCubeImage;
FBO_CreateBuffer( globalImages->renderCubeFbo, GL_DEPTH_COMPONENT24_ARB, 0, 0 );
FBO_CheckFrameBuffer( globalImages->renderCubeFbo );
}
GL_CheckErrors();
FBO_Bind( NULL );
}

/*
============
FBO_Shutdown
============
*/
void idFrameBuffer::FBO_Shutdown( void ) {
int i, j;
FBO_t *fbo;
common->Printf( "------- FBO_Shutdown -------\n" );
if( !glConfig.framebufferObject ) {
return;
}
FBO_Bind( NULL );
for( i = 0; i < globalImages->numActiveFrameBufferImages; i++ ) {
fbo = globalImages->frameBufferObjects;
for( j = 0; j < glConfig.maxColorAttachments; j++ ) {
if( fbo->colorBuffers[j] ) {
glDeleteRenderbuffersEXT( 1, &fbo->colorBuffers[j] );
}
}
if( fbo->depthBuffer ) {
glDeleteRenderbuffersEXT( 1, &fbo->depthBuffer );
}
if( fbo->stencilBuffer ) {
glDeleteRenderbuffersEXT( 1, &fbo->stencilBuffer );
}
if( fbo->frameBuffer ) {
glDeleteFramebuffersEXT( 1, &fbo->frameBuffer );
}
Mem_Free(fbo);
fbo = NULL;
}
}

/*
============
R_FBOList_f
============
*/
void idFrameBuffer::FBO_List( void ) {
int i;
FBO_t *fbo;
if( !glConfig.framebufferObject ) {
common->Printf( "GL_EXT_framebuffer_object is not available.\n" );
return;
}
common->Printf( " size name\n" );
common->Printf( "----------------------------------------------------------\n" );
for( i = 0; i < globalImages->numActiveFrameBufferImages; i++ ) {
fbo = globalImages->frameBufferObjects;
common->Printf( " %4i: %4i %4i %s\n", i, fbo->width, fbo->height, fbo->name );
}
common->Printf( " %i FBOs\n", globalImages->numActiveFrameBufferImages );
}

 

 

Still needs interfaces for ARB shading since the xreal code was GLSL based it does not quite fit with the above.

  • Like 1
Posted

Its an interface for various effects that use framebuffers to create things like bloom - hdr lighting - ssao - blur - shadowmaps etc.

  • Like 2
  • 2 weeks later...
Posted

Ok it compiles now, so my code cant be all wrong even though im pretty novice when it comes to C++ :)

 

Next step implement some effect that uses the frambuffer code so it can be tested, if anyone want to chime in and help with the effort they are welcome.

 

Funny thing idtech4 has a function with a rather presumptious name implying that it uses framebuffers allready, it does not but it does use texture generating.

Could be fun moving those parts to real framebuffers to see what gains if any it could accomplish.

Posted

Nothing to show yet im afraid :) so far only the basic code for the framework has been done.

 

but below is the code if anyone wants to toy with it.

 

 

 

/*
===========================================================================
Copyright © 2006 Kirk Barnes
Copyright © 2006-2008 Robert Beckebans <trebor_7@users.sourceforge.net>

This file is part of XreaL source code.

XreaL source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

XreaL source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XreaL source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_fbo.c
#include "tr_local.h"

#define MAX_QPATH 256 // max length of a game pathname

idFrameBuffer FrameBufferCache;

/*
==============
R_ListFBOMem_f
==============
*/
static void R_ListFBOMem_f( const idCmdArgs &args ) {
FrameBufferCache.FBO_List();
}

/*
============
FBO_Create
============
*/
FBO_t *idFrameBuffer::FBO_Create( const char *name, int width, int height ) {
FBO_t *fbo;
if( strlen( name ) >= MAX_QPATH ) {
common->Error( "FBO_Create: \"%s\" is too long", name );
}
if( width <= 0 || width > glConfig.maxRenderbufferSize ) {
common->Error( "FBO_Create: bad uploadWidth %i", width );
}
if( height <= 0 || height > glConfig.maxRenderbufferSize ) {
common->Error( "FBO_Create: bad uploadHeight %i", height );
}
if( globalImages->numActiveFrameBufferImages == MAX_FRAMEBUFFERS ) {
common->Error( "FBO_Create: MAX_FBAMEBUFFERS hit" );
}
// Use C++ allocation eg. new / delete;
for( int index = 0; index < globalImages->numActiveFrameBufferImages; index++ ) {
fbo = globalImages->frameBufferObjects[index] = new FBO_t[sizeof( *fbo )];
strncpy( fbo->name, name, sizeof( fbo->name ) );
fbo->index = index; // should this be incremented in a for loop ?
fbo->width = width;
fbo->height = height;
glGenFramebuffersEXT( 1, &fbo->frameBuffer );
}
return fbo;
}

/*
============
FBO_CreateBuffer
============
*/
void idFrameBuffer::FBO_CreateBuffer( FBO_t *fbo, int format, int index, int multisample ) {
uint32_t *pRenderBuffer;
GLenum attachment;
bool absent;
switch( format ) {
case GL_RGB:
case GL_RGBA:
case GL_RGB8:
case GL_RGBA8:
case GL_RGB16F_ARB:
case GL_RGBA16F_ARB:
case GL_RGB32F_ARB:
case GL_RGBA32F_ARB:
fbo->colorFormat = format;
pRenderBuffer = &fbo->colorBuffers[index];
attachment = GL_COLOR_ATTACHMENT0_EXT + index;
break;
case GL_DEPTH_COMPONENT:
case GL_DEPTH_COMPONENT16_ARB:
case GL_DEPTH_COMPONENT24_ARB:
case GL_DEPTH_COMPONENT32_ARB:
fbo->depthFormat = format;
pRenderBuffer = &fbo->depthBuffer;
attachment = GL_DEPTH_ATTACHMENT_EXT;
break;
case GL_STENCIL_INDEX:
case GL_STENCIL_INDEX1_EXT:
case GL_STENCIL_INDEX4_EXT:
case GL_STENCIL_INDEX8_EXT:
case GL_STENCIL_INDEX16_EXT:
fbo->stencilFormat = format;
pRenderBuffer = &fbo->stencilBuffer;
attachment = GL_STENCIL_ATTACHMENT_EXT;
break;
case GL_DEPTH_STENCIL_EXT:
case GL_DEPTH24_STENCIL8_EXT:
fbo->packedDepthStencilFormat = format;
pRenderBuffer = &fbo->packedDepthStencilBuffer;
attachment = 0; // special for stencil and depth
break;
default:
common->Printf( "FBO_CreateBuffer: invalid format %d\n", format );
return;
}
absent = *pRenderBuffer == 0;
if( absent ) {
glGenRenderbuffersEXT( 1, pRenderBuffer );
}
glBindRenderbufferEXT( GL_RENDERBUFFER_EXT, *pRenderBuffer );
if( multisample && glConfig.framebufferMultisample ) {
glRenderbufferStorageMultisampleEXT( GL_RENDERBUFFER_EXT, multisample, format, fbo->width, fbo->height );
} else {
glRenderbufferStorageEXT( GL_RENDERBUFFER_EXT, format, fbo->width, fbo->height );
}
if( absent ) {
if( attachment == 0 ) {
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, *pRenderBuffer );
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, *pRenderBuffer );
} else {
glFramebufferRenderbufferEXT( GL_FRAMEBUFFER_EXT, attachment, GL_RENDERBUFFER_EXT, *pRenderBuffer );
}
}
}

/*
=================
FBO_AttachTexture1D
=================
*/
void idFrameBuffer::FBO_AttachTexture1D( int texId, int index ) {
if( index < 0 || index >= glConfig.maxColorAttachments ) {
common->Printf( "R_AttachFBOTexture1D: invalid attachment index %i\n", index );
return;
}
glFramebufferTexture1DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_1D, texId, 0 );
}

/*
=================
FBO_AttachTexture2D
=================
*/
void idFrameBuffer::FBO_AttachTexture2D( int target, int texId, int index ) {
if( target != GL_TEXTURE_2D && ( target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB || target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB ) ) {
common->Printf( "R_AttachFBOTexture2D: invalid target %i\n", target );
return;
}
if( index < 0 || index >= glConfig.maxColorAttachments ) {
common->Printf( "R_AttachFBOTexture2D: invalid attachment index %i\n", index );
return;
}
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, target, texId, 0 );
}

/*
=================
FBO_AttachTexture3D
=================
*/
void idFrameBuffer::FBO_AttachTexture3D( int texId, int index, int zOffset ) {
if( index < 0 || index >= glConfig.maxColorAttachments ) {
common->Printf( "R_AttachFBOTexture3D: invalid attachment index %i\n", index );
return;
}
glFramebufferTexture3DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + index, GL_TEXTURE_3D_EXT, texId, 0, zOffset );
}

/*
=================
FBO_AttachTextureDepth
=================
*/
void idFrameBuffer::FBO_AttachTextureDepth( int texId ) {
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texId, 0 );
}

/*
=================
FBO_AttachTexturePackedDepthStencil
=================
*/
void idFrameBuffer::FBO_AttachTexturePackedDepthStencil( int texId ) {
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, texId, 0 );
glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, texId, 0 );
}

/*
============
FBO_AttachTextureImage
============
*/
void idFrameBuffer::FBO_AttachTextureImage( idImage *img, int index ) {
if( !backEnd.glState.currentFBO ) {
common->Printf( "FBO: attempted to attach a texture image with no FBO bound!\n" );
return;
}
FBO_AttachTexture2D( GL_TEXTURE_2D, img->texnum, index );
backEnd.glState.currentFBO->colorImage[index] = img;
}

/*
============
FBO_Blit
============
*/
void idFrameBuffer::FBO_Blit(FBO_t *src, int srcBox[4], FBO_t *dst, int dstBox[4], int buffers, int filter) {
int srcBoxFinal[4], dstBoxFinal[4];
GLuint srcFb, dstFb;
// Nope ?!?
if (!glConfig.framebufferBlit) {
return;
}
srcFb = src ? src->frameBuffer : 0;
dstFb = dst ? dst->frameBuffer : 0;
if (!srcBox) {
if (src) {
srcBoxFinal[0] = 0;
srcBoxFinal[1] = 0;
srcBoxFinal[2] = src->width;
srcBoxFinal[3] = src->height;
} else {
srcBoxFinal[0] = 0;
srcBoxFinal[1] = 0;
srcBoxFinal[2] = glConfig.vidWidth;
srcBoxFinal[3] = glConfig.vidHeight;
}
} else {
srcBoxFinal[0] = srcBox[0];
srcBoxFinal[1] = srcBox[1];
srcBoxFinal[2] = srcBox[0] + srcBox[2];
srcBoxFinal[3] = srcBox[1] + srcBox[3];
}
if (!dstBox) {
if (dst) {
dstBoxFinal[0] = 0;
dstBoxFinal[1] = 0;
dstBoxFinal[2] = dst->width;
dstBoxFinal[3] = dst->height;
} else {
dstBoxFinal[0] = 0;
dstBoxFinal[1] = 0;
dstBoxFinal[2] = glConfig.vidWidth;
dstBoxFinal[3] = glConfig.vidHeight;
}
} else {
dstBoxFinal[0] = dstBox[0];
dstBoxFinal[1] = dstBox[1];
dstBoxFinal[2] = dstBox[0] + dstBox[2];
dstBoxFinal[3] = dstBox[1] + dstBox[3];
}
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, srcFb);
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, dstFb);
glBlitFramebufferEXT(srcBoxFinal[0], srcBoxFinal[1], srcBoxFinal[2], srcBoxFinal[3], dstBoxFinal[0], dstBoxFinal[1], dstBoxFinal[2], dstBoxFinal[3], buffers, filter);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
backEnd.glState.currentFBO = NULL;
}

/*
============
FBO_Bind
============
*/
void idFrameBuffer::FBO_Bind( FBO_t *fbo ) {
if( backEnd.glState.currentFBO == fbo ) {
return;
}
if( !fbo ) {
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 );
backEnd.glState.currentFBO = NULL;
return;
}
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo->frameBuffer );
backEnd.glState.currentFBO = fbo;
}

/*
=============
FBO_CheckFrameBuffer
=============
*/
bool idFrameBuffer::FBO_CheckFrameBuffer( const FBO_t *fbo ) {
int code;
int id;
glGetIntegerv( GL_FRAMEBUFFER_BINDING_EXT, &id );
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, fbo->frameBuffer );
code = glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
if( code == GL_FRAMEBUFFER_COMPLETE_EXT ) {
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, id );
return true;
}
// an error occured
switch( code ) {
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Unsupported framebuffer format\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete attachment\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, missing attachment\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, attached images must have same dimensions\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, attached images must have same format\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, missing draw buffer\n", fbo->name );
break;
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
common->Warning( "FBO_CheckFrameBuffer: (%s) Framebuffer incomplete, missing read buffer\n", fbo->name );
break;
default:
common->Warning( "FBO_CheckFrameBuffer: (%s) unknown error 0x%X\n", fbo->name, code );
break;
}
glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, id );
return false;
}

/*
============
FBO_Init
============
*/
void idFrameBuffer::FBO_Init( void ) {
int i;
int multisample;
cmdSystem->AddCommand( "ListFBOMem", R_ListFBOMem_f, CMD_FL_RENDERER, "lists Objects Allocated in FrameBufferCache Cache" );
common->Printf( "------- FBO_Init -------\n" );
if( !glConfig.framebufferObject ) {
return;
}
globalImages->numActiveFrameBufferImages = 0;
GL_CheckErrors();
glGetIntegerv( GL_MAX_SAMPLES_EXT, &multisample );
if( r_ext_framebuffer_multisample.GetInteger() < multisample ) {
multisample = r_ext_framebuffer_multisample.GetInteger();
}
if( multisample < 2 || !glConfig.framebufferBlit ) {
multisample = 0;
}
if( multisample != r_ext_framebuffer_multisample.GetInteger() ) {
r_ext_framebuffer_multisample.SetInteger( multisample );
}
// only create a render FBO if we need to resolve MSAA or do HDR
// otherwise just render straight to the screen (tr.renderFbo = NULL)
if( multisample && glConfig.framebufferMultisample ) {
globalImages->renderFbo = FBO_Create( "_render", globalImages->renderDepthImage->uploadWidth, globalImages->renderDepthImage->uploadHeight );
FBO_Bind( globalImages->renderFbo );
FBO_CreateBuffer( globalImages->renderFbo, GL_RGBA8, 0, multisample );
FBO_CreateBuffer( globalImages->renderFbo, GL_DEPTH_COMPONENT24_ARB, 0, multisample );
FBO_CheckFrameBuffer( globalImages->renderFbo );
globalImages->msaaResolveFbo = FBO_Create( "_msaaResolve", globalImages->renderDepthImage->uploadWidth, globalImages->renderDepthImage->uploadHeight );
FBO_Bind( globalImages->msaaResolveFbo );
FBO_AttachTextureImage( globalImages->renderImage, 0 );
FBO_AttachTextureDepth( globalImages->renderDepthImage->texnum );
FBO_CheckFrameBuffer( globalImages->msaaResolveFbo );
}
// clear render buffer
// this fixes the corrupt screen bug with r_hdr 1 on older hardware
if( globalImages->renderFbo ) {
FBO_Bind( globalImages->renderFbo );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
FBO_Bind( NULL );
}
for( i = 0; i < 2; i++ ) {
globalImages->textureScratchFbo = FBO_Create( va( "_texturescratch%d", i ), globalImages->textureScratchImage->uploadWidth, globalImages->textureScratchImage->uploadHeight );
FBO_Bind( globalImages->textureScratchFbo );
FBO_AttachTextureImage( globalImages->textureScratchImage, 0 );
FBO_CheckFrameBuffer( globalImages->textureScratchFbo );
}
{
globalImages->calcLevelsFbo = FBO_Create( "_calclevels", globalImages->calcLevelsImage->uploadWidth, globalImages->calcLevelsImage->uploadHeight );
FBO_Bind( globalImages->calcLevelsFbo );
FBO_AttachTextureImage( globalImages->calcLevelsImage, 0 );
FBO_CheckFrameBuffer( globalImages->calcLevelsFbo );
}
{
globalImages->targetLevelsFbo = FBO_Create( "_targetlevels", globalImages->targetLevelsImage->uploadWidth, globalImages->targetLevelsImage->uploadHeight );
FBO_Bind( globalImages->targetLevelsFbo );
FBO_AttachTextureImage( globalImages->targetLevelsImage, 0 );
FBO_CheckFrameBuffer( globalImages->targetLevelsFbo );
}
for( i = 0; i < 2; i++ ) {
globalImages->quarterFbo = FBO_Create( va( "_quarter%d", i ), globalImages->quarterImage->uploadWidth, globalImages->quarterImage->uploadHeight );
FBO_Bind( globalImages->quarterFbo );
FBO_AttachTextureImage( globalImages->quarterImage, 0 );
FBO_CheckFrameBuffer( globalImages->quarterFbo );
}
if( globalImages->renderCubeImage ) {
globalImages->renderCubeFbo = FBO_Create( "_renderCubeFbo", globalImages->renderCubeImage->uploadWidth, globalImages->renderCubeImage->uploadHeight );
FBO_Bind( globalImages->renderCubeFbo );
FBO_AttachTexture2D( GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, globalImages->renderCubeImage->texnum, 0 );
backEnd.glState.currentFBO->colorImage[0] = globalImages->renderCubeImage;
FBO_CreateBuffer( globalImages->renderCubeFbo, GL_DEPTH_COMPONENT24_ARB, 0, 0 );
FBO_CheckFrameBuffer( globalImages->renderCubeFbo );
}
GL_CheckErrors();
FBO_Bind( NULL );
}

/*
============
FBO_Shutdown
============
*/
void idFrameBuffer::FBO_Shutdown( void ) {
int i, j;
FBO_t *fbo;
common->Printf( "------- FBO_Shutdown -------\n" );
if( !glConfig.framebufferObject ) {
return;
}
FBO_Bind( NULL );
for( i = 0; i < globalImages->numActiveFrameBufferImages; i++ ) {
fbo = globalImages->frameBufferObjects;
for( j = 0; j < glConfig.maxColorAttachments; j++ ) {
if( fbo->colorBuffers[j] ) {
glDeleteRenderbuffersEXT( 1, &fbo->colorBuffers[j] );
}
}
if( fbo->depthBuffer ) {
glDeleteRenderbuffersEXT( 1, &fbo->depthBuffer );
}
if( fbo->stencilBuffer ) {
glDeleteRenderbuffersEXT( 1, &fbo->stencilBuffer );
}
if( fbo->frameBuffer ) {
glDeleteFramebuffersEXT( 1, &fbo->frameBuffer );
}
delete[] fbo;
fbo = NULL;
}
}

/*
============
FBO_List
============
*/
void idFrameBuffer::FBO_List( void ) {
int i;
FBO_t *fbo;
if( !glConfig.framebufferObject ) {
common->Printf( "GL_EXT_framebuffer_object is not available.\n" );
return;
}
common->Printf( " size name\n" );
common->Printf( "----------------------------------------------------------\n" );
for( i = 0; i < globalImages->numActiveFrameBufferImages; i++ ) {
fbo = globalImages->frameBufferObjects;
common->Printf( " %4i: %4i %4i %s\n", i, fbo->width, fbo->height, fbo->name );
}
common->Printf( " %i FBOs\n", globalImages->numActiveFrameBufferImages );
}

 

 

and the header

 

 

 

/*
===========================================================================
Copyright © 2010 James Canete (use.less01@gmail.com)

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_fbo.h

#ifndef __TR_FBO_H__
#define __TR_FBO_H__

typedef struct FBO_s {
char name[256];

int index;

uint32_t frameBuffer;

uint32_t colorBuffers[16];
int colorFormat;
idImage *colorImage[16];

uint32_t depthBuffer;
int depthFormat;

uint32_t stencilBuffer;
int stencilFormat;

uint32_t packedDepthStencilBuffer;
int packedDepthStencilFormat;

int width;
int height;
} FBO_t;

class idFrameBuffer {
public:
void FBO_Init();
void FBO_Shutdown();

bool FBO_CheckFrameBuffer( const FBO_t *fbo );
FBO_t *FBO_Create( const char *name, int width, int height );
void FBO_CreateBuffer( FBO_t *fbo, int format, int index, int multisample );

void FBO_AttachTexture1D( int texId, int index );
void FBO_AttachTexture2D( int target, int texId, int index );
void FBO_AttachTexture3D( int texId, int index, int zOffset );
void FBO_AttachTextureDepth( int texId );
void FBO_AttachTexturePackedDepthStencil( int texId );
void FBO_AttachTextureImage( idImage *img, int index );

void FBO_Blit( FBO_t *src, int srcBox[4], FBO_t *dst, int dstBox[4], int buffers, int filter );

void FBO_Bind( FBO_t *fbo );
void FBO_List( void );
};

extern idFrameBuffer FrameBufferCache;

#endif

 

 

also need some support functions in other parts of the idtech4 source code to determine if framebuffers are avaliable and to hook into the idImage class.

 

Ill upload an experimental branch of my own engine to github soon for people to hack at ;) feel free to change things as you see fit for darkmod.

  • Like 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recent Status Updates

    • JackFarmer

      I will NOT return to Discord. Who wants to try something new?
       
      · 12 replies
    • JackFarmer

      I do not understand why Gilmour/Waters trash the "Atom Heart Mother" album and wish for that "all existing copies get dumped".
      I think it is a great recording. Is there something wrong inside my twisted little brain?
      · 3 replies
    • revelator

      Asus ROG strix x570-E gaming ryzen 3700x 32 gb ram radeon RX9070XT 10 tb ssd space (all full)
      · 5 replies
    • The Black Arrow

      I was playing Doom 3, and I learned how to edit maps text-by-text, this should help me a lot with finding secrets and locations I hope.
      That is, if I feel sneaky and greedy...Which I always do, can't get enough 100% a mission.
      · 3 replies
    • datiswous

      So on a regular basis I try to start building something in Darkradiant but very quickly I loose interest and give up.
      I tend to
      create a room, position an ai in it (or first ai, then room, probably better) put a light in it Put some stuff in it test the room in tdm Now I had this new idea:
      Start of with a gigantic room. To skip the importance of sealing for the time being and to think broader than one room. Then just use ground without wall, just to visualise the structure of the level.  Then just use prefab walls and other elements (arches, etc.). Just building area's that aren't enclosed. I can always close them at a later point. Maybe this is all very logical from a mappers perspective, but tdm is the first game where I have done any kind of real mapping and I always stop at the start. It just takes too long to make something exciting.
      I want to build something at some point, because over time I accumulated lots of ideas.
      · 21 replies
×
×
  • Create New...