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