Ejemplo n.º 1
0
        public void CreateDebugReportCallbackExt()
        {
            var createInfo = new InstanceCreateInfo(
                enabledLayerNames: new[] { InstanceLayer.LunarGStandardValidation },
                enabledExtensionNames: new[] { InstanceExtension.ExtDebugReport });

            using (var instance = new Instance(createInfo))
            {
                var    callbackArgs   = new List <DebugReportCallbackInfo>();
                int    userData       = 1;
                IntPtr userDataHandle = new IntPtr(&userData);
                var    debugReportCallbackCreateInfo = new DebugReportCallbackCreateInfoExt(
                    DebugReportFlagsExt.All,
                    args =>
                {
                    callbackArgs.Add(args);
                    return(false);
                },
                    userDataHandle);

                using (instance.CreateDebugReportCallbackExt(debugReportCallbackCreateInfo))
                {
                    instance.DebugReportMessageExt(DebugReportFlagsExt.Debug, "test");
                    Assert.NotEmpty(callbackArgs);
                    Assert.Equal(1, *(int *)callbackArgs[0].UserData);
                }
                callbackArgs.Clear();
                using (instance.CreateDebugReportCallbackExt(debugReportCallbackCreateInfo, CustomAllocator))
                {
                    instance.DebugReportMessageExt(DebugReportFlagsExt.Debug, "test");
                    Assert.NotEmpty(callbackArgs);
                    Assert.Equal(1, *(int *)callbackArgs[0].UserData);
                }
            }
        }
Ejemplo n.º 2
0
        private void CreateInstance()
        {
            AvailableInstanceExtensions = Instance.EnumerateExtensionProperties();
            string[] selectExtensions = new[] { Constant.InstanceExtension.ExtDebugReport }
            .Where(AvailableInstanceExtensions.Contains)
            .ToArray();

            // Specify standard validation layers.
            var createInfo = new InstanceCreateInfo
            {
                EnabledLayerNames     = new[] { Constant.InstanceLayer.LunarGStandardValidation },
                EnabledExtensionNames = selectExtensions
            };

            Instance = new Instance(createInfo);

            // Attach debug callback and fail any test on any error or warning from validation layers.
            var debugReportCreateInfo = new DebugReportCallbackCreateInfoExt(
                DebugReportFlagsExt.Error |
                DebugReportFlagsExt.Warning |
                DebugReportFlagsExt.PerformanceWarning,
                args =>
            {
                if ((args.Flags & (DebugReportFlagsExt.Error |
                                   DebugReportFlagsExt.PerformanceWarning |
                                   DebugReportFlagsExt.Warning)) > 0)
                {
                    Assert.True(false, $"Validation layer error/warning:\n\n[{args.Flags}] {args.Message}");
                }
                return(false);
            }
                );

            _debugReportCallback = Instance.CreateDebugReportCallbackExt(debugReportCreateInfo);
        }
Ejemplo n.º 3
0
        public void CreateDebugReportCallbackExt()
        {
            var createInfo = new InstanceCreateInfo(
                enabledLayerNames: new[] { InstanceLayer.LunarGStandardValidation },
                enabledExtensionNames: new[] { InstanceExtension.ExtDebugReport });

            using (var instance = new Instance(createInfo))
            {
                var    callbackArgs   = new List <DebugReportCallbackInfo>();
                int    userData       = 1;
                IntPtr userDataHandle = new IntPtr(&userData);
                var    debugReportCallbackCreateInfo = new DebugReportCallbackCreateInfoExt(
                    DebugReportFlagsExt.All,
                    args =>
                {
                    callbackArgs.Add(args);
                    return(false);
                },
                    userDataHandle);

                // Registering the callback should generate DEBUG messages.
                using (instance.CreateDebugReportCallbackExt(debugReportCallbackCreateInfo)) { }
                using (instance.CreateDebugReportCallbackExt(debugReportCallbackCreateInfo, CustomAllocator)) { }

                Assert.True(callbackArgs.Count > 0);
                Assert.Equal(1, *(int *)callbackArgs[0].UserData);
            }
        }
Ejemplo n.º 4
0
        Instance CreateInstance()
        {
            // There is no global state in Vulkan and all per-application state is stored in a
            // `Instance` object. Creating a `Instance` object initializes the Vulkan library
            // and allows the application to pass information about itself to the implementation.

            // For this example, we want Vulkan to act in a 'default' fashion, so we don't
            // pass and ApplicationInfo object and we don't request any layers or extensions

            String[] enabledLayers = new string[]
            {
                "VK_LAYER_LUNARG_standard_validation"
            };

            var enabledExtensions = new[]
            {
                VulkanConstant.ExtDebugReportExtensionName,
            };

            var instanceCreateInfo = new InstanceCreateInfo(enabledLayers, enabledExtensions);
            var instance           = Vk.CreateInstance(instanceCreateInfo);

            debugCallback = DebugUtils.CreateDebugReportCallback(instance, DebugReport);

            return(instance);
        }
