-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayKHR.cpp
More file actions
189 lines (151 loc) · 5.63 KB
/
Copy pathDisplayKHR.cpp
File metadata and controls
189 lines (151 loc) · 5.63 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "DisplayKHR.h"
#include <new>
#include <vector>
#include "DisplayModeKHR.h"
#include "PhysicalDevice.h"
#include "Internal/Error.h"
void gpuDisplayKHRRetain(GPUDisplayKHR display) {
if (display != nullptr) {
++display->refCount;
}
}
void gpuDisplayKHRDrop(GPUDisplayKHR display) {
if (display == nullptr) {
return;
}
if (display->refCount > 0) {
--display->refCount;
}
if (display->refCount == 0) {
gpuPhysicalDeviceDrop(display->physicalDevice);
delete display;
}
}
extern "C" {
GPU_API uint32_t GPU_CALL gpuPhysicalDeviceGetDisplayCountKHR(GPUPhysicalDevice physicalDevice) {
gpu::internal::clearLastError();
if (physicalDevice == nullptr || physicalDevice->destroyRequested) {
gpu::internal::setLastError("Physical device must not be null or released.");
return 0;
}
uint32_t count = 0;
const VkResult result = gpuGetPhysicalDeviceDisplayPropertiesKHR(physicalDevice->handle, &count, nullptr);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to get display count.");
return 0;
}
return count;
}
GPU_API VkResult GPU_CALL gpuPhysicalDeviceGetDisplayKHR(
GPUPhysicalDevice physicalDevice,
uint32_t index,
GPUDisplayKHR* display) {
gpu::internal::clearLastError();
if (physicalDevice == nullptr || display == nullptr) {
gpu::internal::setLastError("Physical device and display output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
if (physicalDevice->destroyRequested) {
gpu::internal::setLastError("Physical device is being released.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*display = nullptr;
uint32_t count = 0;
VkResult result = gpuGetPhysicalDeviceDisplayPropertiesKHR(physicalDevice->handle, &count, nullptr);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to get display count.");
return result;
}
if (index >= count) {
gpu::internal::setLastError("Display index is out of range.");
return VK_ERROR_INITIALIZATION_FAILED;
}
std::vector<VkDisplayPropertiesKHR> properties(count);
result = gpuGetPhysicalDeviceDisplayPropertiesKHR(physicalDevice->handle, &count, properties.data());
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to enumerate displays.");
return result;
}
GPUDisplayKHR wrapper = new (std::nothrow) GPUDisplayKHR_T{physicalDevice, properties[index].display, 1, false};
if (wrapper == nullptr) {
gpu::internal::setLastError("Failed to allocate DisplayKHR.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
gpuPhysicalDeviceRetain(physicalDevice);
*display = wrapper;
return VK_SUCCESS;
}
GPU_API VkDisplayKHR GPU_CALL gpuDisplayKHRGetVkHandle(GPUDisplayKHR display) {
return display != nullptr && !display->destroyRequested ? display->handle : VK_NULL_HANDLE;
}
GPU_API GPUPhysicalDevice GPU_CALL gpuDisplayKHRGetPhysicalDevice(GPUDisplayKHR display) {
return display != nullptr && !display->destroyRequested ? display->physicalDevice : nullptr;
}
GPU_API uint32_t GPU_CALL gpuDisplayKHRGetModeCountKHR(GPUDisplayKHR display) {
gpu::internal::clearLastError();
if (display == nullptr || display->destroyRequested) {
gpu::internal::setLastError("Display must not be null or released.");
return 0;
}
uint32_t count = 0;
const VkResult result =
gpuGetDisplayModePropertiesKHR(display->physicalDevice->handle, display->handle, &count, nullptr);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to get display mode count.");
return 0;
}
return count;
}
GPU_API VkResult GPU_CALL gpuDisplayKHRGetModeKHR(
GPUDisplayKHR display,
uint32_t index,
GPUDisplayModeKHR* displayMode) {
gpu::internal::clearLastError();
if (display == nullptr || displayMode == nullptr) {
gpu::internal::setLastError("Display and display mode output must not be null.");
return VK_ERROR_INITIALIZATION_FAILED;
}
if (display->destroyRequested) {
gpu::internal::setLastError("Display is being released.");
return VK_ERROR_INITIALIZATION_FAILED;
}
*displayMode = nullptr;
uint32_t count = 0;
VkResult result =
gpuGetDisplayModePropertiesKHR(display->physicalDevice->handle, display->handle, &count, nullptr);
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to get display mode count.");
return result;
}
if (index >= count) {
gpu::internal::setLastError("Display mode index is out of range.");
return VK_ERROR_INITIALIZATION_FAILED;
}
std::vector<VkDisplayModePropertiesKHR> properties(count);
result = gpuGetDisplayModePropertiesKHR(
display->physicalDevice->handle,
display->handle,
&count,
properties.data());
if (result != VK_SUCCESS) {
gpu::internal::setLastError("Failed to enumerate display modes.");
return result;
}
GPUDisplayModeKHR wrapper =
new (std::nothrow) GPUDisplayModeKHR_T{display, properties[index].displayMode, 1, false};
if (wrapper == nullptr) {
gpu::internal::setLastError("Failed to allocate DisplayModeKHR.");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
gpuDisplayKHRRetain(display);
*displayMode = wrapper;
return VK_SUCCESS;
}
GPU_API void GPU_CALL gpuDisplayKHRRelease(GPUDisplayKHR display) {
if (display == nullptr || display->destroyRequested) {
return;
}
display->destroyRequested = true;
gpuDisplayKHRDrop(display);
}
} // extern "C"