Jetson Linux API Reference

39.2 Release
NvVulkanRenderer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2023-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: BSD-3-Clause
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
38 #ifndef __NV_VULKAN_RENDERER_H__
39 #define __NV_VULKAN_RENDERER_H__
40 
41 #include <vector>
42 #include <limits>
43 #include <algorithm>
44 
45 #define VK_USE_PLATFORM_XLIB_KHR
46 #include <vulkan/vulkan.h>
47 #include "NvElement.h"
48 
49 #include <X11/Xlib.h>
50 #include <vulkan/vulkan_xlib.h>
51 #include <X11/Xutil.h>
75  bool found;
76  uint32_t typeIndex;
77 };
78 
80  int graphicsFamily = -1;
81  int computeFamily = -1;
82  int presentFamily = -1;
83 };
84 
86  VkSurfaceCapabilitiesKHR capabilities;
87  std::vector<VkSurfaceFormatKHR> formats;
88  std::vector<VkPresentModeKHR> presentModes;
89 };
90 
92 {
93 private:
94  Display * m_XDisplay;
96  Window m_XWindow;
100  VkInstance m_instance;
101  VkDebugUtilsMessengerEXT m_debugMessenger;
102  QueueFamilyIndices m_queueFamilyIndices;
103  VkPhysicalDevice m_physicalDevice;
104  VkDevice m_device;
105  VkQueue m_graphicsQueue;
106  VkQueue m_presentQueue;
107  VkSurfaceKHR m_surface;
108 
109  VkSwapchainKHR m_swapChain;
110  std::vector<VkImage> m_swapChainImages;
111  VkFormat m_swapChainImageFormat;
112  VkExtent2D m_swapChainExtent;
113 
114  std::vector<VkDeviceMemory> m_vkImageMemory;
115  int m_vkImageMemoryIndex, m_oldImageIndex;
116  VkCommandPool m_commandPool;
117  VkCommandBuffer m_commandBuffer;
118 
119  VkSemaphore m_imageAvailableSemaphore;
120  VkSemaphore m_renderFinishedSemaphore;
121  VkFence m_inFlightFence;
122 
123  const std::vector<const char*> m_validationLayers = {
124 #ifdef USE_VALIDATION
125  "VK_LAYER_KHRONOS_validation",
126 #endif
127  };
128 
129  const std::vector<const char*> m_instanceExtensions{
130 #ifdef USE_VALIDATION
131  VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
132 #endif
133  VK_KHR_SURFACE_EXTENSION_NAME,
134  VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
135  };
136 
137  const std::vector<const char*> m_deviceExtensions{
138  VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
139  VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
140  VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
141  VK_KHR_SWAPCHAIN_EXTENSION_NAME,
142  };
143 
144  template <typename T>
145  uint32_t ui32Size(const T& container)
146  {
147  return static_cast<uint32_t>(container.size());
148  }
149  VkImageTiling m_imageTiling = VK_IMAGE_TILING_OPTIMAL;
150  VkFormat m_imageFormat = VK_FORMAT_R8G8B8A8_UNORM;
151 
155  NvVulkanRenderer(const char *name, uint32_t width, uint32_t height,
156  uint32_t x_offset, uint32_t y_offset);
157 
158  static const NvElementProfiler::ProfilerField valid_fields =
162 
163 public:
181  static NvVulkanRenderer *createVulkanRenderer(const char *name, uint32_t width,
182  uint32_t height, uint32_t x_offset,
183  uint32_t y_offset);
185 
186  void initVulkan();
187  void createInstance();
188  void createSurface();
189  void getPhysicalDevice();
190  void getQueueFamilies();
191  void createDevice();
192  void createSwapChain();
193  void createCommandPool();
194  void createCommandBuffer();
195  void createSyncObjects();
196  void displayFrame(VkImage image);
197  void recordCommandBuffer(VkImage image, uint32_t imageIndex);
198  void createVkImageFromFd(int fd);
199  void render(int fd);
200  void setSize(uint32_t width, uint32_t height);
201  PFN_vkVoidFunction getInstanceFunction(VkInstance instance, const char* name);
202 
203  uint32_t m_windowWidth;
204  uint32_t m_windowHeight;
205 };
207 #endif
SwapChainSupportDetails::formats
std::vector< VkSurfaceFormatKHR > formats
Definition: NvVulkanRenderer.h:87
NvVulkanRenderer::m_windowHeight
uint32_t m_windowHeight
Definition: NvVulkanRenderer.h:204
NvVulkanRenderer::setSize
void setSize(uint32_t width, uint32_t height)
NvElementProfiler::PROFILER_FIELD_LATE_UNITS
static const ProfilerField PROFILER_FIELD_LATE_UNITS
Definition: NvElementProfiler.h:82
NvVulkanRenderer::createCommandBuffer
void createCommandBuffer()
NvVulkanRenderer::render
void render(int fd)
SwapChainSupportDetails::presentModes
std::vector< VkPresentModeKHR > presentModes
Definition: NvVulkanRenderer.h:88
NvElementProfiler::ProfilerField
int ProfilerField
Definition: NvElementProfiler.h:79
NvVulkanRenderer::getPhysicalDevice
void getPhysicalDevice()
NvVulkanRenderer::createInstance
void createInstance()
MemoryTypeResult::typeIndex
uint32_t typeIndex
Definition: NvVulkanRenderer.h:76
QueueFamilyIndices
Definition: NvVulkanRenderer.h:79
NvVulkanRenderer::displayFrame
void displayFrame(VkImage image)
NvVulkanRenderer
Definition: NvVulkanRenderer.h:91
MemoryTypeResult::found
bool found
Definition: NvVulkanRenderer.h:75
NvVulkanRenderer::createSyncObjects
void createSyncObjects()
NvVulkanRenderer::getQueueFamilies
void getQueueFamilies()
SwapChainSupportDetails::capabilities
VkSurfaceCapabilitiesKHR capabilities
Definition: NvVulkanRenderer.h:86
NvVulkanRenderer::createVkImageFromFd
void createVkImageFromFd(int fd)
NvVulkanRenderer::createCommandPool
void createCommandPool()
SwapChainSupportDetails
Definition: NvVulkanRenderer.h:85
NvVulkanRenderer::getInstanceFunction
PFN_vkVoidFunction getInstanceFunction(VkInstance instance, const char *name)
NvVulkanRenderer::recordCommandBuffer
void recordCommandBuffer(VkImage image, uint32_t imageIndex)
NvVulkanRenderer::m_windowWidth
uint32_t m_windowWidth
Definition: NvVulkanRenderer.h:203
NvVulkanRenderer::initVulkan
void initVulkan()
NvElementProfiler::PROFILER_FIELD_FPS
static const ProfilerField PROFILER_FIELD_FPS
Definition: NvElementProfiler.h:84
QueueFamilyIndices::presentFamily
int presentFamily
Definition: NvVulkanRenderer.h:82
NvVulkanRenderer::createDevice
void createDevice()
NvElement
Every element has a unique name that can be used for identifying the element in debug logs.
Definition: NvElement.h:63
NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS
static const ProfilerField PROFILER_FIELD_TOTAL_UNITS
Definition: NvElementProfiler.h:81
NvVulkanRenderer::createSwapChain
void createSwapChain()
MemoryTypeResult
NvVulkanRenderer is a helper class for rendering using Vulkan The renderer requires the file descript...
Definition: NvVulkanRenderer.h:74
NvVulkanRenderer::createSurface
void createSurface()
NvVulkanRenderer::~NvVulkanRenderer
~NvVulkanRenderer()
NvElement.h
QueueFamilyIndices::computeFamily
int computeFamily
Definition: NvVulkanRenderer.h:81
NvVulkanRenderer::createVulkanRenderer
static NvVulkanRenderer * createVulkanRenderer(const char *name, uint32_t width, uint32_t height, uint32_t x_offset, uint32_t y_offset)
Creates a new Vulkan-based renderer named name.
QueueFamilyIndices::graphicsFamily
int graphicsFamily
Definition: NvVulkanRenderer.h:80