aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Ava/Ui/Backend/Vulkan/VulkanSurface.cs
blob: 2452cdcd73de72c74696d2b74ce42ccce5d1d2bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using Avalonia;
using Ryujinx.Ava.Ui.Vulkan.Surfaces;
using Silk.NET.Vulkan;
using Silk.NET.Vulkan.Extensions.KHR;

namespace Ryujinx.Ava.Ui.Vulkan
{
    public class VulkanSurface : IDisposable
    {
        private readonly VulkanInstance _instance;
        private readonly IVulkanPlatformSurface _vulkanPlatformSurface;

        private VulkanSurface(IVulkanPlatformSurface vulkanPlatformSurface, VulkanInstance instance)
        {
            _vulkanPlatformSurface = vulkanPlatformSurface;
            _instance = instance;
            ApiHandle = vulkanPlatformSurface.CreateSurface(instance);
        }

        internal SurfaceKHR ApiHandle { get; }

        internal static KhrSurface SurfaceExtension { get; private set; }

        internal PixelSize SurfaceSize => _vulkanPlatformSurface.SurfaceSize;

        public void Dispose()
        {
            SurfaceExtension.DestroySurface(_instance.InternalHandle, ApiHandle, Span<AllocationCallbacks>.Empty);
            _vulkanPlatformSurface.Dispose();
        }

        internal static VulkanSurface CreateSurface(VulkanInstance instance, IVulkanPlatformSurface vulkanPlatformSurface)
        {
            if (SurfaceExtension == null)
            {
                instance.Api.TryGetInstanceExtension(instance.InternalHandle, out KhrSurface extension);

                SurfaceExtension = extension;
            }

            return new VulkanSurface(vulkanPlatformSurface, instance);
        }

        internal bool CanSurfacePresent(VulkanPhysicalDevice physicalDevice)
        {
            SurfaceExtension.GetPhysicalDeviceSurfaceSupport(physicalDevice.InternalHandle, physicalDevice.QueueFamilyIndex, ApiHandle, out var isSupported);

            return isSupported;
        }

        internal SurfaceFormatKHR GetSurfaceFormat(VulkanPhysicalDevice physicalDevice)
        {
            Span<uint> surfaceFormatsCount = stackalloc uint[1];
            SurfaceExtension.GetPhysicalDeviceSurfaceFormats(physicalDevice.InternalHandle, ApiHandle, surfaceFormatsCount, Span<SurfaceFormatKHR>.Empty);
            Span<SurfaceFormatKHR> surfaceFormats = stackalloc SurfaceFormatKHR[(int)surfaceFormatsCount[0]];
            SurfaceExtension.GetPhysicalDeviceSurfaceFormats(physicalDevice.InternalHandle, ApiHandle, surfaceFormatsCount, surfaceFormats);

            if (surfaceFormats.Length == 1 && surfaceFormats[0].Format == Format.Undefined)
            {
                return new SurfaceFormatKHR(Format.B8G8R8A8Unorm, ColorSpaceKHR.ColorspaceSrgbNonlinearKhr);
            }

            foreach (var format in surfaceFormats)
            {
                if (format.Format == Format.B8G8R8A8Unorm && format.ColorSpace == ColorSpaceKHR.ColorspaceSrgbNonlinearKhr)
                {
                    return format;
                }
            }

            return surfaceFormats[0];
        }
    }
}