blob: fe59415dc7a17c7153c56109d89b6532b0bf886f [file] [log] [blame]
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -06001#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 Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060010#include <vulkan.h>
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060011
12#include "gtest/gtest.h"
Courtney Goeltzenleuchter5b804932014-09-01 16:33:55 -060013#include "gtest-1.7.0/include/gtest/gtest.h"
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060014#include "vktestbinding.h"
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060015
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060016#define ASSERT_VK_SUCCESS(err) ASSERT_EQ(VK_SUCCESS, err) << vk_result_string(err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060017
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -060018static inline const char *vk_result_string(VkResult err)
Courtney Goeltzenleuchterb7f50502014-08-12 14:09:50 -060019{
20 switch (err) {
21#define STR(r) case r: return #r
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060022 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 Barbourd1c35722015-04-16 15:59:00 -060031 STR(VK_ERROR_OUT_OF_HOST_MEMORY);
32 STR(VK_ERROR_OUT_OF_DEVICE_MEMORY);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060033 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 Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060047 STR(VK_ERROR_UNSUPPORTED_SHADER_IL_VERSION);
48 STR(VK_ERROR_BAD_SHADER_CODE);
49 STR(VK_ERROR_BAD_PIPELINE_DATA);
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -060050 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 Goeltzenleuchterb7f50502014-08-12 14:09:50 -060059#undef STR
60 default: return "UNKNOWN_RESULT";
61 }
62}
63
Chia-I Wua9a506a2014-12-27 22:04:00 +080064static 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 Stroyan4268d1f2015-07-13 14:45:35 -060070#if defined(__linux__)
71/* Linux-specific common code: */
72
73#include <pthread.h>
74
75// Threads:
76typedef pthread_t test_platform_thread;
77
78static 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}
84static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
85{
86 return pthread_join(thread, retval);
87}
88
89// Thread IDs:
90typedef pthread_t test_platform_thread_id;
91static inline test_platform_thread_id test_platform_get_thread_id()
92{
93 return pthread_self();
94}
95
96// Thread mutex:
97typedef pthread_mutex_t test_platform_thread_mutex;
98static inline void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
99{
100 pthread_mutex_init(pMutex, NULL);
101}
102static inline void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
103{
104 pthread_mutex_lock(pMutex);
105}
106static inline void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
107{
108 pthread_mutex_unlock(pMutex);
109}
110static inline void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
111{
112 pthread_mutex_destroy(pMutex);
113}
114typedef pthread_cond_t test_platform_thread_cond;
115static inline void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
116{
117 pthread_cond_init(pCond, NULL);
118}
119static 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}
123static 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:
134typedef HANDLE test_platform_thread;
135static 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}
146static inline int test_platform_thread_join(test_platform_thread thread, void **retval)
147{
148 return WaitForSingleObject(thread, INFINITE);
149}
150
151// Thread IDs:
152typedef DWORD test_platform_thread_id;
153static test_platform_thread_id test_platform_get_thread_id()
154{
155 return GetCurrentThreadId();
156}
157
158// Thread mutex:
159typedef CRITICAL_SECTION test_platform_thread_mutex;
160static void test_platform_thread_create_mutex(test_platform_thread_mutex* pMutex)
161{
162 InitializeCriticalSection(pMutex);
163}
164static void test_platform_thread_lock_mutex(test_platform_thread_mutex* pMutex)
165{
166 EnterCriticalSection(pMutex);
167}
168static void test_platform_thread_unlock_mutex(test_platform_thread_mutex* pMutex)
169{
170 LeaveCriticalSection(pMutex);
171}
172static void test_platform_thread_delete_mutex(test_platform_thread_mutex* pMutex)
173{
174 DeleteCriticalSection(pMutex);
175}
176typedef CONDITION_VARIABLE test_platform_thread_cond;
177static void test_platform_thread_init_cond(test_platform_thread_cond* pCond)
178{
179 InitializeConditionVariable(pCond);
180}
181static void test_platform_thread_cond_wait(test_platform_thread_cond* pCond, test_platform_thread_mutex* pMutex)
182{
183 SleepConditionVariableCS(pCond, pMutex, INFINITE);
184}
185static 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 Goeltzenleuchterb7f50502014-08-12 14:09:50 -0600202#endif // TEST_COMMON_H