Renderer: Handle more depthstencil formats (as Depth24Stencil8 may not be supported everywhere)

This commit is contained in:
Jérôme Leclercq
2021-06-02 20:16:43 +02:00
parent 9ee3a0d6be
commit 6161bbec76
17 changed files with 294 additions and 102 deletions

View File

@@ -45,6 +45,8 @@ namespace Nz
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
bool IsTextureFormatSupported(PixelFormat format, TextureUsage usage) const override;
inline void NotifyBufferDestruction(GLuint buffer) const;
inline void NotifyFramebufferDestruction(GLuint fbo) const;
inline void NotifyProgramDestruction(GLuint program) const;

View File

@@ -15,17 +15,20 @@ namespace Nz
// TODO: Fill this switch
switch (pixelFormat)
{
case PixelFormat::A8: return GLTextureFormat{ GL_R8, GL_RED, GL_UNSIGNED_BYTE, GL_ZERO, GL_ZERO, GL_ZERO, GL_RED };
case PixelFormat::BGR8: return GLTextureFormat{ GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ONE };
case PixelFormat::BGR8_SRGB: return GLTextureFormat{ GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ONE };
case PixelFormat::BGRA8: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
case PixelFormat::BGRA8_SRGB: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
case PixelFormat::Depth24Stencil8: return GLTextureFormat{ GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_RED, GL_GREEN, GL_ZERO, GL_ZERO };
case PixelFormat::RGB8: return GLTextureFormat{ GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ONE };
case PixelFormat::RGB8_SRGB: return GLTextureFormat{ GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ONE };
case PixelFormat::RGBA8: return GLTextureFormat{ GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
case PixelFormat::RGBA8_SRGB: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
case PixelFormat::RGBA32F: return GLTextureFormat{ GL_RGBA32F, GL_RGBA, GL_FLOAT, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
case PixelFormat::A8: return GLTextureFormat{ GL_R8, GL_RED, GL_UNSIGNED_BYTE, GL_ZERO, GL_ZERO, GL_ZERO, GL_RED };
case PixelFormat::BGR8: return GLTextureFormat{ GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ONE };
case PixelFormat::BGR8_SRGB: return GLTextureFormat{ GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ONE };
case PixelFormat::BGRA8: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
case PixelFormat::BGRA8_SRGB: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA };
case PixelFormat::Depth16: return GLTextureFormat{ GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, GL_RED, GL_ZERO, GL_ZERO, GL_ZERO };
case PixelFormat::Depth24Stencil8: return GLTextureFormat{ GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, GL_RED, GL_GREEN, GL_ZERO, GL_ZERO };
case PixelFormat::Depth32F: return GLTextureFormat{ GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, GL_RED, GL_ZERO, GL_ZERO, GL_ZERO };
case PixelFormat::Depth32FStencil8: return GLTextureFormat{ GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL, GL_FLOAT_32_UNSIGNED_INT_24_8_REV, GL_RED, GL_GREEN, GL_ZERO, GL_ZERO };
case PixelFormat::RGB8: return GLTextureFormat{ GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ONE };
case PixelFormat::RGB8_SRGB: return GLTextureFormat{ GL_SRGB8, GL_RGB, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ONE };
case PixelFormat::RGBA8: return GLTextureFormat{ GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
case PixelFormat::RGBA8_SRGB: return GLTextureFormat{ GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
case PixelFormat::RGBA32F: return GLTextureFormat{ GL_RGBA32F, GL_RGBA, GL_FLOAT, GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA };
default: break;
}

View File

@@ -20,6 +20,7 @@
#include <Nazara/Shader/Ast/Nodes.hpp>
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Utility/AbstractBuffer.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <memory>
#include <string>
@@ -47,6 +48,8 @@ namespace Nz
std::shared_ptr<ShaderModule> InstantiateShaderModule(ShaderStageTypeFlags shaderStages, ShaderLanguage lang, const std::filesystem::path& sourcePath, const ShaderWriter::States& states);
virtual std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) = 0;
virtual std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) = 0;
virtual bool IsTextureFormatSupported(PixelFormat format, TextureUsage usage) const = 0;
};
}

View File

@@ -15,7 +15,7 @@ namespace Nz
{
struct RenderWindowParameters
{
std::vector<PixelFormat> depthFormats = {Nz::PixelFormat::Depth24Stencil8, Nz::PixelFormat::Depth32, Nz::PixelFormat::Depth24}; //< By order of preference
std::vector<PixelFormat> depthFormats = {Nz::PixelFormat::Depth24Stencil8, Nz::PixelFormat::Depth32FStencil8, Nz::PixelFormat::Depth16Stencil8, Nz::PixelFormat::Depth32F, Nz::PixelFormat::Depth24}; //< By order of preference
bool verticalSync = false;
};
}

