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/memory/tracking_allocator.hpp>
21
22#include <libsbx/containers/octree.hpp>
23
24#include <libsbx/math/color.hpp>
25#include <libsbx/math/vector3.hpp>
26#include <libsbx/math/matrix4x4.hpp>
27#include <libsbx/math/volume.hpp>
28
29#include <libsbx/utility/logger.hpp>
30#include <libsbx/utility/timer.hpp>
31#include <libsbx/utility/layout.hpp>
32#include <libsbx/utility/iterator.hpp>
33
34#include <libsbx/core/engine.hpp>
35
36#include <libsbx/graphics/graphics_module.hpp>
37#include <libsbx/graphics/subrenderer.hpp>
38#include <libsbx/graphics/draw_list.hpp>
39
40#include <libsbx/graphics/pipeline/pipeline.hpp>
41#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
42
43#include <libsbx/graphics/descriptor/descriptor_handler.hpp>
44
45#include <libsbx/graphics/buffers/uniform_handler.hpp>
46#include <libsbx/graphics/buffers/storage_handler.hpp>
47#include <libsbx/graphics/buffers/storage_buffer.hpp>
48
49#include <libsbx/graphics/images/image2d.hpp>
50#include <libsbx/graphics/images/separate_image2d_array.hpp>
51#include <libsbx/graphics/images/sampler_state.hpp>
52
53#include <libsbx/assets/assets_module.hpp>
54
55#include <libsbx/scenes/scenes_module.hpp>
56#include <libsbx/scenes/scene.hpp>
57#include <libsbx/scenes/node.hpp>
58
59#include <libsbx/scenes/components/static_mesh.hpp>
60#include <libsbx/scenes/components/id.hpp>
61#include <libsbx/scenes/components/camera.hpp>
62#include <libsbx/scenes/components/tag.hpp>
63#include <libsbx/scenes/components/point_light.hpp>
64#include <libsbx/scenes/components/global_transform.hpp>
65
66#include <libsbx/models/vertex3d.hpp>
67#include <libsbx/models/mesh.hpp>
68#include <libsbx/models/material.hpp>
69#include <libsbx/models/material_draw_list.hpp>
70#include <libsbx/models/static_mesh_material_draw_list.hpp>
71
72namespace sbx::models {
73
75
76 inline static const auto pipeline_definition = graphics::pipeline_definition{
77 .depth = graphics::depth::read_write,
78 .uses_transparency = false,
79 .rasterization_state = graphics::rasterization_state{
80 .polygon_mode = graphics::polygon_mode::fill,
81 .cull_mode = graphics::cull_mode::back,
82 .front_face = graphics::front_face::counter_clockwise
83 }
84 };
85
86public:
87
88 static_mesh_material_subrenderer(const std::vector<graphics::attachment_description>& attachments, const std::filesystem::path& base_pipeline, const static_mesh_material_draw_list::bucket bucket);
89
91
92 auto render(graphics::command_buffer& command_buffer) -> void override;
93
94private:
95
96 struct pipeline_data {
97
99 graphics::push_handler push_handler;
100
101 pipeline_data(const graphics::graphics_pipeline_handle& handle)
102 : pipeline{handle},
103 push_handler{pipeline} { }
104
105 }; // struct pipeline_data
106
107 struct descriptor_data {
108
109 graphics::descriptor_handler scene_descriptor_handler;
110 graphics::descriptor_handler sampler_descriptor_handler;
111 graphics::descriptor_handler image_descriptor_handler;
112
113 descriptor_data(const graphics::graphics_pipeline_handle& handle)
114 : scene_descriptor_handler{handle, 0u},
115 sampler_descriptor_handler{handle, 1u},
116 image_descriptor_handler{handle, 2u} { }
117
118 }; // struct descriptor_data
119
120 auto _get_or_create_pipeline(const material_key& key) -> pipeline_data&;
121
122 auto _get_or_create_descriptor_data(const graphics::graphics_pipeline_handle& handle) -> descriptor_data&;
123
124 inline static const auto _entry_point = std::array<std::string, 3u>{
125 "opaque_main", // alpha_mode::opaque
126 "mask_main", // alpha_mode::mask
127 "blend_main" // alpha_mode::blend
128 };
129
130 std::vector<graphics::attachment_description> _attachments;
131 std::filesystem::path _base_pipeline;
132 static_mesh_material_draw_list::bucket _bucket;
133
134 inline static auto _pipeline_cache = std::unordered_map<material_key, pipeline_data, material_key_hash>{};
135
136 std::unordered_map<graphics::graphics_pipeline_handle, descriptor_data> _descriptor_cache{};
137
138}; // class static_mesh_subrenderer
139
140} // namespace sbx::models
141
142template<>
143struct sbx::utility::enum_mapping<sbx::models::material_feature> {
144
146
147 static constexpr auto values = std::array<entry_type, 6u>{
148 entry_type{sbx::models::material_feature::emission, "emission"},
149 entry_type{sbx::models::material_feature::normal_map, "normal_map"},
150 entry_type{sbx::models::material_feature::occlusion, "occlusion"},
151 entry_type{sbx::models::material_feature::height, "height"},
152 entry_type{sbx::models::material_feature::clearcoat, "clearcoat"},
153 entry_type{sbx::models::material_feature::anisotropy, "anisotropy"},
154 };
155
156}; // struct sbx::utility::enum_mapping
157
158#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:74
RGBA color representation and utilities.
Definition: graphics_pipeline.hpp:104
Definition: graphics_pipeline.hpp:65
Definition: material.hpp:85
Definition: enum.hpp:154
Definition: enum.hpp:161