public static BufferWithMemory CreateBuffer(VkContext vulkan, Vk.DeviceSize size, Vk.BufferUsageFlags usage, Vk.MemoryPropertyFlags memoryProps, Vk.SharingMode sharingMode) { var bufferInfo = new Vk.BufferCreateInfo(); bufferInfo.Size = size; bufferInfo.Usage = usage; bufferInfo.SharingMode = sharingMode; var container = new BufferWithMemory(); container.Buffer = vulkan.Device.CreateBuffer(bufferInfo); var memoryReqs = vulkan.Device.GetBufferMemoryRequirements(container.Buffer); var allocInfo = new Vk.MemoryAllocateInfo(); allocInfo.AllocationSize = memoryReqs.Size; allocInfo.MemoryTypeIndex = FindMemoryType(memoryReqs.MemoryTypeBits, vulkan.PhysicalDevice, memoryProps); container.Memory = vulkan.Device.AllocateMemory(allocInfo); container.Bind(vulkan.Device, 0); return(container); }
public static uint FindMemoryType(uint typeFilter, Vk.PhysicalDevice physicalDevice, Vk.MemoryPropertyFlags properties) { var memProps = physicalDevice.GetMemoryProperties(); for (int i = 0; i < memProps.MemoryTypeCount; i++) { bool bitFieldSet = (typeFilter & (1 << i)) != 0; bool hasProperties = (memProps.MemoryTypes[i].PropertyFlags & properties) == properties; if (bitFieldSet && hasProperties) { return((uint)i); } } throw new System.Exception("Failed to find suitable memory type!"); }
public static ImageWithMemory CreateImage(VkContext state, uint width, uint height, Vk.Format format, Vk.ImageTiling tiling, Vk.ImageUsageFlags usageFlags, Vk.MemoryPropertyFlags props) { var extent = new Vk.Extent3D(); extent.Width = width; extent.Height = height; extent.Depth = 1; var imageInfo = new Vk.ImageCreateInfo(); imageInfo.ImageType = Vk.ImageType.Image2D; imageInfo.Extent = extent; imageInfo.MipLevels = 1; imageInfo.ArrayLayers = 1; imageInfo.Format = format; imageInfo.Tiling = tiling; imageInfo.InitialLayout = Vk.ImageLayout.Undefined; imageInfo.Usage = usageFlags; imageInfo.Samples = Vk.SampleCountFlags.Count1; imageInfo.SharingMode = Vk.SharingMode.Exclusive; var image = new ImageWithMemory(); image.Image = state.Device.CreateImage(imageInfo); var memoryReqs = state.Device.GetImageMemoryRequirements(image.Image); var allocInfo = new Vk.MemoryAllocateInfo(); allocInfo.AllocationSize = memoryReqs.Size; allocInfo.MemoryTypeIndex = FindMemoryType(memoryReqs.MemoryTypeBits, state.PhysicalDevice, props); image.Memory = state.Device.AllocateMemory(allocInfo); image.Bind(state.Device, 0); return(image); }