-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.h
136 lines (117 loc) · 5.13 KB
/
renderer.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
#ifndef __renderer_h__
#define __renderer_h__
#include "definitions.h"
/// <summary>
/// PipelineBuilder is a helper struct to organize various Vulkan objects, which are
/// needed to establish rendering pipeline.
/// </summary>
struct PipelineBuilder {
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
VkPipelineVertexInputStateCreateInfo vertexInputInfo{};
VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
VkPipelineRasterizationStateCreateInfo rasterizer{};
VkPipelineColorBlendAttachmentState colorBlendAttachment{};
VkPipelineMultisampleStateCreateInfo multisampling{};
VkPipelineLayoutCreateInfo ppipelineLayout{};
VkPipelineDepthStencilStateCreateInfo depthStencil{};
VkPipelineDynamicStateCreateInfo dynamic{};
VkDescriptorSetLayout dsLayout = VK_NULL_HANDLE;
VkVertexInputAttributeDescription vertexAttrDesc{};
VkVertexInputBindingDescription vertexBindingDesc{};
std::vector<VkDynamicState> dynamicStates;
VkPipeline buildPipeline(VulkanPointers& vp, VkRenderPass pass);
};
/// <summary>
/// Renderer class handles actual rendering. It uses Vulkan, glTF and glm stuff and
/// as little as possible Qt. Notice that Vulkan function pointers are delivered
/// by Qt.
/// </summary>
class Renderer {
public:
Renderer(VulkanPointers& vulkanPointers);
~Renderer() {}
void createVertexBuffer(tinygltf::Model& model);
void deleteVertexBuffer();
void setProjectionMatrix(float* proj);
void render();
void setViewMatrix(float* view = nullptr);
void setModelMatrix(float* model = nullptr);
void createUniformBuffers();
void deleteUniformBuffers();
void createGraphicsPipeline();
void deleteGraphicsPipeline();
void createSwapChain(int* colorFormat = nullptr,
int* depthFormat = nullptr,
VkSwapchainKHR oldSwapChain = VK_NULL_HANDLE);
void deleteSwapChain();
VkSwapchainKHR getSwapChain() { return mSwapChain; };
void createSyncObjects();
void deleteSyncObjects();
private:
void traverse(int& nodeIndex, tinygltf::Model& model,
std::vector<glm::vec3>& vertices,
glm::mat4x4 transformation = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 });
void getPositions(std::vector<glm::vec3>& vertices,
tinygltf::Model& model, int& posAcc, int indAcc = -1,
glm::mat4x4 transformation = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 });
void setMinMax(glm::vec4& vec);
VkShaderModule createShaderModule(std::string file);
void updateUniformBuffer();
bool createTransientImage(VkFormat format,
VkImageUsageFlags usage,
VkImageAspectFlags aspectMask,
VkImage* images,
VkDeviceMemory* mem,
VkImageView* views,
int count);
uint32_t chooseTransientImageMemType(VkImage img, uint32_t startIndex);
VulkanPointers mVulkanPointers;
VkDeviceMemory mVertexBufferMemory = nullptr;
VkBuffer mVertexBuffer = nullptr;
int mVertexBufferSize = 0;
glm::vec3 mMin = { -1, -1, -1 };
glm::vec3 mMax = { 1, 1, 1 };
std::vector<VkBuffer> mUniformBuffers;
std::vector<VkDeviceMemory> mUniformBuffersMemory;
std::vector<void*> mUniformBuffersMapped;
const std::chrono::system_clock::time_point mStartTime = std::chrono::system_clock::now();
PipelineBuilder mPipelineBuilder;
VkPipeline mPipeline = VK_NULL_HANDLE;
VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
QSize mSwapChainImageSize = QSize(0, 0);
VkDescriptorPool mDescriptorPool = VK_NULL_HANDLE;
uint32_t mActualSwapChainBufferCount = 0;
uint32_t mCurrentFrame = 0;
std::vector<VkDescriptorSet> mDescriptorSets;
VkShaderModule mVertShaderModule = VK_NULL_HANDLE;
VkShaderModule mFragShaderModule = VK_NULL_HANDLE;
VkSemaphore mPresentCompleteSemaphore = VK_NULL_HANDLE;
VkSemaphore mRenderingCompleteSemaphore = VK_NULL_HANDLE;
VkFence mRenderFence = VK_NULL_HANDLE;
PFN_vkCreateSwapchainKHR vkCreateSwapchainKHR = nullptr;
PFN_vkDestroySwapchainKHR vkDestroySwapchainKHR = nullptr;
PFN_vkGetSwapchainImagesKHR vkGetSwapchainImagesKHR = nullptr;
PFN_vkAcquireNextImageKHR vkAcquireNextImageKHR = nullptr;
PFN_vkQueuePresentKHR vkQueuePresentKHR = nullptr;
PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR vkGetPhysicalDeviceSurfaceCapabilitiesKHR = nullptr;
PFN_vkGetPhysicalDeviceSurfaceFormatsKHR vkGetPhysicalDeviceSurfaceFormatsKHR = nullptr;
PFN_vkGetPhysicalDeviceSurfaceSupportKHR vkGetPhysicalDeviceSurfaceSupportKHR = nullptr;
// PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT = nullptr;
struct UniformBufferObject {
glm::mat4 projectionMatrix = glm::mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 1.0);
glm::mat4 modelMatrix = glm::mat4(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
glm::mat4 viewMatrix = glm::mat4(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0);
} mUboMVPMatrices;
struct SwapChainRes {
VkCommandBuffer cmdBuffer = VK_NULL_HANDLE;
VkImage depthImage = VK_NULL_HANDLE;
VkDeviceMemory depthImageMemory = VK_NULL_HANDLE;
VkImage images[MAX_FRAMES_IN_FLIGHT] = {};
VkImageView depthImageView = VK_NULL_HANDLE;
VkImageView imageViews[MAX_FRAMES_IN_FLIGHT] = {};
VkRenderPass renderPass = VK_NULL_HANDLE;
VkFramebuffer frameBuffers[MAX_FRAMES_IN_FLIGHT] = {};
VkCommandPool commandPool = VK_NULL_HANDLE;
} mSwapChainRes;
};
#endif