• Stars
    star
    166
  • Rank 227,748 (Top 5 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated 4 months ago

Reviews

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

Repository Details

Rust bindings for AMD Vulkan Memory Allocator (VMA)

vk-mem

vk-mem on travis-ci.com Latest version Documentation Lines of Code MIT APACHE2

This crate provides an FFI layer and idiomatic rust wrappers for the excellent AMD Vulkan Memory Allocator (VMA) C/C++ library.

Problem

Memory allocation and resource (buffer and image) creation in Vulkan is difficult (comparing to older graphics API-s, like D3D11 or OpenGL) for several reasons:

  • It requires a lot of boilerplate code, just like everything else in Vulkan, because it is a low-level and high-performance API.
  • There is additional level of indirection: VkDeviceMemory is allocated separately from creating VkBuffer/VkImage and they must be bound together. The binding cannot be changed later - resource must be recreated.
  • Driver must be queried for supported memory heaps and memory types. Different IHVs provide different types of it.
  • It is recommended practice to allocate bigger chunks of memory and assign parts of them to particular resources.

Features

This crate can help game developers to manage memory allocations and resource creation by offering some higher-level functions:

  • Functions that help to choose correct and optimal memory type based on intended usage of the memory.
    • Required or preferred traits of the memory are expressed using higher-level description comparing to Vulkan flags.
  • Functions that allocate memory blocks, reserve and return parts of them (VkDeviceMemory + offset + size) to the user.
    • Library keeps track of allocated memory blocks, used and unused ranges inside them, finds best matching unused ranges for new allocations, respects all the rules of alignment and buffer/image granularity.
  • Functions that can create an image/buffer, allocate memory for it and bind them together - all in one call.

Additional features:

  • Cross-platform
    • Windows
    • Linux
    • macOS (MoltenVK)
  • Well tested and documented API
    • Underlying library ships in a number of commerical game titles.
    • Extensive documentation (including full algorithm descriptions in the VMA repository)
  • Support for custom memory pools:
    • Create a pool with desired parameters (e.g. fixed or limited maximum size)
    • Allocate memory out of it.
    • Support for a linear or buddy allocation strategy
    • Create a pool with linear algorithm and use it for much faster allocations and deallocations in free-at-once, stack, double stack, or ring buffer fashion.
  • Detailed statistics:
    • Globally, per memory heap, and per memory type.
    • Amount of memory used
    • Amount of memory unused
    • Number of allocated blocks
    • Number of allocations
    • etc.
  • Debug annotations:
    • Associate string with name or opaque pointer to your own data with every allocation.
  • JSON dump:
    • Obtain a string in JSON format with detailed map of internal state, including list of allocations and gaps between them.
    • Convert this JSON dump into a picture to visualize your memory. See tools/VmaDumpVis.
  • Support for memory mapping:
    • Reference-counted internally.
    • Support for persistently mapped memory; just allocate with appropriate flag and you get access to mapped pointer.
  • Support for defragmenting allocations:
    • Call one function and let the library move data around to free some memory blocks and make your allocations better compacted.
  • Support for lost allocations:
    • Allocate memory with appropriate flags and let the library remove allocations that are not used for many frames to make room for new ones.
  • Support for non-coherent memory and flushing allocations:
    • nonCoherentAtomSize is respected automatically.
  • Supporting for attempting to detect incorrect mapped memory usage:
    • Enable initialization of all allocated memory with a bit pattern to detect usage of uninitialized or freed memory.
    • Enable validation of a magic number before and after every allocation to detect out-of-bounds memory corruption.

Planned Features

  • Extensive unit tests and examples.
    • Some unit tests already, but not full coverage
    • Example isn't written - likely will port the VMA sample to ash and vk_mem
  • Record and replay allocations, for in-depth analysis of memory usage, resource transitions, etc
    • Check for correctness, measure performance, and gather statistics.

Example

Basic usage of this crate is very simple; advanced features are optional.

After you create a vk_mem::Allocator instance, very little code is needed to create a buffer:

// Create the buffer (GPU only, 16KiB in this example)
let create_info = vk_mem::AllocationCreateInfo {
    usage: vk_mem::MemoryUsage::GpuOnly,
    ..Default::default()
};

let (buffer, allocation, allocation_info) = allocator
    .create_buffer(
        &ash::vk::BufferCreateInfo::builder()
            .size(16 * 1024)
            .usage(ash::vk::BufferUsageFlags::VERTEX_BUFFER | ash::vk::BufferUsageFlags::TRANSFER_DST)
            .build(),
        &create_info,
    )
    .unwrap();

// Do stuff with buffer! (type is ash::vk::Buffer)

// Destroy the buffer
allocator.destroy_buffer(buffer, &allocation).unwrap();

With this one function call (vk_mem::Allocator::create_buffer):

  • ash::vk::Buffer (VkBuffer) is created.
  • ash::vk::DeviceMemory (VkDeviceMemory) block is allocated if needed.
  • An unused region of the memory block is bound to this buffer.
  • vk_mem::Allocation is created that represents memory assigned to this buffer. It can be queried for parameters like Vulkan memory handle and offset.

MoltenVK

For MoltenVK on macOS, you need to have the proper environment variables set. Something like:

export SDK_PATH=/path/to/vulkansdk-macos-1.1.106.0
export DYLD_LIBRARY_PATH=$SDK_PATH/macOS/lib
export VK_ICD_FILENAMES=$SDK_PATH/macOS/etc/vulkan/icd.d/MoltenVK_icd.json
export VK_LAYER_PATH=$SDK_PATH/macOS/etc/vulkan/explicit_layer.d
cargo test

Usage

Add this to your Cargo.toml:

[dependencies]
vk-mem = "0.3.0"

and add this to your crate root:

extern crate vk_mem;

Compiling using MinGW W64

Vulkan Memory Allocator requires C++11 threads. MinGW W64 does not support these by default, so you need to switch to the posix build. For example, on debian you need to run the following:

update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
update-alternatives --set i686-w64-mingw32-gcc /usr/bin/i686-w64-mingw32-gcc-posix
update-alternatives --set i686-w64-mingw32-g++ /usr/bin/i686-w64-mingw32-g++-posix

License

Licensed under either of

at your option.

Credits and Special Thanks

Contribution

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

Contributions are always welcome; please look at the issue tracker to see what known improvements are documented.

Code of Conduct

Contribution to the vk-mem crate is organized under the terms of the Contributor Covenant, the maintainer of vk-mem, @gwihlidal, promises to intervene to uphold that code of conduct.

More Repositories

1

QNodeView

Qt5 suite that supports displaying and editing nodes in a graph-like flow. Similar to Unreal Kismet, Frostbite 3 Schematics or Allegorithmic Substance Designer UIs.
C++
197
star
2

meshopt-rs

Rust ffi and idiomatic wrapper for zeux/meshoptimizer, a mesh optimization library that makes indexed meshes more GPU-friendly.
Rust
161
star
3

spirv-reflect-rs

Reflection API in rust for SPIR-V shader byte code, intended for Vulkan applications
Rust
105
star
4

ash-nv-rt

NV ray tracing with rust and ash!
Rust
56
star
5

vk-sync-rs

Simplified Vulkan synchronization logic, written in rust
Rust
53
star
6

smush-rs

Common rust abstraction around a variety of compression codecs.
Rust
33
star
7

render-rs

Collection of rust crates providing rendering abstractions.
C++
31
star
8

grue-rs

Various tools and algorithms for building role-playing and adventure games
Rust
27
star
9

intel-tex-rs

Rust bindings for Intel's ISPC texture compression
Rust
23
star
10

docker-shader

Docker image with a range of shader compilers available
Dockerfile
23
star
11

dxil-signing

Utility to sign DXIL code after compilation
C++
21
star
12

svc-shader

A gRPC micro-service that exposes a variety of GPU shader compilers under a common cloud-based abstraction.
HLSL
13
star
13

fbx2json

A simple command-line utility to convert Autodesk FBX files to a custom JSON format.
C++
11
star
14

docker-dxc

Docker image with Microsoft DirectX shader compiler (dxil and spirv)
Makefile
11
star
15

include-merkle-rs

Functionality to build a Merkle-tree of a given text file with include references, substituting deterministic identities, and flattening include directives into a single file.
Rust
8
star
16

app-engine-rs

Example of rust + rocket + docker running in a flex environment on Google App Engine.
Rust
7
star
17

cloudstore-rs

A gRPC microservice, written in rust, that marshalls objects and files to cloud storage like AWS S3, Google Cloud Storage, or other providers.
Rust
6
star
18

svc-texture

A gRPC micro-service that exposes texture compression routines under a common cloud-based abstraction.
Rust
6
star
19

hindranch

Global Game Jam 2019!
Rust
5
star
20

svc-mesh

A gRPC micro-service that exposes mesh loading routines under a common cloud-based abstraction.
Rust
5
star
21

docker-protoc

Docker image with protobuf compiler and various language plugins
Makefile
4
star
22

docker-fxc

Docker image of FXC running with Wine
Dockerfile
4
star
23

voxelpipe

Automatically exported from code.google.com/p/voxelpipe
C++
2
star
24

clangelscript

Automatic binding generator for Angelscript using clang and cindex
Python
2
star
25

ltalloc

Automatically exported from code.google.com/p/ltalloc
C++
2
star
26

smol-v-rs

Rust bindings for SMOL-V
Rust
1
star
27

gwihlidal.github.io

HTML
1
star
28

web_kvm

REST service controller for the rack mountable TESmart 8-port HDMI enterprise-grade KVM switch (TES-HKS0801A1U-USBK)
Rust
1
star
29

shader-merkle-rs

HLSL
1
star
30

speechtest-rs

Google Cloud text-to-speech prototype
Rust
1
star