Beispiel #1
0
        static VkSwapchainKHR CreateSwapchain(VkSwapchainKHR oldSwapchain, VkInstance instance, VkDevice device, VkPhysicalDevice physicalDevice,
                                              VkSurfaceKHR surface, uint queueFamilyIndex)
        {
            vkDestroySwapchainKHR(device, oldSwapchain, null);//Does nothing if oldswapchain is null

            VkSurfaceCapabilitiesKHR capabilities = new VkSurfaceCapabilitiesKHR();

            Assert(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities));

            GetSwapchainFormat(physicalDevice, surface);
            VkSwapchainKHR           swapchain   = VkSwapchainKHR.Null;
            VkSwapchainCreateInfoKHR pCreateInfo = VkSwapchainCreateInfoKHR.New();

            pCreateInfo.surface               = surface;
            pCreateInfo.minImageCount         = capabilities.minImageCount;
            pCreateInfo.imageFormat           = surfaceFormat.format;//SHORTCUT: Some devices might not support
            pCreateInfo.imageColorSpace       = surfaceFormat.colorSpace;
            pCreateInfo.imageExtent           = capabilities.currentExtent;
            pCreateInfo.imageArrayLayers      = 1;
            pCreateInfo.imageUsage            = VkImageUsageFlags.ColorAttachment;
            pCreateInfo.queueFamilyIndexCount = 1;
            pCreateInfo.pQueueFamilyIndices   = &queueFamilyIndex;
            pCreateInfo.preTransform          = VkSurfaceTransformFlagsKHR.IdentityKHR;
            pCreateInfo.compositeAlpha        = VkCompositeAlphaFlagsKHR.OpaqueKHR;
            pCreateInfo.presentMode           = VkPresentModeKHR.MailboxKHR;

            Assert(vkCreateSwapchainKHR(device, &pCreateInfo, null, &swapchain));

            return(swapchain);
        }
Beispiel #2
0
        public bool GetPresentIsSupported(uint qFamilyIndex, VkSurfaceKHR surf)
        {
            VkBool32 isSupported = false;

            vkGetPhysicalDeviceSurfaceSupportKHR(phy, qFamilyIndex, surf, out isSupported);
            return(isSupported);
        }
Beispiel #3
0
        static (uint graphicsFamily, uint presentFamily) FindQueueFamilies(
            VkPhysicalDevice device, VkSurfaceKHR surface)
        {
            var queueFamilies = vkGetPhysicalDeviceQueueFamilyProperties(device);

            uint graphicsFamily = QueueFamilyIgnored;
            uint presentFamily  = QueueFamilyIgnored;
            uint i = 0;

            foreach (var queueFamily in queueFamilies)
            {
                if ((queueFamily.queueFlags & VkQueueFlags.Graphics) != VkQueueFlags.None)
                {
                    graphicsFamily = i;
                }

                vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, out var presentSupport);

                if (presentSupport)
                {
                    presentFamily = i;
                }

                if (graphicsFamily != QueueFamilyIgnored &&
                    presentFamily != QueueFamilyIgnored)
                {
                    break;
                }

                i++;
            }

            return(graphicsFamily, presentFamily);
        }
Beispiel #4
0
        static void GetSwapchainFormat(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface)
        {
            uint formatCount = 0;

            vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, null);
            VkSurfaceFormatKHR[] formats = new VkSurfaceFormatKHR[formatCount];

            fixed(VkSurfaceFormatKHR *ptr = &formats[0])
            Assert(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &formatCount, ptr));



            for (int i = 0; i < formatCount; i++)
            {
                Console.WriteLine($"Format {formats[i].format} {formats[i].colorSpace} is available.");
                for (int k = 0; k < UNORM_FORMATS.Length; k++)
                {
                    if (formats[i].format == UNORM_FORMATS[k])
                    {
                        surfaceFormat = formats[i];
                        return;
                    }
                }
            }

            surfaceFormat = formats[0];//Fallback
        }
