vkQueuePresentKHR() throws a ImageLayout validation error

Obvious noob disclaimer, been learning vulkan for two days now. The above function throws the following validation layer error:

VALIDATION LAYER : Images passed to present must be in layout VK_IMAGE_LAYOUT_PRESENT_SRC_KHR or VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR but is in VK_IMAGE_LAYOUT_UNDEFINED. The Vulkan spec states: Each element of pImageIndices must be the index of a presentable image acquired from the swapchain specified by the corresponding element of the pSwapchains array, and the presented image subresource must be in the VK_IMAGE_LAYOUT_PRESENT_SRC_KHR layout at the time the operation is executed on a VkDevice (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-VkPresentInfoKHR-pImageIndices-01296)

My render pass code is :

VkAttachmentDescription colorAttachmentDescription = {};
colorAttachmentDescription.format = this->surfaceFormat.format;
colorAttachmentDescription.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachmentDescription.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachmentDescription.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachmentDescription.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachmentDescription.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachmentDescription.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachmentDescription.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;


VkAttachmentReference attachmentReference = {};
attachmentReference.attachment = 0;
attachmentReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;


VkSubpassDescription subpassDescription = {};
subpassDescription.colorAttachmentCount = 1;
subpassDescription.pColorAttachments = &attachmentReference;
subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;


VkSubpassDependency subpassDependency = {};
subpassDependency.srcSubpass = VK_SUBPASS_EXTERNAL;
subpassDependency.dstSubpass = 0;
subpassDependency.srcAccessMask = 0;
subpassDependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;


VkRenderPassCreateInfo renderPassCreateInfo = {};
renderPassCreateInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassCreateInfo.attachmentCount = 1;
renderPassCreateInfo.pAttachments = &colorAttachmentDescription;
renderPassCreateInfo.subpassCount = 1;
renderPassCreateInfo.pSubpasses = &subpassDescription;
renderPassCreateInfo.dependencyCount = 1;
renderPassCreateInfo.pDependencies = &subpassDependency;


vkCreateRenderPass(logicalDevice, &renderPassCreateInfo, nullptr, &renderPass);

Is there a reason why you posted the question twice? Also, where is your code that calls vkQueuePresent, and what are the dependencies involved therein?

Yea, do not crosspost.

And the answer is at https://stackoverflow.com/questions/60360052/vkqueuepresentkhr-throws-a-validation-error

Oh, sorry. Here’s the code:

vkWaitForFences(logicalDevice, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);

uint32_t imageIndex;

VkResult result = vkAcquireNextImageKHR(logicalDevice, swapChain, UINT64_MAX, imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
if (result != VK_SUCCESS)
{
    std::cout << "could not get image";
}

std::cout <<"\n\nAcquire result : "<<result<<"\nIMAGE INDEX : " << imageIndex << "\n";

if (imagesInFlightFences[imageIndex] != VK_NULL_HANDLE)
{
    vkWaitForFences(logicalDevice, 1, &imagesInFlightFences[imageIndex], VK_TRUE, UINT64_MAX);
}

imagesInFlightFences[imageIndex] = inFlightFences[currentFrame];

VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount - 1;
submitInfo.pCommandBuffers = &commandBuffers[imageIndex];

VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;

VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };

submitInfo.pWaitDstStageMask = waitStages;

VkSemaphore signalSemaphores[] = { renderingCompletedSemaphores[currentFrame] };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;

vkResetFences(logicalDevice, 1, &inFlightFences[currentFrame]);

if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS)
{
    throw std::runtime_error("could not submit to queue");
}

VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;

presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;

VkSwapchainKHR swapChains[] = { swapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;

presentInfo.pImageIndices = &imageIndex;
std::cout << "im her \n";
result = vkQueuePresentKHR(presentationQueue, &presentInfo);
std::cout << result<<"im herr \n";

this->currentFrame = (this->currentFrame + 1) % REQUIRED_MAX_FRAMES_IN_FLIGHT;