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 {
20  class NAPAPI BaseVertexAttribute : public Resource
21  {
22  RTTI_ENABLE(Resource)
23  public:
25 
30  virtual void* getRawData() = 0;
31 
35  virtual VkFormat getFormat() const = 0;
36 
40  virtual int getCount() const = 0;
41 
45  virtual size_t getCapacity() const = 0;
46 
50  virtual void reserve(size_t numElements) = 0;
51 
52  std::string mAttributeID;
53  };
54 
55 
57 
64  template<typename ELEMENTTYPE>
66  {
67  RTTI_ENABLE(BaseVertexAttribute)
68  public:
69 
74  virtual void reserve(size_t numElements) override { mData.reserve(numElements); }
75 
79  virtual size_t getCapacity() const override { return mData.capacity(); }
80 
85  void resize(size_t numElements) { mData.resize(numElements); }
86 
90  void clear() { mData.clear(); }
91 
95  const std::vector<ELEMENTTYPE>& getData() const { return mData; }
96 
100  std::vector<ELEMENTTYPE>& getData() { return mData; }
101 
106  void addData(const ELEMENTTYPE& element) { mData.emplace_back(element); }
107 
113  void addData(const ELEMENTTYPE* elements, int numElements);
114 
119  void setData(const std::vector<ELEMENTTYPE>& values) { setData(&(values.front()), values.size()); }
120 
126  void setData(const ELEMENTTYPE* elements, int numElements);
127 
131  virtual VkFormat getFormat() const override;
132 
136  virtual int getCount() const override { return static_cast<int>(mData.size()); }
137 
143  ELEMENTTYPE& operator[](std::size_t index) { return mData[index]; }
144 
150  const ELEMENTTYPE& operator[](std::size_t index) const { return mData[index]; }
151 
152 
153  std::vector<ELEMENTTYPE> mData;
154 
155  protected:
159  virtual void* getRawData() override;
160  };
161 
162 
164  // Type definitions for all supported vertex attribute types
166 
174 
175 
177  // Template Definitions
179 
180  template<typename ELEMENTTYPE>
181  void nap::VertexAttribute<ELEMENTTYPE>::setData(const ELEMENTTYPE* elements, int numElements)
182  {
183  mData.resize(numElements);
184  memcpy(mData.data(), elements, numElements * sizeof(ELEMENTTYPE));
185  }
186 
187  template<typename ELEMENTTYPE>
188  void nap::VertexAttribute<ELEMENTTYPE>::addData(const ELEMENTTYPE* elements, int numElements)
189  {
190  int cur_num_elements = mData.size();
191  mData.resize(cur_num_elements + numElements);
192  memcpy((void*)&mData[cur_num_elements], elements, numElements * sizeof(ELEMENTTYPE));
193  }
194 
195  template<typename ELEMENTTYPE>
197  {
198  return static_cast<void*>(mData.data());
199  }
200 
201 
203  // Forward declarations of templated functions
205 
206  template<>
207  NAPAPI VkFormat UIntVertexAttribute::getFormat() const;
208 
209  template<>
210  NAPAPI VkFormat IntVertexAttribute::getFormat() const;
211 
212  template<>
213  NAPAPI VkFormat FloatVertexAttribute::getFormat() const;
214 
215  template<>
216  NAPAPI VkFormat Vec2VertexAttribute::getFormat() const;
217 
218  template<>
219  NAPAPI VkFormat Vec3VertexAttribute::getFormat() const;
220 
221  template<>
222  NAPAPI VkFormat Vec4VertexAttribute::getFormat() const;
223 
224  template<>
225  NAPAPI VkFormat IVec4VertexAttribute::getFormat() const;
226 } // nap
227 
nap::VertexAttribute::getData
std::vector< ELEMENTTYPE > & getData()
Definition: vertexattribute.h:100
nap::VertexAttribute::setData
void setData(const std::vector< ELEMENTTYPE > &values)
Definition: vertexattribute.h:119
nap::VertexAttribute::addData
void addData(const ELEMENTTYPE &element)
Definition: vertexattribute.h:106
nap::VertexAttribute::clear
void clear()
Definition: vertexattribute.h:90
nap::VertexAttribute
Definition: vertexattribute.h:65
nap::VertexAttribute::getRawData
virtual void * getRawData() override
Definition: vertexattribute.h:196
nap::BaseVertexAttribute
Definition: vertexattribute.h:20
nap::BaseVertexAttribute::mAttributeID
std::string mAttributeID
Name/ID of the attribute.
Definition: vertexattribute.h:52
nap::VertexAttribute::resize
void resize(size_t numElements)
Definition: vertexattribute.h:85
nap::VertexAttribute::getCapacity
virtual size_t getCapacity() const override
Definition: vertexattribute.h:79
nap::VertexAttribute::getData
const std::vector< ELEMENTTYPE > & getData() const
Definition: vertexattribute.h:95
nap::VertexAttribute::getCount
virtual int getCount() const override
Definition: vertexattribute.h:136
nap::VertexAttribute::operator[]
ELEMENTTYPE & operator[](std::size_t index)
Definition: vertexattribute.h:143
nap
Definition: templateapp.h:17
nap::VertexAttribute::reserve
virtual void reserve(size_t numElements) override
Definition: vertexattribute.h:74
nap::Resource
Definition: resource.h:19
nap::VertexAttribute::operator[]
const ELEMENTTYPE & operator[](std::size_t index) const
Definition: vertexattribute.h:150
nap::VertexAttribute::getFormat
virtual VkFormat getFormat() const override
nap::VertexAttribute::mData
std::vector< ELEMENTTYPE > mData
Actual typed data of the attribute.
Definition: vertexattribute.h:153