Grindstone Game Engine v0.2.0
An open source game engine and toolkit.
Loading...
Searching...
No Matches
DeferredRenderer.hpp
1#pragma once
2
3#include <vector>
4#include <glm/glm.hpp>
5#include <Common/ResourcePipeline/Uuid.hpp>
6#include "BaseRenderer.hpp"
7
8namespace Grindstone {
9 namespace GraphicsAPI {
10 class UniformBuffer;
11 class Framebuffer;
12 class RenderTarget;
13 class DepthStencilTarget;
14 class VertexArrayObject;
15 class GraphicsPipeline;
16 class ComputePipeline;
17 class CommandBuffer;
18 };
19
21 public:
23 virtual ~DeferredRenderer();
24 virtual bool OnWindowResize(Events::BaseEvent*) override;
25 virtual void Resize(uint32_t width, uint32_t height) override;
26 virtual void Render(
27 GraphicsAPI::CommandBuffer* commandBuffer,
28 entt::registry& registry,
29 glm::mat4 projectionMatrix,
30 glm::mat4 viewMatrix,
31 glm::vec3 eyePos,
32 GraphicsAPI::Framebuffer* outputFramebuffer
33 ) override;
34
35 virtual uint16_t GetRenderModeCount() const override;
36 virtual const RenderMode* GetRenderModes() const override;
37 virtual void SetRenderMode(uint16_t mode) override;
38
39 static GraphicsAPI::RenderPass* gbufferRenderPass;
40 static GraphicsAPI::RenderPass* mainRenderPass;
41
42 enum class DeferredRenderMode : uint16_t {
43 Default,
44 Position,
45 PositionMod,
46 ViewPosition,
47 ViewPositionMod,
48 Depth,
49 DepthMod,
50 Normal,
51 ViewNormal,
52 Albedo,
53 Specular,
54 Roughness,
55 AmbientOcclusion,
56 Count
57 };
58
59 private:
60 struct DeferredRendererImageSet {
61 GraphicsAPI::Framebuffer* gbuffer = nullptr;
62 GraphicsAPI::DepthStencilTarget* gbufferDepthStencilTarget = nullptr;
63 GraphicsAPI::Framebuffer* litHdrFramebuffer = nullptr;
64 GraphicsAPI::Framebuffer* lightingFramebuffer = nullptr;
65 GraphicsAPI::RenderTarget* litHdrRenderTarget = nullptr;
66 GraphicsAPI::RenderTarget* ssrRenderTarget = nullptr;
67 GraphicsAPI::RenderTarget* gbufferAlbedoRenderTarget = nullptr;
68 GraphicsAPI::RenderTarget* gbufferNormalRenderTarget = nullptr;
69 GraphicsAPI::RenderTarget* gbufferSpecularRoughnessRenderTarget = nullptr;
70
71 GraphicsAPI::Framebuffer* ambientOcclusionFramebuffer = nullptr;
72 GraphicsAPI::RenderTarget* ambientOcclusionRenderTarget = nullptr;
73
74 GraphicsAPI::UniformBuffer* globalUniformBufferObject = nullptr;
75 GraphicsAPI::UniformBuffer* debugUniformBufferObject = nullptr;
76 GraphicsAPI::UniformBuffer* tonemapPostProcessingUniformBufferObject = nullptr;
77
78 GraphicsAPI::DescriptorSet* ssrDescriptorSet = nullptr;
79 GraphicsAPI::DescriptorSet* tonemapDescriptorSet = nullptr;
80 GraphicsAPI::DescriptorSet* lightingDescriptorSet = nullptr;
81 GraphicsAPI::DescriptorSet* debugDescriptorSet = nullptr;
82 GraphicsAPI::DescriptorSet* engineDescriptorSet = nullptr;
83
84 GraphicsAPI::DescriptorSet* dofSourceDescriptorSet = nullptr;
85 GraphicsAPI::DescriptorSet* dofNearBlurDescriptorSet = nullptr;
86 GraphicsAPI::DescriptorSet* dofFarBlurDescriptorSet = nullptr;
87 GraphicsAPI::DescriptorSet* dofCombineDescriptorSet = nullptr;
88
89 GraphicsAPI::DescriptorSet* ambientOcclusionDescriptorSet = nullptr;
90
91 GraphicsAPI::RenderTarget* nearDofRenderTarget = nullptr;
92 GraphicsAPI::RenderTarget* farDofRenderTarget = nullptr;
93 GraphicsAPI::RenderTarget* nearBlurredDofRenderTarget = nullptr;
94 GraphicsAPI::RenderTarget* farBlurredDofRenderTarget = nullptr;
95
96 GraphicsAPI::Framebuffer* dofSeparationFramebuffer = nullptr;
97 GraphicsAPI::Framebuffer* dofNearBlurFramebuffer = nullptr;
98 GraphicsAPI::Framebuffer* dofFarBlurFramebuffer = nullptr;
99 GraphicsAPI::Framebuffer* dofCombinationFramebuffer = nullptr;
100
101 std::vector<GraphicsAPI::RenderTarget*> bloomRenderTargets;
102 std::vector<GraphicsAPI::DescriptorSet*> bloomDescriptorSets;
103 };
104
105 std::map<Uuid, GraphicsAPI::GraphicsPipeline*> graphicsPipelineMap;
106
107 void CreatePipelines();
108 void CreateBloomUniformBuffers();
109 void UpdateBloomDescriptorSet(DeferredRendererImageSet& imageSet);
110 void CreateDepthOfFieldRenderTargetsAndDescriptorSets(DeferredRendererImageSet& imageSet, size_t imageSetIndex);
111 void CreateSsrRenderTargetsAndDescriptorSets(DeferredRendererImageSet& imageSet, size_t imageSetIndex);
112 void CreateBloomRenderTargetsAndDescriptorSets(DeferredRendererImageSet& imageSet, size_t imageSetIndex);
113 void RenderDepthOfField(DeferredRendererImageSet& imageSet, GraphicsAPI::CommandBuffer* currentCommandBuffer);
114 void RenderSsr(DeferredRendererImageSet& imageSet, GraphicsAPI::CommandBuffer* currentCommandBuffer);
115 void RenderBloom(DeferredRendererImageSet& imageSet, GraphicsAPI::CommandBuffer* currentCommandBuffer);
116 void UpdateBloomUBO();
117
118 void RenderSsao(DeferredRendererImageSet& imageSet, GraphicsAPI::CommandBuffer* commandBuffer);
119 void RenderShadowMaps(GraphicsAPI::CommandBuffer* commandBuffer, entt::registry& registry);
120 void RenderLights(uint32_t imageIndex, GraphicsAPI::CommandBuffer* currentCommandBuffer, entt::registry& registry);
121 void PostProcess(uint32_t imageIndex, GraphicsAPI::Framebuffer* framebuffer, GraphicsAPI::CommandBuffer* currentCommandBuffer);
122 void Debug(uint32_t imageIndex, GraphicsAPI::Framebuffer* outputFramebuffer, GraphicsAPI::CommandBuffer* commandBuffer);
123
124 void CreateDepthOfFieldResources();
125 void CreateBloomResources();
126 void CreateSSRResources();
127 void CreateSsaoKernelAndNoise();
128 void CleanupPipelines();
129 void CreateDescriptorSetLayouts();
130 void CreateGbufferFramebuffer();
131 void CreateLitHDRFramebuffer();
132 void CreateDescriptorSets(DeferredRendererImageSet& imageSet);
133 void UpdateDescriptorSets(DeferredRendererImageSet& imageSet);
134 void CreateUniformBuffers();
135 void CreateVertexAndIndexBuffersAndLayouts();
136
137 struct PostProcessUbo {
138 glm::vec4 vignetteColor = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
139 float vignetteRadius = 0.75f;
140 float vignetteSoftness = 0.8f;
141 float grainAmount = 0.0007f;
142 float grainPixelSize = 1.0f;
143 glm::vec2 chromaticDistortionRedOffset = glm::vec2(0.00045f, 0.00045f);
144 glm::vec2 chromaticDistortionGreenOffset = glm::vec2(0.0003f, 0.0003f);
145 glm::vec2 chromaticDistortionBlueOffset = glm::vec2(-0.0003f, -0.0003f);
146 float paniniDistortionStrength = 0.0f;
147 bool isAnimated = true;
148 };
149
150 PostProcessUbo postProcessUboData;
151
152 struct DebugUboData {
153 uint16_t renderMode;
154 };
155
156 DebugUboData debugUboData;
157
158 uint32_t framebufferWidth = 0u;
159 uint32_t framebufferHeight = 0u;
160 uint32_t renderWidth = 0u;
161 uint32_t renderHeight = 0u;
162
163 size_t bloomStoredMipLevelCount = 0;
164 size_t bloomMipLevelCount = 0;
165 Grindstone::GraphicsAPI::Texture* brdfLut = nullptr;
166
167 std::vector<DeferredRendererImageSet> deferredRendererImageSets;
168 std::vector<GraphicsAPI::UniformBuffer*> bloomUniformBuffers;
169 size_t bloomFirstUpsampleIndex = 0;
170
171 GraphicsAPI::RenderPass* dofSeparationRenderPass = nullptr;
172 GraphicsAPI::RenderPass* dofBlurAndCombinationRenderPass = nullptr;
173
174 GraphicsAPI::RenderPass* lightingRenderPass = nullptr;
175 GraphicsAPI::RenderPass* forwardLitRenderPass = nullptr;
176 GraphicsAPI::RenderPass* ssaoRenderPass = nullptr;
177 GraphicsAPI::UniformBuffer* ssaoUniformBuffer = nullptr;
178 GraphicsAPI::Texture* ssaoNoiseTexture = nullptr;
179 GraphicsAPI::DescriptorSetLayout* ambientOcclusionDescriptorSetLayout = nullptr;
180 GraphicsAPI::DescriptorSetLayout* ssaoInputDescriptorSetLayout = nullptr;
181 GraphicsAPI::DescriptorSet* ssaoInputDescriptorSet = nullptr;
182
183 GraphicsAPI::DescriptorSetLayout* environmentMapDescriptorSetLayout = nullptr;
184 GraphicsAPI::DescriptorSet* environmentMapDescriptorSet = nullptr;
185
186 GraphicsAPI::VertexBufferLayout vertexLightPositionLayout{};
187
188 GraphicsAPI::DescriptorSetLayout* bloomDescriptorSetLayout = nullptr;
189 GraphicsAPI::DescriptorSetLayout* ssrDescriptorSetLayout = nullptr;
190 GraphicsAPI::DescriptorSetLayout* tonemapDescriptorSetLayout = nullptr;
191 GraphicsAPI::DescriptorSetLayout* debugDescriptorSetLayout = nullptr;
192 GraphicsAPI::DescriptorSetLayout* lightingDescriptorSetLayout = nullptr;
193 GraphicsAPI::DescriptorSetLayout* lightingUBODescriptorSetLayout = nullptr;
194 GraphicsAPI::DescriptorSetLayout* shadowMappedLightDescriptorSetLayout = nullptr;
195 GraphicsAPI::DescriptorSetLayout* lightingWithShadowUBODescriptorSetLayout = nullptr;
196 GraphicsAPI::DescriptorSetLayout* engineDescriptorSetLayout = nullptr;
197 GraphicsAPI::DescriptorSetLayout* shadowMapDescriptorSetLayout = nullptr;
198
199 GraphicsAPI::DescriptorSet* shadowMapDescriptorSet = nullptr;
200
201 GraphicsAPI::RenderPass* shadowMapRenderPass = nullptr;
202 GraphicsAPI::RenderPass* targetRenderPass = nullptr;
203
204 GraphicsAPI::VertexBuffer* vertexBuffer;
205 GraphicsAPI::IndexBuffer* indexBuffer;
206 GraphicsAPI::VertexArrayObject* planePostProcessVao = nullptr;
207
208 GraphicsAPI::GraphicsPipeline* ssaoPipeline = nullptr;
209 GraphicsAPI::GraphicsPipeline* imageBasedLightingPipeline = nullptr;
210 GraphicsAPI::GraphicsPipeline* spotLightPipeline = nullptr;
211 GraphicsAPI::GraphicsPipeline* pointLightPipeline = nullptr;
212 GraphicsAPI::GraphicsPipeline* directionalLightPipeline = nullptr;
213 GraphicsAPI::GraphicsPipeline* tonemapPipeline = nullptr;
214 GraphicsAPI::GraphicsPipeline* dofSeparationPipeline = nullptr;
215 GraphicsAPI::GraphicsPipeline* dofBlurPipeline = nullptr;
216 GraphicsAPI::GraphicsPipeline* dofCombinationPipeline = nullptr;
217 GraphicsAPI::GraphicsPipeline* shadowMappingPipeline = nullptr;
218 GraphicsAPI::GraphicsPipeline* debugPipeline = nullptr;
219
220 GraphicsAPI::DescriptorSetLayout* dofSourceDescriptorSetLayout = nullptr;
221 GraphicsAPI::DescriptorSetLayout* dofBlurDescriptorSetLayout = nullptr;
222 GraphicsAPI::DescriptorSetLayout* dofCombinationDescriptorSetLayout = nullptr;
223
224 GraphicsAPI::ComputePipeline* ssrPipeline = nullptr;
225 GraphicsAPI::ComputePipeline* bloomPipeline = nullptr;
226
227 static std::array<Grindstone::BaseRenderer::RenderMode, static_cast<uint16_t>(DeferredRenderMode::Count)> renderModes;
228
229 // Used to check when environment map changes, so we can update it
230 Uuid currentEnvironmentMapUuid;
231
232 DeferredRenderMode renderMode;
233 };
234}
Definition BaseRenderer.hpp:17
Definition DeferredRenderer.hpp:20
Definition CommandBuffer.hpp:21
Definition ComputePipeline.hpp:18
Definition DepthStencilTarget.hpp:15
Definition DescriptorSetLayout.hpp:11
Definition DescriptorSet.hpp:11
Definition Framebuffer.hpp:19
Definition GraphicsPipeline.hpp:18
Definition IndexBuffer.hpp:11
Definition RenderPass.hpp:27
Definition RenderTarget.hpp:12
Definition Texture.hpp:40
Definition UniformBuffer.hpp:10
Definition VertexArrayObject.hpp:10
Definition VertexBuffer.hpp:130
Definition Uuid.hpp:6
Definition BaseRenderer.hpp:19
Definition BaseEvent.hpp:7
Definition VertexBuffer.hpp:104