Skip to content

Commit dc4d8d2

Browse files
SaschaWillemsOverv
andauthored
Update tutorial to use dynamic state for viewport and scissor (#293)
* Prefer use of dynamic state for viewport and scissor * Slight rewording, working in-page anchor * Updated command buffers chapter and code to use dynamic viewport and scissor state * Updated swap chain recreation chapter No more need to recreate the graphics pipeline as viewport and scissor state are now dynamic * Updated command buffer chapter code to use dynamic viewport and scissor * Removed render pass destruction/creation for swap chain recreation This is not needed anymore * Use dynamic state for viewport and scissor for samples after the fixed function chapter * Use dynamic state for viewport and scissor for samples after the swap chain recreation chapter Removed render pass destruction/creation for swap chain recreation, no longer necessary * Removed duplicate code * Wording change * Changed wording on chapter intro, moved dynamic state paragraph to the top * Fixed wording * Fixed wording * C++ style cast * Wording * Wording * Wording * Added note on renderpass recreation * Apply suggestions from code review Incorporate suggestions Co-authored-by: Alexander Overvoorde <[email protected]> * Added dynamic state to conclusion chapter * Remove unnecessary link * Remove code referencing command buffers too early Co-authored-by: Alexander Overvoorde <[email protected]>
1 parent 3a9c98c commit dc4d8d2

25 files changed

+604
-436
lines changed

code/10_fixed_functions.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -379,24 +379,10 @@ class HelloTriangleApplication {
379379
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
380380
inputAssembly.primitiveRestartEnable = VK_FALSE;
381381

382-
VkViewport viewport{};
383-
viewport.x = 0.0f;
384-
viewport.y = 0.0f;
385-
viewport.width = (float) swapChainExtent.width;
386-
viewport.height = (float) swapChainExtent.height;
387-
viewport.minDepth = 0.0f;
388-
viewport.maxDepth = 1.0f;
389-
390-
VkRect2D scissor{};
391-
scissor.offset = {0, 0};
392-
scissor.extent = swapChainExtent;
393-
394382
VkPipelineViewportStateCreateInfo viewportState{};
395383
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
396384
viewportState.viewportCount = 1;
397-
viewportState.pViewports = &viewport;
398385
viewportState.scissorCount = 1;
399-
viewportState.pScissors = &scissor;
400386

401387
VkPipelineRasterizationStateCreateInfo rasterizer{};
402388
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -428,6 +414,15 @@ class HelloTriangleApplication {
428414
colorBlending.blendConstants[2] = 0.0f;
429415
colorBlending.blendConstants[3] = 0.0f;
430416

417+
std::vector<VkDynamicState> dynamicStates = {
418+
VK_DYNAMIC_STATE_VIEWPORT,
419+
VK_DYNAMIC_STATE_SCISSOR
420+
};
421+
VkPipelineDynamicStateCreateInfo dynamicState{};
422+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
423+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
424+
dynamicState.pDynamicStates = dynamicStates.data();
425+
431426
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
432427
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
433428
pipelineLayoutInfo.setLayoutCount = 0;

code/11_render_passes.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -414,24 +414,10 @@ class HelloTriangleApplication {
414414
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
415415
inputAssembly.primitiveRestartEnable = VK_FALSE;
416416

417-
VkViewport viewport{};
418-
viewport.x = 0.0f;
419-
viewport.y = 0.0f;
420-
viewport.width = (float) swapChainExtent.width;
421-
viewport.height = (float) swapChainExtent.height;
422-
viewport.minDepth = 0.0f;
423-
viewport.maxDepth = 1.0f;
424-
425-
VkRect2D scissor{};
426-
scissor.offset = {0, 0};
427-
scissor.extent = swapChainExtent;
428-
429417
VkPipelineViewportStateCreateInfo viewportState{};
430418
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
431419
viewportState.viewportCount = 1;
432-
viewportState.pViewports = &viewport;
433420
viewportState.scissorCount = 1;
434-
viewportState.pScissors = &scissor;
435421

436422
VkPipelineRasterizationStateCreateInfo rasterizer{};
437423
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -463,6 +449,15 @@ class HelloTriangleApplication {
463449
colorBlending.blendConstants[2] = 0.0f;
464450
colorBlending.blendConstants[3] = 0.0f;
465451

452+
std::vector<VkDynamicState> dynamicStates = {
453+
VK_DYNAMIC_STATE_VIEWPORT,
454+
VK_DYNAMIC_STATE_SCISSOR
455+
};
456+
VkPipelineDynamicStateCreateInfo dynamicState{};
457+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
458+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
459+
dynamicState.pDynamicStates = dynamicStates.data();
460+
466461
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
467462
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
468463
pipelineLayoutInfo.setLayoutCount = 0;

code/12_graphics_pipeline_complete.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,24 +416,10 @@ class HelloTriangleApplication {
416416
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
417417
inputAssembly.primitiveRestartEnable = VK_FALSE;
418418

419-
VkViewport viewport{};
420-
viewport.x = 0.0f;
421-
viewport.y = 0.0f;
422-
viewport.width = (float) swapChainExtent.width;
423-
viewport.height = (float) swapChainExtent.height;
424-
viewport.minDepth = 0.0f;
425-
viewport.maxDepth = 1.0f;
426-
427-
VkRect2D scissor{};
428-
scissor.offset = {0, 0};
429-
scissor.extent = swapChainExtent;
430-
431419
VkPipelineViewportStateCreateInfo viewportState{};
432420
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
433421
viewportState.viewportCount = 1;
434-
viewportState.pViewports = &viewport;
435422
viewportState.scissorCount = 1;
436-
viewportState.pScissors = &scissor;
437423

438424
VkPipelineRasterizationStateCreateInfo rasterizer{};
439425
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -465,6 +451,15 @@ class HelloTriangleApplication {
465451
colorBlending.blendConstants[2] = 0.0f;
466452
colorBlending.blendConstants[3] = 0.0f;
467453

454+
std::vector<VkDynamicState> dynamicStates = {
455+
VK_DYNAMIC_STATE_VIEWPORT,
456+
VK_DYNAMIC_STATE_SCISSOR
457+
};
458+
VkPipelineDynamicStateCreateInfo dynamicState{};
459+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
460+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
461+
dynamicState.pDynamicStates = dynamicStates.data();
462+
468463
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
469464
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
470465
pipelineLayoutInfo.setLayoutCount = 0;
@@ -484,6 +479,7 @@ class HelloTriangleApplication {
484479
pipelineInfo.pRasterizationState = &rasterizer;
485480
pipelineInfo.pMultisampleState = &multisampling;
486481
pipelineInfo.pColorBlendState = &colorBlending;
482+
pipelineInfo.pDynamicState = &dynamicState;
487483
pipelineInfo.layout = pipelineLayout;
488484
pipelineInfo.renderPass = renderPass;
489485
pipelineInfo.subpass = 0;

code/13_framebuffers.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -422,24 +422,10 @@ class HelloTriangleApplication {
422422
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
423423
inputAssembly.primitiveRestartEnable = VK_FALSE;
424424

425-
VkViewport viewport{};
426-
viewport.x = 0.0f;
427-
viewport.y = 0.0f;
428-
viewport.width = (float) swapChainExtent.width;
429-
viewport.height = (float) swapChainExtent.height;
430-
viewport.minDepth = 0.0f;
431-
viewport.maxDepth = 1.0f;
432-
433-
VkRect2D scissor{};
434-
scissor.offset = {0, 0};
435-
scissor.extent = swapChainExtent;
436-
437425
VkPipelineViewportStateCreateInfo viewportState{};
438426
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
439427
viewportState.viewportCount = 1;
440-
viewportState.pViewports = &viewport;
441428
viewportState.scissorCount = 1;
442-
viewportState.pScissors = &scissor;
443429

444430
VkPipelineRasterizationStateCreateInfo rasterizer{};
445431
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -471,6 +457,15 @@ class HelloTriangleApplication {
471457
colorBlending.blendConstants[2] = 0.0f;
472458
colorBlending.blendConstants[3] = 0.0f;
473459

460+
std::vector<VkDynamicState> dynamicStates = {
461+
VK_DYNAMIC_STATE_VIEWPORT,
462+
VK_DYNAMIC_STATE_SCISSOR
463+
};
464+
VkPipelineDynamicStateCreateInfo dynamicState{};
465+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
466+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
467+
dynamicState.pDynamicStates = dynamicStates.data();
468+
474469
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
475470
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
476471
pipelineLayoutInfo.setLayoutCount = 0;
@@ -490,6 +485,7 @@ class HelloTriangleApplication {
490485
pipelineInfo.pRasterizationState = &rasterizer;
491486
pipelineInfo.pMultisampleState = &multisampling;
492487
pipelineInfo.pColorBlendState = &colorBlending;
488+
pipelineInfo.pDynamicState = &dynamicState;
493489
pipelineInfo.layout = pipelineLayout;
494490
pipelineInfo.renderPass = renderPass;
495491
pipelineInfo.subpass = 0;

code/14_command_buffers.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -429,24 +429,10 @@ class HelloTriangleApplication {
429429
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
430430
inputAssembly.primitiveRestartEnable = VK_FALSE;
431431

432-
VkViewport viewport{};
433-
viewport.x = 0.0f;
434-
viewport.y = 0.0f;
435-
viewport.width = (float) swapChainExtent.width;
436-
viewport.height = (float) swapChainExtent.height;
437-
viewport.minDepth = 0.0f;
438-
viewport.maxDepth = 1.0f;
439-
440-
VkRect2D scissor{};
441-
scissor.offset = {0, 0};
442-
scissor.extent = swapChainExtent;
443-
444432
VkPipelineViewportStateCreateInfo viewportState{};
445433
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
446434
viewportState.viewportCount = 1;
447-
viewportState.pViewports = &viewport;
448435
viewportState.scissorCount = 1;
449-
viewportState.pScissors = &scissor;
450436

451437
VkPipelineRasterizationStateCreateInfo rasterizer{};
452438
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -478,6 +464,15 @@ class HelloTriangleApplication {
478464
colorBlending.blendConstants[2] = 0.0f;
479465
colorBlending.blendConstants[3] = 0.0f;
480466

467+
std::vector<VkDynamicState> dynamicStates = {
468+
VK_DYNAMIC_STATE_VIEWPORT,
469+
VK_DYNAMIC_STATE_SCISSOR
470+
};
471+
VkPipelineDynamicStateCreateInfo dynamicState{};
472+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
473+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
474+
dynamicState.pDynamicStates = dynamicStates.data();
475+
481476
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
482477
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
483478
pipelineLayoutInfo.setLayoutCount = 0;
@@ -497,6 +492,7 @@ class HelloTriangleApplication {
497492
pipelineInfo.pRasterizationState = &rasterizer;
498493
pipelineInfo.pMultisampleState = &multisampling;
499494
pipelineInfo.pColorBlendState = &colorBlending;
495+
pipelineInfo.pDynamicState = &dynamicState;
500496
pipelineInfo.layout = pipelineLayout;
501497
pipelineInfo.renderPass = renderPass;
502498
pipelineInfo.subpass = 0;
@@ -581,6 +577,20 @@ class HelloTriangleApplication {
581577

582578
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
583579

580+
VkViewport viewport{};
581+
viewport.x = 0.0f;
582+
viewport.y = 0.0f;
583+
viewport.width = (float) swapChainExtent.width;
584+
viewport.height = (float) swapChainExtent.height;
585+
viewport.minDepth = 0.0f;
586+
viewport.maxDepth = 1.0f;
587+
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
588+
589+
VkRect2D scissor{};
590+
scissor.offset = {0, 0};
591+
scissor.extent = swapChainExtent;
592+
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
593+
584594
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
585595

586596
vkCmdEndRenderPass(commandBuffer);

code/15_hello_triangle.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -453,24 +453,10 @@ class HelloTriangleApplication {
453453
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
454454
inputAssembly.primitiveRestartEnable = VK_FALSE;
455455

456-
VkViewport viewport{};
457-
viewport.x = 0.0f;
458-
viewport.y = 0.0f;
459-
viewport.width = (float) swapChainExtent.width;
460-
viewport.height = (float) swapChainExtent.height;
461-
viewport.minDepth = 0.0f;
462-
viewport.maxDepth = 1.0f;
463-
464-
VkRect2D scissor{};
465-
scissor.offset = {0, 0};
466-
scissor.extent = swapChainExtent;
467-
468456
VkPipelineViewportStateCreateInfo viewportState{};
469457
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
470458
viewportState.viewportCount = 1;
471-
viewportState.pViewports = &viewport;
472459
viewportState.scissorCount = 1;
473-
viewportState.pScissors = &scissor;
474460

475461
VkPipelineRasterizationStateCreateInfo rasterizer{};
476462
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -502,6 +488,15 @@ class HelloTriangleApplication {
502488
colorBlending.blendConstants[2] = 0.0f;
503489
colorBlending.blendConstants[3] = 0.0f;
504490

491+
std::vector<VkDynamicState> dynamicStates = {
492+
VK_DYNAMIC_STATE_VIEWPORT,
493+
VK_DYNAMIC_STATE_SCISSOR
494+
};
495+
VkPipelineDynamicStateCreateInfo dynamicState{};
496+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
497+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
498+
dynamicState.pDynamicStates = dynamicStates.data();
499+
505500
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
506501
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
507502
pipelineLayoutInfo.setLayoutCount = 0;
@@ -521,6 +516,7 @@ class HelloTriangleApplication {
521516
pipelineInfo.pRasterizationState = &rasterizer;
522517
pipelineInfo.pMultisampleState = &multisampling;
523518
pipelineInfo.pColorBlendState = &colorBlending;
519+
pipelineInfo.pDynamicState = &dynamicState;
524520
pipelineInfo.layout = pipelineLayout;
525521
pipelineInfo.renderPass = renderPass;
526522
pipelineInfo.subpass = 0;
@@ -605,6 +601,20 @@ class HelloTriangleApplication {
605601

606602
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
607603

604+
VkViewport viewport{};
605+
viewport.x = 0.0f;
606+
viewport.y = 0.0f;
607+
viewport.width = (float) swapChainExtent.width;
608+
viewport.height = (float) swapChainExtent.height;
609+
viewport.minDepth = 0.0f;
610+
viewport.maxDepth = 1.0f;
611+
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
612+
613+
VkRect2D scissor{};
614+
scissor.offset = {0, 0};
615+
scissor.extent = swapChainExtent;
616+
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
617+
608618
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
609619

610620
vkCmdEndRenderPass(commandBuffer);

code/16_frames_in_flight.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -456,24 +456,10 @@ class HelloTriangleApplication {
456456
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
457457
inputAssembly.primitiveRestartEnable = VK_FALSE;
458458

459-
VkViewport viewport{};
460-
viewport.x = 0.0f;
461-
viewport.y = 0.0f;
462-
viewport.width = (float) swapChainExtent.width;
463-
viewport.height = (float) swapChainExtent.height;
464-
viewport.minDepth = 0.0f;
465-
viewport.maxDepth = 1.0f;
466-
467-
VkRect2D scissor{};
468-
scissor.offset = {0, 0};
469-
scissor.extent = swapChainExtent;
470-
471459
VkPipelineViewportStateCreateInfo viewportState{};
472460
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
473461
viewportState.viewportCount = 1;
474-
viewportState.pViewports = &viewport;
475462
viewportState.scissorCount = 1;
476-
viewportState.pScissors = &scissor;
477463

478464
VkPipelineRasterizationStateCreateInfo rasterizer{};
479465
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
@@ -505,6 +491,15 @@ class HelloTriangleApplication {
505491
colorBlending.blendConstants[2] = 0.0f;
506492
colorBlending.blendConstants[3] = 0.0f;
507493

494+
std::vector<VkDynamicState> dynamicStates = {
495+
VK_DYNAMIC_STATE_VIEWPORT,
496+
VK_DYNAMIC_STATE_SCISSOR
497+
};
498+
VkPipelineDynamicStateCreateInfo dynamicState{};
499+
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
500+
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
501+
dynamicState.pDynamicStates = dynamicStates.data();
502+
508503
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
509504
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
510505
pipelineLayoutInfo.setLayoutCount = 0;
@@ -524,6 +519,7 @@ class HelloTriangleApplication {
524519
pipelineInfo.pRasterizationState = &rasterizer;
525520
pipelineInfo.pMultisampleState = &multisampling;
526521
pipelineInfo.pColorBlendState = &colorBlending;
522+
pipelineInfo.pDynamicState = &dynamicState;
527523
pipelineInfo.layout = pipelineLayout;
528524
pipelineInfo.renderPass = renderPass;
529525
pipelineInfo.subpass = 0;
@@ -610,6 +606,20 @@ class HelloTriangleApplication {
610606

611607
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);
612608

609+
VkViewport viewport{};
610+
viewport.x = 0.0f;
611+
viewport.y = 0.0f;
612+
viewport.width = (float) swapChainExtent.width;
613+
viewport.height = (float) swapChainExtent.height;
614+
viewport.minDepth = 0.0f;
615+
viewport.maxDepth = 1.0f;
616+
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
617+
618+
VkRect2D scissor{};
619+
scissor.offset = {0, 0};
620+
scissor.extent = swapChainExtent;
621+
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
622+
613623
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
614624

615625
vkCmdEndRenderPass(commandBuffer);

0 commit comments

Comments
 (0)