Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Rubberduckycooly
GitHub Repository: Rubberduckycooly/RSDKv5-Decompilation
Path: blob/master/RSDKv5/RSDK/Graphics/Vulkan/VulkanRenderDevice.hpp
1162 views
1
// TODO: VULKAN_USE_GLFW and other stuff
2
// i'm not gonna worry about those yet
3
4
class RenderDevice : public RenderDeviceBase
5
{
6
7
public:
8
struct WindowInfo {
9
union {
10
struct {
11
int32 width;
12
int32 height;
13
int32 _pad[3];
14
int32 refresh_rate;
15
};
16
GLFWvidmode internal;
17
} * displays;
18
};
19
20
struct SwapChainDetails {
21
VkSurfaceCapabilitiesKHR capabilities;
22
std::vector<VkSurfaceFormatKHR> formats;
23
std::vector<VkPresentModeKHR> presentModes;
24
};
25
26
static WindowInfo displayInfo;
27
28
static bool Init();
29
static void CopyFrameBuffer();
30
static void FlipScreen();
31
static void Release(bool32 isRefresh);
32
33
static void RefreshWindow();
34
static void GetWindowSize(int32 *width, int32 *height);
35
36
static void SetupImageTexture(int32 width, int32 height, uint8 *imagePixels);
37
static void SetupVideoTexture_YUV420(int32 width, int32 height, uint8 *yPlane, uint8 *uPlane, uint8 *vPlane, int32 strideY, int32 strideU,
38
int32 strideV);
39
static void SetupVideoTexture_YUV422(int32 width, int32 height, uint8 *yPlane, uint8 *uPlane, uint8 *vPlane, int32 strideY, int32 strideU,
40
int32 strideV);
41
static void SetupVideoTexture_YUV444(int32 width, int32 height, uint8 *yPlane, uint8 *uPlane, uint8 *vPlane, int32 strideY, int32 strideU,
42
int32 strideV);
43
44
static bool ProcessEvents();
45
46
static void InitFPSCap();
47
static bool CheckFPSCap();
48
static void UpdateFPSCap();
49
50
static bool InitShaders();
51
static void LoadShader(const char *fileName, bool32 linear);
52
53
static inline void ShowCursor(bool32 shown) { glfwSetInputMode(window, GLFW_CURSOR, shown ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_HIDDEN); }
54
static inline bool GetCursorPos(Vector2 *pos)
55
{
56
double cursorX, cursorY;
57
glfwGetCursorPos(window, &cursorX, &cursorY);
58
pos->x = (int32)cursorX;
59
pos->y = (int32)cursorY;
60
return true;
61
};
62
inline static void SetWindowTitle() { glfwSetWindowTitle(window, gameVerInfo.gameTitle); };
63
64
static GLFWwindow *window;
65
66
private:
67
static bool SetupRendering();
68
static bool InitVertexBuffer();
69
static bool InitGraphicsAPI();
70
71
static void GetDisplays();
72
73
static void ProcessKeyEvent(GLFWwindow *, int32 key, int32 scancode, int32 action, int32 mods);
74
static void ProcessFocusEvent(GLFWwindow *, int32 focused);
75
static void ProcessMouseEvent(GLFWwindow *, int32 button, int32 action, int32 mods);
76
static void ProcessJoystickEvent(int32 ID, int32 event);
77
static void ProcessMaximizeEvent(GLFWwindow *, int32 maximized);
78
79
static void SetLinear(bool32 linear);
80
81
static GLFWwindow *CreateGLFWWindow(void);
82
83
#if VK_DEBUG
84
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
85
VkDebugUtilsMessageTypeFlagsEXT messageType,
86
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData);
87
88
static VkDebugUtilsMessengerEXT debugMessenger;
89
#endif
90
91
static bool CheckExtensionSupport(VkPhysicalDevice device);
92
93
static SwapChainDetails QuerySwapChainDetails(VkPhysicalDevice device);
94
95
static bool IsDeviceSuitable(VkPhysicalDevice device)
96
{
97
if (!CheckExtensionSupport(device))
98
return false;
99
100
QuerySwapChainDetails(device);
101
if (currentSwapDetails.formats.empty() || currentSwapDetails.presentModes.empty())
102
return false;
103
104
uint32_t queueFamilyCount = 0;
105
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);
106
107
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
108
vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
109
110
graphicsIndex = 0xFFFFFFFF;
111
presentIndex = 0xFFFFFFFF;
112
113
int i = 0;
114
for (const auto &queueFamily : queueFamilies) {
115
if (queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT)
116
graphicsIndex = i;
117
118
VkBool32 presentSupport = false;
119
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
120
121
if (presentSupport)
122
presentIndex = i;
123
124
if (graphicsIndex != 0xFFFFFFFF && presentIndex != 0xFFFFFFFF)
125
return true;
126
127
++i;
128
}
129
130
return false;
131
};
132
133
static bool CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory);
134
static void ReleaseShaderPipelines();
135
136
static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
137
VkPhysicalDeviceMemoryProperties memProperties;
138
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
139
140
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
141
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
142
return i;
143
}
144
}
145
146
return (uint32_t)-1;
147
};
148
149
static VkInstance instance;
150
static VkPhysicalDevice physicalDevice;
151
static VkDevice device;
152
static VkSurfaceKHR surface;
153
154
static SwapChainDetails currentSwapDetails;
155
static VkSwapchainKHR swapChain;
156
static VkFormat swapChainImageFormat;
157
static VkExtent2D swapChainExtent;
158
159
static std::vector<VkImage> swapChainImages;
160
static std::vector<VkImageView> swapChainImageViews;
161
static std::vector<VkFramebuffer> swapChainFramebuffers;
162
163
struct ShaderConstants;
164
165
static VkBuffer uniformBuffer;
166
static VkDeviceMemory uniformBufferMemory;
167
static ShaderConstants *uniformMap;
168
169
static VkDescriptorPool descriptorPool;
170
static VkDescriptorSet descriptorSet[SCREEN_COUNT];
171
172
static VkRenderPass renderPass;
173
static VkDescriptorSetLayout setLayout;
174
static VkPipelineLayout pipelineLayout;
175
static VkGraphicsPipelineCreateInfo basePipeline;
176
177
static VkBuffer vertexBuffer;
178
static VkDeviceMemory vertexBufferMemory;
179
180
static VkCommandPool commandPool;
181
static VkCommandBuffer commandBuffer;
182
183
static VkQueue graphicsQueue;
184
static VkQueue presentQueue;
185
static uint32 graphicsIndex;
186
static uint32 presentIndex;
187
188
static VkViewport viewport;
189
190
static VkSemaphore imageAvailableSemaphore;
191
static VkSemaphore renderFinishedSemaphore;
192
static VkFence inFlightFence;
193
194
static int32 monitorIndex;
195
196
struct Texture {
197
VkImage image;
198
VkDeviceMemory memory;
199
VkSubresourceLayout layout;
200
VkImageView view;
201
void* map;
202
};
203
204
static VkSampler samplerPoint;
205
static VkSampler samplerLinear;
206
207
static VkDescriptorImageInfo imageInfo;
208
209
static Texture imageTexture;
210
static Texture screenTextures[SCREEN_COUNT];
211
212
static double lastFrame;
213
static double targetFreq;
214
};
215
216
struct ShaderEntry : public ShaderEntryBase {
217
VkPipeline shaderPipeline;
218
};
219
220