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
|
/*
* 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)
{
cs->start = cs->cur = cs->end = NULL;
cs->entry_count = cs->entry_capacity = 0;
cs->entries = NULL;
cs->bo_count = cs->bo_capacity = 0;
cs->bos = NULL;
}
/**
* Finish and release all resources owned by a command stream.
*/
void
tu_cs_finish(struct tu_device *dev, struct tu_cs *cs)
{
for (uint32_t i = 0; i < cs->bo_count; ++i) {
tu_bo_finish(dev, 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_end.
*/
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;
}
/**
* Get the size of the command packets emitted since the last call to
* tu_cs_end.
*/
static uint32_t
tu_cs_get_size(const struct tu_cs *cs)
{
return cs->cur - cs->start;
}
/**
* Get the size of the remaining space in the current BO.
*/
static uint32_t
tu_cs_get_space(const struct tu_cs *cs)
{
return cs->end - cs->cur;
}
/**
* Return true if there is no command packet emitted since the last call to
* tu_cs_end.
*/
static uint32_t
tu_cs_is_empty(const struct tu_cs *cs)
{
return tu_cs_get_size(cs) == 0;
}
/*
* 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_device *dev, struct tu_cs *cs, uint32_t byte_size)
{
/* 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(dev, new_bo, byte_size);
if (result != VK_SUCCESS) {
free(new_bo);
return result;
}
result = tu_bo_map(dev, new_bo);
if (result != VK_SUCCESS) {
tu_bo_finish(dev, new_bo);
free(new_bo);
return result;
}
cs->bos[cs->bo_count++] = new_bo;
cs->start = cs->cur = (uint32_t *) new_bo->map;
cs->end = cs->start + new_bo->size / sizeof(uint32_t);
return VK_SUCCESS;
}
/**
* Begin (or continue) command packet emission. This will reserve space from
* the command stream for at least \a reserve_size uint32_t values.
*/
VkResult
tu_cs_begin(struct tu_device *dev, struct tu_cs *cs, uint32_t reserve_size)
{
/* no dangling command packet */
assert(tu_cs_is_empty(cs));
if (tu_cs_get_space(cs) < reserve_size) {
uint32_t new_size = MAX2(16384, reserve_size * sizeof(uint32_t));
if (cs->bo_count)
new_size = MAX2(new_size, cs->bos[cs->bo_count - 1]->size * 2);
VkResult result = tu_cs_add_bo(dev, cs, new_size);
if (result != VK_SUCCESS)
return result;
}
assert(tu_cs_get_space(cs) >= reserve_size);
return VK_SUCCESS;
}
/**
* End command packet emission by adding an IB entry for the command packets
* emitted since the last call to tu_cs_begin.
*/
VkResult
tu_cs_end(struct tu_cs *cs)
{
/* no command packet at all */
if (tu_cs_is_empty(cs))
return VK_SUCCESS;
/* grow cs->entries if needed */
if (cs->entry_capacity == cs->entry_count) {
uint32_t new_capacity = MAX2(cs->entry_capacity * 2, 4);
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->entries = new_entries;
cs->entry_capacity = new_capacity;
}
assert(cs->bo_count);
/* 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;
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_device *dev, struct tu_cs *cs)
{
for (uint32_t i = 0; i + 1 < cs->bo_count; ++i) {
tu_bo_finish(dev, 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 = (uint32_t *) cs->bos[0]->map;
cs->end = cs->start + cs->bos[0]->size / sizeof(uint32_t);
}
cs->entry_count = 0;
}
|