vOOlkan
An object oriented approach to Vulkan
IndexBuffer.h
Go to the documentation of this file.
1#ifndef VULKAN_INDEXBUFFER
2#define VULKAN_INDEXBUFFER
3
4#include <vulkan/vulkan.h>
5
6#include "LogicalDevice.h"
7#include "PhysicalDevice.h"
8#include "VulkanException.h"
9#include "Buffer.h"
10#include "Model.h"
11
12
13namespace Vulkan::Buffers { class IndexBuffer; }
14
16public:
17
18 IndexBuffer(const LogicalDevice& virtualGpu, const PhysicalDevice& realGpu, size_t size) : Buffer(virtualGpu, realGpu, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
19
20 }
21
22
23 template<typename... C, template<typename...> class... M> requires (std::same_as<M<C...>, Objects::Model<C...>> && ...)
24 void fillBuffer(const M<C...>&... models) {
25 std::vector<uint32_t> data{};
26
27 unsigned int currentOffset = 0;
28 unsigned int totalVertices = 0; //how many vertices (NOT indexes) are there in the models from 0 to current
29 ([this, &currentOffset, &totalVertices, &data](const M<C...>& model) {
30 //copy all the vertices to one vector
31 for (auto index : model.getIndexes()) {
32 data.push_back(index + totalVertices);
33 }
34
35 modelOffsets.push_back(currentOffset); //save the offset for this model
36 currentOffset += model.getIndexes().size(); //increase next offset
37 totalVertices += model.getVertices().size(); //increas the total number of vertices present in the models so far
38 }(models), ...);
39 modelOffsets.push_back(currentOffset); //save the last offset (= indexesCount) in order to speed up the getModelIndexesCount function
40 indexesCount = data.size();
41
42 //TODO staging buffer
43
44 //copy data to vertex buffer
45 void* rawData;
46 vkMapMemory(+virtualGpu, bufferMemory, 0, data.size() * sizeof(data[0]), 0, &rawData);
47 memcpy(rawData, data.data(), data.size() * sizeof(data[0]));
48 vkUnmapMemory(+virtualGpu, bufferMemory);
49 }
50
51
52 unsigned int getIndexesCount() const {
53 return indexesCount;
54 }
55
56
57 unsigned int getModelOffset(unsigned int i) const {
58 return modelOffsets[i];
59 }
60
61
62 unsigned int getModelIndexesCount(unsigned int i) const {
63 return modelOffsets[i + 1] - modelOffsets[i];
64 }
65
66
67 unsigned int getModelsCount() const {
68 return modelOffsets.size() - 1;
69 }
70
71
72private:
73
74 unsigned int indexesCount;
75 std::vector<unsigned int> modelOffsets;
76};
77
78#endif
Definition: Buffer.h:14
const size_t size
Definition: Buffer.h:112
const LogicalDevice & virtualGpu
Definition: Buffer.h:111
VkDeviceMemory bufferMemory
Definition: Buffer.h:110
Definition: IndexBuffer.h:15
unsigned int getIndexesCount() const
Definition: IndexBuffer.h:52
IndexBuffer(const LogicalDevice &virtualGpu, const PhysicalDevice &realGpu, size_t size)
Definition: IndexBuffer.h:18
unsigned int getModelOffset(unsigned int i) const
Definition: IndexBuffer.h:57
void fillBuffer(const M< C... > &... models)
Definition: IndexBuffer.h:24
unsigned int getModelsCount() const
Definition: IndexBuffer.h:67
unsigned int getModelIndexesCount(unsigned int i) const
Definition: IndexBuffer.h:62
A logical device is an abstraction of the physical GPU which we can mainly use to send commands.
Definition: LogicalDevice.h:15
Definition: Model.h:27
Represents the GPU (or any other device) that will be used with Vulkan to perform computer graphics.
Definition: PhysicalDevice.h:17
Definition: Buffer.h:12