2012-10-26 01:23:06 +00:00
|
|
|
/*
|
|
|
|
===========================================================================
|
|
|
|
Copyright (C) 2006 Kirk Barnes
|
|
|
|
Copyright (C) 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"
|
|
|
|
|
2016-01-20 14:32:50 +00:00
|
|
|
#include "tr_dsa.h"
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
/*
|
|
|
|
=============
|
|
|
|
R_CheckFBO
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
qboolean R_CheckFBO(const FBO_t * fbo)
|
|
|
|
{
|
2017-07-13 19:03:10 +00:00
|
|
|
GLenum code = qglCheckNamedFramebufferStatusEXT(fbo->frameBuffer, GL_FRAMEBUFFER);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
if(code == GL_FRAMEBUFFER_COMPLETE)
|
2012-10-26 01:23:06 +00:00
|
|
|
return qtrue;
|
|
|
|
|
|
|
|
// an error occured
|
|
|
|
switch (code)
|
|
|
|
{
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED:
|
2012-10-26 01:23:06 +00:00
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Unsupported framebuffer format\n", fbo->name);
|
|
|
|
break;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
2012-10-26 01:23:06 +00:00
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete attachment\n", fbo->name);
|
|
|
|
break;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
|
2012-10-26 01:23:06 +00:00
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete, missing attachment\n", fbo->name);
|
|
|
|
break;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
|
2012-10-26 01:23:06 +00:00
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete, missing draw buffer\n", fbo->name);
|
|
|
|
break;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
|
2012-10-26 01:23:06 +00:00
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete, missing read buffer\n", fbo->name);
|
|
|
|
break;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
|
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) Framebuffer incomplete multisample\n", fbo->name);
|
|
|
|
break;
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
default:
|
|
|
|
ri.Printf(PRINT_WARNING, "R_CheckFBO: (%s) unknown error 0x%X\n", fbo->name, code);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
FBO_Create
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
FBO_t *FBO_Create(const char *name, int width, int height)
|
|
|
|
{
|
|
|
|
FBO_t *fbo;
|
|
|
|
|
|
|
|
if(strlen(name) >= MAX_QPATH)
|
|
|
|
{
|
2013-05-30 20:32:44 +00:00
|
|
|
ri.Error(ERR_DROP, "FBO_Create: \"%s\" is too long", name);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(width <= 0 || width > glRefConfig.maxRenderbufferSize)
|
|
|
|
{
|
|
|
|
ri.Error(ERR_DROP, "FBO_Create: bad width %i", width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(height <= 0 || height > glRefConfig.maxRenderbufferSize)
|
|
|
|
{
|
|
|
|
ri.Error(ERR_DROP, "FBO_Create: bad height %i", height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tr.numFBOs == MAX_FBOS)
|
|
|
|
{
|
|
|
|
ri.Error(ERR_DROP, "FBO_Create: MAX_FBOS hit");
|
|
|
|
}
|
|
|
|
|
|
|
|
fbo = tr.fbos[tr.numFBOs] = ri.Hunk_Alloc(sizeof(*fbo), h_low);
|
|
|
|
Q_strncpyz(fbo->name, name, sizeof(fbo->name));
|
|
|
|
fbo->index = tr.numFBOs++;
|
|
|
|
fbo->width = width;
|
|
|
|
fbo->height = height;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
qglGenFramebuffers(1, &fbo->frameBuffer);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
return fbo;
|
|
|
|
}
|
|
|
|
|
2016-01-20 14:32:50 +00:00
|
|
|
/*
|
|
|
|
=================
|
|
|
|
FBO_CreateBuffer
|
|
|
|
=================
|
|
|
|
*/
|
2012-10-26 01:23:06 +00:00
|
|
|
void FBO_CreateBuffer(FBO_t *fbo, int format, int index, int multisample)
|
|
|
|
{
|
|
|
|
uint32_t *pRenderBuffer;
|
|
|
|
GLenum attachment;
|
|
|
|
qboolean 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];
|
2017-07-13 19:03:10 +00:00
|
|
|
attachment = GL_COLOR_ATTACHMENT0 + index;
|
2012-10-26 01:23:06 +00:00
|
|
|
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;
|
2017-07-13 19:03:10 +00:00
|
|
|
attachment = GL_DEPTH_ATTACHMENT;
|
2012-10-26 01:23:06 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_STENCIL_INDEX:
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_STENCIL_INDEX1:
|
|
|
|
case GL_STENCIL_INDEX4:
|
|
|
|
case GL_STENCIL_INDEX8:
|
|
|
|
case GL_STENCIL_INDEX16:
|
2012-10-26 01:23:06 +00:00
|
|
|
fbo->stencilFormat = format;
|
|
|
|
pRenderBuffer = &fbo->stencilBuffer;
|
2017-07-13 19:03:10 +00:00
|
|
|
attachment = GL_STENCIL_ATTACHMENT;
|
2012-10-26 01:23:06 +00:00
|
|
|
break;
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
case GL_DEPTH_STENCIL:
|
|
|
|
case GL_DEPTH24_STENCIL8:
|
2012-10-26 01:23:06 +00:00
|
|
|
fbo->packedDepthStencilFormat = format;
|
|
|
|
pRenderBuffer = &fbo->packedDepthStencilBuffer;
|
|
|
|
attachment = 0; // special for stencil and depth
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ri.Printf(PRINT_WARNING, "FBO_CreateBuffer: invalid format %d\n", format);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
absent = *pRenderBuffer == 0;
|
|
|
|
if (absent)
|
2017-07-13 19:03:10 +00:00
|
|
|
qglGenRenderbuffers(1, pRenderBuffer);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (multisample && glRefConfig.framebufferMultisample)
|
2016-07-29 03:04:25 +00:00
|
|
|
qglNamedRenderbufferStorageMultisampleEXT(*pRenderBuffer, multisample, format, fbo->width, fbo->height);
|
2012-10-26 01:23:06 +00:00
|
|
|
else
|
2016-07-29 03:04:25 +00:00
|
|
|
qglNamedRenderbufferStorageEXT(*pRenderBuffer, format, fbo->width, fbo->height);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(absent)
|
|
|
|
{
|
|
|
|
if (attachment == 0)
|
|
|
|
{
|
2017-07-13 19:03:10 +00:00
|
|
|
qglNamedFramebufferRenderbufferEXT(fbo->frameBuffer, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, *pRenderBuffer);
|
|
|
|
qglNamedFramebufferRenderbufferEXT(fbo->frameBuffer, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, *pRenderBuffer);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
2016-01-20 14:32:50 +00:00
|
|
|
{
|
2017-07-13 19:03:10 +00:00
|
|
|
qglNamedFramebufferRenderbufferEXT(fbo->frameBuffer, attachment, GL_RENDERBUFFER, *pRenderBuffer);
|
2016-01-20 14:32:50 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
=================
|
2016-01-20 14:32:50 +00:00
|
|
|
FBO_AttachImage
|
2012-10-26 01:23:06 +00:00
|
|
|
=================
|
|
|
|
*/
|
2016-01-22 06:31:41 +00:00
|
|
|
void FBO_AttachImage(FBO_t *fbo, image_t *image, GLenum attachment, GLuint cubemapside)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-01-20 14:32:50 +00:00
|
|
|
GLenum target = GL_TEXTURE_2D;
|
|
|
|
int index;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-01-20 14:32:50 +00:00
|
|
|
if (image->flags & IMGFLAG_CUBEMAP)
|
2016-01-22 06:31:41 +00:00
|
|
|
target = GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + cubemapside;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-07-29 03:04:25 +00:00
|
|
|
qglNamedFramebufferTexture2DEXT(fbo->frameBuffer, attachment, target, image->texnum, 0);
|
2017-07-13 19:03:10 +00:00
|
|
|
index = attachment - GL_COLOR_ATTACHMENT0;
|
2016-01-20 14:32:50 +00:00
|
|
|
if (index >= 0 && index <= 15)
|
|
|
|
fbo->colorImage[index] = image;
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
FBO_Bind
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void FBO_Bind(FBO_t * fbo)
|
|
|
|
{
|
2016-12-07 22:13:29 +00:00
|
|
|
if (!glRefConfig.framebufferObject)
|
|
|
|
{
|
|
|
|
ri.Printf(PRINT_WARNING, "FBO_Bind() called without framebuffers enabled!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-20 03:34:16 +00:00
|
|
|
if (glState.currentFBO == fbo)
|
2012-10-26 01:23:06 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (r_logFile->integer)
|
|
|
|
{
|
|
|
|
// don't just call LogComment, or we will get a call to va() every frame!
|
2016-01-20 14:32:50 +00:00
|
|
|
GLimp_LogComment(va("--- FBO_Bind( %s ) ---\n", fbo ? fbo->name : "NULL"));
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
GL_BindFramebuffer(GL_FRAMEBUFFER, fbo ? fbo->frameBuffer : 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
glState.currentFBO = fbo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
FBO_Init
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void FBO_Init(void)
|
|
|
|
{
|
|
|
|
int i;
|
2016-02-18 03:49:21 +00:00
|
|
|
int hdrFormat, multisample = 0;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, "------- FBO_Init -------\n");
|
|
|
|
|
|
|
|
if(!glRefConfig.framebufferObject)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tr.numFBOs = 0;
|
|
|
|
|
|
|
|
GL_CheckErrors();
|
|
|
|
|
2013-01-24 22:53:08 +00:00
|
|
|
R_IssuePendingRenderCommands();
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
hdrFormat = GL_RGBA8;
|
2016-12-08 06:30:55 +00:00
|
|
|
if (r_hdr->integer && glRefConfig.textureFloat)
|
2013-11-20 08:48:18 +00:00
|
|
|
hdrFormat = GL_RGBA16F_ARB;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-02-18 03:49:21 +00:00
|
|
|
if (glRefConfig.framebufferMultisample)
|
2017-07-13 19:03:10 +00:00
|
|
|
qglGetIntegerv(GL_MAX_SAMPLES, &multisample);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (r_ext_framebuffer_multisample->integer < multisample)
|
|
|
|
multisample = r_ext_framebuffer_multisample->integer;
|
|
|
|
|
|
|
|
if (multisample < 2 || !glRefConfig.framebufferBlit)
|
|
|
|
multisample = 0;
|
|
|
|
|
|
|
|
if (multisample != r_ext_framebuffer_multisample->integer)
|
|
|
|
ri.Cvar_SetValue("r_ext_framebuffer_multisample", (float)multisample);
|
|
|
|
|
2012-11-20 03:34:16 +00:00
|
|
|
// only create a render FBO if we need to resolve MSAA or do HDR
|
|
|
|
// otherwise just render straight to the screen (tr.renderFbo = NULL)
|
2012-10-26 01:23:06 +00:00
|
|
|
if (multisample && glRefConfig.framebufferMultisample)
|
|
|
|
{
|
|
|
|
tr.renderFbo = FBO_Create("_render", tr.renderDepthImage->width, tr.renderDepthImage->height);
|
|
|
|
FBO_CreateBuffer(tr.renderFbo, hdrFormat, 0, multisample);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_CreateBuffer(tr.renderFbo, GL_DEPTH_COMPONENT24, 0, multisample);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.renderFbo);
|
|
|
|
|
|
|
|
tr.msaaResolveFbo = FBO_Create("_msaaResolve", tr.renderDepthImage->width, tr.renderDepthImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.msaaResolveFbo, tr.renderImage, GL_COLOR_ATTACHMENT0, 0);
|
|
|
|
FBO_AttachImage(tr.msaaResolveFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.msaaResolveFbo);
|
|
|
|
}
|
2012-11-20 03:34:16 +00:00
|
|
|
else if (r_hdr->integer)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
tr.renderFbo = FBO_Create("_render", tr.renderDepthImage->width, tr.renderDepthImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.renderFbo, tr.renderImage, GL_COLOR_ATTACHMENT0, 0);
|
|
|
|
FBO_AttachImage(tr.renderFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.renderFbo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear render buffer
|
|
|
|
// this fixes the corrupt screen bug with r_hdr 1 on older hardware
|
2012-11-20 03:34:16 +00:00
|
|
|
if (tr.renderFbo)
|
|
|
|
{
|
2017-07-13 19:03:10 +00:00
|
|
|
GL_BindFramebuffer(GL_FRAMEBUFFER, tr.renderFbo->frameBuffer);
|
2012-11-20 03:34:16 +00:00
|
|
|
qglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.screenScratchImage)
|
2016-03-07 10:27:03 +00:00
|
|
|
{
|
|
|
|
tr.screenScratchFbo = FBO_Create("screenScratch", tr.screenScratchImage->width, tr.screenScratchImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.screenScratchFbo, tr.screenScratchImage, GL_COLOR_ATTACHMENT0, 0);
|
|
|
|
FBO_AttachImage(tr.screenScratchFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0);
|
2016-03-07 10:27:03 +00:00
|
|
|
R_CheckFBO(tr.screenScratchFbo);
|
|
|
|
}
|
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.sunRaysImage)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-02-05 04:33:58 +00:00
|
|
|
tr.sunRaysFbo = FBO_Create("_sunRays", tr.renderDepthImage->width, tr.renderDepthImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.sunRaysFbo, tr.sunRaysImage, GL_COLOR_ATTACHMENT0, 0);
|
|
|
|
FBO_AttachImage(tr.sunRaysFbo, tr.renderDepthImage, GL_DEPTH_ATTACHMENT, 0);
|
2013-02-05 04:33:58 +00:00
|
|
|
R_CheckFBO(tr.sunRaysFbo);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-13 04:52:36 +00:00
|
|
|
if (MAX_DRAWN_PSHADOWS && tr.pshadowMaps[0])
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2014-01-13 04:52:36 +00:00
|
|
|
for( i = 0; i < MAX_DRAWN_PSHADOWS; i++)
|
|
|
|
{
|
|
|
|
tr.pshadowFbos[i] = FBO_Create(va("_shadowmap%d", i), tr.pshadowMaps[i]->width, tr.pshadowMaps[i]->height);
|
2017-07-14 23:15:02 +00:00
|
|
|
// FIXME: this next line wastes 16mb with 16x512x512 sun shadow maps, skip if OpenGL 4.3+ or ARB_framebuffer_no_attachments
|
|
|
|
FBO_CreateBuffer(tr.pshadowFbos[i], GL_RGBA8, 0, 0);
|
|
|
|
FBO_AttachImage(tr.pshadowFbos[i], tr.pshadowMaps[i], GL_DEPTH_ATTACHMENT, 0);
|
2014-01-13 04:52:36 +00:00
|
|
|
R_CheckFBO(tr.pshadowFbos[i]);
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-13 04:52:36 +00:00
|
|
|
if (tr.sunShadowDepthImage[0])
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-03-10 02:03:23 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2014-01-13 04:52:36 +00:00
|
|
|
{
|
|
|
|
tr.sunShadowFbo[i] = FBO_Create("_sunshadowmap", tr.sunShadowDepthImage[i]->width, tr.sunShadowDepthImage[i]->height);
|
2016-02-18 03:49:21 +00:00
|
|
|
// FIXME: this next line wastes 16mb with 4x1024x1024 sun shadow maps, skip if OpenGL 4.3+ or ARB_framebuffer_no_attachments
|
|
|
|
// This at least gets sun shadows working on older GPUs (Intel)
|
|
|
|
FBO_CreateBuffer(tr.sunShadowFbo[i], GL_RGBA8, 0, 0);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.sunShadowFbo[i], tr.sunShadowDepthImage[i], GL_DEPTH_ATTACHMENT, 0);
|
2014-01-13 04:52:36 +00:00
|
|
|
R_CheckFBO(tr.sunShadowFbo[i]);
|
|
|
|
}
|
2016-03-10 02:03:23 +00:00
|
|
|
}
|
2014-01-13 04:52:36 +00:00
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.screenShadowImage)
|
|
|
|
{
|
2014-01-13 04:52:36 +00:00
|
|
|
tr.screenShadowFbo = FBO_Create("_screenshadow", tr.screenShadowImage->width, tr.screenShadowImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.screenShadowFbo, tr.screenShadowImage, GL_COLOR_ATTACHMENT0, 0);
|
2014-01-13 04:52:36 +00:00
|
|
|
R_CheckFBO(tr.screenShadowFbo);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.textureScratchImage[0])
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-03-10 02:03:23 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
tr.textureScratchFbo[i] = FBO_Create(va("_texturescratch%d", i), tr.textureScratchImage[i]->width, tr.textureScratchImage[i]->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.textureScratchFbo[i], tr.textureScratchImage[i], GL_COLOR_ATTACHMENT0, 0);
|
2016-03-10 02:03:23 +00:00
|
|
|
R_CheckFBO(tr.textureScratchFbo[i]);
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.calcLevelsImage)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
tr.calcLevelsFbo = FBO_Create("_calclevels", tr.calcLevelsImage->width, tr.calcLevelsImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.calcLevelsFbo, tr.calcLevelsImage, GL_COLOR_ATTACHMENT0, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.calcLevelsFbo);
|
|
|
|
}
|
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.targetLevelsImage)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
tr.targetLevelsFbo = FBO_Create("_targetlevels", tr.targetLevelsImage->width, tr.targetLevelsImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.targetLevelsFbo, tr.targetLevelsImage, GL_COLOR_ATTACHMENT0, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.targetLevelsFbo);
|
|
|
|
}
|
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.quarterImage[0])
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-03-10 02:03:23 +00:00
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
tr.quarterFbo[i] = FBO_Create(va("_quarter%d", i), tr.quarterImage[i]->width, tr.quarterImage[i]->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.quarterFbo[i], tr.quarterImage[i], GL_COLOR_ATTACHMENT0, 0);
|
2016-03-10 02:03:23 +00:00
|
|
|
R_CheckFBO(tr.quarterFbo[i]);
|
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.hdrDepthImage)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
|
|
|
tr.hdrDepthFbo = FBO_Create("_hdrDepth", tr.hdrDepthImage->width, tr.hdrDepthImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.hdrDepthFbo, tr.hdrDepthImage, GL_COLOR_ATTACHMENT0, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.hdrDepthFbo);
|
2016-03-10 02:03:23 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-03-10 02:03:23 +00:00
|
|
|
if (tr.screenSsaoImage)
|
|
|
|
{
|
2012-10-26 01:23:06 +00:00
|
|
|
tr.screenSsaoFbo = FBO_Create("_screenssao", tr.screenSsaoImage->width, tr.screenSsaoImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.screenSsaoFbo, tr.screenSsaoImage, GL_COLOR_ATTACHMENT0, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
R_CheckFBO(tr.screenSsaoFbo);
|
|
|
|
}
|
|
|
|
|
2014-01-13 04:52:36 +00:00
|
|
|
if (tr.renderCubeImage)
|
2013-09-16 07:54:26 +00:00
|
|
|
{
|
|
|
|
tr.renderCubeFbo = FBO_Create("_renderCubeFbo", tr.renderCubeImage->width, tr.renderCubeImage->height);
|
2017-07-13 19:03:10 +00:00
|
|
|
FBO_AttachImage(tr.renderCubeFbo, tr.renderCubeImage, GL_COLOR_ATTACHMENT0, 0);
|
2013-09-16 07:54:26 +00:00
|
|
|
FBO_CreateBuffer(tr.renderCubeFbo, GL_DEPTH_COMPONENT24_ARB, 0, 0);
|
|
|
|
R_CheckFBO(tr.renderCubeFbo);
|
|
|
|
}
|
|
|
|
|
2012-10-26 01:23:06 +00:00
|
|
|
GL_CheckErrors();
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
GL_BindFramebuffer(GL_FRAMEBUFFER, 0);
|
2016-01-20 14:32:50 +00:00
|
|
|
glState.currentFBO = NULL;
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
FBO_Shutdown
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void FBO_Shutdown(void)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
FBO_t *fbo;
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, "------- FBO_Shutdown -------\n");
|
|
|
|
|
|
|
|
if(!glRefConfig.framebufferObject)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FBO_Bind(NULL);
|
|
|
|
|
|
|
|
for(i = 0; i < tr.numFBOs; i++)
|
|
|
|
{
|
|
|
|
fbo = tr.fbos[i];
|
|
|
|
|
|
|
|
for(j = 0; j < glRefConfig.maxColorAttachments; j++)
|
|
|
|
{
|
|
|
|
if(fbo->colorBuffers[j])
|
2017-07-13 19:03:10 +00:00
|
|
|
qglDeleteRenderbuffers(1, &fbo->colorBuffers[j]);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(fbo->depthBuffer)
|
2017-07-13 19:03:10 +00:00
|
|
|
qglDeleteRenderbuffers(1, &fbo->depthBuffer);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(fbo->stencilBuffer)
|
2017-07-13 19:03:10 +00:00
|
|
|
qglDeleteRenderbuffers(1, &fbo->stencilBuffer);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if(fbo->frameBuffer)
|
2017-07-13 19:03:10 +00:00
|
|
|
qglDeleteFramebuffers(1, &fbo->frameBuffer);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
R_FBOList_f
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void R_FBOList_f(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FBO_t *fbo;
|
|
|
|
|
|
|
|
if(!glRefConfig.framebufferObject)
|
|
|
|
{
|
|
|
|
ri.Printf(PRINT_ALL, "GL_EXT_framebuffer_object is not available.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, " size name\n");
|
|
|
|
ri.Printf(PRINT_ALL, "----------------------------------------------------------\n");
|
|
|
|
|
|
|
|
for(i = 0; i < tr.numFBOs; i++)
|
|
|
|
{
|
|
|
|
fbo = tr.fbos[i];
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, " %4i: %4i %4i %s\n", i, fbo->width, fbo->height, fbo->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ri.Printf(PRINT_ALL, " %i FBOs\n", tr.numFBOs);
|
|
|
|
}
|
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
void FBO_BlitFromTexture(struct image_s *src, vec4_t inSrcTexCorners, vec2_t inSrcTexScale, FBO_t *dst, ivec4_t inDstBox, struct shaderProgram_s *shaderProgram, vec4_t inColor, int blend)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
ivec4_t dstBox;
|
2012-10-26 01:23:06 +00:00
|
|
|
vec4_t color;
|
|
|
|
vec4_t quadVerts[4];
|
|
|
|
vec2_t texCoords[4];
|
|
|
|
vec2_t invTexRes;
|
|
|
|
FBO_t *oldFbo = glState.currentFBO;
|
2013-11-19 11:23:50 +00:00
|
|
|
mat4_t projection;
|
2012-10-30 22:05:07 +00:00
|
|
|
int width, height;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (!src)
|
2016-01-22 06:31:41 +00:00
|
|
|
{
|
|
|
|
ri.Printf(PRINT_WARNING, "Tried to blit from a NULL texture!\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
return;
|
2016-01-22 06:31:41 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
width = dst ? dst->width : glConfig.vidWidth;
|
|
|
|
height = dst ? dst->height : glConfig.vidHeight;
|
|
|
|
|
|
|
|
if (inSrcTexCorners)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorSet2(texCoords[0], inSrcTexCorners[0], inSrcTexCorners[1]);
|
|
|
|
VectorSet2(texCoords[1], inSrcTexCorners[2], inSrcTexCorners[1]);
|
|
|
|
VectorSet2(texCoords[2], inSrcTexCorners[2], inSrcTexCorners[3]);
|
|
|
|
VectorSet2(texCoords[3], inSrcTexCorners[0], inSrcTexCorners[3]);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorSet2(texCoords[0], 0.0f, 1.0f);
|
|
|
|
VectorSet2(texCoords[1], 1.0f, 1.0f);
|
|
|
|
VectorSet2(texCoords[2], 1.0f, 0.0f);
|
|
|
|
VectorSet2(texCoords[3], 0.0f, 0.0f);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// framebuffers are 0 bottom, Y up.
|
|
|
|
if (inDstBox)
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
dstBox[0] = inDstBox[0];
|
|
|
|
dstBox[1] = height - inDstBox[1] - inDstBox[3];
|
|
|
|
dstBox[2] = inDstBox[0] + inDstBox[2];
|
|
|
|
dstBox[3] = height - inDstBox[1];
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorSet4(dstBox, 0, height, width, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inSrcTexScale)
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorCopy2(inSrcTexScale, invTexRes);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorSet2(invTexRes, 1.0f, 1.0f);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inColor)
|
|
|
|
{
|
|
|
|
VectorCopy4(inColor, color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-06 10:02:01 +00:00
|
|
|
VectorCopy4(colorWhite, color);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!shaderProgram)
|
|
|
|
{
|
|
|
|
shaderProgram = &tr.textureColorShader;
|
|
|
|
}
|
|
|
|
|
|
|
|
FBO_Bind(dst);
|
|
|
|
|
2012-10-30 22:05:07 +00:00
|
|
|
qglViewport( 0, 0, width, height );
|
|
|
|
qglScissor( 0, 0, width, height );
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
Mat4Ortho(0, width, height, 0, 0, 1, projection);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2014-10-29 06:32:50 +00:00
|
|
|
GL_Cull( CT_TWO_SIDED );
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2012-10-30 22:05:07 +00:00
|
|
|
GL_BindToTMU(src, TB_COLORMAP);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorSet4(quadVerts[0], dstBox[0], dstBox[1], 0.0f, 1.0f);
|
|
|
|
VectorSet4(quadVerts[1], dstBox[2], dstBox[1], 0.0f, 1.0f);
|
|
|
|
VectorSet4(quadVerts[2], dstBox[2], dstBox[3], 0.0f, 1.0f);
|
|
|
|
VectorSet4(quadVerts[3], dstBox[0], dstBox[3], 0.0f, 1.0f);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
invTexRes[0] /= src->width;
|
|
|
|
invTexRes[1] /= src->height;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
GL_State( blend );
|
|
|
|
|
|
|
|
GLSL_BindProgram(shaderProgram);
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
GLSL_SetUniformMat4(shaderProgram, UNIFORM_MODELVIEWPROJECTIONMATRIX, projection);
|
2013-03-21 05:53:30 +00:00
|
|
|
GLSL_SetUniformVec4(shaderProgram, UNIFORM_COLOR, color);
|
|
|
|
GLSL_SetUniformVec2(shaderProgram, UNIFORM_INVTEXRES, invTexRes);
|
|
|
|
GLSL_SetUniformVec2(shaderProgram, UNIFORM_AUTOEXPOSUREMINMAX, tr.refdef.autoExposureMinMax);
|
|
|
|
GLSL_SetUniformVec3(shaderProgram, UNIFORM_TONEMINAVGMAXLINEAR, tr.refdef.toneMinAvgMaxLinear);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
RB_InstantQuad2(quadVerts, texCoords);
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
FBO_Bind(oldFbo);
|
|
|
|
}
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
void FBO_Blit(FBO_t *src, ivec4_t inSrcBox, vec2_t srcTexScale, FBO_t *dst, ivec4_t dstBox, struct shaderProgram_s *shaderProgram, vec4_t color, int blend)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
vec4_t srcTexCorners;
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (!src)
|
2013-09-16 07:54:26 +00:00
|
|
|
{
|
|
|
|
ri.Printf(PRINT_WARNING, "Tried to blit from a NULL FBO!\n");
|
2012-10-26 01:23:06 +00:00
|
|
|
return;
|
2013-09-16 07:54:26 +00:00
|
|
|
}
|
2012-10-26 01:23:06 +00:00
|
|
|
|
|
|
|
if (inSrcBox)
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
srcTexCorners[0] = inSrcBox[0] / (float)src->width;
|
|
|
|
srcTexCorners[1] = (inSrcBox[1] + inSrcBox[3]) / (float)src->height;
|
|
|
|
srcTexCorners[2] = (inSrcBox[0] + inSrcBox[2]) / (float)src->width;
|
|
|
|
srcTexCorners[3] = inSrcBox[1] / (float)src->height;
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
VectorSet4(srcTexCorners, 0.0f, 0.0f, 1.0f, 1.0f);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 09:37:05 +00:00
|
|
|
FBO_BlitFromTexture(src->colorImage[0], srcTexCorners, srcTexScale, dst, dstBox, shaderProgram, color, blend | GLS_DEPTHTEST_DISABLE);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 11:23:50 +00:00
|
|
|
void FBO_FastBlit(FBO_t *src, ivec4_t srcBox, FBO_t *dst, ivec4_t dstBox, int buffers, int filter)
|
2012-10-26 01:23:06 +00:00
|
|
|
{
|
2013-11-19 11:23:50 +00:00
|
|
|
ivec4_t srcBoxFinal, dstBoxFinal;
|
2012-10-26 01:23:06 +00:00
|
|
|
GLuint srcFb, dstFb;
|
|
|
|
|
|
|
|
if (!glRefConfig.framebufferBlit)
|
|
|
|
{
|
|
|
|
FBO_Blit(src, srcBox, NULL, dst, dstBox, NULL, NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFb = src ? src->frameBuffer : 0;
|
|
|
|
dstFb = dst ? dst->frameBuffer : 0;
|
|
|
|
|
|
|
|
if (!srcBox)
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
int width = src ? src->width : glConfig.vidWidth;
|
|
|
|
int height = src ? src->height : glConfig.vidHeight;
|
|
|
|
|
|
|
|
VectorSet4(srcBoxFinal, 0, 0, width, height);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VectorSet4(srcBoxFinal, srcBox[0], srcBox[1], srcBox[0] + srcBox[2], srcBox[1] + srcBox[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dstBox)
|
|
|
|
{
|
2016-04-05 09:37:05 +00:00
|
|
|
int width = dst ? dst->width : glConfig.vidWidth;
|
|
|
|
int height = dst ? dst->height : glConfig.vidHeight;
|
|
|
|
|
|
|
|
VectorSet4(dstBoxFinal, 0, 0, width, height);
|
2012-10-26 01:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VectorSet4(dstBoxFinal, dstBox[0], dstBox[1], dstBox[0] + dstBox[2], dstBox[1] + dstBox[3]);
|
|
|
|
}
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
GL_BindFramebuffer(GL_READ_FRAMEBUFFER, srcFb);
|
|
|
|
GL_BindFramebuffer(GL_DRAW_FRAMEBUFFER, dstFb);
|
|
|
|
qglBlitFramebuffer(srcBoxFinal[0], srcBoxFinal[1], srcBoxFinal[2], srcBoxFinal[3],
|
2012-10-26 01:23:06 +00:00
|
|
|
dstBoxFinal[0], dstBoxFinal[1], dstBoxFinal[2], dstBoxFinal[3],
|
|
|
|
buffers, filter);
|
|
|
|
|
2017-07-13 19:03:10 +00:00
|
|
|
GL_BindFramebuffer(GL_FRAMEBUFFER, 0);
|
2012-10-26 01:23:06 +00:00
|
|
|
glState.currentFBO = NULL;
|
2013-01-24 22:53:08 +00:00
|
|
|
}
|