-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelloTriangleApplication.cpp
208 lines (169 loc) · 6.93 KB
/
HelloTriangleApplication.cpp
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
//
// Created by wicas on 2020/1/5.
//
#include "HelloTriangleApplication.h"
#include "common/InputState.h"
#include "components/ecs_register_comp.h"
#include "systems/default_render_sys.h"
#include "systems/sys_asset_loader.hpp"
#include "usr/adhoc_codegen/cust_system.h"
#include "usr/user_data_setup.hpp"
const int WIDTH = 800;
const int HEIGHT = 600;
const int MAX_FRAMES_IN_FLIGHT = 2;
#ifdef NDEBUG
const bool enableValidationLayers = false;
#else
const bool enableValidationLayers = true;
#endif
HelloTriangleApplication* HelloTriangleApplication::event_handling_instance;
void HelloTriangleApplication::mainLoop() {
// glfwSetKeyCallback(window, HelloTriangleApplication::keycallback_dispatch);
while (!container.windowShouldClose()) {
crevice::CVTimer::tick();
container.processInputAndEvent();
ecs.progress();
}
renderServer->waitIdle();
}
void HelloTriangleApplication::cleanupSwapChain() {
// TODO cleanup resource
// vkDestroyImageView(renderServer->vkContext->device, depthImageView,
// nullptr); vkDestroyImage(renderServer->vkContext->device, depthImage,
// nullptr); vkFreeMemory(renderServer->vkContext->device, depthImageMemory,
// nullptr);
// for (auto framebuffer : swapChainFramebuffers) {
// vkDestroyFramebuffer(renderServer->vkContext->device, framebuffer,
// nullptr);
// }
// vkDestroyPipeline(renderServer->vkContext->device, graphicsPipeline,
// nullptr); vkDestroyPipelineLayout(renderServer->vkContext->device,
// pipelineLayout,
// nullptr);
// vkDestroyRenderPass(renderServer->vkContext->device, renderPass, nullptr);
// for (auto imageView : renderServer->windowContext->swapChainImageViews) {
// vkDestroyImageView(renderServer->vkContext->device, imageView, nullptr);
// }
// vkDestroySwapchainKHR(renderServer->vkContext->device,
// renderServer->windowContext->swapChain, nullptr);
}
void HelloTriangleApplication::recreateSwapChain() {
// TODO render pass,pipeline must recate in render graph
// vkDeviceWaitIdle(vkContext.device);
cleanupSwapChain();
// gpuResourceManager.createSwapChain(windowContext);
// gpuResourceManager.createSwapChainImageViews(windowContext, 1);
// createRenderPass();
// createGraphicsPipeline();
// createDepthResources();
// createFramebuffers();
// createCommandBuffers();
}
void HelloTriangleApplication::cleanup() {
cleanupSwapChain();
// TODO resource clear in resource manager;
// vkDestroyDescriptorPool(vkContext.device,
// gpuResourceManager.descriptorPools[0], nullptr);
// for (size_t i = 0; i < windowContext.swapChainImages.size(); i++) {
// vkDestroyBuffer(vkContext.device, cameraUniformBuffers[i], nullptr);
// vkFreeMemory(vkContext.device, cameraUniformBuffersMemory[i], nullptr);
// }
// gpuResourceManager.destroyTexture(obj1TexId);
// gpuResourceManager.destroyTexture(specTexId);
// // gpuResourceManager.destoryDescriptorSetLayout(*descriptorSetLayout);
// gpuResourceManager.destroyMesh(obj1);
// gpuResourceManager.destroyMesh(obj2);
// // for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
// // vkDestroySemaphore(vkContext.device, renderFinishedSemaphores[i],
// nullptr);
// // vkDestroySemaphore(vkContext.device, imageAvailableSemaphores[i],
// nullptr);
// // vkDestroyFence(vkContext.device, inFlightFences[i], nullptr);
// // }
// vkDestroyCommandPool(vkContext.device, vkContext.commandPool, nullptr);
// vkDestroyDevice(vkContext.device, nullptr);
// if (enableValidationLayers) {
// vkUtil::DestroyDebugUtilsMessengerEXT(vkContext.instance,
// vkContext.debugMessenger, nullptr);
// }
// vkDestroySurfaceKHR(vkContext.instance, vkContext.surface, nullptr);
// vkDestroyInstance(vkContext.instance, nullptr);
// glfwDestroyWindow(windowContext.window);
// glfwTerminate();
}
void HelloTriangleApplication::mouse_callback(GLFWwindow* window, double xpos,
double ypos) {
// TODO this should in input server
// if (renderServer->windowContext->firstMouse)
// {
// renderServer->windowContext->lastX = xpos;
// renderServer->windowContext->lastY = ypos;
// renderServer->windowContext->firstMouse = false;
// }
// float xoffset = xpos - renderServer->windowContext->lastX;
// float yoffset = renderServer->windowContext->lastY - ypos;
// renderServer->windowContext->lastX = xpos;
// renderServer->windowContext->lastY = ypos;
// camera.ProcessMouseMovement(xoffset, yoffset, true);
}
void HelloTriangleApplication::processInput(GLFWwindow* window) {
crevice::InputState::states.clear();
// TODO key mapping
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_ESCAPE);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_W);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_S);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_A);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_D);
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_E);
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_Q);
if (glfwGetKey(window, GLFW_KEY_N) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_N);
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS)
crevice::InputState::states.emplace(GLFW_KEY_I);
}
void HelloTriangleApplication::serverSetup() {
container = crevice::GLFWContainer(WIDTH, HEIGHT, "Vulkan");
{
// TODO more than glfw binding
using namespace std::placeholders;
container.inputProcessor = std::bind(
&HelloTriangleApplication::processInput, this, std::placeholders::_1);
container.framebufferResizeCallback =
&HelloTriangleApplication::framebufferResizeCallback;
container.mouse_callback_dispatch =
&HelloTriangleApplication::mouse_callback_dispatch;
}
container.setUpInput(this);
renderServer = crevice::RenderServer::getInstance();
renderServer->setContainer(&container);
renderServer->init();
assetManager = new AssetManager();
rManager = new ResourceManager();
assetResourceBridge = AssetResourceBridge::getInstance();
assetResourceBridge->setup(rManager, assetManager);
vkResourceBridge = crevice::VkResourceBridge::getInstance();
vkResourceBridge->setup(rManager, renderServer->gpuRManager,
renderServer->vkContext);
}
void HelloTriangleApplication::setupECS() {
ecs_reg_comp(ecs);
SetRenderHandler(ecs);
setupDefaultSys(ecs);
setAssetResourceSystems(ecs);
usr_ecs_scene_setup(ecs);
setCustSys(ecs);
}
void HelloTriangleApplication::run() {
serverSetup();
// initVulkan();
setupECS();
mainLoop();
cleanup();
}