File size: 11,468 Bytes
ea55f45 | 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 | /*
* Copyright 2018-2024 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
/**
* This file provides wrapper classes over standard objects from the Vulkan and
* CUDA APIs, so that these objects can be used in a manner similar to the
* instances of the base/derived classes for the decoder and the encoder.
*/
#include <vulkan/vulkan.h>
#include <cuda.h>
#include <vector>
#include <array>
#include "../Utils/NvCodecUtils.h"
#ifdef _WIN32
#include <Windows.h>
#include <vulkan/vulkan_win32.h>
#endif
/**
* @brief Wrapper class around VkInstance
*/
class Vkinst
{
VkInstance m_instance;
VkDebugReportCallbackEXT m_callback;
std::vector<VkPhysicalDevice> m_physicalDevices;
public:
/*
* The 'layers' parameter is a list of choices for validation layers.
* The first entry in this list that is supported by the implementation is
* selected and used when creating the VkInstance.
*/
Vkinst(
const std::vector<const char*>& layers = std::vector<const char*>(),
const std::vector<const char*>& extensions = std::vector<const char*>()
);
~Vkinst();
const std::vector<VkPhysicalDevice>& getPhysicalDevices(void) const
{
return m_physicalDevices;
}
VkInstance get(void) const
{
return m_instance;
}
};
class Vkcmdbuffer;
class Vksema;
/**
* @brief Wrapper class around VkQueue
*/
class Vkque
{
VkQueue m_queue;
VkResult submit(
const std::vector<VkSemaphore>& waitSemaphores = std::vector<VkSemaphore>(),
const std::vector<VkCommandBuffer>& commandBuffers = std::vector<VkCommandBuffer>(),
const std::vector<VkSemaphore>& signalSemaphores = std::vector<VkSemaphore>()
);
public:
Vkque(const VkQueue queue)
{
m_queue = queue;
}
~Vkque(){}
/**
* @brief Wrappers around vkQueueSubmit()
*/
VkResult submit(const Vkcmdbuffer *commandBuffer);
VkResult submit(
const Vkcmdbuffer *commandBuffer,
const Vksema *signalSemaphore
);
VkResult submit(
const Vksema *waitSemaphore,
const Vkcmdbuffer *commandBuffer,
const Vksema *signalSemaphore
);
/**
* @brief Wrapper around vkQueueWaitIdle()
*/
VkResult waitIdle(void);
VkQueue get() const
{
return m_queue;
}
};
/**
* @brief Wrapper class around VkDevice
*/
class Vkdev
{
VkDevice m_device;
uint32_t m_transferQueueFamilyIndex;
VkQueue m_transferQueue;
VkPhysicalDeviceMemoryProperties m_deviceMemProps;
std::array<uint8_t, VK_UUID_SIZE> m_deviceUUID;
public:
Vkdev(
const Vkinst *instance,
const std::vector<const char*>& deviceExtensions = std::vector<const char*>()
);
~Vkdev();
uint32_t getTransferQueueFamilyIndex(void) const
{
return m_transferQueueFamilyIndex;
}
const VkPhysicalDeviceMemoryProperties& getMemoryProperties(void) const
{
return m_deviceMemProps;
}
const Vkque getTransferQueue(void) const
{
const static Vkque transferQueue(m_transferQueue);
return transferQueue;
}
const std::array<uint8_t, VK_UUID_SIZE> getUUID(void) const
{
return m_deviceUUID;
}
VkDevice get() const
{
return m_device;
}
};
/**
* @brief Wrapper class around VkCommandPool
*/
class Vkcmdpool
{
VkCommandPool m_commandPool;
VkDevice m_device;
public:
Vkcmdpool(const Vkdev *device);
~Vkcmdpool();
VkCommandPool get() const
{
return m_commandPool;
}
};
class Vkdevicemem;
/**
* @brief Wrapper class around VkBuffer
*/
class Vkbuf
{
VkBuffer m_buffer;
VkDevice m_device;
VkDeviceSize m_size;
VkDeviceSize m_alignment;
uint32_t m_memoryTypeBits;
public:
Vkbuf(
const Vkdev *device, VkDeviceSize bufferSize,
VkBufferUsageFlags bufferUsage, bool exportCapable = false
);
~Vkbuf();
/**
* @brief Wrapper around vkBindBufferMemory()
*/
void bind(const Vkdevicemem *deviceMem, VkDeviceSize offset = 0);
VkDeviceSize getSize(void)
{
return m_size;
}
uint32_t getMemoryTypeBits(void)
{
return m_memoryTypeBits;
}
VkBuffer get() const
{
return m_buffer;
}
};
/**
* @brief Wrapper class around VkImage (specifically, 2D images)
*/
class Vkimg2d
{
VkImage m_image;
VkDevice m_device;
VkExtent2D m_extent;
VkDeviceSize m_size;
VkDeviceSize m_alignment;
uint32_t m_memoryTypeBits;
public:
Vkimg2d(
const Vkdev *device, VkExtent2D extent, VkImageUsageFlags imageUsage,
bool exportCapable = false
);
~Vkimg2d();
/**
* @brief Wrapper around vkBindImageMemory()
*/
void bind(const Vkdevicemem *deviceMem, VkDeviceSize offset = 0);
VkDeviceSize getSize(void) const
{
return m_size;
}
VkDeviceSize getAlignment(void) const
{
return m_alignment;
}
VkExtent2D getExtent(void) const
{
return m_extent;
}
uint32_t getMemoryTypeBits(void)
{
return m_memoryTypeBits;
}
VkImage get() const
{
return m_image;
}
};
/**
* @brief Wrapper class around VkDeviceMemory
*/
class Vkdevicemem
{
VkDeviceMemory m_deviceMemory;
VkDevice m_device;
VkDeviceSize m_size;
public:
Vkdevicemem(
const Vkdev *device, VkDeviceSize size, uint32_t memoryTypeBits,
VkMemoryPropertyFlags memoryProperties, bool exportCapable = false
);
~Vkdevicemem();
/**
* @brief Wrapper around vkMapMemory()
*/
VkResult map(
void **p, VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0
);
/**
* @brief Wrapper around vkUnmapMemory()
*/
void unmap(void);
void *getExportHandle(void) const;
const VkDeviceMemory getMemory(void) const
{
return m_deviceMemory;
}
VkDeviceSize getSize(void) const
{
return m_size;
}
VkDeviceMemory get() const
{
return m_deviceMemory;
}
};
class Vkimgmembarrier;
/**
* @brief Wrapper class around VkCommandBuffer
*/
class Vkcmdbuffer
{
VkCommandBuffer m_commandBuffer;
VkDevice m_device;
VkCommandPool m_commandPool;
public:
Vkcmdbuffer(const Vkdev *device, const Vkcmdpool *commandPool);
~Vkcmdbuffer();
/**
* @brief Wrappers around vk{Begin,End}CommandBuffer()
*/
VkResult begin(void);
VkResult end(void);
/**
* @brief Wrapper around vkCmdFillBuffer()
*/
void fillBuffer(const Vkbuf *buffer, uint32_t data,
VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
/**
* @brief Wrapper around vkCmdCopyBuffer()
*/
void copyBuffer(const Vkbuf *dstBuffer, const Vkbuf *srcBuffer,
VkDeviceSize size = VK_WHOLE_SIZE);
/**
* @brief Insert a pipeline barrier (wrapper around vkCmdPipelineBarrier())
*/
void pipelineBarrier(
const Vkimgmembarrier *imageBarrier,
VkImageLayout oldLayout,
VkImageLayout newLayout,
VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask,
VkAccessFlags srcAccessMask,
VkAccessFlags dstAccessMask
);
/**
* @brief Wrapper around vkCmdClearColorImage()
*/
void clearImage(const Vkimg2d *image, VkClearColorValue value);
/**
* @brief Wrapper around vkCmdCopyImageToBuffer()
*/
void copyImageToBuffer(const Vkbuf *buffer, const Vkimg2d *image);
/**
* @brief Wrapper around vkCmdCopyBufferToImage()
*/
void copyBufferToImage(const Vkimg2d *image, const Vkbuf *buffer);
VkCommandBuffer get() const
{
return m_commandBuffer;
}
};
/**
* @brief Wrapper class around VkSemaphore
*/
class Vksema
{
VkSemaphore m_semaphore;
VkDevice m_device;
public:
Vksema(const Vkdev *device, bool exportCapable = false);
~Vksema();
void *getExportHandle(void) const;
VkSemaphore get() const
{
return m_semaphore;
}
};
/**
* @brief Wrapper class around VkImageMemoryBarrier
*/
class Vkimgmembarrier
{
VkImageMemoryBarrier m_barrier;
public:
Vkimgmembarrier(const Vkimg2d *image);
~Vkimgmembarrier(){};
VkImageMemoryBarrier get() const
{
return m_barrier;
}
};
/**
* @brief Wrapper class around CUcontext
* This class can be used for creating CUDA contexts on the device referenced
* by the provided Vkdev instance. The methods provided in this class are
* wrappers over CUDA API calls and take care of pushing/popping the CUDA
* context as required.
*/
class Cudactx
{
CUcontext m_context;
public:
Cudactx(const Vkdev *device);
~Cudactx(){};
CUresult memcpyDtoH(void *p, CUdeviceptr dptr, size_t size);
CUresult memcpy2D(void *p, CUarray array, uint32_t width, uint32_t height);
CUcontext get() const
{
return m_context;
}
};
/**
* @brief Wrapper class around CUdeviceptr
* This class can be used for mapping a CUdeviceptr allocation on the device
* memory object referred to by deviceMem. deviceMem should have been created
* with a device memory object backing a VkBuffer. This mapping makes use of
* Vulkan's export of device memory followed by import of this external memory
* by CUDA.
*/
class Cudabuffer
{
CUdeviceptr m_deviceptr;
CUexternalMemory m_extMem;
public:
Cudabuffer(const Vkdevicemem *deviceMem);
~Cudabuffer();
CUdeviceptr get() const
{
return m_deviceptr;
}
};
/**
* @brief Wrapper class around CUarray
* This class can be used for mapping a 2D CUDA array on the device memory
* object referred to by deviceMem. deviceMem should have been created with a
* device memory object backing a 2D VkImage. This mapping makes use of Vulkan's
* export of device memory followed by import of this external memory by CUDA.
*/
class Cudaimage
{
CUarray m_array;
CUmipmappedArray m_mipmapArray;
CUexternalMemory m_extMem;
public:
Cudaimage(const Vkimg2d *image, const Vkdevicemem *deviceMem);
~Cudaimage();
CUarray get() const
{
return m_array;
}
};
/**
* @brief Wrapper class around CUexternalSemaphore
* This class can be used for creating a CUDA external semaphore from a
* VkSemaphore (referred to by the provided Vksema instance). This makes use of
* Vulkan's export of semaphores followed by their import by CUDA.
*/
class Cudasema
{
CUexternalSemaphore m_extSema;
public:
Cudasema(const Vksema *semaphore);
~Cudasema();
CUresult wait(void);
CUresult signal(void);
};
|