deferred_renderer.hpp 8.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#pragma once

#include <mirrage/renderer/camera_comp.hpp>
#include <mirrage/renderer/gbuffer.hpp>
#include <mirrage/renderer/model.hpp>

#include <mirrage/graphic/context.hpp>
#include <mirrage/graphic/device.hpp>
#include <mirrage/graphic/profiler.hpp>

11
12
#include <mirrage/utils/math.hpp>

13
14
15
#include <vulkan/vulkan.hpp>


16
namespace mirrage {
17
18
19
20
	namespace ecs {
		class Entity_manager;
	}
	class Meta_system;
21
} // namespace mirrage
22

23
namespace mirrage::renderer {
24
25
26
27

	class Deferred_renderer_factory;
	class Deferred_renderer;

28

29
	struct Renderer_settings {
30
		int shadowmap_resolution = 2048;
Florian Oetke's avatar
Florian Oetke committed
31
32
		int shadow_quality       = 99; // 0 = lowest

33
34
35
36
		bool  gi                        = true;
		bool  gi_highres                = true;
		int   gi_diffuse_mip_level      = 1;
		int   gi_min_mip_level          = 0;
37
		int   gi_samples                = 64;
Florian Oetke's avatar
Florian Oetke committed
38
		int   gi_lowres_samples         = 512;
39
40
41
		bool  gi_jitter_samples         = false;
		int   gi_low_quality_mip_levels = 0;
		float exposure_override         = 0.f;
Florian Oetke's avatar
Florian Oetke committed
42

43
44
45
		bool ssao  = true;
		bool bloom = true;

46
		float background_intensity = 0.f;
47

48
		bool dynamic_shadows = false;
49
50
		bool debug_disect    = false;
		int  debug_gi_layer  = -1;
51
	};
52

53
#ifdef sf2_structDef
54
55
	sf2_structDef(
	        Renderer_settings, shadowmap_resolution, shadow_quality, gi, dynamic_shadows, debug_gi_layer);
56
57
58
59
60
61
62
63
64
65
#endif

	struct Global_uniforms {
		glm::mat4 view_proj_mat;
		glm::mat4 view_mat;
		glm::mat4 proj_mat;
		glm::mat4 inv_view_mat;
		glm::mat4 inv_proj_mat;
		glm::vec4 eye_pos;
		glm::vec4 proj_planes; //< near, far, fov horizontal, fov vertical
66
		glm::vec4 time;        //< time, sin(time), delta_time
67
		glm::vec4 proj_info;
68
69
70
71
72
	};

	using Command_buffer_source = std::function<vk::CommandBuffer()>;

	class Pass {
73
74
	  public:
		virtual ~Pass() = default;
75

76
		virtual void update(util::Time dt)             = 0;
77
78
79
80
		virtual void draw(vk::CommandBuffer&,
		                  Command_buffer_source&,
		                  vk::DescriptorSet global_uniform_set,
		                  std::size_t       swapchain_image) = 0;
81

82
83
		virtual void shrink_to_fit() {}
		virtual void process_camera(Camera_state&) {} //< allows passes to modify the current camera
84

85
		virtual auto name() const noexcept -> const char* = 0;
86
87
88
	};

	class Pass_factory {
89
90
91
92
93
94
95
96
97
98
	  public:
		virtual ~Pass_factory() = default;

		virtual auto create_pass(Deferred_renderer&,
		                         ecs::Entity_manager&,
		                         util::maybe<Meta_system&>,
		                         bool& write_first_pp_buffer) -> std::unique_ptr<Pass> = 0;

		virtual auto rank_device(vk::PhysicalDevice,
		                         util::maybe<std::uint32_t> graphics_queue,
99
100
		                         int                        current_score) -> int
		{
101
102
103
104
105
			return current_score;
		}

		virtual void configure_device(vk::PhysicalDevice,
		                              util::maybe<std::uint32_t> graphics_queue,
106
107
108
		                              graphic::Device_create_info&)
		{
		}
109
110
	};

111
	template <class T, class... Args>
112
113
	auto make_pass_factory(Args&&... args)
	{
114
115
116
117
118
119
		return std::unique_ptr<Pass_factory>(new T(std::forward<Args>(args)...));
	}


