Pioneer
VertexBuffer.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 
7 #include "graphics/Types.h"
8 #include "matrix4x4.h"
9 
25 namespace Graphics {
26 
27  // fwd declaration
28  class VertexArray;
29 
30  const Uint32 MAX_ATTRIBS = 8;
31 
33  //position, texcoord, normal etc.
35  //float3, float2 etc.
37  //byte offset of the attribute, if zero this
38  //is automatically filled for created buffers
39  uint16_t offset;
40  };
41  static_assert(sizeof(VertexAttribDesc) == 4);
42 
46 
47  //byte offset of an existing attribute
48  Uint32 GetOffset(VertexAttrib) const;
49 
50  //used internally for calculating offsets
51  static Uint32 CalculateOffset(const VertexBufferDesc &, VertexAttrib);
52  static Uint32 GetAttribSize(VertexAttribFormat);
53 
54  void CalculateOffsets();
55 
56  //semantic ATTRIB_NONE ends description (when not using all attribs)
58  Uint32 numVertices;
59  //byte size of one vertex, if zero this is
60  //automatically calculated for created buffers
61  Uint32 stride;
63  };
64 
65  class VertexBuffer : public Mappable {
66  public:
68  Mappable(desc.numVertices),
69  m_desc(desc) {}
70  virtual ~VertexBuffer();
71  const VertexBufferDesc &GetDesc() const { return m_desc; }
72 
73  template <typename T>
74  T *Map(BufferMapMode mode)
75  {
76  return reinterpret_cast<T *>(MapInternal(mode));
77  }
78 
79  //Vertex count used for rendering.
80  //By default the maximum set in description, but
81  //you may set a smaller count for partial rendering
82  bool SetVertexCount(Uint32);
83 
84  // copies the contents of the VertexArray into the buffer
85  virtual bool Populate(const VertexArray &) = 0;
86 
87  // change the buffer data without mapping
88  virtual void BufferData(const size_t, void *) = 0;
89 
90  // Bind the vertex buffer for use in rendering
91  virtual void Bind() = 0;
92 
93  // Release the vertex buffer from rendering
94  virtual void Release() = 0;
95 
96  protected:
97  virtual Uint8 *MapInternal(BufferMapMode) = 0;
99  };
100 
101  // Index buffer
102  class IndexBuffer : public Mappable {
103  public:
104  IndexBuffer(Uint32 size, BufferUsage, IndexBufferSize);
105  virtual ~IndexBuffer();
106  virtual Uint32 *Map(BufferMapMode) = 0;
107  virtual Uint16 *Map16(BufferMapMode) = 0;
108 
109  // change the buffer data without mapping
110  virtual void BufferData(const size_t, void *) = 0;
111 
112  Uint32 GetIndexCount() const { return m_indexCount; }
113  void SetIndexCount(Uint32);
114  BufferUsage GetUsage() const { return m_usage; }
116 
117  virtual void Bind() = 0;
118  virtual void Release() = 0;
119 
120  protected:
121  Uint32 m_indexCount;
124  };
125 
126  // Instance buffer
127  class InstanceBuffer : public Mappable {
128  public:
129  InstanceBuffer(Uint32 size, BufferUsage);
130  virtual ~InstanceBuffer();
131  virtual matrix4x4f *Map(BufferMapMode) = 0;
132 
133  Uint32 GetInstanceCount() const { return m_instanceCount; }
134  void SetInstanceCount(const Uint32);
135  BufferUsage GetUsage() const { return m_usage; }
136 
137  virtual void Bind() = 0;
138  virtual void Release() = 0;
139 
140  protected:
143  };
144 
145  /*
146  * Wraps a vertex buffer and optional index buffer into a single mesh.
147  *
148  * This class maps to OpenGL's vertex array objects, and is used to
149  * coalesce primitive data for drawing commands in one place.
150  *
151  * It is the calling code's responsibility to ensure that once a draw has
152  * been issued using a MeshObject, the MeshObject stays alive until the
153  * command list is executed or reset (e.g. SwapBuffers()). Failure to
154  * observe this requirement will result in undefined behavior.
155  */
156  class MeshObject : public RefCounted {
157  public:
158  virtual ~MeshObject() {}
159 
160  virtual void Bind() = 0;
161  virtual void Release() = 0;
162 
163  virtual VertexBuffer *GetVertexBuffer() const = 0;
164  virtual IndexBuffer *GetIndexBuffer() const = 0;
165  };
166 
167 } // namespace Graphics
Definition: VertexBuffer.h:102
virtual void Bind()=0
BufferUsage m_usage
Definition: VertexBuffer.h:123
virtual ~IndexBuffer()
Definition: VertexBuffer.cpp:144
Uint32 m_indexCount
Definition: VertexBuffer.h:121
IndexBufferSize GetElementSize() const
Definition: VertexBuffer.h:115
virtual Uint32 * Map(BufferMapMode)=0
virtual void BufferData(const size_t, void *)=0
void SetIndexCount(Uint32)
Definition: VertexBuffer.cpp:148
IndexBuffer(Uint32 size, BufferUsage, IndexBufferSize)
Definition: VertexBuffer.cpp:136
Uint32 GetIndexCount() const
Definition: VertexBuffer.h:112
virtual void Release()=0
virtual Uint16 * Map16(BufferMapMode)=0
IndexBufferSize m_elemSize
Definition: VertexBuffer.h:122
BufferUsage GetUsage() const
Definition: VertexBuffer.h:114
Definition: VertexBuffer.h:127
virtual void Release()=0
Uint32 GetInstanceCount() const
Definition: VertexBuffer.h:133
BufferUsage m_usage
Definition: VertexBuffer.h:142
void SetInstanceCount(const Uint32)
Definition: VertexBuffer.cpp:165
virtual ~InstanceBuffer()
Definition: VertexBuffer.cpp:161
virtual matrix4x4f * Map(BufferMapMode)=0
InstanceBuffer(Uint32 size, BufferUsage)
Definition: VertexBuffer.cpp:155
virtual void Bind()=0
Uint32 m_instanceCount
Definition: VertexBuffer.h:141
BufferUsage GetUsage() const
Definition: VertexBuffer.h:135
Definition: BufferCommon.h:16
Definition: VertexBuffer.h:156
virtual void Bind()=0
virtual void Release()=0
virtual IndexBuffer * GetIndexBuffer() const =0
virtual ~MeshObject()
Definition: VertexBuffer.h:158
virtual VertexBuffer * GetVertexBuffer() const =0
Definition: VertexArray.h:19
Definition: VertexBuffer.h:65
bool SetVertexCount(Uint32)
Definition: VertexBuffer.cpp:126
virtual void Release()=0
const VertexBufferDesc & GetDesc() const
Definition: VertexBuffer.h:71
T * Map(BufferMapMode mode)
Definition: VertexBuffer.h:74
virtual ~VertexBuffer()
Definition: VertexBuffer.cpp:122
virtual Uint8 * MapInternal(BufferMapMode)=0
virtual void Bind()=0
virtual void BufferData(const size_t, void *)=0
VertexBufferDesc m_desc
Definition: VertexBuffer.h:98
VertexBuffer(const VertexBufferDesc &desc)
Definition: VertexBuffer.h:67
virtual bool Populate(const VertexArray &)=0
Definition: RefCounted.h:11
Definition: Background.h:14
VertexAttrib
Definition: Types.h:12
IndexBufferSize
Definition: Types.h:76
BufferUsage
Definition: Types.h:65
BufferMapMode
Definition: Types.h:70
const Uint32 MAX_ATTRIBS
Definition: VertexBuffer.h:30
VertexAttribFormat
Definition: Types.h:47
Definition: Types.h:27
Definition: VertexBuffer.h:32
VertexAttrib semantic
Definition: VertexBuffer.h:34
VertexAttribFormat format
Definition: VertexBuffer.h:36
uint16_t offset
Definition: VertexBuffer.h:39
Definition: VertexBuffer.h:43
VertexBufferDesc()
Definition: VertexBuffer.cpp:28
static VertexBufferDesc FromAttribSet(AttributeSet set)
Definition: VertexBuffer.cpp:44
VertexAttribDesc attrib[MAX_ATTRIBS]
Definition: VertexBuffer.h:57
BufferUsage usage
Definition: VertexBuffer.h:62
void CalculateOffsets()
Definition: VertexBuffer.cpp:105
static Uint32 GetAttribSize(VertexAttribFormat)
Definition: VertexBuffer.cpp:12
Uint32 stride
Definition: VertexBuffer.h:61
Uint32 GetOffset(VertexAttrib) const
Definition: VertexBuffer.cpp:79
Uint32 numVertices
Definition: VertexBuffer.h:58
static Uint32 CalculateOffset(const VertexBufferDesc &, VertexAttrib)
Definition: VertexBuffer.cpp:91