Ejemplo n.º 5
0
        private unsafe void instanceCreate()
        {
            var appInfo = new ApplicationInfo
            {
                SType              = StructureType.ApplicationInfo,
                PApplicationName   = (byte *)Marshal.StringToHGlobalAnsi(Assembly.GetExecutingAssembly().GetName().Name),
                ApplicationVersion = new Version32(1, 0, 0),
                PEngineName        = (byte *)Marshal.StringToHGlobalAnsi(Assembly.GetExecutingAssembly().GetName().Name),
                EngineVersion      = new Version32(1, 0, 0),
                ApiVersion         = Vk.Version11
            };

            var createInfo = new InstanceCreateInfo
            {
                SType                   = StructureType.InstanceCreateInfo,
                PApplicationInfo        = &appInfo,
                EnabledExtensionCount   = 0,
                PpEnabledExtensionNames = null,
                EnabledLayerCount       = 0,
                PNext                   = null
            };

            fixed(Instance *instance = &_vk_instance)
            {
                if (_vk.CreateInstance(&createInfo, null, instance) != Result.Success)
                {
                    throw new Exception("Failed to create instance!");
                }
            }

            _vk.CurrentInstance = _vk_instance;

            Marshal.FreeHGlobal((IntPtr)appInfo.PApplicationName);
            Marshal.FreeHGlobal((IntPtr)appInfo.PEngineName);
        }
Ejemplo n.º 6
0
        public static unsafe ReturnSet <Instance> Create()
        {
            try
            {
                var applicationInfo = new ApplicationInfo
                {
                    StructureType = StructureType.ApplicationInfo,
                    EngineVersion = 0,
                    ApiVersion    = Vulkan.ApiVersion
                };

                var enabledExtensionNames =
                    BuildExtensionArray(ExtensionNames.VK_KHR_surface, ExtensionNames.VK_KHR_win32_surface,
                                        ExtensionNames.VK_EXT_debug_report);

                fixed(void *enabledExtensionNamesPointer = &enabledExtensionNames[0])
                {
                    var instanceCreateInfo = new InstanceCreateInfo
                    {
                        StructureType         = StructureType.InstanceCreateInfo,
                        ApplicationInfo       = new IntPtr(&applicationInfo),
                        EnabledExtensionCount = (uint)enabledExtensionNames.Length,
                        EnabledExtensionNames = new IntPtr(enabledExtensionNamesPointer),
                    };

                    return(new ReturnSet <Instance>(Vulkan.CreateInstance(ref instanceCreateInfo)));
                }
            }
            catch (Exception ex)
            {
                return(new ReturnSet <Instance>(ex));
            }
        }
Ejemplo n.º 7
0
        static void CreateInstance()
        {
            var appInfo = new ApplicationInfo();

            appInfo.ApplicationName = "vulkanExample";
            appInfo.EngineName      = "vulkanExample";
            appInfo.ApiVersion      = MakeVersion(1, 0, 0);

            var instanceEnabledExtensions = new[]
            {
                "VK_KHR_surface",       // VK_KHR_SURFACE_EXTENSION_NAME
                "VK_KHR_win32_surface", // VK_KHR_WIN32_SURFACE_EXTENSION_NAME
            };

            var instanceCreateInfo = new InstanceCreateInfo();

            instanceCreateInfo.ApplicationInfo       = appInfo;
            instanceCreateInfo.EnabledExtensionNames = instanceEnabledExtensions;

            instance = new Instance(instanceCreateInfo);
            Console.WriteLine("[ OK ] Instance");

            var physicalDevices = instance.EnumeratePhysicalDevices();

            Console.WriteLine($"Physical Devices: {physicalDevices.Count}");

            physicalDevice = physicalDevices[0];
            Console.WriteLine("[ OK ] Physical Device");
        }
Ejemplo n.º 8
0
        public void ConstructorWithEnabledLayerAndExtension()
        {
            var createInfo = new InstanceCreateInfo(
                enabledLayerNames: new[] { InstanceLayer.LunarGStandardValidation },
                enabledExtensionNames: new[] { InstanceExtension.ExtDebugReport });

            using (new Instance(createInfo)) { }
        }
Ejemplo n.º 9
0
        public void ConstructorWithApplicationInfo()
        {
            var createInfo1 = new InstanceCreateInfo(new ApplicationInfo());
            var createInfo2 = new InstanceCreateInfo(new ApplicationInfo("app name", new Version(1, 0, 0), "engine name", new Version(2, 0, 0)));

            using (new Instance(createInfo1)) { }
            using (new Instance(createInfo2)) { }
        }
Ejemplo n.º 10
0
        void CreateInstance()
        {
            //LayerProperties[] available_layers = DelegateUtility.EnumerateToArray<LayerProperties> ( vk.EnumerateInstanceLayerProperties );

            ExtensionProperties[] available_extensions = DelegateUtility.EnumerateToArray <ExtensionProperties> (vk.EnumerateInstanceExtensionProperties, IntPtr.Zero);

            List <string> extensions = new List <string> {
                KhrWin32Surface.EXTENSION_NAME, KhrSurface.EXTENSION_NAME
            };

            for (int i = 0; i < extensions.Count; ++i)
            {
                if (!CheckExtensionAvailability(extensions[i], available_extensions))
                {
                    //std::cout << "Could not find instance extension named \"" << extensions[i] << "\"!" << std::endl;
                    return;
                }
            }

            if (CheckExtensionAvailability(ExtDebugReport.EXTENSION_NAME, available_extensions))
            {
                extensions.Add(ExtDebugReport.EXTENSION_NAME);
                supportDebugReport = true;
            }


            var exPtr = MarshalUtility.AllocateString(extensions);

            ApplicationInfo application_info = new ApplicationInfo
            {
                sType              = StructureType.ApplicationInfo,
                pNext              = IntPtr.Zero,
                pApplicationName   = (byte *)Marshal.StringToHGlobalAnsi("Vulkan Window"),
                applicationVersion = new Version(1, 0, 0),
                pEngineName        = (byte *)Marshal.StringToHGlobalAnsi("Vulkan Tutorial"),
                engineVersion      = new Version(1, 0, 0),
                apiVersion         = new Version(1, 0, 0),
            };

            InstanceCreateInfo instance_create_info = new InstanceCreateInfo
            {
                sType                   = StructureType.InstanceCreateInfo,
                pNext                   = IntPtr.Zero,
                flags                   = 0,
                pApplicationInfo        = &application_info,
                enabledLayerCount       = 0,
                ppEnabledLayerNames     = (byte *)0,
                enabledExtensionCount   = (uint)extensions.Count,
                ppEnabledExtensionNames = (byte *)exPtr,
            };

            vk.CreateInstance(ref instance_create_info, (AllocationCallbacks *)0, out vulkan.Instance).CheckError();

            MarshalUtility.FreeString(exPtr, extensions.Count);

            Marshal.FreeHGlobal((IntPtr)application_info.pApplicationName);
            Marshal.FreeHGlobal((IntPtr)application_info.pEngineName);
        }
