Files
GhostEngine/Ghost.Shader.Concept/ShaderKeys.cs
Misaki f988c34b3d Add high-performance material/shader system (Ghost.Shader.Concept)
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.
2025-12-26 19:19:30 +09:00

72 lines
2.4 KiB
C#

using System.Runtime.InteropServices;
namespace Ghost.Shader.Concept;
/// <summary>
/// Unique identifier for a shader variant based on keyword combination.
/// Used as key for shader compilation cache.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public readonly struct ShaderVariantKey : IEquatable<ShaderVariantKey>
{
public readonly int ShaderProgramId;
public readonly ulong KeywordHash;
public ShaderVariantKey(int shaderProgramId, ulong keywordHash)
{
ShaderProgramId = shaderProgramId;
KeywordHash = keywordHash;
}
public bool Equals(ShaderVariantKey other) =>
ShaderProgramId == other.ShaderProgramId && KeywordHash == other.KeywordHash;
public override bool Equals(object? obj) => obj is ShaderVariantKey other && Equals(other);
public override int GetHashCode() => HashCode.Combine(ShaderProgramId, KeywordHash);
}
/// <summary>
/// Unique identifier for a graphics pipeline state object.
/// Combines shader variant, render state, and pass information.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public readonly struct GraphicsPipelineKey : IEquatable<GraphicsPipelineKey>
{
public readonly ShaderVariantKey VariantKey;
public readonly ulong RenderStateHash;
public readonly int PassId;
public GraphicsPipelineKey(ShaderVariantKey variantKey, ulong renderStateHash, int passId)
{
VariantKey = variantKey;
RenderStateHash = renderStateHash;
PassId = passId;
}
public bool Equals(GraphicsPipelineKey other) =>
VariantKey.Equals(other.VariantKey) &&
RenderStateHash == other.RenderStateHash &&
PassId == other.PassId;
public override bool Equals(object? obj) => obj is GraphicsPipelineKey other && Equals(other);
public override int GetHashCode() => HashCode.Combine(VariantKey, RenderStateHash, PassId);
}
/// <summary>
/// Mock interface for shader compiler (assumed to exist)
/// </summary>
public interface IShaderCompiler
{
/// <summary>Compiles a shader variant for the given keyword set</summary>
IntPtr CompileVariant(ShaderVariantKey key, in KeywordSet keywords);
}
/// <summary>
/// Mock interface for pipeline library (assumed to exist)
/// </summary>
public interface IPipelineLibrary
{
/// <summary>Gets or creates a PSO for the given pipeline key</summary>
IntPtr GetOrCreatePipeline(in GraphicsPipelineKey key);
}