SimpleGL  1.1.0
A framework for platform independent rendering
vertex_buffer.cpp
Go to the documentation of this file.
1 
9 
10 #include <cstring>
11 
12 #include <stdexcept>
13 
15 
16 using namespace std;
17 using namespace glm;
18 
19 namespace sgl {
20 
21  VertexBuffer::VertexBuffer(uint32_t size) {
22  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
23  VkPhysicalDevice physicalDevice = VulkanContext::getCurrent()->getPhysicalDevice()->getHandle();
24 
25  /* Initialize a buffer create info */
26  VkBufferCreateInfo createInfo = {};
27  createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
28  createInfo.size = size;
29  createInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
30  createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
31 
32  /* Create the vertex buffer */
33  if (vkCreateBuffer(device, &createInfo, nullptr, &handle) != VK_SUCCESS) {
34  throw runtime_error("Could not create a vertex buffer!");
35  }
36 
37  /* Get memory requirements */
38  VkMemoryRequirements memoryRequirements;
39  vkGetBufferMemoryRequirements(device, handle, &memoryRequirements);
40 
41  /* Get the device memory properties */
42  VkPhysicalDeviceMemoryProperties memoryProperties;
43  vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
44 
45  /* Determine memory type index */
46  uint32_t memoryTypeIndex = -1;
47  VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
48  for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) {
49  if ((memoryRequirements.memoryTypeBits & (1 << i)) &&
50  (memoryProperties.memoryTypes[i].propertyFlags & flags) == flags) {
51  memoryTypeIndex = i;
52  break;
53  }
54  }
55 
56  /* Initialize a memory allocate info */
57  VkMemoryAllocateInfo allocateInfo = {};
58  allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
59  allocateInfo.allocationSize = memoryRequirements.size;
60  allocateInfo.memoryTypeIndex = memoryTypeIndex;
61 
62  /* Allocate the vertex buffer memory */
63  if (vkAllocateMemory(device, &allocateInfo, nullptr, &memory) != VK_SUCCESS) {
64  throw runtime_error("Could not allocate vertex buffer memory!");
65  }
66 
67  /* Bind the vertex buffer memory */
68  vkBindBufferMemory(device, handle, memory, 0);
69  }
70 
71  VertexBuffer::~VertexBuffer(void) {
72  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
73 
74  /* Destroy the vertex buffer and free the memory */
75  vkDestroyBuffer(device, handle, nullptr);
76  vkFreeMemory(device, memory, nullptr);
77  }
78 
79  VkBuffer VertexBuffer::getHandle() {
80  return handle;
81  }
82 
83  void VertexBuffer::upload(std::vector<float> data) {
84  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
85 
86  /* Upload data */
87  void* mapData;
88  vkMapMemory(device, memory, 0, data.size() * sizeof (float), 0, &mapData);
89  memcpy(mapData, data.data(), data.size() * sizeof (float));
90  vkUnmapMemory(device, memory);
91  }
92 
93  void VertexBuffer::upload(std::vector<glm::vec2> data) {
94  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
95 
96  /* Upload data */
97  void* mapData;
98  vkMapMemory(device, memory, 0, data.size() * sizeof (vec2), 0, &mapData);
99  memcpy(mapData, data.data(), data.size() * sizeof (vec2));
100  vkUnmapMemory(device, memory);
101  }
102 
103  void VertexBuffer::upload(std::vector<glm::vec3> data) {
104  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
105 
106  /* Upload data */
107  void* mapData;
108  vkMapMemory(device, memory, 0, data.size() * sizeof (vec3), 0, &mapData);
109  memcpy(mapData, data.data(), data.size() * sizeof (vec3));
110  vkUnmapMemory(device, memory);
111  }
112 
113  void VertexBuffer::upload(std::vector<glm::vec4> data) {
114  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
115 
116  /* Upload data */
117  void* mapData;
118  vkMapMemory(device, memory, 0, data.size() * sizeof (vec4), 0, &mapData);
119  memcpy(mapData, data.data(), data.size() * sizeof (vec4));
120  vkUnmapMemory(device, memory);
121  }
122 
123  UniformBuffer::UniformBuffer(uint32_t size) {
124  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
125  VkPhysicalDevice physicalDevice = VulkanContext::getCurrent()->getPhysicalDevice()->getHandle();
126 
127  /* Initialize a buffer create info */
128  VkBufferCreateInfo createInfo = {};
129  createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
130  createInfo.size = size;
131  createInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
132  createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
133 
134  /* Create the vertex buffer */
135  if (vkCreateBuffer(device, &createInfo, nullptr, &handle) != VK_SUCCESS) {
136  throw runtime_error("Could not create a uniform buffer!");
137  }
138 
139  /* Get memory requirements */
140  VkMemoryRequirements memoryRequirements;
141  vkGetBufferMemoryRequirements(device, handle, &memoryRequirements);
142 
143  /* Get the device memory properties */
144  VkPhysicalDeviceMemoryProperties memoryProperties;
145  vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
146 
147  /* Determine memory type index */
148  uint32_t memoryTypeIndex = -1;
149  VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
150  for (uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++) {
151  if ((memoryRequirements.memoryTypeBits & (1 << i)) &&
152  (memoryProperties.memoryTypes[i].propertyFlags & flags) == flags) {
153  memoryTypeIndex = i;
154  break;
155  }
156  }
157 
158  /* Initialize a memory allocate info */
159  VkMemoryAllocateInfo allocateInfo = {};
160  allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
161  allocateInfo.allocationSize = memoryRequirements.size;
162  allocateInfo.memoryTypeIndex = memoryTypeIndex;
163 
164  /* Allocate the uniform buffer memory */
165  if (vkAllocateMemory(device, &allocateInfo, nullptr, &memory) != VK_SUCCESS) {
166  throw runtime_error("Could not allocate uniform buffer memory!");
167  }
168 
169  /* Bind the uniform buffer memory */
170  vkBindBufferMemory(device, handle, memory, 0);
171  }
172 
173  UniformBuffer::~UniformBuffer(void) {
174  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
175 
176  /* Destroy the uniform buffer and free the memory */
177  vkDestroyBuffer(device, handle, nullptr);
178  vkFreeMemory(device, memory, nullptr);
179  }
180 
181  VkBuffer UniformBuffer::getHandle() {
182  return handle;
183  }
184 
185  void UniformBuffer::upload(std::vector<glm::mat2> data) {
186  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
187 
188  /* Upload data */
189  void* mapData;
190  vkMapMemory(device, memory, 0, data.size() * sizeof (mat2), 0, &mapData);
191  memcpy(mapData, data.data(), data.size() * sizeof (mat2));
192  vkUnmapMemory(device, memory);
193  }
194 
195  void UniformBuffer::upload(std::vector<glm::mat3> data) {
196  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
197 
198  /* Upload data */
199  void* mapData;
200  vkMapMemory(device, memory, 0, data.size() * sizeof (mat3), 0, &mapData);
201  memcpy(mapData, data.data(), data.size() * sizeof (mat3));
202  vkUnmapMemory(device, memory);
203  }
204 
205  void UniformBuffer::upload(std::vector<glm::mat4> data) {
206  VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
207 
208  /* Upload data */
209  void* mapData;
210  vkMapMemory(device, memory, 0, data.size() * sizeof (mat4), 0, &mapData);
211  memcpy(mapData, data.data(), data.size() * sizeof (mat4));
212  vkUnmapMemory(device, memory);
213  }
214 }
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18