Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/rendering/rendering_server_default.cpp
20898 views
1
/**************************************************************************/
2
/* rendering_server_default.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "rendering_server_default.h"
32
33
#include "core/os/os.h"
34
#include "core/profiling/profiling.h"
35
#include "renderer_canvas_cull.h"
36
#include "renderer_scene_cull.h"
37
#include "rendering_server_globals.h"
38
39
// careful, these may run in different threads than the rendering server
40
41
int RenderingServerDefault::changes = 0;
42
43
/* FREE */
44
45
void RenderingServerDefault::_free(RID p_rid) {
46
if (unlikely(p_rid.is_null())) {
47
return;
48
}
49
if (RSG::utilities->free(p_rid)) {
50
return;
51
}
52
if (RSG::canvas->free(p_rid)) {
53
return;
54
}
55
if (RSG::viewport->free(p_rid)) {
56
return;
57
}
58
if (RSG::scene->free(p_rid)) {
59
return;
60
}
61
}
62
63
/* EVENT QUEUING */
64
65
void RenderingServerDefault::request_frame_drawn_callback(const Callable &p_callable) {
66
frame_drawn_callbacks.push_back(p_callable);
67
}
68
69
void RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) {
70
GodotProfileZoneGroupedFirst(_profile_zone, "rasterizer->begin_frame");
71
RSG::rasterizer->begin_frame(frame_step);
72
73
TIMESTAMP_BEGIN()
74
75
uint64_t time_usec = OS::get_singleton()->get_ticks_usec();
76
77
RENDER_TIMESTAMP("Prepare Render Frame");
78
79
#ifndef XR_DISABLED
80
GodotProfileZoneGrouped(_profile_zone, "xr_server->pre_render");
81
XRServer *xr_server = XRServer::get_singleton();
82
if (xr_server != nullptr) {
83
// Let XR server know we're about to render a frame.
84
xr_server->pre_render();
85
}
86
#endif // XR_DISABLED
87
88
GodotProfileZoneGrouped(_profile_zone, "scene->update");
89
RSG::scene->update(); //update scenes stuff before updating instances
90
GodotProfileZoneGrouped(_profile_zone, "canvas->update");
91
RSG::canvas->update();
92
93
frame_setup_time = double(OS::get_singleton()->get_ticks_usec() - time_usec) / 1000.0;
94
95
GodotProfileZoneGrouped(_profile_zone, "particles_storage->update_particles");
96
RSG::particles_storage->update_particles(); //need to be done after instances are updated (colliders and particle transforms), and colliders are rendered
97
98
GodotProfileZoneGrouped(_profile_zone, "scene->render_probes");
99
RSG::scene->render_probes();
100
101
GodotProfileZoneGrouped(_profile_zone, "viewport->draw_viewports");
102
RSG::viewport->draw_viewports(p_swap_buffers);
103
104
GodotProfileZoneGrouped(_profile_zone, "canvas_render->update");
105
RSG::canvas_render->update();
106
107
GodotProfileZoneGrouped(_profile_zone, "rasterizer->end_frame");
108
RSG::rasterizer->end_frame(p_swap_buffers);
109
110
#ifndef XR_DISABLED
111
if (xr_server != nullptr) {
112
GodotProfileZone("xr_server->end_frame");
113
// let our XR server know we're done so we can get our frame timing
114
xr_server->end_frame();
115
}
116
#endif // XR_DISABLED
117
118
GodotProfileZoneGrouped(_profile_zone, "update_visibility_notifiers");
119
RSG::canvas->update_visibility_notifiers();
120
RSG::scene->update_visibility_notifiers();
121
122
GodotProfileZoneGrouped(_profile_zone, "post_draw_steps");
123
if (create_thread) {
124
callable_mp(this, &RenderingServerDefault::_run_post_draw_steps).call_deferred();
125
} else {
126
_run_post_draw_steps();
127
}
128
129
if (RSG::utilities->get_captured_timestamps_count()) {
130
GodotProfileZoneGrouped(_profile_zone, "frame_profile");
131
Vector<FrameProfileArea> new_profile;
132
if (RSG::utilities->capturing_timestamps) {
133
new_profile.resize(RSG::utilities->get_captured_timestamps_count());
134
}
135
136
uint64_t base_cpu = RSG::utilities->get_captured_timestamp_cpu_time(0);
137
uint64_t base_gpu = RSG::utilities->get_captured_timestamp_gpu_time(0);
138
for (uint32_t i = 0; i < RSG::utilities->get_captured_timestamps_count(); i++) {
139
uint64_t time_cpu = RSG::utilities->get_captured_timestamp_cpu_time(i);
140
uint64_t time_gpu = RSG::utilities->get_captured_timestamp_gpu_time(i);
141
142
String name = RSG::utilities->get_captured_timestamp_name(i);
143
144
if (name.begins_with("vp_")) {
145
RSG::viewport->handle_timestamp(name, time_cpu, time_gpu);
146
}
147
148
if (RSG::utilities->capturing_timestamps) {
149
new_profile.write[i].gpu_msec = double((time_gpu - base_gpu) / 1000) / 1000.0;
150
new_profile.write[i].cpu_msec = double(time_cpu - base_cpu) / 1000.0;
151
new_profile.write[i].name = RSG::utilities->get_captured_timestamp_name(i);
152
}
153
}
154
155
frame_profile = new_profile;
156
}
157
158
frame_profile_frame = RSG::utilities->get_captured_timestamps_frame();
159
160
if (print_gpu_profile) {
161
GodotProfileZoneGrouped(_profile_zone, "gpu_profile");
162
if (print_frame_profile_ticks_from == 0) {
163
print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();
164
}
165
double total_time = 0.0;
166
167
for (int i = 0; i < frame_profile.size() - 1; i++) {
168
String name = frame_profile[i].name;
169
if (name[0] == '<' || name[0] == '>') {
170
continue;
171
}
172
173
double time = frame_profile[i + 1].gpu_msec - frame_profile[i].gpu_msec;
174
175
if (print_gpu_profile_task_time.has(name)) {
176
print_gpu_profile_task_time[name] += time;
177
} else {
178
print_gpu_profile_task_time[name] = time;
179
}
180
}
181
182
if (frame_profile.size()) {
183
total_time = frame_profile[frame_profile.size() - 1].gpu_msec;
184
}
185
186
uint64_t ticks_elapsed = OS::get_singleton()->get_ticks_usec() - print_frame_profile_ticks_from;
187
print_frame_profile_frame_count++;
188
if (ticks_elapsed > 1000000) {
189
print_line("GPU PROFILE (total " + rtos(total_time) + "ms): ");
190
191
float print_threshold = 0.01;
192
for (const KeyValue<String, float> &E : print_gpu_profile_task_time) {
193
double time = E.value / double(print_frame_profile_frame_count);
194
if (time > print_threshold) {
195
print_line("\t-" + E.key + ": " + rtos(time) + "ms");
196
}
197
}
198
print_gpu_profile_task_time.clear();
199
print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();
200
print_frame_profile_frame_count = 0;
201
}
202
}
203
204
GodotProfileZoneGrouped(_profile_zone, "memory_info");
205
RSG::utilities->update_memory_info();
206
}
207
208
void RenderingServerDefault::_run_post_draw_steps() {
209
while (frame_drawn_callbacks.front()) {
210
Callable c = frame_drawn_callbacks.front()->get();
211
Variant result;
212
Callable::CallError ce;
213
c.callp(nullptr, 0, result, ce);
214
if (ce.error != Callable::CallError::CALL_OK) {
215
String err = Variant::get_callable_error_text(c, nullptr, 0, ce);
216
ERR_PRINT("Error calling frame drawn function: " + err);
217
}
218
219
frame_drawn_callbacks.pop_front();
220
}
221
222
emit_signal(SNAME("frame_post_draw"));
223
}
224
225
double RenderingServerDefault::get_frame_setup_time_cpu() const {
226
return frame_setup_time;
227
}
228
229
bool RenderingServerDefault::has_changed() const {
230
return changes > 0;
231
}
232
233
void RenderingServerDefault::_init() {
234
RSG::threaded = create_thread;
235
236
RSG::canvas = memnew(RendererCanvasCull);
237
RSG::viewport = memnew(RendererViewport);
238
RendererSceneCull *sr = memnew(RendererSceneCull);
239
RSG::camera_attributes = memnew(RendererCameraAttributes);
240
RSG::scene = sr;
241
RSG::rasterizer = RendererCompositor::create();
242
RSG::utilities = RSG::rasterizer->get_utilities();
243
RSG::rasterizer->initialize();
244
RSG::light_storage = RSG::rasterizer->get_light_storage();
245
RSG::material_storage = RSG::rasterizer->get_material_storage();
246
RSG::mesh_storage = RSG::rasterizer->get_mesh_storage();
247
RSG::particles_storage = RSG::rasterizer->get_particles_storage();
248
RSG::texture_storage = RSG::rasterizer->get_texture_storage();
249
RSG::gi = RSG::rasterizer->get_gi();
250
RSG::fog = RSG::rasterizer->get_fog();
251
RSG::canvas_render = RSG::rasterizer->get_canvas();
252
sr->set_scene_render(RSG::rasterizer->get_scene());
253
}
254
255
void RenderingServerDefault::_finish() {
256
if (test_cube.is_valid()) {
257
free_rid(test_cube);
258
}
259
260
RSG::canvas->finalize();
261
memdelete(RSG::canvas);
262
RSG::rasterizer->finalize();
263
memdelete(RSG::viewport);
264
memdelete(RSG::rasterizer);
265
memdelete(RSG::scene);
266
memdelete(RSG::camera_attributes);
267
}
268
269
void RenderingServerDefault::init() {
270
if (create_thread) {
271
print_verbose("RenderingServerWrapMT: Starting render thread");
272
DisplayServer::get_singleton()->release_rendering_thread();
273
WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &RenderingServerDefault::_thread_loop), true, "Rendering Server pump task", true);
274
command_queue.set_pump_task_id(tid);
275
command_queue.push(this, &RenderingServerDefault::_assign_mt_ids, tid);
276
command_queue.push_and_sync(this, &RenderingServerDefault::_init);
277
DEV_ASSERT(server_task_id == tid);
278
} else {
279
server_thread = Thread::MAIN_ID;
280
_init();
281
}
282
}
283
284
void RenderingServerDefault::finish() {
285
if (create_thread) {
286
command_queue.push(this, &RenderingServerDefault::_finish);
287
command_queue.push(this, &RenderingServerDefault::_thread_exit);
288
if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {
289
WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);
290
server_task_id = WorkerThreadPool::INVALID_TASK_ID;
291
}
292
server_thread = Thread::MAIN_ID;
293
} else {
294
_finish();
295
}
296
}
297
298
/* STATUS INFORMATION */
299
300
uint64_t RenderingServerDefault::get_rendering_info(RenderingInfo p_info) {
301
if (p_info == RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME) {
302
return RSG::viewport->get_total_objects_drawn();
303
} else if (p_info == RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME) {
304
return RSG::viewport->get_total_primitives_drawn();
305
} else if (p_info == RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME) {
306
return RSG::viewport->get_total_draw_calls_used();
307
} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS) {
308
return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_CANVAS);
309
} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_MESH) {
310
return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_MESH) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_MESH);
311
} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE) {
312
return RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_SURFACE);
313
} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW) {
314
return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_DRAW) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_DRAW);
315
} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION) {
316
return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_SPECIALIZATION) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_SPECIALIZATION);
317
}
318
return RSG::utilities->get_rendering_info(p_info);
319
}
320
321
RenderingDevice::DeviceType RenderingServerDefault::get_video_adapter_type() const {
322
return RSG::utilities->get_video_adapter_type();
323
}
324
325
void RenderingServerDefault::set_frame_profiling_enabled(bool p_enable) {
326
RSG::utilities->capturing_timestamps = p_enable;
327
}
328
329
uint64_t RenderingServerDefault::get_frame_profile_frame() {
330
return frame_profile_frame;
331
}
332
333
Vector<RenderingServer::FrameProfileArea> RenderingServerDefault::get_frame_profile() {
334
return frame_profile;
335
}
336
337
/* TESTING */
338
339
Color RenderingServerDefault::get_default_clear_color() {
340
return RSG::texture_storage->get_default_clear_color();
341
}
342
343
void RenderingServerDefault::set_default_clear_color(const Color &p_color) {
344
RSG::texture_storage->set_default_clear_color(p_color);
345
}
346
347
#ifndef DISABLE_DEPRECATED
348
bool RenderingServerDefault::has_feature(Features p_feature) const {
349
return false;
350
}
351
#endif
352
353
void RenderingServerDefault::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {
354
RSG::scene->sdfgi_set_debug_probe_select(p_position, p_dir);
355
}
356
357
void RenderingServerDefault::set_print_gpu_profile(bool p_enable) {
358
RSG::utilities->capturing_timestamps = p_enable;
359
print_gpu_profile = p_enable;
360
}
361
362
RID RenderingServerDefault::get_test_cube() {
363
if (!test_cube.is_valid()) {
364
test_cube = _make_test_cube();
365
}
366
return test_cube;
367
}
368
369
bool RenderingServerDefault::has_os_feature(const String &p_feature) const {
370
if (RSG::utilities) {
371
return RSG::utilities->has_os_feature(p_feature);
372
} else {
373
return false;
374
}
375
}
376
377
void RenderingServerDefault::set_debug_generate_wireframes(bool p_generate) {
378
RSG::utilities->set_debug_generate_wireframes(p_generate);
379
}
380
381
bool RenderingServerDefault::is_low_end() const {
382
return RendererCompositor::is_low_end();
383
}
384
385
Size2i RenderingServerDefault::get_maximum_viewport_size() const {
386
if (RSG::utilities) {
387
return RSG::utilities->get_maximum_viewport_size();
388
} else {
389
return Size2i();
390
}
391
}
392
393
void RenderingServerDefault::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {
394
server_thread = Thread::get_caller_id();
395
server_task_id = p_pump_task_id;
396
397
RenderingDevice *rd = RenderingDevice::get_singleton();
398
if (rd) {
399
// This is needed because the main RD is created on the main thread.
400
rd->make_current();
401
}
402
}
403
404
void RenderingServerDefault::_thread_exit() {
405
exit = true;
406
}
407
408
void RenderingServerDefault::_thread_loop() {
409
DisplayServer::get_singleton()->gl_window_make_current(DisplayServer::MAIN_WINDOW_ID); // Move GL to this thread.
410
411
while (!exit) {
412
WorkerThreadPool::get_singleton()->yield();
413
command_queue.flush_all();
414
}
415
416
DisplayServer::get_singleton()->release_rendering_thread();
417
}
418
419
/* INTERPOLATION */
420
421
void RenderingServerDefault::set_physics_interpolation_enabled(bool p_enabled) {
422
RSG::canvas->set_physics_interpolation_enabled(p_enabled);
423
RSG::scene->set_physics_interpolation_enabled(p_enabled);
424
}
425
426
/* EVENT QUEUING */
427
428
void RenderingServerDefault::sync() {
429
if (create_thread) {
430
command_queue.sync();
431
} else {
432
command_queue.flush_all(); // Flush all pending from other threads.
433
}
434
}
435
436
void RenderingServerDefault::draw(bool p_present, double frame_step) {
437
ERR_FAIL_COND_MSG(!Thread::is_main_thread(), "Manually triggering the draw function from the RenderingServer can only be done on the main thread. Call this function from the main thread or use call_deferred().");
438
// Needs to be done before changes is reset to 0, to not force the editor to redraw.
439
RS::get_singleton()->emit_signal(SNAME("frame_pre_draw"));
440
changes = 0;
441
if (create_thread) {
442
command_queue.push(this, &RenderingServerDefault::_draw, p_present, frame_step);
443
} else {
444
_draw(p_present, frame_step);
445
}
446
}
447
448
void RenderingServerDefault::tick() {
449
RSG::canvas->tick();
450
RSG::scene->tick();
451
}
452
453
void RenderingServerDefault::pre_draw(bool p_will_draw) {
454
RSG::scene->pre_draw(p_will_draw);
455
}
456
457
void RenderingServerDefault::_call_on_render_thread(const Callable &p_callable) {
458
p_callable.call();
459
}
460
461
RenderingServerDefault::RenderingServerDefault(bool p_create_thread) {
462
RenderingServer::init();
463
464
create_thread = p_create_thread;
465
}
466
467
RenderingServerDefault::~RenderingServerDefault() {
468
}
469
470