Ejemplo n.º 1
0
    public unsafe void Send(NetworkStream stream, NativeSlice <byte> bytes)
    {
        ValidateAndThrow(stream);

        var basePtr        = new IntPtr(bytes.GetUnsafeReadOnlyPtr());
        int bytesRemaining = bytes.Length;
        int offset         = 0;

        while (bytesRemaining > 0)
        {
            // Memcpy next chunk into destinationBuffer
            int size = Mathf.Min(m_Buffer.Length, bytesRemaining);
            fixed(byte *dst = m_Buffer)
            {
                var src = basePtr + offset;

                UnsafeUtility.MemCpy(dst, (void *)src, size);
            }

            bytesRemaining -= size;
            offset         += size;

            Send(stream, 0, size);
        }
    }
 public static T Unarchive <T>(NativeSlice <byte> bytes) where T : struct, INSObject
 {
     unsafe
     {
         return(Unarchive <T>(bytes.GetUnsafeReadOnlyPtr(), bytes.Length));
     }
 }
Ejemplo n.º 3
0
        public void Execute()
        {
            DracoToUnityMesh *tmpMesh;

            result[0]  = DecodeMeshForUnity(data.GetUnsafeReadOnlyPtr(), data.Length, &tmpMesh);
            outMesh[0] = (IntPtr)tmpMesh;
        }
 public ARReferenceObject(NativeSlice <byte> bytes)
 {
     unsafe
     {
         this = InitWithBytes(bytes.GetUnsafeReadOnlyPtr(), bytes.Length);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="nativeSlice"></param>
 /// <param name="channels"></param>
 public void SetData(ref NativeSlice <float> nativeSlice, int channels, int sampleRate)
 {
     unsafe
     {
         void *ptr = nativeSlice.GetUnsafeReadOnlyPtr();
         NativeMethods.ProcessAudio(GetSelfOrThrow(), (IntPtr)ptr, sampleRate, channels, nativeSlice.Length);
     }
 }
Ejemplo n.º 6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="nativeSlice"></param>
 /// <param name="channels"></param>
 public void SetData(ref NativeSlice <float> nativeSlice, int channels, int sampleRate)
 {
     unsafe
     {
         void *ptr = nativeSlice.GetUnsafeReadOnlyPtr();
         ProcessAudio(_trackSource, (IntPtr)ptr, sampleRate, channels, nativeSlice.Length);
     }
 }
Ejemplo n.º 7
0
 public unsafe void Send <T>(NativeSlice <T> msg)
     where T : struct
 {
     if (ReadyState != RTCDataChannelState.Open)
     {
         throw new InvalidOperationException("DataChannel is not open");
     }
     NativeMethods.DataChannelSendPtr(GetSelfOrThrow(), new IntPtr(msg.GetUnsafeReadOnlyPtr()), msg.Length * UnsafeUtility.SizeOf <T>());
 }
Ejemplo n.º 8
0
        public unsafe void Execute(int row)
        {
            int   inputOffset     = (height - 1 - row) * width;
            int   outputOffset    = row * width;
            void *grayscaleInPtr  = ((byte *)grayscaleIn.GetUnsafeReadOnlyPtr()) + inputOffset;
            void *grayscaleOutPtr = ((byte *)grayscaleOut.GetUnsafePtr()) + outputOffset;

            UnsafeUtility.MemCpy(grayscaleOutPtr, grayscaleInPtr, width);
        }
        public unsafe NatliveSliceIntPtr(NativeSlice <T> nativeSlice)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            safetyHandle = NativeSliceUnsafeUtility.GetAtomicSafetyHandle(nativeSlice);
#endif
            data   = new IntPtr(nativeSlice.GetUnsafeReadOnlyPtr());
            length = nativeSlice.Length;
            stride = nativeSlice.Stride;
        }
        public unsafe void MoveLocationsInSameAreas(NativeSlice <NavMeshLocation> locations, NativeSlice <Vector3> targets, int areaMask = NavMesh.AllAreas)
        {
            if (locations.Length != targets.Length)
            {
                throw new ArgumentException("locations.Length and targets.Length must be equal");
            }

            AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
            MoveLocationsInSameAreas(m_NavMeshQuery, locations.GetUnsafePtr(), targets.GetUnsafeReadOnlyPtr(), locations.Length, areaMask);
        }
        public unsafe bool Open(NativeSlice <byte> data)
        {
            void *src     = data.GetUnsafeReadOnlyPtr();
            bool  success = ktx_basisu_open_basis(nativeReference, src, data.Length);

            if (!success)
            {
                Debug.LogError("Couldn't validate BasisU header!");
            }
            return(success);
        }
Ejemplo n.º 12
0
        public unsafe NativeSlice <byte> Receive(NetworkPipelineContext ctx, NativeSlice <byte> inboundBuffer, ref bool needsResume, ref bool needsUpdate, ref bool needsSendUpdate)
        {
            var headerData = (int *)inboundBuffer.GetUnsafeReadOnlyPtr();

            if (*headerData != 2)
            {
                throw new InvalidOperationException("Header data invalid, got " + *headerData);
            }

            return(new NativeSlice <byte>(inboundBuffer, 4, inboundBuffer.Length - 4));
        }
Ejemplo n.º 13
0
        static unsafe void CopyTo(NativeSlice <VisibleLocalToWorld> transforms, int count, Matrix4x4[] outMatrices, int offset)
        {
            // @TODO: This is using unsafe code because the Unity DrawInstances API takes a Matrix4x4[] instead of NativeArray.
            Assert.AreEqual(sizeof(Matrix4x4), sizeof(VisibleLocalToWorld));
            fixed(Matrix4x4 *resultMatrices = outMatrices)
            {
                VisibleLocalToWorld *sourceMatrices = (VisibleLocalToWorld *)transforms.GetUnsafeReadOnlyPtr();

                UnsafeUtility.MemCpy(resultMatrices + offset, sourceMatrices, UnsafeUtility.SizeOf <Matrix4x4>() * count);
            }
        }
        static unsafe bool NativeArrayMemCmp <T>(NativeSlice <T> array, byte[] buffer)
            where T : struct
        {
            if (array.Length * UnsafeUtility.SizeOf <T>() == buffer.Length)
            {
                var nativeArrayIntPtr = new IntPtr(array.GetUnsafeReadOnlyPtr());
                return(IntPtrMemCmp(nativeArrayIntPtr, buffer));
            }

            return(false);
        }
Ejemplo n.º 15
0
 public static unsafe void UnsafeArrayCopy <T, U>(NativeSlice <T> src, List <U> dst) where T : struct
     where U : struct
 {
     EnsureListElemCount(dst, src.Length);
     if (src.Length > 0)
     {
         U[]   asArray = (U[])ExtractArrayFromList(dst);
         void *vPtr    = UnsafeUtility.AddressOf(ref asArray[0]);
         using (new ProfilerSample("UnsafeArrayCopy.ActualCopy"))
             UnsafeUtility.MemCpy(vPtr, src.GetUnsafeReadOnlyPtr(), src.Length * UnsafeUtility.SizeOf <T>());
     }
 }
        /*
         * KtxClassId classId {
         *  get {
         *      return ktx_get_classId(nativeReference);
         *  }
         * }
         * bool isArray {
         *  get {
         *      return ktx_get_isArray(nativeReference);
         *  }
         * }
         * bool isCubemap {
         *  get {
         *      return ktx_get_isCubemap(nativeReference);
         *  }
         * }
         * bool isCompressed {
         *  get {
         *      return ktx_get_isCompressed(nativeReference);
         *  }
         * }
         * uint numDimensions {
         *  get {
         *      return ktx_get_numDimensions(nativeReference);
         *  }
         * }
         *
         * uint numLayers {
         *  get {
         *      return ktx_get_numLayers(nativeReference);
         *  }
         * }
         * uint numFaces {
         *  get {
         *      return ktx_get_numFaces(nativeReference);
         *  }
         * }
         * uint vkFormat {
         *  get {
         *      return ktx_get_vkFormat(nativeReference);
         *  }
         * }
         * KtxSupercmpScheme supercompressionScheme {
         *  get {
         *      return ktx_get_supercompressionScheme(nativeReference);
         *  }
         * }
         * //*/

        unsafe bool Load(NativeSlice <byte> data)
        {
            var          src = data.GetUnsafeReadOnlyPtr();
            KtxErrorCode status;

            nativeReference = ktx_load_ktx(src, data.Length, out status);
            if (status != KtxErrorCode.KTX_SUCCESS)
            {
                Debug.LogErrorFormat("KTX error code {0}", status);
                return(false);
            }
            return(true);
        }
Ejemplo n.º 17
0
        public DataStreamReader(NativeSlice <byte> slice)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Safety = NativeSliceUnsafeUtility.GetAtomicSafetyHandle(slice);
#endif
            m_bufferPtr = (byte *)slice.GetUnsafeReadOnlyPtr();
            m_Length    = slice.Length;

            uint test = 1;
            unsafe
            {
                byte *test_b = (byte *)&test;
                IsLittleEndian = test_b[0] == 1;
            }
        }
        public static unsafe void CopyFromNativeSlice <T, S>(this NativeArray <T> nativeArray, int dstStartIndex, int dstEndIndex, NativeSlice <S> slice, int srcStartIndex, int srcEndIndex) where T : struct where S : struct
        {
            if ((dstEndIndex - dstStartIndex) != (srcEndIndex - srcStartIndex))
            {
                throw new System.ArgumentException($"Destination and Source copy counts must match.", nameof(slice));
            }

            var dstSizeOf = UnsafeUtility.SizeOf <T>();
            var srcSizeOf = UnsafeUtility.SizeOf <T>();

            byte *srcPtr = (byte *)slice.GetUnsafeReadOnlyPtr();

            srcPtr = srcPtr + (srcStartIndex * srcSizeOf);
            byte *dstPtr = (byte *)nativeArray.GetUnsafePtr();

            dstPtr = dstPtr + (dstStartIndex * dstSizeOf);
            UnsafeUtility.MemCpyStride(dstPtr, srcSizeOf, srcPtr, slice.Stride, dstSizeOf, srcEndIndex - srcStartIndex);
        }
