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