动态管线状态
dynamic state
称为动态管线状态。
提示
更详细内容请阅读Vulkan Spec的内容。
概述
创建一个图形 VkPipeline
对象时,设置 state 的流程为:
// Using viewport state as an example
VkViewport viewport = {0.0, 0.0, 32.0, 32.0, 0.0, 1.0};
// Set value of state
VkPipelineViewportStateCreateInfo viewportStateCreateInfo;
viewportStateCreateInfo.pViewports = &viewport;
viewportStateCreateInfo.viewportCount = 1;
// Create the pipeline with the state value set
VkGraphicsPipelineCreateInfo pipelineCreateInfo;
pipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
vkCreateGraphicsPipelines(pipelineCreateInfo, &pipeline);
vkBeginCommandBuffer();
// Select the pipeline and draw with the state's static value
vkCmdBindPipeline(pipeline);
vkCmdDraw();
vkEndCommandBuffer();
VkPipeline
使用 dynamic state 的场景是,一些信息可以在创建管线时省略,然后在记录命令缓冲区的时候设置,流程为:
// Using viewport state as an example
VkViewport viewport = {0.0, 0.0, 32.0, 32.0, 0.0, 1.0};
VkDynamicState dynamicState = VK_DYNAMIC_STATE_VIEWPORT;
// not used now
VkPipelineViewportStateCreateInfo viewportStateCreateInfo;
viewportStateCreateInfo.pViewports = nullptr;
// still need to say how many viewports will be used here
viewportStateCreateInfo.viewportCount = 1;
// Set the state as being dynamic
VkPipelineDynamicStateCreateInfo dynamicStateCreateInfo;
dynamicStateCreateInfo.dynamicStateCount = 1;
dynamicStateCreateInfo.pDynamicStates = &dynamicState;
// Create the pipeline with state value not known
VkGraphicsPipelineCreateInfo pipelineCreateInfo;
pipelineCreateInfo.pViewportState = &viewportStateCreateInfo;
pipelineCreateInfo.pDynamicState = &dynamicStateCreateInfo;
vkCreateGraphicsPipelines(pipelineCreateInfo, &pipeline);
vkBeginCommandBuffer();
vkCmdBindPipeline(pipeline);
// Set the state for the pipeline at recording time
vkCmdSetViewport(viewport);
vkCmdDraw();
viewport.height = 64.0;
// set a new state value between draws
vkCmdSetViewport(viewport);
vkCmdDraw();
vkEndCommandBuffer();
什么时候使用dynamic state
提示
Vulkan 是一种工具,与大多数事物一样,这个问题没有绝对的答案。
某些驱动实现使用VkDynamicState
相比静态状态可能有性能损失,但使用动态状态,可以让应用程序创建更少的管线,对于会变化的管线状态,应用程序可能更多的会使用dynamic state
。
哪些状态是动态的
动态状态的完整列表可在 Vulkan Spec VkDynamicState 中找到。
添加VK_EXT_extended_dynamic_state
、VK_EXT_extended_dynamic_state2
、VK_EXT_extended_dynamic_state3
、 VK_EXT_vertex_input_dynamic_state
、VK_EXT_attachment_feedback_loop_dynamic_state
和VK_EXT_color_write_enable
扩展的目的,是让应用程序减少编译和绑定管线状态的数量。