- Simplify FGLRenderer::PostProcessScene

This commit is contained in:
Magnus Norddahl 2018-06-29 21:55:46 +02:00
commit ecb5d69ae3
6 changed files with 289 additions and 274 deletions

View file

@ -57,182 +57,31 @@ void FGLRenderer::RenderScreenQuad()
void FGLRenderer::PostProcessScene(int fixedcm, const std::function<void()> &afterBloomDrawEndScene2D)
{
hw_postprocess.fixedcm = fixedcm;
hw_postprocess.SceneWidth = mBuffers->GetSceneWidth();
hw_postprocess.SceneHeight = mBuffers->GetSceneHeight();
hw_postprocess.DeclareShaders();
hw_postprocess.UpdateTextures();
hw_postprocess.UpdateSteps();
mBuffers->BlitSceneToTexture();
UpdateCameraExposure();
mBuffers->CompileEffectShaders();
mBuffers->UpdateEffectTextures();
mBuffers->RenderEffect("UpdateCameraExposure");
mCustomPostProcessShaders->Run("beforebloom");
BloomScene(fixedcm);
mBuffers->RenderEffect("BloomScene");
mBuffers->BindCurrentFB();
afterBloomDrawEndScene2D();
TonemapScene();
ColormapScene(fixedcm);
LensDistortScene();
ApplyFXAA();
mBuffers->RenderEffect("TonemapScene");
mBuffers->RenderEffect("ColormapScene");
mBuffers->RenderEffect("LensDistortScene");
mBuffers->RenderEffect("ApplyFXAA");
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;
case PixelFormat::R32f: glformat = GL_R32F; 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)
{
if (pair->Value.Data)
gltexture = Create2DTexture(pair->Key.GetChars(), glformat, pair->Value.Width, pair->Value.Height, pair->Value.Data.get());
else
gltexture = Create2DTexture(pair->Key.GetChars(), glformat, pair->Value.Width, pair->Value.Height);
gltexture.Width = pair->Value.Width;
gltexture.Height = pair->Value.Height;
glframebuffer = CreateFrameBuffer(pair->Key.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();
}
//-----------------------------------------------------------------------------
//
// Adds ambient occlusion to the scene
@ -345,77 +194,25 @@ void FGLRenderer::AmbientOccludeScene(float m5)
FGLDebug::PopGroup();
}
void FGLRenderer::UpdateCameraExposure()
{
PPCameraExposure exposure;
exposure.DeclareShaders();
exposure.UpdateTextures(mBuffers->GetSceneWidth(), mBuffers->GetSceneHeight());
exposure.UpdateSteps();
mBuffers->RenderEffect("UpdateCameraExposure");
}
void FGLRenderer::BloomScene(int fixedcm)
{
if (mBuffers->GetSceneWidth() <= 0 || mBuffers->GetSceneHeight() <= 0)
return;
PPBloom bloom;
bloom.DeclareShaders();
bloom.UpdateTextures(mBuffers->GetSceneWidth(), mBuffers->GetSceneHeight());
bloom.UpdateSteps(fixedcm);
mBuffers->RenderEffect("BloomScene");
}
void FGLRenderer::BlurScene(float gameinfobluramount)
{
if (mBuffers->GetSceneWidth() <= 0 || mBuffers->GetSceneHeight() <= 0)
return;
hw_postprocess.gameinfobluramount = gameinfobluramount;
hw_postprocess.DeclareShaders();
hw_postprocess.UpdateTextures();
hw_postprocess.UpdateSteps();
mBuffers->CompileEffectShaders();
mBuffers->UpdateEffectTextures();
PPBloom blur;
blur.DeclareShaders();
blur.UpdateTextures(mBuffers->GetSceneWidth(), mBuffers->GetSceneHeight());
blur.UpdateBlurSteps(gameinfobluramount);
mBuffers->RenderEffect("BlurScene");
}
void FGLRenderer::TonemapScene()
{
PPTonemap tonemap;
tonemap.DeclareShaders();
tonemap.UpdateTextures();
tonemap.UpdateSteps();
mBuffers->RenderEffect("TonemapScene");
}
void FGLRenderer::ClearTonemapPalette()
{
hw_postprocess.Textures.Remove("Tonemap.Palette");
}
void FGLRenderer::ColormapScene(int fixedcm)
{
PPColormap colormap;
colormap.DeclareShaders();
colormap.UpdateSteps(fixedcm);
mBuffers->RenderEffect("ColormapScene");
}
void FGLRenderer::LensDistortScene()
{
PPLensDistort lens;
lens.DeclareShaders();
lens.UpdateSteps();
mBuffers->RenderEffect("LensDistortScene");
}
void FGLRenderer::ApplyFXAA()
{
PPFXAA fxaa;
fxaa.DeclareShaders();
fxaa.UpdateSteps();
mBuffers->RenderEffect("ApplyFXAA");
}
//-----------------------------------------------------------------------------
//
// Copies the rendered screen to its final destination