### Setup D3D12 Allocator in Rust Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md Initializes the Direct3D12 memory allocator. Requires a D3D12 device. Debug settings and allocation size preferences can be configured. Returns an initialized Allocator instance for D3D12. ```rust use gpu_allocator::d3d12::*; let mut allocator = Allocator::new(&AllocatorCreateDesc { device: ID3D12DeviceVersion::Device(device), debug_settings: Default::default(), allocation_sizes: Default::default(), }); ``` -------------------------------- ### Setup Vulkan Allocator with Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Demonstrates how to create and configure a Vulkan memory allocator using the gpu-allocator library in Rust. It requires Vulkan instance, device, and physical device handles as input. ```rust use gpu_allocator::vulkan::*; use ash::vk; // Assume you have created Vulkan instance, device, and physical_device let instance: ash::Instance = /* created instance */; let device: ash::Device = /* created device */; let physical_device: vk::PhysicalDevice = /* selected physical device */; let mut allocator = Allocator::new(&AllocatorCreateDesc { instance, device, physical_device, debug_settings: Default::default(), buffer_device_address: true, // Enable if device supports buffer device address allocation_sizes: Default::default(), }) .expect("Failed to create Vulkan allocator"); // Allocator is now ready to use // Remember to drop the allocator before destroying the device ``` -------------------------------- ### Setup Metal Allocator in Rust Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md Initializes the Metal memory allocator. Requires a Metal device object. Options for creating a residency set can be configured. Returns an initialized Allocator instance for Metal. ```rust use gpu_allocator::metal::*; let mut allocator = Allocator::new(&AllocatorCreateDesc { device: device.clone(), debug_settings: Default::default(), allocation_sizes: Default::default(), create_residency_set: false, }); ``` -------------------------------- ### Setup Vulkan Allocator in Rust Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md Initializes the Vulkan memory allocator. Requires Vulkan instance, device, and physical device handles. Debug settings and buffer device address feature can be configured. Output is an initialized Allocator instance. ```rust 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(), }); ``` -------------------------------- ### Setup Metal GPU Allocator Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Initializes a Metal memory allocator. This requires obtaining a default Metal device using `MTLCreateSystemDefaultDevice`. The allocator can be configured with debug settings, default allocation sizes, and options for residency management. ```rust use gpu_allocator::metal::* use objc2_metal::MTLCreateSystemDefaultDevice // Get the default Metal device let device = MTLCreateSystemDefaultDevice() .expect("No MTLDevice found") let mut allocator = Allocator::new(&AllocatorCreateDesc { device: device.clone(), debug_settings: Default::default(), allocation_sizes: Default::default(), create_residency_set: false, // Enable for automatic residency management }) .expect("Failed to create Metal allocator") // Allocator is ready to use ``` -------------------------------- ### Setup DirectX 12 GPU Allocator Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Initializes a DirectX 12 memory allocator. This requires a valid ID3D12Device. The allocator can be configured with debug settings and default allocation sizes. It prepares the system for subsequent memory allocations. ```rust use gpu_allocator::d3d12::* use windows::Win32::Graphics::Direct3D12::ID3D12Device // Assume you have created a D3D12 device let device: ID3D12Device = /* created device */ let mut allocator = Allocator::new(&AllocatorCreateDesc { device: ID3D12DeviceVersion::Device(device.clone()), debug_settings: Default::default(), allocation_sizes: Default::default(), }) .expect("Failed to create D3D12 allocator") // Allocator is ready to use ``` -------------------------------- ### Allocate D3D12 Buffer Memory in Rust Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md Performs a simple Direct3D12 buffer allocation. It defines a buffer resource description, creates an allocation using the gpu-allocator, and then creates a placed resource in D3D12 using the allocated memory. Requires an initialized D3D12 allocator and device. Outputs the created D3D12 resource and its associated allocation. ```rust 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 = None; let hr = unsafe { device.CreatePlacedResource( allocation.heap(), allocation.offset(), &buffer_desc, Direct3D12::D3D12_RESOURCE_STATE_COMMON, None, &mut resource, ) }?; ``` ```rust // Cleanup drop(resource); allocator.free(allocation).unwrap(); ``` -------------------------------- ### Allocate Metal Buffer Memory in Rust Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md Performs a simple Metal buffer allocation. It defines buffer parameters and desired memory location, then allocates the memory using the gpu-allocator. Finally, it creates a Metal buffer using the allocated heap, offset, and size. Requires an initialized Metal allocator and device. Outputs the created Metal buffer and its associated allocation. ```rust use gpu_allocator::metal::*; use gpu_allocator::MemoryLocation; let allocation_desc = AllocationCreateDesc::buffer( &device, "Example allocation", 512, // size in bytes MemoryLocation::GpuOnly, ); let allocation = allocator.allocate(&allocation_desc).unwrap(); let heap = unsafe { allocation.heap() }; let resource = unsafe { heap.newBufferWithLength_options_offset( allocation.size() as usize, heap.resourceOptions(), allocation.offset() as usize, ) } .unwrap(); ``` ```rust // Cleanup drop(resource); allocator.free(&allocation).unwrap(); ``` -------------------------------- ### Configure Dual std and no_std Support in Cargo.toml Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md This configuration allows a project to support both `std` and `no_std` builds by defining features in `Cargo.toml`. It ensures that the `gpu-allocator` crate can be compiled in either environment. ```toml [features] default = ["std", "other features"] std = ["gpu-allocator/std"] hashbrown = ["gpu-allocator/hashbrown"] other_features = [] [dependencies] gpu-allocator = { version = "0.28.0", default-features = false } ``` -------------------------------- ### Allocate Vulkan Buffer Memory in Rust Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md Performs a simple Vulkan buffer allocation. It creates a Vulkan buffer, queries its memory requirements, allocates the memory using the gpu-allocator, and binds the memory to the buffer. Requires an initialized Vulkan allocator and device. Outputs the allocated buffer and its associated memory allocation. ```rust 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) }; ``` -------------------------------- ### Metal Residency Sets Management in Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt This code snippet demonstrates how to manage GPU memory residency automatically using Metal residency sets with the gpu-allocator library in Rust. It showcases how to enable residency tracking, commit residency changes before using resources, and attach to a command buffer for automatic management. ```rust use gpu_allocator::metal::*; // Create allocator with residency set enabled let mut allocator = Allocator::new(&AllocatorCreateDesc { device: device.clone(), debug_settings: Default::default(), allocation_sizes: Default::default(), create_residency_set: true, // Enable residency tracking }) .expect("Failed to create allocator"); // Make allocations... let allocation1 = allocator.allocate(&allocation_desc1).unwrap(); let allocation2 = allocator.allocate(&allocation_desc2).unwrap(); // Get the global residency set containing all heaps if let Some(residency_set) = allocator.residency_set() { // Commit residency changes before using resources unsafe { residency_set.commit() }; // Attach to command buffer for automatic residency management // command_buffer.useResidencySet(&residency_set); } // All allocated heaps are automatically tracked in the residency set ``` -------------------------------- ### GPU Allocator Memory Locations Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Illustrates the different `MemoryLocation` options available in `gpu_allocator` for managing memory access between the CPU and GPU. It provides use cases for `GpuOnly`, `CpuToGpu`, `GpuToCpu`, and `Unknown`. ```rust use gpu_allocator::MemoryLocation; // GPU-only memory - fastest for GPU access, no CPU access // Use for: render targets, depth buffers, GPU-only resources let gpu_only = MemoryLocation::GpuOnly; // CPU-to-GPU memory - CPU writeable, GPU readable // Use for: uniform buffers, vertex data uploaded each frame let cpu_to_gpu = MemoryLocation::CpuToGpu; // GPU-to-CPU memory - GPU writeable, CPU readable // Use for: readback buffers, screenshots, profiling data let gpu_to_cpu = MemoryLocation::GpuToCpu; // Unknown - let the driver decide // Use for: when memory location requirements are flexible let unknown = MemoryLocation::Unknown; // Example: Creating a staging buffer for uploads let staging_allocation = allocator.allocate(&AllocationCreateDesc { name: "Staging buffer", requirements, location: MemoryLocation::CpuToGpu, linear: true, allocation_scheme: AllocationScheme::GpuAllocatorManaged, }) .expect("Failed to allocate staging memory"); ``` -------------------------------- ### Copy Data to CPU-Mapped Memory (Vulkan) Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Shows how to copy data into CPU-accessible Vulkan allocations using the `presser` crate. It covers allocating CPU-visible memory and using `copy_from_slice_to_offset_with_align` or `mapped_slice_mut` for data transfer. ```rust use gpu_allocator::vulkan::*; use gpu_allocator::MemoryLocation; use presser::copy_from_slice_to_offset_with_align; // Allocate CPU-visible memory let mut allocation = allocator.allocate(&AllocationCreateDesc { name: "Upload buffer", requirements, location: MemoryLocation::CpuToGpu, linear: true, allocation_scheme: AllocationScheme::GpuAllocatorManaged, }) .expect("Failed to allocate"); // Data to upload let vertex_data: Vec = vec![0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]; // Copy data using presser (allocation implements presser::Slab) let copy_record = copy_from_slice_to_offset_with_align( &vertex_data, &mut allocation, 0, // start offset 4, // alignment (4 bytes for f32) ) .expect("Failed to copy data"); // The actual start offset (may differ due to alignment) let actual_offset = copy_record.copy_start_offset; // Alternative: use mapped_slice_mut for direct memory access if let Some(mapped_slice) = allocation.mapped_slice_mut() { let data_bytes = bytemuck::cast_slice(&vertex_data); mapped_slice[..data_bytes.len()].copy_from_slice(data_bytes); } ``` -------------------------------- ### Enable Debug Settings in Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt This code snippet demonstrates how to enable debugging features for the gpu-allocator in Rust. It showcases the use of `AllocatorDebugSettings` to log memory information, memory leaks, stack traces, allocations, and frees. It also shows how to manually report memory leaks and generate allocation reports. ```rust use gpu_allocator::AllocatorDebugSettings; let debug_settings = AllocatorDebugSettings { log_memory_information: true, // Log memory heaps on startup log_leaks_on_shutdown: true, // Warn about memory leaks on drop store_stack_traces: true, // Store backtrace for each allocation log_allocations: true, // Log each allocation log_frees: true, // Log each free log_stack_traces: true, // Include stack traces in logs }; let allocator = Allocator::new(&AllocatorCreateDesc { instance, device, physical_device, debug_settings, buffer_device_address: false, allocation_sizes: Default::default(), }) .expect("Failed to create allocator"); // Manually report memory leaks at any time allocator.report_memory_leaks(log::Level::Warn); // Generate detailed allocation report let report = allocator.generate_report(); println!("Total allocated: {} bytes", report.total_allocated_bytes); println!("Total capacity: {} bytes", report.total_capacity_bytes); println!("Number of blocks: {}", report.blocks.len()); println!("Number of allocations: {}", report.allocations.len()); // Debug print allocator state (shows top allocations) println!("{:?}", allocator); // Print all allocations println!("{:.5?}", allocator); // Print top 5 largest allocations ``` -------------------------------- ### Configure Allocation Block Size Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Explains how to configure `AllocationSizes` for the `gpu_allocator`, allowing for fixed or growing memory block sizes. This helps optimize performance by managing memory allocation granularity. ```rust use gpu_allocator::AllocationSizes; const MB: u64 = 1024 * 1024; // Fixed block sizes (default behavior) let fixed_sizes = AllocationSizes::new(256 * MB, 64 * MB); // Growing block sizes - starts small and grows with demand let growing_sizes = AllocationSizes::new(8 * MB, 8 * MB) .with_max_device_memblock_size(256 * MB) .with_max_host_memblock_size(64 * MB); // Use in allocator creation let allocator = Allocator::new(&AllocatorCreateDesc { instance, device, physical_device, debug_settings: Default::default(), buffer_device_address: false, allocation_sizes: growing_sizes, // Use growing strategy }) .expect("Failed to create allocator"); // The allocator will: // - Start with 8MB blocks // - Double block size with each new allocation // - Cap at 256MB for device memory, 64MB for host memory // - Reduce block size when blocks are freed ``` -------------------------------- ### Enable no_std Support in Cargo.toml Source: https://github.com/traverse-research/gpu-allocator/blob/main/README.md This configuration demonstrates how to enable `no_std` support for the `gpu-allocator` crate by disabling default features and explicitly enabling the `hashbrown` feature. It's used when `std` support is not required. ```toml [dependencies] gpu-allocator = { version = "0.28.0", default-features = false, features = ["hashbrown", "other features"] } ``` -------------------------------- ### Create Dedicated Allocations in Vulkan with Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt This code snippet demonstrates how to create dedicated allocations in Vulkan using the gpu-allocator library in Rust. It showcases the use of `DedicatedBuffer` or `DedicatedImage` for large resources or those requiring dedicated allocations. It ensures proper memory binding and resource cleanup. ```rust use gpu_allocator::vulkan::*; use gpu_allocator::MemoryLocation; use ash::vk; // For optimal performance, large resources or those requiring // dedicated allocations should use DedicatedBuffer or DedicatedImage let buffer_info = vk::BufferCreateInfo::default() .size(256 * 1024 * 1024) // Large buffer .usage(vk::BufferUsageFlags::STORAGE_BUFFER); let buffer = unsafe { device.create_buffer(&buffer_info, None) } .expect("Failed to create buffer"); let requirements = unsafe { device.get_buffer_memory_requirements(buffer) }; // Request dedicated allocation for this buffer let allocation = allocator.allocate(&AllocationCreateDesc { name: "Large dedicated buffer", requirements, location: MemoryLocation::GpuOnly, linear: true, allocation_scheme: AllocationScheme::DedicatedBuffer(buffer), }) .expect("Failed to allocate"); // Check if allocation is dedicated assert!(allocation.is_dedicated()); unsafe { device.bind_buffer_memory(buffer, allocation.memory(), allocation.offset()) .expect("Failed to bind memory") }; // Cleanup allocator.free(allocation).expect("Failed to free"); unsafe { device.destroy_buffer(buffer, None) }; ``` -------------------------------- ### Enable no_std Support in Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt This code snippet demonstrates how to enable `no_std` support for the gpu-allocator library in Rust. It showcases the configuration in `Cargo.toml` and the necessary code to use the library in embedded or `no_std` environments, using `hashbrown` for `HashMap`. ```toml [dependencies] gpu-allocator = { version = "0.28.0", default-features = false, features = ["hashbrown", "vulkan"] } ``` ```rust // No std required - works in embedded environments #![no_std] extern crate alloc; use gpu_allocator::vulkan::*; use gpu_allocator::MemoryLocation; // Works identically to std version // Uses hashbrown for HashMap instead of std::collections::HashMap ``` -------------------------------- ### Allocate Vulkan Buffer Memory in Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Shows how to allocate GPU memory for a Vulkan buffer and bind it using the gpu-allocator library. This involves creating a buffer, querying requirements, allocating memory, and then binding them together. ```rust use gpu_allocator::vulkan::*; use gpu_allocator::MemoryLocation; use ash::vk; // Create a Vulkan buffer let buffer_info = vk::BufferCreateInfo::default() .size(512) .usage(vk::BufferUsageFlags::STORAGE_BUFFER); let buffer = unsafe { device.create_buffer(&buffer_info, None) } .expect("Failed to create buffer"); // Query memory requirements let requirements = unsafe { device.get_buffer_memory_requirements(buffer) }; // Allocate memory for the buffer let allocation = allocator.allocate(&AllocationCreateDesc { name: "Example allocation", requirements, location: MemoryLocation::CpuToGpu, linear: true, // Buffers are always linear allocation_scheme: AllocationScheme::GpuAllocatorManaged, }) .expect("Failed to allocate memory"); // Bind the allocated memory to the buffer unsafe { device.bind_buffer_memory(buffer, allocation.memory(), allocation.offset()) .expect("Failed to bind buffer memory") }; // Use the buffer... // Cleanup: free allocation before destroying buffer allocator.free(allocation).expect("Failed to free allocation"); unsafe { device.destroy_buffer(buffer, None) }; ``` -------------------------------- ### Allocate Vulkan Image Memory in Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Illustrates allocating GPU memory for Vulkan images (textures) using the gpu-allocator library. It covers creating an image, obtaining its memory requirements, allocating memory, and binding them. ```rust use gpu_allocator::vulkan::*; use gpu_allocator::MemoryLocation; use ash::vk; // Create an image let image_info = vk::ImageCreateInfo::default() .image_type(vk::ImageType::TYPE_2D) .format(vk::Format::R8G8B8A8_UNORM) .extent(vk::Extent3D { width: 1024, height: 1024, depth: 1, }) .mip_levels(1) .array_layers(1) .samples(vk::SampleCountFlags::TYPE_1) .tiling(vk::ImageTiling::OPTIMAL) .usage(vk::ImageUsageFlags::SAMPLED | vk::ImageUsageFlags::TRANSFER_DST) .sharing_mode(vk::SharingMode::EXCLUSIVE) .initial_layout(vk::ImageLayout::UNDEFINED); let image = unsafe { device.create_image(&image_info, None) } .expect("Failed to create image"); let requirements = unsafe { device.get_image_memory_requirements(image) }; // Allocate memory for the image (non-linear for optimal tiled images) let allocation = allocator.allocate(&AllocationCreateDesc { name: "Texture allocation", requirements, location: MemoryLocation::GpuOnly, linear: false, // Images with OPTIMAL tiling are non-linear allocation_scheme: AllocationScheme::GpuAllocatorManaged, }) .expect("Failed to allocate image memory"); unsafe { device.bind_image_memory(image, allocation.memory(), allocation.offset()) .expect("Failed to bind image memory") }; // Cleanup allocator.free(allocation).expect("Failed to free allocation"); unsafe { device.destroy_image(image, None) }; ``` -------------------------------- ### Allocate Metal Buffer Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Allocates memory for and creates a Metal buffer. This involves defining the buffer's size and memory location, allocating the memory using the Metal allocator, and then creating a `MTLBuffer` from the allocated heap and offset. The allocated memory is subsequently freed. ```rust use gpu_allocator::metal::* use gpu_allocator::MemoryLocation use objc2_metal::MTLHeap // Create allocation descriptor for a buffer let allocation_desc = AllocationCreateDesc::buffer( &device, "Example buffer allocation", 512, // size in bytes MemoryLocation::GpuOnly, ) let allocation = allocator.allocate(&allocation_desc) .expect("Failed to allocate memory") // Create buffer from the allocated heap let heap = unsafe { allocation.heap() } let buffer = unsafe { heap.newBufferWithLength_options_offset( allocation.size() as usize, heap.resourceOptions(), allocation.offset() as usize, ) } .expect("Failed to create buffer") // Use the buffer... // Cleanup drop(buffer) allocator.free(&allocation).expect("Failed to free allocation") ``` -------------------------------- ### Allocate Metal Texture Memory Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Demonstrates how to allocate memory for a Metal texture using the gpu-allocator library. It involves creating a texture descriptor, an allocation descriptor, allocating memory, and then creating the Metal texture from the allocated memory. ```rust use gpu_allocator::metal::*; use gpu_allocator::MemoryLocation; use objc2_metal::{MTLTextureDescriptor, MTLPixelFormat, MTLTextureUsage}; // Create texture descriptor let texture_desc = unsafe { MTLTextureDescriptor::new() }; texture_desc.setWidth(1024); texture_desc.setHeight(1024); texture_desc.setPixelFormat(MTLPixelFormat::RGBA8Unorm); ttexture_desc.setUsage(MTLTextureUsage::ShaderRead | MTLTextureUsage::RenderTarget); // Create allocation descriptor from texture descriptor let allocation_desc = AllocationCreateDesc::texture( &device, "Texture allocation", &texture_desc, ); let allocation = allocator.allocate(&allocation_desc) .expect("Failed to allocate texture memory"); let heap = unsafe { allocation.heap() }; let texture = unsafe { heap.newTextureWithDescriptor_offset( &texture_desc, allocation.offset() as usize, ) } .expect("Failed to create texture"); // Use the texture... // Cleanup drop(texture); allocator.free(&allocation).expect("Failed to free allocation"); ``` -------------------------------- ### Allocate DirectX 12 Buffer Resource Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Allocates memory for and creates a DirectX 12 buffer resource. This involves defining the buffer's properties, creating an allocation descriptor, allocating the memory, and then creating a placed resource using the allocated heap and offset. Finally, the allocated memory is freed. ```rust use gpu_allocator::d3d12::* use gpu_allocator::MemoryLocation use windows::Win32::Graphics::{Direct3D12::*, Dxgi::Common::*} let buffer_desc = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, Alignment: 0, Width: 512, Height: 1, DepthOrArraySize: 1, MipLevels: 1, Format: DXGI_FORMAT_UNKNOWN, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0, }, Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, Flags: D3D12_RESOURCE_FLAG_NONE, } // Create allocation descriptor from resource description let allocation_desc = AllocationCreateDesc::from_d3d12_resource_desc( &device, &buffer_desc, "Example buffer allocation", MemoryLocation::GpuOnly, ) let allocation = allocator.allocate(&allocation_desc) .expect("Failed to allocate memory") // Create placed resource on the allocated heap let mut resource: Option = None unsafe { device.CreatePlacedResource( allocation.heap(), allocation.offset(), &buffer_desc, D3D12_RESOURCE_STATE_COMMON, None, &mut resource, ) } .expect("Failed to create placed resource") let resource = resource.expect("Resource is None") // Use the resource... // Cleanup drop(resource) allocator.free(allocation).expect("Failed to free allocation") ``` -------------------------------- ### Rename Allocations in Rust Source: https://context7.com/traverse-research/gpu-allocator/llms.txt This code snippet demonstrates how to rename allocations using the gpu-allocator library in Rust. It showcases the `rename_allocation` function to update allocation names for better debugging and allocation reports. It ensures the new name appears in debug logs. ```rust use gpu_allocator::vulkan::*; // Create allocation with initial name let mut allocation = allocator.allocate(&AllocationCreateDesc { name: "Temporary buffer", requirements, location: MemoryLocation::GpuOnly, linear: true, allocation_scheme: AllocationScheme::GpuAllocatorManaged, }) .expect("Failed to allocate"); // Later, rename the allocation allocator.rename_allocation(&mut allocation, "Persistent buffer") .expect("Failed to rename allocation"); // The new name will appear in debug logs and allocation reports ``` -------------------------------- ### Create DirectX 12 Committed Resource Source: https://context7.com/traverse-research/gpu-allocator/llms.txt Creates a DirectX 12 resource with a committed allocation, meaning the memory is managed directly by the driver. This is suitable for textures, render targets, and depth-stencil surfaces. It requires defining resource properties and heap configurations. The resource and its memory are freed together. ```rust use gpu_allocator::d3d12::* use gpu_allocator::MemoryLocation use windows::Win32::Graphics::{Direct3D12::*, Dxgi::Common::*} let resource_desc = D3D12_RESOURCE_DESC { Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, Alignment: 0, Width: 1024, Height: 1024, DepthOrArraySize: 1, MipLevels: 1, Format: DXGI_FORMAT_R8G8B8A8_UNORM, SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0, }, Layout: D3D12_TEXTURE_LAYOUT_UNKNOWN, Flags: D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, } let heap_properties = D3D12_HEAP_PROPERTIES { Type: D3D12_HEAP_TYPE_DEFAULT, ..Default::default() } let resource_create_desc = ResourceCreateDesc { name: "Render target", memory_location: MemoryLocation::GpuOnly, resource_category: ResourceCategory::RtvDsvTexture, resource_desc: &resource_desc, castable_formats: &[], clear_value: None, initial_state_or_layout: ResourceStateOrBarrierLayout::ResourceState( D3D12_RESOURCE_STATE_RENDER_TARGET ), resource_type: &ResourceType::Committed { heap_properties: &heap_properties, heap_flags: D3D12_HEAP_FLAG_NONE, }, } let resource = allocator.create_resource(&resource_create_desc) .expect("Failed to create committed resource") // Use the resource... // Cleanup - frees both resource and memory allocator.free_resource(resource).expect("Failed to free resource") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.