-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstance.cpp
More file actions
154 lines (121 loc) · 4.24 KB
/
Copy pathInstance.cpp
File metadata and controls
154 lines (121 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "Instance.h"
#include <new>
#include <vector>
#include "Internal/Error.h"
#include "PhysicalDevice.h"
void gpuInstanceRetain(GPUInstance instance) {
if (instance != nullptr) {
++instance->refCount;
}
}
void gpuInstanceDrop(GPUInstance instance) {
if (instance == nullptr) {
return;
}
if (instance->refCount > 0) {
--instance->refCount;
}
if (instance->refCount == 0) {
if (instance->handle != VK_NULL_HANDLE) {
gpuDestroyInstance(instance->handle, instance->allocator);
}
delete instance;
}
}
extern "C" {
GPU_API VkResult GPU_CALL gpuInstanceCreate(
const VkInstanceCreateInfo* createInfo,
const VkAllocationCallbacks* allocator,
GPUInstance* instance) {
gpu::internal::clearLastError();
if (createInfo == nullptr || instance == nullptr) {
gpu::internal::setLastError("Create info and instance output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*instance = nullptr;
VkInstance handle = VK_NULL_HANDLE;
const VkResult result = gpuCreateInstance(createInfo, allocator, &handle);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to create Vulkan instance.");
return result;
}
GPUInstance wrapper = new (std::nothrow) GPUInstance_T{handle, allocator, 1, false};
if (wrapper == nullptr) {
gpuDestroyInstance(handle, allocator);
gpu::internal::setLastError("Failed to allocate Instance.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
*instance = wrapper;
return VK_SUCCESS;
}
GPU_API void GPU_CALL gpuInstanceDestroy(GPUInstance instance) {
if (instance == nullptr) {
return;
}
if (instance->destroyRequested) {
return;
}
instance->destroyRequested = true;
gpuInstanceDrop(instance);
}
GPU_API VkInstance GPU_CALL gpuInstanceGetVkHandle(GPUInstance instance) {
return instance != nullptr && !instance->destroyRequested ? instance->handle : VK_NULL_HANDLE;
}
GPU_API uint32_t GPU_CALL gpuInstanceGetPhysicalDeviceCount(GPUInstance instance) {
gpu::internal::clearLastError();
if (instance == nullptr) {
gpu::internal::setLastError("Instance must not be null.");
return 0;
}
if (instance->destroyRequested) {
gpu::internal::setLastError("Instance is being destroyed.");
return 0;
}
uint32_t count = 0;
const VkResult result = gpuEnumeratePhysicalDevices(instance->handle, &count, nullptr);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to enumerate physical devices.");
return 0;
}
return count;
}
GPU_API VkResult GPU_CALL gpuInstanceGetPhysicalDevice(
GPUInstance instance,
uint32_t index,
GPUPhysicalDevice* physicalDevice) {
gpu::internal::clearLastError();
if (instance == nullptr || physicalDevice == nullptr) {
gpu::internal::setLastError("Instance and physical device output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
if (instance->destroyRequested) {
gpu::internal::setLastError("Instance is being destroyed.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*physicalDevice = nullptr;
uint32_t count = 0;
VkResult result = gpuEnumeratePhysicalDevices(instance->handle, &count, nullptr);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to enumerate physical devices.");
return result;
}
if (index >= count) {
gpu::internal::setLastError("Physical device index is out of range.");
return VK_ERROR_INITIALIZATION_FAILED;
}
std::vector<VkPhysicalDevice> devices(count);
result = gpuEnumeratePhysicalDevices(instance->handle, &count, devices.data());
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to fetch physical devices.");
return result;
}
GPUPhysicalDevice wrapper = new (std::nothrow) GPUPhysicalDevice_T{instance, devices[index], 1, false};
if (wrapper == nullptr) {
gpu::internal::setLastError("Failed to allocate PhysicalDevice.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
gpuInstanceRetain(instance);
*physicalDevice = wrapper;
return VK_SUCCESS;
}
} // extern "C"