Skip to content

Commit

Permalink
[WIP] Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmNotHanni committed Jul 16, 2024
1 parent f48bcba commit 46fea99
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 26 deletions.
33 changes: 32 additions & 1 deletion include/inexor/vulkan-renderer/render-graph/graphics_pass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "inexor/vulkan-renderer/render-graph/texture.hpp"
#include "inexor/vulkan-renderer/wrapper/descriptors/descriptor_set_layout.hpp"

#include <array>
#include <functional>
#include <memory>
#include <optional>
Expand All @@ -27,6 +28,32 @@ using wrapper::descriptors::DescriptorSetLayout;
/// An attachment is just a texture paired with an optional clear value
using Attachment = std::pair<std::weak_ptr<Texture>, std::optional<VkClearValue>>;

/// The debug label colors for vkCmdBeginDebugUtilsLabelEXT
enum class DebugLabelColor {
RED,
BLUE,
GREEN,
YELLOW,
PURPLE,
ORANGE,
MAGENTA,
CYAN,
BROWN,
PINK,
LIME,
TURQUOISE,
BEIGE,
MAROON,
OLIVE,
NAVY,
TEAL,
};

/// Convert a DebugLabelColor to an array of RGBA float values to pass to vkCmdBeginDebugUtilsLabelEXT
/// @param color The DebugLabelColor
/// @return An array of RGBA float values to be passed into vkCmdBeginDebugUtilsLabelEXT
[[nodiscard]] std::array<float, 4> get_debug_label_color(const DebugLabelColor color);

