Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
DeviceCommandContext.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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_BACKEND_GL_DEVICECOMMANDCONTEXT
19#define INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
20
21#include "graphics/Color.h"
22#include "lib/config2.h"
27
28#if CONFIG2_GLES
30#else
31#include <glad/gl.h>
32#endif
33
34#include <array>
35#include <cstddef>
36#include <cstdint>
37#include <functional>
38#include <memory>
39#include <span>
40#include <utility>
41
42namespace Renderer::Backend { enum class Format; }
43namespace Renderer::Backend::GL { class CBuffer; }
44
45namespace Renderer
46{
47
48namespace Backend
49{
50
51namespace GL
52{
53
54class CDevice;
55class CFramebuffer;
56class CShaderProgram;
57class CTexture;
58
60{
61public:
63
64 IDevice* GetDevice() override;
65
67
68 void SetGraphicsPipelineState(IGraphicsPipelineState* pipelineState) override;
69 void SetComputePipelineState(IComputePipelineState* pipelineState) override;
70
71 void BlitFramebuffer(
72 IFramebuffer* sourceFramebuffer, IFramebuffer* destinationFramebuffer,
73 const Rect& sourceRegion, const Rect& destinationRegion,
74 const Sampler::Filter filter) override;
76 IFramebuffer* sourceFramebuffer, IFramebuffer* destinationFramebuffer) override;
77
78 void BeginFramebufferPass(IFramebuffer* framebuffer) override;
79 void EndFramebufferPass() override;
80 void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override;
82 ISwapChain& swapChain, const uint32_t x, const uint32_t y,
83 const uint32_t width, const uint32_t height,
84 void* data) override;
85
86 void UploadTexture(ITexture* texture, const Format dataFormat,
87 const void* data, const size_t dataSize,
88 const uint32_t level = 0, const uint32_t layer = 0) override;
89 void UploadTextureRegion(ITexture* texture, const Format dataFormat,
90 const void* data, const size_t dataSize,
91 const uint32_t xOffset, const uint32_t yOffset,
92 const uint32_t width, const uint32_t height,
93 const uint32_t level = 0, const uint32_t layer = 0) override;
94
95 using UploadBufferFunction = std::function<void(std::uint8_t*)>;
96 void UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize) override;
97 void UploadBuffer(IBuffer* buffer, const UploadBufferFunction& uploadFunction) override;
99 IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize) override;
101 IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
102 const UploadBufferFunction& uploadFunction) override;
103
104 void SetScissors(const uint32_t scissorCount, const Rect* scissors) override;
105 void SetViewports(const uint32_t viewportCount, const Rect* viewports) override;
106
108 IVertexInputLayout* vertexInputLayout) override;
109
110 void SetVertexBuffer(
111 const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) override;
113 const uint32_t bindingSlot, const void* data, const uint32_t dataSize) override;
114
115 void SetIndexBuffer(IBuffer* buffer) override;
116 void SetIndexBufferData(const void* data, const uint32_t dataSize) override;
117
118 void BeginPass() override;
119 void EndPass() override;
120
121 void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override;
122 void DrawIndexed(
123 const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override;
124 void DrawInstanced(
125 const uint32_t firstVertex, const uint32_t vertexCount,
126 const uint32_t firstInstance, const uint32_t instanceCount) override;
128 const uint32_t firstIndex, const uint32_t indexCount,
129 const uint32_t firstInstance, const uint32_t instanceCount,
130 const int32_t vertexOffset) override;
132 const uint32_t firstIndex, const uint32_t indexCount,
133 const uint32_t start, const uint32_t end) override;
134
135 void BeginComputePass() override;
136 void EndComputePass() override;
137
138 void Dispatch(
139 const uint32_t groupCountX,
140 const uint32_t groupCountY,
141 const uint32_t groupCountZ) override;
142
144 const uint32_t srcStageMask, const uint32_t dstStageMask,
145 const uint32_t srcAccessMask, const uint32_t dstAccessMask) override;
146
147 void SetTexture(const int32_t bindingSlot, ITexture* texture) override;
148
149 void SetStorageTexture(const int32_t bindingSlot, ITexture* texture) override;
150 void SetStorageBuffer(const int32_t bindingSlot, IBuffer* buffer) override;
151
152 void SetUniform(
153 const int32_t bindingSlot,
154 const float value) override;
155 void SetUniform(
156 const int32_t bindingSlot,
157 const float valueX, const float valueY) override;
158 void SetUniform(
159 const int32_t bindingSlot,
160 const float valueX, const float valueY,
161 const float valueZ) override;
162 void SetUniform(
163 const int32_t bindingSlot,
164 const float valueX, const float valueY,
165 const float valueZ, const float valueW) override;
166 void SetUniform(
167 const int32_t bindingSlot, std::span<const float> values) override;
168
169 void InsertTimestampQuery(const uint32_t handle, const bool isScopeBegin) override;
170
171 void BeginScopedLabel(const char* name) override;
172 void EndScopedLabel() override;
173
174 void Flush() override;
175
176 // We need to know when to invalidate our texture bind cache.
177 void OnTextureDestroy(CTexture* texture);
178
179private:
180 friend class CDevice;
181 friend class CTexture;
182
183 static std::unique_ptr<CDeviceCommandContext> Create(CDevice* device);
184
186
187 void ResetStates();
188
190 const SGraphicsPipelineStateDesc& pipelineStateDesc, const bool force);
191
192 void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle);
193 void BindBuffer(const IBuffer::Type type, CBuffer* buffer);
194
195 CDevice* m_Device = nullptr;
196
200 uint32_t m_ScissorCount = 0;
201 // GL2.1 doesn't support more than 1 scissor.
202 std::array<Rect, 1> m_Scissors;
203
205
206 uint32_t m_ScopedLabelDepth = 0;
207
210 const void* m_IndexBufferData = nullptr;
211
213 bool m_InsidePass = false;
215
217 struct BindUnit
218 {
219 GLenum target;
220 GLuint handle;
221 };
222 std::array<BindUnit, 16> m_BoundTextures;
224 {
225 public:
226 ScopedBind(CDeviceCommandContext* deviceCommandContext,
227 const GLenum target, const GLuint handle);
228
229 ~ScopedBind();
230 private:
234 };
235
236 using BoundBuffer = std::pair<GLenum, GLuint>;
237 std::array<BoundBuffer, 4> m_BoundBuffers;
239 {
240 public:
242 CDeviceCommandContext* deviceCommandContext, CBuffer* buffer);
243
245 private:
247 size_t m_CacheIndex = 0;
248 };
249
251 {
253 uint32_t offset;
254 uint32_t stride;
256 uint32_t bindingSlot;
257
258 bool active;
260 };
261 std::array<
264};
265
266} // namespace GL
267
268} // namespace Backend
269
270} // namespace Renderer
271
272#endif // INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
A wrapper for backend shader program to handle high-level operations like file reloading and handling...
Definition: ShaderProgram.h:38
Represents a texture object.
Definition: TextureManager.h:271
Definition: Buffer.h:45
Definition: DeviceCommandContext.h:224
~ScopedBind()
Definition: DeviceCommandContext.cpp:1424
BindUnit m_OldBindUnit
Definition: DeviceCommandContext.h:232
ScopedBind(CDeviceCommandContext *deviceCommandContext, const GLenum target, const GLuint handle)
Definition: DeviceCommandContext.cpp:1413
CDeviceCommandContext * m_DeviceCommandContext
Definition: DeviceCommandContext.h:231
uint32_t m_ActiveTextureUnit
Definition: DeviceCommandContext.h:233
ScopedBufferBind(CDeviceCommandContext *deviceCommandContext, CBuffer *buffer)
Definition: DeviceCommandContext.cpp:1430
size_t m_CacheIndex
Definition: DeviceCommandContext.h:247
CDeviceCommandContext * m_DeviceCommandContext
Definition: DeviceCommandContext.h:246
~ScopedBufferBind()
Definition: DeviceCommandContext.cpp:1452
Definition: DeviceCommandContext.h:60
void ReadbackFramebufferSync(ISwapChain &swapChain, 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:1000
void ResetStates()
Definition: DeviceCommandContext.cpp:614
void SetComputePipelineState(IComputePipelineState *pipelineState) override
Binds the graphics pipeline state.
Definition: DeviceCommandContext.cpp:289
void DrawInstanced(const uint32_t firstVertex, const uint32_t vertexCount, const uint32_t firstInstance, const uint32_t instanceCount) override
Definition: DeviceCommandContext.cpp:1171
void SetVertexBufferData(const uint32_t bindingSlot, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:1081
uint32_t m_ActiveTextureUnit
Definition: DeviceCommandContext.h:216
void DrawIndexed(const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override
Definition: DeviceCommandContext.cpp:1150
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:325
bool m_InsidePass
Definition: DeviceCommandContext.h:213
bool m_InsideFramebufferPass
Definition: DeviceCommandContext.h:212
std::array< BoundBuffer, 4 > m_BoundBuffers
Definition: DeviceCommandContext.h:237
void EndFramebufferPass() override
Finishes a framebuffer pass, performs attachment store operations.
Definition: DeviceCommandContext.cpp:980
void UploadBufferRegion(IBuffer *buffer, const void *data, const uint32_t dataOffset, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:476
void EndScopedLabel() override
Definition: DeviceCommandContext.cpp:528
void Dispatch(const uint32_t groupCountX, const uint32_t groupCountY, const uint32_t groupCountZ) override
Dispatches groupCountX * groupCountY * groupCountZ compute groups.
Definition: DeviceCommandContext.cpp:1252
std::array< VertexAttributeFormat, static_cast< size_t >(VertexAttributeStream::UV7)+1 > m_VertexAttributeFormat
Definition: DeviceCommandContext.h:263
void SetIndexBuffer(IBuffer *buffer) override
Definition: DeviceCommandContext.cpp:1106
SComputePipelineStateDesc m_ComputePipelineStateDesc
Definition: DeviceCommandContext.h:204
void Flush() override
Definition: DeviceCommandContext.cpp:592
void BeginFramebufferPass(IFramebuffer *framebuffer) override
Starts a framebuffer pass, performs attachment load operations.
Definition: DeviceCommandContext.cpp:951
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:313
uint32_t m_ScissorCount
Definition: DeviceCommandContext.h:200
void BindBuffer(const IBuffer::Type type, CBuffer *buffer)
Definition: DeviceCommandContext.cpp:561
void OnTextureDestroy(CTexture *texture)
Definition: DeviceCommandContext.cpp:584
void ResolveFramebuffer(IFramebuffer *sourceFramebuffer, IFramebuffer *destinationFramebuffer) override
Resolves multisample source framebuffer attachments to destination attachments.
Definition: DeviceCommandContext.cpp:891
std::function< void(std::uint8_t *)> UploadBufferFunction
Definition: DeviceCommandContext.h:95
void InsertMemoryBarrier(const uint32_t srcStageMask, const uint32_t dstStageMask, const uint32_t srcAccessMask, const uint32_t dstAccessMask) override
Inserts a memory barrier which guarantees that all memory accesses matched by srcAccessMask in src ar...
Definition: DeviceCommandContext.cpp:1270
void SetVertexInputLayout(IVertexInputLayout *vertexInputLayout) override
Binds the vertex input layout.
Definition: DeviceCommandContext.cpp:1037
void SetIndexBufferData(const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:1114
CDeviceCommandContext(CDevice *device)
Definition: DeviceCommandContext.cpp:232
CDevice * m_Device
Definition: DeviceCommandContext.h:195
void SetStorageBuffer(const int32_t bindingSlot, IBuffer *buffer) override
Definition: DeviceCommandContext.cpp:1359
CFramebuffer * m_Framebuffer
Definition: DeviceCommandContext.h:198
void BeginPass() override
Definition: DeviceCommandContext.cpp:1125
bool m_InsideComputePass
Definition: DeviceCommandContext.h:214
void SetStorageTexture(const int32_t bindingSlot, ITexture *texture) override
Sets a read & write resource to the binding slot.
Definition: DeviceCommandContext.cpp:1341
IDevice * GetDevice() override
Definition: DeviceCommandContext.cpp:268
static std::unique_ptr< CDeviceCommandContext > Create(CDevice *device)
Definition: DeviceCommandContext.cpp:225
uint32_t m_ScopedLabelDepth
Definition: DeviceCommandContext.h:206
void InsertTimestampQuery(const uint32_t handle, const bool isScopeBegin) override
Insert a timestamp query which can be later requested via IDevice.
Definition: DeviceCommandContext.cpp:512
CShaderProgram * m_ShaderProgram
Definition: DeviceCommandContext.h:199
void EndComputePass() override
Finishes a compute pass.
Definition: DeviceCommandContext.cpp:1246
CBuffer * m_VertexBuffer
Definition: DeviceCommandContext.h:208
std::pair< GLenum, GLuint > BoundBuffer
Definition: DeviceCommandContext.h:236
void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle)
Definition: DeviceCommandContext.cpp:538
void DrawIndexedInRange(const uint32_t firstIndex, const uint32_t indexCount, const uint32_t start, const uint32_t end) override
Definition: DeviceCommandContext.cpp:1218
void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override
Definition: DeviceCommandContext.cpp:1137
void SetGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineState)
Definition: DeviceCommandContext.cpp:273
void SetUniform(const int32_t bindingSlot, const float value) override
Definition: DeviceCommandContext.cpp:1372
void EndPass() override
Definition: DeviceCommandContext.cpp:1131
std::array< BindUnit, 16 > m_BoundTextures
Definition: DeviceCommandContext.h:222
void BlitFramebuffer(IFramebuffer *sourceFramebuffer, IFramebuffer *destinationFramebuffer, const Rect &sourceRegion, const Rect &destinationRegion, const Sampler::Filter filter) override
Copies source region into destination region automatically applying compatible format conversion and ...
Definition: DeviceCommandContext.cpp:866
void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override
Clears all mentioned attachments.
Definition: DeviceCommandContext.cpp:915
void SetTexture(const int32_t bindingSlot, ITexture *texture) override
Sets a read-only texture to the binding slot.
Definition: DeviceCommandContext.cpp:1299
const void * m_IndexBufferData
Definition: DeviceCommandContext.h:210
void UploadBuffer(IBuffer *buffer, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:463
CBuffer * m_IndexBuffer
Definition: DeviceCommandContext.h:209
void SetViewports(const uint32_t viewportCount, const Rect *viewports) override
Definition: DeviceCommandContext.cpp:1030
std::array< Rect, 1 > m_Scissors
Definition: DeviceCommandContext.h:202
void BeginScopedLabel(const char *name) override
Definition: DeviceCommandContext.cpp:519
void BeginComputePass() override
Starts a compute pass, can't be called inside a framebuffer pass.
Definition: DeviceCommandContext.cpp:1239
SGraphicsPipelineStateDesc m_GraphicsPipelineStateDesc
Definition: DeviceCommandContext.h:197
void SetScissors(const uint32_t scissorCount, const Rect *scissors) override
Definition: DeviceCommandContext.cpp:1008
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:1189
void SetGraphicsPipelineStateImpl(const SGraphicsPipelineStateDesc &pipelineStateDesc, const bool force)
Definition: DeviceCommandContext.cpp:622
void SetVertexBuffer(const uint32_t bindingSlot, IBuffer *buffer, const uint32_t offset) override
Definition: DeviceCommandContext.cpp:1059
Definition: Device.h:62
Definition: Framebuffer.h:46
A compiled vertex+fragment shader program.
Definition: ShaderProgram.h:89
Represents a low-level GL texture, encapsulates all properties initialization.
Definition: Texture.h:51
Definition: IBuffer.h:32
Type
Definition: IBuffer.h:35
A holder for precompiled compute pipeline description.
Definition: PipelineState.h:209
Definition: IDeviceCommandContext.h:46
Definition: IDevice.h:64
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:87
A holder for precompiled graphics pipeline description.
Definition: PipelineState.h:200
Definition: ISwapChain.h:33
Definition: ITexture.h:33
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:78
Definition: Buffer.cpp:33
Filter
Definition: Sampler.h:33
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
Backend
Definition: Backend.h:28
VertexAttributeRate
Definition: IShaderProgram.h:52
Definition: Canvas2D.h:30
Definition: DeviceCommandContext.h:218
GLenum target
Definition: DeviceCommandContext.h:219
GLuint handle
Definition: DeviceCommandContext.h:220
VertexAttributeRate rate
Definition: DeviceCommandContext.h:255
uint32_t stride
Definition: DeviceCommandContext.h:254
uint32_t bindingSlot
Definition: DeviceCommandContext.h:256
bool initialized
Definition: DeviceCommandContext.h:259
Format format
Definition: DeviceCommandContext.h:252
uint32_t offset
Definition: DeviceCommandContext.h:253
Definition: IDeviceCommandContext.h:62
Definition: PipelineState.h:177
Definition: PipelineState.h:167