Skip to content

Commit

Permalink
Fix copying depth stencil to depth stencil
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Jun 29, 2024
1 parent 81240a9 commit d49b341
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
44 changes: 44 additions & 0 deletions src/XenoAtom.Graphics.Tests/TextureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,50 @@ public unsafe void MapWrite_ThenMapRead_1D()
GD.Unmap(tex1D);
}


[Fact]
public unsafe void Copy_DepthStencil()
{
Texture depthTarget = RF.CreateTexture(
TextureDescription.Texture2D(64, 64, 1, 1, PixelFormat.D32_Float_S8_UInt, TextureUsage.DepthStencil));

Texture depthTarget1 = RF.CreateTexture(
TextureDescription.Texture2D(64, 64, 1, 1, PixelFormat.D32_Float_S8_UInt, TextureUsage.DepthStencil));

Framebuffer fb = RF.CreateFramebuffer(new FramebufferDescription(depthTarget));
{
using CommandList cl = RF.CreateCommandList();
cl.Begin();
cl.SetFramebuffer(fb);
cl.ClearDepthStencil(0.5f, 12);
cl.End();
GD.SubmitCommands(cl);
}

Texture copySrcFromDepth = RF.CreateTexture(TextureDescription.Texture2D(
64, 64, 1, 1, PixelFormat.R32_Float, TextureUsage.Staging));

{
using CommandList cl = RF.CreateCommandList();
cl.Begin();
cl.CopyTexture(depthTarget, 0, 0, 0, 0, 0, depthTarget1, 0, 0, 0, 0, 0, 64, 64, 1, 1);
cl.CopyTexture(depthTarget1, 0, 0, 0, 0, 0, copySrcFromDepth, 0, 0, 0, 0, 0, 64, 64, 1, 1);
cl.End();
GD.SubmitCommands(cl);
}
GD.WaitForIdle();

{
MappedResourceView<float> view = GD.Map<float>(copySrcFromDepth, MapMode.Read);
for (int i = 0; i < 64 * 64; i++)
{
Assert.Equal(0.5f, view[i]);
}
GD.Unmap(copySrcFromDepth);
}
}


[Fact]
public unsafe void Copy_1DTo2D()
{
Expand Down
22 changes: 20 additions & 2 deletions src/XenoAtom.Graphics/Vk/VkCommandList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -867,17 +867,35 @@ internal static void CopyTextureCore_VkCommandBuffer(

if (!sourceIsStaging && !destIsStaging)
{
var srcAspect = (srcVkTexture.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil
? (FormatHelpers.IsStencilFormat(srcVkTexture.Format)
? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT
: VK_IMAGE_ASPECT_DEPTH_BIT)
: VK_IMAGE_ASPECT_COLOR_BIT;

var dstAspect = (dstVkTexture.Usage & TextureUsage.DepthStencil) == TextureUsage.DepthStencil
? (FormatHelpers.IsStencilFormat(dstVkTexture.Format)
? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT
: VK_IMAGE_ASPECT_DEPTH_BIT)
: VK_IMAGE_ASPECT_COLOR_BIT;

if (srcAspect != dstAspect)
{
throw new InvalidOperationException($"Source texture with aspect `{srcAspect}` and destination texture with aspect `{dstAspect}` must have the same aspect.");
}

VkImageSubresourceLayers srcSubresource = new VkImageSubresourceLayers
{
aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
aspectMask = srcAspect,
layerCount = layerCount,
mipLevel = srcMipLevel,
baseArrayLayer = srcBaseArrayLayer
};


VkImageSubresourceLayers dstSubresource = new VkImageSubresourceLayers
{
aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
aspectMask = dstAspect,
layerCount = layerCount,
mipLevel = dstMipLevel,
baseArrayLayer = dstBaseArrayLayer
Expand Down

0 comments on commit d49b341

Please sign in to comment.