1 /*-------------------------------------------------------------------------
2 * Vulkan CTS Framework
3 * --------------------
4 *
5 * Copyright (c) 2015 Google Inc.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Vulkan utilites.
22 *//*--------------------------------------------------------------------*/
23
24 #include "vkDefs.hpp"
25 #include "vkStrUtil.hpp"
26
27 #include <sstream>
28
29 DE_STATIC_ASSERT(sizeof(vk::VkImageType) == sizeof(uint32_t));
30 DE_STATIC_ASSERT(sizeof(vk::VkResult) == sizeof(uint32_t));
31 DE_STATIC_ASSERT(sizeof(vk::VkDevice) == sizeof(void *));
32 DE_STATIC_ASSERT(sizeof(vk::VkBuffer) == sizeof(uint64_t));
33
34 namespace vk
35 {
36
isOutOfMemoryError(VkResult result)37 static bool isOutOfMemoryError(VkResult result)
38 {
39 return result == VK_ERROR_OUT_OF_DEVICE_MEMORY || result == VK_ERROR_OUT_OF_HOST_MEMORY;
40 }
41
Error(VkResult error,const char * message,const char * expr,const char * file,int line,qpTestResult result)42 Error::Error(VkResult error, const char *message, const char *expr, const char *file, int line, qpTestResult result)
43 : tcu::TestError(message, expr, file, line, result)
44 , m_error(error)
45 {
46 }
47
Error(VkResult error,const char * message,const char * expr,const char * file,int line)48 Error::Error(VkResult error, const char *message, const char *expr, const char *file, int line)
49 : tcu::TestError(message, expr, file, line)
50 , m_error(error)
51 {
52 }
53
Error(VkResult error,const std::string & message)54 Error::Error(VkResult error, const std::string &message) : tcu::TestError(message), m_error(error)
55 {
56 }
57
~Error(void)58 Error::~Error(void) throw()
59 {
60 }
61
NotSupportedError(VkResult error,const char * message,const char * expr,const char * file,int line)62 NotSupportedError::NotSupportedError(VkResult error, const char *message, const char *expr, const char *file, int line)
63 : tcu::NotSupportedError(message, expr, file, line)
64 , m_error(error)
65 {
66 }
67
NotSupportedError(VkResult error,const std::string & message)68 NotSupportedError::NotSupportedError(VkResult error, const std::string &message)
69 : tcu::NotSupportedError(message)
70 , m_error(error)
71 {
72 }
73
~NotSupportedError(void)74 NotSupportedError::~NotSupportedError(void) throw()
75 {
76 }
77
OutOfMemoryError(VkResult error,const char * message,const char * expr,const char * file,int line)78 OutOfMemoryError::OutOfMemoryError(VkResult error, const char *message, const char *expr, const char *file, int line)
79 : tcu::ResourceError(message, expr, file, line)
80 , m_error(error)
81 {
82 DE_ASSERT(isOutOfMemoryError(error));
83 }
84
OutOfMemoryError(VkResult error,const std::string & message)85 OutOfMemoryError::OutOfMemoryError(VkResult error, const std::string &message)
86 : tcu::ResourceError(message)
87 , m_error(error)
88 {
89 DE_ASSERT(isOutOfMemoryError(error));
90 }
91
~OutOfMemoryError(void)92 OutOfMemoryError::~OutOfMemoryError(void) throw()
93 {
94 }
95
96 template <typename ERROR>
checkResult(VkResult result,const char * msg,const char * file,int line)97 static void checkResult(VkResult result, const char *msg, const char *file, int line)
98 {
99 if (result != VK_SUCCESS)
100 {
101 std::ostringstream msgStr;
102 if (msg)
103 msgStr << msg << ": ";
104
105 msgStr << getResultStr(result);
106
107 if (isOutOfMemoryError(result))
108 throw OutOfMemoryError(result, msgStr.str().c_str(), DE_NULL, file, line);
109 else if (result == VK_ERROR_DEVICE_LOST)
110 throw Error(result, msgStr.str().c_str(), DE_NULL, file, line, QP_TEST_RESULT_DEVICE_LOST);
111 else
112 throw ERROR(result, msgStr.str().c_str(), DE_NULL, file, line);
113 }
114 }
115
checkResult(VkResult result,const char * msg,const char * file,int line)116 void checkResult(VkResult result, const char *msg, const char *file, int line)
117 {
118 checkResult<Error>(result, msg, file, line);
119 }
120
checkResultSupported(VkResult result,const char * msg,const char * file,int line)121 void checkResultSupported(VkResult result, const char *msg, const char *file, int line)
122 {
123 checkResult<NotSupportedError>(result, msg, file, line);
124 }
125
checkWsiResult(VkResult result,const char * msg,const char * file,int line)126 void checkWsiResult(VkResult result, const char *msg, const char *file, int line)
127 {
128 if (result == VK_SUBOPTIMAL_KHR)
129 return;
130 #ifndef CTS_USES_VULKANSC
131 if (result == VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
132 return;
133 #endif // CTS_USES_VULKANSC
134
135 checkResult(result, msg, file, line);
136 }
137
138 } // namespace vk
139