Ejemplo n.º 19
0
        public static void CopyTo <T>(NativeSlice <T> slice, T[] destination) where T : struct
        {
            if (slice.Length != destination.Length)
            {
                throw new ArgumentException("Source and destination arrays must have the same length");
            }

            try {
                var stride    = slice.Stride;
                var bufferPtr = slice.GetUnsafeReadOnlyPtr();
                for (var i = 0; i < destination.Length; ++i)
                {
                    destination[i] = UnsafeUtility.ReadArrayElementWithStride <T>(bufferPtr, i, stride);
                }
            }
            catch (Exception e) {
                Debug.LogWarning("Failed to make a direct copy. Falling back to CopyTo method. Exception was: " + e);
                slice.CopyTo(destination);
            }
        }
Ejemplo n.º 20
0
    public static unsafe void GatherReliabilityStats(ref SoakStatisticsPoint stats, ref SoakStatisticsPoint lastStats, UdpNetworkDriver driver,
                                                     NetworkPipeline pipeline, NetworkConnection con, long timestamp)
    {
        NativeSlice <byte> receiveBuffer = default;
        NativeSlice <byte> sendBuffer    = default;
        NativeSlice <byte> sharedBuffer  = default;

        driver.GetPipelineBuffers(pipeline, 4, con, ref receiveBuffer, ref sendBuffer, ref sharedBuffer);

        var sharedCtx = (ReliableUtility.SharedContext *)sharedBuffer.GetUnsafeReadOnlyPtr();

        stats.ReliableSent += sharedCtx->stats.PacketsSent - lastStats.ReliableSent;
        //Console.WriteLine("sharedCtx->stats.PacketsSent=" + sharedCtx->stats.PacketsSent + " lastStats.ReliableSent=" + lastStats.ReliableSent + " stats.ReliableSent=" + stats.ReliableSent);
        stats.ReliableResent    += sharedCtx->stats.PacketsResent - lastStats.ReliableResent;
        stats.ReliableDropped   += sharedCtx->stats.PacketsDropped - lastStats.ReliableDropped;
        stats.ReliableReceived  += sharedCtx->stats.PacketsReceived - lastStats.ReliableReceived;
        stats.ReliableDuplicate += sharedCtx->stats.PacketsDuplicated - lastStats.ReliableDuplicate;
        stats.ReliableRTT        = sharedCtx->RttInfo.LastRtt;
        stats.ReliableSRTT       = sharedCtx->RttInfo.SmoothedRtt;
        int resendQueueSize       = 0;
        int oldestResendPacketAge = 0;
        int maxRtt            = 0;
        int maxProcessingTime = 0;

        GatherExtraStats(sendBuffer, sharedBuffer, timestamp, ref resendQueueSize, ref oldestResendPacketAge, ref maxRtt, ref maxProcessingTime);
        stats.ReliableResendQueue           = resendQueueSize;
        stats.ReliableOldestResendPacketAge = oldestResendPacketAge;
        stats.ReliableMaxRTT            = maxRtt;
        stats.ReliableMaxProcessingTime = maxProcessingTime;

        lastStats.ReliableSent      = sharedCtx->stats.PacketsSent;
        lastStats.ReliableResent    = sharedCtx->stats.PacketsResent;
        lastStats.ReliableDropped   = sharedCtx->stats.PacketsDropped;
        lastStats.ReliableReceived  = sharedCtx->stats.PacketsReceived;
        lastStats.ReliableDuplicate = sharedCtx->stats.PacketsDuplicated;
    }
