diff options
-rw-r--r-- | assets/monkey.glb | bin | 98784 -> 98760 bytes | |||
-rw-r--r-- | b.c | 20 | ||||
-rw-r--r-- | src/cplusplus.cpp | 29 | ||||
-rw-r--r-- | src/cplusplus.h | 4 | ||||
-rw-r--r-- | src/render.c | 200 | ||||
-rw-r--r-- | src/shader.frag | 13 | ||||
-rw-r--r-- | src/vksetup.h | 81 |
7 files changed, 184 insertions, 163 deletions
diff --git a/assets/monkey.glb b/assets/monkey.glb Binary files differindex 0337d2e..2aa856f 100644 --- a/assets/monkey.glb +++ b/assets/monkey.glb @@ -10,7 +10,7 @@ void debug_or_release(B_Cmd* cmd) { if (debug_level == 0) - b_cmd_append(cmd, "-O3", "-s"); + b_cmd_append(cmd, "-O3", "-s", "-DNDEBUG"); else if (debug_level == 1) b_cmd_append(cmd, "-O2", "-ggdb", "-DVKDEBUG"); else if (debug_level == 2) @@ -37,6 +37,7 @@ cflags(B_Cmd* cmd) b_cmd_append(cmd, "-march=native"); b_cmd_append(cmd, "-fno-math-errno", "-funroll-loops"); b_cmd_append(cmd, "-flto", "-pthread"); + b_cmd_append(cmd, "-fsanitize=address"); inlcude_dirs(cmd); } @@ -51,6 +52,7 @@ void cxxflags(B_Cmd *cmd) b_cmd_append(cmd, "-march=native"); b_cmd_append(cmd, "-fno-math-errno", "-funroll-loops"); b_cmd_append(cmd, "-flto", "-pthread"); + b_cmd_append(cmd, "-fsanitize=address"); inlcude_dirs(cmd); //b_cmd_append(cmd, "-O3"); @@ -216,6 +218,22 @@ main(int argc, char *argv[]) } } + /* const char* const new_deps[][100] = { */ + /* { "src/state.h" }, */ + /* { "src/vksetup.h" }, */ + /* { "src/cplusplus", */ + /* "lib/imgui-1.90.7/imgui.cpp", */ + /* "lib/imgui-1.90.7/imgui_demo.cpp", */ + /* "lib/imgui-1.90.7/imgui_draw.cpp", */ + /* "lib/imgui-1.90.7/imgui_tables.cpp", */ + /* "lib/imgui-1.90.7/imgui_widgets.cpp", */ + /* "lib/imgui-1.90.7/backends/imgui_impl_sdl2.cpp", */ + /* "lib/imgui-1.90.7/backends/imgui_impl_vulkan.cpp" }, */ + /* {"src/testlib", "src/hash" } */ + /* }; */ + + /* if (!build("render", "src/render.c", new_deps, B_ARRAY_LEN(new_deps))) return 1; */ + const char* cplusplus_deps[] = { "lib/imgui-1.90.7/imgui.cpp", "lib/imgui-1.90.7/imgui_demo.cpp", diff --git a/src/cplusplus.cpp b/src/cplusplus.cpp index 750a5bf..35876f5 100644 --- a/src/cplusplus.cpp +++ b/src/cplusplus.cpp @@ -2,6 +2,9 @@ #include "cplusplus.h" #include "vksetup.h" +#define VMA_DEBUG_INITIALIZE_ALLOCATIONS 1 +#define VMA_STATIC_VULKAN_FUNCTIONS 0 +#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 #define VMA_IMPLEMENTATION #include "vk_mem_alloc.h" @@ -39,10 +42,12 @@ imgui_new_frame(vks_context vk) ImGui::NewFrame(); - + ImGui::ShowDemoWindow(); -} + ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM); + +} bool imgui_proc_event(SDL_Event* e) @@ -52,7 +57,7 @@ imgui_proc_event(SDL_Event* e) static VkDescriptorPool imguiPool; void -init_imgui(vks_context vk) +init_imgui(vks_context *vk) { VkDescriptorPoolSize pool_sizes[] = { { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, @@ -75,7 +80,7 @@ init_imgui(vks_context vk) pool_info.poolSizeCount = std::size(pool_sizes); pool_info.pPoolSizes = pool_sizes; - VK_CHECK(vkCreateDescriptorPool(vk.device, &pool_info, nullptr, &imguiPool)); + VK_CHECK(vkCreateDescriptorPool(vk->device, &pool_info, nullptr, &imguiPool)); IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -84,27 +89,27 @@ init_imgui(vks_context vk) io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls - ImGui_ImplSDL2_InitForVulkan(vk.window); + ImGui_ImplSDL2_InitForVulkan(vk->window); ImGui_ImplVulkan_InitInfo init_info = {}; - init_info.Instance = vk.instance; - init_info.PhysicalDevice = vk.physical_device; - init_info.Device = vk.device; + init_info.Instance = vk->instance; + init_info.PhysicalDevice = vk->physical_device; + init_info.Device = vk->device; //init_info.QueueFamily = ; - init_info.Queue = vk.graphics_and_compute_queue; + init_info.Queue = vk->graphics_and_compute_queue; //init_info.PipelineCache = YOUR_PIPELINE_CACHE; init_info.DescriptorPool = imguiPool; init_info.Subpass = 0; init_info.MinImageCount = 2; init_info.ImageCount = 2; - init_info.MSAASamples = vk.msaa_samples; + init_info.MSAASamples = vk->msaa_samples; init_info.CheckVkResultFn = check_vk_result; init_info.UseDynamicRendering = true; //init_info. init_info.PipelineRenderingCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR; init_info.PipelineRenderingCreateInfo.colorAttachmentCount = 1; - init_info.PipelineRenderingCreateInfo.pColorAttachmentFormats = &vk.swapchain.image_format; - init_info.PipelineRenderingCreateInfo.depthAttachmentFormat = findDepthFormat(vk); + init_info.PipelineRenderingCreateInfo.pColorAttachmentFormats = &vk->swapchain.image_format; + init_info.PipelineRenderingCreateInfo.depthAttachmentFormat = findDepthFormat(*vk); ImGui_ImplVulkan_Init(&init_info); // (this gets a bit more complicated, see example app for full reference) diff --git a/src/cplusplus.h b/src/cplusplus.h index 23d6a87..31c6fcd 100644 --- a/src/cplusplus.h +++ b/src/cplusplus.h @@ -2,7 +2,7 @@ #define CPLUSPLUS_H //#undef VKSETUP_IMPLEMENTATION -#include "vksetup.h" +#include "vksetup.h" #ifdef __cplusplus extern "C" { @@ -11,7 +11,7 @@ extern "C" { // cpp funcs void imgui_destroy(vks_context vk); -void init_imgui(vks_context vk); +void init_imgui(vks_context *vk); bool imgui_proc_event(SDL_Event* e); 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(); diff --git a/src/shader.frag b/src/shader.frag index 3e80e21..c29a640 100644 --- a/src/shader.frag +++ b/src/shader.frag @@ -55,10 +55,15 @@ float fbm(vec2 p) { } void main() { - vec2 uv = gl_FragCoord.xy / ubo.resolution.xy; - vec2 p = uv * 2.0 - vec2(1.0); - float n = fbm(p * 5.0 - vec2(0.0, ubo.time * 2.0)); - outColor = vec4(n, n * 0.5, 0.0, 1.0) * vec4(texture(texSampler, fragTexCoord).rgb, 1.0); + vec2 uv = gl_FragCoord.xy / ubo.resolution.xy; + vec2 p = uv * 2.0 - vec2(1.0); + float n = fbm(p * 5.0 - vec2(0.0, ubo.time * 2.0)); + // outColor = vec4(n, n * 0.5, 0.0, 1.0) * vec4(texture(texSampler, fragTexCoord).rgb, 1.0); + if (mod(gl_FragCoord.x, 20) > 19 || mod(gl_FragCoord.y, 20) > 19) { + outColor = vec4(0,0,0, 1); + } else { + outColor = vec4(n, n * 0.5, 0.0, 1.0); + } } // void main() { diff --git a/src/vksetup.h b/src/vksetup.h index 608975b..adf4699 100644 --- a/src/vksetup.h +++ b/src/vksetup.h @@ -47,8 +47,7 @@ #include <shaderc/shaderc.h> -#define VMA_STATIC_VULKAN_FUNCTIONS 0 -#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 + #include "vk_mem_alloc.h" #include "../lib/cglm/include/cglm/cglm.h" @@ -164,12 +163,14 @@ typedef struct vks_swapchain { typedef struct vks_buffer { VkBuffer handle; VkDeviceMemory memory; + VmaAllocation allocation; } vks_buffer; typedef struct vks_image { VkImage handle; VkDeviceMemory memory; VkImageView view; + VmaAllocation allocation; } vks_image; typedef struct vks_pipeline { @@ -991,13 +992,11 @@ VKSDEF void vks_cleanup_swapchain(const vks_context vk) { vkDestroyImageView(vk.device, vk.color_image.view, NULL); - vkDestroyImage(vk.device, vk.color_image.handle, NULL); - vkFreeMemory(vk.device, vk.color_image.memory, NULL); + vmaDestroyImage(vk.allocator, vk.color_image.handle, vk.color_image.allocation); vkDestroyImageView(vk.device, vk.depth_image.view, NULL); - vkDestroyImage(vk.device, vk.depth_image.handle, NULL); - vkFreeMemory(vk.device, vk.depth_image.memory, NULL); - + vmaDestroyImage(vk.allocator, vk.depth_image.handle, vk.depth_image.allocation); + for (uint32_t i = 0; i < vk.swapchain.image_count; i++) { vkDestroyImageView(vk.device, vk.swapchain.image_views[i], NULL); } @@ -1040,12 +1039,29 @@ vks_create_vulkan_context(vks_context* vk) /* TODO: Create vks_get_queue helpers?? */ _vulkan_create_logical_device(vk); + _vulkan_create_command_pool(vk); /* Create swapchain */ /* TODO: Make swapchain api */ _vulkan_create_swap_chain(vk); + + /* 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 = vk->physical_device; + allocatorCreateInfo.device = vk->device; + allocatorCreateInfo.instance = vk->instance; + allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; + + vmaCreateAllocator(&allocatorCreateInfo, &vk->allocator); + /* Create resources */ _vulkan_create_color_resources(vk); _vulkan_create_depth_resources(vk); @@ -1287,21 +1303,15 @@ vks_create_image(const vks_context vk, imageInfo.samples = numSamples; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - VK_CHECK(vkCreateImage(vk.device, &imageInfo, NULL, &image->handle)); + VmaAllocationCreateInfo allocInfo = {0}; + allocInfo.usage = VMA_MEMORY_USAGE_AUTO; - VkMemoryRequirements memRequirements; - vkGetImageMemoryRequirements(vk.device, image->handle, &memRequirements); - - VkMemoryAllocateInfo allocInfo = { 0 }; - allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - allocInfo.allocationSize = memRequirements.size; - allocInfo.memoryTypeIndex = - _find_memory_type(vk, memRequirements.memoryTypeBits, properties); - - // TODO: group allocations etc... (allocations limited by hardware) - VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &image->memory)); - - vkBindImageMemory(vk.device, image->handle, image->memory, 0); + VK_CHECK(vmaCreateImage(vk.allocator, + &imageInfo, + &allocInfo, + &image->handle, + &image->allocation, + NULL)); } VKSDEF void @@ -1317,19 +1327,26 @@ vks_create_buffer(const vks_context vk, bufferInfo.usage = usage; bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - VK_CHECK(vkCreateBuffer(vk.device, &bufferInfo, NULL, &buffer->handle)); - VkMemoryRequirements mem_requirements; - vkGetBufferMemoryRequirements(vk.device, buffer->handle, &mem_requirements); + VmaAllocationCreateInfo allocInfo = {0}; + allocInfo.usage = VMA_MEMORY_USAGE_AUTO; + allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; + + //if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) + + vmaCreateBuffer(vk.allocator, &bufferInfo, &allocInfo, &buffer->handle, &buffer->allocation, NULL); + + /* VkMemoryRequirements mem_requirements; */ + /* vkGetBufferMemoryRequirements(vk.device, buffer->handle, &mem_requirements); */ - VkMemoryAllocateInfo allocInfo = { 0 }; - allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; - allocInfo.allocationSize = mem_requirements.size; - allocInfo.memoryTypeIndex = - _find_memory_type(vk, mem_requirements.memoryTypeBits, properties); + /* VkMemoryAllocateInfo allocInfo = { 0 }; */ + /* allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; */ + /* allocInfo.allocationSize = mem_requirements.size; */ + /* allocInfo.memoryTypeIndex = */ + /* _find_memory_type(vk, mem_requirements.memoryTypeBits, properties); */ - // TODO: group allocations etc... (allocations limited by hardware) - VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &buffer->memory)); - VK_CHECK(vkBindBufferMemory(vk.device, buffer->handle, buffer->memory, 0)); + /* // TODO: group allocations etc... (allocations limited by hardware) */ + /* VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &buffer->memory)); */ + /* VK_CHECK(vkBindBufferMemory(vk.device, buffer->handle, buffer->memory, 0)); */ } #endif /* VKSETUP_IMPLEMENTATION */ |