Beispiel #1
0
            public NetworkEvent.Type PopEvent(out DataStreamReader bs)
            {
                bs = default;
                if (m_IncomingEvents->Length == 0)
                {
                    return(NetworkEvent.Type.Empty);
                }

                var ev = UnsafeUtility.ReadArrayElement <DriverEvent>(m_IncomingEvents->Ptr, 0);

                if (m_IncomingEvents->Length > 0)
                {
                    //m_IncomingEvents->RemoveAtSwapBack<DriverEvent>(0);
                    m_IncomingEvents->Length--;
                    UnsafeUtility.MemMove(m_IncomingEvents->Ptr, (byte *)m_IncomingEvents->Ptr + sizeof(DriverEvent), sizeof(DriverEvent) * m_IncomingEvents->Length);
                }

                if (ev.Type == NetworkEvent.Type.Data)
                {
                    var slice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <byte>((byte *)m_DataStream->Ptr + ev.StreamOffset, sizeof(byte), ev.Length);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref slice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
#endif

                    bs = new DataStreamReader(slice);
                }

                return(ev.Type);
            }
        internal unsafe static void CalculateBounds(this SpriteSkin spriteSkin)
        {
            Debug.Assert(spriteSkin.isValid);
            var sprite = spriteSkin.sprite;


            var   deformVertexData = new NativeArray <byte>(sprite.GetVertexStreamSize() * sprite.GetVertexCount(), Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            void *dataPtr          = NativeArrayUnsafeUtility.GetUnsafePtr(deformVertexData);
            var   deformedPosSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <Vector3>(dataPtr, sprite.GetVertexStreamSize(), sprite.GetVertexCount());

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref deformedPosSlice, NativeArrayUnsafeUtility.GetAtomicSafetyHandle(deformVertexData));
#endif

            var rootBone = spriteSkin.rootBone;
            spriteSkin.Bake(ref deformVertexData);
            var bounds = new Bounds();

            if (deformVertexData.Length > 0)
            {
                bounds.min = rootBone.InverseTransformPoint(deformedPosSlice[0]);
                bounds.max = bounds.min;
            }

            foreach (var v in deformedPosSlice)
            {
                bounds.Encapsulate(rootBone.InverseTransformPoint(v));
            }

            bounds.extents    = Vector3.Scale(bounds.extents, new Vector3(1.25f, 1.25f, 1f));
            spriteSkin.bounds = bounds;
        }
Beispiel #3
0
        public static NativeSlice <T> GetVertexAttribute <T>(this Sprite sprite, VertexAttribute channel) where T : struct
        {
            SpriteDataAccessExtensions.CheckAttributeTypeMatchesAndThrow <T>(channel);
            SpriteChannelInfo channelInfo = SpriteDataAccessExtensions.GetChannelInfo(sprite, channel);
            NativeSlice <T>   result      = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <T>(channelInfo.buffer, channelInfo.offset, channelInfo.count, channelInfo.stride);

            NativeSliceUnsafeUtility.SetAtomicSafetyHandle <T>(ref result, sprite.GetSafetyHandle());
            return(result);
        }
Beispiel #4
0
        private static AtomicSafetyHandle CreateSafetyChecks <T>(ref NativeSlice <T> array) where T : struct
        {
            var handle = AtomicSafetyHandle.Create();

            AtomicSafetyHandle.SetAllowSecondaryVersionWriting(handle, true);
            AtomicSafetyHandle.UseSecondaryVersion(ref handle);
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle <T>(ref array, handle);
            return(handle);
        }
        public unsafe NativeSlice <T> ToNativeSlice()
        {
            var p = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <T>(data.ToPointer(), stride, length);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref p, safetyHandle);
#endif
            return(p);
        }
Beispiel #6
0
        private unsafe NativeSlice <T> GetChannelDataArray <T>(SpriteShapeDataType dataType, VertexAttribute channel) where T : struct
        {
            this.RefreshSafetyHandle(dataType);
            SpriteChannelInfo channelInfo = this.GetChannelInfo(channel);
            byte *            dataPointer = (byte *)channelInfo.buffer + channelInfo.offset;
            NativeSlice <T>   result      = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <T>((void *)dataPointer, channelInfo.stride, channelInfo.count);

            NativeSliceUnsafeUtility.SetAtomicSafetyHandle <T>(ref result, this.GetSafetyHandle(dataType));
            return(result);
        }
        public unsafe static NativeSlice <T> GetVertexAttribute <T>(this Sprite sprite, VertexAttribute channel) where T : struct
        {
            CheckAttributeTypeMatchesAndThrow <T>(channel);
            var info   = GetChannelInfo(sprite, channel);
            var buffer = (byte *)(info.buffer) + info.offset;
            var slice  = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <T>(buffer, info.stride, info.count);

            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref slice, sprite.GetSafetyHandle());
            return(slice);
        }
        unsafe private NativeSlice <T> GetChannelDataArray <T>(SpriteShapeDataType dataType, VertexAttribute channel) where T : struct
        {
            RefreshSafetyHandle(dataType);

            var info   = GetChannelInfo(channel);
            var buffer = (byte *)(info.buffer) + info.offset;
            var slice  = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <T>(buffer, info.stride, info.count);

            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref slice, GetSafetyHandle(dataType));
            return(slice);
        }
