6

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?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Javin Yang
  • 215
  • 3
  • 11

1 Answers1

14

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:

  1. Fill in VkGraphicsPipelineCreateInfo::pDynamicState with VK_DYNAMIC_STATE_VIEWPORT and VK_DYNAMIC_STATE_SCISSOR in VkPipelineDynamicStateCreateInfo::pDynamicStates.

  2. After binding a pipeline with dynamic viewport and scissor state, call vkCmdSetViewport and vkCmdSetScissor 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.

Jesse Hall
  • 6,441
  • 23
  • 29