sandbox
Loading...
Searching...
No Matches
static_mesh_material_draw_list.hpp
1// SPDX-License-Identifier: MIT
2#ifndef LIBSBX_MODELS_STATIC_MESH_MATERIAL_DRAW_LIST_HPP_
3#define LIBSBX_MODELS_STATIC_MESH_MATERIAL_DRAW_LIST_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/containers/octree.hpp>
21
22#include <libsbx/math/color.hpp>
23#include <libsbx/math/vector3.hpp>
24#include <libsbx/math/matrix4x4.hpp>
25#include <libsbx/math/volume.hpp>
26
27#include <libsbx/utility/logger.hpp>
28#include <libsbx/utility/timer.hpp>
29#include <libsbx/utility/layout.hpp>
30#include <libsbx/utility/iterator.hpp>
31
32#include <libsbx/core/engine.hpp>
33
34#include <libsbx/graphics/graphics_module.hpp>
35#include <libsbx/graphics/subrenderer.hpp>
36#include <libsbx/graphics/draw_list.hpp>
37
38#include <libsbx/graphics/pipeline/pipeline.hpp>
39#include <libsbx/graphics/pipeline/graphics_pipeline.hpp>
40
41#include <libsbx/graphics/descriptor/descriptor_handler.hpp>
42
43#include <libsbx/graphics/buffers/uniform_handler.hpp>
44#include <libsbx/graphics/buffers/storage_handler.hpp>
45#include <libsbx/graphics/buffers/storage_buffer.hpp>
46
47#include <libsbx/graphics/images/image2d.hpp>
48#include <libsbx/graphics/images/separate_image2d_array.hpp>
49#include <libsbx/graphics/images/sampler_state.hpp>
50
51#include <libsbx/assets/assets_module.hpp>
52
53#include <libsbx/scenes/scenes_module.hpp>
54#include <libsbx/scenes/scene.hpp>
55#include <libsbx/scenes/node.hpp>
56
57#include <libsbx/scenes/components/static_mesh.hpp>
58#include <libsbx/scenes/components/id.hpp>
59#include <libsbx/scenes/components/camera.hpp>
60#include <libsbx/scenes/components/tag.hpp>
61#include <libsbx/scenes/components/point_light.hpp>
62#include <libsbx/scenes/components/global_transform.hpp>
63
64#include <libsbx/models/vertex3d.hpp>
65#include <libsbx/models/mesh.hpp>
66#include <libsbx/models/material.hpp>
67#include <libsbx/models/material_draw_list.hpp>
68
69namespace sbx::models {
70
72
73 using mesh_type = models::mesh;
74 struct instance_payload { };
75
76 template<typename DarwList>
77 static auto create_shared_buffers([[maybe_unused]] DarwList& draw_list) -> void {
78
79 }
80
81 template<typename DarwList>
82 static auto destroy_shared_buffers([[maybe_unused]] DarwList& draw_list) -> void {
83
84 }
85
86 template<typename DarwList>
87 static auto update_shared_buffers([[maybe_unused]] DarwList& draw_list) -> void {
88
89 }
90
91 template<typename Callable>
92 static void for_each_submission(scenes::scene& scene, Callable&& callable) {
93 auto query = scene.query<const scenes::static_mesh>();
94
95 for (auto&& [node, static_mesh] : query.each()) {
96 const auto transform_data = models::transform_data{scene.world_transform(node), scene.world_normal(node)};
97
98 for (const auto& submesh : static_mesh.submeshes()) {
99 std::invoke(callable, node, static_mesh.mesh_id(), submesh.index, submesh.material, transform_data, instance_payload{});
100 }
101 }
102 }
103
104 static auto make_instance_data(const scenes::node node, const std::uint32_t transform_index, std::uint32_t material_index, [[maybe_unused]] const instance_payload& payload) -> instance_data {
105 return instance_data{transform_index, material_index, static_cast<std::uint32_t>(node), 0u};
106 }
107
108 template<typename Mesh, typename Emitter>
109 static auto build_draw_commands(const Mesh& mesh, std::uint32_t submesh_index, std::vector<models::instance_data>&& instances, Emitter&& emitter) -> std::uint32_t {
110 if (instances.empty()) {
111 return 0;
112 }
113
114 const auto& submesh = mesh.submesh(submesh_index);
115
116 auto command = VkDrawIndexedIndirectCommand{};
117 command.indexCount = submesh.index_count;
118 command.instanceCount = static_cast<std::uint32_t>(instances.size());
119 command.firstIndex = submesh.index_offset;
120 command.vertexOffset = static_cast<std::int32_t>(submesh.vertex_offset);
121 command.firstInstance = emitter.base_instance;
122
123 emitter.emit_instanced(command, std::move(instances));
124
125 return command.instanceCount;
126 }
127
128private:
129
130
131
132}; // static_mesh_traits
133
134using static_mesh_material_draw_list = basic_material_draw_list<static_mesh_traits>;
135
136} // namespace sbx::models
137
138#endif // LIBSBX_MODELS_STATIC_MESH_MATERIAL_DRAW_LIST_HPP_
Definition: tests.cpp:6
Definition: mesh.hpp:22
RGBA color representation and utilities.
Definition: static_mesh_material_draw_list.hpp:74
Definition: static_mesh_material_draw_list.hpp:71