From 2299b3f89c13671e50cb40166af462eedfbef368 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Mon, 18 Dec 2023 14:00:55 +0000 Subject: [PATCH] Fix #11 --- src/graphics/pipeline.cpp | 30 +++++++++++--------------- src/graphics/pipeline.hpp | 11 +++++++++- src/graphics/rendertarget.cpp | 33 ++++++++++++++++------------- src/graphics/rendertarget.hpp | 16 +++++++++----- src/graphics/shader.cpp | 2 +- src/graphics/vkrenderer.cpp | 6 +++--- src/graphics/vkrenderer.hpp | 4 ++-- src/gui/gui.hpp | 3 ++- src/window.cpp | 40 +++++++++++++++-------------------- 9 files changed, 76 insertions(+), 69 deletions(-) diff --git a/src/graphics/pipeline.cpp b/src/graphics/pipeline.cpp index 71ce810..3171b27 100644 --- a/src/graphics/pipeline.cpp +++ b/src/graphics/pipeline.cpp @@ -14,26 +14,14 @@ namespace inferno::graphics { Pipeline* pipeline_create(GraphicsDevice* device, SwapChain* swap, Shader* shader, - uint32_t descriptorSetLayoutCount, VkDescriptorSetLayout* layouts) + uint32_t descriptorSetLayoutCount, VkDescriptorSetLayout* layouts, PipelineType type) { Pipeline* pipeline = new Pipeline(); - // memset(descriptorSetLayouts, 0, sizeof(VkDescriptorSetLayout)); - // memset(&layout, 0, sizeof(VkPipelineLayout)); - // memset(&graphicsPipeline, 0, sizeof(VkPipeline)); - // memset(&dynamicStates, 0, sizeof(VkPipelineDynamicStateCreateInfo)); - // memset(&vertexInputInfo, 0, sizeof(VkPipelineVertexInputStateCreateInfo)); - // memset(&inputAssembly, 0, sizeof(VkPipelineInputAssemblyStateCreateInfo)); - // memset(&viewportState, 0, sizeof(VkPipelineViewportStateCreateInfo)); - // memset(&rasterizer, 0, sizeof(VkPipelineRasterizationStateCreateInfo)); - // memset(&multisampling, 0, sizeof(VkPipelineMultisampleStateCreateInfo)); - // memset(&colorBlendAttachment, 0, sizeof(VkPipelineColorBlendAttachmentState)); - // memset(&colorBlending, 0, sizeof(VkPipelineColorBlendStateCreateInfo)); - // memset(&depthState, 0, sizeof(VkPipelineDepthStencilStateCreateInfo)); - pipeline->Device = device; pipeline->Swap = swap; pipeline->RelaventShader = shader; + pipeline->Type = type; pipeline->DescriptorSetLayoutCount = descriptorSetLayoutCount; pipeline->DescriptorSetLayouts = layouts; @@ -65,13 +53,18 @@ Pipeline* pipeline_create(GraphicsDevice* device, SwapChain* swap, Shader* shade VkPipelineRasterizationStateCreateInfo rasterizer {}; rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizer.depthClampEnable = VK_FALSE; // NOTE: This is for shadow mapping + rasterizer.polygonMode = VK_POLYGON_MODE_LINE; rasterizer.rasterizerDiscardEnable = VK_FALSE; rasterizer.polygonMode = VK_POLYGON_MODE_FILL; - rasterizer.lineWidth = 1.f; + rasterizer.lineWidth = 1.3f; rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; // rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE; rasterizer.depthBiasEnable = VK_FALSE; + if (type == PIPELINE_TYPE_GRAPHICS_LINE) { + rasterizer.polygonMode = VK_POLYGON_MODE_LINE; + rasterizer.cullMode = VK_CULL_MODE_NONE; + } VkPipelineMultisampleStateCreateInfo multisampling {}; multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; @@ -81,7 +74,7 @@ Pipeline* pipeline_create(GraphicsDevice* device, SwapChain* swap, Shader* shade VkPipelineColorBlendAttachmentState colorBlendAttachment {}; colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; - // Alpha blending basically + // Alpha blending colorBlendAttachment.blendEnable = VK_TRUE; colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; colorBlendAttachment.dstColorBlendFactor @@ -107,7 +100,7 @@ Pipeline* pipeline_create(GraphicsDevice* device, SwapChain* swap, Shader* shade depthState.depthTestEnable = VK_TRUE; depthState.depthWriteEnable = VK_TRUE; depthState.depthCompareOp = VK_COMPARE_OP_LESS; - depthState.depthBoundsTestEnable = VK_FALSE; + depthState.depthBoundsTestEnable = VK_TRUE; depthState.minDepthBounds = 0.0f; depthState.maxDepthBounds = 1.0f; depthState.stencilTestEnable = VK_FALSE; @@ -203,9 +196,10 @@ void pipeline_recreate(Pipeline* pipeline) Shader* shader = pipeline->RelaventShader; uint32_t descriptorSetLayoutCount = descriptorSetLayoutCount; VkDescriptorSetLayout* layouts = pipeline->DescriptorSetLayouts; + PipelineType type = pipeline->Type; pipeline_cleanup(pipeline); - pipeline = pipeline_create(device, swap, shader, descriptorSetLayoutCount, layouts); + pipeline = pipeline_create(device, swap, shader, descriptorSetLayoutCount, layouts, type); } } // namespace inferno::graphics diff --git a/src/graphics/pipeline.hpp b/src/graphics/pipeline.hpp index 5cf19e7..d7e9135 100644 --- a/src/graphics/pipeline.hpp +++ b/src/graphics/pipeline.hpp @@ -9,6 +9,13 @@ struct RenderPass; struct SwapChain; struct Shader; +typedef enum PipelineType { + PIPELINE_TYPE_GRAPHICS, + PIPELINE_TYPE_GRAPHICS_LINE, + PIPELINE_TYPE_COMPUTE, + PIPELINE_TYPE_RAYTRACING +} PipelineType; + typedef struct Pipeline { GraphicsDevice* Device; SwapChain* Swap; @@ -19,10 +26,12 @@ typedef struct Pipeline { VkPipeline GraphicsPipeline; VkPipelineLayout Layout; + + PipelineType Type; } Pipeline; Pipeline* pipeline_create(GraphicsDevice* device, SwapChain* swap, Shader* shader, - uint32_t descriptorSetLayoutCount, VkDescriptorSetLayout* layouts); + uint32_t descriptorSetLayoutCount, VkDescriptorSetLayout* layouts, PipelineType type); void pipeline_cleanup(Pipeline* pipeline); void pipeline_recreate(Pipeline* pipeline); diff --git a/src/graphics/rendertarget.cpp b/src/graphics/rendertarget.cpp index c4cd866..4a64369 100644 --- a/src/graphics/rendertarget.cpp +++ b/src/graphics/rendertarget.cpp @@ -43,12 +43,17 @@ RenderTarget* rendertarget_create( VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, target->Image, target->Memory); + target->ImageView = create_image_view(device, target->Image, format, VK_IMAGE_ASPECT_COLOR_BIT); target->DescriptorSet = ImGui_ImplVulkan_AddTexture( target->Sampler, target->ImageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); + if (depth) { + rendertarget_create_depth(target); + } + return target; } @@ -66,28 +71,26 @@ void rendertarget_cleanup(RenderTarget* target) void rendertarget_create_depth(RenderTarget* target) { VkFormat depthFormat = find_depth_format(target->Device); - target->DepthFormat = depthFormat; + + target->TargetDepth = new DepthAttachment(); + target->TargetDepth->Format = depthFormat; create_image(target->Device, target->Extent.width, target->Extent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, target->DepthImage.value(), - target->DepthMemory.value()); + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, target->TargetDepth->Image, + target->TargetDepth->Memory); - target->DepthImageView = create_image_view(target->Device, target->DepthImage.value(), + target->TargetDepth->ImageView = create_image_view(target->Device, target->TargetDepth->Image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT); } void rendertarget_destroy_depth(RenderTarget* target) { - if (target->DepthImageView.has_value()) { + if (target->TargetDepth != nullptr) { vkDestroyImageView( - target->Device->VulkanDevice, target->DepthImageView.value(), nullptr); - } - if (target->DepthImage.has_value()) { - vkDestroyImage(target->Device->VulkanDevice, target->DepthImage.value(), nullptr); - } - if (target->DepthMemory.has_value()) { - vkFreeMemory(target->Device->VulkanDevice, target->DepthMemory.value(), nullptr); + target->Device->VulkanDevice, target->TargetDepth->ImageView, nullptr); + vkDestroyImage(target->Device->VulkanDevice, target->TargetDepth->Image, nullptr); + vkFreeMemory(target->Device->VulkanDevice, target->TargetDepth->Memory, nullptr); } } @@ -95,7 +98,7 @@ void rendertarget_recreate(RenderTarget* target, VkExtent2D extent, VkFormat for { vkDeviceWaitIdle(target->Device->VulkanDevice); - bool doDepth = target->DepthImage.has_value(); + bool doDepth = target->TargetDepth != nullptr; rendertarget_cleanup(target); @@ -104,8 +107,8 @@ void rendertarget_recreate(RenderTarget* target, VkExtent2D extent, VkFormat for rendertarget_create(target->Device, extent, format, doDepth); - if (doDepth) - rendertarget_create_depth(target); + // if (doDepth) + // rendertarget_create_depth(target); } } diff --git a/src/graphics/rendertarget.hpp b/src/graphics/rendertarget.hpp index fcf6f83..f0e3c29 100644 --- a/src/graphics/rendertarget.hpp +++ b/src/graphics/rendertarget.hpp @@ -8,20 +8,26 @@ namespace inferno::graphics { struct GraphicsDevice; +// TODO: Should probably be "ImageAttachment" and then the +// target will abstract which sort of attachment it is +typedef struct DepthAttachment { + VkImage Image; + VkDeviceMemory Memory; + VkImageView ImageView; + VkFormat Format; +} DepthAttachment; + // TODO: What about the present mode? typedef struct RenderTarget { VkImage Image; VkDeviceMemory Memory; VkImageView ImageView; - std::optional DepthImage; - std::optional DepthMemory; - std::optional DepthImageView; - VkFormat Format; - VkFormat DepthFormat; VkExtent2D Extent; + DepthAttachment* TargetDepth = nullptr; + // NOTE: This is for the ImGui renderer.. it needs a descriptor set of a sampler of an // image VkSampler Sampler; diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 3a8f819..dbce32d 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -174,7 +174,7 @@ void shader_build(Shader* shader) } shader->GraphicsPipeline = pipeline_create(shader->Device, shader->GraphicsSwapchain, - shader, layouts.size(), layouts.data()); + shader, layouts.size(), layouts.data(), PIPELINE_TYPE_GRAPHICS); shader->GlobalUniformBuffer = uniform_buffer_create(shader->Device, true); } diff --git a/src/graphics/vkrenderer.cpp b/src/graphics/vkrenderer.cpp index 8246787..511fbf2 100644 --- a/src/graphics/vkrenderer.cpp +++ b/src/graphics/vkrenderer.cpp @@ -226,9 +226,9 @@ void renderer_begin_pass( attachmentInfo.clearValue = clearColor; VkRenderingAttachmentInfo depthAttachmentInfo; - if (target->DepthImage.has_value()) { + if (target->TargetDepth != nullptr) { depthAttachmentInfo.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR; - depthAttachmentInfo.imageView = target->DepthImageView.value(); + depthAttachmentInfo.imageView = target->TargetDepth->ImageView; depthAttachmentInfo.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR; depthAttachmentInfo.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachmentInfo.storeOp = VK_ATTACHMENT_STORE_OP_STORE; @@ -245,7 +245,7 @@ void renderer_begin_pass( renderingInfo.layerCount = 1; renderingInfo.colorAttachmentCount = 1; renderingInfo.pColorAttachments = &attachmentInfo; - if (target->DepthImage.has_value()) + if (target->TargetDepth != nullptr) renderingInfo.pDepthAttachment = &depthAttachmentInfo; vkCmdBeginRendering( diff --git a/src/graphics/vkrenderer.hpp b/src/graphics/vkrenderer.hpp index 9f5da00..a21c549 100644 --- a/src/graphics/vkrenderer.hpp +++ b/src/graphics/vkrenderer.hpp @@ -67,8 +67,8 @@ void renderer_submit_now(VulkanRenderer* renderer, bool renderer_begin_frame(VulkanRenderer* renderer); -void renderer_begin_pass(VulkanRenderer* renderer, RenderTarget* target, - VkRect2D renderArea); +void renderer_begin_pass( + VulkanRenderer* renderer, RenderTarget* target, VkRect2D renderArea); // this is for rendering to the swapchain / present image void renderer_begin_pass(VulkanRenderer* renderer, VkRect2D renderArea); diff --git a/src/gui/gui.hpp b/src/gui/gui.hpp index b4dbae0..c97ec5b 100644 --- a/src/gui/gui.hpp +++ b/src/gui/gui.hpp @@ -1,6 +1,7 @@ #pragma once #include "graphics.hpp" +#include "imgui/imgui.h" #include "style.hpp" #include "graphics/device.hpp" @@ -18,7 +19,7 @@ namespace inferno::gui { | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove \ | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus \ | ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoBackground \ - | ImGuiWindowFlags_NoDecoration + | ImGuiWindowFlags_NoDecoration | ImGuiMouseCursor_Arrow inline void imgui_init(graphics::VulkanRenderer* renderer) { diff --git a/src/window.cpp b/src/window.cpp index 29da2a8..0b2949f 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,7 +1,7 @@ #include "window.hpp" -#include "gui/style.hpp" #include "graphics/device.hpp" +#include "gui/style.hpp" #include #include @@ -21,8 +21,7 @@ static WINDOW_MODE WinMode = WINDOW_MODE::WIN_MODE_DEFAULT; static KeyCallback UserKeyCallback = nullptr; static int Width, Height; -void glfwKeyCallback(GLFWwindow* window, int key, int scancode, - int action, int mods) +void glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (UserKeyCallback != nullptr) { UserKeyCallback(key, scancode, action, mods); @@ -44,8 +43,6 @@ void setupWindow(std::string title) Window = glfwCreateWindow(Width, Height, title.c_str(), nullptr, nullptr); } - - void shutdownGLFW() { glfwDestroyWindow(Window); @@ -62,24 +59,20 @@ void window_create(std::string title, int width, int height) void window_init_device(GraphicsDevice* device, GLFWframebuffersizefun resizeCallback) { - if (glfwCreateWindowSurface(device->VulkanInstance, Window, nullptr, &device->VulkanSurface) != VK_SUCCESS) { - yolo::error("failed to create window surface!"); - exit(1); + if (glfwCreateWindowSurface( + device->VulkanInstance, Window, nullptr, &device->VulkanSurface) + != VK_SUCCESS) { + yolo::error("failed to create window surface!"); + exit(1); } glfwSetWindowUserPointer(Window, device); glfwSetFramebufferSizeCallback(Window, (GLFWframebuffersizefun)resizeCallback); } -void window_cleanup() -{ - shutdownGLFW(); -} +void window_cleanup() { shutdownGLFW(); } -void window_set_title(std::string title) -{ - glfwSetWindowTitle(Window, title.c_str()); -} +void window_set_title(std::string title) { glfwSetWindowTitle(Window, title.c_str()); } void window_set_size(int w, int h) { @@ -99,9 +92,9 @@ GLFWwindow* window_get_glfw_window() { return Window; } void window_set_mode(WINDOW_MODE mode) { WinMode = mode; - if (mode == WINDOW_MODE::WIN_MODE_FPS) { - glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - } + // if (mode == WINDOW_MODE::WIN_MODE_FPS) { + // glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + // } } void window_set_key_callback(KeyCallback callback) { UserKeyCallback = callback; } @@ -111,14 +104,15 @@ KeyCallback window_get_key_callback() { return UserKeyCallback; } bool window_new_frame() { glfwPollEvents(); - if (WinMode == WIN_MODE_FPS) { - glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - glfwSetCursorPos(Window, (double)Width / 2, (double)Height / 2); - } + // if (WinMode == WIN_MODE_FPS) { + // glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + // glfwSetCursorPos(Window, (double)Width / 2, (double)Height / 2); + // } if (glfwWindowShouldClose(Window)) { return false; } + glfwSetInputMode(Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glfwGetWindowSize(Window, &Width, &Height); return true;