Ejemplo n.º 21
0
 public void DestroyEntity(NativeSlice <Entity> entities)
 {
     DestroyEntityInternal((Entity *)entities.GetUnsafeReadOnlyPtr(), entities.Length);
 }
Ejemplo n.º 22
0
 public unsafe void MoveLocations(NativeSlice <NavMeshLocation> locations, NativeSlice <Vector3> targets, NativeSlice <int> areaMasks)
 {
     MoveLocations(m_NavMeshQuery, locations.GetUnsafePtr(), targets.GetUnsafeReadOnlyPtr(), areaMasks.GetUnsafeReadOnlyPtr(), locations.Length);
 }
Ejemplo n.º 23
0
 unsafe public void MoveLocationsInSameAreas(NativeSlice <NavMeshLocation> locations, NativeSlice <Vector3> targets, int areaMask = NavMesh.AllAreas)
 {
     MoveLocationsInSameAreas(m_NavMeshQuery, locations.GetUnsafePtr(), targets.GetUnsafeReadOnlyPtr(), locations.Length, areaMask);
 }
Ejemplo n.º 24
0
 static unsafe IntPtr GetUnsafeReadOnlyIntPtr(NativeSlice <byte> encodedData)
 {
     return((IntPtr)encodedData.GetUnsafeReadOnlyPtr());
 }
