diff --git a/Ghost.Core/Result.cs b/Ghost.Core/Result.cs index b6ac891..247a8ca 100644 --- a/Ghost.Core/Result.cs +++ b/Ghost.Core/Result.cs @@ -1,16 +1,16 @@ +using Misaki.HighPerformance.LowLevel; using System.Runtime.CompilerServices; namespace Ghost.Core; public readonly struct Result { - private readonly bool _isSuccess; private readonly string? _message; - - public readonly bool IsSuccess => _isSuccess; - public readonly bool IsFailure => !_isSuccess; + private readonly bool _isSuccess; public readonly string? Message => _message; + public readonly bool IsSuccess => _isSuccess; + public readonly bool IsFailure => !_isSuccess; public Result(bool success, string? message = null) { @@ -38,16 +38,10 @@ public readonly struct Result return Result.Failure(message); } - public static Result Create(T value, S status) - where S : Enum + public void Deconstruct(out bool success, out string? message) { - return new Result(value, status); - } - - public static RefResult CreateRef(ref T value, S status) - where S : Enum - { - return new RefResult(ref value, status); + success = IsSuccess; + message = Message; } public override string ToString() => IsSuccess ? "OK" : $"Error: {Message}"; @@ -57,15 +51,14 @@ public readonly struct Result public readonly struct Result { - private readonly bool _isSuccess; private readonly T _value; private readonly string? _message; - - public readonly bool IsSuccess => _isSuccess; - public readonly bool IsFailure => !_isSuccess; + private readonly bool _isSuccess; public readonly T Value => _value; public readonly string? Message => _message; + public readonly bool IsSuccess => _isSuccess; + public readonly bool IsFailure => !_isSuccess; public Result(bool success, T value, string? message = null) { @@ -84,6 +77,13 @@ public readonly struct Result return new Result(false, default!, message); } + public void Deconstruct(out bool success, out T value, out string? message) + { + success = IsSuccess; + value = Value; + message = Message; + } + public override string ToString() => IsSuccess ? $"OK: {Value}" : $"Error: {Message}"; public static implicit operator Result(T? data) => data is not null ? Success(data) : Failure(null); @@ -91,9 +91,9 @@ public readonly struct Result public static implicit operator bool(Result result) => result.IsSuccess; } -public enum ResultStatus : byte +public enum ErrorStatus : byte { - Success, + None, NotFound, InvalidArgument, InvalidState, @@ -106,57 +106,97 @@ public enum ResultStatus : byte UnknownError } -public readonly struct Result - where S : Enum +public readonly struct Result + where E : struct, Enum { private readonly T _value; - private readonly S _status; + private readonly E _error; + private readonly bool _isSuccess; public T Value => _value; - public S Status => _status; + public E Error => _error; + public bool IsSuccess => _isSuccess; + public bool IsFailure => !_isSuccess; - public Result(T value, S status) + public Result(T value, E status, bool isSuccess) { _value = value; - _status = status; + _error = status; + _isSuccess = isSuccess; } - public static Result Create(T value, S status) + public static Result Success(T value) { - return new Result(value, status); + return new Result(value, default, true); } - public override string ToString() => $"Value: {_value}, Status: {_status}"; + public static Result Failure(E status) + { + return new Result(default!, status, false); + } + + public void Deconstruct(out bool success, out T value, out E status) + { + success = IsSuccess; + value = Value; + status = Error; + } + + public override string ToString() => $"Value: {_value}, Status: {_error}"; + + public static implicit operator Result(T data) => new(data, default, true); + public static implicit operator Result(E status) => new(default!, status, false); + public static implicit operator bool(Result result) => result.IsSuccess; } -public readonly ref struct RefResult - where S : Enum +public readonly ref struct RefResult + where E : struct, Enum { private readonly ref T _value; - private readonly S _status; + private readonly E _error; + private readonly bool _isSuccess; public ref T Value => ref _value; - public S Status => _status; + public E Error => _error; + public bool IsSuccess => _isSuccess; + public bool IsFailure => !_isSuccess; - public RefResult(ref T value, S status) + public RefResult(ref T value, E error, bool isSuccess) { _value = ref value; - _status = status; + _error = error; + _isSuccess = isSuccess; } - public static RefResult Create(ref T value, S status) + public static RefResult Success(ref T value) { - return new RefResult(ref value, status); + return new RefResult(ref value, default, true); } - public override string ToString() => $"Value: {_value}, Status: {_status}"; + public static RefResult Failure(E error) + { + return new RefResult(ref Unsafe.NullRef(), error, false); + } + + public void Deconstruct(out bool success, out Ref value, out E status) + { + success = IsSuccess; + value = new Ref(ref Value); + status = Error; + } + + public override string ToString() => $"Value: {_value}, Status: {_error}"; + + public static implicit operator RefResult(Ref data) => new(ref data.Get(), default, true); + public static implicit operator RefResult(E error) => new(ref Unsafe.NullRef(), error, false); + public static implicit operator bool(RefResult result) => result.IsSuccess; } public static class ResultExtensions { - public static void ThrowIfFailed(this ResultStatus result, [CallerArgumentExpression(nameof(result))] string? op = null) + public static void ThrowIfFailed(this ErrorStatus result, [CallerArgumentExpression(nameof(result))] string? op = null) { - if (result != ResultStatus.Success) + if (result != ErrorStatus.None) { throw new InvalidOperationException($"{op} failed: {result}"); } @@ -180,12 +220,12 @@ public static class ResultExtensions return result.Value; } - public static T GetValueOrThrow(this Result result, S expect, [CallerArgumentExpression(nameof(result))] string? op = null) - where S : Enum + public static T GetValueOrThrow(this Result result, [CallerArgumentExpression(nameof(result))] string? op = null) + where S : struct, Enum { - if (!EqualityComparer.Default.Equals(result.Status, expect)) + if (!result.IsSuccess) { - throw new InvalidOperationException($"{op} failed: expected status {expect}, but got {result.Status}"); + throw new InvalidOperationException($"{op} failed: status {result.Error}"); } return result.Value; @@ -196,10 +236,10 @@ public static class ResultExtensions return result.IsSuccess ? result.Value : defaultValue; } - public static T? GetValueOrDefault(this Result result, S expect, T? defaultValue = default) - where S : Enum + public static T? GetValueOrDefault(this Result result, T? defaultValue = default) + where S : struct, Enum { - return (result.Status?.Equals(expect) ?? false) ? defaultValue : result.Value; + return result.IsSuccess ? result.Value : defaultValue; } public static bool TryGetValue(this Result result, out T value) @@ -214,10 +254,10 @@ public static class ResultExtensions return false; } - public static bool TryGetValue(this Result result, S expect, out T value) - where S : Enum + public static bool TryGetValue(this Result result, out T value) + where S : struct, Enum { - if (EqualityComparer.Default.Equals(result.Status, expect)) + if (result.IsSuccess) { value = result.Value; return true; diff --git a/Ghost.Entities.Test/EntityTest.cs b/Ghost.Entities.Test/EntityTest.cs index 07e91dc..5e9ae0d 100644 --- a/Ghost.Entities.Test/EntityTest.cs +++ b/Ghost.Entities.Test/EntityTest.cs @@ -1,16 +1,18 @@ using Ghost.Test.Core; using Misaki.HighPerformance.Jobs; -using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.Mathematics; -using System.Runtime.CompilerServices; namespace Ghost.Entities.Test; -internal struct TestEntityQueryJob : IJobEntity +internal struct TestEntityQueryJob : IJobChunk { - public readonly void Execute(Entity entity, ref Transform transform) + public void Execute(ChunkView view, int threadIndex) { - transform.position += new float3(10, 10, 10); + var transforms = view.GetComponentData(); + for (var i = 0; i < view.Count; i++) + { + transforms[i].position += new float3(10, 10, 10); + } } } @@ -23,8 +25,6 @@ public partial class EntityTest : ITest { _jobScheduler = new JobScheduler(4); _world = World.Create(_jobScheduler); - - Console.WriteLine(Unsafe.SizeOf()); } public void Run() @@ -39,7 +39,7 @@ public partial class EntityTest : ITest ref var query = ref _world.GetEntityQueryReference(queryID); var testJob = new TestEntityQueryJob(); - var handle = query.ScheduleEntityParallel(testJob, Allocator.Temp, 64, JobHandle.Invalid); + var handle = query.ScheduleChunkParallel(testJob, 64, JobHandle.Invalid); _jobScheduler.WaitComplete(handle); query.ForEach((e, ref t) => diff --git a/Ghost.Entities/Archetype.cs b/Ghost.Entities/Archetype.cs index 1c47db0..7b48d50 100644 --- a/Ghost.Entities/Archetype.cs +++ b/Ghost.Entities/Archetype.cs @@ -263,12 +263,12 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly ResultStatus SetComponentData(int chunkIndex, int rowIndex, Identifier componentID, void* pComponent) + public readonly ErrorStatus SetComponentData(int chunkIndex, int rowIndex, Identifier componentID, void* pComponent) { var r = GetLayout(componentID); - if (r.Status != ResultStatus.Success) + if (r.Error != ErrorStatus.None) { - return r.Status; + return r.Error; } var offset = r.Value.offset; @@ -280,14 +280,14 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable MemoryUtility.MemCpy(dst, pComponent, (nuint)size); - return ResultStatus.Success; + return ErrorStatus.None; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly void* GetComponentData(int chunkIndex, int rowIndex, Identifier componentID) { var r = GetLayout(componentID); - if (r.Status != ResultStatus.Success) + if (r.Error != ErrorStatus.None) { return null; } @@ -307,27 +307,27 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly Result GetLayout(int componentID) + public readonly Result GetLayout(int componentID) { if (componentID >= _componentIDToLayoutIndex.Count) { - return Result.Create(default(ComponentMemoryLayout), ResultStatus.InvalidArgument); + return ErrorStatus.InvalidArgument; } var layoutIndex = _componentIDToLayoutIndex[componentID]; if (layoutIndex == -1) { - return Result.Create(default(ComponentMemoryLayout), ResultStatus.NotFound); + return ErrorStatus.NotFound; } - return Result.Create(_layouts[layoutIndex], ResultStatus.Success); + return _layouts[layoutIndex]; } - public ResultStatus RemoveEntity(int chunkIndex, int rowIndex) + public ErrorStatus RemoveEntity(int chunkIndex, int rowIndex) { if (chunkIndex < 0 || chunkIndex >= _chunks.Count) { - return ResultStatus.InvalidArgument; + return ErrorStatus.InvalidArgument; } ref var chunk = ref _chunks[chunkIndex]; @@ -341,13 +341,13 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable var pRowEntity = chunkBase + _entityIdsOffset + (sizeof(Entity) * rowIndex); var wroldResult = World.GetWorld(_worldID); - if (wroldResult.Status != ResultStatus.Success) + if (wroldResult.Error != ErrorStatus.None) { - return wroldResult.Status; + return wroldResult.Error; } var result = wroldResult.Value.EntityManager.UpdateEntityLocation(*(Entity*)pLastEntity, _id, chunkIndex, rowIndex); - if (result != ResultStatus.Success) + if (result != ErrorStatus.None) { return result; } @@ -367,7 +367,7 @@ internal unsafe struct Archetype : IIdentifierType, IDisposable } chunk.Count--; - return ResultStatus.Success; + return ErrorStatus.None; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Ghost.Entities/EntityCommandBuffer.cs b/Ghost.Entities/EntityCommandBuffer.cs index cc2bee0..2bfc6e2 100644 --- a/Ghost.Entities/EntityCommandBuffer.cs +++ b/Ghost.Entities/EntityCommandBuffer.cs @@ -102,14 +102,16 @@ public unsafe class EntityCommandBuffer : IDisposable return bufferPtr; } - public void CreateEntity() + public void CreateEntity(int count = 1) { WriteHeader(ECBOpCode.CreateEntity); + Write(count); } - public void CreateEntity(params ReadOnlySpan> componentTypeIDs) + public void CreateEntity(int count, params ReadOnlySpan> componentTypeIDs) { WriteHeader(ECBOpCode.CreateEntityWithComponents); + Write(count); Write(componentTypeIDs.Length); WriteSpan(componentTypeIDs); } @@ -150,23 +152,23 @@ public unsafe class EntityCommandBuffer : IDisposable { var cursor = 0; var length = _buffer.Count; - var ptr = (byte*)_buffer.GetUnsafePtr(); while (cursor < length) { - var op = *(ECBOpCode*)ptr[cursor]; - cursor += sizeof(ECBOpCode); + var op = Read(ref cursor); switch (op) { case ECBOpCode.CreateEntity: - _entityManager.CreateEntity(); + var count = Read(ref cursor); + _entityManager.CreateEntities(count); break; case ECBOpCode.CreateEntityWithComponents: + var entityCount = Read(ref cursor); var compCount = Read(ref cursor); var compTypeIDs = ReadSpan>(ref cursor, compCount); - _entityManager.CreateEntity(compTypeIDs); + _entityManager.CreateEntities(entityCount, compTypeIDs); break; case ECBOpCode.DestroyEntity: diff --git a/Ghost.Entities/EntityManager.cs b/Ghost.Entities/EntityManager.cs index f9d3971..4353de3 100644 --- a/Ghost.Entities/EntityManager.cs +++ b/Ghost.Entities/EntityManager.cs @@ -6,6 +6,14 @@ using System.Diagnostics; namespace Ghost.Entities; +/// +/// A manager for creating, destroying, and managing entities and their components. +/// +/// +/// All methods in this class are not thread-safe and all of them will cause structural changes if not mentioned otherwise. +/// Use to defer structural changes to a safe point. +/// Use to get a thread-local command buffer for multithreaded scenarios. +/// public unsafe partial class EntityManager : IDisposable { private struct EntityLocation @@ -30,19 +38,19 @@ public unsafe partial class EntityManager : IDisposable Dispose(); } - internal ResultStatus UpdateEntityLocation(Entity entity, Identifier newArchetypeID, int newChunkIndex, int newRowIndex) + internal ErrorStatus UpdateEntityLocation(Entity entity, Identifier newArchetypeID, int newChunkIndex, int newRowIndex) { ref var location = ref _entityLocations.GetElementReferenceAt(entity.ID, entity.Generation, out var exist); if (!exist) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } location.archetypeID = newArchetypeID; location.chunkIndex = newChunkIndex; location.rowIndex = newRowIndex; - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -51,21 +59,10 @@ public unsafe partial class EntityManager : IDisposable /// The created entity. public Entity CreateEntity() { - // Put into empty archetype - ref var emptyArchetype = ref _world.GetArchetypeReference(World.EmptyArchetypeID); - emptyArchetype.AllocateEntity(out var chunkIndex, out var rowIndex); + var entities = (Span)stackalloc Entity[1]; + CreateEntities(1, entities); - var id = _entityLocations.Add(new EntityLocation - { - archetypeID = World.EmptyArchetypeID, - chunkIndex = chunkIndex, - rowIndex = rowIndex - }, out var generation); - - var entity = new Entity(id, generation); - emptyArchetype.SetEntity(chunkIndex, rowIndex, entity); - - return entity; + return entities[0]; } /// @@ -75,28 +72,59 @@ public unsafe partial class EntityManager : IDisposable /// The created entity. public Entity CreateEntity(params ReadOnlySpan> componentTypeIDs) { - var signatureHash = ComponentRegister.GetHashCode(componentTypeIDs); - var arcID = _world.GetArchetypeIDBySignatureHash(signatureHash); + var entities = (Span)stackalloc Entity[1]; + CreateEntities(1, entities, componentTypeIDs); - if (arcID.IsNotValid) + return entities[0]; + } + + /// + /// Create multiple entities with no components. + /// + /// The number of entities to create. + /// The span to store the created entities. + public void CreateEntities(int count, Span entities) + { + ref var emptyArchetype = ref _world.GetArchetypeReference(World.EmptyArchetypeID); + emptyArchetype.AllocateEntity(out var chunkIndex, out var rowIndex); + + for (var i = 0; i < count; i++) { - arcID = _world.CreateArchetype(componentTypeIDs, signatureHash); + var id = _entityLocations.Add(new EntityLocation + { + archetypeID = World.EmptyArchetypeID, + chunkIndex = chunkIndex, + rowIndex = rowIndex + }, out var generation); + + var entity = new Entity(id, generation); + emptyArchetype.SetEntity(chunkIndex, rowIndex, entity); + + entities[i] = entity; } + } - ref var archetype = ref _world.GetArchetypeReference(arcID); - archetype.AllocateEntity(out var chunkIndex, out var rowIndex); + /// + /// Create multiple entities with no components. + /// + /// The number of entities to create. + public void CreateEntities(int count) + { + ref var emptyArchetype = ref _world.GetArchetypeReference(World.EmptyArchetypeID); + emptyArchetype.AllocateEntity(out var chunkIndex, out var rowIndex); - var id = _entityLocations.Add(new EntityLocation + for (var i = 0; i < count; i++) { - archetypeID = arcID, - chunkIndex = chunkIndex, - rowIndex = rowIndex - }, out var generation); + var id = _entityLocations.Add(new EntityLocation + { + archetypeID = World.EmptyArchetypeID, + chunkIndex = chunkIndex, + rowIndex = rowIndex + }, out var generation); - var entity = new Entity(id, generation); - archetype.SetEntity(chunkIndex, rowIndex, entity); - - return entity; + var entity = new Entity(id, generation); + emptyArchetype.SetEntity(chunkIndex, rowIndex, entity); + } } /// @@ -106,7 +134,7 @@ public unsafe partial class EntityManager : IDisposable /// The allocator to use for the returned array. /// The component type IDs to add to the entities. /// An array of the created entities. - public UnsafeArray CreateEntities(int count, Allocator allocator, params ReadOnlySpan> componentTypeIDs) + public void CreateEntities(int count, Span entities, params ReadOnlySpan> componentTypeIDs) { var signatureHash = ComponentRegister.GetHashCode(componentTypeIDs); var arcID = _world.GetArchetypeIDBySignatureHash(signatureHash); @@ -118,7 +146,6 @@ public unsafe partial class EntityManager : IDisposable ref var archetype = ref _world.GetArchetypeReference(arcID); - var entities = new UnsafeArray(count, allocator); for (var i = 0; i < count; i++) { archetype.AllocateEntity(out var chunkIndex, out var rowIndex); @@ -135,8 +162,6 @@ public unsafe partial class EntityManager : IDisposable entities[i] = entity; } - - return entities; } /// @@ -176,26 +201,26 @@ public unsafe partial class EntityManager : IDisposable /// Destroy the specified entity. /// /// The result status of the operation. - public ResultStatus DestroyEntity(Entity entity) + public ErrorStatus DestroyEntity(Entity entity) { if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location)) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } ref var archetype = ref _world.GetArchetypeReference(location.archetypeID); var r = archetype.RemoveEntity(location.chunkIndex, location.rowIndex); - if (r != ResultStatus.Success) + if (r != ErrorStatus.None) { return r; } if (!_entityLocations.Remove(entity.ID, entity.Generation)) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -214,11 +239,11 @@ public unsafe partial class EntityManager : IDisposable /// The component type ID of the singleton. /// Pointer to the component data. /// The result status of the operation. - public ResultStatus CreateSingleton(Identifier componentID, void* pComponent) + public ErrorStatus CreateSingleton(Identifier componentID, void* pComponent) { if (pComponent == null) { - return ResultStatus.InvalidArgument; + return ErrorStatus.InvalidArgument; } // Check if singleton already exists @@ -227,7 +252,7 @@ public unsafe partial class EntityManager : IDisposable if (arcID.IsValid) { - return ResultStatus.InvalidArgument; + return ErrorStatus.InvalidArgument; } arcID = _world.CreateArchetype([componentID], signatureHash); @@ -246,7 +271,7 @@ public unsafe partial class EntityManager : IDisposable archetype.SetEntity(chunkIndex, rowIndex, entity); archetype.SetComponentData(chunkIndex, rowIndex, componentID, pComponent); - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -255,7 +280,7 @@ public unsafe partial class EntityManager : IDisposable /// The component type. /// The component data. /// The result status of the operation. - public ResultStatus CreateSingleton(T component = default) + public ErrorStatus CreateSingleton(T component = default) where T : unmanaged, IComponent { return CreateSingleton(ComponentTypeID.value, &component); @@ -278,7 +303,7 @@ public unsafe partial class EntityManager : IDisposable ref var archetype = ref _world.GetArchetypeReference(arcID); var layoutResult = archetype.GetLayout(componentID); - if (layoutResult.Status != ResultStatus.Success) + if (layoutResult.Error != ErrorStatus.None) { return null; } @@ -311,8 +336,8 @@ public unsafe partial class EntityManager : IDisposable var src = oldArch._chunks[oldChunk].GetUnsafePtr() + layout.offset + (layout.size * oldRow); var r = newArch.GetLayout(layout.componentID); - Debug.Assert(r.Status == ResultStatus.Success); // This should always be true if the system is consistent. - if (r.Status != ResultStatus.Success) + Debug.Assert(r.Error == ErrorStatus.None); // This should always be true if the system is consistent. + if (r.Error != ErrorStatus.None) { continue; } @@ -330,13 +355,13 @@ public unsafe partial class EntityManager : IDisposable /// The component type ID to add. /// Pointer to the component data. /// The result status of the operation. - public ResultStatus AddComponent(Entity entity, Identifier componentID, void* pComponent) + public ErrorStatus AddComponent(Entity entity, Identifier componentID, void* pComponent) { // Find current location ref var location = ref _entityLocations.GetElementReferenceAt(entity.ID, entity.Generation, out var exist); if (!exist) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } // Build new archetype signature @@ -397,8 +422,8 @@ public unsafe partial class EntityManager : IDisposable newArchetype.SetComponentData(newChunkIndex, newRowIndex, componentID, pComponent); var r = oldArchetype.RemoveEntity(location.chunkIndex, location.rowIndex); - Debug.Assert(r == ResultStatus.Success); // We assert it because the entity should exist if the whole system is consistent. - if (r != ResultStatus.Success) + Debug.Assert(r == ErrorStatus.None); // We assert it because the entity should exist if the whole system is consistent. + if (r != ErrorStatus.None) { return r; } @@ -408,7 +433,7 @@ public unsafe partial class EntityManager : IDisposable location.chunkIndex = newChunkIndex; location.rowIndex = newRowIndex; - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -418,7 +443,7 @@ public unsafe partial class EntityManager : IDisposable /// The entity to add the component to. /// The component data. /// The result status of the operation. - public ResultStatus AddComponent(Entity entity, T component = default) + public ErrorStatus AddComponent(Entity entity, T component = default) where T : unmanaged, IComponent { return AddComponent(entity, ComponentTypeID.value, &component); @@ -430,13 +455,13 @@ public unsafe partial class EntityManager : IDisposable /// The entity to remove the component from. /// The component type ID to remove. /// The result status of the operation. - public ResultStatus RemoveComponent(Entity entity, Identifier componentID) + public ErrorStatus RemoveComponent(Entity entity, Identifier componentID) { // Find current location ref var location = ref _entityLocations.GetElementReferenceAt(entity.ID, entity.Generation, out var exist); if (!exist) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } // Build new archetype signature @@ -492,8 +517,8 @@ public unsafe partial class EntityManager : IDisposable newArchetype.SetEntity(newChunkIndex, newRowIndex, entity); var r = oldArchetype.RemoveEntity(location.chunkIndex, location.rowIndex); - Debug.Assert(r == ResultStatus.Success); // We assert it because the entity should exist if the whole system is consistent. - if (r != ResultStatus.Success) + Debug.Assert(r == ErrorStatus.None); // We assert it because the entity should exist if the whole system is consistent. + if (r != ErrorStatus.None) { return r; } @@ -503,7 +528,7 @@ public unsafe partial class EntityManager : IDisposable location.chunkIndex = newChunkIndex; location.rowIndex = newRowIndex; - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -512,7 +537,7 @@ public unsafe partial class EntityManager : IDisposable /// The component type. /// The entity to remove the component from. /// The result status of the operation. - public ResultStatus RemoveComponent(Entity entity) + public ErrorStatus RemoveComponent(Entity entity) where T : unmanaged, IComponent { return RemoveComponent(entity, ComponentTypeID.value); @@ -525,17 +550,17 @@ public unsafe partial class EntityManager : IDisposable /// The component type ID to set. /// Pointer to the component data. /// The result status of the operation. - public ResultStatus SetComponent(Entity entity, Identifier componentID, void* pComponent) + public ErrorStatus SetComponent(Entity entity, Identifier componentID, void* pComponent) { if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location)) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } ref var archetype = ref _world.GetArchetypeReference(location.archetypeID); archetype.SetComponentData(location.chunkIndex, location.rowIndex, componentID, pComponent); - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -544,7 +569,7 @@ public unsafe partial class EntityManager : IDisposable /// The component type. /// The entity to set the component data for. /// The component data. - public ResultStatus SetComponent(Entity entity, T component) + public ErrorStatus SetComponent(Entity entity, T component) where T : unmanaged, IComponent { return SetComponent(entity, ComponentTypeID.value, &component); @@ -616,11 +641,11 @@ public unsafe partial class EntityManager : IDisposable /// The component type ID of the enableable component.True to enable the component, false to disable it. /// The result status of the operation. - public ResultStatus SetEnabled(Entity entity, Identifier componentID, bool enabled) + public ErrorStatus SetEnabled(Entity entity, Identifier componentID, bool enabled) { if (!_entityLocations.TryGetElementAt(entity.ID, entity.Generation, out var location)) { - return ResultStatus.NotFound; + return ErrorStatus.NotFound; } ref var archetype = ref _world.GetArchetypeReference(location.archetypeID); @@ -628,9 +653,9 @@ public unsafe partial class EntityManager : IDisposable var rowIndex = location.rowIndex; var layoutResult = archetype.GetLayout(componentID); - if (layoutResult.Status != ResultStatus.Success) + if (layoutResult.Error != ErrorStatus.None) { - return layoutResult.Status; + return layoutResult.Error; } ref var chunk = ref archetype.GetChunkReference(chunkIndex); @@ -649,7 +674,7 @@ public unsafe partial class EntityManager : IDisposable maskBase[byteIndex] &= (byte)~(1 << bitIndex); } - return ResultStatus.Success; + return ErrorStatus.None; } /// @@ -659,7 +684,7 @@ public unsafe partial class EntityManager : IDisposable /// The entity to set the enabled state for. /// True to enable the component, false to disable it.The result status of the operation. - public ResultStatus SetEnabled(Entity entity, bool enabled) + public ErrorStatus SetEnabled(Entity entity, bool enabled) where T : unmanaged, IEnableableComponent { return SetEnabled(entity, ComponentTypeID.value, enabled); diff --git a/Ghost.Entities/EntityQuery.JobChunk.cs b/Ghost.Entities/EntityQuery.JobChunk.cs new file mode 100644 index 0000000..a1ea698 --- /dev/null +++ b/Ghost.Entities/EntityQuery.JobChunk.cs @@ -0,0 +1,87 @@ +using Ghost.Core; +using Misaki.HighPerformance.Jobs; +using Misaki.HighPerformance.LowLevel.Collections; +using System.Runtime.CompilerServices; + +namespace Ghost.Entities; + +public interface IJobChunk +{ + void Execute(ChunkView view, int threadIndex); +} + +internal unsafe struct ChunkInfo +{ + public Chunk* pChunk; + public Archetype* pArchetype; +} + +internal unsafe struct JobChunkBatch : IJobParallelFor + where TJob : unmanaged, IJobChunk +{ + + public TJob userJob; + public ReadOnlyUnsafeCollection chunkInfos; + + public void Execute(int loopIndex, int threadIndex) + { + var info = chunkInfos[loopIndex]; + var view = new ChunkView(in *info.pArchetype, in *info.pChunk); + + userJob.Execute(view, threadIndex); + } +} + +internal struct DisposeJobChunk : IJob +{ + public UnsafeList list; + + public void Execute(int threadIndex) + { + list.Dispose(); + } +} + +public unsafe partial struct EntityQuery +{ + public JobHandle ScheduleChunkParallel(TJob job, int batchSize, JobHandle dependency) + where TJob : unmanaged, IJobChunk + { + var world = World.GetWorld(_worldID).GetValueOrThrow(); + + var chunkInfos = new UnsafeList(_matchingArchetypes.Count * 2, JobScheduler.TempAllocatorHandle); + + foreach (var archID in _matchingArchetypes) + { + ref var arch = ref world.GetArchetypeReference(archID); + + for (int i = 0; i < arch.ChunkCount; i++) + { + var pChunk = (Chunk*)arch._chunks.GetUnsafePtr() + i; + + chunkInfos.Add(new ChunkInfo + { + pArchetype = (Archetype*)Unsafe.AsPointer(ref arch), + pChunk = pChunk + }); + } + } + + var batchJob = new JobChunkBatch + { + userJob = job, + chunkInfos = chunkInfos.AsReadOnly() + }; + + var handle = world.JobScheduler.ScheduleParallel(ref batchJob, chunkInfos.Count, batchSize, dependency); + + var disposeJob = new DisposeJobChunk + { + list = chunkInfos + }; + + world.JobScheduler.Schedule(ref disposeJob, handle); + + return handle; + } +} diff --git a/Ghost.Entities/Query.cs b/Ghost.Entities/Query.cs index 2749020..2f36726 100644 --- a/Ghost.Entities/Query.cs +++ b/Ghost.Entities/Query.cs @@ -74,91 +74,102 @@ public struct EntityQueryMask : IDisposable, IEquatable } } -public unsafe partial struct EntityQuery : IIdentifierType, IDisposable +/// +/// Provides a read-only view over a chunk of entities and their component data within an archetype. +/// +/// This does not filter disabled/enabled components. You must handle that manually. +public readonly unsafe ref struct ChunkView { - /// - /// Provides a read-only view over a chunk of entities and their component data within an archetype. - /// - /// This does not filter disabled/enabled components. You must handle that manually. - public readonly ref struct ChunkView + private readonly ReadOnlyUnsafeCollection _layouts; + private readonly byte* _pChunkData; + private readonly int _entityOffset; + private readonly int _entityCount; + + public readonly int Count => _entityCount; + + internal ChunkView(ReadOnlyUnsafeCollection layouts, byte* pChunkData, int entityOffset, int entityCount) { - private readonly ref Archetype _archetype; - private readonly ref Chunk _chunk; - - public readonly int Count => _chunk.Count; - - internal ChunkView(ref Archetype archetype, int chunkIndex) - { - _archetype = ref archetype; - _chunk = ref archetype.GetChunkReference(chunkIndex); - } - - /// - /// Returns a read-only span containing structuralAll entities stored in the current chunk. - /// - /// A read-only span of values representing the entities in the chunk. - public readonly ReadOnlySpan GetEntities() - { - var ptr = _chunk.GetUnsafePtr(); - var pEntity = (Entity*)(ptr + _archetype.EntityIDsOffset); - return new ReadOnlySpan(pEntity, _chunk.Count); - } - - /// - /// Gets a span providing direct access to the component data of type T0 for structuralAll entities in the chunk. - /// - /// The type of component to access. Must be an unmanaged type that implements . - /// A span of type containing the component data for each entity in the chunk. - /// Thrown if the specified component type is not present in the archetype. - public readonly Span GetComponentData() - where T : unmanaged, IComponent - { - var layout = _archetype.GetLayout(ComponentTypeID.value).GetValueOrThrow(ResultStatus.Success); - var ptr = _chunk.GetUnsafePtr() + layout.offset; - return new Span(ptr, _chunk.Count); - } - - /// - /// Gets a bit set representing the enabled state of each instance of the specified enableable component - /// type within the current chunk. - /// - /// The component type for which to retrieve enablement bits. Must be unmanaged and implement . - /// A that provides access to the enablement bits for all instances of the specified component type in the chunk. - /// Thrown if the specified component type does not support enablement. - public SpanBitSet GetEnableBits() - where T : unmanaged, IEnableableComponent - { - var layout = _archetype.GetLayout(ComponentTypeID.value).GetValueOrThrow(ResultStatus.Success); - if (layout.enableBitsOffset == -1) - { - throw new InvalidOperationException($"Component {typeof(T).FullName} is not enableable."); - } - - var maskBase = _chunk.GetUnsafePtr() + layout.enableBitsOffset; - return new SpanBitSet(new Span(maskBase, (_chunk.Count + 31) / 32)); - } - - /// - /// Determines whether the specified component of type at the given index is currently enabled. - /// - /// The type of the component to check. Must be an unmanaged type that implements . - /// The zero-based index of the component instance to check within the chunk. - /// true if the component at the specified index is enabled; otherwise, false. - /// Thrown if the specified component type does not support enable/disable functionality. - public readonly bool IsComponentEnabled(int index) - where T : unmanaged, IEnableableComponent - { - var layout = _archetype.GetLayout(ComponentTypeID.value).GetValueOrThrow(ResultStatus.Success); - if (layout.enableBitsOffset == -1) - { - throw new InvalidOperationException($"Component {typeof(T).FullName} is not enableable."); - } - - var maskBase = _chunk.GetUnsafePtr() + layout.enableBitsOffset; - return CheckBit(maskBase, index); - } + _layouts = layouts; + _pChunkData = pChunkData; + _entityOffset = entityOffset; + _entityCount = entityCount; } + internal ChunkView(ref readonly Archetype archetype, ref readonly Chunk chunk) + { + _layouts = archetype._layouts.AsReadOnly(); + _pChunkData = chunk.GetUnsafePtr(); + _entityOffset = archetype.EntityIDsOffset; + _entityCount = chunk.Count; + } + + /// + /// Returns a read-only span containing structuralAll entities stored in the current chunk. + /// + /// A read-only span of values representing the entities in the chunk. + public readonly ReadOnlySpan GetEntities() + { + var pEntity = (Entity*)(_pChunkData + _entityOffset); + return new ReadOnlySpan(pEntity, _entityCount); + } + + /// + /// Gets a span providing direct access to the component data of type T0 for structuralAll entities in the chunk. + /// + /// The type of component to access. Must be an unmanaged type that implements . + /// A span of type containing the component data for each entity in the chunk. + /// Thrown if the specified component type is not present in the archetype. + public readonly Span GetComponentData() + where T : unmanaged, IComponent + { + var layout = _layouts[ComponentTypeID.value]; + var pComponentData = _pChunkData + layout.offset; + return new Span(pComponentData, _entityCount); + } + + /// + /// Gets a bit set representing the enabled state of each instance of the specified enableable component + /// type within the current chunk. + /// + /// The component type for which to retrieve enablement bits. Must be unmanaged and implement . + /// A that provides access to the enablement bits for all instances of the specified component type in the chunk. + /// Thrown if the specified component type does not support enablement. + public readonly SpanBitSet GetEnableBits() + where T : unmanaged, IEnableableComponent + { + var layout = _layouts[ComponentTypeID.value]; + if (layout.enableBitsOffset == -1) + { + throw new InvalidOperationException($"Component {typeof(T).FullName} is not enableable."); + } + + var maskBase = _pChunkData + layout.enableBitsOffset; + return new SpanBitSet(new Span(maskBase, (_entityCount + 31) / 32)); + } + + /// + /// Determines whether the specified component of type at the given index is currently enabled. + /// + /// The type of the component to check. Must be an unmanaged type that implements . + /// The zero-based index of the component instance to check within the chunk. + /// true if the component at the specified index is enabled; otherwise, false. + /// Thrown if the specified component type does not support enable/disable functionality. + public readonly bool IsComponentEnabled(int index) + where T : unmanaged, IEnableableComponent + { + var layout = _layouts[ComponentTypeID.value]; + if (layout.enableBitsOffset == -1) + { + throw new InvalidOperationException($"Component {typeof(T).FullName} is not enableable."); + } + + var pMask = _pChunkData + layout.enableBitsOffset; + return EntityQuery.CheckBit(pMask, index); + } +} + +public unsafe partial struct EntityQuery : IIdentifierType, IDisposable +{ /// /// Provides an enumerator for iterating over chunks of entities and their component data that match a set of archetypes within a world. /// @@ -182,7 +193,8 @@ public unsafe partial struct EntityQuery : IIdentifierType, IDisposable get { ref var archetype = ref _iterator._world.GetArchetypeReference(_iterator._matchingArchetypes[_archetypeIndex]); - return new ChunkView(ref archetype, _chunkIndex); + ref var chunk = ref archetype.GetChunkReference(_chunkIndex); + return new ChunkView(in archetype, in chunk); } } @@ -250,7 +262,7 @@ public unsafe partial struct EntityQuery : IIdentifierType, IDisposable { // Get the EnableBitmask for this component in this chunk var layoutResult = archetype.GetLayout(id); - if (layoutResult.Status != ResultStatus.Success + if (layoutResult.Error != ErrorStatus.None // Not enableable, always true || layoutResult.Value.enableBitsOffset == -1) { @@ -269,7 +281,7 @@ public unsafe partial struct EntityQuery : IIdentifierType, IDisposable while (it.Next(out var id)) { var layoutResult = archetype.GetLayout(id); - if (layoutResult.Status != ResultStatus.Success) + if (layoutResult.Error != ErrorStatus.None) { continue; } @@ -290,7 +302,7 @@ public unsafe partial struct EntityQuery : IIdentifierType, IDisposable while (it.Next(out var id)) { var layoutResult = archetype.GetLayout(id); - if (layoutResult.Status != ResultStatus.Success) + if (layoutResult.Error != ErrorStatus.None) { continue; } diff --git a/Ghost.Entities/Templates/EntityQuery.ComponentIterator.gen.cs b/Ghost.Entities/Templates/EntityQuery.ComponentIterator.gen.cs index 15b9363..f2aa5ff 100644 --- a/Ghost.Entities/Templates/EntityQuery.ComponentIterator.gen.cs +++ b/Ghost.Entities/Templates/EntityQuery.ComponentIterator.gen.cs @@ -54,7 +54,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 1; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -124,7 +124,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -147,10 +146,9 @@ public unsafe partial struct EntityQuery public readonly ComponentIterator GetComponentIterator() where T0 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -223,7 +221,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 2; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -293,7 +291,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -317,10 +314,9 @@ public unsafe partial struct EntityQuery where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -402,7 +398,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 3; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -472,7 +468,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -497,10 +492,9 @@ public unsafe partial struct EntityQuery where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -591,7 +585,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 4; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -661,7 +655,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -687,10 +680,9 @@ public unsafe partial struct EntityQuery where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -790,7 +782,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 5; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -860,7 +852,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -887,10 +878,9 @@ public unsafe partial struct EntityQuery where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -999,7 +989,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 6; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -1069,7 +1059,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -1097,10 +1086,9 @@ public unsafe partial struct EntityQuery where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -1218,7 +1206,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 7; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -1288,7 +1276,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -1317,10 +1304,9 @@ public unsafe partial struct EntityQuery where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - public readonly ref struct ComponentIterator where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent @@ -1447,7 +1433,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 8; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -1517,7 +1503,6 @@ public unsafe partial struct EntityQuery } } } - } private readonly ReadOnlyUnsafeCollection> _matchingArchetypes; @@ -1547,8 +1532,7 @@ public unsafe partial struct EntityQuery where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent { - return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } - } diff --git a/Ghost.Entities/Templates/EntityQuery.ComponentIterator.tt b/Ghost.Entities/Templates/EntityQuery.ComponentIterator.tt index 9d1a526..6a4eb8a 100644 --- a/Ghost.Entities/Templates/EntityQuery.ComponentIterator.tt +++ b/Ghost.Entities/Templates/EntityQuery.ComponentIterator.tt @@ -99,7 +99,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < <#= i #>; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -191,7 +191,7 @@ public unsafe partial struct EntityQuery public readonly ComponentIterator<<#= generics#>> GetComponentIterator<<#= generics#>>() <#= restrictions #> { - return new ComponentIterator<<#= generics#>>(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new ComponentIterator<<#= generics#>>(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } <# } #> diff --git a/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.gen.cs b/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.gen.cs index 78db574..0c66003 100644 --- a/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.gen.cs +++ b/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.gen.cs @@ -77,7 +77,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 1; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -169,7 +169,7 @@ public unsafe partial struct EntityQuery public readonly EntityComponentIterator GetEntityComponentIterator() where T0 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -251,7 +251,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 2; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -344,7 +344,7 @@ public unsafe partial struct EntityQuery where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -435,7 +435,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 3; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -529,7 +529,7 @@ public unsafe partial struct EntityQuery where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -629,7 +629,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 4; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -724,7 +724,7 @@ public unsafe partial struct EntityQuery where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -833,7 +833,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 5; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -929,7 +929,7 @@ public unsafe partial struct EntityQuery where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -1047,7 +1047,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 6; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -1144,7 +1144,7 @@ public unsafe partial struct EntityQuery where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -1271,7 +1271,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 7; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -1369,7 +1369,7 @@ public unsafe partial struct EntityQuery where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } public readonly ref struct EntityComponentIterator @@ -1505,7 +1505,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 8; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -1604,7 +1604,7 @@ public unsafe partial struct EntityQuery where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent { - return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } } diff --git a/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.tt b/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.tt index f45e28e..1bfe419 100644 --- a/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.tt +++ b/Ghost.Entities/Templates/EntityQuery.EntityComponentIterator.tt @@ -100,7 +100,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < <#= i #>; index++) { var layout = _currentArchetype.GetLayout(_compTypeIDs[index]) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); _offsets[index] = layout.offset; _compBasePtrs[index] = (long)(_chunkBasePtr + _offsets[index]); } @@ -192,7 +192,7 @@ public unsafe partial struct EntityQuery public readonly EntityComponentIterator<<#= generics#>> GetEntityComponentIterator<<#= generics#>>() <#= restrictions #> { - return new EntityComponentIterator<<#= generics#>>(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success)); + return new EntityComponentIterator<<#= generics#>>(_matchingArchetypes.AsReadOnly(), _mask, World.GetWorld(_worldID).GetValueOrThrow()); } <# } #> diff --git a/Ghost.Entities/Templates/EntityQuery.ForEach.gen.cs b/Ghost.Entities/Templates/EntityQuery.ForEach.gen.cs index 74d85ca..f111921 100644 --- a/Ghost.Entities/Templates/EntityQuery.ForEach.gen.cs +++ b/Ghost.Entities/Templates/EntityQuery.ForEach.gen.cs @@ -1,4 +1,3 @@ - using Ghost.Core; namespace Ghost.Entities; @@ -8,7 +7,7 @@ public unsafe partial struct EntityQuery public readonly void ForEach(ForEach action) where T0 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value }; var offsets = stackalloc int[1]; @@ -21,7 +20,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 1; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -64,7 +63,7 @@ public unsafe partial struct EntityQuery where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[2]; @@ -77,7 +76,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 2; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -122,7 +121,7 @@ public unsafe partial struct EntityQuery where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[3]; @@ -135,7 +134,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 3; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -182,7 +181,7 @@ public unsafe partial struct EntityQuery where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[4]; @@ -195,7 +194,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 4; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -244,7 +243,7 @@ public unsafe partial struct EntityQuery where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[5]; @@ -257,7 +256,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 5; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -308,7 +307,7 @@ public unsafe partial struct EntityQuery where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[6]; @@ -321,7 +320,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 6; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -374,7 +373,7 @@ public unsafe partial struct EntityQuery where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[7]; @@ -387,7 +386,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 7; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -442,7 +441,7 @@ public unsafe partial struct EntityQuery where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[8]; @@ -455,7 +454,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 8; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -504,7 +503,7 @@ public unsafe partial struct EntityQuery public readonly void ForEach(ForEachWithEntity action) where T0 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value }; var offsets = stackalloc int[1]; @@ -517,7 +516,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 1; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -561,7 +560,7 @@ public unsafe partial struct EntityQuery where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[2]; @@ -574,7 +573,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 2; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -620,7 +619,7 @@ public unsafe partial struct EntityQuery where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[3]; @@ -633,7 +632,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 3; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -681,7 +680,7 @@ public unsafe partial struct EntityQuery where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[4]; @@ -694,7 +693,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 4; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -744,7 +743,7 @@ public unsafe partial struct EntityQuery where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[5]; @@ -757,7 +756,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 5; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -809,7 +808,7 @@ public unsafe partial struct EntityQuery where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[6]; @@ -822,7 +821,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 6; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -876,7 +875,7 @@ public unsafe partial struct EntityQuery where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[7]; @@ -889,7 +888,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 7; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -945,7 +944,7 @@ public unsafe partial struct EntityQuery where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value, ComponentTypeID.value }; var offsets = stackalloc int[8]; @@ -958,7 +957,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < 8; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -1005,4 +1004,4 @@ public unsafe partial struct EntityQuery } } -} \ No newline at end of file +} diff --git a/Ghost.Entities/Templates/EntityQuery.ForEach.tt b/Ghost.Entities/Templates/EntityQuery.ForEach.tt index 88ed12a..1dbc780 100644 --- a/Ghost.Entities/Templates/EntityQuery.ForEach.tt +++ b/Ghost.Entities/Templates/EntityQuery.ForEach.tt @@ -16,8 +16,7 @@ public unsafe partial struct EntityQuery #> <# for (var i = 1; i <= Amount; i++) { - var generics = AppendGenerics(i); - var compGenerics = AppendGenericRefParameters(i); + var generics = AppendParameters(i, "T{0}"); var restrictions = AppendGenericRestrictionsMultiline(i, "unmanaged, IComponent", 2); var delegateTupe = isForEachWithEntity ? "ForEachWithEntity" : "ForEach"; @@ -25,7 +24,7 @@ public unsafe partial struct EntityQuery public readonly void ForEach<<#= generics #>>(<#= delegateTupe #><<#= generics #>> action) <#= restrictions #> { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); var compTypeIDs = stackalloc int[] { <#= AppendGenerics(i, "ComponentTypeID.value") #> }; var offsets = stackalloc int[<#= i #>]; @@ -38,7 +37,7 @@ public unsafe partial struct EntityQuery for (var index = 0; index < <#= i #>; index++) { var layoutResult = archetype.GetLayout(compTypeIDs[index]); - if (layoutResult.Status != ResultStatus.Success) + if (!layoutResult) { hasAllComponents = false; break; @@ -75,9 +74,9 @@ public unsafe partial struct EntityQuery <# if (isForEachWithEntity) { #> var pEntity = (Entity*)(pChunkData + archetype.EntityIDsOffset + (sizeof(Entity) * entityIndex)); - action(*pEntity, <#= AppendRefParameters(i, "*pComp{0}") #>); + action(*pEntity, <#= AppendParameters(i, "ref *pComp{0}") #>); <# } else { #> - action(<#= AppendRefParameters(i, "*pComp{0}") #>); + action(<#= AppendParameters(i, "ref *pComp{0}") #>); <# } #> } } @@ -86,4 +85,4 @@ public unsafe partial struct EntityQuery <# } #> <# } #> -} \ No newline at end of file +} diff --git a/Ghost.Entities/Templates/EntityQuery.JobEntity.gen.cs b/Ghost.Entities/Templates/EntityQuery.JobEntity.gen.cs index 6566a27..8a369ba 100644 --- a/Ghost.Entities/Templates/EntityQuery.JobEntity.gen.cs +++ b/Ghost.Entities/Templates/EntityQuery.JobEntity.gen.cs @@ -8,7 +8,7 @@ namespace Ghost.Entities; public interface IJobEntity where T0 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0); + void Execute(Entity entity, ref T0 component0, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -43,7 +43,7 @@ internal unsafe struct JobEntityBatch : IJobParallelFor continue; } - userJob.Execute(pEntity[i], ref ptr0[i]); + userJob.Execute(pEntity[i], ref ptr0[i], threadIndex); } } } @@ -52,7 +52,7 @@ public interface IJobEntity where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1); + void Execute(Entity entity, ref T0 component0, ref T1 component1, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -100,7 +100,7 @@ internal unsafe struct JobEntityBatch : IJobParallelFor continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], threadIndex); } } } @@ -110,7 +110,7 @@ public interface IJobEntity where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2); + void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -171,7 +171,7 @@ internal unsafe struct JobEntityBatch : IJobParallelFor continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], threadIndex); } } } @@ -182,7 +182,7 @@ public interface IJobEntity where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3); + void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -256,7 +256,7 @@ internal unsafe struct JobEntityBatch : IJobParallelFor continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], threadIndex); } } } @@ -268,7 +268,7 @@ public interface IJobEntity where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4); + void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -355,7 +355,7 @@ internal unsafe struct JobEntityBatch : IJobParallelFo continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], threadIndex); } } } @@ -368,7 +368,7 @@ public interface IJobEntity where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5); + void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -468,7 +468,7 @@ internal unsafe struct JobEntityBatch : IJobParall continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], ref ptr5[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], ref ptr5[i], threadIndex); } } } @@ -482,7 +482,7 @@ public interface IJobEntity where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6); + void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -595,7 +595,7 @@ internal unsafe struct JobEntityBatch : IJobPa continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], ref ptr5[i], ref ptr6[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], ref ptr5[i], ref ptr6[i], threadIndex); } } } @@ -610,7 +610,7 @@ public interface IJobEntity where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent { - void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6, ref T7 component7); + void Execute(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6, ref T7 component7, int threadIndex); } internal unsafe struct JobEntityBatch : IJobParallelFor @@ -736,7 +736,7 @@ internal unsafe struct JobEntityBatch : IJ continue; } - userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], ref ptr5[i], ref ptr6[i], ref ptr7[i]); + userJob.Execute(pEntity[i], ref ptr0[i], ref ptr1[i], ref ptr2[i], ref ptr3[i], ref ptr4[i], ref ptr5[i], ref ptr6[i], ref ptr7[i], threadIndex); } } } @@ -768,7 +768,7 @@ public unsafe partial struct EntityQuery where TJob : unmanaged, IJobEntity where T0 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -790,7 +790,7 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -871,7 +871,7 @@ public unsafe partial struct EntityQuery where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -896,9 +896,9 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -995,7 +995,7 @@ public unsafe partial struct EntityQuery where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -1023,11 +1023,11 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout2 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -1140,7 +1140,7 @@ public unsafe partial struct EntityQuery where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -1171,13 +1171,13 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout2 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout3 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -1306,7 +1306,7 @@ public unsafe partial struct EntityQuery where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -1340,15 +1340,15 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout2 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout3 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout4 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -1493,7 +1493,7 @@ public unsafe partial struct EntityQuery where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -1530,17 +1530,17 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout2 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout3 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout4 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout5 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -1701,7 +1701,7 @@ public unsafe partial struct EntityQuery where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -1741,19 +1741,19 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout2 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout3 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout4 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout5 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout6 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) @@ -1930,7 +1930,7 @@ public unsafe partial struct EntityQuery where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -1973,21 +1973,21 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype var layout0 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout1 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout2 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout3 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout4 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout5 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout6 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); var layout7 = arch.GetLayout(ComponentTypeID.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); // Add all chunks from this archetype for (var i = 0; i < arch.ChunkCount; i++) diff --git a/Ghost.Entities/Templates/EntityQuery.JobEntity.tt b/Ghost.Entities/Templates/EntityQuery.JobEntity.tt index f4763ac..9b1c228 100644 --- a/Ghost.Entities/Templates/EntityQuery.JobEntity.tt +++ b/Ghost.Entities/Templates/EntityQuery.JobEntity.tt @@ -19,7 +19,7 @@ namespace Ghost.Entities; public interface IJobEntity<<#= generics #>> <#= restrictions #> { - void Execute(Entity entity, <#= AppendParameters(i, "ref T{0} component{0}") #>); + void Execute(Entity entity, <#= AppendParameters(i, "ref T{0} component{0}") #>, int threadIndex); } internal unsafe struct JobEntityBatch> : IJobParallelFor @@ -62,7 +62,7 @@ internal unsafe struct JobEntityBatch> : IJobParallelFor } <# } #> - userJob.Execute(pEntity[i], <#= AppendParameters(i, "ref ptr{0}[i]") #>); + userJob.Execute(pEntity[i], <#= AppendParameters(i, "ref ptr{0}[i]") #>, threadIndex); } } } @@ -104,7 +104,7 @@ public unsafe partial struct EntityQuery where TJob : unmanaged, IJobEntity<<#= generics #>> <#= restrictions #> { - var world = World.GetWorld(_worldID).GetValueOrThrow(ResultStatus.Success); + var world = World.GetWorld(_worldID).GetValueOrThrow(); // 1. Flatten the World var chunkList = new UnsafeList(128, allocator); @@ -129,7 +129,7 @@ public unsafe partial struct EntityQuery // Get offsets ONCE per archetype <# for (var j = 0; j < i; j++){ #> var layout<#= j #> = arch.GetLayout(ComponentTypeID>.value) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); <# } #> // Add all chunks from this archetype diff --git a/Ghost.Entities/Templates/ForEach.gen.cs b/Ghost.Entities/Templates/ForEach.gen.cs index dd1105b..bbb85de 100644 --- a/Ghost.Entities/Templates/ForEach.gen.cs +++ b/Ghost.Entities/Templates/ForEach.gen.cs @@ -1,92 +1,35 @@ - namespace Ghost.Entities; public delegate void ForEach(ref T0 component0) where T0 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1, ref T2 component2) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent - where T5 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent - where T5 : unmanaged, IComponent - where T6 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent; public delegate void ForEach(ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6, ref T7 component7) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent - where T5 : unmanaged, IComponent - where T6 : unmanaged, IComponent - where T7 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0) where T0 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent - where T5 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent - where T5 : unmanaged, IComponent - where T6 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent; public delegate void ForEachWithEntity(Entity entity, ref T0 component0, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6, ref T7 component7) - where T0 : unmanaged, IComponent - where T1 : unmanaged, IComponent - where T2 : unmanaged, IComponent - where T3 : unmanaged, IComponent - where T4 : unmanaged, IComponent - where T5 : unmanaged, IComponent - where T6 : unmanaged, IComponent - where T7 : unmanaged, IComponent; + where T0 : unmanaged, IComponent where T1 : unmanaged, IComponent where T2 : unmanaged, IComponent where T3 : unmanaged, IComponent where T4 : unmanaged, IComponent where T5 : unmanaged, IComponent where T6 : unmanaged, IComponent where T7 : unmanaged, IComponent; diff --git a/Ghost.Entities/Templates/ForEach.tt b/Ghost.Entities/Templates/ForEach.tt index 8165864..7cf2691 100644 --- a/Ghost.Entities/Templates/ForEach.tt +++ b/Ghost.Entities/Templates/ForEach.tt @@ -10,18 +10,18 @@ namespace Ghost.Entities; { var generics = AppendParameters(i, "T{0}"); var compGenerics = AppendParameters(i, "ref T{0} component{0}"); - var restrictions = AppendGenericRestrictionsMultiline(i, "unmanaged, IComponent", 1); + var restrictions = AppendGenericRestrictions(i, "unmanaged, IComponent"); #> public delegate void ForEach<<#= generics #>>(<#= compGenerics #>) -<#= restrictions #>; + <#= restrictions #>; <# } #> <# for (var i = 1; i <= Amount; i++) { var generics = AppendParameters(i, "T{0}"); var compGenerics = AppendParameters(i, "ref T{0} component{0}"); - var restrictions = AppendGenericRestrictionsMultiline(i, "unmanaged, IComponent", 1); + var restrictions = AppendGenericRestrictions(i, "unmanaged, IComponent"); #> public delegate void ForEachWithEntity<<#= generics #>>(Entity entity, <#= compGenerics #>) -<#= restrictions #>; + <#= restrictions #>; <# } #> diff --git a/Ghost.Entities/Templates/Helpers.ttinclude b/Ghost.Entities/Templates/Helpers.ttinclude index 8dba87f..efa6700 100644 --- a/Ghost.Entities/Templates/Helpers.ttinclude +++ b/Ghost.Entities/Templates/Helpers.ttinclude @@ -144,4 +144,4 @@ return sb; } -#> \ No newline at end of file +#> diff --git a/Ghost.Entities/World.cs b/Ghost.Entities/World.cs index 0b88779..9333a67 100644 --- a/Ghost.Entities/World.cs +++ b/Ghost.Entities/World.cs @@ -48,20 +48,20 @@ public partial class World } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Result GetWorld(Identifier id) + public static Result GetWorld(Identifier id) { if (id.value < 0 || id.value >= s_worlds.Count) { - return Result.Create(default(World)!, ResultStatus.NotFound); + return ErrorStatus.InvalidArgument; } var world = s_worlds[id.value]; if (world is null) { - return Result.Create(default(World)!, ResultStatus.NotFound); + return ErrorStatus.NotFound; } - return Result.Create(world, ResultStatus.Success); + return world; } } @@ -102,6 +102,9 @@ public partial class World : IIdentifierType, IDisposable, IEquatable /// /// Gets the main entity command buffer for this world. /// + /// + /// Use to get thread-local command buffers for multi-threaded jobs. + /// public EntityCommandBuffer EntityCommandBuffer => _entityCommandBuffer; private World(Identifier id, int entityCapacity, JobScheduler jobScheduler) diff --git a/Ghost.Graphics/Core/Material.cs b/Ghost.Graphics/Core/Material.cs index b3adca8..a1d1422 100644 --- a/Ghost.Graphics/Core/Material.cs +++ b/Ghost.Graphics/Core/Material.cs @@ -1,5 +1,6 @@ using Ghost.Core; using Ghost.Graphics.RHI; +using Misaki.HighPerformance.LowLevel; using Misaki.HighPerformance.LowLevel.Buffer; using Misaki.HighPerformance.LowLevel.Collections; using System.Runtime.CompilerServices; @@ -83,15 +84,15 @@ public struct Material : IResourceReleasable, IHandleType } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly unsafe Result GetPropertyCache() + public readonly unsafe Result GetPropertyCache() where T : unmanaged { if (sizeof(T) != _cBufferCache.Size) { - return Result.Create(default(T), ResultStatus.InvalidArgument); + return ErrorStatus.InvalidArgument; } - return Result.Create(*(T*)_cBufferCache.CpuData.GetUnsafePtr(), ResultStatus.Success); + return *(T*)_cBufferCache.CpuData.GetUnsafePtr(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -106,28 +107,28 @@ public struct Material : IResourceReleasable, IHandleType } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly unsafe ResultStatus SetPropertyCache(ref readonly T data) + public readonly unsafe ErrorStatus SetPropertyCache(ref readonly T data) where T : unmanaged { if (sizeof(T) != _cBufferCache.Size) { - return ResultStatus.InvalidArgument; + return ErrorStatus.InvalidArgument; } Unsafe.WriteUnaligned(_cBufferCache.CpuData.GetUnsafePtr(), data); - return ResultStatus.Success; + return ErrorStatus.None; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public readonly unsafe ResultStatus SetRawPropertyCache(ReadOnlySpan data) + public readonly unsafe ErrorStatus SetRawPropertyCache(ReadOnlySpan data) { if (data.Length != _cBufferCache.Size) { - return ResultStatus.InvalidArgument; + return ErrorStatus.InvalidArgument; } Unsafe.WriteUnaligned(_cBufferCache.CpuData.GetUnsafePtr(), data); - return ResultStatus.Success; + return ErrorStatus.None; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Ghost.Graphics/Core/RenderingContext.cs b/Ghost.Graphics/Core/RenderingContext.cs index b1d631a..a87cba3 100644 --- a/Ghost.Graphics/Core/RenderingContext.cs +++ b/Ghost.Graphics/Core/RenderingContext.cs @@ -49,7 +49,7 @@ public readonly unsafe ref struct RenderingContext CommandBufferType.Graphics => _engine.Device.GraphicsQueue, CommandBufferType.Compute => _engine.Device.ComputeQueue, CommandBufferType.Copy => _engine.Device.CopyQueue, - _ => throw new ArgumentOutOfRangeException(), + _ => throw new InvalidOperationException("Unknown command buffer type."), }; queue.Submit(commandBuffer); @@ -123,8 +123,8 @@ public readonly unsafe ref struct RenderingContext localToWorld = localToWorld, worldBoundsMin = meshData.BoundingBox.Min, worldBoundsMax = meshData.BoundingBox.Max, - vertexBuffer = _engine.ResourceDatabase.GetBindlessIndex(meshData.VertexBuffer.AsResource()).GetValueOrThrow(ResultStatus.Success), - indexBuffer = _engine.ResourceDatabase.GetBindlessIndex(meshData.IndexBuffer.AsResource()).GetValueOrThrow(ResultStatus.Success), + vertexBuffer = _engine.ResourceDatabase.GetBindlessIndex(meshData.VertexBuffer.AsResource()).GetValueOrThrow(), + indexBuffer = _engine.ResourceDatabase.GetBindlessIndex(meshData.IndexBuffer.AsResource()).GetValueOrThrow(), }; var bufferHandle = meshData.ObjectDataBuffer.AsResource(); @@ -147,7 +147,7 @@ public readonly unsafe ref struct RenderingContext where T : unmanaged { var desc = ResourceDatabase.GetResourceDescription(texture.AsResource()) - .GetValueOrThrow(ResultStatus.Success); + .GetValueOrThrow(); if (data.Length * sizeof(T) != desc.TextureDescription.GetTotalBytes()) { @@ -180,7 +180,7 @@ public readonly unsafe ref struct RenderingContext var shader = ResourceDatabase.GetShaderReference(materialRef.Shader); var keyResult = shader.TryGetPassKey(passName, out var passIndex); - if (keyResult.Status != ResultStatus.Success) + if (keyResult.Error != ErrorStatus.None) { throw new Exception(keyResult.ToString()); } @@ -208,4 +208,4 @@ public readonly unsafe ref struct RenderingContext var threadGroupCountX = ((uint)meshRef.IndexCount + numThreadsX - 1) / numThreadsX; _directCmd.DispatchMesh(threadGroupCountX, 1, 1); } -} \ No newline at end of file +} diff --git a/Ghost.Graphics/Core/Shader.cs b/Ghost.Graphics/Core/Shader.cs index 9d56a77..8a173b9 100644 --- a/Ghost.Graphics/Core/Shader.cs +++ b/Ghost.Graphics/Core/Shader.cs @@ -101,17 +101,17 @@ public class Shader : IResourceReleasable, IIdentifierType return ref _passes[index]; } - public RefResult TryGetPassKey(string passName, out int passIndex) + public RefResult TryGetPassKey(string passName, out int passIndex) { var index = _passLookup.GetValueOrDefault(passName, -1); if (index == -1) { passIndex = -1; - return Result.CreateRef(ref Unsafe.NullRef(), ResultStatus.NotFound); + return ErrorStatus.NotFound; } passIndex = index; - return Result.CreateRef(ref _passes[index], ResultStatus.Success); + return RefResult.Success(ref _passes[index]); } void IResourceReleasable.ReleaseResource(IResourceDatabase database) diff --git a/Ghost.Graphics/D3D12/D3D12CommandBuffer.cs b/Ghost.Graphics/D3D12/D3D12CommandBuffer.cs index 3144d4f..a5a77ff 100644 --- a/Ghost.Graphics/D3D12/D3D12CommandBuffer.cs +++ b/Ghost.Graphics/D3D12/D3D12CommandBuffer.cs @@ -130,7 +130,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer #if DEBUG [DoesNotReturn] #endif - private void RecordError(string cmdName, ResultStatus status) + private void RecordError(string cmdName, ErrorStatus status) { #if DEBUG throw new InvalidOperationException($"Error at {cmdName} with {status}"); @@ -183,7 +183,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer _commandList.Get()->Close(); _isRecording = false; - if (_lastError.Status != ResultStatus.Success) + if (_lastError.Status != ErrorStatus.None) { return Result.Failure($"Command buffer ended with errors at {_lastError.CommandIndex}, command '{_lastError.CommandName}': {_lastError.Status}"); } @@ -220,21 +220,21 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer if (!desc.Resource.IsValid) { - RecordError(nameof(ResourceBarrier), ResultStatus.InvalidArgument); + RecordError(nameof(ResourceBarrier), ErrorStatus.InvalidArgument); continue; } var recordResult = _resourceDatabase.GetResourceRecord(desc.Resource); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(ResourceBarrier), recordResult.Status); + RecordError(nameof(ResourceBarrier), recordResult.Error); continue; } ref var record = ref recordResult.Value; if (record.state != desc.StateBefore) { - RecordError(nameof(ResourceBarrier), ResultStatus.InvalidState); + RecordError(nameof(ResourceBarrier), ErrorStatus.InvalidState); continue; } @@ -263,9 +263,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer } var recordResult = _resourceDatabase.GetResourceRecord(resource); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(ResourceBarrier), recordResult.Status); + RecordError(nameof(ResourceBarrier), recordResult.Error); return; } @@ -284,9 +284,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer IncrementCommandCount(); var recordResult = _resourceDatabase.GetResourceRecord(resource); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(ResourceBarrier), recordResult.Status); + RecordError(nameof(ResourceBarrier), recordResult.Error); return; } @@ -316,14 +316,14 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer var handle = renderTargets[i]; if (!handle.IsValid) { - RecordError(nameof(SetRenderTargets), ResultStatus.InvalidArgument); + RecordError(nameof(SetRenderTargets), ErrorStatus.InvalidArgument); continue; } var recordResult = _resourceDatabase.GetResourceRecord(handle.AsResource()); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(SetRenderTargets), recordResult.Status); + RecordError(nameof(SetRenderTargets), recordResult.Error); continue; } @@ -337,9 +337,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer if (pDsvHandle != null) { var recordResult = _resourceDatabase.GetResourceRecord(depthTarget.AsResource()); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(SetRenderTargets), recordResult.Status); + RecordError(nameof(SetRenderTargets), recordResult.Error); return; } @@ -362,14 +362,14 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer var rtDesc = rtDescs[i]; if (!rtDesc.Texture.IsValid) { - RecordError(nameof(BeginRenderPass), ResultStatus.InvalidArgument); + RecordError(nameof(BeginRenderPass), ErrorStatus.InvalidArgument); continue; } var recordResult = _resourceDatabase.GetResourceRecord(rtDesc.Texture.AsResource()); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(BeginRenderPass), recordResult.Status); + RecordError(nameof(BeginRenderPass), recordResult.Error); continue; } @@ -402,9 +402,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer if (pDsvDesc != null) { var recordResult = _resourceDatabase.GetResourceRecord(depthDesc.Texture.AsResource()); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(BeginRenderPass), recordResult.Status); + RecordError(nameof(BeginRenderPass), recordResult.Error); return; } @@ -458,10 +458,10 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer IncrementCommandCount(); var psor = _pipelineLibrary.GetGraphicsPSO(pipelineKey); - if (psor.Status != ResultStatus.Success) + if (psor.Error != ErrorStatus.None) { #if DEBUG || GHOST_EDITOR - Logger.LogError($"Failed to get graphics pipeline state object for key {pipelineKey}: {psor.Status}"); + Logger.LogError($"Failed to get graphics pipeline state object for key {pipelineKey}: {psor.Error}"); #endif return; } @@ -487,9 +487,9 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer IncrementCommandCount(); var recordResult = _resourceDatabase.GetResourceRecord(buffer.AsResource()); - if (recordResult.Status != ResultStatus.Success) + if (recordResult.Error != ErrorStatus.None) { - RecordError(nameof(BeginRenderPass), recordResult.Status); + RecordError(nameof(BeginRenderPass), recordResult.Error); return; } @@ -657,7 +657,7 @@ internal unsafe class D3D12CommandBuffer : ICommandBuffer var pSrcResource = _resourceDatabase.GetResource(src.AsResource()); if (pSrcResource == null || pDestResource == null) { - RecordError(nameof(CopyBuffer), ResultStatus.InvalidArgument); + RecordError(nameof(CopyBuffer), ErrorStatus.InvalidArgument); return; } diff --git a/Ghost.Graphics/D3D12/D3D12PipelineLibrary.cs b/Ghost.Graphics/D3D12/D3D12PipelineLibrary.cs index f911ee7..8332014 100644 --- a/Ghost.Graphics/D3D12/D3D12PipelineLibrary.cs +++ b/Ghost.Graphics/D3D12/D3D12PipelineLibrary.cs @@ -161,7 +161,7 @@ internal unsafe class D3D12PipelineLibrary : IPipelineLibrary if (File.Exists(filePath)) { - var fileBytes = File.ReadAllBytes(filePath!); + var fileBytes = File.ReadAllBytes(filePath); fixed (byte* pFileBytes = fileBytes) { ThrowIfFailed(_device.NativeDevice.Get()->CreatePipelineLibrary(pFileBytes, (nuint)fileBytes.Length, __uuidof(pLibrary), (void**)&pLibrary)); @@ -224,7 +224,7 @@ internal unsafe class D3D12PipelineLibrary : IPipelineLibrary RegisterSlot = info.BindPoint, RegisterSpace = info.Space, SizeInBytes = info.Size, - Properties = info.Properties ?? Array.Empty(), + Properties = info.Properties ?? [], }; } } @@ -401,14 +401,14 @@ internal unsafe class D3D12PipelineLibrary : IPipelineLibrary return key; } - public Result, ResultStatus> GetGraphicsPSO(GraphicsPipelineKey key) + public Result, ErrorStatus> GetGraphicsPSO(GraphicsPipelineKey key) { if (_pipelineCache.TryGetValue(key, out var cacheEntry)) { - return Result.Create(new SharedPtr(cacheEntry.pso.Get()), ResultStatus.Success); + return cacheEntry.pso.Share(); } - return Result.Create(default(SharedPtr), ResultStatus.NotFound); + return ErrorStatus.NotFound; } public void Dispose() diff --git a/Ghost.Graphics/D3D12/D3D12ResourceDatabase.cs b/Ghost.Graphics/D3D12/D3D12ResourceDatabase.cs index ebac75c..35fa312 100644 --- a/Ghost.Graphics/D3D12/D3D12ResourceDatabase.cs +++ b/Ghost.Graphics/D3D12/D3D12ResourceDatabase.cs @@ -178,17 +178,17 @@ internal class D3D12ResourceDatabase : IResourceDatabase return _resources.Contains(handle.id, handle.generation); } - public RefResult GetResourceRecord(Handle handle) + public RefResult GetResourceRecord(Handle handle) { ObjectDisposedException.ThrowIf(_disposed, this); ref var info = ref _resources.GetElementReferenceAt(handle.id, handle.generation, out var exist); if (!exist) { - return Result.CreateRef(ref Unsafe.NullRef(), ResultStatus.NotFound); + return ErrorStatus.NotFound; } - return Result.CreateRef(ref info, ResultStatus.Success); + return RefResult.Success(ref info); } public ref ResourceRecord GetResourceRecord(Handle handle, out bool exist) @@ -202,7 +202,7 @@ internal class D3D12ResourceDatabase : IResourceDatabase ObjectDisposedException.ThrowIf(_disposed, this); var r = GetResourceRecord(handle); - if (r.Status != ResultStatus.Success) + if (r.Error != ErrorStatus.None) { return null; } @@ -210,17 +210,17 @@ internal class D3D12ResourceDatabase : IResourceDatabase return r.Value.ResourcePtr; } - public Result GetResourceState(Handle handle) + public Result GetResourceState(Handle handle) { ObjectDisposedException.ThrowIf(_disposed, this); var r = GetResourceRecord(handle); - if (r.Status != ResultStatus.Success) + if (!r) { - return Result.Create(ResourceState.Common, r.Status); + return r.Error; } - return Result.Create(r.Value.state, ResultStatus.Success); + return r.Value.state; } public void SetResourceState(Handle handle, ResourceState state) @@ -236,31 +236,30 @@ internal class D3D12ResourceDatabase : IResourceDatabase info.state = state; } - public Result GetResourceDescription(Handle handle) + public Result GetResourceDescription(Handle handle) { ObjectDisposedException.ThrowIf(_disposed, this); var r = GetResourceRecord(handle); - - if (r.Status != ResultStatus.Success) + if (!r) { - return Result.Create(default(ResourceDesc), r.Status); + return r.Error; } - return Result.Create(r.Value.desc, ResultStatus.Success); + return r.Value.desc; } - public Result GetBindlessIndex(Handle handle) + public Result GetBindlessIndex(Handle handle) { ObjectDisposedException.ThrowIf(_disposed, this); ref var info = ref GetResourceRecord(handle, out var exist); if (!exist || !info.Allocated) { - return Result.Create(0u, ResultStatus.NotFound); + return ErrorStatus.NotFound; } - return Result.Create((uint)info.viewGroup.srv.value, ResultStatus.Success); + return (uint)info.viewGroup.srv.value; } public string? GetResourceName(Handle handle) diff --git a/Ghost.Graphics/RHI/Common.cs b/Ghost.Graphics/RHI/Common.cs index 50a82fb..f53e85a 100644 --- a/Ghost.Graphics/RHI/Common.cs +++ b/Ghost.Graphics/RHI/Common.cs @@ -98,7 +98,7 @@ internal struct GraphicsPipelineHash // Do we need to store blend state? // TODO: Variants - public GraphicsPipelineKey GetKey() + public readonly GraphicsPipelineKey GetKey() { Span data = stackalloc ulong[3 + D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT]; data[0] = Id.value; @@ -731,7 +731,7 @@ public struct CommandError get; set; } - public ResultStatus Status + public ErrorStatus Status { get; set; } @@ -988,4 +988,4 @@ public enum ComparisonFunction NotEqual, GreaterEqual, Always -} \ No newline at end of file +} diff --git a/Ghost.Graphics/RHI/IResourceDatabase.cs b/Ghost.Graphics/RHI/IResourceDatabase.cs index 1e4babf..420767b 100644 --- a/Ghost.Graphics/RHI/IResourceDatabase.cs +++ b/Ghost.Graphics/RHI/IResourceDatabase.cs @@ -39,7 +39,7 @@ public interface IResourceDatabase : IDisposable /// /// The handle that uniquely identifies the resource whose state is to be retrieved. /// A ResourceState Value representing the current state of the resource associated with the specified handle. - Result GetResourceState(Handle handle); + Result GetResourceState(Handle handle); /// /// Sets the state of the specified resource handle to the given Value. @@ -53,14 +53,14 @@ public interface IResourceDatabase : IDisposable /// /// A handle that identifies the GPU resource for which to obtain the description. Must reference a valid resource. /// A ResourceDesc structure containing details about the specified GPU resource. - Result GetResourceDescription(Handle handle); + Result GetResourceDescription(Handle handle); /// /// Retrieves the bindless index associated with the specified GPU resource handle. /// /// A handle to the GPU resource for which to obtain the bindless index. Must reference a valid, currently registered resource. /// The bindless index corresponding to the specified GPU resource handle. -1 if the resource does not support bindless access or is not found. - Result GetBindlessIndex(Handle handle); + Result GetBindlessIndex(Handle handle); /// /// Retrieves the name of the GPU resource associated with the specified handle. diff --git a/Ghost.Graphics/RenderPasses/MeshRenderPass.cs b/Ghost.Graphics/RenderPasses/MeshRenderPass.cs index ff918ea..eb42c77 100644 --- a/Ghost.Graphics/RenderPasses/MeshRenderPass.cs +++ b/Ghost.Graphics/RenderPasses/MeshRenderPass.cs @@ -27,9 +27,9 @@ internal class MeshRenderPass : IRenderPass public uint texture4; public uint tex_sampler; - private uint _padding1; - private uint _padding2; - private uint _padding3; + private readonly uint _padding1; + private readonly uint _padding2; + private readonly uint _padding3; } private Handle _mesh; @@ -91,7 +91,7 @@ internal class MeshRenderPass : IRenderPass { using var stream = File.OpenRead(_textureFiles[i]); using var imageData = ImageResult.FromStream(stream, ColorComponents.RGBA); - + var desc = new TextureDesc { Width = imageData.Width, @@ -121,14 +121,14 @@ internal class MeshRenderPass : IRenderPass var matProps = new ShaderProperties_MyShader_Standard { color = new float4(1.0f, 1.0f, 1.0f, 1.0f), - texture1 = ctx.ResourceDatabase.GetBindlessIndex(_textures[0].AsResource()).GetValueOrThrow(ResultStatus.Success), - texture2 = ctx.ResourceDatabase.GetBindlessIndex(_textures[1].AsResource()).GetValueOrThrow(ResultStatus.Success), - texture3 = ctx.ResourceDatabase.GetBindlessIndex(_textures[2].AsResource()).GetValueOrThrow(ResultStatus.Success), - texture4 = ctx.ResourceDatabase.GetBindlessIndex(_textures[3].AsResource()).GetValueOrThrow(ResultStatus.Success), + texture1 = ctx.ResourceDatabase.GetBindlessIndex(_textures[0].AsResource()).GetValueOrThrow(), + texture2 = ctx.ResourceDatabase.GetBindlessIndex(_textures[1].AsResource()).GetValueOrThrow(), + texture3 = ctx.ResourceDatabase.GetBindlessIndex(_textures[2].AsResource()).GetValueOrThrow(), + texture4 = ctx.ResourceDatabase.GetBindlessIndex(_textures[3].AsResource()).GetValueOrThrow(), tex_sampler = (uint)sampler.value, }; - Debug.Assert(matRef.SetPropertyCache(in matProps) == ResultStatus.Success); + Debug.Assert(matRef.SetPropertyCache(in matProps) == ErrorStatus.None); matRef.UploadData(ctx.DirectCommandBuffer); } diff --git a/GhostEngine.slnx b/GhostEngine.slnx index a2abc89..0044d31 100644 --- a/GhostEngine.slnx +++ b/GhostEngine.slnx @@ -23,8 +23,7 @@ - - +