Ejemplo n.º 11
0
        internal static DeviceInfo[] GetSuitablePhysicalDevices(Vk api)
        {
            var appName = Marshal.StringToHGlobalAnsi(AppName);

            var applicationInfo = new ApplicationInfo
            {
                PApplicationName   = (byte *)appName,
                ApplicationVersion = 1,
                PEngineName        = (byte *)appName,
                EngineVersion      = 1,
                ApiVersion         = Vk.Version12.Value
            };

            var instanceCreateInfo = new InstanceCreateInfo
            {
                SType                   = StructureType.InstanceCreateInfo,
                PApplicationInfo        = &applicationInfo,
                PpEnabledExtensionNames = null,
                PpEnabledLayerNames     = null,
                EnabledExtensionCount   = 0,
                EnabledLayerCount       = 0
            };

            api.CreateInstance(in instanceCreateInfo, null, out var instance).ThrowOnError();

            Marshal.FreeHGlobal(appName);

            uint physicalDeviceCount;

            api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, null).ThrowOnError();

            PhysicalDevice[] physicalDevices = new PhysicalDevice[physicalDeviceCount];

            fixed(PhysicalDevice *pPhysicalDevices = physicalDevices)
            {
                api.EnumeratePhysicalDevices(instance, &physicalDeviceCount, pPhysicalDevices).ThrowOnError();
            }

            DeviceInfo[] devices = new DeviceInfo[physicalDevices.Length];

            for (int i = 0; i < physicalDevices.Length; i++)
            {
                var physicalDevice = physicalDevices[i];
                api.GetPhysicalDeviceProperties(physicalDevice, out var properties);

                devices[i] = new DeviceInfo(
                    StringFromIdPair(properties.VendorID, properties.DeviceID),
                    VendorUtils.GetNameFromId(properties.VendorID),
                    Marshal.PtrToStringAnsi((IntPtr)properties.DeviceName),
                    properties.DeviceType == PhysicalDeviceType.DiscreteGpu);
            }

            api.DestroyInstance(instance, null);

            return(devices);
        }
Ejemplo n.º 12
0
        public static Instance CreateInstance(bool includeCommonDebug = false)
        {
            IEnumerable <InitRequest> initRequest = null;

            if (includeCommonDebug)
            {
                initRequest = InitRequestCommonDebug;
            }

            InstanceCreateInfo instanceCreateInfo = CreateInstanceCreateInfo(initRequest);

            return(new Instance(instanceCreateInfo));
        }
Ejemplo n.º 13
0
        public static void CreateVkInstance(Vk vk, InstanceCreateInfo instanceCreateInfo, out Instance instance)
        {
            fixed(Instance *_instance = &instance)
            {
                var result = vk.CreateInstance(&instanceCreateInfo, null, _instance);

                if (result != Result.Success)
                {
                    Console.WriteLine("Failed to create vulkan instance");
                    return;
                }
            }
        }
Ejemplo n.º 14
0
        private Instance CreateInstance(bool debug)
        {
            // Specify standard validation layers.
            string surfaceExtension;

            switch (Host.Platform)
            {
            case Platform.Android:
                surfaceExtension = Constant.InstanceExtension.KhrAndroidSurface;
                break;

            case Platform.Win32:
                surfaceExtension = Constant.InstanceExtension.KhrWin32Surface;
                break;

            case Platform.MacOS:
                surfaceExtension = Constant.InstanceExtension.MvkMacOSSurface;
                break;

            default:
                throw new NotImplementedException();
            }

            var createInfo = new InstanceCreateInfo();

            //Currently MoltenVK (used for MacOS) doesn't support the debug layer.
            if (debug && Host.Platform != Platform.MacOS)
            {
                var availableLayers = Instance.EnumerateLayerProperties();
                createInfo.EnabledLayerNames = new[] { Constant.InstanceLayer.LunarGStandardValidation }
                .Where(availableLayers.Contains)
                .ToArray();
                createInfo.EnabledExtensionNames = new[]
                {
                    Constant.InstanceExtension.KhrSurface,
                    surfaceExtension,
                    Constant.InstanceExtension.ExtDebugReport
                };
            }
            else
            {
                createInfo.EnabledExtensionNames = new[]
                {
                    Constant.InstanceExtension.KhrSurface,
                    surfaceExtension,
                };
            }
            return(new Instance(createInfo));
        }
