I’ve tried to use ReadOnlySpan
conversion to pointer within Burst. There seems to be an issue with internal call to GetPinnableReference
:
Burst version: 1.7.0-pre.1
Reproduction code:
public unsafe static class Extensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span<T> AsSpan<T>(this NativeArray<T> array) where T : struct
{
return new Span<T>(array.GetUnsafePtr(), array.Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<T> AsReadOnlySpan<T>(this NativeArray<T> array) where T : struct
{
return new Span<T>(array.GetUnsafePtr(), array.Length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddRange<T>(this NativeList<T> list, Span<T> data) where T : unmanaged
{
fixed (T* ptr = data)
{
list.AddRange(ptr, data.Length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AddRange<T>(this NativeList<T> list, ReadOnlySpan<T> data) where T : unmanaged
{
fixed (T* ptr = data)
{
list.AddRange(ptr, data.Length);
}
}
}
[BurstCompile]
public struct TestJob : IJob
{
public NativeList<int> list;
public NativeArray<int> data;
public void Execute()
{
list.AddRange(data.AsSpan());
}
}
[BurstCompile]
public struct TestJob2 : IJob
{
public NativeList<int> list;
public NativeArray<int> data;
public void Execute()
{
list.AddRange(data.AsReadOnlySpan());
}
}
Error log:
(0,0): Burst error BC0102: Unexpected internal compiler error while processing function `IL_0001: ldfld System.Int32 System.Span`1<T>::_length args(IL_0000(ldarg.0))`: System.InvalidCastException: Specified cast is not valid.
at Burst.Compiler.IL.ILCompiler.Ldfld (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x00123] in <27d4760922654fef9379ac87626f329d>:0
at Burst.Compiler.IL.ILCompiler.CompileInternal (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x004f8] in <27d4760922654fef9379ac87626f329d>:0
at Burst.Compiler.IL.ILCompiler.Compile (Burst.Compiler.IL.Syntax.ILInstruction inst, System.Boolean fromVisiting) [0x00043] in <27d4760922654fef9379ac87626f329d>:0
at Burst.Compiler.IL.ILCompiler.VisitImpl (Burst.Compiler.IL.Syntax.ILInstruction inst) [0x00000] in <27d4760922654fef9379ac87626f329d>:0
at Burst.Compiler.IL.ILVisitor.Visit (Burst.Compiler.IL.Syntax.ILInstruction instruction) [0x00000] in <27d4760922654fef9379ac87626f329d>:0
at System.Span`1<System.Int32>.GetPinnableReference(System.Span`1<int>* this)
at Extensions.AddRange(Unity.Collections.NativeList`1<int>* list, System.Span`1<int> data) (at D:\New Unity Project (2)\Assets\Test.cs:60)
at TestJob.Execute(TestJob* this) (at D:\New Unity Project (2)\Assets\Test.cs:75)
at Unity.Jobs.IJobExtensions.JobStruct`1<TestJob>.Execute(ref TestJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)