21 VertexBuffer::VertexBuffer(uint32_t size) {
22 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
23 VkPhysicalDevice physicalDevice = VulkanContext::getCurrent()->getPhysicalDevice()->getHandle();
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;
33 if (vkCreateBuffer(device, &createInfo,
nullptr, &handle) != VK_SUCCESS) {
34 throw runtime_error(
"Could not create a vertex buffer!");
38 VkMemoryRequirements memoryRequirements;
39 vkGetBufferMemoryRequirements(device, handle, &memoryRequirements);
42 VkPhysicalDeviceMemoryProperties memoryProperties;
43 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
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) {
57 VkMemoryAllocateInfo allocateInfo = {};
58 allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
59 allocateInfo.allocationSize = memoryRequirements.size;
60 allocateInfo.memoryTypeIndex = memoryTypeIndex;
63 if (vkAllocateMemory(device, &allocateInfo,
nullptr, &memory) != VK_SUCCESS) {
64 throw runtime_error(
"Could not allocate vertex buffer memory!");
68 vkBindBufferMemory(device, handle, memory, 0);
71 VertexBuffer::~VertexBuffer(
void) {
72 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
75 vkDestroyBuffer(device, handle,
nullptr);
76 vkFreeMemory(device, memory,
nullptr);
79 VkBuffer VertexBuffer::getHandle() {
83 void VertexBuffer::upload(std::vector<float> data) {
84 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
88 vkMapMemory(device, memory, 0, data.size() *
sizeof (float), 0, &mapData);
89 memcpy(mapData, data.data(), data.size() *
sizeof (float));
90 vkUnmapMemory(device, memory);
93 void VertexBuffer::upload(std::vector<glm::vec2> data) {
94 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
98 vkMapMemory(device, memory, 0, data.size() *
sizeof (vec2), 0, &mapData);
99 memcpy(mapData, data.data(), data.size() *
sizeof (vec2));
100 vkUnmapMemory(device, memory);
103 void VertexBuffer::upload(std::vector<glm::vec3> data) {
104 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
108 vkMapMemory(device, memory, 0, data.size() *
sizeof (vec3), 0, &mapData);
109 memcpy(mapData, data.data(), data.size() *
sizeof (vec3));
110 vkUnmapMemory(device, memory);
113 void VertexBuffer::upload(std::vector<glm::vec4> data) {
114 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
118 vkMapMemory(device, memory, 0, data.size() *
sizeof (vec4), 0, &mapData);
119 memcpy(mapData, data.data(), data.size() *
sizeof (vec4));
120 vkUnmapMemory(device, memory);
123 UniformBuffer::UniformBuffer(uint32_t size) {
124 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
125 VkPhysicalDevice physicalDevice = VulkanContext::getCurrent()->getPhysicalDevice()->getHandle();
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;
135 if (vkCreateBuffer(device, &createInfo,
nullptr, &handle) != VK_SUCCESS) {
136 throw runtime_error(
"Could not create a uniform buffer!");
140 VkMemoryRequirements memoryRequirements;
141 vkGetBufferMemoryRequirements(device, handle, &memoryRequirements);
144 VkPhysicalDeviceMemoryProperties memoryProperties;
145 vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
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) {
159 VkMemoryAllocateInfo allocateInfo = {};
160 allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
161 allocateInfo.allocationSize = memoryRequirements.size;
162 allocateInfo.memoryTypeIndex = memoryTypeIndex;
165 if (vkAllocateMemory(device, &allocateInfo,
nullptr, &memory) != VK_SUCCESS) {
166 throw runtime_error(
"Could not allocate uniform buffer memory!");
170 vkBindBufferMemory(device, handle, memory, 0);
173 UniformBuffer::~UniformBuffer(
void) {
174 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
177 vkDestroyBuffer(device, handle,
nullptr);
178 vkFreeMemory(device, memory,
nullptr);
181 VkBuffer UniformBuffer::getHandle() {
185 void UniformBuffer::upload(std::vector<glm::mat2> data) {
186 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
190 vkMapMemory(device, memory, 0, data.size() *
sizeof (mat2), 0, &mapData);
191 memcpy(mapData, data.data(), data.size() *
sizeof (mat2));
192 vkUnmapMemory(device, memory);
195 void UniformBuffer::upload(std::vector<glm::mat3> data) {
196 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
200 vkMapMemory(device, memory, 0, data.size() *
sizeof (mat3), 0, &mapData);
201 memcpy(mapData, data.data(), data.size() *
sizeof (mat3));
202 vkUnmapMemory(device, memory);
205 void UniformBuffer::upload(std::vector<glm::mat4> data) {
206 VkDevice device = VulkanContext::getCurrent()->getDevice()->getHandle();
210 vkMapMemory(device, memory, 0, data.size() *
sizeof (mat4), 0, &mapData);
211 memcpy(mapData, data.data(), data.size() *
sizeof (mat4));
212 vkUnmapMemory(device, memory);
Generic namespace for the SimpleGL framework.