Пример #1
0
    public unsafe void UnsafeList_Resize_ClearMemory()
    {
        var sizeOf  = UnsafeUtility.SizeOf <int>();
        var alignOf = UnsafeUtility.AlignOf <int>();

        UnsafeList list = new UnsafeList(sizeOf, alignOf, 5, Allocator.Persistent, NativeArrayOptions.ClearMemory);

        list.SetCapacity <int>(32);
        var capacity = list.Capacity;

        list.Resize(sizeOf, alignOf, 5, NativeArrayOptions.UninitializedMemory);
        Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize

        for (var i = 0; i < 5; ++i)
        {
            UnsafeUtility.WriteArrayElement(list.Ptr, i, i);
        }

        list.Resize(sizeOf, alignOf, 10, NativeArrayOptions.ClearMemory);
        Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize

        for (var i = 0; i < 5; ++i)
        {
            Assert.AreEqual(i, UnsafeUtility.ReadArrayElement <int>(list.Ptr, i));
        }

        for (var i = 5; i < list.Length; ++i)
        {
            Assert.AreEqual(0, UnsafeUtility.ReadArrayElement <int>(list.Ptr, i));
        }

        list.Dispose();
    }
Пример #2
0
        internal void AllocateIndexRange(int index, int range)
        {
            if ((index + range) > indexToID.Length)
            {
                indexToID.Resize((index + range), NativeArrayOptions.ClearMemory);
            }

            int idInternal, generation;

            // TODO: should make it possible to allocate ids in a range as well, for cache locality
            if (freeIDs.Length >= range)
            {
                var childIndex = index;
                while (range > 0)
                {
                    var freeID = freeIDs.Length - 1;
                    idInternal = freeIDs[freeID];
                    freeIDs.RemoveAt(freeID);

                    generation            = idToIndex[idInternal].generation + 1;
                    idToIndex[idInternal] = new IndexLookup {
                        index = childIndex, generation = generation
                    };

                    indexToID[childIndex] = idInternal + 1;

                    range--;
                    childIndex++;
                }
            }

            if (range <= 0)
            {
                return;
            }

            generation = 1;
            idInternal = idToIndex.Length;
            idToIndex.Resize((idInternal + range), NativeArrayOptions.ClearMemory);

            for (int childIndex = index, lastID = (idInternal + range); idInternal < lastID; idInternal++, childIndex++)
            {
                indexToID[childIndex] = idInternal + 1;
                idToIndex[idInternal] = new IndexLookup {
                    index = childIndex, generation = generation
                };
            }
        }
    public void CustomAllocatorUnsafeListWorks()
    {
        var customhandle = new AllocatorManager.AllocatorHandle {
            Value = AllocatorManager.FirstUserIndex
        };

        AllocatorManager.Initialize();
        using (var installation = new AllocatorManager.AllocatorInstallation <ClearToValueAllocator>(customhandle))
        {
            installation.Allocator.budgetInBytes = 100;
            for (byte ClearValue = 0; ClearValue < 0xF; ++ClearValue)
            {
                installation.Allocator.ClearValue = ClearValue;
                using (var unsafelist = new UnsafeList <byte>(1, customhandle))
                {
                    const int kLength = 100;
                    unsafelist.Resize(kLength);
                    for (int i = 0; i < kLength; ++i)
                    {
                        Assert.AreEqual(ClearValue, unsafelist[i]);
                    }
                }
            }
        }
        AllocatorManager.Shutdown();
    }
Пример #4
0
    public unsafe void UnsafeList_Resize_ClearMemory()
    {
        var sizeOf  = UnsafeUtility.SizeOf <int>();
        var alignOf = UnsafeUtility.AlignOf <int>();

        UnsafeList list = new UnsafeList(sizeOf, alignOf, 5, Allocator.Persistent, NativeArrayOptions.ClearMemory);

        list.Resize(sizeOf, alignOf, 5, NativeArrayOptions.UninitializedMemory);
        list.Resize(sizeOf, alignOf, 10, NativeArrayOptions.ClearMemory);

        for (var i = 0; i < list.Length; ++i)
        {
            Assert.AreEqual(0, UnsafeUtility.ReadArrayElement <int>(list.Ptr, i));
        }

        list.Dispose();
    }
