Ejemplo n.º 1
0
        /// <summary>
        ///     Reallocate the dense and sparse arrays with additional capacity.
        /// </summary>
        /// <param name="extraCapacity">How many indices to expand the dense and sparse arrays by.</param>
        /// <param name="versionArray">Array containing version numbers to check against.</param>
        /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param>
        /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param>
        public void Expand(int extraCapacity, ref NativeArray <ulong> versionArray, Allocator allocator,
                           NativeArrayOptions nativeArrayOptions)
        {
            int currentCapacity = SparseArray.Length;
            int newCapacity     = currentCapacity + extraCapacity;

            NativeArray <int> newSparseArray      = new NativeArray <int>(newCapacity, allocator, nativeArrayOptions);
            NativeSlice <int> newSparseArraySlice = new NativeSlice <int>(newSparseArray, 0, currentCapacity);

            newSparseArraySlice.CopyFrom(SparseArray);
            SparseArray = newSparseArray;

            NativeArray <int> newDenseArray      = new NativeArray <int>(newCapacity, allocator, nativeArrayOptions);
            NativeSlice <int> newDenseArraySlice = new NativeSlice <int>(newDenseArray, 0, currentCapacity);

            newDenseArraySlice.CopyFrom(DenseArray);
            DenseArray = newDenseArray;

            NativeArray <ulong> newVersionArray      = new NativeArray <ulong>(newCapacity, allocator, nativeArrayOptions);
            NativeSlice <ulong> newVersionArraySlice = new NativeSlice <ulong>(newVersionArray, 0, currentCapacity);

            newVersionArraySlice.CopyFrom(versionArray);
            versionArray = newVersionArray;

            for (int i = currentCapacity; i < newCapacity; i++)
            {
                DenseArray[i]  = -1;    // Set new dense indices as unclaimed.
                SparseArray[i] = i + 1; // Build the free list chain.
            }
        }
Ejemplo n.º 2
0
    public void NativeSlice_CopyFrom()
    {
        NativeArray <int> array = new NativeArray <int>(1000, Allocator.Persistent);

        for (int i = 0; i < array.Length; ++i)
        {
            array[i] = i;
        }

        var copyFromArray = new int[600];

        for (int i = 0; i < copyFromArray.Length; ++i)
        {
            copyFromArray[i] = 0x12345678;
        }

        var slice = new NativeSlice <int>(array, 200, 600);

        slice.CopyFrom(copyFromArray);

        for (var i = 0; i < 600; ++i)
        {
            Assert.AreEqual(slice[i], 0x12345678);
        }

        array.Dispose();
    }
        public int Read(NativeSlice <float> buffer, NativeSlice <float> buffer2)
        {
            if (stopped)
            {
                return(0);
            }

            var read = 0;

            while (true)
            {
                // we need to remember how much was available this round
                var available  = samples.Length / channels - offset;
                var bufferSize = buffer.Length;

                // handle the easier scenario of the sample being longer than the buffer
                if (bufferSize < available)
                {
                    //if (buffer.Length != buffer2.Length)
                    //    throw new System.Exception($"{buffer.Length}!={buffer2.Length}");

                    // copy over the next block of data
                    buffer.CopyFrom(samples.Slice(offset, bufferSize));
                    buffer2.CopyFrom(samples.Slice(offset + samples.Length / channels, buffer2.Length));

                    // advance the sample forward
                    offset += bufferSize;

                    // the buffer is full
                    return((read + bufferSize) / channels);
                }

                // copy over the remaining sample data
                buffer.Slice(0, available)
                .CopyFrom(samples.Slice(offset, available));
                buffer2.Slice(0, available)
                .CopyFrom(samples.Slice(offset + samples.Length / channels, available));
                read += available;

                // reset the sample
                offset = 0;

                // stop if we are not set to loop
                if (!loop)
                {
                    stopped = true;
                    return(read / channels);
                }

                if (buffer.Length == available)
                {
                    return(read / channels);
                }

                // advance the buffer forward
                buffer = buffer.Slice(available);
            }
        }
Ejemplo n.º 4
0
        public void ClearTrajectory(NativeSlice <AffineTransform> trajectory)
        {
            trajectory.CopyFrom(TrajectoryArray);

            int halfTrajectoryLength = TrajectoryArray.Length / 2;

            for (int i = halfTrajectoryLength; i < trajectory.Length; ++i)
            {
                trajectory[i] = AffineTransform.identity;
            }
        }
