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

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

In VulkanGame, add an objectIndex vertex attribute to the ship shader so it can be used as an index into the ssbo object array

  • Property mode set to 100644
File size: 6.3 KB
Line 
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
12using 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
20struct ModelVertex {
21 vec3 pos;
22 vec3 color;
23 vec2 texCoord;
24};
25
26struct OverlayVertex {
27 vec3 pos;
28 vec2 texCoord;
29};
30
31struct ShipVertex {
32 vec3 pos;
33 vec3 color;
34 vec3 normal;
35 unsigned int objIndex;
36};
37
38struct UBO_VP_mats {
39 alignas(16) mat4 view;
40 alignas(16) mat4 proj;
41};
42
43struct SBO_SceneObject {
44 alignas(16) mat4 model;
45};
46
47class VulkanGame {
48 public:
49 VulkanGame(int maxFramesInFlight);
50 ~VulkanGame();
51
52 void run(int width, int height, unsigned char guiFlags);
53
54 private:
55 const int MAX_FRAMES_IN_FLIGHT;
56
57 const float NEAR_CLIP = 0.1f;
58 const float FAR_CLIP = 100.0f;
59 const float FOV_ANGLE = 67.0f;
60
61 vec3 cam_pos;
62
63 UBO_VP_mats object_VP_mats;
64 SBO_SceneObject so_Object;
65
66 UBO_VP_mats ship_VP_mats;
67 SBO_SceneObject so_Ship;
68
69 GameGui* gui;
70
71 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
72 GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
73 GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
74
75 SDL_version sdlVersion;
76 SDL_Window* window = nullptr;
77 SDL_Renderer* renderer = nullptr;
78
79 SDL_Texture* uiOverlay = nullptr;
80
81 VkInstance instance;
82 VkDebugUtilsMessengerEXT debugMessenger;
83 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
84 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
85 VkDevice device;
86
87 VkQueue graphicsQueue;
88 VkQueue presentQueue;
89
90 VkSwapchainKHR swapChain;
91 vector<VkImage> swapChainImages;
92 VkFormat swapChainImageFormat;
93 VkExtent2D swapChainExtent;
94 vector<VkImageView> swapChainImageViews;
95 vector<VkFramebuffer> swapChainFramebuffers;
96
97 VkRenderPass renderPass;
98 VkCommandPool commandPool;
99 vector<VkCommandBuffer> commandBuffers;
100
101 VulkanImage depthImage;
102
103 VkSampler textureSampler;
104
105 // TODO: I should probably rename the uniformBuffer* and storageBuffer*
106 // variables to better reflect the data they hold
107
108 vector<VkBuffer> uniformBuffers_scenePipeline;
109 vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
110
111 vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
112
113 vector<VkBuffer> storageBuffers_scenePipeline;
114 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
115
116 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
117
118 vector<VkBuffer> uniformBuffers_shipPipeline;
119 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
120
121 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
122
123 vector<VkBuffer> storageBuffers_shipPipeline;
124 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
125
126 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
127
128 VulkanImage floorTextureImage;
129 VkDescriptorImageInfo floorTextureImageDescriptor;
130
131 VulkanImage sdlOverlayImage;
132 VkDescriptorImageInfo sdlOverlayImageDescriptor;
133
134 TTF_Font* font;
135 SDL_Texture* fontSDLTexture;
136
137 SDL_Texture* imageSDLTexture;
138
139 vector<VkSemaphore> imageAvailableSemaphores;
140 vector<VkSemaphore> renderFinishedSemaphores;
141 vector<VkFence> inFlightFences;
142
143 size_t currentFrame;
144
145 bool framebufferResized;
146
147 bool initWindow(int width, int height, unsigned char guiFlags);
148 void initVulkan();
149 void initGraphicsPipelines();
150 void initMatrices();
151 void mainLoop();
152 void updateScene(uint32_t currentImage);
153 void renderUI();
154 void renderScene();
155 void cleanup();
156
157 void createVulkanInstance(const vector<const char*> &validationLayers);
158 void setupDebugMessenger();
159 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
160 void createVulkanSurface();
161 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
162 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
163 void createLogicalDevice(
164 const vector<const char*> validationLayers,
165 const vector<const char*>& deviceExtensions);
166 void createSwapChain();
167 void createImageViews();
168 void createRenderPass();
169 VkFormat findDepthFormat();
170 void createCommandPool();
171 void createImageResources();
172
173 void createTextureSampler();
174 void createFramebuffers();
175 void createCommandBuffers();
176 void createSyncObjects();
177
178 template<class VertexType>
179 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
180
181 template<class VertexType>
182 vector<VertexType> addObjectIndex(unsigned int objIndex, vector<VertexType> vertices);
183
184 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
185 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
186
187 void recreateSwapChain();
188
189 void cleanupSwapChain();
190
191 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
192 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
193 VkDebugUtilsMessageTypeFlagsEXT messageType,
194 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
195 void* pUserData);
196};
197
198template<class VertexType>
199vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
200 for (unsigned int i = 0; i < vertices.size(); i += 3) {
201 vec3 p1 = vertices[i].pos;
202 vec3 p2 = vertices[i+1].pos;
203 vec3 p3 = vertices[i+2].pos;
204
205 vec3 normal = normalize(cross(p2 - p1, p3 - p1));
206 normal.z = -normal.z;
207
208 // Add the same normal for all 3 vertices
209 vertices[i].normal = normal;
210 vertices[i+1].normal = normal;
211 vertices[i+2].normal = normal;
212 }
213
214 return vertices;
215}
216
217template<class VertexType>
218vector<VertexType> VulkanGame::addObjectIndex(unsigned int objIndex, vector<VertexType> vertices) {
219 for (VertexType& vertex : vertices) {
220 vertex.objIndex = objIndex;
221 }
222
223 return vertices;
224}
225
226#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.