Skip to content

Commit

Permalink
swapchain textures
Browse files Browse the repository at this point in the history
  • Loading branch information
aquagoose committed Sep 2, 2024
1 parent fbc079b commit aa61a5c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/grabs.Graphics.Vulkan/VkSwapchain.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using Silk.NET.Vulkan;
using Silk.NET.Vulkan.Extensions.KHR;
Expand All @@ -9,6 +10,10 @@ public sealed unsafe class VkSwapchain : Swapchain
{
private readonly Vk _vk;
private readonly VulkanDevice _device;

private readonly Format _format;
private uint _width;
private uint _height;

private readonly KhrSwapchain _khrSwapchain;

Expand All @@ -33,6 +38,10 @@ public VkSwapchain(Vk vk, VkDevice device, SurfaceKHR surface, in SwapchainDescr
_graphicsQueue = device.GraphicsQueue;
_presentQueue = device.PresentQueue;

_format = description.Format;
_width = description.Width;
_height = description.Height;

SwapchainCreateInfoKHR swapchainCreateInfo = new SwapchainCreateInfoKHR()
{
SType = StructureType.SwapchainCreateInfoKhr,
Expand Down Expand Up @@ -81,7 +90,13 @@ public VkSwapchain(Vk vk, VkDevice device, SurfaceKHR surface, in SwapchainDescr

public override Texture GetSwapchainTexture()
{
throw new System.NotImplementedException();
uint numImages;
_khrSwapchain.GetSwapchainImages(_device, Swapchain, &numImages, null);
Span<Image> images = stackalloc Image[(int) numImages];
fixed (Image* pImages = images)
_khrSwapchain.GetSwapchainImages(_device, Swapchain, &numImages, pImages);

return new VkSwapchainTexture(_vk, _device, images, _format, _width, _height);
}

public override void Resize(uint width, uint height)
Expand Down
64 changes: 64 additions & 0 deletions src/grabs.Graphics.Vulkan/VkSwapchainTexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using Silk.NET.Vulkan;
using static grabs.Graphics.Vulkan.VkResult;

namespace grabs.Graphics.Vulkan;

public sealed unsafe class VkSwapchainTexture : Texture
{
private readonly Vk _vk;
private readonly VulkanDevice _device;

public readonly Image[] Images;

public readonly ImageView[] Views;

public VkSwapchainTexture(Vk vk, VulkanDevice device, in ReadOnlySpan<Image> images, Format format, uint width, uint height)
: base(TextureDescription.Texture2D(width, height, 1, format, TextureUsage.None))
{
_vk = vk;
_device = device;

Images = images.ToArray();
Views = new ImageView[images.Length];

for (int i = 0; i < Views.Length; i++)
{
ImageViewCreateInfo viewInfo = new ImageViewCreateInfo()
{
SType = StructureType.ImageViewCreateInfo,

Image = images[i],
ViewType = ImageViewType.Type2D,
Format = VkUtils.FormatToVk(format),

Components = new ComponentMapping()
{
R = ComponentSwizzle.Identity,
G = ComponentSwizzle.Identity,
B = ComponentSwizzle.Identity,
A = ComponentSwizzle.Identity
},

SubresourceRange = new ImageSubresourceRange()
{
AspectMask = ImageAspectFlags.ColorBit,
BaseMipLevel = 0,
LevelCount = 1,
BaseArrayLayer = 0,
LayerCount = 1
}
};

CheckResult(_vk.CreateImageView(_device, &viewInfo, null, out Views[i]), "Create swapchain image view");
}
}

public override void Dispose()
{
for (int i = 0; i < Images.Length; i++)
_vk.DestroyImageView(_device, Views[i], null);

// Cannot destroy the actual images themselves as they are owned by the swapchain.
}
}
4 changes: 4 additions & 0 deletions tests/grabs.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Event = Silk.NET.SDL.Event;
using Instance = grabs.Graphics.Instance;
using Surface = grabs.Graphics.Surface;
using Texture = grabs.Graphics.Texture;

GrabsLog.LogMessage += (type, message) => Console.WriteLine($"[{type}] {message}");

Expand Down Expand Up @@ -122,6 +123,8 @@
Swapchain swapchain =
device.CreateSwapchain(new SwapchainDescription(width, height, bufferCount: 4, presentMode: PresentMode.VerticalSync));

Texture swapchainTexture = swapchain.GetSwapchainTexture();

CommandList commandList = device.CreateCommandList();

bool alive = true;
Expand Down Expand Up @@ -150,6 +153,7 @@
}

commandList.Dispose();
swapchainTexture.Dispose();
swapchain.Dispose();
device.Dispose();
surface.Dispose();
Expand Down

0 comments on commit aa61a5c

Please sign in to comment.