Clean up depth format selection code

Abierto
#224 0 comentarios 1 reacción 1 asignado Ver en GitHub

@SaschaWillems ya está trabajando en esto.

Desde el 12/11/2025.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

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<vk::Format> &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.

Lenguaje dominante
C++
Estrellas
424
Forks
127
Merge medio
2 d 22 h
PR fusionados (30 d)
7

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de KhronosGroup/Vulkan-Tutorial

Todos los issues de KhronosGroup/Vulkan-Tutorial

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.