Beispiel #1
0
        public static void vkUnmapMemory(VkDevice device, VkDeviceMemory memory)
        {
            VkPreconditions.CheckNull(device, nameof(device));
            VkPreconditions.CheckNull(memory, nameof(memory));

            GetDevice(device).UnmapMemory(memory);
        }
Beispiel #2
0
        public static VkResult vkMapMemory(VkDevice device, VkDeviceMemory memory, int offset, int size, int memoryMapFlags, out byte[] ppData)
        {
            VkPreconditions.CheckNull(device, nameof(device));
            VkPreconditions.CheckNull(memory, nameof(memory));

            return(GetDevice(device).MapMemory(memory, offset, size, memoryMapFlags, out ppData));
        }
 public VkMemoryBlock(VkDeviceMemory memory, ulong offset, ulong size, uint memoryTypeIndex)
 {
     DeviceMemory    = memory;
     Offset          = offset;
     Size            = size;
     MemoryTypeIndex = memoryTypeIndex;
 }
Beispiel #4
0
        public static IntPtr MapMemory(VkDeviceMemory memory, ulong offset, ulong size, VkMemoryMapFlags flags)
        {
            void *mappedLocal;

            vkMapMemory(device, memory, offset, size, flags, &mappedLocal);
            return((IntPtr)mappedLocal);
        }
Beispiel #5
0
        internal static unsafe void AllocatePool(out VkDeviceMemory NativeMemory, ref VkDevice device, ref VkPhysicalDevice pDevice, ref VkMemoryPropertyFlags memoryProperties, ref VkMemoryRequirements memoryRequirements, ulong size)
        {
            var allocateInfo = new VkMemoryAllocateInfo
            {
                sType          = VkStructureType.MemoryAllocateInfo,
                allocationSize = size,
            };

            Vortice.Vulkan.Vulkan.vkGetPhysicalDeviceMemoryProperties(pDevice, out var physicalDeviceMemoryProperties);

            var typeBits = memoryRequirements.memoryTypeBits;

            for (uint i = 0; i < physicalDeviceMemoryProperties.memoryTypeCount; i++)
            {
                if ((typeBits & 1) == 1)
                {
                    // Type is available, does it match user properties?
                    var memoryType = *(&physicalDeviceMemoryProperties.memoryTypes_0 + i);
                    if ((memoryType.propertyFlags & memoryProperties) == memoryProperties)
                    {
                        allocateInfo.memoryTypeIndex = i;
                        break;
                    }
                }
                typeBits >>= 1;
            }

            NativeMemory = new VkDeviceMemory();

            Vortice.Vulkan.Vulkan.vkAllocateMemory(device, &allocateInfo, null, out NativeMemory);
        }
        private unsafe void ReleaseDevice()
        {
            EmptyTexelBufferInt.Dispose();
            EmptyTexelBufferInt = null;
            EmptyTexelBufferFloat.Dispose();
            EmptyTexelBufferFloat = null;

            EmptyTexture.Dispose();
            EmptyTexture = null;

            // Wait for all queues to be idle
            vkDeviceWaitIdle(nativeDevice);

            // Destroy all remaining fences
            GetCompletedValue();

            // Mark upload buffer for destruction
            if (nativeUploadBuffer != VkBuffer.Null)
            {
                vkUnmapMemory(NativeDevice, nativeUploadBufferMemory);
                nativeResourceCollector.Add(lastCompletedFence, nativeUploadBuffer);
                nativeResourceCollector.Add(lastCompletedFence, nativeUploadBufferMemory);

                nativeUploadBuffer       = VkBuffer.Null;
                nativeUploadBufferMemory = VkDeviceMemory.Null;
            }

            // Release fenced resources
            nativeResourceCollector.Dispose();
            DescriptorPools.Dispose();

            vkDestroyCommandPool(nativeDevice, NativeCopyCommandPool, null);
            vkDestroyDevice(nativeDevice, null);
        }
 private void DisposeVulkanDeviceMemory(VkDeviceMemory vulkanDeviceMemory)
 {
     if (vulkanDeviceMemory != VK_NULL_HANDLE)
     {
         vkFreeMemory(Collection.Allocator.Device.VulkanDevice, vulkanDeviceMemory, pAllocator: null);
     }
 }
