39e64880aa32ca8b5f26f053a3f70fbb0aa32aa6
[platform/upstream/mesa.git] / src / virtio / vulkan / vn_common.h
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_COMMON_H
12 #define VN_COMMON_H
13
14 #include <assert.h>
15 #include <inttypes.h>
16 #include <limits.h>
17 #include <stdatomic.h>
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <vulkan/vulkan.h>
24
25 #include "c11/threads.h"
26 #include "util/bitscan.h"
27 #include "util/bitset.h"
28 #include "util/compiler.h"
29 #include "util/list.h"
30 #include "util/macros.h"
31 #include "util/os_time.h"
32 #include "util/simple_mtx.h"
33 #include "util/u_math.h"
34 #include "util/xmlconfig.h"
35 #include "vk_alloc.h"
36 #include "vk_debug_report.h"
37 #include "vk_device.h"
38 #include "vk_instance.h"
39 #include "vk_object.h"
40 #include "vk_physical_device.h"
41 #include "vk_util.h"
42
43 #include "vn_entrypoints.h"
44
45 #define VN_DEFAULT_ALIGN 8
46
47 #define VN_DEBUG(category) (unlikely(vn_env.debug & VN_DEBUG_##category))
48 #define VN_PERF(category) (unlikely(vn_env.perf & VN_PERF_##category))
49
50 #define vn_error(instance, error)                                            \
51    (VN_DEBUG(RESULT) ? vn_log_result((instance), (error), __func__) : (error))
52 #define vn_result(instance, result)                                          \
53    ((result) >= VK_SUCCESS ? (result) : vn_error((instance), (result)))
54
55 #ifdef ANDROID
56
57 #include <cutils/trace.h>
58
59 #define VN_TRACE_BEGIN(name) atrace_begin(ATRACE_TAG_GRAPHICS, name)
60 #define VN_TRACE_END() atrace_end(ATRACE_TAG_GRAPHICS)
61
62 #else
63
64 /* XXX we would like to use perfetto, but it lacks a C header */
65 #define VN_TRACE_BEGIN(name)
66 #define VN_TRACE_END()
67
68 #endif /* ANDROID */
69
70 #if __has_attribute(cleanup) && __has_attribute(unused)
71
72 #define VN_TRACE_SCOPE_VAR_CONCAT(name, suffix) name##suffix
73 #define VN_TRACE_SCOPE_VAR(suffix)                                           \
74    VN_TRACE_SCOPE_VAR_CONCAT(_vn_trace_scope_, suffix)
75 #define VN_TRACE_SCOPE(name)                                                 \
76    int VN_TRACE_SCOPE_VAR(__LINE__)                                          \
77       __attribute__((cleanup(vn_trace_scope_end), unused)) =                 \
78          vn_trace_scope_begin(name)
79
80 static inline int
81 vn_trace_scope_begin(const char *name)
82 {
83    VN_TRACE_BEGIN(name);
84    return 0;
85 }
86
87 static inline void
88 vn_trace_scope_end(int *scope)
89 {
90    VN_TRACE_END();
91 }
92
93 #else
94
95 #define VN_TRACE_SCOPE(name)
96
97 #endif /* __has_attribute(cleanup) && __has_attribute(unused) */
98
99 #define VN_TRACE_FUNC() VN_TRACE_SCOPE(__func__)
100
101 struct vn_instance;
102 struct vn_physical_device;
103 struct vn_device;
104 struct vn_queue;
105 struct vn_fence;
106 struct vn_semaphore;
107 struct vn_device_memory;
108 struct vn_buffer;
109 struct vn_buffer_view;
110 struct vn_image;
111 struct vn_image_view;
112 struct vn_sampler;
113 struct vn_sampler_ycbcr_conversion;
114 struct vn_descriptor_set_layout;
115 struct vn_descriptor_pool;
116 struct vn_descriptor_set;
117 struct vn_descriptor_update_template;
118 struct vn_render_pass;
119 struct vn_framebuffer;
120 struct vn_event;
121 struct vn_query_pool;
122 struct vn_shader_module;
123 struct vn_pipeline_layout;
124 struct vn_pipeline_cache;
125 struct vn_pipeline;
126 struct vn_command_pool;
127 struct vn_command_buffer;
128
129 struct vn_cs_encoder;
130 struct vn_cs_decoder;
131
132 struct vn_renderer;
133 struct vn_renderer_shmem;
134 struct vn_renderer_bo;
135 struct vn_renderer_sync;
136
137 enum vn_debug {
138    VN_DEBUG_INIT = 1ull << 0,
139    VN_DEBUG_RESULT = 1ull << 1,
140    VN_DEBUG_VTEST = 1ull << 2,
141    VN_DEBUG_WSI = 1ull << 3,
142    VN_DEBUG_NO_ABORT = 1ull << 4,
143 };
144
145 enum vn_perf {
146    VN_PERF_NO_ASYNC_SET_ALLOC = 1ull << 0,
147 };
148
149 typedef uint64_t vn_object_id;
150
151 /* base class of vn_instance */
152 struct vn_instance_base {
153    struct vk_instance base;
154    vn_object_id id;
155 };
156
157 /* base class of vn_physical_device */
158 struct vn_physical_device_base {
159    struct vk_physical_device base;
160    vn_object_id id;
161 };
162
163 /* base class of vn_device */
164 struct vn_device_base {
165    struct vk_device base;
166    vn_object_id id;
167 };
168
169 /* base class of other driver objects */
170 struct vn_object_base {
171    struct vk_object_base base;
172    vn_object_id id;
173 };
174
175 struct vn_refcount {
176    atomic_int count;
177 };
178
179 struct vn_env {
180    uint64_t debug;
181    uint64_t perf;
182 };
183 extern struct vn_env vn_env;
184
185 void
186 vn_env_init(void);
187
188 void
189 vn_trace_init(void);
190
191 void
192 vn_log(struct vn_instance *instance, const char *format, ...)
193    PRINTFLIKE(2, 3);
194
195 VkResult
196 vn_log_result(struct vn_instance *instance,
197               VkResult result,
198               const char *where);
199
200 #define VN_REFCOUNT_INIT(val)                                                \
201    (struct vn_refcount) { .count = (val) }
202
203 static inline int
204 vn_refcount_load_relaxed(const struct vn_refcount *ref)
205 {
206    return atomic_load_explicit(&ref->count, memory_order_relaxed);
207 }
208
209 static inline int
210 vn_refcount_fetch_add_relaxed(struct vn_refcount *ref, int val)
211 {
212    return atomic_fetch_add_explicit(&ref->count, val, memory_order_relaxed);
213 }
214
215 static inline int
216 vn_refcount_fetch_sub_release(struct vn_refcount *ref, int val)
217 {
218    return atomic_fetch_sub_explicit(&ref->count, val, memory_order_release);
219 }
220
221 static inline bool
222 vn_refcount_is_valid(const struct vn_refcount *ref)
223 {
224    return vn_refcount_load_relaxed(ref) > 0;
225 }
226
227 static inline void
228 vn_refcount_inc(struct vn_refcount *ref)
229 {
230    /* no ordering imposed */
231    ASSERTED const int old = vn_refcount_fetch_add_relaxed(ref, 1);
232    assert(old >= 1);
233 }
234
235 static inline bool
236 vn_refcount_dec(struct vn_refcount *ref)
237 {
238    /* prior reads/writes cannot be reordered after this */
239    const int old = vn_refcount_fetch_sub_release(ref, 1);
240    assert(old >= 1);
241
242    /* subsequent free cannot be reordered before this */
243    if (old == 1)
244       atomic_thread_fence(memory_order_acquire);
245
246    return old == 1;
247 }
248
249 uint32_t
250 vn_extension_get_spec_version(const char *name);
251
252 void
253 vn_relax(uint32_t *iter, const char *reason);
254
255 static_assert(sizeof(vn_object_id) >= sizeof(uintptr_t), "");
256
257 static inline VkResult
258 vn_instance_base_init(
259    struct vn_instance_base *instance,
260    const struct vk_instance_extension_table *supported_extensions,
261    const struct vk_instance_dispatch_table *dispatch_table,
262    const VkInstanceCreateInfo *info,
263    const VkAllocationCallbacks *alloc)
264 {
265    VkResult result = vk_instance_init(&instance->base, supported_extensions,
266                                       dispatch_table, info, alloc);
267    instance->id = (uintptr_t)instance;
268    return result;
269 }
270
271 static inline void
272 vn_instance_base_fini(struct vn_instance_base *instance)
273 {
274    vk_instance_finish(&instance->base);
275 }
276
277 static inline VkResult
278 vn_physical_device_base_init(
279    struct vn_physical_device_base *physical_dev,
280    struct vn_instance_base *instance,
281    const struct vk_device_extension_table *supported_extensions,
282    const struct vk_physical_device_dispatch_table *dispatch_table)
283 {
284    VkResult result =
285       vk_physical_device_init(&physical_dev->base, &instance->base,
286                               supported_extensions, dispatch_table);
287    physical_dev->id = (uintptr_t)physical_dev;
288    return result;
289 }
290
291 static inline void
292 vn_physical_device_base_fini(struct vn_physical_device_base *physical_dev)
293 {
294    vk_physical_device_finish(&physical_dev->base);
295 }
296
297 static inline VkResult
298 vn_device_base_init(struct vn_device_base *dev,
299                     struct vn_physical_device_base *physical_dev,
300                     const struct vk_device_dispatch_table *dispatch_table,
301                     const VkDeviceCreateInfo *info,
302                     const VkAllocationCallbacks *alloc)
303 {
304    VkResult result = vk_device_init(&dev->base, &physical_dev->base,
305                                     dispatch_table, info, alloc);
306    dev->id = (uintptr_t)dev;
307    return result;
308 }
309
310 static inline void
311 vn_device_base_fini(struct vn_device_base *dev)
312 {
313    vk_device_finish(&dev->base);
314 }
315
316 static inline void
317 vn_object_base_init(struct vn_object_base *obj,
318                     VkObjectType type,
319                     struct vn_device_base *dev)
320 {
321    vk_object_base_init(&dev->base, &obj->base, type);
322    obj->id = (uintptr_t)obj;
323 }
324
325 static inline void
326 vn_object_base_fini(struct vn_object_base *obj)
327 {
328    vk_object_base_finish(&obj->base);
329 }
330
331 static inline void
332 vn_object_set_id(void *obj, vn_object_id id, VkObjectType type)
333 {
334    assert(((const struct vk_object_base *)obj)->type == type);
335    switch (type) {
336    case VK_OBJECT_TYPE_INSTANCE:
337       ((struct vn_instance_base *)obj)->id = id;
338       break;
339    case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
340       ((struct vn_physical_device_base *)obj)->id = id;
341       break;
342    case VK_OBJECT_TYPE_DEVICE:
343       ((struct vn_device_base *)obj)->id = id;
344       break;
345    default:
346       ((struct vn_object_base *)obj)->id = id;
347       break;
348    }
349 }
350
351 static inline vn_object_id
352 vn_object_get_id(const void *obj, VkObjectType type)
353 {
354    assert(((const struct vk_object_base *)obj)->type == type);
355    switch (type) {
356    case VK_OBJECT_TYPE_INSTANCE:
357       return ((struct vn_instance_base *)obj)->id;
358    case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
359       return ((struct vn_physical_device_base *)obj)->id;
360    case VK_OBJECT_TYPE_DEVICE:
361       return ((struct vn_device_base *)obj)->id;
362    default:
363       return ((struct vn_object_base *)obj)->id;
364    }
365 }
366
367 #endif /* VN_COMMON_H */