Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/radeonsi/si_buffer.c
4570 views
1
/*
2
* Copyright 2013 Advanced Micro Devices, Inc.
3
* All Rights Reserved.
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* on the rights to use, copy, modify, merge, publish, distribute, sub
9
* license, and/or sell copies of the Software, and to permit persons to whom
10
* the Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
14
* Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22
* USE OR OTHER DEALINGS IN THE SOFTWARE.
23
*/
24
25
#include "radeonsi/si_pipe.h"
26
#include "util/u_memory.h"
27
#include "util/u_transfer.h"
28
#include "util/u_upload_mgr.h"
29
30
#include <inttypes.h>
31
#include <stdio.h>
32
33
bool si_cs_is_buffer_referenced(struct si_context *sctx, struct pb_buffer *buf,
34
enum radeon_bo_usage usage)
35
{
36
return sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, buf, usage);
37
}
38
39
void *si_buffer_map(struct si_context *sctx, struct si_resource *resource,
40
unsigned usage)
41
{
42
return sctx->ws->buffer_map(sctx->ws, resource->buf, &sctx->gfx_cs, usage);
43
}
44
45
void si_init_resource_fields(struct si_screen *sscreen, struct si_resource *res, uint64_t size,
46
unsigned alignment)
47
{
48
struct si_texture *tex = (struct si_texture *)res;
49
50
res->bo_size = size;
51
res->bo_alignment_log2 = util_logbase2(alignment);
52
res->flags = 0;
53
res->texture_handle_allocated = false;
54
res->image_handle_allocated = false;
55
56
switch (res->b.b.usage) {
57
case PIPE_USAGE_STREAM:
58
res->flags |= RADEON_FLAG_GTT_WC;
59
if (sscreen->info.smart_access_memory)
60
res->domains = RADEON_DOMAIN_VRAM;
61
else
62
res->domains = RADEON_DOMAIN_GTT;
63
break;
64
case PIPE_USAGE_STAGING:
65
/* Transfers are likely to occur more often with these
66
* resources. */
67
res->domains = RADEON_DOMAIN_GTT;
68
break;
69
case PIPE_USAGE_DYNAMIC:
70
/* Older kernels didn't always flush the HDP cache before
71
* CS execution
72
*/
73
if (!sscreen->info.kernel_flushes_hdp_before_ib) {
74
res->domains = RADEON_DOMAIN_GTT;
75
res->flags |= RADEON_FLAG_GTT_WC;
76
break;
77
}
78
FALLTHROUGH;
79
case PIPE_USAGE_DEFAULT:
80
case PIPE_USAGE_IMMUTABLE:
81
default:
82
/* Not listing GTT here improves performance in some
83
* apps. */
84
res->domains = RADEON_DOMAIN_VRAM;
85
res->flags |= RADEON_FLAG_GTT_WC;
86
break;
87
}
88
89
if (res->b.b.target == PIPE_BUFFER && res->b.b.flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) {
90
/* Use GTT for all persistent mappings with older
91
* kernels, because they didn't always flush the HDP
92
* cache before CS execution.
93
*
94
* Write-combined CPU mappings are fine, the kernel
95
* ensures all CPU writes finish before the GPU
96
* executes a command stream.
97
*
98
* radeon doesn't have good BO move throttling, so put all
99
* persistent buffers into GTT to prevent VRAM CPU page faults.
100
*/
101
if (!sscreen->info.kernel_flushes_hdp_before_ib || !sscreen->info.is_amdgpu)
102
res->domains = RADEON_DOMAIN_GTT;
103
}
104
105
/* Tiled textures are unmappable. Always put them in VRAM. */
106
if ((res->b.b.target != PIPE_BUFFER && !tex->surface.is_linear) ||
107
res->b.b.flags & SI_RESOURCE_FLAG_UNMAPPABLE) {
108
res->domains = RADEON_DOMAIN_VRAM;
109
res->flags |= RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_GTT_WC;
110
}
111
112
/* Displayable and shareable surfaces are not suballocated. */
113
if (res->b.b.bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
114
res->flags |= RADEON_FLAG_NO_SUBALLOC; /* shareable */
115
else
116
res->flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
117
118
if (res->b.b.bind & PIPE_BIND_PROTECTED ||
119
/* Force scanout/depth/stencil buffer allocation to be encrypted */
120
(sscreen->debug_flags & DBG(TMZ) &&
121
res->b.b.bind & (PIPE_BIND_SCANOUT | PIPE_BIND_DEPTH_STENCIL)))
122
res->flags |= RADEON_FLAG_ENCRYPTED;
123
124
if (res->b.b.flags & PIPE_RESOURCE_FLAG_ENCRYPTED)
125
res->flags |= RADEON_FLAG_ENCRYPTED;
126
127
if (sscreen->debug_flags & DBG(NO_WC))
128
res->flags &= ~RADEON_FLAG_GTT_WC;
129
130
if (res->b.b.flags & SI_RESOURCE_FLAG_READ_ONLY)
131
res->flags |= RADEON_FLAG_READ_ONLY;
132
133
if (res->b.b.flags & SI_RESOURCE_FLAG_32BIT)
134
res->flags |= RADEON_FLAG_32BIT;
135
136
if (res->b.b.flags & SI_RESOURCE_FLAG_DRIVER_INTERNAL)
137
res->flags |= RADEON_FLAG_DRIVER_INTERNAL;
138
139
/* For higher throughput and lower latency over PCIe assuming sequential access.
140
* Only CP DMA and optimized compute benefit from this.
141
* GFX8 and older don't support RADEON_FLAG_UNCACHED.
142
*/
143
if (sscreen->info.chip_class >= GFX9 &&
144
res->b.b.flags & SI_RESOURCE_FLAG_UNCACHED)
145
res->flags |= RADEON_FLAG_UNCACHED;
146
147
/* Set expected VRAM and GART usage for the buffer. */
148
res->vram_usage_kb = 0;
149
res->gart_usage_kb = 0;
150
res->max_forced_staging_uploads = 0;
151
res->b.max_forced_staging_uploads = 0;
152
153
if (res->domains & RADEON_DOMAIN_VRAM) {
154
res->vram_usage_kb = MAX2(1, size / 1024);
155
156
if (!sscreen->info.smart_access_memory) {
157
/* We don't want to evict buffers from VRAM by mapping them for CPU access,
158
* because they might never be moved back again. If a buffer is large enough,
159
* upload data by copying from a temporary GTT buffer. 8K might not seem much,
160
* but there can be 100000 buffers.
161
*
162
* This tweak improves performance for viewperf.
163
*/
164
const unsigned min_size = 8196; /* tuned to minimize mapped VRAM */
165
const unsigned max_staging_uploads = 1; /* number of uploads before mapping directly */
166
167
res->max_forced_staging_uploads = res->b.max_forced_staging_uploads =
168
sscreen->info.has_dedicated_vram && size >= min_size ? max_staging_uploads : 0;
169
}
170
} else if (res->domains & RADEON_DOMAIN_GTT) {
171
res->gart_usage_kb = MAX2(1, size / 1024);
172
}
173
}
174
175
bool si_alloc_resource(struct si_screen *sscreen, struct si_resource *res)
176
{
177
struct pb_buffer *old_buf, *new_buf;
178
179
/* Allocate a new resource. */
180
new_buf = sscreen->ws->buffer_create(sscreen->ws, res->bo_size, 1 << res->bo_alignment_log2,
181
res->domains, res->flags);
182
if (!new_buf) {
183
return false;
184
}
185
186
/* Replace the pointer such that if res->buf wasn't NULL, it won't be
187
* NULL. This should prevent crashes with multiple contexts using
188
* the same buffer where one of the contexts invalidates it while
189
* the others are using it. */
190
old_buf = res->buf;
191
res->buf = new_buf; /* should be atomic */
192
res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf);
193
194
if (res->flags & RADEON_FLAG_32BIT) {
195
uint64_t start = res->gpu_address;
196
uint64_t last = start + res->bo_size - 1;
197
(void)start;
198
(void)last;
199
200
assert((start >> 32) == sscreen->info.address32_hi);
201
assert((last >> 32) == sscreen->info.address32_hi);
202
}
203
204
radeon_bo_reference(sscreen->ws, &old_buf, NULL);
205
206
util_range_set_empty(&res->valid_buffer_range);
207
res->TC_L2_dirty = false;
208
209
/* Print debug information. */
210
if (sscreen->debug_flags & DBG(VM) && res->b.b.target == PIPE_BUFFER) {
211
fprintf(stderr, "VM start=0x%" PRIX64 " end=0x%" PRIX64 " | Buffer %" PRIu64 " bytes\n",
212
res->gpu_address, res->gpu_address + res->buf->size, res->buf->size);
213
}
214
215
if (res->b.b.flags & SI_RESOURCE_FLAG_CLEAR)
216
si_screen_clear_buffer(sscreen, &res->b.b, 0, res->bo_size, 0, SI_OP_SYNC_AFTER);
217
218
return true;
219
}
220
221
static void si_resource_destroy(struct pipe_screen *screen, struct pipe_resource *buf)
222
{
223
if (buf->target == PIPE_BUFFER) {
224
struct si_screen *sscreen = (struct si_screen *)screen;
225
struct si_resource *buffer = si_resource(buf);
226
227
threaded_resource_deinit(buf);
228
util_range_destroy(&buffer->valid_buffer_range);
229
radeon_bo_reference(((struct si_screen*)screen)->ws, &buffer->buf, NULL);
230
util_idalloc_mt_free(&sscreen->buffer_ids, buffer->b.buffer_id_unique);
231
FREE(buffer);
232
} else if (buf->flags & SI_RESOURCE_AUX_PLANE) {
233
struct si_auxiliary_texture *tex = (struct si_auxiliary_texture *)buf;
234
235
radeon_bo_reference(((struct si_screen*)screen)->ws, &tex->buffer, NULL);
236
FREE(tex);
237
} else {
238
struct si_texture *tex = (struct si_texture *)buf;
239
struct si_resource *resource = &tex->buffer;
240
241
si_texture_reference(&tex->flushed_depth_texture, NULL);
242
243
if (tex->cmask_buffer != &tex->buffer) {
244
si_resource_reference(&tex->cmask_buffer, NULL);
245
}
246
radeon_bo_reference(((struct si_screen*)screen)->ws, &resource->buf, NULL);
247
FREE(tex);
248
}
249
}
250
251
/* Reallocate the buffer a update all resource bindings where the buffer is
252
* bound.
253
*
254
* This is used to avoid CPU-GPU synchronizations, because it makes the buffer
255
* idle by discarding its contents.
256
*/
257
static bool si_invalidate_buffer(struct si_context *sctx, struct si_resource *buf)
258
{
259
/* Shared buffers can't be reallocated. */
260
if (buf->b.is_shared)
261
return false;
262
263
/* Sparse buffers can't be reallocated. */
264
if (buf->flags & RADEON_FLAG_SPARSE)
265
return false;
266
267
/* In AMD_pinned_memory, the user pointer association only gets
268
* broken when the buffer is explicitly re-allocated.
269
*/
270
if (buf->b.is_user_ptr)
271
return false;
272
273
/* Check if mapping this buffer would cause waiting for the GPU. */
274
if (si_cs_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) ||
275
!sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, RADEON_USAGE_READWRITE)) {
276
/* Reallocate the buffer in the same pipe_resource. */
277
si_alloc_resource(sctx->screen, buf);
278
si_rebind_buffer(sctx, &buf->b.b);
279
} else {
280
util_range_set_empty(&buf->valid_buffer_range);
281
}
282
283
return true;
284
}
285
286
/* Replace the storage of dst with src. */
287
void si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *dst,
288
struct pipe_resource *src, unsigned num_rebinds, uint32_t rebind_mask,
289
uint32_t delete_buffer_id)
290
{
291
struct si_context *sctx = (struct si_context *)ctx;
292
struct si_resource *sdst = si_resource(dst);
293
struct si_resource *ssrc = si_resource(src);
294
295
radeon_bo_reference(sctx->screen->ws, &sdst->buf, ssrc->buf);
296
sdst->gpu_address = ssrc->gpu_address;
297
sdst->b.b.bind = ssrc->b.b.bind;
298
sdst->b.max_forced_staging_uploads = ssrc->b.max_forced_staging_uploads;
299
sdst->max_forced_staging_uploads = ssrc->max_forced_staging_uploads;
300
sdst->flags = ssrc->flags;
301
302
assert(sdst->vram_usage_kb == ssrc->vram_usage_kb);
303
assert(sdst->gart_usage_kb == ssrc->gart_usage_kb);
304
assert(sdst->bo_size == ssrc->bo_size);
305
assert(sdst->bo_alignment_log2 == ssrc->bo_alignment_log2);
306
assert(sdst->domains == ssrc->domains);
307
308
si_rebind_buffer(sctx, dst);
309
310
util_idalloc_mt_free(&sctx->screen->buffer_ids, delete_buffer_id);
311
}
312
313
static void si_invalidate_resource(struct pipe_context *ctx, struct pipe_resource *resource)
314
{
315
struct si_context *sctx = (struct si_context *)ctx;
316
struct si_resource *buf = si_resource(resource);
317
318
/* We currently only do anyting here for buffers */
319
if (resource->target == PIPE_BUFFER)
320
(void)si_invalidate_buffer(sctx, buf);
321
}
322
323
static void *si_buffer_get_transfer(struct pipe_context *ctx, struct pipe_resource *resource,
324
unsigned usage, const struct pipe_box *box,
325
struct pipe_transfer **ptransfer, void *data,
326
struct si_resource *staging, unsigned offset)
327
{
328
struct si_context *sctx = (struct si_context *)ctx;
329
struct si_transfer *transfer;
330
331
if (usage & PIPE_MAP_THREAD_SAFE)
332
transfer = malloc(sizeof(*transfer));
333
else if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
334
transfer = slab_alloc(&sctx->pool_transfers_unsync);
335
else
336
transfer = slab_alloc(&sctx->pool_transfers);
337
338
transfer->b.b.resource = NULL;
339
pipe_resource_reference(&transfer->b.b.resource, resource);
340
transfer->b.b.level = 0;
341
transfer->b.b.usage = usage;
342
transfer->b.b.box = *box;
343
transfer->b.b.stride = 0;
344
transfer->b.b.layer_stride = 0;
345
transfer->b.b.offset = offset;
346
transfer->b.staging = NULL;
347
transfer->staging = staging;
348
*ptransfer = &transfer->b.b;
349
return data;
350
}
351
352
static void *si_buffer_transfer_map(struct pipe_context *ctx, struct pipe_resource *resource,
353
unsigned level, unsigned usage, const struct pipe_box *box,
354
struct pipe_transfer **ptransfer)
355
{
356
struct si_context *sctx = (struct si_context *)ctx;
357
struct si_resource *buf = si_resource(resource);
358
uint8_t *data;
359
360
assert(box->x + box->width <= resource->width0);
361
362
/* From GL_AMD_pinned_memory issues:
363
*
364
* 4) Is glMapBuffer on a shared buffer guaranteed to return the
365
* same system address which was specified at creation time?
366
*
367
* RESOLVED: NO. The GL implementation might return a different
368
* virtual mapping of that memory, although the same physical
369
* page will be used.
370
*
371
* So don't ever use staging buffers.
372
*/
373
if (buf->b.is_user_ptr)
374
usage |= PIPE_MAP_PERSISTENT;
375
if (usage & PIPE_MAP_ONCE)
376
usage |= RADEON_MAP_TEMPORARY;
377
378
/* See if the buffer range being mapped has never been initialized,
379
* in which case it can be mapped unsynchronized. */
380
if (!(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED)) &&
381
usage & PIPE_MAP_WRITE && !buf->b.is_shared &&
382
!util_ranges_intersect(&buf->valid_buffer_range, box->x, box->x + box->width)) {
383
usage |= PIPE_MAP_UNSYNCHRONIZED;
384
}
385
386
/* If discarding the entire range, discard the whole resource instead. */
387
if (usage & PIPE_MAP_DISCARD_RANGE && box->x == 0 && box->width == resource->width0) {
388
usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
389
}
390
391
/* If a buffer in VRAM is too large and the range is discarded, don't
392
* map it directly. This makes sure that the buffer stays in VRAM.
393
*/
394
bool force_discard_range = false;
395
if (usage & (PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_DISCARD_RANGE) &&
396
!(usage & PIPE_MAP_PERSISTENT) &&
397
/* Try not to decrement the counter if it's not positive. Still racy,
398
* but it makes it harder to wrap the counter from INT_MIN to INT_MAX. */
399
buf->max_forced_staging_uploads > 0 &&
400
p_atomic_dec_return(&buf->max_forced_staging_uploads) >= 0) {
401
usage &= ~(PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_UNSYNCHRONIZED);
402
usage |= PIPE_MAP_DISCARD_RANGE;
403
force_discard_range = true;
404
}
405
406
if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE &&
407
!(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INVALIDATE))) {
408
assert(usage & PIPE_MAP_WRITE);
409
410
if (si_invalidate_buffer(sctx, buf)) {
411
/* At this point, the buffer is always idle. */
412
usage |= PIPE_MAP_UNSYNCHRONIZED;
413
} else {
414
/* Fall back to a temporary buffer. */
415
usage |= PIPE_MAP_DISCARD_RANGE;
416
}
417
}
418
419
if (usage & PIPE_MAP_DISCARD_RANGE &&
420
((!(usage & (PIPE_MAP_UNSYNCHRONIZED | PIPE_MAP_PERSISTENT))) ||
421
(buf->flags & RADEON_FLAG_SPARSE))) {
422
assert(usage & PIPE_MAP_WRITE);
423
424
/* Check if mapping this buffer would cause waiting for the GPU.
425
*/
426
if (buf->flags & RADEON_FLAG_SPARSE || force_discard_range ||
427
si_cs_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) ||
428
!sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, RADEON_USAGE_READWRITE)) {
429
/* Do a wait-free write-only transfer using a temporary buffer. */
430
struct u_upload_mgr *uploader;
431
struct si_resource *staging = NULL;
432
unsigned offset;
433
434
/* If we are not called from the driver thread, we have
435
* to use the uploader from u_threaded_context, which is
436
* local to the calling thread.
437
*/
438
if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
439
uploader = sctx->tc->base.stream_uploader;
440
else
441
uploader = sctx->b.stream_uploader;
442
443
u_upload_alloc(uploader, 0, box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT),
444
sctx->screen->info.tcc_cache_line_size, &offset,
445
(struct pipe_resource **)&staging, (void **)&data);
446
447
if (staging) {
448
data += box->x % SI_MAP_BUFFER_ALIGNMENT;
449
return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging,
450
offset);
451
} else if (buf->flags & RADEON_FLAG_SPARSE) {
452
return NULL;
453
}
454
} else {
455
/* At this point, the buffer is always idle (we checked it above). */
456
usage |= PIPE_MAP_UNSYNCHRONIZED;
457
}
458
}
459
/* Use a staging buffer in cached GTT for reads. */
460
else if (((usage & PIPE_MAP_READ) && !(usage & PIPE_MAP_PERSISTENT) &&
461
(buf->domains & RADEON_DOMAIN_VRAM || buf->flags & RADEON_FLAG_GTT_WC)) ||
462
(buf->flags & RADEON_FLAG_SPARSE)) {
463
struct si_resource *staging;
464
465
assert(!(usage & (TC_TRANSFER_MAP_THREADED_UNSYNC | PIPE_MAP_THREAD_SAFE)));
466
staging = si_aligned_buffer_create(ctx->screen,
467
SI_RESOURCE_FLAG_UNCACHED | SI_RESOURCE_FLAG_DRIVER_INTERNAL,
468
PIPE_USAGE_STAGING,
469
box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT), 256);
470
if (staging) {
471
/* Copy the VRAM buffer to the staging buffer. */
472
si_copy_buffer(sctx, &staging->b.b, resource, box->x % SI_MAP_BUFFER_ALIGNMENT,
473
box->x, box->width, SI_OP_SYNC_BEFORE_AFTER);
474
475
data = si_buffer_map(sctx, staging, usage & ~PIPE_MAP_UNSYNCHRONIZED);
476
if (!data) {
477
si_resource_reference(&staging, NULL);
478
return NULL;
479
}
480
data += box->x % SI_MAP_BUFFER_ALIGNMENT;
481
482
return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging, 0);
483
} else if (buf->flags & RADEON_FLAG_SPARSE) {
484
return NULL;
485
}
486
}
487
488
data = si_buffer_map(sctx, buf, usage);
489
if (!data) {
490
return NULL;
491
}
492
data += box->x;
493
494
return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, NULL, 0);
495
}
496
497
static void si_buffer_do_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer,
498
const struct pipe_box *box)
499
{
500
struct si_context *sctx = (struct si_context *)ctx;
501
struct si_transfer *stransfer = (struct si_transfer *)transfer;
502
struct si_resource *buf = si_resource(transfer->resource);
503
504
if (stransfer->staging) {
505
unsigned src_offset =
506
stransfer->b.b.offset + transfer->box.x % SI_MAP_BUFFER_ALIGNMENT + (box->x - transfer->box.x);
507
508
/* Copy the staging buffer into the original one. */
509
si_copy_buffer(sctx, transfer->resource, &stransfer->staging->b.b, box->x, src_offset,
510
box->width, SI_OP_SYNC_BEFORE_AFTER);
511
}
512
513
util_range_add(&buf->b.b, &buf->valid_buffer_range, box->x, box->x + box->width);
514
}
515
516
static void si_buffer_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer,
517
const struct pipe_box *rel_box)
518
{
519
unsigned required_usage = PIPE_MAP_WRITE | PIPE_MAP_FLUSH_EXPLICIT;
520
521
if ((transfer->usage & required_usage) == required_usage) {
522
struct pipe_box box;
523
524
u_box_1d(transfer->box.x + rel_box->x, rel_box->width, &box);
525
si_buffer_do_flush_region(ctx, transfer, &box);
526
}
527
}
528
529
static void si_buffer_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *transfer)
530
{
531
struct si_context *sctx = (struct si_context *)ctx;
532
struct si_transfer *stransfer = (struct si_transfer *)transfer;
533
534
if (transfer->usage & PIPE_MAP_WRITE && !(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT))
535
si_buffer_do_flush_region(ctx, transfer, &transfer->box);
536
537
if (transfer->usage & (PIPE_MAP_ONCE | RADEON_MAP_TEMPORARY) &&
538
!stransfer->staging)
539
sctx->ws->buffer_unmap(sctx->ws, si_resource(stransfer->b.b.resource)->buf);
540
541
si_resource_reference(&stransfer->staging, NULL);
542
assert(stransfer->b.staging == NULL); /* for threaded context only */
543
pipe_resource_reference(&transfer->resource, NULL);
544
545
if (transfer->usage & PIPE_MAP_THREAD_SAFE) {
546
free(transfer);
547
} else {
548
/* Don't use pool_transfers_unsync. We are always in the driver
549
* thread. Freeing an object into a different pool is allowed.
550
*/
551
slab_free(&sctx->pool_transfers, transfer);
552
}
553
}
554
555
static void si_buffer_subdata(struct pipe_context *ctx, struct pipe_resource *buffer,
556
unsigned usage, unsigned offset, unsigned size, const void *data)
557
{
558
struct pipe_transfer *transfer = NULL;
559
struct pipe_box box;
560
uint8_t *map = NULL;
561
562
usage |= PIPE_MAP_WRITE;
563
564
if (!(usage & PIPE_MAP_DIRECTLY))
565
usage |= PIPE_MAP_DISCARD_RANGE;
566
567
u_box_1d(offset, size, &box);
568
map = si_buffer_transfer_map(ctx, buffer, 0, usage, &box, &transfer);
569
if (!map)
570
return;
571
572
memcpy(map, data, size);
573
si_buffer_transfer_unmap(ctx, transfer);
574
}
575
576
static struct si_resource *si_alloc_buffer_struct(struct pipe_screen *screen,
577
const struct pipe_resource *templ)
578
{
579
struct si_resource *buf;
580
581
buf = MALLOC_STRUCT(si_resource);
582
583
buf->b.b = *templ;
584
buf->b.b.next = NULL;
585
pipe_reference_init(&buf->b.b.reference, 1);
586
buf->b.b.screen = screen;
587
588
threaded_resource_init(&buf->b.b);
589
590
buf->buf = NULL;
591
buf->bind_history = 0;
592
buf->TC_L2_dirty = false;
593
util_range_init(&buf->valid_buffer_range);
594
return buf;
595
}
596
597
static struct pipe_resource *si_buffer_create(struct pipe_screen *screen,
598
const struct pipe_resource *templ, unsigned alignment)
599
{
600
struct si_screen *sscreen = (struct si_screen *)screen;
601
struct si_resource *buf = si_alloc_buffer_struct(screen, templ);
602
603
if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
604
buf->b.b.flags |= SI_RESOURCE_FLAG_UNMAPPABLE;
605
606
si_init_resource_fields(sscreen, buf, templ->width0, alignment);
607
608
if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
609
buf->flags |= RADEON_FLAG_SPARSE;
610
611
if (!si_alloc_resource(sscreen, buf)) {
612
threaded_resource_deinit(&buf->b.b);
613
FREE(buf);
614
return NULL;
615
}
616
617
buf->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids);
618
return &buf->b.b;
619
}
620
621
struct pipe_resource *pipe_aligned_buffer_create(struct pipe_screen *screen, unsigned flags,
622
unsigned usage, unsigned size, unsigned alignment)
623
{
624
struct pipe_resource buffer;
625
626
memset(&buffer, 0, sizeof buffer);
627
buffer.target = PIPE_BUFFER;
628
buffer.format = PIPE_FORMAT_R8_UNORM;
629
buffer.bind = 0;
630
buffer.usage = usage;
631
buffer.flags = flags;
632
buffer.width0 = size;
633
buffer.height0 = 1;
634
buffer.depth0 = 1;
635
buffer.array_size = 1;
636
return si_buffer_create(screen, &buffer, alignment);
637
}
638
639
struct si_resource *si_aligned_buffer_create(struct pipe_screen *screen, unsigned flags,
640
unsigned usage, unsigned size, unsigned alignment)
641
{
642
return si_resource(pipe_aligned_buffer_create(screen, flags, usage, size, alignment));
643
}
644
645
static struct pipe_resource *si_buffer_from_user_memory(struct pipe_screen *screen,
646
const struct pipe_resource *templ,
647
void *user_memory)
648
{
649
struct si_screen *sscreen = (struct si_screen *)screen;
650
struct radeon_winsys *ws = sscreen->ws;
651
struct si_resource *buf = si_alloc_buffer_struct(screen, templ);
652
653
buf->domains = RADEON_DOMAIN_GTT;
654
buf->flags = 0;
655
buf->b.is_user_ptr = true;
656
util_range_add(&buf->b.b, &buf->valid_buffer_range, 0, templ->width0);
657
util_range_add(&buf->b.b, &buf->b.valid_buffer_range, 0, templ->width0);
658
659
/* Convert a user pointer to a buffer. */
660
buf->buf = ws->buffer_from_ptr(ws, user_memory, templ->width0);
661
if (!buf->buf) {
662
threaded_resource_deinit(&buf->b.b);
663
FREE(buf);
664
return NULL;
665
}
666
667
buf->gpu_address = ws->buffer_get_virtual_address(buf->buf);
668
buf->vram_usage_kb = 0;
669
buf->gart_usage_kb = templ->width0 / 1024;
670
buf->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids);
671
return &buf->b.b;
672
}
673
674
struct pipe_resource *si_buffer_from_winsys_buffer(struct pipe_screen *screen,
675
const struct pipe_resource *templ,
676
struct pb_buffer *imported_buf,
677
bool dedicated)
678
{
679
struct si_screen *sscreen = (struct si_screen *)screen;
680
struct si_resource *res = si_alloc_buffer_struct(screen, templ);
681
682
if (!res)
683
return 0;
684
685
res->buf = imported_buf;
686
res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf);
687
res->bo_size = imported_buf->size;
688
res->bo_alignment_log2 = imported_buf->alignment_log2;
689
res->domains = sscreen->ws->buffer_get_initial_domain(res->buf);
690
691
if (res->domains & RADEON_DOMAIN_VRAM)
692
res->vram_usage_kb = MAX2(1, res->bo_size / 1024);
693
else if (res->domains & RADEON_DOMAIN_GTT)
694
res->gart_usage_kb = MAX2(1, res->bo_size / 1024);
695
696
if (sscreen->ws->buffer_get_flags)
697
res->flags = sscreen->ws->buffer_get_flags(res->buf);
698
699
if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE) {
700
res->b.b.flags |= SI_RESOURCE_FLAG_UNMAPPABLE;
701
res->flags |= RADEON_FLAG_SPARSE;
702
}
703
704
res->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids);
705
return &res->b.b;
706
}
707
708
static struct pipe_resource *si_resource_create(struct pipe_screen *screen,
709
const struct pipe_resource *templ)
710
{
711
if (templ->target == PIPE_BUFFER) {
712
return si_buffer_create(screen, templ, 256);
713
} else {
714
return si_texture_create(screen, templ);
715
}
716
}
717
718
static bool si_resource_commit(struct pipe_context *pctx, struct pipe_resource *resource,
719
unsigned level, struct pipe_box *box, bool commit)
720
{
721
struct si_context *ctx = (struct si_context *)pctx;
722
struct si_resource *res = si_resource(resource);
723
724
/*
725
* Since buffer commitment changes cannot be pipelined, we need to
726
* (a) flush any pending commands that refer to the buffer we're about
727
* to change, and
728
* (b) wait for threaded submit to finish, including those that were
729
* triggered by some other, earlier operation.
730
*/
731
if (radeon_emitted(&ctx->gfx_cs, ctx->initial_gfx_cs_size) &&
732
ctx->ws->cs_is_buffer_referenced(&ctx->gfx_cs, res->buf, RADEON_USAGE_READWRITE)) {
733
si_flush_gfx_cs(ctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
734
}
735
ctx->ws->cs_sync_flush(&ctx->gfx_cs);
736
737
assert(resource->target == PIPE_BUFFER);
738
739
return ctx->ws->buffer_commit(ctx->ws, res->buf, box->x, box->width, commit);
740
}
741
742
void si_init_screen_buffer_functions(struct si_screen *sscreen)
743
{
744
sscreen->b.resource_create = si_resource_create;
745
sscreen->b.resource_destroy = si_resource_destroy;
746
sscreen->b.resource_from_user_memory = si_buffer_from_user_memory;
747
}
748
749
void si_init_buffer_functions(struct si_context *sctx)
750
{
751
sctx->b.invalidate_resource = si_invalidate_resource;
752
sctx->b.buffer_map = si_buffer_transfer_map;
753
sctx->b.transfer_flush_region = si_buffer_flush_region;
754
sctx->b.buffer_unmap = si_buffer_transfer_unmap;
755
sctx->b.texture_subdata = u_default_texture_subdata;
756
sctx->b.buffer_subdata = si_buffer_subdata;
757
sctx->b.resource_commit = si_resource_commit;
758
}
759
760