From cab5a5378dd57618f1c979f7fe6eb515687c6509 Mon Sep 17 00:00:00 2001 From: Thomas Choquet Date: Mon, 23 Feb 2026 21:54:05 +0900 Subject: [PATCH] add getters for the swapchain width and height --- CMakeLists.txt | 4 ---- include/Graphics/Swapchain.hpp | 3 +++ src/Metal/MetalSwapchain.hpp | 5 +++++ src/Metal/MetalSwapchain.mm | 4 +++- src/Vulkan/VulkanSwapchain.cpp | 11 +++++------ src/Vulkan/VulkanSwapchain.hpp | 4 ++++ 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7f56d1b..04cbfec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,10 +31,6 @@ if(GFX_BUILD_EXAMPLES) set(GFX_EXAMPLES_TO_BUILD "triangle;multiBuffer;imgui_usage;mc_cube;scop" CACHE STRING "Semicolon separated list of example names to build") endif() -if (GFX_USE_UTILSCPP OR GFX_USE_MATH) - message(FATAL_ERROR "Not yet implemented") -endif() - enable_language(CXX) if(APPLE AND GFX_BUILD_METAL) diff --git a/include/Graphics/Swapchain.hpp b/include/Graphics/Swapchain.hpp index 09d261b..df142ef 100644 --- a/include/Graphics/Swapchain.hpp +++ b/include/Graphics/Swapchain.hpp @@ -37,6 +37,9 @@ class Swapchain Swapchain(const Swapchain&) = delete; Swapchain(Swapchain&&) = delete; + virtual uint32_t width() const = 0; + virtual uint32_t height() const = 0; + virtual std::shared_ptr nextDrawable() = 0; virtual ~Swapchain() = default; diff --git a/src/Metal/MetalSwapchain.hpp b/src/Metal/MetalSwapchain.hpp index 096ee56..68000b6 100644 --- a/src/Metal/MetalSwapchain.hpp +++ b/src/Metal/MetalSwapchain.hpp @@ -30,11 +30,16 @@ class MetalSwapchain : public Swapchain MetalSwapchain(const MetalDevice&, const Swapchain::Descriptor&); + inline uint32_t width() const override { return m_width; } + inline uint32_t height() const override { return m_height; } + std::shared_ptr nextDrawable() override; ~MetalSwapchain() override = default; private: + uint32_t m_width; + uint32_t m_height; CAMetalLayer* m_mtlLayer; public: diff --git a/src/Metal/MetalSwapchain.mm b/src/Metal/MetalSwapchain.mm index bbf7edc..953c87e 100644 --- a/src/Metal/MetalSwapchain.mm +++ b/src/Metal/MetalSwapchain.mm @@ -20,7 +20,9 @@ namespace gfx { -MetalSwapchain::MetalSwapchain(const MetalDevice& device, const Swapchain::Descriptor& desc) { @autoreleasepool +MetalSwapchain::MetalSwapchain(const MetalDevice& device, const Swapchain::Descriptor& desc) + : m_width(desc.width) + , m_height(desc.height) { @autoreleasepool { assert(desc.surface); auto* mtlSurface = dynamic_cast(desc.surface); diff --git a/src/Vulkan/VulkanSwapchain.cpp b/src/Vulkan/VulkanSwapchain.cpp index 3be1f13..ac6cf59 100644 --- a/src/Vulkan/VulkanSwapchain.cpp +++ b/src/Vulkan/VulkanSwapchain.cpp @@ -39,13 +39,12 @@ VulkanSwapchain::VulkanSwapchain(const VulkanDevice* device, const Descriptor& d std::vector surfacePresentModes = vkPhysicalDevice.getSurfacePresentModesKHR(vkSurface); assert(std::ranges::any_of(surfacePresentModes, [&desc](auto& m){return m == toVkPresentModeKHR(desc.presentMode);})); - vk::Extent2D extent; if (surfaceCapabilities.currentExtent.width != std::numeric_limits::max()) - extent = surfaceCapabilities.currentExtent; + m_extent = surfaceCapabilities.currentExtent; else { - extent.width = std::clamp(desc.width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width); - extent.height = std::clamp(desc.height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height); + m_extent.width = std::clamp(desc.width, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width); + m_extent.height = std::clamp(desc.height, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height); } @@ -54,7 +53,7 @@ VulkanSwapchain::VulkanSwapchain(const VulkanDevice* device, const Descriptor& d .setMinImageCount(desc.imageCount) .setImageFormat(toVkFormat(desc.pixelFormat)) .setImageColorSpace(toVkColorSpaceKHR(desc.pixelFormat)) - .setImageExtent(extent) + .setImageExtent(m_extent) .setImageArrayLayers(1) .setImageUsage(vk::ImageUsageFlagBits::eColorAttachment) .setPreTransform(surfaceCapabilities.currentTransform) @@ -77,7 +76,7 @@ VulkanSwapchain::VulkanSwapchain(const VulkanDevice* device, const Descriptor& d s_oldSwapchains[&vkSurface] = *m_vkSwapchain; Texture::Descriptor swapchainImageTexDesc = { - .width = extent.width, .height = extent.height, + .width = m_extent.width, .height = m_extent.height, .pixelFormat = desc.pixelFormat, .usages = TextureUsage::colorAttachment, .storageMode = ResourceStorageMode::deviceLocal diff --git a/src/Vulkan/VulkanSwapchain.hpp b/src/Vulkan/VulkanSwapchain.hpp index c88292c..4ba18df 100644 --- a/src/Vulkan/VulkanSwapchain.hpp +++ b/src/Vulkan/VulkanSwapchain.hpp @@ -31,12 +31,16 @@ class VulkanSwapchain : public Swapchain VulkanSwapchain(const VulkanDevice*, const Swapchain::Descriptor&); + inline uint32_t width() const override { return m_extent.width; } + inline uint32_t height() const override { return m_extent.height; } + std::shared_ptr nextDrawable() override; ~VulkanSwapchain() override = default; private: const VulkanDevice* m_device; + vk::Extent2D m_extent; vk::SwapchainKHR* m_vkSwapchain; std::vector> m_swapchainImages;