Beispiel #5
0
        public unsafe SwapChainSupportDetails(VkPhysicalDevice device, VkSurfaceKHR surface)
        {
            VkSurfaceCapabilitiesKHR capabilitiesKHR;

            VulkanNative.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &capabilitiesKHR);
            capabilities = capabilitiesKHR;
            formats      = null;
            presentModes = null;

            uint formatCount;

            VulkanNative.vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, null);
            if (formatCount > 0)
            {
                formats = new VkSurfaceFormatKHR[formatCount];
                fixed(VkSurfaceFormatKHR *formatsPtr = formats)
                {
                    VulkanNative.vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, formatsPtr);
                }
            }

            uint presentModeCount;

            VulkanNative.vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, null);
            if (presentModeCount > 0)
            {
                presentModes = new VkPresentModeKHR[presentModeCount];
                fixed(VkPresentModeKHR *presentsPtr = presentModes)
                {
                    VulkanNative.vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, presentsPtr);
                }
            }
        }
Beispiel #6
0
        public unsafe QueueFamilyIndices(VkPhysicalDevice device, VkSurfaceKHR surface)
        {
            int graphicsIndex = -1;
            int presentIndex  = -1;

            uint queueFamilyCount = 0;

            VulkanNative.vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, null);
            VkQueueFamilyProperties *queueFamilies = stackalloc VkQueueFamilyProperties[(int)queueFamilyCount];

            VulkanNative.vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies);

            for (int i = 0; i < queueFamilyCount; i++)
            {
                var q = queueFamilies[i];

                if (q.queueCount > 0 && (q.queueFlags & VkQueueFlagBits.VK_QUEUE_GRAPHICS_BIT) != 0)
                {
                    graphicsIndex = i;
                }

                VkBool32 presentSupported = false;
                VkResult result           = VulkanNative.vkGetPhysicalDeviceSurfaceSupportKHR(device, (uint)i, surface, &presentSupported);
                Helpers.CheckErrors(result);
                if (presentIndex < 0 && q.queueCount > 0 && presentSupported)
                {
                    presentIndex = i;
                }
            }

            GraphicsFamily = graphicsIndex;
            PresentFamily  = presentIndex;
        }
        private unsafe void CreateSurface()
        {
            // Check for Window Handle parameter
            if (Description.DeviceWindowHandle == null)
            {
                throw new ArgumentException("DeviceWindowHandle cannot be null");
            }
            // Create surface
#if STRIDE_UI_SDL
            var control = Description.DeviceWindowHandle.NativeWindow as SDL.Window;
            SDL2.SDL.SDL_Vulkan_CreateSurface(control.SdlHandle, GraphicsDevice.NativeInstance.Handle, out var surfacePtr);
            surface = new VkSurfaceKHR(surfacePtr);
#elif STRIDE_PLATFORM_WINDOWS
            var controlHandle = Description.DeviceWindowHandle.Handle;
            if (controlHandle == IntPtr.Zero)
            {
                throw new NotSupportedException($"Form of type [{Description.DeviceWindowHandle.GetType().Name}] is not supported. Only System.Windows.Control are supported");
            }

            var surfaceCreateInfo = new VkWin32SurfaceCreateInfoKHR
            {
                sType          = VkStructureType.Win32SurfaceCreateInfoKHR,
                instanceHandle = Process.GetCurrentProcess().Handle,
                windowHandle   = controlHandle,
            };
            vkCreateWin32SurfaceKHR(GraphicsDevice.NativeInstance, &surfaceCreateInfo, null, out surface);
#elif STRIDE_PLATFORM_ANDROID
            throw new NotImplementedException();
#elif STRIDE_PLATFORM_LINUX
            throw new NotSupportedException("Only SDL is supported for the time being on Linux");
#else
            throw new NotSupportedException();
#endif
        }
