24个重大版本更新
0.27.0 | 2024年7月15日 |
---|---|
0.25.0 | 2024年1月4日 |
0.24.0 | 2023年11月2日 |
0.22.0 | 2023年1月28日 |
0.1.0 | 2020年11月9日 |
在图形API中排名12
每月下载量118,467
用于1,085个crate(直接使用15个)
175KB
3.5K SLoC
📒 gpu-allocator
[dependencies]
gpu-allocator = "0.27.0"
此crate提供了一个用Rust编写的Vulkan、DirectX 12和Metal的完整内存分配器。
Windows-rs 和 winapi
gpu-allocator
最近已从winapi迁移到windows-rs,但仍提供了方便的辅助函数,用于将类型转换为和从winapi,在编译时启用public-winapi
crate功能。
设置Vulkan内存分配器
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(),
});
简单的Vulkan分配示例
use gpu_allocator::vulkan::*;
use gpu_allocator::MemoryLocation;
// Setup vulkan info
let vk_info = vk::BufferCreateInfo::default()
.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) };
设置D3D12内存分配器
use gpu_allocator::d3d12::*;
let mut allocator = Allocator::new(&AllocatorCreateDesc {
device: ID3D12DeviceVersion::Device(device),
debug_settings: Default::default(),
allocation_sizes: Default::default(),
});
简单的d3d12分配示例
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();
设置Metal内存分配器
use gpu_allocator::metal::*;
let mut allocator = Allocator::new(&AllocatorCreateDesc {
device: device.clone(),
debug_settings: Default::default(),
allocation_sizes: Default::default(),
});
简单的Metal分配示例
use gpu_allocator::metal::*;
use gpu_allocator::MemoryLocation;
let allocation_desc = AllocationCreateDesc::buffer(
&device,
"Example allocation",
512, // size in bytes
gpu_allocator::MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let resource = allocation.make_buffer().unwrap();
// Cleanup
drop(resource);
allocator.free(&allocation).unwrap();
最低支持的Rust版本
此crate以及vulkan
、d3d12
和metal
功能的MSRV是Rust 1.70。其他功能(如visualizer
(包含所有egui
依赖项)可能要求更高,并且在我们的CI中未进行测试。
许可证
许可方式为以下之一
- Apache许可证2.0版本,LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0
- MIT许可证,LICENSE-MIT 或 http://opensource.org/licenses/MIT
任选其一。
替代库
贡献
除非您明确声明,否则根据Apache-2.0许可证定义的,您有意提交以包含在本作品中的任何贡献,应按上述方式双许可,不附加任何额外条款或条件。
依赖关系
~0.4–39MB
~595K SLoC