今天继续再看example代码,结果发现在复制顶点着色数据的时候就使用了vkBeginCommandBuffer,然后在draw函数也使用了。我之前理解的vkBeginCommandBuffer只能使用一次?所以我有些内容理解的不对。
再理解下帧缓冲区和图像缓冲区。
需要present到surface的图像,是从image缓冲区来的,但是image缓冲区是在swapchain中的。help spec中描述swapchain如下
“The swap chain is essentially a queue of images
that are waiting to be presented to the screen. Our application will acquire
such an image to draw to it, and then return it to the queue. ”
说明是一个图像队列,然后设置swapchain就是定义了图像的显示方式等。平时使用的话,先获取一个图像,画完后再返回到swapchain等待显示。
这里没有说到framebuffer和swapchain有什么关系。继续看help spec中描述framebuffer“The attachments specifed during render pass creation are bound by wrapping them into a VkFramebuffer object. A framebuffer object references all of the VkImageView objects that represent the attachments.”
说明image缓冲区依赖着色附件,而着色附件依赖framebuffer。比如顶点着色附件,就依赖与顶点数据+render pass信息,这样就可以生成image。关系图如下,所以一开始要将vertex等数据写入设备,使用vkBeginCommandBuffer开始,最后等管道都设置完成后,使用绑定(vkCmdBindVertexBuffers和vkCmdBindIndexBuffer),才开始绘制(vkCmdDrawIndexed)。
问题
- VkCommandBufferAllocateInfo中使用了level为VK_COMMAND_BUFFER_LEVEL_SECONDARY,网上搜索下了。次命令缓冲区必须不能直接被提交到队列。相反,需要被记录到主命令缓冲区的一部分来被执行。但是我看了代码,其实vkBeginCommandBuffer前它又设置为了VK_COMMAND_BUFFER_LEVEL_PRIMARY。但是SECONDARY有什么用呢?
help spec中“We won’t make use of the secondary command buffer functionality here, but you can imagine that it’s helpful to reuse common operations from primary command buffers.”
说明command buffer可以重复使用而已。不必重建,但是我还没有想到应用场景。