Example #1
0
        public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
        {
            var graphs = chunk.GetNativeArray(graphHandle);
            var fields = chunk.GetBufferAccessor(inheritableFieldDataHandle);

            //var graph = graphs[index];
            for (int index = 0; index < graphs.Length; index++)
            {
                foreach (var field in fields[index])
                {
                    var targetNodeIndex = field.index;
                    var header          = graphs[index].GetNodeHeader(targetNodeIndex);
                    var targetField     = graphs[index].GetConfigBlock(targetNodeIndex, field.field.config);
                    var parent          = header.parent;
                    while (parent >= 0)
                    {
                        if (graphs[index].TryGetConfigBlock(parent, field.field.config, out IntPtr result))
                        {
                            var inheritableField = UnsafeUtility.AsRef <UILength>((result + field.field.offset).ToPointer());
                            if (inheritableField.unit != UILengthUnit.Auto && inheritableField.unit != UILengthUnit.Inherit)
                            {
                                UnsafeUtility.MemCpy((targetField + field.field.offset).ToPointer(), UnsafeUtility.AddressOf(ref inheritableField), field.field.length);
                                break;
                            }
                        }
                        parent = graphs[index].GetNodeHeader(parent).parent;
                    }
                }
            }
        }
        protected override unsafe void OnUpdate()
        {
            _mainThreadBlackboard.EntityCommandMainThread.EntityCommandBuffer =
                _endSimulationEntityCommandBufferSystem.CreateCommandBuffer();
            var behaviorTreeJobDeps = new JobHandle();

            Entities.WithoutBurst().ForEach((Entity entity, DynamicBuffer <BehaviorTreeBufferElement> buffers, ref CurrentBehaviorTreeComponent currentBehaviorTree) =>
            {
                _mainThreadBlackboard.Entity = entity;
                _mainThreadBlackboard.EntityCommandMainThread.Entity = entity;
                for (var i = 0; i < buffers.Length; i++)
                {
                    var bufferPtr  = (IntPtr)buffers.GetUnsafeReadOnlyPtr() + UnsafeUtility.SizeOf <BehaviorTreeBufferElement>() * i;
                    ref var buffer = ref UnsafeUtility.AsRef <BehaviorTreeBufferElement>(bufferPtr.ToPointer());
                    if (buffer.RuntimeThread == BehaviorTreeRuntimeThread.MainThread ||
                        buffer.RuntimeThread == BehaviorTreeRuntimeThread.ForceMainThread)
                    {
                        if (buffer.QueryMask.Matches(entity))
                        {
                            var blob = buffers[i].NodeBlob;
                            currentBehaviorTree.Value = bufferPtr;
                            VirtualMachine.Tick(ref blob, ref _mainThreadBlackboard);
                        }
                    }
                    else
                    {
                        // TODO: is this right way to do this? seems not optimize?
                        behaviorTreeJobDeps = JobHandle.CombineDependencies(behaviorTreeJobDeps, buffers[i].Dependency);
                    }
                }
            }).Run();
Example #3
0
        public unsafe T GetData <T>() where T : struct
        {
            var index = TypeManager.GetTypeIndex <T>();
            var ptr   = Entity.GetComponentDataRawRO(EntityManager, index);

            return(UnsafeUtility.AsRef <T>(ptr));
        }
Example #4
0
        public void Execute(int index)
        {
            int  begin        = Begin[index];
            int  end          = End[index];
            int  count        = end - begin;
            int  alignedCount = count & ~7;
            v256 target       = mm256_set1_epi32(Target);

            int *ptr = (int *)List.GetUnsafeReadOnlyPtr() + begin;
            int  i   = 0;

            while (Interlocked.Read(ref UnsafeUtility.AsRef <long>(Result)) == 0 && i < alignedCount)
            {
                var res = mm256_cmpeq_epi32(*(v256 *)(ptr + i), target);
                if (mm256_movemask_ps(res) != 0)
                {
                    Interlocked.Exchange(ref UnsafeUtility.AsRef <long>(Result), 1);
                    return;
                }
                i += 8;
            }

            while (Interlocked.Read(ref UnsafeUtility.AsRef <long>(Result)) == 0 && i < count)
            {
                if (ptr[i++] == Target)
                {
                    Interlocked.Exchange(ref UnsafeUtility.AsRef <long>(Result), 1);
                    return;
                }
            }
        }
        static unsafe JobHandle Schedule <T>(ref T jobData, IntPtr arrayLengthPtr, int innerloopBatchCount, JobHandle dependsOn)
            where T : struct, IJobParallelForDefer
        {
#if UNITY_SINGLETHREADED_JOBS
            var arrayLength = UnsafeUtility.AsRef <int>(arrayLengthPtr.ToPointer());
            for (var i = 0; i < arrayLength; ++i)
            {
                jobData.Execute(i);
            }

            DoDeallocateOnJobCompletion(jobData);
            return(new JobHandle());
#else
            var jobStruct = new JobStructDefer <T>()
            {
                JobData        = jobData,
                ArrayLengthPtr = arrayLengthPtr,
            };

            var jobDataPtr = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <JobStructDefer <T> >(),
                                                  UnsafeUtility.AlignOf <JobStructDefer <T> >(), Allocator.TempJob);
            UnsafeUtility.CopyStructureToPtr(ref jobStruct, jobDataPtr);

            var scheduleParams = new JobsUtility.JobScheduleParameters(jobDataPtr, JobStructDefer <T> .Initialize(),
                                                                       dependsOn, ScheduleMode.Batched);
            return(JobsUtility.ScheduleParallelFor(ref scheduleParams, JobsUtility.JobQueueThreadCount, innerloopBatchCount));
#endif
        }
