Esempio n. 1
0
 /// <summary>
 /// Copies a managed Vector2 array into a native float2 array. Array lengths MUST be the same.
 /// </summary>
 public unsafe static void MemCpy(this Vector2[] source, NativeArray <float2> destination)
 {
     fixed(void *managedArrayPointer = source)
     {
         UnsafeUtility.MemCpy
         (
             destination: NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(destination),
             source: managedArrayPointer,
             size: source.Length * (long)UnsafeUtility.SizeOf <Vector2> ()
         );
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Copies a native float4 array into a managed Color array. Array lengths MUST be the same.
 /// </summary>
 public unsafe static void MemCpy(this NativeArray <float4> source, Color[] destination)
 {
     fixed(void *managedArrayPointer = destination)
     {
         UnsafeUtility.MemCpy
         (
             destination: managedArrayPointer,
             source: NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(source),
             size: destination.Length * (long)UnsafeUtility.SizeOf <Color> ()
         );
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Loads data from disk for a single node.
        /// </summary>
        /// <param name="node">Node for which data should be loaded.</param>
        /// <returns>True if load was successful, false otherwise.</returns>
        private bool ProcessLoad(Node node)
        {
            var nodePath = Path.Combine(owner.PathOnDisk, node.Identifier + TreeUtility.NodeFileExtension);

            if (!File.Exists(nodePath))
            {
                Debug.LogError($"Data for node {node.Identifier} not found.");
                return(false);
            }

            try
            {
                var size               = new FileInfo(nodePath).Length;
                var itemSize           = UnsafeUtility.SizeOf <PointCloudPoint>();
                var expectedPointCount = owner.NodeRecords[node.Identifier].PointCount;

                if (size != itemSize * expectedPointCount)
                {
                    Debug.LogError($"Mismatch between declared ({expectedPointCount}) and actual ({size/itemSize}) point count for node {node.Identifier}.");
                    return(false);
                }

                using (var mmf = MemoryMappedFile.CreateFromFile(nodePath, FileMode.Open))
                {
                    using (var accessor = mmf.CreateViewAccessor(0, size))
                    {
                        unsafe
                        {
                            byte *sourcePtr = null;
                            accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref sourcePtr);

                            try
                            {
                                var targetPtr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(node.Points);
                                UnsafeUtility.MemCpy(targetPtr, sourcePtr, size);
                            }
                            finally
                            {
                                accessor.SafeMemoryMappedViewHandle.ReleasePointer();
                            }
                        }
                    }
                }

                return(true);
            }
            catch (IOException e)
            {
                Debug.LogError(e.Message);
                return(false);
            }
        }
    void GenerateVoxelDensity()
    {
        if (enableJob)
        {
            NativeArray <Voxel> nativeVoxels = new NativeArray <Voxel>(gridSize.x * gridSize.y * gridSize.z, Allocator.TempJob);

            VoxelNoiseJob noiseJob = new VoxelNoiseJob
            {
                cellSize    = cellSize,
                gridSize    = gridSize,
                voxels      = nativeVoxels,
                frequency   = frequency,
                octaves     = octaves,
                worldOffset = worldOffset
            };
            JobHandle noiseJobHandle = noiseJob.Schedule(gridSize.x * gridSize.y * gridSize.z, 32);
            noiseJobHandle.Complete();

            unsafe
            {
                fixed(void *voxelPointer = voxels)
                {
                    UnsafeUtility.MemCpy(voxelPointer, NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeVoxels), voxels.Length * (long)UnsafeUtility.SizeOf <Voxel>());
                }
            }

            nativeVoxels.Dispose();
        }
        else
        {
            for (int x = 1; x < cellSize.x; x++)
            {
                for (int y = 1; y < cellSize.y; y++)
                {
                    for (int z = 1; z < cellSize.z; z++)
                    {
                        Vector3 worldPosition = new Vector3(x, y, z) + worldOffset;

                        Vector3 World2DPosition = new Vector3(worldPosition.x, worldPosition.z);

                        float density = -worldPosition.y + 30f;
                        density += Noise.Perlin2DFractal(World2DPosition, frequency, octaves) * 10f;
                        density += Noise.Perlin2DFractal(World2DPosition, frequency * 0.5f, octaves) * 80f;
                        float height = Mathf.Clamp01((worldPosition.y - 5f) / 30f);

                        voxels[x, y, z].Density = density;
                        voxels[x, y, z].Height  = height;
                    }
                }
            }
        }
    }
 public static NativeArray <byte> EncodeNativeArrayToPNG <T>(NativeArray <T> input, GraphicsFormat format, uint width, uint height, uint rowBytes = 0) where T : struct
 {
     unsafe
     {
         var size   = input.Length * UnsafeUtility.SizeOf <T>();
         var result = UnsafeEncodeNativeArrayToPNG(NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks <T>(input), ref size, format, width, height, rowBytes);
         var output = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <byte>(result, size, Allocator.Persistent);
         var safety = AtomicSafetyHandle.Create();
         NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref output, safety);
         AtomicSafetyHandle.SetAllowReadOrWriteAccess(safety, true);
         return(output);
     }
 }
Esempio n. 6
0
 public static unsafe void CopyTweenBatchDirectlyToNativeArray(
     TweenTransformBatchState[] sourceArray,
     NativeArray <TweenTransformBatchState> destinationArray,
     int length)
 {
     fixed(void *arrayPointer = sourceArray)
     {
         UnsafeUtility.MemCpy(
             NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(destinationArray),
             arrayPointer,
             length * TweenTransformBatchState.SizeOf());
     }
 }
 public virtual JobHandle Initialize(JobHandle inputDeps, float deltaTime)
 {
     if (lambdas.IsCreated)
     {
         // no need for jobs here, memclear is faster and we don't pay scheduling overhead.
         unsafe
         {
             UnsafeUtility.MemClear(NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(lambdas),
                                    lambdas.Length * UnsafeUtility.SizeOf <float>());
         }
     }
     return(inputDeps);
 }
        static unsafe NativeArray <Vector3> GetNativeVertexArrays(Vector3[] vertexArray)
        {
            var verts = new NativeArray <Vector3>(vertexArray.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            fixed(void *vertexBufferPointer = vertexArray)
            {
                // ...and use memcpy to copy the Vector3[] into a NativeArray<floar3> without casting. whould be fast!
                UnsafeUtility.MemCpy(NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(verts),
                                     vertexBufferPointer, vertexArray.Length * (long)UnsafeUtility.SizeOf <Vector3>());
            }

            return(verts);
        }
Esempio n. 9
0
        public static unsafe Vector2[] NativeFloat2ToManagedVector2(NativeArray <float2> nativeBuffer)
        {
            Vector2[] vertexArray = new Vector2[nativeBuffer.Length];

            // pin the target vertex array and get a pointer to it
            fixed(void *vertexArrayPointer = vertexArray)
            {
                // memcopy the native array over the top
                UnsafeUtility.MemCpy(vertexArrayPointer, NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(nativeBuffer), vertexArray.Length * (long)UnsafeUtility.SizeOf <float2>());
            }

            return(vertexArray);
        }
Esempio n. 10
0
        public unsafe NativeArray <int> GetSharedValueIndexCountArray()
        {
            // Capacity cannot be changed, so offset is valid.
            var rawIndices = (int *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer) +
                             2 * m_Source.Length;
            var arr = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <int>(rawIndices, m_Source.Length,
                                                                                      Allocator.Invalid);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            SharedValueIndexCountArraySetSafetyHandle(ref arr);
#endif
            return(arr);
        }
Esempio n. 11
0
        /// <summary>
        /// Make space for an event of <paramref name="sizeInBytes"/> bytes and return a pointer to
        /// the memory for the event.
        /// </summary>
        /// <param name="sizeInBytes">Number of bytes to make available for the event including the event header (see <see cref="InputEvent"/>).</param>
        /// <param name="capacityIncrementInBytes">If the buffer needs to be reallocated to accommodate the event, number of
        ///     bytes to grow the buffer by.</param>
        /// <param name="allocator">If the buffer needs to be reallocated to accommodate the event, the type of allocation to
        /// use.</param>
        /// <returns>A pointer to a block of memory in <see cref="bufferPtr"/>. Store the event data here.</returns>
        /// <exception cref="ArgumentException"><paramref name="sizeInBytes"/> is less than the size needed for the
        /// header of an <see cref="InputEvent"/>. Will automatically be aligned to a multiple of 4.</exception>
        /// <remarks>
        /// Only <see cref="InputEvent.sizeInBytes"/> is initialized by this method. No other fields from the event's
        /// header are touched.
        ///
        /// The event will be appended to the buffer after the last event currently in the buffer (if any).
        /// </remarks>
        public InputEvent *AllocateEvent(int sizeInBytes, int capacityIncrementInBytes = 2048, Allocator allocator = Allocator.Persistent)
        {
            if (sizeInBytes < InputEvent.kBaseEventSize)
            {
                throw new ArgumentException(
                          $"sizeInBytes must be >= sizeof(InputEvent) == {InputEvent.kBaseEventSize} (was {sizeInBytes})",
                          nameof(sizeInBytes));
            }

            var alignedSizeInBytes = sizeInBytes.AlignToMultipleOf(InputEvent.kAlignment);

            // See if we need to enlarge our buffer.
            var necessaryCapacity = m_SizeInBytes + alignedSizeInBytes;
            var currentCapacity   = capacityInBytes;

            if (currentCapacity < necessaryCapacity)
            {
                // Yes, so reallocate.

                var newCapacity = necessaryCapacity.AlignToMultipleOf(capacityIncrementInBytes);
                if (newCapacity > int.MaxValue)
                {
                    throw new NotImplementedException("NativeArray long support");
                }
                var newBuffer =
                    new NativeArray <byte>((int)newCapacity, allocator, NativeArrayOptions.ClearMemory);

                if (m_Buffer.IsCreated)
                {
                    UnsafeUtility.MemCpy(newBuffer.GetUnsafePtr(),
                                         NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer),
                                         this.sizeInBytes);

                    if (m_WeOwnTheBuffer)
                    {
                        m_Buffer.Dispose();
                    }
                }

                m_Buffer         = newBuffer;
                m_WeOwnTheBuffer = true;
            }

            var eventPtr = (InputEvent *)((byte *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer) + m_SizeInBytes);

            eventPtr->sizeInBytes = (uint)sizeInBytes;
            m_SizeInBytes        += alignedSizeInBytes;
            ++m_EventCount;

            return(eventPtr);
        }
Esempio n. 12
0
        /// <summary>
        /// Loads data from disk for a single node.
        /// </summary>
        /// <param name="node">Node for which data should be loaded.</param>
        /// <returns>True if load was successful, false otherwise.</returns>
        private bool ProcessLoad(Node node)
        {
            owner.GetNodeData(node, out var nodePath, out var offset, out var size);

            if (!File.Exists(nodePath))
            {
                Debug.LogWarning($"Data for node {node.Identifier} not found.");
                return(false);
            }

            try
            {
                var itemSize           = UnsafeUtility.SizeOf <PointCloudPoint>();
                var expectedPointCount = owner.NodeRecords[node.Identifier].PointCount;

                if (size != itemSize * expectedPointCount)
                {
                    Debug.LogWarning($"Mismatch between declared ({expectedPointCount}) and actual ({size/itemSize}) point count for node {node.Identifier}.");
                    return(false);
                }

                using (var mmf = MemoryMappedFile.CreateFromFile(File.Open(nodePath, FileMode.Open, FileAccess.Read, FileShare.Read), null, 0L, MemoryMappedFileAccess.Read, HandleInheritability.None, false))
                {
                    using (var accessor = mmf.CreateViewAccessor(offset, size, MemoryMappedFileAccess.Read))
                    {
                        unsafe
                        {
                            byte *sourcePtr = null;
                            accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref sourcePtr);

                            try
                            {
                                var targetPtr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(node.Points);
                                UnsafeUtility.MemCpy(targetPtr, sourcePtr, size);
                            }
                            finally
                            {
                                accessor.SafeMemoryMappedViewHandle.ReleasePointer();
                            }
                        }
                    }
                }

                return(true);
            }
            catch (IOException e)
            {
                Debug.LogError(e.Message);
                return(false);
            }
        }
