mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
1 changed files with 784842 additions and 0 deletions
297
Brutal.Jolt/Generated/Classes/ManagedDebugRendererSimple.gen.cs
Normal file
297
Brutal.Jolt/Generated/Classes/ManagedDebugRendererSimple.gen.cs
Normal file
|
|
@ -0,0 +1,297 @@
|
|||
// <auto-generated/>
|
||||
// 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
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// A managed <see cref="DebugRendererSimple"/>: 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 <see cref="DebugRendererSimple"/> is expected. Dispose it only after the physics system
|
||||
/// has stopped using it. Callbacks run on Jolt's worker threads.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Calling <see cref="Dispose()"/> 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// There is no finalizer: the strong self-GCHandle keeps the instance reachable, so it is not collectable until
|
||||
/// it is disposed. Explicit <see cref="Dispose()"/> is the only lifetime mechanism.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public abstract unsafe class ManagedDebugRendererSimple : IDisposable
|
||||
{
|
||||
private GCHandle _self;
|
||||
private nint _proxy;
|
||||
private DebugRendererSimple _base;
|
||||
private Exception? _lastCallbackException;
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// 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 <see cref="TakeCallbackException"/> clears it.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public Exception? LastCallbackException => Volatile.Read(ref _lastCallbackException);
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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<byte>), 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// The native DebugRendererSimple this object drives. It is owned by this instance - don't Dispose it.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public DebugRendererSimple Handle => _base;
|
||||
|
||||
public static implicit operator DebugRendererSimple(ManagedDebugRendererSimple listener) => listener.Handle;
|
||||
|
||||
private static ManagedDebugRendererSimple Target(nint userData) =>
|
||||
(ManagedDebugRendererSimple)GCHandle.FromIntPtr(userData).Target!;
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Fallback implementation that uses DrawLine to draw a triangle (override this if you have a version that
|
||||
/// renders solid triangles)
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// C++:
|
||||
/// <see href="https://github.com/jrouwe/JoltPhysics/blob/e77f175595e64cb44218cc9d9d56fc365ad0e36a/Jolt/Renderer/DebugRendererSimple.h#L54">Jolt/Renderer/DebugRendererSimple.h:54</see>
|
||||
/// <br/>
|
||||
/// <c>virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow) override</c>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Draw line
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// C++:
|
||||
/// <see href="https://github.com/jrouwe/JoltPhysics/blob/e77f175595e64cb44218cc9d9d56fc365ad0e36a/Jolt/Renderer/DebugRenderer.h#L59">Jolt/Renderer/DebugRenderer.h:59</see>
|
||||
/// <br/>
|
||||
/// <c>virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) = 0</c>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// Draw text
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// C++:
|
||||
/// <see href="https://github.com/jrouwe/JoltPhysics/blob/e77f175595e64cb44218cc9d9d56fc365ad0e36a/Jolt/Renderer/DebugRenderer.h#L285">Jolt/Renderer/DebugRenderer.h:285</see>
|
||||
/// <br/>
|
||||
/// <c>virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor = Color::sWhite, float inHeight = 0.5f) = 0</c>
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
protected virtual void DrawText3D(float3 position, ReadOnlySpan<byte> @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<byte>(@string, checked((int)stringLength)), color, height);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
self?.RecordCallbackException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// 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().
|
||||
/// </para>
|
||||
/// </summary>
|
||||
protected virtual void DisposeManaged() { }
|
||||
|
||||
/// <summary>
|
||||
/// <para>
|
||||
/// 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.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue