Line data Source code
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"
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"
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 :
49 0 : class CDeviceCommandContext final : public IDeviceCommandContext
50 : {
51 : public:
52 : ~CDeviceCommandContext();
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;
65 : void ReadbackFramebufferSync(
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 :
90 : void SetVertexInputLayout(
91 : IVertexInputLayout* vertexInputLayout) override;
92 :
93 : void SetVertexBuffer(
94 : const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) override;
95 : void SetVertexBufferData(
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;
110 : void DrawIndexedInstanced(
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 :
151 : CDeviceCommandContext(CDevice* device);
152 :
153 : void ResetStates();
154 :
155 : void SetGraphicsPipelineStateImpl(
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 :
163 : SGraphicsPipelineStateDesc m_GraphicsPipelineStateDesc{};
164 : CFramebuffer* m_Framebuffer = nullptr;
165 : CShaderProgram* m_ShaderProgram = nullptr;
166 : uint32_t m_ScissorCount = 0;
167 : // GL2.1 doesn't support more than 1 scissor.
168 : std::array<Rect, 1> m_Scissors;
169 :
170 : uint32_t m_ScopedLabelDepth = 0;
171 :
172 : CBuffer* m_VertexBuffer = nullptr;
173 : CBuffer* m_IndexBuffer = nullptr;
174 : const void* m_IndexBufferData = nullptr;
175 :
176 : bool m_InsideFramebufferPass = false;
177 : bool m_InsidePass = false;
178 :
179 : uint32_t m_ActiveTextureUnit = 0;
180 : struct BindUnit
181 : {
182 : GLenum target;
183 : GLuint handle;
184 : };
185 : std::array<BindUnit, 16> m_BoundTextures;
186 : class ScopedBind
187 : {
188 : public:
189 : ScopedBind(CDeviceCommandContext* deviceCommandContext,
190 : const GLenum target, const GLuint handle);
191 :
192 : ~ScopedBind();
193 : private:
194 : CDeviceCommandContext* m_DeviceCommandContext = nullptr;
195 : BindUnit m_OldBindUnit;
196 : uint32_t m_ActiveTextureUnit = 0;
197 : };
198 :
199 : using BoundBuffer = std::pair<GLenum, GLuint>;
200 : std::array<BoundBuffer, 2> m_BoundBuffers;
201 : class ScopedBufferBind
202 : {
203 : public:
204 : ScopedBufferBind(
205 : CDeviceCommandContext* deviceCommandContext, CBuffer* buffer);
206 :
207 : ~ScopedBufferBind();
208 : private:
209 : CDeviceCommandContext* m_DeviceCommandContext = nullptr;
210 : size_t m_CacheIndex = 0;
211 : };
212 :
213 : struct VertexAttributeFormat
214 : {
215 : Format format;
216 : uint32_t offset;
217 : uint32_t stride;
218 : VertexAttributeRate rate;
219 : uint32_t bindingSlot;
220 :
221 : bool active;
222 : bool initialized;
223 : };
224 : std::array<
225 : VertexAttributeFormat,
226 : static_cast<size_t>(VertexAttributeStream::UV7) + 1> m_VertexAttributeFormat;
227 : };
228 :
229 : } // namespace GL
230 :
231 : } // namespace Backend
232 :
233 : } // namespace Renderer
234 :
235 : #endif // INCLUDED_RENDERER_BACKEND_GL_DEVICECOMMANDCONTEXT
|