Random crashes on vkCmdBeginRenderPass

Hey everyone!

I’ve been following vulkan-tutorials.com very closely but have been recently running into a strange problem.

Basically, vulkan crashes randomly on the first occurance of vkCmdBeginRenderPass. And I really mean randomly - 20% crash rate on start as far as I can tell.
Now, all the code that happens before that crash is pretty much the same as in https://vulkan-tutorial.com/code/depth_buffering.cpp with the only difference being, that I have 2 more subpasses, which I add as follows:


	std::vector<VkSubpassDescription> subPasses;
	std::vector<VkSubpassDependency> dependencies;
	{
		VkSubpassDescription subPass = {};
		subPass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
		subPass.colorAttachmentCount = 1;
		subPass.pColorAttachments = &colorAttachmentRef;
		subPass.pDepthStencilAttachment = &depthAttachmentRef;

		VkSubpassDependency dependency = {};
		dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
		dependency.dstSubpass = 0;
		dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
		dependency.srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
		dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
		dependency.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
		dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
			VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;

		subPasses.push_back(subPass);
		dependencies.push_back(dependency);

		dependency.srcSubpass = 0;
		dependency.dstSubpass = 1;
		dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
		dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
		dependency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
		dependency.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
		dependency.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;

		dependencies.push_back(dependency);
	}
	{
		VkSubpassDescription subPass = {};
		subPass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
		subPass.colorAttachmentCount = 1;
		subPass.pColorAttachments = &colorAttachmentRef;
		subPass.pDepthStencilAttachment = &depthAttachmentRef;

		VkSubpassDependency dependency = {};
		dependency.srcSubpass = 1;
		dependency.dstSubpass = 2;
		dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
		dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
		dependency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
		dependency.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
		dependency.dstAccessMask = VK_ACCESS_SHADER_READ_BIT |
			VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
			VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT;

		subPasses.push_back(subPass);
		dependencies.push_back(dependency);
	}
	{
		VkSubpassDescription subPass = {};
		subPass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
		subPass.colorAttachmentCount = 1;
		subPass.pColorAttachments = &colorAttachmentRef;
		subPass.pDepthStencilAttachment = &depthAttachmentRef;

		subPasses.push_back(subPass);
	}

	std::array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
	VkRenderPassCreateInfo renderPassInfo = {};
	renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
	renderPassInfo.attachmentCount = attachments.size();
	renderPassInfo.pAttachments = attachments.data();
	renderPassInfo.subpassCount = subPasses.size();
	renderPassInfo.pSubpasses = subPasses.data();
	renderPassInfo.dependencyCount = dependencies.size();
	renderPassInfo.pDependencies = dependencies.data();

	if (vkCreateRenderPass(device, &renderPassInfo, nullptr, renderPass.replace()) != VK_SUCCESS)
	{
		//failed to create render pass, etc..
	}

The 2nd and 3rd Subpass Pipelines are the same as the first, just copy pasted with the correct VkGraphicsPipelineCreateInfo::subpass Number.

The reason why I’m using VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT (and the others) as the mask is, that I later want to add code for a second Attachment to draw on.

But it just sometimes crashes vkCmdBeginRenderPass and it’s driving me insane. Did I mess up the dependencies too bad? All the other code is the same as on vulkan-tutorial.com.

The error I’m getting:

Unhandled exception at 0x00007FFEB56AB9C6 (VkLayer_core_validation.dll) in Test.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Does anyone have an idea what might be going wrong?

You are making the driver read nullptr somewhere or possibly found layer bug. Try without the layers for a bit if it works as intended.

I am starting to hate that tutorial web. If you mean ALL_COMMANDS use ALL_COMMANDS not BOTTOM_OF_PIPE. Where’s your output dependency (src=2 dst=EXTERNAL).

Thanks for the replies, they have pointed me in the right direction. I’ve been able to make progress by simply adding 0 dependencies. Now, I get a lot of “A dependency between subpasses x and y must exist but one is not specified”, which is to be expected.

BUT! It doesn’t crash anymore :slight_smile: So, progress in my book.

I’m thus relatively sure that my dependencies are messed up.

Does anyone know what the most general dependency possible is? In other words: “Everything in subpass 1 must finish before moving to subpass 2”? I want to start from there and move on.

Layers shouldn’t crash without getting at least one message out. You have 0 Validation Layer error messages I assume?
One way to pinpoint this would be to link against debug version of the layers (but it is annoying that they need VS 2013 debug runtime libraries). That would show exactly what makes it crash…

Try enabling VK_LAYER_LUNARG_api_dump and post output for both crash and non-crash case (for comparison). PS: have it before the standard_validation in the enableLayers array.

Most restrictive (also thus inefficient) barrier-level dependency is
srcStage = dstStage = ALL_COMMANDS,
srcAccess = dstAccess = MEMORY_READ | MEMORY_WRITE,
no VK_DEPENDENCY_BY_REGION_BIT,
possibly always using LAYOUT_GENERAL (with the exception of PRESENT which must be used) and SHARING_MODE_CONCURRENT

otherwise vkDeviceIdle().

Thanks! I’ve used the most general barriers and everything works as expected. No more errors, no crashes.

I’ll try refining it once I get a better understanding for all the dependencies.