Added bloom and tonemap to menus

Added gl_renderbuffers CVAR that disables render buffers
Added patch shader support to FShaderProgram
Added OpenGL 2 fallback support to render buffers
This commit is contained in:
Magnus Norddahl 2016-07-29 21:31:20 +02:00
parent 0efee85bd8
commit 5849c83028
27 changed files with 401 additions and 193 deletions

View file

@ -53,6 +53,8 @@
#include "i_system.h"
#include "doomerrors.h"
CVAR(Bool, gl_renderbuffers, true, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
//==========================================================================
//
// Initialize render buffers and textures used in rendering passes
@ -80,37 +82,45 @@ void FGLRenderBuffers::Clear()
for (int i = 0; i < NumBloomLevels; i++)
{
auto &level = BloomLevels[i];
if (level.HFramebuffer != 0)
glDeleteFramebuffers(1, &level.HFramebuffer);
if (level.VFramebuffer != 0)
glDeleteFramebuffers(1, &level.VFramebuffer);
if (level.HTexture != 0)
glDeleteTextures(1, &level.HTexture);
if (level.VTexture != 0)
glDeleteTextures(1, &level.VTexture);
DeleteFrameBuffer(level.HFramebuffer);
DeleteFrameBuffer(level.VFramebuffer);
DeleteTexture(level.HTexture);
DeleteTexture(level.VTexture);
level = FGLBloomTextureLevel();
}
if (mSceneFB != 0)
glDeleteFramebuffers(1, &mSceneFB);
if (mSceneTexture != 0)
glDeleteTextures(1, &mSceneTexture);
if (mSceneDepthStencil != 0)
glDeleteRenderbuffers(1, &mSceneDepthStencil);
if (mHudFB != 0)
glDeleteFramebuffers(1, &mHudFB);
if (mHudTexture != 0)
glDeleteTextures(1, &mHudTexture);
mSceneFB = 0;
mSceneTexture = 0;
mSceneDepthStencil = 0;
mHudFB = 0;
mHudTexture = 0;
DeleteFrameBuffer(mSceneFB);
DeleteTexture(mSceneTexture);
DeleteRenderBuffer(mSceneDepthStencil);
DeleteRenderBuffer(mSceneDepth);
DeleteRenderBuffer(mSceneStencil);
DeleteFrameBuffer(mHudFB);
DeleteTexture(mHudTexture);
mWidth = 0;
mHeight = 0;
}
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;
}
//==========================================================================
//
// Makes sure all render buffers have sizes suitable for rending at the
@ -125,37 +135,23 @@ void FGLRenderBuffers::Setup(int width, int height)
Clear();
glActiveTexture(GL_TEXTURE0);
GLuint hdrFormat = ((gl.flags & RFL_NO_RGBA16F) != 0) ? GL_RGBA8 : GL_RGBA16F;
glGenFramebuffers(1, &mSceneFB);
glGenFramebuffers(1, &mHudFB);
glGenTextures(1, &mSceneTexture);
glGenTextures(1, &mHudTexture);
glGenRenderbuffers(1, &mSceneDepthStencil);
mSceneTexture = Create2DTexture(hdrFormat, width, height);
if ((gl.flags & RFL_NO_DEPTHSTENCIL) != 0)
{
mSceneDepth = CreateRenderBuffer(GL_DEPTH_COMPONENT24, width, height);
mSceneStencil = CreateRenderBuffer(GL_STENCIL_INDEX8, width, height);
mSceneFB = CreateFrameBuffer(mSceneTexture, mSceneDepth, mSceneStencil);
}
else
{
mSceneDepthStencil = CreateRenderBuffer(GL_DEPTH24_STENCIL8, width, height);
mSceneFB = CreateFrameBuffer(mSceneTexture, mSceneDepthStencil);
}
glBindTexture(GL_TEXTURE_2D, mSceneTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr);
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);
glBindRenderbuffer(GL_RENDERBUFFER, mSceneDepthStencil);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glBindFramebuffer(GL_FRAMEBUFFER, mSceneFB);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mSceneTexture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mSceneDepthStencil);
glBindTexture(GL_TEXTURE_2D, mHudTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, nullptr);
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);
glBindFramebuffer(GL_FRAMEBUFFER, mHudFB);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mHudTexture, 0);
mHudTexture = Create2DTexture(hdrFormat, width, height);
mHudFB = CreateFrameBuffer(mHudTexture);
int bloomWidth = MAX(width / 2, 1);
int bloomHeight = MAX(height / 2, 1);
@ -165,31 +161,16 @@ void FGLRenderBuffers::Setup(int width, int height)
level.Width = MAX(bloomWidth / 2, 1);
level.Height = MAX(bloomHeight / 2, 1);
glGenTextures(1, &level.VTexture);
glGenTextures(1, &level.HTexture);
glGenFramebuffers(1, &level.VFramebuffer);
glGenFramebuffers(1, &level.HFramebuffer);
glBindTexture(GL_TEXTURE_2D, level.VTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, level.Width, level.Height, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, level.HTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, level.Width, level.Height, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, level.VFramebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, level.VTexture, 0);
glBindFramebuffer(GL_FRAMEBUFFER, level.HFramebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, level.HTexture, 0);
level.VTexture = Create2DTexture(hdrFormat, level.Width, level.Height);
level.HTexture = Create2DTexture(hdrFormat, level.Width, level.Height);
level.VFramebuffer = CreateFrameBuffer(level.VTexture);
level.HFramebuffer = CreateFrameBuffer(level.HTexture);
bloomWidth = level.Width;
bloomHeight = level.Height;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, mOutputFB);
@ -198,6 +179,78 @@ void FGLRenderBuffers::Setup(int width, int height)
mHeight = height;
}
//==========================================================================
//
// Creates a 2D texture defaulting to linear filtering and clamp to edge
//
//==========================================================================
GLuint FGLRenderBuffers::Create2DTexture(GLuint format, int width, int height)
{
GLuint type = (format == GL_RGBA16F) ? GL_FLOAT : GL_UNSIGNED_BYTE;
GLuint handle = 0;
glGenTextures(1, &handle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, handle);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, GL_RGBA, type, nullptr);
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;
}
//==========================================================================
//
// Creates a render buffer
//
//==========================================================================
GLuint FGLRenderBuffers::CreateRenderBuffer(GLuint format, int width, int height)
{
GLuint handle = 0;
glGenRenderbuffers(1, &handle);
glBindRenderbuffer(GL_RENDERBUFFER, handle);
glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
return handle;
}
//==========================================================================
//
// Creates a frame buffer
//
//==========================================================================
GLuint FGLRenderBuffers::CreateFrameBuffer(GLuint colorbuffer)
{
GLuint handle = 0;
glGenFramebuffers(1, &handle);
glBindFramebuffer(GL_FRAMEBUFFER, handle);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorbuffer, 0);
return handle;
}
GLuint FGLRenderBuffers::CreateFrameBuffer(GLuint colorbuffer, GLuint depthstencil)
{
GLuint handle = 0;
glGenFramebuffers(1, &handle);
glBindFramebuffer(GL_FRAMEBUFFER, handle);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorbuffer, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthstencil);
return handle;
}
GLuint FGLRenderBuffers::CreateFrameBuffer(GLuint colorbuffer, GLuint depth, GLuint stencil)
{
GLuint handle = 0;
glGenFramebuffers(1, &handle);
glBindFramebuffer(GL_FRAMEBUFFER, handle);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorbuffer, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencil);
return handle;
}
//==========================================================================
//
// Makes the scene frame buffer active
@ -217,7 +270,10 @@ void FGLRenderBuffers::BindSceneFB()
void FGLRenderBuffers::BindHudFB()
{
glBindFramebuffer(GL_FRAMEBUFFER, mHudFB);
if (gl_tonemap != 0)
glBindFramebuffer(GL_FRAMEBUFFER, mHudFB);
else
glBindFramebuffer(GL_FRAMEBUFFER, mSceneFB);
}
//==========================================================================
@ -252,5 +308,19 @@ void FGLRenderBuffers::BindSceneTexture(int index)
void FGLRenderBuffers::BindHudTexture(int index)
{
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_2D, mHudTexture);
if (gl_tonemap != 0)
glBindTexture(GL_TEXTURE_2D, mHudTexture);
else
glBindTexture(GL_TEXTURE_2D, mSceneTexture);
}
//==========================================================================
//
// Returns true if render buffers are supported and should be used
//
//==========================================================================
bool FGLRenderBuffers::IsEnabled()
{
return gl_renderbuffers && gl.glslversion != 0;
}

