Skip to content

Commit

Permalink
Allow profiler to use bundled sketches (#769)
Browse files Browse the repository at this point in the history
* Util to read current git branch

* Allow loading of bundled sketches with "tiltbrush://builtin/" prefix

* Tweak location and content of profiling summary

* Fix speling

* Include build string in profile summary. Add a 40fps bucket
  • Loading branch information
andybak authored Oct 7, 2024
1 parent 3ebd1e0 commit c8c3182
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ jobs:
env:
PRERELEASE: ${{ steps.version.outputs.prerelease }}
run: |
# For a PR action (i.e., syncronize / open), the value of github.ref will be refs/pull/1234/merge
# For a PR action (i.e., synchronize / open), the value of github.ref will be refs/pull/1234/merge
# For a push action, it will be either refs/heads/foo_branch_name OR refs/tags/v1234.
# We want to use the base name for pushes of tags or to main, the PR number for PRs, and the branch name for named branches.
if [[ "$PRERELEASE" == "false" || ${{ github.ref }} == refs/heads/main ]]
Expand Down
25 changes: 21 additions & 4 deletions Assets/Scripts/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public partial class App : MonoBehaviour

private const int kHttpListenerPort = 40074;
private const string kProtocolHandlerPrefix = "tiltbrush://remix/";
private const string kBuiltInSketchPrefix = "tiltbrush://builtin/";
private const string kFileMoveFilename = "WhereHaveMyFilesGone.txt";

private const string kFileMoveContents =
Expand Down Expand Up @@ -472,7 +473,7 @@ void DestroyIntroSketch()
Resources.UnloadUnusedAssets();
}

static string GetStartupString()
public static string GetStartupString()
{
string str = $"{App.kAppDisplayName} {Config.m_VersionNumber}";

Expand Down Expand Up @@ -638,11 +639,18 @@ void Start()

foreach (string s in Config.m_SketchFiles)
{
// Assume all relative paths are relative to the Sketches directory.
string sketch = s;
if (!System.IO.Path.IsPathRooted(sketch))
if (s.StartsWith(kBuiltInSketchPrefix))
{
sketch = System.IO.Path.Combine(App.UserSketchPath(), sketch);
sketch = s;
}
else
{
// Assume all relative paths are relative to the Sketches directory.
if (!System.IO.Path.IsPathRooted(sketch))
{
sketch = System.IO.Path.Combine(App.UserSketchPath(), sketch);
}
}
m_RequestedTiltFileQueue.Enqueue(sketch);
if (Config.m_SdkMode == SdkMode.Ods || Config.OfflineRender)
Expand Down Expand Up @@ -1475,6 +1483,15 @@ private bool HandleExternalTiltOpenRequest()
return HandlePolyRequest(path);
}

if (path.StartsWith(kBuiltInSketchPrefix))
{
path = path.Substring(kBuiltInSketchPrefix.Length);
path = Path.Join(FeaturedSketchesPath(), path);
SketchControlsScript.m_Instance.IssueGlobalCommand(
SketchControlsScript.GlobalCommands.LoadNamedFile, sParam: path);
return true;
}

// Copy to sketch folder in order to discourage the user from explicitly saving
// to gallery for future access, which would (by design) strip attribution.
// Crypto hash suffix is added to the filename for (deterministic) uniqueness.
Expand Down
16 changes: 14 additions & 2 deletions Assets/Scripts/Debug/ProfilingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private struct Sample
private List<float> m_FrameTimes;
private const int k_NumFrames = 75 * 6; // enough space for six seconds of samples.
private Coroutine m_UpdateCoroutine;
private int[] m_ValidFramerates = { 90, 75, 60, 1 };
private int[] m_ValidFramerates = { 90, 75, 60, 40, 1 };
private List<Sample> m_Samples = new List<Sample>();

public static ProfilingManager Instance
Expand Down Expand Up @@ -156,7 +156,7 @@ private string GetProfilingFilename()
string dateTime = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
filename = string.Format("Profile_{0}.data", dateTime);
}
return Path.Combine(Application.persistentDataPath, filename);
return filename;
}

// Grabs the frame times and any functions being profiled.
Expand Down Expand Up @@ -212,6 +212,13 @@ private void OutputStats()
? SaveLoadScript.m_Instance.GetLastFileHumanName()
: App.UserConfig.Profiling.ProfileName;
message.AppendLine("TBProfile: START");
#if UNITY_EDITOR
string branch = GitUtils.GetGitBranchName();
message.AppendLine($"Git branch: {branch}");
#endif
message.AppendLine($"Build: {App.GetStartupString()}");
message.AppendLine($"Profile name: {profileName} Filename: {fileName} Human name: {humanName}");

if (App.UserConfig.Profiling.PerfgateOutput)
{
PerfgateOutput(message, m_FrameTimes.ToArray(), numBatches, numTriangles, file);
Expand All @@ -230,6 +237,11 @@ private void OutputStats()
message.AppendLine("TBProfile: END");

Debug.Log(message.ToString());

string path = Path.Join(
App.UserPath(),
$"{GetProfilingFilename()}_summary.txt");
File.WriteAllText(path, message.ToString());
}

private void HumanReadableOutput(StringBuilder output, Statistics.Summary stats, int numBatches,
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/SketchControlsScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5105,7 +5105,7 @@ private void ToggleProfiling()
}
else
{
ProfilingManager.Instance.StartProfiling(App.UserConfig.Profiling.ProflingMode);
ProfilingManager.Instance.StartProfiling(App.UserConfig.Profiling.ProfilingMode);
}
}

Expand Down Expand Up @@ -5142,7 +5142,7 @@ private IEnumerator DoProfiling(bool andQuit = false)
InputManager.Wand.Geometry.transform.rotation = Camera.main.transform.rotation;
m_PanelManager.LockPanelsToController();

ProfilingManager.Instance.StartProfiling(App.UserConfig.Profiling.ProflingMode);
ProfilingManager.Instance.StartProfiling(App.UserConfig.Profiling.ProfilingMode);
yield return new WaitForSeconds(App.UserConfig.Profiling.Duration);
ProfilingManager.Instance.StopProfiling();

Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/UserConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,15 +601,15 @@ public struct ProfilingConfig
{
public const int kDefaultScreenshotResolution = 1000;
public string[] ProfilingFunctions { get; private set; }
public ProfilingManager.Mode ProflingMode { get; private set; }
public ProfilingManager.Mode ProfilingMode { get; private set; }

public string Mode
{
set
{
try
{
ProflingMode = (ProfilingManager.Mode)Enum.Parse(typeof(ProfilingManager.Mode), value);
ProfilingMode = (ProfilingManager.Mode)Enum.Parse(typeof(ProfilingManager.Mode), value);
}
catch (ArgumentException)
{
Expand Down
24 changes: 24 additions & 0 deletions Assets/Scripts/Util/GitUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Diagnostics;

namespace TiltBrush
{
public static class GitUtils
{
public static string GetGitBranchName()
{
ProcessStartInfo startInfo = new ProcessStartInfo("git", "rev-parse --abbrev-ref HEAD")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};

using (Process process = Process.Start(startInfo))
{
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd().Trim();
return output;
}
}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Util/GitUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c8c3182

Please sign in to comment.