Secondary command buffer and how to use them

I’m using a secondary command buffer in my program and I’m trying to understand what is the proper whey to use them.

This is a simple program that render different meshes. I have one primary command buffer and one renderpass with one subpass that uses a graphic pipeline.

I start the primary command buffer with :

vkBeginCommandBuffer

then i start the renderpass with :

vkCmdBeginRenderPass.

then i recode the secondary command buffer to the primary command buffer (i record to the secondary buffer before using vkCmdExecuteCommands )using :

vkCmdExecuteCommands

Now in the secondary cb i start with :

vkBeginCommandBuffer(secondary_cb,&beginInfo)

I saw on docs.vulkan.org (and from the 520706687 - Validation Error) that in a secondary cb i need to fill in

beginInfo.pInheritanceInfo

which is a VkCommandBufferInheritanceInfo

struct VkCommandBufferInheritanceInfo {
VkStructureType sType;
const void* pNext;
VkRenderPass renderPass;
uint32_t subpass;
VkFramebuffer framebuffer;
VkBool32 occlusionQueryEnable;
VkQueryControlFlags queryFlags;
VkQueryPipelineStatisticFlags pipelineStatistics;
}

My first question is: what do i fill in this struct for my secondary cb.
My second question is about the 3 fields in the VkCommandBufferInheritanceInfo :

VkRenderPass renderPass
uint32_t subpass
VkFramebuffer framebuffer

I guess the renderPass needs to be the renderpass from the primary command buffer or any other renderpass that is close enough to that renderpass so the secondary cb will be compatible with the renderpass from the primary cb.

I have no idea what does the “uint32_t subpass” means. in the specification(Link)
it says:
" subpass is the index of the subpass within the render pass instance that the VkCommandBuffer will be executed within."

clearly i need to fill in 0 because i have only one subpass and 0 is the index of that subpass.
but what does it means by “will be executed within”. how can a secondary cb can be called from a sub pass. As I see it, when i call a command like vkCmdDrawIndexed in the secondary cb, it is the secondary cb that calls all of the sub passes in the renderpass and not the other way around. What don’t i understand here?

And in the “VkFramebuffer framebuffer” I need to fill in the framebuffer i want to render to. but i already filled the command buffer i want to render to in the render pass from the primary cb. can i have here a different framebuffer?

I am sorry it is a bit of a long question but an answer for this can help me a lot.

thank you.

The main purpose of a secondary command buffer is to allow you to record CBs in different threads. So you can have one thread write rendering commands for all static meshes, then another for skinned meshes, then another for shadows. All of these results are aggregated together into one thread that builds the primary CB and submits it.

By calling vkCmdExecuteCommands while the primary CB you’re executing within is in the middle of a subpass. Which subpass you’re currently on is determined by the primary command buffer; you cannot begin a renderpass or change subpasses in a secondary command buffer.

If you provide one, it needs to be equivalent to the primary CB’s framebuffer. But you don’t have to provide one; it’s just a performance hint.