Example #6
0
            public static unsafe void Execute(void *structPtr, int jobIndex)
            {
                var jobStruct = UnsafeUtility.AsRef <JobStructDefer <T> >(structPtr);
                var jobData   = jobStruct.JobData;

                var ranges      = new JobRanges();
                var arrayLength = UnsafeUtility.AsRef <int>(jobStruct.ArrayLengthPtr.ToPointer());

                ranges.ArrayLength     = arrayLength;
                ranges.IndicesPerPhase = JobsUtility.GetDefaultIndicesPerPhase(arrayLength);

                while (true)
                {
                    if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out var begin, out var end))
                    {
                        break;
                    }

                    #if ENABLE_UNITY_COLLECTIONS_CHECKS
                    JobsUtility.PatchBufferMinMaxRanges(IntPtr.Zero, UnsafeUtility.AddressOf(ref jobData), begin, end - begin);
                    #endif

                    for (var i = begin; i < end; ++i)
                    {
                        jobData.Execute(i);
                    }

                    break;
                }
            }
Example #7
0
        public unsafe void CylinderCollider_Create_ResultHasExpectedValues()
        {
            var geometry = new CylinderGeometry
            {
                Center      = new float3(-10.10f, 10.12f, 0.01f),
                Orientation = quaternion.AxisAngle(math.normalize(new float3(1.4f, 0.2f, 1.1f)), 38.50f),
                Height      = 2f,
                Radius      = 0.25f,
                BevelRadius = 0.05f,
                SideCount   = 10
            };

            var collider         = CylinderCollider.Create(geometry);
            var cylinderCollider = UnsafeUtility.AsRef <CylinderCollider>(collider.GetUnsafePtr());

            Assert.AreEqual(geometry.Center, cylinderCollider.Center);
            Assert.AreEqual(geometry.Center, cylinderCollider.Geometry.Center);
            Assert.AreEqual(geometry.Orientation, cylinderCollider.Orientation);
            Assert.AreEqual(geometry.Orientation, cylinderCollider.Geometry.Orientation);
            Assert.AreEqual(geometry.Height, cylinderCollider.Height);
            Assert.AreEqual(geometry.Height, cylinderCollider.Geometry.Height);
            Assert.AreEqual(geometry.Radius, cylinderCollider.Radius);
            Assert.AreEqual(geometry.Radius, cylinderCollider.Geometry.Radius);
            Assert.AreEqual(geometry.BevelRadius, cylinderCollider.BevelRadius);
            Assert.AreEqual(geometry.BevelRadius, cylinderCollider.Geometry.BevelRadius);
            Assert.AreEqual(CollisionType.Convex, cylinderCollider.CollisionType);
            Assert.AreEqual(ColliderType.Cylinder, cylinderCollider.Type);
        }
