Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 1 | #ifndef TEST_COMMON_H |
| 2 | #define TEST_COMMON_H |
| 3 | |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdbool.h> |
| 7 | #include <string.h> |
| 8 | #include <assert.h> |
| 9 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 10 | #include <vulkan.h> |
Mark Lobodzinski | eccbb37 | 2015-09-01 09:00:16 -0600 | [diff] [blame^] | 11 | #include <vk_sdk_platform.h> |
Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 12 | |
| 13 | #include "gtest/gtest.h" |
Courtney Goeltzenleuchter | 5ec6c02 | 2014-09-01 16:33:55 -0600 | [diff] [blame] | 14 | #include "gtest-1.7.0/include/gtest/gtest.h" |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 15 | #include "vktestbinding.h" |
Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 16 | |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 17 | #define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err) |
Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 18 | |
Courtney Goeltzenleuchter | 382489d | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 19 | static inline const char *vk_result_string(VkResult err) |
Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 20 | { |
| 21 | switch (err) { |
| 22 | #define STR(r) case r: return #r |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 23 | STR(VK_SUCCESS); |
| 24 | STR(VK_UNSUPPORTED); |
| 25 | STR(VK_NOT_READY); |
| 26 | STR(VK_TIMEOUT); |
| 27 | STR(VK_EVENT_SET); |
| 28 | STR(VK_EVENT_RESET); |
| 29 | STR(VK_ERROR_UNKNOWN); |
| 30 | STR(VK_ERROR_UNAVAILABLE); |
| 31 | STR(VK_ERROR_INITIALIZATION_FAILED); |
Tony Barbour | 8205d90 | 2015-04-16 15:59:00 -0600 | [diff] [blame] | 32 | STR(VK_ERROR_OUT_OF_HOST_MEMORY); |
| 33 | STR(VK_ERROR_OUT_OF_DEVICE_MEMORY); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 34 | STR(VK_ERROR_DEVICE_ALREADY_CREATED); |
| 35 | STR(VK_ERROR_DEVICE_LOST); |
| 36 | STR(VK_ERROR_INVALID_POINTER); |
| 37 | STR(VK_ERROR_INVALID_VALUE); |
| 38 | STR(VK_ERROR_INVALID_HANDLE); |
| 39 | STR(VK_ERROR_INVALID_ORDINAL); |
| 40 | STR(VK_ERROR_INVALID_MEMORY_SIZE); |
| 41 | STR(VK_ERROR_INVALID_EXTENSION); |
| 42 | STR(VK_ERROR_INVALID_FLAGS); |
| 43 | STR(VK_ERROR_INVALID_ALIGNMENT); |
| 44 | STR(VK_ERROR_INVALID_FORMAT); |
| 45 | STR(VK_ERROR_INVALID_IMAGE); |
| 46 | STR(VK_ERROR_INVALID_DESCRIPTOR_SET_DATA); |
| 47 | STR(VK_ERROR_INVALID_QUEUE_TYPE); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 48 | STR(VK_ERROR_UNSUPPORTED_SHADER_IL_VERSION); |
| 49 | STR(VK_ERROR_BAD_SHADER_CODE); |
| 50 | STR(VK_ERROR_BAD_PIPELINE_DATA); |
Courtney Goeltzenleuchter | 9cc421e | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 51 | STR(VK_ERROR_NOT_MAPPABLE); |
| 52 | STR(VK_ERROR_MEMORY_MAP_FAILED); |
| 53 | STR(VK_ERROR_MEMORY_UNMAP_FAILED); |
| 54 | STR(VK_ERROR_INCOMPATIBLE_DEVICE); |
| 55 | STR(VK_ERROR_INCOMPATIBLE_DRIVER); |
| 56 | STR(VK_ERROR_INCOMPLETE_COMMAND_BUFFER); |
| 57 | STR(VK_ERROR_BUILDING_COMMAND_BUFFER); |
| 58 | STR(VK_ERROR_MEMORY_NOT_BOUND); |
| 59 | STR(VK_ERROR_INCOMPATIBLE_QUEUE); |
Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 60 | #undef STR |
| 61 | default: return "UNKNOWN_RESULT"; |
| 62 | } |
| 63 | } |
| 64 | |
Chia-I Wu | 9dac52d | 2014-12-27 22:04:00 +0800 | [diff] [blame] | 65 | static inline void test_error_callback(const char *expr, const char *file, |
| 66 | unsigned int line, const char *function) |
| 67 | { |
| 68 | ADD_FAILURE_AT(file, line) << "Assertion: `" << expr << "'"; |
| 69 | } |
| 70 | |
Mike Stroyan | 7016f4f | 2015-07-13 14:45:35 -0600 | [diff] [blame] | 71 | #if defined(__linux__) |
| 72 | /* Linux-specific common code: */ |
| 73 | |
| 74 | #include <pthread.h> |
| 75 | |
| 76 | // Threads: |
| 77 | typedef pthread_t test_platform_thread; |
| 78 | |
| 79 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void*), void *data) |
| 80 | { |
| 81 | pthread_attr_t thread_attr; |
| 82 | pthread_attr_init(&thread_attr); |
| 83 | return pthread_create(thread, &thread_attr, func, data); |
| 84 | } |
| 85 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) |
| 86 | { |
| 87 | return pthread_join(thread, retval); |
| 88 | } |
| 89 | |
| 90 | // Thread IDs: |
| 91 | typedef pthread_t test_platform_thread_id; |
| 92 | static inline test_platform_thread_id test_platform_get_thread_id() |
| 93 | { |
| 94 | return pthread_self(); |
| 95 | } |
| 96 | |
| 97 | // Thread mutex: |
| 98 | typedef pthread_mutex_t test_platform_thread_mutex; |
| 99 | static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex) |
| 100 | { |
| 101 | pthread_mutex_init(pMutex, NULL); |
| 102 | } |
| 103 | static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex) |
| 104 | { |
| 105 | pthread_mutex_lock(pMutex); |
| 106 | } |
| 107 | static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex) |
| 108 | { |
| 109 | pthread_mutex_unlock(pMutex); |
| 110 | } |
| 111 | static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex) |
| 112 | { |
| 113 | pthread_mutex_destroy(pMutex); |
| 114 | } |
| 115 | typedef pthread_cond_t test_platform_thread_cond; |
| 116 | static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond) |
| 117 | { |
| 118 | pthread_cond_init(pCond, NULL); |
| 119 | } |
| 120 | static inline void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex) |
| 121 | { |
| 122 | pthread_cond_wait(pCond, pMutex); |
| 123 | } |
| 124 | static inline void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond) |
| 125 | { |
| 126 | pthread_cond_broadcast(pCond); |
| 127 | } |
| 128 | |
| 129 | #elif defined(_WIN32) // defined(__linux__) |
| 130 | /* Windows-specific common code: */ |
| 131 | #include <winsock2.h> |
| 132 | #include <windows.h> |
| 133 | |
| 134 | // Threads: |
| 135 | typedef HANDLE test_platform_thread; |
| 136 | static inline int test_platform_thread_create(test_platform_thread *thread, void *(* func) (void *), void *data) |
| 137 | { |
| 138 | DWORD threadID; |
| 139 | *thread = CreateThread(NULL, // default security attributes |
| 140 | 0, // use default stack size |
| 141 | (LPTHREAD_START_ROUTINE)func, |
| 142 | data, // thread function argument |
| 143 | 0, // use default creation flags |
| 144 | &threadID); // returns thread identifier |
| 145 | return (*thread != NULL); |
| 146 | } |
| 147 | static inline int test_platform_thread_join(test_platform_thread thread, void **retval) |
| 148 | { |
| 149 | return WaitForSingleObject(thread, INFINITE); |
| 150 | } |
| 151 | |
| 152 | // Thread IDs: |
| 153 | typedef DWORD test_platform_thread_id; |
| 154 | static test_platform_thread_id test_platform_get_thread_id() |
| 155 | { |
| 156 | return GetCurrentThreadId(); |
| 157 | } |
| 158 | |
| 159 | // Thread mutex: |
| 160 | typedef CRITICAL_SECTION test_platform_thread_mutex; |
| 161 | static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex) |
| 162 | { |
| 163 | InitializeCriticalSection(pMutex); |
| 164 | } |
| 165 | static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex) |
| 166 | { |
| 167 | EnterCriticalSection(pMutex); |
| 168 | } |
| 169 | static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex) |
| 170 | { |
| 171 | LeaveCriticalSection(pMutex); |
| 172 | } |
| 173 | static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex) |
| 174 | { |
| 175 | DeleteCriticalSection(pMutex); |
| 176 | } |
| 177 | typedef CONDITION_VARIABLE test_platform_thread_cond; |
| 178 | static void test_platform_thread_init_cond(test_platform_thread_cond* pCond) |
| 179 | { |
| 180 | InitializeConditionVariable(pCond); |
| 181 | } |
| 182 | static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex) |
| 183 | { |
| 184 | SleepConditionVariableCS(pCond, pMutex, INFINITE); |
| 185 | } |
| 186 | static void test_platform_thread_cond_broadcast(test_platform_thread_cond* pCond) |
| 187 | { |
| 188 | WakeAllConditionVariable(pCond); |
| 189 | } |
| 190 | #else // defined(_WIN32) |
| 191 | |
| 192 | #error The "test_common.h" file must be modified for this OS. |
| 193 | |
| 194 | // NOTE: In order to support another OS, an #elif needs to be added (above the |
| 195 | // "#else // defined(_WIN32)") for that OS, and OS-specific versions of the |
| 196 | // contents of this file must be created. |
| 197 | |
| 198 | // NOTE: Other OS-specific changes are also needed for this OS. Search for |
| 199 | // files with "WIN32" in it, as a quick way to find files that must be changed. |
| 200 | |
| 201 | #endif // defined(_WIN32) |
| 202 | |
Courtney Goeltzenleuchter | e099abd | 2014-08-12 14:09:50 -0600 | [diff] [blame] | 203 | #endif // TEST_COMMON_H |