Skip to content
This repository was archived by the owner on Mar 19, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Seed.Engine.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ private static void Main()
gBufferPipeline, lightingPipeline, lightingDescriptorSet,
resources,
imageAvailableSemaphore, renderFinishedSemaphore, inFlightFence,
shadowMap, shadowPipeline,
shadowMap, shadowPipeline, shadowBuffer,
pointLightPipeline, pointLightDescriptorSet, sphereMeshId,
spotLightPipeline, spotLightDescriptorSet, coneMeshId));

Expand Down
1 change: 1 addition & 0 deletions src/Seed.Engine.Tests/Platform/Xr/BoundarySystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ public StubBuffer(BufferUsage usage, ulong size)

public BufferUsage Usage { get; }
public ulong SizeInBytes { get; }
public void Update(ReadOnlySpan<byte> data) { }
public void Dispose() { }
}

Expand Down
2 changes: 2 additions & 0 deletions src/Seed.Engine.Tests/Rendering/PipelineSelectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Xunit;

using Seed.Engine.Ecs;
Expand Down Expand Up @@ -364,6 +365,7 @@ public StubBuffer(BufferUsage usage, ulong size)

public BufferUsage Usage { get; }
public ulong SizeInBytes { get; }
public void Update(ReadOnlySpan<byte> data) { }
public void Dispose() { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ private static StereoDeferredRenderSystem CreateSystem(
null,
null,
null,
null,
null,
-1,
null,
null,
Expand Down Expand Up @@ -393,6 +395,7 @@ public StubBuffer(BufferUsage usage, ulong size)

public BufferUsage Usage { get; }
public ulong SizeInBytes { get; }
public void Update(ReadOnlySpan<byte> data) { }
public void Dispose() { }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ public StubBuffer(BufferUsage usage, ulong size)

public BufferUsage Usage { get; }
public ulong SizeInBytes { get; }
public void Update(ReadOnlySpan<byte> data) { }
public void Dispose() { }
}

Expand Down
6 changes: 6 additions & 0 deletions src/Seed.Engine/Graphics/IBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ public interface IBuffer : IDisposable
/// Gets the size in bytes of this buffer.
/// </summary>
ulong SizeInBytes { get; }

/// <summary>
/// Updates the buffer contents with new data.
/// The buffer must have been created with host-visible memory.
/// </summary>
void Update(ReadOnlySpan<byte> data);
}
23 changes: 23 additions & 0 deletions src/Seed.Engine/Graphics/Vulkan/VulkanBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ private Result CreateBuffer(ReadOnlySpan<byte> data)
}
}

/// <inheritdoc />
public void Update(ReadOnlySpan<byte> data)
{
unsafe
{
void* mappedData;
VkResult result = VulkanNative.vkMapMemory(
_vulkanDevice.Device, _memory, 0, SizeInBytes, 0, &mappedData);
if (result != VkResult.VK_SUCCESS)
{
return;
}

fixed (byte* pData = data)
{
System.Runtime.InteropServices.NativeMemory.Copy(
pData, mappedData, (nuint)data.Length);
}

VulkanNative.vkUnmapMemory(_vulkanDevice.Device, _memory);
}
}

