Files
Misaki.HighPerformance/Misaki.HighPerformance.Jobs/JobDataPool.cs
Misaki 8ce7fddd32 Relax job constraints, add ref type support, misc fixes
Relaxed generic constraints for job scheduling/execution to allow reference types (removed struct requirement). Updated IJob, IJobParallelFor, and IJobParallel extension methods to support both value and reference types, introducing RunRef for struct-specific overloads. Adjusted JobExecutor and JobScheduler to match new constraints. Bumped assembly version to 3.1.1. Added Value property to Wrapper<T> for ref access and inlined Get(). Changed GGXMipGenerationJob sample count to linear roughness. Removed unused usings in JobInfo.cs.
2026-04-27 12:54:29 +09:00

51 lines
2.4 KiB
C#

using Misaki.HighPerformance.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Jobs;
/// <summary>
/// This class manages pools of job data for different types. It allows allocating, retrieving, and freeing job data instances using unique IDs and generations to ensure safe access and reuse of resources.
/// </summary>
public static class JobDataPool<T>
{
private static readonly ConcurrentSlotMap<T> s_slots = new ConcurrentSlotMap<T>(8);
/// <summary>
/// Allocates a new instance of type T in the pool and returns its ID and generation.
/// </summary>
/// <typeparam name="T">The type of the data to allocate.</typeparam>
/// <param name="data">The data to allocate.</param>
/// <param name="generation">The generation of the allocated data.</param>
/// <returns>The ID of the allocated data.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Allocate(ref readonly T data, out int generation)
{
return s_slots.Add(data, out generation);
}
/// <summary>
/// Gets a reference to the data of type T associated with the given ID and generation. The 'exists' output parameter indicates whether the data exists in the pool.
/// </summary>
/// <typeparam name="T">The type of the data to retrieve.</typeparam>
/// <param name="id">The ID of the data to retrieve.</param>
/// <param name="generation">The generation of the data to retrieve.</param>
/// <param name="exists">A value indicating whether the data exists in the pool.</param>
/// <returns>A reference to the requested data. Undefined if 'exists' is false.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T GetReference(int id, int generation, out bool exists)
{
return ref s_slots.GetElementReferenceAt(id, generation, out exists);
}
/// <summary>
/// Frees the data of type T associated with the given ID and generation, making it available for future allocations. After calling this method, the ID and generation can be reused for new data.
/// </summary>
/// <typeparam name="T">The type of the data to free.</typeparam>
/// <param name="id">The ID of the data to free.</param>
/// <param name="generation">The generation of the data to free.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Free(int id, int generation)
{
s_slots.Remove(id, generation);
}
}