View file

@ -27,19 +27,29 @@ public:
void BindSceneTexture(int index);
void BindHudTexture(int index);
static bool IsSupported() { return gl.version >= 3.3f; }
enum { NumBloomLevels = 4 };
FGLBloomTextureLevel BloomLevels[NumBloomLevels];
static bool IsEnabled();
private:
void Clear();
GLuint Create2DTexture(GLuint format, int width, int height);
GLuint CreateRenderBuffer(GLuint format, int width, int height);
GLuint CreateFrameBuffer(GLuint colorbuffer);
GLuint CreateFrameBuffer(GLuint colorbuffer, GLuint depthstencil);
GLuint CreateFrameBuffer(GLuint colorbuffer, GLuint depth, GLuint stencil);
void DeleteTexture(GLuint &handle);
void DeleteRenderBuffer(GLuint &handle);
void DeleteFrameBuffer(GLuint &handle);
int mWidth = 0;
int mHeight = 0;
GLuint mSceneTexture = 0;
GLuint mSceneDepthStencil = 0;
GLuint mSceneDepth = 0;
GLuint mSceneStencil = 0;
GLuint mSceneFB = 0;
GLuint mHudTexture = 0;
GLuint mHudFB = 0;

View file

@ -235,7 +235,7 @@ void FGLRenderer::SetupLevel()
void FGLRenderer::Begin2D()
{
if (FGLRenderBuffers::IsSupported())
if (FGLRenderBuffers::IsEnabled())
{
mBuffers->Setup(framebuffer->GetWidth(), framebuffer->GetHeight());
if (mDrawingScene2D)

View file

@ -117,7 +117,7 @@ public:
angle_t FrustumAngle();
void SetViewArea();
void SetOutputViewport(GL_IRECT *bounds);
void Set3DViewport();
void Set3DViewport(bool toscreen);
void Reset3DViewport();
sector_t *RenderViewpoint (AActor * camera, GL_IRECT * bounds, float fov, float ratio, float fovratio, bool mainview, bool toscreen);
void RenderView(player_t *player);

View file

@ -96,6 +96,22 @@ CVAR(Float, gl_mask_threshold, 0.5f,CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR(Float, gl_mask_sprite_threshold, 0.5f,CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR(Bool, gl_sort_textures, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
CVAR(Bool, gl_bloom, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
CVAR(Float, gl_bloom_amount, 1.4f, 0)
CVAR(Float, gl_exposure, 0.0f, 0)
CUSTOM_CVAR(Int, gl_tonemap, 0, CVAR_ARCHIVE|CVAR_GLOBALCONFIG)
{
if (self < 0 || self > 4)
self = 0;
}
CUSTOM_CVAR(Int, gl_bloom_kernel_size, 7, 0)
{
if (self < 3 || self > 15 || self % 2 == 0)
self = 7;
}
EXTERN_CVAR (Bool, cl_capfps)
EXTERN_CVAR (Bool, r_deathcamera)
EXTERN_CVAR(Float, vid_brightness)
@ -163,7 +179,7 @@ void FGLRenderer::SetViewArea()
void FGLRenderer::Reset3DViewport()
{
if (FGLRenderBuffers::IsSupported())
if (FGLRenderBuffers::IsEnabled())
glViewport(0, 0, mOutputViewport.width, mOutputViewport.height);
else
glViewport(mOutputViewport.left, mOutputViewport.top, mOutputViewport.width, mOutputViewport.height);
@ -175,10 +191,10 @@ void FGLRenderer::Reset3DViewport()
//
//-----------------------------------------------------------------------------
void FGLRenderer::Set3DViewport()
void FGLRenderer::Set3DViewport(bool toscreen)
{
const auto &bounds = mOutputViewportLB;
if (FGLRenderBuffers::IsSupported())
if (toscreen && FGLRenderBuffers::IsEnabled())
{
mBuffers->Setup(mOutputViewport.width, mOutputViewport.height);
mBuffers->BindSceneFB();
@ -215,12 +231,12 @@ void FGLRenderer::Set3DViewport()
void FGLRenderer::BloomScene()
{
if (!FGLRenderBuffers::IsSupported())
// Only bloom things if enabled and no special fixed light mode is active
if (!gl_bloom || !FGLRenderBuffers::IsEnabled() || gl_fixedcolormap != CM_DEFAULT)
return;
const float blurAmount = 4.0f;
int sampleCount = 5; // Note: must be uneven number 3 to 15
float exposure = mCameraExposure;
const float blurAmount = gl_bloom_amount;
int sampleCount = gl_bloom_kernel_size;
// TBD: Maybe need a better way to share state with other parts of the pipeline
GLboolean blendEnabled, scissorEnabled;
@ -248,7 +264,7 @@ void FGLRenderer::BloomScene()
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
mBloomExtractShader->Bind();
mBloomExtractShader->SceneTexture.Set(0);
mBloomExtractShader->Exposure.Set(exposure);
mBloomExtractShader->Exposure.Set(mCameraExposure);
{
FFlatVertex *ptr = mVBO->GetBuffer();
ptr->Set(-1.0f, -1.0f, 0, 0.0f, 0.0f); ptr++;
@ -327,6 +343,7 @@ void FGLRenderer::BloomScene()
glEnable(GL_SCISSOR_TEST);
glBlendEquationSeparate(blendEquationRgb, blendEquationAlpha);
glBlendFuncSeparate(blendSrcRgb, blendDestRgb, blendSrcAlpha, blendDestAlpha);
glUseProgram(currentProgram);
}
//-----------------------------------------------------------------------------
@ -337,6 +354,9 @@ void FGLRenderer::BloomScene()
void FGLRenderer::TonemapScene()
{
if (gl_tonemap == 0)
return;
GLboolean blendEnabled, scissorEnabled;
glGetBooleanv(GL_BLEND, &blendEnabled);
glGetBooleanv(GL_SCISSOR_TEST, &scissorEnabled);
@ -371,7 +391,7 @@ void FGLRenderer::TonemapScene()
void FGLRenderer::Flush()
{
if (FGLRenderBuffers::IsSupported())
if (FGLRenderBuffers::IsEnabled())
{
glDisable(GL_MULTISAMPLE);
glDisable(GL_DEPTH_TEST);
@ -969,9 +989,6 @@ void FGLRenderer::EndDrawScene(sector_t * viewsector)
gl_RenderState.ResetColor();
gl_RenderState.EnableTexture(true);
glDisable(GL_SCISSOR_TEST);
BloomScene();
TonemapScene();
}
@ -1078,9 +1095,16 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo
if (toscreen)
{
float light = viewsector->lightlevel / 255.0f;
float exposure = MAX(1.0f + (1.0f - light * light) * 1.5f, 0.5f);
mCameraExposure = mCameraExposure * 0.98f + exposure * 0.02f;
if (gl_exposure == 0.0f)
{
float light = viewsector->lightlevel / 255.0f;
float exposure = MAX(1.0f + (1.0f - light * light) * 0.9f, 0.5f);
mCameraExposure = mCameraExposure * 0.995f + exposure * 0.005f;
}
else
{
mCameraExposure = gl_exposure;
}
}
// 'viewsector' will not survive the rendering so it cannot be used anymore below.
@ -1096,7 +1120,7 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo
eye->SetUp();
// TODO: stereo specific viewport - needed when implementing side-by-side modes etc.
SetOutputViewport(bounds);
Set3DViewport();
Set3DViewport(toscreen);
mDrawingScene2D = true;
mCurrentFoV = fov;
// Stereo mode specific perspective projection
@ -1115,6 +1139,11 @@ sector_t * FGLRenderer::RenderViewpoint (AActor * camera, GL_IRECT * bounds, flo
ProcessScene(toscreen);
if (mainview) EndDrawScene(retval); // do not call this for camera textures.
if (toscreen)
{
BloomScene();
TonemapScene();
}
mDrawingScene2D = false;
eye->TearDown();
}

View file

@ -53,8 +53,8 @@ void FBloomExtractShader::Bind()
{
if (!mShader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/bloomextract.vp");
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/bloomextract.fp");
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/bloomextract.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/bloomextract.fp", "", 330);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/bloomextract");
mShader.SetAttribLocation(0, "PositionInProjection");
@ -68,8 +68,8 @@ void FBloomCombineShader::Bind()
{
if (!mShader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/bloomcombine.vp");
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/bloomcombine.fp");
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/bloomcombine.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/bloomcombine.fp", "", 330);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/bloomcombine");
mShader.SetAttribLocation(0, "PositionInProjection");

View file

@ -129,8 +129,8 @@ FBlurShader::BlurSetup *FBlurShader::GetSetup(float blurAmount, int sampleCount)
FString verticalCode = FragmentShaderCode(blurAmount, sampleCount, true);
blurSetup.VerticalShader = std::make_shared<FShaderProgram>();
blurSetup.VerticalShader->Compile(FShaderProgram::Vertex, "vertical blur vertex shader", vertexCode);
blurSetup.VerticalShader->Compile(FShaderProgram::Fragment, "vertical blur fragment shader", verticalCode);
blurSetup.VerticalShader->Compile(FShaderProgram::Vertex, "vertical blur vertex shader", vertexCode, "", 330);
blurSetup.VerticalShader->Compile(FShaderProgram::Fragment, "vertical blur fragment shader", verticalCode, "", 330);
blurSetup.VerticalShader->SetFragDataLocation(0, "FragColor");
blurSetup.VerticalShader->SetAttribLocation(0, "PositionInProjection");
blurSetup.VerticalShader->Link("vertical blur");
@ -138,8 +138,8 @@ FBlurShader::BlurSetup *FBlurShader::GetSetup(float blurAmount, int sampleCount)
glUniform1i(glGetUniformLocation(*blurSetup.VerticalShader.get(), "SourceTexture"), 0);
blurSetup.HorizontalShader = std::make_shared<FShaderProgram>();
blurSetup.HorizontalShader->Compile(FShaderProgram::Vertex, "horizontal blur vertex shader", vertexCode);
blurSetup.HorizontalShader->Compile(FShaderProgram::Fragment, "horizontal blur fragment shader", horizontalCode);
blurSetup.HorizontalShader->Compile(FShaderProgram::Vertex, "horizontal blur vertex shader", vertexCode, "", 330);
blurSetup.HorizontalShader->Compile(FShaderProgram::Fragment, "horizontal blur fragment shader", horizontalCode, "", 330);
blurSetup.HorizontalShader->SetFragDataLocation(0, "FragColor");
blurSetup.HorizontalShader->SetAttribLocation(0, "PositionInProjection");
blurSetup.HorizontalShader->Link("horizontal blur");
@ -160,8 +160,6 @@ FBlurShader::BlurSetup *FBlurShader::GetSetup(float blurAmount, int sampleCount)
FString FBlurShader::VertexShaderCode()
{
return R"(
#version 330
in vec4 PositionInProjection;
out vec2 TexCoord;
@ -187,7 +185,6 @@ FString FBlurShader::FragmentShaderCode(float blurAmount, int sampleCount, bool
const char *fragmentShader =
R"(
#version 330
in vec2 TexCoord;
uniform sampler2D SourceTexture;
out vec4 FragColor;

View file

@ -53,8 +53,8 @@ void FPresentShader::Bind()
{
if (!mShader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/present.vp");
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/present.fp");
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/present.vp", "", 330);
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/present.fp", "", 330);
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/present");
mShader.SetAttribLocation(0, "PositionInProjection");

View file

@ -56,48 +56,10 @@
#include "gl/renderer/gl_renderstate.h"
#include "gl/system/gl_cvars.h"
#include "gl/shaders/gl_shader.h"
#include "gl/shaders/gl_shaderprogram.h"
#include "gl/textures/gl_material.h"
#include "gl/dynlights/gl_lightbuffer.h"
//==========================================================================
//
// patch the shader source to work with
// GLSL 1.2 keywords and identifiers
//
//==========================================================================
void PatchCommon(FString &code)
{
code.Substitute("precision highp int;", "");
code.Substitute("precision highp float;", "");
}
void PatchVertShader(FString &code)
{
PatchCommon(code);
code.Substitute("in vec", "attribute vec");
code.Substitute("out vec", "varying vec");
code.Substitute("gl_ClipDistance", "//");
}
void PatchFragShader(FString &code)
{
PatchCommon(code);
code.Substitute("out vec4 FragColor;", "");
code.Substitute("FragColor", "gl_FragColor");
code.Substitute("in vec", "varying vec");
// this patches the switch statement to if's.
code.Substitute("break;", "");
code.Substitute("switch (uFixedColormap)", "int i = uFixedColormap;");
code.Substitute("case 0:", "if (i == 0)");
code.Substitute("case 1:", "else if (i == 1)");
code.Substitute("case 2:", "else if (i == 2)");
code.Substitute("case 3:", "else if (i == 3)");
code.Substitute("case 4:", "else if (i == 4)");
code.Substitute("case 5:", "else if (i == 5)");
code.Substitute("texture(", "texture2D(");
}
//==========================================================================
//
//
@ -204,8 +166,8 @@ bool FShader::Load(const char * name, const char * vert_prog_lump, const char *
if (gl.glslversion < 1.3)
{
PatchVertShader(vp_comb);
PatchFragShader(fp_comb);
FShaderProgram::PatchVertShader(vp_comb);
FShaderProgram::PatchFragShader(fp_comb);
}
hVertProg = glCreateShader(GL_VERTEX_SHADER);

View file

@ -93,22 +93,23 @@ void FShaderProgram::CreateShader(ShaderType type)
//
//==========================================================================
void FShaderProgram::Compile(ShaderType type, const char *lumpName)
void FShaderProgram::Compile(ShaderType type, const char *lumpName, const char *defines, int maxGlslVersion)
{
int lump = Wads.CheckNumForFullName(lumpName);
if (lump == -1) I_Error("Unable to load '%s'", lumpName);
if (lump == -1) I_FatalError("Unable to load '%s'", lumpName);
FString code = Wads.ReadLump(lump).GetString().GetChars();
Compile(type, lumpName, code);
Compile(type, lumpName, code, defines, maxGlslVersion);
}
void FShaderProgram::Compile(ShaderType type, const char *name, const FString &code)
void FShaderProgram::Compile(ShaderType type, const char *name, const FString &code, const char *defines, int maxGlslVersion)
{
CreateShader(type);
const auto &handle = mShaders[type];
int lengths[1] = { (int)code.Len() };
const char *sources[1] = { code.GetChars() };
FString patchedCode = PatchShader(type, code, defines, maxGlslVersion);
int lengths[1] = { (int)patchedCode.Len() };
const char *sources[1] = { patchedCode.GetChars() };
glShaderSource(handle, 1, sources, lengths);
glCompileShader(handle);
@ -117,7 +118,7 @@ void FShaderProgram::Compile(ShaderType type, const char *name, const FString &c
glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
I_Error("Compile Shader '%s':\n%s\n", name, GetShaderInfoLog(handle).GetChars());
I_FatalError("Compile Shader '%s':\n%s\n", name, GetShaderInfoLog(handle).GetChars());
}
else
{
@ -152,7 +153,7 @@ void FShaderProgram::Link(const char *name)
glGetProgramiv(mProgram, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
I_Error("Link Shader '%s':\n%s\n", name, GetProgramInfoLog(mProgram).GetChars());
I_FatalError("Link Shader '%s':\n%s\n", name, GetProgramInfoLog(mProgram).GetChars());
}
}
@ -207,3 +208,86 @@ FString FShaderProgram::GetProgramInfoLog(GLuint handle)
glGetProgramInfoLog(handle, 10000, &length, buffer);
return FString(buffer);
}
//==========================================================================
//
// Patches a shader to be compatible with the version of OpenGL in use
//
//==========================================================================
FString FShaderProgram::PatchShader(ShaderType type, const FString &code, const char *defines, int maxGlslVersion)
{
FString patchedCode;
int shaderVersion = MIN((int)round(gl.glslversion * 10) * 10, maxGlslVersion);
patchedCode.AppendFormat("#version %d\n", shaderVersion);
// TODO: Find some way to add extension requirements to the patching
//
// #extension GL_ARB_uniform_buffer_object : require
// #extension GL_ARB_shader_storage_buffer_object : require
if (defines)
patchedCode << defines;
if (gl.glslversion >= 1.3)
{
// these settings are actually pointless but there seem to be some old ATI drivers that fail to compile the shader without setting the precision here.
patchedCode << "precision highp int;\n";
patchedCode << "precision highp float;\n";
}
patchedCode << "#line 1\n";
patchedCode << code;
if (gl.glslversion < 1.3)
{
if (type == Vertex)
PatchVertShader(patchedCode);
else if (type == Fragment)
PatchFragShader(patchedCode);
}
return patchedCode;
}
//==========================================================================
//
// patch the shader source to work with
// GLSL 1.2 keywords and identifiers
//
//==========================================================================
void FShaderProgram::PatchCommon(FString &code)
{
code.Substitute("precision highp int;", "");
code.Substitute("precision highp float;", "");
}
void FShaderProgram::PatchVertShader(FString &code)
{
PatchCommon(code);
code.Substitute("in vec", "attribute vec");
code.Substitute("in float", "attribute float");
code.Substitute("out vec", "varying vec");
code.Substitute("out float", "varying float");
code.Substitute("gl_ClipDistance", "//");
}
void FShaderProgram::PatchFragShader(FString &code)
{
PatchCommon(code);
code.Substitute("out vec4 FragColor;", "");
code.Substitute("FragColor", "gl_FragColor");
code.Substitute("in vec", "varying vec");
// this patches the switch statement to if's.
code.Substitute("break;", "");
code.Substitute("switch (uFixedColormap)", "int i = uFixedColormap;");
code.Substitute("case 0:", "if (i == 0)");
code.Substitute("case 1:", "else if (i == 1)");
code.Substitute("case 2:", "else if (i == 2)");
code.Substitute("case 3:", "else if (i == 3)");
code.Substitute("case 4:", "else if (i == 4)");
code.Substitute("case 5:", "else if (i == 5)");
code.Substitute("texture(", "texture2D(");
}

View file

@ -15,8 +15,8 @@ public:
NumShaderTypes
};
void Compile(ShaderType type, const char *lumpName);
void Compile(ShaderType type, const char *name, const FString &code);
void Compile(ShaderType type, const char *lumpName, const char *defines, int maxGlslVersion);
void Compile(ShaderType type, const char *name, const FString &code, const char *defines, int maxGlslVersion);
void SetFragDataLocation(int index, const char *name);
void Link(const char *name);
void SetAttribLocation(int index, const char *name);
@ -25,7 +25,14 @@ public:
operator GLuint() const { return mProgram; }
explicit operator bool() const { return mProgram != 0; }
// Needed by FShader
static void PatchVertShader(FString &code);
static void PatchFragShader(FString &code);
private:
static FString PatchShader(ShaderType type, const FString &code, const char *defines, int maxGlslVersion);
static void PatchCommon(FString &code);
void CreateShader(ShaderType type);
FString GetShaderInfoLog(GLuint handle);
FString GetProgramInfoLog(GLuint handle);

View file

@ -51,15 +51,28 @@
void FTonemapShader::Bind()
{
if (!mShader)
auto &shader = mShader[gl_tonemap];
if (!shader)
{
mShader.Compile(FShaderProgram::Vertex, "shaders/glsl/tonemap.vp");
mShader.Compile(FShaderProgram::Fragment, "shaders/glsl/tonemap.fp");
mShader.SetFragDataLocation(0, "FragColor");
mShader.Link("shaders/glsl/tonemap");
mShader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(mShader, "InputTexture");
Exposure.Init(mShader, "ExposureAdjustment");
shader.Compile(FShaderProgram::Vertex, "shaders/glsl/tonemap.vp", "", 330);
shader.Compile(FShaderProgram::Fragment, "shaders/glsl/tonemap.fp", GetDefines(gl_tonemap), 330);
shader.SetFragDataLocation(0, "FragColor");
shader.Link("shaders/glsl/tonemap");
shader.SetAttribLocation(0, "PositionInProjection");
SceneTexture.Init(shader, "InputTexture");
Exposure.Init(shader, "ExposureAdjustment");
}
shader.Bind();
}
const char *FTonemapShader::GetDefines(int mode)
{
switch (mode)
{
default:
case Linear: return "#define LINEAR\n";
case Reinhard: return "#define REINHARD\n";
case HejlDawson: return "#define HEJLDAWSON\n";
case Uncharted2: return "#define UNCHARTED2\n";
}
mShader.Bind();
}

View file

@ -12,7 +12,19 @@ public:
FBufferedUniform1f Exposure;
private:
FShaderProgram mShader;
enum TonemapMode
{
None,
Uncharted2,
HejlDawson,
Reinhard,
Linear,
NumTonemapModes
};
static const char *GetDefines(int mode);
FShaderProgram mShader[NumTonemapModes];
};
#endif

View file

@ -41,4 +41,12 @@ EXTERN_CVAR(Bool, gl_seamless)
EXTERN_CVAR(Float, gl_mask_threshold)
EXTERN_CVAR(Float, gl_mask_sprite_threshold)
EXTERN_CVAR(Bool, gl_renderbuffers);
EXTERN_CVAR(Bool, gl_bloom);
EXTERN_CVAR(Float, gl_bloom_amount)
EXTERN_CVAR(Int, gl_bloom_kernel_size)
EXTERN_CVAR(Int, gl_tonemap)
EXTERN_CVAR(Float, gl_exposure)
#endif // _GL_INTERN_H

View file

@ -192,6 +192,9 @@ void gl_LoadExtensions()
{
if (CheckExtension("GL_NV_GPU_shader4") || CheckExtension("GL_EXT_GPU_shader4")) gl.glslversion = 1.21f; // for pre-3.0 drivers that support capable hardware. Needed for Apple.
else gl.glslversion = 0;
if (!CheckExtension("GL_EXT_packed_float")) gl.flags |= RFL_NO_RGBA16F;
if (!CheckExtension("GL_EXT_packed_depth_stencil")) gl.flags |= RFL_NO_DEPTHSTENCIL;
}
else if (gl.version < 4.f)
{

View file

@ -20,6 +20,9 @@ enum RenderFlags
RFL_SHADER_STORAGE_BUFFER = 4,
RFL_BUFFER_STORAGE = 8,
RFL_SAMPLER_OBJECTS = 16,
RFL_NO_RGBA16F = 32,
RFL_NO_DEPTHSTENCIL = 64
};
enum TexMode

View file

@ -156,7 +156,7 @@ bool OpenGLFrameBuffer::WipeStartScreen(int type)
glFinish();
wipestartscreen->Bind(0, false, false);
if (FGLRenderBuffers::IsSupported())
if (FGLRenderBuffers::IsEnabled())
{
GLRenderer->mBuffers->BindHudFB();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, Width, Height);
@ -192,7 +192,7 @@ void OpenGLFrameBuffer::WipeEndScreen()
glFinish();
wipeendscreen->Bind(0, false, false);
if (FGLRenderBuffers::IsSupported())
if (FGLRenderBuffers::IsEnabled())
GLRenderer->mBuffers->BindHudFB();
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, Width, Height);
@ -230,7 +230,7 @@ bool OpenGLFrameBuffer::WipeDo(int ticks)
glDisable(GL_DEPTH_TEST);
glDepthMask(false);
if (FGLRenderBuffers::IsSupported())
if (FGLRenderBuffers::IsEnabled())
{
GLRenderer->mBuffers->BindHudFB();
glViewport(0, 0, GLRenderer->mOutputViewport.width, GLRenderer->mOutputViewport.height);

View file

@ -2618,6 +2618,8 @@ GLPREFMNU_AMBLIGHT = "Ambient light level";
GLPREFMNU_RENDERQUALITY = "Rendering quality";
GLPREFMNU_VRMODE = "Stereo 3D VR";
GLPREFMNU_VRQUADSTEREO = "Enable Quad Stereo";
GLPREFMNU_TONEMAP = "Tonemap Mode";
GLPREFMNU_BLOOM = "Bloom effect";
// Option Values
OPTVAL_SMART = "Smart";
@ -2686,3 +2688,6 @@ OPTVAL_AMBERBLUE = "Amber/Blue";
OPTVAL_LEFTEYE = "Left Eye";
OPTVAL_RIGHTEYE = "Right Eye";
OPTVAL_QUADBUFFERED = "Quad-buffered";
OPTVAL_UNCHARTED2 = "Uncharted 2";
OPTVAL_HEJLDAWSON = "Hejl Dawson";
OPTVAL_REINHARD = "Reinhard";

View file

@ -32,6 +32,15 @@ OptionValue "HWGammaModes"
2, "$OPTVAL_FULLSCREENONLY"
}
OptionValue "TonemapModes"
{
0, "$OPTVAL_OFF"
1, "$OPTVAL_UNCHARTED2"
2, "$OPTVAL_HEJLDAWSON"
3, "$OPTVAL_REINHARD"
4, "$OPTVAL_LINEAR"
}
OptionValue "TextureFormats"
{
0, "$OPTVAL_RGBA8"
@ -198,4 +207,6 @@ OptionMenu "GLPrefOptions"
Option "$GLPREFMNU_RENDERQUALITY", gl_render_precise, "Precision"
Option "$GLPREFMNU_VRMODE", vr_mode, "VRMode"
Option "$GLPREFMNU_VRQUADSTEREO", vr_enable_quadbuffered, "OnOff"
Option "$GLPREFMNU_TONEMAP", gl_tonemap, "TonemapModes"
Option "$GLPREFMNU_BLOOM", gl_bloom, "OnOff"
}

View file

@ -1,6 +1,4 @@
#version 330
in vec2 TexCoord;
out vec4 FragColor;

View file

@ -1,6 +1,4 @@
#version 330
in vec4 PositionInProjection;
out vec2 TexCoord;

View file

@ -1,6 +1,4 @@
#version 330
in vec2 TexCoord;
out vec4 FragColor;

View file

@ -1,6 +1,4 @@
#version 330
in vec4 PositionInProjection;
out vec2 TexCoord;

View file

@ -1,6 +1,4 @@
#version 330
in vec2 TexCoord;
out vec4 FragColor;

View file

@ -1,6 +1,4 @@
#version 330
in vec4 PositionInProjection;
out vec2 TexCoord;

View file

@ -1,6 +1,4 @@
#version 330
in vec2 TexCoord;
out vec4 FragColor;
@ -19,23 +17,31 @@ vec3 sRGB(vec3 c)
return sqrt(c); // cheaper, but assuming gamma of 2.0 instead of 2.2
}
vec3 TonemapLinear(vec3 color)
#if defined(LINEAR)
vec3 Tonemap(vec3 color)
{
return sRGB(color);
}
vec3 TonemapReinhard(vec3 color)
#elif defined(REINHARD)
vec3 Tonemap(vec3 color)
{
color = color / (1 + color);
return sRGB(color);
}
vec3 TonemapHejlDawson(vec3 color)
#elif defined(HEJLDAWSON)
vec3 Tonemap(vec3 color)
{
vec3 x = max(vec3(0), color - 0.004);
return (x * (6.2 * x + 0.5)) / (x * (6.2 * x + 1.7) + 0.06); // no sRGB needed
}
#else
vec3 Uncharted2Tonemap(vec3 x)
{
float A = 0.15;
@ -47,7 +53,7 @@ vec3 Uncharted2Tonemap(vec3 x)
return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
}
vec3 TonemapUncharted2(vec3 color)
vec3 Tonemap(vec3 color)
{
float W = 11.2;
float ExposureBias = 2.0;
@ -56,10 +62,12 @@ vec3 TonemapUncharted2(vec3 color)
return sRGB(curr * whiteScale);
}
#endif
void main()
{
vec3 color = texture(InputTexture, TexCoord).rgb;
color = color * ExposureAdjustment;
color = Linear(color); // needed because gzdoom's scene texture is not linear at the moment
FragColor = vec4(TonemapUncharted2(color), 1.0);
FragColor = vec4(Tonemap(color), 1.0);
}

View file

@ -1,6 +1,4 @@
#version 330
in vec4 PositionInProjection;
out vec2 TexCoord;