16 breaking releases

0.19.1 Sep 5, 2022
0.18.0 May 19, 2022
0.17.0 Feb 25, 2022
0.14.0 Dec 28, 2021
0.1.0 Nov 9, 2020

#18 in Graphics APIs

Download history 361/week @ 2022-06-14 482/week @ 2022-06-21 408/week @ 2022-06-28 424/week @ 2022-07-05 419/week @ 2022-07-12 376/week @ 2022-07-19 421/week @ 2022-07-26 602/week @ 2022-08-02 539/week @ 2022-08-09 480/week @ 2022-08-16 460/week @ 2022-08-23 518/week @ 2022-08-30 614/week @ 2022-09-06 584/week @ 2022-09-13 545/week @ 2022-09-20 469/week @ 2022-09-27

2,330 downloads per month
Used in 21 crates (7 directly)

MIT/Apache

130KB
2.5K SLoC

📒 gpu-allocator

Actions Status Latest version Docs LICENSE LICENSE Contributor Covenant

Banner

[dependencies]
gpu-allocator = "0.19.1"

This crate provides a fully written in Rust memory allocator for Vulkan and DirectX 12.

Windows-rs and winapi

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.

Setting up the Vulkan memory allocator

use gpu_allocator::vulkan::*;

let mut allocator = Allocator::new(&AllocatorCreateDesc {
    instance,
    device,
    physical_device,
    debug_settings: Default::default(),
    buffer_device_address: true,  // Ideally, check the BufferDeviceAddressFeatures struct.
});

Simple Vulkan allocation example

use gpu_allocator::vulkan::*;
use gpu_allocator::MemoryLocation;


// Setup vulkan info
let vk_info = vk::BufferCreateInfo::builder()
    .size(512)
    .usage(vk::BufferUsageFlags::STORAGE_BUFFER);

let buffer = unsafe { device.create_buffer(&vk_info, None) }.unwrap();
let requirements = unsafe { device.get_buffer_memory_requirements(buffer) };

let allocation = allocator
    .allocate(&AllocationCreateDesc {
        name: "Example allocation",
        requirements,
        location: MemoryLocation::CpuToGpu,
        linear: true, // Buffers are always linear
    }).unwrap();

// Bind memory to the buffer
unsafe { device.bind_buffer_memory(buffer, allocation.memory(), allocation.offset()).unwrap() };

// Cleanup
allocator.free(allocation).unwrap();
unsafe { device.destroy_buffer(buffer, None) };

Setting up the D3D12 memory allocator

use gpu_allocator::d3d12::*;

let mut allocator = Allocator::new(&AllocatorCreateDesc {
    device,
    debug_settings: Default::default(),
});

Simple d3d12 allocation example

use gpu_allocator::d3d12::*;
use gpu_allocator::MemoryLocation;


let buffer_desc = Direct3D12::D3D12_RESOURCE_DESC {
    Dimension: Direct3D12::D3D12_RESOURCE_DIMENSION_BUFFER,
    Alignment: 0,
    Width: 512,
    Height: 1,
    DepthOrArraySize: 1,
    MipLevels: 1,
    Format: Dxgi::Common::DXGI_FORMAT_UNKNOWN,
    SampleDesc: Dxgi::Common::DXGI_SAMPLE_DESC {
        Count: 1,
        Quality: 0,
    },
    Layout: Direct3D12::D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
    Flags: Direct3D12::D3D12_RESOURCE_FLAG_NONE,
};
let allocation_desc = AllocationCreateDesc::from_d3d12_resource_desc(
    &allocator.device(),
    &buffer_desc,
    "Example allocation",
    MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let mut resource: Option<Direct3D12::ID3D12Resource> = None;
let hr = unsafe {
    device.CreatePlacedResource(
        allocation.heap(),
        allocation.offset(),
        &buffer_desc,
        Direct3D12::D3D12_RESOURCE_STATE_COMMON,
        std::ptr::null(),
        &mut resource,
    )
}?;

// Cleanup
drop(resource);
allocator.free(allocation).unwrap();

License

Licensed under either of

at your option.

Alternative libraries

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~4–52MB
~1M SLoC