|
|
|
|
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `memory` mod in crate `vulkano`."><meta name="keywords" content="rust, rustlang, rust-lang, memory"><title>vulkano::memory - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize.css"><link rel="stylesheet" type="text/css" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark.css"><link rel="stylesheet" type="text/css" href="../../light.css" id="themeStyle"><script src="../../storage.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="shortcut icon" href="../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../vulkano/index.html'><div class='logo-container'><img src='https://raw.githubusercontent.com/vulkano-rs/vulkano/master/logo.png' alt='logo'></div></a><p class='location'>Module memory</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#reexports">Re-exports</a></li><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'><a href='../index.html'>vulkano</a></p><script>window.sidebarCurrent = {name: 'memory', ty: 'mod', relpath: '../'};</script><script defer src="../sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../../src/vulkano/memory/mod.rs.html#10-209' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>vulkano</a>::<wbr><a class="mod" href=''>memory</a></span></h1><div class='docblock'><p>Device memory allocation and memory pools.</p>
|
|
|
|
|
<p>By default, memory allocation is automatically handled by the vulkano library when you create
|
|
|
|
|
a buffer or an image. But if you want more control, you have the possibility to customise the
|
|
|
|
|
memory allocation strategy.</p>
|
|
|
|
|
<h1 id="memory-types-and-heaps" class="section-header"><a href="#memory-types-and-heaps">Memory types and heaps</a></h1>
|
|
|
|
|
<p>A physical device is composed of one or more <strong>memory heaps</strong>. A memory heap is a pool of
|
|
|
|
|
memory that can be allocated.</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="comment">// Enumerating memory heaps.</span>
|
|
|
|
|
<span class="kw">for</span> <span class="ident">heap</span> <span class="kw">in</span> <span class="ident">physical_device</span>.<span class="ident">memory_heaps</span>() {
|
|
|
|
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Heap #{:?} has a capacity of {:?} bytes"</span>, <span class="ident">heap</span>.<span class="ident">id</span>(), <span class="ident">heap</span>.<span class="ident">size</span>());
|
|
|
|
|
}</pre></div>
|
|
|
|
|
<p>However you can't allocate directly from a memory heap. A memory heap is shared amongst one or
|
|
|
|
|
multiple <strong>memory types</strong>, which you can allocate memory from. Each memory type has different
|
|
|
|
|
characteristics.</p>
|
|
|
|
|
<p>A memory type may or may not be visible to the host. In other words, it may or may not be
|
|
|
|
|
directly writable by the CPU. A memory type may or may not be device-local. A device-local
|
|
|
|
|
memory type has a much quicker access time from the GPU than a non-device-local type. Note
|
|
|
|
|
that non-device-local memory types are still accessible by the device, they are just slower.</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="comment">// Enumerating memory types.</span>
|
|
|
|
|
<span class="kw">for</span> <span class="ident">ty</span> <span class="kw">in</span> <span class="ident">physical_device</span>.<span class="ident">memory_types</span>() {
|
|
|
|
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Memory type belongs to heap #{:?}"</span>, <span class="ident">ty</span>.<span class="ident">heap</span>().<span class="ident">id</span>());
|
|
|
|
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Host-accessible: {:?}"</span>, <span class="ident">ty</span>.<span class="ident">is_host_visible</span>());
|
|
|
|
|
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Device-local: {:?}"</span>, <span class="ident">ty</span>.<span class="ident">is_device_local</span>());
|
|
|
|
|
}</pre></div>
|
|
|
|
|
<p>Memory types are order from "best" to "worse". In other words, the implementation prefers that
|
|
|
|
|
you use the memory types that are earlier in the list. This means that selecting a memory type
|
|
|
|
|
should always be done by enumerating them and taking the first one that matches our criteria.</p>
|
|
|
|
|
<h2 id="in-practice" class="section-header"><a href="#in-practice">In practice</a></h2>
|
|
|
|
|
<p>In practice, desktop machines usually have two memory heaps: one that represents the RAM of
|
|
|
|
|
the CPU, and one that represents the RAM of the GPU. The CPU's RAM is host-accessible but not
|
|
|
|
|
device-local, while the GPU's RAM is not host-accessible but is device-local.</p>
|
|
|
|
|
<p>Mobile machines usually have a single memory heap that is "equally local" to both the CPU and
|
|
|
|
|
the GPU. It is both host-accessible and device-local.</p>
|
|
|
|
|
<h1 id="allocating-memory-and-memory-pools" class="section-header"><a href="#allocating-memory-and-memory-pools">Allocating memory and memory pools</a></h1>
|
|
|
|
|
<p>Allocating memory can be done by calling <code>DeviceMemory::alloc()</code>.</p>
|
|
|
|
|
<p>Here is an example:</p>
|
|
|
|
|
|
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
|
|
|
|
<span class="kw">use</span> <span class="ident">vulkano</span>::<span class="ident">memory</span>::<span class="ident">DeviceMemory</span>;
|
|
|
|
|
|
|
|
|
|
<span class="comment">// Taking the first memory type for the sake of this example.</span>
|
|
|
|
|
<span class="kw">let</span> <span class="ident">ty</span> <span class="op">=</span> <span class="ident">device</span>.<span class="ident">physical_device</span>().<span class="ident">memory_types</span>().<span class="ident">next</span>().<span class="ident">unwrap</span>();
|
|
|
|
|
|
|
|
|
|
<span class="kw">let</span> <span class="ident">alloc</span> <span class="op">=</span> <span class="ident">DeviceMemory</span>::<span class="ident">alloc</span>(<span class="ident">device</span>.<span class="ident">clone</span>(), <span class="ident">ty</span>, <span class="number">1024</span>).<span class="ident">expect</span>(<span class="string">"Failed to allocate memory"</span>);
|
|
|
|
|
|
|
|
|
|
<span class="comment">// The memory is automatically free'd when `alloc` is destroyed.</span></pre></div>
|
|
|
|
|
<p>However allocating and freeing memory is very slow (up to several hundred milliseconds
|
|
|
|
|
sometimes). Instead you are strongly encouraged to use a memory pool. A memory pool is not
|
|
|
|
|
a Vulkan concept but a vulkano concept.</p>
|
|
|
|
|
<p>A memory pool is any object that implements the <code>MemoryPool</code> trait. You can implement that
|
|
|
|
|
trait on your own structure and then use it when you create buffers and images so that they
|
|
|
|
|
get memory from that pool. By default if you don't specify any pool when creating a buffer or
|
|
|
|
|
an image, an instance of <code>StdMemoryPool</code> that is shared by the <code>Device</code> object is used.</p>
|
|
|
|
|
</div><h2 id='reexports' class='section-header'><a href="#reexports">Re-exports</a></h2>
|
|
|
|
|
<table><tr><td><code>pub use self::pool::<a class="trait" href="../../vulkano/memory/pool/trait.MemoryPool.html" title="trait vulkano::memory::pool::MemoryPool">MemoryPool</a>;</code></td></tr></table><h2 id='modules' class='section-header'><a href="#modules">Modules</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="mod" href="pool/index.html" title='vulkano::memory::pool mod'>pool</a></td><td class='docblock-short'></td></tr></table><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="struct" href="struct.CpuAccess.html" title='vulkano::memory::CpuAccess struct'>CpuAccess</a></td><td class='docblock-short'><p>Object that can be used to read or write the content of a <code>MappedDeviceMemory</code>.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.DeviceMemory.html" title='vulkano::memory::DeviceMemory struct'>DeviceMemory</a></td><td class='docblock-short'><p>Represents memory that has been allocated.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.MappedDeviceMemory.html" title='vulkano::memory::MappedDeviceMemory struct'>MappedDeviceMemory</a></td><td class='docblock-short'><p>Represents memory that has been allocated and mapped in CPU accessible space.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="struct" href="struct.MemoryRequirements.html" title='vulkano::memory::MemoryRequirements struct'>MemoryRequirements</a></td><td class='docblock-short'><p>Represents requirements expressed by the Vulkan implementation when it comes to binding memory
|
|
|
|
|
to a resource.</p>
|
|
|
|
|
</td></tr></table><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="enum" href="enum.DedicatedAlloc.html" title='vulkano::memory::DedicatedAlloc enum'>DedicatedAlloc</a></td><td class='docblock-short'><p>Indicates whether we want to allocate memory for a specific resource, or in a generic way.</p>
|
|
|
|
|
</td></tr><tr class='module-item'><td><a class="enum" href="enum.DeviceMemoryAllocError.html" title='vulkano::memory::DeviceMemoryAllocError enum'>DeviceMemoryAllocError</a></td><td class='docblock-short'><p>Error type returned by functions related to <code>DeviceMemory</code>.</p>
|
|
|
|
|
</td></tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
|
|
|
|
|
<table><tr class='module-item'><td><a class="trait" href="trait.Content.html" title='vulkano::memory::Content trait'>Content</a></td><td class='docblock-short'><p>Trait for types of data that can be mapped.</p>
|
|
|
|
|
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "vulkano";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>
|