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 GetCommandBuffer() const;
70
72 void End();
73
75 void NextFrame();
76
78
81 bool ContainsBlitToSwapChain() const;
82
84
87 const std::vector<Timing>& GetTimings() const;
88
89 private:
90 VulkanCommandBuffer(const VulkanCommandBuffer& other) = delete;
91
92 void Begin();
93
94 void TransitionTexture(VulkanTexture* texture, VkImageLayout destinationImageLayout);
95 void BufferBarrier(VulkanBuffer* buffer, VkPipelineStageFlags stages, bool write);
96
97 VulkanRenderer* vulkanRenderer;
98 VkDevice device;
99 VkCommandPool commandPool;
100 VulkanRenderPassAllocator& renderPassAllocator;
101
102 VkCommandBuffer* commandBuffer;
103 VkCommandBuffer renderPassCommandBuffer;
104 std::vector<VkCommandBuffer>* secondaryCommandBuffers;
105 uint32_t currentFrame = 0;
106 uint32_t swapChainImages;
107
108 bool ended = false;
109 bool inRenderPass = false;
110 bool containsBlitToSwapChain = false;
111
112 VkClearValue clearValues[2];
113 VkRenderPassBeginInfo renderPassBeginInfo;
114
115 const VulkanRenderPass* currentRenderPass = nullptr;
116 VulkanGraphicsPipeline* currentGraphicsPipeline = nullptr;
117 VulkanComputePipeline* currentComputePipeline = nullptr;
118
119 std::vector<Timing> timings;
120};
121
122} // 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:434
void SetScissor(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the scissor box.
Definition: VulkanCommandBuffer.cpp:197
void BindMaterial(std::initializer_list< std::pair< Texture *, const Sampler * > > textures) final
Bind a material.
Definition: VulkanCommandBuffer.cpp:293
VkCommandBuffer GetCommandBuffer() const
Get command buffer.
Definition: VulkanCommandBuffer.cpp:430
void SetLineWidth(float width) final
Set width of lines.
Definition: VulkanCommandBuffer.cpp:209
void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed geometry.
Definition: VulkanCommandBuffer.cpp:331
void BindGraphicsPipeline(GraphicsPipeline *graphicsPipeline) final
Bind graphics pipeline.
Definition: VulkanCommandBuffer.cpp:174
void Dispatch(const glm::uvec3 &numGroups, const std::string &name) final
Dispatch compute shader.
Definition: VulkanCommandBuffer.cpp:395
~VulkanCommandBuffer() final
Destructor.
Definition: VulkanCommandBuffer.cpp:50
void BeginRenderPass(RenderPass *renderPass, const std::string &name) final
Begin render pass.
Definition: VulkanCommandBuffer.cpp:60
bool ContainsBlitToSwapChain() const
Get whether the command buffer contains a blit to the swap chain.
Definition: VulkanCommandBuffer.cpp:465
void BindStorageBuffers(std::initializer_list< Buffer * > buffers) final
Bind storage buffers.
Definition: VulkanCommandBuffer.cpp:268
void NextFrame()
Cycle the command buffer for the next frame.
Definition: VulkanCommandBuffer.cpp:448
void EndRenderPass() final
End render pass.
Definition: VulkanCommandBuffer.cpp:153
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:307
void BindComputePipeline(ComputePipeline *computePipeline) final
Bind compute pipeline.
Definition: VulkanCommandBuffer.cpp:386
const std::vector< Timing > & GetTimings() const
Get the all timings in the command buffer.
Definition: VulkanCommandBuffer.cpp:469
void BindGeometry(GeometryBinding *geometryBinding) final
Bind geometry to be used in upcoming draw calls.
Definition: VulkanCommandBuffer.cpp:216
void Draw(unsigned int vertexCount, unsigned int firstVertex) final
Draw geometry.
Definition: VulkanCommandBuffer.cpp:325
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:416
void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed instanced geometry.
Definition: VulkanCommandBuffer.cpp:337
void SetViewport(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the viewport to render to.
Definition: VulkanCommandBuffer.cpp:183
void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer *uniformBuffer) final
Bind uniform buffer for use in a shader.
Definition: VulkanCommandBuffer.cpp:253
void BlitToSwapChain(Texture *texture) final
Blit a texture to the current swap chain image.
Definition: VulkanCommandBuffer.cpp:343
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