Esempio n. 1
0
        internal static IntPtr CopyStringArrays(List <IntPtr> allocatedItems, string[] array, out uint count)
        {
            if (array == null)
            {
                count = 0U;
                return(IntPtr.Zero);
            }

            int noOfElements = array.Length;

            var dest = IntPtr.Zero;

            //  EnabledLayerNames
            if (noOfElements > 0)
            {
                var POINTER_SIZE = Marshal.SizeOf(typeof(IntPtr));

                dest = Marshal.AllocHGlobal(POINTER_SIZE * noOfElements);
                allocatedItems.Add(dest);

                var names = new IntPtr[noOfElements];
                for (int i = 0; i < noOfElements; ++i)
                {
                    names[i] = VkInteropsUtility.NativeUtf8FromString(array[i]);
                    allocatedItems.Add(names[i]);
                }

                Marshal.Copy(names, 0, dest, noOfElements);
            }

            count = (uint)noOfElements;
            return(dest);
        }
Esempio n. 2
0
        static IntPtr ExtractSemaphores(List <IntPtr> attachedItems, IMgSemaphore[] semaphores, out uint semaphoreCount)
        {
            var  dest  = IntPtr.Zero;
            uint count = 0U;

            if (semaphores != null)
            {
                semaphoreCount = (uint)semaphores.Length;
                if (semaphoreCount > 0)
                {
                    dest = VkInteropsUtility.ExtractUInt64HandleArray(
                        semaphores,
                        (arg) =>
                    {
                        var bSemaphore = (VkSemaphore)arg;
                        Debug.Assert(bSemaphore != null);
                        return(bSemaphore.Handle);
                    }
                        );
                    attachedItems.Add(dest);
                }
            }
            semaphoreCount = count;
            return(dest);
        }
Esempio n. 3
0
        uint ExtractSwapchains(List <IntPtr> attachedItems, MgPresentInfoKHRImage[] images, out IntPtr swapchains, out IntPtr imageIndices)
        {
            var  pSwapchains   = IntPtr.Zero;
            var  pImageIndices = IntPtr.Zero;
            uint count         = 0U;

            if (images != null)
            {
                count = (uint)images.Length;
                if (count > 0)
                {
                    pSwapchains = VkInteropsUtility.ExtractUInt64HandleArray(
                        images,
                        (sc) =>
                    {
                        var bSwapchain = (VkSwapchainKHR)sc.Swapchain;
                        Debug.Assert(bSwapchain != null);
                        return(bSwapchain.Handle);
                    });
                    attachedItems.Add(pSwapchains);

                    pImageIndices = VkInteropsUtility.AllocateHGlobalArray(
                        images,
                        (sc) => { return(sc.ImageIndex); });
                    attachedItems.Add(pImageIndices);
                }
            }
            swapchains   = pSwapchains;
            imageIndices = pImageIndices;
            return(count);
        }
Esempio n. 4
0
        static IntPtr ExtractImageBinds(List <IntPtr> attachedItems, MgSparseImageMemoryBindInfo[] imageBinds, out uint imageBindCount)
        {
            var  dest  = IntPtr.Zero;
            uint count = 0U;

            if (imageBinds != null)
            {
                count = (uint)imageBinds.Length;
                if (count > 0)
                {
                    dest = VkInteropsUtility.AllocateNestedHGlobalArray(
                        attachedItems,
                        imageBinds,
                        (items, bind) =>
                    {
                        var bImage = (VkImage)bind.Image;
                        Debug.Assert(bImage != null);

                        Debug.Assert(bind.Binds != null);
                        var bindCount = (uint)bind.Binds.Length;

                        var pBinds = VkInteropsUtility.AllocateHGlobalArray(
                            bind.Binds,
                            (arg) =>
                        {
                            var bDeviceMemory = (VkDeviceMemory)arg.Memory;
                            Debug.Assert(bDeviceMemory != null);

                            return(new VkSparseImageMemoryBind
                            {
                                subresource = new VkImageSubresource
                                {
                                    aspectMask = (VkImageAspectFlags)arg.Subresource.AspectMask,
                                    arrayLayer = arg.Subresource.ArrayLayer,
                                    mipLevel = arg.Subresource.MipLevel,
                                },
                                offset = arg.Offset,
                                extent = arg.Extent,
                                memory = bDeviceMemory.Handle,
                                memoryOffset = arg.MemoryOffset,
                                flags = (VkSparseMemoryBindFlags)arg.Flags,
                            });
                        });
                        items.Add(pBinds);

                        return(new VkSparseImageMemoryBindInfo
                        {
                            image = bImage.Handle,
                            bindCount = bindCount,
                            pBinds = pBinds,
                        });
                    });

                    attachedItems.Add(dest);
                }
            }

            imageBindCount = count;
            return(dest);
        }
