source: opengl-game/vulkan-game.hpp@ 785333b

feature/imgui-sdl points-test
Last change on this file since 785333b 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
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
[3782d66]31struct ShipVertex {
32 vec3 pos;
33 vec3 color;
[06d959f]34 vec3 normal;
[3782d66]35};
36
[055750a]37struct UBO_VP_mats {
[15104a8]38 alignas(16) mat4 view;
39 alignas(16) mat4 proj;
[771b33a]40};
41
[055750a]42struct SBO_SceneObject {
43 alignas(16) mat4 model;
44};
45
[99d44b2]46class VulkanGame {
[e8ebc76]47 public:
[34bdf3a]48 VulkanGame(int maxFramesInFlight);
[99d44b2]49 ~VulkanGame();
[0df3c9a]50
[b6e60b4]51 void run(int width, int height, unsigned char guiFlags);
[0df3c9a]52
53 private:
[34bdf3a]54 const int MAX_FRAMES_IN_FLIGHT;
55
[5ab1b20]56 const float NEAR_CLIP = 0.1f;
57 const float FAR_CLIP = 100.0f;
58 const float FOV_ANGLE = 67.0f;
59
[15104a8]60 vec3 cam_pos;
61
[055750a]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;
[15104a8]67
[0df3c9a]68 GameGui* gui;
[c559904]69
[b8777b7]70 GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
71 GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
[3782d66]72 GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
[7d2b0b9]73
[c559904]74 SDL_version sdlVersion;
[b794178]75 SDL_Window* window = nullptr;
76 SDL_Renderer* renderer = nullptr;
77
78 SDL_Texture* uiOverlay = nullptr;
[c1d9b2a]79
80 VkInstance instance;
81 VkDebugUtilsMessengerEXT debugMessenger;
[fe5c3ba]82 VkSurfaceKHR surface; // TODO: Change the variable name to vulkanSurface
[90a424f]83 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
[c1c2021]84 VkDevice device;
85
86 VkQueue graphicsQueue;
87 VkQueue presentQueue;
[0df3c9a]88
[502bd0b]89 VkSwapchainKHR swapChain;
90 vector<VkImage> swapChainImages;
91 VkFormat swapChainImageFormat;
[603b5bc]92 VkExtent2D swapChainExtent;
[f94eea9]93 vector<VkImageView> swapChainImageViews;
[603b5bc]94 vector<VkFramebuffer> swapChainFramebuffers;
[fa9fa1c]95
[6fc24c7]96 VkRenderPass renderPass;
[fa9fa1c]97 VkCommandPool commandPool;
[603b5bc]98 vector<VkCommandBuffer> commandBuffers;
[502bd0b]99
[603b5bc]100 VulkanImage depthImage;
[b794178]101
102 VkSampler textureSampler;
103
[055750a]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;
[b794178]111
[055750a]112 vector<VkBuffer> storageBuffers_scenePipeline;
113 vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
114
115 vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
[f97c5e7]116
[3782d66]117 vector<VkBuffer> uniformBuffers_shipPipeline;
118 vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
119
120 vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
121
[055750a]122 vector<VkBuffer> storageBuffers_shipPipeline;
123 vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
124
125 vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
126
[b794178]127 VulkanImage floorTextureImage;
128 VkDescriptorImageInfo floorTextureImageDescriptor;
129
130 VulkanImage sdlOverlayImage;
131 VkDescriptorImageInfo sdlOverlayImageDescriptor;
132
[1f25a71]133 TTF_Font* font;
134 SDL_Texture* fontSDLTexture;
135
136 SDL_Texture* imageSDLTexture;
137
[34bdf3a]138 vector<VkSemaphore> imageAvailableSemaphores;
139 vector<VkSemaphore> renderFinishedSemaphores;
140 vector<VkFence> inFlightFences;
141
[87c8f1a]142 size_t currentFrame;
143
144 bool framebufferResized;
[0e09340]145
[b6e60b4]146 bool initWindow(int width, int height, unsigned char guiFlags);
[0df3c9a]147 void initVulkan();
[f97c5e7]148 void initGraphicsPipelines();
[15104a8]149 void initMatrices();
[0df3c9a]150 void mainLoop();
[8e02b6b]151 void updateScene(uint32_t currentImage);
[a0c5f28]152 void renderUI();
153 void renderScene();
[0df3c9a]154 void cleanup();
[c1d9b2a]155
156 void createVulkanInstance(const vector<const char*> &validationLayers);
157 void setupDebugMessenger();
158 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo);
[90a424f]159 void createVulkanSurface();
[fe5c3ba]160 void pickPhysicalDevice(const vector<const char*>& deviceExtensions);
[fa9fa1c]161 bool isDeviceSuitable(VkPhysicalDevice physicalDevice, const vector<const char*>& deviceExtensions);
[c1c2021]162 void createLogicalDevice(
163 const vector<const char*> validationLayers,
164 const vector<const char*>& deviceExtensions);
[502bd0b]165 void createSwapChain();
[f94eea9]166 void createImageViews();
[6fc24c7]167 void createRenderPass();
168 VkFormat findDepthFormat();
[fa9fa1c]169 void createCommandPool();
[603b5bc]170 void createImageResources();
171
[b794178]172 void createTextureSampler();
[603b5bc]173 void createFramebuffers();
174 void createCommandBuffers();
[34bdf3a]175 void createSyncObjects();
[f94eea9]176
[06d959f]177 template<class VertexType>
178 vector<VertexType> addVertexNormals(vector<VertexType> vertices);
179
[055750a]180 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
181 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
[f97c5e7]182
[d2d9286]183 void recreateSwapChain();
184
[c1c2021]185 void cleanupSwapChain();
[c1d9b2a]186
187 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
188 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
189 VkDebugUtilsMessageTypeFlagsEXT messageType,
190 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
191 void* pUserData);
[e8ebc76]192};
193
[06d959f]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
[99d44b2]213#endif // _VULKAN_GAME_H
Note: See TracBrowser for help on using the repository browser.