Beispiel #1
0
        public static void CopyComponentData(Chunk *source, Chunk *destination, int sourceIndex, int destinationIndex)
        {
            // TODO: I've no clue what this is about...

            var types = source->archetype->componentTypes;
            var count = source->archetype->componentCount;
            var sizes = source->archetype->componentSizes;

            var sourceOffsets = source->archetype->componentOffsets;
            var destOffsets   = destination->archetype->componentOffsets;

            for (int i = 0; i < count; ++i)
            {
                int indexInChunk = ArchetypeUtility.GetTypeIndex(destination->archetype, types[i]);

                if (indexInChunk == -1)
                {
                    continue;
                }

                var offsetInSourceBuffer = source->buffer + sourceOffsets[i] + (sourceIndex * sizes[i]);
                var offsetInDestBuffer   = destination->buffer + destOffsets[indexInChunk] + (destinationIndex * sizes[i]);

                Buffer.MemoryCopy(offsetInDestBuffer, offsetInSourceBuffer, (uint)sizes[indexInChunk], (uint)sizes[indexInChunk]);
                // Unsafe.CopyBlock((void*)offsetInDestBuffer, (void*)offsetInSourceBuffer, (uint)sizes[indexInChunk]);
            }
        }
Beispiel #2
0
        public static Span <T> GetComponentData <T>(Chunk *chunk, int typeIndex) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                return(Span <T> .Empty);
            }

            return(new Span <T>(GetComponentPtrInBuffer <T>(chunk, indexInArchetype), chunk->count));
        }
Beispiel #3
0
        public static int CopyComponentData <T>(Chunk *chunk, int typeIndex, T *buffer, int count) where T : unmanaged
        {
            count = (count <= chunk->count) ? count : chunk->count;

            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);             // Make sure that the type is in the archetype beforehand...
            var size             = count * sizeof(T);

            Buffer.MemoryCopy(ChunkUtility.GetComponentPtrInBuffer <T>(chunk, indexInArchetype), buffer, size, size);

            return(count);
        }
        public bool HasComponentData <T>(Entity entity) where T : unmanaged, IComponentData
        {
            if (!IsAlive(entity))
            {
                return(false);
            }

            int typeIndex = TypeRegistry <T> .typeIndex;

            return(ArchetypeUtility.GetTypeIndex(GetArchetypeInternal(entity), typeIndex) >= 0);
        }
Beispiel #5
0
        public static T *GetComponentDataPtr <T>(Chunk *chunk, int index, int typeIndex) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                return(null);
            }

            return(&GetComponentPtrInBuffer <T>(chunk, indexInArchetype)[index]);
        }
Beispiel #6
0
        public static bool TryGetComponentData <T>(Chunk *chunk, int index, int typeIndex, out T value) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                value = default;
                return(false);
            }

            value = GetComponentPtrInBuffer <T>(chunk, indexInArchetype)[index];
            return(true);
        }
Beispiel #7
0
        public static bool SetComponentData <T>(Chunk *chunk, int index, int typeIndex, T value) where T : unmanaged
        {
            var indexInArchetype = ArchetypeUtility.GetTypeIndex(chunk->archetype, typeIndex);

            if (indexInArchetype < 0)
            {
                return(false);
            }

            T *data = GetComponentPtrInBuffer <T>(chunk, indexInArchetype);

            data[index] = value;

            return(true);
        }
Beispiel #8
0
        internal EntityArchetype RemoveTypeFromArchetype <T>(Archetype *archetype) where T : unmanaged
        {
            // TODO: Needs a check if the archetypes contains typeof(T).

            int srcCount = archetype->componentCount;

            if (srcCount <= 1)
            {
                return(CreateArchetype());
            }

            int        destCount     = srcCount - 1;
            Span <int> componentData = destCount > 16
                                ? new int[destCount * 2]
                                : stackalloc int[destCount * 2];

            var destTypes = componentData.Slice(0, destCount);
            var destSizes = componentData.Slice(destCount);

            var sourceTypes = new Span <int>(archetype->componentTypes, srcCount);
            var sourceSizes = new Span <int>(archetype->componentSizes, srcCount);

            int indexInArchetype = ArchetypeUtility.GetTypeIndex(archetype, TypeRegistry <T> .typeIndex);

            // Is it somehow possible to remove the branches here?

            if (indexInArchetype > 0)
            {
                sourceTypes.Slice(0, indexInArchetype).CopyTo(destTypes);
                sourceSizes.Slice(0, indexInArchetype).CopyTo(destSizes);
            }

            if (indexInArchetype < destCount)
            {
                sourceTypes.Slice(indexInArchetype + 1).CopyTo(destTypes.Slice(indexInArchetype));
                sourceSizes.Slice(indexInArchetype + 1).CopyTo(destSizes.Slice(indexInArchetype));
            }

            int blockSize = 0;

            for (int i = 0; i < destSizes.Length; ++i)            // TODO: Use CalculateBlockSize instead.
            {
                blockSize += destSizes[i];
            }

            return(CreateArchetypeInternal(destTypes, destSizes, blockSize));
        }
Beispiel #9
0
        internal EntityArchetype CreateArchetypeInternal(Span <int> componentTypes,
                                                         Span <int> componentSizes, int absoluteBlockSize)
        {
            int hashCode = GetComponentTypeHash(componentTypes);

            if (this.archetypeStore->typeLookup.TryGet(hashCode, out EntityArchetype entityArchetype))
            {
                return(entityArchetype);
            }

            int chunkCapacity = ChunkUtility.CalculateChunkBufferCapacity(absoluteBlockSize);

            if (chunkCapacity == 0)
            {
                throw new ArchetypeTooLargeException(absoluteBlockSize);
            }

            this.archetypeStore->EnsureCapacity();

            int index = this.archetypeStore->count++;

            Archetype *archetype = this.archetypeStore->archetypes + index;

            // TODO: Move more stuff in the utility or avoid the utility. YOU DECIDE!
            ArchetypeUtility.ConstructComponentData(archetype, componentTypes, componentSizes, hashCode);
            ArchetypeUtility.CalculateComponentOffsets(archetype, chunkCapacity);

            archetype->chunkArray    = ArchetypeChunkArray.Allocate(archetype);
            archetype->chunkCapacity = chunkCapacity;

            entityArchetype = new EntityArchetype(index);

            // Move to seperate method that takes Archetype* as argument?
            this.archetypeStore->typeLookup.Add(hashCode, entityArchetype);

            return(entityArchetype);
        }
Beispiel #10
0
        public bool HasComponentData <T>() where T : unmanaged
        {
            int typeIndex = TypeRegistry <T> .typeIndex;

            return(ArchetypeUtility.GetTypeIndex(this.chunk->archetype, typeIndex) != -1);
        }