Path: blob/master/servers/rendering/rendering_server_default.cpp
20898 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 "core/profiling/profiling.h"34#include "renderer_canvas_cull.h"35#include "renderer_scene_cull.h"36#include "rendering_server_globals.h"3738// careful, these may run in different threads than the rendering server3940int RenderingServerDefault::changes = 0;4142/* FREE */4344void RenderingServerDefault::_free(RID p_rid) {45if (unlikely(p_rid.is_null())) {46return;47}48if (RSG::utilities->free(p_rid)) {49return;50}51if (RSG::canvas->free(p_rid)) {52return;53}54if (RSG::viewport->free(p_rid)) {55return;56}57if (RSG::scene->free(p_rid)) {58return;59}60}6162/* EVENT QUEUING */6364void RenderingServerDefault::request_frame_drawn_callback(const Callable &p_callable) {65frame_drawn_callbacks.push_back(p_callable);66}6768void RenderingServerDefault::_draw(bool p_swap_buffers, double frame_step) {69GodotProfileZoneGroupedFirst(_profile_zone, "rasterizer->begin_frame");70RSG::rasterizer->begin_frame(frame_step);7172TIMESTAMP_BEGIN()7374uint64_t time_usec = OS::get_singleton()->get_ticks_usec();7576RENDER_TIMESTAMP("Prepare Render Frame");7778#ifndef XR_DISABLED79GodotProfileZoneGrouped(_profile_zone, "xr_server->pre_render");80XRServer *xr_server = XRServer::get_singleton();81if (xr_server != nullptr) {82// Let XR server know we're about to render a frame.83xr_server->pre_render();84}85#endif // XR_DISABLED8687GodotProfileZoneGrouped(_profile_zone, "scene->update");88RSG::scene->update(); //update scenes stuff before updating instances89GodotProfileZoneGrouped(_profile_zone, "canvas->update");90RSG::canvas->update();9192frame_setup_time = double(OS::get_singleton()->get_ticks_usec() - time_usec) / 1000.0;9394GodotProfileZoneGrouped(_profile_zone, "particles_storage->update_particles");95RSG::particles_storage->update_particles(); //need to be done after instances are updated (colliders and particle transforms), and colliders are rendered9697GodotProfileZoneGrouped(_profile_zone, "scene->render_probes");98RSG::scene->render_probes();99100GodotProfileZoneGrouped(_profile_zone, "viewport->draw_viewports");101RSG::viewport->draw_viewports(p_swap_buffers);102103GodotProfileZoneGrouped(_profile_zone, "canvas_render->update");104RSG::canvas_render->update();105106GodotProfileZoneGrouped(_profile_zone, "rasterizer->end_frame");107RSG::rasterizer->end_frame(p_swap_buffers);108109#ifndef XR_DISABLED110if (xr_server != nullptr) {111GodotProfileZone("xr_server->end_frame");112// let our XR server know we're done so we can get our frame timing113xr_server->end_frame();114}115#endif // XR_DISABLED116117GodotProfileZoneGrouped(_profile_zone, "update_visibility_notifiers");118RSG::canvas->update_visibility_notifiers();119RSG::scene->update_visibility_notifiers();120121GodotProfileZoneGrouped(_profile_zone, "post_draw_steps");122if (create_thread) {123callable_mp(this, &RenderingServerDefault::_run_post_draw_steps).call_deferred();124} else {125_run_post_draw_steps();126}127128if (RSG::utilities->get_captured_timestamps_count()) {129GodotProfileZoneGrouped(_profile_zone, "frame_profile");130Vector<FrameProfileArea> new_profile;131if (RSG::utilities->capturing_timestamps) {132new_profile.resize(RSG::utilities->get_captured_timestamps_count());133}134135uint64_t base_cpu = RSG::utilities->get_captured_timestamp_cpu_time(0);136uint64_t base_gpu = RSG::utilities->get_captured_timestamp_gpu_time(0);137for (uint32_t i = 0; i < RSG::utilities->get_captured_timestamps_count(); i++) {138uint64_t time_cpu = RSG::utilities->get_captured_timestamp_cpu_time(i);139uint64_t time_gpu = RSG::utilities->get_captured_timestamp_gpu_time(i);140141String name = RSG::utilities->get_captured_timestamp_name(i);142143if (name.begins_with("vp_")) {144RSG::viewport->handle_timestamp(name, time_cpu, time_gpu);145}146147if (RSG::utilities->capturing_timestamps) {148new_profile.write[i].gpu_msec = double((time_gpu - base_gpu) / 1000) / 1000.0;149new_profile.write[i].cpu_msec = double(time_cpu - base_cpu) / 1000.0;150new_profile.write[i].name = RSG::utilities->get_captured_timestamp_name(i);151}152}153154frame_profile = new_profile;155}156157frame_profile_frame = RSG::utilities->get_captured_timestamps_frame();158159if (print_gpu_profile) {160GodotProfileZoneGrouped(_profile_zone, "gpu_profile");161if (print_frame_profile_ticks_from == 0) {162print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();163}164double total_time = 0.0;165166for (int i = 0; i < frame_profile.size() - 1; i++) {167String name = frame_profile[i].name;168if (name[0] == '<' || name[0] == '>') {169continue;170}171172double time = frame_profile[i + 1].gpu_msec - frame_profile[i].gpu_msec;173174if (print_gpu_profile_task_time.has(name)) {175print_gpu_profile_task_time[name] += time;176} else {177print_gpu_profile_task_time[name] = time;178}179}180181if (frame_profile.size()) {182total_time = frame_profile[frame_profile.size() - 1].gpu_msec;183}184185uint64_t ticks_elapsed = OS::get_singleton()->get_ticks_usec() - print_frame_profile_ticks_from;186print_frame_profile_frame_count++;187if (ticks_elapsed > 1000000) {188print_line("GPU PROFILE (total " + rtos(total_time) + "ms): ");189190float print_threshold = 0.01;191for (const KeyValue<String, float> &E : print_gpu_profile_task_time) {192double time = E.value / double(print_frame_profile_frame_count);193if (time > print_threshold) {194print_line("\t-" + E.key + ": " + rtos(time) + "ms");195}196}197print_gpu_profile_task_time.clear();198print_frame_profile_ticks_from = OS::get_singleton()->get_ticks_usec();199print_frame_profile_frame_count = 0;200}201}202203GodotProfileZoneGrouped(_profile_zone, "memory_info");204RSG::utilities->update_memory_info();205}206207void RenderingServerDefault::_run_post_draw_steps() {208while (frame_drawn_callbacks.front()) {209Callable c = frame_drawn_callbacks.front()->get();210Variant result;211Callable::CallError ce;212c.callp(nullptr, 0, result, ce);213if (ce.error != Callable::CallError::CALL_OK) {214String err = Variant::get_callable_error_text(c, nullptr, 0, ce);215ERR_PRINT("Error calling frame drawn function: " + err);216}217218frame_drawn_callbacks.pop_front();219}220221emit_signal(SNAME("frame_post_draw"));222}223224double RenderingServerDefault::get_frame_setup_time_cpu() const {225return frame_setup_time;226}227228bool RenderingServerDefault::has_changed() const {229return changes > 0;230}231232void RenderingServerDefault::_init() {233RSG::threaded = create_thread;234235RSG::canvas = memnew(RendererCanvasCull);236RSG::viewport = memnew(RendererViewport);237RendererSceneCull *sr = memnew(RendererSceneCull);238RSG::camera_attributes = memnew(RendererCameraAttributes);239RSG::scene = sr;240RSG::rasterizer = RendererCompositor::create();241RSG::utilities = RSG::rasterizer->get_utilities();242RSG::rasterizer->initialize();243RSG::light_storage = RSG::rasterizer->get_light_storage();244RSG::material_storage = RSG::rasterizer->get_material_storage();245RSG::mesh_storage = RSG::rasterizer->get_mesh_storage();246RSG::particles_storage = RSG::rasterizer->get_particles_storage();247RSG::texture_storage = RSG::rasterizer->get_texture_storage();248RSG::gi = RSG::rasterizer->get_gi();249RSG::fog = RSG::rasterizer->get_fog();250RSG::canvas_render = RSG::rasterizer->get_canvas();251sr->set_scene_render(RSG::rasterizer->get_scene());252}253254void RenderingServerDefault::_finish() {255if (test_cube.is_valid()) {256free_rid(test_cube);257}258259RSG::canvas->finalize();260memdelete(RSG::canvas);261RSG::rasterizer->finalize();262memdelete(RSG::viewport);263memdelete(RSG::rasterizer);264memdelete(RSG::scene);265memdelete(RSG::camera_attributes);266}267268void RenderingServerDefault::init() {269if (create_thread) {270print_verbose("RenderingServerWrapMT: Starting render thread");271DisplayServer::get_singleton()->release_rendering_thread();272WorkerThreadPool::TaskID tid = WorkerThreadPool::get_singleton()->add_task(callable_mp(this, &RenderingServerDefault::_thread_loop), true, "Rendering Server pump task", true);273command_queue.set_pump_task_id(tid);274command_queue.push(this, &RenderingServerDefault::_assign_mt_ids, tid);275command_queue.push_and_sync(this, &RenderingServerDefault::_init);276DEV_ASSERT(server_task_id == tid);277} else {278server_thread = Thread::MAIN_ID;279_init();280}281}282283void RenderingServerDefault::finish() {284if (create_thread) {285command_queue.push(this, &RenderingServerDefault::_finish);286command_queue.push(this, &RenderingServerDefault::_thread_exit);287if (server_task_id != WorkerThreadPool::INVALID_TASK_ID) {288WorkerThreadPool::get_singleton()->wait_for_task_completion(server_task_id);289server_task_id = WorkerThreadPool::INVALID_TASK_ID;290}291server_thread = Thread::MAIN_ID;292} else {293_finish();294}295}296297/* STATUS INFORMATION */298299uint64_t RenderingServerDefault::get_rendering_info(RenderingInfo p_info) {300if (p_info == RENDERING_INFO_TOTAL_OBJECTS_IN_FRAME) {301return RSG::viewport->get_total_objects_drawn();302} else if (p_info == RENDERING_INFO_TOTAL_PRIMITIVES_IN_FRAME) {303return RSG::viewport->get_total_primitives_drawn();304} else if (p_info == RENDERING_INFO_TOTAL_DRAW_CALLS_IN_FRAME) {305return RSG::viewport->get_total_draw_calls_used();306} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_CANVAS) {307return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_CANVAS);308} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_MESH) {309return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_MESH) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_MESH);310} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_SURFACE) {311return RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_SURFACE);312} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW) {313return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_DRAW) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_DRAW);314} else if (p_info == RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION) {315return RSG::canvas_render->get_pipeline_compilations(PIPELINE_SOURCE_SPECIALIZATION) + RSG::scene->get_pipeline_compilations(PIPELINE_SOURCE_SPECIALIZATION);316}317return RSG::utilities->get_rendering_info(p_info);318}319320RenderingDevice::DeviceType RenderingServerDefault::get_video_adapter_type() const {321return RSG::utilities->get_video_adapter_type();322}323324void RenderingServerDefault::set_frame_profiling_enabled(bool p_enable) {325RSG::utilities->capturing_timestamps = p_enable;326}327328uint64_t RenderingServerDefault::get_frame_profile_frame() {329return frame_profile_frame;330}331332Vector<RenderingServer::FrameProfileArea> RenderingServerDefault::get_frame_profile() {333return frame_profile;334}335336/* TESTING */337338Color RenderingServerDefault::get_default_clear_color() {339return RSG::texture_storage->get_default_clear_color();340}341342void RenderingServerDefault::set_default_clear_color(const Color &p_color) {343RSG::texture_storage->set_default_clear_color(p_color);344}345346#ifndef DISABLE_DEPRECATED347bool RenderingServerDefault::has_feature(Features p_feature) const {348return false;349}350#endif351352void RenderingServerDefault::sdfgi_set_debug_probe_select(const Vector3 &p_position, const Vector3 &p_dir) {353RSG::scene->sdfgi_set_debug_probe_select(p_position, p_dir);354}355356void RenderingServerDefault::set_print_gpu_profile(bool p_enable) {357RSG::utilities->capturing_timestamps = p_enable;358print_gpu_profile = p_enable;359}360361RID RenderingServerDefault::get_test_cube() {362if (!test_cube.is_valid()) {363test_cube = _make_test_cube();364}365return test_cube;366}367368bool RenderingServerDefault::has_os_feature(const String &p_feature) const {369if (RSG::utilities) {370return RSG::utilities->has_os_feature(p_feature);371} else {372return false;373}374}375376void RenderingServerDefault::set_debug_generate_wireframes(bool p_generate) {377RSG::utilities->set_debug_generate_wireframes(p_generate);378}379380bool RenderingServerDefault::is_low_end() const {381return RendererCompositor::is_low_end();382}383384Size2i RenderingServerDefault::get_maximum_viewport_size() const {385if (RSG::utilities) {386return RSG::utilities->get_maximum_viewport_size();387} else {388return Size2i();389}390}391392void RenderingServerDefault::_assign_mt_ids(WorkerThreadPool::TaskID p_pump_task_id) {393server_thread = Thread::get_caller_id();394server_task_id = p_pump_task_id;395396RenderingDevice *rd = RenderingDevice::get_singleton();397if (rd) {398// This is needed because the main RD is created on the main thread.399rd->make_current();400}401}402403void RenderingServerDefault::_thread_exit() {404exit = true;405}406407void RenderingServerDefault::_thread_loop() {408DisplayServer::get_singleton()->gl_window_make_current(DisplayServer::MAIN_WINDOW_ID); // Move GL to this thread.409410while (!exit) {411WorkerThreadPool::get_singleton()->yield();412command_queue.flush_all();413}414415DisplayServer::get_singleton()->release_rendering_thread();416}417418/* INTERPOLATION */419420void RenderingServerDefault::set_physics_interpolation_enabled(bool p_enabled) {421RSG::canvas->set_physics_interpolation_enabled(p_enabled);422RSG::scene->set_physics_interpolation_enabled(p_enabled);423}424425/* EVENT QUEUING */426427void RenderingServerDefault::sync() {428if (create_thread) {429command_queue.sync();430} else {431command_queue.flush_all(); // Flush all pending from other threads.432}433}434435void RenderingServerDefault::draw(bool p_present, double frame_step) {436ERR_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().");437// Needs to be done before changes is reset to 0, to not force the editor to redraw.438RS::get_singleton()->emit_signal(SNAME("frame_pre_draw"));439changes = 0;440if (create_thread) {441command_queue.push(this, &RenderingServerDefault::_draw, p_present, frame_step);442} else {443_draw(p_present, frame_step);444}445}446447void RenderingServerDefault::tick() {448RSG::canvas->tick();449RSG::scene->tick();450}451452void RenderingServerDefault::pre_draw(bool p_will_draw) {453RSG::scene->pre_draw(p_will_draw);454}455456void RenderingServerDefault::_call_on_render_thread(const Callable &p_callable) {457p_callable.call();458}459460RenderingServerDefault::RenderingServerDefault(bool p_create_thread) {461RenderingServer::init();462463create_thread = p_create_thread;464}465466RenderingServerDefault::~RenderingServerDefault() {467}468469470