Beispiel #8
0
        void CreateImage(int width, int height,
                         VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
                         out VkImage image, out VkDeviceMemory memory)
        {
            var info = new VkImageCreateInfo();

            info.imageType     = VkImageType._2D;
            info.extent.width  = width;
            info.extent.height = height;
            info.extent.depth  = 1;
            info.mipLevels     = 1;
            info.arrayLayers   = 1;
            info.format        = format;
            info.tiling        = tiling;
            info.initialLayout = VkImageLayout.Undefined;
            info.usage         = usage;
            info.sharingMode   = VkSharingMode.Exclusive;
            info.samples       = VkSampleCountFlags._1_Bit;

            image = new VkImage(device, info);

            var req = image.Requirements;

            var allocInfo = new VkMemoryAllocateInfo();

            allocInfo.allocationSize  = req.size;
            allocInfo.memoryTypeIndex = FindMemoryType(req.memoryTypeBits, properties);

            memory = new VkDeviceMemory(device, allocInfo);

            image.Bind(memory, 0);
        }
Beispiel #9
0
 private VulkanBuffer(VkDevice device, VkCommandPool commandPool, VkBuffer buffer, VkDeviceMemory memory, int count)
 {
     Buffer           = buffer;
     Memory           = memory;
     Count            = count;
     this.device      = device;
     this.commandPool = commandPool;
 }
Beispiel #10
0
        public static VkResult vkBindImageMemory(VkDevice device, VkImage image, VkDeviceMemory memory, int memoryOffset)
        {
            VkPreconditions.CheckNull(device, nameof(device));
            VkPreconditions.CheckNull(image, nameof(image));
            VkPreconditions.CheckNull(memory, nameof(memory));

            return(GetDevice(device).BindImageMemory(image, memory, memoryOffset));
        }
Beispiel #11
0
 private VulkanImage(VulkanContext ctx, VkImage image, VkDeviceMemory memory, VkImageView view, VkFormat format)
 {
     Image    = image;
     Memory   = memory;
     View     = view;
     Format   = format;
     this.ctx = ctx;
 }
Beispiel #12
0
 public MemoryAllocation(VkDeviceMemory handle, ulong off, ulong size,
                         VkMemoryPropertyFlags flags)
 {
     Handle = handle;
     Offset = off;
     Size   = size;
     Flags  = flags;
 }
Beispiel #13
0
 public VkMemoryBlock(VkDeviceMemory memory, ulong offset, ulong size, uint memoryTypeIndex, void *mappedPtr)
 {
     DeviceMemory      = memory;
     Offset            = offset;
     Size              = size;
     MemoryTypeIndex   = memoryTypeIndex;
     BaseMappedPointer = mappedPtr;
 }
Beispiel #14
0
 public static extern VkResult MapMemory(
     VkDevice device,
     VkDeviceMemory memory,
     ulong offset,
     ulong size,
     VkMemoryMapFlags flags,
     out IntPtr ppData
     );
Beispiel #15
0
        public static VkResult vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, int memoryOffset)
        {
            VkPreconditions.CheckNull(device, nameof(device));
            VkPreconditions.CheckNull(buffer, nameof(buffer));
            VkPreconditions.CheckNull(memory, nameof(memory));
            VkPreconditions.CheckRange(memoryOffset, 0, int.MaxValue, nameof(memoryOffset));

            return(GetDevice(device).BindBufferMemory(buffer, memory, memoryOffset));
        }
Beispiel #16
0
        public override void FreeMemory(VkDeviceMemory memory)
        {
            SoftwareDeviceMemory mem = (SoftwareDeviceMemory)memory;

            if (mem != null)
            {
                m_DeviceMemory.Remove(mem);
                mem.Destroy();
            }
        }
