Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove per-frame texture allocation in Metal swapchain #58

Merged
merged 2 commits into from
Aug 19, 2024
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
9 changes: 5 additions & 4 deletions src/Veldrid/MTL/MTLSwapchainFramebuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ internal class MtlSwapchainFramebuffer : MtlFramebuffer
private readonly PixelFormat colorFormat;

private readonly PixelFormat? depthFormat;
private MtlTexture colorTexture;
private readonly MtlSwapchainTexture colorTexture = new MtlSwapchainTexture();
private MtlTexture depthTexture;

private FramebufferAttachment[] colorTargets;
private readonly FramebufferAttachment[] colorTargets;
private FramebufferAttachment? depthTarget;

public MtlSwapchainFramebuffer(
Expand All @@ -44,6 +44,8 @@ public MtlSwapchainFramebuffer(

var colorAttachment = new OutputAttachmentDescription(colorFormat);

colorTargets = new[] { new FramebufferAttachment(colorTexture, 0) };

OutputDescription = new OutputDescription(depthAttachment, colorAttachment);
}

Expand All @@ -59,8 +61,7 @@ public override void Dispose()

public void UpdateTextures(CAMetalDrawable drawable, CGSize size)
{
colorTexture = new MtlTexture(drawable, size, colorFormat);
colorTargets = new[] { new FramebufferAttachment(colorTexture, 0) };
colorTexture.SetDrawable(drawable, size, colorFormat);

if (depthFormat.HasValue && (size.width != depthTexture?.Width || size.height != depthTexture?.Height))
recreateDepthTexture((uint)size.width, (uint)size.height);
Expand Down
45 changes: 45 additions & 0 deletions src/Veldrid/MTL/MTLSwapchainTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using Veldrid.MetalBindings;

namespace Veldrid.MTL
{
internal class MtlSwapchainTexture : MtlTexture
{
public override MTLTexture DeviceTexture => deviceTexture;

public override uint Width => width;

public override uint Height => height;

public override uint Depth => 1;

public override uint ArrayLayers => 1;

public override uint MipLevels => 1;

public override TextureUsage Usage => TextureUsage.RenderTarget;

public override TextureType Type => TextureType.Texture2D;

public override TextureSampleCount SampleCount => TextureSampleCount.Count1;

public override MTLPixelFormat MtlPixelFormat => mtlPixelFormat;

public override MTLTextureType MtlTextureType => MTLTextureType.Type2D;

private MTLTexture deviceTexture;
private uint width;
private uint height;
private MTLPixelFormat mtlPixelFormat;

public void SetDrawable(CAMetalDrawable drawable, CGSize size, PixelFormat format)
{
deviceTexture = drawable.texture;
width = (uint)size.width;
height = (uint)size.height;
mtlPixelFormat = MtlFormats.VdToMtlPixelFormat(Format, false);
}
}
}
21 changes: 4 additions & 17 deletions src/Veldrid/MTL/MTLTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class MtlTexture : Texture
/// <summary>
/// The native MTLTexture object. This property is only valid for non-staging Textures.
/// </summary>
public MTLTexture DeviceTexture { get; }
public virtual MTLTexture DeviceTexture { get; }

/// <summary>
/// The staging MTLBuffer object. This property is only valid for staging Textures.
Expand All @@ -33,8 +33,8 @@ internal class MtlTexture : Texture

public override TextureSampleCount SampleCount { get; }
public override bool IsDisposed => disposed;
public MTLPixelFormat MtlPixelFormat { get; }
public MTLTextureType MtlTextureType { get; }
public virtual MTLPixelFormat MtlPixelFormat { get; }
public virtual MTLTextureType MtlTextureType { get; }
public MTLStorageMode MtlStorageMode { get; }

public unsafe void* StagingBufferPointer { get; private set; }
Expand Down Expand Up @@ -128,21 +128,8 @@ public MtlTexture(ulong nativeTexture, ref TextureDescription description)
(Usage & TextureUsage.Cubemap) != 0);
}

public MtlTexture(CAMetalDrawable drawable, CGSize size, PixelFormat format)
protected MtlTexture()
{
DeviceTexture = drawable.texture;
Width = (uint)size.width;
Height = (uint)size.height;
Depth = 1;
ArrayLayers = 1;
MipLevels = 1;
Format = format;
Usage = TextureUsage.RenderTarget;
Type = TextureType.Texture2D;
SampleCount = TextureSampleCount.Count1;

MtlPixelFormat = MtlFormats.VdToMtlPixelFormat(Format, false);
MtlTextureType = MTLTextureType.Type2D;
}

internal uint GetSubresourceSize(uint mipLevel, uint arrayLayer)
Expand Down
Loading