Skip to content

Commit

Permalink
Add Godot Tracy module
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaCatania authored and aaronfranke committed Jun 2, 2024
1 parent 2a85682 commit 5b3e204
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "modules/jolt/thirdparty/mimalloc"]
path = modules/jolt/thirdparty/mimalloc
url = https://github.com/microsoft/mimalloc.git
[submodule "modules/godot_tracy"]
path = modules/godot_tracy
url = https://github.com/AndreaCatania/godot_tracy.git
18 changes: 18 additions & 0 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
#include "core/templates/local_vector.h"
#include "core/variant/typed_array.h"

#include "modules/modules_enabled.gen.h"

#ifdef MODULE_GODOT_TRACY_ENABLED
#include "modules/godot_tracy/profiler.h"
#endif // MODULE_GODOT_TRACY_ENABLED

#ifdef DEBUG_ENABLED

struct _ObjectDebugLock {
Expand Down Expand Up @@ -784,6 +790,12 @@ Variant Object::callv(const StringName &p_method, const Array &p_args) {
}

Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
#ifdef MODULE_GODOT_TRACY_ENABLED
ZoneScoped;
CharString c = Profiler::stringify_method(p_method, p_args, p_argcount);
ZoneName(c.ptr(), c.size());
#endif // MODULE_GODOT_TRACY_ENABLED

r_error.error = Callable::CallError::CALL_OK;

if (p_method == CoreStringName(free_)) {
Expand Down Expand Up @@ -847,6 +859,12 @@ Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_
}

Variant Object::call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
#ifdef MODULE_GODOT_TRACY_ENABLED
ZoneScoped;
CharString c = Profiler::stringify_method(p_method, p_args, p_argcount);
ZoneName(c.ptr(), c.size());
#endif // MODULE_GODOT_TRACY_ENABLED

r_error.error = Callable::CallError::CALL_OK;

if (p_method == CoreStringName(free_)) {
Expand Down
12 changes: 12 additions & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
#include "core/templates/local_vector.h"
#include "core/templates/oa_hash_map.h"

#include "modules/modules_enabled.gen.h"

#ifdef MODULE_GODOT_TRACY_ENABLED
#include "modules/godot_tracy/profiler.h"
#endif // MODULE_GODOT_TRACY_ENABLED

typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args);
typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args);

Expand Down Expand Up @@ -1194,6 +1200,12 @@ static void register_builtin_method(const Vector<String> &p_argnames, const Vect
}

