Debian Codegraph @

docs/vulkan/command-pools.rst

raw
1 Command Pools
2 =============
3
4 The Vulkan runtime code provides a common ``VkCommandPool`` implementation
5 which makes managing the lifetimes of command buffers and recycling their
6 internal state easier. To use the common command pool a driver needs to
7 fill out a :c:struct:`vk_command_buffer_ops` struct and set the
8 ``command_buffer_ops`` field of :c:struct:`vk_device`.
9
10 .. c:autostruct:: vk_command_buffer_ops
11 :file: src/vulkan/runtime/vk_command_buffer.h
12 :members:
13
14 By reducing the entirety of command buffer lifetime management to these
15 three functions, much of the complexity of command pools can be implemented
16 in common code, providing better, more consistent behavior across Mesa.
17
18
19 Command Buffer Recycling
20 ------------------------
21
22 The common command pool provides automatic command buffer recycling as long
23 as the driver uses the common ``vkAllocateCommandBuffers()`` and
24 ``vkFreeCommandBuffers()`` implementations. The driver must also provide the
25 ``reset`` function pointer in :c:struct:`vk_command_buffer_ops`.
26
27 With the common command buffer pool, when the client calls
28 ``vkFreeCommandBuffers()``, the command buffers are not immediately freed.
29 Instead, they are reset with
30 ``VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT``, their base object is
31 recycled, and they are added to a free list inside the pool. When the
32 client then calls ``vkAllocateCommandBuffers()``, we check the free list
33 and return a recycled command buffer, if any are available. This provides
34 some basic command buffer pooling without the driver doing any additional
35 work.
36
37
38 Custom command pools
39 --------------------
40
41 If a driver wishes to recycle at a finer granularity than whole command
42 buffers, they can do so by providing their own command pool implementation
43 which wraps :c:struct:`vk_command_pool`. The common use-case here is if
44 the driver wants to pool command-buffer-internal objects at a finer
45 granularity than whole command buffers. The command pool provides a place
46 where things like GPU command buffers or upload buffers can be cached
47 without having to take a lock.
48
49 When implementing a custom command pool, drivers need only implement three
50 entrypoints:
51
52 - ``vkCreateCommandPool()``
53 - ``vkDestroyCommandPool()``
54 - ``vkTrimCommandPool()``
55
56 All of the other entrypoints will be handled by common code so long as the
57 driver's command pool derives from :c:struct:`vk_command_pool`.
58
59 The driver implementation of the command buffer ``recycle()`` function
60 should respect ``VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT`` and, when
61 set, return any recyclable resources to the command pool. This may be set
62 by the client when it calls ``vkResetCommandBuffer()``, come from a
63 whole-pool reset via ``VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT``, or
64 come from the common command buffer code when a command buffer is recycled.
65
66 The driver's implementation of ``vkTrimCommandPool()`` should free any
67 resources that have been cached within the command pool back to the device
68 or back to the OS. It **must** also call :c:func:`vk_command_pool_trim`
69 to allow the common code to free any recycled command buffers.
70
71 Reference
72 ---------
73
74 .. c:autostruct:: vk_command_pool
75 :file: src/vulkan/runtime/vk_command_pool.h
76 :members:
77
78 .. c:autofunction:: vk_command_pool_init
79 :file: src/vulkan/runtime/vk_command_pool.h
80
81 .. c:autofunction:: vk_command_pool_finish
82 :file: src/vulkan/runtime/vk_command_pool.h
83
84 .. c:autofunction:: vk_command_pool_trim
85 :file: src/vulkan/runtime/vk_command_pool.h