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_BACKEND_GL_DEVICECOMMANDCONTEXT
19#define INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
20
21#include "lib/ogl.h"
22#include "ps/containers/Span.h"
27
28#include <array>
29#include <cstdint>
30#include <functional>
31#include <memory>
32#include <optional>
33#include <utility>
34
35namespace Renderer
36{
37
38namespace Backend
39{
40
41namespace GL
42{
43
44class CDevice;
45class CFramebuffer;
46class CShaderProgram;
47class CTexture;
48
50{
51public:
53
54 IDevice* GetDevice() override;
55
57
58 void SetGraphicsPipelineState(IGraphicsPipelineState* pipelineState) override;
59 void SetComputePipelineState(IComputePipelineState* pipelineState) override;
60
61 void BlitFramebuffer(
62 IFramebuffer* sourceFramebuffer, IFramebuffer* destinationFramebuffer,
63 const Rect& sourceRegion, const Rect& destinationRegion,
64 const Sampler::Filter filter) override;
66 IFramebuffer* sourceFramebuffer, IFramebuffer* destinationFramebuffer) override;
67
68 void BeginFramebufferPass(IFramebuffer* framebuffer) override;
69 void EndFramebufferPass() override;
70 void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override;
72 const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height,
73 void* data) override;
74
75 void UploadTexture(ITexture* texture, const Format dataFormat,
76 const void* data, const size_t dataSize,
77 const uint32_t level = 0, const uint32_t layer = 0) override;
78 void UploadTextureRegion(ITexture* texture, const Format dataFormat,
79 const void* data, const size_t dataSize,
80 const uint32_t xOffset, const uint32_t yOffset,
81 const uint32_t width, const uint32_t height,
82 const uint32_t level = 0, const uint32_t layer = 0) override;
83
84 using UploadBufferFunction = std::function<void(u8*)>;
85 void UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize) override;
86 void UploadBuffer(IBuffer* buffer, const UploadBufferFunction& uploadFunction) override;
88 IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize) override;
90 IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
91 const UploadBufferFunction& uploadFunction) override;
92
93 void SetScissors(const uint32_t scissorCount, const Rect* scissors) override;
94 void SetViewports(const uint32_t viewportCount, const Rect* viewports) override;
95
97 IVertexInputLayout* vertexInputLayout) override;
98
99 void SetVertexBuffer(
100 const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) override;
102 const uint32_t bindingSlot, const void* data, const uint32_t dataSize) override;
103
104 void SetIndexBuffer(IBuffer* buffer) override;
105 void SetIndexBufferData(const void* data, const uint32_t dataSize) override;
106
107 void BeginPass() override;
108 void EndPass() override;
109
110 void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override;
111 void DrawIndexed(
112 const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override;
113 void DrawInstanced(
114 const uint32_t firstVertex, const uint32_t vertexCount,
115 const uint32_t firstInstance, const uint32_t instanceCount) override;
117 const uint32_t firstIndex, const uint32_t indexCount,
118 const uint32_t firstInstance, const uint32_t instanceCount,
119 const int32_t vertexOffset) override;
121 const uint32_t firstIndex, const uint32_t indexCount,
122 const uint32_t start, const uint32_t end) override;
123
124 void BeginComputePass() override;
125 void EndComputePass() override;
126
127 void Dispatch(
128 const uint32_t groupCountX,
129 const uint32_t groupCountY,
130 const uint32_t groupCountZ) override;
131
132 void SetTexture(const int32_t bindingSlot, ITexture* texture) override;
133
134 void SetStorageTexture(const int32_t bindingSlot, ITexture* texture) override;
135
136 void SetUniform(
137 const int32_t bindingSlot,
138 const float value) override;
139 void SetUniform(
140 const int32_t bindingSlot,
141 const float valueX, const float valueY) override;
142 void SetUniform(
143 const int32_t bindingSlot,
144 const float valueX, const float valueY,
145 const float valueZ) override;
146 void SetUniform(
147 const int32_t bindingSlot,
148 const float valueX, const float valueY,
149 const float valueZ, const float valueW) override;
150 void SetUniform(
151 const int32_t bindingSlot, PS::span<const float> values) override;
152
153 void BeginScopedLabel(const char* name) override;
154 void EndScopedLabel() override;
155
156 void Flush() override;
157
158 // We need to know when to invalidate our texture bind cache.
159 void OnTextureDestroy(CTexture* texture);
160
161private:
162 friend class CDevice;
163 friend class CTexture;
164
165 static std::unique_ptr<CDeviceCommandContext> Create(CDevice* device);
166
168
169 void ResetStates();
170
172 const SGraphicsPipelineStateDesc& pipelineStateDesc, const bool force);
173
174 void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle);
175 void BindBuffer(const IBuffer::Type type, CBuffer* buffer);
176
177 CDevice* m_Device = nullptr;
178
183 // GL2.1 doesn't support more than 1 scissor.
184 std::array<Rect, 1> m_Scissors;
185
187
189
192 const void* m_IndexBufferData = nullptr;
193
195 bool m_InsidePass = false;
197
199 struct BindUnit
200 {
201 GLenum target;
202 GLuint handle;
203 };
204 std::array<BindUnit, 16> m_BoundTextures;
206 {
207 public:
208 ScopedBind(CDeviceCommandContext* deviceCommandContext,
209 const GLenum target, const GLuint handle);
210
211 ~ScopedBind();
212 private:
216 };
217
218 using BoundBuffer = std::pair<GLenum, GLuint>;
219 std::array<BoundBuffer, 2> m_BoundBuffers;
221 {
222 public:
224 CDeviceCommandContext* deviceCommandContext, CBuffer* buffer);
225
227 private:
229 size_t m_CacheIndex = 0;
230 };
231
233 {
239
240 bool active;
242 };
243 std::array<
246};
247
248} // namespace GL
249
250} // namespace Backend
251
252} // namespace Renderer
253
254#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:34
Represents a texture object.
Definition: TextureManager.h:262
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
Definition: Buffer.h:39
Definition: DeviceCommandContext.h:206
~ScopedBind()
Definition: DeviceCommandContext.cpp:1387
BindUnit m_OldBindUnit
Definition: DeviceCommandContext.h:214
ScopedBind(CDeviceCommandContext *deviceCommandContext, const GLenum target, const GLuint handle)
Definition: DeviceCommandContext.cpp:1376
CDeviceCommandContext * m_DeviceCommandContext
Definition: DeviceCommandContext.h:213
uint32_t m_ActiveTextureUnit
Definition: DeviceCommandContext.h:215
ScopedBufferBind(CDeviceCommandContext *deviceCommandContext, CBuffer *buffer)
Definition: DeviceCommandContext.cpp:1393
size_t m_CacheIndex
Definition: DeviceCommandContext.h:229
CDeviceCommandContext * m_DeviceCommandContext
Definition: DeviceCommandContext.h:228
~ScopedBufferBind()
Definition: DeviceCommandContext.cpp:1414
Definition: DeviceCommandContext.h:50
void ResetStates()
Definition: DeviceCommandContext.cpp:577
void SetComputePipelineState(IComputePipelineState *pipelineState) override
Binds the graphics pipeline state.
Definition: DeviceCommandContext.cpp:273
void DrawInstanced(const uint32_t firstVertex, const uint32_t vertexCount, const uint32_t firstInstance, const uint32_t instanceCount) override
Definition: DeviceCommandContext.cpp:1163
void SetVertexBufferData(const uint32_t bindingSlot, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:1071
uint32_t m_ActiveTextureUnit
Definition: DeviceCommandContext.h:198
void DrawIndexed(const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override
Definition: DeviceCommandContext.cpp:1141
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:309
bool m_InsidePass
Definition: DeviceCommandContext.h:195
bool m_InsideFramebufferPass
Definition: DeviceCommandContext.h:194
void EndFramebufferPass() override
Finishes a framebuffer pass, performs attachment store operations.
Definition: DeviceCommandContext.cpp:961
void UploadBufferRegion(IBuffer *buffer, const void *data, const uint32_t dataOffset, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:446
std::array< BoundBuffer, 2 > m_BoundBuffers
Definition: DeviceCommandContext.h:219
void EndScopedLabel() override
Definition: DeviceCommandContext.cpp:489
void Dispatch(const uint32_t groupCountX, const uint32_t groupCountY, const uint32_t groupCountZ) override
Dispatches groupCountX * groupCountY * groupCountZ compute groups.
Definition: DeviceCommandContext.cpp:1255
std::array< VertexAttributeFormat, static_cast< size_t >(VertexAttributeStream::UV7)+1 > m_VertexAttributeFormat
Definition: DeviceCommandContext.h:245
void SetIndexBuffer(IBuffer *buffer) override
Definition: DeviceCommandContext.cpp:1096
SComputePipelineStateDesc m_ComputePipelineStateDesc
Definition: DeviceCommandContext.h:186
void Flush() override
Definition: DeviceCommandContext.cpp:555
void BeginFramebufferPass(IFramebuffer *framebuffer) override
Starts a framebuffer pass, performs attachment load operations.
Definition: DeviceCommandContext.cpp:931
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:297
uint32_t m_ScissorCount
Definition: DeviceCommandContext.h:182
void BindBuffer(const IBuffer::Type type, CBuffer *buffer)
Definition: DeviceCommandContext.cpp:523
void OnTextureDestroy(CTexture *texture)
Definition: DeviceCommandContext.cpp:547
void ResolveFramebuffer(IFramebuffer *sourceFramebuffer, IFramebuffer *destinationFramebuffer) override
Resolves multisample source framebuffer attachments to destination attachments.
Definition: DeviceCommandContext.cpp:867
void SetVertexInputLayout(IVertexInputLayout *vertexInputLayout) override
Binds the vertex input layout.
Definition: DeviceCommandContext.cpp:1027
void SetIndexBufferData(const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:1104
CDeviceCommandContext(CDevice *device)
Definition: DeviceCommandContext.cpp:223
CDevice * m_Device
Definition: DeviceCommandContext.h:177
CFramebuffer * m_Framebuffer
Definition: DeviceCommandContext.h:180
void BeginPass() override
Definition: DeviceCommandContext.cpp:1115
bool m_InsideComputePass
Definition: DeviceCommandContext.h:196
void SetStorageTexture(const int32_t bindingSlot, ITexture *texture) override
Sets a read & write resource to the binding slot.
Definition: DeviceCommandContext.cpp:1315
IDevice * GetDevice() override
Definition: DeviceCommandContext.cpp:252
static std::unique_ptr< CDeviceCommandContext > Create(CDevice *device)
Definition: DeviceCommandContext.cpp:211
uint32_t m_ScopedLabelDepth
Definition: DeviceCommandContext.h:188
std::function< void(u8 *)> UploadBufferFunction
Definition: DeviceCommandContext.h:84
CShaderProgram * m_ShaderProgram
Definition: DeviceCommandContext.h:181
void EndComputePass() override
Finishes a compute pass.
Definition: DeviceCommandContext.cpp:1249
CBuffer * m_VertexBuffer
Definition: DeviceCommandContext.h:190
std::pair< GLenum, GLuint > BoundBuffer
Definition: DeviceCommandContext.h:218
void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle)
Definition: DeviceCommandContext.cpp:499
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:1127
void SetGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineState)
Definition: DeviceCommandContext.cpp:257
void SetUniform(const int32_t bindingSlot, const float value) override
Definition: DeviceCommandContext.cpp:1335
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:987
void EndPass() override
Definition: DeviceCommandContext.cpp:1121
std::array< BindUnit, 16 > m_BoundTextures
Definition: DeviceCommandContext.h:204
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:836
void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override
Clears all mentioned attachments.
Definition: DeviceCommandContext.cpp:894
void SetTexture(const int32_t bindingSlot, ITexture *texture) override
Sets a read-only texture to the binding slot.
Definition: DeviceCommandContext.cpp:1273
const void * m_IndexBufferData
Definition: DeviceCommandContext.h:192
void UploadBuffer(IBuffer *buffer, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:433
CBuffer * m_IndexBuffer
Definition: DeviceCommandContext.h:191
void SetViewports(const uint32_t viewportCount, const Rect *viewports) override
Definition: DeviceCommandContext.cpp:1019
std::array< Rect, 1 > m_Scissors
Definition: DeviceCommandContext.h:184
void BeginScopedLabel(const char *name) override
Definition: DeviceCommandContext.cpp:480
void BeginComputePass() override
Starts a compute pass, can't be called inside a framebuffer pass.
Definition: DeviceCommandContext.cpp:1242
SGraphicsPipelineStateDesc m_GraphicsPipelineStateDesc
Definition: DeviceCommandContext.h:179
void SetScissors(const uint32_t scissorCount, const Rect *scissors) override
Definition: DeviceCommandContext.cpp:996
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:1185
void SetGraphicsPipelineStateImpl(const SGraphicsPipelineStateDesc &pipelineStateDesc, const bool force)
Definition: DeviceCommandContext.cpp:590
void SetVertexBuffer(const uint32_t bindingSlot, IBuffer *buffer, const uint32_t offset) override
Definition: DeviceCommandContext.cpp:1049
Definition: Device.h:51
Definition: Framebuffer.h:41
A compiled vertex+fragment shader program.
Definition: ShaderProgram.h:89
Represents a low-level GL texture, encapsulates all properties initialization.
Definition: Texture.h:43
Definition: IBuffer.h:32
Type
Definition: IBuffer.h:35
A holder for precompiled compute pipeline description.
Definition: PipelineState.h:207
Definition: IDeviceCommandContext.h:42
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
Filter
Definition: Sampler.h:36
Format
Definition: Format.h:28
Backend
Definition: Backend.h:28
VertexAttributeRate
Definition: IShaderProgram.h:48
Definition: VideoMode.h:29
Definition: DeviceCommandContext.h:200
GLenum target
Definition: DeviceCommandContext.h:201
GLuint handle
Definition: DeviceCommandContext.h:202
VertexAttributeRate rate
Definition: DeviceCommandContext.h:237
uint32_t stride
Definition: DeviceCommandContext.h:236
uint32_t bindingSlot
Definition: DeviceCommandContext.h:238
bool initialized
Definition: DeviceCommandContext.h:241
Format format
Definition: DeviceCommandContext.h:234
uint32_t offset
Definition: DeviceCommandContext.h:235
Definition: IDeviceCommandContext.h:58
Definition: PipelineState.h:175
Definition: PipelineState.h:165
uint8_t u8
Definition: types.h:37
unsigned int uint32_t
Definition: wposix_types.h:53