/// A wrapper for graphics passes inside of rendergraph
class GraphicsPass {
friend class RenderGraph;
Expand Down Expand Up @@ -66,6 +93,9 @@ class GraphicsPass {
/// The stencil attachment inside of m_rendering_info
VkRenderingAttachmentInfo m_stencil_attachment_info{};

/// The color of the debug label region (visible in graphics debuggers like RenderDoc)
std::array<float, 4> m_debug_label_color;

public:
/// Default constructor
/// @param name The name of the graphics pass
Expand All @@ -77,7 +107,8 @@ class GraphicsPass {
std::function<void(const CommandBuffer &)> on_record_cmd_buffer,
std::vector<Attachment> color_attachments,
Attachment depth_attachment,
Attachment stencil_attachment);
Attachment stencil_attachment,
DebugLabelColor color);

GraphicsPass(const GraphicsPass &) = delete;
GraphicsPass(GraphicsPass &&other) noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ class GraphicsPassBuilder {

/// Build the graphics pass
/// @param name The name of the graphics pass
/// @param color The debug label color (debug labels are specified per pass and are visible in RenderDoc debugger)
/// @return The graphics pass that was just created
[[nodiscard]] std::shared_ptr<GraphicsPass> build(std::string name);
[[nodiscard]] std::shared_ptr<GraphicsPass> build(std::string name, DebugLabelColor color);

/// Specify that this graphics pass A reads from another graphics pass B, meaning B should be rendered before A
/// @param graphics_pass The graphics pass which is read by this graphics pass
Expand Down
13 changes: 9 additions & 4 deletions include/inexor/vulkan-renderer/wrapper/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class Device {
/// @param required_features The required device features
/// @param required_extensions The required device extensions
/// @return The chosen physical device which is most suitable
static VkPhysicalDevice pick_best_physical_device(const Instance &inst, VkSurfaceKHR surface,
static VkPhysicalDevice pick_best_physical_device(const Instance &inst,
VkSurfaceKHR surface,
const VkPhysicalDeviceFeatures &required_features,
std::span<const char *> required_extensions);

Expand All @@ -106,9 +107,13 @@ class Device {
/// @exception VulkanException vkCreateDevice call failed
/// @exception VulkanException vmaCreateAllocator call failed
/// @note The creation of the physical device will not fail if one of the optional device features is not available
Device(const Instance &inst, VkSurfaceKHR surface, bool prefer_distinct_transfer_queue,
VkPhysicalDevice physical_device, std::span<const char *> required_extensions,
const VkPhysicalDeviceFeatures &required_features, const VkPhysicalDeviceFeatures &optional_features = {});
Device(const Instance &inst,
VkSurfaceKHR surface,
bool prefer_distinct_transfer_queue,
VkPhysicalDevice physical_device,
std::span<const char *> required_extensions,
const VkPhysicalDeviceFeatures &required_features,
const VkPhysicalDeviceFeatures &optional_features = {});

Device(const Device &) = delete;
Device(Device &&) = delete;
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 @@ -523,13 +523,13 @@ void Application::setup_render_graph() {
.bind_index_buffer(m_index_buffer)
.draw_indexed(static_cast<std::uint32_t>(m_octree_indices.size()));
})
.build("Octree");
.build("Octree", render_graph::DebugLabelColor::RED);
return m_octree_pass;
});

// TODO: We don't need to recreate the imgui overlay when swapchain is recreated, use a .recreate() method instead?
// m_imgui_overlay = std::make_unique<renderers::ImGuiRenderer>(*m_device, *m_swapchain, *m_render_graph.get(),
// m_back_buffer, [&]() { update_imgui_overlay(); });
// m_back_buffer, [&]() { update_imgui_overlay(); });

m_render_graph->compile();
}
Expand Down
47 changes: 45 additions & 2 deletions src/vulkan-renderer/render-graph/graphics_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,56 @@

namespace inexor::vulkan_renderer::render_graph {

std::array<float, 4> get_debug_label_color(const DebugLabelColor color) {
switch (color) {
case DebugLabelColor::RED:
return {0.98f, 0.60f, 0.60f, 1.0f};
case DebugLabelColor::BLUE:
return {0.68f, 0.85f, 0.90f, 1.0f};
case DebugLabelColor::GREEN:
return {0.73f, 0.88f, 0.73f, 1.0f};
case DebugLabelColor::YELLOW:
return {0.98f, 0.98f, 0.70f, 1.0f};
case DebugLabelColor::PURPLE:
return {0.80f, 0.70f, 0.90f, 1.0f};
case DebugLabelColor::ORANGE:
return {0.98f, 0.75f, 0.53f, 1.0f};
case DebugLabelColor::MAGENTA:
return {0.96f, 0.60f, 0.76f, 1.0f};
case DebugLabelColor::CYAN:
return {0.70f, 0.98f, 0.98f, 1.0f};
case DebugLabelColor::BROWN:
return {0.82f, 0.70f, 0.55f, 1.0f};
case DebugLabelColor::PINK:
return {0.98f, 0.75f, 0.85f, 1.0f};
case DebugLabelColor::LIME:
return {0.80f, 0.98f, 0.60f, 1.0f};
case DebugLabelColor::TURQUOISE:
return {0.70f, 0.93f, 0.93f, 1.0f};
case DebugLabelColor::BEIGE:
return {0.96f, 0.96f, 0.86f, 1.0f};
case DebugLabelColor::MAROON:
return {0.76f, 0.50f, 0.50f, 1.0f};
case DebugLabelColor::OLIVE:
return {0.74f, 0.75f, 0.50f, 1.0f};
case DebugLabelColor::NAVY:
return {0.53f, 0.70f, 0.82f, 1.0f};
case DebugLabelColor::TEAL:
return {0.53f, 0.80f, 0.75f, 1.0f};
default:
return {0.0f, 0.0f, 0.0f, 1.0f}; // Default to opaque black if the color is not recognized
}
}

GraphicsPass::GraphicsPass(std::string name,
std::function<void(const CommandBuffer &)> on_record_cmd_buffer,
std::vector<Attachment> color_attachments,
Attachment depth_attachment,
Attachment stencil_attachment)
Attachment stencil_attachment,
const DebugLabelColor color)
: m_name(std::move(name)), m_on_record_cmd_buffer(std::move(on_record_cmd_buffer)),
m_color_attachments(std::move(color_attachments)), m_depth_attachment(std::move(depth_attachment)),
m_stencil_attachment(std::move(stencil_attachment)) {}
m_stencil_attachment(std::move(stencil_attachment)), m_debug_label_color(get_debug_label_color(color)) {}

GraphicsPass::GraphicsPass(GraphicsPass &&other) noexcept {
m_name = std::move(other.m_name);
Expand All @@ -25,6 +67,7 @@ GraphicsPass::GraphicsPass(GraphicsPass &&other) noexcept {
m_color_attachment_infos = std::move(other.m_color_attachment_infos);
m_depth_attachment_info = std::move(other.m_depth_attachment_info);
m_stencil_attachment_info = std::move(other.m_stencil_attachment_info);
m_debug_label_color = other.m_debug_label_color;
}

} // namespace inexor::vulkan_renderer::render_graph
4 changes: 2 additions & 2 deletions src/vulkan-renderer/render-graph/graphics_pass_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ GraphicsPassBuilder &GraphicsPassBuilder::add_stencil_attachment(std::weak_ptr<T
return *this;
}

std::shared_ptr<GraphicsPass> GraphicsPassBuilder::build(std::string name) {
std::shared_ptr<GraphicsPass> GraphicsPassBuilder::build(std::string name, const DebugLabelColor color) {
auto graphics_pass = std::make_shared<GraphicsPass>(std::move(name), std::move(m_on_record_cmd_buffer),
std::move(m_color_attachments), std::move(m_depth_attachment),
std::move(m_stencil_attachment));
std::move(m_stencil_attachment), color);
reset();
return graphics_pass;
}
Expand Down
21 changes: 10 additions & 11 deletions src/vulkan-renderer/render-graph/render_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,12 @@ void RenderGraph::create_rendering_infos() {
for (const auto &color_attachment : pass->m_color_attachments) {
pass->m_color_attachment_infos.push_back(fill_rendering_info(color_attachment));
}

// Fill the color attachment (if specified)
const bool has_depth_attachment = !pass->m_depth_attachment.first.expired();
if (has_depth_attachment) {
pass->m_depth_attachment_info = fill_rendering_info(pass->m_depth_attachment);
pass->m_depth_attachment_info.resolveMode = VK_RESOLVE_MODE_NONE;
}

// Fill the stencil attachment (if specified)
const bool has_stencil_attachment = !pass->m_stencil_attachment.first.expired();
if (has_stencil_attachment) {
Expand All @@ -229,18 +227,20 @@ void RenderGraph::create_rendering_infos() {
}

void RenderGraph::create_textures() {
m_device.execute("[RenderGraph::create_textures|", [&](const CommandBuffer &cmd_buf) {
m_device.execute("RenderGraph::create_textures", [&](const CommandBuffer &cmd_buf) {
for (const auto &texture : m_textures) {
// TODO: Check if this initializes all textures (internal ones from rendergraph and external like ImGui?)
if (texture->m_on_init) {
// Rename the command buffer before creating every texture for fine-grained debugging
cmd_buf.set_suboperation_debug_name("|Texture|Initialize:" + texture->m_name);
std::invoke(texture->m_on_init.value());
}
// Rename the command buffer before creating every texture for fine-grained debugging
cmd_buf.set_suboperation_debug_name("Texture|Create:" + texture->m_name + "]");
cmd_buf.set_suboperation_debug_name("|Texture|Create:" + texture->m_name);
texture->create();
if (texture->m_usage == TextureUsage::NORMAL) {
// Rename the command buffer before creating every texture for fine-grained debugging
cmd_buf.set_suboperation_debug_name("Texture|Update:" + texture->m_name + "]");
cmd_buf.set_suboperation_debug_name("|Texture|Update:" + texture->m_name);
// Only external textures are updated, not back or depth buffers used internally in rendergraph
texture->update(cmd_buf);
}
Expand All @@ -255,11 +255,10 @@ void RenderGraph::determine_pass_order() {

void RenderGraph::record_command_buffer_for_pass(const CommandBuffer &cmd_buf, const GraphicsPass &pass) {
// Rename the command buffer before creating every texture for fine-grained debugging
cmd_buf.set_suboperation_debug_name("Pass:" + pass.m_name + "]");
cmd_buf.set_suboperation_debug_name("|Pass:" + pass.m_name);

// TODO: Define default color values for debug labels!
// Start a new debug label for this graphics pass (visible in graphics debuggers like RenderDoc)
cmd_buf.begin_debug_label_region(pass.m_name, {1.0f, 0.0f, 0.0f, 1.0f});
cmd_buf.begin_debug_label_region(pass.m_name, pass.m_debug_label_color);

// TODO: Support multi-target rendering!
auto color_attachment_info = pass.m_color_attachment_infos;
Expand Down Expand Up @@ -294,7 +293,7 @@ void RenderGraph::render() {
m_swapchain.acquire_next_image_index();

// TODO: Refactor this into .exec();
const auto &cmd_buf = m_device.request_command_buffer("[RenderGraph::render|");
const auto &cmd_buf = m_device.request_command_buffer("RenderGraph::render");

// Transform the image layout of the swapchain (it comes in undefined layout after presenting)
cmd_buf.change_image_layout(m_swapchain.m_current_img, VK_IMAGE_LAYOUT_UNDEFINED,
Expand Down Expand Up @@ -325,7 +324,7 @@ void RenderGraph::reset() {
}

void RenderGraph::update_buffers() {
m_device.execute("[RenderGraph::update_buffers]", [&](const CommandBuffer &cmd_buf) {
m_device.execute("RenderGraph::update_buffers", [&](const CommandBuffer &cmd_buf) {
for (const auto &buffer : m_buffers) {
// Does this buffer need to be updated?
if (buffer->m_update_requested) {
Expand All @@ -346,7 +345,7 @@ void RenderGraph::update_descriptor_sets() {
}

void RenderGraph::update_textures() {
m_device.execute("[RenderGraph::update_textures]", [&](const CommandBuffer &cmd_buf) {
m_device.execute("RenderGraph::update_textures", [&](const CommandBuffer &cmd_buf) {
for (const auto &texture : m_textures) {
// Only for dynamic textures m_on_lambda which is not std::nullopt
if (texture->m_on_update) {
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan-renderer/renderers/imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ ImGuiRenderer::ImGuiRenderer(const Device &device,
vertex_offset += cmd_list->VtxBuffer.Size;
}
})
.build("ImGui");
.build("ImGui", render_graph::DebugLabelColor::BLUE);
return m_imgui_pass;
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan-renderer/wrapper/commands/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ const CommandBuffer &CommandBuffer::insert_debug_label(std::string name, std::ar
.pLabelName = name.c_str(),
.color = {color[0], color[1], color[2], color[3]},
});
vkCmdBeginDebugUtilsLabelEXT(m_cmd_buf, &label);
vkCmdInsertDebugUtilsLabelEXT(m_cmd_buf, &label);
return *this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vulkan-renderer/wrapper/commands/command_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const commands::CommandBuffer &CommandPool::request_command_buffer(const std::st

// No free command buffer was found so we need to create a new one
// Note that there is currently no method for shrinking m_cmd_bufs, but this should not be a problem
m_cmd_bufs.emplace_back(std::make_unique<commands::CommandBuffer>(m_device, m_cmd_pool, "command buffer"));
m_cmd_bufs.emplace_back(std::make_unique<commands::CommandBuffer>(m_device, m_cmd_pool, name));

auto &new_cmd_buf = *m_cmd_bufs.back();
new_cmd_buf.begin_command_buffer();
Expand Down

0 comments on commit 46fea99

Please sign in to comment.