From 858f7f78afddd051cb091126b60715c7a2fe04fb Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Mon, 26 Aug 2024 15:46:03 +0200 Subject: [PATCH] Refactor DepthStencilFormat OpenGL mappings into traits object --- src/oscar/CMakeLists.txt | 2 ++ src/oscar/Graphics/GraphicsImplementation.cpp | 11 ++-------- .../OpenGL/DepthStencilFormatOpenGLHelpers.h | 20 +++++++++++++++++++ .../OpenGL/DepthStencilFormatOpenGLTraits.h | 20 +++++++++++++++++++ 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLHelpers.h create mode 100644 src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLTraits.h diff --git a/src/oscar/CMakeLists.txt b/src/oscar/CMakeLists.txt index 7f0c0ec6d..df8344c03 100644 --- a/src/oscar/CMakeLists.txt +++ b/src/oscar/CMakeLists.txt @@ -104,6 +104,8 @@ add_library(oscar STATIC Graphics/OpenGL/CPUDataTypeOpenGLTraits.h Graphics/OpenGL/CPUImageFormatOpenGLTraits.h + Graphics/OpenGL/DepthStencilFormatOpenGLTraits.h + Graphics/OpenGL/DepthStencilFormatOpenGLHelpers.h Graphics/OpenGL/Gl.cpp Graphics/OpenGL/Gl.h Graphics/OpenGL/TextureFormatOpenGLTraits.h diff --git a/src/oscar/Graphics/GraphicsImplementation.cpp b/src/oscar/Graphics/GraphicsImplementation.cpp index a684dede0..c05a8792e 100644 --- a/src/oscar/Graphics/GraphicsImplementation.cpp +++ b/src/oscar/Graphics/GraphicsImplementation.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -2051,8 +2052,6 @@ std::ostream& osc::operator<<(std::ostream& o, const Texture2D&) namespace { - enum class RenderBufferType { Color, Depth, NUM_OPTIONS }; - constexpr auto c_render_texture_format_strings = std::to_array({ "Red8", "ARGB32", @@ -2083,13 +2082,7 @@ namespace constexpr GLenum to_opengl_internal_color_format_enum(const DepthRenderBufferParams& params) { - static_assert(num_options() == 2); - - switch (params.format) { - case DepthStencilFormat::D24_UNorm_S8_UInt: return GL_DEPTH24_STENCIL8; - case DepthStencilFormat::D32_SFloat: return GL_DEPTH_COMPONENT32F; - default: return GL_DEPTH24_STENCIL8; - } + return detail::to_opengl_internal_color_format_enum(params.format); } constexpr CPUImageFormat equivalent_cpu_image_format_of(const RenderTextureFormat& format) diff --git a/src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLHelpers.h b/src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLHelpers.h new file mode 100644 index 000000000..3d01ceffc --- /dev/null +++ b/src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLHelpers.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include +#include +#include + +#include + +namespace osc::detail +{ + constexpr GLenum to_opengl_internal_color_format_enum(const DepthStencilFormat& format) + { + constexpr auto lut = [](OptionList) { + return std::to_array({ DepthStencilFormatOpenGLTraits::internal_color_format... }); + }(DepthStencilFormatList{}); + + return lut.at(to_index(format)); + } +} diff --git a/src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLTraits.h b/src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLTraits.h new file mode 100644 index 000000000..01ed9a86d --- /dev/null +++ b/src/oscar/Graphics/OpenGL/DepthStencilFormatOpenGLTraits.h @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace osc::detail +{ + template + struct DepthStencilFormatOpenGLTraits; + + template<> + struct DepthStencilFormatOpenGLTraits final { + static inline constexpr GLenum internal_color_format = GL_DEPTH24_STENCIL8; + }; + + template<> + struct DepthStencilFormatOpenGLTraits final { + static inline constexpr GLenum internal_color_format = GL_DEPTH_COMPONENT32F; + }; +}