Pioneer
CommandBufferGL.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 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 
14 namespace Graphics {
15 
16  class Material;
17  class MeshObject;
18  class VertexArray;
19 
20  class RendererOGL;
21 
22  namespace OGL {
23 
24  class InstanceBuffer;
25  class Material;
26  class MeshObject;
27  class Program;
28  class Shader;
29  class TextureGL;
30  class RenderTarget;
31  class IndexBuffer;
32  class UniformBuffer;
33  class VertexBuffer;
34 
35  class CommandList {
36  public:
37  struct DrawCmd {
39  InstanceBuffer *inst = nullptr;
40  const Shader *shader = nullptr;
41  Program *program = nullptr;
42  size_t renderStateHash = 0;
43  char *drawData;
44  };
45 
46  struct DynamicDrawCmd {
49  const Shader *shader = nullptr;
50  Program *program = nullptr;
51  size_t renderStateHash = 0;
52  char *drawData;
53  };
54 
55  struct RenderPassCmd {
60  bool setScissor;
62  bool clearDepth;
64  };
65 
66  // development asserts to ensure sizes are kept reasonable.
67  // if you need to go beyond these sizes, add a new command instead.
68  static_assert(sizeof(DrawCmd) <= 64);
69  static_assert(sizeof(DynamicDrawCmd) <= 64);
70  static_assert(sizeof(RenderPassCmd) <= 64);
71 
74 
75  void AddRenderPassCmd(RenderTarget *renderTarget, ViewportExtents extents);
76  void AddScissorCmd(ViewportExtents extents);
77  void AddClearCmd(bool clearColors, bool clearDepth, Color color);
78 
79  protected:
80  using Cmd = std::variant<DrawCmd, DynamicDrawCmd, RenderPassCmd>;
81  const std::vector<Cmd> &GetDrawCmds() const { return m_drawCmds; }
82 
83  bool IsEmpty() const { return m_drawCmds.empty(); }
84  void Reset();
85 
86  private:
87  friend class Graphics::RendererOGL;
89  m_renderer(r)
90  {
91  m_drawCmds.reserve(32);
92  }
93 
94  // Allocate space for all shader data that needs to be cached forward
95  char *AllocDrawData(const Shader *shader);
96  // Create and cache all material data needed for later execution of a draw command
97  char *SetupMaterialData(OGL::Material *mat);
98 
99  // These functions are called before and after a command is executed
100  void ApplyDrawData(const Shader *shader, Program *program, char *drawData) const;
101 
102  void ExecuteDrawCmd(const DrawCmd &);
103  void ExecuteDynamicDrawCmd(const DynamicDrawCmd &);
104  void ExecuteRenderPassCmd(const RenderPassCmd &);
105 
106  static BufferBinding<UniformBuffer> *getBufferBindings(const Shader *shader, char *data);
107  static TextureGL **getTextureBindings(const Shader *shader, char *data);
108 
109  // 16k-sized buckets; we're not likely to have 100s of command lists
110  // (and if we do it's still a drop in the bucket)
111  static constexpr size_t BUCKET_SIZE = 1UL << 14;
112  struct DataBucket {
113  std::unique_ptr<char[]> data;
114  size_t used = 0;
115  size_t capacity = BUCKET_SIZE;
116 
117  char *alloc(size_t size)
118  {
119  char *ret = nullptr;
120  if (capacity - used >= size) {
121  ret = data.get() + used;
122  used += size;
123  }
124  return ret;
125  }
126  };
127 
128  Graphics::RendererOGL *m_renderer;
129  std::vector<Cmd> m_drawCmds;
130  std::vector<DataBucket> m_dataBuckets;
131  bool m_executing = false;
132  };
133 
134  }; // namespace OGL
135 }; // namespace Graphics
Definition: VertexBuffer.h:127
Definition: Material.h:148
Definition: VertexBuffer.h:156
Definition: CommandBufferGL.h:35
bool IsEmpty() const
Definition: CommandBufferGL.h:83
const std::vector< Cmd > & GetDrawCmds() const
Definition: CommandBufferGL.h:81
void AddScissorCmd(ViewportExtents extents)
Definition: CommandBufferGL.cpp:83
void AddDynamicDrawCmd(BufferBinding< Graphics::VertexBuffer > vtx, BufferBinding< Graphics::IndexBuffer > idx, Graphics::Material *mat)
Definition: CommandBufferGL.cpp:35
void AddClearCmd(bool clearColors, bool clearDepth, Color color)
Definition: CommandBufferGL.cpp:104
std::variant< DrawCmd, DynamicDrawCmd, RenderPassCmd > Cmd
Definition: CommandBufferGL.h:80
void AddDrawCmd(Graphics::MeshObject *mesh, Graphics::Material *mat, Graphics::InstanceBuffer *inst=nullptr)
Definition: CommandBufferGL.cpp:18
void Reset()
Definition: CommandBufferGL.cpp:128
void AddRenderPassCmd(RenderTarget *renderTarget, ViewportExtents extents)
Definition: CommandBufferGL.cpp:59
Definition: VertexBufferGL.h:60
Definition: VertexBufferGL.h:81
Definition: MaterialGL.h:35
Definition: VertexBufferGL.h:103
Definition: Program.h:25
Definition: RenderTargetGL.h:21
Definition: Shader.h:67
Definition: TextureGL.h:12
Definition: UniformBuffer.h:16
Definition: VertexBufferGL.h:17
Definition: RendererGL.h:37
Definition: Background.h:14
Definition: Color.h:66
Definition: BufferCommon.h:63
Definition: CommandBufferGL.h:37
char * drawData
Definition: CommandBufferGL.h:43
MeshObject * mesh
Definition: CommandBufferGL.h:38
const Shader * shader
Definition: CommandBufferGL.h:40
Program * program
Definition: CommandBufferGL.h:41
size_t renderStateHash
Definition: CommandBufferGL.h:42
InstanceBuffer * inst
Definition: CommandBufferGL.h:39
Definition: CommandBufferGL.h:46
Program * program
Definition: CommandBufferGL.h:50
BufferBinding< VertexBuffer > vtxBind
Definition: CommandBufferGL.h:47
size_t renderStateHash
Definition: CommandBufferGL.h:51
char * drawData
Definition: CommandBufferGL.h:52
BufferBinding< IndexBuffer > idxBind
Definition: CommandBufferGL.h:48
const Shader * shader
Definition: CommandBufferGL.h:49
Definition: CommandBufferGL.h:55
RenderTarget * renderTarget
Definition: CommandBufferGL.h:56
ViewportExtents extents
Definition: CommandBufferGL.h:57
bool clearColors
Definition: CommandBufferGL.h:61
ViewportExtents scissor
Definition: CommandBufferGL.h:58
bool setRenderTarget
Definition: CommandBufferGL.h:59
Color clearColor
Definition: CommandBufferGL.h:63
bool clearDepth
Definition: CommandBufferGL.h:62
bool setScissor
Definition: CommandBufferGL.h:60
Definition: Graphics.h:59