Esempio n. 13
0
        public static unsafe void CopyFromUnsafe <T>(this ComputeBuffer dst, ref ComponentDataArray <T> src, int length) where T : struct, IComponentData
        {
            var             stride = dst.stride;
            var             ptr    = dst.GetNativeBufferPtr();
            NativeArray <T> chunkArray;

            for (int copiedCount = 0, chunkLength = 0; copiedCount < length; copiedCount += chunkLength)
            {
                chunkArray  = src.GetChunkArray(copiedCount, length - copiedCount);
                chunkLength = chunkArray.Length;
                UnsafeUtility.MemCpy(ptr.ToPointer(), NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(chunkArray), chunkLength * stride);
                ptr += chunkLength * stride;
            }
        }
        //---------------------------------------------


        //From https://gist.github.com/LotteMakesStuff/c2f9b764b15f74d14c00ceb4214356b4
        static unsafe void GetNativeArray(NativeArray <float3> posNativ, Vector3[] posArray)
        {
            // pin the buffer in place...
            fixed(void *bufferPointer = posArray)
            {
                // ...and use memcpy to copy the Vector3[] into a NativeArray<floar3> without casting. whould be fast!
                UnsafeUtility.MemCpy(NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(posNativ),
                                     bufferPointer, posArray.Length * (long)UnsafeUtility.SizeOf <float3>());
            }

            // we only have to fix the .net array in place, the NativeArray is allocated in the C++ side of the engine and
            // wont move arround unexpectedly. We have a pointer to it not a reference! thats basically what fixed does,
            // we create a scope where its 'safe' to get a pointer and directly manipulate the array
        }
        private static readonly int    kMaxIntersectionTolerance = 4; // Maximum Intersection Tolerance per Intersection Loop Check.

        internal static void RemoveDuplicateEdges(ref NativeArray <int2> edges, ref int edgeCount, NativeArray <int> duplicates, int duplicateCount)
        {
            if (duplicateCount == 0)
            {
                for (var i = 0; i < edgeCount; ++i)
                {
                    var e = edges[i];
                    e.x      = math.min(edges[i].x, edges[i].y);
                    e.y      = math.max(edges[i].x, edges[i].y);
                    edges[i] = e;
                }
            }
            else
            {
                for (var i = 0; i < edgeCount; ++i)
                {
                    var e = edges[i];
                    var a = duplicates[e.x];
                    var b = duplicates[e.y];
                    e.x      = math.min(a, b);
                    e.y      = math.max(a, b);
                    edges[i] = e;
                }
            }

            unsafe
            {
                UTess.InsertionSort <int2, TessEdgeCompare>(
                    NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(edges), 0, edgeCount - 1,
                    new TessEdgeCompare());
            }

            var n = 1;

            for (var i = 1; i < edgeCount; ++i)
            {
                var prev = edges[i - 1];
                var next = edges[i];
                if (next.x == prev.x && next.y == prev.y)
                {
                    continue;
                }
                if (next.x == next.y)
                {
                    continue;
                }
                edges[n++] = next;
            }
            edgeCount = n;
        }
        /// <summary>
        ///     Indices into shared values.
        ///     For example, given source array: [A,A,A,B,B,C,C,A,B]
        ///     shared values are: [A,B,C]
        ///     shared index array would contain: [0,0,0,1,1,2,2,0,1]
        /// </summary>
        /// <returns>Index NativeArray where each element refers to the index of a shared value in a list of shared (unique) values.</returns>
        public unsafe NativeArray <int> GetSharedIndexArray()
        {
            // Capacity cannot be changed, so offset is valid.
            var rawIndices = (int *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_WorkingBuffer) +
                             (m_SortBufferIndex ^ 1) * m_SourceBuffer.Length;
            // Get array that maps to the currently unused sort buffer (e.g. if the current sortBufferIndex is 1, return sortBuffer0)
            var arr = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <int>(rawIndices, m_SourceBuffer.Length,
                                                                                      Allocator.Invalid);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            SharedIndexArraySetSafetyHandle(ref arr);
#endif
            return(arr);
        }
            public static DeferredBlittableArray Create <T>(NativeList <T> list)
                where T : struct
            {
                var deferred = list.AsDeferredJobArray();
                DeferredBlittableArray ret;

                ret.m_Ptr = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(deferred);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                ret.m_Safety = NativeArrayUnsafeUtility.GetAtomicSafetyHandle(deferred);
#endif

                return(ret);
            }
