sandbox
Loading...
Searching...
No Matches
tonemap_filter.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_POST_FILTERS_TONEMAP_FILTER_HPP_
3#define LIBSBX_POST_FILTERS_TONEMAP_FILTER_HPP_
4
5#include <libsbx/utility/enum.hpp>
6
7#include <libsbx/math/vector2.hpp>
8
9#include <libsbx/core/settings.hpp>
10
11#include <libsbx/graphics/graphics_module.hpp>
12#include <libsbx/graphics/buffers/push_handler.hpp>
13
14#include <libsbx/post/filter.hpp>
15
16namespace sbx::post {
17
19 std::float_t exposure = 0.0f;
20 std::float_t bloom_mix = 0.1f;
21 std::float_t saturation = 1.0f;
22 std::float_t contrast = 1.0f;
23 std::float_t temperature = 0.0f;
24 std::float_t tint = 0.0f;
25}; // struct tonemap_config
26
27class tonemap_filter final : public filter {
28
29 using base = filter;
30
31 inline static constexpr auto exposure_key = core::prototype::setting_key<std::float_t>{"render.exposure", core::prototype::setting_range<std::float_t>{-5.0f, 5.0f}};
32 inline static constexpr auto bloom_mix_key = core::prototype::setting_key<std::float_t>{"render.bloom_mix", core::prototype::setting_range<std::float_t>{0.0f, 0.4f}};
33 inline static constexpr auto saturation_key = core::prototype::setting_key<std::float_t>{"render.saturation", core::prototype::setting_range<std::float_t>{0.0f, 1.0f}};
34 inline static constexpr auto contrast_key = core::prototype::setting_key<std::float_t>{"render.contrast", core::prototype::setting_range<std::float_t>{0.0f, 1.0f}};
35 inline static constexpr auto temperature_key = core::prototype::setting_key<std::float_t>{"render.temperature", core::prototype::setting_range<std::float_t>{-1.0f, 1.0f}};
36 inline static constexpr auto tint_key = core::prototype::setting_key<std::float_t>{"render.tint", core::prototype::setting_range<std::float_t>{-1.0f, 1.0f}};
37
38 inline static constexpr auto default_shader_path = std::string_view{"engine://shaders/tonemap"};
39
40public:
41
42 tonemap_filter(const std::vector<graphics::attachment_description>& attachments, std::vector<std::pair<std::string, std::string>>&& attachment_names, const tonemap_config& tonemap_config = post::tonemap_config{}, const std::filesystem::path& path = default_shader_path)
43 : base{attachments, path, base::default_pipeline_definition},
44 _push_handler{base::pipeline()},
45 _attachment_names{std::move(attachment_names)},
46 _tonemap_config{tonemap_config} {
47 core::prototype::settings::set(exposure_key, _tonemap_config.exposure);
48 core::prototype::settings::set(bloom_mix_key, _tonemap_config.bloom_mix);
49 core::prototype::settings::set(saturation_key, _tonemap_config.saturation);
50 core::prototype::settings::set(contrast_key, _tonemap_config.contrast);
51 core::prototype::settings::set(temperature_key, _tonemap_config.temperature);
52 core::prototype::settings::set(tint_key, _tonemap_config.tint);
53 }
54
55 ~tonemap_filter() override = default;
56
57 auto render(graphics::command_buffer& command_buffer) -> void override {
58 EASY_BLOCK("tonemap_filter::render");
59
60 auto& pipeline = base::pipeline();
61 auto& descriptor_handler = base::descriptor_handler();
62
63 auto& graphics_module = core::engine::get_module<graphics::graphics_module>();
64
65 pipeline.bind(command_buffer);
66
67 _push_handler.push("exposure", _tonemap_config.exposure);
68 _push_handler.push("bloom_mix", _tonemap_config.bloom_mix);
69 _push_handler.push("saturation", _tonemap_config.saturation);
70 _push_handler.push("contrast", _tonemap_config.contrast);
71 _push_handler.push("temperature", _tonemap_config.temperature);
72 _push_handler.push("tint", _tonemap_config.tint);
73
74 for (const auto& [name, attachment] : _attachment_names) {
75 descriptor_handler.push(name, graphics_module.attachment(attachment));
76 }
77
78 if (!descriptor_handler.update(pipeline)) {
79 return;
80 }
81
82 descriptor_handler.bind_descriptors(command_buffer);
83 _push_handler.bind(command_buffer);
84
85 command_buffer.draw(3, 1, 0, 0);
86 }
87
88private:
89
90 graphics::push_handler _push_handler;
91 std::vector<std::pair<std::string, std::string>> _attachment_names;
92 tonemap_config _tonemap_config;
93
94}; // class tonemap_filter
95
96} // namespace sbx::post
97
98#endif // LIBSBX_POST_FILTERS_TONEMAP_FILTER_HPP_
Definition: command_buffer.hpp:15
Definition: push_handler.hpp:18
Definition: filter.hpp:19
Definition: tonemap_filter.hpp:27
Definition: settings.hpp:30
Definition: settings.hpp:24
Definition: tonemap_filter.hpp:18