//
// Generated by Brutal.Jolt.Generate; do not edit by hand.
#nullable enable
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Brutal.Interop;
using Brutal.Numerics;
namespace Brutal.JoltApi
{
///
///
/// A managed : subclass it and override the callbacks you care about. The
/// instance owns a native DebugRendererSimple subclass that forwards each virtual to this object, so it can be
/// passed anywhere a is expected. Dispose it only after the physics system
/// has stopped using it. Callbacks run on Jolt's worker threads.
///
///
///
///
/// Calling is REQUIRED. The instance pins itself with a strong GCHandle so that native
/// code can always find it, which means it is never garbage collected while the proxy is alive: forgetting to
/// dispose leaks the object and its native proxy for the life of the process. Dispose only once the physics
/// system no longer references it; disposing destroys the native proxy first, so no further callback can
/// arrive, and is safe to call more than once.
///
///
/// There is no finalizer: the strong self-GCHandle keeps the instance reachable, so it is not collectable until
/// it is disposed. Explicit is the only lifetime mechanism.
///
///
public abstract unsafe class ManagedDebugRendererSimple : IDisposable
{
private GCHandle _self;
private nint _proxy;
private DebugRendererSimple _base;
private Exception? _lastCallbackException;
///
///
/// The first exception a callback threw since it was last taken. Exceptions can't cross the native frame,
/// so a throwing callback is swallowed and recorded here instead; check it after stepping if a callback may
/// fail. Callbacks run on Jolt's worker threads, so the first exception wins and later ones are dropped
/// until clears it.
///
///
public Exception? LastCallbackException => Volatile.Read(ref _lastCallbackException);
///
///
/// Atomically returns the recorded callback exception and clears it, so the next callback failure is
/// recorded in turn. Returns null when no callback has thrown.
///
///
public Exception? TakeCallbackException() => Interlocked.Exchange(ref _lastCallbackException, null);
// First exception wins: a later one would hide the cause while jobs keep running
private void RecordCallbackException(Exception exception) =>
Interlocked.CompareExchange(ref _lastCallbackException, exception, null);
// Installing a thunk costs a managed transition on every call, so only the virtuals this
// subclass overrode get one; the shim falls back to the base implementation for the rest.
// The match is on the exact signature AND on the override relationship: a member that merely
// hides one of these callbacks declares its own slot and is never what a thunk would reach.
private static bool IsOverridden(Type type, string name, Type[] parameterTypes)
{
const BindingFlags declared =
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
for (var t = type; t is not null && t != typeof(ManagedDebugRendererSimple); t = t.BaseType)
{
foreach (var method in t.GetMethods(declared))
{
if (method.Name != name || !Matches(method, parameterTypes))
continue;
if (method.GetBaseDefinition().DeclaringType == typeof(ManagedDebugRendererSimple))
return true;
}
}
return false;
}
private static bool Matches(MethodInfo method, Type[] parameterTypes)
{
var parameters = method.GetParameters();
if (parameters.Length != parameterTypes.Length)
return false;
for (var i = 0; i < parameters.Length; i++)
{
if (parameters[i].ParameterType != parameterTypes[i])
return false;
}
return true;
}
protected ManagedDebugRendererSimple()
{
_self = GCHandle.Alloc(this);
try
{
_proxy = Jolt.PInvoke.DebugRendererSimple_CreateProxy(GCHandle.ToIntPtr(_self));
_base = Jolt.PInvoke.DebugRendererSimple_ProxyGetBase(_proxy);
var type = GetType();
if (
IsOverridden(
type,
"DrawTriangle",
[typeof(float3), typeof(float3), typeof(float3), typeof(uint), typeof(DebugRenderer.CastShadow)]
)
)
Jolt.PInvoke.DebugRendererSimple_ProxySetDrawTriangle(_proxy, &DrawTriangleThunk);
if (IsOverridden(type, "DrawLine", [typeof(float3), typeof(float3), typeof(uint)]))
Jolt.PInvoke.DebugRendererSimple_ProxySetDrawLine(_proxy, &DrawLineThunk);
if (
IsOverridden(
type,
"DrawText3D",
[typeof(float3), typeof(ReadOnlySpan), typeof(uint), typeof(float)]
)
)
Jolt.PInvoke.DebugRendererSimple_ProxySetDrawText3D(_proxy, &DrawText3DThunk);
}
catch
{
if (_proxy != 0)
{
Jolt.PInvoke.DebugRendererSimple_DestroyProxy(_proxy);
_proxy = 0;
_base = default;
}
_self.Free();
_self = default;
throw;
}
}
///
///
/// The native DebugRendererSimple this object drives. It is owned by this instance - don't Dispose it.
///
///
public DebugRendererSimple Handle => _base;
public static implicit operator DebugRendererSimple(ManagedDebugRendererSimple listener) => listener.Handle;
private static ManagedDebugRendererSimple Target(nint userData) =>
(ManagedDebugRendererSimple)GCHandle.FromIntPtr(userData).Target!;
///
///
/// Fallback implementation that uses DrawLine to draw a triangle (override this if you have a version that
/// renders solid triangles)
///
///
/// C++:
/// Jolt/Renderer/DebugRendererSimple.h:54
///
/// virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow) override
///
///
protected virtual void DrawTriangle(
float3 v1,
float3 v2,
float3 v3,
uint color,
DebugRenderer.CastShadow castShadow
) { }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static void DrawTriangleThunk(
float3 v1,
float3 v2,
float3 v3,
uint color,
DebugRenderer.CastShadow castShadow,
nint userData
)
{
ManagedDebugRendererSimple? self = null;
try
{
self = Target(userData);
self.DrawTriangle(v1, v2, v3, color, castShadow);
}
catch (Exception exception)
{
self?.RecordCallbackException(exception);
}
}
///
///
/// Draw line
///
///
/// C++:
/// Jolt/Renderer/DebugRenderer.h:59
///
/// virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) = 0
///
///
protected virtual void DrawLine(float3 from, float3 to, uint color) { }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static void DrawLineThunk(float3 from, float3 to, uint color, nint userData)
{
ManagedDebugRendererSimple? self = null;
try
{
self = Target(userData);
self.DrawLine(from, to, color);
}
catch (Exception exception)
{
self?.RecordCallbackException(exception);
}
}
///
///
/// Draw text
///
///
/// C++:
/// Jolt/Renderer/DebugRenderer.h:285
///
/// virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor = Color::sWhite, float inHeight = 0.5f) = 0
///
///
///
///
/// string is UTF-8 text borrowed from Jolt for the duration of the callback and is not null-terminated:
/// decode or copy it before returning.
///
///
protected virtual void DrawText3D(float3 position, ReadOnlySpan @string, uint color, float height) { }
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static void DrawText3DThunk(
float3 position,
byte* @string,
nuint stringLength,
uint color,
float height,
nint userData
)
{
ManagedDebugRendererSimple? self = null;
try
{
self = Target(userData);
self.DrawText3D(position, new ReadOnlySpan(@string, checked((int)stringLength)), color, height);
}
catch (Exception exception)
{
self?.RecordCallbackException(exception);
}
}
///
///
/// Releases resources a subclass owns. Called once by Dispose(), after the native proxy has been destroyed,
/// so no callback can still be running - override this instead of hiding Dispose().
///
///
protected virtual void DisposeManaged() { }
///
///
/// Destroys the native proxy and releases the self-handle, then calls DisposeManaged() so a subclass can
/// release its own resources. Never call this while the listener is still installed; calling it more than
/// once, or from more than one thread, is harmless.
///
///
public void Dispose()
{
var proxy = Interlocked.Exchange(ref _proxy, 0);
if (proxy == 0)
return;
try
{
// The native proxy goes first, so no further callback can arrive into state
// DisposeManaged() is about to release
_base = default;
Jolt.PInvoke.DebugRendererSimple_DestroyProxy(proxy);
if (_self.IsAllocated)
_self.Free();
_self = default;
}
finally
{
DisposeManaged();
}
}
}
}