deferred_renderer.hpp 8.37 KB
Newer Older
1
2
3
4
5
#pragma once

#include <mirrage/renderer/camera_comp.hpp>
#include <mirrage/renderer/gbuffer.hpp>
#include <mirrage/renderer/model.hpp>
6
#include <mirrage/renderer/render_pass.hpp>
7
8
9
10
11

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

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

14
15
#include <glm/gtx/quaternion.hpp>
#include <glm/vec3.hpp>
16
17
18
#include <vulkan/vulkan.hpp>


19
namespace mirrage {
20
21
22
	namespace ecs {
		class Entity_manager;
	}
23
	class Engine;
24
} // namespace mirrage
25

26
namespace mirrage::renderer {
27
28
29
30

	class Deferred_renderer_factory;
	class Deferred_renderer;

31

32
	struct Renderer_settings {
33
		int shadowmap_resolution = 2048;
Florian Oetke's avatar
Florian Oetke committed
34
35
		int shadow_quality       = 99; // 0 = lowest

36
37
38
39
		bool gi                        = true;
		bool gi_highres                = true;
		int  gi_diffuse_mip_level      = 1;
		int  gi_min_mip_level          = 0;
40
41
		int  gi_samples                = 32;
		int  gi_lowres_samples         = 128;
42
43
44
45
46
47
48
		bool gi_jitter_samples         = false;
		int  gi_low_quality_mip_levels = 0;

		bool  tonemapping           = true;
		float exposure_override     = 0.f;
		float min_display_luminance = 2.f;
		float max_display_luminance = 150.0f;
Florian Oetke's avatar
Florian Oetke committed
49

50
51
52
		bool ssao  = true;
		bool bloom = true;

53
		float background_intensity = 0.f;
54

55
		bool dynamic_shadows = false;
56
57
		bool debug_disect    = false;
		int  debug_gi_layer  = -1;
58
	};
59

60
#ifdef sf2_structDef
61
62
	sf2_structDef(
	        Renderer_settings, shadowmap_resolution, shadow_quality, gi, dynamic_shadows, debug_gi_layer);
63
64
65
66
67
68
69
70
71
72
#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
73
		glm::vec4 time;        //< time, sin(time), delta_time
74
		glm::vec4 proj_info;
75
76
77
	};


78
	template <class T, class... Args>
79
80
	auto make_pass_factory(Args&&... args)
	{
81
		return std::unique_ptr<Render_pass_factory>(new T(std::forward<Args>(args)...));
82
83
84
85
86
	}


	// shared among all Deferred_renderers in all screens
	class Deferred_renderer_factory {
87
	  public:
88
89
90
		Deferred_renderer_factory(Engine&          engine,
		                          graphic::Window& window,
		                          std::vector<std::unique_ptr<Render_pass_factory>>);
91
		~Deferred_renderer_factory();
92

93
		auto create_renderer(ecs::Entity_manager&) -> std::unique_ptr<Deferred_renderer>;
94
95
96
97

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

Florian Oetke's avatar
Florian Oetke committed
98
99
		auto create_compute_command_buffer() -> vk::UniqueCommandBuffer;

100
101
102
		auto model_material_sampler() const noexcept { return *_model_material_sampler; }
		auto model_descriptor_set_layout() const noexcept { return *_model_desc_set_layout; }

Florian Oetke's avatar
Florian Oetke committed
103
104
		auto compute_queue() const noexcept { return _compute_queue; }

105
106
107
		void finish_frame();

		auto settings() const -> auto& { return *_settings; }
Florian Oetke's avatar
Florian Oetke committed
108
109
		void settings(const Renderer_settings& s, bool apply = true);
		void save_settings();
110
111
112

	  private:
		friend class Deferred_renderer;
113
		struct Asset_loaders;
114
		using Pass_factories = std::vector<std::unique_ptr<Render_pass_factory>>;
115
		using Settings_ptr   = asset::Ptr<Renderer_settings>;
116

117
		Engine&                         _engine;
118
		asset::Asset_manager&           _assets;
Florian Oetke's avatar
Florian Oetke committed
119
120
121
122
		Settings_ptr                    _settings;
		Pass_factories                  _pass_factories;
		graphic::Window&                _window;
		graphic::Device_ptr             _device;
123
		graphic::Swapchain&             _swapchain;
Florian Oetke's avatar
Florian Oetke committed
124
125
126
127
		std::uint32_t                   _draw_queue_family;
		std::uint32_t                   _compute_queue_family;
		vk::Queue                       _draw_queue;
		vk::Queue                       _compute_queue;
Florian Oetke's avatar
Florian Oetke committed
128
129
130
		vk::UniqueSemaphore             _image_acquired;
		vk::UniqueSemaphore             _image_presented;
		graphic::Command_buffer_pool    _command_buffer_pool;
Florian Oetke's avatar
Florian Oetke committed
131
		graphic::Command_buffer_pool    _compute_command_buffer_pool;
Florian Oetke's avatar
Florian Oetke committed
132
133
134
135
		util::maybe<std::size_t>        _aquired_swapchain_image;
		std::vector<vk::CommandBuffer>  _queued_commands;
		std::vector<Deferred_renderer*> _renderer_instances;
		bool                            _recreation_pending = false;
136
137
138
		vk::UniqueSampler               _model_material_sampler;
		vk::UniqueDescriptorSetLayout   _model_desc_set_layout;
		std::unique_ptr<Asset_loaders>  _asset_loaders;
139

140
		void _present();
141
142
143
144
145
		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;
146
147
	};