Beispiel #17
0
        public override VkResult AllocateMemory(VkMemoryAllocateInfo pAllocateInfo, out VkDeviceMemory pMemory)
        {
            VkPreconditions.CheckRange(pAllocateInfo.allocationSize, 1, int.MaxValue, nameof(pAllocateInfo.allocationSize));
            VkPreconditions.CheckRange(pAllocateInfo.memoryTypeIndex != 0, nameof(pAllocateInfo.memoryTypeIndex));

            var ret = new SoftwareDeviceMemory(this, pAllocateInfo);

            m_DeviceMemory.Add(ret);
            pMemory = ret;
            return(VkResult.VK_SUCCESS);
        }
 internal VulkanMemorySlice(VulkanMemoryPool originPool, ulong size, ulong offset, ulong alignOffset, VkDeviceMemory vkDeviceMemory, uint memoryTypeIndex, VkMemoryPropertyFlags memoryPropertyFlags)
 {
     this.originPool          = originPool;
     this.memoryTypeIndex     = memoryTypeIndex;
     this.memoryPropertyFlags = memoryPropertyFlags;
     this.vkDeviceMemory      = vkDeviceMemory;
     this.size        = size;
     this.offset      = offset;
     this.alignOffset = alignOffset;
     disposed         = false;
     hostVisible      = (memoryPropertyFlags & VkMemoryPropertyFlags.HostVisible) != 0;
 }
Beispiel #19
0
        public void FlushMappedMemoryRanges()
        {
            // Flush to make changes visible to the host
            VkMappedMemoryRange memory_range = new()
            {
                sType  = VkStructureType.MappedMemoryRange,
                memory = memory,
                size   = (ulong)SizeInBytes,
            };

            vkFlushMappedMemoryRanges(NativeDevice.handle, 1, &memory_range).CheckResult();
        }
 public unsafe VkMappedMemoryRange(
     VkDeviceMemory memory,
     ulong offset = 0,
     ulong size   = Vulkan.WholeSize,
     void *pNext  = default)
 {
     sType       = VkStructureType.MappedMemoryRange;
     this.pNext  = pNext;
     this.memory = memory;
     this.offset = offset;
     this.size   = size;
 }
Beispiel #21
0
        public static Span <T> vkMapMemory <T>(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, ulong offset = 0, ulong size = WholeSize, VkMemoryMapFlags flags = VkMemoryMapFlags.None) where T : unmanaged
        {
            void *pData;

            vkMapMemory(device, memory, offset, size, flags, &pData).CheckResult();

            if (size == WholeSize)
            {
                vkGetBufferMemoryRequirements(device, buffer, out var memoryRequirements);
                return(new Span <T>(pData, (int)memoryRequirements.size));
            }

            return(new Span <T>(pData, (int)size));
        }
 public VkMemoryBlock(
     VkDeviceMemory memory,
     ulong offset,
     ulong size,
     uint memoryTypeIndex,
     void *mappedPtr,
     bool dedicatedAllocation)
 {
     DeviceMemory        = memory;
     Offset              = offset;
     Size                = size;
     MemoryTypeIndex     = memoryTypeIndex;
     BaseMappedPointer   = mappedPtr;
     DedicatedAllocation = dedicatedAllocation;
 }
Beispiel #23
0
        public static Span <T> vkMapMemory <T>(VkDevice device, VkImage image, VkDeviceMemory memory, ulong offset = 0, ulong size = WholeSize, VkMemoryMapFlags flags = VkMemoryMapFlags.None) where T : unmanaged
        {
            void *pData;

            vkMapMemory(device, memory, offset, size, flags, &pData).CheckResult();

            if (size == WholeSize)
            {
                vkGetImageMemoryRequirements(device, image, out VkMemoryRequirements memoryRequirements);
                size = memoryRequirements.size;
            }

            int oneItemSize = sizeof(T);
            int spanLength  = (int)size / oneItemSize;

            return(new Span <T>(pData, spanLength));
        }
Beispiel #24
0
        void CreateBuffer(long size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, out VkBuffer buffer, out VkDeviceMemory memory)
        {
            var info = new VkBufferCreateInfo();

            info.size        = size;
            info.usage       = usage;
            info.sharingMode = VkSharingMode.Exclusive;

            buffer = new VkBuffer(device, info);

            var allocInfo = new VkMemoryAllocateInfo();

            allocInfo.allocationSize  = buffer.Requirements.size;
            allocInfo.memoryTypeIndex = FindMemoryType(buffer.Requirements.memoryTypeBits, properties);

            memory = new VkDeviceMemory(device, allocInfo);
            buffer.Bind(memory, 0);
        }
