Hymn to Beauty
C++ 3D Engine
VulkanCommandBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../Interface/CommandBuffer.hpp"
4#include <vulkan/vulkan.h>
5#include <vector>
6
7namespace Video {
8
9class VulkanBuffer;
10class VulkanRenderer;
11class VulkanRenderPass;
12class VulkanRenderPassAllocator;
13class VulkanGraphicsPipeline;
14class VulkanComputePipeline;
15class VulkanTexture;
16
19 public:
21 struct Timing {
23 std::string name;
24
26 uint32_t startQuery;
27
29 uint32_t endQuery;
30 };
31
33
39 VulkanCommandBuffer(VulkanRenderer* vulkanRenderer, VkDevice device, VkCommandPool commandPool, VulkanRenderPassAllocator& renderPassAllocator);
40
43
44 void BeginRenderPass(RenderPass* renderPass, const std::string& name) final;
45 void BeginRenderPass(Texture* colorAttachment, RenderPass::LoadOperation colorLoadOperation, Texture* depthAttachment, RenderPass::LoadOperation depthLoadOperation, const std::string& name) final;
46 void BeginAttachmentlessRenderPass(const glm::uvec2& size, uint32_t msaaSamples, const std::string& name) final;
47 void EndRenderPass() final;
48 void BindGraphicsPipeline(GraphicsPipeline* graphicsPipeline) final;
49 void SetViewport(const glm::uvec2& origin, const glm::uvec2& size) final;
50 void SetScissor(const glm::uvec2& origin, const glm::uvec2& size) final;
51 void SetLineWidth(float width) final;
52 void BindGeometry(GeometryBinding* geometryBinding) final;
53 void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer* uniformBuffer) final;
54 void BindStorageBuffers(std::initializer_list<Buffer*> buffers) final;
55 void BindMaterial(std::initializer_list<std::pair<Texture*, const Sampler*>> textures) final;
56 void PushConstants(const void* data) final;
57 void Draw(unsigned int vertexCount, unsigned int firstVertex) final;
58 void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final;
59 void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final;
60 void BlitToSwapChain(Texture* texture) final;
61 void BindComputePipeline(ComputePipeline* computePipeline) final;
62 void Dispatch(const glm::uvec3& numGroups, const std::string& name) final;
63 void ClearBuffer(Buffer* buffer) final;
64
66
69 VkCommandBuffer GetCurrentCommandBuffer() const;
70
72
75 std::vector<VkCommandBuffer> GetCommandBuffers() const;
76
78 void End();
79
81 void NextFrame();
82
84
87 bool ContainsBlitToSwapChain() const;
88
90
93 const std::vector<Timing>& GetTimings() const;
94
95 private:
96 VulkanCommandBuffer(const VulkanCommandBuffer& other) = delete;
97
98 void AllocateCommandBuffer();
99
100 void TransitionTexture(VulkanTexture* texture, VkImageLayout destinationImageLayout);
101 void BufferBarrier(VulkanBuffer* buffer, VkPipelineStageFlags stages, bool write);
102
103 VulkanRenderer* vulkanRenderer;
104 VkDevice device;
105 VkCommandPool commandPool;
106 VulkanRenderPassAllocator& renderPassAllocator;
107
108 struct FrameCommandBuffers {
109 uint32_t currentBuffer;
110 std::vector<VkCommandBuffer> commandBuffers;
111 };
112 FrameCommandBuffers* frameCommandBuffers;
113 VkCommandBuffer currentCommandBuffer;
114 VkCommandBuffer renderPassCommandBuffer;
115 std::vector<VkCommandBuffer>* secondaryCommandBuffers;
116 uint32_t currentFrame = 0;
117 uint32_t swapChainImages;
118
119 bool ended = false;
120 bool inRenderPass = false;
121 bool containsBlitToSwapChain = false;
122 bool renderPassIsAttachmentless = false;
123
124 VkClearValue clearValues[2];
125 VkRenderPassBeginInfo renderPassBeginInfo;
126
127 const VulkanRenderPass* currentRenderPass = nullptr;
128 VulkanGraphicsPipeline* currentGraphicsPipeline = nullptr;
129 VulkanComputePipeline* currentComputePipeline = nullptr;
130
131 std::vector<Timing> timings;
132};
133
134} // namespace Video
A buffer containing GPU accessible data.
Definition: Buffer.hpp:8
A buffer into which rendering commands are recorded.
Definition: CommandBuffer.hpp:24
A compute pipeline.
Definition: ComputePipeline.hpp:11
Binds together a vertex description with buffers.
Definition: GeometryBinding.hpp:8
A graphics pipeline.
Definition: GraphicsPipeline.hpp:11
A render pass.
Definition: RenderPass.hpp:11
Determines how a texture should be sampled.
Definition: Sampler.hpp:6
A shader program.
Definition: ShaderProgram.hpp:11
A texture.
Definition: Texture.hpp:8
Vulkan implementation of Buffer.
Definition: VulkanBuffer.hpp:13
Vulkan implementation of CommandBuffer.
Definition: VulkanCommandBuffer.hpp:18
void End()
Finish recording the command buffer.
Definition: VulkanCommandBuffer.cpp:450
void SetScissor(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the scissor box.
Definition: VulkanCommandBuffer.cpp:205
void BindMaterial(std::initializer_list< std::pair< Texture *, const Sampler * > > textures) final
Bind a material.
Definition: VulkanCommandBuffer.cpp:301
void SetLineWidth(float width) final
Set width of lines.
Definition: VulkanCommandBuffer.cpp:217
void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed geometry.
Definition: VulkanCommandBuffer.cpp:339
void BindGraphicsPipeline(GraphicsPipeline *graphicsPipeline) final
Bind graphics pipeline.
Definition: VulkanCommandBuffer.cpp:182
void Dispatch(const glm::uvec3 &numGroups, const std::string &name) final
Dispatch compute shader.
Definition: VulkanCommandBuffer.cpp:403
std::vector< VkCommandBuffer > GetCommandBuffers() const
Get command buffers.
Definition: VulkanCommandBuffer.cpp:442
~VulkanCommandBuffer() final
Destructor.
Definition: VulkanCommandBuffer.cpp:44
void BeginRenderPass(RenderPass *renderPass, const std::string &name) final
Begin render pass.
Definition: VulkanCommandBuffer.cpp:59
bool ContainsBlitToSwapChain() const
Get whether the command buffer contains a blit to the swap chain.
Definition: VulkanCommandBuffer.cpp:485
VkCommandBuffer GetCurrentCommandBuffer() const
Get the command buffer currently being recorded into.
Definition: VulkanCommandBuffer.cpp:438
void BindStorageBuffers(std::initializer_list< Buffer * > buffers) final
Bind storage buffers.
Definition: VulkanCommandBuffer.cpp:276
void NextFrame()
Cycle the command buffer for the next frame.
Definition: VulkanCommandBuffer.cpp:464
void EndRenderPass() final
End render pass.
Definition: VulkanCommandBuffer.cpp:154
VulkanCommandBuffer(VulkanRenderer *vulkanRenderer, VkDevice device, VkCommandPool commandPool, VulkanRenderPassAllocator &renderPassAllocator)
Create new Vulkan command buffer.
Definition: VulkanCommandBuffer.cpp:18
void PushConstants(const void *data) final
Update push constants.
Definition: VulkanCommandBuffer.cpp:315
void BindComputePipeline(ComputePipeline *computePipeline) final
Bind compute pipeline.
Definition: VulkanCommandBuffer.cpp:394
const std::vector< Timing > & GetTimings() const
Get the all timings in the command buffer.
Definition: VulkanCommandBuffer.cpp:489
void BindGeometry(GeometryBinding *geometryBinding) final
Bind geometry to be used in upcoming draw calls.
Definition: VulkanCommandBuffer.cpp:224
void Draw(unsigned int vertexCount, unsigned int firstVertex) final
Draw geometry.
Definition: VulkanCommandBuffer.cpp:333
void BeginAttachmentlessRenderPass(const glm::uvec2 &size, uint32_t msaaSamples, const std::string &name) final
Begin attachmentless render pass.
Definition: VulkanCommandBuffer.cpp:148
void ClearBuffer(Buffer *buffer) final
Clear a buffer (fill with 0).
Definition: VulkanCommandBuffer.cpp:424
void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed instanced geometry.
Definition: VulkanCommandBuffer.cpp:345
void SetViewport(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the viewport to render to.
Definition: VulkanCommandBuffer.cpp:191
void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer *uniformBuffer) final
Bind uniform buffer for use in a shader.
Definition: VulkanCommandBuffer.cpp:261
void BlitToSwapChain(Texture *texture) final
Blit a texture to the current swap chain image.
Definition: VulkanCommandBuffer.cpp:351
Vulkan implementation of ComputePipeline.
Definition: VulkanComputePipeline.hpp:13
Vulkan implementation of GraphicsPipeline.
Definition: VulkanGraphicsPipeline.hpp:19
Vulkan implementation of RenderPassAllocator.
Definition: VulkanRenderPassAllocator.hpp:9
Vulkan implementation of RenderPass.
Definition: VulkanRenderPass.hpp:14
Low-level renderer implementing Vulkan.
Definition: VulkanRenderer.hpp:24
Vulkan implementation of Texture.
Definition: VulkanTexture.hpp:13
Definition: Editor.hpp:18
Timing for a block of work.
Definition: VulkanCommandBuffer.hpp:21
uint32_t startQuery
The timestamp at the start of the block of work.
Definition: VulkanCommandBuffer.hpp:26
uint32_t endQuery
The timestamp at the end of the block of work.
Definition: VulkanCommandBuffer.hpp:29
std::string name
Name of the block of work.
Definition: VulkanCommandBuffer.hpp:23