RavEngine
Loading...
Searching...
No Matches
VkCommandBuffer.hpp
1#pragma once
2#include <RGL/Types.hpp>
3#include <RGL/CommandBuffer.hpp>
4#include <volk.h>
5#include "RGLVk.hpp"
6#include <unordered_set>
7#include <unordered_map>
8#include <RGL/SubresourceRange.hpp>
9
10namespace RGL {
12 const struct TextureVk* texture = nullptr;
13 covered_mips_t coveredMips;
14 covered_layers_t coveredLayers;
15 TextureLastUseKey(decltype(texture) texture, decltype(coveredMips) coveredMips, decltype(coveredLayers) coveredLayers) : texture(texture), coveredMips(coveredMips), coveredLayers(coveredLayers) {}
16
17 bool operator==(const TextureLastUseKey& other) const {
18 return texture == other.texture && coveredMips == other.coveredMips && coveredLayers == other.coveredLayers;
19 }
20 };
21}
22
23namespace std {
24 template<>
25 struct hash<RGL::TextureLastUseKey> {
26 size_t operator()(const RGL::TextureLastUseKey& other) const {
27 return uintptr_t(other.texture) ^ other.coveredMips ^ other.coveredLayers;
28 }
29 };
30}
31
32namespace RGL {
33 struct DeviceVk;
34 struct CommandQueueVk;
35 struct RenderPipelineVk;
36 struct RenderPassVk;
37
39 bool isInsideRenderingBlock = false;
40 VkCommandBuffer commandBuffer = VK_NULL_HANDLE; // does not need to be destroyed
41 std::shared_ptr<RenderPassVk> currentRenderPass = nullptr;
42
43 const std::shared_ptr<CommandQueueVk> owningQueue;
44 std::shared_ptr<RenderPipelineVk> currentRenderPipeline = nullptr;
45 std::shared_ptr<struct ComputePipelineVk> currentComputePipeline = nullptr;
46
47 std::unordered_set<struct SwapchainVK*> swapchainsToSignal;
48 std::unordered_set<const struct TextureVk*> swapchainImages;
49
51 VkImageLayout lastLayout = VK_IMAGE_LAYOUT_UNDEFINED;
52 bool written = false;
53 };
54
56 bool written = false;
57 };
58
59
60 bool keyIsAllMips(const TextureLastUseKey& key) {
61 return key.coveredMips == ALL_MIPS;
62 }
63
64 std::unordered_map<TextureLastUseKey, TextureLastUse> activeTextures;
65 std::unordered_map<const struct BufferVk*, BufferLastUse> activeBuffers;
66
68 RGLBufferPtr buffer;
69 const VertexBufferBinding bindingInfo;
70 };
72 RGLBufferPtr buffer;
73 };
74
76 RGLRenderPipelinePtr generic_pipeline;
77 };
78
80 RGLBufferPtr buffer;
81 const uint32_t offsetIntoBuffer;
82 const uint32_t bindingOffset;
83 VkPipelineBindPoint bindPoint;
84 };
85
87 std::byte data[256]{ };
88 uint32_t size = 0;
89 uint32_t offset = 0;
90 };
91
93 RGLSamplerPtr sampler;
94 uint32_t index;
95 bool isCompute = false;
96 };
97
99 TextureView texture;
100 uint32_t index;
101 };
102
103 struct CmdDraw {
104 uint32_t nVertices;
105 const DrawInstancedConfig config;
106 };
107
109 uint32_t nIndices;
110 const DrawIndexedInstancedConfig config;
111 };
112
114 const IndirectConfig config;
115 };
116
118 const IndirectConfig config;
119 };
120
122 const DispatchIndirectConfig config;
123 };
124
126 const std::string label;
127 };
129
131 RGLRenderPassPtr pass;
132 };
133
135 RGLComputePipelinePtr inPipeline;
136 };
137
138 struct CmdEndCompute {};
139
140 struct CmdDispatch {
141 uint32_t threadsX;
142 uint32_t threadsY;
143 uint32_t threadsZ;
144 };
145
147 const Viewport viewport;
148 };
149
151 const Rect scissor;
152 };
153
155 TextureView sourceTexture;
156 const Rect sourceRect;
157 size_t offset;
158 RGLBufferPtr destBuffer;
159 };
160
162 RGLBufferPtr srcBuffer;
163 uint32_t nBytes;
164 TextureView destTexture;
165 Rect destLoc;
166 uint32_t arrayLayer;
167 };
168
170 BufferCopyConfig from;
172 uint32_t size;
173 };
174
176 const VkDescriptorSet set;
177 const uint32_t binding;
178 };
179
181 const VkDescriptorSet set;
182 const uint32_t setIndex;
183 };
184
186 TextureCopyConfig from, to;
187 uint32_t fromMip, fromLayer, toMip, toLayer;
188 };
189
190 std::vector < std::variant <
196 CmdDraw,
217 >
218 > renderCommands;
219
220 void EncodeCommand(auto&& commandValue) {
221 renderCommands.push_back(commandValue);
222 if (!isInsideRenderingBlock) {
223 ApplyBarriers();
224 EncodeQueuedCommands(); // run it immediately if not inside a beginrendering
225 }
226 }
227
228 CommandBufferVk(decltype(owningQueue) owningQueue);
229
230 ~CommandBufferVk();
231
232 // ICommandBuffer
233 void Reset() final;
234 void Begin() final;
235 void End() final;
236 void BindRenderPipeline(RGLRenderPipelinePtr) final;
237
238 void BeginRendering(RGLRenderPassPtr) final;
239 void EndRendering() final;
240
241 void BeginCompute(RGLComputePipelinePtr) final;
242 void EndCompute() final;
243 void DispatchCompute(uint32_t threadsX, uint32_t threadsY, uint32_t threadsZ, uint32_t threadsPerThreadgroupX = 1, uint32_t threadsPerThreadgroupY = 1, uint32_t threadsPerThreadgroupZ = 1) final;
244
245 void BindBuffer(RGLBufferPtr buffer, uint32_t bindingOffset, uint32_t offsetIntoBuffer = 0) final;
246
247 void BindComputeBuffer(RGLBufferPtr buffer, uint32_t binding, uint32_t offsetIntoBuffer = 0) final;
248 void SetVertexBuffer(RGLBufferPtr buffer, const VertexBufferBinding& bindingInfo = {}) final;
249
250 void setPushConstantData(const RGL::untyped_span& data, const uint32_t& offset);
251
252 void SetVertexBytes(const untyped_span data, uint32_t offset) final;
253 void SetFragmentBytes(const untyped_span data, uint32_t offset) final;
254 void SetComputeBytes(const untyped_span data, uint32_t offset) final;
255
256 void SetIndexBuffer(RGLBufferPtr buffer) final;
257
258 void SetVertexSampler(RGLSamplerPtr sampler, uint32_t index) final;
259 void SetFragmentSampler(RGLSamplerPtr sampler, uint32_t index) final;
260 void SetComputeSampler(RGLSamplerPtr sampler, uint32_t index) final;
261
262 void BindBindlessBufferDescriptorSet(uint32_t set_idx) final;
263
264 void SetVertexTexture(const TextureView& texture, uint32_t index) final;
265 void SetFragmentTexture(const TextureView& texture, uint32_t index) final;
266 void SetComputeTexture(const TextureView& texture, uint32_t index) final;
267
268 void Draw(uint32_t nVertices, const DrawInstancedConfig & = {}) final;
269 void DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig & = {}) final;
270
271 void CopyTextureToBuffer(TextureView& sourceTexture, const Rect& sourceRect, size_t offset, RGLBufferPtr destBuffer) final;
272 void CopyBufferToTexture(RGLBufferPtr source, uint32_t size, const TextureDestConfig& dest) final;
273 void CopyBufferToBuffer(BufferCopyConfig from, BufferCopyConfig to, uint32_t size) final;
274 void CopyTextureToTexture(const TextureCopyConfig& from, const TextureCopyConfig& to) final;
275
276 void SetViewport(const Viewport&) final;
277 void SetScissor(const Rect&) final;
278
279 void UseResource(const TextureView& tx) final;
280 void UseResource(const RGLBufferPtr buffer) final;
281
282 void Commit(const CommitConfig&) final;
283
284 void ExecuteIndirectIndexed(const IndirectConfig&) final;
285 void ExecuteIndirect(const IndirectConfig&) final;
286 void DispatchIndirect(const DispatchIndirectConfig&) final;
287
288 void BeginRenderDebugMarker(const std::string& label) final;
289 void BeginComputeDebugMarker(const std::string& label) final;
290
291 void EndRenderDebugMarker() final;
292 void EndComputeDebugMarker() final;
293
294 void BlockUntilCompleted() final;
295
296 private:
297 void GenericBindBuffer(RGLBufferPtr& buffer, const uint32_t& offsetIntoBuffer, const uint32_t& bindingOffset, VkPipelineBindPoint bindPoint);
298 void RecordBufferBinding(const BufferVk* buffer, BufferLastUse usage);
299 void RecordTextureBinding(const TextureView texture, TextureLastUse usage, bool recordOnly = false);
300 void EncodeQueuedCommands();
301 bool IsBufferSlotWritable(uint32_t slot);
302 std::vector<VkBufferMemoryBarrier2> barriersToAdd;
303 void ApplyBarriers();
304 VkFence internalFence;
305 };
306}
307
Definition Span.hpp:11
Definition CommandBuffer.hpp:18
Definition CommandBuffer.hpp:11
Definition VkCommandBuffer.hpp:55
Definition VkCommandBuffer.hpp:134
Definition VkCommandBuffer.hpp:125
Definition VkCommandBuffer.hpp:130
Definition VkCommandBuffer.hpp:79
Definition VkCommandBuffer.hpp:75
Definition VkCommandBuffer.hpp:180
Definition VkCommandBuffer.hpp:175
Definition VkCommandBuffer.hpp:169
Definition VkCommandBuffer.hpp:161
Definition VkCommandBuffer.hpp:154
Definition VkCommandBuffer.hpp:185
Definition VkCommandBuffer.hpp:121
Definition VkCommandBuffer.hpp:140
Definition VkCommandBuffer.hpp:108
Definition VkCommandBuffer.hpp:103
Definition VkCommandBuffer.hpp:138
Definition VkCommandBuffer.hpp:128
Definition VkCommandBuffer.hpp:117
Definition VkCommandBuffer.hpp:113
Definition VkCommandBuffer.hpp:71
Definition VkCommandBuffer.hpp:86
Definition VkCommandBuffer.hpp:92
Definition VkCommandBuffer.hpp:150
Definition VkCommandBuffer.hpp:98
Definition VkCommandBuffer.hpp:67
Definition VkCommandBuffer.hpp:146
Definition VkCommandBuffer.hpp:50
Definition VkCommandBuffer.hpp:38
Definition CommandBuffer.hpp:80
Definition CommandBuffer.hpp:153
Definition CommandBuffer.hpp:159
Definition CommandBuffer.hpp:99
Definition CommandBuffer.hpp:74
Definition CommandBuffer.hpp:40
Definition VkCommandBuffer.hpp:11
Definition Texture.hpp:43
Definition VkTexture.hpp:14
Definition CommandBuffer.hpp:86
Definition CommandBuffer.hpp:31