View File

@@ -245,9 +245,11 @@ namespace Nz
RGBA32I, // 4*int32
RGBA32UI, // 4*uint32
Depth16,
Depth16Stencil8,
Depth24,
Depth24Stencil8,
Depth32,
Depth32F,
Depth32FStencil8,
Stencil1,
Stencil4,
Stencil8,

View File

@@ -14,12 +14,15 @@ namespace Nz
{
switch (format)
{
case VK_FORMAT_B8G8R8A8_UNORM: return PixelFormat::BGRA8;
case VK_FORMAT_B8G8R8A8_SRGB: return PixelFormat::BGRA8_SRGB;
case VK_FORMAT_D24_UNORM_S8_UINT: return PixelFormat::Depth24Stencil8;
case VK_FORMAT_D32_SFLOAT: return PixelFormat::Depth32;
case VK_FORMAT_R8G8B8A8_UNORM: return PixelFormat::RGBA8;
case VK_FORMAT_R8G8B8A8_SRGB: return PixelFormat::RGBA8_SRGB;
case VK_FORMAT_B8G8R8A8_UNORM: return PixelFormat::BGRA8;
case VK_FORMAT_B8G8R8A8_SRGB: return PixelFormat::BGRA8_SRGB;
case VK_FORMAT_D16_UNORM: return PixelFormat::Depth16;
case VK_FORMAT_D16_UNORM_S8_UINT: return PixelFormat::Depth16Stencil8;
case VK_FORMAT_D24_UNORM_S8_UINT: return PixelFormat::Depth24Stencil8;
case VK_FORMAT_D32_SFLOAT: return PixelFormat::Depth32F;
case VK_FORMAT_D32_SFLOAT_S8_UINT: return PixelFormat::Depth32FStencil8;
case VK_FORMAT_R8G8B8A8_UNORM: return PixelFormat::RGBA8;
case VK_FORMAT_R8G8B8A8_SRGB: return PixelFormat::RGBA8_SRGB;
default: break;
}
@@ -248,8 +251,11 @@ namespace Nz
{
case PixelFormat::BGRA8: return VK_FORMAT_B8G8R8A8_UNORM;
case PixelFormat::BGRA8_SRGB: return VK_FORMAT_B8G8R8A8_SRGB;
case PixelFormat::Depth16: return VK_FORMAT_D16_UNORM;
case PixelFormat::Depth16Stencil8: return VK_FORMAT_D16_UNORM_S8_UINT;
case PixelFormat::Depth24Stencil8: return VK_FORMAT_D24_UNORM_S8_UINT;
case PixelFormat::Depth32: return VK_FORMAT_D32_SFLOAT;
case PixelFormat::Depth32F: return VK_FORMAT_D32_SFLOAT;
case PixelFormat::Depth32FStencil8: return VK_FORMAT_D32_SFLOAT_S8_UINT;
case PixelFormat::RGBA8: return VK_FORMAT_R8G8B8A8_UNORM;
case PixelFormat::RGBA8_SRGB: return VK_FORMAT_R8G8B8A8_SRGB;
case PixelFormat::RGBA32F: return VK_FORMAT_R32G32B32A32_SFLOAT;

View File

@@ -36,6 +36,8 @@ namespace Nz
std::shared_ptr<Texture> InstantiateTexture(const TextureInfo& params) override;
std::shared_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
bool IsTextureFormatSupported(PixelFormat format, TextureUsage usage) const override;
VulkanDevice& operator=(const VulkanDevice&) = delete;
VulkanDevice& operator=(VulkanDevice&&) = delete; ///TODO?

View File

@@ -59,20 +59,20 @@ namespace Nz
inline bool Create(const std::string& appName, UInt32 appVersion, const std::string& engineName, UInt32 engineVersion, UInt32 apiVersion, const std::vector<const char*>& layers, const std::vector<const char*>& extensions, const VkAllocationCallbacks* allocator = nullptr);
inline void Destroy();
bool EnumeratePhysicalDevices(std::vector<VkPhysicalDevice>* physicalDevices);
bool EnumeratePhysicalDevices(std::vector<VkPhysicalDevice>* physicalDevices) const;
inline PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name);
inline PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* name) const;
inline UInt32 GetApiVersion() const;
inline VkResult GetLastErrorCode() const;
bool GetPhysicalDeviceExtensions(VkPhysicalDevice device, std::vector<VkExtensionProperties>* extensionProperties);
inline VkPhysicalDeviceFeatures GetPhysicalDeviceFeatures(VkPhysicalDevice device);
inline VkFormatProperties GetPhysicalDeviceFormatProperties(VkPhysicalDevice device, VkFormat format);
inline bool GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* imageFormatProperties);
inline VkPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties(VkPhysicalDevice device);
inline VkPhysicalDeviceProperties GetPhysicalDeviceProperties(VkPhysicalDevice device);
bool GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice device, std::vector<VkQueueFamilyProperties>* queueFamilyProperties);
bool GetPhysicalDeviceExtensions(VkPhysicalDevice device, std::vector<VkExtensionProperties>* extensionProperties) const;
inline VkPhysicalDeviceFeatures GetPhysicalDeviceFeatures(VkPhysicalDevice device) const;
inline VkFormatProperties GetPhysicalDeviceFormatProperties(VkPhysicalDevice device, VkFormat format) const;
inline bool GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* imageFormatProperties) const;
inline VkPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties(VkPhysicalDevice device) const;
inline VkPhysicalDeviceProperties GetPhysicalDeviceProperties(VkPhysicalDevice device) const;
bool GetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice device, std::vector<VkQueueFamilyProperties>* queueFamilyProperties) const;
void InstallDebugMessageCallback();
@@ -95,7 +95,7 @@ namespace Nz
void DestroyInstance();
void ResetPointers();
inline PFN_vkVoidFunction GetProcAddr(const char* name);
inline PFN_vkVoidFunction GetProcAddr(const char* name) const;
struct InternalData;
@@ -104,7 +104,7 @@ namespace Nz
std::unordered_set<std::string> m_loadedLayers;
VkAllocationCallbacks m_allocator;
VkInstance m_instance;
VkResult m_lastErrorCode;
mutable VkResult m_lastErrorCode;
UInt32 m_apiVersion;
};
}

