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 #if defined(VULKAN_CORE_H_)
22 #    error "vulkan.h included before vulkan_platform.h"
23 #endif
24 
25 #include "common/Platform.h"
26 
27 #include <cstddef>
28 #include <cstdint>
29 
30 // vulkan.h defines non-dispatchable handles to opaque pointers on 64bit architectures and uint64_t
31 // on 32bit architectures. This causes a problem in 32bit where the handles cannot be used to
32 // distinguish between overloads of the same function.
33 // Change the definition of non-dispatchable handles to be opaque structures containing a uint64_t
34 // and overload the comparison operators between themselves and VK_NULL_HANDLE (which will be
35 // redefined to be nullptr). This keeps the type-safety of having the handles be different types
36 // (like vulkan.h on 64 bit) but makes sure the types are different on 32 bit architectures.
37 
38 #if defined(DAWN_PLATFORM_64_BIT)
39 #    define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) using object = struct object##_T*;
40 // This function is needed because MSVC doesn't accept reinterpret_cast from uint64_t from uint64_t
41 // TODO(cwallez@chromium.org): Remove this once we rework vulkan_platform.h
42 template <typename T>
NativeNonDispatachableHandleFromU64(uint64_t u64)43 T NativeNonDispatachableHandleFromU64(uint64_t u64) {
44     return reinterpret_cast<T>(u64);
45 }
46 #elif defined(DAWN_PLATFORM_32_BIT)
47 #    define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) using object = uint64_t;
48 template <typename T>
NativeNonDispatachableHandleFromU64(uint64_t u64)49 T NativeNonDispatachableHandleFromU64(uint64_t u64) {
50     return u64;
51 }
52 #else
53 #    error "Unsupported platform"
54 #endif
55 
56 // Define a dummy Vulkan handle for use before we include vulkan.h
DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(VkSomeHandle)57 DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(VkSomeHandle)
58 
59 // Find out the alignment of native handles. Logically we would use alignof(VkSomeHandleNative) so
60 // why bother with the wrapper struct? It turns out that on Linux Intel x86 alignof(uint64_t) is 8
61 // but alignof(struct{uint64_t a;}) is 4. This is because this Intel ABI doesn't say anything about
62 // double-word alignment so for historical reasons compilers violated the standard and use an
63 // alignment of 4 for uint64_t (and double) inside structures.
64 // See https://stackoverflow.com/questions/44877185
65 // One way to get the alignment inside structures of a type is to look at the alignment of it
66 // wrapped in a structure. Hence VkSameHandleNativeWrappe
67 
68 namespace dawn_native { namespace vulkan {
69 
70     namespace detail {
71         template <typename T>
72         struct WrapperStruct {
73             T member;
74         };
75 
76         template <typename T>
77         static constexpr size_t AlignOfInStruct = alignof(WrapperStruct<T>);
78 
79         static constexpr size_t kNativeVkHandleAlignment = AlignOfInStruct<VkSomeHandle>;
80         static constexpr size_t kUint64Alignment = AlignOfInStruct<uint64_t>;
81 
82         // Simple handle types that supports "nullptr_t" as a 0 value.
83         template <typename Tag, typename HandleType>
84         class alignas(detail::kNativeVkHandleAlignment) VkHandle {
85           public:
86             // Default constructor and assigning of VK_NULL_HANDLE
87             VkHandle() = default;
88             VkHandle(std::nullptr_t) {
89             }
90 
91             // Use default copy constructor/assignment
92             VkHandle(const VkHandle<Tag, HandleType>& other) = default;
93             VkHandle& operator=(const VkHandle<Tag, HandleType>&) = default;
94 
95             // Comparisons between handles
96             bool operator==(VkHandle<Tag, HandleType> other) const {
97                 return mHandle == other.mHandle;
98             }
99             bool operator!=(VkHandle<Tag, HandleType> other) const {
100                 return mHandle != other.mHandle;
101             }
102 
103             // Comparisons between handles and VK_NULL_HANDLE
104             bool operator==(std::nullptr_t) const {
105                 return mHandle == 0;
106             }
107             bool operator!=(std::nullptr_t) const {
108                 return mHandle != 0;
109             }
110 
111             // Implicit conversion to real Vulkan types.
112             operator HandleType() const {
113                 return GetHandle();
114             }
115 
116             HandleType GetHandle() const {
117                 return mHandle;
118             }
119 
120             HandleType& operator*() {
121                 return mHandle;
122             }
123 
124             static VkHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
125                 return VkHandle{handle};
126             }
127 
128           private:
129             explicit VkHandle(HandleType handle) : mHandle(handle) {
130             }
131 
132             HandleType mHandle = 0;
133         };
134     }  // namespace detail
135 
136     static constexpr std::nullptr_t VK_NULL_HANDLE = nullptr;
137 
138     template <typename Tag, typename HandleType>
139     HandleType* AsVkArray(detail::VkHandle<Tag, HandleType>* handle) {
140         return reinterpret_cast<HandleType*>(handle);
141     }
142 
143 }}  // namespace dawn_native::vulkan
144 
145 #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object)                                   \
146     DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object)                              \
147     namespace dawn_native { namespace vulkan {                                      \
148             using object = detail::VkHandle<struct VkTag##object, ::object>;        \
149             static_assert(sizeof(object) == sizeof(uint64_t), "");                  \
150             static_assert(alignof(object) == detail::kUint64Alignment, "");         \
151             static_assert(sizeof(object) == sizeof(::object), "");                  \
152             static_assert(alignof(object) == detail::kNativeVkHandleAlignment, ""); \
153         }                                                                           \
154     }  // namespace dawn_native::vulkan
155 
156 // Import additional parts of Vulkan that are supported on our architecture and preemptively include
157 // headers that vulkan.h includes that we have "undefs" for.
158 #if defined(DAWN_PLATFORM_WINDOWS)
159 #    define VK_USE_PLATFORM_WIN32_KHR
160 #    include "common/windows_with_undefs.h"
161 #endif  // DAWN_PLATFORM_WINDOWS
162 
163 #if defined(DAWN_USE_X11)
164 #    define VK_USE_PLATFORM_XLIB_KHR
165 #    include "common/xlib_with_undefs.h"
166 #endif  // defined(DAWN_USE_X11)
167 
168 #if defined(DAWN_ENABLE_BACKEND_METAL)
169 #    define VK_USE_PLATFORM_METAL_EXT
170 #endif  // defined(DAWN_ENABLE_BACKEND_METAL)
171 
172 #if defined(DAWN_PLATFORM_ANDROID)
173 #    define VK_USE_PLATFORM_ANDROID_KHR
174 #endif  // defined(DAWN_PLATFORM_ANDROID)
175 
176 #if defined(DAWN_PLATFORM_FUCHSIA)
177 #    define VK_USE_PLATFORM_FUCHSIA
178 #endif  // defined(DAWN_PLATFORM_FUCHSIA)
179 
180 // The actual inclusion of vulkan.h!
181 #define VK_NO_PROTOTYPES
182 #include <vulkan/vulkan.h>
183 
184 // Redefine VK_NULL_HANDLE for better type safety where possible.
185 #undef VK_NULL_HANDLE
186 #if defined(DAWN_PLATFORM_64_BIT)
187 static constexpr std::nullptr_t VK_NULL_HANDLE = nullptr;
188 #elif defined(DAWN_PLATFORM_32_BIT)
189 static constexpr uint64_t VK_NULL_HANDLE = 0;
190 #else
191 #    error "Unsupported platform"
192 #endif
193 
194 // Include Fuchsia-specific definitions that are not upstreamed yet.
195 #if defined(DAWN_PLATFORM_FUCHSIA)
196 #    include <vulkan/vulkan_fuchsia_extras.h>
197 #endif  // defined(DAWN_PLATFORM_FUCHSIA)
198 
199 #endif  // COMMON_VULKANPLATFORM_H_
200