Esempio n. 1
0
        internal Window(
            string title,
            INativeWindow nativeWindow,
            SurfaceKhr surface,
            HostDevice hostDevice,
            HostDeviceRequirements deviceRequirements,
            Logger logger = null)
        {
            if (nativeWindow == null)
            {
                throw new ArgumentNullException(nameof(nativeWindow));
            }
            if (surface == null)
            {
                throw new ArgumentNullException(nameof(surface));
            }
            if (hostDevice == null)
            {
                throw new ArgumentNullException(nameof(hostDevice));
            }
            this.title        = title;
            this.nativeWindow = nativeWindow;
            this.surface      = surface;
            this.hostDevice   = hostDevice;
            this.logger       = logger;

            //Subscribe to callbacks for the native window
            nativeWindow.CloseRequested += OnNativeWindowCloseRequested;
            nativeWindow.Resized        += OnNativeWindowResized;

            //Create a logical device (and queues on the device)
            IList <string> enabledExtensions;

            (logicalDevice, graphicsQueue, presentQueue, enabledExtensions) = hostDevice.CreateLogicalDevice(
                surface: surface,
                deviceRequirements: deviceRequirements);
            hasDebugMarkerExtension = enabledExtensions.Contains("VK_EXT_debug_marker");
            if (hasDebugMarkerExtension)
            {
                logger?.Log(nameof(Window), "Enabled debug-markers");
            }

            //Get a presentmode to use
            presentMode = hostDevice.GetPresentMode(surface);
            //Get the surfaceformat to use
            (surfaceFormat, surfaceColorspace) = hostDevice.GetSurfaceFormat(surface);
            //Gets how many swapchain images we should use
            swapchainCount = hostDevice.GetSwapchainCount(surface);

            //Create a command-pool attached to this device
            commandPool = logicalDevice.CreateCommandPool(new CommandPoolCreateInfo(
                                                              queueFamilyIndex: graphicsQueue.FamilyIndex,
                                                              flags: CommandPoolCreateFlags.None
                                                              ));

            //Create the swapchain (images to present to the screen)
            CreateSwapchain(swapchainCount);
            //Synchronization objects are used to sync the rendering and presenting
            CreateSynchronizationObjects();
        }
Esempio n. 2
0
        public Window CreateWindow(
            Int2 windowSize,
            HostDeviceRequirements deviceRequirements,
            bool preferDiscreteDevice = true)
        {
            ThrowIfDisposed();
            INativeWindow nativeWindow = nativeApp.CreateWindow(
                size: windowSize,
                minSize: (x: 150, y: 150),
                title: string.Empty);
            SurfaceKhr surface        = CreateSurface(nativeWindow);
            HostDevice graphicsDevice = FindSuitableDevice(
                surface,
                deviceRequirements,
                preferDiscreteDevice);

            return(new Window(
                       title: $"{appName} - {appVersion}",
                       nativeWindow, surface, graphicsDevice, deviceRequirements, logger));
        }
Esempio n. 3
0
        public Host(
            Platform.INativeApp nativeApp,
            string appName,
            int appVersion,
            Logger logger = null)
        {
            if (nativeApp == null)
            {
                throw new ArgumentNullException(nameof(nativeApp));
            }
            this.nativeApp  = nativeApp;
            this.appName    = appName;
            this.appVersion = appVersion;
            this.logger     = logger;

            availableLayers = Instance.EnumerateLayerProperties();
            logger?.LogList(nameof(Host), "Available layers:", availableLayers);
            availbleExtensions = Instance.EnumerateExtensionProperties();
            logger?.LogList(nameof(Host), "Available extensions:", availbleExtensions);

            //Verify that all the required layers are available on this host
            var layersToEnable = new List <string>(GetRequiredLayers(nativeApp.SurfaceType));

            for (int i = 0; i < layersToEnable.Count; i++)
            {
                if (!IsLayerAvailable(layersToEnable[i]))
                {
                    throw new Exception(
                              $"[{nameof(Host)}] Required layer '{layersToEnable[i]}' is not available");
                }
            }

            //Verify that all the required extensions are available on this host
            var extensionsToEnable = new List <string>(GetRequiredExtensions(nativeApp.SurfaceType));

            for (int i = 0; i < extensionsToEnable.Count; i++)
            {
                if (!IsExtensionAvailable(extensionsToEnable[i]))
                {
                    throw new Exception(
                              $"[{nameof(Host)}] Required extension '{extensionsToEnable[i]}' is not available");
                }
            }

            //Add any optional layers IF its supported by this host
            string[] optionalLayers = GetOptionalLayers(nativeApp.SurfaceType);
            for (int i = 0; i < optionalLayers.Length; i++)
            {
                if (IsLayerAvailable(optionalLayers[i]))
                {
                    layersToEnable.Add(optionalLayers[i]);
                }
            }

            //Add any optional extensions IF its supported by this host
            string[] optionalExtensions = GetOptionalExtensions(nativeApp.SurfaceType);
            for (int i = 0; i < optionalExtensions.Length; i++)
            {
                if (IsExtensionAvailable(optionalExtensions[i]))
                {
                    extensionsToEnable.Add(optionalExtensions[i]);
                }
            }

            InstanceCreateInfo createInfo = new InstanceCreateInfo(
                appInfo: new ApplicationInfo(
                    applicationName: appName,
                    applicationVersion: appVersion,
                    engineName: Info.NAME,
                    engineVersion: Info.VERSION,
                    apiVersion: new VulkanCore.Version(major: 1, minor: 0, patch: 69)
                    ),
                enabledLayerNames: layersToEnable.ToArray(),
                enabledExtensionNames: extensionsToEnable.ToArray()
                );

            instance = new Instance(createInfo);

            logger?.Log(nameof(Host), "Created instance");
            logger?.LogList(nameof(Host), "Enabled layers:", layersToEnable);
            logger?.LogList(nameof(Host), "Enabled extensions:", extensionsToEnable);

            #if DEBUG
            if (extensionsToEnable.Contains("VK_EXT_debug_report"))
            {
                debugCallback = instance.CreateDebugReportCallbackExt(
                    new DebugReportCallbackCreateInfoExt(
                        //We want to handle everthing except for info reports
                        flags: DebugReportFlagsExt.All & ~DebugReportFlagsExt.Information,
                        callback: OnDebugReport));
                logger?.Log(nameof(Host), "Enabled debug callback");
            }
            #else
            debugCallback = null;
            #endif

            //Get all the devices in this host
            PhysicalDevice[] physicalDevices = instance.EnumeratePhysicalDevices();
            hostDevices = new HostDevice[physicalDevices.Length];
            for (int i = 0; i < hostDevices.Length; i++)
            {
                hostDevices[i] = new HostDevice(physicalDevices[i], nativeApp.SurfaceType, logger);
            }
        }