-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.cpp
More file actions
133 lines (104 loc) · 3.62 KB
/
Copy pathDevice.cpp
File metadata and controls
133 lines (104 loc) · 3.62 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
#include "Device.h"
#include <new>
#include "Internal/Error.h"
#include "PhysicalDevice.h"
#include "Queue.h"
void gpuDeviceRetain(GPUDevice device) {
if (device != nullptr) {
++device->refCount;
}
}
void gpuDeviceDrop(GPUDevice device) {
if (device == nullptr) {
return;
}
if (device->refCount > 0) {
--device->refCount;
}
if (device->refCount == 0) {
if (device->handle != VK_NULL_HANDLE) {
gpuDestroyDevice(device->handle, device->allocator);
}
gpuPhysicalDeviceDrop(device->physicalDevice);
delete device;
}
}
extern "C" {
GPU_API VkResult GPU_CALL gpuDeviceCreate(
GPUPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* createInfo,
const VkAllocationCallbacks* allocator,
GPUDevice* device) {
gpu::internal::clearLastError();
if (physicalDevice == nullptr || createInfo == nullptr || device == nullptr) {
gpu::internal::setLastError("Physical device, create info, and device output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
if (physicalDevice->destroyRequested) {
gpu::internal::setLastError("Physical device is being destroyed.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*device = nullptr;
VkDevice handle = VK_NULL_HANDLE;
const VkResult result = gpuCreateDevice(physicalDevice->handle, createInfo, allocator, &handle);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to create Vulkan device.");
return result;
}
GPUDevice wrapper = new (std::nothrow) GPUDevice_T{physicalDevice, handle, allocator, 1, false};
if (wrapper == nullptr) {
gpuDestroyDevice(handle, allocator);
gpu::internal::setLastError("Failed to allocate Device.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
gpuPhysicalDeviceRetain(physicalDevice);
*device = wrapper;
return VK_SUCCESS;
}
GPU_API void GPU_CALL gpuDeviceDestroy(GPUDevice device) {
if (device == nullptr) {
return;
}
if (device->destroyRequested) {
return;
}
device->destroyRequested = true;
gpuDeviceDrop(device);
}
GPU_API VkDevice GPU_CALL gpuDeviceGetVkHandle(GPUDevice device) {
return device != nullptr && !device->destroyRequested ? device->handle : VK_NULL_HANDLE;
}
GPU_API GPUPhysicalDevice GPU_CALL gpuDeviceGetPhysicalDevice(GPUDevice device) {
return device != nullptr && !device->destroyRequested ? device->physicalDevice : nullptr;
}
GPU_API VkResult GPU_CALL gpuDeviceGetQueue(
GPUDevice device,
uint32_t familyIndex,
uint32_t queueIndex,
GPUQueue* queue) {
gpu::internal::clearLastError();
if (device == nullptr || queue == nullptr) {
gpu::internal::setLastError("Device and queue output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
if (device->destroyRequested) {
gpu::internal::setLastError("Device is being destroyed.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*queue = nullptr;
VkQueue handle = VK_NULL_HANDLE;
gpuGetDeviceQueue(device->handle, familyIndex, queueIndex, &handle);
if (handle == VK_NULL_HANDLE) {
gpu::internal::setLastError("Failed to get Vulkan queue.");
return VK_ERROR_INITIALIZATION_FAILED;
}
GPUQueue wrapper = new (std::nothrow) GPUQueue_T{device, handle, familyIndex, queueIndex, 1, false};
if (wrapper == nullptr) {
gpu::internal::setLastError("Failed to allocate Queue.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
gpuDeviceRetain(device);
*queue = wrapper;
return VK_SUCCESS;
}
} // extern "C"