Code Monkey home page Code Monkey logo

gfx-memory's Introduction

Attention

This project is discontinued in favor of rendy.

gfx-memory - graphics memory management for gfx-hal.

Build Status Docs Crates.io

This crate provides tools to manage GPU memory provided by gfx-hal.

The main tool is the MemoryAllocator trait, which can be used to allocate Blocks of memory. The most notable MemoryAllocator implementation is SmartAllocator which can be used as-is. All other allocators in this crate are used internally in SmartAllocator, but are also exposed for users who want to create their own implementations in case SmartAllocator don't satisfy their needs.

A Factory is also provided, that wraps the allocation logic in this crate, along with creation of memory resources on a Device (such as Buffer or Image). For most use cases, the Factory provides all capabilities needed to manage memory based resources on a gfx_hal::Device.

Example

Simple example of using SmartAllocator to create a vertex Buffer:

extern crate gfx_hal;
extern crate gfx_memory;

use std::error::Error;

use gfx_hal::{Backend, Device};
use gfx_hal::buffer::Usage;
use gfx_hal::memory::Properties;
use gfx_memory::{MemoryAllocator, SmartAllocator, Type, Block};

type SmartBlock<B> = <SmartAllocator<B> as MemoryAllocator<B>>::Block;

fn make_vertex_buffer<B: Backend>(
    device: &B::Device,
    allocator: &mut SmartAllocator<B>,
    size: u64,
) -> Result<(SmartBlock<B>, B::Buffer), Box<Error>> {
    // Create unbounded buffer object. It has no memory assigned.
    let mut buf = unsafe { device.create_buffer(size, Usage::VERTEX).map_err(Box::new)? };
    // Get memory requirements for the buffer.
    let reqs = unsafe { device.get_buffer_requirements(&buf) };
    // Allocate block of device-local memory that satisfy requirements for buffer.
    let block = unsafe {
        allocator
            .alloc(device, (Type::General, Properties::DEVICE_LOCAL), reqs)
            .map_err(Box::new)?
    };
    // Bind memory block to the buffer.
    Ok(unsafe { device
        .bind_buffer_memory(block.memory(), block.range().start, &mut buf)
        .map(|buffer| (block, buffer))
        .map_err(Box::new)? })
}

This crate is mid-level and it requires the user to follow a few simple rules:

  • When memory blocks are to be freed, they must be returned to the allocator they were allocated from.
  • The same instance of Device must be used for allocating and freeing blocks.

Violating those rules may cause undefined behaviour.

License

Licensed under either of

at your option.

Contribution

We are a community project that welcomes contribution from anyone. If you're interested in helping out, you can contact us either through GitHub, or via gitter.

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.

gfx-memory's People

Contributors

bors[bot] avatar karroffel avatar kvark avatar lymia avatar msiglreith avatar napokue avatar rhuagh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gfx-memory's Issues

SmartAllocator panics on dispose

Due to the way MemoryAllocator::is_used() is implemented, a SmartAllocator which has a CombinedAllocator in it will panic when it is disposed. This is because during dispose(), it calls is_used() on itself, which in turn calls CombinedAllocator::is_used(). That function asserts that if both of its sub-allocators (Arena and Chunk) are unused, then its root allocator is also unused. However, a ChunkedAllocator keeps chunks around when freed, and so even though it will say it is unused, its owner, the RootAllocator will report that it is used (due to the chunks from the ChunkedAllocator).

Either this check should be removed (likely a bad idea since it may allow for more bugs), or some extra bookkeeping needs to happen in order to distinguish whether the only allocations in the RootAllocator are from the two sub-allocators, or whether there are any of its blocks actually given out to the user.

Possible handling of mapped memory

Vulkan doesn't allow mapping the same memory object multiple times. This is somewhat of an issue for the way I'm currently using this library, since I have a library which allocates from a SmartAllocator and then maps the resulting memory in order to update buffers every frame. The problem is that I have no way to know that whatever program is calling the library function doesn't end up with a Block from the same memory object and also want to map that. I think the correct way to go about this would be to have some layer in between which handles mapping so that it would re-map the memory object to cover the range of both blocks if somebody tries to map two Blocks.

I'm currently thinking about just creating a separate SmartAllocator instance inside my library's struct, and then handling mapping by hand. This still isn't simple though, because I have two separate buffers. I would need to make sure that if both buffers refer to the same memory object that it gets mapped once, but if the refer to different objects, that each of them is mapped.

I'm not really sure if this type of thing is in scope for what gfx-memory is trying to do, but I think there should be a layer at some point to help with re-usability.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.