1 /// GrAAL buffer module
2 module gfx.graal.buffer;
3 
4 import gfx.core.rc;
5 import gfx.graal.format;
6 import gfx.graal.memory;
7 import gfx.graal.pipeline : BufferDescriptor, TexelBufferDescriptor;
8 
9 enum BufferUsage
10 {
11     transferSrc     = 0x0001,
12     transferDst     = 0x0002,
13     uniformTexel    = 0x0004,
14     storageTexel    = 0x0008,
15     uniform         = 0x0010,
16     storage         = 0x0020,
17     index           = 0x0040,
18     vertex          = 0x0080,
19     indirect        = 0x0100,
20 }
21 
22 enum IndexType {
23     u16, u32
24 }
25 
26 interface Buffer : IAtomicRefCounted
27 {
28     import gfx.graal.device : Device;
29 
30     /// Get the parent device
31     @property Device device();
32 
33     /// The size in bytes of the buffer
34     @property size_t size();
35 
36     /// The usage this buffer was created for
37     @property BufferUsage usage();
38 
39     /// The memory allocation requirements for this buffer
40     @property MemoryRequirements memoryRequirements();
41 
42     /// Bind a the buffer to a device memory
43     ///
44     /// The buffer will use the memory allocated in the device memory
45     /// starting at the specified offset in bytes, and spanning up
46     /// to the offset + size member of memoryRequirments.
47     /// The buffer keeps a reference to the device memory.
48     void bindMemory(DeviceMemory mem, in size_t offset);
49 
50     /// The memory this buffer is bound to
51     @property DeviceMemory boundMemory();
52 
53     /// Build a view to a part of this buffer to be used as texel buffer
54     /// The returned resource keeps a reference to this buffer.
55     TexelBufferView createTexelView(Format format, size_t offset, size_t size);
56 
57     /// build a descriptor for this resource
58     final BufferDescriptor descriptor(in size_t offset = 0, in size_t size = 0)
59     {
60         const sz = size == 0 ? this.size - offset : size;
61         return BufferDescriptor(this, offset, sz);
62     }
63 }
64 
65 /// A texel view to a buffer
66 interface TexelBufferView : IAtomicRefCounted
67 {
68     /// The texel format
69     @property Format format();
70     /// The underlying buffer resource
71     @property Buffer buffer();
72     /// The offset in bytes from the buffer
73     @property size_t offset();
74     /// The amount of texel bytes in this view
75     @property size_t size();
76 
77     /// Build a descriptor for this resource
78     final TexelBufferDescriptor descriptor()
79     {
80         return TexelBufferDescriptor(this);
81     }
82 }