public unsafe DepthImage(uint width, uint height, PhysicalDevice physicalDevice, Device device) { _width = width; _height = height; _vk = VkUtil.Vk; _physicalDevice = physicalDevice; _device = device; Format = FindFormat(FormatFeatureFlags.FormatFeatureDepthStencilAttachmentBit, Format.D32SfloatS8Uint, Format.D24UnormS8Uint); ImageCreateInfo imageCreateInfo = VkInit.ImageCreateInfo(ImageType.ImageType2D, Format, _width, _height); VkUtil.AssertVulkan(_vk.CreateImage(_device, imageCreateInfo, null, out _image)); _vk.GetImageMemoryRequirements(_device, _image, out MemoryRequirements memReqs); _vk.GetPhysicalDeviceMemoryProperties(_physicalDevice, out PhysicalDeviceMemoryProperties memoryProperties); uint index = VkUtil.FindMemoryTypeIndex(memReqs.MemoryTypeBits, MemoryPropertyFlags.MemoryPropertyDeviceLocalBit, memoryProperties); MemoryAllocateInfo memoryAllocateInfo = VkInit.MemoryAllocateInfo(memReqs.Size, index); VkUtil.AssertVulkan(_vk.AllocateMemory(_device, memoryAllocateInfo, null, out _memory)); VkUtil.AssertVulkan(_vk.BindImageMemory(_device, _image, _memory, 0)); ImageViewCreateInfo imageViewCreateInfo = VkInit.ImageViewCreateInfo(Format, _image, ImageAspectFlags.ImageAspectDepthBit); VkUtil.AssertVulkan(_vk.CreateImageView(_device, imageViewCreateInfo, null, out _imageView)); }
public unsafe MemoryAllocation Allocate(ulong size, ulong alignment, bool map) { // Ensure we have a sane alignment value. if ((ulong)(int)alignment != alignment || (int)alignment <= 0) { throw new ArgumentOutOfRangeException(nameof(alignment), $"Invalid alignment 0x{alignment:X}."); } for (int i = 0; i < _blocks.Count; i++) { var block = _blocks[i]; if (block.Mapped == map && block.Size >= size) { ulong offset = block.Allocate(size, alignment); if (offset != InvalidOffset) { return(new MemoryAllocation(this, block, block.Memory, GetHostPointer(block, offset), offset, size)); } } } ulong blockAlignedSize = BitUtils.AlignUp(size, _blockAlignment); var memoryAllocateInfo = new MemoryAllocateInfo() { SType = StructureType.MemoryAllocateInfo, AllocationSize = blockAlignedSize, MemoryTypeIndex = (uint)MemoryTypeIndex }; _api.AllocateMemory(_device, memoryAllocateInfo, null, out var deviceMemory).ThrowOnError(); IntPtr hostPointer = IntPtr.Zero; if (map) { unsafe { void *pointer = null; _api.MapMemory(_device, deviceMemory, 0, blockAlignedSize, 0, ref pointer).ThrowOnError(); hostPointer = (IntPtr)pointer; } } var newBlock = new Block(deviceMemory, hostPointer, blockAlignedSize); InsertBlock(newBlock); ulong newBlockOffset = newBlock.Allocate(size, alignment); Debug.Assert(newBlockOffset != InvalidOffset); return(new MemoryAllocation(this, newBlock, deviceMemory, GetHostPointer(newBlock, newBlockOffset), newBlockOffset, size)); }
private unsafe DeviceMemory BindImageMemory() { Device device = _renderer.Params.Device; AllocationCallbacks *allocator = (AllocationCallbacks *)_renderer.Params.AllocationCallbacks.ToPointer(); Vk vk = _renderer.Vk; vk.GetImageMemoryRequirements(device, _image, out MemoryRequirements memReqs); MemoryAllocateInfo memoryAllocateInfo = new() { SType = StructureType.MemoryAllocateInfo, AllocationSize = memReqs.Size, MemoryTypeIndex = _renderer.FindMemoryTypeIndex(memReqs.MemoryTypeBits, MemoryPropertyFlags.MemoryPropertyDeviceLocalBit) }; _renderer.AssertVulkan(vk.AllocateMemory(device, memoryAllocateInfo, allocator, out DeviceMemory memory)); _renderer.AssertVulkan(vk.BindImageMemory(device, _image, memory, 0)); return(memory); }