3 #define GLFW_INCLUDE_VULKAN
4 #include <GLFW/glfw3.h>
12 /* static glfw callbacks */
15 clb_key(GLFWwindow *win, int key, int scancode, int action, int mods);
18 clb_reshape(GLFWwindow *win, int width, int height);
20 /* static functions */
32 /* static variables */
34 static GLFWwindow *win;
36 static int win_w = 800;
37 static int win_h = 600;
39 static struct vk_ctx vk_core;
40 static VkSurfaceKHR vk_surf;
42 static struct vk_renderer vk_rnd;
43 static struct vk_swapchain vk_chain;
45 static struct vk_semaphores vk_sema;
47 /* empty for as long as we hardcode the vertices in the vertex shader */
48 static struct vk_vertex_info vk_vert_info;
50 int main(int argc, char** argv)
58 /* reshape window once just in case */
60 glfwGetWindowSize(win, &win_w, &win_h);
61 clb_reshape(win, win_w, win_h);
65 while(!glfwWindowShouldClose(win)) {
73 /* static functions */
83 fprintf(stderr, "Failed to initialize GLFW.\n");
87 if (glfwVulkanSupported() != GLFW_TRUE) {
88 fprintf(stderr, "Vulkan is not supported on this device.\n");
94 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
95 win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0);
98 fprintf(stderr, "Failed to create GLFW window\n");
102 /* initialize Vulkan context (instance) */
104 if (!vk_init_ctx_for_rendering(&vk_core)) {
105 fprintf(stderr, "Failed to initialize Vulkan context.\n");
109 /* create (Xcb) surface */
111 glfwGetFramebufferSize(win, &win_h, &win_h);
112 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
114 fprintf(stderr, "Failed to create XCB surface.\n");
120 /* create semaphores */
121 vk_create_semaphores(&vk_core, false, &vk_sema);
123 /* create swapchain */
124 vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, VK_NULL_HANDLE, &vk_chain);
127 vsdr = sdr_load("data/main.vert.spv", &vsz);
128 fsdr = sdr_load("data/main.frag.spv", &fsz);
130 /* create renderer */
131 if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz,
133 &vk_vert_info, &vk_rnd)) {
134 fprintf(stderr, "Failed to create renderer.\n");
138 /* set GLFW callbacks */
140 glfwSetKeyCallback(win, clb_key);
141 glfwSetWindowSizeCallback(win, clb_reshape);
144 glfwSetCursorPosCallback(win, clb_motion);
145 glfwSetMouseButtonCallback(win, clb_mouse);
159 vk_destroy_renderer(&vk_core, &vk_rnd);
161 vk_cleanup_ctx(&vk_core);
169 clb_key(GLFWwindow *win, int key, int scancode,
170 int action, int mods)
172 if (action == GLFW_PRESS) {
174 case GLFW_KEY_ESCAPE:
175 glfwSetWindowShouldClose(win, GLFW_TRUE);
182 clb_reshape(GLFWwindow *win, int width, int height)