Skip to content

Commit

Permalink
[renderer] Integrate ImGUI with render graph
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetari committed May 16, 2021
1 parent 01a1dbe commit bdf97ca
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 632 deletions.
66 changes: 28 additions & 38 deletions include/inexor/vulkan-renderer/imgui.hpp
Original file line number Diff line number Diff line change
@@ -1,77 +1,67 @@
#pragma once

#include "inexor/vulkan-renderer/wrapper/command_buffer.hpp"
#include "inexor/vulkan-renderer/wrapper/command_pool.hpp"
#include "inexor/vulkan-renderer/render_graph.hpp"
#include "inexor/vulkan-renderer/wrapper/descriptor.hpp"
#include "inexor/vulkan-renderer/wrapper/device.hpp"
#include "inexor/vulkan-renderer/wrapper/fence.hpp"
#include "inexor/vulkan-renderer/wrapper/framebuffer.hpp"
#include "inexor/vulkan-renderer/wrapper/gpu_texture.hpp"
#include "inexor/vulkan-renderer/wrapper/graphics_pipeline.hpp"
#include "inexor/vulkan-renderer/wrapper/make_info.hpp"
#include "inexor/vulkan-renderer/wrapper/mesh_buffer.hpp"
#include "inexor/vulkan-renderer/wrapper/renderpass.hpp"
#include "inexor/vulkan-renderer/wrapper/shader.hpp"
#include "inexor/vulkan-renderer/wrapper/swapchain.hpp"

#include <glm/vec2.hpp>
#include <imgui.h>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_core.h>

#include <memory>
#include <vector>

namespace inexor::vulkan_renderer::wrapper {

class Device;
class Swapchain;

} // namespace inexor::vulkan_renderer::wrapper