Ejemplo n.º 15
0
        public void EnumeratePhysicalDeviceGroupsKhx()
        {
            if (!AvailableDeviceExtensions.Contains(InstanceExtension.KhxDeviceGroupCreation))
            {
                return;
            }

            var createInfo = new InstanceCreateInfo(
                enabledExtensionNames: new[] { InstanceExtension.KhxDeviceGroupCreation });

            using (var instance = new Instance(createInfo))
            {
                instance.EnumeratePhysicalDeviceGroupsKhx();
            }
        }
Ejemplo n.º 16
0
        private void CreateInstance()
        {
            ApplicationInfo applicationInfo = VkCreator.CreateApplicationInfo("Lux Graphics", default(SharpVk.Version), m_applicationName);

            List <string> extensions = new List <string>()
            {
                KhrSurface.ExtensionName,
                KhrWin32Surface.ExtensionName,
            };

            CheckInstanceExtensions(extensions);

            List <string> validationLayers = new List <string>();

            m_logger.WriteLine("{0} validation layers", m_usingValidationLayer ? "Using" : "Not using");

            if (m_usingValidationLayer)
            {
                validationLayers.Add("VK_LAYER_LUNARG_standard_validation");

                m_logger.WriteLine("Requested Instance validation layers are:");

                validationLayers.ForEach(m_logger.WriteLine);

                List <LayerProperties> layerProperties = Instance.EnumerateLayerProperties().ToList();

                m_logger.WriteLine("Available Instance validation layers are:");

                layerProperties.Select(layer => layer.LayerName).ToList().ForEach(m_logger.WriteLine);

                if (validationLayers.Except(layerProperties.Select(layer => layer.LayerName)).Any())
                {
                    m_logger.WriteLine("The following requested validation layers are not available:");

                    validationLayers.Except(layerProperties.Select(layer => layer.LayerName)).ToList().ForEach(m_logger.WriteLine);

                    throw new Exception("Instance validation layers not supported!");
                }

                m_logger.WriteLine("All requested Instance validation layers have been found and are supported.");
            }

            InstanceCreateInfo instanceCreateInfo = VkCreator.CreateInstanceCreateInfo(applicationInfo, extensions.ToArray(), validationLayers.ToArray());

            m_vkInstance = Instance.Create(instanceCreateInfo);

            m_logger.WriteLine("Instance created for application \"{0}\"", m_applicationName);
        }
Ejemplo n.º 17
0
        public static void Main()
        {
            // https://gist.github.com/graphitemaster/e162a24e57379af840d4
            var applicationInfo = new ApplicationInfo
            {
                ApplicationName = "Tutorial 1",
                EngineName      = "",
                EngineVersion   = 1,
                ApiVersion      = Version.Make(1, 0, 0)
            };
            var instanceInfo = new InstanceCreateInfo
            {
                ApplicationInfo       = applicationInfo,
                Flags                 = 0,
                EnabledLayerCount     = 0,
                EnabledLayerNames     = null,
                EnabledExtensionCount = 2,
                EnabledExtensionNames = new [] {
                    Extension.KhrSurface,
                    Extension.KhrWin32Surface
                }
            };
            var instance = new Instance(instanceInfo);
            var devices  = instance.EnumeratePhysicalDevices();

            foreach (var dev in devices)
            {
                PrintDeviceInformation(dev);
            }

            var physicalDevice = devices.First();
            var device         = CreateAbstractDevice(physicalDevice);

            var window = new Window();

            window.Show();

            var swapChain = new SwapChain(instance, physicalDevice, device);

            swapChain.Initialize(window);
            swapChain.Create(null);

            instance.Destroy();

            Console.ReadLine();
        }
Ejemplo n.º 18
0
        internal static Instance CreateInstance()
        {
            // Pick surface extension for platform to create a window surface
            string surfaceExtension;

            switch (Environment.OSVersion.Platform)
            {
            case PlatformID.Win32NT:
                surfaceExtension = Constant.InstanceExtension.KhrWin32Surface;
                break;

            //case PlatformID.Unix:
            //surfaceExtension = Constant.InstanceExtension.KhrXlibSurface;
            //break;
            default:
                throw new PlatformNotSupportedException("No surface extension for this platform");
            }

            // Start defining create info for this instance
            var createInfo = new InstanceCreateInfo();

#if DEBUG
            // Get properties of all available validation layers
            var availableLayers = Instance.EnumerateLayerProperties();
            // Pick all of the layers in the below array that are available
            createInfo.EnabledLayerNames = new[] { Constant.InstanceLayer.LunarGStandardValidation }
            .Where(availableLayers.Contains)
            .ToArray();
            // Set enabled extension names
            createInfo.EnabledExtensionNames = new[]
            {
                Constant.InstanceExtension.KhrSurface,
                surfaceExtension,
                Constant.InstanceExtension.ExtDebugReport // Debug report extension
            };
#else
            // Set enabled extension names
            createInfo.EnabledExtensionNames = new[]
            {
                Constant.InstanceExtension.KhrSurface,
                surfaceExtension,
            };
#endif

            return(new Instance(createInfo));
        }
