Overv / VulkanTutorial

Tutorial for the Vulkan graphics and compute API
https://vulkan-tutorial.com
Creative Commons Attribution Share Alike 4.0 International
3.06k stars 511 forks source link

Proposal: std::clamp instead of std::min/std::max #240

Closed lunasorcery closed 3 years ago

lunasorcery commented 3 years ago

In the "Swap extent" section of https://vulkan-tutorial.com/en/Drawing_a_triangle/Presentation/Swap_chain

actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));

The max and min functions are used here to clamp the value of WIDTH and HEIGHT between the allowed minimum and maximum extents that are supported by the implementation.

Would it not be more idiomatic to use std::clamp in this case? We've already established we're using C++17, so availability of the function shouldn't be an issue.

Proposed code change:

actualExtent.width = std::clamp(actualExtent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
actualExtent.height = std::clamp(actualExtent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);

Proposed wording change:

The clamp function is used here to clamp the value of WIDTH and HEIGHT between the allowed minimum and maximum extents that are supported by the implementation.