summaryrefslogtreecommitdiffstats
path: root/src/render.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/render.c')
-rw-r--r--src/render.c200
1 files changed, 88 insertions, 112 deletions
diff --git a/src/render.c b/src/render.c
index 2558bd4..cb4203b 100644
--- a/src/render.c
+++ b/src/render.c
@@ -30,6 +30,10 @@
//#include "vkutil.h"
#include "state.h"
+#define VMA_STATIC_VULKAN_FUNCTIONS 0
+#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1
+#include "vk_mem_alloc.h"
+
#define MODEL_PATH "assets/human.obj"
#define TEXTURE_PATH "assets/viking_room.png"
@@ -745,28 +749,24 @@ vulkan_create_vertex_buffer()
VkDeviceSize bufferSize = sizeof(s.vertices[0]) * s.vertices_count;
/* VkDeviceSize bufferSize = sizeof(vertices[0]) * VERTICES_SIZE; */
- vks_buffer stagingBuffer = {0};
-
- vks_create_buffer(s.vk, bufferSize,
- VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
- VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
- &stagingBuffer);
-
- void *data;
- VK_CHECK(vkMapMemory(s.vk.device, stagingBuffer.memory, 0, bufferSize, 0, &data));
- // memcpy(data, vertices, (size_t) bufferSize);
- memcpy(data, s.vertices, (size_t) bufferSize);
- vkUnmapMemory(s.vk.device, stagingBuffer.memory);
-
- vks_create_buffer(s.vk, bufferSize,
- VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
- VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
- &s.vertex_buffer);
-
- vks_copy_buffer(s.vk, stagingBuffer.handle, s.vertex_buffer.handle, bufferSize);
+ VkBufferCreateInfo bufferInfo = { 0 };
+ bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+ bufferInfo.size = bufferSize;
+ bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
+ bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
- vkDestroyBuffer(s.vk.device, stagingBuffer.handle, NULL);
- vkFreeMemory(s.vk.device, stagingBuffer.memory, NULL);
+ VmaAllocationCreateInfo allocCreateInfo = {0};
+ allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
+ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
+
+ vmaCreateBuffer(s.vk.allocator,
+ &bufferInfo,
+ &allocCreateInfo,
+ &s.vertex_buffer.handle,
+ &s.vertex_buffer.allocation,
+ NULL);
+
+ vmaCopyMemoryToAllocation(s.vk.allocator, s.vertices, s.vertex_buffer.allocation, 0, (size_t)bufferSize);
}
void
@@ -775,28 +775,24 @@ vulkan_create_index_buffer()
VkDeviceSize bufferSize = sizeof(s.indices[0]) * s.indices_count;
/* VkDeviceSize bufferSize = sizeof(indices[0]) * INDICES_SIZE; */
- vks_buffer stagingBuffer = {0};
-
- vks_create_buffer(s.vk, bufferSize,
- VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
- VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
- &stagingBuffer);
-
- void* data;
- VK_CHECK(vkMapMemory(s.vk.device, stagingBuffer.memory, 0, bufferSize, 0, &data));
- memcpy(data, s.indices, (size_t) bufferSize);
- /* memcpy(data, indices, (size_t) bufferSize); */
- vkUnmapMemory(s.vk.device, stagingBuffer.memory);
-
- vks_create_buffer(s.vk, bufferSize,
- VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
- VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
- &s.index_buffer);
-
- vks_copy_buffer(s.vk, stagingBuffer.handle, s.index_buffer.handle, bufferSize);
+ VkBufferCreateInfo bufferInfo = { 0 };
+ bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+ bufferInfo.size = bufferSize;
+ bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
+ bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
- vkDestroyBuffer(s.vk.device, stagingBuffer.handle, NULL);
- vkFreeMemory(s.vk.device, stagingBuffer.memory, NULL);
+ VmaAllocationCreateInfo allocCreateInfo = {0};
+ allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
+ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
+
+ vmaCreateBuffer(s.vk.allocator,
+ &bufferInfo,
+ &allocCreateInfo,
+ &s.index_buffer.handle,
+ &s.index_buffer.allocation,
+ NULL);
+
+ vmaCopyMemoryToAllocation(s.vk.allocator, s.indices, s.index_buffer.allocation, 0, (size_t)bufferSize);
}
void
@@ -833,12 +829,30 @@ vulkan_create_uniform_buffers()
VkDeviceSize bufferSize = sizeof(UniformBufferObject);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
- vks_create_buffer(s.vk, bufferSize,
- VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
- VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
- &s.frames[i].uniform_buffer);
- VK_CHECK(vkMapMemory(s.vk.device, s.frames[i].uniform_buffer.memory, 0, bufferSize, 0, &s.frames[i].uniform_buffer_mapped));
+ VkBufferCreateInfo bufferInfo = { 0 };
+ bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+ bufferInfo.size = bufferSize;
+ bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
+ bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
+
+
+ VmaAllocationCreateInfo allocCreateInfo = {0};
+ allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
+ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
+
+ //if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
+
+ VmaAllocationInfo allocInfo;
+
+ vmaCreateBuffer(s.vk.allocator,
+ &bufferInfo,
+ &allocCreateInfo,
+ &s.frames[i].uniform_buffer.handle,
+ &s.frames[i].uniform_buffer.allocation,
+ &allocInfo);
+
+ s.frames[i].uniform_buffer_mapped = allocInfo.pMappedData;
}
}
@@ -942,35 +956,6 @@ vulkan_create_descriptor_sets()
}
void
-cleanupSwapChain()
-{
- vkDestroyImageView(s.vk.device, s.vk.color_image.view, NULL);
- vkDestroyImage(s.vk.device, s.vk.color_image.handle, NULL);
- vkFreeMemory(s.vk.device, s.vk.color_image.memory, NULL);
-
- vkDestroyImageView(s.vk.device, s.vk.depth_image.view, NULL);
- vkDestroyImage(s.vk.device, s.vk.depth_image.handle, NULL);
- vkFreeMemory(s.vk.device, s.vk.depth_image.memory, NULL);
-
- for (uint32_t i = 0; i < s.vk.swapchain.image_count; i++) {
- vkDestroyImageView(s.vk.device, s.vk.swapchain.image_views[i], NULL);
- }
- vkDestroySwapchainKHR(s.vk.device, s.vk.swapchain.handle, NULL);
-}
-
-void
-recreateSwapChain()
-{
- vkDeviceWaitIdle(s.vk.device);
-
- vks_cleanup_swapchain(s.vk);
-
- _vulkan_create_swap_chain(&s.vk);
- _vulkan_create_color_resources(&s.vk);
- _vulkan_create_depth_resources(&s.vk);
-}
-
-void
recreate_graphics_pipeline()
{
vks_recreate_swapchain(&s.vk);
@@ -1133,13 +1118,25 @@ vulkan_create_texture_image()
vk_log(VK_ERROR, "failed to load texture image!\n");
}
+ VkBufferCreateInfo bufferInfo = { 0 };
+ bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
+ bufferInfo.size = imageSize;
+ bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
+ bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
+
+ VmaAllocationCreateInfo allocCreateInfo = {0};
+ allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
+ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT;
+
vks_buffer stagingBuffer = {0};
+ vmaCreateBuffer(s.vk.allocator,
+ &bufferInfo,
+ &allocCreateInfo,
+ &stagingBuffer.handle,
+ &stagingBuffer.allocation,
+ NULL);
- vks_create_buffer(s.vk, imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffer);
- void *data;
- vkMapMemory(s.vk.device, stagingBuffer.memory, 0, imageSize, 0, &data);
- memcpy(data, pixels, (size_t)imageSize);
- vkUnmapMemory(s.vk.device, stagingBuffer.memory);
+ vmaCopyMemoryToAllocation(s.vk.allocator, pixels, stagingBuffer.allocation, 0, (size_t)imageSize);
stbi_image_free(pixels);
@@ -1178,8 +1175,7 @@ vulkan_create_texture_image()
vks_transition_image_layout(s.vk, &transition_info);
- vkDestroyBuffer(s.vk.device, stagingBuffer.handle, NULL);
- vkFreeMemory(s.vk.device, stagingBuffer.memory, NULL);
+ vmaDestroyBuffer(s.vk.allocator, stagingBuffer.handle, stagingBuffer.allocation);
s.texture_image.view = vks_create_image_view(s.vk, s.texture_image.handle, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, s.vk_mip_levels);
}
@@ -1483,7 +1479,7 @@ init_vulkan()
vk_log(VK_WARN, "====================================\n");
vks_create_vulkan_context(&s.vk);
-
+
vulkan_create_descriptor_set_layout();
vulkan_create_graphics_pipeline(s.polygon_mode);
@@ -1492,7 +1488,7 @@ init_vulkan()
vulkan_create_texture_image();
vulkan_create_texture_sampler();
- init_imgui(s.vk);
+ init_imgui(&s.vk);
load_model_obj();
//load_model_gltf();
@@ -1527,24 +1523,22 @@ close_vulkan()
vkDestroySampler(s.vk.device, s.vk_texture_sampler, NULL);
vkDestroyImageView(s.vk.device, s.texture_image.view, NULL);
- vkDestroyImage(s.vk.device, s.texture_image.handle, NULL);
- vkFreeMemory(s.vk.device, s.texture_image.memory, NULL);
+ vmaDestroyImage(s.vk.allocator, s.texture_image.handle, s.texture_image.allocation);
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
- vkDestroyBuffer(s.vk.device, s.frames[i].uniform_buffer.handle, NULL);
- vkFreeMemory(s.vk.device, s.frames[i].uniform_buffer.memory, NULL);
+ vmaDestroyBuffer(s.vk.allocator, s.frames[i].uniform_buffer.handle, s.frames[i].uniform_buffer.allocation);
}
vkDestroyDescriptorPool(s.vk.device, s.vk_descriptor_pool, NULL);
vkDestroyDescriptorSetLayout(s.vk.device, s.vk_descriptor_set_layout, NULL);
- vkDestroyBuffer(s.vk.device, s.vertex_buffer.handle, NULL);
- vkFreeMemory(s.vk.device, s.vertex_buffer.memory, NULL);
-
- vkDestroyBuffer(s.vk.device, s.index_buffer.handle, NULL);
- vkFreeMemory(s.vk.device, s.index_buffer.memory, NULL);
+ vmaDestroyBuffer(s.vk.allocator, s.vertex_buffer.handle, s.vertex_buffer.allocation);
+ vmaDestroyBuffer(s.vk.allocator, s.index_buffer.handle, s.index_buffer.allocation);
vkDestroyPipeline(s.vk.device, s.graphics_pipeline.handle, NULL);
vkDestroyPipelineLayout(s.vk.device, s.graphics_pipeline.layout, NULL);
+
+ vmaDestroyAllocator(s.vk.allocator);
+
vkDestroyDevice(s.vk.device, NULL);
vkDestroySurfaceKHR(s.vk.instance, s.vk.surface, NULL);
/* if (enable_validation_layers) { */
@@ -1702,22 +1696,6 @@ main(int argc, char* argv[])
bool quit = false;
-
- /* VMA POC */
- VmaVulkanFunctions vulkanFunctions = {0};
- vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
- vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;
-
- VmaAllocatorCreateInfo allocatorCreateInfo = {0};
- allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
- allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2;
- allocatorCreateInfo.physicalDevice = s.vk.physical_device;
- allocatorCreateInfo.device = s.vk.device;
- allocatorCreateInfo.instance = s.vk.instance;
- allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions;
-
- vmaCreateAllocator(&allocatorCreateInfo, &s.vk.allocator);
-
// At the end, don't forget to:
update_camera(0, 0);
@@ -1730,8 +1708,6 @@ main(int argc, char* argv[])
//SDL_Delay(16);
}
- vmaDestroyAllocator(s.vk.allocator);
-
close_vulkan();
closeSDL();