From d998aaad12511751a70bbc05d410104d274b104d Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Wed, 16 Oct 2019 18:18:13 +0200 Subject: [PATCH] [dxvk] Add some component mapping helpers --- src/dxvk/dxvk_util.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++ src/dxvk/dxvk_util.h | 16 +++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/dxvk/dxvk_util.cpp b/src/dxvk/dxvk_util.cpp index bde6df40..6fc47e29 100644 --- a/src/dxvk/dxvk_util.cpp +++ b/src/dxvk/dxvk_util.cpp @@ -140,6 +140,59 @@ namespace dxvk::util { } + static VkComponentMapping normalizeComponentMapping( + VkComponentMapping mapping) { + mapping.r = mapping.r == VK_COMPONENT_SWIZZLE_IDENTITY ? VK_COMPONENT_SWIZZLE_R : mapping.r; + mapping.g = mapping.g == VK_COMPONENT_SWIZZLE_IDENTITY ? VK_COMPONENT_SWIZZLE_G : mapping.g; + mapping.b = mapping.b == VK_COMPONENT_SWIZZLE_IDENTITY ? VK_COMPONENT_SWIZZLE_B : mapping.b; + mapping.a = mapping.a == VK_COMPONENT_SWIZZLE_IDENTITY ? VK_COMPONENT_SWIZZLE_A : mapping.a; + return mapping; + } + + + static VkComponentSwizzle resolveComponentSwizzle( + VkComponentSwizzle swizzle, + VkComponentMapping dstMapping, + VkComponentMapping srcMapping) { + VkComponentSwizzle dstSwizzle = VK_COMPONENT_SWIZZLE_IDENTITY; + if (dstMapping.r == swizzle) dstSwizzle = VK_COMPONENT_SWIZZLE_R; + if (dstMapping.g == swizzle) dstSwizzle = VK_COMPONENT_SWIZZLE_G; + if (dstMapping.b == swizzle) dstSwizzle = VK_COMPONENT_SWIZZLE_B; + if (dstMapping.a == swizzle) dstSwizzle = VK_COMPONENT_SWIZZLE_A; + + switch (dstSwizzle) { + case VK_COMPONENT_SWIZZLE_R: return srcMapping.r; + case VK_COMPONENT_SWIZZLE_G: return srcMapping.g; + case VK_COMPONENT_SWIZZLE_B: return srcMapping.b; + case VK_COMPONENT_SWIZZLE_A: return srcMapping.a; + default: return VK_COMPONENT_SWIZZLE_IDENTITY; + } + } + + + VkComponentMapping resolveSrcComponentMapping( + VkComponentMapping dstMapping, + VkComponentMapping srcMapping) { + dstMapping = normalizeComponentMapping(dstMapping); + + VkComponentMapping result; + result.r = resolveComponentSwizzle(VK_COMPONENT_SWIZZLE_R, dstMapping, srcMapping); + result.g = resolveComponentSwizzle(VK_COMPONENT_SWIZZLE_G, dstMapping, srcMapping); + result.b = resolveComponentSwizzle(VK_COMPONENT_SWIZZLE_B, dstMapping, srcMapping); + result.a = resolveComponentSwizzle(VK_COMPONENT_SWIZZLE_A, dstMapping, srcMapping); + return result; + } + + + bool isIdentityMapping( + VkComponentMapping mapping) { + return (mapping.r == VK_COMPONENT_SWIZZLE_R || mapping.r == VK_COMPONENT_SWIZZLE_IDENTITY) + && (mapping.g == VK_COMPONENT_SWIZZLE_G || mapping.g == VK_COMPONENT_SWIZZLE_IDENTITY) + && (mapping.b == VK_COMPONENT_SWIZZLE_B || mapping.b == VK_COMPONENT_SWIZZLE_IDENTITY) + && (mapping.a == VK_COMPONENT_SWIZZLE_A || mapping.a == VK_COMPONENT_SWIZZLE_IDENTITY); + } + + uint32_t getComponentIndex( VkComponentSwizzle component, uint32_t identity) { diff --git a/src/dxvk/dxvk_util.h b/src/dxvk/dxvk_util.h index 86d8b79c..06715570 100644 --- a/src/dxvk/dxvk_util.h +++ b/src/dxvk/dxvk_util.h @@ -225,7 +225,23 @@ namespace dxvk::util { */ VkComponentMapping invertComponentMapping( VkComponentMapping mapping); + + /** + * \brief Resolves source component mapping + * + * Returns the source component mapping after rearranging + * the destination mapping to be the identity mapping. + * \param [in] dstMapping Destination mapping + * \param [in] srcMapping Source mapping + * \returns Adjusted src component mapping + */ + VkComponentMapping resolveSrcComponentMapping( + VkComponentMapping dstMapping, + VkComponentMapping srcMapping); + bool isIdentityMapping( + VkComponentMapping mapping); + /** * \brief Computes component index for a component swizzle *