Ejemplo n.º 19
0
        internal Instance(string appName, string engName,
                          VER appVer, VER engVer, VER apiVer, bool bDebug)
        {
            string vlName = "";

            if (bDebug)
            {
                if (!bCheckValidation(out vlName))
                {
                    return;
                }
            }

            ApplicationInfo ai = new ApplicationInfo();

            ai.ApplicationName    = appName;
            ai.ApplicationVersion = appVer;
            ai.EngineName         = engName;
            ai.EngineVersion      = engVer;
            ai.ApiVersion         = apiVer;

            InstanceCreateInfo ici = new InstanceCreateInfo();

            ici.Next            = IntPtr.Zero;
            ici.ApplicationInfo = ai;

            List <string> extensions = new List <string>();

            extensions.AddRange(Vulkan.GetRequiredInstanceExtensions());
            if (bDebug)
            {
                extensions.Add(Constant.InstanceExtension.ExtDebugReport);
            }

            //get extension stuff from glfw
            ici.EnabledExtensionNames = extensions.ToArray();

            if (bDebug)
            {
                ici.EnabledLayerNames = new string[1] {
                    vlName
                };
            }

            mInstance = new INST(ici, null);
        }
Ejemplo n.º 20
0
        private Instance CreateInstance()
        {
            var app = new ApplicationInfo();

            app.ApiVersion         = Vulkan.Version.Make(1, 0, 0);
            app.ApplicationName    = "VulkanSample";
            app.ApplicationVersion = Vulkan.Version.Make(1, 0, 0);
            app.EngineName         = "Engine0";
            app.EngineVersion      = Vulkan.Version.Make(1, 0, 0);

            var info = new InstanceCreateInfo();

            info.ApplicationInfo       = app;
            info.EnabledExtensionNames = new [] { "VK_KHR_surface", "VK_KHR_win32_surface" };

            return(new Instance(info));
        }
Ejemplo n.º 21
0
        public Instance(InstanceCreateInfo CreateInfo, AllocationCallbacks Allocator = null)
        {
            Result result;

            unsafe
            {
                fixed(IntPtr *ptrInstance = &m)
                {
                    result = Interop.NativeMethods.vkCreateInstance(CreateInfo.m, Allocator != null ? Allocator.m : null, ptrInstance);
                }
            }

            if (result != Result.Success)
            {
                throw new ResultException(result);
            }
        }
Ejemplo n.º 22
0
        public Instance(InstanceCreateInfo createInfo, AllocationCallbacks allocator = null)
        {
            Result result;

            unsafe
            {
                fixed(IntPtr *ptrInstance = &_handle)
                {
                    result = Interop.NativeMethods.vkCreateInstance(createInfo._handle, allocator != null ? allocator.Handle : null, ptrInstance);
                }
            }

            if (result != Result.Success)
            {
                throw new ResultException(result);
            }
        }
Ejemplo n.º 23
0
        public static void CreateVkInfo(string appName, out ApplicationInfo applicationInfo, out InstanceCreateInfo instanceCreateInfo)
        {
            applicationInfo = new ApplicationInfo
            {
                SType = StructureType.ApplicationInfo,

                PApplicationName = (byte *)Marshal.StringToHGlobalAnsi(appName),
                ApiVersion       = Vk.Version12
            };

            fixed(ApplicationInfo *appInfo = &applicationInfo)
            {
                instanceCreateInfo = new InstanceCreateInfo
                {
                    SType            = StructureType.InstanceCreateInfo,
                    PApplicationInfo = appInfo
                };
            }
        }
Ejemplo n.º 24
0
        protected Instance CreateInstance()
        {
            String[] enabledLayers = new string[]
            {
                "VK_LAYER_LUNARG_standard_validation"
            };

            var enabledExtensions = new[]
            {
                VulkanConstant.KhrSurfaceExtensionName,
                VulkanConstant.KhrWin32SurfaceExtensionName,
                VulkanConstant.ExtDebugReportExtensionName,
            };

            var instanceCreateInfo = new InstanceCreateInfo(enabledLayers, enabledExtensions);
            var instance           = Vk.CreateInstance(instanceCreateInfo);

            debugCallback = DebugUtils.CreateDebugReportCallback(instance, DebugReport);

            return(instance);
        }
        private Instance CreateInstance()
        {
            using var appName    = SilkMarshal.StringToMemory("Hello Cube");
            using var engineName = SilkMarshal.StringToMemory("Custom Engine");

            var appInfo = new ApplicationInfo
                          (
                pApplicationName: (byte *)appName,
                applicationVersion: new Version32(0, 0, 1),
                pEngineName: (byte *)engineName,
                engineVersion: new Version32(0, 0, 1),
                apiVersion: Vk.Version11
                          );

            List <string> extensions = new List <string>(GetWindowExtensions())
            {
                Debugging.DebugExtensionString
            };

            string[] layers = new string[] { "VK_LAYER_KHRONOS_validation" };

            using var extList   = SilkMarshal.StringArrayToMemory(extensions);
            using var layerList = SilkMarshal.StringArrayToMemory(layers);

            var instInfo = new InstanceCreateInfo(pApplicationInfo: &appInfo,
                                                  enabledLayerCount: (uint)layers.Length,
                                                  ppEnabledLayerNames: (byte **)layerList,
                                                  enabledExtensionCount: (uint)extensions.Count,
                                                  ppEnabledExtensionNames: (byte **)extList);

            Instance inst;
            var      res = VkApi.CreateInstance(&instInfo, null, &inst);

            if (res != Result.Success)
            {
                throw new VMASharp.VulkanResultException("Instance Creation Failed", res);
            }

            return(inst);
        }