void Variant::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) {
#ifdef MODULE_GODOT_TRACY_ENABLED
ZoneScoped;
CharString c = Profiler::stringify_method(p_method, p_args, p_argcount);
ZoneName(c.ptr(), c.size());
#endif // MODULE_GODOT_TRACY_ENABLED

if (type == Variant::OBJECT) {
//call object
Object *obj = _get_obj().obj;
Expand Down
148 changes: 97 additions & 51 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
#endif // DISABLE_DEPRECATED
#endif // TOOLS_ENABLED

#include "modules/modules_enabled.gen.h" // For mono.
#include "modules/modules_enabled.gen.h"

#if defined(MODULE_MONO_ENABLED) && defined(TOOLS_ENABLED)
#include "modules/mono/editor/bindings_generator.h"
Expand All @@ -131,6 +131,14 @@
#include "modules/app_protocol/app_protocol.h"
#endif // MODULE_APP_PROTOCOL_ENABLED

#ifdef MODULE_GODOT_TRACY_ENABLED
#include "modules/godot_tracy/profiler.h"
#else
// Dummy defines to allow compiling without tracy.
#define ZoneScoped
#define ZoneScopedN(a)
#endif // MODULE_GODOT_TRACY_ENABLED

/* Static members */

// Singletons
Expand Down Expand Up @@ -4000,6 +4008,7 @@ static uint64_t navigation_process_max = 0;
// will terminate the program. In case of failure, the OS exit code needs
// to be set explicitly here (defaults to EXIT_SUCCESS).
bool Main::iteration() {
ZoneScoped;
iterating++;

const uint64_t ticks = OS::get_singleton()->get_ticks_usec();
Expand Down Expand Up @@ -4045,62 +4054,84 @@ bool Main::iteration() {
NavigationServer2D::get_singleton()->sync();
NavigationServer3D::get_singleton()->sync();

for (int iters = 0; iters < advance.physics_steps; ++iters) {
if (Input::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
Input::get_singleton()->flush_buffered_events();
}
{
ZoneScopedN("PHYSICS LOOP");
for (int iters = 0; iters < advance.physics_steps; ++iters) {
if (Input::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
Input::get_singleton()->flush_buffered_events();
}

Engine::get_singleton()->_in_physics = true;
Engine::get_singleton()->_in_physics = true;

uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();
uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();

#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->sync();
PhysicsServer3D::get_singleton()->flush_queries();
{
ZoneScopedN("Godot physics 3D - sync");
PhysicsServer3D::get_singleton()->sync();
PhysicsServer3D::get_singleton()->flush_queries();
}
#endif // _3D_DISABLED

// Prepare the fixed timestep interpolated nodes BEFORE they are updated
// by the physics server, otherwise the current and previous transforms
// may be the same, and no interpolation takes place.
OS::get_singleton()->get_main_loop()->iteration_prepare();
// Prepare the fixed timestep interpolated nodes BEFORE they are updated
// by the physics server, otherwise the current and previous transforms
// may be the same, and no interpolation takes place.
OS::get_singleton()->get_main_loop()->iteration_prepare();

{
ZoneScopedN("Godot physics 2D - sync");
PhysicsServer2D::get_singleton()->sync();
PhysicsServer2D::get_singleton()->flush_queries();
}

PhysicsServer2D::get_singleton()->sync();
PhysicsServer2D::get_singleton()->flush_queries();
{
ZoneScopedN("physics_process");

if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) {
if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) {
#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->end_sync();
PhysicsServer3D::get_singleton()->end_sync();
#endif // _3D_DISABLED
PhysicsServer2D::get_singleton()->end_sync();
PhysicsServer2D::get_singleton()->end_sync();

exit = true;
break;
}
exit = true;
break;
}
}

uint64_t navigation_begin = OS::get_singleton()->get_ticks_usec();
uint64_t navigation_begin = OS::get_singleton()->get_ticks_usec();

NavigationServer3D::get_singleton()->process(physics_step * time_scale);
{
ZoneScopedN("Navigation process");
NavigationServer3D::get_singleton()->process(physics_step * time_scale);
}

navigation_process_ticks = MAX(navigation_process_ticks, OS::get_singleton()->get_ticks_usec() - navigation_begin); // keep the largest one for reference
navigation_process_max = MAX(OS::get_singleton()->get_ticks_usec() - navigation_begin, navigation_process_max);
navigation_process_ticks = MAX(navigation_process_ticks, OS::get_singleton()->get_ticks_usec() - navigation_begin); // keep the largest one for reference
navigation_process_max = MAX(OS::get_singleton()->get_ticks_usec() - navigation_begin, navigation_process_max);

message_queue->flush();
message_queue->flush();

#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->end_sync();
PhysicsServer3D::get_singleton()->step(physics_step * time_scale);
{
ZoneScopedN("Godot physics 3D - step");
PhysicsServer3D::get_singleton()->end_sync();
PhysicsServer3D::get_singleton()->step(physics_step * time_scale);
}
#endif // _3D_DISABLED

PhysicsServer2D::get_singleton()->end_sync();
PhysicsServer2D::get_singleton()->step(physics_step * time_scale);
{
ZoneScopedN("Godot physics 2D - step");
PhysicsServer2D::get_singleton()->end_sync();
PhysicsServer2D::get_singleton()->step(physics_step * time_scale);
}

message_queue->flush();
message_queue->flush();

physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
Engine::get_singleton()->_physics_frames++;
physics_process_ticks = MAX(physics_process_ticks, OS::get_singleton()->get_ticks_usec() - physics_begin); // keep the largest one for reference
physics_process_max = MAX(OS::get_singleton()->get_ticks_usec() - physics_begin, physics_process_max);
Engine::get_singleton()->_physics_frames++;

Engine::get_singleton()->_in_physics = false;
Engine::get_singleton()->_in_physics = false;
}
}

if (Input::get_singleton()->is_using_input_buffering() && agile_input_event_flushing) {
Expand All @@ -4109,36 +4140,48 @@ bool Main::iteration() {

uint64_t process_begin = OS::get_singleton()->get_ticks_usec();

if (OS::get_singleton()->get_main_loop()->process(process_step * time_scale)) {
exit = true;
{
ZoneScopedN("Main process");
if (OS::get_singleton()->get_main_loop()->process(process_step * time_scale)) {
exit = true;
}
}
message_queue->flush();

RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames.

if (DisplayServer::get_singleton()->can_any_window_draw() &&
RenderingServer::get_singleton()->is_render_loop_enabled()) {
if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
if (RenderingServer::get_singleton()->has_changed()) {
{
ZoneScopedN("Main::iteration - Draw");
RenderingServer::get_singleton()->sync(); //sync if still drawing from previous frames.

if (DisplayServer::get_singleton()->can_any_window_draw() &&
RenderingServer::get_singleton()->is_render_loop_enabled()) {
if ((!force_redraw_requested) && OS::get_singleton()->is_in_low_processor_usage_mode()) {
if (RenderingServer::get_singleton()->has_changed()) {
RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
Engine::get_singleton()->increment_frames_drawn();
}
} else {
RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
Engine::get_singleton()->increment_frames_drawn();
force_redraw_requested = false;
}
} else {
RenderingServer::get_singleton()->draw(true, scaled_step); // flush visual commands
Engine::get_singleton()->increment_frames_drawn();
force_redraw_requested = false;
}
}

process_ticks = OS::get_singleton()->get_ticks_usec() - process_begin;
process_max = MAX(process_ticks, process_max);
uint64_t frame_time = OS::get_singleton()->get_ticks_usec() - ticks;

for (int i = 0; i < ScriptServer::get_language_count(); i++) {
ScriptServer::get_language(i)->frame();
{
ZoneScopedN("Languages update");
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
ScriptServer::get_language(i)->frame();
}
}

AudioServer::get_singleton()->update();
{
ZoneScopedN("Audio update");
AudioServer::get_singleton()->update();
}

if (EngineDebugger::is_active()) {
EngineDebugger::get_singleton()->iteration(frame_time, process_ticks, physics_process_ticks, physics_step);
Expand Down Expand Up @@ -4198,7 +4241,10 @@ bool Main::iteration() {
return exit;
}

OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw());
{
ZoneScopedN("Frame delay");
OS::get_singleton()->add_frame_delay(DisplayServer::get_singleton()->window_can_draw());
}

#ifdef TOOLS_ENABLED
if (auto_build_solutions) {
Expand Down
12 changes: 12 additions & 0 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@

#include <stdint.h>

#include "modules/modules_enabled.gen.h"

#ifdef MODULE_GODOT_TRACY_ENABLED
#include "modules/godot_tracy/profiler.h"
#endif // MODULE_GODOT_TRACY_ENABLED

///////////////////////////

GDScriptNativeClass::GDScriptNativeClass(const StringName &p_name) {
Expand Down Expand Up @@ -2034,6 +2040,12 @@ void GDScriptInstance::_call_implicit_ready_recursively(GDScript *p_script) {
}

Variant GDScriptInstance::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
#ifdef MODULE_GODOT_TRACY_ENABLED
ZoneScoped;
CharString c = Profiler::stringify_method(p_method, p_args, p_argcount);
ZoneName(c.ptr(), c.size());
#endif // MODULE_GODOT_TRACY_ENABLED

if (unlikely(!script->valid)) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
return Variant();
Expand Down
1 change: 1 addition & 0 deletions modules/godot_tracy
Submodule godot_tracy added at cfa757
17 changes: 17 additions & 0 deletions modules/jolt/jolt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
#include "scene/resources/physics_material.h"
#include "state_recorder_sync.h"

#include "modules/modules_enabled.gen.h"

#ifdef MODULE_GODOT_TRACY_ENABLED
#include "modules/godot_tracy/profiler.h"
#else
// Dummy defines to allow compiling without tracy.
#define ZoneScoped
#define ZoneScopedN(a)
#endif // MODULE_GODOT_TRACY_ENABLED

Jolt *Jolt::the_singleton = nullptr;

Jolt *Jolt::singleton() {
Expand Down Expand Up @@ -239,6 +249,7 @@ void Jolt::world_unregister_sensor(JBody3D *p_sensor) {
}

void Jolt::world_process(uint32_t p_world_id, double p_delta) {
ZoneScoped;
JPH::PhysicsSystem *physics_system = worlds[p_world_id];

if (worlds_need_broadphase_opt[p_world_id]) {
Expand Down Expand Up @@ -304,12 +315,14 @@ bool Jolt::world_is_processing(uint32_t p_world_id) const {
}

void Jolt::world_get_state(uint32_t p_world_id, class StateRecorderSync &p_state, JPH::EStateRecorderState p_recorder_state, const JPH::StateRecorderFilter *p_filter) {
ZoneScoped;
JPH::PhysicsSystem *physics_system = worlds[p_world_id];

CRASH_COND_MSG(p_state.IsFailed(), "The state shound not be failed at this point.");

p_state.Clear();
{
ZoneScopedN("PhysicsSystem::SaveState");
physics_system->SaveState(p_state, p_recorder_state, p_filter);
}

Expand Down Expand Up @@ -386,6 +399,7 @@ JPH::Body *Jolt::create_body(
real_t p_max_angular_velocity_deg,
real_t p_gravity_scale,
bool p_use_ccd) {
ZoneScoped;
JPH::RVec3 origin = convert_r(p_body_initial_transform.get_origin());
JPH::Quat rot = convert(p_body_initial_transform.get_basis().get_rotation_quaternion());
JPH::Vec3 scale = convert(p_body_initial_transform.get_basis().get_scale_abs());
Expand Down Expand Up @@ -1059,6 +1073,7 @@ void Jolt::collide_sphere_broad_phase(
JPH::Ref<JPH::CharacterVirtual> Jolt::create_character_virtual(
const JPH::RMat44 &p_transform,
JPH::CharacterVirtualSettings *p_settings) {
ZoneScoped;
JPH::PhysicsSystem *physics_system = worlds[0];

return JPH::Ref<JPH::CharacterVirtual>(
Expand All @@ -1075,6 +1090,7 @@ void Jolt::character_virtual_pre_step(
JPH::CharacterVirtual *p_character,
JPH::ObjectLayer p_layer_id,
const LocalVector<uint32_t> &p_ignore_bodies) {
ZoneScoped;
const JPH::DefaultBroadPhaseLayerFilter broad_phase_layer_filter(object_vs_broad_phase_layer_filter, p_layer_id);
TableObjectLayerFilter object_layer_filter;
object_layer_filter.layer = p_layer_id;
Expand All @@ -1096,6 +1112,7 @@ void Jolt::character_virtual_step(
JPH::CharacterVirtual::ExtendedUpdateSettings &p_update_settings,
JPH::ObjectLayer p_layer_id,
const LocalVector<uint32_t> &p_ignore_bodies) {
ZoneScoped;
JPH::PhysicsSystem *physics_system = worlds[0];

const JPH::DefaultBroadPhaseLayerFilter broad_phase_layer_filter(object_vs_broad_phase_layer_filter, p_layer_id);
Expand Down
Loading

0 comments on commit 5b3e204

Please sign in to comment.