| 1 |
# 📒 gpu-allocator |
| 2 |
|
| 3 |
[](https://github.com/Traverse-Research/gpu-allocator/actions) |
| 4 |
[](https://crates.io/crates/gpu-allocator) |
| 5 |
[](https://docs.rs/gpu-allocator/) |
| 6 |
[](LICENSE-MIT) |
| 7 |
[](LICENSE-APACHE) |
| 8 |
[](../main/CODE_OF_CONDUCT.md) |
| 9 |
[](https://blog.rust-lang.org/2023/06/01/Rust-1.70.0.html) |
| 10 |
|
| 11 |
[](https://traverseresearch.nl) |
| 12 |
|
| 13 |
```toml |
| 14 |
[dependencies] |
| 15 |
gpu-allocator = "0.27.0" |
| 16 |
``` |
| 17 |
|
| 18 |
 |
| 19 |
|
| 20 |
This crate provides a fully written in Rust memory allocator for Vulkan, DirectX 12 and Metal. |
| 21 |
|
| 22 |
## [Windows-rs] and [winapi] |
| 23 |
|
| 24 |
`gpu-allocator` recently migrated from [winapi] to [windows-rs] but still provides convenient helpers to convert to and from [winapi] types, enabled when compiling with the `public-winapi` crate feature. |
| 25 |
|
| 26 |
[Windows-rs]: https://github.com/microsoft/windows-rs |
| 27 |
[winapi]: https://github.com/retep998/winapi-rs |
| 28 |
|
| 29 |
## Setting up the Vulkan memory allocator |
| 30 |
|
| 31 |
```rust |
| 32 |
use gpu_allocator::vulkan::*; |
| 33 |
|
| 34 |
let mut allocator = Allocator::new(&AllocatorCreateDesc { |
| 35 |
instance, |
| 36 |
device, |
| 37 |
physical_device, |
| 38 |
debug_settings: Default::default(), |
| 39 |
buffer_device_address: true, // Ideally, check the BufferDeviceAddressFeatures struct. |
| 40 |
allocation_sizes: Default::default(), |
| 41 |
}); |
| 42 |
``` |
| 43 |
|
| 44 |
## Simple Vulkan allocation example |
| 45 |
|
| 46 |
```rust |
| 47 |
use gpu_allocator::vulkan::*; |
| 48 |
use gpu_allocator::MemoryLocation; |
| 49 |
|
| 50 |
// Setup vulkan info |
| 51 |
let vk_info = vk::BufferCreateInfo::default() |
| 52 |
.size(512) |
| 53 |
.usage(vk::BufferUsageFlags::STORAGE_BUFFER); |
| 54 |
|
| 55 |
let buffer = unsafe { device.create_buffer(&vk_info, None) }.unwrap(); |
| 56 |
let requirements = unsafe { device.get_buffer_memory_requirements(buffer) }; |
| 57 |
|
| 58 |
let allocation = allocator |
| 59 |
.allocate(&AllocationCreateDesc { |
| 60 |
name: "Example allocation", |
| 61 |
requirements, |
| 62 |
location: MemoryLocation::CpuToGpu, |
| 63 |
linear: true, // Buffers are always linear |
| 64 |
allocation_scheme: AllocationScheme::GpuAllocatorManaged, |
| 65 |
}).unwrap(); |
| 66 |
|
| 67 |
// Bind memory to the buffer |
| 68 |
unsafe { device.bind_buffer_memory(buffer, allocation.memory(), allocation.offset()).unwrap() }; |
| 69 |
|
| 70 |
// Cleanup |
| 71 |
allocator.free(allocation).unwrap(); |
| 72 |
unsafe { device.destroy_buffer(buffer, None) }; |
| 73 |
``` |
| 74 |
|
| 75 |
## Setting up the D3D12 memory allocator |
| 76 |
|
| 77 |
```rust |
| 78 |
use gpu_allocator::d3d12::*; |
| 79 |
|
| 80 |
let mut allocator = Allocator::new(&AllocatorCreateDesc { |
| 81 |
device: ID3D12DeviceVersion::Device(device), |
| 82 |
debug_settings: Default::default(), |
| 83 |
allocation_sizes: Default::default(), |
| 84 |
}); |
| 85 |
``` |
| 86 |
|
| 87 |
## Simple d3d12 allocation example |
| 88 |
|
| 89 |
```rust |
| 90 |
use gpu_allocator::d3d12::*; |
| 91 |
use gpu_allocator::MemoryLocation; |
| 92 |
|
| 93 |
|
| 94 |
let buffer_desc = Direct3D12::D3D12_RESOURCE_DESC { |
| 95 |
Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER, |
| 96 |
Alignment: 0, |
| 97 |
Width: 512, |
| 98 |
Height: 1, |
| 99 |
DepthOrArraySize: 1, |
| 100 |
MipLevels: 1, |
| 101 |
Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN, |
| 102 |
SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC { |
| 103 |
Count: 1, |
| 104 |
Quality: 0, |
| 105 |
}, |
| 106 |
Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR, |
| 107 |
Flags: Direct3D12::D3D12_RESOURCE_FLAG_NONE, |
| 108 |
}; |
| 109 |
let allocation_desc = AllocationCreateDesc::from_d3d12_resource_desc( |
| 110 |
&allocator.device(), |
| 111 |
&buffer_desc, |
| 112 |
"Example allocation", |
| 113 |
MemoryLocation::GpuOnly, |
| 114 |
); |
| 115 |
let allocation = allocator.allocate(&allocation_desc).unwrap(); |
| 116 |
let mut resource: Option<Direct3D12::ID3D12Resource> = None; |
| 117 |
let hr = unsafe { |
| 118 |
device.CreatePlacedResource( |
| 119 |
allocation.heap(), |
| 120 |
allocation.offset(), |
| 121 |
&buffer_desc, |
| 122 |
Direct3D12::D3D12_RESOURCE_STATE_COMMON, |
| 123 |
None, |
| 124 |
&mut resource, |
| 125 |
) |
| 126 |
}?; |
| 127 |
|
| 128 |
// Cleanup |
| 129 |
drop(resource); |
| 130 |
allocator.free(allocation).unwrap(); |
| 131 |
``` |
| 132 |
|
| 133 |
## Setting up the Metal memory allocator |
| 134 |
|
| 135 |
```rust |
| 136 |
use gpu_allocator::metal::*; |
| 137 |
|
| 138 |
let mut allocator = Allocator::new(&AllocatorCreateDesc { |
| 139 |
device: device.clone(), |
| 140 |
debug_settings: Default::default(), |
| 141 |
allocation_sizes: Default::default(), |
| 142 |
}); |
| 143 |
``` |
| 144 |
|
| 145 |
## Simple Metal allocation example |
| 146 |
```rust |
| 147 |
use gpu_allocator::metal::*; |
| 148 |
use gpu_allocator::MemoryLocation; |
| 149 |
|
| 150 |
let allocation_desc = AllocationCreateDesc::buffer( |
| 151 |
&device, |
| 152 |
"Example allocation", |
| 153 |
512, // size in bytes |
| 154 |
gpu_allocator::MemoryLocation::GpuOnly, |
| 155 |
); |
| 156 |
let allocation = allocator.allocate(&allocation_desc).unwrap(); |
| 157 |
let resource = allocation.make_buffer().unwrap(); |
| 158 |
|
| 159 |
// Cleanup |
| 160 |
drop(resource); |
| 161 |
allocator.free(&allocation).unwrap(); |
| 162 |
``` |
| 163 |
|
| 164 |
## Minimum Supported Rust Version |
| 165 |
|
| 166 |
The MSRV for this crate and the `vulkan`, `d3d12` and `metal` features is Rust 1.70. Any other features such as the `visualizer` (with all the `egui` dependencies) may have a higher requirement and are not tested in our CI. |
| 167 |
|
| 168 |
## License |
| 169 |
|
| 170 |
Licensed under either of |
| 171 |
|
| 172 |
- Apache License, Version 2.0, ([LICENSE-APACHE](../master/LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) |
| 173 |
- MIT license ([LICENSE-MIT](../master/LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 174 |
|
| 175 |
at your option. |
| 176 |
|
| 177 |
## Alternative libraries |
| 178 |
|
| 179 |
- [vk-mem-rs](https://github.com/gwihlidal/vk-mem-rs) |
| 180 |
|
| 181 |
## Contribution |
| 182 |
|
| 183 |
Unless you explicitly state otherwise, any contribution intentionally |
| 184 |
submitted for inclusion in the work by you, as defined in the Apache-2.0 |
| 185 |
license, shall be dual licensed as above, without any additional terms or |
| 186 |
conditions. |