Beispiel #9
0
        public unsafe void Execute(int i)
        {
            var   spriteSkin        = spriteSkinData[i];
            byte *deformedPosOffset = (byte *)vertices.GetUnsafePtr();
            NativeSlice <float3> deformableVerticesFloat3 = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <float3>(deformedPosOffset + spriteSkin.deformVerticesStartPos, spriteSkin.spriteVertexStreamSize, spriteSkin.spriteVertexCount);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref deformableVerticesFloat3, NativeSliceUnsafeUtility.GetAtomicSafetyHandle(vertices));
#endif

            bounds[i] = SpriteSkinUtility.CalculateSpriteSkinBounds(deformableVerticesFloat3);
        }
Beispiel #10
0
        /// <summary>
        /// Create a NativeSlice with access to the raw data in the writer, the data size
        /// (start to length) must not exceed the total size of the array or
        /// an exception will be thrown.
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        public NativeSlice <byte> GetNativeSlice(int start, int length)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            ValidateSizeParameters(start, length, length);
#endif

            var slice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <byte>(m_Data->buffer + start, 1,
                                                                                         length);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref slice, m_Safety);
#endif
            return(slice);
        }
Beispiel #11
0
    //-----------------------------------------------------------------------------
    public unsafe static void CopyMatrices(ComponentDataArray <TransformMatrix> transforms, int beginIndex, int length, Matrix4x4[] outMatrices)
    {
        fixed(Matrix4x4 *matricesPtr = outMatrices)
        {
            Assert.AreEqual(sizeof(Matrix4x4), sizeof(TransformMatrix));
            var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <TransformMatrix>(matricesPtr, sizeof(Matrix4x4), length);

                #if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
                #endif
            transforms.CopyTo(matricesSlice, beginIndex);
        }
    }
Beispiel #12
0
    public unsafe static void CopyMatrices(NativeArray <LocalToWorld> transforms, int beginIndex, int length, Matrix4x4[] outMatrices)
    {
        fixed(Matrix4x4 *matricesPtr = outMatrices)
        {
            Assert.AreEqual(sizeof(Matrix4x4), sizeof(LocalToWorld));

            var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <LocalToWorld>(matricesPtr, sizeof(Matrix4x4), length);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
#endif

            matricesSlice.CopyFrom(transforms.Slice(beginIndex, length));
        }
    }
Beispiel #13
0
        // This is the ugly bit, necessary until Graphics.DrawMeshInstanced supports NativeArrays pulling the data in from a job.
        public unsafe static Vector4[] CopyToArray(this NativeSlice <Vector4> src, Vector4[] dst) // where T:struct
        {
            fixed(Vector4 *p = dst)
            {
                UnityEngine.Assertions.Assert.IsTrue(UnsafeUtility.IsBlittable <Vector4>());
                var s = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <Vector4>(p, UnsafeUtility.SizeOf <Vector4>(), src.Length);

                    #if ENABLE_UNITY_COLLECTIONS_CHECKS
                NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref s, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
                    #endif
                s.CopyFrom(src);
            }

            return(dst);
        }
Beispiel #14
0
        // This is the ugly bit, necessary until Graphics.DrawMeshInstanced supports NativeArrays pulling the data in from a job.
        public unsafe static void CopyMatrices(ComponentDataArray <TransformMatrix> transforms, int beginIndex, int length, Matrix4x4[] outMatrices)
        {
            // @TODO: This is using unsafe code because the Unity DrawInstances API takes a Matrix4x4[] instead of NativeArray.
            // We want to use the ComponentDataArray.CopyTo method
            // because internally it uses memcpy to copy the data,
            // if the nativeslice layout matches the layout of the component data. It's very fast...
            fixed(Matrix4x4 *matricesPtr = outMatrices)
            {
                Assert.AreEqual(sizeof(Matrix4x4), sizeof(TransformMatrix));
                var matricesSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <TransformMatrix>(matricesPtr, sizeof(Matrix4x4), length);

                    #if ENABLE_UNITY_COLLECTIONS_CHECKS
                NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref matricesSlice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
                    #endif
                transforms.CopyTo(matricesSlice, beginIndex);
            }
        }
