Pioneer
VertexBuffer.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef GRAPHICS_VERTEXBUFFER_H
5 #define GRAPHICS_VERTEXBUFFER_H
21 #include "Types.h"
22 #include "libs.h"
23 
24 namespace Graphics {
25 
26  // fwd declaration
27  class VertexArray;
28 
29  const Uint32 MAX_ATTRIBS = 8;
30 
32  //position, texcoord, normal etc.
34  //float3, float2 etc.
36  //byte offset of the attribute, if zero this
37  //is automatically filled for created buffers
38  Uint32 offset;
39  };
40 
43  //byte offset of an existing attribute
44  Uint32 GetOffset(VertexAttrib) const;
45 
46  //used internally for calculating offsets
47  static Uint32 CalculateOffset(const VertexBufferDesc &, VertexAttrib);
48  static Uint32 GetAttribSize(VertexAttribFormat);
49 
50  //semantic ATTRIB_NONE ends description (when not using all attribs)
52  Uint32 numVertices;
53  //byte size of one vertex, if zero this is
54  //automatically calculated for created buffers
55  Uint32 stride;
57  };
58 
59  class Mappable : public RefCounted {
60  public:
61  virtual ~Mappable() {}
62  virtual void Unmap() = 0;
63 
64  inline Uint32 GetSize() const { return m_size; }
65  inline Uint32 GetCapacity() const { return m_capacity; }
66 
67  protected:
68  explicit Mappable(const Uint32 size) :
70  m_size(size),
71  m_capacity(size) {}
72  BufferMapMode m_mapMode; //tracking map state
73 
74  // size is the current number of elements in the buffer
75  Uint32 m_size;
76  // capacity is the maximum number of elements that can be put in the buffer
77  Uint32 m_capacity;
78  };
79 
80  class VertexBuffer : public Mappable {
81  public:
83  Mappable(desc.numVertices),
84  m_desc(desc) {}
85  virtual ~VertexBuffer();
86  const VertexBufferDesc &GetDesc() const { return m_desc; }
87 
88  template <typename T>
89  T *Map(BufferMapMode mode)
90  {
91  return reinterpret_cast<T *>(MapInternal(mode));
92  }
93 
94  //Vertex count used for rendering.
95  //By default the maximum set in description, but
96  //you may set a smaller count for partial rendering
97  bool SetVertexCount(Uint32);
98 
99  // copies the contents of the VertexArray into the buffer
100  virtual bool Populate(const VertexArray &) = 0;
101 
102  // change the buffer data without mapping
103  virtual void BufferData(const size_t, void *) = 0;
104 
105  virtual void Bind() = 0;
106  virtual void Release() = 0;
107 
108  protected:
109  virtual Uint8 *MapInternal(BufferMapMode) = 0;
111  };
112 
113  // Index buffer
114  class IndexBuffer : public Mappable {
115  public:
116  IndexBuffer(Uint32 size, BufferUsage);
117  virtual ~IndexBuffer();
118  virtual Uint32 *Map(BufferMapMode) = 0;
119 
120  // change the buffer data without mapping
121  virtual void BufferData(const size_t, void *) = 0;
122 
123  Uint32 GetIndexCount() const { return m_indexCount; }
124  void SetIndexCount(Uint32);
125  BufferUsage GetUsage() const { return m_usage; }
126 
127  virtual void Bind() = 0;
128  virtual void Release() = 0;
129 
130  protected:
131  Uint32 m_indexCount;
133  };
134 
135  // Instance buffer
136  class InstanceBuffer : public Mappable {
137  public:
138  InstanceBuffer(Uint32 size, BufferUsage);
139  virtual ~InstanceBuffer();
140  virtual matrix4x4f *Map(BufferMapMode) = 0;
141 
142  Uint32 GetInstanceCount() const { return m_instanceCount; }
143  void SetInstanceCount(const Uint32);
144  BufferUsage GetUsage() const { return m_usage; }
145 
146  virtual void Bind() = 0;
147  virtual void Release() = 0;
148 
149  protected:
152  };
153 
154 } // namespace Graphics
155 #endif // GRAPHICS_VERTEXBUFFER_H
Definition: VertexBuffer.h:114
virtual void Bind()=0
BufferUsage m_usage
Definition: VertexBuffer.h:132
virtual ~IndexBuffer()
Definition: VertexBuffer.cpp:87
Uint32 m_indexCount
Definition: VertexBuffer.h:131
virtual Uint32 * Map(BufferMapMode)=0
virtual void BufferData(const size_t, void *)=0
void SetIndexCount(Uint32)
Definition: VertexBuffer.cpp:91
IndexBuffer(Uint32 size, BufferUsage)
Definition: VertexBuffer.cpp:80
Uint32 GetIndexCount() const
Definition: VertexBuffer.h:123
virtual void Release()=0
BufferUsage GetUsage() const
Definition: VertexBuffer.h:125
Definition: VertexBuffer.h:136
virtual void Release()=0
Uint32 GetInstanceCount() const
Definition: VertexBuffer.h:142
BufferUsage m_usage
Definition: VertexBuffer.h:151
void SetInstanceCount(const Uint32)
Definition: VertexBuffer.cpp:108
virtual ~InstanceBuffer()
Definition: VertexBuffer.cpp:104
virtual matrix4x4f * Map(BufferMapMode)=0
InstanceBuffer(Uint32 size, BufferUsage)
Definition: VertexBuffer.cpp:98
virtual void Bind()=0
Uint32 m_instanceCount
Definition: VertexBuffer.h:150
BufferUsage GetUsage() const
Definition: VertexBuffer.h:144
Definition: VertexBuffer.h:59
Uint32 m_capacity
Definition: VertexBuffer.h:77
Uint32 m_size
Definition: VertexBuffer.h:75
virtual void Unmap()=0
Uint32 GetSize() const
Definition: VertexBuffer.h:64
Uint32 GetCapacity() const
Definition: VertexBuffer.h:65
BufferMapMode m_mapMode
Definition: VertexBuffer.h:72
virtual ~Mappable()
Definition: VertexBuffer.h:61
Mappable(const Uint32 size)
Definition: VertexBuffer.h:68
Definition: VertexArray.h:19
Definition: VertexBuffer.h:80
bool SetVertexCount(Uint32)
Definition: VertexBuffer.cpp:70
virtual void Release()=0
const VertexBufferDesc & GetDesc() const
Definition: VertexBuffer.h:86
T * Map(BufferMapMode mode)
Definition: VertexBuffer.h:89
virtual ~VertexBuffer()
Definition: VertexBuffer.cpp:66
virtual Uint8 * MapInternal(BufferMapMode)=0
virtual void Bind()=0
virtual void BufferData(const size_t, void *)=0
VertexBufferDesc m_desc
Definition: VertexBuffer.h:110
VertexBuffer(const VertexBufferDesc &desc)
Definition: VertexBuffer.h:82
virtual bool Populate(const VertexArray &)=0
Definition: RefCounted.h:11
Definition: Background.h:13
VertexAttrib
Definition: Types.h:14
VertexAttribFormat
Definition: Types.h:26
BufferUsage
Definition: Types.h:34
BufferMapMode
Definition: Types.h:39
@ BUFFER_MAP_NONE
Definition: Types.h:40
const Uint32 MAX_ATTRIBS
Definition: VertexBuffer.h:29
Definition: VertexBuffer.h:31
VertexAttrib semantic
Definition: VertexBuffer.h:33
VertexAttribFormat format
Definition: VertexBuffer.h:35
Uint32 offset
Definition: VertexBuffer.h:38
Definition: VertexBuffer.h:41
VertexBufferDesc()
Definition: VertexBuffer.cpp:24
VertexAttribDesc attrib[MAX_ATTRIBS]
Definition: VertexBuffer.h:51
BufferUsage usage
Definition: VertexBuffer.h:56
static Uint32 GetAttribSize(VertexAttribFormat)
Definition: VertexBuffer.cpp:8
Uint32 stride
Definition: VertexBuffer.h:55
Uint32 GetOffset(VertexAttrib) const
Definition: VertexBuffer.cpp:40
Uint32 numVertices
Definition: VertexBuffer.h:52
static Uint32 CalculateOffset(const VertexBufferDesc &, VertexAttrib)
Definition: VertexBuffer.cpp:52