From 992f4e1984eaac9dc45e43bd31c10b48993d66f2 Mon Sep 17 00:00:00 2001 From: cdozdil Date: Sat, 14 Nov 2026 01:58:59 +0300 Subject: [PATCH] Cache the Vulkan resource copy's storage format query --- OptiScaler/shaders/resource_copy/RC_Vk.cpp | 13 +++++++++---- OptiScaler/shaders/resource_copy/RC_Vk.h | 6 +++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/OptiScaler/shaders/resource_copy/RC_Vk.cpp b/OptiScaler/shaders/resource_copy/RC_Vk.cpp index 22350e43..5b2f08aa 100644 --- a/OptiScaler/shaders/resource_copy/RC_Vk.cpp +++ b/OptiScaler/shaders/resource_copy/RC_Vk.cpp @@ -65,20 +65,25 @@ ResourceCopy_Vk::ResourceCopy_Vk(std::string InName, VkDevice InDevice, VkPhysic std::ranges::none_of(_descriptorSets, [](VkDescriptorSet set) { return set == VK_NULL_HANDLE; }); } -VkFormat ResourceCopy_Vk::BufferFormat(VkFormat InFormat, bool InDepth) const +VkFormat ResourceCopy_Vk::BufferFormat(VkFormat InFormat, bool InDepth) { // Depth formats can't be storage images, the shader reads the depth aspect as float if (InDepth) return VK_FORMAT_R32_SFLOAT; + if (InFormat == _querySource) + return _queryResult; + // sRGB, BGRA and others without storage support, float keeps the precision of what the shader reads VkFormatProperties properties {}; vkGetPhysicalDeviceFormatProperties(_physicalDevice, InFormat, &properties); - if (!(properties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT)) - return VK_FORMAT_R16G16B16A16_SFLOAT; + _querySource = InFormat; + _queryResult = (properties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT) + ? InFormat + : VK_FORMAT_R16G16B16A16_SFLOAT; - return InFormat; + return _queryResult; } bool ResourceCopy_Vk::IsInteger(VkFormat InFormat) diff --git a/OptiScaler/shaders/resource_copy/RC_Vk.h b/OptiScaler/shaders/resource_copy/RC_Vk.h index 1bb7582b..0466b16a 100644 --- a/OptiScaler/shaders/resource_copy/RC_Vk.h +++ b/OptiScaler/shaders/resource_copy/RC_Vk.h @@ -8,6 +8,10 @@ class ResourceCopy_Vk : public Shader_Vk uint32_t InNumThreadsX = 16; uint32_t InNumThreadsY = 16; + // Last BufferFormat query, the source format rarely changes + VkFormat _querySource = VK_FORMAT_MAX_ENUM; + VkFormat _queryResult = VK_FORMAT_UNDEFINED; + public: ResourceCopy_Vk(std::string InName, VkDevice InDevice, VkPhysicalDevice InPhysicalDevice); ~ResourceCopy_Vk() = default; @@ -17,7 +21,7 @@ class ResourceCopy_Vk : public Shader_Vk VkExtent2D OutExtent, VkOffset2D InSrc = {}, VkOffset2D InDst = {}); // Storage image format for a copy of InFormat, depth is copied to R32_SFLOAT - VkFormat BufferFormat(VkFormat InFormat, bool InDepth) const; + VkFormat BufferFormat(VkFormat InFormat, bool InDepth); // Integer formats can't be read as float static bool IsInteger(VkFormat InFormat);