Esempio n. 5
0
        public Result EnumerateInstanceLayerProperties(out MgLayerProperties[] properties)
        {
            UInt32 pPropertyCount = 0;

            var first = Interops.vkEnumerateInstanceLayerProperties(ref pPropertyCount, null);

            if (first != Result.SUCCESS)
            {
                properties = null;
                return(first);
            }

            var layerProperties = new VkLayerProperties[pPropertyCount];
            var last            = Interops.vkEnumerateInstanceLayerProperties(ref pPropertyCount, layerProperties);

            properties = new MgLayerProperties[pPropertyCount];
            for (uint i = 0; i < pPropertyCount; ++i)
            {
                properties[i] = new MgLayerProperties
                {
                    LayerName             = VkInteropsUtility.ByteArrayToTrimmedString(layerProperties[i].layerName),
                    SpecVersion           = layerProperties[i].specVersion,
                    ImplementationVersion = layerProperties[i].implementationVersion,
                    Description           = VkInteropsUtility.ByteArrayToTrimmedString(layerProperties[i].description),
                };
            }
            return(last);
        }
Esempio n. 6
0
        static IntPtr ExtractBufferBinds(List <IntPtr> attachedItems, MgSparseBufferMemoryBindInfo[] bufferBinds, out uint bufferBindCount)
        {
            var  dest  = IntPtr.Zero;
            uint count = 0U;

            if (bufferBinds != null)
            {
                count = (uint)bufferBinds.Length;
                if (count > 0)
                {
                    dest = VkInteropsUtility.AllocateNestedHGlobalArray(
                        attachedItems,
                        bufferBinds,
                        (items, bind) =>
                    {
                        var bBuffer = (VkBuffer)bind.Buffer;
                        Debug.Assert(bBuffer != null);

                        Debug.Assert(bind.Binds != null);
                        var bindCount = (uint)bind.Binds.Length;
                        var pBinds    = VkInteropsUtility.AllocateHGlobalArray(
                            bind.Binds,
                            (arg) =>
                        {
                            var bDeviceMemory = (VkDeviceMemory)arg.Memory;
                            Debug.Assert(bDeviceMemory != null);

                            return(new VkSparseMemoryBind
                            {
                                resourceOffset = arg.ResourceOffset,
                                size = arg.Size,
                                memory = bDeviceMemory.Handle,
                                memoryOffset = arg.MemoryOffset,
                                flags = (VkSparseMemoryBindFlags)arg.Flags,
                            });
                        });
                        items.Add(pBinds);

                        return(new VkSparseBufferMemoryBindInfo
                        {
                            buffer = bBuffer.Handle,
                            bindCount = bindCount,
                            pBinds = pBinds,
                        });
                    }
                        );
                    attachedItems.Add(dest);
                }
            }

            bufferBindCount = count;
            return(dest);
        }
Esempio n. 7
0
 static IntPtr CopySingleString(List <IntPtr> allocatedItems, string name)
 {
     if (!string.IsNullOrWhiteSpace(name))
     {
         var dest = VkInteropsUtility.NativeUtf8FromString(name);
         allocatedItems.Add(dest);
         return(dest);
     }
     else
     {
         return(IntPtr.Zero);
     }
 }
