From 7dca5d23addc8a7eb1f95f29928bdda2a0fccac5 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Fri, 6 Jan 2023 23:07:50 +0000 Subject: [PATCH] Adds preferred depth stencil format to renderer backend. This was SVN commit r27379. --- source/renderer/ShadowMap.cpp | 10 ++++++++-- source/renderer/WaterManager.cpp | 12 +++++++++--- source/renderer/backend/IDevice.h | 7 +++++++ source/renderer/backend/dummy/Device.cpp | 6 ++++++ source/renderer/backend/dummy/Device.h | 3 +++ source/renderer/backend/gl/Device.cpp | 14 ++++++++++++++ source/renderer/backend/gl/Device.h | 3 +++ source/renderer/backend/vulkan/Device.cpp | 9 +++++++++ source/renderer/backend/vulkan/Device.h | 3 +++ 9 files changed, 62 insertions(+), 5 deletions(-) diff --git a/source/renderer/ShadowMap.cpp b/source/renderer/ShadowMap.cpp index b0a980d6d1..2f8ac57ef1 100644 --- a/source/renderer/ShadowMap.cpp +++ b/source/renderer/ShadowMap.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -518,7 +518,13 @@ void ShadowMapInternals::CreateTexture() case 16: formatName = "Format::D16"; backendFormat = Renderer::Backend::Format::D16; break; case 24: formatName = "Format::D24"; backendFormat = Renderer::Backend::Format::D24; break; case 32: formatName = "Format::D32"; backendFormat = Renderer::Backend::Format::D32; break; - default: formatName = "Format::D24"; backendFormat = Renderer::Backend::Format::D24; break; + default: + formatName = "Default"; + backendFormat = backendDevice->GetPreferredDepthStencilFormat( + Renderer::Backend::ITexture::Usage::SAMPLED | + Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT, + true, false); + break; } #endif ENSURE(formatName); diff --git a/source/renderer/WaterManager.cpp b/source/renderer/WaterManager.cpp index f39f10ff75..23dd2ed822 100644 --- a/source/renderer/WaterManager.cpp +++ b/source/renderer/WaterManager.cpp @@ -235,6 +235,12 @@ void WaterManager::RecreateOrLoadTexturesIfNeeded() m_RefTextureSize = newRefTextureSize; } + const Renderer::Backend::Format depthFormat = + backendDevice->GetPreferredDepthStencilFormat( + Renderer::Backend::ITexture::Usage::SAMPLED | + Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT, + true, false); + // Create reflection textures. const bool needsReflectionTextures = g_RenderingOptions.GetWaterEffects() && @@ -252,7 +258,7 @@ void WaterManager::RecreateOrLoadTexturesIfNeeded() m_ReflFboDepthTexture = backendDevice->CreateTexture2D("WaterReflectionDepthTexture", Renderer::Backend::ITexture::Usage::SAMPLED | Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT, - Renderer::Backend::Format::D24, m_RefTextureSize, m_RefTextureSize, + depthFormat, m_RefTextureSize, m_RefTextureSize, Renderer::Backend::Sampler::MakeDefaultSampler( Renderer::Backend::Sampler::Filter::NEAREST, Renderer::Backend::Sampler::AddressMode::REPEAT)); @@ -294,7 +300,7 @@ void WaterManager::RecreateOrLoadTexturesIfNeeded() m_RefrFboDepthTexture = backendDevice->CreateTexture2D("WaterRefractionDepthTexture", Renderer::Backend::ITexture::Usage::SAMPLED | Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT, - Renderer::Backend::Format::D24, m_RefTextureSize, m_RefTextureSize, + depthFormat, m_RefTextureSize, m_RefTextureSize, Renderer::Backend::Sampler::MakeDefaultSampler( Renderer::Backend::Sampler::Filter::NEAREST, Renderer::Backend::Sampler::AddressMode::REPEAT)); @@ -345,7 +351,7 @@ void WaterManager::RecreateOrLoadTexturesIfNeeded() m_FancyTextureDepth = backendDevice->CreateTexture2D("WaterFancyDepthTexture", Renderer::Backend::ITexture::Usage::DEPTH_STENCIL_ATTACHMENT, - Renderer::Backend::Format::D24, g_Renderer.GetWidth(), g_Renderer.GetHeight(), + depthFormat, g_Renderer.GetWidth(), g_Renderer.GetHeight(), Renderer::Backend::Sampler::MakeDefaultSampler( Renderer::Backend::Sampler::Filter::LINEAR, Renderer::Backend::Sampler::AddressMode::REPEAT)); diff --git a/source/renderer/backend/IDevice.h b/source/renderer/backend/IDevice.h index add6b86f41..e72aee42f1 100644 --- a/source/renderer/backend/IDevice.h +++ b/source/renderer/backend/IDevice.h @@ -163,6 +163,13 @@ public: virtual bool IsFramebufferFormatSupported(const Format format) const = 0; + /** + * Returns the most suitable format for the usage. Returns + * Format::UNDEFINED if there is no such format. + */ + virtual Format GetPreferredDepthStencilFormat( + const uint32_t usage, const bool depth, const bool stencil) const = 0; + virtual const Capabilities& GetCapabilities() const = 0; }; diff --git a/source/renderer/backend/dummy/Device.cpp b/source/renderer/backend/dummy/Device.cpp index 5163b58cf4..7985702f3e 100644 --- a/source/renderer/backend/dummy/Device.cpp +++ b/source/renderer/backend/dummy/Device.cpp @@ -153,6 +153,12 @@ bool CDevice::IsFramebufferFormatSupported(const Format UNUSED(format)) const return true; } +Format CDevice::GetPreferredDepthStencilFormat( + const uint32_t, const bool, const bool) const +{ + return Format::D24_S8; +} + std::unique_ptr CreateDevice(SDL_Window* UNUSED(window)) { return std::make_unique(); diff --git a/source/renderer/backend/dummy/Device.h b/source/renderer/backend/dummy/Device.h index d5657b0eaa..4cc0e71333 100644 --- a/source/renderer/backend/dummy/Device.h +++ b/source/renderer/backend/dummy/Device.h @@ -95,6 +95,9 @@ public: bool IsFramebufferFormatSupported(const Format format) const override; + Format GetPreferredDepthStencilFormat( + const uint32_t usage, const bool depth, const bool stencil) const override; + const Capabilities& GetCapabilities() const override { return m_Capabilities; } protected: diff --git a/source/renderer/backend/gl/Device.cpp b/source/renderer/backend/gl/Device.cpp index 8e42726feb..f521c2714e 100644 --- a/source/renderer/backend/gl/Device.cpp +++ b/source/renderer/backend/gl/Device.cpp @@ -1055,6 +1055,20 @@ bool CDevice::IsFramebufferFormatSupported(const Format format) const return supported; } +Format CDevice::GetPreferredDepthStencilFormat( + const uint32_t UNUSED(usage), const bool depth, const bool stencil) const +{ + ENSURE(depth || stencil); + if (stencil) +#if CONFIG2_GLES + return Format::UNDEFINED; +#else + return Format::D24_S8; +#endif + else + return Format::D24; +} + std::unique_ptr CreateDevice(SDL_Window* window, const bool arb) { return GL::CDevice::Create(window, arb); diff --git a/source/renderer/backend/gl/Device.h b/source/renderer/backend/gl/Device.h index c248498363..37996677eb 100644 --- a/source/renderer/backend/gl/Device.h +++ b/source/renderer/backend/gl/Device.h @@ -114,6 +114,9 @@ public: bool IsFramebufferFormatSupported(const Format format) const override; + Format GetPreferredDepthStencilFormat( + const uint32_t usage, const bool depth, const bool stencil) const override; + const Capabilities& GetCapabilities() const override { return m_Capabilities; } private: diff --git a/source/renderer/backend/vulkan/Device.cpp b/source/renderer/backend/vulkan/Device.cpp index 00e29dd1d7..99a745f2c4 100644 --- a/source/renderer/backend/vulkan/Device.cpp +++ b/source/renderer/backend/vulkan/Device.cpp @@ -188,6 +188,15 @@ bool CDevice::IsFramebufferFormatSupported(const Format format) const return false; } +Format CDevice::GetPreferredDepthStencilFormat( + const uint32_t usage, const bool depth, const bool stencil) const +{ + UNUSED2(usage); + UNUSED2(depth); + UNUSED2(stencil); + return Format::UNDEFINED; +} + std::unique_ptr CreateDevice(SDL_Window* window) { return Vulkan::CDevice::Create(window); diff --git a/source/renderer/backend/vulkan/Device.h b/source/renderer/backend/vulkan/Device.h index 2a67bf34e5..efbe76ac2f 100644 --- a/source/renderer/backend/vulkan/Device.h +++ b/source/renderer/backend/vulkan/Device.h @@ -98,6 +98,9 @@ public: bool IsFramebufferFormatSupported(const Format format) const override; + Format GetPreferredDepthStencilFormat( + const uint32_t usage, const bool depth, const bool stencil) const override; + const Capabilities& GetCapabilities() const override { return m_Capabilities; } private: