Example #1
0
        public static ShaderModule LoadShaderModule(IVulkanAppHost host, VulkanContext ctx, string path)
        {
            const int defaultBufferSize = 4096;

            using (Stream stream = host.Open(path))
                using (var ms = new MemoryStream())
                {
                    stream.CopyTo(ms, defaultBufferSize);
                    return(ctx.Device.CreateShaderModule(new ShaderModuleCreateInfo(ms.ToArray())));
                }
        }
Example #2
0
        public static VkShaderModule LoadShaderModule(IVulkanAppHost host, VulkanContext ctx, string path)
        {
            const int defaultBufferSize = 4096;

            using (Stream stream = host.Open(path))
                using (var ms = new MemoryStream())
                {
                    stream.CopyTo(ms, defaultBufferSize);

                    byte[] bytes = ms.ToArray();

                    // Create a new shader module that will be used for Pipeline creation
                    VkShaderModule shaderModule;
                    vkCreateShaderModule(ctx.Device, bytes, null, out shaderModule);
                    return(shaderModule);
                }
        }
Example #3
0
        internal unsafe VulkanPhysicalDevice(Vk vk, VulkanContext context, PhysicalDevice physicalDevice) : base(vk)
        {
            if (context.Instance is null)
            {
                throw new NullReferenceException(nameof(context.Instance));
            }

            _Context        = context;
            _PhysicalDevice = physicalDevice;
            VK.GetPhysicalDeviceProperties(this, out PhysicalDeviceProperties properties);

            APIVersion    = properties.ApiVersion;
            DriverVersion = properties.DriverVersion;
            VendorID      = properties.VendorID;
            DeviceID      = properties.DeviceID;
            Type          = properties.DeviceType;
            Name          = SilkMarshal.PtrToString((nint)properties.DeviceName);

            SwapChainSupportDetails = GetSwapChainSupport();
            _Extensions             = GetExtensions();
        }
Example #4
0
        public static VulkanImage LoadKtxVulkanImage(IVulkanAppHost host, VulkanContext ctx, string path)
        {
            using (var reader = new BinaryReader(host.Open(path)))
            {
                byte[] identifier = reader.ReadBytes(12);

                if (!identifier.SequenceEqual(KtxIdentifier))
                {
                    throw new InvalidOperationException("File is not in Khronos Texture format.");
                }

                int endienness            = reader.ReadInt32();
                int glType                = reader.ReadInt32();
                int glTypeSize            = reader.ReadInt32();
                int glFormat              = reader.ReadInt32();
                int glInternalFormat      = reader.ReadInt32();
                int glBaseInternalFormat  = reader.ReadInt32();
                int pixelWidth            = reader.ReadInt32();
                int pixelHeight           = reader.ReadInt32();
                int pixelDepth            = reader.ReadInt32();
                int numberOfArrayElements = reader.ReadInt32();
                int numberOfFaces         = reader.ReadInt32();
                int numberOfMipmapLevels  = reader.ReadInt32();
                int bytesOfKeyValueData   = reader.ReadInt32();

                // Skip key-value data.
                reader.ReadBytes(bytesOfKeyValueData);

                // Some of the values may be 0 - ensure at least 1.
                pixelWidth            = Math.Max(pixelWidth, 1);
                pixelHeight           = Math.Max(pixelHeight, 1);
                pixelDepth            = Math.Max(pixelDepth, 1);
                numberOfArrayElements = Math.Max(numberOfArrayElements, 1);
                numberOfFaces         = Math.Max(numberOfFaces, 1);
                numberOfMipmapLevels  = Math.Max(numberOfMipmapLevels, 1);

                int numberOfSlices = Math.Max(numberOfFaces, numberOfArrayElements);

                if (!_glInternalFormatToVkFormat.TryGetValue(glInternalFormat, out Format format))
                {
                    throw new NotImplementedException("glInternalFormat not mapped to VkFormat.");
                }

                var data = new TextureData
                {
                    Mipmaps = new TextureData.Mipmap[numberOfMipmapLevels],
                    Format  = format
                };

                for (int i = 0; i < numberOfMipmapLevels; i++)
                {
                    var mipmap = new TextureData.Mipmap();
                    mipmap.Size     = reader.ReadInt32();
                    mipmap.Extent   = new Extent3D(pixelWidth, pixelHeight, pixelDepth);
                    mipmap.Data     = reader.ReadBytes(mipmap.Size);
                    data.Mipmaps[i] = mipmap;
                    break; // TODO: impl
                    //for (int j = 0; j < numberOfArrayElements; j++)
                    //{
                    //    for (int k = 0; k < numberOfFaces; k++)
                    //    {
                    //        for (int l = 0; l < pixelDepth; l++)
                    //        {
                    //            //for (int row = 0;
                    //            //    row < )
                    //        }
                    //    }
                    //}
                }

                return(VulkanImage.Texture2D(ctx, data));
            }
        }