Ejemplo n.º 25
0
 public unsafe NativeCustomSlice(NativeSlice <byte> slice, int length, int stride)
 {
     this.data   = new IntPtr(slice.GetUnsafeReadOnlyPtr());
     this.length = length;
     this.stride = stride;
 }
 /// <summary>
 /// Adds a native version of <see cref="List{T}.AddRange(IEnumerable{T})"/>.
 /// </summary>
 /// <typeparam name="T">The type.</typeparam>
 /// <param name="list">The <see cref="List{T}"/> to add to.</param>
 /// <param name="nativeSlice">The array to add to the list.</param>
 public static unsafe void AddRange <T>(this List <T> list, NativeSlice <T> nativeSlice)
     where T : struct
 {
     list.AddRange(nativeSlice.GetUnsafeReadOnlyPtr(), nativeSlice.Length);
 }
Ejemplo n.º 27
0
 unsafe public static JobHandle CombineDependencies(NativeSlice <JobHandle> jobs)
 {
     return(CombineDependenciesInternalPtr(jobs.GetUnsafeReadOnlyPtr(), jobs.Length));
 }
Ejemplo n.º 28
0
 public static unsafe void SetPacket(NativeSlice <byte> self, int sequence, NativeSlice <byte> data)
 {
     SetPacket(self, sequence, data.GetUnsafeReadOnlyPtr(), data.Length);
 }
 public unsafe NativeCustomSlice(NativeSlice <T> nativeSlice)
 {
     data   = new IntPtr(nativeSlice.GetUnsafeReadOnlyPtr());
     length = nativeSlice.Length;
     stride = nativeSlice.Stride;
 }
Ejemplo n.º 30
0
        public void MoveLocationsInSameAreas(NativeSlice <NavMeshLocation> locations, NativeSlice <Vector3> targets, int areaMask = -1)
        {
            bool flag = locations.Length != targets.Length;

            if (flag)
            {
                throw new ArgumentException("locations.Length and targets.Length must be equal");
            }
            AtomicSafetyHandle.CheckReadAndThrow(this.m_Safety);
            NavMeshQuery.MoveLocationsInSameAreas(this.m_NavMeshQuery, locations.GetUnsafePtr <NavMeshLocation>(), targets.GetUnsafeReadOnlyPtr <Vector3>(), locations.Length, areaMask);
        }