Beispiel #8
0
        protected VkSwapchainKHR CreateSwapchain(VkDevice device, VkSurfaceKHR surface,
                                                 VkSurfaceFormatKHR surfaceFormat, VkSurfaceCapabilitiesKHR surfaceCapabilities)
        {
            var compositeAlpha = surfaceCapabilities.supportedCompositeAlpha.HasFlag(
                VkCompositeAlphaFlagBitsKHR.InheritKHR)
                ? VkCompositeAlphaFlagBitsKHR.InheritKHR
                : VkCompositeAlphaFlagBitsKHR.OpaqueKHR;
            UInt32 index = 0;
            var    info  = new VkSwapchainCreateInfoKHR {
                sType = VkStructureType.SwapchainCreateInfoKHR
            };

            info.surface            = surface;
            info.minImageCount      = surfaceCapabilities.minImageCount;
            info.imageFormat        = surfaceFormat.format;
            info.imageColorSpace    = surfaceFormat.colorSpace;
            info.imageExtent        = surfaceCapabilities.currentExtent;
            info.imageUsage         = VkImageUsageFlagBits.ColorAttachment;
            info.preTransform       = VkSurfaceTransformFlagBitsKHR.IdentityKHR;
            info.imageArrayLayers   = 1;
            info.imageSharingMode   = VkSharingMode.Exclusive;
            info.queueFamilyIndices = index;
            info.presentMode        = VkPresentModeKHR.FifoKHR;
            info.compositeAlpha     = compositeAlpha;
            //return device.CreateSwapchainKHR(ref info, null);
            VkSwapchainKHR swapchain;

            vkAPI.vkCreateSwapchainKHR(device, &info, null, &swapchain).Check();

            return(swapchain);
        }
Beispiel #9
0
        public VkSurfaceCapabilitiesKHR GetSurfaceCapabilities(VkSurfaceKHR surf)
        {
            VkSurfaceCapabilitiesKHR caps;

            vkGetPhysicalDeviceSurfaceCapabilitiesKHR(phy, surf, out caps);
            return(caps);
        }
 public VkSwapchainFramebuffer(VkGraphicsDevice gd, VkSurfaceKHR surface, uint width, uint height)
     : base()
 {
     _gd      = gd;
     _surface = surface;
     CreateSwapchain(width, height);
     OutputDescription = OutputDescription.CreateFromFramebuffer(this);
 }
Beispiel #11
0
        static VkSurfaceKHR CreateSurface(VkInstance instance, NativeWindow window)
        {
            Assert((VkResult)GLFW.Vulkan.CreateWindowSurface(instance.Handle, window.Handle, IntPtr.Zero, out var ptr));

            VkSurfaceKHR surface = new VkSurfaceKHR((ulong)ptr.ToInt64());

            return(surface);
        }
        /// <inheritdoc/>
        protected internal override unsafe void OnDestroyed()
        {
            DestroySwapchain();

            vkDestroySurfaceKHR(GraphicsDevice.NativeInstance, surface, null);
            surface = VkSurfaceKHR.Null;

            base.OnDestroyed();
        }
Beispiel #13
0
        public PresentQueue(Device _dev, VkQueueFlags requestedFlags, VkSurfaceKHR _surface, float _priority = 0.0f)
        {
            dev      = _dev;
            priority = _priority;
            Surface  = _surface;

            qFamIndex = searchQFamily(requestedFlags);
            dev.queues.Add(this);
        }
Beispiel #14
0
        public VkSurfaceFormatKHR [] GetSurfaceFormats(VkSurfaceKHR surf)
        {
            vkGetPhysicalDeviceSurfaceFormatsKHR(phy, surf, out uint count, IntPtr.Zero);
            VkSurfaceFormatKHR [] formats = new VkSurfaceFormatKHR [count];

            vkGetPhysicalDeviceSurfaceFormatsKHR(phy, surf, out count, formats.Pin());
            formats.Unpin();

            return(formats);
        }
Beispiel #15
0
        private static VkDevice CreateDevice(VkInstance instance, out VkPhysicalDevice physicalDevice,
                                             VkSurfaceKHR surface, out uint queueFamilyIndex)
        {
            queueFamilyIndex = 0;//SHORTCUT computed from queue properties
            physicalDevice   = PickPhysicalDevice(instance, surface, out queueFamilyIndex);

            List <GCHandle> handles            = new List <GCHandle>();
            List <string>   requiredExtensions = new List <string>();

            requiredExtensions.Add("VK_KHR_swapchain");
            string[] extensionNames        = requiredExtensions.ToArray();
            byte[][] pExtensionNames       = new byte[extensionNames.Length][];
            byte *[] ppExtensionNamesArray = new byte *[extensionNames.Length];

            for (int i = 0; i < pExtensionNames.Length; i++)
            {
                pExtensionNames[i] = Encoding.UTF8.GetBytes(extensionNames[i] + char.MinValue);
                GCHandle handle = GCHandle.Alloc(pExtensionNames[i]);
                handles.Add(handle);
                fixed(byte *p = &(((byte[])handle.Target)[0]))
                {
                    ppExtensionNamesArray[i] = p;
                }
            }
            VkDevice device;

            fixed(byte **extensions = &ppExtensionNamesArray[0])
            {
                float[] pQueuePriorities = new float[] { 1.0f };
                VkDeviceQueueCreateInfo deviceQueueCreateInfo = VkDeviceQueueCreateInfo.New();

                deviceQueueCreateInfo.queueFamilyIndex = queueFamilyIndex;
                deviceQueueCreateInfo.queueCount       = 1;

                fixed(float *ptr = &(pQueuePriorities[0]))
                deviceQueueCreateInfo.pQueuePriorities = ptr;

                VkDeviceCreateInfo createInfo = VkDeviceCreateInfo.New();

                createInfo.queueCreateInfoCount    = 1;
                createInfo.pQueueCreateInfos       = &deviceQueueCreateInfo;
                createInfo.ppEnabledExtensionNames = extensions;
                createInfo.enabledExtensionCount   = (uint)extensionNames.Length;

                device = VkDevice.Null;
                Assert(vkCreateDevice(physicalDevice, &createInfo, null, &device));

                foreach (var handle in handles)
                {
                    handle.Free();
                }
            }

            return(device);
        }
Beispiel #16
0
        unsafe public VkPresentModeKHR[] GetSurfacePresentModes(VkSurfaceKHR surf)
        {
            uint count = 0;

            vkGetPhysicalDeviceSurfacePresentModesKHR(phy, surf, out count, IntPtr.Zero);
            VkPresentModeKHR[] modes = new VkPresentModeKHR[count];

            vkGetPhysicalDeviceSurfacePresentModesKHR(phy, surf, out count, modes.Pin());
            modes.Unpin();

            return(modes);
        }
Beispiel #17
0
        /// <summary>
        /// Create the minimum vulkan objects to quickly start a new application. The folowing objects are created:
        /// - Vulkan Instance with extensions present in the `EnabledInstanceExtensions` property.
        /// - Vulkan Surface for the GLFW native window.
        /// - Vulkan device for the selected physical one with configured enabledFeatures and extensions present in `EnabledDeviceExtensions` list. Selection of the default physical device
        ///   may be replaced by the `selectPhysicalDevice` method override.
        /// - Create a default Graphic Queue with presentable support. The default queue creation may be customized by overriding the `createQueues` method.
        /// - Default vulkan Swapchain creation. Some swapchain's parameters are controled through static fields of the `SwapChain` class (ex: `SwapChain.PREFERED_FORMAT`).
        /// - Create a default command pool for the `presentQueue` family index.
        /// - Create an empty command buffer collection (`cmds`).
        /// - Create one unsignaled vulkan semaphore (named `drawComplete` per swapchain image used to control presentation submission to the graphic queue.
        /// - Create a signaled vulkan fence (`drawFence`). (TODO: improve this.
        /// With all these objects, vulkan application programming startup is reduced to the minimal.
        /// </summary>
        protected virtual void initVulkan()
        {
            List <string> instExts = new List <string> (Glfw3.GetRequiredInstanceExtensions());

            if (EnabledInstanceExtensions != null)
            {
                instExts.AddRange(EnabledInstanceExtensions);
            }

            instance = new Instance(instExts.ToArray());

            hSurf = instance.CreateSurface(hWin);

            selectPhysicalDevice();

            VkPhysicalDeviceFeatures enabledFeatures = default;

            configureEnabledFeatures(phy.Features, ref enabledFeatures);

            //First create the c# device class
            dev = new Device(phy);
            dev.debugUtilsEnabled = instance.debugUtilsEnabled;            //store a boolean to prevent testing against the extension string presence.

            //create queue class
            createQueues();

            //activate the device to have effective queues created accordingly to what's available
            dev.Activate(enabledFeatures, EnabledDeviceExtensions);

            swapChain = new SwapChain(presentQueue as PresentQueue, Width, Height, SwapChain.PREFERED_FORMAT,
                                      VSync ? VkPresentModeKHR.FifoKHR : VkPresentModeKHR.ImmediateKHR);
            swapChain.Activate();

            Width  = swapChain.Width;
            Height = swapChain.Height;

            cmdPool = new CommandPool(dev, presentQueue.qFamIndex, VkCommandPoolCreateFlags.ResetCommandBuffer);

            cmds         = new PrimaryCommandBuffer[swapChain.ImageCount];
            drawComplete = new VkSemaphore[swapChain.ImageCount];
            drawFence    = new Fence(dev, true, "draw fence");

            for (int i = 0; i < swapChain.ImageCount; i++)
            {
                drawComplete[i] = dev.CreateSemaphore();
                drawComplete[i].SetDebugMarkerName(dev, "Semaphore DrawComplete" + i);
            }

            cmdPool.SetName("main CmdPool");
        }
Beispiel #18
0
        private void CreateSurface(IntPtr windowHandle)
        {
            VkWin32SurfaceCreateInfoKHR windowsSurfaceInfo = new VkWin32SurfaceCreateInfoKHR();

            windowsSurfaceInfo.sType     = VkStructureType.VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
            windowsSurfaceInfo.hwnd      = windowHandle;
            windowsSurfaceInfo.hinstance = Process.GetCurrentProcess().Handle;

            VkSurfaceKHR newVkSurface;
            var          result = VulkanNative.vkCreateWin32SurfaceKHR(this.vkInstance, &windowsSurfaceInfo, null, &newVkSurface);

            this.vkSurface = newVkSurface;
            Helpers.CheckErrors(result);
        }
Beispiel #19
0
        public GraphicsSwapChain(GraphicsDevice device) : base(device)
        {
            Parameters = NativeDevice.NativeParameters;



            Surface = CreateSurface();

            CreateSwapChain();

            Backbuffer = new Texture(device);

            CreateBackBuffers();
        }
        public VkSwapchainFramebuffer(
            VkGraphicsDevice gd,
            VkSurfaceKHR surface,
            uint width,
            uint height,
            PixelFormat?depthFormat)
            : base()
        {
            _gd          = gd;
            _surface     = surface;
            _depthFormat = depthFormat;

            AttachmentCount = depthFormat.HasValue ? 2u : 1u; // 1 Color + 1 Depth
        }
Beispiel #21
0
        public SwapChain(Device device, SwapchainDescription description) : base(device)
        {
            Description   = description;
            AdapterConfig = NativeDevice.AdapterConfig;

            SwapchainSource = description.Source;

            surface = CreateSurface();
            init_queue_family();

            CreateSwapChain(description.Width, description.Height);

            CreateBackBuffers();

            SetupDepthStencil();
        }
