sandbox
Loading...
Searching...
No Matches
static_mesh_material_subrenderer.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_MODELS_STATIC_MESH_MATERIAL_SUBRENDERER_HPP_
3#define LIBSBX_MODELS_STATIC_MESH_MATERIAL_SUBRENDERER_HPP_
4
5#include <cstddef>
6#include <filesystem>
7#include <unordered_set>
8#include <ranges>
9#include <algorithm>
10
11#include <easy/profiler.h>
12
13#include <fmt/format.h>
14
15#include <range/v3/view/enumerate.hpp>
16
17#include <libsbx/utility/logger.hpp>
18#include <libsbx/utility/enum.hpp>
19
20#include <libsbx/reflection/description.hpp>
21
22#include <libsbx/memory/tracking_allocator.hpp>
23
24#include <libsbx/containers/octree.hpp>
25
26#include <libsbx/math/color.hpp>
27#include <libsbx/math/vector3.hpp>
28#include <libsbx/math/matrix4x4.hpp>
29#include <libsbx/math/volume.hpp>
30
31#include <libsbx/utility/logger.hpp>
32#include <libsbx/utility/timer.hpp>
33#include <libsbx/utility/layout.hpp>
34#include <libsbx/utility/iterator.hpp>
35
36#include <libsbx/core/engine.hpp>
37
38#include <libsbx/graphics/graphics_module.hpp>
39#include <libsbx/graphics/subrenderer.hpp>
40#include <libsbx/graphics/draw_list.hpp>
41
42#include <libsbx/graphics/pipeline/pipeline.hpp>
43#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
44
45#include <libsbx/graphics/descriptor/descriptor_handler.hpp>
46
47#include <libsbx/graphics/buffers/uniform_handler.hpp>
48#include <libsbx/graphics/buffers/storage_handler.hpp>
49#include <libsbx/graphics/buffers/storage_buffer.hpp>
50
51#include <libsbx/graphics/images/image2d.hpp>
52#include <libsbx/graphics/images/separate_image2d_array.hpp>
53#include <libsbx/graphics/images/sampler_state.hpp>
54
55#include <libsbx/assets/assets_module.hpp>
56
57#include <libsbx/scenes/scenes_module.hpp>
58#include <libsbx/scenes/scene.hpp>
59#include <libsbx/scenes/node.hpp>
60
61#include <libsbx/scenes/components/static_mesh.hpp>
62#include <libsbx/scenes/components/id.hpp>
63#include <libsbx/scenes/components/camera.hpp>
64#include <libsbx/scenes/components/tag.hpp>
65#include <libsbx/scenes/components/point_light.hpp>
66#include <libsbx/scenes/components/global_transform.hpp>
67
68#include <libsbx/models/vertex3d.hpp>
69#include <libsbx/models/mesh.hpp>
70#include <libsbx/models/material.hpp>
71#include <libsbx/models/material_draw_list.hpp>
72#include <libsbx/models/static_mesh_material_draw_list.hpp>
73#include <libsbx/models/frustum_culling_task.hpp>
74
75namespace sbx::models {
76
78
79 inline static const auto pipeline_definition = graphics::pipeline_definition{
80 .depth = graphics::depth::read_write,
81 .uses_transparency = false,
82 .rasterization_state = graphics::rasterization_state{
83 .polygon_mode = graphics::polygon_mode::fill,
84 .cull_mode = graphics::cull_mode::back,
85 .front_face = graphics::front_face::counter_clockwise
86 }
87 };
88
89 inline static constexpr auto default_pipeline_path = std::string_view{"engine://shaders/deferred_pbr_material"};
90
91public:
92
93 static_mesh_material_subrenderer(const std::vector<graphics::attachment_description>& attachments, const static_mesh_material_draw_list::bucket bucket, const std::filesystem::path& base_pipeline = default_pipeline_path);
94
96
97 auto render(graphics::command_buffer& command_buffer) -> void override;
98
99private:
100
101 struct pipeline_data {
102
104 graphics::push_handler push_handler;
105
106 pipeline_data(const graphics::graphics_pipeline_handle& handle)
107 : pipeline{handle},
108 push_handler{pipeline} { }
109
110 }; // struct pipeline_data
111
112 struct descriptor_data {
113
114 graphics::descriptor_handler scene_descriptor_handler;
115 graphics::descriptor_handler sampler_descriptor_handler;
116 graphics::descriptor_handler image_descriptor_handler;
117
118 descriptor_data(const graphics::graphics_pipeline_handle& handle)
119 : scene_descriptor_handler{handle, 0u},
120 sampler_descriptor_handler{handle, 1u},
121 image_descriptor_handler{handle, 2u} { }
122
123 }; // struct descriptor_data
124
125 auto _get_or_create_pipeline(const material_key& key) -> pipeline_data&;
126
127 auto _get_or_create_descriptor_data(const graphics::graphics_pipeline_handle& handle) -> descriptor_data&;
128
129 inline static const auto _entry_point = std::array<std::string, 3u>{
130 "opaque_main", // alpha_mode::opaque
131 "mask_main", // alpha_mode::mask
132 "blend_main" // alpha_mode::blend
133 };
134
135 std::vector<graphics::attachment_description> _attachments;
136 std::filesystem::path _base_pipeline;
137 static_mesh_material_draw_list::bucket _bucket;
138
139 inline static auto _pipeline_cache = std::unordered_map<material_key, pipeline_data, material_key_hash>{};
140
141 std::unordered_map<graphics::graphics_pipeline_handle, descriptor_data> _descriptor_cache{};
142
143}; // class static_mesh_subrenderer
144
145} // namespace sbx::models
146
147template<>
148struct sbx::reflection::description<sbx::models::material_feature> {
149
150 static constexpr auto name() -> std::string_view {
151 return "material_feature";
152 }
153
154 static constexpr auto enumerators() {
155 return std::make_tuple(
156 enumerator{"cast_shadow", sbx::models::material_feature::cast_shadow},
157 enumerator{"receive_shadow", sbx::models::material_feature::receive_shadow},
158 enumerator{"invert_backface_normals", sbx::models::material_feature::invert_backface_normals}
159 );
160 }
161
162}; // struct sbx::reflection::description<sbx::models::material_feature>
163
164#endif // LIBSBX_MODELS_STATIC_MESH_MATERIAL_SUBRENDERER_HPP_
Definition: command_buffer.hpp:15
Definition: descriptor_handler.hpp:26
Definition: push_handler.hpp:18
Definition: resource_storage.hpp:18
Definition: subrenderer.hpp:14
Definition: static_mesh_material_subrenderer.hpp:77
RGBA color representation and utilities.
Definition: graphics_pipeline.hpp:106
Definition: graphics_pipeline.hpp:67
Definition: material.hpp:87
Definition: description.hpp:16
Definition: enumerator.hpp:10