Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Trace #222

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Jitter2/Collision/DynamicTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ private int AllocateNode()
if (nodePointer == Nodes.Length)
{
Array.Resize(ref Nodes, Nodes.Length * 2);
Trace.WriteLine($"Resized array of AABBTree to {Nodes.Length} elements.");
Trace.TraceInformation($"{nameof(DynamicTree)}: Resized array of tree to {Nodes.Length} elements.");
}

return nodePointer;
Expand Down
6 changes: 3 additions & 3 deletions src/Jitter2/Collision/NarrowPhase/NarrowPhase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ private bool SolveMPREPA(in MinkowskiDifference mkd, ref JVector point1, ref JVe
}
}

Trace.WriteLine($"EPA: Could not converge within {MaxIter} iterations.");
Trace.TraceWarning($"{nameof(NarrowPhase)}: EPA, Could not converge within {MaxIter} iterations.");

return false;

Expand Down Expand Up @@ -434,7 +434,7 @@ 3. This notice may not be removed or altered from any source distribution.
{
// was: return true;
// better not return a collision
Trace.WriteLine("MPR: This should not happen.");
Debug.Assert(false, "MPR: This should not happen.");
return false;
}

Expand Down Expand Up @@ -649,7 +649,7 @@ public bool Collision(in MinkowskiDifference mkd,
point1 = point2 = normal = JVector.Zero;
penetration = (Real)0.0;

Trace.WriteLine($"EPA: Could not converge within {MaxIter} iterations.");
Trace.TraceWarning($"{nameof(NarrowPhase)}: EPA, Could not converge within {MaxIter} iterations.");

return false;

Expand Down
2 changes: 1 addition & 1 deletion src/Jitter2/Collision/PairHashSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private void Resize(int size)
{
if (Slots.Length == size) return;

Trace.WriteLine($"PairHashSet: Resizing {Slots.Length} -> {size}");
Trace.TraceInformation($"{nameof(PairHashSet)}: Resizing {Slots.Length} -> {size}");

var newSlots = new Pair[size];

Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/AngularMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public struct AngularMotorData

protected override void Create()
{
Trace.Assert(sizeof(AngularMotorData) <= sizeof(ConstraintData));
CheckDataSize<AngularMotorData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<AngularMotorData>(Handle);
Expand Down
2 changes: 1 addition & 1 deletion src/Jitter2/Dynamics/Constraints/BallSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public struct BallSocketData

protected override void Create()
{
Trace.Assert(sizeof(BallSocketData) <= sizeof(ConstraintData));
CheckDataSize<BallSocketData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/ConeLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public struct ConeLimitData

protected override void Create()
{
Trace.Assert(sizeof(ConeLimitData) <= sizeof(ConstraintData));
CheckDataSize<ConeLimitData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<ConeLimitData>(Handle);
Expand Down
19 changes: 13 additions & 6 deletions src/Jitter2/Dynamics/Constraints/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@

namespace Jitter2.Dynamics.Constraints;

[StructLayout(LayoutKind.Sequential, Size = ConstraintSize)]
[StructLayout(LayoutKind.Sequential, Size = Precision.ConstraintSizeSmall)]
public unsafe struct SmallConstraintData
{
public const int ConstraintSize = Jitter2.Precision.ConstraintSizeSmall;

internal int _internal;
public delegate*<ref SmallConstraintData, Real, void> Iterate;
public delegate*<ref SmallConstraintData, Real, void> PrepareForIteration;
Expand All @@ -40,11 +38,9 @@ public unsafe struct SmallConstraintData
public JHandle<RigidBodyData> Body2;
}

[StructLayout(LayoutKind.Sequential, Size = ConstraintSize)]
[StructLayout(LayoutKind.Sequential, Size = Precision.ConstraintSizeFull)]
public unsafe struct ConstraintData
{
public const int ConstraintSize = Jitter2.Precision.ConstraintSizeFull;

internal int _internal;
public delegate*<ref ConstraintData, Real, void> Iterate;
public delegate*<ref ConstraintData, Real, void> PrepareForIteration;
Expand All @@ -70,6 +66,17 @@ public abstract class Constraint : IDebugDrawable

public JHandle<SmallConstraintData> SmallHandle => JHandle<ConstraintData>.AsHandle<SmallConstraintData>(Handle);

/// <summary>
/// Helper to check if the constraint data is small enough.
/// </summary>
protected static unsafe void CheckDataSize<T>() where T : unmanaged
{
if (sizeof(T) > sizeof(ConstraintData))
{
throw new InvalidOperationException("The size of the constraint data is too large.");
}
}

/// <summary>
/// This method must be overridden. It initializes the function pointers for
/// <see cref="ConstraintData.Iterate"/> and <see cref="ConstraintData.PrepareForIteration"/>.
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/DistanceLimit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public struct DistanceLimitData

protected override void Create()
{
Trace.Assert(sizeof(DistanceLimitData) <= sizeof(ConstraintData));
CheckDataSize<DistanceLimitData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<DistanceLimitData>(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/FixedAngle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public struct FixedAngleData

protected override void Create()
{
Trace.Assert(sizeof(FixedAngleData) <= sizeof(ConstraintData));
CheckDataSize<FixedAngleData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<FixedAngleData>(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/HingeAngle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public struct HingeAngleData

protected override void Create()
{
Trace.Assert(sizeof(HingeAngleData) <= sizeof(ConstraintData));
CheckDataSize<HingeAngleData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<HingeAngleData>(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/LinearMotor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public struct LinearMotorData

protected override void Create()
{
Trace.Assert(sizeof(LinearMotorData) <= sizeof(ConstraintData));
CheckDataSize<LinearMotorData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<LinearMotorData>(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/PointOnLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public struct PointOnLineData

protected override void Create()
{
Trace.Assert(sizeof(PointOnLineData) <= sizeof(ConstraintData));
CheckDataSize<PointOnLineData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<PointOnLineData>(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/PointOnPlane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public struct SliderData

protected override void Create()
{
Trace.Assert(sizeof(SliderData) <= sizeof(ConstraintData));
CheckDataSize<SliderData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<SliderData>(Handle);
Expand Down
3 changes: 2 additions & 1 deletion src/Jitter2/Dynamics/Constraints/TwistAngle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public struct TwistLimitData

protected override void Create()
{
Trace.Assert(sizeof(TwistLimitData) <= sizeof(ConstraintData));
CheckDataSize<TwistLimitData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<TwistLimitData>(Handle);
Expand Down
2 changes: 1 addition & 1 deletion src/Jitter2/Parallelization/ThreadPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private ThreadPool()
// To avoid this issue, multi-threading is disabled when a debugger is attached on non-Windows systems.
if (!OperatingSystem.IsWindows() && Debugger.IsAttached)
{
System.Diagnostics.Trace.WriteLine(
System.Diagnostics.Debug.WriteLine(
"Multi-threading disabled to prevent potential hangups: Debugger attached, " +
".NET version < 9.0, non-Windows system detected.");
initialThreadCount = 1; // Forces single-threading to avoid hangups
Expand Down
5 changes: 3 additions & 2 deletions src/Jitter2/SoftBodies/SpringConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public struct SpringData

protected override void Create()
{
Trace.Assert(sizeof(SpringData) <= sizeof(ConstraintData));
CheckDataSize<SpringData>();

iterate = &Iterate;
prepareForIteration = &PrepareForIteration;
handle = JHandle<ConstraintData>.AsHandle<SpringData>(Handle);
}

public override bool IsSmallConstraint { get; } = sizeof(SpringData) <= SmallConstraintData.ConstraintSize;
public override bool IsSmallConstraint { get; } = sizeof(SpringData) <= sizeof(SmallConstraintData);

/// <summary>
/// Initializes the constraint.
Expand Down
2 changes: 1 addition & 1 deletion src/Jitter2/Unmanaged/PartitionedBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public JHandle<T> Allocate(bool active = false, bool clear = false)

size = Math.Min(2 * osize, maximumSize);

Trace.WriteLine($"{nameof(PartitionedBuffer<T>)}: " +
Trace.TraceInformation($"{nameof(PartitionedBuffer<T>)}: " +
$"Resizing to {size}x{typeof(T)} ({size}x{sizeof(T)} Bytes).");

var oldmemory = memory;
Expand Down
3 changes: 3 additions & 0 deletions src/JitterDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using JitterDemo.Renderer.OpenGL;

namespace JitterDemo;
Expand All @@ -17,6 +18,8 @@ private static void PrintException(Exception ex, string info)

public static void Main()
{
Trace.Listeners.Add(new ConsoleTraceListener());

CreationSettings cs = new(1200, 800, "JitterDemo");

try
Expand Down