• Stars
    star
    376
  • Rank 113,810 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿฆ€ GPU memory allocator for Vulkan, DirectX 12 and Metal. Written in pure Rust

๐Ÿ“’ gpu-allocator

Actions Status Latest version Docs LICENSE LICENSE Contributor Covenant

Banner

[dependencies]
gpu-allocator = "0.22.0"

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.
    allocation_sizes: Default::default(),
});

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
        allocation_scheme: AllocationScheme::GpuAllocatorManaged,
    }).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: ID3D12DeviceVersion::Device(device),
    debug_settings: Default::default(),
    allocation_sizes: 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,
        None,
        &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.

More Repositories

1

Volumetric-data-structures-for-real-time-ray-tracing

All documents related to my master thesis
TeX
57
star
2

hassle-rs

๐Ÿฆ€ This crate provides an FFI layer and idiomatic rust wrappers for the new DirectXShaderCompiler library.
Rust
55
star
3

vdb-rs

๐Ÿฆ€ Rust crate for manipulating OpenVDB files
Rust
53
star
4

rspirv-reflect

๐Ÿฆ€ Minimal SPIR-V reflection library.
Rust
41
star
5

tech-talks

A collection of presentations and talks given by Traverse Research
15
star
6

android-sdkmanager-rs

๐Ÿค–๐Ÿฆ€ A rust native replacement for Android's `sdkmanager`
Rust
15
star
7

opensource-ecosystem

Open-source tracking repository for Traverse Research
12
star
8

ispc-downsampler

Image downsampler using a Lanczos filter implemented in ISPC
Rust
11
star
9

ogawa-rs

๐Ÿฆ€ Rust crate for reading ogawa alembic cache data
Rust
11
star
10

NeuBTF

Neural Bidirectional Texture Function Compression and Rendering
Jupyter Notebook
6
star
11

fidelityfx-rs

C
3
star
12

direct-storage-rs

Rust
3
star
13

tiny-ordered-float

Rust
3
star
14

Assessing-alternatives-to-SAH

Master thesis by Athos van Kralingen on alternatives to SAH for constructing Bounding Volume Hierarchies
2
star
15

amd-ext-d3d-rs

๐Ÿฆ€ Rust bindings for AMD's DirectX12 RGP markers
Rust
2
star
16

fresnel-coated-materials

2
star
17

adlx-rs

Bindings for AMD's Device Library eXtra (ADLX).
Rust
2
star
18

resizinator

Simple tool for debugging issues with window resizing by spamming resize commands to a window.
Rust
1
star
19

tileable-volume-noise

A Rust implementation of https://github.com/sebh/TileableVolumeNoise
Rust
1
star
20

mitsuba-conda-docker

Dockerfiles to build Mitsuba with bindings to a python version installed with Conda.
Dockerfile
1
star
21

spirv-linker

Rust
1
star
22

saxaboom

Small helper library around the `metal_irconverter` to create metal shader libraries from DXIL files.
Rust
1
star