internal static int GetTypeOffset(this ArchetypeChunk self, ChunkTypeIndex index)
 {
     unsafe
     {
         return(self.m_Chunk->Archetype->Offsets[index.Value]);
     }
 }
        internal static NativeView GetComponentView(this ArchetypeChunk self, ChunkTypeIndex index)
        {
            var count         = self.Count;
            var typeSize      = self.GetTypeSize(index);
            var lengthInBytes = typeSize * count;

            var offset = self.GetTypeOffset(index);

            unsafe
            {
                var ptr = self.m_Chunk->Buffer + offset;
                return(new NativeView(ptr, lengthInBytes));
            }
        }
        static void CheckValidTypeIndexOrThrow(ChunkTypeIndex index, int typeCount)
        {
            // todo: improve error message (which would currently not be burstable), e.g.:
            // throw new ArgumentException($"Type {TypeManager.GetType(type.TypeIndex)} is not part of this Archetype");

            if (index.Value < 0)
            {
                throw new ArgumentException("Type is not part of this Archetype");
            }

            if (index.Value >= typeCount)
            {
                throw new ArgumentException("Index exceeds type count");
            }
        }
Esempio n. 4
0
        public static NativeArray <uint> GetChunkHashes <T>(ArchetypeChunk chunk, T hasher, uint seed) where T : struct, IHasher
        {
            var types = chunk.GetNonzeroComponentTypes(Allocator.Temp);

            var typeCount = types.Length;
            var hashes    = new NativeArray <uint>(typeCount + 1, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            for (int i = 0; i < typeCount; i++)
            {
                var chunkIndex = new ChunkTypeIndex
                {
                    Value = i
                };

                var type = types[i];

                if (type.IsBuffer)
                {
                    var elementSize = TypeManager.GetTypeInfo(type.TypeIndex).ElementSize;
                    var bufferViews = chunk.GetBufferViews(chunkIndex, elementSize, Allocator.Temp);

                    var bufferCount  = bufferViews.Length;
                    var bufferHashes = new NativeArray <uint>(bufferCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

                    for (int j = 0; j < bufferCount; j++)
                    {
                        bufferHashes[j] = hasher.Execute(bufferViews[j], type, seed);
                    }

                    hashes[i] = GetDenseHash(bufferHashes, seed);

                    bufferHashes.Dispose();
                    bufferViews.Dispose();
                }
                else
                {
                    var view = chunk.GetComponentView(chunkIndex);
                    hashes[i] = hasher.Execute(view, type, seed);
                }
            }

            hashes[typeCount] = GetChunkHeaderHash(chunk, seed);
            types.Dispose();

            return(hashes);
        }
        internal static NativeArray <NativeView> GetBufferViews(this ArchetypeChunk self, ChunkTypeIndex index, int elementSize, Allocator allocator)
        {
            var typeSize = self.GetTypeSize(index);
            var offset   = self.GetTypeOffset(index);

            var count = self.Count;
            var views = new NativeArray <NativeView>(count, allocator, NativeArrayOptions.UninitializedMemory);

            unsafe
            {
                var ptr = self.m_Chunk->Buffer + offset;

                for (int i = 0; i < count; i++)
                {
                    var buffer = (BufferHeader *)(ptr + i * typeSize);
                    var data   = BufferHeader.GetElementPointer(buffer);

                    views[i] = new NativeView(data, buffer->Length * elementSize);
                }
            }
            return(views);
        }