summaryrefslogtreecommitdiff
path: root/src/utils.h
blob: ee0b6c6b45724ac6666950387310469e1e36726a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#ifndef _UTILS_H_
#define _UTILS_H_

#define len(X) sizeof(X) / sizeof(*X)

// Yes, i know.
#define min(X, Y) X < Y ? X : Y
#define max(X, Y) X > Y ? X : Y

#define array(type) { size_t len, cap; type *data; }
#define array_init(array) do {\
		(array).len = 0; \
		(array).cap = 5; \
		(array).data = calloc(5, sizeof(*(array).data)); \
	} while (0)
#define array_new(type) { \
		.len = 0, \
		.cap = 5, \
		.data = calloc(5, sizeof(type)) \
	}

#define foreach(item, array) \
	for (typeof((array).data) item = (array).data; item < (array).data + (array).len; item++)

#define array_append(array, item) do { \
		ensure_ptr((array).data, (array).len + 1, (array).cap); \
		(array).data[(array).len++] = item; \
	} while(0)

#define ensure(var, ptr_mem, len_mem, cap_mem) ensure_ptr((var).ptr_mem, (var).len_mem, (var).cap_mem)
#define ensure_ptr(ptr, len, cap) do { \
		assert(ptr); \
		if ((len) > (cap)) { \
			(ptr) = realloc((ptr), ((cap) = (cap) * 2 > (len) ? (cap) * 2 : (len)) * sizeof(*(ptr))); \
			if (!(ptr)) \
				abort(); \
		} \
	} while (0)

#define STRINGFY(X) #X
#define STRGY(X) STRINGFY(X)


#ifndef NDEBUG
#	include <stdio.h>
#	define dbglog(F) fprintf(stderr, __FILE__ ":%s:" STRGY(__LINE__) ": " F "\n", __func__)
#	define dbglogf(F, ...) fprintf(stderr, __FILE__ ":%s:" STRGY(__LINE__) ": " F "\n", __func__, __VA_ARGS__)
#else
#	define dbglog(F) do { /* noop */ } while(0)
#	define dbglogf(F, X) do { /* noop */ } while(0)
#endif

#include <vulkan/vk_enum_string_helper.h>
#include <stdbool.h>

static inline bool vlkn_check_expr(VkResult exp, const char *exp_str) {
	if (exp == VK_SUCCESS)
		return true;
	dbglogf("%s failed with %s", exp_str, string_VkResult(exp));
	return false;
}

#define vlkn_check(exp) vlkn_check_expr(exp, #exp)

#include <vulkan/vulkan.h>
struct vlkn_renderer;

VkCommandBuffer begin_single_command(struct vlkn_renderer *ren);
void end_single_command(struct vlkn_renderer *ren, VkCommandBuffer buffer);

#endif