-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompute.cuh
285 lines (241 loc) · 12.2 KB
/
compute.cuh
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/
#pragma once
#include <cuda_runtime.h>
#include <cstdio>
#include <chrono>
#include <iostream>
#include "gl_vkpp.hpp"
#include "nvh/fileoperations.hpp"
#include "nvvk/commands_vk.hpp"
#include "nvvk/images_vk.hpp"
#include "nvvk/shaders_vk.hpp"
#ifdef NDEBUG
#define CheckCUDA(call) call;
#define cudaCheckError() ;
#else
#define CheckCUDA(call) \
{ \
const cudaError_t err = call; \
if(err != cudaSuccess) \
{ \
std::cerr << "CudaDebugCall() failed at: " << __FILE__ << ":" << __LINE__ << "; "; \
std::cerr << "code: " << err << "; description: " << cudaGetErrorString(err) << std::endl; \
exit(EXIT_FAILURE); \
} \
}
//Macro for checking cuda errors following a cuda launch or api call
#define cudaCheckError() \
{ \
cudaError_t e = cudaGetLastError(); \
if(e != cudaSuccess) \
{ \
printf("Cuda failure %s:%d: '%s'\n", __FILE__, __LINE__, cudaGetErrorString(e)); \
exit(0); \
} \
}
#endif
inline VkExternalSemaphoreHandleTypeFlagBits getDefaultSemaphoreHandleType()
{
#ifdef _WIN64
return VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_BIT;
#else
return VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT;
#endif
}
inline VkExternalMemoryHandleTypeFlagBits getDefaultMemHandleType()
{
#ifdef _WIN64
return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
#else
return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
#endif
}
#ifdef _WIN64
using HandleType_T = HANDLE;
#else
using HandleType_T = int;
#endif
inline HandleType_T getMemHandle(const VkDevice& device, const VkDeviceMemory& memory)
{
#ifdef _WIN64
HandleType_T handle = 0;
VkMemoryGetWin32HandleInfoKHR memInfo{VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR};
memInfo.memory = memory;
memInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT;
VkResult result = vkGetMemoryWin32HandleKHR(device, &memInfo, &handle);
#else
HandleType_T handle = -1;
VkMemoryGetFdInfoKHR memInfo{VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR};
memInfo.memory = memory;
memInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
VkResult result = vkGetMemoryFdKHR(device, &memInfo, &handle);
#endif
if(result != VK_SUCCESS)
{
assert(result == VK_SUCCESS);
}
return handle;
}
class ComputeImageVk
{
public:
ComputeImageVk() = default;
void setup(const VkDevice& device, const VkPhysicalDevice& physicalDevice)
{
m_device = device;
m_physicalDevice = physicalDevice;
m_alloc.init(device, physicalDevice);
}
VkDevice m_device;
nvvkpp::Texture2DVkGL m_textureTarget;
VkPhysicalDevice m_physicalDevice;
nvvk::ExportResourceAllocatorDedicated m_alloc;
cudaArray_t m_cudaTextureArray;
cudaExternalMemory_t m_cudaImageMemory;
cudaMipmappedArray_t m_cudaImageMipmappedArray;
cudaSurfaceObject_t m_texSurfObj;
struct Semaphores
{
VkSemaphore vkCudaToGl;
VkSemaphore vkGlToCuda;
GLuint glReady;
GLuint glComplete;
cudaExternalSemaphore_t cudaReady;
cudaExternalSemaphore_t cudaComplete;
} m_semaphores;
void destroy()
{
CheckCUDA(cudaDestroySurfaceObject(m_texSurfObj));
m_textureTarget.destroy(m_alloc);
vkDestroySemaphore(m_device, m_semaphores.vkCudaToGl, nullptr);
vkDestroySemaphore(m_device, m_semaphores.vkGlToCuda, nullptr);
}
void prepare(int width, int height)
{
createSemaphores();
m_textureTarget =
prepareTextureTarget(VK_IMAGE_LAYOUT_GENERAL, {uint32_t(width), uint32_t(height), 1}, VK_FORMAT_R8G8B8A8_UNORM);
}
void createSemaphores()
{
glGenSemaphoresEXT(1, &m_semaphores.glReady);
glGenSemaphoresEXT(1, &m_semaphores.glComplete);
// Create semaphores
const auto handleType = getDefaultSemaphoreHandleType();
VkExportSemaphoreCreateInfo esci{VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO};
esci.handleTypes = handleType;
VkSemaphoreCreateInfo sci{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO};
sci.pNext = &esci;
vkCreateSemaphore(m_device, &sci, nullptr, &m_semaphores.vkCudaToGl);
vkCreateSemaphore(m_device, &sci, nullptr, &m_semaphores.vkGlToCuda);
// Import vk semaphores to GL and CUDA
#ifdef WIN32
{
HANDLE hglReady{NULL};
HANDLE hglComplete{NULL};
VkSemaphoreGetWin32HandleInfoKHR handleInfo{VK_STRUCTURE_TYPE_SEMAPHORE_GET_WIN32_HANDLE_INFO_KHR};
handleInfo.handleType = handleType;
handleInfo.semaphore = m_semaphores.vkCudaToGl;
vkGetSemaphoreWin32HandleKHR(m_device, &handleInfo, &hglReady);
handleInfo.semaphore = m_semaphores.vkGlToCuda;
vkGetSemaphoreWin32HandleKHR(m_device, &handleInfo, &hglComplete);
glImportSemaphoreWin32HandleEXT(m_semaphores.glReady, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, hglReady);
glImportSemaphoreWin32HandleEXT(m_semaphores.glComplete, GL_HANDLE_TYPE_OPAQUE_WIN32_EXT, hglComplete);
cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc = {};
externalSemaphoreHandleDesc.type = cudaExternalSemaphoreHandleTypeOpaqueWin32;
externalSemaphoreHandleDesc.flags = 0;
externalSemaphoreHandleDesc.handle.win32.handle = hglReady;
CheckCUDA(cudaImportExternalSemaphore(&m_semaphores.cudaComplete, &externalSemaphoreHandleDesc));
externalSemaphoreHandleDesc.handle.win32.handle = hglComplete;
CheckCUDA(cudaImportExternalSemaphore(&m_semaphores.cudaReady, &externalSemaphoreHandleDesc));
}
#else
// it seems to be a bug that after using a handle with gl or cuda import "consumes" the handle and it had to be queried again from vk
{
HandleType_T fdReady = 0;
HandleType_T fdComplete = 0;
VkSemaphoreGetFdInfoKHR handleInfo{VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR};
handleInfo.handleType = handleType;
handleInfo.semaphore = m_semaphores.vkCudaToGl;
vkGetSemaphoreFdKHR(m_device, &handleInfo, &fdReady);
handleInfo.semaphore = m_semaphores.vkGlToCuda;
vkGetSemaphoreFdKHR(m_device, &handleInfo, &fdComplete);
cudaExternalSemaphoreHandleDesc externalSemaphoreHandleDesc = {.type = cudaExternalSemaphoreHandleTypeOpaqueFd, .flags = 0};
externalSemaphoreHandleDesc.handle.fd = fdReady;
CheckCUDA(cudaImportExternalSemaphore(&m_semaphores.cudaComplete, &externalSemaphoreHandleDesc));
externalSemaphoreHandleDesc.handle.fd = fdComplete;
CheckCUDA(cudaImportExternalSemaphore(&m_semaphores.cudaReady, &externalSemaphoreHandleDesc));
}
{
HandleType_T fdReady = 0;
HandleType_T fdComplete = 0;
VkSemaphoreGetFdInfoKHR handleInfo{VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR};
handleInfo.handleType = handleType;
handleInfo.semaphore = m_semaphores.vkCudaToGl;
vkGetSemaphoreFdKHR(m_device, &handleInfo, &fdReady);
handleInfo.semaphore = m_semaphores.vkGlToCuda;
vkGetSemaphoreFdKHR(m_device, &handleInfo, &fdComplete);
glImportSemaphoreFdEXT(m_semaphores.glReady, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fdReady);
glImportSemaphoreFdEXT(m_semaphores.glComplete, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fdComplete);
}
#endif
}
nvvkpp::Texture2DVkGL prepareTextureTarget(VkImageLayout targetLayout, const VkExtent3D& extent, VkFormat format)
{
VkExtent2D imgSize = VkExtent2D{extent.width, extent.height};
VkImageUsageFlags usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
VkImageCreateInfo imageCreateInfo = nvvk::makeImage2DCreateInfo(imgSize, format, usage);
VkExternalMemoryImageCreateInfo extMemInfo{VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO};
extMemInfo.handleTypes = getDefaultMemHandleType();
imageCreateInfo.pNext = &extMemInfo;
nvvkpp::Texture2DVkGL texture;
nvvk::Image image = m_alloc.createImage(imageCreateInfo);
VkImageViewCreateInfo ivInfo = nvvk::makeImageViewCreateInfo(image.image, imageCreateInfo);
// Create the texture from the image and adding a default sampler
VkSamplerCreateInfo samplerCreateInfo{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
texture.texVk = m_alloc.createTexture(image, ivInfo, samplerCreateInfo);
texture.imgSize = imgSize;
auto mem_info = m_alloc.getMemoryAllocator()->getMemoryInfo(image.memHandle);
auto mem_handle = getMemHandle(m_alloc.getDevice(), mem_info.memory);
cudaExternalMemoryHandleDesc cudaExtMemHandleDesc = {};
#ifdef _WIN64
cudaExtMemHandleDesc.type = cudaExternalMemoryHandleTypeOpaqueWin32;
cudaExtMemHandleDesc.handle.win32.handle = mem_handle;
#else
cudaExtMemHandleDesc.type = cudaExternalMemoryHandleTypeOpaqueFd;
cudaExtMemHandleDesc.handle.fd = mem_handle;
#endif
cudaExtMemHandleDesc.size = mem_info.size;
CheckCUDA(cudaImportExternalMemory(&m_cudaImageMemory, &cudaExtMemHandleDesc));
cudaExternalMemoryMipmappedArrayDesc mipmappedArrayDesc = {};
mipmappedArrayDesc.extent = make_cudaExtent(texture.imgSize.width, texture.imgSize.height, 0); // depth must be zero for 2D textures and not one! Seems to result in different image alignments.
mipmappedArrayDesc.formatDesc = cudaCreateChannelDesc(8, 8, 8, 8, cudaChannelFormatKindUnsigned);
mipmappedArrayDesc.numLevels = 1;
mipmappedArrayDesc.offset = 0;
mipmappedArrayDesc.flags = cudaArraySurfaceLoadStore;
CheckCUDA(cudaExternalMemoryGetMappedMipmappedArray(&m_cudaImageMipmappedArray, m_cudaImageMemory, &mipmappedArrayDesc));
CheckCUDA(cudaGetMipmappedArrayLevel(&m_cudaTextureArray, m_cudaImageMipmappedArray, 0));
cudaResourceDesc resDescr = {};
resDescr.resType = cudaResourceTypeArray;
resDescr.res.array.array = m_cudaTextureArray;
// Create surface object
CheckCUDA(cudaCreateSurfaceObject(&m_texSurfObj, &resDescr));
return texture;
}
void compute(VkExtent2D& size);
};