source: opengl-game/vulkan-game.hpp@ aa00bf2

feature/imgui-sdl points-test
Last change on this file since aa00bf2 was f97c5e7, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Create an initGraphicsPipelines() function and templatize and generalize the createUniformBuffers() functionso it can be used to create a uniform buffer of any type

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[99d44b2]1#ifndef _VULKAN_GAME_H
2#define _VULKAN_GAME_H
[e8ebc76]3
[771b33a]4#include <glm/glm.hpp>
[15104a8]5#include <glm/gtc/matrix_transform.hpp>
[771b33a]6
[0df3c9a]7#include "game-gui-sdl.hpp"
[7d2b0b9]8#include "graphics-pipeline_vulkan.hpp"
[0df3c9a]9
[b794178]10#include "vulkan-utils.hpp"
11
[15104a8]12using namespace glm;
13
[2e77b3f]14#ifdef NDEBUG
15 const bool ENABLE_VALIDATION_LAYERS = false;
16#else
17 const bool ENABLE_VALIDATION_LAYERS = true;
18#endif
19
[b8777b7]20struct ModelVertex {
[15104a8]21 vec3 pos;
22 vec3 color;
23 vec2 texCoord;
[771b33a]24};
25
26struct OverlayVertex {
[15104a8]27 vec3 pos;
28 vec2 texCoord;
29};
30
[8e02b6b]31struct UBO_MvpMat {
[15104a8]32 alignas(16) mat4 model;
33 alignas(16) mat4 view;
34 alignas(16) mat4 proj;
[771b33a]35};
36
[99d44b2]37class VulkanGame {
[e8ebc76]38 public:
[34bdf3a]39 VulkanGame(int maxFramesInFlight);
[99d44b2]40 ~VulkanGame();
[0df3c9a]41
[b6e60b4]42 void run(int width, int height, unsigned char guiFlags);
[0df3c9a]43
44 private:
[34bdf3a]45 const int MAX_FRAMES_IN_FLIGHT;
46
[5ab1b20]47 const float NEAR_CLIP = 0.1f;
48 const float FAR_CLIP = 100.0f;
49 const float FOV_ANGLE = 67.0f;
50
[15104a8]51 vec3 cam_pos;
52
[8e02b6b]53 UBO_MvpMat modelMvpMats;
[15104a8]54
[0df3c9a]55 GameGui* gui;
[c559904]56
[b8777b7]57 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
58 GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
[7d2b0b9]59
[c559904]60 SDL_version sdlVersion;
[b794178]61 SDL_Window* window = nullptr;
62 SDL_Renderer* renderer = nullptr;
63
64 SDL_Texture* uiOverlay = nullptr;
[c1d9b2a]65
66 VkInstance instance;
67 VkDebugUtilsMessengerEXT debugMessenger;
[fe5c3ba]68 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
[90a424f]69 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
[c1c2021]70 VkDevice device;
71
72 VkQueue graphicsQueue;
73 VkQueue presentQueue;
[0df3c9a]74
[502bd0b]75 VkSwapchainKHR swapChain;
76 vector<VkImage> swapChainImages;
77 VkFormat swapChainImageFormat;
[603b5bc]78 VkExtent2D swapChainExtent;
[f94eea9]79 vector<VkImageView> swapChainImageViews;
[603b5bc]80 vector<VkFramebuffer> swapChainFramebuffers;
[fa9fa1c]81
[6fc24c7]82 VkRenderPass renderPass;
[fa9fa1c]83 VkCommandPool commandPool;
[603b5bc]84 vector<VkCommandBuffer> commandBuffers;
[502bd0b]85
[603b5bc]86 VulkanImage depthImage;
[b794178]87
88 VkSampler textureSampler;
89
90 // These are currently to store the MVP matrix
91 // I should figure out if it makes sense to use them for other uniforms in the future
92 // If not, I should rename them to better indicate their purpose.
93 vector<VkBuffer> uniformBuffers;
94 vector<VkDeviceMemory> uniformBuffersMemory;
95
[f97c5e7]96 vector<VkDescriptorBufferInfo> uniformBufferInfoList;
97
[b794178]98 VulkanImage floorTextureImage;
99 VkDescriptorImageInfo floorTextureImageDescriptor;
100
101 VulkanImage sdlOverlayImage;
102 VkDescriptorImageInfo sdlOverlayImageDescriptor;
103
[1f25a71]104 TTF_Font* font;
105 SDL_Texture* fontSDLTexture;
106
107 SDL_Texture* imageSDLTexture;
108
[34bdf3a]109 vector<VkSemaphore> imageAvailableSemaphores;
110 vector<VkSemaphore> renderFinishedSemaphores;
111 vector<VkFence> inFlightFences;
112
[87c8f1a]113 size_t currentFrame;
114
115 bool framebufferResized;
[0e09340]116
[b6e60b4]117 bool initWindow(int width, int height, unsigned char guiFlags);
[0df3c9a]118 void initVulkan();
[f97c5e7]119 void initGraphicsPipelines();
[15104a8]120 void initMatrices();
[0df3c9a]121 void mainLoop();
[8e02b6b]122 void updateScene(uint32_t currentImage);
[a0c5f28]123 void renderUI();
124 void renderScene();
[0df3c9a]125 void cleanup();
[c1d9b2a]126
127 void createVulkanInstance(const vector<const char*> &validationLayers);
128 void setupDebugMessenger();
129 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
[90a424f]130 void createVulkanSurface();
[fe5c3ba]131 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
[fa9fa1c]132 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
[c1c2021]133 void createLogicalDevice(
134 const vector<const char*> validationLayers,
135 const vector<const char*>& deviceExtensions);
[502bd0b]136 void createSwapChain();
[f94eea9]137 void createImageViews();
[6fc24c7]138 void createRenderPass();
139 VkFormat findDepthFormat();
[fa9fa1c]140 void createCommandPool();
[603b5bc]141 void createImageResources();
142
[b794178]143 void createTextureSampler();
[603b5bc]144 void createFramebuffers();
145 void createCommandBuffers();
[34bdf3a]146 void createSyncObjects();
[f94eea9]147
[f97c5e7]148 template<class UniformType>
149 void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
150 vector<VkDescriptorBufferInfo>& bufferInfoList);
151
[d2d9286]152 void recreateSwapChain();
153
[c1c2021]154 void cleanupSwapChain();
[c1d9b2a]155
156 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
157 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
158 VkDebugUtilsMessageTypeFlagsEXT messageType,
159 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
160 void* pUserData);
[e8ebc76]161};
162
[f97c5e7]163template<class UniformType>
164void VulkanGame::createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
165 vector<VkDescriptorBufferInfo>& bufferInfoList) {
166 buffers.resize(swapChainImages.size());
167 buffersMemory.resize(swapChainImages.size());
168 bufferInfoList.resize(swapChainImages.size());
169
170 for (size_t i = 0; i < swapChainImages.size(); i++) {
171 VulkanUtils::createBuffer(device, physicalDevice, sizeof(UniformType), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
172 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
173 buffers[i], buffersMemory[i]);
174
175 bufferInfoList[i].buffer = buffers[i];
176 bufferInfoList[i].offset = 0;
177 bufferInfoList[i].range = sizeof(UniformType);
178 }
179}
180
[99d44b2]181#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.