/ Core / Clockworks / CwGraphics-VertexBuffer.cpp
CwGraphics-VertexBuffer.cpp
 1  #include <Clockworks/CwGraphics.hpp>
 2  
 3  void CwGraphics::CreateVertexBuffer() {
 4          VkBufferCreateInfo CInfo{};
 5          CInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
 6          CInfo.size = Vertices.size() * sizeof(Vertices[0]);
 7          CInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
 8          CInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
 9  
10          // std::cout << sizeof(Vertex);
11          if (vkCreateBuffer(Dev, &CInfo, nullptr, &VertexBuffer) != VK_SUCCESS)
12                  throw std::runtime_error("CW-ERROR: Failed to create The VertexBuffer! :(\n");
13  
14          VkMemoryRequirements MemoryRequirements;
15          vkGetBufferMemoryRequirements(Dev, VertexBuffer, &MemoryRequirements);
16  
17          VkMemoryAllocateInfo AllocateInfo{};
18          AllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
19          AllocateInfo.allocationSize = MemoryRequirements.size;
20          AllocateInfo.memoryTypeIndex =
21              FindMemoryType(MemoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
22  
23          if (vkAllocateMemory(Dev, &AllocateInfo, nullptr, &VertexBufferMemory))
24                  throw std::runtime_error("CW-ERROR: Failed to Allocate VertexBuffer Memory :(\n");
25  
26          vkBindBufferMemory(Dev, VertexBuffer, VertexBufferMemory, 0);
27          void* data;
28          vkMapMemory(Dev, VertexBufferMemory, 0, CInfo.size, 0, &data);
29          memcpy(data, Vertices.data(), (size_t)CInfo.size);
30          vkUnmapMemory(Dev, VertexBufferMemory);
31  }
32  
33  uint32_t CwGraphics::FindMemoryType(uint32_t TypeFilter, VkMemoryPropertyFlags Properties) {
34          VkPhysicalDeviceMemoryProperties MemoryProperties;
35          vkGetPhysicalDeviceMemoryProperties(PhysicalDev, &MemoryProperties);
36  
37          for (uint32_t i = 0; i < MemoryProperties.memoryTypeCount; i++) {
38                  if ((TypeFilter & (1 << i)) && (MemoryProperties.memoryTypes[i].propertyFlags & Properties) == Properties) {
39                          return i;
40                  }
41          }
42          throw std::runtime_error("CW-ERROR: failed to find suitable memory type! :(\n");
43  }