2 * Copyright © 2019 Google LLC
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:
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
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.
27 * Initialize a command stream.
30 tu_cs_init(struct tu_cs
*cs
)
32 cs
->start
= cs
->cur
= cs
->end
= NULL
;
34 cs
->entry_count
= cs
->entry_capacity
= 0;
37 cs
->bo_count
= cs
->bo_capacity
= 0;
42 * Finish and release all resources owned by a command stream.
45 tu_cs_finish(struct tu_device
*dev
, struct tu_cs
*cs
)
47 for (uint32_t i
= 0; i
< cs
->bo_count
; ++i
) {
48 tu_bo_finish(dev
, cs
->bos
[i
]);
57 * Allocate and add a BO to a command stream. Following command packets will
58 * be emitted to the new BO.
61 tu_cs_add_bo(struct tu_device
*dev
, struct tu_cs
*cs
, uint32_t byte_size
)
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
*));
69 return VK_ERROR_OUT_OF_HOST_MEMORY
;
71 cs
->bo_capacity
= new_capacity
;
75 struct tu_bo
*new_bo
= malloc(sizeof(struct tu_bo
));
77 return VK_ERROR_OUT_OF_HOST_MEMORY
;
79 VkResult result
= tu_bo_init_new(dev
, new_bo
, byte_size
);
80 if (result
!= VK_SUCCESS
) {
85 result
= tu_bo_map(dev
, new_bo
);
86 if (result
!= VK_SUCCESS
) {
87 tu_bo_finish(dev
, new_bo
);
92 cs
->bos
[cs
->bo_count
++] = new_bo
;
94 cs
->start
= cs
->cur
= (uint32_t *) new_bo
->map
;
95 cs
->end
= cs
->start
+ new_bo
->size
/ sizeof(uint32_t);
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.
105 tu_cs_begin(struct tu_device
*dev
, struct tu_cs
*cs
, uint32_t reserve_size
)
107 assert(reserve_size
);
109 if (cs
->end
- cs
->cur
< reserve_size
) {
110 uint32_t new_size
= MAX2(16384, reserve_size
* sizeof(uint32_t));
112 new_size
= MAX2(new_size
, cs
->bos
[cs
->bo_count
- 1]->size
* 2);
114 VkResult result
= tu_cs_add_bo(dev
, cs
, new_size
);
115 if (result
!= VK_SUCCESS
)
124 * End command packet emission by adding an IB entry for the command packets
125 * emitted since the last call to tu_cs_begin.
128 tu_cs_end(struct tu_cs
*cs
)
130 if (cs
->start
== cs
->cur
)
133 if (cs
->entry_capacity
== cs
->entry_count
) {
134 uint32_t new_capacity
= MAX2(cs
->entry_capacity
* 2, 4);
135 struct tu_cs_entry
*new_entries
=
136 realloc(cs
->entries
, new_capacity
* sizeof(struct tu_cs_entry
));
140 cs
->entries
= new_entries
;
141 cs
->entry_capacity
= new_capacity
;
144 assert(cs
->bo_count
);
146 struct tu_cs_entry entry
;
147 entry
.bo
= cs
->bos
[cs
->bo_count
- 1];
148 entry
.size
= (cs
->cur
- cs
->start
) * sizeof(uint32_t);
149 entry
.offset
= (cs
->start
- (uint32_t *) entry
.bo
->map
) * sizeof(uint32_t);
151 cs
->entries
[cs
->entry_count
] = entry
;
158 * Reset a command stream to its initial state. This discards all comand
159 * packets in \a cs, but does not necessarily release all resources.
162 tu_cs_reset(struct tu_device
*dev
, struct tu_cs
*cs
)
164 for (uint32_t i
= 0; i
+ 1 < cs
->bo_count
; ++i
) {
165 tu_bo_finish(dev
, cs
->bos
[i
]);
170 cs
->bos
[0] = cs
->bos
[cs
->bo_count
- 1];
173 cs
->start
= cs
->cur
= (uint32_t *) cs
->bos
[0]->map
;
174 cs
->end
= cs
->start
+ cs
->bos
[0]->size
/ sizeof(uint32_t);
181 * Reserve space from a command stream for \a size uint32_t values.
184 tu_cs_check_space(struct tu_device
*dev
, struct tu_cs
*cs
, size_t size
)
186 if (cs
->end
- cs
->cur
>= size
)
189 VkResult result
= tu_cs_end(cs
);
190 if (result
!= VK_SUCCESS
)
193 return tu_cs_begin(dev
, cs
, size
);