Ejemplo n.º 26
0
        public bool TryInit(string appName, uint version, string[] validationLayers, string[] extensionNames)
        {
            LayerProperties[] layers = Commands.EnumerateInstanceLayerProperties();
            if (!ValidateLayers(validationLayers, layers))
            {
                return(false);
            }

            ExtensionProperties[] extensions = Commands.EnumerateInstanceExtensionProperties("");
            if (!ValidateExtentsions(extensionNames, extensions))
            {
                return(false);
            }

            InstanceCreateInfo info = new InstanceCreateInfo
            {
                ApplicationInfo = new ApplicationInfo
                {
                    ApplicationName    = appName,
                    ApplicationVersion = 0,
                    EngineName         = appName,
                    EngineVersion      = 0,
                    ApiVersion         = ApiVersion,
                },
                EnabledLayerNames     = validationLayers,
                EnabledExtensionNames = extensionNames,
            };

            Instance = new Instance(info);

            gpuList = Instance.EnumeratePhysicalDevices();
            if (gpuList.Length < 0)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 27
0
        public void DebugReportMessageExt()
        {
            const string message = "message õäöü";
            const DebugReportObjectTypeExt objectType = DebugReportObjectTypeExt.DebugReportCallback;
            const long   @object     = long.MaxValue;
            var          location    = new IntPtr(int.MaxValue);
            const int    messageCode = 1;
            const string layerPrefix = "prefix õäöü";

            bool visitedCallback = false;

            var instanceCreateInfo = new InstanceCreateInfo(
                enabledExtensionNames: new[] { InstanceExtension.ExtDebugReport });

            using (var instance = new Instance(instanceCreateInfo))
            {
                var debugReportCallbackCreateInfo = new DebugReportCallbackCreateInfoExt(
                    DebugReportFlagsExt.Error,
                    args =>
                {
                    Assert.Equal(objectType, args.ObjectType);
                    Assert.Equal(@object, args.Object);
                    Assert.Equal(location, args.Location);
                    Assert.Equal(messageCode, args.MessageCode);
                    Assert.Equal(layerPrefix, args.LayerPrefix);
                    Assert.Equal(message, args.Message);
                    visitedCallback = true;
                    return(false);
                });
                using (instance.CreateDebugReportCallbackExt(debugReportCallbackCreateInfo))
                {
                    instance.DebugReportMessageExt(DebugReportFlagsExt.Error, message, objectType,
                                                   @object, location, messageCode, layerPrefix);
                }
            }

            Assert.True(visitedCallback);
        }
        private void CreateInstance()
        {
            ApplicationInfo appInfo = new ApplicationInfo()
            {
                ApplicationName = "Hello Triangle",
                EngineName      = "WaveEngine",
                EngineVersion   = Vulkan.Version.Make(3, 0, 0),
            };

            string[] extensions = new string[]
            {
                "VK_KHR_surface",
                "VK_KHR_win32_surface",
            };

            InstanceCreateInfo createInfo = new InstanceCreateInfo()
            {
                EnabledExtensionNames = extensions,
                ApplicationInfo       = appInfo,
            };

            vkInstance = new Instance(createInfo);
        }
Ejemplo n.º 29
0
        public static unsafe Instance CreateInstance(string[] instanceLayers, string[] instanceExtensions, IVkSurface windowSurface)
        {
            ApplicationInfo appInfo = new()
            {
                SType = StructureType.ApplicationInfo,

                PApplicationName   = (byte *)SilkMarshal.StringToPtr("SilkyNvg-Vulkan-Example"),
                ApplicationVersion = Vk.MakeVersion(1, 0, 0),
                PEngineName        = (byte *)SilkMarshal.StringToPtr("SilkyNvg (Renderer: Vulkan)"),
                EngineVersion      = Vk.MakeVersion(1, 0, 0),
                ApiVersion         = Vk.Version12
            };

            byte **windowExtensionsPtr   = windowSurface.GetRequiredExtensions(out uint windowExtensionCount);
            byte **instanceExtensionsPtr = (byte **)SilkMarshal.StringArrayToPtr(instanceExtensions);
            byte **extensions            = stackalloc byte *[(int)windowExtensionCount + instanceExtensions.Length];
            int    i = 0;

            for (; i < windowExtensionCount; i++)
            {
                extensions[i] = windowExtensionsPtr[i];
            }
            for (; i < windowExtensionCount + instanceExtensions.Length; i++)
            {
                extensions[i] = instanceExtensionsPtr[i];
            }
            CheckInstanceExtensionsPresent(extensions, (int)windowExtensionCount + instanceExtensions.Length);

            CheckInstanceLayersPresent(instanceLayers);
            byte **layers = (byte **)SilkMarshal.StringArrayToPtr(instanceLayers);

            InstanceCreateInfo instanceCreateInfo = VkInit.InstanceCreateInfo(appInfo, layers,
                                                                              (uint)instanceLayers.Length, extensions, windowExtensionCount);

            AssertVulkan(Vk.CreateInstance(instanceCreateInfo, null, out Instance instance));
            return(instance);
        }
Ejemplo n.º 30
0
        public static InstanceCreateInfo CreateInstanceCreateInfo(ApplicationInfo appInfo, IEnumerable <InitRequest> initRequest = null)
        {
            string[] layers;
            string[] extentions;
            if (initRequest != null)
            {
                layers     = GetInstanceLayers(initRequest);
                extentions = GetInstanceExtention(initRequest);
            }
            else
            {
                layers     = new string[0];
                extentions = new string[0];
            }

            InstanceCreateInfo instanceCreateInfo = new InstanceCreateInfo
            {
                ApplicationInfo       = appInfo,
                EnabledLayerNames     = layers,
                EnabledExtensionNames = extentions,
            };

            return(instanceCreateInfo);
        }
Ejemplo n.º 31
0
 public static unsafe Instance CreateInstance(ref InstanceCreateInfo createInfo, AllocationCallbacks* allocator = null)
 {
     Instance instance;
     fixed (InstanceCreateInfo* __createInfo__ = &createInfo)
     {
         vkCreateInstance(__createInfo__, allocator, &instance).CheckError();
     }
     return instance;
 }
        public unsafe GraphicsAdapterFactoryInstance(bool enableValidation)
        {
            var applicationInfo = new ApplicationInfo
            {
                StructureType = StructureType.ApplicationInfo,
                ApiVersion = new SharpVulkan.Version(1, 0, 0),
                EngineName = Marshal.StringToHGlobalAnsi("Xenko"),
                //EngineVersion = new SharpVulkan.Version()
            };

            var desiredLayerNames = new[]
            {
                    //"VK_LAYER_LUNARG_standard_validation",
                    "VK_LAYER_GOOGLE_threading",
                    "VK_LAYER_LUNARG_parameter_validation",
                    "VK_LAYER_LUNARG_device_limits",
                    "VK_LAYER_LUNARG_object_tracker",
                    "VK_LAYER_LUNARG_image",
                    "VK_LAYER_LUNARG_core_validation",
                    "VK_LAYER_LUNARG_swapchain",
                    "VK_LAYER_GOOGLE_unique_objects",
                    //"VK_LAYER_LUNARG_api_dump",
                    //"VK_LAYER_LUNARG_vktrace"
                };

            IntPtr[] enabledLayerNames = new IntPtr[0];

            if (enableValidation)
            {
                var layers = Vulkan.InstanceLayerProperties;
                var availableLayerNames = new HashSet<string>();

                for (int index = 0; index < layers.Length; index++)
                {
                    var properties = layers[index];
                    var namePointer = new IntPtr(Interop.Fixed(ref properties.LayerName));
                    var name = Marshal.PtrToStringAnsi(namePointer);

                    availableLayerNames.Add(name);
                }

                enabledLayerNames = desiredLayerNames
                    .Where(x => availableLayerNames.Contains(x))
                    .Select(Marshal.StringToHGlobalAnsi).ToArray();
            }

            var extensionProperties = Vulkan.GetInstanceExtensionProperties();
            var availableExtensionNames = new List<string>();
            var desiredExtensionNames = new List<string>();

            for (int index = 0; index < extensionProperties.Length; index++)
            {
                var namePointer = new IntPtr(Interop.Fixed(ref extensionProperties[index].ExtensionName));
                var name = Marshal.PtrToStringAnsi(namePointer);
                availableExtensionNames.Add(name);
            }

            desiredExtensionNames.Add("VK_KHR_surface");
            if (!availableExtensionNames.Contains("VK_KHR_surface"))
                throw new InvalidOperationException("Required extension VK_KHR_surface is not available");

#if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
            desiredExtensionNames.Add("VK_KHR_win32_surface");
            if (!availableExtensionNames.Contains("VK_KHR_win32_surface"))
                throw new InvalidOperationException("Required extension VK_KHR_win32_surface is not available");
#elif SILICONSTUDIO_PLATFORM_ANDROID
                desiredExtensionNames.Add("VK_KHR_android_surface");
                if (!availableExtensionNames.Contains("VK_KHR_android_surface"))
                    throw new InvalidOperationException("Required extension VK_KHR_android_surface is not available");
#elif SILICONSTUDIO_PLATFORM_LINUX
                if (availableExtensionNames.Contains("VK_KHR_xlib_surface"))
                {
                    desiredExtensionNames.Add("VK_KHR_xlib_surface");
                    HasXlibSurfaceSupport = true;
                }
                else if (availableExtensionNames.Contains("VK_KHR_xcb_surface"))
                {
                    desiredExtensionNames.Add("VK_KHR_xcb_surface");
                }
                else
                {
                    throw new InvalidOperationException("None of the supported surface extensions VK_KHR_xcb_surface or VK_KHR_xlib_surface is available");
                }
#endif
            bool enableDebugReport = enableValidation && availableExtensionNames.Contains("VK_EXT_debug_report");
            if (enableDebugReport)
                desiredExtensionNames.Add("VK_EXT_debug_report");

            var enabledExtensionNames = desiredExtensionNames.Select(Marshal.StringToHGlobalAnsi).ToArray();

            var createDebugReportCallbackName = Marshal.StringToHGlobalAnsi("vkCreateDebugReportCallbackEXT");

            try
            {
                fixed (void* enabledExtensionNamesPointer = &enabledExtensionNames[0])
                {
                    var instanceCreateInfo = new InstanceCreateInfo
                    {
                        StructureType = StructureType.InstanceCreateInfo,
                        ApplicationInfo = new IntPtr(&applicationInfo),
                        EnabledLayerCount = enabledLayerNames != null ? (uint)enabledLayerNames.Length : 0,
                        EnabledLayerNames = enabledLayerNames?.Length > 0 ? new IntPtr(Interop.Fixed(enabledLayerNames)) : IntPtr.Zero,
                        EnabledExtensionCount = (uint)enabledExtensionNames.Length,
                        EnabledExtensionNames = new IntPtr(enabledExtensionNamesPointer)
                    };

                    NativeInstance = Vulkan.CreateInstance(ref instanceCreateInfo);
                }

                if (enableDebugReport)
                {
                    var createDebugReportCallback = (CreateDebugReportCallbackDelegate)Marshal.GetDelegateForFunctionPointer(NativeInstance.GetProcAddress((byte*)createDebugReportCallbackName), typeof(CreateDebugReportCallbackDelegate));

                    debugReport = DebugReport;
                    var createInfo = new DebugReportCallbackCreateInfo
                    {
                        StructureType = StructureType.DebugReportCallbackCreateInfo,
                        Flags = (uint)(DebugReportFlags.Error | DebugReportFlags.Warning /* | DebugReportFlags.PerformanceWarning | DebugReportFlags.Information | DebugReportFlags.Debug*/),
                        Callback = Marshal.GetFunctionPointerForDelegate(debugReport)
                    };
                    createDebugReportCallback(NativeInstance, ref createInfo, null, out debugReportCallback);
                }

                if (availableExtensionNames.Contains("VK_EXT_debug_marker"))
                {
                    var beginDebugMarkerName = System.Text.Encoding.ASCII.GetBytes("vkCmdDebugMarkerBeginEXT");

                    var ptr = NativeInstance.GetProcAddress((byte*)Interop.Fixed(beginDebugMarkerName));
                    if (ptr != IntPtr.Zero)
                        BeginDebugMarker = (BeginDebugMarkerDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(BeginDebugMarkerDelegate));

                    var endDebugMarkerName = System.Text.Encoding.ASCII.GetBytes("vkCmdDebugMarkerEndEXT");
                    ptr = NativeInstance.GetProcAddress((byte*)Interop.Fixed(endDebugMarkerName));
                    if (ptr != IntPtr.Zero)
                        EndDebugMarker = (EndDebugMarkerDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(EndDebugMarkerDelegate));
                }
            }
            finally
            {
                foreach (var enabledExtensionName in enabledExtensionNames)
                {
                    Marshal.FreeHGlobal(enabledExtensionName);
                }

                foreach (var enabledLayerName in enabledLayerNames)
                {
                    Marshal.FreeHGlobal(enabledLayerName);
                }

                Marshal.FreeHGlobal(applicationInfo.EngineName);
                Marshal.FreeHGlobal(createDebugReportCallbackName);
            }
        }
Ejemplo n.º 33
0
        protected virtual void CreateInstance()
        {
            var applicationInfo = new ApplicationInfo
            {
                StructureType = StructureType.ApplicationInfo,
                EngineVersion = 0,
                ApiVersion = Vulkan.ApiVersion
            };

            var enabledLayerNames = new []
            {
                Marshal.StringToHGlobalAnsi("VK_LAYER_LUNARG_standard_validation"),
            };

            var enabledExtensionNames = new []
            {
                Marshal.StringToHGlobalAnsi("VK_KHR_surface"),
                Marshal.StringToHGlobalAnsi("VK_KHR_win32_surface"),
                Marshal.StringToHGlobalAnsi("VK_EXT_debug_report"),
            };

            try
            {
                fixed (void* enabledLayerNamesPointer = &enabledLayerNames[0])
                fixed (void* enabledExtensionNamesPointer = &enabledExtensionNames[0])
                {
                    var instanceCreateInfo = new InstanceCreateInfo
                    {
                        StructureType = StructureType.InstanceCreateInfo,
                        ApplicationInfo = new IntPtr(&applicationInfo),
                        EnabledExtensionCount = (uint)enabledExtensionNames.Length,
                        EnabledExtensionNames = new IntPtr(enabledExtensionNamesPointer),
                    };

                    if (validate)
                    {
                        instanceCreateInfo.EnabledLayerCount = (uint)enabledLayerNames.Length;
                        instanceCreateInfo.EnabledLayerNames = new IntPtr(enabledLayerNamesPointer);
                    }

                    instance = Vulkan.CreateInstance(ref instanceCreateInfo);
                }

                if (validate)
                {
                    var createDebugReportCallbackName = Encoding.ASCII.GetBytes("vkCreateDebugReportCallbackEXT");
                    fixed (byte* createDebugReportCallbackNamePointer = &createDebugReportCallbackName[0])
                    {
                        var createDebugReportCallback = Marshal.GetDelegateForFunctionPointer<CreateDebugReportCallbackDelegate>(instance.GetProcAddress(createDebugReportCallbackNamePointer));

                        debugReport = DebugReport;
                        var createInfo = new DebugReportCallbackCreateInfo
                        {
                            StructureType = StructureType.DebugReportCallbackCreateInfo,
                            Flags = (uint)(DebugReportFlags.Error | DebugReportFlags.Warning | DebugReportFlags.PerformanceWarning),
                            Callback = Marshal.GetFunctionPointerForDelegate(debugReport)
                        };
                        createDebugReportCallback(instance, ref createInfo, null, out debugReportCallback);
                    }
                }
            }
            finally
            {
                foreach (var enabledExtensionName in enabledExtensionNames)
                    Marshal.FreeHGlobal(enabledExtensionName);

                foreach (var enabledLayerName in enabledLayerNames)
                    Marshal.FreeHGlobal(enabledLayerName);
            }

            physicalDevice = instance.PhysicalDevices[0];

            var props = physicalDevice.QueueFamilyProperties;
        }
Ejemplo n.º 34
0
 internal static unsafe extern Result vkCreateInstance(InstanceCreateInfo* createInfo, AllocationCallbacks* allocator, Instance* instance);