Beispiel #22
0
        private VkDevice CreateDevice(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface)
        {
            //VkQueueFamilyProperties[] properties = physicalDevice.GetQueueFamilyProperties();
            VkQueueFamilyProperties[] properties = vkAPI.QueueFamilyProperties(physicalDevice);
            uint index;

            for (index = 0; index < properties.Length; ++index)
            {
                VkBool32 supported;
                //physicalDevice.GetSurfaceSupportKHR(index, surface, out supported);
                vkAPI.vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, index, surface, &supported).Check();
                if (!supported)
                {
                    continue;
                }

                if (properties[index].queueFlags.HasFlag(VkQueueFlagBits.Graphics))
                {
                    break;
                }
            }

            var queueInfo = new VkDeviceQueueCreateInfo {
                sType = VkStructureType.DeviceQueueCreateInfo
            };

            queueInfo.queuePriorities  = 1.0f;
            queueInfo.queueFamilyIndex = index;

            var info = new VkDeviceCreateInfo {
                sType = VkStructureType.DeviceCreateInfo
            };

            info.EnabledExtensions = vkAPI.VK_KHR_swapchain;
            info.queueCreateInfos  = queueInfo;

            VkDevice vkDevice;

            //physicalDevice.CreateDevice(ref deviceInfo, null, out device);
            vkAPI.vkCreateDevice(physicalDevice, &info, null, &vkDevice).Check();

            info.Free();
            queueInfo.Free();

            return(vkDevice);
        }
        public VkSurfaceFormatKHR[] GetPhysicalDeviceSurfaceFormatsKHR(VkSurfaceKHR surface)
        {
            unsafe
            {
                VkSurfaceFormatKHR[] props;
                uint count = 0;
                do
                {
                    props = new VkSurfaceFormatKHR[count];

                    fixed(VkSurfaceFormatKHR *pptr = props)
                    VkException.Check(vkGetPhysicalDeviceSurfaceFormatsKHR(this, surface, &count, pptr));
                } while (props.Length != count);

                return(props);
            }
        }
Beispiel #24
0
        void initVulkan(bool vSync, bool debugMarkers)
        {
            instance = new Instance();

            hSurf = instance.CreateSurface(hWin);

            phy = instance.GetAvailablePhysicalDevice().Where(p => p.HasSwapChainSupport).FirstOrDefault();

            VkPhysicalDeviceFeatures enabledFeatures = default(VkPhysicalDeviceFeatures);

            configureEnabledFeatures(phy.Features, ref enabledFeatures);

            if (debugMarkers)
            {
                debugMarkers = phy.GetDeviceExtensionSupported(Ext.D.VK_EXT_debug_marker);
            }
            //First create the c# device class
            dev = new Device(phy, debugMarkers);
            //create queue class
            createQueues();

            //activate the device to have effective queues created accordingly to what's available
            dev.Activate(enabledFeatures, EnabledExtensions);

            swapChain = new SwapChain(presentQueue as PresentQueue, width, height, VkFormat.B8g8r8a8Srgb,
                                      vSync ? VkPresentModeKHR.FifoKHR : VkPresentModeKHR.MailboxKHR);
            swapChain.Create();

            cmdPool = new CommandPool(dev, presentQueue.qFamIndex);

            cmds         = new CommandBuffer[swapChain.ImageCount];
            drawComplete = new VkSemaphore[swapChain.ImageCount];

            for (int i = 0; i < swapChain.ImageCount; i++)
            {
                drawComplete[i] = dev.CreateSemaphore();
            }

            cmdPool.SetName("main CmdPool");
            for (int i = 0; i < swapChain.ImageCount; i++)
            {
                drawComplete[i].SetDebugMarkerName(dev, "Semaphore DrawComplete" + i);
            }
        }