Esempio n. 8
0
        public Result EnumerateInstanceExtensionProperties(string layerName, out MgExtensionProperties[] pProperties)
        {
            var pLayerName = IntPtr.Zero;

            try
            {
                if (!string.IsNullOrWhiteSpace(layerName))
                {
                    pLayerName = VkInteropsUtility.NativeUtf8FromString(layerName);
                }

                UInt32 pPropertyCount = 0;
                var    first          = Interops.vkEnumerateInstanceExtensionProperties(pLayerName, ref pPropertyCount, null);

                if (first != Result.SUCCESS)
                {
                    pProperties = null;
                    return(first);
                }

                var extensionProperties = new VkExtensionProperties[pPropertyCount];
                var last = Interops.vkEnumerateInstanceExtensionProperties(pLayerName, ref pPropertyCount, extensionProperties);

                pProperties = new MgExtensionProperties[pPropertyCount];
                for (uint i = 0; i < pPropertyCount; ++i)
                {
                    pProperties[i] = new MgExtensionProperties
                    {
                        ExtensionName = VkInteropsUtility.ByteArrayToTrimmedString(extensionProperties[i].extensionName),
                        SpecVersion   = extensionProperties[i].specVersion,
                    };
                }
                return(last);
            }
            finally
            {
                if (pLayerName != IntPtr.Zero)
                {
                }
            }
        }
Esempio n. 9
0
        public Result QueueSubmit(MgSubmitInfo[] pSubmits, IMgFence fence)
        {
            var bFence    = (VkFence)fence;
            var bFencePtr = bFence != null ? bFence.Handle : 0UL;

            var attachedItems = new List <IntPtr>();

            try
            {
                unsafe
                {
                    uint submitCount = 0;

                    if (pSubmits != null)
                    {
                        submitCount = (uint)pSubmits.Length;
                    }

                    var submissions = stackalloc VkSubmitInfo[(int)submitCount];

                    for (var i = 0; i < submitCount; ++i)
                    {
                        var currentInfo = pSubmits[i];

                        var waitSemaphoreCount = 0U;
                        var pWaitSemaphores    = IntPtr.Zero;
                        var pWaitDstStageMask  = IntPtr.Zero;

                        if (currentInfo.WaitSemaphores != null)
                        {
                            waitSemaphoreCount = (uint)currentInfo.WaitSemaphores.Length;
                            if (waitSemaphoreCount > 0)
                            {
                                pWaitSemaphores = VkInteropsUtility.ExtractUInt64HandleArray(
                                    currentInfo.WaitSemaphores,
                                    (arg) =>
                                {
                                    var bSemaphore = (VkSemaphore)arg.WaitSemaphore;
                                    Debug.Assert(bSemaphore != null);
                                    return(bSemaphore.Handle);
                                });
                                attachedItems.Add(pWaitSemaphores);

                                pWaitDstStageMask = VkInteropsUtility.AllocateHGlobalArray <MgSubmitInfoWaitSemaphoreInfo, uint>(
                                    currentInfo.WaitSemaphores,
                                    (arg) => { return((uint)arg.WaitDstStageMask); });
                                attachedItems.Add(pWaitDstStageMask);
                            }
                        }

                        var commandBufferCount = 0U;
                        var pCommandBuffers    = IntPtr.Zero;

                        if (currentInfo.CommandBuffers != null)
                        {
                            commandBufferCount = (uint)currentInfo.CommandBuffers.Length;
                            if (commandBufferCount > 0)
                            {
                                pCommandBuffers = VkInteropsUtility.ExtractIntPtrHandleArray
                                                  (
                                    currentInfo.CommandBuffers,
                                    (arg) =>
                                {
                                    var bCommandBuffer = (VkCommandBuffer)arg;
                                    Debug.Assert(bCommandBuffer != null);
                                    return(bCommandBuffer.Handle);
                                }
                                                  );
                                attachedItems.Add(pCommandBuffers);
                            }
                        }

                        var signalSemaphoreCount = 0U;
                        var pSignalSemaphores    = IntPtr.Zero;

                        if (currentInfo.SignalSemaphores != null)
                        {
                            signalSemaphoreCount = (uint)currentInfo.SignalSemaphores.Length;

                            if (signalSemaphoreCount > 0)
                            {
                                pSignalSemaphores = VkInteropsUtility.ExtractUInt64HandleArray(
                                    currentInfo.SignalSemaphores,
                                    (arg) =>
                                {
                                    var bSemaphore = (VkSemaphore)arg;
                                    Debug.Assert(bSemaphore != null);
                                    return(bSemaphore.Handle);
                                });
                                attachedItems.Add(pSignalSemaphores);
                            }
                        }

                        submissions[i] = new VkSubmitInfo
                        {
                            sType = VkStructureType.StructureTypeSubmitInfo,
                            pNext = IntPtr.Zero,
                            waitSemaphoreCount   = waitSemaphoreCount,
                            pWaitSemaphores      = pWaitSemaphores,
                            pWaitDstStageMask    = pWaitDstStageMask,
                            commandBufferCount   = commandBufferCount,
                            pCommandBuffers      = pCommandBuffers,
                            signalSemaphoreCount = signalSemaphoreCount,
                            pSignalSemaphores    = pSignalSemaphores,
                        };
                    }

                    return(Interops.vkQueueSubmit(Handle, submitCount, submitCount > 0  ? submissions : null, bFencePtr));
                }
            }
            finally
            {
                foreach (var item in attachedItems)
                {
                    Marshal.FreeHGlobal(item);
                }
            }
        }