Example #8
0
        unsafe public void TestCreateQuad()
        {
            float3[] vertices =
            {
                new float3(-4.5f, 0.0f, 1.0f),
                new float3(3.4f,  0.7f, 1.0f),
                new float3(3.4f,  2.7f, 1.0f),
                new float3(-3.4f, 1.2f, 1.0f)
            };
            float3 normal = math.normalize(math.cross(vertices[2] - vertices[1], vertices[0] - vertices[1]));

            var collider     = PolygonCollider.CreateQuad(vertices[0], vertices[1], vertices[2], vertices[3]);
            var quadCollider = UnsafeUtility.AsRef <PolygonCollider>(collider.GetUnsafePtr());

            Assert.IsFalse(quadCollider.IsTriangle);
            Assert.IsTrue(quadCollider.IsQuad);

            TestUtils.AreEqual(quadCollider.Vertices[0], vertices[0], 1e-3f);
            TestUtils.AreEqual(quadCollider.Vertices[1], vertices[1], 1e-3f);
            TestUtils.AreEqual(quadCollider.Vertices[2], vertices[2], 1e-3f);
            TestUtils.AreEqual(quadCollider.Vertices[3], vertices[3], 1e-3f);
            Assert.AreEqual(2, quadCollider.Planes.Length);
            TestUtils.AreEqual(normal, quadCollider.Planes[0].Normal, 1e-3f);
            TestUtils.AreEqual(-normal, quadCollider.Planes[1].Normal, 1e-3f);
            Assert.AreEqual(ColliderType.Quad, quadCollider.Type);
            Assert.AreEqual(CollisionType.Convex, quadCollider.CollisionType);
        }
