Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/vulkan/util/vk_device.c
7132 views
1
/*
2
* Copyright © 2020 Intel Corporation
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
* IN THE SOFTWARE.
22
*/
23
24
#include "vk_device.h"
25
26
#include "vk_common_entrypoints.h"
27
#include "vk_instance.h"
28
#include "vk_physical_device.h"
29
#include "util/hash_table.h"
30
#include "util/ralloc.h"
31
32
VkResult
33
vk_device_init(struct vk_device *device,
34
struct vk_physical_device *physical_device,
35
const struct vk_device_dispatch_table *dispatch_table,
36
const VkDeviceCreateInfo *pCreateInfo,
37
const VkAllocationCallbacks *alloc)
38
{
39
memset(device, 0, sizeof(*device));
40
vk_object_base_init(device, &device->base, VK_OBJECT_TYPE_DEVICE);
41
if (alloc != NULL)
42
device->alloc = *alloc;
43
else
44
device->alloc = physical_device->instance->alloc;
45
46
device->physical = physical_device;
47
48
device->dispatch_table = *dispatch_table;
49
50
/* Add common entrypoints without overwriting driver-provided ones. */
51
vk_device_dispatch_table_from_entrypoints(
52
&device->dispatch_table, &vk_common_device_entrypoints, false);
53
54
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
55
int idx;
56
for (idx = 0; idx < VK_DEVICE_EXTENSION_COUNT; idx++) {
57
if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
58
vk_device_extensions[idx].extensionName) == 0)
59
break;
60
}
61
62
if (idx >= VK_DEVICE_EXTENSION_COUNT)
63
return VK_ERROR_EXTENSION_NOT_PRESENT;
64
65
if (!physical_device->supported_extensions.extensions[idx])
66
return VK_ERROR_EXTENSION_NOT_PRESENT;
67
68
#ifdef ANDROID
69
if (!vk_android_allowed_device_extensions.extensions[idx])
70
return VK_ERROR_EXTENSION_NOT_PRESENT;
71
#endif
72
73
device->enabled_extensions.extensions[idx] = true;
74
}
75
76
p_atomic_set(&device->private_data_next_index, 0);
77
78
#ifdef ANDROID
79
mtx_init(&device->swapchain_private_mtx, mtx_plain);
80
device->swapchain_private = NULL;
81
#endif /* ANDROID */
82
83
return VK_SUCCESS;
84
}
85
86
void
87
vk_device_finish(UNUSED struct vk_device *device)
88
{
89
#ifdef ANDROID
90
if (device->swapchain_private) {
91
hash_table_foreach(device->swapchain_private, entry)
92
util_sparse_array_finish(entry->data);
93
ralloc_free(device->swapchain_private);
94
}
95
#endif /* ANDROID */
96
97
vk_object_base_finish(&device->base);
98
}
99
100
PFN_vkVoidFunction
101
vk_device_get_proc_addr(const struct vk_device *device,
102
const char *name)
103
{
104
if (device == NULL || name == NULL)
105
return NULL;
106
107
struct vk_instance *instance = device->physical->instance;
108
return vk_device_dispatch_table_get_if_supported(&device->dispatch_table,
109
name,
110
instance->app_info.api_version,
111
&instance->enabled_extensions,
112
&device->enabled_extensions);
113
}
114
115
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL
116
vk_common_GetDeviceProcAddr(VkDevice _device,
117
const char *pName)
118
{
119
VK_FROM_HANDLE(vk_device, device, _device);
120
return vk_device_get_proc_addr(device, pName);
121
}
122
123
VKAPI_ATTR void VKAPI_CALL
124
vk_common_GetDeviceQueue(VkDevice _device,
125
uint32_t queueFamilyIndex,
126
uint32_t queueIndex,
127
VkQueue *pQueue)
128
{
129
VK_FROM_HANDLE(vk_device, device, _device);
130
131
const VkDeviceQueueInfo2 info = {
132
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2,
133
.pNext = NULL,
134
/* flags = 0 because (Vulkan spec 1.2.170 - vkGetDeviceQueue):
135
*
136
* "vkGetDeviceQueue must only be used to get queues that were
137
* created with the flags parameter of VkDeviceQueueCreateInfo set
138
* to zero. To get queues that were created with a non-zero flags
139
* parameter use vkGetDeviceQueue2."
140
*/
141
.flags = 0,
142
.queueFamilyIndex = queueFamilyIndex,
143
.queueIndex = queueIndex,
144
};
145
146
device->dispatch_table.GetDeviceQueue2(_device, &info, pQueue);
147
}
148
149
VKAPI_ATTR void VKAPI_CALL
150
vk_common_GetBufferMemoryRequirements(VkDevice _device,
151
VkBuffer buffer,
152
VkMemoryRequirements *pMemoryRequirements)
153
{
154
VK_FROM_HANDLE(vk_device, device, _device);
155
156
VkBufferMemoryRequirementsInfo2 info = {
157
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
158
.buffer = buffer,
159
};
160
VkMemoryRequirements2 reqs = {
161
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
162
};
163
device->dispatch_table.GetBufferMemoryRequirements2(_device, &info, &reqs);
164
165
*pMemoryRequirements = reqs.memoryRequirements;
166
}
167
168
VKAPI_ATTR VkResult VKAPI_CALL
169
vk_common_BindBufferMemory(VkDevice _device,
170
VkBuffer buffer,
171
VkDeviceMemory memory,
172
VkDeviceSize memoryOffset)
173
{
174
VK_FROM_HANDLE(vk_device, device, _device);
175
176
VkBindBufferMemoryInfo bind = {
177
.sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
178
.buffer = buffer,
179
.memory = memory,
180
.memoryOffset = memoryOffset,
181
};
182
183
return device->dispatch_table.BindBufferMemory2(_device, 1, &bind);
184
}
185
186
VKAPI_ATTR void VKAPI_CALL
187
vk_common_GetImageMemoryRequirements(VkDevice _device,
188
VkImage image,
189
VkMemoryRequirements *pMemoryRequirements)
190
{
191
VK_FROM_HANDLE(vk_device, device, _device);
192
193
VkImageMemoryRequirementsInfo2 info = {
194
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
195
.image = image,
196
};
197
VkMemoryRequirements2 reqs = {
198
.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
199
};
200
device->dispatch_table.GetImageMemoryRequirements2(_device, &info, &reqs);
201
202
*pMemoryRequirements = reqs.memoryRequirements;
203
}
204
205
VKAPI_ATTR VkResult VKAPI_CALL
206
vk_common_BindImageMemory(VkDevice _device,
207
VkImage image,
208
VkDeviceMemory memory,
209
VkDeviceSize memoryOffset)
210
{
211
VK_FROM_HANDLE(vk_device, device, _device);
212
213
VkBindImageMemoryInfo bind = {
214
.sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO,
215
.image = image,
216
.memory = memory,
217
.memoryOffset = memoryOffset,
218
};
219
220
return device->dispatch_table.BindImageMemory2(_device, 1, &bind);
221
}
222
223