Refactor and enhance resource management and rendering

Updated multiple components to improve encapsulation, maintainability, and performance. Key changes include:

- Upgraded package dependencies in project files.
- Refactored `Mesh` and `RenderingContext` to use properties and added support for per-object constant buffers.
- Improved resource management in `D3D12CommandBuffer`, `D3D12CommandQueue`, and `D3D12ResourceAllocator` with better encapsulation and disposal handling.
- Added validation for constant buffer sizes in `D3D12PipelineLibrary`.
- Simplified `MeshBuilder` methods to accept allocators and removed hardcoded values.
- Enhanced debugging with `GPUResourceLeakException` and resource tracking updates.
- Updated shaders and rendering logic for testing, including hardcoded triangle rendering.
- Removed redundant base classes and interfaces for cleaner code structure.
This commit is contained in:
2025-11-26 01:48:24 +09:00
parent dfe786a2aa
commit 0720444c2c
40 changed files with 1008 additions and 903 deletions

View File

@@ -6,6 +6,7 @@ using Ghost.Graphics.RHI;
using Ghost.Graphics.Utilities;
using Ghost.SDL.Compiler;
using Misaki.HighPerformance.Image;
using Misaki.HighPerformance.Mathematics;
using Misaki.HighPerformance.Utilities;
using TerraFX.Interop.Windows;
@@ -57,42 +58,40 @@ internal unsafe class MeshRenderPass : IRenderPass
ctx.PipelineLibrary.CompilePSO(in psoDes, in compileResult.GetValueRef()).GetValueOrThrow();
}
MeshBuilder.CreateCube(0.75f, default, out var vertices, out var indices);
MeshBuilder.CreateCube(0.75f, default, Misaki.HighPerformance.LowLevel.Buffer.Allocator.Persistent, out var vertices, out var indices);
_mesh = ctx.CreateMesh(vertices, indices);
ctx.UpdateObjectData(_mesh, float4x4.identity);
ctx.UploadMesh(_mesh, true);
_shader = ctx.ResourceAllocator.CreateGraphicsShader(shaderDescriptor);
_material = ctx.ResourceAllocator.CreateMaterial(_shader);
var imageResults = new ImageResult[_textureFiles.Length];
//_textures = new Handle<Texture>[_textureFiles.Length];
//for (var i = 0; i < _textureFiles.Length; i++)
//{
// using var stream = File.OpenRead(_textureFiles[i]);
// using var imageData = ImageResult.FromStream(stream);
_textures = new Handle<Texture>[_textureFiles.Length];
for (var i = 0; i < _textureFiles.Length; i++)
{
using var stream = File.OpenRead(_textureFiles[i]);
using var imageData = ImageResult.FromStream(stream);
imageResults[i] = imageData;
// var desc = new TextureDesc
// {
// Width = imageData.Width,
// Height = imageData.Height,
// Dimension = TextureDimension.Texture2D,
// Format = TextureFormat.R8G8B8A8_UNorm,
// MipLevels = 1,
// Slice = 1,
// Usage = TextureUsage.ShaderResource,
// };
var desc = new TextureDesc
{
Width = imageData.Width,
Height = imageData.Height,
Dimension = TextureDimension.Texture2D,
Format = TextureFormat.R8G8B8A8_UNorm,
MipLevels = 1,
Slice = 1,
Usage = TextureUsage.ShaderResource,
};
_textures[i] = ctx.CreateTexture(ref desc);
ctx.UploadTexture(_textures[i], new Span<byte>(imageData.Data, (int)imageData.Size));
}
// _textures[i] = ctx.CreateTexture(ref desc);
// ctx.UploadTexture(_textures[i], new Span<byte>(imageData.Data, (int)imageData.Size));
//}
}
public void Execute(ref readonly RenderingContext ctx)
{
ctx.DispatchMesh(_mesh, _material, "Forward", 8);
ctx.DispatchMesh(_mesh, _material, "Forward", 3);
}
public void Cleanup(IResourceDatabase resourceDatabase)

View File

@@ -26,6 +26,7 @@ void MSMain(
out vertices PixelInput outVerts[3],
out indices uint3 outTris[1])
{
#if 0
// Fetch bindless buffers
ByteAddressBuffer vertexBuffer = ResourceDescriptorHeap[g_PerObjectData.vertexBuffer];
ByteAddressBuffer indexBuffer = ResourceDescriptorHeap[g_PerObjectData.indexBuffer];
@@ -45,8 +46,7 @@ void MSMain(
v.uv = asfloat(vertexBuffer.Load4(vertexOffset + 64));
SetMeshOutputCounts(3, 1);
v.position = mul(g_PerViewData.cameraMatrix, mul(g_PerObjectData.localToWorld, v.position));
//v.position = mul(g_PerViewData.cameraMatrix, mul(g_PerObjectData.localToWorld, v.position));
// Write vertex output
outVerts[vertexId].position = v.position;
@@ -58,15 +58,50 @@ void MSMain(
{
outTris[0] = uint3(0, 1, 2);
}
#else
// 1. Tell the hardware how much data to expect
SetMeshOutputCounts(3, 1);
// 2. Hardcoded Clip Space Positions (X, Y, Z, W)
// Visible range: X[-1, 1], Y[-1, 1], Z[0, 1]
// W must be 1.0
float4 positions[3] =
{
float4(0.0f, 0.5f, 0.5f, 1.0f), // Top
float4(0.5f, -0.5f, 0.5f, 1.0f), // Bottom Right
float4(-0.5f, -0.5f, 0.5f, 1.0f) // Bottom Left
};
float4 colors[3] =
{
float4(g_PerObjectData.vertexBuffer, 0.0f, 0.0f, 1.0f), // Red
float4(0.0f, g_PerObjectData.indexBuffer, 0.0f, 1.0f), // Green
float4(0.0f, 0.0f, 0.0f, 1.0f) // Blue
};
uint gtid = groupThreadID.x;
// 3. Write Vertex Data (Parallel)
outVerts[gtid].position = positions[gtid];
outVerts[gtid].color = colors[gtid];
// 4. Write Index Data (Only 1st thread needs to do this)
if (gtid == 0)
{
// Clockwise winding (Standard for DX12)
outTris[0] = uint3(0, 1, 2);
}
#endif
}
float4 PSMain(PixelInput input) : SV_TARGET
{
float4 color1 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture1, 0, input.uv.xy);
float4 color2 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture2, 0, input.uv.xy);
float4 color3 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture3, 0, input.uv.xy);
float4 color4 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture4, 0, input.uv.xy);
//float4 color1 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture1, 0, input.uv.xy);
//float4 color2 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture2, 0, input.uv.xy);
//float4 color3 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture3, 0, input.uv.xy);
//float4 color4 = SAMPLE_TEXTURE2D_BINDLESS(g_PerMaterialData.texture4, 0, input.uv.xy);
float4 blendedColor = (color1 + color2 + color3 + color4) * 0.25f;
return blendedColor * g_PerMaterialData.color;
//float4 blendedColor = (color1 + color2 + color3 + color4) * 0.25f;
return g_PerMaterialData.color + input.color;;
//return input.color;
}