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