wren
Vulkan-based game engine
Loading...
Searching...
No Matches
render_pass.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4#include <string>
5#include <tl/expected.hpp>
6#include <vulkan/vulkan.hpp>
7#include <wren/vk/buffer.hpp>
8#include <wren/vk/image.hpp>
9#include <wren/vk/shader.hpp>
10
11#include "render_target.hpp"
12#include "wren/context.hpp"
13
14namespace wren {
15
16// Forward declarations
17struct Context;
18
19namespace vk {
20class Buffer;
21}
22
24 std::unordered_map<std::string, std::shared_ptr<vk::Shader>> shaders;
25 std::string target_name;
26 std::shared_ptr<RenderTarget> render_target;
27};
28
30 public:
31 using execute_fn_t = std::function<void(RenderPass&, ::vk::CommandBuffer&)>;
32
33 static auto create(const std::shared_ptr<Context>& ctx,
34 const std::string& name, const PassResources& resources,
35 const execute_fn_t& fn,
36 const std::optional<vk::Image>& image)
38
39 void execute();
40
41 template <typename T>
42 void write_scratch_buffer(const ::vk::CommandBuffer& cmd, uint32_t set,
43 uint32_t binding, T data);
44 [[nodiscard]] auto get_scratch_buffer(uint32_t set, uint32_t binding,
45 size_t size) -> void*;
46
47 auto resize_target(const math::vec2i& new_size) -> expected<void>;
48
49 void on_resource_resized(const std::pair<float, float>& size);
50
51 auto current_target_size() { return resources_.render_target->size; }
52
53 [[nodiscard]] auto get_command_buffers() const { return command_buffers_; }
54
55 [[nodiscard]] auto get_framebuffer() const { return framebuffer_; }
56
57 void recreate_framebuffers(const ::vk::Device& device);
58
59 void bind_pipeline(const std::string& pipeline_name);
60
61 [[nodiscard]] auto get() const { return render_pass_; }
62
63 [[nodiscard]] auto target() const { return target_; }
64
65 private:
66 RenderPass(const std::shared_ptr<Context>& ctx, std::string name,
67 PassResources resources, execute_fn_t fn,
68 const std::optional<vk::Image>& image);
69
70 std::shared_ptr<Context> ctx_;
71
72 std::string name_;
73 PassResources resources_;
74 std::shared_ptr<vk::Shader> last_bound_shader_;
75
76 execute_fn_t execute_fn_;
77
78 ::vk::RenderPass render_pass_;
79
80 ::vk::CommandPool command_pool_;
81 std::vector<::vk::CommandBuffer> command_buffers_;
82
83 std::optional<vk::Image> target_image_;
84 std::shared_ptr<RenderTarget> target_;
85 ::vk::Framebuffer framebuffer_;
86
87 std::map<std::pair<uint32_t, uint32_t>, std::shared_ptr<vk::Buffer>> ubos_;
88};
89
90template <typename T>
91void RenderPass::write_scratch_buffer(const ::vk::CommandBuffer& cmd,
92 uint32_t set, uint32_t binding, T data) {
93 if (!ubos_.contains({set, binding})) {
94 // Create buffer
95
96 ubos_.insert(
97 {{set, binding},
99 ctx_->graphics_context->allocator(), sizeof(data),
100 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
101 VmaAllocationCreateFlagBits::
102 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT)});
103 }
104
105 auto buffer = ubos_.at({set, binding});
106 buffer->set_data_raw(&data, sizeof(T));
107
108 ::vk::DescriptorBufferInfo buffer_info(buffer->get(), 0, sizeof(T));
109 std::array writes = {::vk::WriteDescriptorSet{
110 {}, binding, 0, ::vk::DescriptorType::eUniformBuffer, {}, buffer_info}};
111
112 cmd.pushDescriptorSetKHR(::vk::PipelineBindPoint::eGraphics,
113 last_bound_shader_->pipeline_layout(), set, writes);
114}
115
116} // namespace wren
Definition render_pass.hpp:29
auto get_command_buffers() const
Definition render_pass.hpp:53
auto resize_target(const math::vec2i &new_size) -> expected< void >
Definition render_pass.cpp:86
static auto create(const std::shared_ptr< Context > &ctx, const std::string &name, const PassResources &resources, const execute_fn_t &fn, const std::optional< vk::Image > &image) -> expected< std::shared_ptr< RenderPass > >
Definition render_pass.cpp:14
auto get_scratch_buffer(uint32_t set, uint32_t binding, size_t size) -> void *
Definition render_pass.cpp:188
void execute()
Definition render_pass.cpp:155
void recreate_framebuffers(const ::vk::Device &device)
Definition render_pass.cpp:132
void bind_pipeline(const std::string &pipeline_name)
Definition render_pass.cpp:199
auto target() const
Definition render_pass.hpp:63
auto get_framebuffer() const
Definition render_pass.hpp:55
auto get() const
Definition render_pass.hpp:61
void on_resource_resized(const std::pair< float, float > &size)
Definition render_pass.cpp:128
void write_scratch_buffer(const ::vk::CommandBuffer &cmd, uint32_t set, uint32_t binding, T data)
Definition render_pass.hpp:91
std::function< void(RenderPass &, ::vk::CommandBuffer &)> execute_fn_t
Definition render_pass.hpp:31
auto current_target_size()
Definition render_pass.hpp:51
Definition buffer.hpp:14
static auto create(const VmaAllocator &allocator, size_t size, VkBufferUsageFlags usage, const std::optional< VmaAllocationCreateFlags > &flags={}) -> std::shared_ptr< Buffer >
Definition buffer.cpp:14
Definition ui.hpp:5
Definition editor.hpp:14
tl::expected< T, Err > expected
Definition errors.hpp:49
Definition render_pass.hpp:23
std::unordered_map< std::string, std::shared_ptr< vk::Shader > > shaders
Definition render_pass.hpp:24
std::shared_ptr< RenderTarget > render_target
Definition render_pass.hpp:26
std::string target_name
Definition render_pass.hpp:25
Definition vector.hpp:110