diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2024-06-05 19:04:25 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2024-06-05 19:04:25 +0300 |
commit | a6501b5d7ad204c3b998c843c7ed68ef7be323ba (patch) | |
tree | 1a442e6095abf160e36baa31a319858774cd1178 /src/render.c | |
parent | 6af7e785890a322f60c21a51e3d41cd9015e6773 (diff) | |
download | cgame-a6501b5d7ad204c3b998c843c7ed68ef7be323ba.tar.gz cgame-a6501b5d7ad204c3b998c843c7ed68ef7be323ba.tar.bz2 cgame-a6501b5d7ad204c3b998c843c7ed68ef7be323ba.zip |
more vks, stupid mutlistep build, and imgui
Diffstat (limited to 'src/render.c')
-rw-r--r-- | src/render.c | 201 |
1 files changed, 55 insertions, 146 deletions
diff --git a/src/render.c b/src/render.c index ae9c05b..4893d64 100644 --- a/src/render.c +++ b/src/render.c @@ -18,15 +18,15 @@ #include "vk_mem_alloc.h" #define STB_IMAGE_IMPLEMENTATION -#include "../lib/stb_image.h" +#include <stb_image.h> #define FAST_OBJ_IMPLEMENTATION -#include "../lib/fast_obj.h" +#include <fast_obj.h> #define CGLTF_IMPLEMENTATION -#include "../lib/cgltf.h" +#include <cgltf.h> -//#include "cplusplus.h" +#include "cplusplus.h" //#include "vkutil.h" #include "state.h" @@ -383,78 +383,6 @@ load_compile_shader_data(const char * path, shaderc_shader_kind shader_kind) return result; } -VkFormat -findSupportedFormat(VkFormat *candidates, size_t n, - VkImageTiling tiling, - VkFormatFeatureFlags features) -{ - for (size_t i = 0; i < n; i++) { - VkFormat format = candidates[i]; - VkFormatProperties props; - vkGetPhysicalDeviceFormatProperties(s.vk.physical_device, format, &props); - - if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & features) == features) { - return format; - } else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & features) == features) { - return format; - } - } - - vk_log(VK_ERROR, "failed to find supported format!\n"); - abort(); -} - -VkFormat -findDepthFormat() -{ - VkFormat formats[] = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT}; - return findSupportedFormat(formats, VK_ARRAY_LEN(formats), - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); -} - -void -vulkan_create_depth_resources() -{ - VkFormat depth_format = findDepthFormat(); - vks_create_image(s.vk, s.vk.swapchain.extent.width, s.vk.swapchain.extent.height, 1, - depth_format, VK_IMAGE_TILING_OPTIMAL, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, s.vk.msaa_samples, - &s.depth_image); - s.depth_image.view = vks_create_image_view(s.vk, s.depth_image.handle, depth_format, - VK_IMAGE_ASPECT_DEPTH_BIT, 1); - - vks_transition_image_layout_info transition_info = { 0 }; - transition_info.cmd_info.vk = s.vk; - transition_info.cmd_info.pool = s.vk_command_pool; - transition_info.cmd_info.queue = s.vk_graphics_and_compute_queue; - transition_info.image = s.depth_image.handle; - transition_info.format = depth_format; - transition_info.srcAccessMask = VK_ACCESS_NONE; - transition_info.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | - VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; - transition_info.srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - transition_info.dstStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; - transition_info.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; - transition_info.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - transition_info.mipLevels = 1; - - vks_transition_image_layout(&transition_info); -} - -void vulkan_create_color_resources() { - VkFormat colorFormat = s.vk.swapchain.image_format; - - vks_create_image(s.vk, s.vk.swapchain.extent.width, s.vk.swapchain.extent.height, 1, - colorFormat, VK_IMAGE_TILING_OPTIMAL, - VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, s.vk.msaa_samples, - &s.color_image); - s.color_image.view = vks_create_image_view(s.vk, s.color_image.handle, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1); -} - void vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode) { @@ -634,11 +562,12 @@ vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode) depthStencil.back = (VkStencilOpState){0}; // Optional VkPipelineRenderingCreateInfo pipeline_rendering_create_info = { - .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR, - .colorAttachmentCount = 1, - .pColorAttachmentFormats = &s.vk.swapchain.image_format, - .depthAttachmentFormat = findDepthFormat(), - }; + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR, + .colorAttachmentCount = 1, + .pColorAttachmentFormats = &s.vk.swapchain.image_format, + .depthAttachmentFormat = findDepthFormat(s.vk), + }; + VkGraphicsPipelineCreateInfo pipelineInfo = {0}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.stageCount = 2; @@ -675,27 +604,13 @@ vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode) } void -vulkan_create_command_pool() -{ - QueueFamilyIndices queueFamilyIndices = vulkan_find_queue_families(s.vk.physical_device); - - VkCommandPoolCreateInfo poolInfo = {0}; - poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - // AMD doesn't like this flag for some reason - poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsAndComputeFamily; - - VK_CHECK(vkCreateCommandPool(s.vk.device, &poolInfo, NULL, &s.vk_command_pool)); -} - -void vulkan_create_command_buffer() { // TODO Find a way to group allocation for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { VkCommandBufferAllocateInfo allocInfo = {0}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - allocInfo.commandPool = s.vk_command_pool; + allocInfo.commandPool = s.vk.command_pool; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.commandBufferCount = 1; @@ -719,9 +634,6 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) vks_transition_image_layout_info transition_info = { 0 }; - transition_info.cmd_info.vk = s.vk; - transition_info.cmd_info.pool = s.vk_command_pool; - transition_info.cmd_info.queue = s.vk_graphics_and_compute_queue; transition_info.image = s.vk.swapchain.images[imageIndex]; transition_info.format = VK_FORMAT_R8G8B8A8_SRGB; transition_info.srcAccessMask = 0; @@ -732,11 +644,11 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) transition_info.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; transition_info.mipLevels = 1; - vks_transition_image_layout(&transition_info); + vks_transition_image_layout(s.vk, &transition_info); VkRenderingAttachmentInfo colorAttachment = {0}; colorAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO; - colorAttachment.imageView = s.color_image.view; + colorAttachment.imageView = s.vk.color_image.view; colorAttachment.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; colorAttachment.resolveMode = VK_RESOLVE_MODE_AVERAGE_BIT; colorAttachment.resolveImageView = s.vk.swapchain.image_views[imageIndex]; @@ -748,7 +660,7 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) VkRenderingAttachmentInfo depthAttachment = {0}; depthAttachment.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO; - depthAttachment.imageView = s.depth_image.view; + depthAttachment.imageView = s.vk.depth_image.view; depthAttachment.imageLayout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL; depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; @@ -763,6 +675,7 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) renderingInfo.pColorAttachments = &colorAttachment; renderingInfo.pDepthAttachment = &depthAttachment; + vkCmdBeginRendering(commandBuffer, &renderingInfo); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, @@ -791,8 +704,10 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) s.graphics_pipeline.layout, 0, 1, &s.frames[currentFrame].vk_descriptor_set, 0, NULL); - vkCmdDrawIndexed(commandBuffer, s.indices_count, 1, 0, 0, 0); + + imgui_draw(); // does this need to be here? + imgui_draw_cmd(commandBuffer); vkCmdEndRendering(commandBuffer); transition_info.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; @@ -802,7 +717,7 @@ recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) transition_info.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; transition_info.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; - vks_transition_image_layout(&transition_info); + vks_transition_image_layout(s.vk, &transition_info); VK_CHECK(vkEndCommandBuffer(commandBuffer)); } @@ -848,8 +763,7 @@ vulkan_create_vertex_buffer() VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &s.vertex_buffer); - vks_command_info cmd_info = {s.vk, s.vk_command_pool, s.vk_graphics_and_compute_queue}; - vks_copy_buffer(&cmd_info, stagingBuffer.handle, s.vertex_buffer.handle, bufferSize); + vks_copy_buffer(s.vk, stagingBuffer.handle, s.vertex_buffer.handle, bufferSize); vkDestroyBuffer(s.vk.device, stagingBuffer.handle, NULL); vkFreeMemory(s.vk.device, stagingBuffer.memory, NULL); @@ -879,8 +793,7 @@ vulkan_create_index_buffer() VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &s.index_buffer); - vks_command_info cmd_info = {s.vk, s.vk_command_pool, s.vk_graphics_and_compute_queue}; - vks_copy_buffer(&cmd_info, stagingBuffer.handle, s.index_buffer.handle, bufferSize); + vks_copy_buffer(s.vk, stagingBuffer.handle, s.index_buffer.handle, bufferSize); vkDestroyBuffer(s.vk.device, stagingBuffer.handle, NULL); vkFreeMemory(s.vk.device, stagingBuffer.memory, NULL); @@ -1031,13 +944,13 @@ vulkan_create_descriptor_sets() void cleanupSwapChain() { - vkDestroyImageView(s.vk.device, s.color_image.view, NULL); - vkDestroyImage(s.vk.device, s.color_image.handle, NULL); - vkFreeMemory(s.vk.device, s.color_image.memory, NULL); + 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.depth_image.view, NULL); - vkDestroyImage(s.vk.device, s.depth_image.handle, NULL); - vkFreeMemory(s.vk.device, s.depth_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); @@ -1050,17 +963,17 @@ recreateSwapChain() { vkDeviceWaitIdle(s.vk.device); - cleanupSwapChain(); + vks_cleanup_swapchain(s.vk); _vulkan_create_swap_chain(&s.vk); - vulkan_create_color_resources(); - vulkan_create_depth_resources(); + _vulkan_create_color_resources(&s.vk); + _vulkan_create_depth_resources(&s.vk); } void recreate_graphics_pipeline() { - recreateSwapChain(); + vks_recreate_swapchain(&s.vk); vkDestroyPipeline(s.vk.device, s.graphics_pipeline.handle, NULL); vkDestroyPipelineLayout(s.vk.device, s.graphics_pipeline.layout, NULL); vulkan_create_graphics_pipeline(s.polygon_mode); @@ -1078,6 +991,8 @@ handle_input(bool * quit) SDL_Event e; while (SDL_PollEvent(&e) != 0) { + imgui_proc_event(&e); + // User requests quit if (e.type == SDL_QUIT) { *quit = true; @@ -1233,13 +1148,8 @@ vulkan_create_texture_image() VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, VK_SAMPLE_COUNT_1_BIT, &s.texture_image); - vks_command_info cmd_info = {0}; - cmd_info.vk = s.vk; - cmd_info.pool = s.vk_command_pool; - cmd_info.queue = s.vk_graphics_and_compute_queue; vks_transition_image_layout_info transition_info = { 0 }; - transition_info.cmd_info = cmd_info; transition_info.image = s.texture_image.handle; transition_info.format = VK_FORMAT_R8G8B8A8_SRGB; transition_info.srcAccessMask = 0; @@ -1250,14 +1160,14 @@ vulkan_create_texture_image() transition_info.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; transition_info.mipLevels = s.vk_mip_levels; - vks_transition_image_layout(&transition_info); + vks_transition_image_layout(s.vk, &transition_info); - vks_copy_buffer_to_image(&cmd_info, + vks_copy_buffer_to_image(s.vk, stagingBuffer.handle, s.texture_image.handle, (uint32_t)texWidth, (uint32_t)texHeight); - vks_generate_mipmaps(&cmd_info, s.texture_image.handle, VK_FORMAT_R8G8B8A8_SRGB, texWidth, texHeight, s.vk_mip_levels); + vks_generate_mipmaps(s.vk, s.texture_image.handle, VK_FORMAT_R8G8B8A8_SRGB, texWidth, texHeight, s.vk_mip_levels); transition_info.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; transition_info.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; @@ -1266,15 +1176,11 @@ vulkan_create_texture_image() transition_info.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; transition_info.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - vks_transition_image_layout(&transition_info); + vks_transition_image_layout(s.vk, &transition_info); vkDestroyBuffer(s.vk.device, stagingBuffer.handle, NULL); vkFreeMemory(s.vk.device, stagingBuffer.memory, NULL); -} -void -vulkan_create_texture_image_view() -{ 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); } @@ -1425,7 +1331,6 @@ void load_model_gltf() { s.vertices = (Vertex *)malloc(s.vertices_count * sizeof(Vertex)); // Read vertex positions for (cgltf_size k = 0; k < position_accessor->count; ++k) { - float vertex[3]; cgltf_accessor_read_float(position_accessor, k, (cgltf_float *)(&s.vertices[k].pos), 3); } } @@ -1517,11 +1422,10 @@ void vulkan_create_compute_stuff() memcpy(data, particles, (size_t)bufferSize); vkUnmapMemory(s.vk.device, stagingBuffer.memory); - vks_command_info cmd_info = {s.vk, s.vk_command_pool, s.vk_graphics_and_compute_queue}; for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { vks_create_buffer(s.vk, bufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &s.frames[i].shader_storage_buffer); // Copy data from the staging buffer (host) to the shader storage buffer (GPU) - vks_copy_buffer(&cmd_info, stagingBuffer.handle, s.frames[i].shader_storage_buffer.handle, bufferSize); + vks_copy_buffer(s.vk, stagingBuffer.handle, s.frames[i].shader_storage_buffer.handle, bufferSize); } VkDescriptorSetLayoutBinding layoutBindings[3]; @@ -1569,7 +1473,6 @@ void vulkan_create_compute_stuff() } vkDestroyShaderModule(s.vk.device, compShaderModule, NULL); - } void @@ -1579,24 +1482,27 @@ init_vulkan() vk_log(VK_WARN, " DEBUG ON \n"); vk_log(VK_WARN, "====================================\n"); - vks_create_vulkan_context(&s.vk, &s.vk_graphics_and_compute_queue, &s.vk_present_queue); + vks_create_vulkan_context(&s.vk); vulkan_create_descriptor_set_layout(); + vulkan_create_graphics_pipeline(s.polygon_mode); //vulkan_create_compute_stuff(); - vulkan_create_command_pool(); - vulkan_create_depth_resources(); - vulkan_create_color_resources(); + vulkan_create_texture_image(); - vulkan_create_texture_image_view(); vulkan_create_texture_sampler(); + + init_imgui(s.vk); + load_model_obj(); //load_model_gltf(); vulkan_create_vertex_buffer(); vulkan_create_index_buffer(); + vulkan_create_uniform_buffers(); vulkan_create_descriptor_pool(); vulkan_create_descriptor_sets(); + vulkan_create_command_buffer(); vulkan_create_sync_objects(); } @@ -1612,9 +1518,11 @@ close_vulkan() vkDestroyFence(s.vk.device, s.frames[i].in_flight_fence, NULL); } - vkDestroyCommandPool(s.vk.device, s.vk_command_pool, NULL); + vkDestroyCommandPool(s.vk.device, s.vk.command_pool, NULL); + + vks_cleanup_swapchain(s.vk); - cleanupSwapChain(); + imgui_destroy(s.vk); vkDestroySampler(s.vk.device, s.vk_texture_sampler, NULL); vkDestroyImageView(s.vk.device, s.texture_image.view, NULL); @@ -1725,7 +1633,7 @@ draw_frame() VkResult result = vkAcquireNextImageKHR(s.vk.device, s.vk.swapchain.handle, UINT64_MAX, s.frames[currentFrame].image_available_semaphore, VK_NULL_HANDLE, &imageIndex); if (result == VK_ERROR_OUT_OF_DATE_KHR) { - recreateSwapChain(); + vks_recreate_swapchain(&s.vk); return; } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { vk_log(VK_ERROR, "failed to acquire swap chain image!\n"); @@ -1753,7 +1661,7 @@ draw_frame() submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = &s.frames[currentFrame].render_finished_semaphore; - VK_CHECK(vkQueueSubmit(s.vk_graphics_and_compute_queue, 1, &submitInfo, s.frames[currentFrame].in_flight_fence)); + VK_CHECK(vkQueueSubmit(s.vk.graphics_and_compute_queue, 1, &submitInfo, s.frames[currentFrame].in_flight_fence)); VkSwapchainKHR swapChains[] = {s.vk.swapchain.handle}; VkPresentInfoKHR presentInfo = {0}; @@ -1765,11 +1673,11 @@ draw_frame() presentInfo.pImageIndices = &imageIndex; presentInfo.pResults = NULL; - result = vkQueuePresentKHR(s.vk_present_queue, &presentInfo); + result = vkQueuePresentKHR(s.vk.present_queue, &presentInfo); if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || s.sdl_window_resized) { s.sdl_window_resized = 0; - recreateSwapChain(); + vks_recreate_swapchain(&s.vk); } else if (result != VK_SUCCESS) { vk_log(VK_ERROR, "failed to present swap chain image!\n"); } @@ -1820,6 +1728,7 @@ main(int argc, char* argv[]) update_camera(0, 0); while (!quit) { handle_input(&quit); + imgui_new_frame(s.vk); draw_frame(); //SDL_Delay(16); } |