• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef COMMON_VULKANPLATFORM_H_
16 #define COMMON_VULKANPLATFORM_H_
17 
18 #if !defined(DAWN_ENABLE_BACKEND_VULKAN)
19 #    error "vulkan_platform.h included without the Vulkan backend enabled"
20 #endif
21 
22 #include "common/Platform.h"
23 
24 #include <cstddef>
25 #include <cstdint>
26 
27 // vulkan.h defines non-dispatchable handles to opaque pointers on 64bit architectures and uint64_t
28 // on 32bit architectures. This causes a problem in 32bit where the handles cannot be used to
29 // distinguish between overloads of the same function.
30 // Change the definition of non-dispatchable handles to be opaque structures containing a uint64_t
31 // and overload the comparison operators between themselves and VK_NULL_HANDLE (which will be
32 // redefined to be nullptr). This keeps the type-safety of having the handles be different types
33 // (like vulkan.h on 64 bit) but makes sure the types are different on 32 bit architectures.
34 
35 #if defined(DAWN_PLATFORM_64_BIT)
36 #    define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) \
37         using object##Native = struct object##_T*;
38 #elif defined(DAWN_PLATFORM_32_BIT)
39 #    define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) using object##Native = uint64_t;
40 #else
41 #    error "Unsupported platform"
42 #endif
43 
44 // Define a dummy Vulkan handle for use before we include vulkan.h
45 DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(VkSomeHandle)
46 
47 // Find out the alignment of native handles. Logically we would use alignof(VkSomeHandleNative) so
48 // why bother with the wrapper struct? It turns out that on Linux Intel x86 alignof(uint64_t) is 8
49 // but alignof(struct{uint64_t a;}) is 4. This is because this Intel ABI doesn't say anything about
50 // double-word alignment so for historical reasons compilers violated the standard and use an
51 // alignment of 4 for uint64_t (and double) inside structures.
52 // See https://stackoverflow.com/questions/44877185
53 // One way to get the alignment inside structures of a type is to look at the alignment of it
54 // wrapped in a structure. Hence VkSameHandleNativeWrappe
55 
56 template <typename T>
57 struct WrapperStruct {
58     T member;
59 };
60 
61 template <typename T>
62 static constexpr size_t AlignOfInStruct = alignof(WrapperStruct<T>);
63 
64 static constexpr size_t kNativeVkHandleAlignment = AlignOfInStruct<VkSomeHandleNative>;
65 static constexpr size_t kUint64Alignment = AlignOfInStruct<VkSomeHandleNative>;
66 
67 // Simple handle types that supports "nullptr_t" as a 0 value.
68 template <typename Tag, typename HandleType>
69 class alignas(kNativeVkHandleAlignment) VkNonDispatchableHandle {
70   public:
71     // Default constructor and assigning of VK_NULL_HANDLE
72     VkNonDispatchableHandle() = default;
VkNonDispatchableHandle(std::nullptr_t)73     VkNonDispatchableHandle(std::nullptr_t) : mHandle(0) {
74     }
75 
76     // Use default copy constructor/assignment
77     VkNonDispatchableHandle(const VkNonDispatchableHandle<Tag, HandleType>& other) = default;
78     VkNonDispatchableHandle& operator=(const VkNonDispatchableHandle<Tag, HandleType>&) = default;
79 
80     // Comparisons between handles
81     bool operator==(VkNonDispatchableHandle<Tag, HandleType> other) {
82         return mHandle == other.mHandle;
83     }
84     bool operator!=(VkNonDispatchableHandle<Tag, HandleType> other) {
85         return mHandle != other.mHandle;
86     }
87 
88     // Comparisons between handles and VK_NULL_HANDLE
89     bool operator==(std::nullptr_t) {
90         return mHandle == 0;
91     }
92     bool operator!=(std::nullptr_t) {
93         return mHandle != 0;
94     }
95 
96     // The regular Vulkan handle type depends on the pointer width but is always 64 bits wide.
97     //  - On 64bit it is an opaque pointer type, probably to help with type safety
98     //  - On 32bit it is a uint64_t because pointers aren't wide enough (and non dispatchable
99     //    handles can be optimized to not be pointer but contain GPU virtual addresses or the
100     //    data in a packed form).
101     // Because of this we need two types of conversions from our handle type: to uint64_t and to
102     // the "native" Vulkan type that may not be an uint64_t
103 
CreateFromU64(uint64_t handle)104     static VkNonDispatchableHandle<Tag, HandleType> CreateFromU64(uint64_t handle) {
105         return {handle};
106     }
107 
GetU64()108     uint64_t GetU64() const {
109         return mHandle;
110     }
111 
112 #if defined(DAWN_PLATFORM_64_BIT)
CreateFromHandle(HandleType handle)113     static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
114         return CreateFromU64(static_cast<uint64_t>(reinterpret_cast<intptr_t>(handle)));
115     }
116 
GetHandle()117     HandleType GetHandle() const {
118         return mHandle;
119     }
120 #elif defined(DAWN_PLATFORM_32_BIT)
CreateFromHandle(HandleType handle)121     static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
122         return {handle};
123     }
124 
GetHandle()125     HandleType GetHandle() const {
126         return mHandle;
127     }
128 #else
129 #    error "Unsupported platform"
130 #endif
131 
132   private:
VkNonDispatchableHandle(uint64_t handle)133     VkNonDispatchableHandle(uint64_t handle) : mHandle(handle) {
134     }
135 
136     uint64_t mHandle = 0;
137 };
138 
139 #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object)                          \
140     struct VkTag##object;                                                  \
141     DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object)                     \
142     using object = VkNonDispatchableHandle<VkTag##object, object##Native>; \
143     static_assert(sizeof(object) == sizeof(uint64_t), "");                 \
144     static_assert(alignof(object) == kUint64Alignment, "");                \
145     static_assert(sizeof(object) == sizeof(object##Native), "");           \
146     static_assert(alignof(object) == kNativeVkHandleAlignment, "");
147 
148 #    include <vulkan/vulkan.h>
149 
150     // VK_NULL_HANDLE is defined to 0 but we don't want our handle type to compare to arbitrary
151     // integers. Redefine VK_NULL_HANDLE to nullptr that has its own type.
152 #    undef VK_NULL_HANDLE
153 #    define VK_NULL_HANDLE nullptr
154 
155 // Remove windows.h macros after vulkan_platform's include of windows.h
156 #if defined(DAWN_PLATFORM_WINDOWS)
157 #    include "common/windows_with_undefs.h"
158 #endif
159 // Remove X11/Xlib.h macros after vulkan_platform's include of it.
160 #if defined(DAWN_PLATFORM_LINUX)
161 #    include "common/xlib_with_undefs.h"
162 #endif
163 
164 #endif  // COMMON_VULKANPLATFORM_H_
165