source: opengl-game/vulkan-game.hpp@ 055750a

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

In VulkanGame, use SSBOs in the ship and scene shaders to store per-object data (currently just the model matrix)

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