verus/Verus/src/CGI/DeferredShading.h

187 lines
5.5 KiB
C
Raw Normal View History

2022-02-06 10:56:57 +01:00
// Copyright (C) 2021-2022, Dmitry Maluev (dmaluev@gmail.com). All rights reserved.
2019-09-01 13:49:15 +02:00
#pragma once
namespace verus
{
namespace CGI
{
enum class LightType : int
{
none,
dir,
omni,
spot
};
class DeferredShading : public Object
{
2019-10-02 19:35:07 +02:00
#include "../Shaders/DS.inc.hlsl"
2022-04-01 18:23:39 +02:00
#include "../Shaders/DS_Ambient.inc.hlsl"
2021-02-01 18:22:02 +01:00
#include "../Shaders/DS_Compose.inc.hlsl"
#include "../Shaders/DS_Reflection.inc.hlsl"
2020-08-02 01:00:00 +02:00
#include "../Shaders/DS_BakeSprites.inc.hlsl"
2019-09-01 13:49:15 +02:00
2020-05-01 20:50:55 +02:00
enum SHADER
2019-09-01 13:49:15 +02:00
{
2020-05-01 20:50:55 +02:00
SHADER_LIGHT,
2022-04-01 18:23:39 +02:00
SHADER_AMBIENT,
2021-02-01 18:22:02 +01:00
SHADER_COMPOSE,
SHADER_REFLECTION,
2020-08-02 01:00:00 +02:00
SHADER_BAKE_SPRITES,
2020-05-01 20:50:55 +02:00
SHADER_COUNT
2019-09-01 13:49:15 +02:00
};
2019-10-02 19:35:07 +02:00
enum PIPE
2019-09-01 13:49:15 +02:00
{
2019-10-02 19:35:07 +02:00
PIPE_INSTANCED_DIR,
PIPE_INSTANCED_OMNI,
PIPE_INSTANCED_SPOT,
2022-04-01 18:23:39 +02:00
PIPE_AMBIENT,
2019-10-02 19:35:07 +02:00
PIPE_COMPOSE,
2021-02-01 18:22:02 +01:00
PIPE_REFLECTION,
2022-04-01 18:23:39 +02:00
PIPE_REFLECTION_DEBUG,
2020-05-01 20:50:55 +02:00
PIPE_TONE_MAPPING,
2019-10-02 19:35:07 +02:00
PIPE_QUAD,
2020-08-02 01:00:00 +02:00
PIPE_BAKE_SPRITES,
2020-05-01 20:50:55 +02:00
PIPE_COUNT
2019-09-01 13:49:15 +02:00
};
enum TEX
{
2022-04-01 18:23:39 +02:00
TEX_GBUFFER_0, // {Albedo.rgb, SSSHue}.
TEX_GBUFFER_1, // {Normal.xy, Emission, MotionBlur}.
TEX_GBUFFER_2, // {Occlusion, Roughness, Metallic, WrapDiffuse}.
TEX_GBUFFER_3, // {Tangent.xy, AnisoSpec, RoughDiffuse}.
TEX_LIGHT_ACC_AMBIENT,
TEX_LIGHT_ACC_DIFFUSE,
TEX_LIGHT_ACC_SPECULAR,
2020-06-01 18:25:41 +02:00
TEX_COMPOSED_A,
TEX_COMPOSED_B,
2022-04-01 18:23:39 +02:00
2020-05-01 20:50:55 +02:00
TEX_COUNT
2019-09-01 13:49:15 +02:00
};
2020-08-02 01:00:00 +02:00
static UB_PerFrame s_ubPerFrame;
static UB_TexturesFS s_ubTexturesFS;
static UB_PerMeshVS s_ubPerMeshVS;
static UB_ShadowFS s_ubShadowFS;
static UB_PerObject s_ubPerObject;
2021-02-01 18:22:02 +01:00
2022-04-01 18:23:39 +02:00
static UB_AmbientVS s_ubAmbientVS;
static UB_AmbientFS s_ubAmbientFS;
2021-02-01 18:22:02 +01:00
static UB_ComposeVS s_ubComposeVS;
static UB_ComposeFS s_ubComposeFS;
static UB_ReflectionVS s_ubReflectionVS;
static UB_ReflectionFS s_ubReflectionFS;
2020-08-02 01:00:00 +02:00
static UB_BakeSpritesVS s_ubBakeSpritesVS;
static UB_BakeSpritesFS s_ubBakeSpritesFS;
2019-10-02 19:35:07 +02:00
2022-04-01 18:23:39 +02:00
Vector4 _backgroundColor = Vector4(0);
2020-05-01 20:50:55 +02:00
ShaderPwns<SHADER_COUNT> _shader;
PipelinePwns<PIPE_COUNT> _pipe;
TexturePwns<TEX_COUNT> _tex;
2022-04-01 18:23:39 +02:00
TexturePtr _texAtmoShadow;
TexturePtr _texTerrainHeightmap;
TexturePtr _texTerrainBlend;
2020-05-01 20:50:55 +02:00
UINT64 _frame = 0;
2021-02-01 18:22:02 +01:00
2020-05-01 20:50:55 +02:00
RPHandle _rph;
RPHandle _rphCompose;
2022-04-01 18:23:39 +02:00
RPHandle _rphForwardRendering;
2021-02-01 18:22:02 +01:00
RPHandle _rphReflection;
2020-08-02 01:00:00 +02:00
RPHandle _rphBakeSprites;
2021-02-01 18:22:02 +01:00
2020-05-01 20:50:55 +02:00
FBHandle _fbh;
FBHandle _fbhCompose;
2022-04-01 18:23:39 +02:00
FBHandle _fbhForwardRendering;
2021-02-01 18:22:02 +01:00
FBHandle _fbhReflection;
2020-08-02 01:00:00 +02:00
FBHandle _fbhBakeSprites;
2021-02-01 18:22:02 +01:00
2020-05-01 20:50:55 +02:00
CSHandle _cshLight;
2022-04-01 18:23:39 +02:00
CSHandle _cshAmbient;
2020-05-01 20:50:55 +02:00
CSHandle _cshCompose;
2021-02-01 18:22:02 +01:00
CSHandle _cshReflection;
2020-05-01 20:50:55 +02:00
CSHandle _cshToneMapping;
2022-04-01 18:23:39 +02:00
CSHandle _cshQuad[7];
2020-08-02 01:00:00 +02:00
CSHandle _cshBakeSprites;
2021-02-01 18:22:02 +01:00
2022-04-01 18:23:39 +02:00
int _terrainMapSide = 1;
2020-05-01 20:50:55 +02:00
bool _activeGeometryPass = false;
bool _activeLightingPass = false;
2022-04-01 18:23:39 +02:00
bool _activeForwardRendering = false;
2020-05-01 20:50:55 +02:00
bool _async_initPipe = false;
2019-09-01 13:49:15 +02:00
public:
DeferredShading();
~DeferredShading();
void Init();
void InitGBuffers(int w, int h);
2019-11-03 09:31:18 +01:00
void InitByAtmosphere(TexturePtr texShadow);
2020-07-01 21:14:17 +02:00
void InitByBloom(TexturePtr tex);
2022-04-01 18:23:39 +02:00
void InitByTerrain(TexturePtr texHeightmap, TexturePtr texBlend, int mapSide);
2019-09-01 13:49:15 +02:00
void Done();
2019-12-01 00:09:41 +01:00
void OnSwapChainResized(bool init, bool done);
2019-10-02 19:35:07 +02:00
static bool IsLoaded();
2020-08-02 01:00:00 +02:00
void ResetInstanceCount();
2022-04-01 18:23:39 +02:00
void Draw(int gbuffer,
RcVector4 rMultiplexer = Vector4(1, 0, 0, 0),
RcVector4 gMultiplexer = Vector4(0, 1, 0, 0),
RcVector4 bMultiplexer = Vector4(0, 0, 1, 0),
RcVector4 aMultiplexer = Vector4(0, 0, 0, 1));
2019-09-01 13:49:15 +02:00
2020-05-01 20:50:55 +02:00
RPHandle GetRenderPassHandle() const { return _rph; }
2022-04-01 18:23:39 +02:00
RPHandle GetRenderPassHandle_ForwardRendering() const { return _rphForwardRendering; }
2019-10-02 19:35:07 +02:00
2022-04-01 18:23:39 +02:00
// Steps:
void BeginGeometryPass();
void EndGeometryPass();
bool BeginLightingPass(bool ambient = true, bool terrainOcclusion = true);
2019-09-01 13:49:15 +02:00
void EndLightingPass();
2022-04-01 18:23:39 +02:00
void BeginComposeAndForwardRendering(bool underwaterMask = true);
void EndComposeAndForwardRendering();
2021-02-01 18:22:02 +01:00
void DrawReflection();
2020-09-02 13:21:05 +02:00
void ToneMapping();
2022-04-01 18:23:39 +02:00
bool IsActiveGeometryPass() const { return _activeGeometryPass; }
bool IsActiveLightingPass() const { return _activeLightingPass; }
bool IsActiveForwardRendering() const { return _activeForwardRendering; }
2019-09-01 13:49:15 +02:00
2022-04-01 18:23:39 +02:00
// Lighting:
2019-09-01 13:49:15 +02:00
static bool IsLightUrl(CSZ url);
2020-08-02 01:00:00 +02:00
void OnNewLightType(CommandBufferPtr cb, LightType type, bool wireframe = false);
void BindDescriptorsPerMeshVS(CommandBufferPtr cb);
2019-11-03 09:31:18 +01:00
static UB_PerMeshVS& GetUbPerMeshVS() { return s_ubPerMeshVS; }
2020-08-02 01:00:00 +02:00
2019-09-01 13:49:15 +02:00
void Load();
TexturePtr GetGBuffer(int index);
2022-04-01 18:23:39 +02:00
TexturePtr GetLightAccAmbientTexture();
TexturePtr GetLightAccDiffuseTexture();
TexturePtr GetLightAccSpecularTexture();
2020-06-01 18:25:41 +02:00
TexturePtr GetComposedTextureA();
TexturePtr GetComposedTextureB();
2020-08-02 01:00:00 +02:00
2022-04-01 18:23:39 +02:00
static Vector4 GetClearValueForGBuffer0(float albedo = 0);
static Vector4 GetClearValueForGBuffer1(float normal = 0, float motionBlur = 1);
static Vector4 GetClearValueForGBuffer2(float roughness = 0);
static Vector4 GetClearValueForGBuffer3(float tangent = 0);
2020-08-02 01:00:00 +02:00
2022-04-01 18:23:39 +02:00
void SetBackgroundColor(RcVector4 backgroundColor) { _backgroundColor = backgroundColor; }
2021-01-03 00:37:27 +01:00
2022-04-01 18:23:39 +02:00
void BakeSprites(TexturePtr texGBufferIn[4], TexturePtr texGBufferOut[4], PBaseCommandBuffer pCB = nullptr);
void BakeSpritesCleanup();
2019-09-01 13:49:15 +02:00
};
VERUS_TYPEDEFS(DeferredShading);
}
}