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 <utility>
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 public:
26 : target_prefix_(std::move(target_prefix)) {}
27
28 auto add_shader(const std::string& name,
29 const std::shared_ptr<vk::Shader>& shader) -> PassResources& {
30 shaders_.insert({name, shader});
31 return *this;
32 }
33
35 colour_target_ = true;
36 return *this;
37 }
38
40 depth_target_ = true;
41 return *this;
42 }
43
44 auto has_colour_target() const { return colour_target_; }
45 auto has_depth_target() const { return depth_target_; }
46
47 auto target_prefix() const { return target_prefix_; }
48 auto shaders() const { return shaders_; }
49
50 private:
51 std::string target_prefix_;
52
53 bool colour_target_ = false;
54 bool depth_target_ = false;
55
56 std::unordered_map<std::string, std::shared_ptr<vk::Shader>> shaders_;
57};
58
60 public:
61 using execute_fn_t = std::function<void(RenderPass&, ::vk::CommandBuffer&)>;
62
63 static auto create(const std::shared_ptr<Context>& ctx,
64 const std::string& name, const PassResources& resources,
65 const std::shared_ptr<RenderTarget>& colour_target,
66 const std::shared_ptr<RenderTarget>& depth_target,
67 const execute_fn_t& fn)
69
70 void execute();
71
72 template <typename T>
73 void write_scratch_buffer(const ::vk::CommandBuffer& cmd, uint32_t set,
74 uint32_t binding, T data);
75 [[nodiscard]] auto get_scratch_buffer(uint32_t set, uint32_t binding,
76 size_t size) -> void*;
77
78 auto resize_target(const math::Vec2f& new_size) -> expected<void>;
79
80 void on_resource_resized(const std::pair<float, float>& size);
81
82 auto output_size() const { return size_; }
83
84 auto colour_target() const { return colour_target_; }
85 auto resources() const { return resources_; }
86
87 [[nodiscard]] auto get_command_buffers() const { return command_buffers_; }
88
89 [[nodiscard]] auto get_framebuffer() const { return framebuffer_; }
90
91 void recreate_framebuffers(const ::vk::Device& device);
92
93 void bind_pipeline(const std::string& pipeline_name);
94
95 [[nodiscard]] auto get() const { return render_pass_; }
96
97 private:
98 RenderPass(const std::shared_ptr<Context>& ctx, std::string name,
100 const std::shared_ptr<RenderTarget>& colour_target,
101 const std::shared_ptr<RenderTarget>& depth_target,
102 execute_fn_t fn);
103
104 std::shared_ptr<Context> ctx_;
105
106 std::string name_;
108 std::shared_ptr<vk::Shader> last_bound_shader_;
109
111
113
114 ::vk::RenderPass render_pass_;
115
116 ::vk::CommandPool command_pool_;
117 std::vector<::vk::CommandBuffer> command_buffers_;
118
119 std::shared_ptr<RenderTarget> colour_target_;
120 std::shared_ptr<RenderTarget> depth_target_;
121
122 ::vk::Framebuffer framebuffer_;
123
124 std::map<std::pair<uint32_t, uint32_t>, std::shared_ptr<vk::Buffer>> ubos_;
125};
126
127template <typename T>
128void RenderPass::write_scratch_buffer(const ::vk::CommandBuffer& cmd,
129 uint32_t set, uint32_t binding, T data) {
130 if (!ubos_.contains({set, binding})) {
131 // Create buffer
132
133 ubos_.insert(
134 {{set, binding},
136 ctx_->graphics_context->allocator(), sizeof(data),
137 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
138 VmaAllocationCreateFlagBits::
139 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT)});
140 }
141
142 auto buffer = ubos_.at({set, binding});
143 buffer->set_data_raw(&data, sizeof(T));
144
145 ::vk::DescriptorBufferInfo buffer_info(buffer->get(), 0, sizeof(T));
146 std::array writes = {::vk::WriteDescriptorSet{
147 {}, binding, 0, ::vk::DescriptorType::eUniformBuffer, {}, buffer_info}};
148
149 cmd.pushDescriptorSetKHR(::vk::PipelineBindPoint::eGraphics,
150 last_bound_shader_->pipeline_layout(), set, writes);
151}
152
153} // namespace wren
Definition render_pass.hpp:23
auto has_colour_target() const
Definition render_pass.hpp:44
bool depth_target_
Definition render_pass.hpp:54
auto target_prefix() const
Definition render_pass.hpp:47
auto has_depth_target() const
Definition render_pass.hpp:45
auto shaders() const
Definition render_pass.hpp:48
std::string target_prefix_
Definition render_pass.hpp:51
std::unordered_map< std::string, std::shared_ptr< vk::Shader > > shaders_
Definition render_pass.hpp:56
auto add_shader(const std::string &name, const std::shared_ptr< vk::Shader > &shader) -> PassResources &
Definition render_pass.hpp:28
bool colour_target_
Definition render_pass.hpp:53
auto add_depth_target() -> PassResources &
Definition render_pass.hpp:39
PassResources(std::string target_prefix)
Definition render_pass.hpp:25
auto add_colour_target() -> PassResources &
Definition render_pass.hpp:34
Definition render_pass.hpp:59
static auto create(const std::shared_ptr< Context > &ctx, const std::string &name, const PassResources &resources, const std::shared_ptr< RenderTarget > &colour_target, const std::shared_ptr< RenderTarget > &depth_target, const execute_fn_t &fn) -> expected< std::shared_ptr< RenderPass > >
Definition render_pass.cpp:13
auto resources() const
Definition render_pass.hpp:85
auto output_size() const
Definition render_pass.hpp:82
std::string name_
Definition render_pass.hpp:106
auto get_command_buffers() const
Definition render_pass.hpp:87
std::vector<::vk::CommandBuffer > command_buffers_
Definition render_pass.hpp:117
std::shared_ptr< vk::Shader > last_bound_shader_
Definition render_pass.hpp:108
std::shared_ptr< Context > ctx_
Definition render_pass.hpp:104
::vk::CommandPool command_pool_
Definition render_pass.hpp:116
std::shared_ptr< RenderTarget > colour_target_
Definition render_pass.hpp:119
::vk::Framebuffer framebuffer_
Definition render_pass.hpp:122
auto get_scratch_buffer(uint32_t set, uint32_t binding, size_t size) -> void *
Definition render_pass.cpp:211
void execute()
Definition render_pass.cpp:167
void recreate_framebuffers(const ::vk::Device &device)
Definition render_pass.cpp:125
::vk::RenderPass render_pass_
Definition render_pass.hpp:114
void bind_pipeline(const std::string &pipeline_name)
Definition render_pass.cpp:222
RenderPass(const std::shared_ptr< Context > &ctx, std::string name, PassResources resources, const std::shared_ptr< RenderTarget > &colour_target, const std::shared_ptr< RenderTarget > &depth_target, execute_fn_t fn)
Definition render_pass.cpp:231
math::Vec2f size_
Definition render_pass.hpp:110
auto get_framebuffer() const
Definition render_pass.hpp:89
auto get() const
Definition render_pass.hpp:95
auto colour_target() const
Definition render_pass.hpp:84
execute_fn_t execute_fn_
Definition render_pass.hpp:112
void on_resource_resized(const std::pair< float, float > &size)
Definition render_pass.cpp:121
void write_scratch_buffer(const ::vk::CommandBuffer &cmd, uint32_t set, uint32_t binding, T data)
Definition render_pass.hpp:128
PassResources resources_
Definition render_pass.hpp:107
std::shared_ptr< RenderTarget > depth_target_
Definition render_pass.hpp:120
std::map< std::pair< uint32_t, uint32_t >, std::shared_ptr< vk::Buffer > > ubos_
Definition render_pass.hpp:124
std::function< void(RenderPass &, ::vk::CommandBuffer &)> execute_fn_t
Definition render_pass.hpp:61
auto resize_target(const math::Vec2f &new_size) -> expected< void >
Definition render_pass.cpp:105
Definition buffer.hpp:13
static auto create(const VmaAllocator &allocator, size_t size, VkBufferUsageFlags usage, const std::optional< VmaAllocationCreateFlags > &flags={}) -> std::shared_ptr< Buffer >
Definition buffer.cpp:13
Definition ui.hpp:5
Definition editor_scene.hpp:5
std::expected< T, Err > expected
Definition result.hpp:49
Definition vector.hpp:124