Beispiel #25
0
        private unsafe void CreateSurface()
        {
            // Check for Window Handle parameter
            if (Description.DeviceWindowHandle == null)
            {
                throw new ArgumentException("DeviceWindowHandle cannot be null");
            }
            // Create surface
#if STRIDE_UI_SDL
            var control = Description.DeviceWindowHandle.NativeWindow as SDL.Window;
            Silk.NET.Core.Native.VkNonDispatchableHandle surfaceHandle = default;
            SDL.Window.SDL.VulkanCreateSurface((Silk.NET.SDL.Window *)control.SdlHandle, new Silk.NET.Core.Native.VkHandle(GraphicsDevice.NativeInstance.Handle), ref surfaceHandle);
            surface = new VkSurfaceKHR(surfaceHandle.Handle);
#else
            if (Platform.Type == PlatformType.Windows)
            {
                var controlHandle = Description.DeviceWindowHandle.Handle;
                if (controlHandle == IntPtr.Zero)
                {
                    throw new NotSupportedException($"Form of type [{Description.DeviceWindowHandle.GetType().Name}] is not supported. Only System.Windows.Control are supported");
                }

                var surfaceCreateInfo = new VkWin32SurfaceCreateInfoKHR
                {
                    sType          = VkStructureType.Win32SurfaceCreateInfoKHR,
                    instanceHandle = Process.GetCurrentProcess().Handle,
                    windowHandle   = controlHandle,
                };
                vkCreateWin32SurfaceKHR(GraphicsDevice.NativeInstance, &surfaceCreateInfo, null, out surface);
            }
            else if (Platform.Type == PlatformType.Android)
            {
                throw new NotImplementedException();
            }
            else if (Platform.Type == PlatformType.Linux)
            {
                throw new NotSupportedException("Only SDL is supported for the time being on Linux");
            }
            else
            {
                throw new NotSupportedException();
            }
#endif
        }
Beispiel #26
0
        public SwapChain(Device device, PresentationParameters parameters) : base(device)
        {
            Parameters = parameters;

            SwapchainSource ??= parameters.SwapchainSource;

            surface = CreateSurface();

            CreateSwapChain();


            CreateBackBuffers();


            //DepthStencil = new Texture(device, new TextureDescription
            //{
            //    Flags = TextureFlags.DepthStencil,
            //    Usage = GraphicsResourceUsage.Default,
            //    Width = Parameters.BackBufferWidth,
            //    Height = Parameters.BackBufferHeight,
            //});
        }
Beispiel #27
0
        protected VkSurfaceFormatKHR SelectFormat(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface)
        {
            VkSurfaceFormatKHR[] formats;
            //physicalDevice.GetSurfaceFormatsKHR(surface, out formats);
            {
                UInt32 count;
                vkAPI.vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &count, null).Check();
                formats = new VkSurfaceFormatKHR[count];
                fixed(VkSurfaceFormatKHR *pointer = formats)
                {
                    vkAPI.vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &count, pointer).Check();
                }
            }
            foreach (var f in formats)
            {
                if (f.format == VkFormat.R8g8b8a8Unorm || f.format == VkFormat.B8g8r8a8Unorm)
                {
                    return(f);
                }
            }

            throw new System.Exception("didn't find the R8G8B8A8Unorm or B8G8R8A8Unorm format");
        }
Beispiel #28
0
 public VkPresentModeKHR[] GetSurfacePresentModes(VkSurfaceKHR surf)
 {
     vkGetPhysicalDeviceSurfacePresentModesKHR(phy, surf, out uint count, IntPtr.Zero);
     if (Type.GetType("Mono.Runtime") == null)
     {
         uint[] modes = new uint[count];                //this cause an error on mono
         vkGetPhysicalDeviceSurfacePresentModesKHR(phy, surf, out count, modes.Pin());
         modes.Unpin();
         VkPresentModeKHR[] mds = new VkPresentModeKHR[count];
         for (int i = 0; i < count; i++)
         {
             mds[i] = (VkPresentModeKHR)modes[i];
         }
         return(mds);
     }
     else
     {
         VkPresentModeKHR[] modes = new VkPresentModeKHR[count];                //enums not blittable on ms.Net
         vkGetPhysicalDeviceSurfacePresentModesKHR(phy, surf, out count, modes.Pin());
         modes.Unpin();
         return(modes);
     }
 }
