Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created May 5, 2020 14:07
Show Gist options
  • Save jeffvella/0867d16c938f9f884200472e53370ed5 to your computer and use it in GitHub Desktop.
Save jeffvella/0867d16c938f9f884200472e53370ed5 to your computer and use it in GitHub Desktop.
Model for burst function use.
internal struct GridUtility
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetFlags(int* flagsPtr, int length, ref NodeFlags addFlags)
{
var flagToSet = (int4)(int)addFlags;
var batchFlags = (int4*)flagsPtr;
// array length must be 4x aligned for overrun on last batch
var batchLen = length / 4 + math.select(1, 0, length % 4 != 0);
for (var i = 0; i < batchLen; i++)
{
batchFlags[i] |= flagToSet;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetFlags(ref GridComponent grid, ref NodeFlags addFlags)
{
ref var blobValue = ref grid.GridData.Value;
SetFlags((int*)blobValue.Flags.GetUnsafePtr(), blobValue.Flags.Length, ref addFlags);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetFlags(ref GridComponent grid, int3 min, int3 max, ref NodeFlags addFlags)
{
// untested
ref var blobValue = ref grid.GridData.Value;
var batchFlags = (int*)blobValue.Flags.GetUnsafePtr();
var flagToSet = (int)addFlags;
for (var x = min.x; x <= max.x; x++)
{
for (var y = min.y; y <= max.y; y++)
{
for (var z = min.z; z <= max.z; z++)
{
batchFlags[blobValue.GetIndex(x, y, z)] |= flagToSet;
}
}
}
}
[BurstCompile]
internal struct Managed
{
public unsafe delegate void SetFlagsDelegate(ref GridComponent grid, ref NodeFlags addFlags);
public static SetFlagsDelegate SetFlags;
public unsafe delegate void SetFlagsRawDelegate(int* flagsPtr, int length, ref NodeFlags addFlags);
public static SetFlagsRawDelegate SetFlagsRaw;
[RuntimeInitializeOnLoadMethod]
public static unsafe void Initialize()
{
if (SetFlags != null)
return;
SetFlags = BurstCompiler.CompileFunctionPointer<SetFlagsDelegate>(SetFlagsExecute).Invoke;
SetFlagsRaw = BurstCompiler.CompileFunctionPointer<SetFlagsRawDelegate>(SetFlagsRawExecute).Invoke;
}
[BurstCompile, AOT.MonoPInvokeCallback(typeof(SetFlagsDelegate))]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void SetFlagsExecute(ref GridComponent grid, ref NodeFlags addFlags)
{
GridUtility.SetFlags(ref grid, ref addFlags);
}
[BurstCompile, AOT.MonoPInvokeCallback(typeof(SetFlagsRawDelegate))]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void SetFlagsRawExecute(int* flagsPtr, int length, ref NodeFlags addFlags)
{
GridUtility.SetFlags(flagsPtr, length, ref addFlags);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment