-
Notifications
You must be signed in to change notification settings - Fork 2
/
vkrender.h
240 lines (190 loc) · 7.02 KB
/
vkrender.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
240
#ifndef VKRENDER_H
#define VKRENDER_H
#include <array>
#include <unordered_map>
#include <vector>
#ifndef NOMINMAX
#define NOMINMAX
#endif // !NOMINMAX
#include <vulkan/vulkan.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
struct QueueFamilyIndices {
uint32_t graphicsFamily = -1;
uint32_t presentFamily = -1;
bool isComplete() { return graphicsFamily != -1 && presentFamily != -1; }
};
struct SwapChainSupportDetails {
VkSurfaceCapabilitiesKHR capabilities;
std::vector<VkSurfaceFormatKHR> formats;
std::vector<VkPresentModeKHR> presentModes;
};
struct UniformBufferObject {
glm::mat4 view;
glm::mat4 proj;
glm::vec3 camPos;
glm::vec3 worldUp;
};
struct InstanceData {
glm::mat4 transform;
glm::vec4 color;
};
struct DrawItem {
int meshId;
uint32_t instanceBase;
uint32_t instanceCount;
};
struct Vertex {
glm::vec3 pos;
};
const int VERTEX_BUFFER_BIND_ID = 0;
const int INSTANCE_BUFFER_BIND_ID = 1;
class VkRender {
public:
#ifdef VK_USE_PLATFORM_MACOS_MVK
VkRender(const void* viewId);
#elif VK_USE_PLATFORM_WIN32_KHR
VkRender(HWND viewId);
#endif
virtual ~VkRender();
void drawFrame();
void updateCamera(glm::mat4x4& view, glm::mat4x4& proj, glm::vec3& camPos, glm::vec3& worldUp);
void updateUniformBuffer();
void recreateVertexBuffer();
void recreateIndexBuffer();
void buildCommandBuffers(const std::vector<DrawItem>& drawItems);
void updateInstanceBuffer(const std::vector<InstanceData>& instData);
void resize(uint32_t w, uint32_t h);
void clear();
bool isCached(int id);
void addGeometry(const std::vector<glm::vec3>& vertices, const std::vector<uint32_t>& indices, int id);
private:
VkClearColorValue mClearColor = {{0.8f, 0.8f, 0.8f, 1.0f}};
glm::mat4x4 proj, view;
glm::vec3 camPos, worldUp;
uint32_t height = 0;
uint32_t width = 0;
const std::vector<const char*> validationLayers = {"VK_LAYER_LUNARG_standard_validation"};
const std::vector<const char*> deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
const std::vector<Vertex> vertices = {{{-0.5f, -0.5f, 0.0f}}, {{0.5f, -0.5f, 0.0f}}, {{0.5f, 0.5f, 0.0f}}, {{-0.5f, 0.5f, 0.0f}}};
const std::vector<uint32_t> indices = {0, 1, 2, 2, 3, 0};
#ifdef NDEBUG
const bool enableValidationLayers = true;
#else
const bool enableValidationLayers = true;
#endif
void initVulkan();
void setupDebugCallback();
void pickPhysicalDevice();
void createLogicalDevice();
void createSurface();
void createSwapChain();
void createImageViews();
void createRenderPass();
void createGraphicsPipeline();
void createFramebuffers();
void createCommandPool();
VkFormat findDepthFormat();
VkFormat findSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags);
void createImage(uint32_t width,
uint32_t height,
VkFormat format,
VkImageTiling tiling,
VkImageUsageFlags usage,
VkMemoryPropertyFlags properties,
VkImage& image,
VkDeviceMemory& imageMemory);
VkCommandBuffer beginSingleTimeCommands();
void endSingleTimeCommands(VkCommandBuffer commandBuffer);
bool hasStencilComponent(VkFormat format);
void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout);
void createDepthResources();
void rebuildCommandBuffer(const size_t index);
void createCommandBuffers();
void createSemaphores();
void createFences();
void recreateSwapChain();
void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory);
void copyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);
void createVertexBuffer();
void createIndexBuffer();
void createInstanceBuffer(const int maxInstanceCount);
void createUniformBuffer();
void createDescriptorPool();
void createDescriptorSet();
void createDescriptorSetLayout();
void cleanup();
void cleanupSwapChain();
void createInstance();
void checkExtensions();
bool checkValidationLayerSupport();
std::vector<const char*> getRequiredExtensions();
bool isDeviceSuitable(VkPhysicalDevice device);
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats);
VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> availablePresentModes);
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
bool checkDeviceExtensionSupport(VkPhysicalDevice device);
VkShaderModule createShaderModule(const std::vector<char>& code);
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
bool isInstanceDirty = false;
std::vector<InstanceData> mInstances;
std::vector<Vertex> mVertices;
std::vector<uint32_t> mIndices;
std::vector<DrawItem> mDrawItems;
struct Mesh {
int32_t vertexBase;
uint32_t indexBase;
uint32_t indexCount;
};
std::vector<Mesh> mMeshes;
std::unordered_map<int, Mesh> mIdToMesh;
VkInstance instance;
VkPhysicalDevice physicalDevice = nullptr;
VkDevice device;
VkSurfaceKHR surface;
VkQueue graphicsQueue;
VkQueue presentQueue;
VkSwapchainKHR swapChain;
std::vector<VkImage> swapChainImages;
VkFormat swapChainImageFormat;
VkExtent2D swapChainExtent;
std::vector<VkImageView> swapChainImageViews;
std::vector<VkFramebuffer> swapChainFramebuffers;
VkImage depthImage;
VkDeviceMemory depthImageMemory;
VkImageView depthImageView;
VkRenderPass renderPass;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkPipeline graphicsPipeline;
VkSemaphore imageAvailableSemaphore;
VkSemaphore renderFinishedSemaphore;
VkCommandPool commandPool;
std::vector<VkCommandBuffer> commandBuffers;
std::vector<VkFence> waitFences;
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;
VkBuffer indexBuffer;
VkDeviceMemory indexBufferMemory;
VkBuffer uniformBuffer;
VkDeviceMemory uniformBufferMemory;
VkBuffer instanceBuffer;
VkDeviceMemory instanceBufferMemory;
size_t instanceBuffersCount = 0;
std::vector<VkBuffer> instanceStagingBuffer;
std::vector<VkDeviceMemory> instanceStagingBufferMemory;
VkDescriptorPool descriptorPool;
VkDescriptorSet descriptorSet;
VkDebugReportCallbackEXT callback;
#ifdef VK_USE_PLATFORM_MACOS_MVK
const void* viewId;
#elif VK_USE_PLATFORM_WIN32_KHR
HWND viewId;
#endif
};
#endif // VKRENDER_H