1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * SPDX-License-Identifier: MIT
4 */
5 #ifndef NVK_DESCRIPTOR_TABLE_H
6 #define NVK_DESCRIPTOR_TABLE_H 1
7
8 #include "nvk_private.h"
9
10 #include "util/bitset.h"
11 #include "util/simple_mtx.h"
12 #include "nvkmd/nvkmd.h"
13
14 struct nvk_device;
15
16 struct nvk_descriptor_table {
17 simple_mtx_t mutex;
18
19 uint32_t desc_size; /**< Size of a descriptor */
20 uint32_t alloc; /**< Number of descriptors allocated */
21 uint32_t max_alloc; /**< Maximum possible number of descriptors */
22 uint32_t next_desc; /**< Next unallocated descriptor */
23 uint32_t free_count; /**< Size of free_table */
24
25 struct nvkmd_mem *mem;
26
27 /* Bitset of all descriptors currently in use. This is the single source
28 * of truth for what is and isn't free. The free_table and next_desc are
29 * simply hints to make finding a free descrptor fast. Every free
30 * descriptor will either be above next_desc or in free_table but not
31 * everything which satisfies those two criteria is actually free.
32 */
33 BITSET_WORD *in_use;
34
35 /* Stack for free descriptor elements */
36 uint32_t *free_table;
37 };
38
39 VkResult nvk_descriptor_table_init(struct nvk_device *dev,
40 struct nvk_descriptor_table *table,
41 uint32_t descriptor_size,
42 uint32_t min_descriptor_count,
43 uint32_t max_descriptor_count);
44
45 void nvk_descriptor_table_finish(struct nvk_device *dev,
46 struct nvk_descriptor_table *table);
47
48 VkResult nvk_descriptor_table_add(struct nvk_device *dev,
49 struct nvk_descriptor_table *table,
50 const void *desc_data, size_t desc_size,
51 uint32_t *index_out);
52
53 VkResult nvk_descriptor_table_insert(struct nvk_device *dev,
54 struct nvk_descriptor_table *table,
55 uint32_t index,
56 const void *desc_data, size_t desc_size);
57
58 void nvk_descriptor_table_remove(struct nvk_device *dev,
59 struct nvk_descriptor_table *table,
60 uint32_t index);
61
62 static inline struct nvkmd_mem *
nvk_descriptor_table_get_mem_ref(struct nvk_descriptor_table * table,uint32_t * alloc_count_out)63 nvk_descriptor_table_get_mem_ref(struct nvk_descriptor_table *table,
64 uint32_t *alloc_count_out)
65 {
66 simple_mtx_lock(&table->mutex);
67 struct nvkmd_mem *mem = table->mem;
68 if (mem)
69 nvkmd_mem_ref(mem);
70 *alloc_count_out = table->alloc;
71 simple_mtx_unlock(&table->mutex);
72
73 return mem;
74 }
75
76 #endif
77