View File

@@ -49,7 +49,7 @@ namespace Nz
}
}
inline PFN_vkVoidFunction Instance::GetDeviceProcAddr(VkDevice device, const char* name)
inline PFN_vkVoidFunction Instance::GetDeviceProcAddr(VkDevice device, const char* name) const
{
PFN_vkVoidFunction func = vkGetDeviceProcAddr(device, name);
if (!func)
@@ -88,7 +88,7 @@ namespace Nz
return m_instance;
}
inline VkPhysicalDeviceFeatures Instance::GetPhysicalDeviceFeatures(VkPhysicalDevice device)
inline VkPhysicalDeviceFeatures Instance::GetPhysicalDeviceFeatures(VkPhysicalDevice device) const
{
VkPhysicalDeviceFeatures features;
vkGetPhysicalDeviceFeatures(device, &features);
@@ -96,7 +96,7 @@ namespace Nz
return features;
}
inline VkFormatProperties Instance::GetPhysicalDeviceFormatProperties(VkPhysicalDevice device, VkFormat format)
inline VkFormatProperties Instance::GetPhysicalDeviceFormatProperties(VkPhysicalDevice device, VkFormat format) const
{
VkFormatProperties formatProperties;
vkGetPhysicalDeviceFormatProperties(device, format, &formatProperties);
@@ -104,7 +104,7 @@ namespace Nz
return formatProperties;
}
inline bool Instance::GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* imageFormatProperties)
inline bool Instance::GetPhysicalDeviceImageFormatProperties(VkPhysicalDevice physicalDevice, VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties* imageFormatProperties) const
{
m_lastErrorCode = vkGetPhysicalDeviceImageFormatProperties(physicalDevice, format, type, tiling, usage, flags, imageFormatProperties);
if (m_lastErrorCode != VkResult::VK_SUCCESS)
@@ -116,7 +116,7 @@ namespace Nz
return true;
}
inline VkPhysicalDeviceMemoryProperties Instance::GetPhysicalDeviceMemoryProperties(VkPhysicalDevice device)
inline VkPhysicalDeviceMemoryProperties Instance::GetPhysicalDeviceMemoryProperties(VkPhysicalDevice device) const
{
VkPhysicalDeviceMemoryProperties memoryProperties;
vkGetPhysicalDeviceMemoryProperties(device, &memoryProperties);
@@ -124,7 +124,7 @@ namespace Nz
return memoryProperties;
}
inline VkPhysicalDeviceProperties Instance::GetPhysicalDeviceProperties(VkPhysicalDevice device)
inline VkPhysicalDeviceProperties Instance::GetPhysicalDeviceProperties(VkPhysicalDevice device) const
{
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(device, &properties);
@@ -132,7 +132,7 @@ namespace Nz
return properties;
}
inline PFN_vkVoidFunction Instance::GetProcAddr(const char* name)
inline PFN_vkVoidFunction Instance::GetProcAddr(const char* name) const
{
PFN_vkVoidFunction func = Loader::GetInstanceProcAddr(m_instance, name);
if (!func)