From: Eleni Maria Stea Date: Sat, 18 Sep 2021 20:20:53 +0000 (+0300) Subject: added support for c++, mv main.c->main.cc X-Git-Url: https://eleni.mutantstargoat.com/git/?p=vkrt;a=commitdiff_plain;h=6c8e0d1dcf1e08982b54a1b2d2e914e409e09386 added support for c++, mv main.c->main.cc --- diff --git a/Makefile b/Makefile index 0e3b0b4..70c5194 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ src = $(wildcard src/*.c) -obj = $(src:.c=.o) +ccsrc = $(wildcard src/*.cc) +obj = $(src:.c=.o) $(ccsrc:.cc=.o) dep = $(obj:.o=.d) vsdr = $(wildcard data/*.vert) fsdr = $(wildcard data/*.frag) @@ -10,14 +11,16 @@ bin = rt dbg = -g CC = gcc +CXX = g++ CFLAGS = -pedantic -Wall $(dbg) -MMD +CXXFLAGS = -pedantic -Wall $(dbg) -MMD LDFLAGS = -lvulkan -lglfw .PHONY: all all: $(bin) $(spv) $(bin): $(obj) - $(CC) -o $@ $(obj) $(LDFLAGS) + $(CXX) -o $@ $(obj) $(LDFLAGS) %.vert.spv: %.vert glslangValidator -V $< -o $@ diff --git a/src/main.c b/src/main.c deleted file mode 100644 index af4277c..0000000 --- a/src/main.c +++ /dev/null @@ -1,301 +0,0 @@ -#include - -#define GLFW_INCLUDE_VULKAN -#include -#include -#include - -#include "vk.h" -#include "util.h" - - -/* static glfw callbacks */ - -static void -clb_key(GLFWwindow *win, int key, int scancode, int action, int mods); - -static void -clb_reshape(GLFWwindow *win, int width, int height); - -/* static functions */ - -static bool -init(void); - -static void -cleanup(void); - -static void -display(void); - -/* static variables */ - -static GLFWwindow *win; -static bool redraw_pending; - -static bool vk_enable_layers = true; - -static int win_w = 800; -static int win_h = 600; - -static struct vk_ctx vk_core; -static VkSurfaceKHR vk_surf; -static struct vk_renderer vk_rnd; -static struct vk_swapchain vk_chain; -static struct vk_semaphores vk_sema; -static struct vk_attachment vk_depth_att; -static float vk_fb_color[4] = { 0.0, 0.0, 0.5, 1.0 }; - -/* make array later if many cmd buffers */ -static VkCommandBuffer vk_cmd_buf; - -/* empty for as long as we hardcode the vertices in the vertex shader */ -static struct vk_vertex_info vk_vert_info; - -int main(int argc, char** argv) -{ - atexit(cleanup); - - if (!init()) { - return 1; - } - - /* reshape window once just in case */ - - glfwGetWindowSize(win, &win_w, &win_h); - clb_reshape(win, win_w, win_h); - - /* event loop */ - redraw_pending = true; - - while(!glfwWindowShouldClose(win)) { - glfwWaitEvents(); - if (redraw_pending) { - redraw_pending = false; - display(); - } - } - - return 0; -} - -/* static functions */ - -static bool -init(void) -{ - char *vsdr = 0; - char *fsdr = 0; - int vsz, fsz; - - /* initialize GLFW */ - - if (!glfwInit()) { - fprintf(stderr, "Failed to initialize GLFW.\n"); - return false; - } - - if (glfwVulkanSupported() != GLFW_TRUE) { - fprintf(stderr, "Vulkan is not supported on this device.\n"); - return false; - } - - /* create window */ - - glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0); - - if (!win) { - fprintf(stderr, "Failed to create GLFW window\n"); - return false; - } - - glfwSetKeyCallback(win, clb_key); - - /* initialize Vulkan context (instance) */ - - if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) { - fprintf(stderr, "Failed to initialize Vulkan context.\n"); - return false; - } - - /* create (Xcb) surface */ - - glfwGetFramebufferSize(win, &win_h, &win_h); - if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf) - != VK_SUCCESS) { - fprintf(stderr, "Failed to create XCB surface.\n"); - return false; - } - - /* create semaphores */ - if (!vk_create_semaphores(&vk_core, false, &vk_sema)) { - fprintf(stderr, "No semaphores were created.\n"); - goto fail; - } - - /* create swapchain */ - if (!vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, 0, &vk_chain)) { - fprintf(stderr, "No swapchain was created.\n"); - goto fail; - } - - if (vk_chain.swapchain == VK_NULL_HANDLE) { - fprintf(stderr, "Invalid swapchain handle.\n"); - goto fail; - } - - /* create shaders */ - vsdr = sdr_load("data/main.vert.spv", &vsz); - fsdr = sdr_load("data/main.frag.spv", &fsz); - - /* create depth attachment (for the moment we are going to use this - * for all images */ - if (!vk_fill_image_props(&vk_core, - win_w, win_h, 1, - 1, 1, 1, - VK_FORMAT_D32_SFLOAT_S8_UINT, - VK_IMAGE_TILING_OPTIMAL, - VK_IMAGE_LAYOUT_UNDEFINED, - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, - false, true, false, - &vk_depth_att.props)) { - fprintf(stderr, "Unsupported depth image properties\n"); - return false; - } - if (!vk_create_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) { - fprintf(stderr, "Failed to create depth attachment.\n"); - goto fail; - } - - /* create renderer */ - if (!vk_create_renderer(&vk_core, - vsdr, vsz, fsdr, fsz, - win_w, win_h, 1, - false, false, - vk_chain.num_atts, vk_chain.atts, &vk_depth_att, - &vk_vert_info, &vk_rnd)) { - goto fail; - } - - /* create cmd buffer */ - if ((vk_cmd_buf = vk_create_cmd_buffer(&vk_core)) == VK_NULL_HANDLE) { - fprintf(stderr, "Failed to create command buffer.\n"); - goto fail; - } - - /* record cmd buffer */ - if (!vk_record_cmd_buffer(&vk_core, vk_cmd_buf, - &vk_rnd, 0, - 4, vk_fb_color, - vk_chain.num_atts + 1, 0, - 0, 0, win_w, win_h)) { - fprintf(stderr, "Failed to record command buffer.\n"); - goto fail; - } - - free(vsdr); - free(fsdr); - - /* set GLFW callbacks */ - - /* glfwSetWindowSizeCallback(win, clb_reshape); */ - - /* - glfwSetCursorPosCallback(win, clb_motion); - glfwSetMouseButtonCallback(win, clb_mouse); - */ - - return true; - -fail: - free(vsdr); - free(fsdr); - - return false; -} - -static void -display(void) -{ - uint32_t img_idx; - VkSubmitInfo sinfo; - VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - VkPresentInfoKHR pinfo; - - vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain, - UINT64_MAX, vk_sema.frame_ready, 0, &img_idx); - - memset(&sinfo, 0, sizeof sinfo); - sinfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - sinfo.waitSemaphoreCount = 1; - sinfo.pWaitSemaphores = &vk_sema.frame_ready; - sinfo.pWaitDstStageMask = &wait_stages; - sinfo.commandBufferCount = 1; - sinfo.pCommandBuffers = &vk_cmd_buf; - sinfo.signalSemaphoreCount = 1; - sinfo.pSignalSemaphores = &vk_sema.frame_done; - - if (vkQueueSubmit(vk_core.queue, 1, &sinfo, 0) != 0) { - fprintf(stderr, "Failed to submit draw commands.\n"); - abort(); - } - - memset(&pinfo, 0, sizeof pinfo); - pinfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; - pinfo.waitSemaphoreCount = 1; - pinfo.pWaitSemaphores = &vk_sema.frame_done; - pinfo.swapchainCount = 1; - pinfo.pSwapchains = &vk_chain.swapchain; - pinfo.pImageIndices = &img_idx; - - vkQueuePresentKHR(vk_core.queue, &pinfo); -} - -static void -cleanup(void) -{ - vkQueueWaitIdle(vk_core.queue); - - if (vk_cmd_buf != VK_NULL_HANDLE) { - vkResetCommandBuffer(vk_cmd_buf, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); - } - - vk_destroy_image(&vk_core, &vk_depth_att.obj); - vk_destroy_semaphores(&vk_core, &vk_sema); - vk_destroy_renderer(&vk_core, &vk_rnd); - - if (vk_chain.swapchain) { - vk_destroy_swapchain(&vk_core, &vk_chain); - vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0); - } - - glfwDestroyWindow(win); - - if (!vk_enable_layers) - vkFreeCommandBuffers(vk_core.dev, vk_core.cmd_pool, 1, &vk_cmd_buf); - - vk_cleanup_ctx(&vk_core, vk_enable_layers); - - glfwTerminate(); -} - -/* glfw callbacks */ - -static void -clb_key(GLFWwindow *win, int key, int scancode, - int action, int mods) -{ - if (action == GLFW_PRESS) { - switch(key) { - case GLFW_KEY_ESCAPE: - glfwSetWindowShouldClose(win, GLFW_TRUE); - return; - } - } -} - -static void -clb_reshape(GLFWwindow *win, int width, int height) -{ -} diff --git a/src/main.cc b/src/main.cc new file mode 100644 index 0000000..af4277c --- /dev/null +++ b/src/main.cc @@ -0,0 +1,301 @@ +#include + +#define GLFW_INCLUDE_VULKAN +#include +#include +#include + +#include "vk.h" +#include "util.h" + + +/* static glfw callbacks */ + +static void +clb_key(GLFWwindow *win, int key, int scancode, int action, int mods); + +static void +clb_reshape(GLFWwindow *win, int width, int height); + +/* static functions */ + +static bool +init(void); + +static void +cleanup(void); + +static void +display(void); + +/* static variables */ + +static GLFWwindow *win; +static bool redraw_pending; + +static bool vk_enable_layers = true; + +static int win_w = 800; +static int win_h = 600; + +static struct vk_ctx vk_core; +static VkSurfaceKHR vk_surf; +static struct vk_renderer vk_rnd; +static struct vk_swapchain vk_chain; +static struct vk_semaphores vk_sema; +static struct vk_attachment vk_depth_att; +static float vk_fb_color[4] = { 0.0, 0.0, 0.5, 1.0 }; + +/* make array later if many cmd buffers */ +static VkCommandBuffer vk_cmd_buf; + +/* empty for as long as we hardcode the vertices in the vertex shader */ +static struct vk_vertex_info vk_vert_info; + +int main(int argc, char** argv) +{ + atexit(cleanup); + + if (!init()) { + return 1; + } + + /* reshape window once just in case */ + + glfwGetWindowSize(win, &win_w, &win_h); + clb_reshape(win, win_w, win_h); + + /* event loop */ + redraw_pending = true; + + while(!glfwWindowShouldClose(win)) { + glfwWaitEvents(); + if (redraw_pending) { + redraw_pending = false; + display(); + } + } + + return 0; +} + +/* static functions */ + +static bool +init(void) +{ + char *vsdr = 0; + char *fsdr = 0; + int vsz, fsz; + + /* initialize GLFW */ + + if (!glfwInit()) { + fprintf(stderr, "Failed to initialize GLFW.\n"); + return false; + } + + if (glfwVulkanSupported() != GLFW_TRUE) { + fprintf(stderr, "Vulkan is not supported on this device.\n"); + return false; + } + + /* create window */ + + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + win = glfwCreateWindow(win_w, win_h, "helloworld rt", 0, 0); + + if (!win) { + fprintf(stderr, "Failed to create GLFW window\n"); + return false; + } + + glfwSetKeyCallback(win, clb_key); + + /* initialize Vulkan context (instance) */ + + if (!vk_init_ctx_for_rendering(&vk_core, true, vk_enable_layers)) { + fprintf(stderr, "Failed to initialize Vulkan context.\n"); + return false; + } + + /* create (Xcb) surface */ + + glfwGetFramebufferSize(win, &win_h, &win_h); + if (glfwCreateWindowSurface(vk_core.inst, win, 0, &vk_surf) + != VK_SUCCESS) { + fprintf(stderr, "Failed to create XCB surface.\n"); + return false; + } + + /* create semaphores */ + if (!vk_create_semaphores(&vk_core, false, &vk_sema)) { + fprintf(stderr, "No semaphores were created.\n"); + goto fail; + } + + /* create swapchain */ + if (!vk_create_swapchain(&vk_core, win_w, win_h, false, vk_surf, 0, &vk_chain)) { + fprintf(stderr, "No swapchain was created.\n"); + goto fail; + } + + if (vk_chain.swapchain == VK_NULL_HANDLE) { + fprintf(stderr, "Invalid swapchain handle.\n"); + goto fail; + } + + /* create shaders */ + vsdr = sdr_load("data/main.vert.spv", &vsz); + fsdr = sdr_load("data/main.frag.spv", &fsz); + + /* create depth attachment (for the moment we are going to use this + * for all images */ + if (!vk_fill_image_props(&vk_core, + win_w, win_h, 1, + 1, 1, 1, + VK_FORMAT_D32_SFLOAT_S8_UINT, + VK_IMAGE_TILING_OPTIMAL, + VK_IMAGE_LAYOUT_UNDEFINED, + VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + false, true, false, + &vk_depth_att.props)) { + fprintf(stderr, "Unsupported depth image properties\n"); + return false; + } + if (!vk_create_image(&vk_core, &vk_depth_att.props, &vk_depth_att.obj)) { + fprintf(stderr, "Failed to create depth attachment.\n"); + goto fail; + } + + /* create renderer */ + if (!vk_create_renderer(&vk_core, + vsdr, vsz, fsdr, fsz, + win_w, win_h, 1, + false, false, + vk_chain.num_atts, vk_chain.atts, &vk_depth_att, + &vk_vert_info, &vk_rnd)) { + goto fail; + } + + /* create cmd buffer */ + if ((vk_cmd_buf = vk_create_cmd_buffer(&vk_core)) == VK_NULL_HANDLE) { + fprintf(stderr, "Failed to create command buffer.\n"); + goto fail; + } + + /* record cmd buffer */ + if (!vk_record_cmd_buffer(&vk_core, vk_cmd_buf, + &vk_rnd, 0, + 4, vk_fb_color, + vk_chain.num_atts + 1, 0, + 0, 0, win_w, win_h)) { + fprintf(stderr, "Failed to record command buffer.\n"); + goto fail; + } + + free(vsdr); + free(fsdr); + + /* set GLFW callbacks */ + + /* glfwSetWindowSizeCallback(win, clb_reshape); */ + + /* + glfwSetCursorPosCallback(win, clb_motion); + glfwSetMouseButtonCallback(win, clb_mouse); + */ + + return true; + +fail: + free(vsdr); + free(fsdr); + + return false; +} + +static void +display(void) +{ + uint32_t img_idx; + VkSubmitInfo sinfo; + VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; + VkPresentInfoKHR pinfo; + + vkAcquireNextImageKHR(vk_core.dev, vk_chain.swapchain, + UINT64_MAX, vk_sema.frame_ready, 0, &img_idx); + + memset(&sinfo, 0, sizeof sinfo); + sinfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + sinfo.waitSemaphoreCount = 1; + sinfo.pWaitSemaphores = &vk_sema.frame_ready; + sinfo.pWaitDstStageMask = &wait_stages; + sinfo.commandBufferCount = 1; + sinfo.pCommandBuffers = &vk_cmd_buf; + sinfo.signalSemaphoreCount = 1; + sinfo.pSignalSemaphores = &vk_sema.frame_done; + + if (vkQueueSubmit(vk_core.queue, 1, &sinfo, 0) != 0) { + fprintf(stderr, "Failed to submit draw commands.\n"); + abort(); + } + + memset(&pinfo, 0, sizeof pinfo); + pinfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; + pinfo.waitSemaphoreCount = 1; + pinfo.pWaitSemaphores = &vk_sema.frame_done; + pinfo.swapchainCount = 1; + pinfo.pSwapchains = &vk_chain.swapchain; + pinfo.pImageIndices = &img_idx; + + vkQueuePresentKHR(vk_core.queue, &pinfo); +} + +static void +cleanup(void) +{ + vkQueueWaitIdle(vk_core.queue); + + if (vk_cmd_buf != VK_NULL_HANDLE) { + vkResetCommandBuffer(vk_cmd_buf, VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT); + } + + vk_destroy_image(&vk_core, &vk_depth_att.obj); + vk_destroy_semaphores(&vk_core, &vk_sema); + vk_destroy_renderer(&vk_core, &vk_rnd); + + if (vk_chain.swapchain) { + vk_destroy_swapchain(&vk_core, &vk_chain); + vkDestroySurfaceKHR(vk_core.inst, vk_surf, 0); + } + + glfwDestroyWindow(win); + + if (!vk_enable_layers) + vkFreeCommandBuffers(vk_core.dev, vk_core.cmd_pool, 1, &vk_cmd_buf); + + vk_cleanup_ctx(&vk_core, vk_enable_layers); + + glfwTerminate(); +} + +/* glfw callbacks */ + +static void +clb_key(GLFWwindow *win, int key, int scancode, + int action, int mods) +{ + if (action == GLFW_PRESS) { + switch(key) { + case GLFW_KEY_ESCAPE: + glfwSetWindowShouldClose(win, GLFW_TRUE); + return; + } + } +} + +static void +clb_reshape(GLFWwindow *win, int width, int height) +{ +}