Adds BeginFramebufferPass/EndFramebufferPass instead of SetFramebuffer.
[0ad.git] / source / renderer / backend / gl / DeviceCommandContext.h
blob93ee26003657ed25fd197a99a85072f21fbb05ca
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
19 #define INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
21 #include "lib/ogl.h"
22 #include "ps/containers/Span.h"
23 #include "renderer/backend/Format.h"
24 #include "renderer/backend/gl/Buffer.h"
25 #include "renderer/backend/IDeviceCommandContext.h"
26 #include "renderer/backend/PipelineState.h"
28 #include <array>
29 #include <cstdint>
30 #include <functional>
31 #include <memory>
32 #include <optional>
33 #include <utility>
35 namespace Renderer
38 namespace Backend
41 namespace GL
44 class CDevice;
45 class CFramebuffer;
46 class CShaderProgram;
47 class CTexture;
49 class CDeviceCommandContext final : public IDeviceCommandContext
51 public:
52 ~CDeviceCommandContext();
54 IDevice* GetDevice() override;
56 void SetGraphicsPipelineState(const GraphicsPipelineStateDesc& pipelineStateDesc) override;
58 void BlitFramebuffer(IFramebuffer* destinationFramebuffer, IFramebuffer* sourceFramebuffer) override;
60 void ClearFramebuffer() override;
61 void ClearFramebuffer(const bool color, const bool depth, const bool stencil) override;
62 void BeginFramebufferPass(IFramebuffer* framebuffer) override;
63 void EndFramebufferPass() override;
64 void ReadbackFramebufferSync(
65 const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height,
66 void* data) override;
68 void UploadTexture(ITexture* texture, const Format dataFormat,
69 const void* data, const size_t dataSize,
70 const uint32_t level = 0, const uint32_t layer = 0) override;
71 void UploadTextureRegion(ITexture* texture, const Format dataFormat,
72 const void* data, const size_t dataSize,
73 const uint32_t xOffset, const uint32_t yOffset,
74 const uint32_t width, const uint32_t height,
75 const uint32_t level = 0, const uint32_t layer = 0) override;
77 using UploadBufferFunction = std::function<void(u8*)>;
78 void UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize) override;
79 void UploadBuffer(IBuffer* buffer, const UploadBufferFunction& uploadFunction) override;
80 void UploadBufferRegion(
81 IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize) override;
82 void UploadBufferRegion(
83 IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
84 const UploadBufferFunction& uploadFunction) override;
86 void SetScissors(const uint32_t scissorCount, const Rect* scissors) override;
87 void SetViewports(const uint32_t viewportCount, const Rect* viewports) override;
89 void SetVertexAttributeFormat(
90 const VertexAttributeStream stream,
91 const Format format,
92 const uint32_t offset,
93 const uint32_t stride,
94 const VertexAttributeRate rate,
95 const uint32_t bindingSlot) override;
96 void SetVertexBuffer(
97 const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) override;
98 void SetVertexBufferData(
99 const uint32_t bindingSlot, const void* data, const uint32_t dataSize) override;
101 void SetIndexBuffer(IBuffer* buffer) override;
102 void SetIndexBufferData(const void* data, const uint32_t dataSize) override;
104 void BeginPass() override;
105 void EndPass() override;
107 void Draw(const uint32_t firstVertex, const uint32_t vertexCount) override;
108 void DrawIndexed(
109 const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) override;
110 void DrawInstanced(
111 const uint32_t firstVertex, const uint32_t vertexCount,
112 const uint32_t firstInstance, const uint32_t instanceCount) override;
113 void DrawIndexedInstanced(
114 const uint32_t firstIndex, const uint32_t indexCount,
115 const uint32_t firstInstance, const uint32_t instanceCount,
116 const int32_t vertexOffset) override;
117 void DrawIndexedInRange(
118 const uint32_t firstIndex, const uint32_t indexCount,
119 const uint32_t start, const uint32_t end) override;
121 void SetTexture(const int32_t bindingSlot, ITexture* texture) override;
123 void SetUniform(
124 const int32_t bindingSlot,
125 const float value) override;
126 void SetUniform(
127 const int32_t bindingSlot,
128 const float valueX, const float valueY) override;
129 void SetUniform(
130 const int32_t bindingSlot,
131 const float valueX, const float valueY,
132 const float valueZ) override;
133 void SetUniform(
134 const int32_t bindingSlot,
135 const float valueX, const float valueY,
136 const float valueZ, const float valueW) override;
137 void SetUniform(
138 const int32_t bindingSlot, PS::span<const float> values) override;
140 void BeginScopedLabel(const char* name) override;
141 void EndScopedLabel() override;
143 void Flush() override;
145 // We need to know when to invalidate our texture bind cache.
146 void OnTextureDestroy(CTexture* texture);
148 private:
149 friend class CDevice;
150 friend class CTexture;
152 static std::unique_ptr<CDeviceCommandContext> Create(CDevice* device);
154 CDeviceCommandContext(CDevice* device);
156 void ResetStates();
158 void SetGraphicsPipelineStateImpl(
159 const GraphicsPipelineStateDesc& pipelineStateDesc, const bool force);
161 void BindTexture(const uint32_t unit, const GLenum target, const GLuint handle);
162 void BindBuffer(const IBuffer::Type type, CBuffer* buffer);
164 CDevice* m_Device = nullptr;
166 GraphicsPipelineStateDesc m_GraphicsPipelineStateDesc{};
167 CFramebuffer* m_Framebuffer = nullptr;
168 CShaderProgram* m_ShaderProgram = nullptr;
169 uint32_t m_ScissorCount = 0;
170 // GL2.1 doesn't support more than 1 scissor.
171 std::array<Rect, 1> m_Scissors;
173 uint32_t m_ScopedLabelDepth = 0;
175 CBuffer* m_VertexBuffer = nullptr;
176 CBuffer* m_IndexBuffer = nullptr;
177 const void* m_IndexBufferData = nullptr;
179 bool m_InsideFramebufferPass = false;
180 bool m_InsidePass = false;
182 uint32_t m_ActiveTextureUnit = 0;
183 struct BindUnit
185 GLenum target;
186 GLuint handle;
188 std::array<BindUnit, 16> m_BoundTextures;
189 class ScopedBind
191 public:
192 ScopedBind(CDeviceCommandContext* deviceCommandContext,
193 const GLenum target, const GLuint handle);
195 ~ScopedBind();
196 private:
197 CDeviceCommandContext* m_DeviceCommandContext = nullptr;
198 BindUnit m_OldBindUnit;
199 uint32_t m_ActiveTextureUnit = 0;
202 using BoundBuffer = std::pair<GLenum, GLuint>;
203 std::array<BoundBuffer, 2> m_BoundBuffers;
204 class ScopedBufferBind
206 public:
207 ScopedBufferBind(
208 CDeviceCommandContext* deviceCommandContext, CBuffer* buffer);
210 ~ScopedBufferBind();
211 private:
212 CDeviceCommandContext* m_DeviceCommandContext = nullptr;
213 size_t m_CacheIndex = 0;
216 struct VertexAttributeFormat
218 Format format;
219 uint32_t offset;
220 uint32_t stride;
221 VertexAttributeRate rate;
222 uint32_t bindingSlot;
224 bool active;
225 bool initialized;
227 std::array<
228 VertexAttributeFormat,
229 static_cast<size_t>(VertexAttributeStream::UV7) + 1> m_VertexAttributeFormat;
232 } // namespace GL
234 } // namespace Backend
236 } // namespace Renderer
238 #endif // INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT