SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
RenderCommandQueue.hpp
Go to the documentation of this file.
1#ifndef _RENDERCOMMANDQUEUE_H
2#define _RENDERCOMMANDQUEUE_H
3
6#include <cstddef>
7#include <memory>
8#include <new>
9#include <utility>
10#include <vector>
11
12#include "RenderCommands.hpp"
13
14namespace Sleak {
15 namespace RenderEngine {
16 /// Frame-scoped queue of recorded draw/state commands, replayed into a RenderContext at flush time.
18 public:
19 /// Queues an indexed draw with its vertex/index buffers and constant buffers.
21 RefPtr<BufferBase> vertexBuffer,
22 RefPtr<BufferBase> indexBuffer,
23 List<RefPtr<BufferBase>> constantBuffers,
24 uint32_t indexCount,
25 uint32_t startIndexLocation = 0,
26 int32_t baseVertexLocation = 0,
27 bool castsShadow = true
28 );
29
30 /// Queues a non-indexed draw with its vertex buffer and constant buffers.
31 void SubmitDraw(
32 RefPtr<BufferBase> vertexBuffer,
33 List<RefPtr<BufferBase>> constantBuffers,
34 uint32_t vertexCount,
35 uint32_t startVertexLocation = 0
36 );
37
38 /// Queues a constant buffer bind at the given slot.
40 RefPtr<BufferBase> buffer,
41 uint8_t slot
42 );
43
44 /// Queues a constant buffer write with data captured at submit time.
46 RefPtr<BufferBase> buffer,
47 void* Data,
48 uint16_t Size
49 );
50
51 /// Queues a material bind, switching shader/texture state for subsequent draws.
53
55
57
58 /// Queues an arbitrary callback to run inline with other render commands.
60
61 // Execute all queued commands.
62 // In deferred mode: runs opaque draws in the geometry pass, then triggers the
63 // lighting pass, then runs transparent/custom draws in the forward pass.
64 // In forward mode: runs all commands in order (legacy behavior).
65 void ExecuteCommands(RenderContext* context);
66
67 void ExecuteShadowPass(RenderContext* context);
68
69 bool HasCachedShadowDraws() const { return !cachedShadowDraws.empty(); }
70
71 /// Drops queued commands for the current frame, keeping the cached shadow draw list.
72 void Clear();
73
74 /// Drops queued commands and the cached shadow draw list.
75 void ClearAll();
76 static void Shutdown();
77
78 void SortCommands();
79 void OptimizeBatching();
80
81 /// Lazily creates and returns the process-wide singleton instance.
83 {
84 Instance = Instance ? Instance : new RenderCommandQueue();
85 return Instance;
86 }
87
88 /// A draw command paired with the transform/bone buffers it needs to replay in the shadow pass.
92 nullptr; // last-bound slot-0 buffer
94 nullptr; // slot-3 bone UBO for skinned draws
95 };
96
97 private:
98 /// Bump allocator owning one frame's commands until the slot is
99 /// recycled.
100 class CommandArena {
101 public:
102 CommandArena() = default;
103 CommandArena(const CommandArena&) = delete;
104 CommandArena& operator=(const CommandArena&) = delete;
105 ~CommandArena() { Reset(); }
106
107 /// Constructs a command in arena memory and appends it to the
108 /// frame's list.
109 template <typename T, typename... Args>
110 T* Create(Args&&... args) {
111 void* memory = Allocate(sizeof(T), alignof(T));
112 T* command = new (memory) T(std::forward<Args>(args)...);
113 m_commands.push_back(command);
114 return command;
115 }
116
117 /// Destroys every command and rewinds the blocks for reuse.
118 void Reset();
119
120 const std::vector<RenderCommandBase*>& Commands() const {
121 return m_commands;
122 }
123
124 private:
125 struct Block {
126 std::unique_ptr<std::byte[]> data;
127 size_t size = 0;
128 };
129
130 /// Returns aligned storage from the current block, adding blocks
131 /// as needed.
132 void* Allocate(size_t size, size_t alignment);
133
134 static constexpr size_t BLOCK_SIZE = 64 * 1024;
135 std::vector<Block> m_blocks;
136 size_t m_blockIndex = 0;
137 size_t m_offset = 0;
138 std::vector<RenderCommandBase*> m_commands;
139 };
140
141 /// Moves submission to the next arena, destroying the commands it last
142 /// held.
143 void AdvanceArena();
144
145 CommandArena& CurrentArena() { return m_arenas[m_currentArena]; }
146
147 // A frame's commands stay alive while its shadow replay and GPU frames
148 // in flight may use them
149 static constexpr int RETIRE_FRAMES = 3;
150 static constexpr int ARENA_COUNT = RETIRE_FRAMES + 2;
151 static RenderCommandQueue* Instance;
152 CommandArena m_arenas[ARENA_COUNT];
153 int m_currentArena = 0;
154 std::vector<ShadowDrawEntry> cachedShadowDraws;
155 std::vector<RenderCommandBase*> m_pendingState;
156 std::vector<RenderCommandBase*> m_forwardCmds;
157 };
158 }
159}
160
161#endif
Implements a dynamic array-like list for storing and managing a collection of elements.
Definition List.hpp:21
std::function< void(RenderContext *)> ExecuteFunction
One recorded draw plus the state it needs to replay into a command list.
Frame-scoped queue of recorded draw/state commands, replayed into a RenderContext at flush time.
void Clear()
Drops queued commands for the current frame, keeping the cached shadow draw list.
void SubmitUpdateConstantBuffer(RefPtr< BufferBase > buffer, void *Data, uint16_t Size)
Queues a constant buffer write with data captured at submit time.
void SubmitCustomCommand(CustomCommand::ExecuteFunction function)
Queues an arbitrary callback to run inline with other render commands.
void ExecuteCommands(RenderContext *context)
static RenderCommandQueue * GetInstance()
Lazily creates and returns the process-wide singleton instance.
void SubmitBindMaterial(::Sleak::Material *material)
Queues a material bind, switching shader/texture state for subsequent draws.
void ExecuteShadowPass(RenderContext *context)
void SubmitDrawIndexed(RefPtr< BufferBase > vertexBuffer, RefPtr< BufferBase > indexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t indexCount, uint32_t startIndexLocation=0, int32_t baseVertexLocation=0, bool castsShadow=true)
Queues an indexed draw with its vertex/index buffers and constant buffers.
void ClearAll()
Drops queued commands and the cached shadow draw list.
void SubmitDraw(RefPtr< BufferBase > vertexBuffer, List< RefPtr< BufferBase > > constantBuffers, uint32_t vertexCount, uint32_t startVertexLocation=0)
Queues a non-indexed draw with its vertex buffer and constant buffers.
void SubmitBindConstantBuffer(RefPtr< BufferBase > buffer, uint8_t slot)
Queues a constant buffer bind at the given slot.
Abstract interface for high-level graphics command execution and resource management.
Backend-facing rendering layer shared by the four graphics backends.
RenderMode
Rasterizer fill style for a draw.
RenderFace
Which triangle winding gets culled.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
A draw command paired with the transform/bone buffers it needs to replay in the shadow pass.