RavEngine
Loading...
Searching...
No Matches
RenderEngine.hpp
1#pragma once
2//
3// RenderEngine.hpp
4// RavEngine_Static
5//
6// Copyright © 2020 Ravbug.
7//
8#if !RVE_SERVER
9#include "Ref.hpp"
10#include <RmlUi/Core/SystemInterface.h>
11#include <RmlUi/Core/RenderInterface.h>
12#include <DebugDraw.h>
13#include "Common3D.hpp"
14#include "Defines.hpp"
15#include "PhysXDefines.h"
16#include <RGL/Types.hpp>
17#include <RGL/TextureFormat.hpp>
18#include <RGL/Buffer.hpp>
19#include <span>
20#include "SpinLock.hpp"
21#include "MeshAllocation.hpp"
22#include "RenderTargetCollection.hpp"
23#include "PostProcess.hpp"
24#include <unordered_set>
25#include "cluster_defs.h"
26#include "Queue.hpp"
27#include "Layer.hpp"
28#include "Mesh.hpp"
29
30struct SDL_Window;
31
32namespace Im3d {
33 struct DrawList;
34};
35
36namespace RavEngine {
37
38 struct AppConfig;
39 class DeferredBlitShader;
40 class GUIMaterialInstance;
41 class InputManager;
42 struct Entity;
43 class World;
44 struct MeshAsset;
45 struct GUIComponent;
46 struct DummyTonemapInstance;
47
48 namespace Clustered {
49 constexpr static uint32_t gridSizeX = 12;
50 constexpr static uint32_t gridSizeY = 12;
51 constexpr static uint32_t gridSizeZ = 24;
52 constexpr static uint32_t numClusters = gridSizeX * gridSizeY * gridSizeZ;
53
54 struct Cluster {
55 glm::vec3 minPoint;
56 glm::vec3 maxPoint;
57 uint32_t pointLightCount;
58 uint32_t spotLightCount;
59 uint32_t pointLightIndices[CLUSTER_MAX_POINTS];
60 uint32_t spotLightIndices[CLUSTER_MAX_SPOTS];
61 };
62 }
63
64 class RenderEngine : public Rml::SystemInterface, public Rml::RenderInterface, public duDebugDraw {
65 friend class App;
66 private:
67 uint64_t frameCount = 0;
68 decltype(frameCount) lastResizeFrameVB = 0, lastResizeFrameIB = 0;
69
70 using dim = dim_t<uint32_t>;
71
72 RGLDevicePtr device;
73 RGLCommandQueuePtr mainCommandQueue;
74 RGLCommandBufferPtr mainCommandBuffer, transformSyncCommandBuffer, transientCommandBuffer;
75 bool transientSubmittedLastFrame = false;
76
77 RGLTexturePtr dummyShadowmap, dummyCubemap;
78 RGLSamplerPtr textureSampler, textureClampSampler, shadowSampler, depthPyramidSampler;
79 RGLRenderPassPtr litRenderPass, unlitRenderPass, envSkyboxPass, depthPrepassRenderPass, depthPrepassRenderPassLit, postProcessRenderPass, postProcessRenderPassClear, finalRenderPass, finalRenderPassNoDepth, shadowRenderPass, lightingClearRenderPass, litClearRenderPass, finalClearRenderPass, depthPyramidCopyPass, litTransparentPass, unlitTransparentPass, transparentClearPass, transparencyApplyPass, ssaoPassClear, ssgiPassNoClear, ssgiAmbientApplyPass, ssgiPassClear;
80
81 RGLRenderPipelinePtr depthPyramidCopyPipeline,
82 im3dLineRenderPipeline, im3dPointRenderPipeline, im3dTriangleRenderPipeline, recastLinePipeline, recastPointPipeline, recastTrianglePipeline, guiRenderPipeline, transparencyApplyPipeline, ssgipipeline, gtaoPipeline, ssaoPipeline, ssgiDownsamplePipeline, ssgiUpsamplePipeline, ambientSSGIApplyPipeline, aoUpsamplePipeline, ssgiUpsamplePipleineFinalStep, environmentPreFilterPipeline, environmentIrradiancePipeline;
83 RGLComputePipelinePtr skinnedMeshComputePipeline, defaultCullingComputePipeline, skinningDrawCallPreparePipeline, depthPyramidPipeline, particleCreatePipeline, particleDispatchSetupPipeline, particleDispatchSetupPipelineIndexed, particleKillPipeline, clusterBuildGridPipeline, clusterPopulatePipeline, blurKernelX, blurKernelY;
84 RGLBufferPtr screenTriVerts,
85 sharedPositionBuffer,sharedNormalBuffer, sharedTangentBuffer, sharedBitangentBuffer, sharedUV0Buffer, sharedLightmapUVBuffer, sharedIndexBuffer, sharedSkeletonMatrixBuffer, sharedSkinnedPositionBuffer, sharedSkinnedNormalBuffer, sharedSkinnedTangentBuffer, sharedSkinnedBitangentBuffer, sharedSkinnedUV0Buffer, quadVertBuffer, lightClusterBuffer, debugRenderBufferUpload;
86 uint32_t debugRenderBufferSize = 0, debugRenderBufferOffset = 0;
87
88 constexpr static uint32_t initialVerts = 1024, initialIndices = 1536;
89
90 struct PyramidCopyUBO {
91 uint32_t size;
92 };
93
94 Ref<DummyTonemapInstance> dummyTonemap;
95
96 enum DefaultCullBindings : uint32_t{
97 Cubo = 0,
98 modelMatrix = 1,
99 renderLayer = 2,
100 perObject = 7,
101 depthPyramid = 8,
102 depthPyramidSamplerBinding = 9,
103 };
104
105#pragma pack(push, 1)
106 struct GridBuildUBO {
107 glm::mat4 invProj{ 0 };
108 glm::uvec3 gridSize{ 0 };
109 float zNear = 0;
110 glm::uvec2 screenDim{ 0 };
111 float zFar = 0;
112 };
113#pragma pack(pop)
114
115 struct GridAssignUBO {
116 glm::mat4 viewMat;
117 uint32_t pointLightCount, spotLightCount;
118 };
119
120 struct DownsampleUBO {
121 glm::uvec4 targetDim;
122 };
123
124 struct UpsampleUBO {
125 glm::uvec4 targetDim;
126 float filterRadius;
127 };
128
129 struct SkyboxEnvPrefilterUBO {
130 float roughness;
131 };
132
133 struct AmbientSSGIApplyUBO {
134 glm::mat4 invView{ 1 };
135 uint32_t ambientLightCount = 0;
136 float ssaoStrength = 1;
137 uint32_t options = 0;
138 static constexpr uint32_t SSAOBIT = 1, SSGIBIT = 1 << 1;
139 };
140
141 RGLShaderLibraryPtr defaultPostEffectVSH;
142
143 friend class Material;
144 public:
145 static constexpr RGL::TextureFormat depthPyramidFormat = RGL::TextureFormat::R32_Float;
146 constexpr static uint32_t maxssgimips = 5;
147
148 auto GetDefaultPostEffectVSH() const{
149 return defaultPostEffectVSH;
150 }
151
152 constexpr static RGL::TextureFormat
153 colorTexFormat = RGL::TextureFormat::RGBA16_Sfloat,
154 radianceFormat = RGL::TextureFormat::RGBA16_Sfloat,
155 albedoFormat = RGL::TextureFormat::RGBA16_Sfloat,
156 viewSpaceNormalTextureFormat = RGL::TextureFormat::RGBA16_Sfloat,
157 ssgiOutputFormat = RGL::TextureFormat::RGBA16_Sfloat,
158 ssaoOutputFormat = RGL::TextureFormat::R16_Float,
159 depthFormat = RGL::TextureFormat::D32SFloat;
160
161 struct navDebugUBO {
162 glm::mat4 model;
163 glm::mat4 viewProj;
164 } currentNavState;
165
167 glm::ivec4 viewRect;
168 };
169
170 struct GUIUBO {
171 glm::mat4 viewProj;
172 };
173
174 struct DebugUBO {
175 glm::mat4 viewProj;
176 };
177
178 struct SkinningUBO {
179 uint32_t numObjects = 0;
180 uint32_t numVertices = 0;
181 uint32_t numBones = 0;
182 uint32_t boneReadOffset = 0;
183 uint32_t vertexWriteOffset = 0;
184 uint32_t vertexReadOffset = 0;
185 };
186
187 struct SSGIUBO {
188 glm::mat4 projection;
189 glm::mat4 invProj;
190 glm::ivec2 outputDim;
191 float sampleCount;
192 float sampleRadius;
193 float sliceCount;
194 float hitThickness;
195 };
196
197 struct GTAOUBO {
198 glm::ivec2 screenDim;
199 };
200
201 struct SSAOUBO {
202 glm::mat4 proj;
203 glm::mat4 invProj;
204 glm::ivec2 screenDim;
205 };
206
208 uint32_t joint_index;
209 float influence;
210 };
211
213 uint32_t indirectBufferOffset = 0; // needs to be like this because of padding / alignment
214 uint32_t numObjects = 0;
215 uint32_t cullingBufferOffset = 0;
216 float radius = 0;
217 uint32_t numLODs = 0;
218 uint32_t idOutputBufferBindlessHandle = 0;
219 uint32_t entityIDInputBufferBindlessHandle = 0;
220 uint32_t indirectOutputBufferBindlessHandle = 0;
221 uint32_t lodDistanceBufferBindlessHandle = 0;
222 };
223
224 struct CullingUBO {
225 glm::mat4 viewProj;
226 glm::vec3 camPos;
227 uint32_t numCubos;
228 renderlayer_t cameraRenderLayers = 0;
229 uint32_t singleInstanceModeAndShadowMode = 0; // skinning vs not skinning
230 };
231
233 uint32_t indexBufferOffset = 0;
234 uint32_t vertexBufferOffset = 0;
235 uint32_t nIndicesInThisMesh = 0;
236 uint32_t nVerticesInThisMesh = 0;
237 uint32_t nTotalObjects = 0;
238 uint32_t drawCallBufferOffset = 0;
239 uint32_t baseInstanceOffset = 0;
240 };
241
243 uint32_t particlesToSpawn;
244 uint32_t maxParticles;
245 };
246
248 uint32_t maxTotalParticles;
249 };
250
251 virtual ~RenderEngine();
252 RenderEngine(const AppConfig&, RGLDevicePtr device);
253
254 RenderTargetCollection CreateRenderTargetCollection(dim size, bool createDepth = true);
255 void ResizeRenderTargetCollection(RenderTargetCollection& collection, dim size);
256
257 //render a world, for internal use only
258 RGLCommandBufferPtr Draw(Ref<RavEngine::World>, const std::span<RenderViewCollection> screenTargets, float guiScaleFactor);
259
263 static const std::string_view GetCurrentBackendName();
264
265 enum class AOMethod : uint8_t {
266 SSAO,
267 GTAO
268 };
269
270 static struct vs {
271 bool vsync = true;
272 AOMethod aoMethod = AOMethod::SSAO;
273 } VideoSettings;
274
275 void SyncVideoSettings();
276
277 // Rml::SystemInterface overrides, used internally
278 double GetElapsedTime() override;
279 void SetMouseCursor(const Rml::String& cursor_name) override;
280 void SetClipboardText(const Rml::String& text) override;
281 void GetClipboardText(Rml::String& text) override;
282
284 void RenderGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f& translation) override;
285
287 Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rml::TextureHandle texture) override;
288
290 void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f& translation) override;
292 void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override;
293
295 void EnableScissorRegion(bool enable) override;
297 void SetScissorRegion(int x, int y, int width, int height) override;
298
300 bool LoadTexture(Rml::TextureHandle& texture_handle, Rml::Vector2i& texture_dimensions, const Rml::String& source) override;
302 bool GenerateTexture(Rml::TextureHandle& texture_handle, const Rml::byte* source, const Rml::Vector2i& source_dimensions) override;
304 void ReleaseTexture(Rml::TextureHandle texture_handle) override;
305
307 void SetTransform(const Rml::Matrix4f* transform) override;
308
309 bool LogMessage(Rml::Log::Type type, const Rml::String& message) override;
310
311#ifndef NDEBUG
312 static std::optional<GUIComponent> debuggerContext;
313 void InitDebugger() const;
314 void DeactivateDebugger() const;
315 static std::unique_ptr<InputManager> debuggerInput;
316#endif
317 // navigation debug rendering -- internal use only
318 void depthMask(bool state) final;
319 void texture(bool state) final;
320 void begin(duDebugDrawPrimitives prim, float size = 1.0f) final;
321 void vertex(const float* pos, unsigned int color) final;
322 void vertex(const float x, const float y, const float z, unsigned int color) final;
323 void vertex(const float* pos, unsigned int color, const float* uv) final;
324 void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v) final;
325 void end() final;
326
327 void DebugRender(const Im3d::DrawList&);
328
329 size_t GetCurrentVRAMUse();
330
331 size_t GetTotalVRAM();
332
333 ConcurrentQueue<RGLBufferPtr> gcBuffers;
334 ConcurrentQueue<RGLTexturePtr> gcTextures;
335 ConcurrentQueue<RGLPipelineLayoutPtr> gcPipelineLayout;
336 ConcurrentQueue<RGLRenderPipelinePtr> gcRenderPipeline;
337
338 MeshRange AllocateMesh(const MeshPartView& mesh);
339
340 void DeallocateMesh(const MeshRange& range);
341
342 protected:
343 dim_t<int> currentRenderSize;
344 matrix4 make_gui_matrix(Rml::Vector2f translation);
345
346 constexpr static uint32_t transientSizeBytes = 65536;
347 RGLBufferPtr transientBuffer, transientStagingBuffer;
348 uint32_t transientOffset = 0;
349
355 uint32_t WriteTransient(RGL::untyped_span data);
356
357 allocation_freelist_t vertexFreeList{ 1, Range{.start = 0, .count = initialVerts}};
358 allocation_freelist_t indexFreeList{ 1, Range{.start = 0, .count = initialIndices}};
359 allocation_allocatedlist_t vertexAllocatedList, indexAllocatedList;
360 uint32_t currentVertexSize = initialVerts, currentIndexSize = initialIndices;
361
362 void ReallocateVertexAllocationToSize(uint32_t newSize);
363 void ReallocateIndexAllocationToSize(uint32_t newSize);
364 void ReallocateGeneric(RGLBufferPtr& reallocBuffer, uint32_t& reallocBufferSize, uint32_t newSize, allocation_allocatedlist_t& allocatedList, allocation_freelist_t& freelist, uint32_t stride, RGL::BufferConfig::Type bufferType, decltype(frameCount)& lastResizeFrame, const char* debugName = nullptr);
365
366 SpinLock allocationLock;
367
368 void DestroyUnusedResources();
369 static RavEngine::Vector<VertexColorUV> navMeshPolygon;
370 bool navDebugDepthEnabled = false;
371
372 public:
373
374 protected:
375
376 static Ref<GUIMaterialInstance> guiMaterial;
377
378 matrix4 currentGUIMatrix;
379
380 struct scissor{
381 uint16_t x, y, width, height;
382 bool enabled = false;
383 } RMLScissor;
384
385 };
386}
387#endif
Definition App.hpp:43
Definition Material.hpp:45
Definition RenderEngine.hpp:64
bool GenerateTexture(Rml::TextureHandle &texture_handle, const Rml::byte *source, const Rml::Vector2i &source_dimensions) override
Called by RmlUi when a texture is required to be built from an internally-generated sequence of pixel...
Definition RenderEngine_RML.cpp:270
void SetScissorRegion(int x, int y, int width, int height) override
Called by RmlUi when it wants to change the scissor region.
Definition RenderEngine_RML.cpp:241
void ReleaseCompiledGeometry(Rml::CompiledGeometryHandle geometry) override
Called by RmlUi when it wants to release application-compiled geometry.
Definition RenderEngine_RML.cpp:230
void EnableScissorRegion(bool enable) override
Called by RmlUi when it wants to enable or disable scissoring to clip content.
Definition RenderEngine_RML.cpp:237
uint32_t WriteTransient(RGL::untyped_span data)
Definition RenderEngine_Allocate.cpp:150
void SetTransform(const Rml::Matrix4f *transform) override
Called by RmlUi when it wants to set the current transform matrix to a new matrix.
Definition RenderEngine_RML.cpp:284
MeshRange AllocateMesh(const MeshPartView &mesh)
Definition RenderEngine_Allocate.cpp:12
static const std::string_view GetCurrentBackendName()
Definition RenderEngine.cpp:2381
void begin(duDebugDrawPrimitives prim, float size=1.0f) final
Definition RenderEngine_NavDbg.cpp:23
bool LoadTexture(Rml::TextureHandle &texture_handle, Rml::Vector2i &texture_dimensions, const Rml::String &source) override
Called by RmlUi when a texture is required by the library.
Definition RenderEngine_RML.cpp:249
RGLCommandBufferPtr Draw(Ref< RavEngine::World >, const std::span< RenderViewCollection > screenTargets, float guiScaleFactor)
Definition RenderEngine_Draw.cpp:98
Rml::CompiledGeometryHandle CompileGeometry(Rml::Vertex *vertices, int num_vertices, int *indices, int num_indices, Rml::TextureHandle texture) override
Called by RmlUi when it wants to compile geometry it believes will be static for the forseeable futur...
Definition RenderEngine_RML.cpp:146
void RenderCompiledGeometry(Rml::CompiledGeometryHandle geometry, const Rml::Vector2f &translation) override
Called by RmlUi when it wants to render application-compiled geometry.
Definition RenderEngine_RML.cpp:203
void ReleaseTexture(Rml::TextureHandle texture_handle) override
Called by RmlUi when a loaded texture is no longer required.
Definition RenderEngine_RML.cpp:277
void vertex(const float *pos, unsigned int color) final
Definition RenderEngine_NavDbg.cpp:43
void end() final
End drawing primitives.
Definition RenderEngine_NavDbg.cpp:71
void RenderGeometry(Rml::Vertex *vertices, int num_vertices, int *indices, int num_indices, Rml::TextureHandle texture, const Rml::Vector2f &translation) override
Called by RmlUi when it wants to render geometry that it does not wish to optimise.
Definition RenderEngine_RML.cpp:99
Definition concurrentqueue.h:747
uint32 uint32_t
Definition fwd.hpp:131
Definition Animation.hpp:6
Definition Buffer.hpp:25
Definition Common3D.hpp:137
Definition App.hpp:25
Definition RenderEngine.hpp:54
Definition RenderEngine.hpp:224
Definition RenderEngine.hpp:212
Definition RenderEngine.hpp:174
Definition RenderEngine.hpp:197
Definition RenderEngine.hpp:170
Definition RenderEngine.hpp:207
Definition RenderEngine.hpp:247
Definition RenderEngine.hpp:166
Definition RenderEngine.hpp:201
Definition RenderEngine.hpp:187
Definition RenderEngine.hpp:232
Definition RenderEngine.hpp:178
Definition RenderEngine.hpp:161
Definition RenderEngine.hpp:380
Definition RenderTargetCollection.hpp:19
Definition mathtypes.hpp:128
Abstract debug draw interface.
Definition DebugDraw.h:35
Definition qualifier.hpp:36
Definition qualifier.hpp:35