Unravel Engine C++ Reference
Loading...
Searching...
No Matches
pipeline.cpp
Go to the documentation of this file.
1#include "pipeline.h"
8
13
15#define POOLSTL_STD_SUPPLEMENT 1
16#include <poolstl/poolstl.hpp>
17
18namespace unravel
19{
20namespace rendering
21{
22auto pipeline::init(rtti::context& ctx) -> bool
23{
24 prefilter_pass_.init(ctx);
25 blit_pass_.init(ctx);
26 atmospheric_pass_.init(ctx);
27 atmospheric_pass_perez_.init(ctx);
28 atmospheric_pass_skybox_.init(ctx);
29 fxaa_pass_.init(ctx);
30 tonemapping_pass_.init(ctx);
31 assao_pass_.init(ctx);
32 ssr_pass_.init(ctx);
33 hiz_pass_.init(ctx);
34
35 auto& am = ctx.get_cached<asset_manager>();
36
37 auto load_program = [&](const std::string& vs, const std::string& fs)
38 {
39 auto vs_shader = am.get_asset<gfx::shader>("engine:/data/shaders/" + vs + ".sc");
40 auto fs_shadfer = am.get_asset<gfx::shader>("engine:/data/shaders/" + fs + ".sc");
41
42 return std::make_unique<gpu_program>(vs_shader, fs_shadfer);
43 };
44
45 return true;
46}
47
48auto pipeline::gather_visible_models(scene& scn, const math::frustum* frustum, visibility_flags query, const layer_mask& render_mask)
50{
51 APP_SCOPE_PERF("Cull Models Legacy");
52
53 auto view = scn.registry->view<transform_component, model_component, layer_component, active_component>();
54
55 // Pre-allocate with estimated size
57
58 // Thread-safe collection using mutex
59 //std::mutex result_mutex;
60
61 // Use parallel execution for visibility testing
62 std::for_each(/*std::execution::par_unseq,*/ view.begin(), view.end(),
63 [&](auto entity)
64 {
65 auto&& [transform_comp, model_comp, layer_comp, active_comp] = view.get(entity);
66
67 // Get layer component if it exists, otherwise use default layer
68 auto entity_layer = layer_comp.layers;
69
70 // Early exit checks
71 if(!model_comp.is_enabled())
72 {
73 return;
74 }
75 if((query & visibility_query::is_static) && !model_comp.is_static())
76 {
77 return;
78 }
79 if((query & visibility_query::is_reflection_caster) && !model_comp.casts_reflection())
80 {
81 return;
82 }
83 if((query & visibility_query::is_shadow_caster) && !model_comp.casts_shadow())
84 {
85 return;
86 }
87
88 // Layer filtering - check if entity's layer matches camera's render mask
89 if((entity_layer.mask & render_mask.mask) == 0)
90 {
91 return; // Entity's layer is not visible to this camera
92 }
93
94 bool is_visible = true;
95
96 if(frustum)
97 {
98 const auto& world_transform = transform_comp.get_transform_global();
99 const auto& local_bounds = model_comp.get_local_bounds();
100
101 // Test the bounding box of the mesh
102 is_visible = frustum->test_obb(local_bounds, world_transform);
103 // Alternative: is_visible = frustum->test_aabb(model_comp.get_world_bounds());
104 }
105
106 if(is_visible)
107 {
108 // Thread-safe insertion
109 //std::lock_guard<std::mutex> lock(result_mutex);
110 result.emplace_back(scn.create_handle(entity));
111 }
112 });
113
114 return result;
115}
116
117
118auto pipeline::create_run_params(entt::handle camera_ent) const -> rendering::pipeline::run_params
119{
121
122 if(auto assao_comp = camera_ent.try_get<assao_component>(); assao_comp && assao_comp->enabled)
123 {
124 params.fill_assao_params = [camera_ent](assao_pass::run_params& params)
125 {
126 if(auto assao_comp = camera_ent.try_get<assao_component>())
127 {
128 params.params = assao_comp->settings;
129 }
130 };
131 }
132
133 if(auto tonemapping_comp = camera_ent.try_get<tonemapping_component>(); tonemapping_comp && tonemapping_comp->enabled)
134 {
135 params.fill_hdr_params = [camera_ent](tonemapping_pass::run_params& params)
136 {
137 if(auto tonemapping_comp = camera_ent.try_get<tonemapping_component>())
138 {
139 params.config = tonemapping_comp->settings;
140 }
141 };
142 }
143 else
144 {
145 // Always set up tonemapping params but with disabled method if component is disabled
146 params.fill_hdr_params = [camera_ent](tonemapping_pass::run_params& params)
147 {
148 params.config.method = tonemapping_method::none;
149 };
150 }
151
152 if(auto fxaa_comp = camera_ent.try_get<fxaa_component>(); fxaa_comp && fxaa_comp->enabled)
153 {
154 params.fill_fxaa_params = [camera_ent](fxaa_pass::run_params& params)
155 {
156 if(auto fxaa_comp = camera_ent.try_get<fxaa_component>())
157 {
158 // Fill FXAA parameters
159 }
160 };
161 }
162
163 if(auto ssr_comp = camera_ent.try_get<ssr_component>(); ssr_comp && ssr_comp->enabled)
164 {
165 params.fill_ssr_params = [camera_ent](ssr_pass::run_params& params)
166 {
167 if(auto ssr_comp = camera_ent.try_get<ssr_component>())
168 {
169 params.settings = ssr_comp->settings;
170 }
171 };
172 }
173
174 return params;
175}
176
177void pipeline::ui_pass(scene& scn, const camera& camera, gfx::render_view& rview, const gfx::frame_buffer::ptr& output)
178{
179 APP_SCOPE_PERF("Rendering/UI Pass");
180
181 const auto& view = camera.get_view();
182 const auto& proj = camera.get_projection();
183 auto& fbo = rview.fbo_get("OBUFFER_DEPTH");
184
185
186 gfx::render_pass pass("ui_elements_pass");
187 pass.bind(fbo.get());
188 pass.set_view_proj(view, proj);
189
191 [&](auto e, auto&& transform_comp, auto&& text_comp, auto&& active)
192 {
193 const auto& world_transform = transform_comp.get_transform_global();
194 auto bbox = text_comp.get_bounds();
195
196 if(!camera.test_obb(bbox, world_transform))
197 {
198 return;
199 }
200
201 text_comp.submit(pass.id, world_transform, BGFX_STATE_DEPTH_TEST_LESS);
202 });
203
204 gfx::discard();
205}
206
207} // namespace rendering
208} // namespace unravel
auto fbo_get(const hpp::string_view &id) const -> const frame_buffer::ptr &
Storage for frustum planes / values and wraps up common functionality.
Definition frustum.h:18
bool enabled
Whether ASSAO is enabled.
Manages assets, including loading, unloading, and storage.
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:35
auto test_obb(const math::bbox &bounds, const math::transform &t) const -> bool
Tests if the specified OBB is within the frustum.
Definition camera.cpp:444
auto get_projection() const -> const math::transform &
Retrieves the current projection matrix.
Definition camera.cpp:203
auto get_view() const -> const math::transform &
Retrieves the current view matrix.
Definition camera.cpp:276
bool enabled
Whether FXAA is enabled.
Class that contains core data for meshes.
virtual auto init(rtti::context &ctx) -> bool
Definition pipeline.cpp:22
virtual auto gather_visible_models(scene &scn, const math::frustum *frustum, visibility_flags query, const layer_mask &render_mask) -> visibility_set_models_t
Gathers visible models from the scene based on the given query.
Definition pipeline.cpp:48
uint32_t visibility_flags
Type alias for visibility flags.
Definition pipeline.h:79
bool enabled
Whether SSR is enabled.
bool enabled
Whether tonemapping is enabled.
Component that handles transformations (position, rotation, scale, etc.) in the ACE framework.
Definition cache.hpp:11
void discard(uint8_t _flags)
Definition graphics.cpp:968
hpp::small_vector< entt::handle > visibility_set_models_t
Definition pipeline.h:44
#define APP_SCOPE_PERF(name)
Create a scoped performance timer that only accepts string literals.
Definition profiler.h:160
entt::entity entity
void set_view_proj(const float *v, const float *p)
gfx::view_id id
Definition render_pass.h:98
void bind(const frame_buffer *fb=nullptr) const
Component that provides a layer mask for an entity.
std::function< void(assao_pass::run_params &params)> fill_assao_params
Definition pipeline.h:85
std::function< void(ssr_pass::run_params &params)> fill_ssr_params
Definition pipeline.h:88
std::function< void(tonemapping_pass::run_params &params)> fill_hdr_params
Definition pipeline.h:86
std::function< void(fxaa_pass::run_params &params)> fill_fxaa_params
Definition pipeline.h:87
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:21
std::unique_ptr< entt::registry > registry
The registry that manages all entities in the scene.
Definition scene.h:117