diff --git a/vk_engine.cpp b/vk_engine.cpp index da914ab..6e9dbc8 100644 --- a/vk_engine.cpp +++ b/vk_engine.cpp @@ -150,16 +150,15 @@ void VulkanEngine::draw() // this will put the image we just rendered to into the visible window. // we want to wait on the _renderSemaphore for that, // as its necessary that drawing commands have finished before the image is displayed to the user - VkPresentInfoKHR presentInfo{}; - presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; - presentInfo.pNext = nullptr; - presentInfo.pSwapchains = &_swapchain; - presentInfo.swapchainCount = 1; - - presentInfo.pWaitSemaphores = &get_current_frame()._renderSemaphore; - presentInfo.waitSemaphoreCount = 1; - - presentInfo.pImageIndices = &swapchainImageIndex; + VkPresentInfoKHR presentInfo{ + .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, + .pNext = nullptr, + .waitSemaphoreCount = 1, + .pWaitSemaphores = &get_current_frame()._renderSemaphore, + .swapchainCount = 1, + .pSwapchains = &_swapchain, + .pImageIndices = &swapchainImageIndex, + }; VK_CHECK(vkQueuePresentKHR(_graphicsQueue, &presentInfo)); @@ -251,18 +250,19 @@ void VulkanEngine::init_vulkan() { SDL_Vulkan_CreateSurface(_window, _instance, &_surface); //vulkan 1.3 features - VkPhysicalDeviceVulkan13Features features13{}; - - features13.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES; - features13.dynamicRendering = true; - features13.synchronization2 = true; - + VkPhysicalDeviceVulkan13Features features13{ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES, + .synchronization2 = true, + .dynamicRendering = true, + }; + //vulkan 1.2 features - VkPhysicalDeviceVulkan12Features features12{}; - features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES; - features12.bufferDeviceAddress = true; - features12.descriptorIndexing = true; - + VkPhysicalDeviceVulkan12Features features12{ + .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES, + .descriptorIndexing = true, + .bufferDeviceAddress = true, + }; + // use vkbootstrap to select a gpu // We want a gpu that can write to the SDL surface vkb::PhysicalDeviceSelector selector{ vkb_inst }; @@ -284,11 +284,13 @@ void VulkanEngine::init_vulkan() { _graphicsQueueFamily = vkbDevice.get_queue_index(vkb::QueueType::graphics).value(); // initialize the memory allocator - VmaAllocatorCreateInfo allocatorInfo{}; - allocatorInfo.physicalDevice = _chosenGPU; - allocatorInfo.device = _device; - allocatorInfo.instance = _instance; - allocatorInfo.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT; + VmaAllocatorCreateInfo allocatorInfo{ + .flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT, + .physicalDevice = _chosenGPU, + .device = _device, + .instance = _instance, + }; + vmaCreateAllocator(&allocatorInfo, &_allocator); _mainDeletionQueue.push_function([&]() { @@ -339,18 +341,19 @@ void VulkanEngine::init_swapchain() { _drawImage.imageFormat = VK_FORMAT_R16G16B16A16_SFLOAT; _drawImage.imageExtent = drawImageExtent; - VkImageUsageFlags drawImageUsages{}; - drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; - drawImageUsages |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; - drawImageUsages |= VK_IMAGE_USAGE_STORAGE_BIT; - drawImageUsages |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + VkImageUsageFlags drawImageUsages = + VK_IMAGE_USAGE_TRANSFER_SRC_BIT + | VK_IMAGE_USAGE_TRANSFER_DST_BIT + | VK_IMAGE_USAGE_STORAGE_BIT + | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; VkImageCreateInfo rimg_info = vkinit::image_create_info(_drawImage.imageFormat, drawImageUsages, drawImageExtent); - VmaAllocationCreateInfo rimg_allocinfo{}; - rimg_allocinfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - rimg_allocinfo.requiredFlags = VkMemoryPropertyFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); - + VmaAllocationCreateInfo rimg_allocinfo{ + .usage = VMA_MEMORY_USAGE_GPU_ONLY, + .requiredFlags = VkMemoryPropertyFlags(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT), + }; + //allocate and create the image vmaCreateImage(_allocator, &rimg_info, &rimg_allocinfo, &_drawImage.image, &_drawImage.allocation, nullptr); @@ -443,19 +446,19 @@ void VulkanEngine::init_descriptors() { //allocate a descriptor set for our draw image _drawImageDescriptors = globalDescriptorAllocator.allocate(_device,_drawImageDescriptorLayout); - VkDescriptorImageInfo imgInfo{}; - imgInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL; - imgInfo.imageView = _drawImage.imageView; - - VkWriteDescriptorSet drawImageWrite = {}; - drawImageWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - drawImageWrite.pNext = nullptr; - - drawImageWrite.dstBinding = 0; - drawImageWrite.dstSet = _drawImageDescriptors; - drawImageWrite.descriptorCount = 1; - drawImageWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; - drawImageWrite.pImageInfo = &imgInfo; + VkDescriptorImageInfo imgInfo{ + .imageView = _drawImage.imageView, + .imageLayout = VK_IMAGE_LAYOUT_GENERAL, + }; + + VkWriteDescriptorSet drawImageWrite = { + .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .dstSet = _drawImageDescriptors, + .dstBinding = 0, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, + .pImageInfo = &imgInfo, + }; vkUpdateDescriptorSets(_device, 1, &drawImageWrite, 0, nullptr), @@ -474,20 +477,20 @@ void VulkanEngine::init_pipelines() void VulkanEngine::init_background_pipelines() { - VkPipelineLayoutCreateInfo computeLayout{}; - computeLayout.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - computeLayout.pNext = nullptr; - computeLayout.pSetLayouts = &_drawImageDescriptorLayout; - computeLayout.setLayoutCount = 1; - - VkPushConstantRange pushConstant{}; - pushConstant.offset = 0; - pushConstant.size = sizeof(ComputePushConstants) ; - pushConstant.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT; - - computeLayout.pPushConstantRanges = &pushConstant; - computeLayout.pushConstantRangeCount = 1; - + VkPushConstantRange pushConstant{ + .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, + .offset = 0, + .size = sizeof(ComputePushConstants) , + }; + + VkPipelineLayoutCreateInfo computeLayout{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = 1, + .pSetLayouts = &_drawImageDescriptorLayout, + .pushConstantRangeCount = 1, + .pPushConstantRanges = &pushConstant, + }; + VK_CHECK(vkCreatePipelineLayout(_device, &computeLayout, nullptr, &_gradientPipelineLayout)); VkShaderModule gradientShader; @@ -497,40 +500,42 @@ void VulkanEngine::init_background_pipelines() VkShaderModule skyShader; good = vkutil::load_shader_module("sky.comp.spv", _device, &skyShader); - VkPipelineShaderStageCreateInfo stageinfo{}; - stageinfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - stageinfo.pNext = nullptr; - stageinfo.stage = VK_SHADER_STAGE_COMPUTE_BIT; - stageinfo.module = gradientShader; - stageinfo.pName = "main"; - - VkComputePipelineCreateInfo computePipelineCreateInfo{}; - computePipelineCreateInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; - computePipelineCreateInfo.pNext = nullptr; - computePipelineCreateInfo.layout = _gradientPipelineLayout; - computePipelineCreateInfo.stage = stageinfo; - - ComputeEffect gradient; - gradient.layout = _gradientPipelineLayout; - gradient.name = "gradient"; - gradient.data = {}; - gradient.data.data1 = glm::vec4(1, 0, 0, 1); - gradient.data.data2 = glm::vec4(0, 0, 1, 1); + VkPipelineShaderStageCreateInfo stageinfo{ + .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, + .stage = VK_SHADER_STAGE_COMPUTE_BIT, + .module = gradientShader, + .pName = "main", + }; + + VkComputePipelineCreateInfo computePipelineCreateInfo{ + .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, + .stage = stageinfo, + .layout = _gradientPipelineLayout, + }; + + ComputeEffect gradient{ + .name = "gradient", + .layout = _gradientPipelineLayout, + .data = { + .data1 = glm::vec4(1, 0, 0, 1), + .data2 = glm::vec4(0, 0, 1, 1), + }, + }; + VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &gradient.pipeline)); // change the shader module only to create the sky computePipelineCreateInfo.stage.module = skyShader; - ComputeEffect sky; - sky.layout = _gradientPipelineLayout; - sky.name = "sky"; - sky.data = {}; - // default sky - sky.data.data1 = glm::vec4(0.1, 0.2, 0.4, 0.97); + ComputeEffect sky{ + .name = "sky", + .layout = _gradientPipelineLayout, + .data{.data1 = glm::vec4(0.1, 0.2, 0.4, 0.97)}, + }; + VK_CHECK(vkCreateComputePipelines(_device, VK_NULL_HANDLE, 1, &computePipelineCreateInfo, nullptr, &sky.pipeline)); - backgroundEffects.push_back(gradient); backgroundEffects.push_back(sky); @@ -574,25 +579,28 @@ void VulkanEngine::init_imgui() // 1: create descriptor pool for IMGUI // the size of the pool is very oversize, but it's copied from imgui demo // itself. - VkDescriptorPoolSize pool_sizes[] = { { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, - { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, - { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 }, - { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 }, - { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 }, - { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 }, - { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 }, - { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 }, - { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 }, - { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 }, - { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 } }; + VkDescriptorPoolSize pool_sizes[] = { + { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 }, + { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 }, + { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 }, + { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 }, + { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 }, + { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 }, + { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 }, + { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 }, + { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 }, + { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 }, + { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 } + }; - VkDescriptorPoolCreateInfo pool_info{}; - pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; - pool_info.maxSets = 1000; - pool_info.poolSizeCount = (uint32_t)std::size(pool_sizes); - pool_info.pPoolSizes = pool_sizes; - + VkDescriptorPoolCreateInfo pool_info{ + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, + .maxSets = 1000, + .poolSizeCount = (uint32_t)std::size(pool_sizes), + .pPoolSizes = pool_sizes, + }; + VkDescriptorPool imguiPool; VK_CHECK(vkCreateDescriptorPool(_device, &pool_info, nullptr, &imguiPool)); @@ -600,21 +608,24 @@ void VulkanEngine::init_imgui() ImGui::CreateContext(); ImGui_ImplSDL2_InitForVulkan(_window); - ImGui_ImplVulkan_InitInfo init_info{}; - init_info.Instance = _instance; - init_info.PhysicalDevice = _chosenGPU; - init_info.Device = _device; - init_info.Queue = _graphicsQueue; - init_info.DescriptorPool = imguiPool; - init_info.MinImageCount = 3; - init_info.ImageCount = 3; - init_info.UseDynamicRendering = true; - init_info.PipelineRenderingCreateInfo = {.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO}; - init_info.PipelineRenderingCreateInfo.colorAttachmentCount = 1; - init_info.PipelineRenderingCreateInfo.pColorAttachmentFormats = &_swapchainImageFormat; + ImGui_ImplVulkan_InitInfo init_info{ + .Instance = _instance, + .PhysicalDevice = _chosenGPU, + .Device = _device, + .Queue = _graphicsQueue, + .DescriptorPool = imguiPool, + .MinImageCount = 3, + .ImageCount = 3, + .MSAASamples = VK_SAMPLE_COUNT_1_BIT, + .UseDynamicRendering = true, + .PipelineRenderingCreateInfo = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO, + .colorAttachmentCount = 1, + .pColorAttachmentFormats = &_swapchainImageFormat, + }, + }; - init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT; ImGui_ImplVulkan_Init(&init_info); ImGui_ImplVulkan_CreateFontsTexture();