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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/*
* Copyright © 2019 Google LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "tu_cs.h"
/**
* Initialize a command stream.
*/
void
tu_cs_init(struct tu_cs *cs,
struct tu_device *device,
enum tu_cs_mode mode,
uint32_t initial_size)
{
assert(mode != TU_CS_MODE_EXTERNAL);
memset(cs, 0, sizeof(*cs));
cs->device = device;
cs->mode = mode;
cs->next_bo_size = initial_size;
}
/**
* Initialize a command stream as a wrapper to an external buffer.
*/
void
tu_cs_init_external(struct tu_cs *cs, uint32_t *start, uint32_t *end)
{
memset(cs, 0, sizeof(*cs));
cs->mode = TU_CS_MODE_EXTERNAL;
cs->start = cs->reserved_end = cs->cur = start;
cs->end = end;
}
/**
* Finish and release all resources owned by a command stream.
*/
void
tu_cs_finish(struct tu_cs *cs)
{
for (uint32_t i = 0; i < cs->bo_count; ++i) {
tu_bo_finish(cs->device, cs->bos[i]);
free(cs->bos[i]);
}
free(cs->entries);
free(cs->bos);
}
/**
* Get the offset of the command packets emitted since the last call to
* tu_cs_add_entry.
*/
static uint32_t
tu_cs_get_offset(const struct tu_cs *cs)
{
assert(cs->bo_count);
return cs->start - (uint32_t *) cs->bos[cs->bo_count - 1]->map;
}
/*
* Allocate and add a BO to a command stream. Following command packets will
* be emitted to the new BO.
*/
static VkResult
tu_cs_add_bo(struct tu_cs *cs, uint32_t size)
{
/* no BO for TU_CS_MODE_EXTERNAL */
assert(cs->mode != TU_CS_MODE_EXTERNAL);
/* no dangling command packet */
assert(tu_cs_is_empty(cs));
/* grow cs->bos if needed */
if (cs->bo_count == cs->bo_capacity) {
uint32_t new_capacity = MAX2(4, 2 * cs->bo_capacity);
struct tu_bo **new_bos =
realloc(cs->bos, new_capacity * sizeof(struct tu_bo *));
if (!new_bos)
return VK_ERROR_OUT_OF_HOST_MEMORY;
cs->bo_capacity = new_capacity;
cs->bos = new_bos;
}
struct tu_bo *new_bo = malloc(sizeof(struct tu_bo));
if (!new_bo)
return VK_ERROR_OUT_OF_HOST_MEMORY;
VkResult result =
tu_bo_init_new(cs->device, new_bo, size * sizeof(uint32_t));
if (result != VK_SUCCESS) {
free(new_bo);
return result;
}
result = tu_bo_map(cs->device, new_bo);
if (result != VK_SUCCESS) {
tu_bo_finish(cs->device, new_bo);
free(new_bo);
return result;
}
cs->bos[cs->bo_count++] = new_bo;
cs->start = cs->cur = cs->reserved_end = (uint32_t *) new_bo->map;
cs->end = cs->start + new_bo->size / sizeof(uint32_t);
return VK_SUCCESS;
}
/**
* Reserve an IB entry.
*/
static VkResult
tu_cs_reserve_entry(struct tu_cs *cs)
{
/* entries are only for TU_CS_MODE_GROW */
assert(cs->mode == TU_CS_MODE_GROW);
/* grow cs->entries if needed */
if (cs->entry_count == cs->entry_capacity) {
uint32_t new_capacity = MAX2(4, cs->entry_capacity * 2);
struct tu_cs_entry *new_entries =
realloc(cs->entries, new_capacity * sizeof(struct tu_cs_entry));
if (!new_entries)
return VK_ERROR_OUT_OF_HOST_MEMORY;
cs->entry_capacity = new_capacity;
cs->entries = new_entries;
}
return VK_SUCCESS;
}
/**
* Add an IB entry for the command packets emitted since the last call to this
* function.
*/
static void
tu_cs_add_entry(struct tu_cs *cs)
{
/* entries are only for TU_CS_MODE_GROW */
assert(cs->mode == TU_CS_MODE_GROW);
/* disallow empty entry */
assert(!tu_cs_is_empty(cs));
/*
* because we disallow empty entry, tu_cs_add_bo and tu_cs_reserve_entry
* must both have been called
*/
assert(cs->bo_count);
assert(cs->entry_count < cs->entry_capacity);
/* add an entry for [cs->start, cs->cur] */
cs->entries[cs->entry_count++] = (struct tu_cs_entry) {
.bo = cs->bos[cs->bo_count - 1],
.size = tu_cs_get_size(cs) * sizeof(uint32_t),
.offset = tu_cs_get_offset(cs) * sizeof(uint32_t),
};
cs->start = cs->cur;
}
/**
* same behavior as tu_cs_emit_call but without the indirect
*/
VkResult
tu_cs_add_entries(struct tu_cs *cs, struct tu_cs *target)
{
VkResult result;
assert(cs->mode == TU_CS_MODE_GROW);
assert(target->mode == TU_CS_MODE_GROW);
if (!tu_cs_is_empty(cs))
tu_cs_add_entry(cs);
for (unsigned i = 0; i < target->entry_count; i++) {
result = tu_cs_reserve_entry(cs);
if (result != VK_SUCCESS)
return result;
cs->entries[cs->entry_count++] = target->entries[i];
}
return VK_SUCCESS;
}
/**
* Begin (or continue) command packet emission. This does nothing but sanity
* checks currently. \a cs must not be in TU_CS_MODE_SUB_STREAM mode.
*/
void
tu_cs_begin(struct tu_cs *cs)
{
assert(cs->mode != TU_CS_MODE_SUB_STREAM);
assert(tu_cs_is_empty(cs));
}
/**
* End command packet emission. This adds an IB entry when \a cs is in
* TU_CS_MODE_GROW mode.
*/
void
tu_cs_end(struct tu_cs *cs)
{
assert(cs->mode != TU_CS_MODE_SUB_STREAM);
if (cs->mode == TU_CS_MODE_GROW && !tu_cs_is_empty(cs))
tu_cs_add_entry(cs);
}
/**
* Begin command packet emission to a sub-stream. \a cs must be in
* TU_CS_MODE_SUB_STREAM mode.
*
* Return \a sub_cs which is in TU_CS_MODE_EXTERNAL mode. tu_cs_begin and
* tu_cs_reserve_space are implied and \a sub_cs is ready for command packet
* emission.
*/
VkResult
tu_cs_begin_sub_stream(struct tu_cs *cs, uint32_t size, struct tu_cs *sub_cs)
{
assert(cs->mode == TU_CS_MODE_SUB_STREAM);
assert(size);
VkResult result = tu_cs_reserve_space(cs, size);
if (result != VK_SUCCESS)
return result;
tu_cs_init_external(sub_cs, cs->cur, cs->reserved_end);
tu_cs_begin(sub_cs);
result = tu_cs_reserve_space(sub_cs, size);
assert(result == VK_SUCCESS);
return VK_SUCCESS;
}
/**
* Allocate count*size dwords, aligned to size dwords.
* \a cs must be in TU_CS_MODE_SUB_STREAM mode.
*
*/
VkResult
tu_cs_alloc(struct tu_cs *cs,
uint32_t count,
uint32_t size,
struct ts_cs_memory *memory)
{
assert(cs->mode == TU_CS_MODE_SUB_STREAM);
assert(size && size <= 1024);
if (!count)
return VK_SUCCESS;
/* TODO: smarter way to deal with alignment? */
VkResult result = tu_cs_reserve_space(cs, count * size + (size-1));
if (result != VK_SUCCESS)
return result;
struct tu_bo *bo = cs->bos[cs->bo_count - 1];
size_t offset = align(tu_cs_get_offset(cs), size);
memory->map = bo->map + offset * sizeof(uint32_t);
memory->iova = bo->iova + offset * sizeof(uint32_t);
cs->start = cs->cur = (uint32_t*) bo->map + offset + count * size;
return VK_SUCCESS;
}
/**
* End command packet emission to a sub-stream. \a sub_cs becomes invalid
* after this call.
*
* Return an IB entry for the sub-stream. The entry has the same lifetime as
* \a cs.
*/
struct tu_cs_entry
tu_cs_end_sub_stream(struct tu_cs *cs, struct tu_cs *sub_cs)
{
assert(cs->mode == TU_CS_MODE_SUB_STREAM);
assert(cs->bo_count);
assert(sub_cs->start == cs->cur && sub_cs->end == cs->reserved_end);
tu_cs_sanity_check(sub_cs);
tu_cs_end(sub_cs);
cs->cur = sub_cs->cur;
struct tu_cs_entry entry = {
.bo = cs->bos[cs->bo_count - 1],
.size = tu_cs_get_size(cs) * sizeof(uint32_t),
.offset = tu_cs_get_offset(cs) * sizeof(uint32_t),
};
cs->start = cs->cur;
return entry;
}
/**
* Reserve space from a command stream for \a reserved_size uint32_t values.
* This never fails when \a cs has mode TU_CS_MODE_EXTERNAL.
*/
VkResult
tu_cs_reserve_space(struct tu_cs *cs, uint32_t reserved_size)
{
if (tu_cs_get_space(cs) < reserved_size) {
if (cs->mode == TU_CS_MODE_EXTERNAL) {
unreachable("cannot grow external buffer");
return VK_ERROR_OUT_OF_HOST_MEMORY;
}
/* add an entry for the exiting command packets */
if (!tu_cs_is_empty(cs)) {
/* no direct command packet for TU_CS_MODE_SUB_STREAM */
assert(cs->mode != TU_CS_MODE_SUB_STREAM);
tu_cs_add_entry(cs);
}
if (cs->cond_flags) {
/* Subtract one here to account for the DWORD field itself. */
*cs->cond_dwords = cs->cur - cs->cond_dwords - 1;
/* space for CP_COND_REG_EXEC in next bo */
reserved_size += 3;
}
/* switch to a new BO */
uint32_t new_size = MAX2(cs->next_bo_size, reserved_size);
VkResult result = tu_cs_add_bo(cs, new_size);
if (result != VK_SUCCESS)
return result;
/* if inside a condition, emit a new CP_COND_REG_EXEC */
if (cs->cond_flags) {
cs->reserved_end = cs->cur + reserved_size;
tu_cs_emit_pkt7(cs, CP_COND_REG_EXEC, 2);
tu_cs_emit(cs, cs->cond_flags);
cs->cond_dwords = cs->cur;
/* Emit dummy DWORD field here */
tu_cs_emit(cs, CP_COND_REG_EXEC_1_DWORDS(0));
}
/* double the size for the next bo */
new_size <<= 1;
if (cs->next_bo_size < new_size)
cs->next_bo_size = new_size;
}
assert(tu_cs_get_space(cs) >= reserved_size);
cs->reserved_end = cs->cur + reserved_size;
if (cs->mode == TU_CS_MODE_GROW) {
/* reserve an entry for the next call to this function or tu_cs_end */
return tu_cs_reserve_entry(cs);
}
return VK_SUCCESS;
}
/**
* Reset a command stream to its initial state. This discards all comand
* packets in \a cs, but does not necessarily release all resources.
*/
void
tu_cs_reset(struct tu_cs *cs)
{
if (cs->mode == TU_CS_MODE_EXTERNAL) {
assert(!cs->bo_count && !cs->entry_count);
cs->reserved_end = cs->cur = cs->start;
return;
}
for (uint32_t i = 0; i + 1 < cs->bo_count; ++i) {
tu_bo_finish(cs->device, cs->bos[i]);
free(cs->bos[i]);
}
if (cs->bo_count) {
cs->bos[0] = cs->bos[cs->bo_count - 1];
cs->bo_count = 1;
cs->start = cs->cur = cs->reserved_end = (uint32_t *) cs->bos[0]->map;
cs->end = cs->start + cs->bos[0]->size / sizeof(uint32_t);
}
cs->entry_count = 0;
}
|