Unravel Engine C++ Reference
Loading...
Searching...
No Matches
rendering_system.cpp
Go to the documentation of this file.
1#include "rendering_system.h"
2#include "particle_system.h"
3
9
13#include <engine/engine.h>
14#include <engine/events.h>
15
18
23
24namespace unravel
25{
26
28{
29 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
30 auto& ev = ctx.get_cached<events>();
31
32 ev.on_frame_end.connect(sentinel_, 1000, this, &rendering_system::on_frame_end);
33
34 debug_draw_callbacks_.reserve(128);
35 return true;
36}
37
39{
40 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
41
42 return true;
43}
44
46{
47 APP_SCOPE_PERF("Rendering/On Frame End");
48 debug_draw_callbacks_.clear();
49}
50
52{
53 APP_SCOPE_PERF("Rendering/On Frame Update");
54 auto& ctx = engine::context();
55 ctx.get_cached<transform_system>().on_frame_update(scn, dt);
56 ctx.get_cached<camera_system>().on_frame_update(scn, dt);
57 ctx.get_cached<model_system>().on_frame_update(scn, dt);
58 ctx.get_cached<animation_system>().on_frame_update(scn, dt);
59 ctx.get_cached<reflection_probe_system>().on_frame_update(scn, dt);
60 ctx.get_cached<skylight_system>().on_frame_update(scn, dt);
61}
62
64{
65 APP_SCOPE_PERF("Rendering/On Frame Before Render");
66 auto& ctx = engine::context();
67 ctx.get_cached<model_system>().on_frame_before_render(scn, dt);
68 ctx.get_cached<camera_system>().on_frame_before_render(scn, dt);
69 ctx.get_cached<particle_system>().on_frame_before_render(scn, dt);
70
71
72}
73
74void rendering_system::on_play_begin(hpp::span<const entt::handle> entities, delta_t dt)
75{
76 APP_SCOPE_PERF("Rendering/On Play Begin");
77 auto& ctx = engine::context();
78 ctx.get_cached<transform_system>().on_play_begin(entities, dt);
79 ctx.get_cached<camera_system>().on_play_begin(entities, dt);
80 ctx.get_cached<model_system>().on_play_begin(entities, dt);
81 ctx.get_cached<animation_system>().on_play_begin(entities, dt);
82 ctx.get_cached<reflection_probe_system>().on_play_begin(entities, dt);
83}
84
86{
87 APP_SCOPE_PERF("Rendering/Release Pipeline Resources");
88 auto& ctx = engine::context();
89 scn.registry->view<camera_component>().each(
90 [&](auto e, auto&& camera_comp)
91 {
92 auto& pipeline_data = camera_comp.get_pipeline_data();
93 auto& camera = pipeline_data.get_camera();
94 auto& pipeline = pipeline_data.get_pipeline();
95 auto& rview = camera_comp.get_render_view();
96 rview = {};
97 return;
98 });
99 return true;
100}
101
102auto rendering_system::render_scene(entt::handle camera_ent, camera_component& camera_comp, scene& scn, delta_t dt,
103 bool render_screen_space) -> gfx::frame_buffer::ptr
104{
105 APP_SCOPE_PERF("Rendering/Render Scene Camera");
106 auto& ctx = engine::context();
107 auto& pipeline_data = camera_comp.get_pipeline_data();
108 auto& camera = pipeline_data.get_camera();
109 auto& pipeline = pipeline_data.get_pipeline();
110 auto& rview = camera_comp.get_render_view();
111
112 auto& ui = ctx.get_cached<ui_system>();
113 bool process_input = render_screen_space;
114 ui.update_screen_space(ctx, camera_ent, scn, process_input);
115 ui.update_world_space(ctx, camera_ent, scn, process_input);
116
117 ui.render_world_space(ctx, camera_ent, scn, dt);
118
119 auto params = pipeline->create_run_params(camera_ent, &scn, &camera);
120 auto result = pipeline->run_pipeline(scn, camera, rview, dt, params, camera_comp.get_render_mask());
121 render_debug(camera_ent);
122 if(render_screen_space)
123 {
124 ui.render_screen_space(ctx, result, camera_ent, scn, dt);
125 }
126 return result;
127}
128
130{
131 APP_SCOPE_PERF("Rendering/Render Scene");
132 auto& ctx = engine::context();
133 gfx::frame_buffer::ptr output{};
134 scn.registry->view<camera_component>().each(
135 [&](auto e, auto&& camera_comp)
136 {
137 auto handle = scn.create_handle(e);
138 output = render_scene(handle, camera_comp, scn, dt, true);
139 return;
140 });
141 return output;
142}
143
145 entt::handle camera_ent,
146 camera_component& camera_comp,
147 scene& scn,
148 delta_t dt,
149 bool render_screen_space)
150{
151 APP_SCOPE_PERF("Rendering/Render Scene");
152 auto& ctx = engine::context();
153 auto& pipeline_data = camera_comp.get_pipeline_data();
154 auto& camera = pipeline_data.get_camera();
155 auto& pipeline = pipeline_data.get_pipeline();
156 auto& rview = camera_comp.get_render_view();
157 auto& ui = ctx.get_cached<ui_system>();
158
159 bool process_input = render_screen_space;
160 ui.update_screen_space(ctx, camera_ent, scn, process_input);
161 ui.update_world_space(ctx, camera_ent, scn, process_input);
162
163 ui.render_world_space(ctx, camera_ent, scn, dt);
164
165 auto params = pipeline->create_run_params(camera_ent, &scn, &camera);
166 pipeline->run_pipeline(output, scn, camera, rview, dt, params, camera_comp.get_render_mask());
167 render_debug(camera_ent);
168 if(render_screen_space)
169 {
170 ui.render_screen_space(ctx, output, camera_ent, scn, dt);
171 }
172}
173
175{
176 APP_SCOPE_PERF("Rendering/Render Scene");
177 auto& ctx = engine::context();
178 scn.registry->view<camera_component>().each(
179 [&](auto e, auto&& camera_comp)
180 {
181 auto handle = scn.create_handle(e);
182 render_scene(output, handle, camera_comp, scn, dt, true);
183 });
184}
185
186void rendering_system::render_debug(entt::handle camera_entity)
187{
188 if(debug_draw_callbacks_.empty())
189 {
190 return;
191 }
192
193 APP_SCOPE_PERF("Rendering/Render Debug");
194
195 auto& camera_comp = camera_entity.get<camera_component>();
196 const auto& rview = camera_comp.get_render_view();
197 const auto& camera = camera_comp.get_camera();
198 const auto& view = camera.get_view();
199 const auto& proj = camera.get_projection();
200 const auto& obuffer = rview.fbo_get("OBUFFER");
201
202 gfx::render_pass pass("Debug Draw Pass");
203 pass.bind(obuffer.get());
204 pass.set_view_proj(view, proj);
205
206 gfx::dd_raii dd(pass.id);
207
208 for(const auto& callback : debug_draw_callbacks_)
209 {
210 callback(dd);
211 }
212}
213
214void rendering_system::add_debugdraw_call(const std::function<void(gfx::dd_raii& dd)>& callback)
215{
216 debug_draw_callbacks_.emplace_back(callback);
217}
218
219
220} // namespace unravel
Class that contains core camera data, used for rendering and other purposes.
auto get_pipeline_data() -> pipeline_camera &
auto get_render_mask() const -> layer_mask
Gets the effective render mask (include - exclude).
auto get_render_view() -> gfx::render_view &
Gets the render view.
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:62
System that manages and updates particle emitters in the ECS.
void on_play_begin(hpp::span< const entt::handle > entities, delta_t dt)
auto init(rtti::context &ctx) -> bool
Initializes the rendering path with the given context.
void add_debugdraw_call(const std::function< void(gfx::dd_raii &dd)> &callback)
void on_frame_update(scene &scn, delta_t dt)
Prepares the scene for rendering.
void on_frame_before_render(scene &scn, delta_t dt)
void on_frame_end(rtti::context &ctx, delta_t)
auto release_pipeline_resources(scene &scn) -> bool
auto render_scene(scene &scn, delta_t dt) -> gfx::frame_buffer::ptr
Renders the scene and returns the frame buffer.
auto deinit(rtti::context &ctx) -> bool
Deinitializes the rendering path with the given context.
std::chrono::duration< float > delta_t
uint16_t view
#define APPLOG_TRACE(...)
Definition logging.h:17
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
static auto context() -> rtti::context &
Definition engine.cpp:111
hpp::event< void(rtti::context &, delta_t)> on_frame_end
Definition events.h:20
Represents a scene in the ACE framework, managing entities and their relationships.
Definition scene.h:70
std::unique_ptr< entt::registry > registry
The registry that manages all entities in the scene.
Definition scene.h:187
auto create_handle(entt::entity e) -> entt::handle
Creates an entity in the scene.
Definition scene.cpp:428
System responsible for managing user interface components and rendering.
Definition ui_system.h:39
void update_screen_space(rtti::context &ctx, entt::handle camera_entity, scene &scn, bool &process_input)
Update screen-space UI documents (common + screen-space specific). Call before render_screen_space.
gfx::uniform_handle handle
Definition uniform.cpp:9