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