Pyrogenesis  trunk
DeviceCommandContext.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 
35 namespace Renderer
36 {
37 
38 namespace Backend
39 {
40 
41 namespace GL
42 {
43 
44 class CDevice;
45 class CFramebuffer;
46 class CShaderProgram;
47 class CTexture;
48 
50 {
51 public:
53 
54  IDevice* GetDevice() override;
55 
56  void SetGraphicsPipelineState(const SGraphicsPipelineStateDesc& pipelineState);
57 
58  void SetGraphicsPipelineState(IGraphicsPipelineState* pipelineState) override;
59 
60  void BlitFramebuffer(IFramebuffer* destinationFramebuffer, IFramebuffer* sourceFramebuffer) override;
61 
62  void BeginFramebufferPass(IFramebuffer* framebuffer) override;
63  void EndFramebufferPass() override;
64  void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override;
66  const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height,
67  void* data) override;
68 
69  void UploadTexture(ITexture* texture, const Format dataFormat,
70  const void* data, const size_t dataSize,
71  const uint32_t level = 0, const uint32_t layer = 0) override;
72  void UploadTextureRegion(ITexture* texture, const Format dataFormat,
73  const void* data, const size_t dataSize,
74  const uint32_t xOffset, const uint32_t yOffset,
75  const uint32_t width, const uint32_t height,
76  const uint32_t level = 0, const uint32_t layer = 0) override;
77 
78  using UploadBufferFunction = std::function<void(u8*)>;
79  void UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize) override;
80  void UploadBuffer(IBuffer* buffer, const UploadBufferFunction& uploadFunction) override;
81  void UploadBufferRegion(
82  IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize) override;
83  void UploadBufferRegion(
84  IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
85  const UploadBufferFunction& uploadFunction) override;
86 
87  void SetScissors(const uint32_t scissorCount, const Rect* scissors) override;
88  void SetViewports(const uint32_t viewportCount, const Rect* viewports) override;
89 
91  IVertexInputLayout* vertexInputLayout) override;
92 
93  void SetVertexBuffer(
94  const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) override;
96  const uint32_t bindingSlot, const void* data, const uint32_t dataSize) override;
97 
98  void SetIndexBuffer(IBuffer* buffer) override;
99  void SetIndexBufferData(const void* data, const uint32_t dataSize) override;
100 
101  void BeginPass() override;
102  void EndPass() override;
103 
104  void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override;
105  void DrawIndexed(
106  const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override;
107  void DrawInstanced(
108  const uint32_t firstVertex, const uint32_t vertexCount,
109  const uint32_t firstInstance, const uint32_t instanceCount) override;
111  const uint32_t firstIndex, const uint32_t indexCount,
112  const uint32_t firstInstance, const uint32_t instanceCount,
113  const int32_t vertexOffset) override;
114  void DrawIndexedInRange(
115  const uint32_t firstIndex, const uint32_t indexCount,
116  const uint32_t start, const uint32_t end) override;
117 
118  void SetTexture(const int32_t bindingSlot, ITexture* texture) override;
119 
120  void SetUniform(
121  const int32_t bindingSlot,
122  const float value) override;
123  void SetUniform(
124  const int32_t bindingSlot,
125  const float valueX, const float valueY) override;
126  void SetUniform(
127  const int32_t bindingSlot,
128  const float valueX, const float valueY,
129  const float valueZ) override;
130  void SetUniform(
131  const int32_t bindingSlot,
132  const float valueX, const float valueY,
133  const float valueZ, const float valueW) override;
134  void SetUniform(
135  const int32_t bindingSlot, PS::span<const float> values) override;
136 
137  void BeginScopedLabel(const char* name) override;
138  void EndScopedLabel() override;
139 
140  void Flush() override;
141 
142  // We need to know when to invalidate our texture bind cache.
143  void OnTextureDestroy(CTexture* texture);
144 
145 private:
146  friend class CDevice;
147  friend class CTexture;
148 
149  static std::unique_ptr<CDeviceCommandContext> Create(CDevice* device);
150 
152 
153  void ResetStates();
154 
156  const SGraphicsPipelineStateDesc& pipelineStateDesc, const bool force);
157 
158  void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle);
159  void BindBuffer(const IBuffer::Type type, CBuffer* buffer);
160 
161  CDevice* m_Device = nullptr;
162 
167  // GL2.1 doesn't support more than 1 scissor.
168  std::array<Rect, 1> m_Scissors;
169 
171 
173  CBuffer* m_IndexBuffer = nullptr;
174  const void* m_IndexBufferData = nullptr;
175 
177  bool m_InsidePass = false;
178 
180  struct BindUnit
181  {
182  GLenum target;
183  GLuint handle;
184  };
185  std::array<BindUnit, 16> m_BoundTextures;
187  {
188  public:
189  ScopedBind(CDeviceCommandContext* deviceCommandContext,
190  const GLenum target, const GLuint handle);
191 
192  ~ScopedBind();
193  private:
194  CDeviceCommandContext* m_DeviceCommandContext = nullptr;
196  uint32_t m_ActiveTextureUnit = 0;
197  };
198 
199  using BoundBuffer = std::pair<GLenum, GLuint>;
200  std::array<BoundBuffer, 2> m_BoundBuffers;
202  {
203  public:
205  CDeviceCommandContext* deviceCommandContext, CBuffer* buffer);
206 
207  ~ScopedBufferBind();
208  private:
209  CDeviceCommandContext* m_DeviceCommandContext = nullptr;
210  size_t m_CacheIndex = 0;
211  };
212 
214  {
220 
221  bool active;
223  };
224  std::array<
227 };
228 
229 } // namespace GL
230 
231 } // namespace Backend
232 
233 } // namespace Renderer
234 
235 #endif // INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override
Definition: DeviceCommandContext.cpp:1066
BindUnit m_OldBindUnit
Definition: DeviceCommandContext.h:195
VertexAttributeRate
Definition: IShaderProgram.h:47
Definition: IBuffer.h:31
CShaderProgram * m_ShaderProgram
Definition: DeviceCommandContext.h:165
CDeviceCommandContext(CDevice *device)
Definition: DeviceCommandContext.cpp:223
void SetGraphicsPipelineStateImpl(const SGraphicsPipelineStateDesc &pipelineStateDesc, const bool force)
Definition: DeviceCommandContext.cpp:562
CBuffer * m_VertexBuffer
Definition: DeviceCommandContext.h:172
Definition: DeviceCommandContext.h:49
void EndPass() override
Definition: DeviceCommandContext.cpp:1060
CDevice * m_Device
Definition: DeviceCommandContext.h:161
void BeginScopedLabel(const char *name) override
Definition: DeviceCommandContext.cpp:454
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:271
void BeginPass() override
Definition: DeviceCommandContext.cpp:1054
std::pair< GLenum, GLuint > BoundBuffer
Definition: DeviceCommandContext.h:199
Definition: ITexture.h:33
Definition: Buffer.h:38
Type
Definition: IBuffer.h:34
void DrawIndexedInRange(const uint32_t firstIndex, const uint32_t indexCount, const uint32_t start, const uint32_t end) override
Definition: DeviceCommandContext.cpp:1157
Definition: Framebuffer.h:40
void ResetStates()
Definition: DeviceCommandContext.cpp:549
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:73
void BeginFramebufferPass(IFramebuffer *framebuffer) override
Starts a framebuffer pass, performs attachment load operations.
Definition: DeviceCommandContext.cpp:872
uint32_t bindingSlot
Definition: DeviceCommandContext.h:219
void SetGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineState)
Definition: DeviceCommandContext.cpp:257
void BlitFramebuffer(IFramebuffer *destinationFramebuffer, IFramebuffer *sourceFramebuffer) override
Definition: DeviceCommandContext.cpp:808
static std::unique_ptr< CDeviceCommandContext > Create(CDevice *device)
Definition: DeviceCommandContext.cpp:211
void SetViewports(const uint32_t viewportCount, const Rect *viewports) override
Definition: DeviceCommandContext.cpp:958
uint32_t m_ScissorCount
Definition: DeviceCommandContext.h:166
Definition: DeviceCommandContext.h:180
Definition: DeviceCommandContext.h:186
Format format
Definition: DeviceCommandContext.h:215
void SetVertexBuffer(const uint32_t bindingSlot, IBuffer *buffer, const uint32_t offset) override
Definition: DeviceCommandContext.cpp:988
Format
Definition: Format.h:27
void EndFramebufferPass() override
Finishes a framebuffer pass, performs attachment store operations.
Definition: DeviceCommandContext.cpp:902
std::array< Rect, 1 > m_Scissors
Definition: DeviceCommandContext.h:168
void SetIndexBufferData(const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:1043
A compiled vertex+fragment shader program.
Definition: ShaderProgram.h:88
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:1124
void ReadbackFramebufferSync(const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height, void *data) override
Definition: DeviceCommandContext.cpp:926
void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override
Clears all mentioned attachments.
Definition: DeviceCommandContext.cpp:835
void SetUniform(const int32_t bindingSlot, const float value) override
Definition: DeviceCommandContext.cpp:1223
Definition: IDevice.h:47
void BindBuffer(const IBuffer::Type type, CBuffer *buffer)
Definition: DeviceCommandContext.cpp:497
void OnTextureDestroy(CTexture *texture)
Definition: DeviceCommandContext.cpp:521
Definition: PipelineState.h:164
Represents a texture object.
Definition: TextureManager.h:256
A holder for precompiled graphics pipeline description.
Definition: PipelineState.h:190
void SetIndexBuffer(IBuffer *buffer) override
Definition: DeviceCommandContext.cpp:1035
Backend
Definition: Backend.h:27
uint32_t offset
Definition: DeviceCommandContext.h:216
CBuffer * m_IndexBuffer
Definition: DeviceCommandContext.h:173
void SetVertexBufferData(const uint32_t bindingSlot, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:1010
void SetVertexInputLayout(IVertexInputLayout *vertexInputLayout) override
Binds the vertex input layout.
Definition: DeviceCommandContext.cpp:966
uint32_t stride
Definition: DeviceCommandContext.h:217
void UploadBufferRegion(IBuffer *buffer, const void *data, const uint32_t dataOffset, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:420
void UploadBuffer(IBuffer *buffer, const void *data, const uint32_t dataSize) override
Definition: DeviceCommandContext.cpp:407
void DrawInstanced(const uint32_t firstVertex, const uint32_t vertexCount, const uint32_t firstInstance, const uint32_t instanceCount) override
Definition: DeviceCommandContext.cpp:1102
void SetTexture(const int32_t bindingSlot, ITexture *texture) override
Definition: DeviceCommandContext.cpp:1181
CFramebuffer * m_Framebuffer
Definition: DeviceCommandContext.h:164
SGraphicsPipelineStateDesc m_GraphicsPipelineStateDesc
Definition: DeviceCommandContext.h:163
const void * m_IndexBufferData
Definition: DeviceCommandContext.h:174
void Flush() override
Definition: DeviceCommandContext.cpp:529
Represents a low-level GL texture, encapsulates all properties initialization.
Definition: Texture.h:42
std::function< void(u8 *)> UploadBufferFunction
Definition: DeviceCommandContext.h:78
void SetScissors(const uint32_t scissorCount, const Rect *scissors) override
Definition: DeviceCommandContext.cpp:935
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
IDevice * GetDevice() override
Definition: DeviceCommandContext.cpp:252
bool m_InsideFramebufferPass
Definition: DeviceCommandContext.h:176
std::array< BindUnit, 16 > m_BoundTextures
Definition: DeviceCommandContext.h:185
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:283
bool initialized
Definition: DeviceCommandContext.h:222
std::array< VertexAttributeFormat, static_cast< size_t >VertexAttributeStream::UV7)+1 > m_VertexAttributeFormat
Definition: DeviceCommandContext.h:226
uint32_t m_ActiveTextureUnit
Definition: DeviceCommandContext.h:179
void DrawIndexed(const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override
Definition: DeviceCommandContext.cpp:1080
GLenum target
Definition: DeviceCommandContext.h:182
void EndScopedLabel() override
Definition: DeviceCommandContext.cpp:463
A wrapper for backend shader program to handle high-level operations like file reloading and handling...
Definition: ShaderProgram.h:33
Definition: IDeviceCommandContext.h:40
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:84
uint32_t m_ScopedLabelDepth
Definition: DeviceCommandContext.h:170
VertexAttributeRate rate
Definition: DeviceCommandContext.h:218
std::array< BoundBuffer, 2 > m_BoundBuffers
Definition: DeviceCommandContext.h:200
bool m_InsidePass
Definition: DeviceCommandContext.h:177
GLuint handle
Definition: DeviceCommandContext.h:183
void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle)
Definition: DeviceCommandContext.cpp:473
Definition: Device.h:50
Simplifed version of std::span (C++20) as we don&#39;t support the original one yet.
Definition: Span.h:36