	// shared among all Deferred_renderers in all screens
	class Deferred_renderer_factory {
120
121
	  public:
		Deferred_renderer_factory(graphic::Context&,
122
123
		                          graphic::Window&      window,
		                          asset::Asset_manager& assets,
124
		                          std::vector<std::unique_ptr<Pass_factory>>);
125
		~Deferred_renderer_factory();
126
127
128
129
130
131
132

		auto create_renderer(ecs::Entity_manager&, util::maybe<Meta_system&>)
		        -> std::unique_ptr<Deferred_renderer>;

		void queue_commands(vk::CommandBuffer);
		auto queue_temporary_command_buffer() -> vk::CommandBuffer;

133
134
135
		auto model_material_sampler() const noexcept { return *_model_material_sampler; }
		auto model_descriptor_set_layout() const noexcept { return *_model_desc_set_layout; }

136
137
138
		void finish_frame();

		auto settings() const -> auto& { return *_settings; }
Florian Oetke's avatar
Florian Oetke committed
139
140
		void settings(const Renderer_settings& s, bool apply = true);
		void save_settings();
141
142
143

	  private:
		friend class Deferred_renderer;
144
		struct Asset_loaders;
145
		using Pass_factories = std::vector<std::unique_ptr<Pass_factory>>;
146
		using Settings_ptr   = asset::Ptr<Renderer_settings>;
147

148
		asset::Asset_manager&           _assets;
Florian Oetke's avatar
Florian Oetke committed
149
150
151
152
		Settings_ptr                    _settings;
		Pass_factories                  _pass_factories;
		graphic::Window&                _window;
		graphic::Device_ptr             _device;
153
		graphic::Swapchain&             _swapchain;
Florian Oetke's avatar
Florian Oetke committed
154
155
156
157
158
159
160
161
162
		std::uint32_t                   _queue_family;
		vk::Queue                       _queue;
		vk::UniqueSemaphore             _image_acquired;
		vk::UniqueSemaphore             _image_presented;
		graphic::Command_buffer_pool    _command_buffer_pool;
		util::maybe<std::size_t>        _aquired_swapchain_image;
		std::vector<vk::CommandBuffer>  _queued_commands;
		std::vector<Deferred_renderer*> _renderer_instances;
		bool                            _recreation_pending = false;
163
164
165
		vk::UniqueSampler               _model_material_sampler;
		vk::UniqueDescriptorSetLayout   _model_desc_set_layout;
		std::unique_ptr<Asset_loaders>  _asset_loaders;
166

167
		void _present();
168
169
170
171
172
		auto _rank_device(vk::PhysicalDevice, util::maybe<std::uint32_t> gqueue) -> int;
		auto _init_device(vk::PhysicalDevice, util::maybe<std::uint32_t> gqueue)
		        -> graphic::Device_create_info;

		auto _aquire_next_image() -> std::size_t;
173
174
175
	};

	class Deferred_renderer {
176
177
178
179
180
	  public:
		Deferred_renderer(Deferred_renderer_factory&,
		                  std::vector<std::unique_ptr<Pass_factory>>&,
		                  ecs::Entity_manager&,
		                  util::maybe<Meta_system&>);
Florian Oetke's avatar
Florian Oetke committed
181
182
		Deferred_renderer(const Deferred_renderer&) = delete;
		auto operator=(const Deferred_renderer&) -> Deferred_renderer& = delete;
183
		~Deferred_renderer();
184

Florian Oetke's avatar
Florian Oetke committed
185
186
		void recreate();

187
		template <class T>
188
189
		auto find_pass() -> util::tracking_ptr<T>
		{
190
191
			auto pass = std::find_if(
			        _passes.begin(), _passes.end(), [](auto& p) { return dynamic_cast<T*>(&*p) != nullptr; });
192

Florian Oetke's avatar
Florian Oetke committed
193
194
			return pass != _passes.end() ? util::tracking_ptr<T>(pass->create_ptr())
			                             : util::tracking_ptr<T>{};
195
196
197
198
199
200
201
		}

