NAP
gpumesh.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 // Local includes
8 #include "gpubuffer.h"
9 
10 // External Includes
11 #include <memory>
12 #include <string>
13 #include <vulkan/vulkan_core.h>
14 #include <unordered_map>
15 #include <utility/dllexport.h>
16 
17 namespace nap
18 {
38  class NAPAPI GPUMesh
39  {
40  public:
45  GPUMesh(RenderService& renderService, EMemoryUsage usage);
46 
47  // Default destructor
48  virtual ~GPUMesh() = default;
49 
50  // Copy construction and copy assignment not allowed
51  GPUMesh(const GPUMesh& other) = delete;
52  GPUMesh& operator=(const GPUMesh& other) = delete;
53 
59  template<typename ELEMENTTYPE>
60  VertexBuffer<ELEMENTTYPE>& addVertexBuffer(const std::string& id);
61 
67  const GPUBufferNumeric* findVertexBuffer(const std::string& id) const;
68 
73  GPUBufferNumeric& getVertexBuffer(const std::string& id);
74 
81  IndexBuffer& getOrCreateIndexBuffer(int index);
82 
88  const IndexBuffer& getIndexBuffer(int index) const;
89 
90  private:
91  using AttributeMap = std::unordered_map<std::string, std::unique_ptr<GPUBufferNumeric>>;
92 
93  Core* mCore;
94  AttributeMap mAttributes;
95  std::vector<std::unique_ptr<IndexBuffer>> mIndexBuffers;
97  };
98 
99 
101  // Template Definitions
103 
104  template<typename ELEMENTTYPE>
106  {
107  auto vertex_buffer = std::make_unique<VertexBuffer<ELEMENTTYPE>>(*mCore, mUsage, false);
108  auto it = mAttributes.emplace(std::make_pair(id, std::move(vertex_buffer))).first;
109  return static_cast<VertexBuffer<ELEMENTTYPE>&>(*it->second);
110  }
111 }
nap::IndexBuffer
Definition: gpubuffer.h:441
nap::EMemoryUsage::Static
@ Static
Buffer data is uploaded only once from the CPU to the GPU.
nap::GPUBufferNumeric
Definition: gpubuffer.h:222
nap::GPUMesh
Definition: gpumesh.h:38
nap::EMemoryUsage
EMemoryUsage
Definition: gpubuffer.h:41
nap::GPUMesh::addVertexBuffer
VertexBuffer< ELEMENTTYPE > & addVertexBuffer(const std::string &id)
Definition: gpumesh.h:105
nap::RenderService
Definition: renderservice.h:275
nap::Core
Definition: core.h:82
nap
Definition: templateapp.h:17
nap::VertexBuffer
Definition: gpubuffer.h:387