Skip to content

Commit

Permalink
[*] WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmNotHanni committed Nov 22, 2023
1 parent 9bd37ce commit d888c5b
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 317 deletions.
3 changes: 3 additions & 0 deletions include/inexor/vulkan-renderer/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class Application {
std::weak_ptr<render_graph::BufferResource> m_vertex_buffer;
std::weak_ptr<render_graph::BufferResource> m_uniform_buffer;

std::shared_ptr<wrapper::pipelines::GraphicsPipeline> m_octree_pipeline;
std::shared_ptr<render_graph::GraphicsPass> m_octree_pass;

struct ModelViewPerspectiveMatrices {
glm::mat4 model{1.0f};
glm::mat4 view{1.0f};
Expand Down
5 changes: 4 additions & 1 deletion include/inexor/vulkan-renderer/imgui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ class ImGUIOverlay {
const wrapper::Device &m_device;
std::weak_ptr<render_graph::BufferResource> m_index_buffer;
std::weak_ptr<render_graph::BufferResource> m_vertex_buffer;
std::weak_ptr<render_graph::GraphicsStage> m_stage;
std::weak_ptr<render_graph::GraphicsPass> m_stage;
std::unique_ptr<wrapper::GpuTexture> m_imgui_texture;
wrapper::Shader m_vertex_shader;
wrapper::Shader m_fragment_shader;

std::shared_ptr<wrapper::pipelines::GraphicsPipeline> m_imgui_pipeline;
std::shared_ptr<render_graph::GraphicsPass> m_imgui_pass;

// We need to collect the vertices and indices generated by ImGui
// because it does not store them in one array, but rather in chunks
std::vector<std::uint32_t> m_index_data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace inexor::vulkan_renderer::render_graph {
class RenderGraph;

/// A wrapper for graphics stages inside of rendergraph
class GraphicsStage {
class GraphicsPass {
friend class RenderGraph;

private:
Expand Down Expand Up @@ -51,19 +51,19 @@ class GraphicsStage {
/// @param clear_screen If specified, ``VkAttachmentLoadOp`` in ``VkRenderingAttachmentInfo`` will be set to
/// ``VK_ATTACHMENT_LOAD_OP_CLEAR``, and the clear values specified here are used (``std::nullopt`` by default, in
/// which case ``VK_ATTACHMENT_LOAD_OP_LOAD`` is used)
GraphicsStage(
GraphicsPass(
std::string name,
std::vector<std::pair<std::weak_ptr<BufferResource>, std::optional<VkShaderStageFlagBits>>> buffer_reads,
std::vector<std::pair<std::weak_ptr<TextureResource>, std::optional<VkShaderStageFlagBits>>> texture_reads,
std::vector<std::weak_ptr<TextureResource>> texture_writes,
std::function<void(const wrapper::CommandBuffer &)> on_record, std::optional<VkClearValue> clear_values);

GraphicsStage(const GraphicsStage &) = delete;
GraphicsStage(GraphicsStage &&other) noexcept;
~GraphicsStage() = default;
GraphicsPass(const GraphicsPass &) = delete;
GraphicsPass(GraphicsPass &&other) noexcept;
~GraphicsPass() = default;

GraphicsStage &operator=(const GraphicsStage &) = delete;
GraphicsStage &operator=(GraphicsStage &&) = delete;
GraphicsPass &operator=(const GraphicsPass &) = delete;
GraphicsPass &operator=(GraphicsPass &&) = delete;
};

} // namespace inexor::vulkan_renderer::render_graph
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "inexor/vulkan-renderer/render-graph/buffer_resource.hpp"
#include "inexor/vulkan-renderer/render-graph/graphics_stage.hpp"
#include "inexor/vulkan-renderer/render-graph/graphics_pass.hpp"
#include "inexor/vulkan-renderer/render-graph/texture_resource.hpp"
#include "inexor/vulkan-renderer/wrapper/make_info.hpp"

Expand All @@ -17,7 +17,7 @@ class CommandBuffer;
namespace inexor::vulkan_renderer::render_graph {

/// A builder class for graphics stages in the rendergraph
class GraphicsStageBuilder {
class GraphicsPassBuilder {
private:
/// Indicates if the screen is cleared at the beginning of this stage
std::optional<VkClearValue> m_clear_value;
Expand All @@ -41,75 +41,17 @@ class GraphicsStageBuilder {
// TODO: Copy all data into one piece of memory and call vkCmdPushConstants only once! (ChatGPT says yes to this)
void compile_push_constants();

/*
[[nodiscard]] GraphicsStageBuilder &finalize_push_constants(VkCommandBuffer command_buffer) {
// Calculate the total size needed for push constants
std::size_t total_size = 0;
for (const auto &push_constant_range : m_push_constant_ranges) {
total_size += push_constant_range.first.size;
}
// Allocate a single block of memory to hold all push constant data
std::vector<std::uint8_t> push_constants_data(total_size);
// Loop through push constant ranges and copy data into the combined memory block
std::size_t offset = 0;
for (const auto &push_constant_range : m_push_constant_ranges) {
std::memcpy(push_constants_data.data() + offset,
reinterpret_cast<const void *>(&push_constant_range.second),
push_constant_range.first.size);
offset += push_constant_range.first.size;
}
// Loop through push constant ranges and calculate offsets and stage flags
std::vector<VkPushConstantRange> vk_push_constant_ranges;
std::vector<VkShaderStageFlags> stage_flags;
std::size_t vk_offset = 0;
for (const auto &push_constant_range : m_push_constant_ranges) {
vk_push_constant_ranges.push_back({
.stageFlags = push_constant_range.first.stageFlags,
.offset = vk_offset,
.size = push_constant_range.first.size,
});
// Track shader stages for combining stage flags
stage_flags.push_back(push_constant_range.first.stageFlags);
vk_offset += push_constant_range.first.size;
}
// Combine all shader stages
VkShaderStageFlags combined_stage_flags = 0;
for (auto stage : stage_flags) {
combined_stage_flags |= stage;
}
// Issue a single vkCmdPushConstants call
vkCmdPushConstants(command_buffer,
/* pipelineLayout / your_pipeline_layout,
combined_stage_flags,
0, // Start offset in combined data block
total_size,
push_constants_data.data());
// Optionally, clear the push constant ranges if you don't need them anymore
m_push_constant_ranges.clear();
return *this;
}
*/

/// Reset all data of the graphics stage builder
void reset();

public:
GraphicsStageBuilder();
GraphicsStageBuilder(const GraphicsStageBuilder &) = delete;
GraphicsStageBuilder(GraphicsStageBuilder &&) noexcept;
~GraphicsStageBuilder() = default;
GraphicsPassBuilder();
GraphicsPassBuilder(const GraphicsPassBuilder &) = delete;
GraphicsPassBuilder(GraphicsPassBuilder &&) noexcept;
~GraphicsPassBuilder() = default;

GraphicsStageBuilder &operator=(const GraphicsStageBuilder &) = delete;
GraphicsStageBuilder &operator=(GraphicsStageBuilder &&) noexcept;
GraphicsPassBuilder &operator=(const GraphicsPassBuilder &) = delete;
GraphicsPassBuilder &operator=(GraphicsPassBuilder &&) noexcept;

/// Add a push constant range to the graphics stage
/// @param shader_stage The shader stage for the push constant range
Expand All @@ -118,7 +60,7 @@ class GraphicsStageBuilder {
/// @param offset The offset in the push constant range
/// @return A const reference to the this pointer (allowing method calls to be chained)
template <typename PushConstantDataType>
[[nodiscard]] GraphicsStageBuilder &
[[nodiscard]] GraphicsPassBuilder &
add_push_constant_range(VkShaderStageFlags shader_stage, const PushConstantDataType &push_constant,
std::function<void()> on_update, std::uint32_t offset = 0) {
m_push_constant_ranges.push_back(std::make_pair(
Expand All @@ -134,41 +76,41 @@ class GraphicsStageBuilder {
/// Build the graphics stage and reset the builder's data
/// @param name The name of the graphics stage
/// @return The graphics stage which was created
[[nodiscard]] std::shared_ptr<GraphicsStage> build(std::string name);
[[nodiscard]] std::shared_ptr<GraphicsPass> build(std::string name);

[[nodiscard]] GraphicsStageBuilder &
[[nodiscard]] GraphicsPassBuilder &
reads_from_buffer(std::weak_ptr<BufferResource> buffer,
std::optional<VkShaderStageFlagBits> shader_stage = std::nullopt) {
m_buffer_reads.push_back(std::make_pair(std::move(buffer), shader_stage));
return *this;
}

[[nodiscard]] GraphicsStageBuilder &
[[nodiscard]] GraphicsPassBuilder &
reads_from_texture(std::weak_ptr<TextureResource> texture,
std::optional<VkShaderStageFlagBits> shader_stage = std::nullopt) {
m_texture_reads.push_back(std::make_pair(std::move(texture), shader_stage));
return *this;
}

[[nodiscard]] GraphicsStageBuilder &writes_to_texture(std::weak_ptr<TextureResource> texture) {
[[nodiscard]] GraphicsPassBuilder &writes_to_texture(std::weak_ptr<TextureResource> texture) {
m_texture_writes.push_back(std::move(texture));
return *this;
}

/// Set the clear status for the stage
/// @param clear_value The clear value for color and depth
/// @return A const reference to the this pointer (allowing method calls to be chained)
[[nodiscard]] GraphicsStageBuilder &set_clear_value(VkClearValue clear_value);
[[nodiscard]] GraphicsPassBuilder &set_clear_value(VkClearValue clear_value);

/// Enable or disable depth testing
/// @param depth_test ``true`` if depth testing is enabled for this stage
/// @return A const reference to the this pointer (allowing method calls to be chained)
[[nodiscard]] GraphicsStageBuilder &set_depth_test(bool depth_test);
[[nodiscard]] GraphicsPassBuilder &set_depth_test(bool depth_test);

/// 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]] GraphicsStageBuilder &set_on_record(std::function<void(const wrapper::CommandBuffer &)> on_record);
[[nodiscard]] GraphicsPassBuilder &set_on_record(std::function<void(const wrapper::CommandBuffer &)> on_record);
};

} // namespace inexor::vulkan_renderer::render_graph
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ class GraphicsPipelineBuilder {
// With the builder we can either call add_color_blend_attachment or set_color_blend_attachments
std::vector<VkPipelineColorBlendAttachmentState> m_color_blend_attachment_states;

public:
/// Default constructor
/// Default constructor is private, so only rendergraph can access it
/// @param device The device wrapper
explicit GraphicsPipelineBuilder();

public:
GraphicsPipelineBuilder(const GraphicsPipelineBuilder &) = delete;
GraphicsPipelineBuilder(GraphicsPipelineBuilder &&other) noexcept;
~GraphicsPipelineBuilder() = default;
Expand Down
Loading

0 comments on commit d888c5b

Please sign in to comment.