Pioneer
Loading...
Searching...
No Matches
CommandBufferGL.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#include "Color.h"
7#include "graphics/Graphics.h"
8#include "graphics/Types.h"
10
11#include "OpenGLLibs.h"
12#include <variant>
13
14namespace Graphics {
15
16 class Material;
17 class MeshObject;
18 class VertexArray;
19 class RenderTarget;
20
21 class RendererOGL;
22
23 namespace OGL {
24
25 class InstanceBuffer;
26 class Material;
27 class MeshObject;
28 class Program;
29 class Shader;
30 class TextureGL;
31 class RenderTarget;
32 class IndexBuffer;
33 class UniformBuffer;
34 class VertexBuffer;
35
37 public:
38 struct DrawCmd {
40 InstanceBuffer *inst = nullptr;
41 const Shader *shader = nullptr;
42 Program *program = nullptr;
43 size_t renderStateHash = 0;
44 char *drawData;
45 };
46
50 const Shader *shader = nullptr;
51 Program *program = nullptr;
52 size_t renderStateHash = 0;
53 char *drawData;
54 };
55
65 };
66
75 };
76
77 // development asserts to ensure sizes are kept reasonable.
78 // if you need to go beyond these sizes, add a new command instead.
79 static_assert(sizeof(DrawCmd) <= 64);
80 static_assert(sizeof(DynamicDrawCmd) <= 64);
81 static_assert(sizeof(RenderPassCmd) <= 64);
82
85
86 void AddRenderPassCmd(RenderTarget *renderTarget, ViewportExtents extents);
87 void AddScissorCmd(ViewportExtents extents);
88 void AddClearCmd(bool clearColors, bool clearDepth, Color color);
89
90 // NOTE: bound render target state will be invalidated.
91 // BlitRenderTargetCmd should be followed by a RenderPassCmd
94 const ViewportExtents &srcExtents,
95 const ViewportExtents &dstExtents,
96 bool resolveMSAA = false, bool blitDepthBuffer = false, bool linearFilter = true);
97
98 protected:
99 using Cmd = std::variant<DrawCmd, DynamicDrawCmd, RenderPassCmd, BlitRenderTargetCmd>;
100 const std::vector<Cmd> &GetDrawCmds() const { return m_drawCmds; }
101
102 bool IsEmpty() const { return m_drawCmds.empty(); }
103 void Reset();
104
105 private:
108 m_renderer(r)
109 {
110 m_drawCmds.reserve(32);
111 }
112
113 // Allocate space for all shader data that needs to be cached forward
114 char *AllocDrawData(const Shader *shader);
115 // Create and cache all material data needed for later execution of a draw command
116 char *SetupMaterialData(OGL::Material *mat);
117
118 // These functions are called before and after a command is executed
119 void ApplyDrawData(const Shader *shader, Program *program, char *drawData) const;
120
121 void ExecuteDrawCmd(const DrawCmd &);
122 void ExecuteDynamicDrawCmd(const DynamicDrawCmd &);
123 void ExecuteRenderPassCmd(const RenderPassCmd &);
124 void ExecuteBlitRenderTargetCmd(const BlitRenderTargetCmd &);
125
126 static BufferBinding<UniformBuffer> *getBufferBindings(const Shader *shader, char *data);
127 static TextureGL **getTextureBindings(const Shader *shader, char *data);
128
129 // 16k-sized buckets; we're not likely to have 100s of command lists
130 // (and if we do it's still a drop in the bucket)
131 static constexpr size_t BUCKET_SIZE = 1UL << 14;
132 struct DataBucket {
133 std::unique_ptr<char[]> data;
134 size_t used = 0;
135 size_t capacity = BUCKET_SIZE;
136
137 char *alloc(size_t size)
138 {
139 char *ret = nullptr;
140 if (capacity - used >= size) {
141 ret = data.get() + used;
142 used += size;
143 }
144 return ret;
145 }
146 };
147
148 Graphics::RendererOGL *m_renderer;
149 std::vector<Cmd> m_drawCmds;
150 std::vector<DataBucket> m_dataBuckets;
151 bool m_executing = false;
152 };
153
154 }; // namespace OGL
155}; // namespace Graphics
Definition: VertexBuffer.h:127
Definition: Material.h:148
Definition: VertexBuffer.h:156
Definition: CommandBufferGL.h:36
bool IsEmpty() const
Definition: CommandBufferGL.h:102
void AddScissorCmd(ViewportExtents extents)
Definition: CommandBufferGL.cpp:85
void AddDynamicDrawCmd(BufferBinding< Graphics::VertexBuffer > vtx, BufferBinding< Graphics::IndexBuffer > idx, Graphics::Material *mat)
Definition: CommandBufferGL.cpp:37
void AddClearCmd(bool clearColors, bool clearDepth, Color color)
Definition: CommandBufferGL.cpp:106
const std::vector< Cmd > & GetDrawCmds() const
Definition: CommandBufferGL.h:100
void AddDrawCmd(Graphics::MeshObject *mesh, Graphics::Material *mat, Graphics::InstanceBuffer *inst=nullptr)
Definition: CommandBufferGL.cpp:20
std::variant< DrawCmd, DynamicDrawCmd, RenderPassCmd, BlitRenderTargetCmd > Cmd
Definition: CommandBufferGL.h:99
void AddBlitRenderTargetCmd(Graphics::RenderTarget *src, Graphics::RenderTarget *dst, const ViewportExtents &srcExtents, const ViewportExtents &dstExtents, bool resolveMSAA=false, bool blitDepthBuffer=false, bool linearFilter=true)
Definition: CommandBufferGL.cpp:130
void Reset()
Definition: CommandBufferGL.cpp:156
void AddRenderPassCmd(RenderTarget *renderTarget, ViewportExtents extents)
Definition: CommandBufferGL.cpp:61
Definition: VertexBufferGL.h:60
Definition: VertexBufferGL.h:81
Definition: MaterialGL.h:35
Definition: VertexBufferGL.h:103
Definition: Program.h:25
Definition: RenderTargetGL.h:22
Definition: Shader.h:67
Definition: TextureGL.h:12
Definition: UniformBuffer.h:16
Definition: VertexBufferGL.h:17
Definition: RenderTarget.h:38
Definition: RendererGL.h:39
Definition: Background.h:14
Definition: Color.h:66
Definition: BufferCommon.h:63
Definition: CommandBufferGL.h:67
bool linearFilter
Definition: CommandBufferGL.h:74
bool blitDepthBuffer
Definition: CommandBufferGL.h:73
RenderTarget * dstTarget
Definition: CommandBufferGL.h:69
ViewportExtents dstExtents
Definition: CommandBufferGL.h:71
RenderTarget * srcTarget
Definition: CommandBufferGL.h:68
ViewportExtents srcExtents
Definition: CommandBufferGL.h:70
bool resolveMSAA
Definition: CommandBufferGL.h:72
Definition: CommandBufferGL.h:38
char * drawData
Definition: CommandBufferGL.h:44
MeshObject * mesh
Definition: CommandBufferGL.h:39
const Shader * shader
Definition: CommandBufferGL.h:41
Program * program
Definition: CommandBufferGL.h:42
size_t renderStateHash
Definition: CommandBufferGL.h:43
InstanceBuffer * inst
Definition: CommandBufferGL.h:40
Definition: CommandBufferGL.h:47
Program * program
Definition: CommandBufferGL.h:51
BufferBinding< VertexBuffer > vtxBind
Definition: CommandBufferGL.h:48
size_t renderStateHash
Definition: CommandBufferGL.h:52
char * drawData
Definition: CommandBufferGL.h:53
BufferBinding< IndexBuffer > idxBind
Definition: CommandBufferGL.h:49
const Shader * shader
Definition: CommandBufferGL.h:50
Definition: CommandBufferGL.h:56
RenderTarget * renderTarget
Definition: CommandBufferGL.h:57
ViewportExtents extents
Definition: CommandBufferGL.h:58
bool clearColors
Definition: CommandBufferGL.h:62
ViewportExtents scissor
Definition: CommandBufferGL.h:59
bool setRenderTarget
Definition: CommandBufferGL.h:60
Color clearColor
Definition: CommandBufferGL.h:64
bool clearDepth
Definition: CommandBufferGL.h:63
bool setScissor
Definition: CommandBufferGL.h:61
Definition: Graphics.h:59