Beispiel #25
0
        private void CreateDepthImage(VkPhysicalDevice physicalDevice, uint width, uint height)
        {
            VkImageCreateInfo createInfo = VkImageCreateInfo.New();

            createInfo.imageType     = VkImageType.Image2D;
            createInfo.extent.width  = width;
            createInfo.extent.height = height;
            createInfo.extent.depth  = 1;
            createInfo.mipLevels     = 1;
            createInfo.arrayLayers   = 1;
            createInfo.format        = VkFormat.D32Sfloat;
            createInfo.tiling        = VkImageTiling.Optimal;
            createInfo.initialLayout = VkImageLayout.Undefined;
            createInfo.usage         = VkImageUsageFlags.DepthStencilAttachment;
            createInfo.sharingMode   = VkSharingMode.Exclusive;
            createInfo.samples       = VkSampleCountFlags.Count1;

            VkImage depthImage;

            Assert(vkCreateImage(device, &createInfo, null, &depthImage));

            VkMemoryRequirements memoryRequirements;

            vkGetImageMemoryRequirements(device, depthImage, &memoryRequirements);

            VkPhysicalDeviceMemoryProperties memoryProperties = new VkPhysicalDeviceMemoryProperties();

            vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);

            VkMemoryAllocateInfo allocateInfo = VkMemoryAllocateInfo.New();

            allocateInfo.allocationSize  = memoryRequirements.size;
            allocateInfo.memoryTypeIndex = FDataBuffer <byte> .SelectMemoryType(memoryProperties, memoryRequirements.memoryTypeBits,
                                                                                VkMemoryPropertyFlags.DeviceLocal);

            VkDeviceMemory depthImageMemory;

            Assert(vkAllocateMemory(device, &allocateInfo, null, &depthImageMemory));

            vkBindImageMemory(device, depthImage, depthImageMemory, 0);

            this.depthImage       = depthImage;
            this.depthImageMemory = depthImageMemory;
        }
Beispiel #26
0
        public void UpdateSize()
        {
            if (width == originalWidth)
            {
                width  = originalWidth / 2;
                height = originalHeight / 2;
            }
            else
            {
                width  = originalWidth;
                height = originalHeight;
            }
            var      uniformBufferData = new AreaUniformBuffer(width, height);
            var      length            = 1;
            var      size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(AreaUniformBuffer)) * length;
            VkDevice device = this.device; VkDeviceMemory deviceMemory = this.vkDeviceMemory;
            {
                IntPtr memPtr = IntPtr.Zero;
                vkAPI.vkMapMemory(device, deviceMemory, 0, (UInt64)size, 0, &memPtr).Check();

                System.Runtime.InteropServices.Marshal.StructureToPtr(uniformBufferData, memPtr, false);
                //GCHandle pin = GCHandle.Alloc(uniformBufferData, GCHandleType.Pinned);
                //IntPtr address = pin.AddrOfPinnedObject();
                //var src = (byte*)address;
                //var dst = (byte*)memPtr;
                //for (int i = 0; i < size; i++) {
                //    dst[i] = src[i];
                //}
                //pin.Free();

                //// Flush to make changes visible to the host
                //var memoryRange = new VkMappedMemoryRange();
                //{
                //    memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
                //    memoryRange.memory = deviceMemory;
                //    memoryRange.size = this.uniformSize;
                //}

                //vkAPI.vkFlushMappedMemoryRanges(device, 1, &memoryRange);
                vkAPI.vkUnmapMemory(device, deviceMemory);
            }
        }
Beispiel #27
0
        public FDataBuffer(VkDevice device, VkPhysicalDevice physicalDevice, int length, VkBufferUsageFlags usage, VkSharingMode sharingMode)
        {
            this.device = device;

            VkBufferCreateInfo createInfo = VkBufferCreateInfo.New();

            size                   = (ulong)(sizeof(T) * length);
            createInfo.size        = size;
            createInfo.usage       = usage;
            createInfo.sharingMode = sharingMode;

            VkBuffer buffer = VkBuffer.Null;

            Assert(vkCreateBuffer(device, &createInfo, null, &buffer));
            this.Buffer = buffer;

            VkMemoryRequirements memoryRequirements = new VkMemoryRequirements();

            vkGetBufferMemoryRequirements(device, this.Buffer, &memoryRequirements);

            VkPhysicalDeviceMemoryProperties memoryProperties = new VkPhysicalDeviceMemoryProperties();

            vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);

            VkMemoryAllocateInfo allocateInfo = VkMemoryAllocateInfo.New();

            allocateInfo.allocationSize  = memoryRequirements.size;
            allocateInfo.memoryTypeIndex = SelectMemoryType(memoryProperties, memoryRequirements.memoryTypeBits,
                                                            VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent);

            VkDeviceMemory memory = new VkDeviceMemory();

            Assert(vkAllocateMemory(device, &allocateInfo, null, &memory));
            Memory = memory;

            Assert(vkBindBufferMemory(device, this.Buffer, memory, 0));

            spanLength = length;
        }
