Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
DeviceCommandContext.h
Go to the documentation of this file.
1/* Copyright (C) 2024 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_RENDERER_VULKAN_DEVICECOMMANDCONTEXT
19#define INCLUDED_RENDERER_VULKAN_DEVICECOMMANDCONTEXT
20
24
25#include <glad/vulkan.h>
26#include <memory>
27#include <vector>
28
29namespace Renderer
30{
31
32namespace Backend
33{
34
35namespace Vulkan
36{
37
38class CBuffer;
39class CComputePipelineState;
40class CDevice;
41class CFramebuffer;
42class CGraphicsPipelineState;
43class CRingCommandContext;
44class CShaderProgram;
45class CVertexInputLayout;
46
48{
49public:
50 ~CDeviceCommandContext() override;
51
52 IDevice* GetDevice() override;
53
54 void SetGraphicsPipelineState(IGraphicsPipelineState* pipelineState) override;
55 void SetComputePipelineState(IComputePipelineState* pipelineState) override;
56
57 void BlitFramebuffer(
58 IFramebuffer* destinationFramebuffer, IFramebuffer* sourceFramebuffer,
59 const Rect& destinationRegion, const Rect& sourceRegion,
60 const Sampler::Filter filter) override;
62 IFramebuffer* destinationFramebuffer, IFramebuffer* sourceFramebuffer) override;
63
64 void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override;
65 void BeginFramebufferPass(IFramebuffer* framebuffer) override;
66 void EndFramebufferPass() override;
68 const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height,
69 void* data) override;
70
71 void UploadTexture(ITexture* texture, const Format dataFormat,
72 const void* data, const size_t dataSize,
73 const uint32_t level = 0, const uint32_t layer = 0) override;
74 void UploadTextureRegion(ITexture* texture, const Format dataFormat,
75 const void* data, const size_t dataSize,
76 const uint32_t xOffset, const uint32_t yOffset,
77 const uint32_t width, const uint32_t height,
78 const uint32_t level = 0, const uint32_t layer = 0) override;
79
80 void UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize) override;
81 void UploadBuffer(IBuffer* buffer, const UploadBufferFunction& uploadFunction) override;
83 IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize) override;
85 IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
86 const UploadBufferFunction& uploadFunction) override;
87
88 void SetScissors(const uint32_t scissorCount, const Rect* scissors) override;
89 void SetViewports(const uint32_t viewportCount, const Rect* viewports) override;
90
92 IVertexInputLayout* vertexInputLayout) override;
93
94 void SetVertexBuffer(
95 const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) override;
97 const uint32_t bindingSlot, const void* data, const uint32_t dataSize) override;
98
99 void SetIndexBuffer(IBuffer* buffer) override;
100 void SetIndexBufferData(const void* data, const uint32_t dataSize) override;
101
102 void BeginPass() override;
103 void EndPass() override;
104
105 void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override;
106 void DrawIndexed(
107 const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override;
108 void DrawInstanced(
109 const uint32_t firstVertex, const uint32_t vertexCount,
110 const uint32_t firstInstance, const uint32_t instanceCount) override;
112 const uint32_t firstIndex, const uint32_t indexCount,
113 const uint32_t firstInstance, const uint32_t instanceCount,
114 const int32_t vertexOffset) override;
116 const uint32_t firstIndex, const uint32_t indexCount,
117 const uint32_t start, const uint32_t end) override;
118
119 void BeginComputePass() override;
120 void EndComputePass() override;
121
122 void Dispatch(
123 const uint32_t groupCountX,
124 const uint32_t groupCountY,
125 const uint32_t groupCountZ) override;
126
127 void SetTexture(const int32_t bindingSlot, ITexture* texture) override;
128
129 void SetStorageTexture(const int32_t bindingSlot, ITexture* texture) override;
130
131 void SetUniform(
132 const int32_t bindingSlot,
133 const float value) override;
134 void SetUniform(
135 const int32_t bindingSlot,
136 const float valueX, const float valueY) override;
137 void SetUniform(
138 const int32_t bindingSlot,
139 const float valueX, const float valueY,
140 const float valueZ) override;
141 void SetUniform(
142 const int32_t bindingSlot,
143 const float valueX, const float valueY,
144 const float valueZ, const float valueW) override;
145 void SetUniform(
146 const int32_t bindingSlot, PS::span<const float> values) override;
147
148 void BeginScopedLabel(const char* name) override;
149 void EndScopedLabel() override;
150
151 void Flush() override;
152
153private:
154 friend class CDevice;
155
156 static std::unique_ptr<IDeviceCommandContext> Create(CDevice* device);
157
159
160 void PreDraw();
163 void BindVertexBuffer(const uint32_t bindingSlot, CBuffer* buffer, uint32_t offset);
164 void BindIndexBuffer(CBuffer* buffer, uint32_t offset);
165
166 CDevice* m_Device = nullptr;
167
169
170 std::unique_ptr<CRingCommandContext> m_PrependCommandContext;
171 std::unique_ptr<CRingCommandContext> m_CommandContext;
172
179 VkPipeline m_LastBoundPipeline = VK_NULL_HANDLE;
180
182 bool m_InsidePass = false;
184
185 // Currently bound buffers to skip the same buffer bind.
188
189 class CUploadRing;
191
192 VkDescriptorPool m_UniformDescriptorPool = VK_NULL_HANDLE;
193 VkDescriptorSet m_UniformDescriptorSet = VK_NULL_HANDLE;
194
195 // Currently we support readbacks only from backbuffer.
197 {
198 uint32_t x = 0, y = 0;
200 // It's a responsibility of the caller to guarantee that data is valid.
201 void* data = nullptr;
202 };
204
206};
207
208} // namespace Vulkan
209
210} // namespace Backend
211
212} // namespace Renderer
213
214#endif // INCLUDED_RENDERER_VULKAN_DEVICECOMMANDCONTEXT
A wrapper for backend shader program to handle high-level operations like file reloading and handling...
Definition: ShaderProgram.h:34
A conntainer close to std::vector but the elements are stored in place: There is a fixed capacity and...
Definition: StaticVector.h:89
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
Definition: IBuffer.h:32
A holder for precompiled compute pipeline description.
Definition: PipelineState.h:207
Definition: IDeviceCommandContext.h:42
std::function< void(u8 *)> UploadBufferFunction
Definition: IDeviceCommandContext.h:122
Definition: IDevice.h:48
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:85
A holder for precompiled graphics pipeline description.
Definition: PipelineState.h:198
Definition: ITexture.h:34
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:74
Definition: Buffer.h:39
Definition: DeviceCommandContext.h:48
void BeginFramebufferPass(IFramebuffer *framebuffer) override
Starts a framebuffer pass, performs attachment load operations.
Definition: DeviceCommandContext.cpp:571
CFramebuffer * m_Framebuffer
Definition: DeviceCommandContext.h:175
IDevice * GetDevice() override
Definition: DeviceCommandContext.cpp:389
CBuffer * m_BoundIndexBuffer
Definition: DeviceCommandContext.h:186
std::unique_ptr< CUploadRing > m_UniformUploadRing
Definition: DeviceCommandContext.h:190
std::unique_ptr< CRingCommandContext > m_CommandContext
Definition: DeviceCommandContext.h:171
void DrawInstanced(const uint32_t firstVertex, const uint32_t vertexCount, const uint32_t firstInstance, const uint32_t instanceCount) override
Definition: DeviceCommandContext.cpp:890
void EndComputePass() override
Finishes a compute pass.
Definition: DeviceCommandContext.cpp:923
std::unique_ptr< CUploadRing > m_IndexUploadRing
Definition: DeviceCommandContext.h:190
void DrawIndexedInRange(const uint32_t firstIndex, const uint32_t indexCount, const uint32_t start, const uint32_t end) override
Definition: DeviceCommandContext.cpp:909
void DrawIndexed(const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override
Definition: DeviceCommandContext.cpp:882
bool m_InsidePass
Definition: DeviceCommandContext.h:182
void SetScissors(const uint32_t scissorCount, const Rect *scissors) override
Definition: DeviceCommandContext.cpp:766
void SetUniform(const int32_t bindingSlot, const float value) override
Definition: DeviceCommandContext.cpp:984
void UploadTextureRegion(ITexture *texture, const Format dataFormat, const void *data, const size_t dataSize, const uint32_t xOffset, const uint32_t yOffset, const uint32_t width, const uint32_t height, const uint32_t level=0, const uint32_t layer=0) override
Definition: DeviceCommandContext.cpp:726
void SetVertexBufferData(const uint32_t bindingSlot, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:835
void ApplyPipelineStateIfDirty()
Definition: DeviceCommandContext.cpp:1167
void UploadBuffer(IBuffer *buffer, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:736
void SetVertexInputLayout(IVertexInputLayout *vertexInputLayout) override
Binds the vertex input layout.
Definition: DeviceCommandContext.cpp:818
void UpdateOutdatedConstants()
Definition: DeviceCommandContext.cpp:1146
void BindVertexBuffer(const uint32_t bindingSlot, CBuffer *buffer, uint32_t offset)
Definition: DeviceCommandContext.cpp:1202
void SetStorageTexture(const int32_t bindingSlot, ITexture *texture) override
Sets a read & write resource to the binding slot.
Definition: DeviceCommandContext.cpp:975
void EndPass() override
Definition: DeviceCommandContext.cpp:870
std::unique_ptr< CUploadRing > m_VertexUploadRing
Definition: DeviceCommandContext.h:190
std::unique_ptr< CRingCommandContext > m_PrependCommandContext
Definition: DeviceCommandContext.h:170
void BeginPass() override
Definition: DeviceCommandContext.cpp:864
VkPipeline m_LastBoundPipeline
Definition: DeviceCommandContext.h:179
void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override
Clears all mentioned attachments.
Definition: DeviceCommandContext.cpp:519
void ResolveFramebuffer(IFramebuffer *destinationFramebuffer, IFramebuffer *sourceFramebuffer) override
Resolves multisample source framebuffer attachments to destination attachments.
Definition: DeviceCommandContext.cpp:480
void SetVertexBuffer(const uint32_t bindingSlot, IBuffer *buffer, const uint32_t offset) override
Definition: DeviceCommandContext.cpp:829
void SetGraphicsPipelineState(IGraphicsPipelineState *pipelineState) override
Binds the graphics pipeline state.
Definition: DeviceCommandContext.cpp:394
void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override
Definition: DeviceCommandContext.cpp:876
void EndFramebufferPass() override
Finishes a framebuffer pass, performs attachment store operations.
Definition: DeviceCommandContext.cpp:655
~CDeviceCommandContext() override
Definition: DeviceCommandContext.cpp:379
void BlitFramebuffer(IFramebuffer *destinationFramebuffer, IFramebuffer *sourceFramebuffer, const Rect &destinationRegion, const Rect &sourceRegion, const Sampler::Filter filter) override
Copies source region into destination region automatically applying compatible format conversion and ...
Definition: DeviceCommandContext.cpp:426
void BeginScopedLabel(const char *name) override
Definition: DeviceCommandContext.cpp:1025
void PreDraw()
Definition: DeviceCommandContext.cpp:1138
CGraphicsPipelineState * m_GraphicsPipelineState
Definition: DeviceCommandContext.h:173
void Flush() override
Definition: DeviceCommandContext.cpp:1042
void SetIndexBuffer(IBuffer *buffer) override
Definition: DeviceCommandContext.cpp:847
bool m_InsideFramebufferPass
Definition: DeviceCommandContext.h:181
void ReadbackFramebufferSync(const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height, void *data) override
Readbacks the current backbuffer to data in R8G8B8_UNORM format somewhen between the function call an...
Definition: DeviceCommandContext.cpp:700
bool m_DebugBarrierAfterFramebufferPass
Definition: DeviceCommandContext.h:205
void SetViewports(const uint32_t viewportCount, const Rect *viewports) override
Definition: DeviceCommandContext.cpp:802
void BeginComputePass() override
Starts a compute pass, can't be called inside a framebuffer pass.
Definition: DeviceCommandContext.cpp:916
bool m_DebugScopedLabels
Definition: DeviceCommandContext.h:168
bool m_InsideComputePass
Definition: DeviceCommandContext.h:183
void SetIndexBufferData(const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:852
CDevice * m_Device
Definition: DeviceCommandContext.h:166
CVertexInputLayout * m_VertexInputLayout
Definition: DeviceCommandContext.h:174
static std::unique_ptr< IDeviceCommandContext > Create(CDevice *device)
Definition: DeviceCommandContext.cpp:310
CComputePipelineState * m_ComputePipelineState
Definition: DeviceCommandContext.h:176
void Dispatch(const uint32_t groupCountX, const uint32_t groupCountY, const uint32_t groupCountZ) override
Dispatches groupCountX * groupCountY * groupCountZ compute groups.
Definition: DeviceCommandContext.cpp:937
void SetTexture(const int32_t bindingSlot, ITexture *texture) override
Sets a read-only texture to the binding slot.
Definition: DeviceCommandContext.cpp:951
uint32_t m_BoundIndexBufferOffset
Definition: DeviceCommandContext.h:187
VkDescriptorSet m_UniformDescriptorSet
Definition: DeviceCommandContext.h:193
PS::StaticVector< QueuedReadback, 2 > m_QueuedReadbacks
Definition: DeviceCommandContext.h:203
void SetComputePipelineState(IComputePipelineState *pipelineState) override
Binds the graphics pipeline state.
Definition: DeviceCommandContext.cpp:410
void EndScopedLabel() override
Definition: DeviceCommandContext.cpp:1035
bool m_IsPipelineStateDirty
Definition: DeviceCommandContext.h:178
void BindIndexBuffer(CBuffer *buffer, uint32_t offset)
Definition: DeviceCommandContext.cpp:1211
VkDescriptorPool m_UniformDescriptorPool
Definition: DeviceCommandContext.h:192
CShaderProgram * m_ShaderProgram
Definition: DeviceCommandContext.h:177
void UploadBufferRegion(IBuffer *buffer, const void *data, const uint32_t dataOffset, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:750
void UploadTexture(ITexture *texture, const Format dataFormat, const void *data, const size_t dataSize, const uint32_t level=0, const uint32_t layer=0) override
Definition: DeviceCommandContext.cpp:718
void DrawIndexedInstanced(const uint32_t firstIndex, const uint32_t indexCount, const uint32_t firstInstance, const uint32_t instanceCount, const int32_t vertexOffset) override
Definition: DeviceCommandContext.cpp:899
Definition: Device.h:60
Definition: Framebuffer.h:41
Definition: ShaderProgram.h:82
Definition: ShaderProgram.h:50
Filter
Definition: Sampler.h:36
Format
Definition: Format.h:28
Backend
Definition: Backend.h:28
Definition: VideoMode.h:29
Definition: IDeviceCommandContext.h:58
uint32_t height
Definition: DeviceCommandContext.h:199
uint32_t x
Definition: DeviceCommandContext.h:198
void * data
Definition: DeviceCommandContext.h:201
uint32_t width
Definition: DeviceCommandContext.h:199
uint32_t y
Definition: DeviceCommandContext.h:198
unsigned int uint32_t
Definition: wposix_types.h:53