Ejemplo n.º 5
0
        public void SetTriangles(int[] triangles, int submesh)
        {
            if (submesh >= subMeshCount)
            {
                subMeshCount = submesh + 1;
            }

            var preSliceLength = m_SubmeshOffset[submesh];

            if (preSliceLength < 0)
            {
                if (submesh > 0)
                {
                    m_SubmeshOffset[submesh] = trianglesCount;
                    preSliceLength           = trianglesCount;
                }
                else
                {
                    m_SubmeshOffset[submesh] = 0;
                    preSliceLength           = 0;
                }
            }
            var totalCount = preSliceLength; // count prior to submesh

            totalCount += triangles.Length;  // new submesh triangle count

            var postSliceOffset = 0;
            var postSliceLength = 0;

            if (submesh < subMeshCount - 2) // count of all triangles after submesh
            {
                postSliceOffset = m_SubmeshOffset[submesh + 1];
                if (postSliceOffset >= 0)
                {
                    postSliceLength = trianglesCount - postSliceOffset;
                    totalCount     += postSliceLength;
                }
            }

            trianglesCount = totalCount;

            // Shift other following triangles up/down
            if (postSliceOffset > 0)
            {
                var offset = preSliceLength + triangles.Length;
                m_SubmeshOffset[submesh + 1] = offset;
                var sourceSlice = new NativeSlice <int>(m_Triangles, postSliceOffset, postSliceLength);
                var destSlice   = new NativeSlice <int>(m_Triangles, offset, postSliceLength);
                destSlice.CopyFrom(sourceSlice);
            }

            m_Triangles.Slice(preSliceLength, triangles.Length).CopyFrom(triangles);
        }
        public WriteTextureJob(NativeArray <Color> TextureData, GenerationLayer[] layers, bool is_3d, int resolution, int x_tile, NativeArray <float>[] ValueBuffers)
        {
            this.TextureData = TextureData;
            this.layers      = new NativeArray <GenerationLayer>(layers, Allocator.Persistent);
            this.is_3d       = is_3d;
            this.resolution  = resolution;
            this.pixel_count = resolution * resolution * ((is_3d) ? resolution : 1);
            this.x_tile      = x_tile;

            this.ValueBuffer = new NativeArray <float>(layers.Length * pixel_count, Allocator.Persistent);
            for (int i = 0; i < layers.Length; i++)
            {
                NativeSlice <float> layer_slice  = new NativeSlice <float>(ValueBuffers[i]);
                NativeSlice <float> buffer_slice = new NativeSlice <float>(this.ValueBuffer, i * pixel_count, pixel_count);

                buffer_slice.CopyFrom(layer_slice);
            }
        }
Ejemplo n.º 7
0
    public void NativeSlice_Performance_CopyFrom()
    {
        const int numElements = 16 << 10;

        NativeArray <int> array = new NativeArray <int>(numElements, Allocator.Persistent);
        var slice = new NativeSlice <int>(array, 0, numElements);

        var copyToArray = new int[numElements];

        Measure.Method(() =>
        {
            slice.CopyFrom(copyToArray);
        })
        .WarmupCount(100)
        .MeasurementCount(1000)
        .Run();

        array.Dispose();
    }
Ejemplo n.º 8
0
        /// <summary>
        ///     Adds a sparse/dense index pair to the set and expands the arrays if necessary.
        /// </summary>
        /// <param name="expandBy">How many indices to expand by.</param>
        /// <param name="sparseIndex">The sparse index allocated.</param>
        /// <param name="denseIndex">The dense index allocated.</param>
        /// <param name="allocator">The <see cref="Unity.Collections.Allocator" /> type to use.</param>
        /// <param name="nativeArrayOptions">Should the memory be cleared on allocation?</param>
        /// <returns>True if the index pool expanded.</returns>
        public bool AddWithExpandCheck(int expandBy, out int sparseIndex, out int denseIndex, Allocator allocator,
                                       NativeArrayOptions nativeArrayOptions)
        {
            int  indexToClaim    = FreeIndex;
            int  currentCapacity = SparseArray.Length;
            bool needsExpansion  = false;

            if (indexToClaim >= currentCapacity)
            {
                // We're out of space, the last free index points to nothing. Allocate more indices.
                needsExpansion = true;

                int newCapacity = currentCapacity + expandBy;

                NativeArray <int> newSparseArray      = new NativeArray <int>(newCapacity, allocator, nativeArrayOptions);
                NativeSlice <int> newSparseArraySlice = new NativeSlice <int>(newSparseArray, 0, currentCapacity);
                newSparseArraySlice.CopyFrom(SparseArray);
                SparseArray = newSparseArray;

                NativeArray <int> newDenseArray      = new NativeArray <int>(newCapacity, allocator, nativeArrayOptions);
                NativeSlice <int> newDenseArraySlice = new NativeSlice <int>(newDenseArray, 0, currentCapacity);
                newDenseArraySlice.CopyFrom(DenseArray);
                DenseArray = newDenseArray;

                for (int i = currentCapacity; i < newCapacity; i++)
                {
                    SparseArray[i] = i + 1; // Build the free list chain.
                    DenseArray[i]  = -1;    // Set new dense indices as unclaimed.
                }
            }

            int nextFreeIndex = SparseArray[indexToClaim];

            DenseArray[Count]         = indexToClaim; // Point the next dense id at our newly claimed sparse index.
            SparseArray[indexToClaim] = Count;        // Point our newly claimed sparse index at the dense index.
            denseIndex = Count;

            ++Count;
            FreeIndex = nextFreeIndex; // Set the free list head for next time.

            sparseIndex = indexToClaim;
            return(needsExpansion);
        }
