forked from Misaki/GhostEngine
Introduces a new Ghost.Shader.Concept project implementing a modern, data-oriented material and shader system with: - Global/local keyword bitsets (fast O(1) ops, 64 bytes) - Multi-pass shader program and per-pass render state overrides - Thread-safe, 16-byte aligned material property blocks - Material pooling to reduce GC pressure - Batch renderer for efficient PSO grouping and async variant warmup - Full demo (Program.cs) and extensive documentation (ARCHITECTURE.md, README.md, PROJECT_SUMMARY.md) - Minor integration: new enums, doc updates, and keyword handling in existing code No breaking changes to the existing engine; all new code is isolated. This serves as a reference implementation for high-performance, extensible material/shader architectures.
59 lines
1.4 KiB
C#
59 lines
1.4 KiB
C#
namespace Ghost.Shader.Concept;
|
|
|
|
/// <summary>
|
|
/// Material instance pool for efficient reuse and memory management.
|
|
/// Reduces GC pressure for frequently created/destroyed materials.
|
|
/// </summary>
|
|
public sealed class MaterialPool
|
|
{
|
|
private readonly Dictionary<ShaderProgram, Stack<Material>> _pools = new();
|
|
private readonly object _lock = new();
|
|
|
|
public Material Rent(ShaderProgram shaderProgram)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_pools.TryGetValue(shaderProgram, out var pool) && pool.Count > 0)
|
|
{
|
|
var material = pool.Pop();
|
|
return material;
|
|
}
|
|
}
|
|
|
|
return new Material(shaderProgram);
|
|
}
|
|
|
|
public void Return(Material material)
|
|
{
|
|
if (material == null)
|
|
return;
|
|
|
|
lock (_lock)
|
|
{
|
|
if (!_pools.TryGetValue(material.ShaderProgram, out var pool))
|
|
{
|
|
pool = new Stack<Material>();
|
|
_pools[material.ShaderProgram] = pool;
|
|
}
|
|
|
|
pool.Push(material);
|
|
}
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
foreach (var pool in _pools.Values)
|
|
{
|
|
while (pool.Count > 0)
|
|
{
|
|
var material = pool.Pop();
|
|
material.Dispose();
|
|
}
|
|
}
|
|
_pools.Clear();
|
|
}
|
|
}
|
|
}
|