		void update(util::Time dt);
		void draw();

		void shrink_to_fit();

Florian Oetke's avatar
Florian Oetke committed
202
203
		auto gbuffer() noexcept -> auto& { return *_gbuffer; }
		auto gbuffer() const noexcept -> auto& { return *_gbuffer; }
204
		auto global_uniforms() const noexcept -> auto& { return _global_uniforms; }
205
		auto global_uniforms_layout() const noexcept { return *_global_uniform_descriptor_set_layout; }
206

207
		auto asset_manager() noexcept -> auto& { return _factory->_assets; }
Florian Oetke's avatar
Florian Oetke committed
208
209
210
211
		auto device() noexcept -> auto& { return *_factory->_device; }
		auto window() noexcept -> auto& { return _factory->_window; }
		auto swapchain() noexcept -> auto& { return _factory->_swapchain; }
		auto queue_family() const noexcept { return _factory->_queue_family; }
212

213
		auto create_descriptor_set(vk::DescriptorSetLayout, std::uint32_t bindings) -> graphic::DescriptorSet;
214
215
		auto descriptor_pool() noexcept -> auto& { return _descriptor_set_pool; }

216
217
218
		auto noise_descriptor_set_layout() const noexcept { return *_noise_descriptor_set_layout; }
		auto noise_descriptor_set() const noexcept { return *_noise_descriptor_set; }

219
220
221
		auto model_material_sampler() const noexcept { return _factory->model_material_sampler(); }
		auto model_descriptor_set_layout() const noexcept { return _factory->model_descriptor_set_layout(); }

222
223
		auto active_camera() noexcept -> util::maybe<Camera_state&>;

Florian Oetke's avatar
Florian Oetke committed
224
225
		auto settings() const -> auto& { return _factory->settings(); }
		void save_settings() { _factory->save_settings(); }
226
		void settings(const Renderer_settings& s, bool apply = true) { _factory->settings(s, apply); }
Florian Oetke's avatar
Florian Oetke committed
227

228
229
230
231
232

		auto profiler() const noexcept -> auto& { return _profiler; }
		auto profiler() noexcept -> auto& { return _profiler; }

	  private:
233
234
		friend class Deferred_renderer_factory;

Florian Oetke's avatar
Florian Oetke committed
235
236
237
		Deferred_renderer_factory* _factory;
		ecs::Entity_manager*       _entity_manager;
		util::maybe<Meta_system&>  _meta_system;
238
239
		graphic::Descriptor_pool   _descriptor_set_pool;

Florian Oetke's avatar
Florian Oetke committed
240
241
242
		std::unique_ptr<GBuffer> _gbuffer;
		Global_uniforms          _global_uniforms;
		graphic::Profiler        _profiler;
243
244
245
		float                    _time_acc      = 0.f;
		float                    _delta_time    = 0.f;
		std::uint32_t            _frame_counter = 0;
246
247

		vk::UniqueDescriptorSetLayout _global_uniform_descriptor_set_layout;
248
		graphic::DescriptorSet        _global_uniform_descriptor_set;
249
250
		graphic::Dynamic_buffer       _global_uniform_buffer;

251
252
253
		graphic::Texture_ptr                 _blue_noise;
		vk::UniqueSampler                    _noise_sampler;
		graphic::Image_descriptor_set_layout _noise_descriptor_set_layout;
254
		graphic::DescriptorSet               _noise_descriptor_set;
255

Florian Oetke's avatar
Florian Oetke committed
256
		std::vector<util::trackable<Pass>> _passes;
257

Florian Oetke's avatar
Florian Oetke committed
258
		Camera_comp::Pool*        _cameras;
259
260
261
262
263
		util::maybe<Camera_state> _active_camera;

		void _write_global_uniform_descriptor_set();
		void _update_global_uniforms(vk::CommandBuffer, const Camera_state& camera);
	};
264
} // namespace mirrage::renderer