Пример #5
0
        public bool ConvexPartition(int curveSegments, out UnsafeList <SegmentVertex> polygonVerticesArray, out UnsafeList <int> polygonVerticesSegments, Allocator allocator)
        {
            using (var shapeVertices = new NativeList <SegmentVertex>(Allocator.Temp))
            {
                GetPathVertices(curveSegments, shapeVertices);

                //Profiler.BeginSample("ConvexPartition");
                if (shapeVertices.Length == 3)
                {
                    polygonVerticesArray    = new UnsafeList <SegmentVertex>(3, allocator);
                    polygonVerticesSegments = new UnsafeList <int>(1, allocator);

                    polygonVerticesArray.Resize(3, NativeArrayOptions.UninitializedMemory);
                    polygonVerticesArray[0] = shapeVertices[0];
                    polygonVerticesArray[1] = shapeVertices[1];
                    polygonVerticesArray[2] = shapeVertices[2];

                    polygonVerticesSegments.Resize(1, NativeArrayOptions.UninitializedMemory);
                    polygonVerticesSegments[0] = polygonVerticesArray.Length;
                }
                else
                {
                    polygonVerticesArray    = new UnsafeList <SegmentVertex>(shapeVertices.Length * math.max(1, shapeVertices.Length / 2), allocator);
                    polygonVerticesSegments = new UnsafeList <int>(shapeVertices.Length, allocator);
                    if (!External.BayazitDecomposerBursted.ConvexPartition(shapeVertices,
                                                                           ref polygonVerticesArray,
                                                                           ref polygonVerticesSegments))
                    {
                        polygonVerticesArray.Dispose();
                        polygonVerticesSegments.Dispose();
                        polygonVerticesArray    = default;
                        polygonVerticesSegments = default;
                        return(false);
                    }
                }

                for (int i = 0; i < polygonVerticesSegments.Length; i++)
                {
                    var range = new Range
                    {
                        start = i == 0 ? 0 : polygonVerticesSegments[i - 1],
                        end   = polygonVerticesSegments[i]
                    };

                    if (CalculateOrientation(polygonVerticesArray, range) < 0)
                    {
                        External.BayazitDecomposerBursted.Reverse(polygonVerticesArray, range);
                    }
                }
                //Profiler.EndSample();

                //Debug.Assert(polygonVerticesArray.Length == 0 || polygonVerticesArray.Length == polygonVerticesSegments[polygonVerticesSegments.Length - 1]);
                return(true);
            }
        }
Пример #6
0
        public static void GetCircleMatrices(out UnsafeList <float4x4> matrices, int segments, float3 axis, float totalAngle, Allocator allocator)
        {
            var radiansPerSegment = math.radians(totalAngle / segments);

            segments++;

            matrices = new UnsafeList <float4x4>(segments, allocator);
            matrices.Resize(segments, NativeArrayOptions.ClearMemory);
            for (int s = 0; s < segments; s++)
            {
                var hRadians = (s * radiansPerSegment);
                var rotation = quaternion.AxisAngle(axis, hRadians);
                matrices[s] = float4x4.TRS(float3.zero, rotation, new float3(1));
            }
        }
Пример #7
0
    public unsafe void UnsafeList_Resize_Zero()
    {
        var sizeOf  = UnsafeUtility.SizeOf <int>();
        var alignOf = UnsafeUtility.AlignOf <int>();

        UnsafeList list     = new UnsafeList(sizeOf, alignOf, 5, Allocator.Persistent, NativeArrayOptions.ClearMemory);
        var        capacity = list.Capacity;

        list.Add(1);
        list.Resize <int>(0);
        Assert.AreEqual(0, list.Length);
        Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize

        list.Add(2);
        list.Clear();
        Assert.AreEqual(0, list.Length);
        Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize

        list.Dispose();
    }
Пример #8
0
    public void CustomAllocatorUnsafeListWorks()
    {
        AllocatorManager.Initialize();
        var parent    = AllocatorManager.Persistent;
        var allocator = ClearToValueAllocator.New(0xFE, ref parent);

        allocator.Register();
        for (byte ClearValue = 0; ClearValue < 0xF; ++ClearValue)
        {
            allocator.m_clearValue = ClearValue;
            var       unsafelist = new UnsafeList <byte>(1, allocator.Handle);
            const int kLength    = 100;
            unsafelist.Resize(kLength);
            for (int i = 0; i < kLength; ++i)
            {
                Assert.AreEqual(ClearValue, unsafelist[i]);
            }
            unsafelist.Dispose();
        }
        allocator.Dispose();
        AllocatorManager.Shutdown();
    }