2 * Copyright 2019 Google LLC
3 * SPDX-License-Identifier: MIT
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
17 #include <stdatomic.h>
23 #include <vulkan/vulkan.h>
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"
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"
43 #include "vn_entrypoints.h"
45 #define VN_DEFAULT_ALIGN 8
47 #define VN_DEBUG(category) (unlikely(vn_env.debug & VN_DEBUG_##category))
48 #define VN_PERF(category) (unlikely(vn_env.perf & VN_PERF_##category))
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)))
57 #include <cutils/trace.h>
59 #define VN_TRACE_BEGIN(name) atrace_begin(ATRACE_TAG_GRAPHICS, name)
60 #define VN_TRACE_END() atrace_end(ATRACE_TAG_GRAPHICS)
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()
70 #if __has_attribute(cleanup) && __has_attribute(unused)
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)
81 vn_trace_scope_begin(const char *name)
88 vn_trace_scope_end(int *scope)
95 #define VN_TRACE_SCOPE(name)
97 #endif /* __has_attribute(cleanup) && __has_attribute(unused) */
99 #define VN_TRACE_FUNC() VN_TRACE_SCOPE(__func__)
102 struct vn_physical_device;
107 struct vn_device_memory;
109 struct vn_buffer_view;
111 struct vn_image_view;
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;
121 struct vn_query_pool;
122 struct vn_shader_module;
123 struct vn_pipeline_layout;
124 struct vn_pipeline_cache;
126 struct vn_command_pool;
127 struct vn_command_buffer;
129 struct vn_cs_encoder;
130 struct vn_cs_decoder;
133 struct vn_renderer_shmem;
134 struct vn_renderer_bo;
135 struct vn_renderer_sync;
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,
146 VN_PERF_NO_ASYNC_SET_ALLOC = 1ull << 0,
149 typedef uint64_t vn_object_id;
151 /* base class of vn_instance */
152 struct vn_instance_base {
153 struct vk_instance base;
157 /* base class of vn_physical_device */
158 struct vn_physical_device_base {
159 struct vk_physical_device base;
163 /* base class of vn_device */
164 struct vn_device_base {
165 struct vk_device base;
169 /* base class of other driver objects */
170 struct vn_object_base {
171 struct vk_object_base base;
183 extern struct vn_env vn_env;
192 vn_log(struct vn_instance *instance, const char *format, ...)
196 vn_log_result(struct vn_instance *instance,
200 #define VN_REFCOUNT_INIT(val) \
201 (struct vn_refcount) { .count = (val) }
204 vn_refcount_load_relaxed(const struct vn_refcount *ref)
206 return atomic_load_explicit(&ref->count, memory_order_relaxed);
210 vn_refcount_fetch_add_relaxed(struct vn_refcount *ref, int val)
212 return atomic_fetch_add_explicit(&ref->count, val, memory_order_relaxed);
216 vn_refcount_fetch_sub_release(struct vn_refcount *ref, int val)
218 return atomic_fetch_sub_explicit(&ref->count, val, memory_order_release);
222 vn_refcount_is_valid(const struct vn_refcount *ref)
224 return vn_refcount_load_relaxed(ref) > 0;
228 vn_refcount_inc(struct vn_refcount *ref)
230 /* no ordering imposed */
231 ASSERTED const int old = vn_refcount_fetch_add_relaxed(ref, 1);
236 vn_refcount_dec(struct vn_refcount *ref)
238 /* prior reads/writes cannot be reordered after this */
239 const int old = vn_refcount_fetch_sub_release(ref, 1);
242 /* subsequent free cannot be reordered before this */
244 atomic_thread_fence(memory_order_acquire);
250 vn_extension_get_spec_version(const char *name);
253 vn_relax(uint32_t *iter, const char *reason);
255 static_assert(sizeof(vn_object_id) >= sizeof(uintptr_t), "");
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)
265 VkResult result = vk_instance_init(&instance->base, supported_extensions,
266 dispatch_table, info, alloc);
267 instance->id = (uintptr_t)instance;
272 vn_instance_base_fini(struct vn_instance_base *instance)
274 vk_instance_finish(&instance->base);
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)
285 vk_physical_device_init(&physical_dev->base, &instance->base,
286 supported_extensions, dispatch_table);
287 physical_dev->id = (uintptr_t)physical_dev;
292 vn_physical_device_base_fini(struct vn_physical_device_base *physical_dev)
294 vk_physical_device_finish(&physical_dev->base);
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)
304 VkResult result = vk_device_init(&dev->base, &physical_dev->base,
305 dispatch_table, info, alloc);
306 dev->id = (uintptr_t)dev;
311 vn_device_base_fini(struct vn_device_base *dev)
313 vk_device_finish(&dev->base);
317 vn_object_base_init(struct vn_object_base *obj,
319 struct vn_device_base *dev)
321 vk_object_base_init(&dev->base, &obj->base, type);
322 obj->id = (uintptr_t)obj;
326 vn_object_base_fini(struct vn_object_base *obj)
328 vk_object_base_finish(&obj->base);
332 vn_object_set_id(void *obj, vn_object_id id, VkObjectType type)
334 assert(((const struct vk_object_base *)obj)->type == type);
336 case VK_OBJECT_TYPE_INSTANCE:
337 ((struct vn_instance_base *)obj)->id = id;
339 case VK_OBJECT_TYPE_PHYSICAL_DEVICE:
340 ((struct vn_physical_device_base *)obj)->id = id;
342 case VK_OBJECT_TYPE_DEVICE:
343 ((struct vn_device_base *)obj)->id = id;
346 ((struct vn_object_base *)obj)->id = id;
351 static inline vn_object_id
352 vn_object_get_id(const void *obj, VkObjectType type)
354 assert(((const struct vk_object_base *)obj)->type == 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;
363 return ((struct vn_object_base *)obj)->id;
367 #endif /* VN_COMMON_H */