diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/window.cpp | 60 |
1 files changed, 24 insertions, 36 deletions
diff --git a/src/window.cpp b/src/window.cpp index 2315da9..c44a110 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -45,10 +45,8 @@ VkImageView *swapchainImageViews = nullptr; VkSampler presentSampler; VkRenderPass vkRenderPass; VkDescriptorSetLayout vkDescriptorSetLayout; -/* VkDescriptorPool presentDescriptorPool; -VkDescriptorSet *presentDescriptorSets = nullptr; -*/ +VkDescriptorSet presentDescriptorSets[MAX_FRAMES_IN_FLIGHT]; VkPipelineLayout pipelineLayout; VkPipeline graphicsPipeline; VkFramebuffer *swapchainFramebuffers = nullptr; @@ -517,12 +515,12 @@ void CreateSwapchain() { vkSwapchainCreateInfo.imageColorSpace = vkSurfaceFormatKHR.colorSpace; vkSwapchainCreateInfo.imageExtent = extent; vkSwapchainCreateInfo.imageArrayLayers = 1; - vkSwapchainCreateInfo.imageUsage = VkImageUsageFlagBits::VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - vkSwapchainCreateInfo.imageSharingMode = {}; + vkSwapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; + // vkSwapchainCreateInfo.imageSharingMode = {}; vkSwapchainCreateInfo.queueFamilyIndexCount = 0; vkSwapchainCreateInfo.pQueueFamilyIndices = nullptr; vkSwapchainCreateInfo.preTransform = surfaceCapabilities.currentTransform; - vkSwapchainCreateInfo.compositeAlpha = VkCompositeAlphaFlagBitsKHR::VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + vkSwapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; vkSwapchainCreateInfo.presentMode = vkPresentModeKHR; vkSwapchainCreateInfo.clipped = VK_TRUE; // vkSwapchainCreateInfo.oldSwapchain = vkSwapchainKHR; @@ -530,7 +528,7 @@ void CreateSwapchain() { unsigned int qfi[2] = { graphicsFamilyIndex, presentFamilyIndex }; if (graphicsFamilyIndex != presentFamilyIndex) { - vkSwapchainCreateInfo.imageSharingMode = VkSharingMode::VK_SHARING_MODE_CONCURRENT; + vkSwapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; vkSwapchainCreateInfo.queueFamilyIndexCount = 2; vkSwapchainCreateInfo.pQueueFamilyIndices = qfi; } else { @@ -757,32 +755,25 @@ void CreateGraphicsPipeline() { * PipelineLayout + DescriptorSets */ - /* VkDescriptorSetLayoutBinding imageSamplerLayoutBinding; imageSamplerLayoutBinding.binding = 0; imageSamplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; imageSamplerLayoutBinding.descriptorCount = 1; imageSamplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; imageSamplerLayoutBinding.pImmutableSamplers = nullptr; - */ VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo; descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCreateInfo.pNext = nullptr; descriptorSetLayoutCreateInfo.flags = 0; - descriptorSetLayoutCreateInfo.bindingCount = 0; - descriptorSetLayoutCreateInfo.pBindings = nullptr; - /* descriptorSetLayoutCreateInfo.bindingCount = 1; descriptorSetLayoutCreateInfo.pBindings = &imageSamplerLayoutBinding; - */ auto result = vkCreateDescriptorSetLayout(vkDevice, &descriptorSetLayoutCreateInfo, vkAllocator, &vkDescriptorSetLayout); if (result != VK_SUCCESS) { throw "failed to create descriptor set layout"; } - /* VkDescriptorPoolSize vkDescriptorPoolSize; vkDescriptorPoolSize.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; vkDescriptorPoolSize.descriptorCount = swapchainLength; @@ -790,22 +781,28 @@ void CreateGraphicsPipeline() { VkDescriptorPoolCreateInfo descriptorPoolCreateInfo; descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; descriptorPoolCreateInfo.pNext = nullptr; - descriptorPoolCreateInfo.flags = 0; - descriptorPoolCreateInfo.maxSets = 1; + descriptorPoolCreateInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; + descriptorPoolCreateInfo.maxSets = MAX_FRAMES_IN_FLIGHT; descriptorPoolCreateInfo.poolSizeCount = 1; descriptorPoolCreateInfo.pPoolSizes = &vkDescriptorPoolSize; vkCreateDescriptorPool(vkDevice, &descriptorPoolCreateInfo, vkAllocator, &presentDescriptorPool); + VkDescriptorSetLayout setLayouts[MAX_FRAMES_IN_FLIGHT]; + for (long i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { + setLayouts[i] = vkDescriptorSetLayout; + } VkDescriptorSetAllocateInfo allocInfo; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocInfo.pNext = nullptr; allocInfo.descriptorPool = presentDescriptorPool; - allocInfo.descriptorSetCount = 1; - allocInfo.pSetLayouts = &vkDescriptorSetLayout; + allocInfo.descriptorSetCount = MAX_FRAMES_IN_FLIGHT; + allocInfo.pSetLayouts = setLayouts; - vkAllocateDescriptorSets(vkDevice, &allocInfo, presentDescriptorSets); - */ + result = vkAllocateDescriptorSets(vkDevice, &allocInfo, presentDescriptorSets); + if (result != VK_SUCCESS) { + throw "failed to allocate present descriptor sets"; + } VkPipelineLayoutCreateInfo pipelineLayoutInfo; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; @@ -849,34 +846,26 @@ void CreateGraphicsPipeline() { throw "failed to create graphics pipeline."; } - /* - VkWriteDescriptorSet writeDescriptorSets[3]; - VkDescriptorImageInfo descriptorImageInfo[3]; - for (long i = 0; i < 3; ++i) { + VkWriteDescriptorSet writeDescriptorSets[MAX_FRAMES_IN_FLIGHT]; + VkDescriptorImageInfo descriptorImageInfo[MAX_FRAMES_IN_FLIGHT]; + for (long i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) { writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - // writeDescriptorSets[i].dstSet = VK_NULL_HANDLE; + writeDescriptorSets[i].pNext = nullptr; writeDescriptorSets[i].dstBinding = 0U; writeDescriptorSets[i].dstArrayElement = 0U; writeDescriptorSets[i].descriptorCount = 1; writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; - // writeDescriptorSets[i].pImageInfo = nullptr; writeDescriptorSets[i].pBufferInfo = nullptr; writeDescriptorSets[i].pTexelBufferView = nullptr; - // descriptorImageInfo[i].sampler = nullptr; - // descriptorImageInfo[i].imageView = nullptr; descriptorImageInfo[i].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - } - - for (long i = 0; i < 3; ++i) { descriptorImageInfo[i].sampler = presentSampler; - descriptorImageInfo[i].imageView = swapchainImageViews[i]; + descriptorImageInfo[i].imageView = swapchainImageViews[i]; writeDescriptorSets[i].pImageInfo = &descriptorImageInfo[i]; writeDescriptorSets[i].dstSet = presentDescriptorSets[i]; } - vkUpdateDescriptorSets(vkDevice, 2, writeDescriptorSets, 0, nullptr); - */ + vkUpdateDescriptorSets(vkDevice, MAX_FRAMES_IN_FLIGHT, writeDescriptorSets, 0, nullptr); vkDestroyShaderModule(vkDevice, fragShader, vkAllocator); vkDestroyShaderModule(vkDevice, vertShader, vkAllocator); @@ -1265,9 +1254,8 @@ void DestroyWindow() { vkDestroyCommandPool(vkDevice, transferCommandPool, vkAllocator); vkDestroyPipeline(vkDevice, graphicsPipeline, vkAllocator); vkDestroyPipelineLayout(vkDevice, pipelineLayout, vkAllocator); - /* + vkFreeDescriptorSets(vkDevice, presentDescriptorPool, MAX_FRAMES_IN_FLIGHT, presentDescriptorSets); vkDestroyDescriptorPool(vkDevice, presentDescriptorPool, vkAllocator); - */ vkDestroyDescriptorPool(vkDevice, imGuiDescriptorPool, vkAllocator); vkDestroyDescriptorSetLayout(vkDevice, vkDescriptorSetLayout, vkAllocator); vkDestroyRenderPass(vkDevice, vkRenderPass, vkAllocator); |