Example #9
0
        public unsafe void should_able_to_create_and_fetch_data_from_node_blob()
        {
            Debug.Log($"sizeof NodeA: {sizeof(NodeA)}");
            Debug.Log($"sizeof NodeB: {sizeof(NodeB)}");

            var size = sizeof(NodeA) + sizeof(NodeB);

            using (var blobBuilder = new BlobBuilder(Allocator.Temp))
            {
                ref var blob = ref blobBuilder.ConstructRoot <NodeBlob>();

                var types = blobBuilder.Allocate(ref blob.Types, 3);
                types[0] = 11;
                types[1] = 22;
                types[2] = 33;

                var endIndices = blobBuilder.Allocate(ref blob.EndIndices, 3);
                endIndices[0] = 3;
                endIndices[1] = 2;
                endIndices[2] = 3;

                var offsets   = blobBuilder.Allocate(ref blob.Offsets, 4);
                var unsafePtr = (byte *)blobBuilder.Allocate(ref blob.DefaultDataBlob, size).GetUnsafePtr();
                var offset    = 0;
                offsets[0] = offset;
                offsets[1] = offset;
                UnsafeUtility.AsRef <NodeA>(unsafePtr + offset).A = 111;
                offset    += sizeof(NodeA);
                offsets[2] = offset;
                ref var local2 = ref UnsafeUtility.AsRef <NodeB>(unsafePtr + offset);
Example #10
0
        unsafe public void TestCreateTriangle()
        {
            float3[] vertices =
            {
                new float3(-1.4f, 1.4f, 5.6f),
                new float3(1.4f,  1.4f, 3.6f),
                new float3(0.2f,  1.2f, 5.6f)
            };
            float3 normal = math.normalize(math.cross(vertices[1] - vertices[0], vertices[2] - vertices[0]));

            var collider         = PolygonCollider.CreateTriangle(vertices[0], vertices[1], vertices[2]);
            var triangleCollider = UnsafeUtility.AsRef <PolygonCollider>(collider.GetUnsafePtr());

            Assert.IsTrue(triangleCollider.IsTriangle);
            Assert.IsFalse(triangleCollider.IsQuad);

            TestUtils.AreEqual(triangleCollider.Vertices[0], vertices[0], 1e-3f);
            TestUtils.AreEqual(triangleCollider.Vertices[1], vertices[1], 1e-3f);
            TestUtils.AreEqual(triangleCollider.Vertices[2], vertices[2], 1e-3f);
            Assert.AreEqual(2, triangleCollider.Planes.Length);
            TestUtils.AreEqual(normal, triangleCollider.Planes[0].Normal, 1e-3f);
            TestUtils.AreEqual(-normal, triangleCollider.Planes[1].Normal, 1e-3f);
            Assert.AreEqual(ColliderType.Triangle, triangleCollider.Type);
            Assert.AreEqual(CollisionType.Convex, triangleCollider.CollisionType);
        }
Example #11
0
        public unsafe T GetData <T>() where T : struct
        {
            var index = TypeManager.GetTypeIndex <T>();
            var ptr   = GetPtrByTypeIndexRO(index);

            return(UnsafeUtility.AsRef <T>(ptr));
        }
Example #12
0
        public static unsafe JobHandle ScheduleParallelFor(ref JobScheduleParameters parameters, int arrayLength, int innerloopBatchCount)
        {
            UnsafeUtility.AssertHeap(parameters.JobDataPtr.ToPointer());
            UnsafeUtility.AssertHeap(parameters.ReflectionData.ToPointer());
            ReflectionDataProxy jobReflectionData = UnsafeUtility.AsRef <ReflectionDataProxy>(parameters.ReflectionData.ToPointer());

            Assert.IsFalse(jobReflectionData.GenExecuteFunctionPtr.ToPointer() == null);
            Assert.IsFalse(jobReflectionData.GenCleanupFunctionPtr.ToPointer() == null);

            void *      jobMetaPtr  = parameters.JobDataPtr.ToPointer();
            JobMetaData jobMetaData = default;

            jobMetaData.JobRanges.ArrayLength     = arrayLength;
            jobMetaData.JobRanges.IndicesPerPhase = GetDefaultIndicesPerPhase(arrayLength);
            UnsafeUtility.CopyStructureToPtr(ref jobMetaData, jobMetaPtr);

#if UNITY_SINGLETHREADED_JOBS
            // In the single threaded case, this is synchronous execution.
            UnsafeUtility.CallFunctionPtr_pi(jobReflectionData.GenExecuteFunctionPtr.ToPointer(), jobMetaPtr, 0);
            UnsafeUtility.CallFunctionPtr_p(jobReflectionData.GenCleanupFunctionPtr.ToPointer(), jobMetaPtr);

            // This checks that the generated code was actually called; the last responsibility of
            // the generated code is to clean up the memory. Unfortunately only works in single threaded mode,
            Assert.IsTrue(UnsafeUtility.GetLastFreePtr() == jobMetaPtr);
            return(new JobHandle());
#else
            return(ScheduleJobParallelFor(jobReflectionData.GenExecuteFunctionPtr, jobReflectionData.GenCleanupFunctionPtr,
                                          parameters.JobDataPtr, arrayLength, innerloopBatchCount, parameters.Dependency));
#endif
        }
Example #13
0
            public static unsafe void SetManagedObjectType <T>(object source)
            {
                void *ptr = UnsafeUtility.PinGCObjectAndGetAddress(source, out ulong gcHandle);

                UnsafeUtility.AsRef <ObjectHeader>(ptr) = TypeHeaders <T> .Header;
                UnsafeUtility.ReleaseGCObject(gcHandle);
            }
Example #14
0
            public static unsafe void SetListBackingArray(object InternalList, Array array)
            {
                void *ptr = UnsafeUtility.PinGCObjectAndGetAddress(InternalList, out ulong gcHandle);

                UnsafeUtility.AsRef <ListHeader>(ptr).Array = array;
                UnsafeUtility.ReleaseGCObject(gcHandle);
            }
Example #15
0
        public void Execute(ref ExecuteContext<Parameters, SampleProviders> context)
        {
            bool bufferStarved = false;

            m_currentSubframe++;
            if (m_currentSubframe >= m_ildBuffer.subframesPerFrame)
            {
                m_currentSubframe = 0;
                m_currentFrame++;
                m_ildBuffer          = m_queuedIldBuffer;
                m_lastPlayedBufferID = m_ildBuffer.bufferId;  //we need to report the buffer we just consumed, the audio system knows to keep that one around yet
            }

            for (int outputChannelIndex = 0; outputChannelIndex < context.Outputs.Count; outputChannelIndex++)
            {
                var channelOutput = context.Outputs.GetSampleBuffer(outputChannelIndex);
                if (channelOutput.Channels <= 0)
                    continue;
                var outputBuffer = channelOutput.GetBuffer(0);
                if (m_ildBuffer.channelCount <= outputChannelIndex)
                {
                    for (int i = 0; i < outputBuffer.Length; i++)
                    {
                        outputBuffer[i] = 0f;
                    }
                }
                else if (m_currentFrame - m_ildBuffer.frame >= m_ildBuffer.framesInBuffer)
                {
                    for (int i = 0; i < outputBuffer.Length; i++)
                    {
                        outputBuffer[i] = 0f;
                    }
                    bufferStarved = true;
                }
                else
                {
                    var ildBufferChannel = m_ildBuffer.bufferChannels[outputChannelIndex];

                    int bufferOffset  = m_ildBuffer.subframesPerFrame * (m_currentFrame - m_ildBuffer.frame) + m_currentSubframe;
                    bufferOffset     *= outputBuffer.Length;
                    for (int i = 0; i < outputBuffer.Length; i++)
                    {
                        outputBuffer[i] = ildBufferChannel.buffer[bufferOffset + i];
                    }
                }
            }

            if (bufferStarved && m_ildBuffer.warnIfStarved)
            {
                UnityEngine.Debug.LogWarning(
                    $"Dsp buffer starved. Kernel frame: {m_currentFrame}, IldBuffer frame: {m_ildBuffer.frame}, ildBuffer Id: {m_ildBuffer.bufferId}, frames in buffer: {m_ildBuffer.framesInBuffer}, subframe: {m_currentSubframe}/{m_ildBuffer.subframesPerFrame}");
            }

            if (m_currentSubframe == 0)
            {
                long     packed   = m_currentFrame + (((long)m_lastPlayedBufferID) << 32);
                ref long location = ref UnsafeUtility.AsRef<long>(m_packedFrameCounterBufferId);
                Interlocked.Exchange(ref location, packed);
            }
Example #16
0
    private static unsafe bool IsTrigger(NativeArray <RigidBody> bodies, int rigidBodyIndex, ColliderKey colliderKey)
    {
        RigidBody hitBody = bodies[rigidBodyIndex];

        hitBody.Collider.Value.GetLeaf(colliderKey, out ChildCollider leafCollider);
        Unity.Physics.Material material = UnsafeUtility.AsRef <ConvexColliderHeader>(leafCollider.Collider).Material;
        return(material.CollisionResponse == CollisionResponsePolicy.RaiseTriggerEvents);
    }
Example #17
0
            public static unsafe T[] GetInternalArray <T>(List <T> list)
            {
                void *ptr   = UnsafeUtility.PinGCObjectAndGetAddress(list, out ulong gcHandle);
                var   array = UnsafeUtility.AsRef <ListHeader>(ptr).Array;

                UnsafeUtility.ReleaseGCObject(gcHandle);
                return(array as T[]);
            }
Example #18
0
 void SmoothComponent(void *currentData, void *previousData, ComponentType componentType)
 {
     // FIXME: apply smoothing between the current value and the previous predicted value
     // FIXME: placeholder implementation hardcoded to only do something for translation
     if (componentType == translationType)
     {
         ref var trans  = ref UnsafeUtility.AsRef <Translation>(currentData);
         ref var backup = ref UnsafeUtility.AsRef <Translation>(previousData);
Example #19
0
            public static unsafe void Cleanup(void *structPtr)
            {
                var jobStruct = UnsafeUtility.AsRef <JobStructDefer <T> >(structPtr);
                var jobData   = jobStruct.JobData;

                DoDeallocateOnJobCompletion(jobData);
                UnsafeUtility.Free(structPtr, Allocator.TempJob);
            }
Example #20
0
 public unsafe void View(IntPtr dataPtr, Type type, RuntimeViewerFactoryRegister register)
 {
     try
     {
         ref var data      = ref UnsafeUtility.AsRef <BlobVariant>(dataPtr.ToPointer());
         var     valuePtr  = data.ReadOnlyPtrWithReadWriteFallback(_index, ref _blob, ref _blackboard);
         var     valueType = type.GetGenericArguments()[0];
         Value = Marshal.PtrToStructure(valuePtr, valueType).ToString();
     }
 public static unsafe void Allocate <T>(
     this VariableProperty <T> variable
     , ref BlobBuilder builder
     , void *blobVariablePtr
     , [NotNull] INodeDataBuilder self
     , [NotNull] ITreeNode <INodeDataBuilder>[] tree
     ) where T : unmanaged
 {
     variable.Allocate(ref builder, ref UnsafeUtility.AsRef <BlobVariable <T> >(blobVariablePtr), self, tree);
 }
Example #22
0
            public static unsafe void OverwriteArrayLength(Array array, int length)
            {
                if (array.Length == length)
                {
                    return;
                }

                void *ptr = UnsafeUtility.PinGCObjectAndGetAddress(array, out ulong gcHandle);

                UnsafeUtility.AsRef <ArrayHeader>(ptr).ManagedArrayLength = length;
                UnsafeUtility.ReleaseGCObject(gcHandle);
            }
Example #23
0
        public T ReadBlittable <T>() where T : struct
        {
            T val;

            unsafe
            {
                int size = UnsafeUtility.SizeOf <T>();
                NativeSlice <byte> bytes = ReadSlice(size);

                val = UnsafeUtility.AsRef <T>((void *)((byte *)bytes.GetUnsafePtr()));
            }

            return(val);
        }
Example #24
0
        static unsafe JobHandle Schedule <T>(ref T jobData, IntPtr arrayLengthPtr, int innerloopBatchCount, JobHandle dependsOn)
            where T : struct, IJobParallelForDefer
        {
#if UNITY_SINGLETHREADED_JOBS
            var arrayLength = UnsafeUtility.AsRef <int>(arrayLengthPtr.ToPointer());
            for (var i = 0; i < arrayLength; ++i)
            {
                jobData.Execute(i);
            }

            DoDeallocateOnJobCompletion(jobData);
            return(new JobHandle());
#else
            // Protect against garbage collection
            if (!JobStructDefer <T> .ExecuteHandle.IsAllocated)
            {
                JobStructDefer <T> .ExecuteDelegate    = JobStructDefer <T> .Execute;
                JobStructDefer <T> .ExecuteHandle      = GCHandle.Alloc(JobStructDefer <T> .ExecuteDelegate);
                JobStructDefer <T> .ExecuteFunctionPtr = Marshal.GetFunctionPointerForDelegate(JobStructDefer <T> .ExecuteDelegate);
            }

            // Protect against garbage collection
            if (!JobStructDefer <T> .CleanupHandle.IsAllocated)
            {
                JobStructDefer <T> .CleanupDelegate    = JobStructDefer <T> .Cleanup;
                JobStructDefer <T> .CleanupHandle      = GCHandle.Alloc(JobStructDefer <T> .CleanupDelegate);
                JobStructDefer <T> .CleanupFunctionPtr = Marshal.GetFunctionPointerForDelegate(JobStructDefer <T> .CleanupDelegate);
            }

            var jobFunctionPtr    = JobStructDefer <T> .ExecuteFunctionPtr;
            var completionFuncPtr = JobStructDefer <T> .CleanupFunctionPtr;

            var jobStruct = new JobStructDefer <T>()
            {
                JobData        = jobData,
                ArrayLengthPtr = arrayLengthPtr,
            };

            var jobDataPtr = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <JobStructDefer <T> >(),
                                                  UnsafeUtility.AlignOf <JobStructDefer <T> >(), Allocator.TempJob);
            UnsafeUtility.CopyStructureToPtr(ref jobStruct, jobDataPtr);

            return(JobsUtility.ScheduleJobForEach(jobFunctionPtr, completionFuncPtr, new IntPtr(jobDataPtr),
                                                  JobsUtility.JobQueueThreadCount, innerloopBatchCount, dependsOn));
#endif
        }
Example #25
0
            public unsafe void SetType <T>()
            {
                var type = typeof(T);

                if (currentType == typeof(T))
                {
                    return;
                }

                currentType = type;
                void *arrayPtr = UnsafeUtility.PinGCObjectAndGetAddress(InternalBackingArray, out ulong arrayGcHandle);
                void *listPtr  = UnsafeUtility.PinGCObjectAndGetAddress(InternalList, out ulong listGcHandle);

                UnsafeUtility.AsRef <ObjectHeader>(arrayPtr) = TypeHeaders <T[]> .Header;
                UnsafeUtility.AsRef <ObjectHeader>(listPtr)  = TypeHeaders <List <T> > .Header;
                UnsafeUtility.ReleaseGCObject(arrayGcHandle);
                UnsafeUtility.ReleaseGCObject(listGcHandle);
            }
Example #26
0
        unsafe public void TestSphereColliderCreate()
        {
            var sphere = new SphereGeometry
            {
                Center = new float3(-8.45f, 9.65f, -0.10f),
                Radius = 0.98f
            };

            var collider       = SphereCollider.Create(sphere);
            var sphereCollider = UnsafeUtility.AsRef <SphereCollider>(collider.GetUnsafePtr());

            TestUtils.AreEqual(sphere.Center, sphereCollider.Center, 1e-3f);
            TestUtils.AreEqual(sphere.Center, sphereCollider.Geometry.Center, 1e-3f);
            TestUtils.AreEqual(sphere.Radius, sphereCollider.Radius, 1e-3f);
            TestUtils.AreEqual(sphere.Radius, sphereCollider.Geometry.Radius, 1e-3f);
            Assert.AreEqual(ColliderType.Sphere, sphereCollider.Type);
            Assert.AreEqual(CollisionType.Convex, sphereCollider.CollisionType);
        }
Example #27
0
#pragma warning restore 649

        void ResolveDeferredList()
        {
            var bufferAddress = (long)m_Buffer;

            // We use the first bit of the pointer to infer that the array is in list mode
            // Thus the job scheduling code will need to patch it.
            if ((bufferAddress & 1) != 0)
            {
                var listData = UnsafeUtility.AsRef <NativeListData>((void *)(bufferAddress & ~1));

                m_Buffer = listData.buffer;
                m_Length = listData.length;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                m_MinIndex = 0;
                m_MaxIndex = listData.length - 1;
#endif
            }
        }
 unsafe public void TestCapsuleColliderCreate()
 {
     var geometry = new CapsuleGeometry
     {
         Vertex0 = new float3(1.45f, 0.34f, -8.65f),
         Vertex1 = new float3(100.45f, -80.34f, -8.65f),
         Radius = 1.45f
     };
     var collider = CapsuleCollider.Create(geometry);
     var capsuleCollider = UnsafeUtility.AsRef<CapsuleCollider>(collider.GetUnsafePtr());
     Assert.AreEqual(ColliderType.Capsule, capsuleCollider.Type);
     Assert.AreEqual(CollisionType.Convex, capsuleCollider.CollisionType);
     TestUtils.AreEqual(geometry.Vertex0, capsuleCollider.Vertex0);
     TestUtils.AreEqual(geometry.Vertex0, capsuleCollider.Geometry.Vertex0);
     TestUtils.AreEqual(geometry.Vertex1, capsuleCollider.Vertex1);
     TestUtils.AreEqual(geometry.Vertex1, capsuleCollider.Geometry.Vertex1);
     TestUtils.AreEqual(geometry.Radius, capsuleCollider.Radius);
     TestUtils.AreEqual(geometry.Radius, capsuleCollider.Geometry.Radius);
 }
Example #29
0
            public unsafe void EnsureListSyncedWithArray()
            {
                var capacity = InternalList.Capacity;

                // detect if the list's capacity has changed
                // this indicates that the internal array has been replaced with a new one
                // (in that case, we want to start using its internal array as the new staticArray)
                if (capacity == InternalBackingArray.Length)
                {
                    return;
                }

                void *ptr   = UnsafeUtility.PinGCObjectAndGetAddress(InternalList, out ulong gcHandle);
                var   array = UnsafeUtility.AsRef <ListHeader>(ptr).Array;

                UnsafeUtility.ReleaseGCObject(gcHandle);
                InternalBackingArray = array;
                actualCapacity       = capacity;
            }
Example #30
0
            public static unsafe void Execute(void *structPtr, int jobIndex)
            {
                var jobStruct = UnsafeUtility.AsRef <ParallelForBatchJobStruct <T> >(structPtr);
                var ranges    = jobStruct.Ranges;
                var jobData   = jobStruct.JobData;

                while (true)
                {
                    if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out int begin, out int end))
                    {
                        break;
                    }

                    #if ENABLE_UNITY_COLLECTIONS_CHECKS
                    JobsUtility.PatchBufferMinMaxRanges(IntPtr.Zero, UnsafeUtility.AddressOf(ref jobData), begin, end - begin);
                    #endif

                    jobData.Execute(begin, end - begin);
                    break;
                }
            }