-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.h
239 lines (163 loc) · 5.03 KB
/
Engine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma once
#include "Types.h"
#include <string>
#include <vector>
#include <functional>
#include <deque>
#include "Mesh.h"
#include "Octree.h"
#include <unordered_map>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
class PipelineBuilder {
public:
std::vector<VkPipelineShaderStageCreateInfo> _shaderStages;
VkPipelineVertexInputStateCreateInfo _vertexInputInfo;
VkPipelineInputAssemblyStateCreateInfo _inputAssembly;
VkViewport _viewport;
VkRect2D _scissor;
VkPipelineRasterizationStateCreateInfo _rasterizer;
VkPipelineColorBlendAttachmentState _colorBlendAttachment;
VkPipelineMultisampleStateCreateInfo _multisampling;
VkPipelineLayout _pipelineLayout;
VkPipelineDepthStencilStateCreateInfo _depthStencil;
VkPipeline build_pipeline(VkDevice device, VkRenderPass pass);
};
struct DeletionQueue
{
std::deque<std::function<void()>> deletors;
void push_function(std::function<void()>&& function) {
deletors.push_back(function);
}
void flush() {
// reverse iterate the deletion queue to execute all the functions
for (auto it = deletors.rbegin(); it != deletors.rend(); it++) {
(*it)(); //call functors
}
deletors.clear();
}
};
struct MeshPushConstants {
glm::vec4 data;
glm::mat4 render_matrix;
};
struct Material {
VkPipeline pipeline;
VkPipelineLayout pipelineLayout;
};
struct RenderObject {
Mesh* mesh;
Material* material;
glm::mat4 transformMatrix;
};
struct RenderObjectOctree {
Octree* octree;
Material* material;
glm::mat4 transformMatrix;
uint8_t lod; // The LOD to render the octree at.
};
struct FrameData {
VkSemaphore _presentSemaphore, _renderSemaphore;
VkFence _renderFence;
DeletionQueue _frameDeletionQueue;
VkCommandPool _commandPool;
VkCommandBuffer _mainCommandBuffer;
AllocatedBuffer cameraBuffer;
VkDescriptorSet globalDescriptor;
AllocatedBuffer pointsBuffer;
AllocatedBuffer pyramidBuffer;
VkDescriptorSet octreeDescriptor;
};
struct GPUCameraData{
glm::mat4 view;
glm::mat4 proj;
glm::mat4 viewproj;
};
struct GPUSceneData {
glm::vec4 fogColor; // w is for exponent
glm::vec4 fogDistances; //x for min, y for max, zw unused.
glm::vec4 ambientColor;
glm::vec4 sunlightDirection; //w for sun power
glm::vec4 sunlightColor;
};
constexpr unsigned int FRAME_OVERLAP = 2;
class VulkanEngine {
public:
bool _isInitialized{ false };
int _frameNumber {0};
int _selectedShader{ 0 };
VkExtent2D _windowExtent{ 1700 , 900 };
struct SDL_Window* _window{ nullptr };
VkInstance _instance;
VkDebugUtilsMessengerEXT _debug_messenger;
VkPhysicalDevice _chosenGPU;
VkDevice _device;
VkPhysicalDeviceProperties _gpuProperties;
FrameData _frames[FRAME_OVERLAP];
VkQueue _graphicsQueue;
uint32_t _graphicsQueueFamily;
VkRenderPass _renderPass;
VkSurfaceKHR _surface;
VkSwapchainKHR _swapchain;
VkFormat _swachainImageFormat;
std::vector<VkFramebuffer> _framebuffers;
std::vector<VkImage> _swapchainImages;
std::vector<VkImageView> _swapchainImageViews;
DeletionQueue _mainDeletionQueue;
VmaAllocator _allocator; //vma lib allocator
//depth resources
VkImageView _depthImageView;
AllocatedImage _depthImage;
//the format for the depth image
VkFormat _depthFormat;
VkDescriptorPool _descriptorPool;
VkDescriptorSetLayout _globalSetLayout;
VkDescriptorSetLayout _octreeSetLayout;
GPUSceneData _sceneParameters;
AllocatedBuffer _sceneParameterBuffer;
//initializes everything in the engine
void init();
//shuts down the engine
void cleanup();
//draw loop
void draw();
//run main loop
void run();
FrameData& get_current_frame();
FrameData& get_last_frame();
//default array of renderable objects
std::vector<RenderObject> _renderables;
std::vector<RenderObjectOctree> _renderableOctrees;
std::unordered_map<std::string, Material> _materials;
std::unordered_map<std::string, Mesh> _meshes;
std::unordered_map<std::string, Octree> _octrees;
//functions
//create material and add it to the map
Material* create_material(VkPipeline pipeline, VkPipelineLayout layout, const std::string& name);
//returns nullptr if it cant be found
Material* get_material(const std::string& name);
//returns nullptr if it cant be found
Mesh* get_mesh(const std::string& name);
// returns nullptr if it cant be found
Octree* get_octree(const std::string& name);
//our draw function
void draw_objects(VkCommandBuffer cmd, RenderObject* first, int count);
AllocatedBuffer create_buffer(size_t allocSize, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage);
size_t pad_uniform_buffer_size(size_t originalSize);
private:
void init_vulkan();
void init_swapchain();
void init_default_renderpass();
void init_framebuffers();
void init_commands();
void init_sync_structures();
void init_pipelines();
void init_scene();
void init_descriptors();
//loads a shader module from a spir-v file. Returns false if it errors
bool load_shader_module(const char* filePath, VkShaderModule* outShaderModule);
void load_meshes();
void load_octrees();
void upload_mesh(Mesh& mesh);
void upload_octree(Octree& octree);
};