FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_commandBuffer.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_VULKAN_C_COMMANDBUFFER_HPP_INCLUDED
18#define _FGE_VULKAN_C_COMMANDBUFFER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21
22#include "FastEngine/vulkan/C_contextAware.hpp"
23#include "FastEngine/vulkan/vulkanGlobal.hpp"
24#include <unordered_map>
25#include <utility>
26
27namespace fge::vulkan
28{
29
35class FGE_API CommandBuffer : public ContextAware
36{
37public:
38 enum class RenderPassScopes
39 {
40 INSIDE,
41 OUTSIDE,
42 BOTH
43 };
44
45 enum SupportedQueueTypes : uint32_t
46 {
47 SUPPORTED_QUEUE_GRAPHICS = 1 << 0,
48 SUPPORTED_QUEUE_COMPUTE = 1 << 1,
49 SUPPORTED_QUEUE_TRANSFER = 1 << 2,
50
51 SUPPORTED_QUEUE_ALL = SUPPORTED_QUEUE_GRAPHICS | SUPPORTED_QUEUE_COMPUTE | SUPPORTED_QUEUE_TRANSFER
52 };
53 using SupportedQueueTypes_t = std::underlying_type_t<SupportedQueueTypes>;
54
55 CommandBuffer(Context const& context);
56 CommandBuffer(Context const& context, VkCommandBufferLevel level, VkCommandPool commandPool);
57 CommandBuffer(Context const& context,
58 VkCommandBufferLevel level,
59 VkCommandBuffer commandBuffer,
60 VkCommandPool commandPool);
61 CommandBuffer(CommandBuffer const& r) = delete;
62 CommandBuffer(CommandBuffer&& r) noexcept;
63 ~CommandBuffer() override;
64
65 CommandBuffer& operator=(CommandBuffer const& r) = delete;
66 CommandBuffer& operator=(CommandBuffer&& r) noexcept;
67
68 void create(VkCommandBufferLevel level, VkCommandPool commandPool);
69 void create(VkCommandBufferLevel level, VkCommandBuffer commandBuffer, VkCommandPool commandPool);
70 void destroy() final;
71 [[nodiscard]] std::pair<VkCommandBuffer, VkCommandPool> release();
72
73 void reset();
74 void begin(VkCommandBufferUsageFlags flags, VkCommandBufferInheritanceInfo const* inheritanceInfo = nullptr);
75 void end();
76
77 [[nodiscard]] VkCommandBuffer get() const;
78 [[nodiscard]] VkCommandBuffer const* getPtr() const;
79 [[nodiscard]] VkCommandPool getPool() const;
80 [[nodiscard]] VkCommandBufferLevel getLevel() const;
81 [[nodiscard]] RenderPassScopes getRenderPassScope() const;
82 [[nodiscard]] SupportedQueueTypes_t getSupportedQueues() const;
83 [[nodiscard]] uint32_t getRecordedCommandsCount() const;
84 [[nodiscard]] bool isEnded() const;
85
86 void forceEnd();
87 void forceRenderPassScope(RenderPassScopes scope);
88 void forceSupportedQueues(SupportedQueueTypes_t queues);
89 void forceRecordedCommandsCount(uint32_t count);
90
100 void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
112 void transitionImageLayout(VkImage image,
113 VkFormat format,
114 VkImageLayout oldLayout,
115 VkImageLayout newLayout,
116 uint32_t mipLevels);
129 void copyBufferToImage(VkBuffer buffer,
130 VkImage image,
131 uint32_t width,
132 uint32_t height,
133 int32_t offsetX = 0,
134 int32_t offsetY = 0);
145 void copyImageToBuffer(VkImage image, VkBuffer buffer, uint32_t width, uint32_t height);
158 void copyImageToImage(VkImage srcImage,
159 VkImage dstImage,
160 uint32_t width,
161 uint32_t height,
162 int32_t offsetX = 0,
163 int32_t offsetY = 0);
164
174 void pushConstants(VkPipelineLayout pipelineLayout,
175 VkShaderStageFlags stageFlags,
176 uint32_t offset,
177 uint32_t size,
178 void const* pValues);
179
189 void beginRenderPass(VkRenderPass renderPass,
190 VkFramebuffer framebuffer,
191 VkExtent2D extent,
192 VkClearValue clearColor,
193 VkSubpassContents contents);
194
201
211 void bindDescriptorSets(VkPipelineLayout pipelineLayout,
212 VkPipelineBindPoint pipelineBindPoint,
213 VkDescriptorSet const* descriptorSet,
214 uint32_t descriptorCount,
215 uint32_t firstSet);
216
228 void bindDescriptorSets(VkPipelineLayout pipelineLayout,
229 VkPipelineBindPoint pipelineBindPoint,
230 VkDescriptorSet const* descriptorSet,
231 uint32_t descriptorCount,
232 uint32_t dynamicOffsetCount,
233 uint32_t const* pDynamicOffsets,
234 uint32_t firstSet);
235
242 void bindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline);
243
253 void setViewport(uint32_t firstViewport, uint32_t viewportCount, VkViewport const* pViewports);
263 void setScissor(uint32_t firstScissor, uint32_t scissorCount, VkRect2D const* pScissors);
264
273 void bindVertexBuffers(uint32_t firstBinding,
274 uint32_t bindingCount,
275 VkBuffer const* pBuffers,
276 VkDeviceSize const* pOffsets);
284 void bindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType);
285
294 void draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance);
304 void drawIndexed(uint32_t indexCount,
305 uint32_t instanceCount,
306 uint32_t firstIndex,
307 int32_t vertexOffset,
308 uint32_t firstInstance);
317 void drawIndirect(VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount, uint32_t stride);
318
319private:
320 VkCommandBuffer g_commandBuffer;
321 VkCommandPool g_commandPool;
322 VkCommandBufferLevel g_level;
323 RenderPassScopes g_renderPassScope;
324 SupportedQueueTypes_t g_queueType;
325 uint32_t g_recordedCommands;
326 bool g_isEnded;
327
328 //Cache
329 struct CacheDescriptorSets
330 {
331 constexpr CacheDescriptorSets(VkPipelineLayout pipelineLayout,
332 VkPipelineBindPoint pipelineBindPoint,
333 VkDescriptorSet descriptorSet) :
334 _pipelineLayout(pipelineLayout),
335 _pipelineBindPoint(pipelineBindPoint),
336 _descriptorSet(descriptorSet)
337 {}
338
339 VkPipelineLayout _pipelineLayout;
340 VkPipelineBindPoint _pipelineBindPoint;
341 VkDescriptorSet _descriptorSet;
342
343 [[nodiscard]] constexpr bool operator==(CacheDescriptorSets const& r) const
344 {
345 return this->_pipelineLayout == r._pipelineLayout && this->_pipelineBindPoint == r._pipelineBindPoint &&
346 this->_descriptorSet == r._descriptorSet;
347 }
348 };
349
350 VkPipeline g_lastBoundPipeline{VK_NULL_HANDLE};
351 VkViewport g_lastSetViewport{};
352 VkRect2D g_lastSetScissor{};
353 std::unordered_map<uint32_t, CacheDescriptorSets> g_lastBoundDescriptorSets;
354};
355
356} // namespace fge::vulkan
357
358#endif //_FGE_VULKAN_C_COMMANDBUFFER_HPP_INCLUDED
Vulkan command buffer wrapper.
Definition C_commandBuffer.hpp:36
void drawIndirect(VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount, uint32_t stride)
Draw indirect.
void bindIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType)
Bind an index buffer.
void bindVertexBuffers(uint32_t firstBinding, uint32_t bindingCount, VkBuffer const *pBuffers, VkDeviceSize const *pOffsets)
Bind vertex buffers.
void beginRenderPass(VkRenderPass renderPass, VkFramebuffer framebuffer, VkExtent2D extent, VkClearValue clearColor, VkSubpassContents contents)
Begin a render pass.
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size)
Copy a buffer to another.
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels)
Transition an image layout.
void setViewport(uint32_t firstViewport, uint32_t viewportCount, VkViewport const *pViewports)
Set the viewport dynamically.
void pushConstants(VkPipelineLayout pipelineLayout, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size, void const *pValues)
Push constants to the pipeline.
void endRenderPass()
End a render pass.
void bindDescriptorSets(VkPipelineLayout pipelineLayout, VkPipelineBindPoint pipelineBindPoint, VkDescriptorSet const *descriptorSet, uint32_t descriptorCount, uint32_t firstSet)
Bind descriptor sets without dynamic parameters.
void copyImageToImage(VkImage srcImage, VkImage dstImage, uint32_t width, uint32_t height, int32_t offsetX=0, int32_t offsetY=0)
Copy an image to another image.
void draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
Draw.
void setScissor(uint32_t firstScissor, uint32_t scissorCount, VkRect2D const *pScissors)
Set the scissor dynamically.
void bindDescriptorSets(VkPipelineLayout pipelineLayout, VkPipelineBindPoint pipelineBindPoint, VkDescriptorSet const *descriptorSet, uint32_t descriptorCount, uint32_t dynamicOffsetCount, uint32_t const *pDynamicOffsets, uint32_t firstSet)
Bind descriptor sets with dynamic parameters.
void bindPipeline(VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline)
Bind a pipeline.
void copyImageToBuffer(VkImage image, VkBuffer buffer, uint32_t width, uint32_t height)
Copy an image to a buffer.
void drawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance)
Draw indexed.
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height, int32_t offsetX=0, int32_t offsetY=0)
Copy a buffer to an image.
Definition C_contextAware.hpp:28
Vulkan context.
Definition C_context.hpp:70