From 72995482b98ff2a014ddd737131a0935ead89977 Mon Sep 17 00:00:00 2001 From: Eleni Maria Stea Date: Thu, 16 Nov 2017 16:18:09 +0200 Subject: [PATCH] quick backup --- src/gfxapi.cc | 10 +-- src/gfxapi.h | 3 +- src/main.cc | 16 +++- src/opengl/opengl.cc | 12 ++- src/renderer.cc | 6 +- src/shader_manager.cc | 3 + src/vulkan/shader-vk.cc | 101 ++++++++++++++++++++++ src/vulkan/shader-vk.h | 52 +++++++++++ src/vulkan/vk.cc | 63 +++++++++++++- src/vulkan/vkutil.cc | 220 ++++++++++++++++++++++++++++++++++++++++++----- src/vulkan/vkutil.h | 24 +++++- 11 files changed, 471 insertions(+), 39 deletions(-) create mode 100644 src/vulkan/shader-vk.cc create mode 100644 src/vulkan/shader-vk.h diff --git a/src/gfxapi.cc b/src/gfxapi.cc index 5296677..f17055f 100644 --- a/src/gfxapi.cc +++ b/src/gfxapi.cc @@ -7,6 +7,7 @@ #include "opengl/texture-gl.h" #include "opengl/shader-gl.h" +#include "vulkan/shader-vk.h" #include "vulkan/mesh-vk.h" #include "vulkan/texture-vk.h" @@ -17,6 +18,7 @@ void (*gfx_viewport)(int x, int y, int width, int height); void (*gfx_zbuffer)(bool enable); void (*gfx_cull_face)(Gfx_cull_face cf); void (*gfx_reshape)(int width, int height); +void (*gfx_wireframe)(bool enable); bool gfx_init(Gfx_API api) { @@ -76,8 +78,7 @@ ShaderProgram *gfx_create_shader_program() case GFX_GL: return new ShaderProgramGL; case GFX_VK: - // return new ShaderProgramVK; - return 0; + return new ShaderProgramVK; } return 0; } @@ -88,8 +89,7 @@ Shader *gfx_create_shader() case GFX_GL: return new ShaderGL; case GFX_VK: - // return new ShaderVK; - return 0; + return new ShaderVK; } return 0; } @@ -103,4 +103,4 @@ char *gfx_get_shader_path() return (char *)"vk_shaders"; } return (char *)""; -} \ No newline at end of file +} diff --git a/src/gfxapi.h b/src/gfxapi.h index 62a8a3a..d766021 100644 --- a/src/gfxapi.h +++ b/src/gfxapi.h @@ -22,6 +22,7 @@ extern void (*gfx_viewport)(int x, int y, int width, int height); extern void (*gfx_zbuffer)(bool enable); extern void (*gfx_cull_face)(Gfx_cull_face cf); extern void (*gfx_reshape)(int width, int height); +extern void (*gfx_wireframe)(bool enable); bool gfx_init(Gfx_API api); void gfx_cleanup(); @@ -32,4 +33,4 @@ ShaderProgram *gfx_create_shader_program(); Shader *gfx_create_shader(); char *gfx_get_shader_path(); -#endif // GFXAPI_H_ \ No newline at end of file +#endif // GFXAPI_H_ diff --git a/src/main.cc b/src/main.cc index 4e2c72f..4d0dd42 100644 --- a/src/main.cc +++ b/src/main.cc @@ -264,12 +264,20 @@ static void clbk_key(GLFWwindow *win, int key, int scancode, int action, int mod break; // case 'F': - // fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1; - // break; + // fog_density = fog_density < 1 - 0.0009 ? fog_density + 0.0001 : 1; + // break; // case 'U': - // fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0; - // break; + // fog_density = fog_density > 0.0001 ? fog_density - 0.0001 : 0; + // break; + + case 'P': + gfx_wireframe(true); + break; + + case 'F': + gfx_wireframe(false); + break; default: break; diff --git a/src/opengl/opengl.cc b/src/opengl/opengl.cc index f6a7cef..66dd002 100644 --- a/src/opengl/opengl.cc +++ b/src/opengl/opengl.cc @@ -13,7 +13,8 @@ static void clear(float r, float g, float b); static void viewport(int x, int y, int width, int height); static void zbuffer(bool enable); static void cull_face(Gfx_cull_face cf); -static void reshape(int width, int height) {}; +static void reshape(int width, int height) {} +static void wireframe(bool enable); bool init_opengl() { @@ -43,6 +44,7 @@ bool init_opengl() gfx_zbuffer = zbuffer; gfx_cull_face = cull_face; gfx_reshape = reshape; + gfx_wireframe = wireframe; // glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); return true; @@ -91,3 +93,11 @@ static void cull_face(Gfx_cull_face cf) break; } } + +static void wireframe(bool enabled) +{ + if(enabled) + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + else + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +} diff --git a/src/renderer.cc b/src/renderer.cc index b20ef19..fd18d55 100644 --- a/src/renderer.cc +++ b/src/renderer.cc @@ -41,9 +41,9 @@ Renderer::~Renderer() bool Renderer::create() { //debug - if(!(nprog = sdr_man->create_shader_program("debug.v.glsl", "debug.f.glsl"))) { - fprintf(stderr, "Failed to load debug shaders.\n"); - } + // if(!(nprog = sdr_man->create_shader_program("debug.v.glsl", "debug.f.glsl"))) { + // fprintf(stderr, "Failed to load debug shaders.\n"); + // } if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) { return false; diff --git a/src/shader_manager.cc b/src/shader_manager.cc index 7ebbce6..5444f95 100644 --- a/src/shader_manager.cc +++ b/src/shader_manager.cc @@ -48,6 +48,9 @@ ShaderProgram *ShaderManager::create_shader_program(const char *vname, const cha { path = std::string(gfx_get_shader_path()); + if(!vname || !fname) + return 0; + Shader *vsdr = load_shader(vname, SDR_VERTEX); if(!vsdr) return 0; diff --git a/src/vulkan/shader-vk.cc b/src/vulkan/shader-vk.cc new file mode 100644 index 0000000..eb22b97 --- /dev/null +++ b/src/vulkan/shader-vk.cc @@ -0,0 +1,101 @@ +#include "vkutil.h" +#include "shader-vk.h" + +/* static variables */ +static vku_buffer ubo; + +ShaderVK::ShaderVK() +{ +} + +ShaderVK::~ShaderVK() +{ + destroy(); +} + +bool ShaderVK::create(char *buf, unsigned int bsz, const char *fname) +{ + return true; +} + +bool ShaderVK::load(const char *fname, SType type) +{ + return true; +} + +void ShaderVK::destroy() +{ +} + +ShaderProgramVK::ShaderProgramVK() +{ +} + +ShaderProgramVK::~ShaderProgramVK() +{ +} + +bool ShaderProgramVK::create() +{ + return true; +} + +bool ShaderProgramVK::link() +{ + return true; +} + +bool ShaderProgramVK::use() const +{ + return true; +} + +void ShaderProgramVK::destroy() +{ +} + +void ShaderProgramVK::attach_shader(Shader *shader) +{ +} + +int ShaderProgramVK::get_uniform_location(const char *name) const +{ + return 0; +} + +void ShaderProgramVK::set_uniformi(int location, int value) +{ +} + +void ShaderProgramVK::set_uniformi(int location, int x, int y) +{ +} + +void ShaderProgramVK::set_uniformi(int location, int x, int y, int z) +{ +} + +void ShaderProgramVK::set_uniformi(int location, int x, int y, int z, int w) +{ +} + + +void ShaderProgramVK::set_uniformf(int location, float value) +{ +} + +void ShaderProgramVK::set_uniformf(int location, float x, float y) +{ +} + +void ShaderProgramVK::set_uniformf(int location, float x, float y, float z) +{ +} + +void ShaderProgramVK::set_uniformf(int location, float x, float y, float z, float w) +{ +} + +void ShaderProgramVK::set_uniform_matrix(int location, const Mat4 &mat) +{ +} \ No newline at end of file diff --git a/src/vulkan/shader-vk.h b/src/vulkan/shader-vk.h new file mode 100644 index 0000000..88e07bc --- /dev/null +++ b/src/vulkan/shader-vk.h @@ -0,0 +1,52 @@ +#ifndef SHADER_VK_H_ +#define SHADER_VK_H_ + +#include +#include "shader.h" + +class ShaderVK : public Shader +{ +protected: + SType type; + std::string name; + + virtual bool create(char *buf, unsigned int bsz, const char *fname) override; + +public: + ShaderVK(); + virtual ~ShaderVK(); + + virtual bool load(const char *fname, SType type); + virtual void destroy() override; +}; + +class ShaderProgramVK : public ShaderProgram +{ +protected: + /*ubo*/ + +public: + ShaderProgramVK(); + virtual ~ShaderProgramVK(); + + virtual bool create() override; + virtual bool use() const override; + virtual bool link() override; + virtual void destroy() override; + virtual void attach_shader(Shader *shader) override; + + virtual int get_uniform_location(const char *name) const override; + + virtual void set_uniformi(int location, int value) override; + virtual void set_uniformi(int location, int x, int y) override; + virtual void set_uniformi(int location, int x, int y, int z) override; + virtual void set_uniformi(int location, int x, int y, int z, int w) override; + + virtual void set_uniformf(int location, float value) override; + virtual void set_uniformf(int location, float x, float y) override; + virtual void set_uniformf(int location, float x, float y, float z) override; + virtual void set_uniformf(int location, float x, float y, float z, float w) override; + + virtual void set_uniform_matrix(int location, const Mat4 &mat) override; +}; +#endif // SHADER_VK_H_ \ No newline at end of file diff --git a/src/vulkan/vk.cc b/src/vulkan/vk.cc index 871824f..b29c542 100644 --- a/src/vulkan/vk.cc +++ b/src/vulkan/vk.cc @@ -17,6 +17,10 @@ extern int win_h; /* static functions */ static void error_callback(int error, const char *descr); +static void clear(float r, float g, float b); +static void viewport(int x, int y, int width, int height); +static void zbuffer(bool enable); +static void cull_face(Gfx_cull_face cf); static void reshape(int width, int height); bool init_vulkan() @@ -54,6 +58,39 @@ bool init_vulkan() return false; } + if(!(vkswapchain = vku_create_swapchain(vksurface, win_w, win_h, 2, + VK_PRESENT_MODE_FIFO_KHR, 0))) { + fprintf(stderr, "Failed to create swapchain.\n"); + return false; + } + + vkswapchain_images = vku_get_swapchain_images(vkswapchain, &vknum_swapchain_images); + if(!vkswapchain_images) { + fprintf(stderr, "Failed to get swapchain images.\n"); + return false; + } + + vkswapchain_views = vku_create_image_views(vkswapchain_images, vknum_swapchain_images); + if(!vkswapchain_views) { + fprintf(stderr, "Failed to create swapchain image views.\n"); + delete [] vkswapchain_images; + return false; + } + + if(!vku_create_renderpass()) { + fprintf(stderr, "Failed to create renderpass'\n"); + return false; + } + + if(!vku_create_framebuffers(vkswapchain_views, vknum_swapchain_images, win_w, win_h)) { + fprintf(stderr, "Failed to create framebuffers.\n"); + return false; + } + + gfx_clear = clear; + gfx_viewport = viewport; + gfx_zbuffer = zbuffer; + gfx_cull_face = cull_face; gfx_reshape = reshape; return true; @@ -61,10 +98,16 @@ bool init_vulkan() void cleanup_vulkan() { + if(win) { + glfwDestroyWindow(win); + } + glfwTerminate(); + //TODOs according to the book: // 1- make sure all threads have been terminated (when I add threads) // 2- destroy objects in *reverse* order + vkDestroyRenderPass(vkdev, vkrpass, 0); vkDestroySurfaceKHR(vkinst, vksurface, 0); vku_cleanup(); @@ -85,7 +128,23 @@ static void reshape(int width, int height) } vkswapchain = sc; - free(vkswapchain_images); + delete [] vkswapchain_images; vkswapchain_images = vku_get_swapchain_images(sc, 0); vknext_swapchain_image = vku_get_next_image(vkswapchain); -} \ No newline at end of file +} + +static void clear(float r, float g, float b) +{ +} + +static void viewport(int x, int y, int width, int height) +{ +} + +static void zbuffer(bool enable) +{ +} + +static void cull_face(Gfx_cull_face cf) +{ +} diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc index 051ee4d..698e6f8 100644 --- a/src/vulkan/vkutil.cc +++ b/src/vulkan/vkutil.cc @@ -10,8 +10,13 @@ /* global variables */ +VkPipeline *vkgraphics_pipeline; +VkFramebuffer *vkfbufs; +VkRenderPass vkrpass; VkSwapchainKHR vkswapchain; VkImage *vkswapchain_images; +VkImageView *vkswapchain_views; +int vknum_swapchain_images; int vknext_swapchain_image; VkSurfaceKHR vksurface; VkInstance vkinst; @@ -46,10 +51,7 @@ bool vku_have_extension(const char *name) vkext_count = 0; vkEnumerateInstanceExtensionProperties(0, &vkext_count, 0); if(vkext_count) { - if(!(vkext = (VkExtensionProperties *)malloc(vkext_count * sizeof *vkext))) { - perror("failed to allocate instance extension list"); - return false; - } + vkext = new VkExtensionProperties[vkext_count]; vkEnumerateInstanceExtensionProperties(0, &vkext_count, vkext); printf("instance extensions:\n"); @@ -75,10 +77,7 @@ bool vku_have_device_extension(const char *name) vkdevext_count = 0; vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, 0); if(vkdevext_count) { - if(!(vkdevext = (VkExtensionProperties *)malloc(vkdevext_count * sizeof *vkdevext))) { - perror("failed to allocate device extension list"); - return false; - } + vkdevext = new VkExtensionProperties[vkdevext_count]; vkEnumerateDeviceExtensionProperties(phys_devices[sel_dev], 0, &vkdevext_count, vkdevext); printf("selected device extensions:\n"); @@ -137,7 +136,7 @@ bool vku_create_device() fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; } - phys_devices = (VkPhysicalDevice *)malloc(num_devices * sizeof *phys_devices); + phys_devices = new VkPhysicalDevice[num_devices]; if(vkEnumeratePhysicalDevices(vkinst, &num_devices, phys_devices) != 0) { fprintf(stderr, "failed to enumerate vulkan physical devices\n"); return false; @@ -179,7 +178,7 @@ bool vku_create_device() if(qprop_count <= 0) { continue; } - qprop = (VkQueueFamilyProperties *)malloc(qprop_count * sizeof *qprop); + qprop = new VkQueueFamilyProperties[qprop_count]; vkGetPhysicalDeviceQueueFamilyProperties(phys_devices[i], &qprop_count, qprop); for(unsigned int j=0; jbuf, 0); - free(buf); + delete buf; } } @@ -754,6 +860,80 @@ const char *vku_get_vulkan_error_str(VkResult error) return errmsg.c_str(); } +bool vku_create_graphics_pipeline(VkPipelineLayout *layout) +{ + VkGraphicsPipelineCreateInfo inf; + memset(&inf, 0, sizeof inf); + + inf.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; + inf.layout = *layout; + inf.renderPass = vkrpass; + + /* states */ + + /* how primitives are assembled */ + VkPipelineInputAssemblyStateCreateInfo ias; + memset(&ias, 0, sizeof ias); + ias.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; + ias.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + + /* rasterisation */ + VkPipelineRasterizationStateCreateInfo rsi; + memset(&rsi, 0, sizeof rsi); + rsi.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; + rsi.polygonMode = VK_POLYGON_MODE_FILL; + rsi.cullMode = VK_CULL_MODE_BACK_BIT; + rsi.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + rsi.lineWidth = 1.0f; + + /* blend factors */ + VkPipelineColorBlendAttachmentState bas[1]; + memset(&bas[0], 0, sizeof bas[0]); + + VkPipelineColorBlendStateCreateInfo cbs; + memset(&cbs, 0, sizeof cbs); + cbs.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; + cbs.attachmentCount = 1; + cbs.pAttachments = bas; + + /* number of viewport and scissors in this pipeline */ + VkPipelineViewportStateCreateInfo vs; + memset(&vs, 0, sizeof vs); + vs.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; + vs.viewportCount = 1; + vs.scissorCount = 1; + + /* dynamic states: that can be changed later */ + std::vector ds_enabled; + ds_enabled.push_back(VK_DYNAMIC_STATE_VIEWPORT); + //ds_enabled.push_back(VK_DYNAMIC_STATE_SCISSOR); + VkPipelineDynamicStateCreateInfo ds; + ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; + ds.pDynamicStates = ds_enabled.data(); + ds.dynamicStateCount = static_cast(ds_enabled.size()); + + /* depth tests */ + VkPipelineDepthStencilStateCreateInfo dsi; + memset(&dsi, 0, sizeof dsi); + dsi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; + dsi.depthTestEnable = VK_TRUE; + dsi.depthWriteEnable = VK_TRUE; + dsi.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; + dsi.back.failOp = VK_STENCIL_OP_KEEP; + dsi.back.passOp = VK_STENCIL_OP_KEEP; + dsi.back.compareOp = VK_COMPARE_OP_ALWAYS; + dsi.front = dsi.back; + + /* multisampling - must be set even if not used */ + VkPipelineMultisampleStateCreateInfo msi; + memset(&msi, 0, sizeof msi); + msi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; + msi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + + //TODO in progress + return true; +} + static const char *get_device_name_str(VkPhysicalDeviceType type) { switch(type) { diff --git a/src/vulkan/vkutil.h b/src/vulkan/vkutil.h index f9cbf94..853349f 100644 --- a/src/vulkan/vkutil.h +++ b/src/vulkan/vkutil.h @@ -2,9 +2,16 @@ #define VKUTIL_H_ #include +#include +extern VkPipeline *vkgraphics_pipeline; +extern VkDescriptorSet *vkdescset; +extern VkFramebuffer *vkfbufs; +extern VkRenderPass vkrpass; extern VkSwapchainKHR vkswapchain; extern VkImage *vkswapchain_images; +extern VkImageView *vkswapchain_views; +extern int vknum_swapchain_images; extern VkSurfaceKHR vksurface; extern VkInstance vkinst; extern VkPhysicalDevice vkpdev; @@ -36,24 +43,35 @@ void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf); void vku_begin_cmdbuf(VkCommandBuffer buf, unsigned int flags); void vku_end_cmdbuf(VkCommandBuffer buf); void vku_reset_cmdbuf(VkCommandBuffer buf); - void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence); /* swapchain */ VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n, VkPresentModeKHR pmode, VkSwapchainKHR prev); VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count); +VkImageView *vku_create_image_views(VkImage *images, int count); int vku_get_next_image(VkSwapchainKHR sc); void vku_present(VkSwapchainKHR sc, int img_idx); +/* rederpass */ +bool vku_create_renderpass(); + +/* framebuffer */ +bool vku_create_framebuffers(VkImageView *image_views, int count, int w, int h); + /* buffers */ vku_buffer *vku_create_buffer(int sz, unsigned int usage); void vku_destroy_buffer(vku_buffer *buf); - void vku_cmd_copybuf(VkCommandBuffer cmdbuf, VkBuffer dest, int doffs, VkBuffer src, int soffs, int size); /* other */ const char *vku_get_vulkan_error_str(VkResult error); -#endif // VKUTIL_H_ \ No newline at end of file +/* pipeline */ +bool vku_create_graphics_pipeline(VkPipelineLayout *layout); + +/* descriptor set */ +//bool vku_create_descriptor_set() + +#endif // VKUTIL_H_ -- 1.7.10.4