diff --git a/src/ECS/Query/Chunk.cs b/src/ECS/Query/Chunk.cs index 2f74c303..43337990 100644 --- a/src/ECS/Query/Chunk.cs +++ b/src/ECS/Query/Chunk.cs @@ -68,7 +68,7 @@ public readonly struct Chunk /// /// public Span AsSpan256() where TTo : struct - => MemoryMarshal.Cast(new Span(ArchetypeComponents, 0, (Length + StructPadding.PadCount256) & 0x7fff_ffe0)); + => MemoryMarshal.Cast(new Span(ArchetypeComponents, start, (Length + StructPadding.PadCount256) & 0x7fff_ffe0)); /// /// Return the components as a of type .
@@ -76,7 +76,7 @@ public Span AsSpan256() where TTo : struct /// See Example.. ///
public Span AsSpan128() where TTo : struct - => MemoryMarshal.Cast(new Span(ArchetypeComponents, 0, (Length + StructPadding.PadCount128) & 0x7fff_fff0)); + => MemoryMarshal.Cast(new Span(ArchetypeComponents, start, (Length + StructPadding.PadCount128) & 0x7fff_fff0)); /// /// Return the components as a of type .
@@ -84,7 +84,7 @@ public Span AsSpan128() where TTo : struct /// See Example.. ///
public Span AsSpan512() where TTo : struct - => MemoryMarshal.Cast(new Span(ArchetypeComponents, 0, (Length + StructPadding.PadCount512) & 0x7fff_ffc0)); + => MemoryMarshal.Cast(new Span(ArchetypeComponents, start, (Length + StructPadding.PadCount512) & 0x7fff_ffc0)); /// /// The step value in a for loop when converting a value to a Vector128{T}. diff --git a/src/Tests/ECS/Github/Test_GitHub_47.cs b/src/Tests/ECS/Github/Test_GitHub_47.cs new file mode 100644 index 00000000..a22c3d15 --- /dev/null +++ b/src/Tests/ECS/Github/Test_GitHub_47.cs @@ -0,0 +1,35 @@ +using System; +using Friflo.Engine.ECS; +using NUnit.Framework; +using Tests.Utils; + +// ReSharper disable InconsistentNaming +namespace Tests.ECS.Github +{ + /// https://github.com/friflo/Friflo.Engine.ECS/issues/47 + public static class Test_GitHub_47 + { + [Test] + public static void Parallel_QueryJob_using_AsSpan() + { + const int threads = 2; + Console.WriteLine("Threads count: "+ threads); + var runner = new ParallelJobRunner(threads); + var store = new EntityStore() {JobRunner = runner}; + for (int n = 0; n < 64; n++) { + store.CreateEntity(new MyComponent1 { a = n }); + } + + var query = store.Query(); + var queryJob = query.ForEach((chunk, _) => + { + Span values = chunk.AsSpan256(); + for (int i = 0; i < values.Length; i++) { + Mem.AreEqual(chunk[i].a, values[i]); + } + }); + queryJob.MinParallelChunkLength = 16; + queryJob.RunParallel(); + } + } +}