1 module sbylib.wrapper.vulkan.buffer;
2 
3 import std;
4 import erupted;
5 import sbylib.wrapper.vulkan.device;
6 import sbylib.wrapper.vulkan.devicememory;
7 import sbylib.wrapper.vulkan.enums;
8 import sbylib.wrapper.vulkan.physicaldevice;
9 import sbylib.wrapper.vulkan.memoryproperties;
10 import sbylib.wrapper.vulkan.util;
11 
12 class Buffer {
13     static struct CreateInfo {
14         @vkProp() {
15             immutable VkBufferCreateFlags flags;
16             immutable VkDeviceSize size;
17             immutable BufferUsage usage;
18             immutable SharingMode sharingMode;
19         }
20 
21         @vkProp("pQueueFamilyIndices", "queueFamilyIndexCount") {
22             uint[] queueFamilyIndices;
23         }
24 
25         const mixin VkTo!(VkBufferCreateInfo);
26     }
27 
28     private Device device;
29     package VkBuffer buffer;
30 
31     mixin ImplNameSetter!(device, buffer, DebugReportObjectType.Buffer);
32 
33     this(Device device, CreateInfo _info) {
34         import std.exception : enforce;
35 
36         this.device = device;
37 
38         auto info = _info.vkTo();
39 
40         enforceVK(vkCreateBuffer(device.device, &info, null, &buffer));
41         enforce(buffer != VK_NULL_HANDLE);
42     }
43 
44     ~this() {
45         vkDestroyBuffer(device.device, buffer, null);
46     }
47 
48     mixin VkTo!(VkBuffer);
49 
50     DeviceMemory allocateMemory(PhysicalDevice gpu, MemoryProperties.MemoryType.Flags memoryTypeFlag) {
51         const requirements = device.getBufferMemoryRequirements(this);
52         DeviceMemory.AllocateInfo deviceMemoryAllocInfo = {
53             allocationSize: requirements.size,
54             memoryTypeIndex: cast(uint)gpu.getMemoryProperties().memoryTypes.enumerate
55                 .countUntil!(p => requirements.acceptable(p.index) && p.value.supports(memoryTypeFlag))
56         };
57         enforce(deviceMemoryAllocInfo.memoryTypeIndex != -1);
58         return new DeviceMemory(device, deviceMemoryAllocInfo);
59     }
60 }