/// <inheritdoc />
public void Dispose()
{
Expand Down
16 changes: 16 additions & 0 deletions src/Seed.Engine/Rendering/DeferredRenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public sealed class DeferredRenderSystem : ISystem

private readonly IRenderTarget? _shadowMap;
private readonly IPipeline? _shadowPipeline;
private readonly IBuffer? _shadowBuffer;

private readonly IPipeline? _pointLightPipeline;
private readonly IDescriptorSet? _pointLightDescriptorSet;
Expand Down Expand Up @@ -62,6 +63,7 @@ public DeferredRenderSystem(
IntPtr inFlightFence,
IRenderTarget? shadowMap = null,
IPipeline? shadowPipeline = null,
IBuffer? shadowBuffer = null,
IPipeline? pointLightPipeline = null,
IDescriptorSet? pointLightDescriptorSet = null,
int sphereMeshId = -1,
Expand All @@ -82,6 +84,7 @@ public DeferredRenderSystem(
_inFlightFence = inFlightFence;
_shadowMap = shadowMap;
_shadowPipeline = shadowPipeline;
_shadowBuffer = shadowBuffer;
_pointLightPipeline = pointLightPipeline;
_pointLightDescriptorSet = pointLightDescriptorSet;
_sphereMeshId = sphereMeshId;
Expand Down Expand Up @@ -154,6 +157,19 @@ public void Execute(World world)
Matrix4x4 lightSpaceVP = ComputeLightSpaceVP(world);
ExecuteShadowPass(world, lightSpaceVP);
_commandBuffer.PipelineBarrier();

if (_shadowBuffer != null)
{
var invertResult = viewProjection.Invert();
Matrix4x4 inverseVP = invertResult.IsSuccess
? invertResult.Value
: Matrix4x4.Identity;

Span<byte> shadowData = stackalloc byte[128];
MemoryMarshal.Write(shadowData, in lightSpaceVP);
MemoryMarshal.Write(shadowData.Slice(64), in inverseVP);
_shadowBuffer.Update(shadowData);
}
}

ExecuteGBufferPass(world, viewProjection);
Expand Down
30 changes: 29 additions & 1 deletion src/Seed.Engine/Rendering/StereoDeferredRenderSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public sealed class StereoDeferredRenderSystem : ISystem

private readonly IRenderTarget? _shadowMap;
private readonly IPipeline? _shadowPipeline;
private readonly IBuffer? _leftShadowBuffer;
private readonly IBuffer? _rightShadowBuffer;

private readonly IPipeline? _pointLightPipeline;
private readonly IDescriptorSet? _leftPointLightDescriptorSet;
Expand Down Expand Up @@ -76,6 +78,8 @@ public StereoDeferredRenderSystem(
float farPlane,
IRenderTarget? shadowMap,
IPipeline? shadowPipeline,
IBuffer? leftShadowBuffer,
IBuffer? rightShadowBuffer,
IPipeline? pointLightPipeline,
IDescriptorSet? leftPointLightDescriptorSet,
IDescriptorSet? rightPointLightDescriptorSet,
Expand Down Expand Up @@ -103,6 +107,8 @@ public StereoDeferredRenderSystem(
_farPlane = farPlane;
_shadowMap = shadowMap;
_shadowPipeline = shadowPipeline;
_leftShadowBuffer = leftShadowBuffer;
_rightShadowBuffer = rightShadowBuffer;
_pointLightPipeline = pointLightPipeline;
_leftPointLightDescriptorSet = leftPointLightDescriptorSet;
_rightPointLightDescriptorSet = rightPointLightDescriptorSet;
Expand Down Expand Up @@ -208,9 +214,10 @@ public void Execute(World world)
}

// Shadow pass is view-independent, execute once
Matrix4x4 lightSpaceVP = Matrix4x4.Identity;
if (_shadowMap != null && _shadowPipeline != null)
{
Matrix4x4 lightSpaceVP = ComputeLightSpaceVP(world);
lightSpaceVP = ComputeLightSpaceVP(world);
ExecuteShadowPass(world, lightSpaceVP);
_commandBuffer.PipelineBarrier();
}
Expand All @@ -219,6 +226,7 @@ public void Execute(World world)
var leftAcquire = _leftSwapChain.AcquireNextImage(IntPtr.Zero);
if (leftAcquire.IsSuccess)
{
UpdateShadowBuffer(_leftShadowBuffer, lightSpaceVP, leftVP);
ExecuteGBufferPass(world, leftVP, _leftGBuffer);
_commandBuffer.PipelineBarrier();
ExecuteLightingPass(
Expand All @@ -232,6 +240,7 @@ public void Execute(World world)
var rightAcquire = _rightSwapChain.AcquireNextImage(IntPtr.Zero);
if (rightAcquire.IsSuccess)
{
UpdateShadowBuffer(_rightShadowBuffer, lightSpaceVP, rightVP);
ExecuteGBufferPass(world, rightVP, _rightGBuffer);
_commandBuffer.PipelineBarrier();
ExecuteLightingPass(
Expand Down Expand Up @@ -598,4 +607,23 @@ private Matrix4x4 ComputeLightSpaceVP(World world)

return Matrix4x4.Identity;
}

private static void UpdateShadowBuffer(
IBuffer? shadowBuffer, Matrix4x4 lightSpaceVP, Matrix4x4 viewProjection)
{
if (shadowBuffer == null)
{
return;
}

var invertResult = viewProjection.Invert();
Matrix4x4 inverseVP = invertResult.IsSuccess
? invertResult.Value
: Matrix4x4.Identity;

Span<byte> data = stackalloc byte[128];
MemoryMarshal.Write(data, in lightSpaceVP);
MemoryMarshal.Write(data.Slice(64), in inverseVP);
shadowBuffer.Update(data);
}
}