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(deUint32));
30 DE_STATIC_ASSERT(sizeof(vk::VkResult)		== sizeof(deUint32));
31 DE_STATIC_ASSERT(sizeof(vk::VkDevice)		== sizeof(void*));
32 DE_STATIC_ASSERT(sizeof(vk::VkBuffer)		== sizeof(deUint64));
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	||
40 		   result == VK_ERROR_OUT_OF_HOST_MEMORY;
41 }
42 
Error(VkResult error,const char * message,const char * expr,const char * file,int line)43 Error::Error (VkResult error, const char* message, const char* expr, const char* file, int line)
44 	: tcu::TestError	(message, expr, file, line)
45 	, m_error			(error)
46 {
47 }
48 
Error(VkResult error,const std::string & message)49 Error::Error (VkResult error, const std::string& message)
50 	: tcu::TestError	(message)
51 	, m_error			(error)
52 {
53 }
54 
~Error(void)55 Error::~Error (void) throw()
56 {
57 }
58 
OutOfMemoryError(VkResult error,const char * message,const char * expr,const char * file,int line)59 OutOfMemoryError::OutOfMemoryError (VkResult error, const char* message, const char* expr, const char* file, int line)
60 	: tcu::ResourceError(message, expr, file, line)
61 	, m_error			(error)
62 {
63 	DE_ASSERT(isOutOfMemoryError(error));
64 }
65 
OutOfMemoryError(VkResult error,const std::string & message)66 OutOfMemoryError::OutOfMemoryError (VkResult error, const std::string& message)
67 	: tcu::ResourceError(message)
68 	, m_error			(error)
69 {
70 	DE_ASSERT(isOutOfMemoryError(error));
71 }
72 
~OutOfMemoryError(void)73 OutOfMemoryError::~OutOfMemoryError (void) throw()
74 {
75 }
76 
checkResult(VkResult result,const char * msg,const char * file,int line)77 void checkResult (VkResult result, const char* msg, const char* file, int line)
78 {
79 	if (result != VK_SUCCESS)
80 	{
81 		std::ostringstream msgStr;
82 		if (msg)
83 			msgStr << msg << ": ";
84 
85 		msgStr << getResultStr(result);
86 
87 		if (isOutOfMemoryError(result))
88 			throw OutOfMemoryError(result, msgStr.str().c_str(), DE_NULL, file, line);
89 		else
90 			throw Error(result, msgStr.str().c_str(), DE_NULL, file, line);
91 	}
92 }
93 
checkWsiResult(VkResult result,const char * msg,const char * file,int line)94 void checkWsiResult (VkResult result, const char* msg, const char* file, int line)
95 {
96 	if (result == VK_SUBOPTIMAL_KHR)
97 		return;
98 
99 	if (result == VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT)
100 		return;
101 
102 	checkResult(result, msg, file, line);
103 }
104 
105 } // vk
106