Fix VkNonDispatchableHandle's alignment on 32bit platforms

On 32bit platforms, even if it contains a uint64_t the handle wrapper
will align to 4 bytes because the assembly load operations can't take
advantage of 64bit aligned loads.

Fix this by forcing the alignment to the alignment of what would be the
native Vulkan handle type.

Also use macros with defined()

Change-Id: I0de9b4a77e648c416b04311b854c956762248868
This commit is contained in:
Corentin Wallez 2018-08-23 23:47:50 +02:00 committed by Corentin Wallez
parent 25b4d318c9
commit d90748b256
1 changed files with 17 additions and 5 deletions

View File

@ -32,9 +32,21 @@
// redefined to be nullptr). This keeps the type-safety of having the handles be different types // redefined to be nullptr). This keeps the type-safety of having the handles be different types
// (like vulkan.h on 64 bit) but makes sure the types are different on 32 bit architectures. // (like vulkan.h on 64 bit) but makes sure the types are different on 32 bit architectures.
// Force the handle type to have the same alignment as what would have been the Vulkan
// non-dispatchable handle type.
#if defined(DAWN_PLATFORM_64_BIT)
// In 64 bit handles are pointers to some structure, we just declare one inline here.
using NativeVulkanHandleType = struct VkSomeHandle*;
#elif defined(DAWN_PLATFORM_32_BIT)
using NativeVulkanHandleType = uint64_t;
# define ALIGNAS_VULKAN_HANDLE alignas(alignof(uint64_t))
#else
# error "Unsupported platform"
#endif
// Simple handle types that supports "nullptr_t" as a 0 value. // Simple handle types that supports "nullptr_t" as a 0 value.
template <typename Tag, typename HandleType> template <typename Tag, typename HandleType>
class VkNonDispatchableHandle { class alignas(alignof(NativeVulkanHandleType)) VkNonDispatchableHandle {
public: public:
// Default constructor and assigning of VK_NULL_HANDLE // Default constructor and assigning of VK_NULL_HANDLE
VkNonDispatchableHandle() = default; VkNonDispatchableHandle() = default;
@ -77,7 +89,7 @@ class VkNonDispatchableHandle {
return mHandle; return mHandle;
} }
#if DAWN_PLATFORM_64_BIT #if defined(DAWN_PLATFORM_64_BIT)
static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) { static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
return CreateFromU64(static_cast<uint64_t>(reinterpret_cast<intptr_t>(handle))); return CreateFromU64(static_cast<uint64_t>(reinterpret_cast<intptr_t>(handle)));
} }
@ -85,7 +97,7 @@ class VkNonDispatchableHandle {
HandleType GetHandle() const { HandleType GetHandle() const {
return mHandle; return mHandle;
} }
#elif DAWN_PLATFORM_32_BIT #elif defined(DAWN_PLATFORM_32_BIT)
static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) { static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
return {handle}; return {handle};
} }
@ -104,10 +116,10 @@ class VkNonDispatchableHandle {
uint64_t mHandle = 0; uint64_t mHandle = 0;
}; };
#if DAWN_PLATFORM_64_BIT #if defined(DAWN_PLATFORM_64_BIT)
# define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) \ # define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) \
using object##Native = struct object##_T*; using object##Native = struct object##_T*;
#elif DAWN_PLATFORM_32_BIT #elif defined(DAWN_PLATFORM_32_BIT)
# define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) using object##Native = uint64_t; # define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) using object##Native = uint64_t;
#else #else
# error "Unsupported platform" # error "Unsupported platform"