NAP
vertexattribute.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <utility/dllexport.h>
8 #include <nap/resource.h>
9 #include <glm/glm.hpp>
10 #include <nap/numeric.h>
11 
12 #include "vulkan/vulkan_core.h"
13 
14 namespace nap
15 {
21  class NAPAPI BaseVertexAttribute : public Resource
22  {
23  RTTI_ENABLE(Resource)
24  public:
29  BaseVertexAttribute(const nap::rtti::TypeInfo& elementType);
30 
34  virtual void* getRawData() = 0;
35 
39  virtual VkFormat getFormat() const = 0;
40 
44  virtual int getCount() const = 0;
45 
49  virtual size_t getCapacity() const = 0;
50 
54  virtual void reserve(size_t numElements) = 0;
55 
59  const nap::rtti::TypeInfo& getElementType() const { return mElementType; }
60 
61  std::string mAttributeID;
63  };
64 
65 
67 
74  template<typename ELEMENTTYPE>
76  {
77  RTTI_ENABLE(BaseVertexAttribute)
78  public:
79 
80  VertexAttribute() : BaseVertexAttribute(RTTI_OF(ELEMENTTYPE)) { }
81 
86  virtual void reserve(size_t numElements) override { mData.reserve(numElements); }
87 
91  virtual size_t getCapacity() const override { return mData.capacity(); }
92 
97  void resize(size_t numElements) { mData.resize(numElements); }
98 
102  void clear() { mData.clear(); }
103 
107  const std::vector<ELEMENTTYPE>& getData() const { return mData; }
108 
112  std::vector<ELEMENTTYPE>& getData() { return mData; }
113 
118  void addData(const ELEMENTTYPE& element) { mData.emplace_back(element); }
119 
124  void addData(const ELEMENTTYPE&& element) noexcept { mData.emplace_back(std::move(element)); }
125 
131  void addData(const ELEMENTTYPE* elements, int numElements);
132 
137  void setData(const std::vector<ELEMENTTYPE>& values) { setData(&(values.front()), values.size()); }
138 
144  void setData(const ELEMENTTYPE* elements, int numElements);
145 
149  virtual VkFormat getFormat() const override;
150 
154  virtual int getCount() const override { return static_cast<int>(mData.size()); }
155 
161  ELEMENTTYPE& operator[](std::size_t index) { return mData[index]; }
162 
168  const ELEMENTTYPE& operator[](std::size_t index) const { return mData[index]; }
169 
170  std::vector<ELEMENTTYPE> mData;
171 
172  protected:
176  virtual void* getRawData() override;
177  };
178 
179 
181  // Type definitions for all supported vertex attribute types
183 
191 
192 
194  // Template Definitions
196 
197  template<typename ELEMENTTYPE>
198  void nap::VertexAttribute<ELEMENTTYPE>::setData(const ELEMENTTYPE* elements, int numElements)
199  {
200  mData.resize(numElements);
201  memcpy(mData.data(), elements, numElements * sizeof(ELEMENTTYPE));
202  }
203 
204  template<typename ELEMENTTYPE>
205  void nap::VertexAttribute<ELEMENTTYPE>::addData(const ELEMENTTYPE* elements, int numElements)
206  {
207  int cur_num_elements = mData.size();
208  mData.resize(cur_num_elements + numElements);
209  memcpy((void*)&mData[cur_num_elements], elements, numElements * sizeof(ELEMENTTYPE));
210  }
211 
212  template<typename ELEMENTTYPE>
214  {
215  return static_cast<void*>(mData.data());
216  }
217 
218 
220  // Forward declarations of templated functions
222 
223  template<>
224  NAPAPI VkFormat UIntVertexAttribute::getFormat() const;
225 
226  template<>
227  NAPAPI VkFormat IntVertexAttribute::getFormat() const;
228 
229  template<>
230  NAPAPI VkFormat FloatVertexAttribute::getFormat() const;
231 
232  template<>
233  NAPAPI VkFormat Vec2VertexAttribute::getFormat() const;
234 
235  template<>
236  NAPAPI VkFormat Vec3VertexAttribute::getFormat() const;
237 
238  template<>
239  NAPAPI VkFormat Vec4VertexAttribute::getFormat() const;
240 
241  template<>
242  NAPAPI VkFormat IVec4VertexAttribute::getFormat() const;
243 } // nap
244 
nap::VertexAttribute::getData
std::vector< ELEMENTTYPE > & getData()
Definition: vertexattribute.h:112
nap::VertexAttribute::setData
void setData(const std::vector< ELEMENTTYPE > &values)
Definition: vertexattribute.h:137
nap::VertexAttribute::addData
void addData(const ELEMENTTYPE &element)
Definition: vertexattribute.h:118
nap::BaseVertexAttribute::mElementType
rtti::TypeInfo mElementType
Element type.
Definition: vertexattribute.h:62
nap::VertexAttribute::clear
void clear()
Definition: vertexattribute.h:102
nap::VertexAttribute
Definition: vertexattribute.h:75
nap::VertexAttribute::getRawData
virtual void * getRawData() override
Definition: vertexattribute.h:213
nap::BaseVertexAttribute
Definition: vertexattribute.h:21
nap::VertexAttribute::VertexAttribute
VertexAttribute()
Definition: vertexattribute.h:80
nap::BaseVertexAttribute::mAttributeID
std::string mAttributeID
Name/ID of the attribute.
Definition: vertexattribute.h:61
nap::VertexAttribute::resize
void resize(size_t numElements)
Definition: vertexattribute.h:97
nap::BaseVertexAttribute::getElementType
const nap::rtti::TypeInfo & getElementType() const
Definition: vertexattribute.h:59
nap::VertexAttribute::getCapacity
virtual size_t getCapacity() const override
Definition: vertexattribute.h:91
nap::VertexAttribute::addData
void addData(const ELEMENTTYPE &&element) noexcept
Definition: vertexattribute.h:124
nap::VertexAttribute::getData
const std::vector< ELEMENTTYPE > & getData() const
Definition: vertexattribute.h:107
nap::VertexAttribute::getCount
virtual int getCount() const override
Definition: vertexattribute.h:154
nap::VertexAttribute::operator[]
ELEMENTTYPE & operator[](std::size_t index)
Definition: vertexattribute.h:161
nap
Definition: templateapp.h:17
nap::VertexAttribute::reserve
virtual void reserve(size_t numElements) override
Definition: vertexattribute.h:86
nap::Resource
Definition: resource.h:19
nap::VertexAttribute::operator[]
const ELEMENTTYPE & operator[](std::size_t index) const
Definition: vertexattribute.h:168
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:141
nap::VertexAttribute::getFormat
virtual VkFormat getFormat() const override
nap::VertexAttribute::mData
std::vector< ELEMENTTYPE > mData
Actual typed data of the attribute.
Definition: vertexattribute.h:170