Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/frontends/nine/nine_buffer_upload.c
4561 views
1
/**************************************************************************
2
*
3
* Copyright 2009 VMware, Inc.
4
* Copyright 2016 Axel Davy <[email protected]>
5
* All Rights Reserved.
6
*
7
* Permission is hereby granted, free of charge, to any person obtaining a
8
* copy of this software and associated documentation files (the
9
* "Software"), to deal in the Software without restriction, including
10
* without limitation the rights to use, copy, modify, merge, publish,
11
* distribute, sub license, and/or sell copies of the Software, and to
12
* permit persons to whom the Software is furnished to do so, subject to
13
* the following conditions:
14
*
15
* The above copyright notice and this permission notice (including the
16
* next paragraph) shall be included in all copies or substantial portions
17
* of the Software.
18
*
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
*
27
**************************************************************************/
28
/* Adapted from u_upload_mgr.
29
* Makes suballocations from bigger allocations,
30
* while enabling fast mapping. */
31
32
#include "pipe/p_defines.h"
33
#include "util/u_inlines.h"
34
#include "pipe/p_context.h"
35
#include "util/u_memory.h"
36
#include "util/u_math.h"
37
#include "util/slab.h"
38
39
#include "nine_buffer_upload.h"
40
41
#include "nine_debug.h"
42
43
#define DBG_CHANNEL (DBG_INDEXBUFFER|DBG_VERTEXBUFFER)
44
45
struct nine_buffer_group {
46
unsigned refcount; /* How many sub-buffers live inside the buffer */
47
struct pipe_resource *resource;
48
struct pipe_transfer *transfer;
49
uint8_t *map;
50
unsigned free_offset; /* Aligned offset to the upload buffer, pointing
51
* at the first unused byte. */
52
};
53
54
struct nine_subbuffer {
55
struct nine_buffer_group *parent; /* Can be NULL */
56
struct pipe_resource *resource; /* The parent resource if apply */
57
unsigned offset; /* Offset inside the resource */
58
/* If there is no parent, the resource map. Else NULL. */
59
struct pipe_transfer *transfer;
60
uint8_t *map;
61
};
62
63
struct nine_buffer_upload {
64
struct pipe_context *pipe;
65
struct slab_mempool buffer_pool;
66
67
unsigned buffers_size; /* Size of the big allocated buffers */
68
unsigned num_buffers;
69
struct nine_buffer_group *buffers;
70
};
71
72
static void
73
nine_upload_create_buffer_group(struct nine_buffer_upload *upload,
74
struct nine_buffer_group *group)
75
{
76
struct pipe_resource resource;
77
struct pipe_screen *screen = upload->pipe->screen;
78
DBG("Allocating %p %p\n", upload, group);
79
80
memset(&resource, 0, sizeof(resource));
81
resource.target = PIPE_BUFFER;
82
resource.format = PIPE_FORMAT_R8_UNORM;
83
resource.bind = PIPE_BIND_VERTEX_BUFFER;
84
resource.usage = PIPE_USAGE_STREAM;
85
resource.width0 = upload->buffers_size;
86
resource.height0 = 1;
87
resource.depth0 = 1;
88
resource.array_size = 1;
89
resource.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
90
PIPE_RESOURCE_FLAG_MAP_COHERENT;
91
92
group->refcount = 0;
93
group->resource = screen->resource_create(screen, &resource);
94
if (group->resource == NULL)
95
return;
96
97
group->map = pipe_buffer_map_range(upload->pipe, group->resource,
98
0, upload->buffers_size,
99
PIPE_MAP_WRITE |
100
#ifdef PIPE_ARCH_X86
101
PIPE_MAP_ONCE |
102
#endif
103
PIPE_MAP_PERSISTENT |
104
PIPE_MAP_COHERENT,
105
&group->transfer);
106
if (group->map == NULL) {
107
group->transfer = NULL;
108
pipe_resource_reference(&group->resource, NULL);
109
return;
110
}
111
112
group->free_offset = 0;
113
DBG("Success: %p %p\n", group->map, group->map+upload->buffers_size);
114
}
115
116
static void
117
nine_upload_destroy_buffer_group(struct nine_buffer_upload *upload,
118
struct nine_buffer_group *group)
119
{
120
DBG("%p %p\n", upload, group);
121
DBG("Release: %p %p\n", group->map, group->map+upload->buffers_size);
122
assert(group->refcount == 0);
123
124
if (group->transfer)
125
pipe_buffer_unmap(upload->pipe, group->transfer);
126
if (group->resource)
127
pipe_resource_reference(&group->resource, NULL);
128
group->transfer = NULL;
129
group->map = NULL;
130
}
131
132
struct nine_buffer_upload *
133
nine_upload_create(struct pipe_context *pipe, unsigned buffers_size,
134
unsigned num_buffers)
135
{
136
struct nine_buffer_upload *upload;
137
int i;
138
139
DBG("\n");
140
141
if (!pipe->screen->get_param(pipe->screen,
142
PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT))
143
return NULL;
144
145
upload = CALLOC_STRUCT(nine_buffer_upload);
146
147
if (!upload)
148
return NULL;
149
150
slab_create(&upload->buffer_pool, sizeof(struct nine_subbuffer), 4096);
151
152
upload->pipe = pipe;
153
upload->buffers_size = align(buffers_size, 4096);
154
upload->num_buffers = num_buffers;
155
156
upload->buffers = CALLOC(num_buffers, sizeof(struct nine_buffer_group));
157
if (!upload->buffers)
158
goto buffers_fail;
159
160
for (i = 0; i < num_buffers; i++)
161
nine_upload_create_buffer_group(upload, &upload->buffers[i]);
162
163
return upload;
164
165
buffers_fail:
166
slab_destroy(&upload->buffer_pool);
167
FREE(upload);
168
return NULL;
169
}
170
171
void
172
nine_upload_destroy(struct nine_buffer_upload *upload)
173
{
174
int i;
175
176
DBG("%p\n", upload);
177
178
for (i = 0; i < upload->num_buffers; i++)
179
nine_upload_destroy_buffer_group(upload, &upload->buffers[i]);
180
slab_destroy(&upload->buffer_pool);
181
FREE(upload);
182
}
183
184
struct nine_subbuffer *
185
nine_upload_create_buffer(struct nine_buffer_upload *upload,
186
unsigned buffer_size)
187
{
188
struct nine_subbuffer *buf = slab_alloc_st(&upload->buffer_pool);
189
struct nine_buffer_group *group = NULL;
190
unsigned size = align(buffer_size, 4096);
191
int i = 0;
192
193
DBG("%p %d\n", upload, buffer_size);
194
195
if (!buf)
196
return NULL;
197
198
for (i = 0; i < upload->num_buffers; i++) {
199
group = &upload->buffers[i];
200
if (group->resource &&
201
group->free_offset + size <= upload->buffers_size)
202
break;
203
}
204
205
if (i == upload->num_buffers) {
206
/* Allocate lonely buffer */
207
struct pipe_resource resource;
208
struct pipe_screen *screen = upload->pipe->screen;
209
210
DBG("Allocating buffer\n");
211
buf->parent = NULL;
212
213
memset(&resource, 0, sizeof(resource));
214
resource.target = PIPE_BUFFER;
215
resource.format = PIPE_FORMAT_R8_UNORM;
216
resource.bind = PIPE_BIND_VERTEX_BUFFER;
217
resource.usage = PIPE_USAGE_STREAM;
218
resource.width0 = buffer_size;
219
resource.height0 = 1;
220
resource.depth0 = 1;
221
resource.array_size = 1;
222
resource.flags = PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
223
PIPE_RESOURCE_FLAG_MAP_COHERENT;
224
225
buf->resource = screen->resource_create(screen, &resource);
226
if (buf->resource == NULL) {
227
slab_free_st(&upload->buffer_pool, buf);
228
return NULL;
229
}
230
231
buf->map = pipe_buffer_map_range(upload->pipe, buf->resource,
232
0, buffer_size,
233
PIPE_MAP_WRITE |
234
#ifdef PIPE_ARCH_X86
235
PIPE_MAP_ONCE |
236
#endif
237
PIPE_MAP_PERSISTENT |
238
PIPE_MAP_COHERENT,
239
&buf->transfer);
240
if (buf->map == NULL) {
241
pipe_resource_reference(&buf->resource, NULL);
242
slab_free_st(&upload->buffer_pool, buf);
243
return NULL;
244
}
245
buf->offset = 0;
246
return buf;
247
}
248
249
DBG("Using buffer group %d\n", i);
250
251
buf->parent = group;
252
buf->resource = NULL;
253
pipe_resource_reference(&buf->resource, group->resource);
254
buf->offset = group->free_offset;
255
256
group->free_offset += size;
257
group->refcount += 1;
258
259
return buf;
260
}
261
262
void
263
nine_upload_release_buffer(struct nine_buffer_upload *upload,
264
struct nine_subbuffer *buf)
265
{
266
DBG("%p %p %p\n", upload, buf, buf->parent);
267
268
if (buf->parent) {
269
pipe_resource_reference(&buf->resource, NULL);
270
buf->parent->refcount--;
271
if (buf->parent->refcount == 0) {
272
/* Allocate new buffer */
273
nine_upload_destroy_buffer_group(upload, buf->parent);
274
nine_upload_create_buffer_group(upload, buf->parent);
275
}
276
} else {
277
/* lonely buffer */
278
if (buf->transfer)
279
pipe_buffer_unmap(upload->pipe, buf->transfer);
280
pipe_resource_reference(&buf->resource, NULL);
281
}
282
283
slab_free_st(&upload->buffer_pool, buf);
284
}
285
286
uint8_t *
287
nine_upload_buffer_get_map(struct nine_subbuffer *buf)
288
{
289
if (buf->parent) {
290
DBG("%d\n", buf->parent->refcount);
291
return buf->parent->map + buf->offset;
292
}
293
/* lonely buffer */
294
return buf->map;
295
}
296
297
struct pipe_resource *
298
nine_upload_buffer_resource_and_offset(struct nine_subbuffer *buf,
299
unsigned *offset)
300
{
301
*offset = buf->offset;
302
return buf->resource;
303
}
304
305