Esempio n. 18
0
        public InputEventStream(ref InputEventBuffer eventBuffer)
        {
            m_CurrentNativeEventWritePtr = m_CurrentNativeEventReadPtr =
                (InputEvent *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(eventBuffer.data);

            m_NativeBuffer = eventBuffer;
            m_RemainingNativeEventCount = m_NativeBuffer.eventCount;
            m_NumEventsRetainedInBuffer = 0;

            m_CurrentAppendEventReadPtr = m_CurrentAppendEventWritePtr = default;
            m_AppendBuffer = default;
            m_RemainingAppendEventCount = 0;

            m_IsOpen = true;
        }
Esempio n. 19
0
        /// <summary>
        ///     Array of indices into source NativeArray which share the same shared value
        ///     For example, given source array: [A,A,A,B,B,C,C,A,B]
        ///     shared values are: [A,B,C]
        ///     Shared value counts: [4,3,2] (number of occurrences of a shared value)
        ///     Shared value start offsets (into sorted indices): [0,4,7]
        ///     Given the index 0 into the shared value array (A), the returned array would contain [0,1,2,7] (indices into the source array which point to the shared value A).
        /// </summary>
        /// <param name="sharedValueIndex">Index of shared value</param>
        /// <returns>Index NativeArray where each element refers to an index into the source array.</returns>
        public unsafe NativeArray <int> GetSharedValueIndicesBySharedIndex(int sharedValueIndex)
        {
            var sortedSourceIndex     = m_SortedSourceIndexBySharedIndex[sharedValueIndex];
            var sharedValueIndexCount = m_SharedIndexCountsBySharedIndex[sharedValueIndex];

            // Capacity cannot be changed, so offset is valid.
            var rawIndices =
                ((int *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(
                     m_SourceIndexBySortedSourceIndex)) + sortedSourceIndex;
            var arr = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <int>(rawIndices, sharedValueIndexCount, Allocator.None);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            SharedValueIndicesSetSafetyHandle(ref arr);
#endif
            return(arr);
        }
Esempio n. 20
0
        public void SeekFrame(int frameIndex)
        {
            ASSERT_BUFFERS();

#if VERBOSE
            Debug.Log("SEEK " + frameIndex);
#endif

            // sanitize user input
            frameIndex = Mathf.Clamp(frameIndex, 0, frameCount - 1);

            // expand range within bounds
            int readIndexLo = Mathf.Max(frameIndex - seekRadius, 0);
            int readIndexHi = Mathf.Min(frameIndex + seekRadius, frameCount - 1);

            // schedule each read individually
            var ringDataPtr = (byte *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(ringData);
            for (int readIndex = readIndexLo; readIndex <= readIndexHi; readIndex++)
            {
                // map frame to index in ring
                int ringIndex = (readIndex & (ringCapacityPow2 - 1));

                // if frame is not already in ring
                if (ringDataTag[ringIndex] != readIndex)
                {
                    ringDataTag[ringIndex] = readIndex;

                    // wait for pending element operation
                    var dataHnd = ringDataHnd[ringIndex];
                    if (dataHnd.IsValid() && dataHnd.Status == ReadStatus.InProgress)
                    {
                        dataHnd.JobHandle.Complete();
                    }

                    // schedule the read
                    ReadCommand cmd;
                    cmd.Buffer             = frameSize * ringIndex + ringDataPtr;
                    cmd.Offset             = frameSize * (long)readIndex + frameOffset;
                    cmd.Size               = frameSize;
                    ringDataHnd[ringIndex] = AsyncReadManager.Read(filename, &cmd, 1);

#if VERBOSE
                    Debug.Log("schedule " + frameIndex + " -> ringIndex " + ringIndex);
#endif
                }
            }
        }
Esempio n. 21
0
        public unsafe JobHandle GetEntityQueryMatchDiffAsync(EntityQuery query, NativeList <Entity> newEntities, NativeList <Entity> missingEntities)
        {
            newEntities.Clear();
            missingEntities.Clear();

            var queryMask = m_World.EntityManager.GetEntityQueryMask(query);

            var chunks          = query.CreateArchetypeChunkArrayAsync(Allocator.TempJob, out var chunksJobHandle);
            var gatheredChanges = new NativeArray <ChangesCollector>(chunks.Length, Allocator.TempJob);
            var allocatedShadowChunksForTheFrame = new NativeArray <ShadowChunk>(chunks.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var removedChunkEntities             = new NativeList <Entity>(Allocator.TempJob);

            var gatherEntityChangesJob = new GatherEntityChangesJob
            {
                QueryMask = queryMask,
                Chunks    = chunks,
                ShadowChunksBySequenceNumber = m_ShadowChunks,
                GatheredChanges = (ChangesCollector *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(gatheredChanges)
            }.Schedule(chunks.Length, 1, chunksJobHandle);

            var allocateNewShadowChunksJobHandle = new AllocateNewShadowChunksJob
            {
                QueryMask = queryMask,
                Chunks    = chunks,
                ShadowChunksBySequenceNumber = m_ShadowChunks,
                AllocatedShadowChunks        = (ShadowChunk *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(allocatedShadowChunksForTheFrame)
            }.Schedule(chunks.Length, 1, chunksJobHandle);

            var copyJobHandle = new CopyEntityDataJob
            {
                QueryMask = queryMask,
                Chunks    = chunks,                                              // deallocate on job completion
                ShadowChunksBySequenceNumber = m_ShadowChunks,
                AllocatedShadowChunks        = allocatedShadowChunksForTheFrame, // deallocate on job completion
                RemovedChunkEntities         = removedChunkEntities
            }.Schedule(JobHandle.CombineDependencies(gatherEntityChangesJob, allocateNewShadowChunksJobHandle));

            var concatResults = new ConcatResultsJob
            {
                GatheredChanges      = gatheredChanges, // deallocate on job completion
                RemovedChunkEntities = removedChunkEntities.AsDeferredJobArray(),
                AddedEntities        = newEntities,
                RemovedEntities      = missingEntities
            }.Schedule(copyJobHandle);

            return(removedChunkEntities.Dispose(concatResults));
        }
Esempio n. 22
0
        protected override unsafe void OnUpdate()
        {
            var typeMoveCommand     = GetArchetypeChunkComponentType <MoveCommand>(true);
            var typeDestroyable     = GetArchetypeChunkComponentType <DestroyableComponentData>();
            var typeTicks           = GetArchetypeChunkComponentType <DateTimeTicksToProcess>(true);
            var typeTeamTag         = GetArchetypeChunkComponentType <TeamTag>(true);
            var typePhysicsVelocity = GetArchetypeChunkComponentType <PhysicsVelocity>();
            var currentTicks        = DateTime.Now.Ticks;

            using (var moveCommandChunks = _queryMoveCommand.CreateArchetypeChunkArray(Allocator.TempJob))
                using (var velocitiesChunks = _query.CreateArchetypeChunkArray(Allocator.TempJob))
                {
                    foreach (var moveCommandChunk in moveCommandChunks)
                    {
                        var moveCommands = moveCommandChunk.GetNativeArray(typeMoveCommand);
                        var destroys     = moveCommandChunk.GetNativeArray(typeDestroyable);
                        var ticks        = moveCommandChunk.GetNativeArray(typeTicks);
                        for (var i = 0; i < ticks.Length; i++)
                        {
                            if (ticks[i].Value < currentTicks || destroys[i].ShouldDestroy)
                            {
                                continue;
                            }
                            destroys[i] = new DestroyableComponentData()
                            {
                                ShouldDestroy = true
                            };
                            foreach (var velocitiesChunk in velocitiesChunks)
                            {
                                var teamTags          = velocitiesChunk.GetNativeArray(typeTeamTag);
                                var physicsVelocities = velocitiesChunk.GetNativeArray(typePhysicsVelocity);
                                for (var j = 0; j < teamTags.Length; j++)
                                {
                                    if (teamTags[j].Id != moveCommands[i].Id)
                                    {
                                        continue;
                                    }
                                    UnsafeUtilityEx
                                    .ArrayElementAsRef <PhysicsVelocity>(NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(physicsVelocities), j)
                                    .Linear += moveCommands[i].DeltaVelocity;
                                }
                            }
                        }
                    }
                }
        }
        unsafe void OnBoundaryChanged(ARPlaneBoundaryChangedEventArgs eventArgs)
        {
            var boundary = this.m_Plane.boundary;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            try
            {
                NativeArray <Vector2> newBoundary = new NativeArray <Vector2>(boundary.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
                Vector2 *boundaryPointer          = (Vector2 *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(boundary);
                UnsafeUtility.MemCpy(NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(newBoundary), boundaryPointer, boundary.Length * (long)UnsafeUtility.SizeOf <Vector2>());
                boundary = newBoundary;
#endif
            if (!ARPlaneMeshGenerators.GenerateMesh(this.mesh, new Pose(this.transform.localPosition, this.transform.localRotation), boundary))
            {
                return;
            }

            var lineRenderer = this.GetComponent <LineRenderer>();
            if (lineRenderer != null)
            {
                lineRenderer.positionCount = boundary.Length;
                for (int i = 0; i < boundary.Length; ++i)
                {
                    var point2 = boundary[i];
                    lineRenderer.SetPosition(i, new Vector3(point2.x, 0, point2.y));
                }
            }

            var meshFilter = this.GetComponent <MeshFilter>();
            if (meshFilter != null)
            {
                meshFilter.sharedMesh = this.mesh;
            }

            var meshCollider = this.GetComponent <MeshCollider>();
            if (meshCollider != null)
            {
                meshCollider.sharedMesh = this.mesh;
            }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
        }

        finally
        { boundary.Dispose(); }
#endif
        }
        /// <summary>
        /// Array of indices into source NativeArray which share the same source value
        /// </summary>
        /// <param name="index">Index of shared value</param>
        /// <returns></returns>
        public unsafe NativeArray <int> GetSharedValueIndicesBySharedIndex(int index)
        {
            int sharedValueIndexCountOffset   = 2 * m_Source.Length;
            int sharedValueIndexCount         = m_Buffer[sharedValueIndexCountOffset + index];
            int sharedValueStartIndicesOffset = 3 * m_Source.Length;
            int sharedValueStartIndex         = m_Buffer[sharedValueStartIndicesOffset + index];
            int sortedValueOffset             = m_SortedBuffer * m_Source.Length;

            // Capacity cannot be changed, so offset is valid.
            int *rawIndices = (int *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_Buffer) + (sortedValueOffset + sharedValueStartIndex);
            var  arr        = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <int>(rawIndices, sharedValueIndexCount, Allocator.Invalid);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            SharedValueIndicesSetSafetyHandle(ref arr);
#endif
            return(arr);
        }
Esempio n. 25
0
            void Prepare(NativeArray <TessEdge> edgesIn)
            {
                m_Stars = new NativeArray <TessStar>(edgesIn.Length, Allocator.Temp);

                for (int i = 0; i < edgesIn.Length; ++i)
                {
                    TessEdge e = edgesIn[i];
                    e.a        = (edgesIn[i].a < edgesIn[i].b) ? edgesIn[i].a : edgesIn[i].b;
                    e.b        = (edgesIn[i].a > edgesIn[i].b) ? edgesIn[i].a : edgesIn[i].b;
                    edgesIn[i] = e;
                    TessStar s = m_Stars[i];
                    s.points     = new ArraySlice <int>(m_SPArray, i * m_StarCount, m_StarCount);
                    s.pointCount = 0;
                    m_Stars[i]   = s;
                }

                unsafe
                {
                    TessUtils.InsertionSort <TessEdge, TessEdgeCompare>(
                        NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(edgesIn), 0, edgesIn.Length - 1,
                        new TessEdgeCompare());
                }

                m_Edges = new NativeArray <TessEdge>(edgesIn.Length, Allocator.Temp);
                m_Edges.CopyFrom(edgesIn);

                // Fill stars.
                for (int i = 0; i < m_CellCount; ++i)
                {
                    int      a  = m_Cells[i].a;
                    int      b  = m_Cells[i].b;
                    int      c  = m_Cells[i].c;
                    TessStar sa = m_Stars[a];
                    TessStar sb = m_Stars[b];
                    TessStar sc = m_Stars[c];
                    sa.points[sa.pointCount++] = b;
                    sa.points[sa.pointCount++] = c;
                    sb.points[sb.pointCount++] = c;
                    sb.points[sb.pointCount++] = a;
                    sc.points[sc.pointCount++] = a;
                    sc.points[sc.pointCount++] = b;
                    m_Stars[a] = sa;
                    m_Stars[b] = sb;
                    m_Stars[c] = sc;
                }
            }
Esempio n. 26
0
        public unsafe void Destroy(ref Entity entity)
        {
            Assert.IsTrue(0 <= entity.Index && entity.Index < _capacity);
            var data = _dataManager.Get(entity.Index);

            if (data->ArcheType != null)
            {
                _componentBlockManager.Free(data->ArcheType, data->Chunk, data->IndexInChunk);
            }
            int idx = entity.Index;
            var buf = (Entity *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(_entities);

            buf[idx].Index = -1;
            entity.Index   = -1;
            _occupation.Clear(idx);
            --_count;
            _lastUnused = idx;
        }
        public unsafe static JobHandle ScheduleBatch(NativeArray <RaycastCommand> commands, NativeArray <RaycastHit> results, int minCommandsPerJob, int maxHits, JobHandle dependsOn = new JobHandle())
        {
            if (maxHits < 1)
            {
                Debug.LogWarning("maxHits should be greater than 0.");
                return(new JobHandle());
            }
            else if (results.Length < maxHits * commands.Length)
            {
                Debug.LogWarning("The supplied results buffer is too small, there should be at least maxHits space per each command in the batch.");
                return(new JobHandle());
            }

            var jobData        = new BatchQueryJob <RaycastCommand, RaycastHit>(commands, results);
            var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), BatchQueryJobStruct <BatchQueryJob <RaycastCommand, RaycastHit> > .Initialize(), dependsOn, ScheduleMode.Parallel);

            return(ScheduleRaycastBatch(ref scheduleParams, NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(commands), commands.Length, NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(results), results.Length, minCommandsPerJob, maxHits));
        }
Esempio n. 28
0
            public void SetSplitterJob(DrawingData gizmos, JobHandle splitterJob)
            {
                this.splitterJob = splitterJob;
                if (type == Type.Static)
                {
                    unsafe {
                        buildJob = CommandBuilder.Build(gizmos, (MeshBuffers *)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(temporaryMeshBuffers), null, splitterJob);
                    }

                    SubmittedJobs++;
                    // ScheduleBatchedJobs is expensive, so only do it once in a while
                    if (SubmittedJobs % 8 == 0)
                    {
                        Profiler.BeginSample("ScheduleJobs");
                        JobHandle.ScheduleBatchedJobs();
                        Profiler.EndSample();
                    }
                }
            }
        /// <summary>
        /// Commit a video frame from a texture.
        /// </summary>
        /// <param name="texture">Source texture.</param>
        /// <param name="timestamp">Frame timestamp in nanoseconds.</param>
        public unsafe void CommitFrame(Texture texture, long timestamp)
        {
            // Blit
            var(width, height) = recorder.frameSize;
            var renderTexture = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.ARGB32);

            Graphics.Blit(texture, renderTexture);
            // Readback // Completely kills performance
            var prevActive = RenderTexture.active;

            RenderTexture.active = renderTexture;
            readbackBuffer.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
            RenderTexture.active = prevActive;
            RenderTexture.ReleaseTemporary(renderTexture);
            // Commit
            recorder.CommitFrame(
                NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(readbackBuffer.GetRawTextureData <byte>()),
                timestamp
                );
        }
        /// <summary>
        /// Reinterpret a native array as being of another type, aliasing its contents via type punning.
        /// </summary>
        /// <param name="array">The array to alias</param>
        /// <typeparam name="T">Source type of array elements</typeparam>
        /// <typeparam name="U">Target type of array elements</typeparam>
        /// <returns>The same array, with a different type of element</returns>
        public static NativeArray <U> Reinterpret <T, U>(this NativeArray <T> array) where U : struct where T : struct
        {
            var tSize = UnsafeUtility.SizeOf <T>();
            var uSize = UnsafeUtility.SizeOf <U>();

            var byteLen = ((long)array.Length) * tSize;
            var uLen    = byteLen / uSize;

            CheckReinterpretSize <T, U>(ref array);

            var ptr    = NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(array);
            var result = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <U>(ptr, (int)uLen, Allocator.None);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            var handle = NativeArrayUnsafeUtility.GetAtomicSafetyHandle(array);
            NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref result, handle);
#endif

            return(result);
        }