KhronosGroup / KhronosGroup/Vulkan-Tutorial

Clean up depth format selection code

Open
#224 0 comments 1 reaction 1 assignee Claimed by @SaschaWillems View on GitHub
Dominant language
C++
Stars
418
Forks
126
Avg merge
11d 6h
Merged PRs (30d)
31

Description

All samples that do depth buffering have code like this:

```
void createDepthResources()
{
vk::Format depthFormat = findDepthFormat();

createImage(swapChainExtent.width, swapChainExtent.height, depthFormat, vk::ImageTiling::eOptimal, vk::ImageUsageFlagBits::eDepthStencilAttachment, vk::MemoryPropertyFlagBits::eDeviceLocal, depthImage, depthImageMemory);
depthImageView = createImageView(depthImage, depthFormat, vk::ImageAspectFlagBits::eDepth);
}

vk::Format findSupportedFormat(const std::vector &candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features) const
{
for (const auto format : candidates)
{
vk::FormatProperties props = physicalDevice.getFormatProperties(format);

if (tiling == vk::ImageTiling::eLinear && (props.linearTilingFeatures & features) == features)
{
return format;
}
if (tiling == vk::ImageTiling::eOptimal && (props.optimalTilingFeatures & features) == features)
{
return format;
}
}

throw std::runtime_error("failed to find supported format!");
}

[[nodiscard]] vk::Format findDepthFormat() const
{
return findSupportedFormat(
{vk::Format::eD32Sfloat, vk::Format::eD32SfloatS8Uint, vk::Format::eD24UnormS8Uint},
vk::ImageTiling::eOptimal,
vk::FormatFeatureFlagBits::eDepthStencilAttachment);
}

....
// Somewhere in a function A
vk::Format depthFormat = findDepthFormat();
....
// Somewhere in a function B
vk::Format depthFormat = findDepthFormat();
```

* `findDepthFormat` is often called multiple times of just querying once and then storing it.
* `findSupportedFormat` is only ever used in `findDepthFormat`
* `findDepthFormat` is declared after the calling function(s) `createDepthResources`

There is potential here to simplify code and make it easier to follow.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.