Beispiel #15
0
        public unsafe void Execute(int i)
        {
            int j = vertexLookupData[i].x;
            int k = vertexLookupData[i].y;

            PerSkinJobData perSkinData = perSkinJobData[j];
            var            spriteSkin  = spriteSkinData[j];
            float3         srcVertex   = spriteSkin.vertices[k];
            float4         tangents    = spriteSkin.tangents[k];
            var            influence   = spriteSkin.boneWeights[k];

            int bone0 = influence.boneIndex0 + perSkinData.bindPosesIndex.x;
            int bone1 = influence.boneIndex1 + perSkinData.bindPosesIndex.x;
            int bone2 = influence.boneIndex2 + perSkinData.bindPosesIndex.x;
            int bone3 = influence.boneIndex3 + perSkinData.bindPosesIndex.x;

            byte *deformedPosOffset = (byte *)vertices.GetUnsafePtr();
            byte *deformedPosStart  = deformedPosOffset + spriteSkin.deformVerticesStartPos;
            NativeSlice <float3> deformableVerticesFloat3 = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <float3>(deformedPosStart, spriteSkin.spriteVertexStreamSize, spriteSkin.spriteVertexCount);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref deformableVerticesFloat3, NativeSliceUnsafeUtility.GetAtomicSafetyHandle(vertices));
#endif
            if (spriteSkin.hasTangents)
            {
                byte *deformedTanOffset        = deformedPosStart + spriteSkin.tangentVertexOffset;
                var   deformableTangentsFloat4 = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <float4>(deformedTanOffset, spriteSkin.spriteVertexStreamSize, spriteSkin.spriteVertexCount);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref deformableTangentsFloat4, NativeSliceUnsafeUtility.GetAtomicSafetyHandle(vertices));
#endif
                var tangent = new float4(tangents.xyz, 0.0f);
                tangent =
                    math.mul(finalBoneTransforms[bone0], tangent) * influence.weight0 +
                    math.mul(finalBoneTransforms[bone1], tangent) * influence.weight1 +
                    math.mul(finalBoneTransforms[bone2], tangent) * influence.weight2 +
                    math.mul(finalBoneTransforms[bone3], tangent) * influence.weight3;
                deformableTangentsFloat4[k] = new float4(math.normalize(tangent.xyz), tangents.w);
            }

            deformableVerticesFloat3[k] =
                math.transform(finalBoneTransforms[bone0], srcVertex) * influence.weight0 +
                math.transform(finalBoneTransforms[bone1], srcVertex) * influence.weight1 +
                math.transform(finalBoneTransforms[bone2], srcVertex) * influence.weight2 +
                math.transform(finalBoneTransforms[bone3], srcVertex) * influence.weight3;
        }
Beispiel #16
0
        internal unsafe static void CalculateBounds(this SpriteSkin spriteSkin)
        {
            Debug.Assert(spriteSkin.isValid);
            var sprite = spriteSkin.sprite;


            var   deformVertexData = new NativeArray <byte>(sprite.GetVertexStreamSize() * sprite.GetVertexCount(), Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            void *dataPtr          = NativeArrayUnsafeUtility.GetUnsafePtr(deformVertexData);
            var   deformedPosSlice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <Vector3>(dataPtr, sprite.GetVertexStreamSize(), sprite.GetVertexCount());

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref deformedPosSlice, NativeArrayUnsafeUtility.GetAtomicSafetyHandle(deformVertexData));
#endif

            spriteSkin.Bake(ref deformVertexData);
            UpdateBounds(spriteSkin, deformVertexData);
            deformVertexData.Dispose();
        }
Beispiel #17
0
    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);
    }
Beispiel #18
0
    GetNativeSlice <T>(this ReadOnlySpan <T> span, int offset, int stride)
        where T : unmanaged
    {
        fixed(void *ptr = &span.GetPinnableReference())
        {
            var headPtr      = (T *)ptr + offset;
            var strideInByte = sizeof(T) * stride;
            var elementCount = span.Length / stride - offset / stride;

            var slice =
                NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <T>
                    (headPtr, strideInByte, elementCount);

          #if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeSliceUnsafeUtility.SetAtomicSafetyHandle
                (ref slice, AtomicSafetyHandle.GetTempUnsafePtrSliceHandle());
          #endif

            return(slice);
        }
    }
        public unsafe void Execute(int index, TransformAccess transform)
        {
            float4x4 rotationTranslationMatrix = new float4x4(transform.rotation, transform.position);
            float4x4 scaleMatrix = float4x4.Scale(transform.localScale);
            float4x4 matrix      = math.mul(rotationTranslationMatrix, scaleMatrix);

            //matrix = float4x4.TRS(transform.position, transform.rotation, new float3(1,1,1));
            //var b = boneTransformOutput.ToNativeSlice();
            for (int i = 0; i < boneTransformOutput.Length; ++i)
            {
                if (index < nativeArraySize[i])
                {
                    var array = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice <float4x4>(boneTransformOutput[i].ToPointer(), sizeof(float4x4), nativeArraySize[i]);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    NativeSliceUnsafeUtility.SetAtomicSafetyHandle(ref array, nativeArrayNativeSafetyHandles[i]);
#endif
                    array[index] = matrix;
                    break;
                }
                index -= nativeArraySize[i];
            }
        }