Example #5
0
 internal unsafe VulkanSwapChain(Vk vk, VulkanContext context, ChooseSwapChainSurfaceFormat chooseFormat, ChooseSwapChainPresentMode choosePresentMode,
                                 ChooseSwapChainExtents chooseExtents) : base(vk)
 {
Example #6
0
        internal unsafe VulkanLogicalDevice(Vk vk, VulkanContext context, string[] extensions, string[]?validationLayers) : base(vk)
        {
            _Context           = context;
            SwapchainExtension = GetDeviceExtension <SwapchainExtension>();

            float queuePriority = 1f;
            QueueFamilyIndices queueFamilyIndices = _Context.PhysicalDevice.GetQueueFamilies();
            uint queueFamiliesCount = queueFamilyIndices.GetLength;
            DeviceQueueCreateInfo *deviceQueueCreateInfos = stackalloc DeviceQueueCreateInfo[2];

            for (int i = 0; i < queueFamiliesCount; i++)
            {
                Debug.Assert(queueFamilyIndices[i] != null);

                deviceQueueCreateInfos[i] = new DeviceQueueCreateInfo
                {
                    SType            = StructureType.DeviceQueueCreateInfo,
                    QueueFamilyIndex = queueFamilyIndices[i] !.Value,
                    QueueCount       = 1,
                    PQueuePriorities = &queuePriority
                };
            }

            nint extensionsPointer       = SilkMarshal.StringArrayToPtr(extensions);
            nint?validationLayersPointer = null;

            DeviceCreateInfo deviceCreateInfo = new DeviceCreateInfo
            {
                SType = StructureType.DeviceCreateInfo,
                EnabledExtensionCount   = (uint)extensions.Length,
                PpEnabledExtensionNames = (byte **)extensionsPointer,
                QueueCreateInfoCount    = queueFamiliesCount,
                PQueueCreateInfos       = deviceQueueCreateInfos,
                PEnabledFeatures        = (PhysicalDeviceFeatures *)null !,
                EnabledLayerCount       = 0,
                PpEnabledLayerNames     = null
            };

            if (validationLayers is not null)
            {
                validationLayersPointer              = SilkMarshal.StringArrayToPtr(validationLayers);
                deviceCreateInfo.EnabledLayerCount   = (uint)validationLayers.Length;
                deviceCreateInfo.PpEnabledLayerNames = (byte **)validationLayersPointer.Value;
            }

            Result result = VK.CreateDevice(_Context.PhysicalDevice, &deviceCreateInfo, (AllocationCallbacks *)null !, out _LogicalDevice);

            if (result is not Result.Success)
            {
                throw new VulkanException(result, "Failed to create logical device.");
            }

            VK.GetDeviceQueue(this, queueFamilyIndices.GraphicsFamily !.Value, 0, out _GraphicsQueue);
            VK.GetDeviceQueue(this, queueFamilyIndices.PresentationFamily !.Value, 0, out _PresentationQueue);

            SilkMarshal.Free(extensionsPointer);

            if (validationLayersPointer is not null)
            {
                SilkMarshal.Free(validationLayersPointer.Value);
            }
        }