Ejemplo n.º 9
0
        internal static void Copy(NativeSlice <T> dst, T[] src, int length)
        {
            NativeSlice <T> dstSet = new NativeSlice <T>(dst, 0, length);

            dstSet.CopyFrom(src);
        }
Ejemplo n.º 10
0
            public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
            {
                NativeArray <NavigationGridData>  navigationGrids    = chunk.GetNativeArray(this.navigationGridType);
                BufferAccessor <AStarNodeElement> nodeBufferAccessor = chunk.GetBufferAccessor(this.nodeBufferType);

                for (int ci = 0, cn = chunk.Count; ci < cn; ci++)
                {
                    NavigationGridData navigationGrid           = outNavigationGrid[0] = navigationGrids[ci];
                    DynamicBuffer <AStarNodeElement> nodeBuffer = nodeBufferAccessor[ci];

                    // Solve path if requested

                    if (navigationGrid.pathRequested)
                    {
                        // All nodes
                        NativeArray <AStarNode> nodes = nodeBuffer.Reinterpret <AStarNode>().ToNativeArray(Allocator.Temp);

                        // Create temp grid
                        AStarGrid aStarGrid = new AStarGrid(navigationGrid.lengthX, navigationGrid.lengthY, nodes);

                        // Start/end nodes
                        AStarNode start = navigationGrid.pathStart;
                        AStarNode end   = navigationGrid.pathEnd;

                        // Solve path
                        NativeList <AStarNode> pathNodeList = new NativeList <AStarNode>(Allocator.Temp);
                        bool pathFound  = AStarSolver.SolvePath(aStarGrid, start, end, ref pathNodeList);
                        int  pathLength = pathNodeList.Length;
                        if (pathFound && navigationGrid.pathMustBeStraightLine)
                        {
                            // Check if path is straight line if specfied as a requirement
                            bool xDiverge = false;
                            bool yDiverge = false;
                            for (int i = 1, n = pathNodeList.Length; i < n; i++)
                            {
                                if (!xDiverge)
                                {
                                    xDiverge = pathNodeList[i].XCoord != pathNodeList[i - 1].XCoord;
                                }
                                if (!yDiverge)
                                {
                                    yDiverge = pathNodeList[i].YCoord != pathNodeList[i - 1].YCoord;
                                }
                                if (xDiverge && yDiverge)
                                {
                                    pathFound = false;
                                    break;
                                }
                            }
                        }


                        // Copy path node list to output array
                        NativeSlice <AStarNode> pathNodeListSlice = new NativeSlice <AStarNode>(pathNodeList.AsArray());
                        NativeSlice <AStarNode> pathNodeSlice     = new NativeSlice <AStarNode>(this.outPathNodes, 0, pathLength);
                        pathNodeSlice.CopyFrom(pathNodeListSlice);

                        // Dispose native containers
                        pathNodeList.Dispose();
                        aStarGrid.Dispose();
                        nodes.Dispose();

                        this.outPathFound[0]                  = pathFound ? 1 : 0;
                        this.outPathFound[1]                  = pathLength;
                        navigationGrid.pathRequested          = false;
                        navigationGrid.pathMustBeStraightLine = false;

                        // Apply changes
                        navigationGrids[ci] = navigationGrid;
                    }
                }
            }