1// Copyright 2016-2023 The Khronos Group Inc.
2//
3// SPDX-License-Identifier: CC-BY-4.0
4
5include::{generated}/meta/{refprefix}VK_KHR_dedicated_allocation.adoc[]
6
7=== Other Extension Metadata
8
9*Last Modified Date*::
10    2017-09-05
11*IP Status*::
12    No known IP claims.
13*Interactions and External Dependencies*::
14  - Promoted to Vulkan 1.1 Core
15*Contributors*::
16  - Jeff Bolz, NVIDIA
17  - Faith Ekstrand, Intel
18
19=== Description
20
21This extension enables resources to be bound to a dedicated allocation,
22rather than suballocated.
23For any particular resource, applications can: query whether a dedicated
24allocation is recommended, in which case using a dedicated allocation may:
25improve the performance of access to that resource.
26Normal device memory allocations must support multiple resources per
27allocation, memory aliasing and sparse binding, which could interfere with
28some optimizations.
29Applications should query the implementation for when a dedicated allocation
30may: be beneficial by adding a sname:VkMemoryDedicatedRequirementsKHR
31structure to the pname:pNext chain of the sname:VkMemoryRequirements2
32structure passed as the pname:pMemoryRequirements parameter of a call to
33fname:vkGetBufferMemoryRequirements2 or fname:vkGetImageMemoryRequirements2.
34Certain external handle types and external images or buffers may: also
35depend on dedicated allocations on implementations that associate image or
36buffer metadata with OS-level memory objects.
37
38This extension adds a two small structures to memory requirements querying
39and memory allocation: a new structure that flags whether an image/buffer
40should have a dedicated allocation, and a structure indicating the image or
41buffer that an allocation will be bound to.
42
43=== Promotion to Vulkan 1.1
44
45All functionality in this extension is included in core Vulkan 1.1, with the
46KHR suffix omitted.
47The original type, enum and command names are still available as aliases of
48the core functionality.
49
50include::{generated}/interfaces/VK_KHR_dedicated_allocation.adoc[]
51
52=== Examples
53
54[source,c++]
55----
56    // Create an image with a dedicated allocation based on the
57    // implementation's preference
58
59    VkImageCreateInfo imageCreateInfo =
60    {
61        // Image creation parameters
62    };
63
64    VkImage image;
65    VkResult result = vkCreateImage(
66        device,
67        &imageCreateInfo,
68        NULL,               // pAllocator
69        &image);
70
71    VkMemoryDedicatedRequirementsKHR dedicatedRequirements =
72    {
73        .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR,
74        .pNext = NULL,
75    };
76
77    VkMemoryRequirements2 memoryRequirements =
78    {
79        .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
80        .pNext = &dedicatedRequirements,
81    };
82
83    const VkImageMemoryRequirementsInfo2 imageRequirementsInfo =
84    {
85        .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
86        .pNext = NULL,
87        .image = image
88    };
89
90    vkGetImageMemoryRequirements2(
91        device,
92        &imageRequirementsInfo,
93        &memoryRequirements);
94
95    if (dedicatedRequirements.prefersDedicatedAllocation) {
96        // Allocate memory with VkMemoryDedicatedAllocateInfoKHR::image
97        // pointing to the image we are allocating the memory for
98
99        VkMemoryDedicatedAllocateInfoKHR dedicatedInfo =
100        {
101            .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR,
102            .pNext = NULL,
103            .image = image,
104            .buffer = VK_NULL_HANDLE,
105        };
106
107        VkMemoryAllocateInfo memoryAllocateInfo =
108        {
109            .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
110            .pNext = &dedicatedInfo,
111            .allocationSize = memoryRequirements.size,
112            .memoryTypeIndex = FindMemoryTypeIndex(memoryRequirements.memoryTypeBits),
113        };
114
115        VkDeviceMemory memory;
116        vkAllocateMemory(
117            device,
118            &memoryAllocateInfo,
119            NULL,               // pAllocator
120            &memory);
121
122        // Bind the image to the memory
123
124        vkBindImageMemory(
125            device,
126            image,
127            memory,
128            0);
129    } else {
130        // Take the normal memory sub-allocation path
131    }
132----
133
134=== Version History
135
136  * Revision 1, 2017-02-27 (James Jones)
137  ** Copy content from VK_NV_dedicated_allocation
138  ** Add some references to external object interactions to the overview.
139
140  * Revision 2, 2017-03-27 (Faith Ekstrand)
141  ** Rework the extension to be query-based
142
143  * Revision 3, 2017-07-31 (Faith Ekstrand)
144  ** Clarify that memory objects allocated with
145     VkMemoryDedicatedAllocateInfoKHR can only have the specified resource
146     bound and no others.
147