Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0f2713fdc | ||
|
|
6ad5aa3f2c | ||
|
|
d9582aecdd | ||
|
|
7853a768de | ||
|
|
83f77d1ebe | ||
|
|
2d4139fb2c | ||
|
|
5eaf3547dc | ||
|
|
d80e1177b9 | ||
|
|
cf6cd080c6 | ||
|
|
87ee6df46f | ||
|
|
57b42d8daa | ||
|
|
2a351f7b9d | ||
|
|
7225d13880 | ||
|
|
567714a52d | ||
|
|
2c84e650d6 | ||
|
|
82fe47f627 | ||
|
|
6be63195f0 | ||
|
|
9387bfa59c | ||
|
|
86b54e1521 | ||
|
|
ba88432e77 | ||
|
|
5cc876fce9 | ||
|
|
91b4f5fafb | ||
|
|
0d14a32726 | ||
|
|
b3adef3a40 | ||
|
|
30deeca452 |
@@ -16,3 +16,6 @@
|
|||||||
[submodule "Nerfed.Runtime/Libraries/ImGui.NET"]
|
[submodule "Nerfed.Runtime/Libraries/ImGui.NET"]
|
||||||
path = Nerfed.Runtime/Libraries/ImGui.NET
|
path = Nerfed.Runtime/Libraries/ImGui.NET
|
||||||
url = https://github.com/ImGuiNET/ImGui.NET.git
|
url = https://github.com/ImGuiNET/ImGui.NET.git
|
||||||
|
[submodule "Nerfed.Runtime/Libraries/MoonTools.ECS"]
|
||||||
|
path = Nerfed.Runtime/Libraries/MoonTools.ECS
|
||||||
|
url = https://github.com/MoonsideGames/MoonTools.ECS.git
|
||||||
|
|||||||
Generated
+1
@@ -4,6 +4,7 @@
|
|||||||
<mapping directory="" vcs="Git" />
|
<mapping directory="" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/FAudio" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/FAudio" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/ImGui.NET" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/ImGui.NET" vcs="Git" />
|
||||||
|
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/MoonTools.ECS" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/RefreshCS" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/RefreshCS" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/SDL2CS" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/SDL2CS" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/WellspringCS" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$/Nerfed.Runtime/Libraries/WellspringCS" vcs="Git" />
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace Nerfed.Compiler;
|
|
||||||
|
|
||||||
public class AssemblyDefinition
|
|
||||||
{
|
|
||||||
public string Name { get; set; }
|
|
||||||
public string Guid { get; set; }
|
|
||||||
//public bool IsEditor { get; set; }
|
|
||||||
// Add platform stuff here..?
|
|
||||||
// Add dll's here..?
|
|
||||||
// Add dependencies here..?
|
|
||||||
|
|
||||||
public static bool Create(string assemblyDefinitionFilePath, string name, out AssemblyDefinition assemblyDefinition)
|
|
||||||
{
|
|
||||||
assemblyDefinition = null;
|
|
||||||
|
|
||||||
if (File.Exists(assemblyDefinitionFilePath))
|
|
||||||
{
|
|
||||||
Console.WriteLine($"ERROR: File already exists!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create project file.
|
|
||||||
assemblyDefinition = new AssemblyDefinition
|
|
||||||
{
|
|
||||||
Name = name,
|
|
||||||
Guid = System.Guid.NewGuid().ToString("B").ToUpper(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Save(assemblyDefinition, assemblyDefinitionFilePath);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool Save(AssemblyDefinition assemblyDefinition, string assemblyDefinitionFilePath)
|
|
||||||
{
|
|
||||||
string jsonString = JsonSerializer.Serialize(assemblyDefinition, AssemblyDefinitionContext.Default.AssemblyDefinition);
|
|
||||||
|
|
||||||
File.WriteAllText(assemblyDefinitionFilePath, jsonString);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool Open(string assemblyDefinitionFilePath, out AssemblyDefinition assemblyDefinition)
|
|
||||||
{
|
|
||||||
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
|
|
||||||
assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
|
|
||||||
|
|
||||||
if (assemblyDefinition == null)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"ERROR: Could not open {typeof(AssemblyDefinition)}.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonSerializable(typeof(AssemblyDefinition))]
|
|
||||||
public partial class AssemblyDefinitionContext : JsonSerializerContext
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Nerfed.Compiler;
|
|
||||||
|
|
||||||
public static class Compiler
|
|
||||||
{
|
|
||||||
public static bool Compile(string projectFilePath, string configuration = "Debug")
|
|
||||||
{
|
|
||||||
string projectDirectory = Path.GetDirectoryName(projectFilePath);
|
|
||||||
|
|
||||||
if (!File.Exists(projectFilePath))
|
|
||||||
{
|
|
||||||
Console.WriteLine($"ERROR: Project file not found at {projectDirectory}.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Project.Open(projectFilePath, out Project project))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Check project version, to make sure we can compile it or something...
|
|
||||||
|
|
||||||
// Generate solution.
|
|
||||||
Generator.GenerateSolution(projectDirectory, project, out string solutionFilePath);
|
|
||||||
|
|
||||||
// Compile solution.
|
|
||||||
ProcessStartInfo processInfo = new()
|
|
||||||
{
|
|
||||||
WorkingDirectory = Path.GetDirectoryName(solutionFilePath),
|
|
||||||
CreateNoWindow = true,
|
|
||||||
UseShellExecute = false,
|
|
||||||
RedirectStandardError = true,
|
|
||||||
RedirectStandardOutput = true,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
||||||
{
|
|
||||||
processInfo.FileName = "/bin/bash";
|
|
||||||
processInfo.Arguments = $"-c \"dotnet build '{Path.GetFileName(solutionFilePath)}'\"" + (string.IsNullOrWhiteSpace(configuration) ? $" --configuration {configuration}" : "");
|
|
||||||
}
|
|
||||||
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
||||||
{
|
|
||||||
processInfo.FileName = "cmd.exe";
|
|
||||||
processInfo.Arguments = $"/c dotnet build \"{Path.GetFileName(solutionFilePath)}\"" + (string.IsNullOrWhiteSpace(configuration) ? $" --configuration {configuration}" : "");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine($"ERROR: Platform not supported!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Process process = Process.Start(processInfo) ?? throw new Exception();
|
|
||||||
process.OutputDataReceived += (sender, dataArgs) => {
|
|
||||||
string data = dataArgs.Data;
|
|
||||||
|
|
||||||
if (data is null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
process.BeginOutputReadLine();
|
|
||||||
process.BeginErrorReadLine();
|
|
||||||
process.ErrorDataReceived += (sender, dataArgs) => {
|
|
||||||
if (dataArgs.Data is not null)
|
|
||||||
{
|
|
||||||
Console.WriteLine(dataArgs.Data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
process.WaitForExit();
|
|
||||||
|
|
||||||
int exitCode = process.ExitCode;
|
|
||||||
process.Close();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,161 +0,0 @@
|
|||||||
using System.Reflection;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
|
|
||||||
namespace Nerfed.Compiler;
|
|
||||||
|
|
||||||
public static class Generator
|
|
||||||
{
|
|
||||||
public const string AssemblyDefinitionExtensionName = ".asmdef";
|
|
||||||
public const string CSProjectExtensionName = ".csproj";
|
|
||||||
public const string SolutionExtensionName = ".sln";
|
|
||||||
|
|
||||||
public static void GenerateSolution(string projectDirectory, Project project, out string solutionFilePath)
|
|
||||||
{
|
|
||||||
// Clear files.
|
|
||||||
ClearCSProjectFiles(projectDirectory);
|
|
||||||
|
|
||||||
// Generate projects.
|
|
||||||
string[] assemblyDefinitionFilePaths = Directory.GetFiles(projectDirectory, AssemblyDefinitionExtensionName, SearchOption.AllDirectories);
|
|
||||||
foreach (string assemblyDefinitionFilePath in assemblyDefinitionFilePaths)
|
|
||||||
{
|
|
||||||
GenerateCSProject(assemblyDefinitionFilePath, projectDirectory, out string csProjectFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate solution.
|
|
||||||
string[] csProjectPaths = Directory.GetFiles(projectDirectory, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly);
|
|
||||||
string[] csProjectGuids = new string[csProjectPaths.Length];
|
|
||||||
for (int i = 0; i < csProjectPaths.Length; i++)
|
|
||||||
{
|
|
||||||
csProjectGuids[i] = Guid.NewGuid().ToString("B").ToUpper();
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuilder content = new StringBuilder();
|
|
||||||
|
|
||||||
// Write the solution file header
|
|
||||||
content.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
|
|
||||||
content.AppendLine("# Visual Studio Version 17");
|
|
||||||
content.AppendLine("VisualStudioVersion = 17.10.35013.160");
|
|
||||||
content.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1");
|
|
||||||
|
|
||||||
// Add each project to the solution file
|
|
||||||
for (int i = 0; i < csProjectPaths.Length; i++)
|
|
||||||
{
|
|
||||||
string csProjectPath = csProjectPaths[i];
|
|
||||||
string csProjectGuid = csProjectGuids[i];
|
|
||||||
string csProjectName = Path.GetFileNameWithoutExtension(csProjectPath);
|
|
||||||
string csProjectRelativePath = Path.GetRelativePath(projectDirectory, csProjectPath);
|
|
||||||
// FAE04EC0-301F-11D3-BF4B-00C04F79EFBC for C# projects.
|
|
||||||
content.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{csProjectName}\", \"{csProjectRelativePath}\", \"{csProjectGuid}\"");
|
|
||||||
content.AppendLine("EndProject");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add global sections (these can be extended as needed)
|
|
||||||
content.AppendLine("Global");
|
|
||||||
content.AppendLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution");
|
|
||||||
content.AppendLine(" Test|x64 = Test|x64");
|
|
||||||
content.AppendLine(" Release|x64 = Release|x64");
|
|
||||||
content.AppendLine(" Debug|x64 = Debug|x64");
|
|
||||||
content.AppendLine(" EndGlobalSection");
|
|
||||||
content.AppendLine(" GlobalSection(ProjectConfigurationPlatforms) = postSolution");
|
|
||||||
|
|
||||||
for (int i = 0; i < csProjectPaths.Length; i++)
|
|
||||||
{
|
|
||||||
string projectGuid = csProjectGuids[i];
|
|
||||||
content.AppendLine($" {projectGuid}.Test|x64.ActiveCfg = Test|x64");
|
|
||||||
content.AppendLine($" {projectGuid}.Test|x64.Build.0 = Test|x64");
|
|
||||||
content.AppendLine($" {projectGuid}.Release|x64.ActiveCfg = Release|x64");
|
|
||||||
content.AppendLine($" {projectGuid}.Release|x64.Build.0 = Release|x64");
|
|
||||||
content.AppendLine($" {projectGuid}.Debug|x64.ActiveCfg = Debug|x64");
|
|
||||||
content.AppendLine($" {projectGuid}.Debug|x64.Build.0 = Debug|x64");
|
|
||||||
}
|
|
||||||
|
|
||||||
content.AppendLine(" EndGlobalSection");
|
|
||||||
content.AppendLine(" GlobalSection(SolutionProperties) = preSolution");
|
|
||||||
content.AppendLine(" HideSolutionNode = FALSE");
|
|
||||||
content.AppendLine(" EndGlobalSection");
|
|
||||||
content.AppendLine("EndGlobal");
|
|
||||||
|
|
||||||
// Write the solution file content to disk
|
|
||||||
string solutionName = project.Name + SolutionExtensionName;
|
|
||||||
solutionFilePath = Path.Combine(projectDirectory, solutionName);
|
|
||||||
File.WriteAllText(solutionFilePath, content.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool GenerateCSProject(string assemblyDefinitionFilePath, string projectPath, out string csProjectFilePath)
|
|
||||||
{
|
|
||||||
if (!File.Exists(assemblyDefinitionFilePath))
|
|
||||||
{
|
|
||||||
csProjectFilePath = string.Empty;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string jsonString = File.ReadAllText(assemblyDefinitionFilePath);
|
|
||||||
AssemblyDefinition assemblyDefinition = JsonSerializer.Deserialize(jsonString, AssemblyDefinitionContext.Default.AssemblyDefinition);
|
|
||||||
|
|
||||||
Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
|
||||||
Assembly runtimeAssembly = loadedAssemblies.FirstOrDefault(assembly => assembly.GetName().Name == "Nerfed.Runtime") ?? throw new Exception("Failed to find Runtime Assembly!");
|
|
||||||
|
|
||||||
// TODO: get all dependencies.
|
|
||||||
// TODO: properly get assemblies.
|
|
||||||
|
|
||||||
StringBuilder content = new StringBuilder();
|
|
||||||
|
|
||||||
content.AppendLine("<Project Sdk=\"Microsoft.NET.Sdk\">");
|
|
||||||
|
|
||||||
content.AppendLine(" <PropertyGroup>");
|
|
||||||
content.AppendLine(" <TargetFramework>net8.0</TargetFramework>");
|
|
||||||
content.AppendLine(" <ImplicitUsings>enable</ImplicitUsings>");
|
|
||||||
content.AppendLine(" <Nullable>disable</Nullable>");
|
|
||||||
content.AppendLine(" <PublishAot>true</PublishAot>");
|
|
||||||
content.AppendLine(" <InvariantGlobalization>true</InvariantGlobalization>");
|
|
||||||
content.AppendLine(" <AllowUnsafeBlocks>true</AllowUnsafeBlocks>");
|
|
||||||
content.AppendLine(" <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>");
|
|
||||||
content.AppendLine(" <IsPackable>false</IsPackable>");
|
|
||||||
content.AppendLine(" <Configurations>Debug;Test;Release</Configurations>");
|
|
||||||
content.AppendLine(" <Platforms>x64</Platforms>");
|
|
||||||
content.AppendLine(" </PropertyGroup>");
|
|
||||||
|
|
||||||
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Debug|x64' \">");
|
|
||||||
content.AppendLine(" <DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>");
|
|
||||||
content.AppendLine(" </PropertyGroup>");
|
|
||||||
|
|
||||||
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Test|x64' \">");
|
|
||||||
content.AppendLine(" <DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>");
|
|
||||||
content.AppendLine(" <Optimize>true</Optimize>");
|
|
||||||
content.AppendLine(" </PropertyGroup>");
|
|
||||||
|
|
||||||
content.AppendLine(" <PropertyGroup Condition=\" '$(Configuration)|$(Platform)' == 'Release|x64' \">");
|
|
||||||
content.AppendLine(" <DefineConstants>TRACE;LOG_ERROR</DefineConstants>");
|
|
||||||
content.AppendLine(" <Optimize>true</Optimize>");
|
|
||||||
content.AppendLine(" </PropertyGroup>");
|
|
||||||
|
|
||||||
content.AppendLine(" <ItemGroup>");
|
|
||||||
content.AppendLine($" <Compile Include=\"{assemblyDefinitionFilePath}/**/*.cs\"/>");
|
|
||||||
content.AppendLine(" </ItemGroup>");
|
|
||||||
|
|
||||||
content.AppendLine(" <ItemGroup>");
|
|
||||||
content.AppendLine(" <Reference Include=\"Nerfed.Runtime\">");
|
|
||||||
content.AppendLine($" <HintPath>{runtimeAssembly.Location}</HintPath>");
|
|
||||||
content.AppendLine(" <Private>false</Private>");
|
|
||||||
content.AppendLine(" </Reference>");
|
|
||||||
content.AppendLine(" </ItemGroup>");
|
|
||||||
|
|
||||||
content.AppendLine("</Project>");
|
|
||||||
|
|
||||||
string csProjectName = assemblyDefinition.Name + CSProjectExtensionName;
|
|
||||||
csProjectFilePath = Path.Combine(projectPath, csProjectName);
|
|
||||||
File.WriteAllText(csProjectFilePath, content.ToString());
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void ClearCSProjectFiles(string projectPath)
|
|
||||||
{
|
|
||||||
string[] csProjectFiles = Directory.GetFiles(projectPath, $"*{CSProjectExtensionName}", SearchOption.TopDirectoryOnly);
|
|
||||||
foreach (string csProjectFile in csProjectFiles)
|
|
||||||
{
|
|
||||||
File.Delete(csProjectFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<OutputType>Exe</OutputType>
|
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
|
||||||
<Nullable>disable</Nullable>
|
|
||||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
<Configurations>Debug;Test;Release</Configurations>
|
|
||||||
<Platforms>x64</Platforms>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
|
||||||
<Optimize>false</Optimize>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Test|x64' ">
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
namespace Nerfed.Compiler;
|
|
||||||
|
|
||||||
public class Program
|
|
||||||
{
|
|
||||||
internal static void Main(string[] args)
|
|
||||||
{
|
|
||||||
if (args.Length != 2)
|
|
||||||
{
|
|
||||||
Console.WriteLine("projectFilePath, configuration (Debug, Test, Release)");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Compiler.Compile(args[0], args[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace Nerfed.Compiler;
|
|
||||||
|
|
||||||
public class Project
|
|
||||||
{
|
|
||||||
public string Name { get; set; }
|
|
||||||
|
|
||||||
public static bool Create(string path, string name, out Project project)
|
|
||||||
{
|
|
||||||
// Create project file.
|
|
||||||
project = new Project
|
|
||||||
{
|
|
||||||
Name = name,
|
|
||||||
};
|
|
||||||
|
|
||||||
Save(project, path);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool Save(Project project, string projectFilePath)
|
|
||||||
{
|
|
||||||
string jsonString = JsonSerializer.Serialize(project, ProjectContext.Default.Project);
|
|
||||||
|
|
||||||
File.WriteAllText(projectFilePath, jsonString);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool Open(string path, out Project project)
|
|
||||||
{
|
|
||||||
string jsonString = File.ReadAllText(path);
|
|
||||||
project = JsonSerializer.Deserialize(jsonString, ProjectContext.Default.Project);
|
|
||||||
|
|
||||||
if (project == null)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"ERROR: Could not open {typeof(Project)}.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[JsonSerializable(typeof(Project))]
|
|
||||||
public partial class ProjectContext : JsonSerializerContext
|
|
||||||
{
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Nerfed.Editor.Components;
|
||||||
|
|
||||||
|
public readonly record struct SelectedInHierachy;
|
||||||
|
public readonly record struct ClickedInHierachy;
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using ImGuiNET;
|
using ImGuiNET;
|
||||||
using Nerfed.Editor.Project;
|
|
||||||
using Nerfed.Runtime;
|
using Nerfed.Runtime;
|
||||||
using Nerfed.Runtime.Graphics;
|
using Nerfed.Runtime.Graphics;
|
||||||
using Nerfed.Runtime.Gui;
|
using Nerfed.Runtime.Gui;
|
||||||
@@ -57,7 +56,6 @@ namespace Nerfed.Editor
|
|||||||
}
|
}
|
||||||
ImGui.EndMenu();
|
ImGui.EndMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui.EndMainMenuBar();
|
ImGui.EndMainMenuBar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -69,7 +67,11 @@ namespace Nerfed.Editor
|
|||||||
|
|
||||||
ImGui.ShowDemoWindow();
|
ImGui.ShowDemoWindow();
|
||||||
|
|
||||||
EditorProjectGui.OnGui();
|
foreach (MoonTools.ECS.System system in Program.editorSystems)
|
||||||
|
{
|
||||||
|
using ProfilerScope scope = new(system.GetType().Name);
|
||||||
|
system.Update(Engine.Timestep);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<Configurations>Debug;Test;Release</Configurations>
|
<Configurations>Debug;Test;Release</Configurations>
|
||||||
<Platforms>x64</Platforms>
|
<Platforms>x64</Platforms>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
||||||
@@ -23,7 +24,6 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Nerfed.Compiler\Nerfed.Compiler.csproj" />
|
|
||||||
<ProjectReference Include="..\Nerfed.Runtime\Nerfed.Runtime.csproj" />
|
<ProjectReference Include="..\Nerfed.Runtime\Nerfed.Runtime.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,19 @@
|
|||||||
using Nerfed.Runtime;
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Editor.Systems;
|
||||||
|
using Nerfed.Runtime;
|
||||||
|
using Nerfed.Runtime.Components;
|
||||||
|
using Nerfed.Runtime.Systems;
|
||||||
|
using Nerfed.Runtime.Util;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
namespace Nerfed.Editor;
|
namespace Nerfed.Editor;
|
||||||
|
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
|
private static readonly World world = new World();
|
||||||
|
private static List<MoonTools.ECS.System> systems = new List<MoonTools.ECS.System>();
|
||||||
|
public static List<MoonTools.ECS.System> editorSystems = new List<MoonTools.ECS.System>();
|
||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
Engine.OnInitialize += HandleOnInitialize;
|
Engine.OnInitialize += HandleOnInitialize;
|
||||||
@@ -16,6 +26,45 @@ internal class Program
|
|||||||
|
|
||||||
private static void HandleOnInitialize()
|
private static void HandleOnInitialize()
|
||||||
{
|
{
|
||||||
|
//systems.Add(new ParentSystem(world));
|
||||||
|
systems.Add(new LocalToWorldSystem(world));
|
||||||
|
editorSystems.Add(new EditorProfilerWindow(world));
|
||||||
|
editorSystems.Add(new EditorHierarchyWindow(world));
|
||||||
|
#if DEBUG
|
||||||
|
editorSystems.Add(new EditorInspectorWindow(world));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
Entity ent1 = world.CreateEntity("parent");
|
||||||
|
world.Set(ent1, new Root());
|
||||||
|
world.Set(ent1, new LocalTransform(new Vector3(1, 0, 0), Quaternion.Identity, Vector3.One));
|
||||||
|
|
||||||
|
Entity ent2 = world.CreateEntity("child");
|
||||||
|
world.Set(ent2, new LocalTransform(new Vector3(0, 1, 0), Quaternion.Identity, Vector3.One));
|
||||||
|
Transform.SetParent(world, ent2, ent1);
|
||||||
|
|
||||||
|
Entity ent3 = world.CreateEntity("entity3");
|
||||||
|
world.Set(ent3, new Root());
|
||||||
|
Transform.SetParent(world, ent3, ent2);
|
||||||
|
|
||||||
|
Entity ent4 = world.CreateEntity("entity4");
|
||||||
|
world.Set(ent4, new Root());
|
||||||
|
|
||||||
|
Entity ent5 = world.CreateBaseEntity("entity5");
|
||||||
|
|
||||||
|
for (int i = 0; i < 256; i++)
|
||||||
|
{
|
||||||
|
Entity newEnt = world.CreateBaseEntity();
|
||||||
|
world.Set(newEnt, new LocalTransform(new Vector3(i, i, i), Quaternion.Identity, Vector3.One));
|
||||||
|
|
||||||
|
Entity parent = newEnt;
|
||||||
|
for (int j = 0; j < 2; j++) {
|
||||||
|
Entity newChildEnt = world.CreateEntity();
|
||||||
|
world.Set(newChildEnt, new LocalTransform(new Vector3(i + j * i, i - j * i, j - i * i), Quaternion.Identity, Vector3.One));
|
||||||
|
Transform.SetParent(world, newChildEnt, parent);
|
||||||
|
parent = newChildEnt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Open project.
|
// Open project.
|
||||||
// Setip EditorGui.
|
// Setip EditorGui.
|
||||||
EditorGui.Initialize();
|
EditorGui.Initialize();
|
||||||
@@ -23,16 +72,32 @@ internal class Program
|
|||||||
|
|
||||||
private static void HandleOnUpdate()
|
private static void HandleOnUpdate()
|
||||||
{
|
{
|
||||||
// Editor Update.
|
foreach (MoonTools.ECS.System system in systems)
|
||||||
EditorGui.Update();
|
{
|
||||||
|
using ProfilerScope scope = new(system.GetType().Name);
|
||||||
|
system.Update(Engine.Timestep);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (new ProfilerScope("EditorGui.Update"))
|
||||||
|
{
|
||||||
|
// Editor Update.
|
||||||
|
EditorGui.Update();
|
||||||
|
}
|
||||||
|
|
||||||
// Try Catch UserCode Update.
|
// Try Catch UserCode Update.
|
||||||
|
|
||||||
|
using (new ProfilerScope("world.FinishUpdate"))
|
||||||
|
{
|
||||||
|
world.FinishUpdate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void HandleOnRender()
|
private static void HandleOnRender()
|
||||||
{
|
{
|
||||||
EditorGui.Render();
|
using (new ProfilerScope("EditorGui.Render"))
|
||||||
|
{
|
||||||
|
EditorGui.Render();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void HandleOnQuit()
|
private static void HandleOnQuit()
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
using System.Runtime.Loader;
|
|
||||||
|
|
||||||
namespace Nerfed.Editor.Project;
|
|
||||||
|
|
||||||
internal class EditorAssemblyLoadContext : AssemblyLoadContext
|
|
||||||
{
|
|
||||||
public EditorAssemblyLoadContext() : base(isCollectible: true) { }
|
|
||||||
}
|
|
||||||
@@ -1,124 +0,0 @@
|
|||||||
using Nerfed.Runtime;
|
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Nerfed.Editor.Project;
|
|
||||||
|
|
||||||
// https://github.com/godotengine/godot/blob/master/modules/mono/glue/GodotSharp/GodotPlugins/Main.c
|
|
||||||
// https://gitlab.com/robertk92/assemblyreloadtest/-/blob/main/AppContextTest/Program.cs
|
|
||||||
|
|
||||||
internal static class EditorAssemblyLoader
|
|
||||||
{
|
|
||||||
internal sealed class EditorAssemblyLoadContextWrapper
|
|
||||||
{
|
|
||||||
private EditorAssemblyLoadContext assemblyLoadContext;
|
|
||||||
private readonly WeakReference weakReference;
|
|
||||||
|
|
||||||
private EditorAssemblyLoadContextWrapper(EditorAssemblyLoadContext assemblyLoadContext, WeakReference weakReference)
|
|
||||||
{
|
|
||||||
this.assemblyLoadContext = assemblyLoadContext;
|
|
||||||
this.weakReference = weakReference;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsCollectible
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
||||||
// If assemblyLoadContext is null we already started unloading, so it was collectible.
|
|
||||||
get => assemblyLoadContext?.IsCollectible ?? true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool IsAlive
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
||||||
get => weakReference.IsAlive;
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
||||||
public static (Assembly, EditorAssemblyLoadContextWrapper) CreateAndLoad(AssemblyName assemblyName)
|
|
||||||
{
|
|
||||||
EditorAssemblyLoadContext context = new EditorAssemblyLoadContext();
|
|
||||||
WeakReference reference = new WeakReference(context, trackResurrection: true);
|
|
||||||
EditorAssemblyLoadContextWrapper wrapper = new EditorAssemblyLoadContextWrapper(context, reference);
|
|
||||||
Assembly assembly = context.LoadFromAssemblyName(assemblyName);
|
|
||||||
return (assembly, wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
||||||
public static (Assembly, EditorAssemblyLoadContextWrapper) CreateAndLoad(string assemblyPath)
|
|
||||||
{
|
|
||||||
EditorAssemblyLoadContext context = new EditorAssemblyLoadContext();
|
|
||||||
WeakReference reference = new WeakReference(context, trackResurrection: true);
|
|
||||||
EditorAssemblyLoadContextWrapper wrapper = new EditorAssemblyLoadContextWrapper(context, reference);
|
|
||||||
Assembly assembly = context.LoadFromAssemblyPath(assemblyPath);
|
|
||||||
return (assembly, wrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
||||||
internal void Unload()
|
|
||||||
{
|
|
||||||
assemblyLoadContext?.Unload();
|
|
||||||
assemblyLoadContext = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static (Assembly, EditorAssemblyLoadContextWrapper) Load(string assemblyFilePath)
|
|
||||||
{
|
|
||||||
string assemblyFileName = Path.GetFileNameWithoutExtension(assemblyFilePath);
|
|
||||||
|
|
||||||
AssemblyName assemblyName = new AssemblyName(assemblyFileName);
|
|
||||||
|
|
||||||
return EditorAssemblyLoadContextWrapper.CreateAndLoad(assemblyName);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static (Assembly, EditorAssemblyLoadContextWrapper) LoadFromPath(string assemblyFilePath)
|
|
||||||
{
|
|
||||||
return EditorAssemblyLoadContextWrapper.CreateAndLoad(assemblyFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static bool Unload(EditorAssemblyLoadContextWrapper assemblyLoadContextWrapper)
|
|
||||||
{
|
|
||||||
if (assemblyLoadContextWrapper == null)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!assemblyLoadContextWrapper.IsCollectible)
|
|
||||||
{
|
|
||||||
Log.Error($"{assemblyLoadContextWrapper} is not collectable!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
assemblyLoadContextWrapper.Unload();
|
|
||||||
|
|
||||||
GC.Collect();
|
|
||||||
GC.WaitForPendingFinalizers();
|
|
||||||
|
|
||||||
TimeSpan timeout = TimeSpan.FromSeconds(30);
|
|
||||||
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
||||||
|
|
||||||
while (assemblyLoadContextWrapper.IsAlive)
|
|
||||||
{
|
|
||||||
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
|
|
||||||
GC.WaitForPendingFinalizers();
|
|
||||||
|
|
||||||
if (!assemblyLoadContextWrapper.IsAlive)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stopwatch.Elapsed.TotalSeconds % 10 == 0)
|
|
||||||
{
|
|
||||||
Log.Info("Tring to unload assembly...");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (stopwatch.Elapsed >= timeout)
|
|
||||||
{
|
|
||||||
Log.Error("Failed to unload assembly!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
using Nerfed.Runtime;
|
|
||||||
|
|
||||||
namespace Nerfed.Editor.Project;
|
|
||||||
|
|
||||||
internal static class EditorProject
|
|
||||||
{
|
|
||||||
internal static Compiler.Project Project { get; private set; } = null;
|
|
||||||
internal static string ProjectFilePath { get; private set; } = string.Empty;
|
|
||||||
internal static string ProjectSolutionFilePath { get; private set; } = string.Empty;
|
|
||||||
internal static string ProjectDirectory { get; private set; } = string.Empty;
|
|
||||||
internal static string ProjectContentDirectory { get; private set; } = string.Empty;
|
|
||||||
internal static string ProjectTempDirectory { get; private set; } = string.Empty;
|
|
||||||
|
|
||||||
private static readonly List<(string, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper)> editorAssemblyLoadContextWrappers = [];
|
|
||||||
|
|
||||||
internal static bool Create(string projectFilePath, string projectName)
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
|
|
||||||
if (!Compiler.Project.Create(projectFilePath, projectName, out Compiler.Project project))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Open(projectFilePath);
|
|
||||||
|
|
||||||
Log.Info($"Succesfully created project.");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static bool Open(string projectFilePath)
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
|
|
||||||
if(!Compiler.Project.Open(projectFilePath, out Compiler.Project project))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Project = project;
|
|
||||||
ProjectFilePath = projectFilePath;
|
|
||||||
ProjectDirectory = Path.GetDirectoryName(projectFilePath);
|
|
||||||
|
|
||||||
string projectSolutionFilePath = Path.Combine(ProjectDirectory, Project.Name + Compiler.Generator.SolutionExtensionName);
|
|
||||||
if (File.Exists(projectSolutionFilePath))
|
|
||||||
{
|
|
||||||
ProjectSolutionFilePath = projectSolutionFilePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
SetupDefaultFolders();
|
|
||||||
Compile();
|
|
||||||
|
|
||||||
Log.Info($"Opened project: {project.Name}");
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void Close()
|
|
||||||
{
|
|
||||||
Project = null;
|
|
||||||
ProjectFilePath = string.Empty;
|
|
||||||
ProjectSolutionFilePath = string.Empty;
|
|
||||||
ProjectDirectory = string.Empty;
|
|
||||||
ProjectContentDirectory = string.Empty;
|
|
||||||
ProjectTempDirectory = string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static bool Save()
|
|
||||||
{
|
|
||||||
if(Project == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Compiler.Project.Save(Project, ProjectFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void Compile()
|
|
||||||
{
|
|
||||||
if(Project == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
UnloadAssemblies();
|
|
||||||
|
|
||||||
Compiler.Compiler.Compile(ProjectFilePath, "Debug");
|
|
||||||
|
|
||||||
LoadAssemblies();
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static void GenerateSolution()
|
|
||||||
{
|
|
||||||
if(Project == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Compiler.Generator.GenerateSolution(ProjectDirectory, Project, out string solutionFilePath);
|
|
||||||
ProjectSolutionFilePath = solutionFilePath;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void SetupDefaultFolders()
|
|
||||||
{
|
|
||||||
if (Project == null || ProjectDirectory == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
string contentDirectory = Path.Combine(ProjectDirectory, "Content");
|
|
||||||
if (!Directory.Exists(contentDirectory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(contentDirectory);
|
|
||||||
}
|
|
||||||
ProjectContentDirectory = contentDirectory;
|
|
||||||
string scriptsDirectory = Path.Combine(ProjectContentDirectory, "Scripts");
|
|
||||||
if (!Directory.Exists(scriptsDirectory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(scriptsDirectory);
|
|
||||||
}
|
|
||||||
string scriptsRuntimePath = Path.Combine(scriptsDirectory, "Runtime");
|
|
||||||
if (!Directory.Exists(scriptsRuntimePath))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(scriptsRuntimePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test create csproject.
|
|
||||||
string gameplayRuntimeFilePath = Path.Combine(scriptsRuntimePath, Compiler.Generator.AssemblyDefinitionExtensionName);
|
|
||||||
if (!File.Exists(gameplayRuntimeFilePath))
|
|
||||||
{
|
|
||||||
Compiler.AssemblyDefinition.Create(gameplayRuntimeFilePath, "Gameplay", out Compiler.AssemblyDefinition project);
|
|
||||||
}
|
|
||||||
|
|
||||||
string tempDirectory = Path.Combine(ProjectDirectory, "Temp");
|
|
||||||
if (!Directory.Exists(tempDirectory))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(tempDirectory);
|
|
||||||
}
|
|
||||||
ProjectTempDirectory = tempDirectory;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void LoadAssemblies()
|
|
||||||
{
|
|
||||||
string[] assemblies = Directory.GetFiles(Path.Combine(ProjectDirectory, "bin"), "*.dll", SearchOption.AllDirectories);
|
|
||||||
|
|
||||||
foreach (string assembly in assemblies)
|
|
||||||
{
|
|
||||||
(System.Reflection.Assembly, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper) a = EditorAssemblyLoader.LoadFromPath(assembly);
|
|
||||||
string name = a.Item1.GetName().Name;
|
|
||||||
editorAssemblyLoadContextWrappers.Add((name, a.Item2));
|
|
||||||
Log.Info($"loaded {name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
Nerfed.Runtime.Generator.Hook.InvokeHooks();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void UnloadAssemblies()
|
|
||||||
{
|
|
||||||
for (int i = editorAssemblyLoadContextWrappers.Count - 1; i >= 0; i--)
|
|
||||||
{
|
|
||||||
(string, EditorAssemblyLoader.EditorAssemblyLoadContextWrapper) a = editorAssemblyLoadContextWrappers[i];
|
|
||||||
if (EditorAssemblyLoader.Unload(a.Item2))
|
|
||||||
{
|
|
||||||
Log.Info($"Unloaded {a.Item1}");
|
|
||||||
editorAssemblyLoadContextWrappers.RemoveAt(i);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.Error($"Could not unload {a.Item1}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
using ImGuiNET;
|
|
||||||
|
|
||||||
namespace Nerfed.Editor.Project;
|
|
||||||
|
|
||||||
internal static class EditorProjectGui
|
|
||||||
{
|
|
||||||
private static string projectDirectory = string.Empty;
|
|
||||||
private static string projectName = string.Empty;
|
|
||||||
private static string projectFilePath = string.Empty;
|
|
||||||
|
|
||||||
internal static void OnGui()
|
|
||||||
{
|
|
||||||
ImGui.Begin("Project");
|
|
||||||
ImGui.BeginGroup();
|
|
||||||
|
|
||||||
ImGui.InputText("Project Directory", ref projectDirectory, 512);
|
|
||||||
ImGui.InputText("Project Name", ref projectName, 512);
|
|
||||||
|
|
||||||
string newProjectFilePath = Path.Combine(projectDirectory, ".project");
|
|
||||||
ImGui.Text(newProjectFilePath);
|
|
||||||
|
|
||||||
if (ImGui.Button("Create Project"))
|
|
||||||
{
|
|
||||||
EditorProject.Create(newProjectFilePath, projectName);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.EndGroup();
|
|
||||||
ImGui.BeginGroup();
|
|
||||||
|
|
||||||
ImGui.InputText("Project File Path", ref projectFilePath, 512);
|
|
||||||
if (ImGui.Button("Open Project"))
|
|
||||||
{
|
|
||||||
EditorProject.Open(projectFilePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.Text("Loaded project: ");
|
|
||||||
if(EditorProject.Project != null)
|
|
||||||
{
|
|
||||||
ImGui.Text(EditorProject.Project.Name);
|
|
||||||
ImGui.Text(EditorProject.ProjectFilePath);
|
|
||||||
ImGui.Text(EditorProject.ProjectSolutionFilePath);
|
|
||||||
ImGui.Text(EditorProject.ProjectDirectory);
|
|
||||||
ImGui.Text(EditorProject.ProjectContentDirectory);
|
|
||||||
ImGui.Text(EditorProject.ProjectTempDirectory);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
ImGui.Text("None");
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.EndGroup();
|
|
||||||
ImGui.BeginGroup();
|
|
||||||
|
|
||||||
if (ImGui.Button("Generate Solution"))
|
|
||||||
{
|
|
||||||
EditorProject.GenerateSolution();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ImGui.Button("Compile"))
|
|
||||||
{
|
|
||||||
EditorProject.Compile();
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui.EndGroup();
|
|
||||||
ImGui.End();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
using ImGuiNET;
|
||||||
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Editor.Components;
|
||||||
|
using Nerfed.Runtime;
|
||||||
|
using Nerfed.Runtime.Components;
|
||||||
|
using Nerfed.Runtime.Util;
|
||||||
|
|
||||||
|
namespace Nerfed.Editor.Systems
|
||||||
|
{
|
||||||
|
// Window that draws entities.
|
||||||
|
internal class EditorHierarchyWindow : MoonTools.ECS.System
|
||||||
|
{
|
||||||
|
private const ImGuiTreeNodeFlags baseFlags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.OpenOnDoubleClick | ImGuiTreeNodeFlags.SpanAvailWidth;
|
||||||
|
|
||||||
|
//private readonly Filter rootEntitiesWithTransformFilter;
|
||||||
|
//private readonly Filter rootEntitiesFilterBroken;
|
||||||
|
private readonly Filter rootEntitiesFilter;
|
||||||
|
|
||||||
|
private readonly EditorHierachySelectionSystem hierachySelectionSystem;
|
||||||
|
|
||||||
|
public EditorHierarchyWindow(World world) : base(world)
|
||||||
|
{
|
||||||
|
//rootEntitiesWithTransformFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
||||||
|
|
||||||
|
// TODO: this doesn't work.
|
||||||
|
//rootEntitiesFilterBroken = FilterBuilder.Exclude<Child>().Build();
|
||||||
|
|
||||||
|
// Maybe the parent/child functions should add a root component when not being a child.
|
||||||
|
rootEntitiesFilter = FilterBuilder.Include<Root>().Build();
|
||||||
|
|
||||||
|
// Maybe instead of a root, if we need a component that is always on an entity and has some use we could create something like a VersionComponent which only hold an int.
|
||||||
|
// The version would update each time something changes on the entity.
|
||||||
|
// Or a EditorComponent, just a component that always gets added when in editor mode.
|
||||||
|
|
||||||
|
hierachySelectionSystem = new EditorHierachySelectionSystem(world);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(TimeSpan delta)
|
||||||
|
{
|
||||||
|
ImGui.Begin("Hierarchy");
|
||||||
|
|
||||||
|
ImGuiTreeNodeFlags flags = baseFlags;
|
||||||
|
flags |= ImGuiTreeNodeFlags.DefaultOpen;
|
||||||
|
|
||||||
|
if (ImGui.TreeNodeEx("World", flags))
|
||||||
|
{
|
||||||
|
if (ImGui.BeginDragDropTarget())
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
|
||||||
|
if (payload.NativePtr != null)
|
||||||
|
{
|
||||||
|
Entity* data = (Entity*)payload.Data;
|
||||||
|
Entity child = data[0];
|
||||||
|
|
||||||
|
Log.Info($"Dropped {child.ID}");
|
||||||
|
|
||||||
|
Transform.RemoveParent(World, child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui.EndDragDropTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
//foreach (Entity entity in rootEntitiesWithTransformFilter.Entities)
|
||||||
|
//{
|
||||||
|
// DrawEntityAndChildren(entity);
|
||||||
|
//}
|
||||||
|
|
||||||
|
foreach (Entity entity in rootEntitiesFilter.Entities)
|
||||||
|
{
|
||||||
|
DrawEntityAndChildren(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.TreePop();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
|
||||||
|
hierachySelectionSystem.Update(delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawEntityAndChildren(in Entity entity)
|
||||||
|
{
|
||||||
|
ImGuiTreeNodeFlags flags = baseFlags;
|
||||||
|
|
||||||
|
if (!World.HasInRelation<ChildParentRelation>(entity))
|
||||||
|
{
|
||||||
|
flags |= ImGuiTreeNodeFlags.Leaf;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (World.Has<SelectedInHierachy>(entity))
|
||||||
|
{
|
||||||
|
flags |= ImGuiTreeNodeFlags.Selected;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ImGui.TreeNodeEx($"{entity.ID} | {GetTag(entity)}", flags))
|
||||||
|
{
|
||||||
|
// TODO: fix selection, look at ImGui 1.91, https://github.com/ocornut/imgui/wiki/Multi-Select
|
||||||
|
// Selection.
|
||||||
|
if (ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen())
|
||||||
|
{
|
||||||
|
World.Set(entity, new ClickedInHierachy());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drag and drop.
|
||||||
|
if (ImGui.BeginDragDropSource())
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
fixed (Entity* payload = &entity)
|
||||||
|
{
|
||||||
|
ImGui.SetDragDropPayload($"{nameof(EditorHierarchyWindow)}", (IntPtr)payload, (uint)sizeof(Entity));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndDragDropSource();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ImGui.BeginDragDropTarget())
|
||||||
|
{
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
ImGuiPayloadPtr payload = ImGui.AcceptDragDropPayload($"{nameof(EditorHierarchyWindow)}");
|
||||||
|
if (payload.NativePtr != null)
|
||||||
|
{
|
||||||
|
Entity ent = *(Entity*)payload.Data;
|
||||||
|
|
||||||
|
Log.Info($"Dropped {ent.ID}");
|
||||||
|
|
||||||
|
Transform.SetParent(World, ent, entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui.EndDragDropTarget();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw children.
|
||||||
|
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
||||||
|
foreach (Entity childEntity in childEntities)
|
||||||
|
{
|
||||||
|
DrawEntityAndChildren(childEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.TreePop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// System for handling the selected entities in the hierachy.
|
||||||
|
private class EditorHierachySelectionSystem : MoonTools.ECS.System
|
||||||
|
{
|
||||||
|
private readonly Filter selectedEntities;
|
||||||
|
private readonly Filter clickedEntities;
|
||||||
|
|
||||||
|
public EditorHierachySelectionSystem(World world) : base(world)
|
||||||
|
{
|
||||||
|
selectedEntities = FilterBuilder.Include<SelectedInHierachy>().Build();
|
||||||
|
clickedEntities = FilterBuilder.Include<ClickedInHierachy>().Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(TimeSpan delta)
|
||||||
|
{
|
||||||
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
|
|
||||||
|
if (!clickedEntities.Empty && !io.KeyCtrl)
|
||||||
|
{
|
||||||
|
foreach (Entity entity in selectedEntities.Entities)
|
||||||
|
{
|
||||||
|
Remove<SelectedInHierachy>(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Entity entity in clickedEntities.Entities)
|
||||||
|
{
|
||||||
|
// Unselect.
|
||||||
|
if (Has<SelectedInHierachy>(entity))
|
||||||
|
{
|
||||||
|
Remove<SelectedInHierachy>(entity);
|
||||||
|
}
|
||||||
|
// Select.
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Set(entity, new SelectedInHierachy());
|
||||||
|
}
|
||||||
|
|
||||||
|
Remove<ClickedInHierachy>(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
using ImGuiNET;
|
||||||
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Editor.Components;
|
||||||
|
using Nerfed.Runtime.Serialization;
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
namespace Nerfed.Editor.Systems
|
||||||
|
{
|
||||||
|
// Window that draws entities.
|
||||||
|
internal class EditorInspectorWindow : MoonTools.ECS.DebugSystem
|
||||||
|
{
|
||||||
|
private readonly Filter selectedEntityFilter;
|
||||||
|
|
||||||
|
public EditorInspectorWindow(World world) : base(world)
|
||||||
|
{
|
||||||
|
selectedEntityFilter = FilterBuilder.Include<SelectedInHierachy>().Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(TimeSpan delta)
|
||||||
|
{
|
||||||
|
ImGui.Begin("Inspector");
|
||||||
|
|
||||||
|
foreach (Entity entity in selectedEntityFilter.Entities)
|
||||||
|
{
|
||||||
|
DrawEntityComponents(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawEntityComponents(Entity entity)
|
||||||
|
{
|
||||||
|
World.ComponentTypeEnumerator componentTypes = World.Debug_GetAllComponentTypes(entity);
|
||||||
|
|
||||||
|
// Add button of all types that we can add. Also filter out types we already have.
|
||||||
|
List<Type> componentTypesToAdd = ComponentHelper.AddComponentByType.Keys.ToList();
|
||||||
|
foreach (Type componentType in componentTypes)
|
||||||
|
{
|
||||||
|
componentTypesToAdd.Remove(componentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
const string popupId = "AddComponentPopup";
|
||||||
|
if (ImGui.Button("Add Component"))
|
||||||
|
{
|
||||||
|
ImGui.OpenPopup(popupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ImGui.BeginPopup(popupId))
|
||||||
|
{
|
||||||
|
foreach (Type componentType in componentTypesToAdd)
|
||||||
|
{
|
||||||
|
if (ImGui.Selectable(componentType.Name))
|
||||||
|
{
|
||||||
|
if (ComponentHelper.AddComponentByType.TryGetValue(componentType, out Action<World, Entity> componentSetter))
|
||||||
|
{
|
||||||
|
componentSetter.Invoke(World, entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui.EndPopup();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.Dummy(new Vector2(16, 16));
|
||||||
|
|
||||||
|
ImGui.Text("ComponentInspectorByType");
|
||||||
|
foreach (Type componentType in componentTypes)
|
||||||
|
{
|
||||||
|
if (ComponentHelper.ComponentInspectorByType.TryGetValue(componentType, out Action<World, Entity> componentInspector))
|
||||||
|
{
|
||||||
|
componentInspector(World, entity);
|
||||||
|
}
|
||||||
|
else if (ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter))
|
||||||
|
{
|
||||||
|
ValueType component = componentGetter.Invoke(World, entity);
|
||||||
|
ImGui.Text(component.ToString());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ImGui.Text(componentType.Name);
|
||||||
|
}
|
||||||
|
ImGui.Separator();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.Dummy(new Vector2(16, 16));
|
||||||
|
|
||||||
|
// ImGui.Text("Reflection");
|
||||||
|
// foreach (Type component in componentTypes)
|
||||||
|
// {
|
||||||
|
// System.Reflection.MethodInfo getMethodInfo = typeof(World).GetMethod("Get");
|
||||||
|
// System.Reflection.MethodInfo getComponentMethod = getMethodInfo.MakeGenericMethod(component);
|
||||||
|
// object result = getComponentMethod.Invoke(World, [entity]);
|
||||||
|
//
|
||||||
|
// // process here
|
||||||
|
// ImGui.Text(result.ToString());
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,343 @@
|
|||||||
|
using ImGuiNET;
|
||||||
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Runtime;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Nerfed.Editor.Systems
|
||||||
|
{
|
||||||
|
internal class EditorProfilerWindow : MoonTools.ECS.System
|
||||||
|
{
|
||||||
|
const ImGuiTableFlags tableFlags = ImGuiTableFlags.Resizable | ImGuiTableFlags.BordersOuter | ImGuiTableFlags.NoBordersInBody | ImGuiTableFlags.ScrollY | ImGuiTableFlags.ScrollX;
|
||||||
|
const ImGuiTreeNodeFlags treeNodeFlags = ImGuiTreeNodeFlags.SpanAllColumns;
|
||||||
|
const ImGuiTreeNodeFlags treeNodeLeafFlags = ImGuiTreeNodeFlags.SpanAllColumns | ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen;
|
||||||
|
|
||||||
|
private int selectedFrame = 0;
|
||||||
|
private int previousSelectedFrame = -1;
|
||||||
|
private IOrderedEnumerable<KeyValuePair<string, (double ms, double selfMs, uint calls, double avgMs, double p95Ms, long allocBytes, long selfAllocBytes)>> orderedCombinedData = null;
|
||||||
|
private IOrderedEnumerable<KeyValuePair<int, Profiler.RollingThreadMetrics>> orderedThreadRollingData = null;
|
||||||
|
private readonly ProfilerVisualizer.TimelineState timelineState = new ProfilerVisualizer.TimelineState();
|
||||||
|
private readonly List<Profiler.Frame> frameSnapshot = new List<Profiler.Frame>(256);
|
||||||
|
|
||||||
|
public EditorProfilerWindow(World world) : base(world)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(TimeSpan delta)
|
||||||
|
{
|
||||||
|
if (Profiler.Frames.Count <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Profiler.CopyFramesTo(frameSnapshot) <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedFrame = Math.Clamp(selectedFrame, 0, frameSnapshot.Count - 1);
|
||||||
|
timelineState.SelectedFrameIndex = Math.Clamp(timelineState.SelectedFrameIndex, -1, frameSnapshot.Count - 1);
|
||||||
|
timelineState.VisibleFrameCount = Math.Clamp(timelineState.VisibleFrameCount, 1, frameSnapshot.Count);
|
||||||
|
|
||||||
|
ImGui.Begin("Profiler");
|
||||||
|
|
||||||
|
ImGui.BeginChild("Toolbar", new Vector2(0, 0), ImGuiChildFlags.AutoResizeY);
|
||||||
|
if (ImGui.RadioButton("Recording", Profiler.IsRecording))
|
||||||
|
{
|
||||||
|
Profiler.SetActive(!Profiler.IsRecording);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.SameLine();
|
||||||
|
ImGui.Text("Follow");
|
||||||
|
ImGui.SameLine();
|
||||||
|
ImGui.Checkbox("##follow-timeline", ref timelineState.FollowLatest);
|
||||||
|
ImGui.SameLine();
|
||||||
|
|
||||||
|
int visibleFrameCount = timelineState.VisibleFrameCount;
|
||||||
|
ImGui.SetNextItemWidth(130f);
|
||||||
|
if (ImGui.SliderInt("Window", ref visibleFrameCount, 1, frameSnapshot.Count))
|
||||||
|
{
|
||||||
|
timelineState.VisibleFrameCount = visibleFrameCount;
|
||||||
|
timelineState.FollowLatest = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.SameLine();
|
||||||
|
if (ImGui.Button("Reset Zoom"))
|
||||||
|
{
|
||||||
|
timelineState.Zoom = 1f;
|
||||||
|
timelineState.PanTicks = 0d;
|
||||||
|
timelineState.FollowLatest = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.SameLine();
|
||||||
|
int mode = (int)Profiler.Mode;
|
||||||
|
ImGui.SetNextItemWidth(130f);
|
||||||
|
if (ImGui.Combo("Mode", ref mode, "Instrumented\0Sampled\0"))
|
||||||
|
{
|
||||||
|
Profiler.Mode = (Profiler.CaptureMode)mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.SameLine();
|
||||||
|
int stride = Profiler.SamplingStride;
|
||||||
|
ImGui.SetNextItemWidth(90f);
|
||||||
|
if (ImGui.SliderInt("Stride", ref stride, 1, 64))
|
||||||
|
{
|
||||||
|
Profiler.SamplingStride = stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Profiler.IsRecording)
|
||||||
|
{
|
||||||
|
// Select last frame when recording to see latest frame data.
|
||||||
|
selectedFrame = frameSnapshot.Count - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sliderFrame = selectedFrame;
|
||||||
|
if (ImGui.SliderInt("Frame", ref sliderFrame, 0, frameSnapshot.Count - 1))
|
||||||
|
{
|
||||||
|
selectedFrame = sliderFrame;
|
||||||
|
timelineState.SelectedFrameIndex = selectedFrame;
|
||||||
|
timelineState.FollowLatest = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Profiler.Frame frame = frameSnapshot[selectedFrame];
|
||||||
|
double ms = frame.ElapsedMilliseconds();
|
||||||
|
double s = 1000;
|
||||||
|
ImGui.Text($"Frame: {frame.FrameCount} ({ms:0.000} ms | {(s / ms):0} fps)");
|
||||||
|
ImGui.Text($"Budget: {frame.BudgetMilliseconds:0.00} ms ({(frame.OverBudget ? "over" : "within")})");
|
||||||
|
ImGui.Text($"Thread Budget: {Profiler.ThreadBudgetMilliseconds:0.00} ms | Capture: {Profiler.Mode}");
|
||||||
|
ImGui.Text($"Alloc: {frame.AllocatedBytesDelta / 1024d:0.0} KB | GC: G0 {frame.Gen0CollectionsDelta}, G1 {frame.Gen1CollectionsDelta}, G2 {frame.Gen2CollectionsDelta}");
|
||||||
|
ImGui.EndChild();
|
||||||
|
|
||||||
|
ProfilerVisualizer.TimelineRenderResult timelineResult = DrawFlameGraph(frameSnapshot, timelineState);
|
||||||
|
if (timelineResult.SelectionChanged)
|
||||||
|
{
|
||||||
|
selectedFrame = timelineResult.SelectedFrameIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedFrame = Math.Clamp(selectedFrame, 0, frameSnapshot.Count - 1);
|
||||||
|
frame = frameSnapshot[selectedFrame];
|
||||||
|
|
||||||
|
if (previousSelectedFrame != selectedFrame)
|
||||||
|
{
|
||||||
|
previousSelectedFrame = selectedFrame;
|
||||||
|
orderedCombinedData = CalculateCombinedData(frame);
|
||||||
|
orderedThreadRollingData = CalculateThreadRollingData();
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawThreadRolling(orderedThreadRollingData);
|
||||||
|
|
||||||
|
DrawHierachy(frame);
|
||||||
|
|
||||||
|
ImGui.SameLine();
|
||||||
|
|
||||||
|
DrawCombined(orderedCombinedData);
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawHierachy(Profiler.Frame frame)
|
||||||
|
{
|
||||||
|
if(frame == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.BeginChild("Hierachy", new Vector2(150, 0), ImGuiChildFlags.ResizeX);
|
||||||
|
|
||||||
|
if (ImGui.BeginTable("ProfilerData", 8, tableFlags, new Vector2(0, 0)))
|
||||||
|
{
|
||||||
|
ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthStretch, 0.30f, 0);
|
||||||
|
ImGui.TableSetupColumn("category", ImGuiTableColumnFlags.WidthStretch, 0.12f, 1);
|
||||||
|
ImGui.TableSetupColumn("tags", ImGuiTableColumnFlags.WidthStretch, 0.08f, 2);
|
||||||
|
ImGui.TableSetupColumn("thread", ImGuiTableColumnFlags.WidthStretch, 0.08f, 3);
|
||||||
|
ImGui.TableSetupColumn("ms", ImGuiTableColumnFlags.WidthStretch, 0.10f, 4);
|
||||||
|
ImGui.TableSetupColumn("self", ImGuiTableColumnFlags.WidthStretch, 0.10f, 5);
|
||||||
|
ImGui.TableSetupColumn("alloc(B)", ImGuiTableColumnFlags.WidthStretch, 0.11f, 6);
|
||||||
|
ImGui.TableSetupColumn("self alloc", ImGuiTableColumnFlags.WidthStretch, 0.11f, 7);
|
||||||
|
ImGui.TableSetupScrollFreeze(0, 1); // Make row always visible
|
||||||
|
ImGui.TableHeadersRow();
|
||||||
|
|
||||||
|
foreach (Profiler.ScopeNode node in frame.RootNodes)
|
||||||
|
{
|
||||||
|
DrawHierachyNode(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndChild();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawHierachyNode(Profiler.ScopeNode node)
|
||||||
|
{
|
||||||
|
ImGui.TableNextRow();
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
|
||||||
|
bool isOpen = false;
|
||||||
|
bool isLeaf = node.Children.Count == 0;
|
||||||
|
|
||||||
|
if (isLeaf) {
|
||||||
|
ImGui.TreeNodeEx(node.Label, treeNodeLeafFlags);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isOpen = ImGui.TreeNodeEx(node.Label, treeNodeFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{node.Category}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"0x{node.TagMask:X}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{node.ManagedThreadId}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{node.ElapsedMilliseconds():0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{node.SelfMilliseconds():0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
if (node.ProfilerSetupBytes > 0)
|
||||||
|
{
|
||||||
|
ImGui.Text($"{node.AllocatedBytes} !");
|
||||||
|
if (ImGui.IsItemHovered())
|
||||||
|
ImGui.SetTooltip($"{node.ProfilerSetupBytes} B is profiler warmup overhead");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ImGui.Text($"{node.AllocatedBytes}");
|
||||||
|
}
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{node.SelfAllocatedBytes()}");
|
||||||
|
|
||||||
|
if (isOpen)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
DrawHierachyNode(node.Children[i]);
|
||||||
|
}
|
||||||
|
ImGui.TreePop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawCombined(in IOrderedEnumerable<KeyValuePair<string, (double ms, double selfMs, uint calls, double avgMs, double p95Ms, long allocBytes, long selfAllocBytes)>> orderedCombinedData)
|
||||||
|
{
|
||||||
|
if(orderedCombinedData == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.BeginChild("Combined", new Vector2(0, 0));
|
||||||
|
|
||||||
|
if (ImGui.BeginTable("ProfilerCombinedData", 8, tableFlags, new Vector2(0, 0)))
|
||||||
|
{
|
||||||
|
ImGui.TableSetupColumn("name", ImGuiTableColumnFlags.WidthStretch, 0.32f, 0);
|
||||||
|
ImGui.TableSetupColumn("ms", ImGuiTableColumnFlags.WidthStretch, 0.11f, 1);
|
||||||
|
ImGui.TableSetupColumn("self", ImGuiTableColumnFlags.WidthStretch, 0.11f, 2);
|
||||||
|
ImGui.TableSetupColumn("calls", ImGuiTableColumnFlags.WidthStretch, 0.08f, 3);
|
||||||
|
ImGui.TableSetupColumn("avg", ImGuiTableColumnFlags.WidthStretch, 0.09f, 4);
|
||||||
|
ImGui.TableSetupColumn("p95", ImGuiTableColumnFlags.WidthStretch, 0.09f, 5);
|
||||||
|
ImGui.TableSetupColumn("alloc(B)", ImGuiTableColumnFlags.WidthStretch, 0.10f, 6);
|
||||||
|
ImGui.TableSetupColumn("self alloc", ImGuiTableColumnFlags.WidthStretch, 0.10f, 7);
|
||||||
|
ImGui.TableSetupScrollFreeze(0, 1); // Make row always visible
|
||||||
|
ImGui.TableHeadersRow();
|
||||||
|
|
||||||
|
foreach (KeyValuePair<string, (double ms, double selfMs, uint calls, double avgMs, double p95Ms, long allocBytes, long selfAllocBytes)> combinedData in orderedCombinedData)
|
||||||
|
{
|
||||||
|
ImGui.TableNextRow();
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Key}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.ms:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.selfMs:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.calls}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.avgMs:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.p95Ms:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.allocBytes}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{combinedData.Value.selfAllocBytes}");
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndChild();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IOrderedEnumerable<KeyValuePair<string, (double ms, double selfMs, uint calls, double avgMs, double p95Ms, long allocBytes, long selfAllocBytes)>> CalculateCombinedData(Profiler.Frame frame)
|
||||||
|
{
|
||||||
|
IReadOnlyDictionary<string, Profiler.RollingLabelMetrics> rollingData = Profiler.GetRollingLabelMetricsSnapshot();
|
||||||
|
Dictionary<string, (double ms, double selfMs, uint calls, double avgMs, double p95Ms, long allocBytes, long selfAllocBytes)> combinedRecordData = new Dictionary<string, (double ms, double selfMs, uint calls, double avgMs, double p95Ms, long allocBytes, long selfAllocBytes)>(128);
|
||||||
|
foreach (KeyValuePair<string, Profiler.LabelMetrics> metric in frame.LabelMetrics)
|
||||||
|
{
|
||||||
|
Profiler.RollingLabelMetrics rolling = default;
|
||||||
|
if (rollingData.TryGetValue(metric.Key, out Profiler.RollingLabelMetrics found))
|
||||||
|
{
|
||||||
|
rolling = found;
|
||||||
|
}
|
||||||
|
|
||||||
|
combinedRecordData[metric.Key] = (metric.Value.InclusiveMs, metric.Value.SelfMs, metric.Value.Calls, rolling.AverageMs, rolling.P95Ms, metric.Value.AllocatedBytes, metric.Value.SelfAllocatedBytes);
|
||||||
|
}
|
||||||
|
return combinedRecordData.OrderByDescending(x => x.Value.ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IOrderedEnumerable<KeyValuePair<int, Profiler.RollingThreadMetrics>> CalculateThreadRollingData()
|
||||||
|
{
|
||||||
|
IReadOnlyDictionary<int, Profiler.RollingThreadMetrics> rollingData = Profiler.GetRollingThreadMetricsSnapshot();
|
||||||
|
return rollingData.OrderByDescending(x => x.Value.P95Ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawThreadRolling(in IOrderedEnumerable<KeyValuePair<int, Profiler.RollingThreadMetrics>> orderedThreadRollingData)
|
||||||
|
{
|
||||||
|
if (orderedThreadRollingData == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.BeginChild("ThreadRolling", new Vector2(0, 140), ImGuiChildFlags.Border);
|
||||||
|
if (ImGui.BeginTable("ProfilerThreadRollingData", 6, tableFlags, new Vector2(0, 0)))
|
||||||
|
{
|
||||||
|
ImGui.TableSetupColumn("thread", ImGuiTableColumnFlags.WidthStretch, 0.15f, 0);
|
||||||
|
ImGui.TableSetupColumn("avg", ImGuiTableColumnFlags.WidthStretch, 0.20f, 1);
|
||||||
|
ImGui.TableSetupColumn("p95", ImGuiTableColumnFlags.WidthStretch, 0.20f, 2);
|
||||||
|
ImGui.TableSetupColumn("max", ImGuiTableColumnFlags.WidthStretch, 0.20f, 3);
|
||||||
|
ImGui.TableSetupColumn("samples", ImGuiTableColumnFlags.WidthStretch, 0.15f, 4);
|
||||||
|
ImGui.TableSetupColumn("misses", ImGuiTableColumnFlags.WidthStretch, 0.15f, 5);
|
||||||
|
ImGui.TableHeadersRow();
|
||||||
|
|
||||||
|
foreach (KeyValuePair<int, Profiler.RollingThreadMetrics> metric in orderedThreadRollingData)
|
||||||
|
{
|
||||||
|
ImGui.TableNextRow();
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"T{metric.Key}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{metric.Value.AverageMs:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{metric.Value.P95Ms:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{metric.Value.MaxMs:0.000}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{metric.Value.Samples}");
|
||||||
|
ImGui.TableNextColumn();
|
||||||
|
ImGui.Text($"{metric.Value.BudgetMisses}");
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndChild();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ProfilerVisualizer.TimelineRenderResult DrawFlameGraph(IReadOnlyList<Profiler.Frame> frames, ProfilerVisualizer.TimelineState timelineState)
|
||||||
|
{
|
||||||
|
if (frames == null || frames.Count == 0)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ProfilerVisualizer.RenderTimeline(frames, timelineState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
using Microsoft.CodeAnalysis;
|
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace Nerfed.Runtime.Generator
|
|
||||||
{
|
|
||||||
[Generator]
|
|
||||||
public class HookSourceGenerator : ISourceGenerator
|
|
||||||
{
|
|
||||||
public void Execute(GeneratorExecutionContext context)
|
|
||||||
{
|
|
||||||
// Ensure the syntax receiver is not null and is of the expected type
|
|
||||||
if (context.SyntaxReceiver is not HookSyntaxReceiver syntaxReceiver)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Check if we have collected any hook methods
|
|
||||||
List<MethodDeclarationSyntax> hookMethods = syntaxReceiver.HookMethods;
|
|
||||||
if (hookMethods == null || !hookMethods.Any())
|
|
||||||
return;
|
|
||||||
|
|
||||||
StringBuilder codeBuilder = new StringBuilder();
|
|
||||||
|
|
||||||
codeBuilder.AppendLine("using System;");
|
|
||||||
codeBuilder.AppendLine("");
|
|
||||||
codeBuilder.AppendLine("namespace Nerfed.Runtime.Generator;");
|
|
||||||
codeBuilder.AppendLine("");
|
|
||||||
codeBuilder.AppendLine($"// Generated by {typeof(HookSourceGenerator)}");
|
|
||||||
codeBuilder.AppendLine("public static class Hook");
|
|
||||||
codeBuilder.AppendLine("{");
|
|
||||||
codeBuilder.AppendLine(" public static void InvokeHooks()");
|
|
||||||
codeBuilder.AppendLine(" {");
|
|
||||||
|
|
||||||
foreach (MethodDeclarationSyntax method in hookMethods)
|
|
||||||
{
|
|
||||||
SemanticModel model = context.Compilation.GetSemanticModel(method.SyntaxTree);
|
|
||||||
|
|
||||||
if (model.GetDeclaredSymbol(method) is not IMethodSymbol methodSymbol)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (methodSymbol.DeclaredAccessibility != Accessibility.Public || !methodSymbol.IsStatic)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
codeBuilder.AppendLine($" {methodSymbol.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.{methodSymbol.Name}();");
|
|
||||||
}
|
|
||||||
|
|
||||||
codeBuilder.AppendLine(" }");
|
|
||||||
codeBuilder.AppendLine("}");
|
|
||||||
|
|
||||||
// Add the generated code to the compilation
|
|
||||||
context.AddSource("Hook.g.cs", codeBuilder.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void Initialize(GeneratorInitializationContext context)
|
|
||||||
{
|
|
||||||
context.RegisterForSyntaxNotifications(() => new HookSyntaxReceiver());
|
|
||||||
}
|
|
||||||
|
|
||||||
public class HookSyntaxReceiver : ISyntaxReceiver
|
|
||||||
{
|
|
||||||
public List<MethodDeclarationSyntax> HookMethods { get; } = new List<MethodDeclarationSyntax>();
|
|
||||||
|
|
||||||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
|
|
||||||
{
|
|
||||||
// Check if the node is a method declaration
|
|
||||||
if (syntaxNode is MethodDeclarationSyntax methodDeclaration)
|
|
||||||
{
|
|
||||||
// Ensure the method declaration has attribute lists
|
|
||||||
if (methodDeclaration.AttributeLists.Count == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Check if the method has the Hook attribute
|
|
||||||
bool hasHookAttribute = methodDeclaration.AttributeLists
|
|
||||||
.SelectMany(attrList => attrList.Attributes)
|
|
||||||
.Any(attr => attr.Name.ToString() == "Hook");
|
|
||||||
|
|
||||||
if (hasHookAttribute)
|
|
||||||
{
|
|
||||||
HookMethods.Add(methodDeclaration);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
|
||||||
<Configurations>Debug;Test;Release</Configurations>
|
|
||||||
<Platforms>x64</Platforms>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
|
||||||
<PrivateAssets>all</PrivateAssets>
|
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
|
||||||
</PackageReference>
|
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0"/>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
|
||||||
<DefineConstants>TRACE;LOG_INFO;PROFILING</DefineConstants>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Test|x64' ">
|
|
||||||
<DefineConstants>TRACE;LOG_ERROR;PROFILING</DefineConstants>
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
|
|
||||||
<DefineConstants>TRACE;LOG_ERROR</DefineConstants>
|
|
||||||
<Optimize>true</Optimize>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Components
|
||||||
|
{
|
||||||
|
public readonly record struct LocalToWorld(Matrix4x4 localToWorldMatrix);
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Components
|
||||||
|
{
|
||||||
|
public readonly record struct LocalTransform(Vector3 position, Quaternion rotation, Vector3 scale)
|
||||||
|
{
|
||||||
|
public static readonly LocalTransform Identity = new(Vector3.Zero, Quaternion.Identity, Vector3.One);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
using MoonTools.ECS;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Components
|
||||||
|
{
|
||||||
|
public readonly record struct Root;
|
||||||
|
//public readonly record struct Parent;
|
||||||
|
//public readonly record struct PreviousParent;
|
||||||
|
public readonly record struct Child;
|
||||||
|
// Describes a relation from the child to the parent.
|
||||||
|
public readonly record struct ChildParentRelation;
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Nerfed.Runtime.Components
|
||||||
|
{
|
||||||
|
public readonly record struct Test();
|
||||||
|
}
|
||||||
@@ -71,8 +71,9 @@ public static class Engine
|
|||||||
|
|
||||||
AudioDevice = new AudioDevice();
|
AudioDevice = new AudioDevice();
|
||||||
|
|
||||||
|
JobSystem.Default.Initialize();
|
||||||
|
|
||||||
OnInitialize?.Invoke();
|
OnInitialize?.Invoke();
|
||||||
Nerfed.Runtime.Generator.Hook.InvokeHooks();
|
|
||||||
|
|
||||||
while (!quit)
|
while (!quit)
|
||||||
{
|
{
|
||||||
@@ -85,6 +86,7 @@ public static class Engine
|
|||||||
MainWindow.Dispose();
|
MainWindow.Dispose();
|
||||||
GraphicsDevice.Dispose();
|
GraphicsDevice.Dispose();
|
||||||
AudioDevice.Dispose();
|
AudioDevice.Dispose();
|
||||||
|
JobSystem.Default.Shutdown();
|
||||||
SDL.SDL_Quit();
|
SDL.SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,10 +114,14 @@ public static class Engine
|
|||||||
|
|
||||||
private static void Tick()
|
private static void Tick()
|
||||||
{
|
{
|
||||||
|
Profiler.BeginFrame();
|
||||||
|
|
||||||
AdvanceElapsedTime();
|
AdvanceElapsedTime();
|
||||||
|
|
||||||
if (framerateCapped)
|
if (framerateCapped)
|
||||||
{
|
{
|
||||||
|
Profiler.BeginSample("framerateCapped");
|
||||||
|
|
||||||
/* We want to wait until the framerate cap,
|
/* We want to wait until the framerate cap,
|
||||||
* but we don't want to oversleep. Requesting repeated 1ms sleeps and
|
* but we don't want to oversleep. Requesting repeated 1ms sleeps and
|
||||||
* seeing how long we actually slept for lets us estimate the worst case
|
* seeing how long we actually slept for lets us estimate the worst case
|
||||||
@@ -138,6 +144,8 @@ public static class Engine
|
|||||||
Thread.SpinWait(1);
|
Thread.SpinWait(1);
|
||||||
AdvanceElapsedTime();
|
AdvanceElapsedTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Profiler.EndSample();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not let any step take longer than our maximum.
|
// Do not let any step take longer than our maximum.
|
||||||
@@ -150,6 +158,7 @@ public static class Engine
|
|||||||
{
|
{
|
||||||
while (accumulatedUpdateTime >= Timestep)
|
while (accumulatedUpdateTime >= Timestep)
|
||||||
{
|
{
|
||||||
|
Profiler.BeginSample("Update");
|
||||||
Keyboard.Update();
|
Keyboard.Update();
|
||||||
Mouse.Update();
|
Mouse.Update();
|
||||||
GamePad.Update();
|
GamePad.Update();
|
||||||
@@ -157,19 +166,26 @@ public static class Engine
|
|||||||
ProcessSDLEvents();
|
ProcessSDLEvents();
|
||||||
|
|
||||||
// Tick game here...
|
// Tick game here...
|
||||||
|
Profiler.BeginSample("OnUpdate");
|
||||||
OnUpdate?.Invoke();
|
OnUpdate?.Invoke();
|
||||||
|
Profiler.EndSample();
|
||||||
|
|
||||||
AudioDevice.WakeThread();
|
AudioDevice.WakeThread();
|
||||||
accumulatedUpdateTime -= Timestep;
|
accumulatedUpdateTime -= Timestep;
|
||||||
|
Profiler.EndSample();
|
||||||
}
|
}
|
||||||
|
|
||||||
double alpha = accumulatedUpdateTime / Timestep;
|
double alpha = accumulatedUpdateTime / Timestep;
|
||||||
|
|
||||||
// Render here..
|
// Render here..
|
||||||
|
Profiler.BeginSample("OnRender");
|
||||||
OnRender?.Invoke();
|
OnRender?.Invoke();
|
||||||
|
Profiler.EndSample();
|
||||||
|
|
||||||
accumulatedDrawTime -= framerateCapTimeSpan;
|
accumulatedDrawTime -= framerateCapTimeSpan;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Profiler.EndFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TimeSpan AdvanceElapsedTime()
|
private static TimeSpan AdvanceElapsedTime()
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Nerfed.Runtime.Hook
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
|
||||||
public class HookAttribute : Attribute
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
namespace Nerfed.Runtime.Hook
|
|
||||||
{
|
|
||||||
public static class HookTest
|
|
||||||
{
|
|
||||||
[Hook]
|
|
||||||
public static void Test()
|
|
||||||
{
|
|
||||||
Log.Info("Hook!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime;
|
||||||
|
|
||||||
|
public sealed class JobSystem : IDisposable
|
||||||
|
{
|
||||||
|
public static readonly JobSystem Default = new JobSystem();
|
||||||
|
|
||||||
|
private Thread[] workers;
|
||||||
|
private SemaphoreSlim startSignal;
|
||||||
|
private CountdownEvent completionEvent;
|
||||||
|
private volatile bool running;
|
||||||
|
|
||||||
|
// Shared per-dispatch state written by main thread before workers wake.
|
||||||
|
private volatile Action<int> currentAction;
|
||||||
|
private int workCount;
|
||||||
|
private int nextIndex; // grabbed with Interlocked.Increment for work-stealing
|
||||||
|
|
||||||
|
public int WorkerCount => workers?.Length ?? 0;
|
||||||
|
public bool IsInitialized => workers != null;
|
||||||
|
|
||||||
|
public void Initialize(int threadCount = -1)
|
||||||
|
{
|
||||||
|
if (IsInitialized)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("JobSystem is already initialized. Call Shutdown first.");
|
||||||
|
}
|
||||||
|
|
||||||
|
threadCount = threadCount < 0
|
||||||
|
? Math.Max(1, Environment.ProcessorCount - 2)
|
||||||
|
: Math.Max(1, threadCount);
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
startSignal = new SemaphoreSlim(0);
|
||||||
|
completionEvent = new CountdownEvent(1);
|
||||||
|
workers = new Thread[threadCount];
|
||||||
|
|
||||||
|
for (int i = 0; i < threadCount; i++)
|
||||||
|
{
|
||||||
|
workers[i] = new Thread(WorkerLoop)
|
||||||
|
{
|
||||||
|
IsBackground = true,
|
||||||
|
Name = $"Job-{i}",
|
||||||
|
};
|
||||||
|
workers[i].Start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispatch(int count, Action<int> action)
|
||||||
|
{
|
||||||
|
if (count <= 0) return;
|
||||||
|
|
||||||
|
if (!IsInitialized)
|
||||||
|
{
|
||||||
|
// Safe fallback: run inline if Initialize was never called.
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
action(i);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentAction = action;
|
||||||
|
workCount = count;
|
||||||
|
Volatile.Write(ref nextIndex, 0);
|
||||||
|
|
||||||
|
completionEvent.Reset(workers.Length);
|
||||||
|
startSignal.Release(workers.Length);
|
||||||
|
completionEvent.Wait();
|
||||||
|
|
||||||
|
currentAction = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Shutdown()
|
||||||
|
{
|
||||||
|
if (!IsInitialized) return;
|
||||||
|
|
||||||
|
running = false;
|
||||||
|
startSignal.Release(workers.Length); // wake all workers so they can see running=false and exit
|
||||||
|
|
||||||
|
foreach (Thread t in workers)
|
||||||
|
t.Join();
|
||||||
|
|
||||||
|
completionEvent.Dispose();
|
||||||
|
startSignal.Dispose();
|
||||||
|
workers = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose() => Shutdown();
|
||||||
|
|
||||||
|
private void WorkerLoop()
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
startSignal.Wait();
|
||||||
|
if (!running) return;
|
||||||
|
|
||||||
|
Action<int> action = currentAction;
|
||||||
|
int total = workCount;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int index = Interlocked.Increment(ref nextIndex) - 1;
|
||||||
|
if (index >= total) break;
|
||||||
|
action(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
completionEvent.Signal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Submodule
+1
Submodule Nerfed.Runtime/Libraries/MoonTools.ECS added at 76b18a6ba9
@@ -11,7 +11,6 @@
|
|||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<Configurations>Debug;Test;Release</Configurations>
|
<Configurations>Debug;Test;Release</Configurations>
|
||||||
<Platforms>x64</Platforms>
|
<Platforms>x64</Platforms>
|
||||||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
@@ -33,18 +32,13 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Libraries\SDL2CS\src\SDL2.cs" />
|
|
||||||
<Compile Include="Libraries\RefreshCS\RefreshCS.cs" />
|
|
||||||
<Compile Include="Libraries\FAudio\csharp\FAudio.cs" />
|
<Compile Include="Libraries\FAudio\csharp\FAudio.cs" />
|
||||||
|
<Compile Include="Libraries\ImGui.NET\src\ImGui.NET\**\*.cs" />
|
||||||
|
<Compile Include="Libraries\MoonTools.ECS\src\**\*.cs" />
|
||||||
|
<Compile Include="Libraries\RefreshCS\RefreshCS.cs" />
|
||||||
|
<Compile Include="Libraries\SDL2CS\src\SDL2.cs" />
|
||||||
<Compile Include="Libraries\WellspringCS\WellspringCS.cs" />
|
<Compile Include="Libraries\WellspringCS\WellspringCS.cs" />
|
||||||
<Compile Include="Libraries\dav1dfile\csharp\dav1dfile.cs" />
|
<Compile Include="Libraries\dav1dfile\csharp\dav1dfile.cs" />
|
||||||
<Compile Include="Libraries\ImGui.NET\src\ImGui.NET\**\*.cs" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\Nerfed.Runtime.Generator\Nerfed.Runtime.Generator.csproj"
|
|
||||||
OutputItemType="Analyzer"
|
|
||||||
ReferenceOutputAssembly="false" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
+792
-9
@@ -1,29 +1,812 @@
|
|||||||
using System.Diagnostics;
|
using System.Collections.Concurrent;
|
||||||
using System.Reflection;
|
using System.Diagnostics;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Nerfed.Runtime;
|
namespace Nerfed.Runtime;
|
||||||
|
|
||||||
public struct ProfilerScope : IDisposable
|
public struct ProfilerScope : IDisposable
|
||||||
{
|
{
|
||||||
public ProfilerScope(string label) {
|
public ProfilerScope(string label)
|
||||||
|
{
|
||||||
Profiler.BeginSample(label);
|
Profiler.BeginSample(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose() {
|
public ProfilerScope(string label, string category, ulong tagMask = 0)
|
||||||
|
{
|
||||||
|
Profiler.BeginSample(label, category, tagMask);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
Profiler.EndSample();
|
Profiler.EndSample();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Profiler
|
public static class Profiler
|
||||||
{
|
{
|
||||||
[Conditional("PROFILING")]
|
public enum CaptureMode
|
||||||
public static void BeginSample(string label) {
|
{
|
||||||
|
Instrumented = 0,
|
||||||
|
SampledInstrumentation = 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class ThreadProfilerState
|
||||||
|
{
|
||||||
|
public readonly Stack<ScopeNode> Scopes = new Stack<ScopeNode>();
|
||||||
|
public readonly Stack<bool> CaptureDecisions = new Stack<bool>();
|
||||||
|
public int SampleCursor;
|
||||||
|
public int ThreadId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct LabelMetrics
|
||||||
|
{
|
||||||
|
public LabelMetrics(double inclusiveMs, double selfMs, uint calls, double minInclusiveMs, double maxInclusiveMs, long allocatedBytes, long selfAllocatedBytes)
|
||||||
|
{
|
||||||
|
InclusiveMs = inclusiveMs;
|
||||||
|
SelfMs = selfMs;
|
||||||
|
Calls = calls;
|
||||||
|
MinInclusiveMs = minInclusiveMs;
|
||||||
|
MaxInclusiveMs = maxInclusiveMs;
|
||||||
|
AllocatedBytes = allocatedBytes;
|
||||||
|
SelfAllocatedBytes = selfAllocatedBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double InclusiveMs { get; }
|
||||||
|
public double SelfMs { get; }
|
||||||
|
public uint Calls { get; }
|
||||||
|
public double MinInclusiveMs { get; }
|
||||||
|
public double MaxInclusiveMs { get; }
|
||||||
|
public long AllocatedBytes { get; }
|
||||||
|
public long SelfAllocatedBytes { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct RollingLabelMetrics
|
||||||
|
{
|
||||||
|
public RollingLabelMetrics(double averageMs, double minMs, double maxMs, double p95Ms, int samples)
|
||||||
|
{
|
||||||
|
AverageMs = averageMs;
|
||||||
|
MinMs = minMs;
|
||||||
|
MaxMs = maxMs;
|
||||||
|
P95Ms = p95Ms;
|
||||||
|
Samples = samples;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double AverageMs { get; }
|
||||||
|
public double MinMs { get; }
|
||||||
|
public double MaxMs { get; }
|
||||||
|
public double P95Ms { get; }
|
||||||
|
public int Samples { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct ThreadMetrics
|
||||||
|
{
|
||||||
|
public ThreadMetrics(double inclusiveMs, double selfMs, uint calls, bool overBudget)
|
||||||
|
{
|
||||||
|
InclusiveMs = inclusiveMs;
|
||||||
|
SelfMs = selfMs;
|
||||||
|
Calls = calls;
|
||||||
|
OverBudget = overBudget;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double InclusiveMs { get; }
|
||||||
|
public double SelfMs { get; }
|
||||||
|
public uint Calls { get; }
|
||||||
|
public bool OverBudget { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct RollingThreadMetrics
|
||||||
|
{
|
||||||
|
public RollingThreadMetrics(double averageMs, double p95Ms, double maxMs, int samples, int budgetMisses)
|
||||||
|
{
|
||||||
|
AverageMs = averageMs;
|
||||||
|
P95Ms = p95Ms;
|
||||||
|
MaxMs = maxMs;
|
||||||
|
Samples = samples;
|
||||||
|
BudgetMisses = budgetMisses;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double AverageMs { get; }
|
||||||
|
public double P95Ms { get; }
|
||||||
|
public double MaxMs { get; }
|
||||||
|
public int Samples { get; }
|
||||||
|
public int BudgetMisses { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class RollingWindow
|
||||||
|
{
|
||||||
|
private readonly double[] values;
|
||||||
|
private readonly double[] sortBuffer; // pre-allocated; avoids per-snapshot heap allocation
|
||||||
|
private int index;
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
public RollingWindow(int capacity)
|
||||||
|
{
|
||||||
|
int size = Math.Max(8, capacity);
|
||||||
|
values = new double[size];
|
||||||
|
sortBuffer = new double[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(double value)
|
||||||
|
{
|
||||||
|
values[index] = value;
|
||||||
|
index = (index + 1) % values.Length;
|
||||||
|
if (count < values.Length)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RollingLabelMetrics Snapshot()
|
||||||
|
{
|
||||||
|
if (count == 0)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
double sum = 0;
|
||||||
|
double min = double.MaxValue;
|
||||||
|
double max = double.MinValue;
|
||||||
|
|
||||||
|
int start = (index - count + values.Length) % values.Length;
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
double value = values[(start + i) % values.Length];
|
||||||
|
sortBuffer[i] = value;
|
||||||
|
sum += value;
|
||||||
|
min = Math.Min(min, value);
|
||||||
|
max = Math.Max(max, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Array.Sort(sortBuffer, 0, count);
|
||||||
|
int percentileIndex = (int)Math.Ceiling((count - 1) * 0.95d);
|
||||||
|
double p95 = sortBuffer[Math.Clamp(percentileIndex, 0, count - 1)];
|
||||||
|
return new RollingLabelMetrics(sum / count, min, max, p95, count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class RollingThreadWindow
|
||||||
|
{
|
||||||
|
private readonly double[] durations;
|
||||||
|
private readonly double[] sortBuffer; // pre-allocated; avoids per-snapshot heap allocation
|
||||||
|
private readonly byte[] misses;
|
||||||
|
private int index;
|
||||||
|
private int count;
|
||||||
|
|
||||||
|
public RollingThreadWindow(int capacity)
|
||||||
|
{
|
||||||
|
int size = Math.Max(8, capacity);
|
||||||
|
durations = new double[size];
|
||||||
|
sortBuffer = new double[size];
|
||||||
|
misses = new byte[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(double durationMs, bool budgetMiss)
|
||||||
|
{
|
||||||
|
durations[index] = durationMs;
|
||||||
|
misses[index] = budgetMiss ? (byte)1 : (byte)0;
|
||||||
|
index = (index + 1) % durations.Length;
|
||||||
|
if (count < durations.Length)
|
||||||
|
{
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RollingThreadMetrics Snapshot()
|
||||||
|
{
|
||||||
|
if (count == 0)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
double sum = 0;
|
||||||
|
double max = double.MinValue;
|
||||||
|
int budgetMisses = 0;
|
||||||
|
|
||||||
|
int start = (index - count + durations.Length) % durations.Length;
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
int at = (start + i) % durations.Length;
|
||||||
|
double value = durations[at];
|
||||||
|
sum += value;
|
||||||
|
max = Math.Max(max, value);
|
||||||
|
budgetMisses += misses[at];
|
||||||
|
sortBuffer[i] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
Array.Sort(sortBuffer, 0, count);
|
||||||
|
int percentileIndex = (int)Math.Ceiling((count - 1) * 0.95d);
|
||||||
|
double p95 = sortBuffer[Math.Clamp(percentileIndex, 0, count - 1)];
|
||||||
|
return new RollingThreadMetrics(sum / count, p95, max, count, budgetMisses);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Frame
|
||||||
|
{
|
||||||
|
private readonly List<ScopeNode> rootNodes = new List<ScopeNode>(8);
|
||||||
|
private readonly object rootNodesLock = new object();
|
||||||
|
private readonly Dictionary<string, LabelMetrics> labelMetrics = new Dictionary<string, LabelMetrics>(128, StringComparer.Ordinal);
|
||||||
|
private readonly Dictionary<string, LabelMetrics> categoryMetrics = new Dictionary<string, LabelMetrics>(32, StringComparer.Ordinal);
|
||||||
|
private readonly Dictionary<int, ThreadMetrics> threadMetrics = new Dictionary<int, ThreadMetrics>(16);
|
||||||
|
private readonly List<int> knownThreadIds = new List<int>(16); // avoids Keys.ToArray() in ApplyThreadBudgetFlags
|
||||||
|
|
||||||
|
public uint FrameCount { get; private set; }
|
||||||
|
public long StartTime { get; private set; }
|
||||||
|
public long EndTime { get; private set; }
|
||||||
|
|
||||||
|
public IReadOnlyList<ScopeNode> RootNodes => rootNodes;
|
||||||
|
public IReadOnlyDictionary<string, LabelMetrics> LabelMetrics => labelMetrics;
|
||||||
|
public IReadOnlyDictionary<string, LabelMetrics> CategoryMetrics => categoryMetrics;
|
||||||
|
public IReadOnlyDictionary<int, ThreadMetrics> ThreadMetrics => threadMetrics;
|
||||||
|
|
||||||
|
// Return concrete types so callers can use the struct enumerator and avoid boxing.
|
||||||
|
internal Dictionary<string, LabelMetrics> LabelMetricsRaw => labelMetrics;
|
||||||
|
internal Dictionary<int, ThreadMetrics> ThreadMetricsRaw => threadMetrics;
|
||||||
|
|
||||||
|
public long AllocatedBytesStart { get; private set; }
|
||||||
|
public long AllocatedBytesEnd { get; private set; }
|
||||||
|
public long AllocatedBytesDelta { get; private set; }
|
||||||
|
public int Gen0CollectionsStart { get; private set; }
|
||||||
|
public int Gen1CollectionsStart { get; private set; }
|
||||||
|
public int Gen2CollectionsStart { get; private set; }
|
||||||
|
public int Gen0CollectionsEnd { get; private set; }
|
||||||
|
public int Gen1CollectionsEnd { get; private set; }
|
||||||
|
public int Gen2CollectionsEnd { get; private set; }
|
||||||
|
public int Gen0CollectionsDelta { get; private set; }
|
||||||
|
public int Gen1CollectionsDelta { get; private set; }
|
||||||
|
public int Gen2CollectionsDelta { get; private set; }
|
||||||
|
public bool OverBudget { get; private set; }
|
||||||
|
public double BudgetMilliseconds { get; private set; }
|
||||||
|
|
||||||
|
internal void Reset(uint frameCount)
|
||||||
|
{
|
||||||
|
FrameCount = frameCount;
|
||||||
|
StartTime = Stopwatch.GetTimestamp();
|
||||||
|
EndTime = 0;
|
||||||
|
OverBudget = false;
|
||||||
|
BudgetMilliseconds = 0;
|
||||||
|
AllocatedBytesStart = GC.GetTotalAllocatedBytes(false);
|
||||||
|
AllocatedBytesEnd = 0;
|
||||||
|
AllocatedBytesDelta = 0;
|
||||||
|
Gen0CollectionsStart = GC.CollectionCount(0);
|
||||||
|
Gen1CollectionsStart = GC.CollectionCount(1);
|
||||||
|
Gen2CollectionsStart = GC.CollectionCount(2);
|
||||||
|
Gen0CollectionsEnd = 0;
|
||||||
|
Gen1CollectionsEnd = 0;
|
||||||
|
Gen2CollectionsEnd = 0;
|
||||||
|
Gen0CollectionsDelta = 0;
|
||||||
|
Gen1CollectionsDelta = 0;
|
||||||
|
Gen2CollectionsDelta = 0;
|
||||||
|
lock (rootNodesLock)
|
||||||
|
{
|
||||||
|
rootNodes.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void AddRootNode(ScopeNode rootNode)
|
||||||
|
{
|
||||||
|
lock (rootNodesLock)
|
||||||
|
{
|
||||||
|
rootNodes.Add(rootNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void End(double budgetMilliseconds)
|
||||||
|
{
|
||||||
|
EndTime = Stopwatch.GetTimestamp();
|
||||||
|
BudgetMilliseconds = budgetMilliseconds;
|
||||||
|
OverBudget = budgetMilliseconds > 0 && ElapsedMilliseconds() > budgetMilliseconds;
|
||||||
|
|
||||||
|
AllocatedBytesEnd = GC.GetTotalAllocatedBytes(false);
|
||||||
|
AllocatedBytesDelta = AllocatedBytesEnd - AllocatedBytesStart;
|
||||||
|
|
||||||
|
Gen0CollectionsEnd = GC.CollectionCount(0);
|
||||||
|
Gen1CollectionsEnd = GC.CollectionCount(1);
|
||||||
|
Gen2CollectionsEnd = GC.CollectionCount(2);
|
||||||
|
Gen0CollectionsDelta = Gen0CollectionsEnd - Gen0CollectionsStart;
|
||||||
|
Gen1CollectionsDelta = Gen1CollectionsEnd - Gen1CollectionsStart;
|
||||||
|
Gen2CollectionsDelta = Gen2CollectionsEnd - Gen2CollectionsStart;
|
||||||
|
|
||||||
|
BuildLabelMetrics();
|
||||||
|
}
|
||||||
|
|
||||||
|
public double ElapsedMilliseconds()
|
||||||
|
{
|
||||||
|
long elapsedTicks = EndTime - StartTime;
|
||||||
|
return ((double)(elapsedTicks * 1000)) / Stopwatch.Frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void BuildLabelMetrics()
|
||||||
|
{
|
||||||
|
labelMetrics.Clear();
|
||||||
|
categoryMetrics.Clear();
|
||||||
|
threadMetrics.Clear();
|
||||||
|
knownThreadIds.Clear();
|
||||||
|
lock (rootNodesLock)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < rootNodes.Count; i++)
|
||||||
|
{
|
||||||
|
AccumulateLabelMetrics(rootNodes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < rootNodes.Count; i++)
|
||||||
|
{
|
||||||
|
ScopeNode rootNode = rootNodes[i];
|
||||||
|
for (int j = 0; j < rootNode.Children.Count; j++)
|
||||||
|
{
|
||||||
|
AccumulateThreadMetrics(rootNode.ManagedThreadId, rootNode.Children[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ApplyThreadBudgetFlags();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AccumulateLabelMetrics(ScopeNode node)
|
||||||
|
{
|
||||||
|
double inclusiveMs = node.ElapsedMilliseconds();
|
||||||
|
double selfMs = node.SelfMilliseconds();
|
||||||
|
long allocBytes = node.AllocatedBytes;
|
||||||
|
long selfAllocBytes = node.SelfAllocatedBytes();
|
||||||
|
|
||||||
|
if (labelMetrics.TryGetValue(node.Label, out LabelMetrics current))
|
||||||
|
{
|
||||||
|
labelMetrics[node.Label] = new LabelMetrics(
|
||||||
|
current.InclusiveMs + inclusiveMs,
|
||||||
|
current.SelfMs + selfMs,
|
||||||
|
current.Calls + 1,
|
||||||
|
Math.Min(current.MinInclusiveMs, inclusiveMs),
|
||||||
|
Math.Max(current.MaxInclusiveMs, inclusiveMs),
|
||||||
|
current.AllocatedBytes + allocBytes,
|
||||||
|
current.SelfAllocatedBytes + selfAllocBytes);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
labelMetrics[node.Label] = new LabelMetrics(inclusiveMs, selfMs, 1, inclusiveMs, inclusiveMs, allocBytes, selfAllocBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (categoryMetrics.TryGetValue(node.Category, out LabelMetrics categoryCurrent))
|
||||||
|
{
|
||||||
|
categoryMetrics[node.Category] = new LabelMetrics(
|
||||||
|
categoryCurrent.InclusiveMs + inclusiveMs,
|
||||||
|
categoryCurrent.SelfMs + selfMs,
|
||||||
|
categoryCurrent.Calls + 1,
|
||||||
|
Math.Min(categoryCurrent.MinInclusiveMs, inclusiveMs),
|
||||||
|
Math.Max(categoryCurrent.MaxInclusiveMs, inclusiveMs),
|
||||||
|
categoryCurrent.AllocatedBytes + allocBytes,
|
||||||
|
categoryCurrent.SelfAllocatedBytes + selfAllocBytes);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
categoryMetrics[node.Category] = new LabelMetrics(inclusiveMs, selfMs, 1, inclusiveMs, inclusiveMs, allocBytes, selfAllocBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
AccumulateLabelMetrics(node.Children[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AccumulateThreadMetrics(int threadId, ScopeNode node)
|
||||||
|
{
|
||||||
|
double inclusiveMs = node.ElapsedMilliseconds();
|
||||||
|
double selfMs = node.SelfMilliseconds();
|
||||||
|
|
||||||
|
if (threadMetrics.TryGetValue(threadId, out ThreadMetrics current))
|
||||||
|
{
|
||||||
|
threadMetrics[threadId] = new ThreadMetrics(current.InclusiveMs + inclusiveMs, current.SelfMs + selfMs, current.Calls + 1, false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
threadMetrics[threadId] = new ThreadMetrics(inclusiveMs, selfMs, 1, false);
|
||||||
|
knownThreadIds.Add(threadId);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
AccumulateThreadMetrics(threadId, node.Children[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ApplyThreadBudgetFlags()
|
||||||
|
{
|
||||||
|
double perThreadBudget = Math.Max(0d, ThreadBudgetMilliseconds);
|
||||||
|
if (perThreadBudget <= 0d)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// knownThreadIds avoids Keys.ToArray() allocation
|
||||||
|
for (int i = 0; i < knownThreadIds.Count; i++)
|
||||||
|
{
|
||||||
|
int key = knownThreadIds[i];
|
||||||
|
ThreadMetrics metric = threadMetrics[key];
|
||||||
|
threadMetrics[key] = new ThreadMetrics(metric.InclusiveMs, metric.SelfMs, metric.Calls, metric.InclusiveMs > perThreadBudget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ScopeNode
|
||||||
|
{
|
||||||
|
public string Label { get; private set; } = string.Empty;
|
||||||
|
public string Category { get; private set; } = DefaultCategory;
|
||||||
|
public ulong TagMask { get; private set; }
|
||||||
|
public long StartTime { get; private set; }
|
||||||
|
public long EndTime { get; private set; }
|
||||||
|
public int ManagedThreadId { get; private set; }
|
||||||
|
public List<ScopeNode> Children { get; } = new List<ScopeNode>();
|
||||||
|
public long AllocatedBytes { get; private set; }
|
||||||
|
public long ProfilerSetupBytes { get; private set; }
|
||||||
|
internal ScopeNode Parent { get; private set; }
|
||||||
|
internal long ChildrenDurationTicks { get; private set; }
|
||||||
|
internal long ChildrenAllocatedBytes { get; private set; }
|
||||||
|
private long allocatedBytesAtStart;
|
||||||
|
|
||||||
|
internal void Reset(string label, string category, ulong tagMask, int managedThreadId, ScopeNode parent)
|
||||||
|
{
|
||||||
|
Label = label;
|
||||||
|
Category = string.IsNullOrEmpty(category) ? DefaultCategory : category;
|
||||||
|
TagMask = tagMask;
|
||||||
|
ManagedThreadId = managedThreadId;
|
||||||
|
Parent = parent;
|
||||||
|
StartTime = Stopwatch.GetTimestamp();
|
||||||
|
EndTime = 0;
|
||||||
|
ChildrenDurationTicks = 0;
|
||||||
|
ChildrenAllocatedBytes = 0;
|
||||||
|
AllocatedBytes = 0;
|
||||||
|
ProfilerSetupBytes = 0;
|
||||||
|
Children.Clear();
|
||||||
|
allocatedBytesAtStart = GC.GetAllocatedBytesForCurrentThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void End()
|
||||||
|
{
|
||||||
|
if (EndTime != 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EndTime = Stopwatch.GetTimestamp();
|
||||||
|
if (Parent != null)
|
||||||
|
{
|
||||||
|
// Root nodes are ended from FinalizeCurrentFrame on the main thread, so their
|
||||||
|
// GC counter would be from the wrong thread. Only track alloc on non-root nodes.
|
||||||
|
AllocatedBytes = Math.Max(0, GC.GetAllocatedBytesForCurrentThread() - allocatedBytesAtStart);
|
||||||
|
Parent.ChildrenDurationTicks += Math.Max(0, EndTime - StartTime);
|
||||||
|
Parent.ChildrenAllocatedBytes += AllocatedBytes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double ElapsedMilliseconds()
|
||||||
|
{
|
||||||
|
return ((double)(Math.Max(0, EndTime - StartTime))) * 1000 / Stopwatch.Frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double SelfMilliseconds()
|
||||||
|
{
|
||||||
|
long elapsedTicks = Math.Max(0, EndTime - StartTime);
|
||||||
|
long selfTicks = Math.Max(0, elapsedTicks - ChildrenDurationTicks);
|
||||||
|
return ((double)selfTicks) * 1000 / Stopwatch.Frequency;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long SelfAllocatedBytes()
|
||||||
|
{
|
||||||
|
return Math.Max(0, AllocatedBytes - ChildrenAllocatedBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Called after all profiler setup (Children.Add + scopes.Push) to measure overhead within this scope's window.
|
||||||
|
internal void NoteSetupOverhead()
|
||||||
|
{
|
||||||
|
ProfilerSetupBytes = Math.Max(0, GC.GetAllocatedBytesForCurrentThread() - allocatedBytesAtStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal ScopeNode AddChild(string label, string category, ulong tagMask)
|
||||||
|
{
|
||||||
|
ScopeNode child = RentNode(label, category, tagMask, ManagedThreadId, this);
|
||||||
|
Children.Add(child);
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private const int maxFrames = 128;
|
||||||
|
private const int rollingWindowSize = 240;
|
||||||
|
private const string DefaultCategory = "General";
|
||||||
|
|
||||||
|
public static bool IsRecording { get; private set; } = true;
|
||||||
|
public static double FrameBudgetMilliseconds { get; set; } = 16.667;
|
||||||
|
public static double ThreadBudgetMilliseconds { get; set; } = 8.333;
|
||||||
|
public static CaptureMode Mode { get; set; } = CaptureMode.Instrumented;
|
||||||
|
public static int SamplingStride { get; set; } = 8;
|
||||||
|
|
||||||
|
public static readonly BoundedQueue<Frame> Frames = new(maxFrames);
|
||||||
|
|
||||||
|
// trackAllValues=false; registeredThreadStates avoids ThreadLocal.Values allocating a ReadOnlyCollection each call
|
||||||
|
private static readonly ThreadLocal<ThreadProfilerState> threadStates =
|
||||||
|
new ThreadLocal<ThreadProfilerState>(() =>
|
||||||
|
{
|
||||||
|
ThreadProfilerState state = new ThreadProfilerState();
|
||||||
|
lock (registeredThreadStatesLock)
|
||||||
|
{
|
||||||
|
registeredThreadStates.Add(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
return state;
|
||||||
|
});
|
||||||
|
|
||||||
|
private static readonly List<ThreadProfilerState> registeredThreadStates = new List<ThreadProfilerState>(8);
|
||||||
|
private static readonly object registeredThreadStatesLock = new object();
|
||||||
|
|
||||||
|
private static readonly ConcurrentDictionary<int, string> threadRootLabelCache = new ConcurrentDictionary<int, string>();
|
||||||
|
private static readonly ConcurrentBag<ScopeNode> nodePool = new ConcurrentBag<ScopeNode>();
|
||||||
|
private static readonly ConcurrentBag<Frame> framePool = new ConcurrentBag<Frame>(); // pooled; avoids per-frame Frame allocation
|
||||||
|
private static readonly Dictionary<string, RollingWindow> rollingWindows = new Dictionary<string, RollingWindow>(256, StringComparer.Ordinal);
|
||||||
|
private static readonly Dictionary<int, RollingThreadWindow> rollingThreadWindows = new Dictionary<int, RollingThreadWindow>(16);
|
||||||
|
private static readonly object rollingWindowsLock = new object();
|
||||||
|
|
||||||
|
private static Frame currentFrame = null;
|
||||||
|
private static uint frameCount = 0;
|
||||||
|
|
||||||
|
public static void SetActive(bool isRecording)
|
||||||
|
{
|
||||||
|
if (IsRecording && !isRecording)
|
||||||
|
{
|
||||||
|
FinalizeCurrentFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
IsRecording = isRecording;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int CopyFramesTo(List<Frame> destination)
|
||||||
|
{
|
||||||
|
return Frames.CopyTo(destination);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IReadOnlyDictionary<string, RollingLabelMetrics> GetRollingLabelMetricsSnapshot()
|
||||||
|
{
|
||||||
|
lock (rollingWindowsLock)
|
||||||
|
{
|
||||||
|
Dictionary<string, RollingLabelMetrics> snapshot = new Dictionary<string, RollingLabelMetrics>(rollingWindows.Count, StringComparer.Ordinal);
|
||||||
|
foreach (KeyValuePair<string, RollingWindow> pair in rollingWindows)
|
||||||
|
{
|
||||||
|
snapshot[pair.Key] = pair.Value.Snapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IReadOnlyDictionary<int, RollingThreadMetrics> GetRollingThreadMetricsSnapshot()
|
||||||
|
{
|
||||||
|
lock (rollingWindowsLock)
|
||||||
|
{
|
||||||
|
Dictionary<int, RollingThreadMetrics> snapshot = new Dictionary<int, RollingThreadMetrics>(rollingThreadWindows.Count);
|
||||||
|
foreach (KeyValuePair<int, RollingThreadWindow> pair in rollingThreadWindows)
|
||||||
|
{
|
||||||
|
snapshot[pair.Key] = pair.Value.Snapshot();
|
||||||
|
}
|
||||||
|
|
||||||
|
return snapshot;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Conditional("PROFILING")]
|
[Conditional("PROFILING")]
|
||||||
public static void EndSample() {
|
public static void BeginFrame()
|
||||||
|
{
|
||||||
|
if (!IsRecording)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentFrame != null)
|
||||||
|
{
|
||||||
|
FinalizeCurrentFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
currentFrame = RentFrame(frameCount);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
[Conditional("PROFILING")]
|
||||||
|
public static void EndFrame()
|
||||||
|
{
|
||||||
|
if (!IsRecording)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FinalizeCurrentFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Conditional("PROFILING")]
|
||||||
|
public static void BeginSample(string label)
|
||||||
|
{
|
||||||
|
BeginSample(label, DefaultCategory, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Conditional("PROFILING")]
|
||||||
|
public static void BeginSample(string label, string category, ulong tagMask = 0)
|
||||||
|
{
|
||||||
|
if (!IsRecording || currentFrame == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadProfilerState state = threadStates.Value;
|
||||||
|
state.ThreadId = Environment.CurrentManagedThreadId;
|
||||||
|
|
||||||
|
bool parentCaptured = state.CaptureDecisions.Count > 0 && state.CaptureDecisions.Peek();
|
||||||
|
bool capture = parentCaptured || Mode == CaptureMode.Instrumented || ShouldSample(state);
|
||||||
|
state.CaptureDecisions.Push(capture);
|
||||||
|
|
||||||
|
if (!capture)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stack<ScopeNode> scopes = state.Scopes;
|
||||||
|
Frame frame = currentFrame;
|
||||||
|
if (frame == null)
|
||||||
|
{
|
||||||
|
state.CaptureDecisions.Pop();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scopes.Count == 0)
|
||||||
|
{
|
||||||
|
int threadId = state.ThreadId;
|
||||||
|
ScopeNode rootScopeNode = RentNode(GetThreadRootLabel(threadId), DefaultCategory, 0, threadId, null);
|
||||||
|
scopes.Push(rootScopeNode);
|
||||||
|
frame.AddRootNode(rootScopeNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopeNode newScope = scopes.Peek().AddChild(label, category, tagMask);
|
||||||
|
scopes.Push(newScope);
|
||||||
|
newScope.NoteSetupOverhead();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Conditional("PROFILING")]
|
||||||
|
public static void EndSample()
|
||||||
|
{
|
||||||
|
if (!IsRecording || currentFrame == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadProfilerState state = threadStates.Value;
|
||||||
|
if (state.CaptureDecisions.Count == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool captured = state.CaptureDecisions.Pop();
|
||||||
|
if (!captured)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Stack<ScopeNode> scopes = state.Scopes;
|
||||||
|
|
||||||
|
if (scopes.Count > 1)
|
||||||
|
{
|
||||||
|
ScopeNode currentScope = scopes.Pop();
|
||||||
|
currentScope.End();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool ShouldSample(ThreadProfilerState state)
|
||||||
|
{
|
||||||
|
int stride = Math.Max(1, SamplingStride);
|
||||||
|
state.SampleCursor++;
|
||||||
|
return state.SampleCursor % stride == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetThreadRootLabel(int threadId)
|
||||||
|
{
|
||||||
|
return threadRootLabelCache.GetOrAdd(threadId, static id => $"Thread-{id}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Frame RentFrame(uint count)
|
||||||
|
{
|
||||||
|
if (!framePool.TryTake(out Frame frame))
|
||||||
|
{
|
||||||
|
frame = new Frame();
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.Reset(count);
|
||||||
|
return frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ScopeNode RentNode(string label, string category, ulong tagMask, int managedThreadId, ScopeNode parent)
|
||||||
|
{
|
||||||
|
if (!nodePool.TryTake(out ScopeNode node))
|
||||||
|
{
|
||||||
|
node = new ScopeNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
node.Reset(label, category, tagMask, managedThreadId, parent);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ReturnNodeTree(ScopeNode node)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
ReturnNodeTree(node.Children[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
node.Reset(string.Empty, DefaultCategory, 0, 0, null);
|
||||||
|
nodePool.Add(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void FinalizeCurrentFrame()
|
||||||
|
{
|
||||||
|
Frame frame = currentFrame;
|
||||||
|
if (frame == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lock (registeredThreadStatesLock)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < registeredThreadStates.Count; i++)
|
||||||
|
{
|
||||||
|
ThreadProfilerState state = registeredThreadStates[i];
|
||||||
|
Stack<ScopeNode> scopes = state.Scopes;
|
||||||
|
while (scopes.Count > 0)
|
||||||
|
{
|
||||||
|
scopes.Pop().End();
|
||||||
|
}
|
||||||
|
|
||||||
|
state.CaptureDecisions.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.End(FrameBudgetMilliseconds);
|
||||||
|
|
||||||
|
if (Frames.Enqueue(frame, out Frame evictedFrame))
|
||||||
|
{
|
||||||
|
for (int i = 0; i < evictedFrame.RootNodes.Count; i++)
|
||||||
|
{
|
||||||
|
ReturnNodeTree(evictedFrame.RootNodes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
framePool.Add(evictedFrame);
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateRollingWindows(frame);
|
||||||
|
UpdateRollingThreadWindows(frame);
|
||||||
|
frameCount++;
|
||||||
|
currentFrame = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void UpdateRollingWindows(Frame frame)
|
||||||
|
{
|
||||||
|
lock (rollingWindowsLock)
|
||||||
|
{
|
||||||
|
foreach (KeyValuePair<string, LabelMetrics> pair in frame.LabelMetricsRaw)
|
||||||
|
{
|
||||||
|
if (!rollingWindows.TryGetValue(pair.Key, out RollingWindow window))
|
||||||
|
{
|
||||||
|
window = new RollingWindow(rollingWindowSize);
|
||||||
|
rollingWindows.Add(pair.Key, window);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.Add(pair.Value.InclusiveMs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void UpdateRollingThreadWindows(Frame frame)
|
||||||
|
{
|
||||||
|
lock (rollingWindowsLock)
|
||||||
|
{
|
||||||
|
foreach (KeyValuePair<int, ThreadMetrics> pair in frame.ThreadMetricsRaw)
|
||||||
|
{
|
||||||
|
if (!rollingThreadWindows.TryGetValue(pair.Key, out RollingThreadWindow window))
|
||||||
|
{
|
||||||
|
window = new RollingThreadWindow(rollingWindowSize);
|
||||||
|
rollingThreadWindows.Add(pair.Key, window);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.Add(pair.Value.InclusiveMs, pair.Value.OverBudget);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,556 @@
|
|||||||
|
using ImGuiNET;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime;
|
||||||
|
|
||||||
|
public static class ProfilerVisualizer
|
||||||
|
{
|
||||||
|
public sealed class TimelineState
|
||||||
|
{
|
||||||
|
public int SelectedFrameIndex = -1;
|
||||||
|
public int WindowStartIndex = 0;
|
||||||
|
public int VisibleFrameCount = 64;
|
||||||
|
public bool FollowLatest = true;
|
||||||
|
public float Zoom = 1f;
|
||||||
|
public double PanTicks = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly struct TimelineRenderResult
|
||||||
|
{
|
||||||
|
public TimelineRenderResult(int selectedFrameIndex, bool selectionChanged, bool userNavigated)
|
||||||
|
{
|
||||||
|
SelectedFrameIndex = selectedFrameIndex;
|
||||||
|
SelectionChanged = selectionChanged;
|
||||||
|
UserNavigated = userNavigated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int SelectedFrameIndex { get; }
|
||||||
|
public bool SelectionChanged { get; }
|
||||||
|
public bool UserNavigated { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private readonly struct HoverEntry
|
||||||
|
{
|
||||||
|
public HoverEntry(Profiler.ScopeNode node, Profiler.Frame frame, int frameIndex, int depth, double timelineStartTicks)
|
||||||
|
{
|
||||||
|
Node = node;
|
||||||
|
Frame = frame;
|
||||||
|
FrameIndex = frameIndex;
|
||||||
|
Depth = depth;
|
||||||
|
DurationMs = TicksToMilliseconds(node.EndTime - node.StartTime);
|
||||||
|
SelfMs = node.SelfMilliseconds();
|
||||||
|
StartInFrameMs = TicksToMilliseconds(node.StartTime - frame.StartTime);
|
||||||
|
EndInFrameMs = TicksToMilliseconds(node.EndTime - frame.StartTime);
|
||||||
|
StartInTimelineMs = TicksToMilliseconds(node.StartTime - timelineStartTicks);
|
||||||
|
EndInTimelineMs = TicksToMilliseconds(node.EndTime - timelineStartTicks);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Profiler.ScopeNode Node { get; }
|
||||||
|
public Profiler.Frame Frame { get; }
|
||||||
|
public int FrameIndex { get; }
|
||||||
|
public int Depth { get; }
|
||||||
|
public double DurationMs { get; }
|
||||||
|
public double SelfMs { get; }
|
||||||
|
public double StartInFrameMs { get; }
|
||||||
|
public double EndInFrameMs { get; }
|
||||||
|
public double StartInTimelineMs { get; }
|
||||||
|
public double EndInTimelineMs { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private const float BarHeight = 18f;
|
||||||
|
private const float BarPadding = 2f;
|
||||||
|
private const float ThreadGap = 8f;
|
||||||
|
private const float HeaderHeight = 28f;
|
||||||
|
private const float TimelineHeight = 220f;
|
||||||
|
private const float MinTextWidth = 36f;
|
||||||
|
private static readonly double TickToMs = 1000d / System.Diagnostics.Stopwatch.Frequency;
|
||||||
|
|
||||||
|
// Backwards-compatible entry point used by existing call sites.
|
||||||
|
public static void RenderFlameGraph(Profiler.Frame frame)
|
||||||
|
{
|
||||||
|
if (frame == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Profiler.Frame> frames = new List<Profiler.Frame>(1)
|
||||||
|
{
|
||||||
|
frame
|
||||||
|
};
|
||||||
|
|
||||||
|
TimelineState state = new TimelineState
|
||||||
|
{
|
||||||
|
VisibleFrameCount = 1,
|
||||||
|
SelectedFrameIndex = 0,
|
||||||
|
FollowLatest = true
|
||||||
|
};
|
||||||
|
|
||||||
|
RenderTimeline(frames, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TimelineRenderResult RenderTimeline(IReadOnlyList<Profiler.Frame> frames, TimelineState state)
|
||||||
|
{
|
||||||
|
if (frames == null || frames.Count == 0 || state == null)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool selectionChanged = false;
|
||||||
|
bool userNavigated = false;
|
||||||
|
|
||||||
|
int frameCount = frames.Count;
|
||||||
|
state.VisibleFrameCount = Math.Clamp(state.VisibleFrameCount, 1, frameCount);
|
||||||
|
state.Zoom = Math.Clamp(state.Zoom, 1f, 128f);
|
||||||
|
|
||||||
|
int maxStartIndex = Math.Max(0, frameCount - state.VisibleFrameCount);
|
||||||
|
if (state.FollowLatest)
|
||||||
|
{
|
||||||
|
state.WindowStartIndex = maxStartIndex;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state.WindowStartIndex = Math.Clamp(state.WindowStartIndex, 0, maxStartIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
int visibleStartIndex = state.WindowStartIndex;
|
||||||
|
int visibleEndIndex = visibleStartIndex + state.VisibleFrameCount - 1;
|
||||||
|
|
||||||
|
if (state.SelectedFrameIndex < 0)
|
||||||
|
{
|
||||||
|
state.SelectedFrameIndex = visibleEndIndex;
|
||||||
|
selectionChanged = true;
|
||||||
|
}
|
||||||
|
state.SelectedFrameIndex = Math.Clamp(state.SelectedFrameIndex, visibleStartIndex, visibleEndIndex);
|
||||||
|
|
||||||
|
Profiler.Frame firstFrame = frames[visibleStartIndex];
|
||||||
|
Profiler.Frame lastFrame = frames[visibleEndIndex];
|
||||||
|
|
||||||
|
double timelineStartTicks = firstFrame.StartTime;
|
||||||
|
double timelineEndTicks = Math.Max(lastFrame.EndTime, firstFrame.StartTime + 1);
|
||||||
|
double timelineDurationTicks = Math.Max(1d, timelineEndTicks - timelineStartTicks);
|
||||||
|
|
||||||
|
double visibleDurationTicks = Math.Max(1d, timelineDurationTicks / state.Zoom);
|
||||||
|
double maxPanTicks = Math.Max(0d, timelineDurationTicks - visibleDurationTicks);
|
||||||
|
if (state.FollowLatest)
|
||||||
|
{
|
||||||
|
state.PanTicks = maxPanTicks;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
state.PanTicks = Math.Clamp(state.PanTicks, 0d, maxPanTicks);
|
||||||
|
}
|
||||||
|
|
||||||
|
double visibleStartTicks = timelineStartTicks + state.PanTicks;
|
||||||
|
double visibleEndTicks = visibleStartTicks + visibleDurationTicks;
|
||||||
|
|
||||||
|
Dictionary<int, int> threadDepths = BuildThreadDepths(frames, visibleStartIndex, visibleEndIndex);
|
||||||
|
List<int> threadOrder = threadDepths.Keys.OrderBy(x => x).ToList();
|
||||||
|
|
||||||
|
Dictionary<int, float> threadBaseY = new Dictionary<int, float>(threadOrder.Count);
|
||||||
|
float yCursor = HeaderHeight;
|
||||||
|
for (int i = 0; i < threadOrder.Count; i++)
|
||||||
|
{
|
||||||
|
int threadId = threadOrder[i];
|
||||||
|
threadBaseY[threadId] = yCursor;
|
||||||
|
yCursor += ((threadDepths[threadId] + 1) * (BarHeight + BarPadding)) + ThreadGap;
|
||||||
|
}
|
||||||
|
|
||||||
|
float contentHeight = Math.Max(TimelineHeight, yCursor + 6f);
|
||||||
|
|
||||||
|
ImGui.BeginChild("ProfilerTimeline", new Vector2(0, TimelineHeight), ImGuiChildFlags.Border | ImGuiChildFlags.ResizeY, ImGuiWindowFlags.AlwaysVerticalScrollbar);
|
||||||
|
|
||||||
|
ImDrawListPtr drawList = ImGui.GetWindowDrawList();
|
||||||
|
Vector2 origin = ImGui.GetCursorScreenPos();
|
||||||
|
Vector2 viewSize = ImGui.GetContentRegionAvail();
|
||||||
|
float canvasWidth = Math.Max(1f, viewSize.X);
|
||||||
|
|
||||||
|
uint frameBgColor = ImGui.GetColorU32(ImGuiCol.FrameBg);
|
||||||
|
uint frameBgHoveredColor = ImGui.GetColorU32(ImGuiCol.FrameBgHovered);
|
||||||
|
uint headerColor = ImGui.GetColorU32(ImGuiCol.Header);
|
||||||
|
uint headerHoveredColor = ImGui.GetColorU32(ImGuiCol.HeaderHovered);
|
||||||
|
uint textColor = ImGui.GetColorU32(ImGuiCol.Text);
|
||||||
|
uint borderColor = ImGui.GetColorU32(ImGuiCol.Border);
|
||||||
|
|
||||||
|
float clipMinX = origin.X;
|
||||||
|
float clipMaxX = origin.X + canvasWidth;
|
||||||
|
float clipMinY = origin.Y;
|
||||||
|
float clipMaxY = origin.Y + Math.Max(1f, ImGui.GetWindowHeight());
|
||||||
|
|
||||||
|
DrawTimelineHeader(drawList, origin, canvasWidth, timelineStartTicks, visibleStartTicks, visibleDurationTicks, textColor, borderColor);
|
||||||
|
|
||||||
|
HoverEntry? hovered = null;
|
||||||
|
|
||||||
|
for (int frameIndex = visibleStartIndex; frameIndex <= visibleEndIndex; frameIndex++)
|
||||||
|
{
|
||||||
|
Profiler.Frame frame = frames[frameIndex];
|
||||||
|
float frameStartX = ToScreenX(frame.StartTime, visibleStartTicks, visibleDurationTicks, origin.X, canvasWidth);
|
||||||
|
float frameEndX = ToScreenX(frame.EndTime, visibleStartTicks, visibleDurationTicks, origin.X, canvasWidth);
|
||||||
|
|
||||||
|
if (frameEndX < clipMinX || frameStartX > clipMaxX)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isSelectedFrame = frameIndex == state.SelectedFrameIndex;
|
||||||
|
uint frameShadeColor = (frameIndex & 1) == 0 ? frameBgColor : frameBgHoveredColor;
|
||||||
|
if (isSelectedFrame)
|
||||||
|
{
|
||||||
|
frameShadeColor = LerpColor(headerColor, headerHoveredColor, 0.45f);
|
||||||
|
}
|
||||||
|
|
||||||
|
drawList.AddRectFilled(new Vector2(frameStartX, origin.Y + HeaderHeight), new Vector2(frameEndX, origin.Y + contentHeight), SetAlpha(frameShadeColor, isSelectedFrame ? 0.20f : 0.08f));
|
||||||
|
drawList.AddLine(new Vector2(frameStartX, origin.Y + HeaderHeight), new Vector2(frameStartX, origin.Y + contentHeight), SetAlpha(borderColor, 0.55f), 1f);
|
||||||
|
|
||||||
|
foreach (Profiler.ScopeNode root in frame.RootNodes)
|
||||||
|
{
|
||||||
|
if (!threadBaseY.TryGetValue(root.ManagedThreadId, out float baseY))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawThreadLabel(drawList, origin.X, origin.Y + baseY, root.ManagedThreadId, textColor);
|
||||||
|
for (int i = 0; i < root.Children.Count; i++)
|
||||||
|
{
|
||||||
|
RenderNode(
|
||||||
|
drawList,
|
||||||
|
root.Children[i],
|
||||||
|
frame,
|
||||||
|
frameIndex,
|
||||||
|
baseY,
|
||||||
|
0,
|
||||||
|
visibleStartTicks,
|
||||||
|
visibleDurationTicks,
|
||||||
|
origin.X,
|
||||||
|
origin.Y,
|
||||||
|
canvasWidth,
|
||||||
|
clipMinX,
|
||||||
|
clipMaxX,
|
||||||
|
clipMaxY,
|
||||||
|
ref hovered,
|
||||||
|
textColor,
|
||||||
|
headerColor,
|
||||||
|
headerHoveredColor,
|
||||||
|
frameIndex == state.SelectedFrameIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drawList.AddLine(new Vector2(frameEndX, origin.Y + HeaderHeight), new Vector2(frameEndX, origin.Y + contentHeight), SetAlpha(borderColor, 0.30f), 1f);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.Dummy(new Vector2(canvasWidth, contentHeight));
|
||||||
|
|
||||||
|
bool windowHovered = ImGui.IsWindowHovered(ImGuiHoveredFlags.ChildWindows);
|
||||||
|
if (windowHovered)
|
||||||
|
{
|
||||||
|
HandleZoomAndPan(state, timelineStartTicks, timelineDurationTicks, visibleDurationTicks, origin.X, canvasWidth, ref userNavigated);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (windowHovered && ImGui.IsMouseClicked(ImGuiMouseButton.Left))
|
||||||
|
{
|
||||||
|
int clickedFrame = FindFrameIndexByMouseX(frames, visibleStartIndex, visibleEndIndex, visibleStartTicks, visibleDurationTicks, origin.X, canvasWidth, ImGui.GetMousePos().X);
|
||||||
|
if (clickedFrame >= visibleStartIndex && clickedFrame <= visibleEndIndex && clickedFrame != state.SelectedFrameIndex)
|
||||||
|
{
|
||||||
|
state.SelectedFrameIndex = clickedFrame;
|
||||||
|
selectionChanged = true;
|
||||||
|
state.FollowLatest = false;
|
||||||
|
userNavigated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hovered.HasValue)
|
||||||
|
{
|
||||||
|
if (ImGui.IsMouseClicked(ImGuiMouseButton.Left) && hovered.Value.FrameIndex != state.SelectedFrameIndex)
|
||||||
|
{
|
||||||
|
state.SelectedFrameIndex = hovered.Value.FrameIndex;
|
||||||
|
selectionChanged = true;
|
||||||
|
state.FollowLatest = false;
|
||||||
|
userNavigated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
DrawTooltip(hovered.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.EndChild();
|
||||||
|
|
||||||
|
return new TimelineRenderResult(state.SelectedFrameIndex, selectionChanged, userNavigated);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void HandleZoomAndPan(TimelineState state, double timelineStartTicks, double timelineDurationTicks, double visibleDurationTicks, float originX, float canvasWidth, ref bool userNavigated)
|
||||||
|
{
|
||||||
|
ImGuiIOPtr io = ImGui.GetIO();
|
||||||
|
if (Math.Abs(io.MouseWheel) < float.Epsilon)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!io.KeyCtrl && !io.KeyShift)
|
||||||
|
{
|
||||||
|
return; // plain scroll goes to ImGui vertical scrolling
|
||||||
|
}
|
||||||
|
|
||||||
|
float wheel = io.MouseWheel;
|
||||||
|
io.MouseWheel = 0; // consume so the child window doesn't also scroll vertically
|
||||||
|
|
||||||
|
if (io.KeyCtrl)
|
||||||
|
{
|
||||||
|
float previousZoom = state.Zoom;
|
||||||
|
double visibleStartTicksBefore = timelineStartTicks + state.PanTicks;
|
||||||
|
double mouseT = Math.Clamp((ImGui.GetMousePos().X - originX) / Math.Max(1f, canvasWidth), 0f, 1f);
|
||||||
|
double pivotTick = visibleStartTicksBefore + (visibleDurationTicks * mouseT);
|
||||||
|
|
||||||
|
state.Zoom = Math.Clamp(state.Zoom * MathF.Pow(1.12f, wheel), 1f, 128f);
|
||||||
|
if (Math.Abs(previousZoom - state.Zoom) > float.Epsilon)
|
||||||
|
{
|
||||||
|
double newVisibleDurationTicks = Math.Max(1d, timelineDurationTicks / state.Zoom);
|
||||||
|
double newVisibleStartTicks = pivotTick - (newVisibleDurationTicks * mouseT);
|
||||||
|
state.PanTicks = Math.Clamp(newVisibleStartTicks - timelineStartTicks, 0d, Math.Max(0d, timelineDurationTicks - newVisibleDurationTicks));
|
||||||
|
state.FollowLatest = false;
|
||||||
|
userNavigated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Shift + scroll: horizontal pan
|
||||||
|
state.PanTicks -= wheel * (visibleDurationTicks * 0.10d);
|
||||||
|
state.FollowLatest = false;
|
||||||
|
userNavigated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int FindFrameIndexByMouseX(IReadOnlyList<Profiler.Frame> frames, int visibleStartIndex, int visibleEndIndex, double visibleStartTicks, double visibleDurationTicks, float originX, float width, float mouseX)
|
||||||
|
{
|
||||||
|
double t = Math.Clamp((mouseX - originX) / Math.Max(1f, width), 0f, 1f);
|
||||||
|
double timelineTicks = visibleStartTicks + (visibleDurationTicks * t);
|
||||||
|
|
||||||
|
for (int i = visibleStartIndex; i <= visibleEndIndex; i++)
|
||||||
|
{
|
||||||
|
Profiler.Frame frame = frames[i];
|
||||||
|
if (timelineTicks >= frame.StartTime && timelineTicks <= frame.EndTime)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawTimelineHeader(ImDrawListPtr drawList, Vector2 origin, float width, double timelineStartTicks, double visibleStartTicks, double visibleDurationTicks, uint textColor, uint borderColor)
|
||||||
|
{
|
||||||
|
drawList.AddLine(new Vector2(origin.X, origin.Y + HeaderHeight), new Vector2(origin.X + width, origin.Y + HeaderHeight), SetAlpha(borderColor, 0.65f), 1f);
|
||||||
|
|
||||||
|
int tickCount = Math.Clamp((int)(width / 130f), 4, 20);
|
||||||
|
for (int i = 0; i <= tickCount; i++)
|
||||||
|
{
|
||||||
|
float t = i / (float)tickCount;
|
||||||
|
float x = origin.X + (t * width);
|
||||||
|
drawList.AddLine(new Vector2(x, origin.Y + HeaderHeight - 8f), new Vector2(x, origin.Y + HeaderHeight), SetAlpha(borderColor, 0.8f), 1f);
|
||||||
|
|
||||||
|
double ms = TicksToMilliseconds((visibleStartTicks - timelineStartTicks) + (visibleDurationTicks * t));
|
||||||
|
drawList.AddText(new Vector2(x + 2f, origin.Y + 4f), textColor, $"+{ms:0.0} ms");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawThreadLabel(ImDrawListPtr drawList, float x, float y, int threadId, uint textColor)
|
||||||
|
{
|
||||||
|
drawList.AddText(new Vector2(x + 4f, y + 2f), SetAlpha(textColor, 0.85f), $"T{threadId}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawTooltip(HoverEntry hover)
|
||||||
|
{
|
||||||
|
ImGui.BeginTooltip();
|
||||||
|
ImGui.Text($"{hover.Node.Label}");
|
||||||
|
ImGui.Separator();
|
||||||
|
ImGui.Text($"Category: {hover.Node.Category}");
|
||||||
|
ImGui.Text($"Tags: 0x{hover.Node.TagMask:X}");
|
||||||
|
ImGui.Text($"Frame: {hover.Frame.FrameCount} (idx {hover.FrameIndex})");
|
||||||
|
ImGui.Text($"Thread: {hover.Node.ManagedThreadId}");
|
||||||
|
ImGui.Text($"Depth: {hover.Depth}");
|
||||||
|
ImGui.Text($"Duration: {hover.DurationMs:0.000} ms");
|
||||||
|
ImGui.Text($"Self: {hover.SelfMs:0.000} ms");
|
||||||
|
ImGui.Text($"Frame Start: {hover.StartInFrameMs:0.000} ms");
|
||||||
|
ImGui.Text($"Frame End: {hover.EndInFrameMs:0.000} ms");
|
||||||
|
ImGui.Text($"Timeline Start: {hover.StartInTimelineMs:0.000} ms");
|
||||||
|
ImGui.Text($"Timeline End: {hover.EndInTimelineMs:0.000} ms");
|
||||||
|
ImGui.Text($"Children: {hover.Node.Children.Count}");
|
||||||
|
if (hover.Node.ProfilerSetupBytes > 0)
|
||||||
|
{
|
||||||
|
ImGui.Separator();
|
||||||
|
ImGui.TextColored(new Vector4(1f, 0.75f, 0f, 1f), $"\u26a0 {hover.Node.ProfilerSetupBytes} B of alloc is profiler warmup overhead");
|
||||||
|
}
|
||||||
|
ImGui.EndTooltip();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RenderNode(
|
||||||
|
ImDrawListPtr drawList,
|
||||||
|
Profiler.ScopeNode node,
|
||||||
|
Profiler.Frame frame,
|
||||||
|
int frameIndex,
|
||||||
|
float baseY,
|
||||||
|
int depth,
|
||||||
|
double visibleStartTicks,
|
||||||
|
double visibleDurationTicks,
|
||||||
|
float originX,
|
||||||
|
float originY,
|
||||||
|
float width,
|
||||||
|
float clipMinX,
|
||||||
|
float clipMaxX,
|
||||||
|
float clipMaxY,
|
||||||
|
ref HoverEntry? hovered,
|
||||||
|
uint textColor,
|
||||||
|
uint headerColor,
|
||||||
|
uint headerHoveredColor,
|
||||||
|
bool selectedFrame)
|
||||||
|
{
|
||||||
|
long nodeEndTime = Math.Max(node.EndTime, node.StartTime + 1);
|
||||||
|
if (nodeEndTime < visibleStartTicks || node.StartTime > visibleStartTicks + visibleDurationTicks)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float y = originY + baseY + (depth * (BarHeight + BarPadding));
|
||||||
|
if (y > clipMaxY)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float minX = ToScreenX(node.StartTime, visibleStartTicks, visibleDurationTicks, originX, width);
|
||||||
|
float maxX = ToScreenX(nodeEndTime, visibleStartTicks, visibleDurationTicks, originX, width);
|
||||||
|
if (maxX < clipMinX || minX > clipMaxX)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
float barWidth = Math.Max(1f, maxX - minX);
|
||||||
|
Vector2 min = new Vector2(minX, y + (BarPadding * 0.5f));
|
||||||
|
Vector2 max = new Vector2(minX + barWidth, y + (BarPadding * 0.5f) + BarHeight);
|
||||||
|
|
||||||
|
uint barColor = BuildBarColor(node.Label, depth, selectedFrame);
|
||||||
|
uint borderColor = LerpColor(headerColor, headerHoveredColor, 0.45f);
|
||||||
|
drawList.AddRectFilled(min, max, barColor, 3f);
|
||||||
|
drawList.AddRect(min, max, SetAlpha(borderColor, 0.55f), 3f, ImDrawFlags.None, 1f);
|
||||||
|
|
||||||
|
Vector2 mousePos = ImGui.GetMousePos();
|
||||||
|
bool isHovered = mousePos.X >= min.X && mousePos.X <= max.X && mousePos.Y >= min.Y && mousePos.Y <= max.Y;
|
||||||
|
if (isHovered)
|
||||||
|
{
|
||||||
|
hovered = new HoverEntry(node, frame, frameIndex, depth, visibleStartTicks);
|
||||||
|
drawList.AddRect(min, max, headerHoveredColor, 3f, ImDrawFlags.None, 1.5f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (barWidth > MinTextWidth)
|
||||||
|
{
|
||||||
|
string label = node.Label;
|
||||||
|
float textWidth = ImGui.CalcTextSize(label).X;
|
||||||
|
if (textWidth + 8f <= barWidth)
|
||||||
|
{
|
||||||
|
drawList.AddText(new Vector2(min.X + 4f, min.Y + 2f), textColor, label);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
RenderNode(
|
||||||
|
drawList,
|
||||||
|
node.Children[i],
|
||||||
|
frame,
|
||||||
|
frameIndex,
|
||||||
|
baseY,
|
||||||
|
depth + 1,
|
||||||
|
visibleStartTicks,
|
||||||
|
visibleDurationTicks,
|
||||||
|
originX,
|
||||||
|
originY,
|
||||||
|
width,
|
||||||
|
clipMinX,
|
||||||
|
clipMaxX,
|
||||||
|
clipMaxY,
|
||||||
|
ref hovered,
|
||||||
|
textColor,
|
||||||
|
headerColor,
|
||||||
|
headerHoveredColor,
|
||||||
|
selectedFrame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<int, int> BuildThreadDepths(IReadOnlyList<Profiler.Frame> frames, int startIndex, int endIndex)
|
||||||
|
{
|
||||||
|
Dictionary<int, int> threadMaxDepths = new Dictionary<int, int>(8);
|
||||||
|
for (int frameIndex = startIndex; frameIndex <= endIndex; frameIndex++)
|
||||||
|
{
|
||||||
|
foreach (Profiler.ScopeNode root in frames[frameIndex].RootNodes)
|
||||||
|
{
|
||||||
|
int maxDepth = 0;
|
||||||
|
for (int i = 0; i < root.Children.Count; i++)
|
||||||
|
{
|
||||||
|
maxDepth = Math.Max(maxDepth, GetMaxDepth(root.Children[i], 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threadMaxDepths.TryGetValue(root.ManagedThreadId, out int currentMax))
|
||||||
|
{
|
||||||
|
if (maxDepth > currentMax)
|
||||||
|
{
|
||||||
|
threadMaxDepths[root.ManagedThreadId] = maxDepth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
threadMaxDepths[root.ManagedThreadId] = maxDepth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return threadMaxDepths;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int GetMaxDepth(Profiler.ScopeNode node, int depth)
|
||||||
|
{
|
||||||
|
if (node.Children.Count == 0)
|
||||||
|
{
|
||||||
|
return depth;
|
||||||
|
}
|
||||||
|
|
||||||
|
int maxDepth = depth;
|
||||||
|
for (int i = 0; i < node.Children.Count; i++)
|
||||||
|
{
|
||||||
|
maxDepth = Math.Max(maxDepth, GetMaxDepth(node.Children[i], depth + 1));
|
||||||
|
}
|
||||||
|
return maxDepth;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static uint BuildBarColor(string label, int depth, bool selectedFrame)
|
||||||
|
{
|
||||||
|
int hash = label.GetHashCode();
|
||||||
|
float hue = ((hash & 1023) / 1023f + (depth * 0.031f)) % 1f;
|
||||||
|
ImGui.ColorConvertHSVtoRGB(hue, 0.52f, selectedFrame ? 0.82f : 0.68f, out float r, out float g, out float b);
|
||||||
|
|
||||||
|
Vector4 frameBg = ImGui.ColorConvertU32ToFloat4(ImGui.GetColorU32(ImGuiCol.FrameBg));
|
||||||
|
Vector4 accent = new Vector4(r, g, b, 1f);
|
||||||
|
Vector4 mixed = Vector4.Lerp(frameBg, accent, 0.72f);
|
||||||
|
return ImGui.ColorConvertFloat4ToU32(mixed);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float ToScreenX(double ticks, double visibleStartTicks, double visibleDurationTicks, float startX, float width)
|
||||||
|
{
|
||||||
|
double normalized = (ticks - visibleStartTicks) / visibleDurationTicks;
|
||||||
|
return startX + (float)(normalized * width);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double TicksToMilliseconds(double ticks)
|
||||||
|
{
|
||||||
|
return ticks * TickToMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static uint SetAlpha(uint color, float alpha)
|
||||||
|
{
|
||||||
|
Vector4 c = ImGui.ColorConvertU32ToFloat4(color);
|
||||||
|
c.W *= alpha;
|
||||||
|
return ImGui.ColorConvertFloat4ToU32(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static uint LerpColor(uint a, uint b, float t)
|
||||||
|
{
|
||||||
|
Vector4 av = ImGui.ColorConvertU32ToFloat4(a);
|
||||||
|
Vector4 bv = ImGui.ColorConvertU32ToFloat4(b);
|
||||||
|
return ImGui.ColorConvertFloat4ToU32(Vector4.Lerp(av, bv, Math.Clamp(t, 0f, 1f)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
using ImGuiNET;
|
||||||
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Runtime.Components;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Serialization;
|
||||||
|
|
||||||
|
public static class ComponentHelper
|
||||||
|
{
|
||||||
|
// Auto generate this.
|
||||||
|
public static readonly Dictionary<Type, Func<World, Entity, ValueType>> GetComponentByType = new()
|
||||||
|
{
|
||||||
|
{ typeof(LocalTransform), (world, entity) => world.Get<LocalTransform>(entity) },
|
||||||
|
{ typeof(Root), (world, entity) => world.Get<Root>(entity) },
|
||||||
|
};
|
||||||
|
|
||||||
|
// Auto generate this.
|
||||||
|
public static readonly Dictionary<Type, Action<World, Entity, ValueType>> SetComponentByType = new()
|
||||||
|
{
|
||||||
|
{ typeof(LocalTransform), (world, entity, component) => world.Set(entity, (LocalTransform)component) },
|
||||||
|
{ typeof(Root), (world, entity, component) => world.Set(entity, (Root)component) },
|
||||||
|
};
|
||||||
|
|
||||||
|
// Auto generate this, but it should only contain user assignable components (so something like 'root' should be excluded).
|
||||||
|
// Maybe use an attribute for this.
|
||||||
|
public static readonly Dictionary<Type, Action<World, Entity>> AddComponentByType = new()
|
||||||
|
{
|
||||||
|
{ typeof(LocalTransform), (world, entity) => world.Set(entity, LocalTransform.Identity) },
|
||||||
|
};
|
||||||
|
|
||||||
|
// Auto generate this, but also keep the option for 'custom inspectors'.
|
||||||
|
// Maybe via attribute?
|
||||||
|
public static readonly Dictionary<Type, Action<World, Entity>> ComponentInspectorByType = new()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
typeof(LocalTransform), (world, entity) =>
|
||||||
|
{
|
||||||
|
(Vector3 position, Quaternion rotation, Vector3 scale) = world.Get<LocalTransform>(entity);
|
||||||
|
Vector3 eulerAngles = MathEx.ToEulerAngles(rotation);
|
||||||
|
eulerAngles = new Vector3(float.RadiansToDegrees(eulerAngles.X), float.RadiansToDegrees(eulerAngles.Y), float.RadiansToDegrees(eulerAngles.Z));
|
||||||
|
bool isDirty = false;
|
||||||
|
|
||||||
|
ImGui.BeginGroup();
|
||||||
|
ImGui.Text($"{nameof(LocalTransform)}");
|
||||||
|
isDirty |= ImGui.DragFloat3("Position", ref position, 0.2f, float.MinValue, float.MaxValue /*, "%f0 m" */); // TODO: right format.
|
||||||
|
isDirty |= ImGui.DragFloat3("Rotation", ref eulerAngles);
|
||||||
|
isDirty |= ImGui.DragFloat3("Scale", ref scale);
|
||||||
|
ImGui.EndGroup();
|
||||||
|
|
||||||
|
if (!isDirty)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
eulerAngles = new Vector3(float.DegreesToRadians(eulerAngles.X), float.DegreesToRadians(eulerAngles.Y), float.DegreesToRadians(eulerAngles.Z));
|
||||||
|
world.Set(entity, new LocalTransform(position, MathEx.ToQuaternion(eulerAngles), scale));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
typeof(Root), (world, entity) =>
|
||||||
|
{
|
||||||
|
ImGui.BeginGroup();
|
||||||
|
ImGui.Text($"{nameof(Root)}");
|
||||||
|
ImGui.EndGroup();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Runtime.Components;
|
||||||
|
using Nerfed.Runtime.Util;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// Explore if having a WorldTransform and LocalTransfom component each holding position, rotation, scale values and the matricies is useful.
|
||||||
|
// Often you need to either get or set these values.
|
||||||
|
// If so, we probably need a utility funciton to do so. Since changing these values means that we need to update all the related data + children as well.
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// When modifying transform all the children need to be updated as well.
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Systems
|
||||||
|
{
|
||||||
|
public class LocalToWorldSystem : MoonTools.ECS.System
|
||||||
|
{
|
||||||
|
private readonly JobSystem jobs;
|
||||||
|
private readonly Filter rootEntitiesFilter;
|
||||||
|
private readonly Filter entitiesWithoutLocalToWorldFilter;
|
||||||
|
private readonly Action<int> updateWorldTransformByIndex;
|
||||||
|
|
||||||
|
public LocalToWorldSystem(World world, JobSystem jobs = null) : base(world)
|
||||||
|
{
|
||||||
|
this.jobs = jobs ?? JobSystem.Default;
|
||||||
|
rootEntitiesFilter = FilterBuilder.Include<LocalTransform>().Exclude<Child>().Build();
|
||||||
|
entitiesWithoutLocalToWorldFilter = FilterBuilder.Include<LocalTransform>().Exclude<LocalToWorld>().Build();
|
||||||
|
updateWorldTransformByIndex = UpdateWorldTransformByIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(TimeSpan delta)
|
||||||
|
{
|
||||||
|
if (rootEntitiesFilter.Empty)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.jobs.WorkerCount > 0)
|
||||||
|
{
|
||||||
|
Profiler.BeginSample("LocalToWorldCheck");
|
||||||
|
// Structural pre-pass: ensure LocalToWorld exists on all entities before parallel writes.
|
||||||
|
foreach (Entity entity in entitiesWithoutLocalToWorldFilter.Entities)
|
||||||
|
{
|
||||||
|
Set(entity, new LocalToWorld(Matrix4x4.Identity));
|
||||||
|
}
|
||||||
|
Profiler.EndSample();
|
||||||
|
|
||||||
|
Profiler.BeginSample("LocalToWorldUpdate");
|
||||||
|
this.jobs.Dispatch(rootEntitiesFilter.Count, updateWorldTransformByIndex);
|
||||||
|
Profiler.EndSample();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (Entity entity in rootEntitiesFilter.Entities)
|
||||||
|
{
|
||||||
|
Profiler.BeginSample("UpdateWorldTransform");
|
||||||
|
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
||||||
|
Profiler.EndSample();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateWorldTransformByIndex(int entityFilterIndex)
|
||||||
|
{
|
||||||
|
using ProfilerScope scope = new("UpdateWorldTransformByIndex");
|
||||||
|
Entity entity = rootEntitiesFilter.NthEntity(entityFilterIndex);
|
||||||
|
UpdateWorldTransform(entity, Matrix4x4.Identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateWorldTransform(in Entity entity, Matrix4x4 localToWorldMatrix)
|
||||||
|
{
|
||||||
|
if (Has<LocalTransform>(entity))
|
||||||
|
{
|
||||||
|
LocalTransform localTransform = Get<LocalTransform>(entity);
|
||||||
|
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||||
|
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||||
|
#if DEBUG
|
||||||
|
if (!Has<LocalToWorld>(entity))
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException(
|
||||||
|
$"Entity {entity} is missing LocalToWorld. Ensure the structural pre-pass runs before parallel dispatch.");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Set(entity, localToWorld);
|
||||||
|
}
|
||||||
|
|
||||||
|
ReverseSpanEnumerator<Entity> childEntities = World.InRelations<ChildParentRelation>(entity);
|
||||||
|
foreach (Entity childEntity in childEntities)
|
||||||
|
{
|
||||||
|
UpdateWorldTransform(childEntity, localToWorldMatrix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime;
|
||||||
|
|
||||||
|
public class BoundedQueue<T> : IEnumerable<T>, ICollection, IReadOnlyCollection<T>
|
||||||
|
{
|
||||||
|
private readonly Queue<T> queue = null;
|
||||||
|
private readonly object syncLock = new object();
|
||||||
|
private readonly int maxSize = 10;
|
||||||
|
private T lastAddedElement;
|
||||||
|
|
||||||
|
public BoundedQueue(int maxSize)
|
||||||
|
{
|
||||||
|
this.maxSize = maxSize;
|
||||||
|
queue = new Queue<T>(maxSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Enqueue(T item)
|
||||||
|
{
|
||||||
|
Enqueue(item, out _);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Enqueue(T item, out T evictedItem)
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
queue.Enqueue(item);
|
||||||
|
if (queue.Count > maxSize)
|
||||||
|
{
|
||||||
|
evictedItem = queue.Dequeue();
|
||||||
|
lastAddedElement = item;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
evictedItem = default;
|
||||||
|
lastAddedElement = item;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Dequeue()
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
return queue.Dequeue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Peek()
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
return queue.Peek();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T LastAddedElement()
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
return lastAddedElement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
queue.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains(T item)
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
return queue.Contains(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterates the internal Queue<T> directly (struct enumerator, no allocation) under the lock.
|
||||||
|
public int CopyTo(List<T> destination)
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
destination.Clear();
|
||||||
|
foreach (T item in queue)
|
||||||
|
{
|
||||||
|
destination.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
return destination.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<T> GetEnumerator()
|
||||||
|
{
|
||||||
|
T[] snapshot;
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
snapshot = queue.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return ((IEnumerable<T>)snapshot).GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CopyTo(Array array, int index)
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
((ICollection)queue).CopyTo(array, index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
return queue.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Capacity => maxSize;
|
||||||
|
public bool IsSynchronized => true;
|
||||||
|
public object SyncRoot => syncLock;
|
||||||
|
|
||||||
|
int IReadOnlyCollection<T>.Count
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
lock (syncLock)
|
||||||
|
{
|
||||||
|
return queue.Count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
namespace Nerfed.Runtime;
|
namespace Nerfed.Runtime;
|
||||||
|
|
||||||
public static class MathEx
|
public static class MathEx
|
||||||
@@ -17,4 +19,51 @@ public static class MathEx
|
|||||||
public static float Remap(float value, float oldMin, float oldMax, float newMin, float newMax) {
|
public static float Remap(float value, float oldMin, float oldMax, float newMin, float newMax) {
|
||||||
return (value - oldMin) / (oldMax - oldMin) * (newMax - newMin) + newMin;
|
return (value - oldMin) / (oldMax - oldMin) * (newMax - newMin) + newMin;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// https://stackoverflow.com/questions/70462758/c-sharp-how-to-convert-quaternions-to-euler-angles-xyz
|
||||||
|
public static Quaternion ToQuaternion(Vector3 v)
|
||||||
|
{
|
||||||
|
float cy = (float)Math.Cos(v.Z * 0.5);
|
||||||
|
float sy = (float)Math.Sin(v.Z * 0.5);
|
||||||
|
float cp = (float)Math.Cos(v.Y * 0.5);
|
||||||
|
float sp = (float)Math.Sin(v.Y * 0.5);
|
||||||
|
float cr = (float)Math.Cos(v.X * 0.5);
|
||||||
|
float sr = (float)Math.Sin(v.X * 0.5);
|
||||||
|
|
||||||
|
return new Quaternion
|
||||||
|
{
|
||||||
|
W = (cr * cp * cy + sr * sp * sy),
|
||||||
|
X = (sr * cp * cy - cr * sp * sy),
|
||||||
|
Y = (cr * sp * cy + sr * cp * sy),
|
||||||
|
Z = (cr * cp * sy - sr * sp * cy),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Vector3 ToEulerAngles(Quaternion q)
|
||||||
|
{
|
||||||
|
Vector3 angles = new();
|
||||||
|
|
||||||
|
// roll / x
|
||||||
|
double sinrCosp = 2 * (q.W * q.X + q.Y * q.Z);
|
||||||
|
double cosrCosp = 1 - 2 * (q.X * q.X + q.Y * q.Y);
|
||||||
|
angles.X = (float)Math.Atan2(sinrCosp, cosrCosp);
|
||||||
|
|
||||||
|
// pitch / y
|
||||||
|
double sinp = 2 * (q.W * q.Y - q.Z * q.X);
|
||||||
|
if (Math.Abs(sinp) >= 1)
|
||||||
|
{
|
||||||
|
angles.Y = (float)Math.CopySign(Math.PI / 2, sinp);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
angles.Y = (float)Math.Asin(sinp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// yaw / z
|
||||||
|
double sinyCosp = 2 * (q.W * q.Z + q.X * q.Y);
|
||||||
|
double cosyCosp = 1 - 2 * (q.Y * q.Y + q.Z * q.Z);
|
||||||
|
angles.Z = (float)Math.Atan2(sinyCosp, cosyCosp);
|
||||||
|
|
||||||
|
return angles;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Util;
|
||||||
|
|
||||||
|
public static class RandomId
|
||||||
|
{
|
||||||
|
public static uint GenerateSecureRandomUInt()
|
||||||
|
{
|
||||||
|
byte[] buffer = new byte[4];
|
||||||
|
RandomNumberGenerator.Fill(buffer);
|
||||||
|
return BitConverter.ToUInt32(buffer, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
using MoonTools.ECS;
|
||||||
|
using Nerfed.Runtime.Components;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Nerfed.Runtime.Util
|
||||||
|
{
|
||||||
|
// https://github.com/needle-mirror/com.unity.entities/blob/master/Unity.Transforms/TransformHelpers.cs
|
||||||
|
public static class Transform
|
||||||
|
{
|
||||||
|
public static Vector3 Forward(in this Matrix4x4 matrix) => new Vector3(matrix.M31, matrix.M32, matrix.M33);
|
||||||
|
public static Vector3 Back(in this Matrix4x4 matrix) => -matrix.Forward();
|
||||||
|
public static Vector3 Up(in this Matrix4x4 matrix) => new Vector3(matrix.M21, matrix.M22, matrix.M23);
|
||||||
|
public static Vector3 Down(in this Matrix4x4 matrix) => -matrix.Up();
|
||||||
|
public static Vector3 Right(in this Matrix4x4 matrix) => new Vector3(matrix.M11, matrix.M12, matrix.M13);
|
||||||
|
public static Vector3 Left(in this Matrix4x4 matrix) => -matrix.Right();
|
||||||
|
//public static Vector3 Translation(in this Matrix4x4 matrix) => new Vector3();
|
||||||
|
//public static Quaternion Rotation(in this Matrix4x4 matrix) => new Quaternion();
|
||||||
|
|
||||||
|
public static Matrix4x4 TRS(in this LocalTransform localTransform)
|
||||||
|
{
|
||||||
|
return Matrix4x4.CreateScale(localTransform.scale) *
|
||||||
|
Matrix4x4.CreateFromQuaternion(localTransform.rotation) *
|
||||||
|
Matrix4x4.CreateTranslation(localTransform.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sets the parent child relation and adds a child component.
|
||||||
|
// Relation goes from child to parent.
|
||||||
|
public static void SetParent(in World world, in Entity child, in Entity parent)
|
||||||
|
{
|
||||||
|
RemoveParent(world, child);
|
||||||
|
|
||||||
|
world.Relate(child, parent, new ChildParentRelation());
|
||||||
|
world.Set(child, new Child());
|
||||||
|
world.Remove<Root>(child);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Removes any parent child relation ship, thus making it a 'root' object.
|
||||||
|
public static void RemoveParent(in World world, in Entity child)
|
||||||
|
{
|
||||||
|
if (!world.HasOutRelation<ChildParentRelation>(child))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Entity parent = world.OutRelationSingleton<ChildParentRelation>(child);
|
||||||
|
|
||||||
|
// TODO: Check if Unrelate all also unrelates incomming relations..?
|
||||||
|
world.Unrelate<ChildParentRelation>(child, parent);
|
||||||
|
world.Remove<Child>(child);
|
||||||
|
world.Set(child, new Root());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Entity CreateBaseEntity(this World world, string tag = "")
|
||||||
|
{
|
||||||
|
Entity entity = world.CreateEntity(tag);
|
||||||
|
world.Set(entity, new Root());
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force update the transform data of an entity (and children).
|
||||||
|
// Useful for when you need precise up to date transform data.
|
||||||
|
public static void ForceUpdateLocalToWorld(in World world, in Entity entity)
|
||||||
|
{
|
||||||
|
Matrix4x4 parentLocalToWorldMatrix = Matrix4x4.Identity;
|
||||||
|
|
||||||
|
if (world.HasOutRelation<ChildParentRelation>(entity)) {
|
||||||
|
Entity parent = world.OutRelationSingleton<ChildParentRelation>(entity);
|
||||||
|
|
||||||
|
if (world.Has<LocalToWorld>(parent))
|
||||||
|
{
|
||||||
|
parentLocalToWorldMatrix = world.Get<LocalToWorld>(parent).localToWorldMatrix;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ForceUpdateLocalToWorld(world, entity, parentLocalToWorldMatrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ForceUpdateLocalToWorld(in World world, in Entity entity, Matrix4x4 localToWorldMatrix)
|
||||||
|
{
|
||||||
|
if (world.Has<LocalTransform>(entity))
|
||||||
|
{
|
||||||
|
LocalTransform localTransform = world.Get<LocalTransform>(entity);
|
||||||
|
localToWorldMatrix = Matrix4x4.Multiply(localToWorldMatrix, localTransform.TRS());
|
||||||
|
LocalToWorld localToWorld = new(localToWorldMatrix);
|
||||||
|
world.Set(entity, localToWorld);
|
||||||
|
|
||||||
|
Log.Info($"Entity {entity} | local position {localTransform.position} | world position {localToWorldMatrix.Translation}");
|
||||||
|
}
|
||||||
|
|
||||||
|
ReverseSpanEnumerator<Entity> childEntities = world.InRelations<ChildParentRelation>(entity);
|
||||||
|
foreach (Entity childEntity in childEntities)
|
||||||
|
{
|
||||||
|
ForceUpdateLocalToWorld(world, childEntity, localToWorldMatrix);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
-34
@@ -5,54 +5,38 @@ VisualStudioVersion = 17.10.35013.160
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Runtime", "Nerfed.Runtime\Nerfed.Runtime.csproj", "{98E09BAF-587F-4238-89BD-7693C036C233}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Runtime", "Nerfed.Runtime\Nerfed.Runtime.csproj", "{98E09BAF-587F-4238-89BD-7693C036C233}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Builder", "Nerfed.Builder\Nerfed.Builder.csproj", "{1B88DE56-2AD8-441E-9B10-073AA43840BF}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nerfed.Builder", "Nerfed.Builder\Nerfed.Builder.csproj", "{1B88DE56-2AD8-441E-9B10-073AA43840BF}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Editor", "Nerfed.Editor\Nerfed.Editor.csproj", "{FF7D032D-7F0B-4700-A818-0606D66AECF8}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nerfed.Editor", "Nerfed.Editor\Nerfed.Editor.csproj", "{FF7D032D-7F0B-4700-A818-0606D66AECF8}"
|
||||||
ProjectSection(ProjectDependencies) = postProject
|
ProjectSection(ProjectDependencies) = postProject
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF} = {1B88DE56-2AD8-441E-9B10-073AA43840BF}
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF} = {1B88DE56-2AD8-441E-9B10-073AA43840BF}
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nerfed.Compiler", "Nerfed.Compiler\Nerfed.Compiler.csproj", "{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}"
|
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nerfed.Runtime.Generator", "Nerfed.Runtime.Generator\Nerfed.Runtime.Generator.csproj", "{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}"
|
|
||||||
EndProject
|
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|x64 = Debug|x64
|
|
||||||
Release|x64 = Release|x64
|
|
||||||
Test|x64 = Test|x64
|
Test|x64 = Test|x64
|
||||||
|
Release|x64 = Release|x64
|
||||||
|
Debug|x64 = Debug|x64
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Debug|x64.ActiveCfg = Debug|x64
|
|
||||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Debug|x64.Build.0 = Debug|x64
|
|
||||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.ActiveCfg = Release|x64
|
|
||||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.Build.0 = Release|x64
|
|
||||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.ActiveCfg = Test|x64
|
|
||||||
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.Build.0 = Test|x64
|
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.ActiveCfg = Debug|x64
|
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.Build.0 = Debug|x64
|
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.ActiveCfg = Release|x64
|
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.Build.0 = Release|x64
|
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.ActiveCfg = Test|x64
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.ActiveCfg = Test|x64
|
||||||
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.Build.0 = Test|x64
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Test|x64.Build.0 = Test|x64
|
||||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.ActiveCfg = Debug|x64
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.ActiveCfg = Release|x64
|
||||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.Build.0 = Debug|x64
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Release|x64.Build.0 = Release|x64
|
||||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.ActiveCfg = Release|x64
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.Build.0 = Release|x64
|
{1B88DE56-2AD8-441E-9B10-073AA43840BF}.Debug|x64.Build.0 = Debug|x64
|
||||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.ActiveCfg = Test|x64
|
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.ActiveCfg = Test|x64
|
||||||
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.Build.0 = Test|x64
|
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Test|x64.Build.0 = Test|x64
|
||||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Debug|x64.ActiveCfg = Debug|x64
|
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.ActiveCfg = Release|x64
|
||||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Debug|x64.Build.0 = Debug|x64
|
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Release|x64.Build.0 = Release|x64
|
||||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Release|x64.ActiveCfg = Release|x64
|
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Release|x64.Build.0 = Release|x64
|
{FF7D032D-7F0B-4700-A818-0606D66AECF8}.Debug|x64.Build.0 = Debug|x64
|
||||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Test|x64.ActiveCfg = Test|x64
|
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.ActiveCfg = Test|x64
|
||||||
{3DFEB8A4-5354-41EA-A249-27ADC7F666CF}.Test|x64.Build.0 = Test|x64
|
{98E09BAF-587F-4238-89BD-7693C036C233}.Test|x64.Build.0 = Test|x64
|
||||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Debug|x64.ActiveCfg = Debug|x64
|
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.ActiveCfg = Release|x64
|
||||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Debug|x64.Build.0 = Debug|x64
|
{98E09BAF-587F-4238-89BD-7693C036C233}.Release|x64.Build.0 = Release|x64
|
||||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Release|x64.ActiveCfg = Release|x64
|
{98E09BAF-587F-4238-89BD-7693C036C233}.Debug|x64.ActiveCfg = Debug|x64
|
||||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Release|x64.Build.0 = Release|x64
|
{98E09BAF-587F-4238-89BD-7693C036C233}.Debug|x64.Build.0 = Debug|x64
|
||||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Test|x64.ActiveCfg = Test|x64
|
|
||||||
{8743FDEF-4FF6-48F9-9F64-7BDEC543C105}.Test|x64.Build.0 = Test|x64
|
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user