1 | #ifndef _VULKAN_GAME_H
|
---|
2 | #define _VULKAN_GAME_H
|
---|
3 |
|
---|
4 | #include <glm/glm.hpp>
|
---|
5 | #include <glm/gtc/matrix_transform.hpp>
|
---|
6 |
|
---|
7 | #include "game-gui-sdl.hpp"
|
---|
8 | #include "graphics-pipeline_vulkan.hpp"
|
---|
9 |
|
---|
10 | #include "vulkan-utils.hpp"
|
---|
11 |
|
---|
12 | using namespace glm;
|
---|
13 |
|
---|
14 | #ifdef NDEBUG
|
---|
15 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
16 | #else
|
---|
17 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | struct ModelVertex {
|
---|
21 | vec3 pos;
|
---|
22 | vec3 color;
|
---|
23 | vec2 texCoord;
|
---|
24 | };
|
---|
25 |
|
---|
26 | struct OverlayVertex {
|
---|
27 | vec3 pos;
|
---|
28 | vec2 texCoord;
|
---|
29 | };
|
---|
30 |
|
---|
31 | struct UBO_MvpMat {
|
---|
32 | alignas(16) mat4 model;
|
---|
33 | alignas(16) mat4 view;
|
---|
34 | alignas(16) mat4 proj;
|
---|
35 | };
|
---|
36 |
|
---|
37 | class VulkanGame {
|
---|
38 | public:
|
---|
39 | VulkanGame(int maxFramesInFlight);
|
---|
40 | ~VulkanGame();
|
---|
41 |
|
---|
42 | void run(int width, int height, unsigned char guiFlags);
|
---|
43 |
|
---|
44 | private:
|
---|
45 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
46 |
|
---|
47 | const float NEAR_CLIP = 0.1f;
|
---|
48 | const float FAR_CLIP = 100.0f;
|
---|
49 | const float FOV_ANGLE = 67.0f;
|
---|
50 |
|
---|
51 | vec3 cam_pos;
|
---|
52 |
|
---|
53 | UBO_MvpMat modelMvpMats;
|
---|
54 |
|
---|
55 | GameGui* gui;
|
---|
56 |
|
---|
57 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
58 | GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
|
---|
59 |
|
---|
60 | SDL_version sdlVersion;
|
---|
61 | SDL_Window* window = nullptr;
|
---|
62 | SDL_Renderer* renderer = nullptr;
|
---|
63 |
|
---|
64 | SDL_Texture* uiOverlay = nullptr;
|
---|
65 |
|
---|
66 | VkInstance instance;
|
---|
67 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
68 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
69 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
70 | VkDevice device;
|
---|
71 |
|
---|
72 | VkQueue graphicsQueue;
|
---|
73 | VkQueue presentQueue;
|
---|
74 |
|
---|
75 | VkSwapchainKHR swapChain;
|
---|
76 | vector<VkImage> swapChainImages;
|
---|
77 | VkFormat swapChainImageFormat;
|
---|
78 | VkExtent2D swapChainExtent;
|
---|
79 | vector<VkImageView> swapChainImageViews;
|
---|
80 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
81 |
|
---|
82 | VkRenderPass renderPass;
|
---|
83 | VkCommandPool commandPool;
|
---|
84 | vector<VkCommandBuffer> commandBuffers;
|
---|
85 |
|
---|
86 | VulkanImage depthImage;
|
---|
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 |
|
---|
96 | vector<VkDescriptorBufferInfo> uniformBufferInfoList;
|
---|
97 |
|
---|
98 | VulkanImage floorTextureImage;
|
---|
99 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
100 |
|
---|
101 | VulkanImage sdlOverlayImage;
|
---|
102 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
103 |
|
---|
104 | TTF_Font* font;
|
---|
105 | SDL_Texture* fontSDLTexture;
|
---|
106 |
|
---|
107 | SDL_Texture* imageSDLTexture;
|
---|
108 |
|
---|
109 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
110 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
111 | vector<VkFence> inFlightFences;
|
---|
112 |
|
---|
113 | size_t currentFrame;
|
---|
114 |
|
---|
115 | bool framebufferResized;
|
---|
116 |
|
---|
117 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
118 | void initVulkan();
|
---|
119 | void initGraphicsPipelines();
|
---|
120 | void initMatrices();
|
---|
121 | void mainLoop();
|
---|
122 | void updateScene(uint32_t currentImage);
|
---|
123 | void renderUI();
|
---|
124 | void renderScene();
|
---|
125 | void cleanup();
|
---|
126 |
|
---|
127 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
128 | void setupDebugMessenger();
|
---|
129 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
130 | void createVulkanSurface();
|
---|
131 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
132 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
133 | void createLogicalDevice(
|
---|
134 | const vector<const char*> validationLayers,
|
---|
135 | const vector<const char*>& deviceExtensions);
|
---|
136 | void createSwapChain();
|
---|
137 | void createImageViews();
|
---|
138 | void createRenderPass();
|
---|
139 | VkFormat findDepthFormat();
|
---|
140 | void createCommandPool();
|
---|
141 | void createImageResources();
|
---|
142 |
|
---|
143 | void createTextureSampler();
|
---|
144 | void createFramebuffers();
|
---|
145 | void createCommandBuffers();
|
---|
146 | void createSyncObjects();
|
---|
147 |
|
---|
148 | template<class UniformType>
|
---|
149 | void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
|
---|
150 | vector<VkDescriptorBufferInfo>& bufferInfoList);
|
---|
151 |
|
---|
152 | void recreateSwapChain();
|
---|
153 |
|
---|
154 | void cleanupSwapChain();
|
---|
155 |
|
---|
156 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
157 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
158 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
159 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
160 | void* pUserData);
|
---|
161 | };
|
---|
162 |
|
---|
163 | template<class UniformType>
|
---|
164 | void 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 |
|
---|
181 | #endif // _VULKAN_GAME_H
|
---|