Beispiel #29
0
        internal VkSurfaceKHR CreateSurface()
        {
            VkInstance   instance   = NativeDevice.NativeInstance.NativeInstance;
            VkSurfaceKHR defSurface = default;

            VkWin32SurfaceCreateInfoKHR Win32SurfaceCreateInfo = new VkWin32SurfaceCreateInfoKHR()
            {
                sType     = VkStructureType.Win32SurfaceCreateInfoKHR,
                hinstance = Process.GetCurrentProcess().Handle,
                hwnd      = Parameters.DeviceHandle,
            };



            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                vkCreateWin32SurfaceKHR(instance, &Win32SurfaceCreateInfo, null, &defSurface);
            }



            return(defSurface);
        }
Beispiel #30
0
        public void Init(IntPtr hwnd, IntPtr processHandle)
        {
            if (this.isInitialized)
            {
                return;
            }

            this.instance = InitInstance();
            InitDebugCallback(this.instance);
            this.surface          = InitSurface(this.instance, hwnd, processHandle);
            this.vkPhysicalDevice = InitPhysicalDevice(this.instance);
            VkSurfaceFormatKHR surfaceFormat = SelectFormat(this.vkPhysicalDevice, this.surface);

            this.device = CreateDevice(this.vkPhysicalDevice, this.surface);

            this.vkQueue = this.device.GetQueue(0, 0);

            VkSurfaceCapabilitiesKHR surfaceCapabilities;

            //this.vkPhysicalDevice.GetSurfaceCapabilitiesKhr(this.vkSurface, out surfaceCapabilities);
            vkAPI.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this.vkPhysicalDevice, this.surface, &surfaceCapabilities).Check();

            this.swapchain = CreateSwapchain(this.device, this.surface, surfaceFormat, surfaceCapabilities);

            this.vkImages = this.device.GetSwapchainImages(this.swapchain);

            this.renderPass = CreateRenderPass(this.device, surfaceFormat);

            this.framebuffers = CreateFramebuffers(this.device, this.vkImages, surfaceFormat, this.renderPass, surfaceCapabilities);

            this.vkFence     = this.device.CreateFence();
            this.vkSemaphore = this.device.CreateSemaphore();

            // buffers for vertex data.
            VkBuffer vertexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Vertices, VkBufferUsageFlagBits.VertexBuffer, typeof(float));

            VkBuffer indexBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, Indexes, VkBufferUsageFlagBits.IndexBuffer, typeof(short));

            var uniformBufferData = new AreaUniformBuffer(1, 1);

            this.originalWidth  = 1; this.width = this.originalWidth;
            this.originalHeight = 1; this.height = this.originalHeight;

            this.uniformBuffer = CreateBuffer(this.vkPhysicalDevice, this.device, uniformBufferData, VkBufferUsageFlagBits.UniformBuffer, typeof(AreaUniformBuffer));

            this.descriptorSetLayout = CreateDescriptorSetLayout(this.device);

            this.vkPipelineLayout = CreatePipelineLayout(this.device, this.descriptorSetLayout);

            VkPipeline pipeline = CreatePipeline(this.device, surfaceCapabilities, this.renderPass, this.vkPipelineLayout);

            this.descriptorSet = CreateDescriptorSet(this.device, this.descriptorSetLayout);

            UpdateDescriptorSets(this.device, this.uniformBuffer, this.descriptorSet);

            this.commandBuffers = CreateCommandBuffers(
                this.device, this.renderPass, surfaceCapabilities,
                this.vkImages, this.framebuffers, pipeline,
                vertexBuffer, indexBuffer, (uint)Indexes.Length,
                this.vkPipelineLayout, this.descriptorSet);

            this.isInitialized = true;
        }