namespace inexor::vulkan_renderer {

class ImGUIOverlay {
const wrapper::Device &m_device;
const wrapper::Swapchain &m_swapchain;
VkPipelineLayout m_pipeline_layout{VK_NULL_HANDLE};

float m_scale{1.0f};

std::unique_ptr<wrapper::MeshBuffer<ImDrawVert, ImDrawIdx>> m_imgui_mesh;
BufferResource *m_index_buffer{nullptr};
BufferResource *m_vertex_buffer{nullptr};
GraphicsStage *m_stage{nullptr};

std::unique_ptr<wrapper::GpuTexture> m_imgui_texture;
std::unique_ptr<wrapper::RenderPass> m_renderpass;
std::unique_ptr<wrapper::Shader> m_vert_shader;
std::unique_ptr<wrapper::Shader> m_frag_shader;
std::unique_ptr<wrapper::CommandPool> m_command_pool;
std::unique_ptr<wrapper::Shader> m_vertex_shader;
std::unique_ptr<wrapper::Shader> m_fragment_shader;
std::unique_ptr<wrapper::ResourceDescriptor> m_descriptor;
std::unique_ptr<wrapper::GraphicsPipeline> m_pipeline;
std::unique_ptr<wrapper::Fence> m_ui_rendering_finished;

std::uint32_t m_subpass{0};
std::uint32_t m_vertex_count{0};
std::uint32_t m_index_count{0};
std::vector<std::uint32_t> m_index_data;
std::vector<ImDrawVert> m_vertex_data;

std::vector<VkPipelineShaderStageCreateInfo> m_shaders;
std::vector<std::unique_ptr<wrapper::CommandBuffer>> m_command_buffers;
std::vector<std::unique_ptr<wrapper::Framebuffer>> m_framebuffers;

// TODO: Implement an RAII wrapper for push constants!
struct PushConstBlock {
glm::vec2 scale;
glm::vec2 translate;
} m_push_const_block{};

public:
/// @brief Default constructor
/// @param device A reference to the device wrapper.
/// @param swapchain A reference to the swapchain.
ImGUIOverlay(const wrapper::Device &device, const wrapper::Swapchain &swapchain);
/// @brief Construct a new ImGUI overlay.
/// @param device A reference to the device wrapper
/// @param swapchain A reference to the swapchain
/// @param render_graph A pointer to the render graph
/// @param back_buffer A pointer to the target of the ImGUI rendering
ImGUIOverlay(const wrapper::Device &device, const wrapper::Swapchain &swapchain, RenderGraph *render_graph,
TextureResource *back_buffer);
ImGUIOverlay(const ImGUIOverlay &) = delete;
ImGUIOverlay(ImGUIOverlay &&) noexcept = delete;
ImGUIOverlay(ImGUIOverlay &&) = delete;
~ImGUIOverlay();

ImGUIOverlay &operator=(const ImGUIOverlay &) = delete;
ImGUIOverlay &operator=(ImGUIOverlay &&) = delete;

[[nodiscard]] float get_scale() const {
void update();

[[nodiscard]] float scale() const {
return m_scale;
}

void update();
void render(std::uint32_t image_index);
};

} // namespace inexor::vulkan_renderer
37 changes: 34 additions & 3 deletions include/inexor/vulkan-renderer/render_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "inexor/vulkan-renderer/wrapper/device.hpp"
#include "inexor/vulkan-renderer/wrapper/fence.hpp"
#include "inexor/vulkan-renderer/wrapper/framebuffer.hpp"
#include "inexor/vulkan-renderer/wrapper/semaphore.hpp"
#include "inexor/vulkan-renderer/wrapper/shader.hpp"
#include "inexor/vulkan-renderer/wrapper/swapchain.hpp"

Expand Down Expand Up @@ -99,6 +100,12 @@ class BufferResource : public RenderResource {
/// @note Calling this function is only valid on buffers of type BufferUsage::VERTEX_BUFFER!
void add_vertex_attribute(VkFormat format, std::uint32_t offset);

/// @brief Specifies the element size of the buffer upfront if data is not to be uploaded immediately.
/// @param element_size The element size in bytes
void set_element_size(std::size_t element_size) {
m_element_size = element_size;
}

/// @brief Specifies that data should be uploaded to this buffer during render graph compilation
/// @param count The number of elements (not bytes) to upload from CPU memory to GPU memory
/// @param data A pointer to a contiguous block of memory that is at least `count * sizeof(T)` bytes long
Expand Down Expand Up @@ -156,7 +163,8 @@ class RenderStage : public RenderGraphObject {
std::vector<const RenderResource *> m_reads;

std::vector<VkDescriptorSetLayout> m_descriptor_layouts;
std::function<void(const PhysicalStage &, const wrapper::CommandBuffer &)> m_on_record;
std::vector<VkPushConstantRange> m_push_constant_ranges;
std::function<void(const PhysicalStage &, const wrapper::CommandBuffer &)> m_on_record{[](auto &, auto &) {}};

protected:
explicit RenderStage(std::string name) : m_name(std::move(name)) {}
Expand All @@ -182,6 +190,12 @@ class RenderStage : public RenderGraphObject {
m_descriptor_layouts.push_back(layout);
}

/// @brief Add a push constant range to this render stage.
/// @param range The push constant range
void add_push_constant_range(VkPushConstantRange range) {
m_push_constant_ranges.push_back(range);
}

/// @brief Specifies a function that will be called during command buffer recordation for this stage
/// @details This function can be used to specify other vulkan commands during command buffer recordation. The most
/// common use for this is for draw commands.
Expand All @@ -195,6 +209,9 @@ class GraphicsStage : public RenderStage {

private:
bool m_clears_screen{false};
bool m_depth_test{false};
bool m_depth_write{false};
VkPipelineColorBlendAttachmentState m_blend_attachment{};
std::unordered_map<const BufferResource *, std::uint32_t> m_buffer_bindings;
std::vector<VkPipelineShaderStageCreateInfo> m_shaders;

Expand All @@ -212,6 +229,20 @@ class GraphicsStage : public RenderStage {
m_clears_screen = clears_screen;
}

/// @brief Specifies the depth options for this stage.
/// @param depth_test Whether depth testing should be performed
/// @param depth_write Whether depth writing should be performed
void set_depth_options(bool depth_test, bool depth_write) {
m_depth_test = depth_test;
m_depth_write = depth_write;
}

/// @brief Set the blend attachment for this stage.
/// @param blend_attachment The blend attachment
void set_blend_attachment(VkPipelineColorBlendAttachmentState blend_attachment) {
m_blend_attachment = blend_attachment;
}

/// @brief Specifies that `buffer` should map to `binding` in the shaders of this stage
void bind_buffer(const BufferResource *buffer, std::uint32_t binding);

Expand Down Expand Up @@ -298,6 +329,7 @@ class PhysicalStage : public RenderGraphObject {
private:
std::vector<wrapper::CommandBuffer> m_command_buffers;
const wrapper::Device &m_device;
std::unique_ptr<wrapper::Semaphore> m_finished_semaphore;
VkPipeline m_pipeline{VK_NULL_HANDLE};
VkPipelineLayout m_pipeline_layout{VK_NULL_HANDLE};

Expand Down Expand Up @@ -393,8 +425,7 @@ class RenderGraph {

/// @brief Submits the command frame's command buffers for drawing
/// @param image_index The current frame, typically retrieved from vkAcquireNextImageKhr
void render(int image_index, VkFence signal_fence, VkSemaphore signal_semaphore, VkSemaphore wait_semaphore,
VkQueue graphics_queue);
VkSemaphore render(int image_index, VkSemaphore wait_semaphore, VkQueue graphics_queue);
};

template <typename T>
Expand Down
4 changes: 2 additions & 2 deletions include/inexor/vulkan-renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ class VulkanRenderer {
std::unique_ptr<wrapper::Swapchain> m_swapchain;
std::unique_ptr<wrapper::CommandPool> m_command_pool;
std::unique_ptr<ImGUIOverlay> m_imgui_overlay;
std::unique_ptr<wrapper::Fence> m_frame_finished_fence;
std::unique_ptr<wrapper::Semaphore> m_image_available_semaphore;
std::unique_ptr<wrapper::Semaphore> m_rendering_finished_semaphore;
std::unique_ptr<RenderGraph> m_render_graph;

std::vector<wrapper::Shader> m_shaders;
Expand All @@ -79,6 +77,8 @@ class VulkanRenderer {
std::vector<OctreeGpuVertex> m_octree_vertices;
std::vector<std::uint32_t> m_octree_indices;

TextureResource *m_back_buffer{nullptr};

// Render graph buffers for octree geometry.
BufferResource *m_index_buffer{nullptr};
BufferResource *m_vertex_buffer{nullptr};
Expand Down
7 changes: 7 additions & 0 deletions include/inexor/vulkan-renderer/wrapper/command_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class CommandBuffer {
/// @param layout The pipeline layout which will be used to bind the resource descriptor.
void bind_descriptor(const ResourceDescriptor &descriptor, VkPipelineLayout layout) const;

/// @brief Update push constant data.
/// @param layout The pipeline layout
/// @param stage The shader stage that will be accepting the push constants
/// @param size The size of the push constant data in bytes
/// @param data A pointer to the push constant data
void push_constants(VkPipelineLayout layout, VkShaderStageFlags stage, std::uint32_t size, void *data) const;

/// @brief Call vkEndCommandBuffer.
void end() const;

Expand Down
49 changes: 0 additions & 49 deletions include/inexor/vulkan-renderer/wrapper/graphics_pipeline.hpp

This file was deleted.

43 changes: 0 additions & 43 deletions include/inexor/vulkan-renderer/wrapper/renderpass.hpp

This file was deleted.

2 changes: 0 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ set(INEXOR_SOURCE_FILES
vulkan-renderer/wrapper/glfw_context.cpp
vulkan-renderer/wrapper/gpu_memory_buffer.cpp
vulkan-renderer/wrapper/gpu_texture.cpp
vulkan-renderer/wrapper/graphics_pipeline.cpp
vulkan-renderer/wrapper/image.cpp
vulkan-renderer/wrapper/instance.cpp
vulkan-renderer/wrapper/make_info.cpp
vulkan-renderer/wrapper/once_command_buffer.cpp
vulkan-renderer/wrapper/renderpass.cpp
vulkan-renderer/wrapper/semaphore.cpp
vulkan-renderer/wrapper/shader.cpp
vulkan-renderer/wrapper/staging_buffer.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/vulkan-renderer/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void Application::update_imgui_overlay() {
auto cursor_pos = m_input_data->get_cursor_pos();

ImGuiIO &io = ImGui::GetIO();
io.DeltaTime = std::clamp(m_time_passed, 0.001f, 100.0f);
io.DeltaTime = m_time_passed;
io.MousePos = ImVec2(static_cast<float>(cursor_pos[0]), static_cast<float>(cursor_pos[1]));
io.MouseDown[0] = m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_LEFT);
io.MouseDown[1] = m_input_data->is_mouse_button_pressed(GLFW_MOUSE_BUTTON_RIGHT);
Expand Down Expand Up @@ -542,7 +542,7 @@ void Application::update_imgui_overlay() {
ImGui::Text("Yaw: %.2f pitch: %.2f roll: %.2f", m_camera->yaw(), m_camera->pitch(), m_camera->roll());
const auto cam_fov = m_camera->fov();
ImGui::Text("Field of view: %d", static_cast<std::uint32_t>(cam_fov));
ImGui::PushItemWidth(150.0f * m_imgui_overlay->get_scale());
ImGui::PushItemWidth(150.0f * m_imgui_overlay->scale());
ImGui::PopItemWidth();
ImGui::End();
ImGui::PopStyleVar();
Expand Down
Loading

0 comments on commit bdf97ca

Please sign in to comment.