Cleaned up the struct inits in the engine too.

This commit is contained in:
Zed A. Shaw 2025-12-07 23:56:47 -05:00
parent d1d57ab682
commit 4f7ab6db68

View file

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