Beispiel #28
0
        VkBuffer CreateBuffer(VkPhysicalDevice physicalDevice, VkDevice device, object values, VkBufferUsageFlagBits usageFlags, System.Type type)
        {
            var      array  = values as System.Array;
            var      length = (array != null) ? array.Length : 1;
            var      size   = System.Runtime.InteropServices.Marshal.SizeOf(type) * length;
            VkBuffer buffer;
            {
                UInt32 index = 0;
                var    info  = new VkBufferCreateInfo {
                    sType = VkStructureType.BufferCreateInfo
                };
                info.size               = (UInt64)size;
                info.usage              = usageFlags;
                info.sharingMode        = VkSharingMode.Exclusive;
                info.queueFamilyIndices = index;
                //VkBuffer buffer = device.CreateBuffer(ref info);
                vkAPI.vkCreateBuffer(device, &info, null, &buffer).Check();
                info.Free();
            }
            VkDeviceMemory       deviceMemory; // = device.AllocateMemory(ref allocInfo);
            VkMemoryRequirements memoryReq;

            //VkMemoryRequirements memoryReq = device.GetBufferMemoryRequirements(buffer);
            vkAPI.vkGetBufferMemoryRequirements(device, buffer, &memoryReq);
            var allocInfo = new VkMemoryAllocateInfo {
                sType = VkStructureType.MemoryAllocateInfo
            };

            {
                allocInfo.allocationSize = memoryReq.size;
                VkPhysicalDeviceMemoryProperties memoryProperties;
                vkAPI.vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memoryProperties);
                allocInfo.memoryTypeIndex = GetMemoryTypeIndex(memoryProperties, memoryReq,
                                                               VkMemoryPropertyFlagBits.HostVisible | VkMemoryPropertyFlagBits.HostCoherent);
                vkAPI.vkAllocateMemory(device, &allocInfo, null, &deviceMemory).Check();
            }
            {
                IntPtr memPtr = IntPtr.Zero;
                vkAPI.vkMapMemory(device, deviceMemory, 0, (UInt64)size, 0, &memPtr).Check();

                if (type == typeof(float))
                {
                    System.Runtime.InteropServices.Marshal.Copy(values as float[], 0, memPtr, length);
                }
                else if (type == typeof(short))
                {
                    System.Runtime.InteropServices.Marshal.Copy(values as short[], 0, memPtr, length);
                }
                else if (type == typeof(AreaUniformBuffer))
                {
                    System.Runtime.InteropServices.Marshal.StructureToPtr(values, memPtr, false);
                    this.vkBuffer       = buffer;
                    this.vkDeviceMemory = deviceMemory;
                    this.uniformSize    = memoryReq.size;
                }

                vkAPI.vkUnmapMemory(device, deviceMemory);
            }
            vkAPI.vkBindBufferMemory(device, buffer, deviceMemory, 0).Check();

            return(buffer);
        }
Beispiel #29
0
        public static void vkFreeMemory(VkDevice device, VkDeviceMemory memory, VkAllocationCallbacks pAllocator)
        {
            VkPreconditions.CheckNull(device, nameof(device));

            GetDevice(device).FreeMemory(memory);
        }
Beispiel #30
0
        public static VkResult vkAllocateMemory(VkDevice device, VkMemoryAllocateInfo pAllocateInfo, VkAllocationCallbacks pAllocator, out VkDeviceMemory pMemory)
        {
            VkPreconditions.CheckNull(device, nameof(device));

            return(GetDevice(device).AllocateMemory(pAllocateInfo, out pMemory));
        }