From b70983a0e4c17f4be05d25449c5ddca04fb901a5 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Tue, 17 Sep 2024 20:50:09 -0400 Subject: [PATCH 1/5] Build arguments for Tracy --- .vscode/settings.json | 5 +++++ project.hxp | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 227cb94ec..af5986fbf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -96,6 +96,11 @@ "target": "windows", "args": ["-debug", "-DFEATURE_DEBUG_FUNCTIONS"] }, + { + "label": "Windows / Debug (Tracy)", + "target": "windows", + "args": ["-debug", "-DFEATURE_DEBUG_TRACY", "-DFEATURE_DEBUG_FUNCTIONS"] + }, { "label": "Linux / Debug", "target": "linux", diff --git a/project.hxp b/project.hxp index 1193a9cd4..934cc1d2d 100644 --- a/project.hxp +++ b/project.hxp @@ -149,6 +149,14 @@ class Project extends HXProject { */ static final FEATURE_DEBUG_FUNCTIONS:FeatureFlag = "FEATURE_DEBUG_FUNCTIONS"; + /** + * `-DFEATURE_DEBUG_TRACY` + * If this flag is enabled, the game will have the necessary hooks for the Tracy profiler. + * Only enable this if you're using the correct fork of Haxe to support this. + * @see https://github.com/HaxeFoundation/hxcpp/pull/1153 + */ + static final FEATURE_DEBUG_TRACY:FeatureFlag = "FEATURE_DEBUG_TRACY"; + /** * `-DFEATURE_DISCORD_RPC` * If this flag is enabled, the game will enable the Discord Remote Procedure Call library. @@ -516,6 +524,16 @@ class Project extends HXProject { // Cleaner looking compiler errors. setHaxedef("message.reporting", "pretty"); + + if (FEATURE_DEBUG_TRACY.isEnabled(this)) { + setHaxedef("HXCPP_TELEMETRY"); // Enable telemetry + setHaxedef("HXCPP_TRACY"); // Enable Tracy telemetry + setHaxedef("HXCPP_TRACY_MEMORY"); // Track memory allocations + setHaxedef("HXCPP_TRACY_ON_DEMAND"); // Only collect telemetry when Tracy is open and reachable + // setHaxedef("HXCPP_TRACY_INCLUDE_CALLSTACKS"); // Inspect callstacks per zone, inflating telemetry data + + setHaxedef("absolute-paths"); // Fix source locations so Tracy can see them + } } /** From 61d23b9985d629ef40b2630898aed006d5edfb41 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Tue, 17 Sep 2024 20:56:54 -0400 Subject: [PATCH 2/5] Use branch of HXCPP --- hmm.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hmm.json b/hmm.json index d967a69b3..4c473ed37 100644 --- a/hmm.json +++ b/hmm.json @@ -105,8 +105,8 @@ "name": "hxcpp", "type": "git", "dir": null, - "ref": "904ea40643b050a5a154c5e4c33a83fd2aec18b1", - "url": "https://github.com/HaxeFoundation/hxcpp" + "ref": "c6bac3d6c7d683f25104296b2f4c50f8c90b8349", + "url": "https://github.com/cortex-engine/hxcpp" }, { "name": "hxcpp-debug-server", From 119148acf0b25de8af2f030c56d99cdfefcb4934 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Wed, 18 Sep 2024 02:11:31 -0400 Subject: [PATCH 3/5] Define for enabling/disabling log trace --- project.hxp | 8 ++++++++ source/funkin/util/logging/AnsiTrace.hx | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/project.hxp b/project.hxp index 934cc1d2d..73a12538a 100644 --- a/project.hxp +++ b/project.hxp @@ -157,6 +157,13 @@ class Project extends HXProject { */ static final FEATURE_DEBUG_TRACY:FeatureFlag = "FEATURE_DEBUG_TRACY"; + /** + * `-DFEATURE_LOG_TRACE` + * If this flag is enabled, the game will print debug traces to the console. + * Disable to improve performance a bunch. + */ + static final FEATURE_LOG_TRACE:FeatureFlag = "FEATURE_LOG_TRACE"; + /** * `-DFEATURE_DISCORD_RPC` * If this flag is enabled, the game will enable the Discord Remote Procedure Call library. @@ -461,6 +468,7 @@ class Project extends HXProject { // Should be true on debug builds or if GITHUB_BUILD is enabled. FEATURE_DEBUG_FUNCTIONS.apply(this, isDebug() || GITHUB_BUILD.isEnabled(this)); + FEATURE_LOG_TRACE.apply(this, isDebug()); // Should default to true on workspace builds and false on release builds. REDIRECT_ASSETS_FOLDER.apply(this, isDebug() && isDesktop()); diff --git a/source/funkin/util/logging/AnsiTrace.hx b/source/funkin/util/logging/AnsiTrace.hx index 322a66820..18c6d1d92 100644 --- a/source/funkin/util/logging/AnsiTrace.hx +++ b/source/funkin/util/logging/AnsiTrace.hx @@ -6,7 +6,7 @@ class AnsiTrace // but adds nice cute ANSI things public static function trace(v:Dynamic, ?info:haxe.PosInfos) { - #if TREMOVE + #if NO_FEATURE_LOG_TRACE return; #end var str = formatOutput(v, info); From f7fb640e52ff8820579ae90d2fef6b001f54e6a3 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Wed, 18 Sep 2024 02:29:11 -0400 Subject: [PATCH 4/5] Copy TracyProfiler from pull request --- source/cpp/vm/tracy/TracyProfiler.hx | 102 +++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 source/cpp/vm/tracy/TracyProfiler.hx diff --git a/source/cpp/vm/tracy/TracyProfiler.hx b/source/cpp/vm/tracy/TracyProfiler.hx new file mode 100644 index 000000000..1bedd521d --- /dev/null +++ b/source/cpp/vm/tracy/TracyProfiler.hx @@ -0,0 +1,102 @@ +/* + * Pulled from Tracey profiler PR + * @see https://github.com/HaxeFoundation/haxe/pull/11772 + */ + +package cpp.vm.tracy; + +#if (!HXCPP_TRACY) +#error "This class cannot be used without -D HXCPP_TRACY" +#end +enum abstract PlotFormatType(cpp.UInt8) from cpp.UInt8 to cpp.UInt8 +{ + var Number = 0; + var Memory = 1; + var Percentage = 2; +} + +@:include('hx/TelemetryTracy.h') +extern class Native_TracyProfiler +{ + /** + Mark a frame. Call this at the end of each frame loop. + **/ + @:native('::__hxcpp_tracy_framemark') + public static function frameMark():Void; + + /** + Print a message into Tracy's log. + **/ + @:native('::__hxcpp_tracy_message') + public static function message(_msg:String, ?_color:Int = 0x000000):Void; + + /** + Tracy can collect additional information about the profiled application, + which will be available in the trace description. + This can include data such as the source repository revision, + the application’s environment (dev/prod), etc. + **/ + @:native('::__hxcpp_tracy_message_app_info') + public static function messageAppInfo(_info:String):Void; + + /** + Plot a named value to tracy. This will generate a graph in the profiler for you. + **/ + @:native('::__hxcpp_tracy_plot') + public static function plot(_name:String, _val:cpp.Float32):Void; + + /** + Configure how values are plotted and displayed. + **/ + @:native('::__hxcpp_tracy_plot_config') + public static function plotConfig(_name:String, _format:PlotFormatType, ?_step:Bool = false, ?_fill:Bool = false, ?_color:Int = 0x000000):Void; + + /** + Set a name for the current thread this function is called in. Supply an optional groupHint so threads become grouped in Tracy's UI. + **/ + @:native('::__hxcpp_tracy_set_thread_name_and_group') + public static function setThreadName(_name:String, ?_groupHint:Int = 1):Void; + + /** + Create a custom named scoped zone in your code. + **/ + @:native('HXCPP_TRACY_ZONE') + public static function zoneScoped(_name:String):Void; +} + +#if (scriptable || cppia) +class Cppia_TracyProfiler +{ + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.frameMark) + public static function frameMark() + Native_TracyProfiler.frameMark(); + + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.message) + public static function message(_msg:String, ?_color:Int = 0x000000) + Native_TracyProfiler.message(_msg, _color); + + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.messageAppInfo) + public static function messageAppInfo(_info:String) + Native_TracyProfiler.messageAppInfo(_info); + + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.plot) + public static function plot(_name:String, _val:Float) + Native_TracyProfiler.plot(_name, _val); + + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.plotConfig) + public static function plotConfig(_name:String, _format:PlotFormatType, ?_step:Bool = false, ?_fill:Bool = false, ?_color:Int = 0x000000) + Native_TracyProfiler.plotConfig(_name, _format, _step, _fill, _color); + + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.setThreadName) + public static function setThreadName(_name:String, ?_groupHint:Int = 1) + Native_TracyProfiler.setThreadName(_name, _groupHint); + + @:inheritDoc(cpp.vm.tracy.Native_TracyProfiler.zoneScoped) + public static function zoneScoped(_name:String) + Native_TracyProfiler.zoneScoped(_name); +} + +typedef TracyProfiler = Cppia_TracyProfiler; +#else +typedef TracyProfiler = Native_TracyProfiler; +#end From 2a5269c2a755068d0b1ad94de7030d7988671942 Mon Sep 17 00:00:00 2001 From: EliteMasterEric Date: Wed, 18 Sep 2024 02:30:48 -0400 Subject: [PATCH 5/5] Disable on demand telemetry --- project.hxp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.hxp b/project.hxp index 73a12538a..74402016f 100644 --- a/project.hxp +++ b/project.hxp @@ -537,7 +537,7 @@ class Project extends HXProject { setHaxedef("HXCPP_TELEMETRY"); // Enable telemetry setHaxedef("HXCPP_TRACY"); // Enable Tracy telemetry setHaxedef("HXCPP_TRACY_MEMORY"); // Track memory allocations - setHaxedef("HXCPP_TRACY_ON_DEMAND"); // Only collect telemetry when Tracy is open and reachable + // setHaxedef("HXCPP_TRACY_ON_DEMAND"); // Only collect telemetry when Tracy is open and reachable // setHaxedef("HXCPP_TRACY_INCLUDE_CALLSTACKS"); // Inspect callstacks per zone, inflating telemetry data setHaxedef("absolute-paths"); // Fix source locations so Tracy can see them