1 | #ifndef _VULKAN_GAME_H
|
---|
2 | #define _VULKAN_GAME_H
|
---|
3 |
|
---|
4 | #include <glm/glm.hpp>
|
---|
5 |
|
---|
6 | #include "game-gui-sdl.hpp"
|
---|
7 | #include "graphics-pipeline_vulkan.hpp"
|
---|
8 |
|
---|
9 | #include "vulkan-utils.hpp"
|
---|
10 |
|
---|
11 | #ifdef NDEBUG
|
---|
12 | const bool ENABLE_VALIDATION_LAYERS = false;
|
---|
13 | #else
|
---|
14 | const bool ENABLE_VALIDATION_LAYERS = true;
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | struct ModelVertex {
|
---|
18 | glm::vec3 pos;
|
---|
19 | glm::vec3 color;
|
---|
20 | glm::vec2 texCoord;
|
---|
21 | };
|
---|
22 |
|
---|
23 | struct OverlayVertex {
|
---|
24 | glm::vec3 pos;
|
---|
25 | glm::vec2 texCoord;
|
---|
26 | };
|
---|
27 |
|
---|
28 | class VulkanGame {
|
---|
29 | public:
|
---|
30 | VulkanGame(int maxFramesInFlight);
|
---|
31 | ~VulkanGame();
|
---|
32 |
|
---|
33 | void run(int width, int height, unsigned char guiFlags);
|
---|
34 |
|
---|
35 | private:
|
---|
36 | const int MAX_FRAMES_IN_FLIGHT;
|
---|
37 |
|
---|
38 | const float NEAR_CLIP = 0.1f;
|
---|
39 | const float FAR_CLIP = 100.0f;
|
---|
40 | const float FOV_ANGLE = 67.0f;
|
---|
41 |
|
---|
42 | GameGui* gui;
|
---|
43 |
|
---|
44 | GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
|
---|
45 | GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
|
---|
46 |
|
---|
47 | SDL_version sdlVersion;
|
---|
48 | SDL_Window* window = nullptr;
|
---|
49 | SDL_Renderer* renderer = nullptr;
|
---|
50 |
|
---|
51 | SDL_Texture* uiOverlay = nullptr;
|
---|
52 |
|
---|
53 | VkInstance instance;
|
---|
54 | VkDebugUtilsMessengerEXT debugMessenger;
|
---|
55 | VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
|
---|
56 | VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
---|
57 | VkDevice device;
|
---|
58 |
|
---|
59 | VkQueue graphicsQueue;
|
---|
60 | VkQueue presentQueue;
|
---|
61 |
|
---|
62 | VkSwapchainKHR swapChain;
|
---|
63 | vector<VkImage> swapChainImages;
|
---|
64 | VkFormat swapChainImageFormat;
|
---|
65 | VkExtent2D swapChainExtent;
|
---|
66 | vector<VkImageView> swapChainImageViews;
|
---|
67 | vector<VkFramebuffer> swapChainFramebuffers;
|
---|
68 |
|
---|
69 | VkRenderPass renderPass;
|
---|
70 | VkCommandPool commandPool;
|
---|
71 | vector<VkCommandBuffer> commandBuffers;
|
---|
72 |
|
---|
73 | VulkanImage depthImage;
|
---|
74 |
|
---|
75 | VkSampler textureSampler;
|
---|
76 |
|
---|
77 | vector<VkDescriptorBufferInfo> uniformBufferInfoList;
|
---|
78 |
|
---|
79 | // These are currently to store the MVP matrix
|
---|
80 | // I should figure out if it makes sense to use them for other uniforms in the future
|
---|
81 | // If not, I should rename them to better indicate their purpose.
|
---|
82 | // I should also decide if I can use these for all shaders, or if I need a separapte set of buffers for each one
|
---|
83 | vector<VkBuffer> uniformBuffers;
|
---|
84 | vector<VkDeviceMemory> uniformBuffersMemory;
|
---|
85 |
|
---|
86 | VulkanImage floorTextureImage;
|
---|
87 | VkDescriptorImageInfo floorTextureImageDescriptor;
|
---|
88 |
|
---|
89 | VulkanImage sdlOverlayImage;
|
---|
90 | VkDescriptorImageInfo sdlOverlayImageDescriptor;
|
---|
91 |
|
---|
92 | TTF_Font* font;
|
---|
93 | SDL_Texture* fontSDLTexture;
|
---|
94 |
|
---|
95 | SDL_Texture* imageSDLTexture;
|
---|
96 |
|
---|
97 | vector<VkSemaphore> imageAvailableSemaphores;
|
---|
98 | vector<VkSemaphore> renderFinishedSemaphores;
|
---|
99 | vector<VkFence> inFlightFences;
|
---|
100 |
|
---|
101 | size_t currentFrame;
|
---|
102 | size_t numPlanes = 0; // temp
|
---|
103 |
|
---|
104 | bool framebufferResized;
|
---|
105 |
|
---|
106 | bool initWindow(int width, int height, unsigned char guiFlags);
|
---|
107 | void initVulkan();
|
---|
108 | void mainLoop();
|
---|
109 | void renderUI();
|
---|
110 | void renderScene();
|
---|
111 | void cleanup();
|
---|
112 |
|
---|
113 | void createVulkanInstance(const vector<const char*> &validationLayers);
|
---|
114 | void setupDebugMessenger();
|
---|
115 | void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
|
---|
116 | void createVulkanSurface();
|
---|
117 | void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
|
---|
118 | bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
|
---|
119 | void createLogicalDevice(
|
---|
120 | const vector<const char*> validationLayers,
|
---|
121 | const vector<const char*>& deviceExtensions);
|
---|
122 | void createSwapChain();
|
---|
123 | void createImageViews();
|
---|
124 | void createRenderPass();
|
---|
125 | VkFormat findDepthFormat();
|
---|
126 | void createCommandPool();
|
---|
127 | void createImageResources();
|
---|
128 |
|
---|
129 | void createTextureSampler();
|
---|
130 | void createFramebuffers();
|
---|
131 | void createUniformBuffers();
|
---|
132 | void createCommandBuffers();
|
---|
133 | void createSyncObjects();
|
---|
134 |
|
---|
135 | void recreateSwapChain();
|
---|
136 | void updateUniformBuffer(uint32_t currentImage);
|
---|
137 |
|
---|
138 | void cleanupSwapChain();
|
---|
139 |
|
---|
140 | static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
---|
141 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
---|
142 | VkDebugUtilsMessageTypeFlagsEXT messageType,
|
---|
143 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
---|
144 | void* pUserData);
|
---|
145 | };
|
---|
146 |
|
---|
147 | #endif // _VULKAN_GAME_H
|
---|