Esempio n. 10
0
        public Result CreateInstance(MgInstanceCreateInfo createInfo, IMgAllocationCallbacks allocator, out IMgInstance instance)
        {
            var allocatedItems = new List <IntPtr>();

            try
            {
                uint enabledLayerCount;
                var  ppEnabledLayerNames = VkInteropsUtility.CopyStringArrays(allocatedItems, createInfo.EnabledLayerNames, out enabledLayerCount);

                uint enabledExtensionCount;
                var  ppEnabledExtensionNames = VkInteropsUtility.CopyStringArrays(allocatedItems, createInfo.EnabledExtensionNames, out enabledExtensionCount);

                var pApplicationInfo = IntPtr.Zero;
                if (createInfo.ApplicationInfo != null)
                {
                    var acInfo = new VkApplicationInfo
                    {
                        sType              = VkStructureType.StructureTypeApplicationInfo,
                        pNext              = IntPtr.Zero,
                        apiVersion         = createInfo.ApplicationInfo.ApiVersion,
                        applicationVersion = createInfo.ApplicationInfo.ApplicationVersion,
                        engineVersion      = createInfo.ApplicationInfo.EngineVersion,
                        pApplicationName   = CopySingleString(allocatedItems, createInfo.ApplicationInfo.ApplicationName),
                        pEngineName        = CopySingleString(allocatedItems, createInfo.ApplicationInfo.EngineName),
                    };

                    var destPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VkApplicationInfo)));
                    allocatedItems.Add(destPtr);

                    Marshal.StructureToPtr(acInfo, destPtr, false);

                    pApplicationInfo = destPtr;
                }

                var ici = new VkInstanceCreateInfo
                {
                    sType                   = VkStructureType.StructureTypeInstanceCreateInfo,
                    pNext                   = IntPtr.Zero,
                    enabledLayerCount       = enabledLayerCount,
                    ppEnabledLayerNames     = ppEnabledLayerNames,
                    enabledExtensionCount   = enabledExtensionCount,
                    ppEnabledExtensionNames = ppEnabledExtensionNames,
                    pApplicationInfo        = pApplicationInfo,
                };

                var    bAllocator   = allocator as MgVkAllocationCallbacks;
                IntPtr allocatorPtr = bAllocator != null ? bAllocator.Handle : IntPtr.Zero;

                IntPtr handle = IntPtr.Zero;
                var    result = Interops.vkCreateInstance(ref ici, allocatorPtr, ref handle);

                instance = new VkInstance(handle);

                return((Result)result);
            }
            finally
            {
                //foreach (var mem in allocatedItems)
                //{
                //	Marshal.FreeHGlobal(mem);
                //}
            }
        }