From eabede8cdf8aac026656349a709d86e202afa16e Mon Sep 17 00:00:00 2001 From: Bill Hollings Date: Sun, 12 Feb 2023 20:14:18 +0000 Subject: [PATCH] Replace static inline with constexpr where possible. --- .../Commands/MVKCommandEncodingPool.mm | 2 +- MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h | 2 +- MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm | 2 +- MoltenVK/MoltenVK/Utility/MVKFoundation.h | 47 +++++++++---------- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/MoltenVK/MoltenVK/Commands/MVKCommandEncodingPool.mm b/MoltenVK/MoltenVK/Commands/MVKCommandEncodingPool.mm index 11e07cec..d861422b 100644 --- a/MoltenVK/MoltenVK/Commands/MVKCommandEncodingPool.mm +++ b/MoltenVK/MoltenVK/Commands/MVKCommandEncodingPool.mm @@ -109,7 +109,7 @@ id MVKCommandEncodingPool::getCmdFillBufferMTLComputePi MVK_ENC_REZ_ACCESS(_mtlFillBufferComputePipelineState, newCmdFillBufferMTLComputePipelineState(_commandPool)); } -static inline uint32_t getRenderpassLoadStoreStateIndex(MVKFormatType type) { +constexpr uint32_t getRenderpassLoadStoreStateIndex(MVKFormatType type) { switch (type) { case kMVKFormatColorHalf: case kMVKFormatColorFloat: diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h b/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h index 85ec750a..414e33a5 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h @@ -314,7 +314,7 @@ uint32_t mvkGetAttachments(const VkRenderingInfo* pRenderingInfo, VkClearValue clearValues[]); /** Returns whether the view mask uses multiview. */ -static inline bool mvkIsMultiview(uint32_t viewMask) { return viewMask != 0; } +constexpr bool mvkIsMultiview(uint32_t viewMask) { return viewMask != 0; } /** Returns whether the attachment is being used. */ bool mvkIsColorAttachmentUsed(const VkPipelineRenderingCreateInfo* pRendInfo, uint32_t colorAttIdx); diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm b/MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm index 792c81fa..af87fd60 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKSwapchain.mm @@ -176,7 +176,7 @@ struct CIE1931XY { // According to D.3.28: // "[x and y] specify the normalized x and y chromaticity coordinates, respectively... // in normalized increments of 0.00002." -static inline uint16_t FloatToCIE1931Unorm(float x) { return OSSwapHostToBigInt16((uint16_t)(x * 100000 / 2)); } +constexpr uint16_t FloatToCIE1931Unorm(float x) { return OSSwapHostToBigInt16((uint16_t)(x * 100000 / 2)); } static inline CIE1931XY VkXYColorEXTToCIE1931XY(VkXYColorEXT xy) { return { FloatToCIE1931Unorm(xy.x), FloatToCIE1931Unorm(xy.y) }; } diff --git a/MoltenVK/MoltenVK/Utility/MVKFoundation.h b/MoltenVK/MoltenVK/Utility/MVKFoundation.h index d03b42f9..cbaaf685 100644 --- a/MoltenVK/MoltenVK/Utility/MVKFoundation.h +++ b/MoltenVK/MoltenVK/Utility/MVKFoundation.h @@ -137,8 +137,7 @@ static inline std::string mvkGetMoltenVKVersionString(uint32_t mvkVersion) { /** Returns whether the specified positive value is a power-of-two. */ template -static inline bool mvkIsPowerOfTwo(T value) { - // Test POT: (x != 0) && ((x & (x - 1)) == 0) +constexpr bool mvkIsPowerOfTwo(T value) { return value && ((value & (value - 1)) == 0); } @@ -148,7 +147,7 @@ static inline bool mvkIsPowerOfTwo(T value) { * that is larger than the specified value is returned. */ template -static inline T mvkEnsurePowerOfTwo(T value) { +constexpr T mvkEnsurePowerOfTwo(T value) { if (mvkIsPowerOfTwo(value)) { return value; } T pot = 1; @@ -163,7 +162,7 @@ static inline T mvkEnsurePowerOfTwo(T value) { * This implementation returns zero for both zero and one as inputs. */ template -static inline T mvkPowerOfTwoExponent(T value) { +constexpr T mvkPowerOfTwoExponent(T value) { T p2Value = mvkEnsurePowerOfTwo(value); // Count the trailing zeros @@ -184,7 +183,7 @@ static inline T mvkPowerOfTwoExponent(T value) { * This is a low level utility method. Usually you will use the convenience functions * mvkAlignAddress() and mvkAlignByteCount() to align addresses and offsets respectively. */ -static inline uintptr_t mvkAlignByteRef(uintptr_t byteRef, uintptr_t byteAlignment, bool alignDown = false) { +constexpr uintptr_t mvkAlignByteRef(uintptr_t byteRef, uintptr_t byteAlignment, bool alignDown = false) { if (byteAlignment == 0) { return byteRef; } assert(mvkIsPowerOfTwo(byteAlignment)); @@ -213,7 +212,7 @@ static inline void* mvkAlignAddress(void* address, uintptr_t byteAlignment, bool * which will be greater than or equal to the original offset if alignDown is false, or less * than or equal to the original offset if alignDown is true. */ -static inline uintptr_t mvkAlignByteCount(uintptr_t byteCount, uintptr_t byteAlignment, bool alignDown = false) { +constexpr uint64_t mvkAlignByteCount(uint64_t byteCount, uint64_t byteAlignment, bool alignDown = false) { return mvkAlignByteRef(byteCount, byteAlignment, alignDown); } @@ -254,22 +253,22 @@ static inline VkExtent2D mvkVkExtent2DFromVkExtent3D(VkExtent3D e) { return {e.w static inline VkExtent3D mvkVkExtent3DFromVkExtent2D(VkExtent2D e) { return {e.width, e.height, 1U }; } /** Returns whether the two Vulkan extents are equal by comparing their respective components. */ -static inline bool mvkVkExtent2DsAreEqual(VkExtent2D e1, VkExtent2D e2) { +constexpr bool mvkVkExtent2DsAreEqual(VkExtent2D e1, VkExtent2D e2) { return (e1.width == e2.width) && (e1.height == e2.height); } /** Returns whether the two Vulkan extents are equal by comparing their respective components. */ -static inline bool mvkVkExtent3DsAreEqual(VkExtent3D e1, VkExtent3D e2) { +constexpr bool mvkVkExtent3DsAreEqual(VkExtent3D e1, VkExtent3D e2) { return (e1.width == e2.width) && (e1.height == e2.height) && (e1.depth == e2.depth); } /** Returns whether the two Vulkan offsets are equal by comparing their respective components. */ -static inline bool mvkVkOffset2DsAreEqual(VkOffset2D os1, VkOffset2D os2) { +constexpr bool mvkVkOffset2DsAreEqual(VkOffset2D os1, VkOffset2D os2) { return (os1.x == os2.x) && (os1.y == os2.y); } /** Returns whether the two Vulkan offsets are equal by comparing their respective components. */ -static inline bool mvkVkOffset3DsAreEqual(VkOffset3D os1, VkOffset3D os2) { +constexpr bool mvkVkOffset3DsAreEqual(VkOffset3D os1, VkOffset3D os2) { return (os1.x == os2.x) && (os1.y == os2.y) && (os1.z == os2.z); } @@ -286,9 +285,9 @@ static inline VkOffset3D mvkVkOffset3DDifference(VkOffset3D minuend, VkOffset3D } /** Packs the four swizzle components into a single 32-bit word. */ -static inline uint32_t mvkPackSwizzle(VkComponentMapping components) { - return ((components.r & 0xFF) << 0) | ((components.g & 0xFF) << 8) | - ((components.b & 0xFF) << 16) | ((components.a & 0xFF) << 24); +constexpr uint32_t mvkPackSwizzle(VkComponentMapping components) { + return (((components.r & 0xFF) << 0) | ((components.g & 0xFF) << 8) | + ((components.b & 0xFF) << 16) | ((components.a & 0xFF) << 24)); } /** Unpacks a single 32-bit word containing four swizzle components. */ @@ -311,9 +310,9 @@ static inline VkComponentMapping mvkUnpackSwizzle(uint32_t packed) { * 3) Either cs1 or cs2 is VK_COMPONENT_SWIZZLE_MAX_ENUM, which is considered a wildcard, * and matches any value. */ -static inline bool mvkVKComponentSwizzlesMatch(VkComponentSwizzle cs1, - VkComponentSwizzle cs2, - VkComponentSwizzle csPos) { +constexpr bool mvkVKComponentSwizzlesMatch(VkComponentSwizzle cs1, + VkComponentSwizzle cs2, + VkComponentSwizzle csPos) { return ((cs1 == cs2) || ((cs1 == VK_COMPONENT_SWIZZLE_IDENTITY) && (cs2 == csPos)) || ((cs2 == VK_COMPONENT_SWIZZLE_IDENTITY) && (cs1 == csPos)) || @@ -328,7 +327,7 @@ static inline bool mvkVKComponentSwizzlesMatch(VkComponentSwizzle cs1, * A component value of VK_COMPONENT_SWIZZLE_MAX_ENUM is considered a wildcard and matches * any value in the corresponding component in the other mapping. */ -static inline bool mvkVkComponentMappingsMatch(VkComponentMapping cm1, VkComponentMapping cm2) { +constexpr bool mvkVkComponentMappingsMatch(VkComponentMapping cm1, VkComponentMapping cm2) { return (mvkVKComponentSwizzlesMatch(cm1.r, cm2.r, VK_COMPONENT_SWIZZLE_R) && mvkVKComponentSwizzlesMatch(cm1.g, cm2.g, VK_COMPONENT_SWIZZLE_G) && mvkVKComponentSwizzlesMatch(cm1.b, cm2.b, VK_COMPONENT_SWIZZLE_B) && @@ -342,7 +341,7 @@ static inline bool mvkVkComponentMappingsMatch(VkComponentMapping cm1, VkCompone #pragma mark Math /** Rounds the value to nearest integer using half-to-even rounding. */ -static inline double mvkRoundHalfToEven(const double val) { +constexpr double mvkRoundHalfToEven(const double val) { return val - std::remainder(val, 1.0); // remainder() uses half-to-even rounding } @@ -556,7 +555,7 @@ bool mvkAreEqual(const T* pV1, const T* pV2, size_t count = 1) { * otherwise returns false. This functionality is different than the char version of mvkAreEqual(), * which works on individual chars or char arrays, not strings. */ -static inline bool mvkStringsAreEqual(const char* pV1, const char* pV2, size_t count = 1) { +constexpr bool mvkStringsAreEqual(const char* pV1, const char* pV2, size_t count = 1) { return (pV1 && pV2) ? (strcmp(pV1, pV2) == 0) : false; } @@ -573,7 +572,7 @@ static inline bool mvkStringsAreEqual(const char* pV1, const char* pV2, size_t c * If the destination pointer is NULL, does nothing, and returns false. */ template -bool mvkSetOrClear(T* pDest, const T* pSrc) { +constexpr bool mvkSetOrClear(T* pDest, const T* pSrc) { if (pDest && pSrc) { *pDest = *pSrc; return true; @@ -595,17 +594,17 @@ void mvkDisableFlags(Tv& value, const Tm bitMask) { value = (Tv)(value & ~(Tv)bi /** Returns whether the specified value has ANY of the flags specified in bitMask enabled (set to 1). */ template -bool mvkIsAnyFlagEnabled(Tv value, const Tm bitMask) { return ((value & bitMask) != 0); } +constexpr bool mvkIsAnyFlagEnabled(Tv value, const Tm bitMask) { return ((value & bitMask) != 0); } /** Returns whether the specified value has ALL of the flags specified in bitMask enabled (set to 1). */ template -bool mvkAreAllFlagsEnabled(Tv value, const Tm bitMask) { return ((value & bitMask) == bitMask); } +constexpr bool mvkAreAllFlagsEnabled(Tv value, const Tm bitMask) { return ((value & bitMask) == bitMask); } /** Returns whether the specified value has ONLY one or more of the flags specified in bitMask enabled (set to 1), and none others. */ template -bool mvkIsOnlyAnyFlagEnabled(Tv value, const Tm bitMask) { return (mvkIsAnyFlagEnabled(value, bitMask) && ((value | bitMask) == bitMask)); } +constexpr bool mvkIsOnlyAnyFlagEnabled(Tv value, const Tm bitMask) { return (mvkIsAnyFlagEnabled(value, bitMask) && ((value | bitMask) == bitMask)); } /** Returns whether the specified value has ONLY ALL of the flags specified in bitMask enabled (set to 1), and none others. */ template -bool mvkAreOnlyAllFlagsEnabled(Tv value, const Tm bitMask) { return (value == bitMask); } +constexpr bool mvkAreOnlyAllFlagsEnabled(Tv value, const Tm bitMask) { return (value == bitMask); }