1/*
2 * Copyright 2019 Google LLC
3 * SPDX-License-Identifier: MIT
4 *
5 * based in part on anv and radv which are:
6 * Copyright © 2015 Intel Corporation
7 * Copyright © 2016 Red Hat.
8 * Copyright © 2016 Bas Nieuwenhuizen
9 */
10
11#ifndef VN_DESCRIPTOR_SET_H
12#define VN_DESCRIPTOR_SET_H
13
14#include "vn_common.h"
15
16/* TODO accommodate new discrete type enums by:
17 * 1. increase the number of types here
18 * 2. add a helper to map to continuous array index
19 */
20#define VN_NUM_DESCRIPTOR_TYPES (VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT + 1)
21
22struct vn_descriptor_set_layout_binding {
23   VkDescriptorType type;
24   uint32_t count;
25   bool has_immutable_samplers;
26};
27
28struct vn_descriptor_set_layout {
29   struct vn_object_base base;
30
31   struct vn_refcount refcount;
32
33   uint32_t last_binding;
34   bool has_variable_descriptor_count;
35
36   /* bindings must be the last field in the layout */
37   struct vn_descriptor_set_layout_binding bindings[];
38};
39VK_DEFINE_NONDISP_HANDLE_CASTS(vn_descriptor_set_layout,
40                               base.base,
41                               VkDescriptorSetLayout,
42                               VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT)
43
44struct vn_descriptor_pool_state {
45   uint32_t set_count;
46   uint32_t descriptor_counts[VN_NUM_DESCRIPTOR_TYPES];
47};
48
49struct vn_descriptor_pool {
50   struct vn_object_base base;
51
52   VkAllocationCallbacks allocator;
53   bool async_set_allocation;
54   struct vn_descriptor_pool_state max;
55   struct vn_descriptor_pool_state used;
56
57   struct list_head descriptor_sets;
58};
59VK_DEFINE_NONDISP_HANDLE_CASTS(vn_descriptor_pool,
60                               base.base,
61                               VkDescriptorPool,
62                               VK_OBJECT_TYPE_DESCRIPTOR_POOL)
63
64struct vn_update_descriptor_sets {
65   uint32_t write_count;
66   VkWriteDescriptorSet *writes;
67   VkDescriptorImageInfo *images;
68   VkDescriptorBufferInfo *buffers;
69   VkBufferView *views;
70};
71
72struct vn_descriptor_set {
73   struct vn_object_base base;
74
75   struct vn_descriptor_set_layout *layout;
76   uint32_t last_binding_descriptor_count;
77
78   struct list_head head;
79};
80VK_DEFINE_NONDISP_HANDLE_CASTS(vn_descriptor_set,
81                               base.base,
82                               VkDescriptorSet,
83                               VK_OBJECT_TYPE_DESCRIPTOR_SET)
84
85struct vn_descriptor_update_template_entry {
86   size_t offset;
87   size_t stride;
88};
89
90struct vn_descriptor_update_template {
91   struct vn_object_base base;
92
93   mtx_t mutex;
94   struct vn_update_descriptor_sets *update;
95
96   struct vn_descriptor_update_template_entry entries[];
97};
98VK_DEFINE_NONDISP_HANDLE_CASTS(vn_descriptor_update_template,
99                               base.base,
100                               VkDescriptorUpdateTemplate,
101                               VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE)
102
103void
104vn_descriptor_set_layout_destroy(struct vn_device *dev,
105                                 struct vn_descriptor_set_layout *layout);
106
107static inline struct vn_descriptor_set_layout *
108vn_descriptor_set_layout_ref(struct vn_device *dev,
109                             struct vn_descriptor_set_layout *layout)
110{
111   vn_refcount_inc(&layout->refcount);
112   return layout;
113}
114
115static inline void
116vn_descriptor_set_layout_unref(struct vn_device *dev,
117                               struct vn_descriptor_set_layout *layout)
118{
119   if (vn_refcount_dec(&layout->refcount))
120      vn_descriptor_set_layout_destroy(dev, layout);
121}
122
123#endif /* VN_DESCRIPTOR_SET_H */
124