• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dawn_native/vulkan/VulkanError.h"
16 
17 #include <string>
18 
19 namespace dawn_native { namespace vulkan {
20 
VkResultAsString(::VkResult result)21     const char* VkResultAsString(::VkResult result) {
22         // Convert to a int32_t to silence and MSVC warning that the fake errors don't appear in
23         // the original VkResult enum.
24         int32_t code = static_cast<int32_t>(result);
25 
26         switch (code) {
27             case VK_SUCCESS:
28                 return "VK_SUCCESS";
29             case VK_NOT_READY:
30                 return "VK_NOT_READY";
31             case VK_TIMEOUT:
32                 return "VK_TIMEOUT";
33             case VK_EVENT_SET:
34                 return "VK_EVENT_SET";
35             case VK_EVENT_RESET:
36                 return "VK_EVENT_RESET";
37             case VK_INCOMPLETE:
38                 return "VK_INCOMPLETE";
39             case VK_ERROR_OUT_OF_HOST_MEMORY:
40                 return "VK_ERROR_OUT_OF_HOST_MEMORY";
41             case VK_ERROR_OUT_OF_DEVICE_MEMORY:
42                 return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
43             case VK_ERROR_INITIALIZATION_FAILED:
44                 return "VK_ERROR_INITIALIZATION_FAILED";
45             case VK_ERROR_DEVICE_LOST:
46                 return "VK_ERROR_DEVICE_LOST";
47             case VK_ERROR_MEMORY_MAP_FAILED:
48                 return "VK_ERROR_MEMORY_MAP_FAILED";
49             case VK_ERROR_LAYER_NOT_PRESENT:
50                 return "VK_ERROR_LAYER_NOT_PRESENT";
51             case VK_ERROR_EXTENSION_NOT_PRESENT:
52                 return "VK_ERROR_EXTENSION_NOT_PRESENT";
53             case VK_ERROR_FEATURE_NOT_PRESENT:
54                 return "VK_ERROR_FEATURE_NOT_PRESENT";
55             case VK_ERROR_INCOMPATIBLE_DRIVER:
56                 return "VK_ERROR_INCOMPATIBLE_DRIVER";
57             case VK_ERROR_TOO_MANY_OBJECTS:
58                 return "VK_ERROR_TOO_MANY_OBJECTS";
59             case VK_ERROR_FORMAT_NOT_SUPPORTED:
60                 return "VK_ERROR_FORMAT_NOT_SUPPORTED";
61             case VK_ERROR_FRAGMENTED_POOL:
62                 return "VK_ERROR_FRAGMENTED_POOL";
63 
64             case VK_ERROR_SURFACE_LOST_KHR:
65                 return "VK_ERROR_SURFACE_LOST_KHR";
66             case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
67                 return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
68 
69             case VK_FAKE_DEVICE_OOM_FOR_TESTING:
70                 return "VK_FAKE_DEVICE_OOM_FOR_TESTING";
71             case VK_FAKE_ERROR_FOR_TESTING:
72                 return "VK_FAKE_ERROR_FOR_TESTING";
73             default:
74                 return "<Unknown VkResult>";
75         }
76     }
77 
CheckVkSuccessImpl(VkResult result,const char * context)78     MaybeError CheckVkSuccessImpl(VkResult result, const char* context) {
79         if (DAWN_LIKELY(result == VK_SUCCESS)) {
80             return {};
81         }
82 
83         std::string message = std::string(context) + " failed with " + VkResultAsString(result);
84 
85         if (result == VK_ERROR_DEVICE_LOST) {
86             return DAWN_DEVICE_LOST_ERROR(message);
87         } else {
88             return DAWN_INTERNAL_ERROR(message);
89         }
90     }
91 
CheckVkOOMThenSuccessImpl(VkResult result,const char * context)92     MaybeError CheckVkOOMThenSuccessImpl(VkResult result, const char* context) {
93         if (DAWN_LIKELY(result == VK_SUCCESS)) {
94             return {};
95         }
96 
97         std::string message = std::string(context) + " failed with " + VkResultAsString(result);
98 
99         if (result == VK_ERROR_OUT_OF_DEVICE_MEMORY || result == VK_ERROR_OUT_OF_HOST_MEMORY ||
100             result == VK_FAKE_DEVICE_OOM_FOR_TESTING) {
101             return DAWN_OUT_OF_MEMORY_ERROR(message);
102         } else if (result == VK_ERROR_DEVICE_LOST) {
103             return DAWN_DEVICE_LOST_ERROR(message);
104         } else {
105             return DAWN_INTERNAL_ERROR(message);
106         }
107     }
108 
109 }}  // namespace dawn_native::vulkan
110