-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgl_vk.hpp
More file actions
348 lines (300 loc) · 11.7 KB
/
Copy pathgl_vk.hpp
File metadata and controls
348 lines (300 loc) · 11.7 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Copyright (c) 2023-2025, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2023-2025, NVIDIA CORPORATION.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <nvvk/check_error.hpp>
#include <nvvk/resource_allocator.hpp>
#include <nvgl/extensions.hpp>
#include <unordered_map>
#include <atomic>
#ifdef WIN32
#include <handleapi.h>
#else
#include <unistd.h>
#endif
namespace nvvk {
//------------------------------------------------------------------------------------------------
// Memory object manager for Vulkan-OpenGL interop with reference counting
// Manages OpenGL memory objects with automatic cleanup when reference count reaches zero
class MemoryObjectManager
{
public:
~MemoryObjectManager() { assert(m_importedMemoryObjects.empty() && "Missing to call clear()"); }
// Acquire a memory object for the given VMA allocation
// Returns the OpenGL memory object handle
GLuint acquireMemoryObject(VmaAllocation allocation, nvvk::ResourceAllocatorExport& allocator)
{
VmaAllocationInfo2 allocationInfo2;
vmaGetAllocationInfo2(allocator, allocation, &allocationInfo2);
VkDeviceMemory deviceMemory = allocationInfo2.allocationInfo.deviceMemory;
// Check if already imported
auto it = m_importedMemoryObjects.find(deviceMemory);
if(it != m_importedMemoryObjects.end())
{
// Increment reference count
auto refIt = m_refCounts.find(it->second);
if(refIt != m_refCounts.end())
{
refIt->second.fetch_add(1);
}
return it->second;
}
// Create new memory object
GLuint memoryObject;
glCreateMemoryObjectsEXT(1, &memoryObject);
#ifdef WIN32
HANDLE handle;
NVVK_CHECK(vmaGetMemoryWin32Handle(allocator, allocation, nullptr, &handle));
glImportMemoryWin32HandleEXT(memoryObject, allocationInfo2.blockSize, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, handle);
// Store the handle for later cleanup
m_win32Handles[memoryObject] = handle;
#else
VkMemoryGetFdInfoKHR getInfo = {.sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
.memory = deviceMemory,
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR};
int fd{-1};
NVVK_CHECK(vkGetMemoryFdKHR(allocator.getDevice(), &getInfo, &fd));
glImportMemoryFdEXT(memoryObject, allocationInfo2.blockSize, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
#endif
// Store the mapping and initialize reference count
m_importedMemoryObjects[deviceMemory] = memoryObject;
m_refCounts[memoryObject] = 1;
return memoryObject;
}
// Release a memory object (decrement reference count)
void releaseMemoryObject(GLuint memObject)
{
auto it = m_refCounts.find(memObject);
if(it != m_refCounts.end())
{
uint64_t newCount = it->second.fetch_sub(1) - 1;
if(newCount == 0)
{
// Reference count reached zero, delete the OpenGL memory object
glDeleteMemoryObjectsEXT(1, &memObject);
#ifdef WIN32
// Close the Windows handle if it exists
auto handleIt = m_win32Handles.find(memObject);
if(handleIt != m_win32Handles.end())
{
CloseHandle(handleIt->second);
m_win32Handles.erase(handleIt);
}
#endif
// Remove from both maps
m_refCounts.erase(it);
// Find and remove from importedMemoryObjects map
for(auto memIt = m_importedMemoryObjects.begin(); memIt != m_importedMemoryObjects.end(); ++memIt)
{
if(memIt->second == memObject)
{
m_importedMemoryObjects.erase(memIt);
break;
}
}
}
}
}
// Clear all memory objects (useful for cleanup)
void clear()
{
for(auto& [memoryObject, refCount] : m_refCounts)
{
if(memoryObject != 0)
{
glDeleteMemoryObjectsEXT(1, &memoryObject);
}
}
#ifdef WIN32
// Close all Windows handles
for(auto& [memoryObject, handle] : m_win32Handles)
{
if(handle != nullptr)
{
CloseHandle(handle);
}
}
m_win32Handles.clear();
#endif
m_importedMemoryObjects.clear();
m_refCounts.clear();
}
// Remove a specific memory object by device memory
void remove(VkDeviceMemory deviceMemory)
{
auto it = m_importedMemoryObjects.find(deviceMemory);
if(it != m_importedMemoryObjects.end())
{
releaseMemoryObject(it->second);
}
}
private:
std::unordered_map<VkDeviceMemory, GLuint> m_importedMemoryObjects;
std::unordered_map<GLuint, std::atomic_uint64_t> m_refCounts;
#ifdef WIN32
std::unordered_map<GLuint, HANDLE> m_win32Handles;
#endif
};
// Global manager instance
static MemoryObjectManager g_memoryObjectManager;
// Utility function to clear the global memory object manager
// Call this when cleaning up the application or when you want to free all cached memory objects
inline void clearMemoryObjectManager()
{
g_memoryObjectManager.clear();
}
//------------------------------------------------------------------------------------------------
// Vulkan-OpenGL synchronization semaphores
// Manages paired Vulkan and OpenGL semaphores for cross-API synchronization
// during Vulkan-OpenGL interop operations
//
struct SemaphoresVkGL
{
VkSemaphore vkReady;
VkSemaphore vkComplete;
GLuint glReady;
GLuint glComplete;
void create(VkDevice device)
{
glGenSemaphoresEXT(1, &glReady);
glGenSemaphoresEXT(1, &glComplete);
// Create semaphores
#ifdef WIN32
const auto handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT;
#else
const auto handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
#endif
VkExportSemaphoreCreateInfo esci{.sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, .handleTypes = handleType};
VkSemaphoreCreateInfo sci{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, .pNext = &esci};
NVVK_CHECK(vkCreateSemaphore(device, &sci, nullptr, &vkReady));
NVVK_CHECK(vkCreateSemaphore(device, &sci, nullptr, &vkComplete));
// Import semaphores
#ifdef WIN32
{
HANDLE hglReady{};
VkSemaphoreGetWin32HandleInfoKHR handleInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_WIN32_HANDLE_INFO_KHR,
.semaphore = vkReady,
.handleType = handleType};
NVVK_CHECK(vkGetSemaphoreWin32HandleKHR(device, &handleInfo, &hglReady));
HANDLE hglComplete{};
handleInfo.semaphore = vkComplete;
NVVK_CHECK(vkGetSemaphoreWin32HandleKHR(device, &handleInfo, &hglComplete));
glImportSemaphoreWin32HandleEXT(glReady, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, hglReady);
glImportSemaphoreWin32HandleEXT(glComplete, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, hglComplete);
}
#else
{
int fdReady{};
VkSemaphoreGetFdInfoKHR handleInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR, .semaphore = vkReady, .handleType = handleType};
NVVK_CHECK(vkGetSemaphoreFdKHR(device, &handleInfo, &fdReady));
int fdComplete{};
handleInfo.semaphore = vkComplete;
NVVK_CHECK(vkGetSemaphoreFdKHR(device, &handleInfo, &fdComplete));
glImportSemaphoreFdEXT(glReady, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fdReady);
glImportSemaphoreFdEXT(glComplete, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fdComplete);
}
#endif
}
void destroy(VkDevice device)
{
vkDestroySemaphore(device, vkReady, nullptr);
vkDestroySemaphore(device, vkComplete, nullptr);
glDeleteSemaphoresEXT(1, &glReady);
glDeleteSemaphoresEXT(1, &glComplete);
}
};
//------------------------------------------------------------------------------------------------
// Vulkan-OpenGL shared buffer resource
// Encapsulates a buffer that can be accessed by both Vulkan and OpenGL APIs
// through shared memory allocation and cross-API handle management
//
struct BufferVkGL
{
nvvk::Buffer bufVk; // The allocated buffer
GLuint memoryObject = 0; // OpenGL memory object
GLuint oglId = 0; // OpenGL object ID
void destroy(nvvk::ResourceAllocatorExport& alloc)
{
alloc.destroyBuffer(bufVk);
glDeleteBuffers(1, &oglId);
// Release the memory object reference
if(memoryObject != 0)
{
g_memoryObjectManager.releaseMemoryObject(memoryObject);
memoryObject = 0;
}
}
};
//------------------------------------------------------------------------------------------------
// Vulkan-OpenGL shared 2D texture resource
// Encapsulates a 2D texture that can be accessed by both Vulkan and OpenGL APIs
// through shared memory allocation and cross-API handle management
//
struct Texture2DVkGL
{
nvvk::Image imageExportVk;
uint32_t mipLevels{1};
GLuint memoryObject{0}; // OpenGL memory object
GLuint oglId{0}; // OpenGL object ID
void destroy(nvvk::ResourceAllocatorExport& alloc)
{
glDeleteTextures(1, &oglId);
// Release the memory object reference
if(memoryObject != 0)
{
g_memoryObjectManager.releaseMemoryObject(memoryObject);
memoryObject = 0;
}
alloc.destroyImage(imageExportVk);
}
};
//------------------------------------------------------------------------------------------------
// Creates an OpenGL buffer that shares memory with a Vulkan buffer
// Uses the reference-counted memory object system for efficient cross-API resource sharing
//
inline void createBufferGL(nvvk::ResourceAllocatorExport& allocator, BufferVkGL& bufGl)
{
VkDevice device = allocator.getDevice();
VmaAllocationInfo2 allocationInfo2;
vmaGetAllocationInfo2(allocator, bufGl.bufVk.allocation, &allocationInfo2);
glCreateBuffers(1, &bufGl.oglId);
// Use reference-counted memory object manager
bufGl.memoryObject = g_memoryObjectManager.acquireMemoryObject(bufGl.bufVk.allocation, allocator);
glNamedBufferStorageMemEXT(bufGl.oglId, allocationInfo2.allocationInfo.size, bufGl.memoryObject,
allocationInfo2.allocationInfo.offset);
}
//------------------------------------------------------------------------------------------------
// Creates an OpenGL texture that shares memory with a Vulkan image
// Uses the reference-counted memory object system for efficient cross-API resource sharing
//
inline void createTextureGL(nvvk::ResourceAllocatorExport& allocator, Texture2DVkGL& texGl, int format, int minFilter, int magFilter, int wrap)
{
VkDevice device = allocator.getDevice();
VmaAllocationInfo2 allocationInfo2;
vmaGetAllocationInfo2(allocator, texGl.imageExportVk.allocation, &allocationInfo2);
// Use reference-counted memory object manager
texGl.memoryObject = g_memoryObjectManager.acquireMemoryObject(texGl.imageExportVk.allocation, allocator);
glCreateTextures(GL_TEXTURE_2D, 1, &texGl.oglId);
glTextureStorageMem2DEXT(texGl.oglId, texGl.mipLevels, format, texGl.imageExportVk.extent.width,
texGl.imageExportVk.extent.height, texGl.memoryObject, allocationInfo2.allocationInfo.offset);
glTextureParameteri(texGl.oglId, GL_TEXTURE_MIN_FILTER, minFilter);
glTextureParameteri(texGl.oglId, GL_TEXTURE_MAG_FILTER, magFilter);
glTextureParameteri(texGl.oglId, GL_TEXTURE_WRAP_S, wrap);
glTextureParameteri(texGl.oglId, GL_TEXTURE_WRAP_T, wrap);
}
} // namespace nvvk