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