1 module sbylib.wrapper.vulkan.devicememory; 2 3 import erupted; 4 import sbylib.wrapper.vulkan.buffer; 5 import sbylib.wrapper.vulkan.device; 6 import sbylib.wrapper.vulkan.enums; 7 import sbylib.wrapper.vulkan.image; 8 import sbylib.wrapper.vulkan.util; 9 10 class DeviceMemory { 11 static struct AllocateInfo { 12 @vkProp() { 13 VkDeviceSize allocationSize; 14 uint32_t memoryTypeIndex; 15 } 16 17 const mixin VkTo!(VkMemoryAllocateInfo); 18 } 19 20 private Device device; 21 private VkDeviceMemory deviceMemory; 22 23 mixin ImplNameSetter!(device, deviceMemory, DebugReportObjectType.DeviceMemory); 24 25 this(Device device, AllocateInfo _info) { 26 this.device = device; 27 28 auto info = _info.vkTo(); 29 30 enforceVK(vkAllocateMemory(device.device, &info, null, &deviceMemory)); 31 } 32 33 ~this() { 34 vkFreeMemory(device.device, deviceMemory, null); 35 } 36 37 ubyte* mapWhole(VkDeviceSize offset, VkMemoryMapFlags flags) { 38 ubyte* pData; 39 enforceVK(vkMapMemory(device.device, deviceMemory, offset, VK_WHOLE_SIZE, flags, 40 cast(void**)(&pData))); 41 return pData; 42 } 43 44 ubyte[] map(VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags) { 45 ubyte* pData; 46 enforceVK(vkMapMemory(device.device, deviceMemory, offset, size, flags, 47 cast(void**)(&pData))); 48 return pData[0 .. size]; 49 } 50 51 void unmap() { 52 vkUnmapMemory(device.device, deviceMemory); 53 } 54 55 void bindBuffer(Buffer buffer, VkDeviceSize memoryOffset) { 56 enforceVK(vkBindBufferMemory(device.device, buffer.buffer, deviceMemory, memoryOffset)); 57 } 58 59 void bindImage(Image image, VkDeviceSize memoryOffset) { 60 enforceVK(vkBindImageMemory(device.device, image.image, deviceMemory, memoryOffset)); 61 } 62 }