forked from Misaki/GhostEngine
Refactor and enhance rendering pipeline
- Added new C# formatting rules in .editorconfig. - Introduced `IKeyType`, `Key<T>`, and `Ptr<T>` structs. - Updated `Result` and `Result<T>` for implicit conversions. - Added AOT compatibility to project files. - Introduced a `Camera` class and refactored namespaces. - Enhanced rendering with bindless support and pipeline state management. - Refactored `D3D12CommandBuffer` for new rendering features. - Improved `D3D12PipelineLibrary` with disk caching methods. - Added support for UAVs and raw buffers in `D3D12ResourceAllocator`. - Improved shader compilation and reflection in `D3D12ShaderCompiler`. - Refactored descriptor heap and swap chain initialization. - Added enums and structs for rendering configurations. - Expanded `ICommandBuffer` and `IPipelineLibrary` interfaces. - Updated `MeshRenderPass` to align with the new pipeline. - Consolidated namespaces and improved code maintainability.
This commit is contained in:
5
Ghost.Graphics/Core/Camera.cs
Normal file
5
Ghost.Graphics/Core/Camera.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
public class Camera
|
||||
{
|
||||
}
|
||||
114
Ghost.Graphics/Core/Color.cs
Normal file
114
Ghost.Graphics/Core/Color.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System.Drawing;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color with 4 bytes components.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 4)]
|
||||
public struct Color32 : IEquatable<Color32>
|
||||
{
|
||||
public byte r;
|
||||
public byte g;
|
||||
public byte b;
|
||||
public byte a;
|
||||
|
||||
public Color32(byte r, byte g, byte b, byte a)
|
||||
{
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
this.b = b;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public Color32(Color color)
|
||||
: this(color.R, color.G, color.B, color.A)
|
||||
{
|
||||
}
|
||||
|
||||
public Color32(Color128 color128)
|
||||
: this((byte)(color128.r * 255.0f), (byte)(color128.g * 255.0f), (byte)(color128.b * 255.0f), (byte)(color128.a * 255.0f))
|
||||
{
|
||||
}
|
||||
|
||||
public readonly bool Equals(Color32 other)
|
||||
{
|
||||
return r == other.r && g == other.g && b == other.b && a == other.a;
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object? obj)
|
||||
{
|
||||
return obj is Color32 color && Equals(color);
|
||||
}
|
||||
|
||||
public override readonly int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(r, g, b, a);
|
||||
}
|
||||
|
||||
public static bool operator ==(Color32 left, Color32 right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Color32 left, Color32 right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color with 16 bytes components.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
||||
public struct Color128 : IEquatable<Color128>
|
||||
{
|
||||
public float r;
|
||||
public float g;
|
||||
public float b;
|
||||
public float a;
|
||||
|
||||
public Color128(float r, float g, float b, float a)
|
||||
{
|
||||
this.r = r;
|
||||
this.g = g;
|
||||
this.b = b;
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public Color128(Color color)
|
||||
: this(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f)
|
||||
{
|
||||
}
|
||||
|
||||
public Color128(Color32 color32)
|
||||
: this(color32.r / 255.0f, color32.g / 255.0f, color32.b / 255.0f, color32.a / 255.0f)
|
||||
{
|
||||
}
|
||||
|
||||
public readonly bool Equals(Color128 other)
|
||||
{
|
||||
return r.Equals(other.r) && g.Equals(other.g) && b.Equals(other.b) && a.Equals(other.a);
|
||||
}
|
||||
|
||||
public override readonly bool Equals(object? obj)
|
||||
{
|
||||
return obj is Color128 color && Equals(color);
|
||||
}
|
||||
|
||||
public readonly override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(r, g, b, a);
|
||||
}
|
||||
|
||||
public static bool operator ==(Color128 left, Color128 right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
public static bool operator !=(Color128 left, Color128 right)
|
||||
{
|
||||
return !(left == right);
|
||||
}
|
||||
}
|
||||
217
Ghost.Graphics/Core/Material.cs
Normal file
217
Ghost.Graphics/Core/Material.cs
Normal file
@@ -0,0 +1,217 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
internal struct CBufferCache : IResourceReleasable
|
||||
{
|
||||
private UnsafeArray<byte> _cpuData;
|
||||
private Handle<GraphicsBuffer> _gpuResource;
|
||||
private uint _alignedSize;
|
||||
|
||||
public readonly UnsafeArray<byte> CpuData => _cpuData;
|
||||
public readonly Handle<GraphicsBuffer> GpuResource => _gpuResource;
|
||||
public readonly uint AlignedSize => _alignedSize;
|
||||
|
||||
public unsafe CBufferCache(Handle<GraphicsBuffer> buffer, uint bufferSize)
|
||||
{
|
||||
_alignedSize = (bufferSize + 255u) & ~255u;
|
||||
|
||||
_cpuData = new((int)AlignedSize, Allocator.Persistent);
|
||||
_gpuResource = buffer;
|
||||
}
|
||||
|
||||
public void ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
_cpuData.Dispose();
|
||||
|
||||
database.ReleaseResource(GpuResource.AsResource());
|
||||
_gpuResource = Handle<GraphicsBuffer>.Invalid;
|
||||
|
||||
_alignedSize = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public struct Material : IResourceReleasable, IHandleType
|
||||
{
|
||||
private Identifier<Shader> _shader;
|
||||
private UnsafeArray<CBufferCache> _materialPropertiesCache; // One per shader pass
|
||||
|
||||
public readonly Identifier<Shader> Shader => _shader;
|
||||
|
||||
internal ref CBufferCache GetPassCache(int passIndex)
|
||||
{
|
||||
return ref _materialPropertiesCache[passIndex];
|
||||
}
|
||||
|
||||
public void SetShader(Identifier<Shader> shaderId, IResourceAllocator allocator, IResourceDatabase database)
|
||||
{
|
||||
if (!shaderId.IsValid)
|
||||
{
|
||||
throw new ArgumentException("Shader ID is invalid.");
|
||||
}
|
||||
|
||||
_shader = shaderId;
|
||||
|
||||
var shader = database.GetShaderReference(shaderId);
|
||||
_materialPropertiesCache = new UnsafeArray<CBufferCache>(shader.PassCount, Allocator.Persistent);
|
||||
for (var i = 0; i < shader.PassCount; i++)
|
||||
{
|
||||
var pass = database.GetShaderPass(shader.GetPassKey(i));
|
||||
var cbufferInfo = pass.PassPropertyInfo;
|
||||
|
||||
var desc = new BufferDesc
|
||||
{
|
||||
Size = cbufferInfo.Size,
|
||||
Usage = BufferUsage.Constant,
|
||||
MemoryType = ResourceMemoryType.Default,
|
||||
};
|
||||
|
||||
var buffer = allocator.CreateBuffer(ref desc);
|
||||
_materialPropertiesCache[i] = new CBufferCache(buffer, cbufferInfo.Size);
|
||||
}
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
foreach (var cache in _materialPropertiesCache)
|
||||
{
|
||||
cache.ReleaseResource(database);
|
||||
}
|
||||
|
||||
_materialPropertiesCache.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public ref struct MaterialAccessor
|
||||
{
|
||||
private ref Material _materialData;
|
||||
private Shader _shader;
|
||||
|
||||
private readonly IResourceDatabase _resourceDatabase;
|
||||
|
||||
public MaterialAccessor(Handle<Material> material, IResourceDatabase resourceDatabase)
|
||||
{
|
||||
_resourceDatabase = resourceDatabase;
|
||||
|
||||
_materialData = ref resourceDatabase.GetMaterialReference(material);
|
||||
_shader = resourceDatabase.GetShaderReference(_materialData.Shader);
|
||||
}
|
||||
|
||||
private readonly unsafe void WriteToCache<T>(string propertyName, in T value)
|
||||
where T : unmanaged
|
||||
{
|
||||
foreach (var index in _shader.GetPropertyPassIndices(propertyName))
|
||||
{
|
||||
var passKey = _shader.GetPassKey(index);
|
||||
var pass = _resourceDatabase.GetShaderPass(passKey);
|
||||
var propertyInfo = pass.GetPropertyInfo(propertyName);
|
||||
|
||||
if (propertyInfo.Size != sizeof(T))
|
||||
{
|
||||
throw new ArgumentException($"Property '{propertyName}' has a size mismatch. Expected {propertyInfo.Size} bytes, but got {sizeof(T)} bytes.");
|
||||
}
|
||||
|
||||
ref var cache = ref _materialData.GetPassCache(index);
|
||||
Unsafe.WriteUnaligned(ref cache.CpuData[propertyInfo.ByteOffset], value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a float property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetFloat(string propertyName, in float value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a uint property in the material's constant buffer (useful for texture indices).
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetUInt(string propertyName, in uint value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Vector property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetVector(string propertyName, in float4 value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a Matrix property in the material's constant buffer.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the property to set.</param>
|
||||
/// <param name="value">The value to set for the property.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetMatrix(string propertyName, in float4x4 value)
|
||||
{
|
||||
WriteToCache(propertyName, in value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a texture index for a shader property (for bindless texture access)
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The name of the shader property (e.g., "_TextureIndex1")</param>
|
||||
/// <param name="texture">The bindless texture to reference</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetTextureBindless(string propertyName, Handle<Texture> texture)
|
||||
{
|
||||
var bindlessIndex = _resourceDatabase.GetBindlessIndex(texture.AsResource());
|
||||
if (bindlessIndex == -1)
|
||||
{
|
||||
throw new ArgumentException("The provided texture does not have a valid bindless index. Ensure the texture is created with bindless support.");
|
||||
}
|
||||
|
||||
SetUInt(propertyName, (uint)bindlessIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mesh buffer indices for bindless vertex and index buffer access
|
||||
/// </summary>
|
||||
/// <param name="mesh">The mesh whose buffer indices to set</param>
|
||||
/// <param name="vertexBufferIndexProperty">The name of the vertex buffer index property</param>
|
||||
/// <param name="indexBufferIndexProperty">The name of the index buffer index property</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void SetBufferBindless(string propertyName, Handle<GraphicsBuffer> buffer)
|
||||
{
|
||||
var bindlessIndex = _resourceDatabase.GetBindlessIndex(buffer.AsResource());
|
||||
if (bindlessIndex == -1)
|
||||
{
|
||||
throw new ArgumentException("The provided buffer does not have a valid bindless index. Ensure the buffer is created with bindless support.");
|
||||
}
|
||||
|
||||
SetUInt(propertyName, (uint)bindlessIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads all cached material data to the GPU using the specified command buffer.
|
||||
/// </summary>
|
||||
/// <param name="cmb">The command buffer used to perform the upload operations to the GPU. Cannot be null.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void UploadMaterialData(ICommandBuffer cmb)
|
||||
{
|
||||
for (var i = 0; i < _shader.PassCount; i++)
|
||||
{
|
||||
ref var cache = ref _materialData.GetPassCache(i);
|
||||
cmb.Upload(cache.GpuResource, cache.CpuData.AsSpan());
|
||||
}
|
||||
}
|
||||
}
|
||||
172
Ghost.Graphics/Core/Mesh.cs
Normal file
172
Ghost.Graphics/Core/Mesh.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using Misaki.HighPerformance.Mathematics.Geometry;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
public struct Mesh : IResourceReleasable, IHandleType
|
||||
{
|
||||
public UnsafeList<Vertex> vertices;
|
||||
public UnsafeList<uint> indices;
|
||||
public AABB boundingBox;
|
||||
public Handle<GraphicsBuffer> vertexBuffer;
|
||||
public Handle<GraphicsBuffer> indexBuffer;
|
||||
|
||||
public Mesh()
|
||||
{
|
||||
vertexBuffer = Handle<GraphicsBuffer>.Invalid;
|
||||
indexBuffer = Handle<GraphicsBuffer>.Invalid;
|
||||
}
|
||||
|
||||
internal Mesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices, Handle<GraphicsBuffer> vertexBuffer, Handle<GraphicsBuffer> indexBuffer)
|
||||
{
|
||||
this.vertices = new(vertices.Length, Allocator.Persistent);
|
||||
this.indices = new(indices.Length, Allocator.Persistent);
|
||||
this.vertices.CopyFrom(vertices);
|
||||
this.indices.CopyFrom(indices);
|
||||
this.vertexBuffer = vertexBuffer;
|
||||
this.indexBuffer = indexBuffer;
|
||||
|
||||
this.ComputeBounds();
|
||||
}
|
||||
|
||||
public void ReleaseCpuResources()
|
||||
{
|
||||
vertices.Dispose();
|
||||
indices.Dispose();
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
ReleaseCpuResources();
|
||||
|
||||
database.ReleaseResource(vertexBuffer.AsResource());
|
||||
database.ReleaseResource(indexBuffer.AsResource());
|
||||
}
|
||||
}
|
||||
|
||||
public static class MeshExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Computes the bounding box of the mesh based on its vertices.
|
||||
/// </summary>
|
||||
public static void ComputeBounds(ref this Mesh mesh)
|
||||
{
|
||||
if (mesh.vertices.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var min = new float3(float.MaxValue);
|
||||
var max = new float3(float.MinValue);
|
||||
foreach (var vertex in mesh.vertices)
|
||||
{
|
||||
var pos = vertex.position.xyz;
|
||||
min = math.min(min, pos);
|
||||
max = math.max(max, pos);
|
||||
}
|
||||
|
||||
mesh.boundingBox = new AABB(min, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Auto-compute smooth per-vertex normals.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this method before vertices and indices are valid.
|
||||
/// </remarks>
|
||||
public static void ComputeNormal(ref this Mesh mesh)
|
||||
{
|
||||
if (!mesh.vertices.IsCreated || mesh.vertices.Count < 3
|
||||
|| !mesh.indices.IsCreated || mesh.indices.Count < 3)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < mesh.indices.Count; i += 3)
|
||||
{
|
||||
var i0 = mesh.indices[i];
|
||||
var i1 = mesh.indices[i + 1];
|
||||
var i2 = mesh.indices[i + 2];
|
||||
|
||||
var v0 = mesh.vertices[i0];
|
||||
var v1 = mesh.vertices[i1];
|
||||
var v2 = mesh.vertices[i2];
|
||||
|
||||
var edge1 = v1.position - v0.position;
|
||||
var edge2 = v2.position - v0.position;
|
||||
var faceNormal = math.cross(edge1.xyz, edge2.xyz);
|
||||
|
||||
mesh.vertices[i0].normal.xyz += faceNormal;
|
||||
mesh.vertices[i1].normal.xyz += faceNormal;
|
||||
mesh.vertices[i2].normal.xyz += faceNormal;
|
||||
}
|
||||
|
||||
for (var i = 0; i < mesh.vertices.Count; i++)
|
||||
{
|
||||
mesh.vertices[i].normal = math.normalize(mesh.vertices[i].normal);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Auto-compute per-vertex tangents.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Call this method before vertices, normals, and UVs are valid.
|
||||
/// </remarks>
|
||||
public static void ComputeTangents(ref this Mesh mesh)
|
||||
{
|
||||
var bitangents = new float4[mesh.vertices.Count];
|
||||
|
||||
for (var i = 0; i < mesh.indices.Count; i += 3)
|
||||
{
|
||||
var i0 = mesh.indices[i];
|
||||
var i1 = mesh.indices[i + 1];
|
||||
var i2 = mesh.indices[i + 2];
|
||||
|
||||
var v0 = mesh.vertices[i0];
|
||||
var v1 = mesh.vertices[i1];
|
||||
var v2 = mesh.vertices[i2];
|
||||
|
||||
var uv0 = mesh.vertices[i0].uv;
|
||||
var uv1 = mesh.vertices[i1].uv;
|
||||
var uv2 = mesh.vertices[i2].uv;
|
||||
|
||||
var deltaPos1 = v1.position - v0.position;
|
||||
var deltaPos2 = v2.position - v0.position;
|
||||
var deltaUV1 = uv1 - uv0;
|
||||
var deltaUV2 = uv2 - uv0;
|
||||
|
||||
var r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
|
||||
var tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
|
||||
var bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r;
|
||||
|
||||
for (var j = 0; j < 3; j++)
|
||||
{
|
||||
var idx = mesh.indices[i + j];
|
||||
var t = mesh.vertices[idx].tangent;
|
||||
mesh.vertices[idx].tangent.xyz = t.xyz + tangent.xyz;
|
||||
|
||||
bitangents[idx] += bitangent;
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0; i < mesh.vertices.Count; i++)
|
||||
{
|
||||
var n = mesh.vertices[i].normal;
|
||||
var t = mesh.vertices[i].tangent;
|
||||
|
||||
var proj = n * math.dot(n, t);
|
||||
t = math.normalize(t - proj);
|
||||
|
||||
var b = bitangents[i];
|
||||
var w = math.dot(math.cross(n.xyz, t.xyz), b.xyz) < 0.0f ? -1.0f : 1.0f;
|
||||
|
||||
mesh.vertices[i].tangent = new float4(t.x, t.y, t.z, w);
|
||||
}
|
||||
}
|
||||
}
|
||||
186
Ghost.Graphics/Core/RenderingContext.cs
Normal file
186
Ghost.Graphics/Core/RenderingContext.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Core.Graphics;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.LowLevel.Utilities;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
public unsafe readonly ref struct RenderingContext
|
||||
{
|
||||
private readonly IRenderDevice _device;
|
||||
private readonly ICommandBuffer _directCmb;
|
||||
private readonly ICommandBuffer _copyCmb;
|
||||
private readonly ICommandBuffer _computeCmb;
|
||||
private readonly IResourceAllocator _resourceAllocator;
|
||||
private readonly IResourceDatabase _resourceDatabase;
|
||||
private readonly IPipelineLibrary _stateController;
|
||||
|
||||
internal RenderingContext(
|
||||
IRenderDevice device,
|
||||
ICommandBuffer directCmd,
|
||||
ICommandBuffer copyCmd,
|
||||
ICommandBuffer computeCmd,
|
||||
IResourceAllocator resourceAllocator,
|
||||
IResourceDatabase resourceDatabase,
|
||||
IPipelineLibrary stateController)
|
||||
{
|
||||
_device = device;
|
||||
_directCmb = directCmd;
|
||||
_copyCmb = copyCmd;
|
||||
_computeCmb = computeCmd;
|
||||
_resourceAllocator = resourceAllocator;
|
||||
_resourceDatabase = resourceDatabase;
|
||||
_stateController = stateController;
|
||||
}
|
||||
|
||||
public Handle<Mesh> CreateMesh(UnsafeList<Vertex> vertices, UnsafeList<uint> indices)
|
||||
{
|
||||
var mesh = _resourceAllocator.CreateMesh(vertices, indices);
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public Handle<Mesh> CreateMesh(ReadOnlySpan<Vertex> vertices, ReadOnlySpan<uint> indices)
|
||||
{
|
||||
var vertexList = new UnsafeList<Vertex>(vertices.Length, Allocator.Persistent);
|
||||
var indexList = new UnsafeList<uint>(indices.Length, Allocator.Persistent);
|
||||
|
||||
vertexList.CopyFrom(vertices);
|
||||
indexList.CopyFrom(indices);
|
||||
|
||||
return CreateMesh(vertexList, indexList);
|
||||
}
|
||||
|
||||
// TODO: Make one memory pool for upload.
|
||||
|
||||
/// <summary>
|
||||
/// Uploads the mesh data to the GPU.
|
||||
/// </summary>
|
||||
/// <param name="mesh">The handle point to the mesh buffer</param>
|
||||
/// <param name="markMeshStatic">Whether to mark the mesh as static. If it's true, the cpu buffer of the mesh will not be avaliable any more</param>
|
||||
public void UploadMesh(Handle<Mesh> mesh, bool markMeshStatic)
|
||||
{
|
||||
ref var meshData = ref _resourceDatabase.GetMeshReference(mesh);
|
||||
var vertexState = _resourceDatabase.GetResourceState(meshData.vertexBuffer.AsResource());
|
||||
var indexState = _resourceDatabase.GetResourceState(meshData.indexBuffer.AsResource());
|
||||
var needVertexTransition = vertexState != ResourceState.CopyDest;
|
||||
var needIndexTransition = indexState != ResourceState.CopyDest;
|
||||
|
||||
if (needVertexTransition)
|
||||
{
|
||||
_copyCmb.ResourceBarrier(meshData.vertexBuffer.AsResource(), vertexState, ResourceState.CopyDest);
|
||||
_resourceDatabase.SetResourceState(meshData.vertexBuffer.AsResource(), ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
if (needIndexTransition)
|
||||
{
|
||||
_copyCmb.ResourceBarrier(meshData.indexBuffer.AsResource(), indexState, ResourceState.CopyDest);
|
||||
_resourceDatabase.SetResourceState(meshData.indexBuffer.AsResource(), ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
_copyCmb.Upload(meshData.vertexBuffer, meshData.vertices.AsSpan());
|
||||
_copyCmb.Upload(meshData.indexBuffer, meshData.indices.AsSpan());
|
||||
|
||||
if (needVertexTransition)
|
||||
{
|
||||
_copyCmb.ResourceBarrier(meshData.vertexBuffer.AsResource(), ResourceState.CopyDest, vertexState);
|
||||
_resourceDatabase.SetResourceState(meshData.vertexBuffer.AsResource(), vertexState);
|
||||
}
|
||||
|
||||
if (needIndexTransition)
|
||||
{
|
||||
_resourceDatabase.SetResourceState(meshData.indexBuffer.AsResource(), indexState);
|
||||
_copyCmb.ResourceBarrier(meshData.indexBuffer.AsResource(), ResourceState.CopyDest, indexState);
|
||||
}
|
||||
|
||||
if (markMeshStatic)
|
||||
{
|
||||
meshData.ReleaseCpuResources();
|
||||
}
|
||||
}
|
||||
|
||||
public Handle<Texture> CreateTexture(ref readonly TextureDesc desc, bool tempResource = false)
|
||||
{
|
||||
return _resourceAllocator.CreateTexture(in desc, tempResource);
|
||||
}
|
||||
|
||||
public void UploadTexture(Handle<Texture> texture, ReadOnlySpan<byte> data)
|
||||
{
|
||||
var desc = _resourceDatabase.GetResourceDescription(texture.AsResource());
|
||||
desc.textureDescription.Format.GetSurfaceInfo((int)desc.textureDescription.Width, (int)desc.textureDescription.Height, out var rowPitch, out var slicePitch, out _);
|
||||
|
||||
var subresourceData = new SubResourceData
|
||||
{
|
||||
pData = data.GetUnsafePtr(),
|
||||
rowPitch = rowPitch,
|
||||
slicePitch = slicePitch
|
||||
};
|
||||
|
||||
var sateBefore = _resourceDatabase.GetResourceState(texture.AsResource());
|
||||
var needTransition = sateBefore != ResourceState.CopyDest;
|
||||
|
||||
if (needTransition)
|
||||
{
|
||||
_copyCmb.ResourceBarrier(texture.AsResource(), sateBefore, ResourceState.CopyDest);
|
||||
_resourceDatabase.SetResourceState(texture.AsResource(), ResourceState.CopyDest);
|
||||
}
|
||||
|
||||
_copyCmb.Upload(texture, subresourceData);
|
||||
|
||||
if (needTransition)
|
||||
{
|
||||
_copyCmb.ResourceBarrier(texture.AsResource(), ResourceState.CopyDest, sateBefore);
|
||||
_resourceDatabase.SetResourceState(texture.AsResource(), sateBefore);
|
||||
}
|
||||
}
|
||||
|
||||
public void RenderMesh(Handle<Mesh> mesh, Handle<Material> material, string passName)
|
||||
{
|
||||
//_cmd.DrawMesh(mesh, material);
|
||||
ref var meshRef = ref _resourceDatabase.GetMeshReference(mesh);
|
||||
ref var materialRef = ref _resourceDatabase.GetMaterialReference(material);
|
||||
var shader = _resourceDatabase.GetShaderReference(materialRef.Shader);
|
||||
|
||||
shader.TryGetPassKey(passName, out var passKey);
|
||||
var hash = new GraphicsPipelineHash
|
||||
{
|
||||
id = passKey,
|
||||
rtvCount = 1,
|
||||
dsvFormat = TextureFormat.Unknown,
|
||||
};
|
||||
|
||||
hash.rtvFormats[0] = TextureFormat.B8G8R8A8_UNorm;
|
||||
var pipelineKey = hash.GetKey();
|
||||
_directCmb.SetPipelineState(pipelineKey);
|
||||
|
||||
// FIX: Get valid root signature. In D3D12, we use fixed root signature layout for bindless rendering.
|
||||
// However, our code should not assume that blindly. Each pipeline should have contained root signature info even if there are fixed.
|
||||
// This ensures that future changes to root signature layout can be accommodated.
|
||||
|
||||
// for (int i = 0; i < 4; i++)
|
||||
{
|
||||
ref var cache = ref materialRef.GetPassCache((int)passKey.value);
|
||||
_directCmb.SetConstantBufferView(RootSignatureLayout.PER_MATERIAL_BUFFER_SLOT, cache.GpuResource);
|
||||
}
|
||||
|
||||
// NOTE: Since we are using true bindless resources, we only need to set the descriptor heaps, not individual tables.
|
||||
// TODO: Matbe handle the transitional bindless model?
|
||||
#if false
|
||||
var samplerGpuHandle = _descriptorAllocator.GetSamplerHeap()->GetGPUDescriptorHandleForHeapStart();
|
||||
_commandList.Get()->SetGraphicsRootDescriptorTable(rootParamIndex, samplerGpuHandle);
|
||||
#endif
|
||||
_directCmb.SetPrimitiveTopology(PrimitiveTopology.Triangle);
|
||||
|
||||
// Draw without vertex/index buffers - use instanced drawing
|
||||
// Each instance represents a triangle (3 vertices)
|
||||
var triangleCount = (uint)meshRef.indices.Count / 3;
|
||||
_directCmb.Draw(3, triangleCount, 0, 0);
|
||||
}
|
||||
|
||||
public void ExecuteCopyCommands()
|
||||
{
|
||||
_device.CopyQueue.Submit(_copyCmb);
|
||||
_device.CopyQueue.WaitIdle();
|
||||
}
|
||||
}
|
||||
30
Ghost.Graphics/Core/ResourceHandle.cs
Normal file
30
Ghost.Graphics/Core/ResourceHandle.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Ghost.Core;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
public readonly struct GPUResource : IHandleType;
|
||||
public readonly struct Texture : IHandleType;
|
||||
public readonly struct GraphicsBuffer : IHandleType;
|
||||
|
||||
public static class ResourceHandleExtensions
|
||||
{
|
||||
public static Handle<GPUResource> AsResource(this Handle<Texture> texture)
|
||||
{
|
||||
return new Handle<GPUResource>(texture.id, texture.generation);
|
||||
}
|
||||
|
||||
public static Handle<GPUResource> AsResource(this Handle<GraphicsBuffer> buffer)
|
||||
{
|
||||
return new Handle<GPUResource>(buffer.id, buffer.generation);
|
||||
}
|
||||
|
||||
internal static Handle<Texture> AsTexture(this Handle<GPUResource> resource)
|
||||
{
|
||||
return new Handle<Texture>(resource.id, resource.generation);
|
||||
}
|
||||
|
||||
internal static Handle<GraphicsBuffer> AsGraphicsBuffer(this Handle<GPUResource> resource)
|
||||
{
|
||||
return new Handle<GraphicsBuffer>(resource.id, resource.generation);
|
||||
}
|
||||
}
|
||||
172
Ghost.Graphics/Core/Shader.cs
Normal file
172
Ghost.Graphics/Core/Shader.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using Ghost.Core;
|
||||
using Ghost.Core.Graphics;
|
||||
using Ghost.Graphics.RHI;
|
||||
using Misaki.HighPerformance.LowLevel.Buffer;
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
public readonly struct TextureInfo
|
||||
{
|
||||
public uint RegisterSlot
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint RootParameterIndex
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct PropertyInfo
|
||||
{
|
||||
public uint CBufferIndex
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint ByteOffset
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint Size
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct CBufferInfo
|
||||
{
|
||||
public uint Size
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
|
||||
public uint RegisterSlot
|
||||
{
|
||||
get; init;
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe class ShaderPass : IResourceReleasable
|
||||
{
|
||||
// NOTE: This is for per pass cbuffer only. Global, per view, and per mesh cbuffers are fixed.
|
||||
private readonly Dictionary<string, int> _propertyLookup;
|
||||
private readonly UnsafeList<PropertyInfo> _properties;
|
||||
|
||||
internal CBufferInfo PassPropertyInfo
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ShaderPass(CBufferInfo info, UnsafeList<PropertyInfo> properties, Dictionary<string, int> propertyNameToIdMap)
|
||||
{
|
||||
PassPropertyInfo = info;
|
||||
_properties = properties;
|
||||
_propertyLookup = propertyNameToIdMap;
|
||||
}
|
||||
|
||||
public int GetPropertyId(string propertyName)
|
||||
{
|
||||
return _propertyLookup.TryGetValue(propertyName, out var id) ? id : -1;
|
||||
}
|
||||
|
||||
public PropertyInfo GetPropertyInfo(int id)
|
||||
{
|
||||
return _properties[id];
|
||||
}
|
||||
|
||||
public PropertyInfo GetPropertyInfo(string propertyName)
|
||||
{
|
||||
return _properties[GetPropertyId(propertyName)];
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
_properties.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A representation of a GPU shader, including all the passes it contains.
|
||||
/// </summary>
|
||||
public class Shader : IResourceReleasable, IIdentifierType
|
||||
{
|
||||
private UnsafeArray<ShaderPassKey> _passIDs;
|
||||
private readonly Dictionary<string, int> _passLookup; // pass name to index
|
||||
private readonly Dictionary<string, List<int>> _propertyLookup; // property name to pass index (property name to list of pass indices that contain the property)
|
||||
|
||||
public int PassCount => _passIDs.Count;
|
||||
|
||||
internal Shader(ShaderDescriptor descriptor)
|
||||
{
|
||||
_passIDs = new UnsafeArray<ShaderPassKey>(descriptor.passes.Count, Allocator.Persistent);
|
||||
_passLookup = new(descriptor.passes.Count);
|
||||
_propertyLookup = new(descriptor.passes.Count);
|
||||
|
||||
for (var i = 0; i < descriptor.passes.Count; i++)
|
||||
{
|
||||
var pass = descriptor.passes[i];
|
||||
var passKey = new ShaderPassKey(pass.Identifier);
|
||||
|
||||
_passIDs[i] = passKey;
|
||||
_passLookup[pass.Name] = i;
|
||||
|
||||
if (pass is FullPassDescriptor fullPass)
|
||||
{
|
||||
if (fullPass.properties == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var property in fullPass.properties)
|
||||
{
|
||||
ref var passIndices = ref CollectionsMarshal.GetValueRefOrAddDefault(_propertyLookup, property.name, out var exists);
|
||||
if (!exists || passIndices == null)
|
||||
{
|
||||
passIndices = new List<int>();
|
||||
}
|
||||
|
||||
passIndices.Add(i);
|
||||
}
|
||||
}
|
||||
// TODO: handle inherited passes
|
||||
}
|
||||
}
|
||||
|
||||
public ShaderPassKey GetPassKey(int index)
|
||||
{
|
||||
return _passIDs[index];
|
||||
}
|
||||
|
||||
public bool TryGetPassKey(string passName, out ShaderPassKey passID)
|
||||
{
|
||||
var index = _passLookup.GetValueOrDefault(passName, -1);
|
||||
if (index == -1)
|
||||
{
|
||||
passID = new(0);
|
||||
return false;
|
||||
}
|
||||
|
||||
passID = _passIDs[index];
|
||||
return true;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<int> GetPropertyPassIndices(string propertyName)
|
||||
{
|
||||
if (_propertyLookup.TryGetValue(propertyName, out var passIndices))
|
||||
{
|
||||
return passIndices;
|
||||
}
|
||||
|
||||
return Array.Empty<int>();
|
||||
}
|
||||
|
||||
void IResourceReleasable.ReleaseResource(IResourceDatabase database)
|
||||
{
|
||||
_passIDs.Dispose();
|
||||
}
|
||||
}
|
||||
55
Ghost.Graphics/Core/SwapChainPresenter.cs
Normal file
55
Ghost.Graphics/Core/SwapChainPresenter.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Ghost.Graphics.Contracts;
|
||||
using Ghost.Graphics.Core;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
internal readonly struct SwapChainPresenter
|
||||
{
|
||||
public enum TargetType
|
||||
{
|
||||
Composition,
|
||||
Hwnd
|
||||
}
|
||||
|
||||
public readonly TargetType Type
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public readonly ISwapChainPanelNative SwapChainPanelNative
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public readonly nint Hwnd
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public readonly uint Width
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public readonly uint Height
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public SwapChainPresenter(ISwapChainPanelNative swapChainPanelNative, uint width, uint height)
|
||||
{
|
||||
Type = TargetType.Composition;
|
||||
SwapChainPanelNative = swapChainPanelNative;
|
||||
Hwnd = nint.Zero;
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
|
||||
public SwapChainPresenter(nint hwnd, uint width, uint height)
|
||||
{
|
||||
Type = TargetType.Hwnd;
|
||||
Hwnd = hwnd;
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
}
|
||||
29
Ghost.Graphics/Core/Vertex.cs
Normal file
29
Ghost.Graphics/Core/Vertex.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Misaki.HighPerformance.LowLevel.Collections;
|
||||
using Misaki.HighPerformance.Mathematics;
|
||||
using System.Runtime.InteropServices;
|
||||
using TerraFX.Interop.DirectX;
|
||||
using Ghost.Graphics.Core;
|
||||
|
||||
namespace Ghost.Graphics.Core;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Vertex
|
||||
{
|
||||
public unsafe static class Semantic
|
||||
{
|
||||
public const DXGI_FORMAT ALIGNED_FORMAT = DXGI_FORMAT.DXGI_FORMAT_R32G32B32A32_FLOAT;
|
||||
public const int COUNT = 5;
|
||||
|
||||
public static readonly FixedText32 position = new("POSITION");
|
||||
public static readonly FixedText32 normal = new("NORMAL");
|
||||
public static readonly FixedText32 tangent = new("TANGENT");
|
||||
public static readonly FixedText32 uv = new("TEXCOORD");
|
||||
public static readonly FixedText32 color = new("COLOR");
|
||||
}
|
||||
|
||||
public float4 position;
|
||||
public float4 normal;
|
||||
public float4 tangent;
|
||||
public float4 uv;
|
||||
public Color128 color;
|
||||
}
|
||||
Reference in New Issue
Block a user