2023-01-06 01:39:25 +01:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2012-10-15 12:34:23 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_POSTPROCMANAGER
|
|
|
|
#define INCLUDED_POSTPROCMANAGER
|
|
|
|
|
2020-11-21 12:20:29 +01:00
|
|
|
#include "graphics/ShaderTechniquePtr.h"
|
2019-08-20 23:15:15 +02:00
|
|
|
#include "ps/CStr.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/IFramebuffer.h"
|
|
|
|
#include "renderer/backend/IDeviceCommandContext.h"
|
2023-01-06 01:39:25 +01:00
|
|
|
#include "renderer/backend/IShaderProgram.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/ITexture.h"
|
2019-08-20 23:15:15 +02:00
|
|
|
|
2022-02-05 17:59:23 +01:00
|
|
|
#include <array>
|
2019-08-20 23:15:15 +02:00
|
|
|
#include <vector>
|
2012-10-15 12:34:23 +02:00
|
|
|
|
|
|
|
class CPostprocManager
|
|
|
|
{
|
2019-08-20 23:15:15 +02:00
|
|
|
public:
|
|
|
|
CPostprocManager();
|
|
|
|
~CPostprocManager();
|
|
|
|
|
2022-01-28 07:34:34 +01:00
|
|
|
// Returns true if the the manager can be used.
|
|
|
|
bool IsEnabled() const;
|
|
|
|
|
2019-08-20 23:15:15 +02:00
|
|
|
// Create all buffers/textures in GPU memory and set default effect.
|
|
|
|
// @note Must be called before using in the renderer. May be called multiple times.
|
|
|
|
void Initialize();
|
|
|
|
|
|
|
|
// Update the size of the screen
|
|
|
|
void Resize();
|
|
|
|
|
|
|
|
// Returns a list of xml files found in shaders/effects/postproc.
|
|
|
|
static std::vector<CStrW> GetPostEffects();
|
|
|
|
|
|
|
|
// Returns the name of the current effect.
|
|
|
|
const CStrW& GetPostEffect() const
|
|
|
|
{
|
|
|
|
return m_PostProcEffect;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the current effect.
|
|
|
|
void SetPostEffect(const CStrW& name);
|
|
|
|
|
2020-02-05 22:53:10 +01:00
|
|
|
// Triggers update of shaders and FBO if needed.
|
|
|
|
void UpdateAntiAliasingTechnique();
|
2020-08-08 00:16:55 +02:00
|
|
|
void UpdateSharpeningTechnique();
|
|
|
|
void UpdateSharpnessFactor();
|
2020-02-05 22:53:10 +01:00
|
|
|
|
2019-09-20 09:54:44 +02:00
|
|
|
void SetDepthBufferClipPlanes(float nearPlane, float farPlane);
|
|
|
|
|
2019-08-20 23:15:15 +02:00
|
|
|
// @note CPostprocManager must be initialized first
|
2022-12-29 07:53:06 +01:00
|
|
|
Renderer::Backend::IFramebuffer* PrepareAndGetOutputFramebuffer();
|
2019-08-20 23:15:15 +02:00
|
|
|
|
|
|
|
// First renders blur textures, then calls ApplyEffect for each effect pass,
|
|
|
|
// ping-ponging the buffers at each step.
|
|
|
|
// @note CPostprocManager must be initialized first
|
2022-01-19 18:28:47 +01:00
|
|
|
void ApplyPostproc(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
|
2019-08-20 23:15:15 +02:00
|
|
|
|
2022-11-26 22:55:17 +01:00
|
|
|
// Blits the final postprocessed texture to the system framebuffer. The system
|
|
|
|
// framebuffer is selected as the output buffer. Should be called before
|
|
|
|
// silhouette rendering.
|
2019-08-20 23:15:15 +02:00
|
|
|
// @note CPostprocManager must be initialized first
|
2022-12-29 07:53:06 +01:00
|
|
|
void BlitOutputFramebuffer(
|
2022-11-26 22:55:17 +01:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
|
|
|
|
Renderer::Backend::IFramebuffer* destination);
|
2019-08-20 23:15:15 +02:00
|
|
|
|
2020-11-15 21:07:48 +01:00
|
|
|
// Returns true if we render main scene in the MSAA framebuffer.
|
|
|
|
bool IsMultisampleEnabled() const;
|
|
|
|
|
|
|
|
// Resolves the MSAA framebuffer into the regular one.
|
2022-02-05 17:59:23 +01:00
|
|
|
void ResolveMultisampleFramebuffer(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
|
2020-11-15 21:07:48 +01:00
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
private:
|
2020-11-15 21:07:48 +01:00
|
|
|
void CreateMultisampleBuffer();
|
|
|
|
void DestroyMultisampleBuffer();
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2022-10-12 23:51:27 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::IFramebuffer> m_CaptureFramebuffer;
|
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// Two framebuffers, that we flip between at each shader pass.
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::IFramebuffer>
|
2022-10-12 23:51:27 +02:00
|
|
|
m_PingFramebuffer, m_PongFramebuffer;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2015-03-16 00:59:48 +01:00
|
|
|
// Unique color textures for the framebuffers.
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::ITexture> m_ColorTex1, m_ColorTex2;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// The framebuffers share a depth/stencil texture.
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::ITexture> m_DepthTex;
|
2019-09-20 09:54:44 +02:00
|
|
|
float m_NearPlane, m_FarPlane;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
|
|
|
// A framebuffer and textures x2 for each blur level we render.
|
2022-02-05 17:59:23 +01:00
|
|
|
struct BlurScale
|
|
|
|
{
|
|
|
|
struct Step
|
|
|
|
{
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::IFramebuffer> framebuffer;
|
|
|
|
std::unique_ptr<Renderer::Backend::ITexture> texture;
|
2022-02-05 17:59:23 +01:00
|
|
|
};
|
|
|
|
std::array<Step, 2> steps;
|
|
|
|
};
|
|
|
|
std::array<BlurScale, 3> m_BlurScales;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// Indicates which of the ping-pong buffers is used for reading and which for drawing.
|
|
|
|
bool m_WhichBuffer;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2023-01-06 01:39:25 +01:00
|
|
|
Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr;
|
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// The name and shader technique we are using. "default" name means no technique is used
|
|
|
|
// (i.e. while we do allocate the buffers, no effects are rendered).
|
|
|
|
CStrW m_PostProcEffect;
|
|
|
|
CShaderTechniquePtr m_PostProcTech;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2020-08-08 00:16:55 +02:00
|
|
|
CStr m_SharpName;
|
|
|
|
CShaderTechniquePtr m_SharpTech;
|
|
|
|
float m_Sharpness;
|
|
|
|
|
2020-02-05 22:53:10 +01:00
|
|
|
CStr m_AAName;
|
|
|
|
CShaderTechniquePtr m_AATech;
|
2020-11-15 21:07:48 +01:00
|
|
|
bool m_UsingMultisampleBuffer;
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::IFramebuffer> m_MultisampleFramebuffer;
|
|
|
|
std::unique_ptr<Renderer::Backend::ITexture>
|
2021-12-27 22:01:43 +01:00
|
|
|
m_MultisampleColorTex, m_MultisampleDepthTex;
|
2022-02-13 22:46:03 +01:00
|
|
|
uint32_t m_MultisampleCount;
|
|
|
|
std::vector<uint32_t> m_AllowedSampleCounts;
|
2020-02-05 22:53:10 +01:00
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// The current screen dimensions in pixels.
|
|
|
|
int m_Width, m_Height;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2014-10-05 22:50:10 +02:00
|
|
|
// Is the postproc manager initialized? Buffers created? Default effect loaded?
|
|
|
|
bool m_IsInitialized;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// Creates blur textures at various scales, for bloom, DOF, etc.
|
2022-01-19 18:28:47 +01:00
|
|
|
void ApplyBlur(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2012-10-15 12:34:23 +02:00
|
|
|
// High quality GPU image scaling to half size. outTex must have exactly half the size
|
|
|
|
// of inTex. inWidth and inHeight are the dimensions of inTex in texels.
|
2022-01-19 18:28:47 +01:00
|
|
|
void ApplyBlurDownscale2x(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
|
|
|
|
Renderer::Backend::IFramebuffer* framebuffer,
|
|
|
|
Renderer::Backend::ITexture* inTex,
|
2022-02-05 17:59:23 +01:00
|
|
|
int inWidth, int inHeight);
|
2016-11-23 12:18:37 +01:00
|
|
|
|
|
|
|
// GPU-based Gaussian blur in two passes. inOutTex contains the input image and will be filled
|
|
|
|
// with the blurred image. tempTex must have the same size as inOutTex.
|
2012-10-15 12:34:23 +02:00
|
|
|
// inWidth and inHeight are the dimensions of the images in texels.
|
2022-01-19 18:28:47 +01:00
|
|
|
void ApplyBlurGauss(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
|
|
|
|
Renderer::Backend::ITexture* inTex,
|
|
|
|
Renderer::Backend::ITexture* tempTex,
|
|
|
|
Renderer::Backend::IFramebuffer* tempFramebuffer,
|
|
|
|
Renderer::Backend::IFramebuffer* outFramebuffer,
|
2022-02-05 17:59:23 +01:00
|
|
|
int inWidth, int inHeight);
|
2016-11-23 12:18:37 +01:00
|
|
|
|
|
|
|
// Applies a pass of a given effect to the entire current framebuffer. The shader is
|
2012-10-15 12:34:23 +02:00
|
|
|
// provided with a number of general-purpose variables, including the rendered screen so far,
|
|
|
|
// the depth buffer, a number of blur textures, the screen size, the zNear/zFar planes and
|
|
|
|
// some other parameters used by the optional bloom/HDR pass.
|
2022-01-19 18:28:47 +01:00
|
|
|
void ApplyEffect(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
|
2022-02-05 17:59:23 +01:00
|
|
|
const CShaderTechniquePtr& shaderTech, int pass);
|
2014-10-05 22:50:10 +02:00
|
|
|
|
|
|
|
// Delete all allocated buffers/textures from GPU memory.
|
|
|
|
void Cleanup();
|
|
|
|
|
2014-10-27 01:53:31 +01:00
|
|
|
// Delete existing buffers/textures and create them again, using a new screen size if needed.
|
|
|
|
// (the textures are also attached to the framebuffers)
|
|
|
|
void RecreateBuffers();
|
2012-10-15 12:34:23 +02:00
|
|
|
};
|
|
|
|
|
2019-08-20 23:15:15 +02:00
|
|
|
#endif // INCLUDED_POSTPROCMANAGER
|