Skip to content

Commit

Permalink
- write OpenGL backend for hw_postprocess (FGLRenderBuffers::RenderEf…
Browse files Browse the repository at this point in the history
…fect)

- remove old fxaa and lens shader classes
- render the fxaa and lens effects
  • Loading branch information
dpjudas committed Jun 20, 2018
1 parent e3997d5 commit 151ed22
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 319 deletions.
2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -1072,8 +1072,6 @@ set (PCH_SOURCES
hwrenderer/postprocessing/hw_blurshader.cpp
hwrenderer/postprocessing/hw_colormapshader.cpp
hwrenderer/postprocessing/hw_tonemapshader.cpp
hwrenderer/postprocessing/hw_lensshader.cpp
hwrenderer/postprocessing/hw_fxaashader.cpp
hwrenderer/textures/hw_material.cpp
hwrenderer/textures/hw_precache.cpp
hwrenderer/utility/hw_clock.cpp
Expand Down
195 changes: 193 additions & 2 deletions src/gl/renderer/gl_postprocess.cpp
Expand Up @@ -44,9 +44,9 @@
#include "hwrenderer/postprocessing/hw_blurshader.h"
#include "hwrenderer/postprocessing/hw_tonemapshader.h"
#include "hwrenderer/postprocessing/hw_colormapshader.h"
#include "hwrenderer/postprocessing/hw_lensshader.h"
#include "hwrenderer/postprocessing/hw_fxaashader.h"
#include "hwrenderer/postprocessing/hw_presentshader.h"
#include "hwrenderer/postprocessing/hw_postprocess.h"
#include "hwrenderer/postprocessing/hw_postprocess_cvars.h"
#include "gl/shaders/gl_postprocessshaderinstance.h"
#include "gl/stereo3d/gl_stereo3d.h"
#include "gl/textures/gl_hwtexture.h"
Expand Down Expand Up @@ -74,6 +74,164 @@ void FGLRenderer::PostProcessScene(int fixedcm, const std::function<void()> &aft
mCustomPostProcessShaders->Run("scene");
}

void FGLRenderBuffers::RenderEffect(const FString &name)
{
// Create/update textures (To do: move out of RunEffect)
{
TMap<FString, PPTextureDesc>::Iterator it(hw_postprocess.Textures);
TMap<FString, PPTextureDesc>::Pair *pair;
while (it.NextPair(pair))
{
auto &gltexture = GLTextures[pair->Key];
auto &glframebuffer = GLTextureFBs[pair->Key];

int glformat;
switch (pair->Value.Format)
{
default:
case PixelFormat::Rgba8: glformat = GL_RGBA8; break;
case PixelFormat::Rgba16f: glformat = GL_RGBA16F; break;
}

if (gltexture && (gltexture.Width != pair->Value.Width || gltexture.Height != pair->Value.Height))
{
glDeleteTextures(1, &gltexture.handle);
glDeleteFramebuffers(1, &glframebuffer.handle);
gltexture.handle = 0;
glframebuffer.handle = 0;
}

if (!gltexture)
{
gltexture = Create2DTexture(name.GetChars(), glformat, pair->Value.Width, pair->Value.Height);
gltexture.Width = pair->Value.Width;
gltexture.Height = pair->Value.Height;
glframebuffer = CreateFrameBuffer(name.GetChars(), gltexture);
}
}
}

// Compile shaders (To do: move out of RunEffect)
{
TMap<FString, PPShader>::Iterator it(hw_postprocess.Shaders);
TMap<FString, PPShader>::Pair *pair;
while (it.NextPair(pair))
{
const auto &desc = pair->Value;
auto &glshader = GLShaders[pair->Key];
if (!glshader)
{
glshader = std::make_unique<FShaderProgram>();

FString prolog;
if (!desc.Uniforms.empty())
prolog = UniformBlockDecl::Create("Uniforms", desc.Uniforms, POSTPROCESS_BINDINGPOINT);
prolog += desc.Defines;

glshader->Compile(IShaderProgram::Vertex, desc.VertexShader, "", desc.Version);
glshader->Compile(IShaderProgram::Fragment, desc.FragmentShader, prolog, desc.Version);
glshader->Link(pair->Key.GetChars());
if (!desc.Uniforms.empty())
glshader->SetUniformBufferLocation(POSTPROCESS_BINDINGPOINT, "Uniforms");
}
}
}

// Render the effect

FGLDebug::PushGroup(name.GetChars());

FGLPostProcessState savedState;
savedState.SaveTextureBindings(3);

for (const PPStep &step : hw_postprocess.Effects[name])
{
// Bind input textures
for (unsigned int index = 0; index < step.Textures.Size(); index++)
{
const PPTextureInput &input = step.Textures[index];
int filter = (input.Filter == PPFilterMode::Nearest) ? GL_NEAREST : GL_LINEAR;

switch (input.Type)
{
default:
case PPTextureType::CurrentPipelineTexture:
BindCurrentTexture(index, filter);
break;

case PPTextureType::NextPipelineTexture:
I_FatalError("PPTextureType::NextPipelineTexture not allowed as input\n");
break;

case PPTextureType::PPTexture:
GLTextures[input.Texture].Bind(index, filter);
break;
}
}

// Set render target
switch (step.Output.Type)
{
default:
case PPTextureType::CurrentPipelineTexture:
BindCurrentFB();
break;

case PPTextureType::NextPipelineTexture:
BindNextFB();
break;

case PPTextureType::PPTexture:
GLTextureFBs[step.Output.Texture].Bind();
break;
}

// Set blend mode
if (step.BlendMode.BlendOp == STYLEOP_Add && step.BlendMode.SrcAlpha == STYLEALPHA_One && step.BlendMode.DestAlpha == STYLEALPHA_Zero && step.BlendMode.Flags == 0)
{
glDisable(GL_BLEND);
}
else
{
// To do: support all the modes
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
if (step.BlendMode.SrcAlpha == STYLEALPHA_One && step.BlendMode.DestAlpha == STYLEALPHA_One)
glBlendFunc(GL_ONE, GL_ONE);
else
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

// Setup viewport
glViewport(step.Viewport.left, step.Viewport.top, step.Viewport.width, step.Viewport.height);

auto &shader = GLShaders[step.ShaderName];

// Set uniforms
if (step.Uniforms.Size > 0)
{
if (!shader->Uniforms)
shader->Uniforms.reset(screen->CreateUniformBuffer(step.Uniforms.Size));
shader->Uniforms->SetData(step.Uniforms.Data);
shader->Uniforms->Bind(POSTPROCESS_BINDINGPOINT);
}

// Set shader
shader->Bind(NOQUEUE);

// Draw the screen quad
GLRenderer->RenderScreenQuad();

// Advance to next PP texture if our output was sent there
if (step.Output.Type == PPTextureType::NextPipelineTexture)
NextTexture();
}

glViewport(screen->mScreenViewport.left, screen->mScreenViewport.top, screen->mScreenViewport.width, screen->mScreenViewport.height);

FGLDebug::PopGroup();
}


//-----------------------------------------------------------------------------
//
Expand Down Expand Up @@ -304,6 +462,18 @@ static void RenderBlur(FGLRenderer *renderer, float blurAmount, PPTexture input,

void FGLRenderer::BloomScene(int fixedcm)
{
#if 0

if (mBuffers->GetSceneWidth() <= 0 || mBuffers->GetSceneHeight() <= 0)
return;

PPBloom bloom;
bloom.DeclareShaders();
bloom.UpdateTextures(mBuffers->GetSceneWidth(), mBuffers->GetSceneHeight());
bloom.UpdateSteps(fixedcm);
mBuffers->RenderEffect("Bloom");

#else
// Only bloom things if enabled and no special fixed light mode is active
if (!gl_bloom || fixedcm != CM_DEFAULT || gl_ssao_debug)
return;
Expand Down Expand Up @@ -372,6 +542,7 @@ void FGLRenderer::BloomScene(int fixedcm)
glViewport(mScreenViewport.left, mScreenViewport.top, mScreenViewport.width, mScreenViewport.height);

FGLDebug::PopGroup();
#endif
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -566,6 +737,14 @@ void FGLRenderer::ColormapScene(int fixedcm)

void FGLRenderer::LensDistortScene()
{
#if 1

PPLensDistort lens;
lens.DeclareShaders();
lens.UpdateSteps();
mBuffers->RenderEffect("LensDistortScene");

#else
if (gl_lens == 0)
return;
Expand Down Expand Up @@ -610,6 +789,7 @@ void FGLRenderer::LensDistortScene()
mBuffers->NextTexture();
FGLDebug::PopGroup();
#endif
}

//-----------------------------------------------------------------------------
Expand All @@ -620,6 +800,15 @@ void FGLRenderer::LensDistortScene()

void FGLRenderer::ApplyFXAA()
{
#if 1

PPFXAA fxaa;
fxaa.DeclareShaders();
fxaa.UpdateSteps();
mBuffers->RenderEffect("ApplyFXAA");

#else
if (0 == gl_fxaa)
{
return;
Expand All @@ -644,6 +833,8 @@ void FGLRenderer::ApplyFXAA()
mBuffers->NextTexture();
FGLDebug::PopGroup();
#endif
}

//-----------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions src/gl/renderer/gl_renderbuffers.h
Expand Up @@ -2,6 +2,7 @@
#define __GL_RENDERBUFFERS_H

#include "gl/shaders/gl_shader.h"
#include "hwrenderer/postprocessing/hw_postprocess.h"

class PPTexture
{
Expand All @@ -16,6 +17,11 @@ class PPTexture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
}

int Width = -1;
int Height = -1;

explicit operator bool() const { return handle != 0; }

private:
GLuint handle = 0;

Expand All @@ -30,6 +36,8 @@ class PPFrameBuffer
glBindFramebuffer(GL_FRAMEBUFFER, handle);
}

explicit operator bool() const { return handle != 0; }

private:
GLuint handle = 0;

Expand All @@ -41,6 +49,8 @@ class PPRenderBuffer
private:
GLuint handle = 0;

explicit operator bool() const { return handle != 0; }

friend class FGLRenderBuffers;
};

Expand All @@ -64,6 +74,8 @@ class FGLExposureTextureLevel
int Height = 0;
};

class FShaderProgram;

class FGLRenderBuffers
{
public:
Expand All @@ -72,6 +84,12 @@ class FGLRenderBuffers

bool Setup(int width, int height, int sceneWidth, int sceneHeight);

void RenderEffect(const FString &name);

TMap<PPTextureName, PPTexture> GLTextures;
TMap<PPTextureName, PPFrameBuffer> GLTextureFBs;
TMap<PPShaderName, std::shared_ptr<FShaderProgram>> GLShaders;

void BindSceneFB(bool sceneData);
void BindSceneColorTexture(int index);
void BindSceneFogTexture(int index);
Expand Down
11 changes: 0 additions & 11 deletions src/gl/renderer/gl_renderer.cpp
Expand Up @@ -55,8 +55,6 @@
#include "hwrenderer/postprocessing/hw_blurshader.h"
#include "hwrenderer/postprocessing/hw_tonemapshader.h"
#include "hwrenderer/postprocessing/hw_colormapshader.h"
#include "hwrenderer/postprocessing/hw_lensshader.h"
#include "hwrenderer/postprocessing/hw_fxaashader.h"
#include "hwrenderer/postprocessing/hw_presentshader.h"
#include "hwrenderer/postprocessing/hw_present3dRowshader.h"
#include "hwrenderer/postprocessing/hw_shadowmapshader.h"
Expand Down Expand Up @@ -112,13 +110,10 @@ FGLRenderer::FGLRenderer(OpenGLFrameBuffer *fb)
mTonemapShader = nullptr;
mTonemapPalette = nullptr;
mColormapShader = nullptr;
mLensShader = nullptr;
mLinearDepthShader = nullptr;
mDepthBlurShader = nullptr;
mSSAOShader = nullptr;
mSSAOCombineShader = nullptr;
mFXAAShader = nullptr;
mFXAALumaShader = nullptr;
mShadowMapShader = nullptr;
mCustomPostProcessShaders = nullptr;
}
Expand All @@ -141,9 +136,6 @@ void FGLRenderer::Initialize(int width, int height)
mTonemapShader = new FTonemapShader();
mColormapShader = new FColormapShader();
mTonemapPalette = nullptr;
mLensShader = new FLensShader();
mFXAAShader = new FFXAAShader;
mFXAALumaShader = new FFXAALumaShader;
mPresentShader = new FPresentShader();
mPresent3dCheckerShader = new FPresent3DCheckerShader();
mPresent3dColumnShader = new FPresent3DColumnShader();
Expand Down Expand Up @@ -207,11 +199,8 @@ FGLRenderer::~FGLRenderer()
if (mTonemapShader) delete mTonemapShader;
if (mTonemapPalette) delete mTonemapPalette;
if (mColormapShader) delete mColormapShader;
if (mLensShader) delete mLensShader;
if (mShadowMapShader) delete mShadowMapShader;
delete mCustomPostProcessShaders;
delete mFXAAShader;
delete mFXAALumaShader;
}

//===========================================================================
Expand Down
6 changes: 0 additions & 6 deletions src/gl/renderer/gl_renderer.h
Expand Up @@ -37,9 +37,6 @@ class FExposureCombineShader;
class FBlurShader;
class FTonemapShader;
class FColormapShader;
class FLensShader;
class FFXAALumaShader;
class FFXAAShader;
class FPresentShader;
class FPresent3DCheckerShader;
class FPresent3DColumnShader;
Expand Down Expand Up @@ -93,9 +90,6 @@ class FGLRenderer
FTonemapShader *mTonemapShader;
FColormapShader *mColormapShader;
FHardwareTexture *mTonemapPalette;
FLensShader *mLensShader;
FFXAALumaShader *mFXAALumaShader;
FFXAAShader *mFXAAShader;
FPresentShader *mPresentShader;
FPresent3DCheckerShader *mPresent3dCheckerShader;
FPresent3DColumnShader *mPresent3dColumnShader;
Expand Down
2 changes: 2 additions & 0 deletions src/gl/shaders/gl_shaderprogram.h
Expand Up @@ -21,6 +21,8 @@ class FShaderProgram : public IShaderProgram
GLuint Handle() { return mProgram; }
//explicit operator bool() const { return mProgram != 0; }

std::unique_ptr<IUniformBuffer> Uniforms;

private:
FShaderProgram(const FShaderProgram &) = delete;
FShaderProgram &operator=(const FShaderProgram &) = delete;
Expand Down

0 comments on commit 151ed22

Please sign in to comment.