2016-09-14 18:01:13 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright(C) 2016 Magnus Norddahl
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with this program. If not, see http://www.gnu.org/licenses/
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
2016-07-26 19:27:02 +00:00
|
|
|
/*
|
|
|
|
** gl_renderbuffers.cpp
|
|
|
|
** Render buffers used during rendering
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gl/system/gl_system.h"
|
|
|
|
#include "m_swap.h"
|
|
|
|
#include "v_video.h"
|
|
|
|
#include "gl/gl_functions.h"
|
|
|
|
#include "vectors.h"
|
|
|
|
#include "gl/system/gl_interface.h"
|
|
|
|
#include "gl/system/gl_framebuffer.h"
|
|
|
|
#include "gl/system/gl_cvars.h"
|
2016-08-17 21:18:47 +00:00
|
|
|
#include "gl/system/gl_debug.h"
|
2016-07-26 19:27:02 +00:00
|
|
|
#include "gl/renderer/gl_renderer.h"
|
|
|
|
#include "gl/renderer/gl_renderbuffers.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "doomerrors.h"
|
2016-08-29 11:10:22 +00:00
|
|
|
#include <random>
|
2016-07-26 19:27:02 +00:00
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
CVAR(Int, gl_multisample, 1, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
|
2016-09-02 22:36:23 +00:00
|
|
|
CVAR(Bool, gl_renderbuffers, true, CVAR_ARCHIVE | CVAR_GLOBALCONFIG | CVAR_NOINITCALL)
|
2016-07-29 19:31:20 +00:00
|
|
|
|
2016-07-26 19:27:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Initialize render buffers and textures used in rendering passes
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FGLRenderBuffers::FGLRenderBuffers()
|
|
|
|
{
|
2016-08-27 04:24:36 +00:00
|
|
|
for (int i = 0; i < NumPipelineTextures; i++)
|
|
|
|
{
|
|
|
|
mPipelineTexture[i] = 0;
|
|
|
|
mPipelineFB[i] = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-06 05:36:49 +00:00
|
|
|
for (int i = 0; i < NumAmbientRandomTextures; i++)
|
|
|
|
{
|
|
|
|
AmbientRandomTexture[i] = 0;
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:27:02 +00:00
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&mOutputFB);
|
2016-08-17 15:59:47 +00:00
|
|
|
glGetIntegerv(GL_MAX_SAMPLES, &mMaxSamples);
|
2016-07-26 19:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Free render buffer resources
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
FGLRenderBuffers::~FGLRenderBuffers()
|
|
|
|
{
|
2016-07-30 13:33:30 +00:00
|
|
|
ClearScene();
|
2016-08-04 15:16:49 +00:00
|
|
|
ClearPipeline();
|
2016-09-07 18:31:42 +00:00
|
|
|
ClearEyeBuffers();
|
2016-07-30 13:33:30 +00:00
|
|
|
ClearBloom();
|
2016-09-18 13:57:22 +00:00
|
|
|
ClearExposureLevels();
|
2016-08-29 11:10:22 +00:00
|
|
|
ClearAmbientOcclusion();
|
2017-06-03 22:44:49 +00:00
|
|
|
ClearShadowMap();
|
2016-07-27 19:50:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
void FGLRenderBuffers::ClearScene()
|
|
|
|
{
|
|
|
|
DeleteFrameBuffer(mSceneFB);
|
2016-08-29 11:10:22 +00:00
|
|
|
DeleteFrameBuffer(mSceneDataFB);
|
2016-10-05 01:56:58 +00:00
|
|
|
if (mSceneUsesTextures)
|
|
|
|
{
|
|
|
|
DeleteTexture(mSceneMultisample);
|
2016-10-05 05:57:27 +00:00
|
|
|
DeleteTexture(mSceneFog);
|
|
|
|
DeleteTexture(mSceneNormal);
|
2016-10-05 01:56:58 +00:00
|
|
|
DeleteTexture(mSceneDepthStencil);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DeleteRenderBuffer(mSceneMultisample);
|
2016-10-05 05:57:27 +00:00
|
|
|
DeleteRenderBuffer(mSceneFog);
|
|
|
|
DeleteRenderBuffer(mSceneNormal);
|
2016-10-05 01:56:58 +00:00
|
|
|
DeleteRenderBuffer(mSceneDepthStencil);
|
|
|
|
}
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::ClearPipeline()
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
for (int i = 0; i < NumPipelineTextures; i++)
|
|
|
|
{
|
|
|
|
DeleteFrameBuffer(mPipelineFB[i]);
|
|
|
|
DeleteTexture(mPipelineTexture[i]);
|
|
|
|
}
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FGLRenderBuffers::ClearBloom()
|
2016-07-27 19:50:30 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < NumBloomLevels; i++)
|
|
|
|
{
|
|
|
|
auto &level = BloomLevels[i];
|
2016-07-29 19:31:20 +00:00
|
|
|
DeleteFrameBuffer(level.HFramebuffer);
|
|
|
|
DeleteFrameBuffer(level.VFramebuffer);
|
|
|
|
DeleteTexture(level.HTexture);
|
|
|
|
DeleteTexture(level.VTexture);
|
2016-07-27 19:50:30 +00:00
|
|
|
level = FGLBloomTextureLevel();
|
|
|
|
}
|
2016-07-26 19:27:02 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 13:57:22 +00:00
|
|
|
void FGLRenderBuffers::ClearExposureLevels()
|
|
|
|
{
|
|
|
|
for (auto &level : ExposureLevels)
|
|
|
|
{
|
|
|
|
DeleteTexture(level.Texture);
|
|
|
|
DeleteFrameBuffer(level.Framebuffer);
|
|
|
|
}
|
|
|
|
ExposureLevels.Clear();
|
|
|
|
DeleteTexture(ExposureTexture);
|
|
|
|
DeleteFrameBuffer(ExposureFB);
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:31:42 +00:00
|
|
|
void FGLRenderBuffers::ClearEyeBuffers()
|
|
|
|
{
|
|
|
|
for (auto handle : mEyeFBs)
|
|
|
|
DeleteFrameBuffer(handle);
|
|
|
|
|
2016-09-07 19:52:43 +00:00
|
|
|
for (auto handle : mEyeTextures)
|
|
|
|
DeleteTexture(handle);
|
|
|
|
|
2016-09-07 18:31:42 +00:00
|
|
|
mEyeTextures.Clear();
|
|
|
|
mEyeFBs.Clear();
|
|
|
|
}
|
|
|
|
|
2016-08-29 11:10:22 +00:00
|
|
|
void FGLRenderBuffers::ClearAmbientOcclusion()
|
|
|
|
{
|
2016-10-09 04:17:48 +00:00
|
|
|
DeleteFrameBuffer(LinearDepthFB);
|
2016-08-29 11:10:22 +00:00
|
|
|
DeleteFrameBuffer(AmbientFB0);
|
|
|
|
DeleteFrameBuffer(AmbientFB1);
|
2016-10-09 04:17:48 +00:00
|
|
|
DeleteTexture(LinearDepthTexture);
|
2016-08-29 11:10:22 +00:00
|
|
|
DeleteTexture(AmbientTexture0);
|
|
|
|
DeleteTexture(AmbientTexture1);
|
2016-10-06 05:36:49 +00:00
|
|
|
for (int i = 0; i < NumAmbientRandomTextures; i++)
|
|
|
|
DeleteTexture(AmbientRandomTexture[i]);
|
2016-08-29 11:10:22 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
void FGLRenderBuffers::DeleteTexture(GLuint &handle)
|
|
|
|
{
|
|
|
|
if (handle != 0)
|
|
|
|
glDeleteTextures(1, &handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGLRenderBuffers::DeleteRenderBuffer(GLuint &handle)
|
|
|
|
{
|
|
|
|
if (handle != 0)
|
|
|
|
glDeleteRenderbuffers(1, &handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGLRenderBuffers::DeleteFrameBuffer(GLuint &handle)
|
|
|
|
{
|
|
|
|
if (handle != 0)
|
|
|
|
glDeleteFramebuffers(1, &handle);
|
|
|
|
handle = 0;
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:27:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Makes sure all render buffers have sizes suitable for rending at the
|
|
|
|
// specified resolution
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-28 16:07:44 +00:00
|
|
|
bool FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHeight)
|
2016-07-26 19:27:02 +00:00
|
|
|
{
|
2016-09-02 22:13:05 +00:00
|
|
|
if (gl_renderbuffers != BuffersActive)
|
|
|
|
{
|
|
|
|
if (BuffersActive)
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mOutputFB);
|
|
|
|
BuffersActive = gl_renderbuffers;
|
2016-09-02 22:36:23 +00:00
|
|
|
GLRenderer->mShaderManager->ResetFixedColormap();
|
2016-09-02 22:13:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 21:42:37 +00:00
|
|
|
if (!IsEnabled())
|
2016-08-28 16:07:44 +00:00
|
|
|
return false;
|
2016-08-13 16:43:30 +00:00
|
|
|
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
I_FatalError("Requested invalid render buffer sizes: screen = %dx%d", width, height);
|
2016-08-06 21:42:37 +00:00
|
|
|
|
2016-08-17 15:59:47 +00:00
|
|
|
int samples = clamp((int)gl_multisample, 0, mMaxSamples);
|
2016-10-05 01:56:58 +00:00
|
|
|
bool needsSceneTextures = (gl_ssao != 0);
|
2016-07-30 13:33:30 +00:00
|
|
|
|
2016-08-13 20:24:21 +00:00
|
|
|
GLint activeTex;
|
|
|
|
GLint textureBinding;
|
|
|
|
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);
|
|
|
|
|
2016-10-05 01:56:58 +00:00
|
|
|
if (width != mWidth || height != mHeight)
|
2016-08-04 15:16:49 +00:00
|
|
|
CreatePipeline(width, height);
|
2016-10-05 01:56:58 +00:00
|
|
|
|
|
|
|
if (width != mWidth || height != mHeight || mSamples != samples || mSceneUsesTextures != needsSceneTextures)
|
|
|
|
CreateScene(width, height, samples, needsSceneTextures);
|
|
|
|
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
mSamples = samples;
|
|
|
|
mSceneUsesTextures = needsSceneTextures;
|
2016-07-30 13:33:30 +00:00
|
|
|
|
2016-08-12 15:44:59 +00:00
|
|
|
// Bloom bluring buffers need to match the scene to avoid bloom bleeding artifacts
|
2016-09-18 13:57:22 +00:00
|
|
|
if (mSceneWidth != sceneWidth || mSceneHeight != sceneHeight)
|
2016-08-12 15:44:59 +00:00
|
|
|
{
|
|
|
|
CreateBloom(sceneWidth, sceneHeight);
|
2016-09-18 13:57:22 +00:00
|
|
|
CreateExposureLevels(sceneWidth, sceneHeight);
|
2016-08-29 11:10:22 +00:00
|
|
|
CreateAmbientOcclusion(sceneWidth, sceneHeight);
|
2016-09-18 13:57:22 +00:00
|
|
|
mSceneWidth = sceneWidth;
|
|
|
|
mSceneHeight = sceneHeight;
|
2016-08-12 15:44:59 +00:00
|
|
|
}
|
|
|
|
|
2016-08-17 19:25:31 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, textureBinding);
|
2016-08-13 20:24:21 +00:00
|
|
|
glActiveTexture(activeTex);
|
2016-07-30 13:33:30 +00:00
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2016-08-28 16:07:44 +00:00
|
|
|
|
|
|
|
if (FailedCreate)
|
|
|
|
{
|
|
|
|
ClearScene();
|
|
|
|
ClearPipeline();
|
2016-09-07 18:31:42 +00:00
|
|
|
ClearEyeBuffers();
|
2016-08-28 16:07:44 +00:00
|
|
|
ClearBloom();
|
2016-09-18 13:57:22 +00:00
|
|
|
ClearExposureLevels();
|
2016-08-28 16:07:44 +00:00
|
|
|
mWidth = 0;
|
|
|
|
mHeight = 0;
|
|
|
|
mSamples = 0;
|
2016-09-18 13:57:22 +00:00
|
|
|
mSceneWidth = 0;
|
|
|
|
mSceneHeight = 0;
|
2016-08-28 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return !FailedCreate;
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates the scene buffers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-10-05 01:56:58 +00:00
|
|
|
void FGLRenderBuffers::CreateScene(int width, int height, int samples, bool needsSceneTextures)
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
|
|
|
ClearScene();
|
2016-07-26 19:27:02 +00:00
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
if (samples > 1)
|
2016-08-29 11:10:22 +00:00
|
|
|
{
|
2016-10-05 01:56:58 +00:00
|
|
|
if (needsSceneTextures)
|
|
|
|
{
|
|
|
|
mSceneMultisample = Create2DMultisampleTexture("SceneMultisample", GL_RGBA16F, width, height, samples, false);
|
|
|
|
mSceneDepthStencil = Create2DMultisampleTexture("SceneDepthStencil", GL_DEPTH24_STENCIL8, width, height, samples, false);
|
2016-10-05 05:57:27 +00:00
|
|
|
mSceneFog = Create2DMultisampleTexture("SceneFog", GL_RGBA8, width, height, samples, false);
|
|
|
|
mSceneNormal = Create2DMultisampleTexture("SceneNormal", GL_RGB10_A2, width, height, samples, false);
|
|
|
|
mSceneFB = CreateFrameBuffer("SceneFB", mSceneMultisample, 0, 0, mSceneDepthStencil, true);
|
|
|
|
mSceneDataFB = CreateFrameBuffer("SceneGBufferFB", mSceneMultisample, mSceneFog, mSceneNormal, mSceneDepthStencil, true);
|
2016-10-05 01:56:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSceneMultisample = CreateRenderBuffer("SceneMultisample", GL_RGBA16F, width, height, samples);
|
|
|
|
mSceneDepthStencil = CreateRenderBuffer("SceneDepthStencil", GL_DEPTH24_STENCIL8, width, height, samples);
|
|
|
|
mSceneFB = CreateFrameBuffer("SceneFB", mSceneMultisample, mSceneDepthStencil, true);
|
2016-10-05 05:57:27 +00:00
|
|
|
mSceneDataFB = CreateFrameBuffer("SceneGBufferFB", mSceneMultisample, mSceneDepthStencil, true);
|
2016-10-05 01:56:58 +00:00
|
|
|
}
|
2016-08-29 11:10:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-05 01:56:58 +00:00
|
|
|
if (needsSceneTextures)
|
|
|
|
{
|
|
|
|
mSceneDepthStencil = Create2DTexture("SceneDepthStencil", GL_DEPTH24_STENCIL8, width, height);
|
2016-10-05 05:57:27 +00:00
|
|
|
mSceneFog = Create2DTexture("SceneFog", GL_RGBA8, width, height);
|
|
|
|
mSceneNormal = Create2DTexture("SceneNormal", GL_RGB10_A2, width, height);
|
|
|
|
mSceneFB = CreateFrameBuffer("SceneFB", mPipelineTexture[0], 0, 0, mSceneDepthStencil, false);
|
|
|
|
mSceneDataFB = CreateFrameBuffer("SceneGBufferFB", mPipelineTexture[0], mSceneFog, mSceneNormal, mSceneDepthStencil, false);
|
2016-10-05 01:56:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSceneDepthStencil = CreateRenderBuffer("SceneDepthStencil", GL_DEPTH24_STENCIL8, width, height);
|
|
|
|
mSceneFB = CreateFrameBuffer("SceneFB", mPipelineTexture[0], mSceneDepthStencil, false);
|
2016-10-05 05:57:27 +00:00
|
|
|
mSceneDataFB = CreateFrameBuffer("SceneGBufferFB", mPipelineTexture[0], mSceneDepthStencil, false);
|
2016-10-05 01:56:58 +00:00
|
|
|
}
|
2016-08-29 11:10:22 +00:00
|
|
|
}
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-08-04 15:16:49 +00:00
|
|
|
// Creates the buffers needed for post processing steps
|
2016-07-30 13:33:30 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
2016-07-28 22:36:43 +00:00
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::CreatePipeline(int width, int height)
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
ClearPipeline();
|
2016-09-07 18:31:42 +00:00
|
|
|
ClearEyeBuffers();
|
2016-08-04 15:16:49 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < NumPipelineTextures; i++)
|
|
|
|
{
|
2016-09-02 22:13:05 +00:00
|
|
|
mPipelineTexture[i] = Create2DTexture("PipelineTexture", GL_RGBA16F, width, height);
|
2016-08-17 21:18:47 +00:00
|
|
|
mPipelineFB[i] = CreateFrameBuffer("PipelineFB", mPipelineTexture[i]);
|
2016-08-04 15:16:49 +00:00
|
|
|
}
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates bloom pass working buffers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::CreateBloom(int width, int height)
|
|
|
|
{
|
|
|
|
ClearBloom();
|
2016-08-13 16:43:30 +00:00
|
|
|
|
|
|
|
// No scene, no bloom!
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return;
|
2016-07-28 22:36:43 +00:00
|
|
|
|
2016-10-09 04:17:48 +00:00
|
|
|
int bloomWidth = (width + 1) / 2;
|
|
|
|
int bloomHeight = (height + 1) / 2;
|
2016-07-27 19:50:30 +00:00
|
|
|
for (int i = 0; i < NumBloomLevels; i++)
|
|
|
|
{
|
|
|
|
auto &level = BloomLevels[i];
|
2016-10-09 04:17:48 +00:00
|
|
|
level.Width = (bloomWidth + 1) / 2;
|
|
|
|
level.Height = (bloomHeight + 1) / 2;
|
2016-07-27 19:50:30 +00:00
|
|
|
|
2016-09-02 22:13:05 +00:00
|
|
|
level.VTexture = Create2DTexture("Bloom.VTexture", GL_RGBA16F, level.Width, level.Height);
|
|
|
|
level.HTexture = Create2DTexture("Bloom.HTexture", GL_RGBA16F, level.Width, level.Height);
|
2016-08-17 21:18:47 +00:00
|
|
|
level.VFramebuffer = CreateFrameBuffer("Bloom.VFramebuffer", level.VTexture);
|
|
|
|
level.HFramebuffer = CreateFrameBuffer("Bloom.HFramebuffer", level.HTexture);
|
2016-07-27 19:50:30 +00:00
|
|
|
|
|
|
|
bloomWidth = level.Width;
|
|
|
|
bloomHeight = level.Height;
|
|
|
|
}
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
2016-07-27 19:50:30 +00:00
|
|
|
|
2016-08-29 11:10:22 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates ambient occlusion working buffers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::CreateAmbientOcclusion(int width, int height)
|
|
|
|
{
|
|
|
|
ClearAmbientOcclusion();
|
|
|
|
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
return;
|
|
|
|
|
2016-10-09 04:17:48 +00:00
|
|
|
AmbientWidth = (width + 1) / 2;
|
|
|
|
AmbientHeight = (height + 1) / 2;
|
|
|
|
LinearDepthTexture = Create2DTexture("LinearDepthTexture", GL_R32F, AmbientWidth, AmbientHeight);
|
|
|
|
AmbientTexture0 = Create2DTexture("AmbientTexture0", GL_RG16F, AmbientWidth, AmbientHeight);
|
|
|
|
AmbientTexture1 = Create2DTexture("AmbientTexture1", GL_RG16F, AmbientWidth, AmbientHeight);
|
|
|
|
LinearDepthFB = CreateFrameBuffer("LinearDepthFB", LinearDepthTexture);
|
2016-08-29 11:10:22 +00:00
|
|
|
AmbientFB0 = CreateFrameBuffer("AmbientFB0", AmbientTexture0);
|
|
|
|
AmbientFB1 = CreateFrameBuffer("AmbientFB1", AmbientTexture1);
|
|
|
|
|
2016-10-06 05:36:49 +00:00
|
|
|
// Must match quality enum in FSSAOShader::GetDefines
|
|
|
|
double numDirections[NumAmbientRandomTextures] = { 2.0, 4.0, 8.0 };
|
|
|
|
|
2016-08-29 11:10:22 +00:00
|
|
|
std::mt19937 generator(1337);
|
2016-10-06 05:36:49 +00:00
|
|
|
std::uniform_real_distribution<double> distribution(0.0, 1.0);
|
|
|
|
for (int quality = 0; quality < NumAmbientRandomTextures; quality++)
|
2016-08-29 11:10:22 +00:00
|
|
|
{
|
2016-10-06 05:36:49 +00:00
|
|
|
int16_t randomValues[16 * 4];
|
2016-08-29 11:10:22 +00:00
|
|
|
|
2016-10-06 05:36:49 +00:00
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
{
|
|
|
|
double angle = 2.0 * M_PI * distribution(generator) / numDirections[quality];
|
|
|
|
double x = cos(angle);
|
|
|
|
double y = sin(angle);
|
|
|
|
double z = distribution(generator);
|
|
|
|
double w = distribution(generator);
|
|
|
|
|
|
|
|
randomValues[i * 4 + 0] = (int16_t)clamp(x * 32767.0, -32768.0, 32767.0);
|
|
|
|
randomValues[i * 4 + 1] = (int16_t)clamp(y * 32767.0, -32768.0, 32767.0);
|
|
|
|
randomValues[i * 4 + 2] = (int16_t)clamp(z * 32767.0, -32768.0, 32767.0);
|
|
|
|
randomValues[i * 4 + 3] = (int16_t)clamp(w * 32767.0, -32768.0, 32767.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
AmbientRandomTexture[quality] = Create2DTexture("AmbientRandomTexture", GL_RGBA16_SNORM, 4, 4, randomValues);
|
|
|
|
}
|
2016-08-29 11:10:22 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 13:57:22 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates camera exposure level buffers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::CreateExposureLevels(int width, int height)
|
|
|
|
{
|
|
|
|
ClearExposureLevels();
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
width = MAX(width / 2, 1);
|
|
|
|
height = MAX(height / 2, 1);
|
|
|
|
|
|
|
|
FString textureName, fbName;
|
|
|
|
textureName.Format("Exposure.Texture%d", i);
|
|
|
|
fbName.Format("Exposure.Framebuffer%d", i);
|
|
|
|
i++;
|
|
|
|
|
|
|
|
FGLExposureTextureLevel level;
|
|
|
|
level.Width = width;
|
|
|
|
level.Height = height;
|
|
|
|
level.Texture = Create2DTexture(textureName, GL_R32F, level.Width, level.Height);
|
|
|
|
level.Framebuffer = CreateFrameBuffer(fbName, level.Texture);
|
|
|
|
ExposureLevels.Push(level);
|
|
|
|
} while (width > 1 || height > 1);
|
|
|
|
|
|
|
|
ExposureTexture = Create2DTexture("Exposure.CameraTexture", GL_R32F, 1, 1);
|
|
|
|
ExposureFB = CreateFrameBuffer("Exposure.CameraFB", ExposureTexture);
|
|
|
|
|
|
|
|
FirstExposureFrame = true;
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:31:42 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates eye buffers if needed
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::CreateEyeBuffers(int eye)
|
|
|
|
{
|
2017-01-05 13:45:15 +00:00
|
|
|
if (mEyeFBs.Size() > unsigned(eye))
|
2016-09-07 18:31:42 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
GLint activeTex, textureBinding, frameBufferBinding;
|
|
|
|
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &frameBufferBinding);
|
|
|
|
|
2017-01-05 13:45:15 +00:00
|
|
|
while (mEyeFBs.Size() <= unsigned(eye))
|
2016-09-07 18:31:42 +00:00
|
|
|
{
|
|
|
|
GLuint texture = Create2DTexture("EyeTexture", GL_RGBA16F, mWidth, mHeight);
|
|
|
|
mEyeTextures.Push(texture);
|
|
|
|
mEyeFBs.Push(CreateFrameBuffer("EyeFB", texture));
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureBinding);
|
|
|
|
glActiveTexture(activeTex);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferBinding);
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates a 2D texture defaulting to linear filtering and clamp to edge
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-09-18 13:57:22 +00:00
|
|
|
GLuint FGLRenderBuffers::Create2DTexture(const FString &name, GLuint format, int width, int height, const void *data)
|
2016-07-29 19:31:20 +00:00
|
|
|
{
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenTextures(1, &handle);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, handle);
|
2016-08-17 21:18:47 +00:00
|
|
|
FGLDebug::LabelObject(GL_TEXTURE, handle, name);
|
2016-08-29 11:10:22 +00:00
|
|
|
|
2017-01-05 13:47:16 +00:00
|
|
|
GLenum dataformat = 0, datatype = 0;
|
2016-08-29 11:10:22 +00:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case GL_RGBA8: dataformat = GL_RGBA; datatype = GL_UNSIGNED_BYTE; break;
|
|
|
|
case GL_RGBA16: dataformat = GL_RGBA; datatype = GL_UNSIGNED_SHORT; break;
|
|
|
|
case GL_RGBA16F: dataformat = GL_RGBA; datatype = GL_FLOAT; break;
|
|
|
|
case GL_RGBA32F: dataformat = GL_RGBA; datatype = GL_FLOAT; break;
|
2016-10-09 04:17:48 +00:00
|
|
|
case GL_RGBA16_SNORM: dataformat = GL_RGBA; datatype = GL_SHORT; break;
|
2016-08-29 11:10:22 +00:00
|
|
|
case GL_R32F: dataformat = GL_RED; datatype = GL_FLOAT; break;
|
2016-10-09 04:17:48 +00:00
|
|
|
case GL_R16F: dataformat = GL_RED; datatype = GL_FLOAT; break;
|
2016-08-29 11:10:22 +00:00
|
|
|
case GL_RG32F: dataformat = GL_RG; datatype = GL_FLOAT; break;
|
2016-10-09 04:17:48 +00:00
|
|
|
case GL_RG16F: dataformat = GL_RG; datatype = GL_FLOAT; break;
|
2016-10-05 05:57:27 +00:00
|
|
|
case GL_RGB10_A2: dataformat = GL_RGBA; datatype = GL_UNSIGNED_INT_10_10_10_2; break;
|
2016-08-29 11:10:22 +00:00
|
|
|
case GL_DEPTH_COMPONENT24: dataformat = GL_DEPTH_COMPONENT; datatype = GL_FLOAT; break;
|
|
|
|
case GL_STENCIL_INDEX8: dataformat = GL_STENCIL_INDEX; datatype = GL_INT; break;
|
|
|
|
case GL_DEPTH24_STENCIL8: dataformat = GL_DEPTH_STENCIL; datatype = GL_UNSIGNED_INT_24_8; break;
|
|
|
|
default: I_FatalError("Unknown format passed to FGLRenderBuffers.Create2DTexture");
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, dataformat, datatype, data);
|
2016-07-29 19:31:20 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-08-29 11:10:22 +00:00
|
|
|
GLuint FGLRenderBuffers::Create2DMultisampleTexture(const FString &name, GLuint format, int width, int height, int samples, bool fixedSampleLocations)
|
|
|
|
{
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenTextures(1, &handle);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, handle);
|
|
|
|
FGLDebug::LabelObject(GL_TEXTURE, handle, name);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, format, width, height, fixedSampleLocations);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates a render buffer
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-17 21:18:47 +00:00
|
|
|
GLuint FGLRenderBuffers::CreateRenderBuffer(const FString &name, GLuint format, int width, int height)
|
2016-07-29 19:31:20 +00:00
|
|
|
{
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenRenderbuffers(1, &handle);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, handle);
|
2016-08-17 21:18:47 +00:00
|
|
|
FGLDebug::LabelObject(GL_RENDERBUFFER, handle, name);
|
2016-07-29 19:31:20 +00:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-10-09 04:17:48 +00:00
|
|
|
GLuint FGLRenderBuffers::CreateRenderBuffer(const FString &name, GLuint format, int width, int height, int samples)
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
|
|
|
if (samples <= 1)
|
2016-08-17 21:18:47 +00:00
|
|
|
return CreateRenderBuffer(name, format, width, height);
|
2016-07-30 13:33:30 +00:00
|
|
|
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenRenderbuffers(1, &handle);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, handle);
|
2016-08-17 21:18:47 +00:00
|
|
|
FGLDebug::LabelObject(GL_RENDERBUFFER, handle, name);
|
2016-07-30 13:33:30 +00:00
|
|
|
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, format, width, height);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-07-29 19:31:20 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Creates a frame buffer
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-17 21:18:47 +00:00
|
|
|
GLuint FGLRenderBuffers::CreateFrameBuffer(const FString &name, GLuint colorbuffer)
|
2016-07-29 19:31:20 +00:00
|
|
|
{
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenFramebuffers(1, &handle);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, handle);
|
2016-08-17 21:18:47 +00:00
|
|
|
FGLDebug::LabelObject(GL_FRAMEBUFFER, handle, name);
|
2016-07-29 19:31:20 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorbuffer, 0);
|
2016-08-28 16:07:44 +00:00
|
|
|
if (CheckFrameBufferCompleteness())
|
|
|
|
ClearFrameBuffer(false, false);
|
2016-07-29 19:31:20 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-10-05 01:56:58 +00:00
|
|
|
GLuint FGLRenderBuffers::CreateFrameBuffer(const FString &name, GLuint colorbuffer, GLuint depthstencil, bool colorIsARenderBuffer)
|
|
|
|
{
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenFramebuffers(1, &handle);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, handle);
|
|
|
|
FGLDebug::LabelObject(GL_FRAMEBUFFER, handle, name);
|
|
|
|
if (colorIsARenderBuffer)
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorbuffer);
|
|
|
|
else
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorbuffer, 0);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthstencil);
|
|
|
|
if (CheckFrameBufferCompleteness())
|
|
|
|
ClearFrameBuffer(true, true);
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:57:27 +00:00
|
|
|
GLuint FGLRenderBuffers::CreateFrameBuffer(const FString &name, GLuint colorbuffer0, GLuint colorbuffer1, GLuint colorbuffer2, GLuint depthstencil, bool multisample)
|
2016-07-29 19:31:20 +00:00
|
|
|
{
|
|
|
|
GLuint handle = 0;
|
|
|
|
glGenFramebuffers(1, &handle);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, handle);
|
2016-08-17 21:18:47 +00:00
|
|
|
FGLDebug::LabelObject(GL_FRAMEBUFFER, handle, name);
|
2016-08-29 11:10:22 +00:00
|
|
|
if (multisample)
|
|
|
|
{
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, colorbuffer0, 0);
|
|
|
|
if (colorbuffer1 != 0)
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D_MULTISAMPLE, colorbuffer1, 0);
|
2016-10-05 05:57:27 +00:00
|
|
|
if (colorbuffer2 != 0)
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D_MULTISAMPLE, colorbuffer2, 0);
|
2016-08-29 11:10:22 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D_MULTISAMPLE, depthstencil, 0);
|
|
|
|
}
|
2016-07-31 01:16:48 +00:00
|
|
|
else
|
2016-08-29 11:10:22 +00:00
|
|
|
{
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorbuffer0, 0);
|
|
|
|
if (colorbuffer1 != 0)
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, colorbuffer1, 0);
|
2016-10-05 05:57:27 +00:00
|
|
|
if (colorbuffer2 != 0)
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, colorbuffer2, 0);
|
2016-08-29 11:10:22 +00:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthstencil, 0);
|
|
|
|
}
|
2016-08-28 16:07:44 +00:00
|
|
|
if (CheckFrameBufferCompleteness())
|
|
|
|
ClearFrameBuffer(true, true);
|
2016-07-29 19:31:20 +00:00
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2016-07-26 19:27:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-07-30 13:33:30 +00:00
|
|
|
// Verifies that the frame buffer setup is valid
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-28 16:07:44 +00:00
|
|
|
bool FGLRenderBuffers::CheckFrameBufferCompleteness()
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
|
|
|
GLenum result = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
if (result == GL_FRAMEBUFFER_COMPLETE)
|
2016-08-28 16:07:44 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
FailedCreate = true;
|
2016-07-30 13:33:30 +00:00
|
|
|
|
2016-08-29 11:10:22 +00:00
|
|
|
if (gl_debug_level > 0)
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
2016-08-29 11:10:22 +00:00
|
|
|
FString error = "glCheckFramebufferStatus failed: ";
|
|
|
|
switch (result)
|
|
|
|
{
|
|
|
|
default: error.AppendFormat("error code %d", (int)result); break;
|
|
|
|
case GL_FRAMEBUFFER_UNDEFINED: error << "GL_FRAMEBUFFER_UNDEFINED"; break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: error << "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"; break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: error << "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"; break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: error << "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"; break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER: error << "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER"; break;
|
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED: error << "GL_FRAMEBUFFER_UNSUPPORTED"; break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: error << "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE"; break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: error << "GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS"; break;
|
|
|
|
}
|
|
|
|
Printf("%s\n", error.GetChars());
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
2016-08-28 16:07:44 +00:00
|
|
|
|
|
|
|
return false;
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 16:25:25 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Clear frame buffer to make sure it never contains uninitialized data
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-17 22:21:33 +00:00
|
|
|
void FGLRenderBuffers::ClearFrameBuffer(bool stencil, bool depth)
|
2016-08-13 16:25:25 +00:00
|
|
|
{
|
2016-08-13 20:24:21 +00:00
|
|
|
GLboolean scissorEnabled;
|
|
|
|
GLint stencilValue;
|
2016-08-13 16:25:25 +00:00
|
|
|
GLdouble depthValue;
|
2016-08-13 20:24:21 +00:00
|
|
|
glGetBooleanv(GL_SCISSOR_TEST, &scissorEnabled);
|
2016-08-13 16:25:25 +00:00
|
|
|
glGetIntegerv(GL_STENCIL_CLEAR_VALUE, &stencilValue);
|
|
|
|
glGetDoublev(GL_DEPTH_CLEAR_VALUE, &depthValue);
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
glClearDepth(0.0);
|
|
|
|
glClearStencil(0);
|
2016-08-17 22:21:33 +00:00
|
|
|
GLenum flags = GL_COLOR_BUFFER_BIT;
|
|
|
|
if (stencil)
|
|
|
|
flags |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
if (depth)
|
|
|
|
flags |= GL_DEPTH_BUFFER_BIT;
|
|
|
|
glClear(flags);
|
2016-08-13 16:25:25 +00:00
|
|
|
glClearStencil(stencilValue);
|
|
|
|
glClearDepth(depthValue);
|
|
|
|
if (scissorEnabled)
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Resolves the multisample frame buffer by copying it to the scene texture
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BlitSceneToTexture()
|
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
mCurrentPipelineTexture = 0;
|
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
if (mSamples <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, mSceneFB);
|
2016-08-04 15:16:49 +00:00
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mPipelineFB[mCurrentPipelineTexture]);
|
2016-07-30 13:33:30 +00:00
|
|
|
glBlitFramebuffer(0, 0, mWidth, mHeight, 0, 0, mWidth, mHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
2016-08-17 15:37:49 +00:00
|
|
|
|
2016-08-18 23:11:11 +00:00
|
|
|
if ((gl.flags & RFL_INVALIDATE_BUFFER) != 0)
|
2016-08-17 15:37:49 +00:00
|
|
|
{
|
|
|
|
GLenum attachments[2] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_STENCIL_ATTACHMENT };
|
|
|
|
glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, 2, attachments);
|
|
|
|
}
|
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
}
|
|
|
|
|
2016-09-07 18:31:42 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Eye textures and their frame buffers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BlitToEyeTexture(int eye)
|
|
|
|
{
|
|
|
|
CreateEyeBuffers(eye);
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, mPipelineFB[mCurrentPipelineTexture]);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mEyeFBs[eye]);
|
|
|
|
glBlitFramebuffer(0, 0, mWidth, mHeight, 0, 0, mWidth, mHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
|
|
|
|
|
|
|
|
if ((gl.flags & RFL_INVALIDATE_BUFFER) != 0)
|
|
|
|
{
|
|
|
|
GLenum attachments[2] = { GL_COLOR_ATTACHMENT0, GL_DEPTH_STENCIL_ATTACHMENT };
|
|
|
|
glInvalidateFramebuffer(GL_READ_FRAMEBUFFER, 2, attachments);
|
|
|
|
}
|
|
|
|
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BindEyeTexture(int eye, int texunit)
|
|
|
|
{
|
|
|
|
CreateEyeBuffers(eye);
|
|
|
|
glActiveTexture(GL_TEXTURE0 + texunit);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mEyeTextures[eye]);
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:52:43 +00:00
|
|
|
void FGLRenderBuffers::BindEyeFB(int eye, bool readBuffer)
|
2016-09-07 18:31:42 +00:00
|
|
|
{
|
|
|
|
CreateEyeBuffers(eye);
|
2016-09-07 19:52:43 +00:00
|
|
|
glBindFramebuffer(readBuffer ? GL_READ_FRAMEBUFFER : GL_FRAMEBUFFER, mEyeFBs[eye]);
|
2016-09-07 18:31:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 16:17:33 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Shadow map texture and frame buffers
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BindShadowMapFB()
|
|
|
|
{
|
|
|
|
CreateShadowMap();
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mShadowMapFB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BindShadowMapTexture(int texunit)
|
|
|
|
{
|
|
|
|
CreateShadowMap();
|
|
|
|
glActiveTexture(GL_TEXTURE0 + texunit);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mShadowMapTexture);
|
|
|
|
}
|
|
|
|
|
2017-06-03 22:44:49 +00:00
|
|
|
void FGLRenderBuffers::ClearShadowMap()
|
|
|
|
{
|
|
|
|
DeleteFrameBuffer(mShadowMapFB);
|
|
|
|
DeleteTexture(mShadowMapTexture);
|
|
|
|
mCurrentShadowMapSize = 0;
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:17:33 +00:00
|
|
|
void FGLRenderBuffers::CreateShadowMap()
|
|
|
|
{
|
2017-06-03 22:44:49 +00:00
|
|
|
if (mShadowMapTexture != 0 && gl_shadowmap_quality == mCurrentShadowMapSize)
|
2017-03-01 16:17:33 +00:00
|
|
|
return;
|
|
|
|
|
2017-06-03 22:44:49 +00:00
|
|
|
ClearShadowMap();
|
|
|
|
|
2017-03-01 16:17:33 +00:00
|
|
|
GLint activeTex, textureBinding, frameBufferBinding;
|
|
|
|
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &frameBufferBinding);
|
|
|
|
|
2017-06-03 22:44:49 +00:00
|
|
|
mShadowMapTexture = Create2DTexture("ShadowMap", GL_R32F, gl_shadowmap_quality, 1024);
|
2017-03-07 14:58:22 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
2017-03-01 16:17:33 +00:00
|
|
|
mShadowMapFB = CreateFrameBuffer("ShadowMapFB", mShadowMapTexture);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, textureBinding);
|
|
|
|
glActiveTexture(activeTex);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, frameBufferBinding);
|
2017-06-03 22:44:49 +00:00
|
|
|
|
|
|
|
mCurrentShadowMapSize = gl_shadowmap_quality;
|
2017-03-01 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Makes the scene frame buffer active (multisample, depth, stecil, etc.)
|
2016-07-26 19:27:02 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-29 11:10:22 +00:00
|
|
|
void FGLRenderBuffers::BindSceneFB(bool sceneData)
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, sceneData ? mSceneDataFB : mSceneFB);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Binds the scene color texture to the specified texture unit
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BindSceneColorTexture(int index)
|
2016-07-26 19:27:02 +00:00
|
|
|
{
|
2016-08-29 11:10:22 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + index);
|
|
|
|
if (mSamples > 1)
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mSceneMultisample);
|
|
|
|
else
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mPipelineTexture[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-10-05 05:57:27 +00:00
|
|
|
// Binds the scene fog data texture to the specified texture unit
|
2016-08-29 11:10:22 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-10-05 05:57:27 +00:00
|
|
|
void FGLRenderBuffers::BindSceneFogTexture(int index)
|
2016-08-29 11:10:22 +00:00
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + index);
|
|
|
|
if (mSamples > 1)
|
2016-10-05 05:57:27 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mSceneFog);
|
2016-08-29 11:10:22 +00:00
|
|
|
else
|
2016-10-05 05:57:27 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, mSceneFog);
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Binds the scene normal data texture to the specified texture unit
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BindSceneNormalTexture(int index)
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + index);
|
|
|
|
if (mSamples > 1)
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mSceneNormal);
|
|
|
|
else
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mSceneNormal);
|
2016-08-29 11:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Binds the depth texture to the specified texture unit
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
void FGLRenderBuffers::BindSceneDepthTexture(int index)
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + index);
|
|
|
|
if (mSamples > 1)
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, mSceneDepthStencil);
|
|
|
|
else
|
|
|
|
glBindTexture(GL_TEXTURE_2D, mSceneDepthStencil);
|
2016-07-26 19:27:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 13:33:30 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-08-04 15:16:49 +00:00
|
|
|
// Binds the current scene/effect/hud texture to the specified texture unit
|
2016-07-30 13:33:30 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::BindCurrentTexture(int index)
|
2016-07-30 13:33:30 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0 + index);
|
2016-08-27 04:24:36 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, mPipelineTexture[mCurrentPipelineTexture]);
|
2016-07-30 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 22:36:43 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-08-04 15:16:49 +00:00
|
|
|
// Makes the frame buffer for the current texture active
|
2016-07-28 22:36:43 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::BindCurrentFB()
|
2016-07-28 22:36:43 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mPipelineFB[mCurrentPipelineTexture]);
|
2016-07-28 22:36:43 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 19:27:02 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-08-04 15:16:49 +00:00
|
|
|
// Makes the frame buffer for the next texture active
|
2016-07-26 19:27:02 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::BindNextFB()
|
2016-07-26 19:27:02 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
int out = (mCurrentPipelineTexture + 1) % NumPipelineTextures;
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mPipelineFB[out]);
|
2016-07-26 19:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-08-04 15:16:49 +00:00
|
|
|
// Next pipeline texture now contains the output
|
2016-07-26 19:27:02 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::NextTexture()
|
2016-07-26 19:27:02 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
mCurrentPipelineTexture = (mCurrentPipelineTexture + 1) % NumPipelineTextures;
|
2016-07-26 19:27:02 +00:00
|
|
|
}
|
2016-07-28 22:36:43 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2016-08-04 15:16:49 +00:00
|
|
|
// Makes the screen frame buffer active
|
2016-07-28 22:36:43 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2016-08-04 15:16:49 +00:00
|
|
|
void FGLRenderBuffers::BindOutputFB()
|
2016-07-28 22:36:43 +00:00
|
|
|
{
|
2016-08-04 15:16:49 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, mOutputFB);
|
2016-07-29 19:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Returns true if render buffers are supported and should be used
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
bool FGLRenderBuffers::IsEnabled()
|
|
|
|
{
|
2016-09-02 22:13:05 +00:00
|
|
|
return BuffersActive && !gl.legacyMode && !FailedCreate;
|
2016-07-28 22:36:43 +00:00
|
|
|
}
|
2016-08-28 16:07:44 +00:00
|
|
|
|
|
|
|
bool FGLRenderBuffers::FailedCreate = false;
|
2016-09-02 22:13:05 +00:00
|
|
|
bool FGLRenderBuffers::BuffersActive = false;
|