turnip: add tu_cs_reserve_space(_assert)
[mesa.git] / src / freedreno / vulkan / tu_cs.c
1 /*
2 * Copyright © 2019 Google LLC
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "tu_cs.h"
25
26 /**
27 * Initialize a command stream.
28 */
29 void
30 tu_cs_init(struct tu_cs *cs)
31 {
32 cs->start = cs->cur = cs->end = NULL;
33
34 cs->entry_count = cs->entry_capacity = 0;
35 cs->entries = NULL;
36
37 cs->bo_count = cs->bo_capacity = 0;
38 cs->bos = NULL;
39 }
40
41 /**
42 * Finish and release all resources owned by a command stream.
43 */
44 void
45 tu_cs_finish(struct tu_device *dev, struct tu_cs *cs)
46 {
47 for (uint32_t i = 0; i < cs->bo_count; ++i) {
48 tu_bo_finish(dev, cs->bos[i]);
49 free(cs->bos[i]);
50 }
51
52 free(cs->entries);
53 free(cs->bos);
54 }
55
56 /*
57 * Allocate and add a BO to a command stream. Following command packets will
58 * be emitted to the new BO.
59 */
60 static VkResult
61 tu_cs_add_bo(struct tu_device *dev, struct tu_cs *cs, uint32_t byte_size)
62 {
63 /* grow cs->bos if needed */
64 if (cs->bo_count == cs->bo_capacity) {
65 uint32_t new_capacity = MAX2(4, 2 * cs->bo_capacity);
66 struct tu_bo **new_bos =
67 realloc(cs->bos, new_capacity * sizeof(struct tu_bo *));
68 if (!new_bos)
69 return VK_ERROR_OUT_OF_HOST_MEMORY;
70
71 cs->bo_capacity = new_capacity;
72 cs->bos = new_bos;
73 }
74
75 struct tu_bo *new_bo = malloc(sizeof(struct tu_bo));
76 if (!new_bo)
77 return VK_ERROR_OUT_OF_HOST_MEMORY;
78
79 VkResult result = tu_bo_init_new(dev, new_bo, byte_size);
80 if (result != VK_SUCCESS) {
81 free(new_bo);
82 return result;
83 }
84
85 result = tu_bo_map(dev, new_bo);
86 if (result != VK_SUCCESS) {
87 tu_bo_finish(dev, new_bo);
88 free(new_bo);
89 return result;
90 }
91
92 cs->bos[cs->bo_count++] = new_bo;
93
94 cs->start = cs->cur = (uint32_t *) new_bo->map;
95 cs->end = cs->start + new_bo->size / sizeof(uint32_t);
96
97 return VK_SUCCESS;
98 }
99
100 /**
101 * Begin (or continue) command packet emission. This will reserve space from
102 * the command stream for at least \a reserve_size uint32_t values.
103 */
104 VkResult
105 tu_cs_begin(struct tu_device *dev, struct tu_cs *cs, uint32_t reserve_size)
106 {
107 /* no dangling command packet */
108 assert(cs->start == cs->cur);
109
110 if (cs->end - cs->cur < reserve_size) {
111 uint32_t new_size = MAX2(16384, reserve_size * sizeof(uint32_t));
112 if (cs->bo_count)
113 new_size = MAX2(new_size, cs->bos[cs->bo_count - 1]->size * 2);
114
115 VkResult result = tu_cs_add_bo(dev, cs, new_size);
116 if (result != VK_SUCCESS)
117 return result;
118 }
119
120 assert(cs->end - cs->cur >= reserve_size);
121
122 return VK_SUCCESS;
123 }
124
125 /**
126 * End command packet emission by adding an IB entry for the command packets
127 * emitted since the last call to tu_cs_begin.
128 */
129 VkResult
130 tu_cs_end(struct tu_cs *cs)
131 {
132 /* no command packet at all */
133 if (cs->start == cs->cur)
134 return VK_SUCCESS;
135
136 /* grow cs->entries if needed */
137 if (cs->entry_capacity == cs->entry_count) {
138 uint32_t new_capacity = MAX2(cs->entry_capacity * 2, 4);
139 struct tu_cs_entry *new_entries =
140 realloc(cs->entries, new_capacity * sizeof(struct tu_cs_entry));
141 if (!new_entries)
142 return VK_ERROR_OUT_OF_HOST_MEMORY;
143
144 cs->entries = new_entries;
145 cs->entry_capacity = new_capacity;
146 }
147
148 assert(cs->bo_count);
149 const struct tu_bo *bo = cs->bos[cs->bo_count - 1];
150
151 /* add an entry for [cs->start, cs->cur] */
152 cs->entries[cs->entry_count++] = (struct tu_cs_entry) {
153 .bo = bo,
154 .size = (cs->cur - cs->start) * sizeof(uint32_t),
155 .offset = (cs->start - (uint32_t *) bo->map) * sizeof(uint32_t),
156 };
157
158 cs->start = cs->cur;
159
160 return VK_SUCCESS;
161 }
162
163 /**
164 * Reset a command stream to its initial state. This discards all comand
165 * packets in \a cs, but does not necessarily release all resources.
166 */
167 void
168 tu_cs_reset(struct tu_device *dev, struct tu_cs *cs)
169 {
170 for (uint32_t i = 0; i + 1 < cs->bo_count; ++i) {
171 tu_bo_finish(dev, cs->bos[i]);
172 free(cs->bos[i]);
173 }
174
175 if (cs->bo_count) {
176 cs->bos[0] = cs->bos[cs->bo_count - 1];
177 cs->bo_count = 1;
178
179 cs->start = cs->cur = (uint32_t *) cs->bos[0]->map;
180 cs->end = cs->start + cs->bos[0]->size / sizeof(uint32_t);
181 }
182
183 cs->entry_count = 0;
184 }