From a87cb6c94df5542c3ee632f334050ee2796de3fa Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Fri, 25 Feb 2022 20:02:03 +0000 Subject: [PATCH] Moves GL depth comparison function to SamplerDesc. This was SVN commit r26485. --- source/renderer/PostprocManager.cpp | 4 -- source/renderer/ShadowMap.cpp | 19 +++----- source/renderer/backend/CompareOp.cpp | 47 +++++++++++++++++++ source/renderer/backend/CompareOp.h | 57 +++++++++++++++++++++++ source/renderer/backend/PipelineState.cpp | 17 ------- source/renderer/backend/PipelineState.h | 23 +-------- source/renderer/backend/Sampler.cpp | 1 + source/renderer/backend/Sampler.h | 5 +- source/renderer/backend/gl/Texture.cpp | 18 +++++++ 9 files changed, 134 insertions(+), 57 deletions(-) create mode 100644 source/renderer/backend/CompareOp.cpp create mode 100644 source/renderer/backend/CompareOp.h diff --git a/source/renderer/PostprocManager.cpp b/source/renderer/PostprocManager.cpp index 22c851de42..c3f901dac0 100644 --- a/source/renderer/PostprocManager.cpp +++ b/source/renderer/PostprocManager.cpp @@ -161,10 +161,6 @@ void CPostprocManager::RecreateBuffers() Renderer::Backend::Sampler::Filter::LINEAR, Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE)); - g_Renderer.GetDeviceCommandContext()->BindTexture(0, GL_TEXTURE_2D, m_DepthTex->GetHandle()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); - g_Renderer.GetDeviceCommandContext()->BindTexture(0, GL_TEXTURE_2D, 0); - // Set up the framebuffers with some initial textures. m_CaptureFramebuffer = backendDevice->CreateFramebuffer("PostprocCaptureFramebuffer", m_ColorTex1.get(), m_DepthTex.get(), diff --git a/source/renderer/ShadowMap.cpp b/source/renderer/ShadowMap.cpp index cf2fc0c308..b1fd5a119a 100644 --- a/source/renderer/ShadowMap.cpp +++ b/source/renderer/ShadowMap.cpp @@ -537,8 +537,7 @@ void ShadowMapInternals::CreateTexture() Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE)); } - Texture = backendDevice->CreateTexture2D("ShadowMapDepth", - backendFormat, Width, Height, + Renderer::Backend::Sampler::Desc samplerDesc = Renderer::Backend::Sampler::MakeDefaultSampler( #if CONFIG2_GLES // GLES doesn't do depth comparisons, so treat it as a @@ -548,19 +547,13 @@ void ShadowMapInternals::CreateTexture() // Use GL_LINEAR to trigger automatic PCF on some devices Renderer::Backend::Sampler::Filter::LINEAR, #endif - Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE)); - - -#if !CONFIG2_GLES - g_Renderer.GetDeviceCommandContext()->BindTexture(0, GL_TEXTURE_2D, Texture->GetHandle()); + Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE); // Enable automatic depth comparisons - glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); - g_Renderer.GetDeviceCommandContext()->BindTexture(0, GL_TEXTURE_2D, 0); -#endif + samplerDesc.compareEnabled = true; + samplerDesc.compareOp = Renderer::Backend::CompareOp::LESS_OR_EQUAL; - ogl_WarnIfError(); + Texture = backendDevice->CreateTexture2D("ShadowMapDepth", + backendFormat, Width, Height, samplerDesc); Framebuffer = backendDevice->CreateFramebuffer("ShadowMapFramebuffer", g_RenderingOptions.GetShadowAlphaFix() ? DummyTexture.get() : nullptr, Texture.get()); diff --git a/source/renderer/backend/CompareOp.cpp b/source/renderer/backend/CompareOp.cpp new file mode 100644 index 0000000000..d56084c637 --- /dev/null +++ b/source/renderer/backend/CompareOp.cpp @@ -0,0 +1,47 @@ +/* Copyright (C) 2022 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "CompareOp.h" + +namespace Renderer +{ + +namespace Backend +{ + +CompareOp ParseCompareOp(const CStr& str) +{ + // TODO: it might make sense to use upper case in XML for consistency. +#define CASE(NAME, VALUE) if (str == NAME) return CompareOp::VALUE + CASE("never", NEVER); + CASE("less", LESS); + CASE("equal", EQUAL); + CASE("lequal", LESS_OR_EQUAL); + CASE("greater", GREATER); + CASE("notequal", NOT_EQUAL); + CASE("gequal", GREATER_OR_EQUAL); + CASE("always", ALWAYS); +#undef CASE + debug_warn("Invalid compare op"); + return CompareOp::NEVER; +} + +} // namespace Backend + +} // namespace Renderer diff --git a/source/renderer/backend/CompareOp.h b/source/renderer/backend/CompareOp.h new file mode 100644 index 0000000000..9522e3c315 --- /dev/null +++ b/source/renderer/backend/CompareOp.h @@ -0,0 +1,57 @@ +/* Copyright (C) 2022 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#ifndef INCLUDED_RENDERER_BACKEND_COMPAREOP +#define INCLUDED_RENDERER_BACKEND_COMPAREOP + +#include "graphics/Color.h" + +class CStr; + +namespace Renderer +{ + +namespace Backend +{ + +enum class CompareOp +{ + // Never passes the comparison. + NEVER, + // Passes if the source value is less than the destination value. + LESS, + // Passes if the source depth value is equal to the destination value. + EQUAL, + // Passes if the source depth value is less than or equal to the destination value. + LESS_OR_EQUAL, + // Passes if the source depth value is greater than the destination value. + GREATER, + // Passes if the source depth value is not equal to the destination value. + NOT_EQUAL, + // Passes if the source depth value is greater than or equal to the destination value. + GREATER_OR_EQUAL, + // Always passes the comparison. + ALWAYS +}; + +CompareOp ParseCompareOp(const CStr& str); + +} // namespace Backend + +} // namespace Renderer + +#endif // INCLUDED_RENDERER_BACKEND_COMPAREOP diff --git a/source/renderer/backend/PipelineState.cpp b/source/renderer/backend/PipelineState.cpp index 0c5d3b6368..3a2606b1f1 100644 --- a/source/renderer/backend/PipelineState.cpp +++ b/source/renderer/backend/PipelineState.cpp @@ -59,23 +59,6 @@ GraphicsPipelineStateDesc MakeDefaultGraphicsPipelineStateDesc() return desc; } -CompareOp ParseCompareOp(const CStr& str) -{ - // TODO: it might make sense to use upper case in XML for consistency. -#define CASE(NAME, VALUE) if (str == NAME) return CompareOp::VALUE - CASE("never", NEVER); - CASE("less", LESS); - CASE("equal", EQUAL); - CASE("lequal", LESS_OR_EQUAL); - CASE("greater", GREATER); - CASE("notequal", NOT_EQUAL); - CASE("gequal", GREATER_OR_EQUAL); - CASE("always", ALWAYS); -#undef CASE - debug_warn("Invalid compare op"); - return CompareOp::NEVER; -} - StencilOp ParseStencilOp(const CStr& str) { #define CASE(NAME) if (str == #NAME) return StencilOp::NAME diff --git a/source/renderer/backend/PipelineState.h b/source/renderer/backend/PipelineState.h index 1eba8deadd..c5de53bd40 100644 --- a/source/renderer/backend/PipelineState.h +++ b/source/renderer/backend/PipelineState.h @@ -19,6 +19,7 @@ #define INCLUDED_RENDERER_BACKEND_PIPELINESTATE #include "graphics/Color.h" +#include "renderer/backend/CompareOp.h" class CStr; @@ -28,26 +29,6 @@ namespace Renderer namespace Backend { -enum class CompareOp -{ - // Never passes the comparison. - NEVER, - // Passes if the source value is less than the destination value. - LESS, - // Passes if the source depth value is equal to the destination value. - EQUAL, - // Passes if the source depth value is less than or equal to the destination value. - LESS_OR_EQUAL, - // Passes if the source depth value is greater than the destination value. - GREATER, - // Passes if the source depth value is not equal to the destination value. - NOT_EQUAL, - // Passes if the source depth value is greater than or equal to the destination value. - GREATER_OR_EQUAL, - // Always passes the comparison. - ALWAYS -}; - enum class StencilOp { // Keeps the current value. @@ -180,8 +161,6 @@ struct GraphicsPipelineStateDesc // should be described with a related shader and should be switched together. GraphicsPipelineStateDesc MakeDefaultGraphicsPipelineStateDesc(); -CompareOp ParseCompareOp(const CStr& str); - StencilOp ParseStencilOp(const CStr& str); BlendFactor ParseBlendFactor(const CStr& str); diff --git a/source/renderer/backend/Sampler.cpp b/source/renderer/backend/Sampler.cpp index c4a3fecc2c..db650b7a58 100644 --- a/source/renderer/backend/Sampler.cpp +++ b/source/renderer/backend/Sampler.cpp @@ -40,6 +40,7 @@ Desc MakeDefaultSampler(Filter filter, AddressMode addressMode) desc.anisotropyEnabled = false; desc.mipLODBias = 0.0f; desc.borderColor = CColor(0.0f, 0.0f, 0.0f, 0.0f); + desc.compareEnabled = false; return desc; } diff --git a/source/renderer/backend/Sampler.h b/source/renderer/backend/Sampler.h index fc18987e28..e34dad9b73 100644 --- a/source/renderer/backend/Sampler.h +++ b/source/renderer/backend/Sampler.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,6 +19,7 @@ #define INCLUDED_RENDERER_BACKEND_SAMPLER #include "graphics/Color.h" +#include "renderer/backend/CompareOp.h" #include @@ -58,6 +59,8 @@ struct Desc float maxAnisotropy; // When some filter is CLAMP_TO_BORDER. CColor borderColor; + bool compareEnabled; + CompareOp compareOp; }; Desc MakeDefaultSampler(Filter filter, AddressMode addressMode); diff --git a/source/renderer/backend/gl/Texture.cpp b/source/renderer/backend/gl/Texture.cpp index 71a75f397d..df6ee49eed 100644 --- a/source/renderer/backend/gl/Texture.cpp +++ b/source/renderer/backend/gl/Texture.cpp @@ -23,6 +23,7 @@ #include "lib/config2.h" #include "renderer/backend/gl/Device.h" #include "renderer/backend/gl/DeviceCommandContext.h" +#include "renderer/backend/gl/Mapping.h" #include @@ -257,6 +258,23 @@ std::unique_ptr CTexture::Create(CDevice* device, const char* name, } } + +#if !CONFIG2_GLES + if (format == Format::D16 || format == Format::D24 || format == Format::D32 || + format == Format::D24_S8) + { + if (defaultSamplerDesc.compareEnabled) + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); + glTexParameteri( + GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, + Mapping::FromCompareOp(defaultSamplerDesc.compareOp)); + } + else + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); + } +#endif + ogl_WarnIfError(); if (texture->m_Device->GetCapabilities().debugLabels)