evan 2026.1.4
Evan
Loading...
Searching...
No Matches
Renderer.hpp
1/*
2** ETIB PROJECT, 2026
3** evan
4** File description:
5** Renderer
6*/
7
8#pragma once
9
10#include "evan/ADeviceBackend.hpp"
11#include "evan/DeviceContext.hpp"
12#include "evan/ASwapchainContext.hpp"
13
14#include "evan/Frame.hpp"
15
16#include "evan/FrameStats.hpp"
17
18#include "evan/RenderSettings.hpp"
19#include "evan/RessourceManager.hpp"
20
21#include "evan/GPUShader.hpp"
22#include "evan/GPUVertex.hpp"
23#include "evan/GPUMesh.hpp"
24
25#include "evan/RenderObject.hpp"
26
27#include <utility/graphic/scissor.hpp>
28#include <utility/graphic/view.hpp>
29
30#include <utility/logging/loggable.hpp>
31#include <utility/logging/default_logger.hpp>
32
33#include <fstream>
34#include <algorithm>
35#include <map>
36#include <memory>
37#include <optional>
38#include <cstdint>
39#include <map>
40#include <span>
41#include <vector>
42
43namespace evan
44{
61 class Renderer:
62 protected utility::logging::Loggable<Renderer,
63 utility::logging::DefaultLogger>
64 {
65 public:
99 Renderer(std::shared_ptr<DeviceContext> deviceContext,
100 VkRenderPass renderPass, VkSampleCountFlagBits msaaSamples,
101 std::size_t perFrameSlotCount,
102 std::shared_ptr<RessourceManager> ressourceManager);
103
104 ~Renderer();
105
127
138 size_t addObject(std::shared_ptr<RenderObject> object);
139
151 bool updateObject(std::shared_ptr<RenderObject> object,
152 size_t objectID);
153
160 bool removeObject(size_t objectID);
161
169 std::shared_ptr<RenderObject> getObject(size_t objectID) const;
170
201 Error drawFrame(const DeviceContext &deviceContext,
203
213 void setScissor(const utility::graphic::ScissorRect &rect);
214
218 void clearScissor(void);
219
230 [[nodiscard]] static VkRect2D
231 clampScissor(const utility::graphic::ScissorRect &rect,
233
251 [[nodiscard]] static bool
253 std::size_t viewCount) noexcept;
254
271 {
272 return _stats;
273 }
274
282 {
284 }
285
301
317 const std::vector<VkBuffer> getUniformBuffers() const;
318
335
346 void setCullingEnabled(bool enabled)
347 {
348 _cullingEnabled = enabled;
349 }
350
357 {
358 return _cullingEnabled;
359 }
360
370 {
371 _maxDrawDistance = maxDistance;
372 }
373
380 {
381 return _maxDrawDistance;
382 }
383
402 void setInstancingEnabled(bool enabled)
403 {
404 _mergeIdenticalRuns = enabled;
405 }
406
413 {
414 return _mergeIdenticalRuns;
415 }
416
427 void setIndirectDrawingEnabled(bool enabled)
428 {
429 _indirectDrawingEnabled = enabled;
430 }
431
438 {
439 return _indirectDrawingEnabled;
440 }
441
451 uint32_t shaderID = 0;
452 BlendMode blendMode = BlendMode::Opaque;
453
460 bool operator<(const PipelineVariant &other) const
461 {
462 if (shaderID != other.shaderID) {
463 return shaderID < other.shaderID;
464 }
465 return static_cast<int>(blendMode)
466 < static_cast<int>(other.blendMode);
467 }
468 };
469
487 void setShaderBlendMode(uint32_t shaderID, BlendMode blendMode);
488
496 [[nodiscard]] BlendMode getShaderBlendMode(uint32_t shaderID) const;
497
503 void setOpaqueSortMode(OpaqueSortMode mode)
504 {
505 _opaqueSortMode = mode;
506 }
507
513 [[nodiscard]] OpaqueSortMode getOpaqueSortMode() const
514 {
515 return _opaqueSortMode;
516 }
517
518 protected:
519 std::map<PipelineVariant, VkPipeline>
525
526 std::map<uint32_t, VkPipelineLayout>
539
550 std::vector<std::shared_ptr<Frame>> _frames;
551
558 std::shared_ptr<DeviceContext> _deviceContext;
559
570
579
583 float _timestampPeriod = 0.0f;
584
590
603
616
625 std::map<size_t, std::shared_ptr<RenderObject>> _objects;
626
631 size_t _nextObjectID = 1;
632
639 std::optional<utility::graphic::ScissorRect> _scissor;
640
641 private:
653 void updateUniformBuffer(const utility::graphic::ViewF &view,
654 std::size_t viewSlot);
655
665 void updateMultiviewUniformBuffer(
666 const std::vector<utility::graphic::ViewF> &views,
667 std::size_t viewSlot);
668
685 void recordCommandBuffer(
688 std::span<const utility::graphic::ViewF> views);
689
698 void reportStatsIfDue();
699
711 void readGpuTimestamps(const Frame &frame);
712
721 VkPipeline pipelineFor(uint32_t shaderID, BlendMode blendMode) const;
722
735 void createDescriptorSetLayout(VkDevice device);
736
750 void createGraphicsPipelines(VkDevice device, VkRenderPass renderPass,
751 VkSampleCountFlagBits msaaSamples);
752
767 void createDescriptorPool(VkDevice device, uint32_t materialCount);
768
783 std::vector<std::shared_ptr<GPUMesh>> getMeshes() const;
784
785 std::shared_ptr<RessourceManager>
786 _ressourceManager;
797
801 bool _cullingEnabled = true;
802
806 float _maxDrawDistance = 0.0f;
807
813 bool _mergeIdenticalRuns = false;
814
818 bool _indirectDrawingEnabled = false;
819
826 std::map<uint32_t, BlendMode> _shaderBlendModes;
827
831 OpaqueSortMode _opaqueSortMode = OpaqueSortMode::FrontToBack;
832 };
833} // namespace evan
Represents the context for managing the swapchain in the rendering system.
Represents the device context for the engine, managing Vulkan-related resources and operations.
Frame class representing a single frame of rendering in the Vulkan application.
Definition Frame.hpp:43
The Renderer class is responsible for managing the graphics pipeline, descriptor sets,...
Definition Renderer.hpp:64
void resetStats() noexcept
Starts a new statistics window.
Definition Renderer.hpp:281
std::shared_ptr< RenderObject > getObject(size_t objectID) const
Retrieves a render object from the renderer's registry.
bool isInstancingEnabled() const
Checks whether identical meshes are merged into one draw.
Definition Renderer.hpp:412
void destroy(VkDevice device)
Destroys the Renderer and releases associated resources.
BlendMode getShaderBlendMode(uint32_t shaderID) const
Retrieves the blend mode used for a shader.
size_t _nextObjectID
A counter to generate unique object IDs for the registered render objects. Incremented each time a ne...
Definition Renderer.hpp:631
const FrameStats & getStats() const noexcept
Retrieves the frame statistics of this renderer.
Definition Renderer.hpp:270
std::map< PipelineVariant, VkPipeline > _pipelines
Definition Renderer.hpp:520
std::shared_ptr< DeviceContext > _deviceContext
The device context used to create this renderer's resources.
Definition Renderer.hpp:558
Renderer(std::shared_ptr< DeviceContext > deviceContext, VkRenderPass renderPass, VkSampleCountFlagBits msaaSamples, std::size_t perFrameSlotCount, std::shared_ptr< RessourceManager > ressourceManager)
Constructs a Renderer object.
bool isIndirectDrawingEnabled() const
Checks whether indirect drawing is enabled.
Definition Renderer.hpp:437
Error drawFrame(const DeviceContext &deviceContext, ASwapchainContext &swapchainContext)
Draws a frame by recording command buffers and submitting them to the graphics queue.
void setIndirectDrawingEnabled(bool enabled)
Enables or disables indirect drawing.
Definition Renderer.hpp:427
static bool viewOwnsInFlightFence(std::size_t viewIndex, std::size_t viewCount) noexcept
Whether the view at viewIndex carries the in-flight fence for a submission group of viewCount views.
bool updateObject(std::shared_ptr< RenderObject > object, size_t objectID)
Updates a render object in the renderer's object registry.
float getMaxDrawDistance() const
Retrieves the maximum draw distance.
Definition Renderer.hpp:379
void clearScissor(void)
Clear the active scissor rectangle (restore the full extent).
FrameStats _stats
Frame statistics window (CPU phases, counters, GPU passes).
Definition Renderer.hpp:578
static VkRect2D clampScissor(const utility::graphic::ScissorRect &rect, VkExtent2D extent)
Clamp a scissor rectangle to a render target extent.
void setInstancingEnabled(bool enabled)
Enables or disables merging of identical meshes.
Definition Renderer.hpp:402
uint32_t _timestampValidBits
Number of valid bits in a GPU timestamp, cached at construction.
Definition Renderer.hpp:589
void setShaderBlendMode(uint32_t shaderID, BlendMode blendMode)
Overrides the blend mode used for a shader.
VkDescriptorPool getDescriptorPool() const
Retrieves the Vulkan descriptor pool associated with the Renderer.
std::map< uint32_t, VkPipelineLayout > _pipelineLayouts
Definition Renderer.hpp:527
bool removeObject(size_t objectID)
Removes a render object from the renderer's registry.
void setMaxDrawDistance(float maxDistance)
Sets the maximum draw distance for distance culling.
Definition Renderer.hpp:369
const std::vector< VkBuffer > getUniformBuffers() const
Retrieves the Vulkan descriptor set layout associated with the Renderer.
std::optional< utility::graphic::ScissorRect > _scissor
The active scissor rectangle, in framebuffer pixels.
Definition Renderer.hpp:639
VkDescriptorPool _descriptorPool
Vulkan descriptor pool used for allocating descriptor sets.
Definition Renderer.hpp:615
OpaqueSortMode getOpaqueSortMode() const
Retrieves the ordering strategy used for opaque draws.
Definition Renderer.hpp:513
void setOpaqueSortMode(OpaqueSortMode mode)
Sets the ordering strategy used for opaque draws.
Definition Renderer.hpp:503
std::vector< std::shared_ptr< Frame > > _frames
A collection of frames used for rendering.
Definition Renderer.hpp:550
uint32_t _currentFrameIndex
The index of the current frame being rendered.
Definition Renderer.hpp:569
VkDescriptorSetLayout _descriptorSetLayout
Vulkan descriptor set layout used for rendering operations.
Definition Renderer.hpp:602
void setScissor(const utility::graphic::ScissorRect &rect)
Set the scissor (clip) rectangle applied when recording.
std::map< size_t, std::shared_ptr< RenderObject > > _objects
The registry of render objects owned by the renderer.
Definition Renderer.hpp:625
void setCullingEnabled(bool enabled)
Enables or disables frustum and distance culling.
Definition Renderer.hpp:346
float _timestampPeriod
Nanoseconds per GPU timestamp tick, cached at construction.
Definition Renderer.hpp:583
size_t addObject(std::shared_ptr< RenderObject > object)
Registers a render object in the renderer's object registry.
bool isCullingEnabled() const
Checks whether culling is currently enabled.
Definition Renderer.hpp:356
VkDescriptorSetLayout getDescriptorSetLayout() const
Retrieves the Vulkan descriptor set layout associated with the Renderer.
Per-frame instrumentation data (CPU phases and counters).
void resetWindow() noexcept
Starts a new report window.
Identifies one graphics pipeline variant.
Definition Renderer.hpp:450
bool operator<(const PipelineVariant &other) const
Orders variants so they can be stored in a std::map.
Definition Renderer.hpp:460
Small result wrapper used by helpers that must return both a status and a value (e....
Definition Error.hpp:89