• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _VKDEFS_HPP
2 #define _VKDEFS_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2015 Google Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Vulkan utilites.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "tcuDefs.hpp"
27 #include <cstddef>
28 #include <type_traits>
29 
30 #if (DE_OS == DE_OS_ANDROID) && defined(__ARM_ARCH) && defined(__ARM_32BIT_STATE)
31 #define VKAPI_ATTR __attribute__((pcs("aapcs-vfp")))
32 #else
33 #define VKAPI_ATTR
34 #endif
35 
36 #if (DE_OS == DE_OS_WIN32) && \
37     ((defined(_MSC_VER) && _MSC_VER >= 800) || defined(__MINGW32__) || defined(_STDCALL_SUPPORTED))
38 #define VKAPI_CALL __stdcall
39 #else
40 #define VKAPI_CALL
41 #endif
42 
43 // A helper to make sure VK_NULL_HANDLE is not accidentally used where it doesn't make sense.  When
44 // assigned to a handle defined by VK_DEFINE_HANDLE below (a pointer), it is automatically cast to
45 // nullptr.  When assigned to a Handle object, a special constructor that accepts this type sets it
46 // internal value to 0.
47 struct VkNullHandleType
48 {
49     template <typename T>
operator T*VkNullHandleType50     operator T *()
51     {
52         return nullptr;
53     }
54 };
55 
56 #define VK_NULL_HANDLE \
57     VkNullHandleType   \
58     {                  \
59     }
60 #define VK_DEFINE_HANDLE(NAME, TYPE) typedef struct NAME##_s *NAME
61 #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(NAME, TYPE) typedef Handle<TYPE> NAME
62 
63 #define VK_DEFINE_PLATFORM_TYPE(NAME, COMPATIBLE)                 \
64     namespace pt                                                  \
65     {                                                             \
66     struct NAME                                                   \
67     {                                                             \
68         COMPATIBLE internal;                                      \
69         explicit NAME(COMPATIBLE internal_) : internal(internal_) \
70         {                                                         \
71         }                                                         \
72         NAME(std::nullptr_t) : internal()                         \
73         {                                                         \
74         }                                                         \
75     };                                                            \
76     } // pt
77 
78 #define VK_MAKE_API_VERSION(VARIANT, MAJOR, MINOR, PATCH) \
79     ((((uint32_t)(VARIANT)) << 29) | (((uint32_t)(MAJOR)) << 22) | (((uint32_t)(MINOR)) << 12) | ((uint32_t)(PATCH)))
80 #define VKSC_API_VARIANT 1
81 #define VK_MAKE_VERSION(MAJOR, MINOR, PATCH) VK_MAKE_API_VERSION(0, MAJOR, MINOR, PATCH)
82 #define VK_BIT(NUM) (1u << (uint32_t)(NUM))
83 
84 #define VK_API_VERSION_VARIANT(version) ((uint32_t)(version) >> 29)
85 #define VK_API_VERSION_MAJOR(version) (((uint32_t)(version) >> 22) & 0x7FU)
86 #define VK_API_VERSION_MINOR(version) (((uint32_t)(version) >> 12) & 0x3FFU)
87 #define VK_API_VERSION_PATCH(version) ((uint32_t)(version)&0xFFFU)
88 
89 #define VK_CHECK(EXPR) vk::checkResult((EXPR), #EXPR, __FILE__, __LINE__)
90 #define VK_CHECK_SUPPORTED(EXPR) vk::checkResultSupported((EXPR), #EXPR, __FILE__, __LINE__)
91 #define VK_CHECK_MSG(EXPR, MSG) vk::checkResult((EXPR), MSG, __FILE__, __LINE__)
92 #define VK_CHECK_WSI(EXPR) vk::checkWsiResult((EXPR), #EXPR, __FILE__, __LINE__)
93 
94 #define VK_MAKE_VIDEO_STD_VERSION(major, minor, patch) \
95     ((((uint32_t)(major)) << 22) | (((uint32_t)(minor)) << 12) | ((uint32_t)(patch)))
96 
97 /*--------------------------------------------------------------------*//*!
98  * \brief Vulkan utilities
99  *//*--------------------------------------------------------------------*/
100 namespace vk
101 {
102 
103 typedef uint64_t VkDeviceSize;
104 typedef uint32_t VkSampleMask;
105 typedef uint32_t VkBool32;
106 typedef uint32_t VkFlags;
107 typedef uint64_t VkFlags64;
108 typedef uint64_t VkDeviceAddress;
109 
110 // enum HandleType { HANDLE_TYPE_INSTANCE, ... };
111 #include "vkHandleType.inl"
112 
113 template <HandleType Type>
114 class Handle
115 {
116 public:
Handle(void)117     Handle(void)
118     {
119     } // \note Left uninitialized on purpose
Handle(VkNullHandleType)120     Handle(VkNullHandleType) : m_internal(0)
121     {
122     }
123 
124     // Helpers for vkNullDriver (an internal implementation of a Vulkan driver).
125     template <typename T>
Handle(const T * obj)126     Handle(const T *obj) : m_internal(reinterpret_cast<uintptr_t>(obj))
127     {
128     }
129     template <typename T>
as() const130     T *as() const
131     {
132         return reinterpret_cast<T *>(m_internal);
133     }
134 
operator =(VkNullHandleType)135     Handle &operator=(VkNullHandleType)
136     {
137         m_internal = 0;
138         return *this;
139     }
140 
operator ==(const Handle<Type> & other) const141     bool operator==(const Handle<Type> &other) const
142     {
143         return this->m_internal == other.m_internal;
144     }
operator !=(const Handle<Type> & other) const145     bool operator!=(const Handle<Type> &other) const
146     {
147         return this->m_internal != other.m_internal;
148     }
149 
operator !(void) const150     bool operator!(void) const
151     {
152         return !m_internal;
153     }
154 
getInternal(void) const155     uint64_t getInternal(void) const
156     {
157         return m_internal;
158     }
159 
160     enum
161     {
162         HANDLE_TYPE = Type
163     };
164 
165 private:
166     uint64_t m_internal;
167 };
168 
169 template <HandleType Type>
operator <(const Handle<Type> & lhs,const Handle<Type> & rhs)170 bool operator<(const Handle<Type> &lhs, const Handle<Type> &rhs)
171 {
172     return lhs.getInternal() < rhs.getInternal();
173 }
174 
175 #include "vkBasicTypes.inl"
176 
177 #define VK_CORE_FORMAT_LAST ((vk::VkFormat)(vk::VK_FORMAT_ASTC_12x12_SRGB_BLOCK + 1))
178 #define VK_CORE_IMAGE_TILING_LAST ((vk::VkImageTiling)(vk::VK_IMAGE_TILING_LINEAR + 1))
179 #define VK_CORE_IMAGE_TYPE_LAST ((vk::VkImageType)(vk::VK_IMAGE_TYPE_3D + 1))
180 
181 enum SpirvVersion
182 {
183     SPIRV_VERSION_1_0 = 0, //!< SPIR-V 1.0
184     SPIRV_VERSION_1_1 = 1, //!< SPIR-V 1.1
185     SPIRV_VERSION_1_2 = 2, //!< SPIR-V 1.2
186     SPIRV_VERSION_1_3 = 3, //!< SPIR-V 1.3
187     SPIRV_VERSION_1_4 = 4, //!< SPIR-V 1.4
188     SPIRV_VERSION_1_5 = 5, //!< SPIR-V 1.5
189     SPIRV_VERSION_1_6 = 6, //!< SPIR-V 1.6
190 
191     SPIRV_VERSION_LAST
192 };
193 
194 typedef struct
195 {
196     uint32_t magic;
197     uint32_t version;
198     uint32_t generator;
199     uint32_t bound;
200 } SpirvBinaryHeader;
201 
202 namespace wsi
203 {
204 
205 enum Type
206 {
207     TYPE_XLIB = 0,
208     TYPE_XCB,
209     TYPE_WAYLAND,
210     TYPE_ANDROID,
211     TYPE_WIN32,
212     TYPE_METAL,
213     TYPE_HEADLESS,
214     TYPE_DIRECT_DRM,
215 
216     TYPE_LAST
217 };
218 
219 } // namespace wsi
220 
221 typedef VKAPI_ATTR void(VKAPI_CALL *PFN_vkVoidFunction)(void);
222 
223 typedef VKAPI_ATTR void *(VKAPI_CALL *PFN_vkAllocationFunction)(void *pUserData, size_t size, size_t alignment,
224                                                                 VkSystemAllocationScope allocationScope);
225 typedef VKAPI_ATTR void *(VKAPI_CALL *PFN_vkReallocationFunction)(void *pUserData, void *pOriginal, size_t size,
226                                                                   size_t alignment,
227                                                                   VkSystemAllocationScope allocationScope);
228 typedef VKAPI_ATTR void(VKAPI_CALL *PFN_vkFreeFunction)(void *pUserData, void *pMem);
229 typedef VKAPI_ATTR void(VKAPI_CALL *PFN_vkInternalAllocationNotification)(void *pUserData, size_t size,
230                                                                           VkInternalAllocationType allocationType,
231                                                                           VkSystemAllocationScope allocationScope);
232 typedef VKAPI_ATTR void(VKAPI_CALL *PFN_vkInternalFreeNotification)(void *pUserData, size_t size,
233                                                                     VkInternalAllocationType allocationType,
234                                                                     VkSystemAllocationScope allocationScope);
235 
236 #ifndef CTS_USES_VULKANSC
237 
238 typedef VKAPI_ATTR VkBool32(VKAPI_CALL *PFN_vkDebugReportCallbackEXT)(VkDebugReportFlagsEXT flags,
239                                                                       VkDebugReportObjectTypeEXT objectType,
240                                                                       uint64_t object, size_t location,
241                                                                       int32_t messageCode, const char *pLayerPrefix,
242                                                                       const char *pMessage, void *pUserData);
243 
244 typedef VKAPI_ATTR PFN_vkVoidFunction(VKAPI_CALL *PFN_vkGetInstanceProcAddrLUNARG)(VkInstance instance,
245                                                                                    const char pName);
246 
247 #endif // CTS_USES_VULKANSC
248 
249 typedef VKAPI_ATTR VkBool32(VKAPI_CALL *PFN_vkDebugUtilsMessengerCallbackEXT)(
250     VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageTypes,
251     const struct VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData);
252 
253 typedef VKAPI_ATTR void(VKAPI_CALL *PFN_vkDeviceMemoryReportCallbackEXT)(
254     const struct VkDeviceMemoryReportCallbackDataEXT *pCallbackData, void *pUserData);
255 
256 #ifdef CTS_USES_VULKANSC
257 struct VkFaultData;
258 typedef VKAPI_ATTR void(VKAPI_CALL *PFN_vkFaultCallbackFunction)(VkBool32 incompleteFaultData, uint32_t faultCount,
259                                                                  const VkFaultData *pFaultData);
260 #endif // CTS_USES_VULKANSC
261 
262 #include "vkStructTypes.inl"
263 
264 typedef void *VkRemoteAddressNV;
265 
266 extern "C"
267 {
268 #include "vkFunctionPointerTypes.inl"
269 }
270 
271 class PlatformInterface
272 {
273 public:
274 #include "vkVirtualPlatformInterface.inl"
275 
276     virtual GetInstanceProcAddrFunc getGetInstanceProcAddr() const = 0;
277 
~PlatformInterface()278     virtual ~PlatformInterface()
279     {
280     }
281     PlatformInterface(const PlatformInterface &)            = delete;
282     PlatformInterface &operator=(const PlatformInterface &) = delete;
283 
284 protected:
PlatformInterface(void)285     PlatformInterface(void)
286     {
287     }
288 };
289 
290 class InstanceInterface
291 {
292 public:
293 #include "vkVirtualInstanceInterface.inl"
294 
~InstanceInterface()295     virtual ~InstanceInterface()
296     {
297     }
298     InstanceInterface(const InstanceInterface &)            = delete;
299     InstanceInterface &operator=(const InstanceInterface &) = delete;
300 
301 protected:
InstanceInterface(void)302     InstanceInterface(void)
303     {
304     }
305 };
306 
307 class DeviceInterface
308 {
309 public:
310 #include "vkVirtualDeviceInterface.inl"
311 
312 #ifdef CTS_USES_VULKANSC
313     virtual VkResult createShaderModule(VkDevice device, const VkShaderModuleCreateInfo *pCreateInfo,
314                                         const VkAllocationCallbacks *pAllocator,
315                                         VkShaderModule *pShaderModule) const = 0;
316 #endif // CTS_USES_VULKANSC
317 
~DeviceInterface()318     virtual ~DeviceInterface()
319     {
320     }
321     DeviceInterface(const DeviceInterface &)            = delete;
322     DeviceInterface &operator=(const DeviceInterface &) = delete;
323 
324 protected:
DeviceInterface(void)325     DeviceInterface(void)
326     {
327     }
328 };
329 
330 class Error : public tcu::TestError
331 {
332 public:
333     Error(VkResult error, const char *message, const char *expr, const char *file, int line, qpTestResult result);
334     Error(VkResult error, const char *message, const char *expr, const char *file, int line);
335     Error(VkResult error, const std::string &message);
336     virtual ~Error(void) throw();
337 
getError(void) const338     VkResult getError(void) const
339     {
340         return m_error;
341     }
342 
343 private:
344     const VkResult m_error;
345 };
346 
347 class NotSupportedError : public tcu::NotSupportedError
348 {
349 public:
350     NotSupportedError(VkResult error, const char *message, const char *expr, const char *file, int line);
351     NotSupportedError(VkResult error, const std::string &message);
352     virtual ~NotSupportedError(void) throw();
353 
getError(void) const354     VkResult getError(void) const
355     {
356         return m_error;
357     }
358 
359 private:
360     const VkResult m_error;
361 };
362 
363 class OutOfMemoryError : public tcu::ResourceError
364 {
365 public:
366     OutOfMemoryError(VkResult error, const char *message, const char *expr, const char *file, int line);
367     OutOfMemoryError(VkResult error, const std::string &message);
368     virtual ~OutOfMemoryError(void) throw();
369 
getError(void) const370     VkResult getError(void) const
371     {
372         return m_error;
373     }
374 
375 private:
376     const VkResult m_error;
377 };
378 
379 void checkResult(VkResult result, const char *message, const char *file, int line);
380 void checkResultSupported(VkResult result, const char *message, const char *file, int line);
381 void checkWsiResult(VkResult result, const char *message, const char *file, int line);
382 
383 } // namespace vk
384 
385 #endif // _VKDEFS_HPP
386