Hymn to Beauty
C++ 3D Engine
OpenGLCommandBuffer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "../Interface/CommandBuffer.hpp"
4
5#include <vector>
6#include <glad/glad.h>
7
8namespace Video {
9
10class OpenGLRenderer;
11class OpenGLRenderPassAllocator;
12class OpenGLShaderProgram;
13
16 public:
18 struct Timing {
20 std::string name;
21
23 GLuint startQuery;
24
26 GLuint endQuery;
27 };
28
30
34 OpenGLCommandBuffer(OpenGLRenderer& openGLRenderer, OpenGLRenderPassAllocator& renderPassAllocator);
35
38
39 void BeginRenderPass(RenderPass* renderPass, const std::string& name) final;
40 void BeginRenderPass(Texture* colorAttachment, RenderPass::LoadOperation colorLoadOperation, Texture* depthAttachment, RenderPass::LoadOperation depthLoadOperation, const std::string& name) final;
41 void BeginAttachmentlessRenderPass(const glm::uvec2& size, uint32_t msaaSamples, const std::string& name) final;
42 void EndRenderPass() final;
43 void BindGraphicsPipeline(GraphicsPipeline* graphicsPipeline) final;
44 void SetViewport(const glm::uvec2& origin, const glm::uvec2& size) final;
45 void SetScissor(const glm::uvec2& origin, const glm::uvec2& size) final;
46 void SetLineWidth(float width) final;
47 void BindGeometry(GeometryBinding* geometryBinding) final;
48 void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer* uniformBuffer) final;
49 void BindStorageBuffers(std::initializer_list<Buffer*> buffers) final;
50 void BindMaterial(std::initializer_list<std::pair<Texture*, const Sampler*>> textures) final;
51 void PushConstants(const void* data) final;
52 void Draw(unsigned int vertexCount, unsigned int firstVertex) final;
53 void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final;
54 void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final;
55 void BlitToSwapChain(Texture* texture) final;
56 void BindComputePipeline(ComputePipeline* computePipeline) final;
57 void Dispatch(const glm::uvec3& numGroups, const std::string& name) final;
58 void ClearBuffer(Buffer* buffer) final;
59
61 void Submit();
62
64
67 const std::vector<Timing>& GetTimings() const;
68
69 private:
70 OpenGLCommandBuffer(const OpenGLCommandBuffer& other) = delete;
71
72 struct BeginRenderPassCommand {
73 GLuint frameBuffer;
74 GLbitfield clearMask;
75 unsigned int timingIndex;
76 };
77
78 struct BindGraphicsPipelineCommand {
79 GLuint program;
80 GLenum polygonMode;
81 bool cullFaceEnabled;
82 GLenum cullFace;
83 bool blendingEnabled;
84 GLenum blendingSourceFactor;
85 GLenum blendingDestinationFactor;
86 bool depthTestEnabled;
87 GLboolean depthMaskEnabled;
88 GLenum depthFunction;
89 bool depthClampEnabled;
90 bool conservativeRasterizationEnabled;
91 };
92
93 struct SetViewportCommand {
94 unsigned int origin[2];
95 unsigned int size[2];
96 };
97
98 struct SetScissorCommand {
99 unsigned int origin[2];
100 unsigned int size[2];
101 };
102
103 struct SetLineWidthCommand {
104 float width;
105 };
106
107 struct BindGeometryCommand {
108 GLuint vertexArray;
109 };
110
111 struct SetUniformIntegerCommand {
112 unsigned int location;
113 int32_t value;
114 };
115
116 struct SetUniformUnsignedIntegerCommand {
117 unsigned int location;
118 uint32_t value;
119 };
120
121 struct SetUniformFloatCommand {
122 unsigned int location;
123 float value;
124 };
125
126 struct SetUniformVector2Command {
127 unsigned int location;
128 float value[2];
129 };
130
131 struct SetUniformIVector2Command {
132 unsigned int location;
133 int32_t value[2];
134 };
135
136 struct SetUniformUVector2Command {
137 unsigned int location;
138 uint32_t value[2];
139 };
140
141 struct SetUniformVector3Command {
142 unsigned int location;
143 float value[3];
144 };
145
146 struct SetUniformIVector3Command {
147 unsigned int location;
148 int32_t value[3];
149 };
150
151 struct SetUniformUVector3Command {
152 unsigned int location;
153 uint32_t value[3];
154 };
155
156 struct SetUniformVector4Command {
157 unsigned int location;
158 float value[4];
159 };
160
161 struct SetUniformIVector4Command {
162 unsigned int location;
163 int32_t value[4];
164 };
165
166 struct SetUniformUVector4Command {
167 unsigned int location;
168 uint32_t value[4];
169 };
170
171 struct SetUniformMatrix3Command {
172 unsigned int location;
173 float value[3 * 3];
174 };
175
176 struct SetUniformMatrix4Command {
177 unsigned int location;
178 float value[4 * 4];
179 };
180
181 struct BindTextureCommand {
182 GLenum slot;
183 GLuint texture;
184 GLuint sampler;
185 };
186
187 struct BindUniformBufferCommand {
188 GLuint index;
189 GLuint uniformBuffer;
190 GLintptr offset;
191 GLsizeiptr size;
192 };
193
194 struct BindStorageBufferCommand {
195 GLuint index;
196 GLuint storageBuffer;
197 GLintptr offset;
198 GLsizeiptr size;
199 };
200
201 struct DrawCommand {
202 GLenum mode;
203 GLint first;
204 GLsizei count;
205 };
206
207 struct DrawIndexedCommand {
208 GLenum mode;
209 GLsizei count;
210 GLenum type;
211 GLvoid* indices;
212 GLint baseVertex;
213 };
214
215 struct DrawIndexedInstancedCommand {
216 GLenum mode;
217 GLsizei count;
218 GLenum type;
219 GLvoid* indices;
220 GLsizei instanceCount;
221 GLint baseVertex;
222 };
223
224 struct BlitToSwapChainCommand {
225 GLuint texture;
226 };
227
228 struct BindComputePipelineCommand {
229 GLuint program;
230 };
231
232 struct DispatchCommand {
233 GLuint groupsX;
234 GLuint groupsY;
235 GLuint groupsZ;
236 unsigned int timingIndex;
237 };
238
239 struct ClearBufferCommand {
240 GLenum target;
241 GLuint buffer;
242 GLintptr offset;
243 GLsizeiptr size;
244 };
245
246 struct Command {
247 union {
248 BeginRenderPassCommand beginRenderPassCommand;
249 BindGraphicsPipelineCommand bindGraphicsPipelineCommand;
250 SetViewportCommand setViewportCommand;
251 SetScissorCommand setScissorCommand;
252 SetLineWidthCommand setLineWidthCommand;
253 BindGeometryCommand bindGeometryCommand;
254 SetUniformIntegerCommand setUniformIntegerCommand;
255 SetUniformUnsignedIntegerCommand setUniformUnsignedIntegerCommand;
256 SetUniformFloatCommand setUniformFloatCommand;
257 SetUniformVector2Command setUniformVector2Command;
258 SetUniformIVector2Command setUniformIVector2Command;
259 SetUniformUVector2Command setUniformUVector2Command;
260 SetUniformVector3Command setUniformVector3Command;
261 SetUniformIVector3Command setUniformIVector3Command;
262 SetUniformUVector3Command setUniformUVector3Command;
263 SetUniformVector4Command setUniformVector4Command;
264 SetUniformIVector4Command setUniformIVector4Command;
265 SetUniformUVector4Command setUniformUVector4Command;
266 SetUniformMatrix3Command setUniformMatrix3Command;
267 SetUniformMatrix4Command setUniformMatrix4Command;
268 BindTextureCommand bindTextureCommand;
269 BindUniformBufferCommand bindUniformBufferCommand;
270 BindStorageBufferCommand bindStorageBufferCommand;
271 DrawCommand drawCommand;
272 DrawIndexedCommand drawIndexedCommand;
273 DrawIndexedInstancedCommand drawIndexedInstancedCommand;
274 BlitToSwapChainCommand blitToSwapChainCommand;
275 BindComputePipelineCommand bindComputePipelineCommand;
276 DispatchCommand dispatchCommand;
277 ClearBufferCommand clearBufferCommand;
278 };
279
280 enum class Type {
281 BEGIN_RENDER_PASS,
282 END_RENDER_PASS,
283 BIND_GRAPHICS_PIPELINE,
284 SET_VIEWPORT,
285 SET_SCISSOR,
286 SET_LINE_WIDTH,
287 BIND_GEOMETRY,
288 SET_UNIFORM_INTEGER,
289 SET_UNIFORM_UNSIGNED_INTEGER,
290 SET_UNIFORM_FLOAT,
291 SET_UNIFORM_VECTOR2,
292 SET_UNIFORM_IVECTOR2,
293 SET_UNIFORM_UVECTOR2,
294 SET_UNIFORM_VECTOR3,
295 SET_UNIFORM_IVECTOR3,
296 SET_UNIFORM_UVECTOR3,
297 SET_UNIFORM_VECTOR4,
298 SET_UNIFORM_IVECTOR4,
299 SET_UNIFORM_UVECTOR4,
300 SET_UNIFORM_MATRIX3,
301 SET_UNIFORM_MATRIX4,
302 BIND_TEXTURE,
303 BIND_UNIFORM_BUFFER,
304 BIND_STORAGE_BUFFER,
305 DRAW,
306 DRAW_INDEXED,
307 DRAW_INDEXED_INSTANCED,
308 BLIT_TO_SWAP_CHAIN,
309 BIND_COMPUTE_PIPELINE,
310 DISPATCH,
311 CLEAR_BUFFER,
312 MEMORY_BARRIER
313 } type;
314 };
315
316 void SetUniformInteger(unsigned int location, int32_t value);
317 void SetUniformUnsignedInteger(unsigned int location, uint32_t value);
318 void SetUniformFloat(unsigned int location, float value);
319 void SetUniformVector2(unsigned int location, const glm::vec2& value);
320 void SetUniformIVector2(unsigned int location, const glm::ivec2& value);
321 void SetUniformUVector2(unsigned int location, const glm::uvec2& value);
322 void SetUniformVector3(unsigned int location, const glm::vec3& value);
323 void SetUniformIVector3(unsigned int location, const glm::ivec3& value);
324 void SetUniformUVector3(unsigned int location, const glm::uvec3& value);
325 void SetUniformVector4(unsigned int location, const glm::vec4& value);
326 void SetUniformIVector4(unsigned int location, const glm::ivec4& value);
327 void SetUniformUVector4(unsigned int location, const glm::uvec4& value);
328 void SetUniformMatrix3(unsigned int location, const glm::mat3& value);
329 void SetUniformMatrix4(unsigned int location, const glm::mat4& value);
330
331 void AddCommand(const Command& command);
332 void SubmitCommand(const Command& command);
333
334 OpenGLRenderer& openGLRenderer;
335 OpenGLRenderPassAllocator& renderPassAllocator;
336
337 std::vector<Command> commands;
338 GLenum primitiveType;
339 GLenum indexType;
340 uint32_t indexSize;
341 uint32_t frameBufferHeight;
342 uintptr_t indexBufferOffset;
343
344 bool inRenderPass = false;
345
346 const OpenGLShaderProgram* currentShaderProgram = nullptr;
347
348 const OpenGLShaderProgram* blitShaderProgram;
349 GLuint blitSampler;
350
351 bool writesStorageBuffer = false;
352
353 std::vector<Timing> timings;
354 unsigned int timingIndex;
355};
356
357} // namespace Video
A buffer containing GPU accessible data.
Definition: Buffer.hpp:8
A buffer into which rendering commands are recorded.
Definition: CommandBuffer.hpp:24
A compute pipeline.
Definition: ComputePipeline.hpp:11
Binds together a vertex description with buffers.
Definition: GeometryBinding.hpp:8
A graphics pipeline.
Definition: GraphicsPipeline.hpp:11
OpenGL implementation of CommandBuffer.
Definition: OpenGLCommandBuffer.hpp:15
void BindGeometry(GeometryBinding *geometryBinding) final
Bind geometry to be used in upcoming draw calls.
Definition: OpenGLCommandBuffer.cpp:230
void DrawIndexedInstanced(unsigned int indexCount, unsigned int instanceCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed instanced geometry.
Definition: OpenGLCommandBuffer.cpp:403
void SetScissor(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the scissor box.
Definition: OpenGLCommandBuffer.cpp:206
void EndRenderPass() final
End render pass.
Definition: OpenGLCommandBuffer.cpp:65
void BindComputePipeline(ComputePipeline *computePipeline) final
Bind compute pipeline.
Definition: OpenGLCommandBuffer.cpp:431
void BindUniformBuffer(ShaderProgram::BindingType bindingType, Buffer *uniformBuffer) final
Bind uniform buffer for use in a shader.
Definition: OpenGLCommandBuffer.cpp:256
void ClearBuffer(Buffer *buffer) final
Clear a buffer (fill with 0).
Definition: OpenGLCommandBuffer.cpp:473
void Draw(unsigned int vertexCount, unsigned int firstVertex) final
Draw geometry.
Definition: OpenGLCommandBuffer.cpp:375
void BlitToSwapChain(Texture *texture) final
Blit a texture to the current swap chain image.
Definition: OpenGLCommandBuffer.cpp:419
void DrawIndexed(unsigned int indexCount, unsigned int firstIndex, unsigned int baseVertex) final
Draw indexed geometry.
Definition: OpenGLCommandBuffer.cpp:388
void SetViewport(const glm::uvec2 &origin, const glm::uvec2 &size) final
Set the viewport to render to.
Definition: OpenGLCommandBuffer.cpp:193
void BeginAttachmentlessRenderPass(const glm::uvec2 &size, uint32_t msaaSamples, const std::string &name) final
Begin attachmentless render pass.
Definition: OpenGLCommandBuffer.cpp:60
void Dispatch(const glm::uvec3 &numGroups, const std::string &name) final
Dispatch compute shader.
Definition: OpenGLCommandBuffer.cpp:446
OpenGLCommandBuffer(OpenGLRenderer &openGLRenderer, OpenGLRenderPassAllocator &renderPassAllocator)
Create new OpenGL command buffer.
Definition: OpenGLCommandBuffer.cpp:21
void BeginRenderPass(RenderPass *renderPass, const std::string &name) final
Begin render pass.
Definition: OpenGLCommandBuffer.cpp:28
void Submit()
Submit the commands in the command buffer to the GPU.
Definition: OpenGLCommandBuffer.cpp:490
void BindGraphicsPipeline(GraphicsPipeline *graphicsPipeline) final
Bind graphics pipeline.
Definition: OpenGLCommandBuffer.cpp:83
const std::vector< Timing > & GetTimings() const
Get the all timings in the command buffer.
Definition: OpenGLCommandBuffer.cpp:499
void PushConstants(const void *data) final
Update push constants.
Definition: OpenGLCommandBuffer.cpp:321
void SetLineWidth(float width) final
Set width of lines.
Definition: OpenGLCommandBuffer.cpp:219
~OpenGLCommandBuffer() final
Destructor.
Definition: OpenGLCommandBuffer.cpp:26
void BindStorageBuffers(std::initializer_list< Buffer * > buffers) final
Bind storage buffers.
Definition: OpenGLCommandBuffer.cpp:282
void BindMaterial(std::initializer_list< std::pair< Texture *, const Sampler * > > textures) final
Bind a material.
Definition: OpenGLCommandBuffer.cpp:302
OpenGL implementation of RenderPassAllocator.
Definition: OpenGLRenderPassAllocator.hpp:8
Low-level renderer implementing OpenGL.
Definition: OpenGLRenderer.hpp:23
A render pass.
Definition: RenderPass.hpp:11
Determines how a texture should be sampled.
Definition: Sampler.hpp:6
A shader program.
Definition: ShaderProgram.hpp:11
A texture.
Definition: Texture.hpp:8
Definition: Editor.hpp:18
Timing for a block of work.
Definition: OpenGLCommandBuffer.hpp:18
GLuint endQuery
The timestamp for ending the block of work.
Definition: OpenGLCommandBuffer.hpp:26
GLuint startQuery
The timestamp for starting the block of work.
Definition: OpenGLCommandBuffer.hpp:23
std::string name
The name of the block of work.
Definition: OpenGLCommandBuffer.hpp:20