123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #ifndef __NV_VULKAN_RENDERER_H__
- #define __NV_VULKAN_RENDERER_H__
- #include <vector>
- #include <limits>
- #include <algorithm>
- #define VK_USE_PLATFORM_XLIB_KHR
- #include <vulkan/vulkan.h>
- #include "NvElement.h"
- #include <X11/Xlib.h>
- #include <vulkan/vulkan_xlib.h>
- #include <X11/Xutil.h>
- struct MemoryTypeResult {
- bool found;
- uint32_t typeIndex;
- };
- struct QueueFamilyIndices {
- int graphicsFamily = -1;
- int computeFamily = -1;
- int presentFamily = -1;
- };
- struct SwapChainSupportDetails {
- VkSurfaceCapabilitiesKHR capabilities;
- std::vector<VkSurfaceFormatKHR> formats;
- std::vector<VkPresentModeKHR> presentModes;
- };
- class NvVulkanRenderer:public NvElement
- {
- private:
- Display * m_XDisplay;
- Window m_XWindow;
- VkInstance m_instance;
- VkDebugUtilsMessengerEXT m_debugMessenger;
- QueueFamilyIndices m_queueFamilyIndices;
- VkPhysicalDevice m_physicalDevice;
- VkDevice m_device;
- VkQueue m_graphicsQueue;
- VkQueue m_presentQueue;
- VkSurfaceKHR m_surface;
- VkSwapchainKHR m_swapChain;
- std::vector<VkImage> m_swapChainImages;
- VkFormat m_swapChainImageFormat;
- VkExtent2D m_swapChainExtent;
- std::vector<VkDeviceMemory> m_vkImageMemory;
- int m_vkImageMemoryIndex, m_oldImageIndex;
- VkCommandPool m_commandPool;
- VkCommandBuffer m_commandBuffer;
- VkSemaphore m_imageAvailableSemaphore;
- VkSemaphore m_renderFinishedSemaphore;
- VkFence m_inFlightFence;
- const std::vector<const char*> m_validationLayers = {
- #ifdef USE_VALIDATION
- "VK_LAYER_KHRONOS_validation",
- #endif
- };
- const std::vector<const char*> m_instanceExtensions{
- #ifdef USE_VALIDATION
- VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
- #endif
- VK_KHR_SURFACE_EXTENSION_NAME,
- VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
- };
- const std::vector<const char*> m_deviceExtensions{
- VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
- VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
- VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
- VK_KHR_SWAPCHAIN_EXTENSION_NAME,
- };
- template <typename T>
- uint32_t ui32Size(const T& container)
- {
- return static_cast<uint32_t>(container.size());
- }
- VkImageTiling m_imageTiling = VK_IMAGE_TILING_OPTIMAL;
- VkFormat m_imageFormat = VK_FORMAT_R8G8B8A8_UNORM;
-
- NvVulkanRenderer(const char *name, uint32_t width, uint32_t height,
- uint32_t x_offset, uint32_t y_offset);
- static const NvElementProfiler::ProfilerField valid_fields =
- NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS |
- NvElementProfiler::PROFILER_FIELD_FPS |
- NvElementProfiler::PROFILER_FIELD_LATE_UNITS;
- public:
-
- static NvVulkanRenderer *createVulkanRenderer(const char *name, uint32_t width,
- uint32_t height, uint32_t x_offset,
- uint32_t y_offset);
- ~NvVulkanRenderer();
- void initVulkan();
- void createInstance();
- void createSurface();
- void getPhysicalDevice();
- void getQueueFamilies();
- void createDevice();
- void createSwapChain();
- void createCommandPool();
- void createCommandBuffer();
- void createSyncObjects();
- void displayFrame(VkImage image);
- void recordCommandBuffer(VkImage image, uint32_t imageIndex);
- void createVkImageFromFd(int fd);
- void render(int fd);
- void setSize(uint32_t width, uint32_t height);
- PFN_vkVoidFunction getInstanceFunction(VkInstance instance, const char* name);
- uint32_t m_windowWidth;
- uint32_t m_windowHeight;
- };
- #endif
|