vk: Set binding table layout for CS
[mesa.git] / src / vulkan / util.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <assert.h>
30
31 #include "private.h"
32
33 void anv_printflike(3, 4)
34 __anv_finishme(const char *file, int line, const char *format, ...)
35 {
36 va_list ap;
37 char buffer[256];
38
39 va_start(ap, format);
40 vsnprintf(buffer, sizeof(buffer), format, ap);
41 va_end(ap);
42
43 fprintf(stderr, "%s:%d: FINISHME: %s\n", file, line, buffer);
44 }
45
46 void anv_noreturn anv_printflike(1, 2)
47 anv_abortf(const char *format, ...)
48 {
49 va_list va;
50
51 va_start(va, format);
52 anv_abortfv(format, va);
53 va_end(va);
54 }
55
56 void anv_noreturn
57 anv_abortfv(const char *format, va_list va)
58 {
59 fprintf(stderr, "vk: error: ");
60 vfprintf(stderr, format, va);
61 fprintf(stderr, "\n");
62 abort();
63 }
64
65 int
66 anv_vector_init(struct anv_vector *vector, uint32_t element_size, uint32_t size)
67 {
68 assert(is_power_of_two(size));
69 assert(element_size < size && is_power_of_two(element_size));
70
71 vector->head = 0;
72 vector->tail = 0;
73 vector->element_size = element_size;
74 vector->size = size;
75 vector->data = malloc(size);
76
77 return vector->data != NULL;
78 }
79
80 void *
81 anv_vector_add(struct anv_vector *vector)
82 {
83 uint32_t offset, size, split, tail;
84 void *data;
85
86 if (vector->head - vector->tail == vector->size) {
87 size = vector->size * 2;
88 data = malloc(size);
89 if (data == NULL)
90 return NULL;
91 split = ALIGN_U32(vector->tail, vector->size);
92 tail = vector->tail & (vector->size - 1);
93 if (vector->head - split < vector->size) {
94 memcpy(data + tail,
95 vector->data + tail,
96 split - vector->tail);
97 memcpy(data + vector->size,
98 vector->data, vector->head - split);
99 } else {
100 memcpy(data + tail,
101 vector->data + tail,
102 vector->head - vector->tail);
103 }
104 free(vector->data);
105 vector->data = data;
106 vector->size = size;
107 }
108
109 assert(vector->head - vector->tail < vector->size);
110
111 offset = vector->head & (vector->size - 1);
112 vector->head += vector->element_size;
113
114 return vector->data + offset;
115 }
116
117 void *
118 anv_vector_remove(struct anv_vector *vector)
119 {
120 uint32_t offset;
121
122 if (vector->head == vector->tail)
123 return NULL;
124
125 assert(vector->head - vector->tail <= vector->size);
126
127 offset = vector->tail & (vector->size - 1);
128 vector->tail += vector->element_size;
129
130 return vector->data + offset;
131 }