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;
44 static struct vk_semaphores vk_sema;
45 static struct vk_image_attachment vk_color_att;
46 static struct vk_image_attachment vk_depth_att;
47 static struct vk_image_props vk_depth_att_props;
49 /* empty for as long as we hardcode the vertices in the vertex shader */
50 static struct vk_vertex_info vk_vert_info;
52 int main(int argc, char** argv)
60 /* reshape window once just in case */
62 glfwGetWindowSize(win, &win_w, &win_h);
63 clb_reshape(win, win_w, win_h);
67 while(!glfwWindowShouldClose(win)) {
75 /* static functions */
85 fprintf(stderr, "Failed to initialize GLFW.\n");
89 if (glfwVulkanSupported() != GLFW_TRUE) {
90 fprintf(stderr, "Vulkan is not supported on this device.\n");
96 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
97 win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0);
100 fprintf(stderr, "Failed to create GLFW window\n");
104 /* initialize Vulkan context (instance) */
106 if (!vk_init_ctx_for_rendering(&vk_core)) {
107 fprintf(stderr, "Failed to initialize Vulkan context.\n");
111 /* create (Xcb) surface */
113 glfwGetFramebufferSize(win, &win_h, &win_h);
114 if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf)
116 fprintf(stderr, "Failed to create XCB surface.\n");
122 /* create semaphores */
123 vk_create_semaphores(&vk_core, false, &vk_sema);
125 /* create swapchain */
126 vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, VK_NULL_HANDLE, &vk_chain);
129 vsdr = sdr_load("data/main.vert.spv", &vsz);
130 fsdr = sdr_load("data/main.frag.spv", &fsz);
132 /* create depth attachment (for the moment we are going to use this
134 if (!vk_fill_image_props(&vk_core,
137 VK_FORMAT_D24_UNORM_S8_UINT,
138 VK_IMAGE_TILING_OPTIMAL,
139 VK_IMAGE_LAYOUT_UNDEFINED,
140 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
141 false, &vk_depth_att_props)) {
142 fprintf(stderr, "Unsupported depth image properties\n");
145 if (!vk_create_image(&vk_core, &vk_depth_att_props, &vk_depth_att.obj)) {
146 fprintf(stderr, "Failed to create depth attachment.\n");
150 /* create renderer */
151 if (!vk_create_renderer(&vk_core, vsdr, vsz, fsdr, fsz,
152 false, false, 0, &vk_depth_att,
153 &vk_vert_info, &vk_rnd)) {
154 fprintf(stderr, "Failed to create renderer.\n");
158 /* set GLFW callbacks */
160 glfwSetKeyCallback(win, clb_key);
161 glfwSetWindowSizeCallback(win, clb_reshape);
164 glfwSetCursorPosCallback(win, clb_motion);
165 glfwSetMouseButtonCallback(win, clb_mouse);
179 vk_destroy_renderer(&vk_core, &vk_rnd);
181 vk_cleanup_ctx(&vk_core);
189 clb_key(GLFWwindow *win, int key, int scancode,
190 int action, int mods)
192 if (action == GLFW_PRESS) {
194 case GLFW_KEY_ESCAPE:
195 glfwSetWindowShouldClose(win, GLFW_TRUE);
202 clb_reshape(GLFWwindow *win, int width, int height)