-
Notifications
You must be signed in to change notification settings - Fork 0
/
Renderer.h
183 lines (146 loc) · 5.18 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
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
#pragma once
#include "Basics.h"
#include "DeviceMemoryManager.h"
#include "pipelines/PipelineRasterization.h"
#include "pipelines/PipelineRaytracing.h"
#include "pipelines/PipelineImGui.h"
#include "Swapchain.h"
#include "simple_scene_graph/Scene.h"
#include <glfw3.h>
#include "imgui/imgui.h"
#include <iostream>
#include <vector>
#include <chrono>
//TODO: change version numbers
constexpr VkApplicationInfo applicationInfo =
{
VK_STRUCTURE_TYPE_APPLICATION_INFO,
nullptr,
"MelonRendererDemo",
VK_MAKE_VERSION(0, 1, 0),
"MelonEngine",
VK_MAKE_VERSION(0, 1, 0),
VK_API_VERSION_1_2
};
struct QueueFamilyInfo
{
uint32_t familyIndex = UINT32_MAX;
std::vector<float> queuePriorities;
};
#define MELON_WINDOW_NAME "MelonRenderer"
#if defined _WIN32
#define VULKAN_LIBRARY_TYPE HMODULE
#elif defined __linux
#include <dlfcn.h>
#define VULKAN_LIBRARY_TYPE void*
#endif
namespace MelonRenderer
{
const unsigned int defaultWidth = 1600, defaultHeight = 900;
void MouseButtonCallback(GLFWwindow* window, int button, int action, int mods);
void ScrollCallback(GLFWwindow* window, double xOffset, double yOffset);
void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
void CharCallback(GLFWwindow* window, unsigned int c);
static const char* GetClipboardText(void* user_data)
{
return glfwGetClipboardString((GLFWwindow*)user_data);
}
static void SetClipboardText(void* user_data, const char* text)
{
glfwSetClipboardString((GLFWwindow*)user_data, text);
}
struct GlfwInputData
{
bool m_mouseButtonPressed[5] = { false, false, false, false, false };
GLFWcursor* m_mouseCursors[ImGuiMouseCursor_COUNT] = {};
GLFWmousebuttonfun m_prevUserCallbackMousebutton = nullptr;
GLFWscrollfun m_prevUserCallbackScroll = nullptr;
GLFWkeyfun m_prevUserCallbackKey = nullptr;
GLFWcharfun m_prevUserCallbackChar = nullptr;
};
class Renderer
{
public:
void Init();
bool Tick();
void Loop();
void Fini();
private:
bool CreateGLFWWindow();
bool LoadVulkanLibrary();
bool LoadExportedFunctions();
bool LoadGlobalFunctions();
bool CheckInstanceExtensions();
bool CreateInstance();
bool LoadInstanceFunctions();
bool LoadInstanceExtensionFunctions();
bool EnumeratePhysicalDevices();
bool CheckRequiredDeviceExtensions(VkPhysicalDevice & device);
bool CheckRequiredDeviceFeatures(VkPhysicalDevice& device);
bool CheckRequiredDeviceProperties(VkPhysicalDevice& device);
bool CheckQueueFamiliesAndProperties(VkPhysicalDevice& device);
bool FindCompatibleQueueFamily(VkQueueFlags flags, std::vector<QueueFamilyInfo>& familyIndices);
bool LoadDeviceFunctions();
bool LoadDeviceExtensionFunctions();
bool AquireQueueHandles();
bool CreatePresentationSurface();
bool EnumeratePresentationModes(VkPhysicalDevice& device);
bool SelectPresentationMode(VkPresentModeKHR desiredPresentMode);
bool CheckPresentationSurfaceCapabilities(VkPhysicalDevice& device);
bool CreateLogicalDeviceAndQueue(VkPhysicalDevice& device);
//renderpass for output
bool CreateRenderpass();
bool BeginRenderpass(VkCommandBuffer& commandBuffer);
bool EndRenderpass(VkCommandBuffer& commandBuffer);
//VkRenderPass m_renderpass;
bool BeginCommandBuffer(VkCommandBuffer& commandBuffer);
bool EndCommandBuffer(VkCommandBuffer& commandBuffer);
bool CopyOutputToSwapchain(VkCommandBuffer& commandBuffer, VkImage storage);
bool Resize();
//input
//-------------------------------------
bool GlfwInputInit();
bool GlfwInputTick();
bool GlfwUpdateMousePosAndButtons();
bool GlfwUpdateMouseCursor();
bool GlfwUpdateGamepads();
GlfwInputData m_inputData;
//-------------------------------------
VkPresentModeKHR m_presentMode;
const uint32_t m_queueFamilyIndex = 0;
VkExtent2D m_extent;
Swapchain m_swapchain;
PipelineRaytracing m_raytracingPipeline;
PipelineRasterization m_rasterizationPipeline;
PipelineImGui m_imguiPipeline;
Camera m_camera;
Scene m_scene;
DeviceMemoryManager m_memoryManager;
Renderpass* m_renderpass;
std::vector<mat3x4> m_transformMats;
std::vector<NodeDrawable> m_drawableNodes;
Node m_objectNode;
//time logic
//---------------------------------------
std::chrono::time_point<std::chrono::steady_clock> timeLast;
std::chrono::time_point<std::chrono::steady_clock> timeNow;
//---------------------------------------
std::vector<char const*> m_requiredInstanceExtensions;
std::vector<const char*> m_requiredDeviceExtensions;
std::vector<VkPhysicalDevice> m_physicalDevices;
uint32_t m_currentPhysicalDeviceIndex = 0;
VkPhysicalDeviceFeatures m_currentPhysicalDeviceFeatures;
VkPhysicalDeviceProperties m_currentPhysicalDeviceProperties;
VkPhysicalDeviceProperties2 m_currentPhysicalDeviceProperties2;
VkSurfaceCapabilitiesKHR m_currentSurfaceCapabilities;
std::vector<VkPresentModeKHR> m_currentPhysicalDevicePresentModes;
std::vector<VkQueueFamilyProperties> m_currentQueueFamilyProperties;
VkPhysicalDeviceRayTracingPropertiesNV m_raytracingProperties;
bool m_hasRaytracingCapabilities;
GLFWwindow* m_window;
VkSurfaceKHR m_presentationSurface;
VkPhysicalDeviceMemoryProperties m_physicalDeviceMemoryProperties;
VkInstance m_vulkanInstance;
VULKAN_LIBRARY_TYPE m_vulkanLibrary;
};
}