• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Bas Nieuwenhuizen
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef TU_DESCRIPTOR_SET_H
7 #define TU_DESCRIPTOR_SET_H
8 
9 #include "tu_common.h"
10 
11 #include "vk_descriptor_set_layout.h"
12 
13 /* The hardware supports up to 8 descriptor sets since A7XX.
14  * Note: This is the maximum across generations, not the maximum for a
15  * particular generation so it should only be used for allocation.
16  */
17 #define MAX_SETS 8
18 
19 /* I have no idea what the maximum size is, but the hardware supports very
20  * large numbers of descriptors (at least 2^16). This limit is based on
21  * CP_LOAD_STATE6, which has a 28-bit field for the DWORD offset, so that
22  * we don't have to think about what to do if that overflows, but really
23  * nothing is likely to get close to this.
24  */
25 #define MAX_SET_SIZE ((1 << 28) * 4)
26 
27 struct tu_descriptor_set_binding_layout
28 {
29    VkDescriptorType type;
30 
31    /* Number of array elements in this binding */
32    uint32_t array_size;
33 
34    /* The size in bytes of each Vulkan descriptor. */
35    uint32_t size;
36 
37    uint32_t offset;
38 
39    /* Byte offset in the array of dynamic descriptors (offsetted by
40     * tu_pipeline_layout::set::dynamic_offset_start).
41     */
42    uint32_t dynamic_offset_offset;
43 
44    /* Offset in the tu_descriptor_set_layout of the immutable samplers, or 0
45     * if there are no immutable samplers. */
46    uint32_t immutable_samplers_offset;
47 
48    /* Offset in the tu_descriptor_set_layout of the ycbcr samplers, or 0
49     * if there are no immutable samplers. */
50    uint32_t ycbcr_samplers_offset;
51 
52    /* Shader stages that use this binding */
53    uint32_t shader_stages;
54 };
55 
56 struct tu_descriptor_set_layout
57 {
58    struct vk_descriptor_set_layout vk;
59 
60    /* The create flags for this descriptor set layout */
61    VkDescriptorSetLayoutCreateFlags flags;
62 
63    /* Number of bindings in this descriptor set */
64    uint32_t binding_count;
65 
66    /* Total size of the descriptor set with room for all array entries */
67    uint32_t size;
68 
69    /* Shader stages affected by this descriptor set */
70    uint16_t shader_stages;
71 
72    /* Size of dynamic offset descriptors used by this descriptor set */
73    uint16_t dynamic_offset_size;
74 
75    bool has_immutable_samplers;
76    bool has_variable_descriptors;
77    bool has_inline_uniforms;
78 
79    struct tu_bo *embedded_samplers;
80 
81    /* Bindings in this descriptor set */
82    struct tu_descriptor_set_binding_layout binding[0];
83 };
84 VK_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set_layout, vk.base,
85                                VkDescriptorSetLayout,
86                                VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT)
87 
88 struct tu_pipeline_layout
89 {
90    struct vk_object_base base;
91 
92    struct
93    {
94       struct tu_descriptor_set_layout *layout;
95       uint32_t size;
96    } set[MAX_SETS];
97 
98    uint32_t num_sets;
99    uint32_t push_constant_size;
100 
101    unsigned char sha1[20];
102 };
103 VK_DEFINE_NONDISP_HANDLE_CASTS(tu_pipeline_layout, base, VkPipelineLayout,
104                                VK_OBJECT_TYPE_PIPELINE_LAYOUT)
105 
106 void tu_pipeline_layout_init(struct tu_pipeline_layout *layout);
107 
108 struct tu_descriptor_set
109 {
110    struct vk_object_base base;
111 
112    /* Link to descriptor pool's desc_sets list . */
113    struct list_head pool_link;
114 
115    struct tu_descriptor_set_layout *layout;
116    struct tu_descriptor_pool *pool;
117    uint32_t size;
118 
119    uint64_t va;
120    /* Pointer to the GPU memory for the set for non-push descriptors, or pointer
121     * to a host memory copy for push descriptors.
122     */
123    uint32_t *mapped_ptr;
124 
125    /* Size of the host memory allocation for push descriptors */
126    uint32_t host_size;
127 
128    uint32_t *dynamic_descriptors;
129 };
130 VK_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_set, base, VkDescriptorSet,
131                                VK_OBJECT_TYPE_DESCRIPTOR_SET)
132 
133 struct tu_descriptor_pool_entry
134 {
135    uint32_t offset;
136    uint32_t size;
137    struct tu_descriptor_set *set;
138 };
139 
140 struct tu_descriptor_pool
141 {
142    struct vk_object_base base;
143 
144    struct tu_bo *bo;
145    uint64_t current_offset;
146    uint64_t size;
147 
148    uint8_t *host_memory_base;
149    uint8_t *host_memory_ptr;
150    uint8_t *host_memory_end;
151    uint8_t *host_bo;
152 
153    struct list_head desc_sets;
154 
155    uint32_t entry_count;
156    uint32_t max_entry_count;
157    struct tu_descriptor_pool_entry entries[0];
158 };
159 VK_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_pool, base, VkDescriptorPool,
160                                VK_OBJECT_TYPE_DESCRIPTOR_POOL)
161 
162 struct tu_descriptor_update_template_entry
163 {
164    VkDescriptorType descriptor_type;
165 
166    /* The number of descriptors to update */
167    uint32_t descriptor_count;
168 
169    /* Into mapped_ptr or dynamic_descriptors, in units of the respective array
170     */
171    uint32_t dst_offset;
172 
173    /* In dwords. Not valid/used for dynamic descriptors */
174    uint32_t dst_stride;
175 
176    uint32_t buffer_offset;
177 
178    /* Only valid for combined image samplers and samplers */
179    uint16_t has_sampler;
180 
181    /* In bytes */
182    size_t src_offset;
183    size_t src_stride;
184 
185    /* For push descriptors */
186    const struct tu_sampler *immutable_samplers;
187 };
188 
189 struct tu_descriptor_update_template
190 {
191    struct vk_object_base base;
192 
193    uint32_t entry_count;
194    VkPipelineBindPoint bind_point;
195    struct tu_descriptor_update_template_entry entry[0];
196 };
197 VK_DEFINE_NONDISP_HANDLE_CASTS(tu_descriptor_update_template, base,
198                                VkDescriptorUpdateTemplate,
199                                VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE)
200 
201 struct tu_sampler_ycbcr_conversion {
202    struct vk_object_base base;
203 
204    VkFormat format;
205    VkSamplerYcbcrModelConversion ycbcr_model;
206    VkSamplerYcbcrRange ycbcr_range;
207    VkComponentMapping components;
208    VkChromaLocation chroma_offsets[2];
209    VkFilter chroma_filter;
210 };
211 VK_DEFINE_NONDISP_HANDLE_CASTS(tu_sampler_ycbcr_conversion, base, VkSamplerYcbcrConversion,
212                                VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION)
213 
214 void
215 tu_update_descriptor_sets(const struct tu_device *device,
216                           VkDescriptorSet overrideSet,
217                           uint32_t descriptorWriteCount,
218                           const VkWriteDescriptorSet *pDescriptorWrites,
219                           uint32_t descriptorCopyCount,
220                           const VkCopyDescriptorSet *pDescriptorCopies);
221 
222 void
223 tu_update_descriptor_set_with_template(
224    const struct tu_device *device,
225    struct tu_descriptor_set *set,
226    VkDescriptorUpdateTemplate descriptorUpdateTemplate,
227    const void *pData);
228 
229 static inline const struct tu_sampler *
tu_immutable_samplers(const struct tu_descriptor_set_layout * set,const struct tu_descriptor_set_binding_layout * binding)230 tu_immutable_samplers(const struct tu_descriptor_set_layout *set,
231                       const struct tu_descriptor_set_binding_layout *binding)
232 {
233    return (struct tu_sampler *) ((const char *) set +
234                                  binding->immutable_samplers_offset);
235 }
236 
237 static inline const struct tu_sampler_ycbcr_conversion *
tu_immutable_ycbcr_samplers(const struct tu_descriptor_set_layout * set,const struct tu_descriptor_set_binding_layout * binding)238 tu_immutable_ycbcr_samplers(const struct tu_descriptor_set_layout *set,
239                             const struct tu_descriptor_set_binding_layout *binding)
240 {
241    if (!binding->ycbcr_samplers_offset)
242       return NULL;
243 
244    return (
245       struct tu_sampler_ycbcr_conversion *) ((const char *) set +
246                                              binding->ycbcr_samplers_offset);
247 }
248 
249 #endif /* TU_DESCRIPTOR_SET_H */
250