Skip to content

Commit

Permalink
[*] WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmNotHanni committed Apr 11, 2024
1 parent 4097b45 commit e71a1e7
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 66 deletions.
6 changes: 3 additions & 3 deletions include/inexor/vulkan-renderer/render-graph/graphics_pass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <string>

// Forward declaration
namespace inexor::vulkan_renderer::wrapper {
namespace inexor::vulkan_renderer::wrapper::commands {
class CommandBuffer;
}

Expand All @@ -38,7 +38,7 @@ class GraphicsPass {
/// An optional clear value
std::optional<VkClearValue> m_clear_values{std::nullopt};
/// Add members which describe data related to graphics passes here
std::function<void(const wrapper::CommandBuffer &)> m_on_record{[](auto &) {}};
std::function<void(const wrapper::commands::CommandBuffer &)> m_on_record{[](auto &) {}};

/// The buffers the graphics passes reads from
/// If the buffer's ``BufferType`` is ``UNIFORM_BUFFER``, a value for the shader stage flag must be specified,
Expand Down Expand Up @@ -76,7 +76,7 @@ class GraphicsPass {
/// which case ``VK_ATTACHMENT_LOAD_OP_LOAD`` is used)
/// @exception std::runtime_error More than one index buffer is specified
GraphicsPass(std::string name, BufferReads buffer_reads, TextureReads texture_reads, TextureWrites texture_writes,
std::function<void(const wrapper::CommandBuffer &)> on_record,
std::function<void(const wrapper::commands::CommandBuffer &)> on_record,
std::optional<VkClearValue> clear_values);

GraphicsPass(const GraphicsPass &) = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <memory>
#include <utility>

namespace inexor::vulkan_renderer::wrapper::commands {
// Forward declaration
namespace inexor::vulkan_renderer::wrapper {
class CommandBuffer;
}
} // namespace inexor::vulkan_renderer::wrapper::commands

namespace inexor::vulkan_renderer::render_graph {

Expand All @@ -23,7 +23,7 @@ class GraphicsPassBuilder {
/// Indicates if the screen is cleared at the beginning of this stage
std::optional<VkClearValue> m_clear_value;
/// Add members which describe data related to graphics stages here
std::function<void(const wrapper::CommandBuffer &)> m_on_record;
std::function<void(const wrapper::commands::CommandBuffer &)> m_on_record;
/// Depth testing
bool m_depth_test;

Expand Down Expand Up @@ -126,7 +126,7 @@ class GraphicsPassBuilder {
/// Set the function which will be called when the stage's command buffer is being recorded
/// @param on_record The function which will be called when the stage's command buffer is being recorded
/// @return A const reference to the this pointer (allowing method calls to be chained)
[[nodiscard]] auto &set_on_record(std::function<void(const wrapper::CommandBuffer &)> on_record) {
[[nodiscard]] auto &set_on_record(std::function<void(const wrapper::commands::CommandBuffer &)> on_record) {
m_on_record = std::move(on_record);
return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions include/inexor/vulkan-renderer/render-graph/render_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ class RenderGraph {
/// @param is_first_pass ``true`` if this is the first pass in the graphics pass stack
/// @param is_last_pass ``true`` if this is the last pass in the graphics pass stack
/// @param img_index The swapchain image index
void record_command_buffer_for_pass(const wrapper::CommandBuffer &cmd_buf, const GraphicsPass &pass,
void record_command_buffer_for_pass(const wrapper::commands::CommandBuffer &cmd_buf, const GraphicsPass &pass,
bool is_first_pass, bool is_last_pass, std::uint32_t img_index);

/// Record all command buffers required for the passes
/// @param cmd_buf The command buffer to record all passes with
/// @param img_index The swapchain image index
void record_command_buffers(const wrapper::CommandBuffer &cmd_buf, std::uint32_t img_index);
void record_command_buffers(const wrapper::commands::CommandBuffer &cmd_buf, std::uint32_t img_index);

/// Update the vertex-, index-, and uniform-buffers
/// @note If a uniform buffer has been updated, an update of the associated descriptor set will be performed
Expand Down
18 changes: 10 additions & 8 deletions include/inexor/vulkan-renderer/wrapper/commands/command_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include <span>
#include <vector>

namespace inexor::vulkan_renderer::wrapper {
// Forward declaration
class Device;
} // namespace inexor::vulkan_renderer::wrapper

namespace inexor::vulkan_renderer::wrapper::pipelines {
// Forward declaration
class GraphicsPipeline;
Expand All @@ -19,18 +24,15 @@ namespace inexor::vulkan_renderer::render_graph {
class Buffer;
} // namespace inexor::vulkan_renderer::render_graph

namespace inexor::vulkan_renderer::wrapper {

// Forward declaration
class Device;
namespace inexor::vulkan_renderer::wrapper::commands {

/// RAII wrapper class for VkCommandBuffer
/// @todo Make trivially copyable (this class doesn't really "own" the command buffer, more just an OOP wrapper).
class CommandBuffer {
VkCommandBuffer m_cmd_buf{VK_NULL_HANDLE};
const Device &m_device;
std::string m_name;
std::unique_ptr<Fence> m_wait_fence;
std::unique_ptr<synchronization::Fence> m_wait_fence;

// The Device wrapper must be able to call begin_command_buffer and end_command_buffer
friend class Device;
Expand Down Expand Up @@ -89,7 +91,7 @@ class CommandBuffer {
/// @param device A const reference to the device wrapper class
/// @param cmd_pool The command pool from which the command buffer will be allocated
/// @param name The internal debug marker name of the command buffer (must not be empty)
CommandBuffer(const Device &device, VkCommandPool cmd_pool, std::string name);
CommandBuffer(const wrapper::Device &device, VkCommandPool cmd_pool, std::string name);

CommandBuffer(const CommandBuffer &) = delete;
CommandBuffer(CommandBuffer &&) noexcept;
Expand Down Expand Up @@ -373,7 +375,7 @@ class CommandBuffer {
return m_cmd_buf;
}

[[nodiscard]] const Fence &get_wait_fence() const {
[[nodiscard]] const synchronization::Fence &get_wait_fence() const {
return *m_wait_fence;
}

Expand Down Expand Up @@ -407,4 +409,4 @@ class CommandBuffer {
const CommandBuffer &submit_and_wait() const; // NOLINT
};

} // namespace inexor::vulkan_renderer::wrapper
} // namespace inexor::vulkan_renderer::wrapper::commands
16 changes: 9 additions & 7 deletions include/inexor/vulkan-renderer/wrapper/commands/command_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@
#include <cassert>

namespace inexor::vulkan_renderer::wrapper {

// Forward declaration
class Device;
} // namespace inexor::vulkan_renderer::wrapper

/// @brief RAII wrapper class for VkCommandPool.
namespace inexor::vulkan_renderer::wrapper::commands {

/// RAII wrapper class for VkCommandPool
class CommandPool {
std::string m_name;
const Device &m_device;
const wrapper::Device &m_device;
VkCommandPool m_cmd_pool{VK_NULL_HANDLE};

/// The command buffers which can be requested by the current thread
std::vector<std::unique_ptr<CommandBuffer>> m_cmd_bufs;
std::vector<std::unique_ptr<commands::CommandBuffer>> m_cmd_bufs;

public:
/// Default constructor
/// @param device The device wrapper instance
/// @param name The internal debug marker name which will be assigned to this command pool
CommandPool(const Device &device, std::string name);
CommandPool(const wrapper::Device &device, std::string name);

CommandPool(const CommandPool &) = delete;
CommandPool(CommandPool &&) noexcept;
Expand All @@ -46,7 +48,7 @@ class CommandPool {
/// Request a command buffer
/// @param name The internal debug name which will be assigned to this command buffer (must not be empty)
/// @return A command buffer handle instance which allows access to the requested command buffer
[[nodiscard]] const CommandBuffer &request_command_buffer(const std::string &name);
[[nodiscard]] const commands::CommandBuffer &request_command_buffer(const std::string &name);
};

} // namespace inexor::vulkan_renderer::wrapper
} // namespace inexor::vulkan_renderer::wrapper::commands
9 changes: 5 additions & 4 deletions include/inexor/vulkan-renderer/wrapper/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Device {

/// According to NVidia, we should aim for one command pool per thread
/// https://developer.nvidia.com/blog/vulkan-dos-donts/
mutable std::vector<std::unique_ptr<CommandPool>> m_cmd_pools;
mutable std::vector<std::unique_ptr<commands::CommandPool>> m_cmd_pools;
mutable std::mutex m_mutex;

/// Set the debug name of a Vulkan object using debug utils extension (VK_EXT_debug_utils)
Expand All @@ -66,7 +66,7 @@ class Device {

/// Get the thread_local command pool
/// @note This method will create a command pool for the thread if it doesn't already exist
CommandPool &thread_graphics_pool() const;
commands::CommandPool &thread_graphics_pool() const;

public:
/// Pick the best physical device automatically
Expand Down Expand Up @@ -144,7 +144,8 @@ class Device {
/// submits it and waits for it.
/// @param name The internal debug name of the command buffer (must not be empty)
/// @param cmd_lambda The command lambda to execute
void execute(const std::string &name, const std::function<void(const CommandBuffer &cmd_buf)> &cmd_lambda) const;
void execute(const std::string &name,
const std::function<void(const commands::CommandBuffer &cmd_buf)> &cmd_lambda) const;

/// Find a queue family index that suits a specific criteria
/// @param criteria_lambda The lambda to sort out unsuitable queue families
Expand Down Expand Up @@ -203,7 +204,7 @@ class Device {
/// Request a command buffer from the thread_local command pool
/// @param name The name which will be assigned to the command buffer
/// @return A command buffer from the thread_local command pool
[[nodiscard]] const CommandBuffer &request_command_buffer(const std::string &name);
[[nodiscard]] const commands::CommandBuffer &request_command_buffer(const std::string &name);

template <typename VulkanObjectType>
void set_debug_name(const VulkanObjectType vk_object, const std::string &name) const {
Expand Down
8 changes: 6 additions & 2 deletions include/inexor/vulkan-renderer/wrapper/pipelines/pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

namespace inexor::vulkan_renderer::wrapper {
// Forward declarations
class CommandBuffer;
class Device;
} // namespace inexor::vulkan_renderer::wrapper

namespace inexor::vulkan_renderer::wrapper::commands {
// Forward declarations
class CommandBuffer;
} // namespace inexor::vulkan_renderer::wrapper::commands

namespace inexor::vulkan_renderer::render_graph {
// Forward declaration
class RenderGraph;
Expand All @@ -22,7 +26,7 @@ namespace inexor::vulkan_renderer::wrapper::pipelines {
// TODO: Compute pipelines
class GraphicsPipeline {
// The CommandBuffer wrapper needs to access m_pipeline
friend CommandBuffer;
friend commands::CommandBuffer;
friend render_graph::RenderGraph;

private:
Expand Down
10 changes: 7 additions & 3 deletions include/inexor/vulkan-renderer/wrapper/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
#include <optional>
#include <vector>

namespace inexor::vulkan_renderer::wrapper::synchronization {
// Forward declaration
class Semaphore;
} // namespace inexor::vulkan_renderer::wrapper::synchronization

namespace inexor::vulkan_renderer::wrapper {

// Forward declarations
// Forward declaration
class Device;
class Semaphore;

/// RAII wrapper class for swapchains
class Swapchain {
Expand All @@ -24,7 +28,7 @@ class Swapchain {
std::vector<VkImage> m_imgs;
std::vector<VkImageView> m_img_views;
VkExtent2D m_extent{};
std::unique_ptr<Semaphore> m_img_available;
std::unique_ptr<synchronization::Semaphore> m_img_available;
bool m_vsync_enabled{false};
std::uint32_t m_img_index;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <string>

namespace inexor::vulkan_renderer::wrapper {

// Forward declaration
class Device;
} // namespace inexor::vulkan_renderer::wrapper

namespace inexor::vulkan_renderer::wrapper::synchronization {

/// A RAII wrapper for VkFence
class Fence {
Expand All @@ -23,7 +25,7 @@ class Fence {
/// @param name The internal debug marker name of the VkFence.
/// @param in_signaled_state True if the VkFence will be constructed in signaled state, false otherwise.
/// @warning Make sure to specify in_signaled_state correctly as needed, otherwise synchronization problems occur.
Fence(const Device &device, const std::string &name, bool in_signaled_state);
Fence(const wrapper::Device &device, const std::string &name, bool in_signaled_state);

Fence(const Fence &) = delete;
Fence(Fence &&) noexcept;
Expand All @@ -49,4 +51,4 @@ class Fence {
[[nodiscard]] VkResult status() const;
};

} // namespace inexor::vulkan_renderer::wrapper
} // namespace inexor::vulkan_renderer::wrapper::synchronization
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <string>

namespace inexor::vulkan_renderer::wrapper {

// Forward declaration
class Device;
} // namespace inexor::vulkan_renderer::wrapper

namespace inexor::vulkan_renderer::wrapper::synchronization {

/// RAII wrapper class for VkSemaphore
class Semaphore {
Expand All @@ -34,4 +36,4 @@ class Semaphore {
}
};

} // namespace inexor::vulkan_renderer::wrapper
} // namespace inexor::vulkan_renderer::wrapper::synchronization
2 changes: 1 addition & 1 deletion src/vulkan-renderer/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ void Application::setup_render_graph() {
.color = {1.0f, 0.0f, 0.0f},
})
.set_depth_test(true)
.set_on_record([&](const wrapper::CommandBuffer &cmd_buf) {
.set_on_record([&](const wrapper::commands::CommandBuffer &cmd_buf) {
cmd_buf.bind_pipeline(*m_octree_pipeline)
.bind_vertex_buffer(m_vertex_buffer)
.bind_index_buffer(m_index_buffer)
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan-renderer/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ ImGuiOverlay::ImGuiOverlay(const wrapper::Device &device, render_graph::RenderGr
m_push_const_block.scale =
glm::vec2(2.0f / io.DisplaySize.x, 2.0f / io.DisplaySize.y);
})
.set_on_record([&](const wrapper::CommandBuffer &cmd_buf) {
.set_on_record([&](const wrapper::commands::CommandBuffer &cmd_buf) {
cmd_buf.bind_pipeline(*m_imgui_pipeline);
ImDrawData *draw_data = ImGui::GetDrawData();
if (draw_data == nullptr) {
Expand Down
3 changes: 2 additions & 1 deletion src/vulkan-renderer/render-graph/graphics_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace inexor::vulkan_renderer::render_graph {

GraphicsPass::GraphicsPass(std::string name, BufferReads buffer_reads, TextureReads texture_reads,
TextureWrites texture_writes, std::function<void(const wrapper::CommandBuffer &)> on_record,
TextureWrites texture_writes,
std::function<void(const wrapper::commands::CommandBuffer &)> on_record,
std::optional<VkClearValue> clear_values)
: m_name(std::move(name)), m_buffer_reads(std::move(buffer_reads)), m_texture_reads(std::move(texture_reads)),
m_texture_writes(std::move(texture_writes)), m_on_record(std::move(on_record)),
Expand Down
9 changes: 5 additions & 4 deletions src/vulkan-renderer/render-graph/render_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ void RenderGraph::determine_pass_order() {
// TODO: Implement dfs
}

void RenderGraph::record_command_buffer_for_pass(const wrapper::CommandBuffer &cmd_buf, const GraphicsPass &pass,
const bool is_first_pass, const bool is_last_pass,
const std::uint32_t img_index) {
void RenderGraph::record_command_buffer_for_pass(const wrapper::commands::CommandBuffer &cmd_buf,
const GraphicsPass &pass, const bool is_first_pass,
const bool is_last_pass, const std::uint32_t img_index) {
// Start a new debug label for this graphics pass
// These debug labels are visible in RenderDoc
// TODO: Generate color gradient depending on the number of passes? (Interpolate e.g. in 12 steps for 12 passes)
Expand Down Expand Up @@ -283,7 +283,8 @@ void RenderGraph::record_command_buffer_for_pass(const wrapper::CommandBuffer &c
cmd_buf.end_debug_label_region();
}

void RenderGraph::record_command_buffers(const wrapper::CommandBuffer &cmd_buf, const std::uint32_t img_index) {
void RenderGraph::record_command_buffers(const wrapper::commands::CommandBuffer &cmd_buf,
const std::uint32_t img_index) {
// TODO: Support multiple passes per command buffer, not just recording everything into one command buffer
// TODO: Record command buffers in parallel

Expand Down
8 changes: 4 additions & 4 deletions src/vulkan-renderer/wrapper/commands/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <memory>
#include <utility>

namespace inexor::vulkan_renderer::wrapper {
namespace inexor::vulkan_renderer::wrapper::commands {

CommandBuffer::CommandBuffer(const Device &device, const VkCommandPool cmd_pool, std::string name)
CommandBuffer::CommandBuffer(const wrapper::Device &device, const VkCommandPool cmd_pool, std::string name)
: m_device(device), m_name(std::move(name)) {
const auto cmd_buf_ai = make_info<VkCommandBufferAllocateInfo>({
.commandPool = cmd_pool,
Expand All @@ -29,7 +29,7 @@ CommandBuffer::CommandBuffer(const Device &device, const VkCommandPool cmd_pool,

m_device.set_debug_name(m_cmd_buf, m_name);

m_wait_fence = std::make_unique<Fence>(m_device, m_name, false);
m_wait_fence = std::make_unique<synchronization::Fence>(m_device, m_name, false);
}

CommandBuffer::CommandBuffer(CommandBuffer &&other) noexcept : m_device(other.m_device) {
Expand Down Expand Up @@ -362,4 +362,4 @@ const CommandBuffer &CommandBuffer::submit_and_wait() const {
}));
}

} // namespace inexor::vulkan_renderer::wrapper
} // namespace inexor::vulkan_renderer::wrapper::commands
Loading

0 comments on commit e71a1e7

Please sign in to comment.