public void ResizeListCorrectCount() { var list = new List <int>(5); NoAllocHelpers.ResizeList(list, 3); Assert.AreEqual(3, list.Count); }
public void SetIndexCount(int count) { if (count > Indices.Capacity) { Indices.Capacity = count; } NoAllocHelpers.ResizeList(Indices, count); }
public static void CopyTo <T>(List <T> dst, List <T> src) where T : unmanaged { dst.Capacity = src.Count; NoAllocHelpers.ResizeList(dst, src.Count); using (dst.ViewAsNativeArray(out var dstArray)) using (src.ViewAsNativeArray(out var srcArray)) dstArray.CopyFrom(srcArray); }
public static void ResizeList <T>(List <T> list, int count) { if (list.Capacity < count) { list.Capacity = count; } NoAllocHelpers.ResizeList(list, count); }
public static unsafe List <int> V128Filter(List <int> src, int target) { var dst = new List <int>(src.Count); using var srcDispose = src.ViewAsNativeArray(out var srcArray); using var dstDispose = dst.ViewAsNativeArray(out var dstArray); V128Filter((int *)srcArray.GetUnsafeReadOnlyPtr(), srcArray.Length, target, (int *)dstArray.GetUnsafePtr(), out var dstCount); NoAllocHelpers.ResizeList(dst, dstCount); return(dst); }
public static unsafe List <int> V128Filter(List <int> list, int greaterThan, int lessThan) { var dst = new List <int>(list.Count); using var srcDispose = list.ViewAsNativeArray(out var srcArray); using var dstDispose = dst.ViewAsNativeArray(out var dstArray); V128Filter((int *)srcArray.GetUnsafeReadOnlyPtr(), srcArray.Length, greaterThan, lessThan, (int *)dstArray.GetUnsafePtr(), out var dstCount); NoAllocHelpers.ResizeList(dst, dstCount); return(dst); }
public SoAMesh(int vertexCount, int indexCount) { Vertices = new List <Vector3>(vertexCount); Normals = new List <Vector3>(vertexCount); Colors = new List <Color32>(vertexCount); Indices = new List <int>(indexCount); NoAllocHelpers.ResizeList(Vertices, vertexCount); NoAllocHelpers.ResizeList(Normals, vertexCount); NoAllocHelpers.ResizeList(Colors, vertexCount); NoAllocHelpers.ResizeList(Indices, indexCount); }
public static unsafe List <u32> V128ForLoop(List <u8> src) { var dst = new List <u32>(src.Count / 3); using (dst.ViewAsNativeArray(out var dstArray)) using (src.ViewAsNativeArray(out var srcArray)) V128ForLoop((u8 *)srcArray.GetUnsafePtr(), (u32 *)dstArray.GetUnsafePtr(), dst.Capacity); NoAllocHelpers.ResizeList(dst, src.Count / 3); return(dst); }
// Note: No checking that there is actually this many vertices in here. // However, it will increase capacity if needed. public void SetVertexCount(int count) { if (count > Vertices.Capacity) { Vertices.Capacity = count; Normals.Capacity = count; Colors.Capacity = count; } NoAllocHelpers.ResizeList(Vertices, count); NoAllocHelpers.ResizeList(Normals, count); NoAllocHelpers.ResizeList(Colors, count); }
// https://forum.unity.com/threads/nativearray-and-mesh.522951/ // avoid having to call NativeList.ToArray() when assigning a Mesh attribute which results in garbage // There seems some GCAllocs still happen, but CPU spikes seem to be improved alot // NOTE: that the buffer resizes up to the size of native, and does not shrink to avoid allocations -> Potential Memory Hog // TODO: This HACK will go away with the official support of NativeArrays / NativeLists? in https://forum.unity.com/threads/feedback-wanted-mesh-scripting-api-improvements.684670/ static unsafe void assignNativeListToBuffer <TNative, T> (NativeList <TNative> native, ref List <T> buffer) where TNative : struct where T : struct { //Debug.Assert(buffer.Count == 0); Debug.Assert(UnsafeUtility.SizeOf <TNative>() == UnsafeUtility.SizeOf <T>()); if (native.Length > 0) { if (buffer.Capacity < native.Length) { buffer.Capacity = native.Length; } var arr = NoAllocHelpers.ExtractArrayFromListT(buffer); var size = UnsafeUtility.SizeOf <T>(); var ptr = (byte *)UnsafeUtility.AddressOf(ref arr[0]); UnsafeUtility.MemCpy(ptr, native.GetUnsafePtr(), native.Length * (long)size); } NoAllocHelpers.ResizeList(buffer, native.Length); }
public static unsafe void CopyIntegers(NativeList <int> src, List <int> dst) { if (dst.Capacity < src.Length) { dst.Capacity = src.Length; } var array = NoAllocHelpers.ExtractArrayFromListT(dst); fixed(int *arrayPtr = array) { var dstSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <int>(arrayPtr, sizeof(int), src.Length); #if ENABLE_UNITY_COLLECTIONS_CHECKS NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref dstSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle()); #endif dstSlice.CopyFrom((NativeArray <int>)src); } NoAllocHelpers.ResizeList(dst, src.Length); }
static unsafe void NativeAddRange <T>(this List <T> list, NativeSlice <T> nativeSlice) where T : struct { var index = list.Count; var newLength = index + nativeSlice.Length; // Resize our list if we require if (list.Capacity < newLength) { list.Capacity = newLength; } var items = NoAllocHelpers.ExtractArrayFromListT(list); var size = UnsafeUtility.SizeOf <T>(); // Get the pointer to the end of the list var bufferStart = (IntPtr)UnsafeUtility.AddressOf(ref items[0]); var buffer = (byte *)(bufferStart + (size * index)); UnsafeUtility.MemCpy(buffer, nativeSlice.GetUnsafePtr(), nativeSlice.Length * (long)size); NoAllocHelpers.ResizeList(list, newLength); }
private static unsafe void NativeInject <T>(this List <T> list, int startIndex, void *src, int srcIndex, int length) where T : struct { var newLength = startIndex + length; if (list.Capacity < newLength) { list.Capacity = newLength; } var size = UnsafeUtility.SizeOf <T>(); var dst = list.GetUnsafePtr(out var gcHandle); UnsafeUtility.MemCpy( (void *)((IntPtr)dst + startIndex * size), (void *)((IntPtr)src + srcIndex * size), length * size ); UnsafeUtility.ReleaseGCObject(gcHandle); NoAllocHelpers.ResizeList(list, newLength); }
private static GameObject CreateMesh(string ifcGuid, IFCObjectID ifcObjectData, MemoryMap map) { var dimensions = map.Dimensions; int offset = 0; for (int i = 0; i < ifcObjectData.Voxel.Layer; i++) { offset += dimensions[i].x * dimensions[i].y * dimensions[i].z; } var begin = map.Contains[offset + ifcObjectData.Voxel.Index].Begin; var vertexInfo = map.VerticesInfo[begin + ifcObjectData.Index].Vertex; var vertexCount = vertexInfo.End - vertexInfo.Begin; var indexInfo = map.VerticesInfo[begin + ifcObjectData.Index].Indices; var indexCount = indexInfo.End - indexInfo.Begin; Debug.Assert(indexCount > 0); var vertices = new List <Vector3>(vertexCount); NoAllocHelpers.ResizeList(vertices, vertexCount); fixed(Vector3 *p = NoAllocHelpers.ExtractArrayFromListT(vertices)) { UnsafeUtility.MemCpy(p, &map.Vertices[vertexInfo.Begin], vertexCount * sizeof(Vector3)); } var normals = new List <Vector3>(vertexCount); NoAllocHelpers.ResizeList(normals, vertexCount); fixed(Vector3 *p = NoAllocHelpers.ExtractArrayFromListT(normals)) { UnsafeUtility.MemCpy(p, &map.Normals[vertexInfo.Begin], vertexCount * sizeof(Vector3)); } var colors = new List <Color32>(vertexCount); NoAllocHelpers.ResizeList(colors, vertexCount); for (int i = 0; i < colors.Count; i++) { colors[i] = Color.magenta; } var indices = new List <int>(indexCount); NoAllocHelpers.ResizeList(indices, indexCount); fixed(int *p = NoAllocHelpers.ExtractArrayFromListT(indices)) { UnsafeUtility.MemCpy(p, &map.Indices[indexInfo.Begin], indexCount * sizeof(int)); } for (int i = 0; i < indices.Count; i++) { vertices[indices[i]] += normals[indices[i]] * 0.01f; } var mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; mesh.SetVertices(vertices); mesh.SetNormals(normals); mesh.SetColors(colors); mesh.SetTriangles(indices, 0); var go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.name = ifcGuid; var filter = go.GetComponent <MeshFilter>(); filter.mesh = mesh; var renderer = go.GetComponent <MeshRenderer>(); renderer.material = Globals.Instance.data.OpaqueMaterial; return(go); }