Path: blob/master/RSDKv5/RSDK/Graphics/Vulkan/VulkanRenderDevice.hpp
1162 views
// TODO: VULKAN_USE_GLFW and other stuff1// i'm not gonna worry about those yet23class RenderDevice : public RenderDeviceBase4{56public:7struct WindowInfo {8union {9struct {10int32 width;11int32 height;12int32 _pad[3];13int32 refresh_rate;14};15GLFWvidmode internal;16} * displays;17};1819struct SwapChainDetails {20VkSurfaceCapabilitiesKHR capabilities;21std::vector<VkSurfaceFormatKHR> formats;22std::vector<VkPresentModeKHR> presentModes;23};2425static WindowInfo displayInfo;2627static bool Init();28static void CopyFrameBuffer();29static void FlipScreen();30static void Release(bool32 isRefresh);3132static void RefreshWindow();33static void GetWindowSize(int32 *width, int32 *height);3435static void SetupImageTexture(int32 width, int32 height, uint8 *imagePixels);36static void SetupVideoTexture_YUV420(int32 width, int32 height, uint8 *yPlane, uint8 *uPlane, uint8 *vPlane, int32 strideY, int32 strideU,37int32 strideV);38static void SetupVideoTexture_YUV422(int32 width, int32 height, uint8 *yPlane, uint8 *uPlane, uint8 *vPlane, int32 strideY, int32 strideU,39int32 strideV);40static void SetupVideoTexture_YUV444(int32 width, int32 height, uint8 *yPlane, uint8 *uPlane, uint8 *vPlane, int32 strideY, int32 strideU,41int32 strideV);4243static bool ProcessEvents();4445static void InitFPSCap();46static bool CheckFPSCap();47static void UpdateFPSCap();4849static bool InitShaders();50static void LoadShader(const char *fileName, bool32 linear);5152static inline void ShowCursor(bool32 shown) { glfwSetInputMode(window, GLFW_CURSOR, shown ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); }53static inline bool GetCursorPos(Vector2 *pos)54{55double cursorX, cursorY;56glfwGetCursorPos(window, &cursorX, &cursorY);57pos->x = (int32)cursorX;58pos->y = (int32)cursorY;59return true;60};61inline static void SetWindowTitle() { glfwSetWindowTitle(window, gameVerInfo.gameTitle); };6263static GLFWwindow *window;6465private:66static bool SetupRendering();67static bool InitVertexBuffer();68static bool InitGraphicsAPI();6970static void GetDisplays();7172static void ProcessKeyEvent(GLFWwindow *, int32 key, int32 scancode, int32 action, int32 mods);73static void ProcessFocusEvent(GLFWwindow *, int32 focused);74static void ProcessMouseEvent(GLFWwindow *, int32 button, int32 action, int32 mods);75static void ProcessJoystickEvent(int32 ID, int32 event);76static void ProcessMaximizeEvent(GLFWwindow *, int32 maximized);7778static void SetLinear(bool32 linear);7980static GLFWwindow *CreateGLFWWindow(void);8182#if VK_DEBUG83static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,84VkDebugUtilsMessageTypeFlagsEXT messageType,85const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData);8687static VkDebugUtilsMessengerEXT debugMessenger;88#endif8990static bool CheckExtensionSupport(VkPhysicalDevice device);9192static SwapChainDetails QuerySwapChainDetails(VkPhysicalDevice device);9394static bool IsDeviceSuitable(VkPhysicalDevice device)95{96if (!CheckExtensionSupport(device))97return false;9899QuerySwapChainDetails(device);100if (currentSwapDetails.formats.empty() || currentSwapDetails.presentModes.empty())101return false;102103uint32_t queueFamilyCount = 0;104vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);105106std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);107vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());108109graphicsIndex = 0xFFFFFFFF;110presentIndex = 0xFFFFFFFF;111112int i = 0;113for (const auto &queueFamily : queueFamilies) {114if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)115graphicsIndex = i;116117VkBool32 presentSupport = false;118vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);119120if (presentSupport)121presentIndex = i;122123if (graphicsIndex != 0xFFFFFFFF && presentIndex != 0xFFFFFFFF)124return true;125126++i;127}128129return false;130};131132static bool CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);133static void ReleaseShaderPipelines();134135static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {136VkPhysicalDeviceMemoryProperties memProperties;137vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);138139for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {140if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {141return i;142}143}144145return (uint32_t)-1;146};147148static VkInstance instance;149static VkPhysicalDevice physicalDevice;150static VkDevice device;151static VkSurfaceKHR surface;152153static SwapChainDetails currentSwapDetails;154static VkSwapchainKHR swapChain;155static VkFormat swapChainImageFormat;156static VkExtent2D swapChainExtent;157158static std::vector<VkImage> swapChainImages;159static std::vector<VkImageView> swapChainImageViews;160static std::vector<VkFramebuffer> swapChainFramebuffers;161162struct ShaderConstants;163164static VkBuffer uniformBuffer;165static VkDeviceMemory uniformBufferMemory;166static ShaderConstants *uniformMap;167168static VkDescriptorPool descriptorPool;169static VkDescriptorSet descriptorSet[SCREEN_COUNT];170171static VkRenderPass renderPass;172static VkDescriptorSetLayout setLayout;173static VkPipelineLayout pipelineLayout;174static VkGraphicsPipelineCreateInfo basePipeline;175176static VkBuffer vertexBuffer;177static VkDeviceMemory vertexBufferMemory;178179static VkCommandPool commandPool;180static VkCommandBuffer commandBuffer;181182static VkQueue graphicsQueue;183static VkQueue presentQueue;184static uint32 graphicsIndex;185static uint32 presentIndex;186187static VkViewport viewport;188189static VkSemaphore imageAvailableSemaphore;190static VkSemaphore renderFinishedSemaphore;191static VkFence inFlightFence;192193static int32 monitorIndex;194195struct Texture {196VkImage image;197VkDeviceMemory memory;198VkSubresourceLayout layout;199VkImageView view;200void* map;201};202203static VkSampler samplerPoint;204static VkSampler samplerLinear;205206static VkDescriptorImageInfo imageInfo;207208static Texture imageTexture;209static Texture screenTextures[SCREEN_COUNT];210211static double lastFrame;212static double targetFreq;213};214215struct ShaderEntry : public ShaderEntryBase {216VkPipeline shaderPipeline;217};218219220