Path: blob/master/servers/rendering/rendering_server_default.cpp
10277 views
/**************************************************************************/1/* rendering_server_default.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "rendering_server_default.h"3132#include "core/os/os.h"33#include "renderer_canvas_cull.h"34#include "renderer_scene_cull.h"35#include "rendering_server_globals.h"3637// careful, these may run in different threads than the rendering server3839int RenderingServerDefault::changes = 0;4041/* FREE */4243void RenderingServerDefault::_free(RID p_rid) {44if (unlikely(p_rid.is_null())) {45return;46}47if (RSG::utilities->free(p_rid)) {48return;49}50if (RSG::canvas->free(p_rid)) {51return;52}53if (RSG::viewport->free(p_rid)) {54return;55}56if (RSG::scene->free(p_rid)) {57return;58}59}6061/* EVENT QUEUING */6263void RenderingServerDefault::request_frame_drawn_callback(const Callable &p_callable) {64frame_drawn_callbacks.push_back(p_callable);65}6667void RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) {68RSG::rasterizer->begin_frame(frame_step);6970TIMESTAMP_BEGIN()7172uint64_t time_usec = OS::get_singleton()->get_ticks_usec();7374RENDER_TIMESTAMP("Prepare Render Frame");7576#ifndef XR_DISABLED77XRServer *xr_server = XRServer::get_singleton();78if (xr_server != nullptr) {79// Let XR server know we're about to render a frame.80xr_server->pre_render();81}82#endif // XR_DISABLED8384RSG::scene->update(); //update scenes stuff before updating instances85RSG::canvas->update();8687frame_setup_time = double(OS::get_singleton()->get_ticks_usec() - time_usec) / 1000.0;8889RSG::particles_storage->update_particles(); //need to be done after instances are updated (colliders and particle transforms), and colliders are rendered9091RSG::scene->render_probes();9293RSG::viewport->draw_viewports(p_swap_buffers);94RSG::canvas_render->update();9596RSG::rasterizer->end_frame(p_swap_buffers);9798#ifndef XR_DISABLED99if (xr_server != nullptr) {100// let our XR server know we're done so we can get our frame timing101xr_server->end_frame();102}103#endif // XR_DISABLED104105RSG::canvas->update_visibility_notifiers();106RSG::scene->update_visibility_notifiers();107108if (create_thread) {109callable_mp(this, &RenderingServerDefault::_run_post_draw_steps).call_deferred();110} else {111_run_post_draw_steps();112}113114if (RSG::utilities->get_captured_timestamps_count()) {115Vector<FrameProfileArea> new_profile;116if (RSG::utilities->capturing_timestamps) {117new_profile.resize(RSG::utilities->get_captured_timestamps_count());118}119120uint64_t base_cpu = RSG::utilities->get_captured_timestamp_cpu_time(0);121uint64_t base_gpu = RSG::utilities->get_captured_timestamp_gpu_time(0);122for (uint32_t i = 0; i < RSG::utilities->get_captured_timestamps_count(); i++) {123uint64_t time_cpu = RSG::utilities->get_captured_timestamp_cpu_time(i);124uint64_t time_gpu = RSG::utilities->get_captured_timestamp_gpu_time(i);125126String name = RSG::utilities->get_captured_timestamp_name(i);127128if (name.begins_with("vp_")) {129RSG::viewport->handle_timestamp(name, time_cpu, time_gpu);130}131132if (RSG::utilities->capturing_timestamps) {133new_profile.write[i].gpu_msec = double((time_gpu - base_gpu) / 1000) / 1000.0;134new_profile.write[i].cpu_msec = double(time_cpu - base_cpu) / 1000.0;135new_profile.write[i].name = RSG::utilities->get_captured_timestamp_name(i);136}137}138139frame_profile = new_profile;140}141142frame_profile_frame = RSG::utilities->get_captured_timestamps_frame();143144if (print_gpu_profile) {145if (print_frame_profile_ticks_from == 0) {146print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();147}148double total_time = 0.0;149150for (int i = 0; i < frame_profile.size() - 1; i++) {151String name = frame_profile[i].name;152if (name[0] == '<' || name[0] == '>') {153continue;154}155156double time = frame_profile[i + 1].gpu_msec - frame_profile[i].gpu_msec;157158if (print_gpu_profile_task_time.has(name)) {159print_gpu_profile_task_time[name] += time;160} else {161print_gpu_profile_task_time[name] = time;162}163}164165if (frame_profile.size()) {166total_time = frame_profile[frame_profile.size() - 1].gpu_msec;167}168169uint64_t ticks_elapsed = OS::get_singleton()->get_ticks_usec() - print_frame_profile_ticks_from;170print_frame_profile_frame_count++;171if (ticks_elapsed > 1000000) {172print_line("GPU PROFILE (total " + rtos(total_time) + "ms): ");173174float print_threshold = 0.01;175for (const KeyValue<String, float> &E : print_gpu_profile_task_time) {176double time = E.value / double(print_frame_profile_frame_count);177if (time > print_threshold) {178print_line("\t-" + E.key + ": " + rtos(time) + "ms");179}180}181print_gpu_profile_task_time.clear();182print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();183print_frame_profile_frame_count = 0;184}185}186187RSG::utilities->update_memory_info();188}189190void RenderingServerDefault::_run_post_draw_steps() {191while (frame_drawn_callbacks.front()) {192Callable c = frame_drawn_callbacks.front()->get();193Variant result;194Callable::CallError ce;195c.callp(nullptr, 0, result, ce);196if (ce.error != Callable::CallError::CALL_OK) {197String err = Variant::get_callable_error_text(c, nullptr, 0, ce);198ERR_PRINT("Error calling frame drawn function: " + err);199}200201frame_drawn_callbacks.pop_front();202}203204emit_signal(SNAME("frame_post_draw"));205}206207double RenderingServerDefault::get_frame_setup_time_cpu() const {208return frame_setup_time;209}210211bool RenderingServerDefault::has_changed() const {212return changes > 0;213}214215void RenderingServerDefault::_init() {216RSG::threaded = create_thread;217218RSG::canvas = memnew(RendererCanvasCull);219RSG::viewport = memnew(RendererViewport);220RendererSceneCull *sr = memnew(RendererSceneCull);221RSG::camera_attributes = memnew(RendererCameraAttributes);222RSG::scene = sr;223RSG::rasterizer = RendererCompositor::create();224RSG::utilities = RSG::rasterizer->get_utilities();225RSG::rasterizer->initialize();226RSG::light_storage = RSG::rasterizer->get_light_storage();227RSG::material_storage = RSG::rasterizer->get_material_storage();228RSG::mesh_storage = RSG::rasterizer->get_mesh_storage();229RSG::particles_storage = RSG::rasterizer->get_particles_storage();230RSG::texture_storage = RSG::rasterizer->get_texture_storage();231RSG::gi = RSG::rasterizer->get_gi();232RSG::fog = RSG::rasterizer->get_fog();233RSG::canvas_render = RSG::rasterizer->get_canvas();234sr->set_scene_render(RSG::rasterizer->get_scene());235}236237void RenderingServerDefault::_finish() {238if (test_cube.is_valid()) {239free(test_cube);240}241242RSG::canvas->finalize();243memdelete(RSG::canvas);244RSG::rasterizer->finalize();245memdelete(RSG::viewport);246memdelete(RSG::rasterizer);247memdelete(RSG::scene);248memdelete(RSG::camera_attributes);249}250251void RenderingServerDefault::init() {252if (create_thread) {253print_verbose("RenderingServerWrapMT: Starting render thread");254DisplayServer::get_singleton()->release_rendering_thread();255WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &RenderingServerDefault::_thread_loop), true, "Rendering Server pump task", true);256command_queue.set_pump_task_id(tid);257command_queue.push(this, &RenderingServerDefault::_assign_mt_ids, tid);258command_queue.push_and_sync(this, &RenderingServerDefault::_init);259DEV_ASSERT(server_task_id == tid);260} else {261server_thread = Thread::MAIN_ID;262_init();263}264}265266void RenderingServerDefault::finish() {267if (create_thread) {268command_queue.push(this, &RenderingServerDefault::_finish);269command_queue.push(this, &RenderingServerDefault::_thread_exit);270if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {271WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);272server_task_id = WorkerThreadPool::INVALID_TASK_ID;273}274server_thread = Thread::MAIN_ID;275} else {276_finish();277}278}279280/* STATUS INFORMATION */281282uint64_t RenderingServerDefault::get_rendering_info(RenderingInfo p_info) {283if (p_info == RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME) {284return RSG::viewport->get_total_objects_drawn();285} else if (p_info == RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME) {286return RSG::viewport->get_total_primitives_drawn();287} else if (p_info == RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME) {288return RSG::viewport->get_total_draw_calls_used();289} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS) {290return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_CANVAS);291} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_MESH) {292return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_MESH) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_MESH);293} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE) {294return RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_SURFACE);295} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW) {296return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_DRAW) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_DRAW);297} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION) {298return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_SPECIALIZATION) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_SPECIALIZATION);299}300return RSG::utilities->get_rendering_info(p_info);301}302303RenderingDevice::DeviceType RenderingServerDefault::get_video_adapter_type() const {304return RSG::utilities->get_video_adapter_type();305}306307void RenderingServerDefault::set_frame_profiling_enabled(bool p_enable) {308RSG::utilities->capturing_timestamps = p_enable;309}310311uint64_t RenderingServerDefault::get_frame_profile_frame() {312return frame_profile_frame;313}314315Vector<RenderingServer::FrameProfileArea> RenderingServerDefault::get_frame_profile() {316return frame_profile;317}318319/* TESTING */320321Color RenderingServerDefault::get_default_clear_color() {322return RSG::texture_storage->get_default_clear_color();323}324325void RenderingServerDefault::set_default_clear_color(const Color &p_color) {326RSG::texture_storage->set_default_clear_color(p_color);327}328329#ifndef DISABLE_DEPRECATED330bool RenderingServerDefault::has_feature(Features p_feature) const {331return false;332}333#endif334335void RenderingServerDefault::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {336RSG::scene->sdfgi_set_debug_probe_select(p_position, p_dir);337}338339void RenderingServerDefault::set_print_gpu_profile(bool p_enable) {340RSG::utilities->capturing_timestamps = p_enable;341print_gpu_profile = p_enable;342}343344RID RenderingServerDefault::get_test_cube() {345if (!test_cube.is_valid()) {346test_cube = _make_test_cube();347}348return test_cube;349}350351bool RenderingServerDefault::has_os_feature(const String &p_feature) const {352if (RSG::utilities) {353return RSG::utilities->has_os_feature(p_feature);354} else {355return false;356}357}358359void RenderingServerDefault::set_debug_generate_wireframes(bool p_generate) {360RSG::utilities->set_debug_generate_wireframes(p_generate);361}362363bool RenderingServerDefault::is_low_end() const {364return RendererCompositor::is_low_end();365}366367Size2i RenderingServerDefault::get_maximum_viewport_size() const {368if (RSG::utilities) {369return RSG::utilities->get_maximum_viewport_size();370} else {371return Size2i();372}373}374375void RenderingServerDefault::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {376server_thread = Thread::get_caller_id();377server_task_id = p_pump_task_id;378379RenderingDevice *rd = RenderingDevice::get_singleton();380if (rd) {381// This is needed because the main RD is created on the main thread.382rd->make_current();383}384}385386void RenderingServerDefault::_thread_exit() {387exit = true;388}389390void RenderingServerDefault::_thread_loop() {391DisplayServer::get_singleton()->gl_window_make_current(DisplayServer::MAIN_WINDOW_ID); // Move GL to this thread.392393while (!exit) {394WorkerThreadPool::get_singleton()->yield();395command_queue.flush_all();396}397398DisplayServer::get_singleton()->release_rendering_thread();399}400401/* INTERPOLATION */402403void RenderingServerDefault::set_physics_interpolation_enabled(bool p_enabled) {404RSG::canvas->set_physics_interpolation_enabled(p_enabled);405RSG::scene->set_physics_interpolation_enabled(p_enabled);406}407408/* EVENT QUEUING */409410void RenderingServerDefault::sync() {411if (create_thread) {412command_queue.sync();413} else {414command_queue.flush_all(); // Flush all pending from other threads.415}416}417418void RenderingServerDefault::draw(bool p_present, double frame_step) {419ERR_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().");420// Needs to be done before changes is reset to 0, to not force the editor to redraw.421RS::get_singleton()->emit_signal(SNAME("frame_pre_draw"));422changes = 0;423if (create_thread) {424command_queue.push(this, &RenderingServerDefault::_draw, p_present, frame_step);425} else {426_draw(p_present, frame_step);427}428}429430void RenderingServerDefault::tick() {431RSG::canvas->tick();432RSG::scene->tick();433}434435void RenderingServerDefault::pre_draw(bool p_will_draw) {436RSG::scene->pre_draw(p_will_draw);437}438439void RenderingServerDefault::_call_on_render_thread(const Callable &p_callable) {440p_callable.call();441}442443RenderingServerDefault::RenderingServerDefault(bool p_create_thread) {444RenderingServer::init();445446create_thread = p_create_thread;447}448449RenderingServerDefault::~RenderingServerDefault() {450}451452453