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 
173 
174 
176  // Template Definitions
178 
179  template<typename ELEMENTTYPE>
180  void nap::VertexAttribute<ELEMENTTYPE>::setData(const ELEMENTTYPE* elements, int numElements)
181  {
182  mData.resize(numElements);
183  memcpy(mData.data(), elements, numElements * sizeof(ELEMENTTYPE));
184  }
185 
186  template<typename ELEMENTTYPE>
187  void nap::VertexAttribute<ELEMENTTYPE>::addData(const ELEMENTTYPE* elements, int numElements)
188  {
189  int cur_num_elements = mData.size();
190  mData.resize(cur_num_elements + numElements);
191  memcpy((void*)&mData[cur_num_elements], elements, numElements * sizeof(ELEMENTTYPE));
192  }
193 
194  template<typename ELEMENTTYPE>
196  {
197  return static_cast<void*>(mData.data());
198  }
199 
200 
202  // Forward declarations of templated functions
204 
205  template<>
206  NAPAPI VkFormat UIntVertexAttribute::getFormat() const;
207 
208  template<>
209  NAPAPI VkFormat IntVertexAttribute::getFormat() const;
210 
211  template<>
212  NAPAPI VkFormat FloatVertexAttribute::getFormat() const;
213 
214  template<>
215  NAPAPI VkFormat Vec2VertexAttribute::getFormat() const;
216 
217  template<>
218  NAPAPI VkFormat Vec3VertexAttribute::getFormat() const;
219 
220  template<>
221  NAPAPI VkFormat Vec4VertexAttribute::getFormat() const;
222 
223 } // nap
224 
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:195
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