148

149
	class Deferred_renderer {
150
151
	  public:
		Deferred_renderer(Deferred_renderer_factory&,
152
		                  std::vector<std::unique_ptr<Render_pass_factory>>&,
153
		                  ecs::Entity_manager&,
154
		                  Engine&);
Florian Oetke's avatar
Florian Oetke committed
155
156
		Deferred_renderer(const Deferred_renderer&) = delete;
		auto operator=(const Deferred_renderer&) -> Deferred_renderer& = delete;
157
		~Deferred_renderer();
158

Florian Oetke's avatar
Florian Oetke committed
159
160
		void recreate();

161
		template <class T>
162
163
		auto find_pass() -> util::tracking_ptr<T>
		{
164
165
			auto pass = std::find_if(
			        _passes.begin(), _passes.end(), [](auto& p) { return dynamic_cast<T*>(&*p) != nullptr; });
166

Florian Oetke's avatar
Florian Oetke committed
167
168
			return pass != _passes.end() ? util::tracking_ptr<T>(pass->create_ptr())
			                             : util::tracking_ptr<T>{};
169
170
171
172
173
174
175
		}

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

		void shrink_to_fit();

Florian Oetke's avatar
Florian Oetke committed
176
177
		auto gbuffer() noexcept -> auto& { return *_gbuffer; }
		auto gbuffer() const noexcept -> auto& { return *_gbuffer; }
178
		auto global_uniforms() const noexcept -> auto& { return _global_uniforms; }
179
		auto global_uniforms_layout() const noexcept { return *_global_uniform_descriptor_set_layout; }
180

181
		auto asset_manager() noexcept -> auto& { return _factory->_assets; }
Florian Oetke's avatar
Florian Oetke committed
182
183
184
		auto device() noexcept -> auto& { return *_factory->_device; }
		auto window() noexcept -> auto& { return _factory->_window; }
		auto swapchain() noexcept -> auto& { return _factory->_swapchain; }
Florian Oetke's avatar
Florian Oetke committed
185
186
187
188
189
		auto queue_family() const noexcept { return _factory->_draw_queue_family; }
		auto compute_queue_family() const noexcept { return _factory->_compute_queue_family; }

		auto compute_queue() const noexcept { return _factory->compute_queue(); }
		auto create_compute_command_buffer() { return _factory->create_compute_command_buffer(); }
190

191
		auto create_descriptor_set(vk::DescriptorSetLayout, std::int32_t bindings) -> graphic::DescriptorSet;
192
193
		auto descriptor_pool() noexcept -> auto& { return _descriptor_set_pool; }

194
195
196
		auto noise_descriptor_set_layout() const noexcept { return *_noise_descriptor_set_layout; }
		auto noise_descriptor_set() const noexcept { return *_noise_descriptor_set; }

197
198
199
		auto model_material_sampler() const noexcept { return _factory->model_material_sampler(); }
		auto model_descriptor_set_layout() const noexcept { return _factory->model_descriptor_set_layout(); }

200
201
		auto active_camera() noexcept -> util::maybe<Camera_state&>;

Florian Oetke's avatar
Florian Oetke committed
202
203
		auto settings() const -> auto& { return _factory->settings(); }
		void save_settings() { _factory->save_settings(); }
204
		void settings(const Renderer_settings& s, bool apply = true) { _factory->settings(s, apply); }
Florian Oetke's avatar
Florian Oetke committed
205

206
207
208
209
210

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

	  private:
211
212
		friend class Deferred_renderer_factory;

213
		Engine*                    _engine;
Florian Oetke's avatar
Florian Oetke committed
214
215
		Deferred_renderer_factory* _factory;
		ecs::Entity_manager*       _entity_manager;
216
217
		graphic::Descriptor_pool   _descriptor_set_pool;

Florian Oetke's avatar
Florian Oetke committed
218
219
220
		std::unique_ptr<GBuffer> _gbuffer;
		Global_uniforms          _global_uniforms;
		graphic::Profiler        _profiler;
221
222
223
		float                    _time_acc      = 0.f;
		float                    _delta_time    = 0.f;
		std::uint32_t            _frame_counter = 0;
224
225

		vk::UniqueDescriptorSetLayout _global_uniform_descriptor_set_layout;
226
		graphic::DescriptorSet        _global_uniform_descriptor_set;
227
228
		graphic::Dynamic_buffer       _global_uniform_buffer;

229
230
231
		graphic::Texture_ptr                 _blue_noise;
		vk::UniqueSampler                    _noise_sampler;
		graphic::Image_descriptor_set_layout _noise_descriptor_set_layout;
232
		graphic::DescriptorSet               _noise_descriptor_set;
233

234
		std::vector<util::trackable<Render_pass>> _passes;
235

Florian Oetke's avatar
Florian Oetke committed
236
		Camera_comp::Pool*        _cameras;
237
		util::maybe<Camera_state> _active_camera;
238
		Frame_data                _frame_data;
239
240
241
242

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