问题
As shown at https://vulkan-tutorial.com/code/23_texture_image.cpp:
Calling createGraphicsPipeline()
to recreate the Pipeline when changing the window size, the dimensions are set in the code below.
VkViewport viewport = {};
viewport.x = 0.0f;
viewport.y = 0.0f;
viewport.width = (float) swapChainExtent.width;
viewport.height = (float) swapChainExtent.height;
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor = {};
scissor.offset = {0, 0};
scissor.extent = swapChainExtent;
VkPipelineViewportStateCreateInfo viewportState = {};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.pViewports = &viewport;
viewportState.scissorCount = 1;
viewportState.pScissors = &scissor;
If the scene contains lots of materials, changing the window size will recreate all materials, which is very time consuming. Can I modify their VkRect2D
and VkViewport
directly without recreate the pipeline?
回答1:
You'd have to make the scissor and viewport dynamic state in all the pipelines where they will depend on the window size. To do this:
Fill in
VkGraphicsPipelineCreateInfo::pDynamicState
withVK_DYNAMIC_STATE_VIEWPORT
andVK_DYNAMIC_STATE_SCISSOR
inVkPipelineDynamicStateCreateInfo::pDynamicStates
.After binding a pipeline with dynamic viewport and scissor state, call
vkCmdSetViewport
andvkCmdSetScissor
to set the current viewport and scissor. You don't have to call these again after every binding each pipeline, only for the first dynamic-state pipeline in a command buffer, and after a dynamic-state pipeline if the previous pipeline didn't use dynamic state.
来源:https://stackoverflow.com/questions/57950008/must-a-vulkan-pipeline-be-recreate-when-changing-the-size-of-the-window