1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 *
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
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
13 * Software.
14 *
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Marek Olšák
25 */
26
27 #include "r600_cs.h"
28 #include "evergreen_compute.h"
29 #include "util/u_memory.h"
30 #include "util/u_upload_mgr.h"
31 #include <inttypes.h>
32 #include <stdio.h>
33
r600_rings_is_buffer_referenced(struct r600_common_context * ctx,struct pb_buffer * buf,enum radeon_bo_usage usage)34 bool r600_rings_is_buffer_referenced(struct r600_common_context *ctx,
35 struct pb_buffer *buf,
36 enum radeon_bo_usage usage)
37 {
38 if (ctx->ws->cs_is_buffer_referenced(&ctx->gfx.cs, buf, usage)) {
39 return true;
40 }
41 if (radeon_emitted(&ctx->dma.cs, 0) &&
42 ctx->ws->cs_is_buffer_referenced(&ctx->dma.cs, buf, usage)) {
43 return true;
44 }
45 return false;
46 }
47
r600_buffer_map_sync_with_rings(struct r600_common_context * ctx,struct r600_resource * resource,unsigned usage)48 void *r600_buffer_map_sync_with_rings(struct r600_common_context *ctx,
49 struct r600_resource *resource,
50 unsigned usage)
51 {
52 enum radeon_bo_usage rusage = RADEON_USAGE_READWRITE;
53 bool busy = false;
54
55 assert(!(resource->flags & RADEON_FLAG_SPARSE));
56
57 if (usage & PIPE_MAP_UNSYNCHRONIZED) {
58 return ctx->ws->buffer_map(ctx->ws, resource->buf, NULL, usage);
59 }
60
61 if (!(usage & PIPE_MAP_WRITE)) {
62 /* have to wait for the last write */
63 rusage = RADEON_USAGE_WRITE;
64 }
65
66 if (radeon_emitted(&ctx->gfx.cs, ctx->initial_gfx_cs_size) &&
67 ctx->ws->cs_is_buffer_referenced(&ctx->gfx.cs,
68 resource->buf, rusage)) {
69 if (usage & PIPE_MAP_DONTBLOCK) {
70 ctx->gfx.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
71 return NULL;
72 } else {
73 ctx->gfx.flush(ctx, 0, NULL);
74 busy = true;
75 }
76 }
77 if (radeon_emitted(&ctx->dma.cs, 0) &&
78 ctx->ws->cs_is_buffer_referenced(&ctx->dma.cs,
79 resource->buf, rusage)) {
80 if (usage & PIPE_MAP_DONTBLOCK) {
81 ctx->dma.flush(ctx, PIPE_FLUSH_ASYNC, NULL);
82 return NULL;
83 } else {
84 ctx->dma.flush(ctx, 0, NULL);
85 busy = true;
86 }
87 }
88
89 if (busy || !ctx->ws->buffer_wait(ctx->ws, resource->buf, 0, rusage)) {
90 if (usage & PIPE_MAP_DONTBLOCK) {
91 return NULL;
92 } else {
93 /* We will be wait for the GPU. Wait for any offloaded
94 * CS flush to complete to avoid busy-waiting in the winsys. */
95 ctx->ws->cs_sync_flush(&ctx->gfx.cs);
96 if (ctx->dma.cs.priv)
97 ctx->ws->cs_sync_flush(&ctx->dma.cs);
98 }
99 }
100
101 /* Setting the CS to NULL will prevent doing checks we have done already. */
102 return ctx->ws->buffer_map(ctx->ws, resource->buf, NULL, usage);
103 }
104
r600_init_resource_fields(struct r600_common_screen * rscreen,struct r600_resource * res,uint64_t size,unsigned alignment)105 void r600_init_resource_fields(struct r600_common_screen *rscreen,
106 struct r600_resource *res,
107 uint64_t size, unsigned alignment)
108 {
109 struct r600_texture *rtex = (struct r600_texture*)res;
110
111 res->bo_size = size;
112 res->bo_alignment = alignment;
113 res->flags = 0;
114 res->texture_handle_allocated = false;
115 res->image_handle_allocated = false;
116
117 switch (res->b.b.usage) {
118 case PIPE_USAGE_STREAM:
119 res->flags = RADEON_FLAG_GTT_WC;
120 FALLTHROUGH;
121 case PIPE_USAGE_STAGING:
122 /* Transfers are likely to occur more often with these
123 * resources. */
124 res->domains = RADEON_DOMAIN_GTT;
125 break;
126 case PIPE_USAGE_DYNAMIC:
127 /* Older kernels didn't always flush the HDP cache before
128 * CS execution
129 */
130 if (rscreen->info.drm_minor < 40) {
131 res->domains = RADEON_DOMAIN_GTT;
132 res->flags |= RADEON_FLAG_GTT_WC;
133 break;
134 }
135 FALLTHROUGH;
136 case PIPE_USAGE_DEFAULT:
137 case PIPE_USAGE_IMMUTABLE:
138 default:
139 /* Not listing GTT here improves performance in some
140 * apps. */
141 res->domains = RADEON_DOMAIN_VRAM;
142 res->flags |= RADEON_FLAG_GTT_WC;
143 break;
144 }
145
146 if (res->b.b.target == PIPE_BUFFER &&
147 res->b.b.flags & (PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
148 PIPE_RESOURCE_FLAG_MAP_COHERENT)) {
149 /* Use GTT for all persistent mappings with older
150 * kernels, because they didn't always flush the HDP
151 * cache before CS execution.
152 *
153 * Write-combined CPU mappings are fine, the kernel
154 * ensures all CPU writes finish before the GPU
155 * executes a command stream.
156 */
157 if (rscreen->info.drm_minor < 40)
158 res->domains = RADEON_DOMAIN_GTT;
159 }
160
161 /* Tiled textures are unmappable. Always put them in VRAM. */
162 if ((res->b.b.target != PIPE_BUFFER && !rtex->surface.is_linear) ||
163 res->flags & R600_RESOURCE_FLAG_UNMAPPABLE) {
164 res->domains = RADEON_DOMAIN_VRAM;
165 res->flags |= RADEON_FLAG_NO_CPU_ACCESS |
166 RADEON_FLAG_GTT_WC;
167 }
168
169 /* Displayable and shareable surfaces are not suballocated. */
170 if (res->b.b.bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
171 res->flags |= RADEON_FLAG_NO_SUBALLOC; /* shareable */
172 else
173 res->flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
174
175 if (rscreen->debug_flags & DBG_NO_WC)
176 res->flags &= ~RADEON_FLAG_GTT_WC;
177
178 /* Set expected VRAM and GART usage for the buffer. */
179 res->vram_usage = 0;
180 res->gart_usage = 0;
181
182 if (res->domains & RADEON_DOMAIN_VRAM)
183 res->vram_usage = size;
184 else if (res->domains & RADEON_DOMAIN_GTT)
185 res->gart_usage = size;
186 }
187
r600_alloc_resource(struct r600_common_screen * rscreen,struct r600_resource * res)188 bool r600_alloc_resource(struct r600_common_screen *rscreen,
189 struct r600_resource *res)
190 {
191 struct pb_buffer *old_buf, *new_buf;
192
193 /* Allocate a new resource. */
194 new_buf = rscreen->ws->buffer_create(rscreen->ws, res->bo_size,
195 res->bo_alignment,
196 res->domains, res->flags);
197 if (!new_buf) {
198 return false;
199 }
200
201 /* Replace the pointer such that if res->buf wasn't NULL, it won't be
202 * NULL. This should prevent crashes with multiple contexts using
203 * the same buffer where one of the contexts invalidates it while
204 * the others are using it. */
205 old_buf = res->buf;
206 res->buf = new_buf; /* should be atomic */
207
208 if (rscreen->info.r600_has_virtual_memory)
209 res->gpu_address = rscreen->ws->buffer_get_virtual_address(res->buf);
210 else
211 res->gpu_address = 0;
212
213 pb_reference(&old_buf, NULL);
214
215 util_range_set_empty(&res->valid_buffer_range);
216
217 /* Print debug information. */
218 if (rscreen->debug_flags & DBG_VM && res->b.b.target == PIPE_BUFFER) {
219 fprintf(stderr, "VM start=0x%"PRIX64" end=0x%"PRIX64" | Buffer %"PRIu64" bytes\n",
220 res->gpu_address, res->gpu_address + res->buf->size,
221 res->buf->size);
222 }
223 return true;
224 }
225
r600_buffer_destroy(struct pipe_screen * screen,struct pipe_resource * buf)226 void r600_buffer_destroy(struct pipe_screen *screen, struct pipe_resource *buf)
227 {
228 struct r600_resource *rbuffer = r600_resource(buf);
229
230 threaded_resource_deinit(buf);
231 util_range_destroy(&rbuffer->valid_buffer_range);
232 pipe_resource_reference((struct pipe_resource**)&rbuffer->immed_buffer, NULL);
233 pb_reference(&rbuffer->buf, NULL);
234 FREE(rbuffer);
235 }
236
237 static bool
r600_invalidate_buffer(struct r600_common_context * rctx,struct r600_resource * rbuffer)238 r600_invalidate_buffer(struct r600_common_context *rctx,
239 struct r600_resource *rbuffer)
240 {
241 /* Shared buffers can't be reallocated. */
242 if (rbuffer->b.is_shared)
243 return false;
244
245 /* Sparse buffers can't be reallocated. */
246 if (rbuffer->flags & RADEON_FLAG_SPARSE)
247 return false;
248
249 /* In AMD_pinned_memory, the user pointer association only gets
250 * broken when the buffer is explicitly re-allocated.
251 */
252 if (rbuffer->b.is_user_ptr)
253 return false;
254
255 /* Check if mapping this buffer would cause waiting for the GPU. */
256 if (r600_rings_is_buffer_referenced(rctx, rbuffer->buf, RADEON_USAGE_READWRITE) ||
257 !rctx->ws->buffer_wait(rctx->ws, rbuffer->buf, 0, RADEON_USAGE_READWRITE)) {
258 rctx->invalidate_buffer(&rctx->b, &rbuffer->b.b);
259 } else {
260 util_range_set_empty(&rbuffer->valid_buffer_range);
261 }
262
263 return true;
264 }
265
266 /* Replace the storage of dst with src. */
r600_replace_buffer_storage(struct pipe_context * ctx,struct pipe_resource * dst,struct pipe_resource * src)267 void r600_replace_buffer_storage(struct pipe_context *ctx,
268 struct pipe_resource *dst,
269 struct pipe_resource *src)
270 {
271 struct r600_common_context *rctx = (struct r600_common_context *)ctx;
272 struct r600_resource *rdst = r600_resource(dst);
273 struct r600_resource *rsrc = r600_resource(src);
274 uint64_t old_gpu_address = rdst->gpu_address;
275
276 pb_reference(&rdst->buf, rsrc->buf);
277 rdst->gpu_address = rsrc->gpu_address;
278 rdst->b.b.bind = rsrc->b.b.bind;
279 rdst->flags = rsrc->flags;
280
281 assert(rdst->vram_usage == rsrc->vram_usage);
282 assert(rdst->gart_usage == rsrc->gart_usage);
283 assert(rdst->bo_size == rsrc->bo_size);
284 assert(rdst->bo_alignment == rsrc->bo_alignment);
285 assert(rdst->domains == rsrc->domains);
286
287 rctx->rebind_buffer(ctx, dst, old_gpu_address);
288 }
289
r600_invalidate_resource(struct pipe_context * ctx,struct pipe_resource * resource)290 void r600_invalidate_resource(struct pipe_context *ctx,
291 struct pipe_resource *resource)
292 {
293 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
294 struct r600_resource *rbuffer = r600_resource(resource);
295
296 /* We currently only do anyting here for buffers */
297 if (resource->target == PIPE_BUFFER)
298 (void)r600_invalidate_buffer(rctx, rbuffer);
299 }
300
r600_buffer_get_transfer(struct pipe_context * ctx,struct pipe_resource * resource,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer,void * data,struct r600_resource * staging,unsigned offset)301 static void *r600_buffer_get_transfer(struct pipe_context *ctx,
302 struct pipe_resource *resource,
303 unsigned usage,
304 const struct pipe_box *box,
305 struct pipe_transfer **ptransfer,
306 void *data, struct r600_resource *staging,
307 unsigned offset)
308 {
309 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
310 struct r600_transfer *transfer;
311
312 if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
313 transfer = slab_alloc(&rctx->pool_transfers_unsync);
314 else
315 transfer = slab_alloc(&rctx->pool_transfers);
316
317 transfer->b.b.resource = NULL;
318 pipe_resource_reference(&transfer->b.b.resource, resource);
319 transfer->b.b.level = 0;
320 transfer->b.b.usage = usage;
321 transfer->b.b.box = *box;
322 transfer->b.b.stride = 0;
323 transfer->b.b.layer_stride = 0;
324 transfer->b.staging = NULL;
325 transfer->b.b.offset = offset;
326 transfer->staging = staging;
327 *ptransfer = &transfer->b.b;
328 return data;
329 }
330
r600_can_dma_copy_buffer(struct r600_common_context * rctx,unsigned dstx,unsigned srcx,unsigned size)331 static bool r600_can_dma_copy_buffer(struct r600_common_context *rctx,
332 unsigned dstx, unsigned srcx, unsigned size)
333 {
334 bool dword_aligned = !(dstx % 4) && !(srcx % 4) && !(size % 4);
335
336 return rctx->screen->has_cp_dma ||
337 (dword_aligned && (rctx->dma.cs.priv ||
338 rctx->screen->has_streamout));
339
340 }
341
r600_buffer_transfer_map(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)342 void *r600_buffer_transfer_map(struct pipe_context *ctx,
343 struct pipe_resource *resource,
344 unsigned level,
345 unsigned usage,
346 const struct pipe_box *box,
347 struct pipe_transfer **ptransfer)
348 {
349 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
350 struct r600_common_screen *rscreen = (struct r600_common_screen*)ctx->screen;
351 struct r600_resource *rbuffer = r600_resource(resource);
352 uint8_t *data;
353
354 if (r600_resource(resource)->compute_global_bo) {
355 return r600_compute_global_transfer_map(ctx, resource, level, usage, box, ptransfer);
356 }
357
358 assert(box->x + box->width <= resource->width0);
359
360 /* From GL_AMD_pinned_memory issues:
361 *
362 * 4) Is glMapBuffer on a shared buffer guaranteed to return the
363 * same system address which was specified at creation time?
364 *
365 * RESOLVED: NO. The GL implementation might return a different
366 * virtual mapping of that memory, although the same physical
367 * page will be used.
368 *
369 * So don't ever use staging buffers.
370 */
371 if (rbuffer->b.is_user_ptr)
372 usage |= PIPE_MAP_PERSISTENT;
373
374 /* See if the buffer range being mapped has never been initialized,
375 * in which case it can be mapped unsynchronized. */
376 if (!(usage & (PIPE_MAP_UNSYNCHRONIZED |
377 TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED)) &&
378 usage & PIPE_MAP_WRITE &&
379 !rbuffer->b.is_shared &&
380 !util_ranges_intersect(&rbuffer->valid_buffer_range, box->x, box->x + box->width)) {
381 usage |= PIPE_MAP_UNSYNCHRONIZED;
382 }
383
384 /* If discarding the entire range, discard the whole resource instead. */
385 if (usage & PIPE_MAP_DISCARD_RANGE &&
386 box->x == 0 && box->width == resource->width0) {
387 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
388 }
389
390 if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE &&
391 !(usage & (PIPE_MAP_UNSYNCHRONIZED |
392 TC_TRANSFER_MAP_NO_INVALIDATE))) {
393 assert(usage & PIPE_MAP_WRITE);
394
395 if (r600_invalidate_buffer(rctx, rbuffer)) {
396 /* At this point, the buffer is always idle. */
397 usage |= PIPE_MAP_UNSYNCHRONIZED;
398 } else {
399 /* Fall back to a temporary buffer. */
400 usage |= PIPE_MAP_DISCARD_RANGE;
401 }
402 }
403
404 if ((usage & PIPE_MAP_DISCARD_RANGE) &&
405 !(rscreen->debug_flags & DBG_NO_DISCARD_RANGE) &&
406 ((!(usage & (PIPE_MAP_UNSYNCHRONIZED |
407 PIPE_MAP_PERSISTENT)) &&
408 r600_can_dma_copy_buffer(rctx, box->x, 0, box->width)) ||
409 (rbuffer->flags & RADEON_FLAG_SPARSE))) {
410 assert(usage & PIPE_MAP_WRITE);
411
412 /* Check if mapping this buffer would cause waiting for the GPU.
413 */
414 if (rbuffer->flags & RADEON_FLAG_SPARSE ||
415 r600_rings_is_buffer_referenced(rctx, rbuffer->buf, RADEON_USAGE_READWRITE) ||
416 !rctx->ws->buffer_wait(rctx->ws, rbuffer->buf, 0, RADEON_USAGE_READWRITE)) {
417 /* Do a wait-free write-only transfer using a temporary buffer. */
418 unsigned offset;
419 struct r600_resource *staging = NULL;
420
421 u_upload_alloc(ctx->stream_uploader, 0,
422 box->width + (box->x % R600_MAP_BUFFER_ALIGNMENT),
423 rctx->screen->info.tcc_cache_line_size,
424 &offset, (struct pipe_resource**)&staging,
425 (void**)&data);
426
427 if (staging) {
428 data += box->x % R600_MAP_BUFFER_ALIGNMENT;
429 return r600_buffer_get_transfer(ctx, resource, usage, box,
430 ptransfer, data, staging, offset);
431 } else if (rbuffer->flags & RADEON_FLAG_SPARSE) {
432 return NULL;
433 }
434 } else {
435 /* At this point, the buffer is always idle (we checked it above). */
436 usage |= PIPE_MAP_UNSYNCHRONIZED;
437 }
438 }
439 /* Use a staging buffer in cached GTT for reads. */
440 else if (((usage & PIPE_MAP_READ) &&
441 !(usage & PIPE_MAP_PERSISTENT) &&
442 (rbuffer->domains & RADEON_DOMAIN_VRAM ||
443 rbuffer->flags & RADEON_FLAG_GTT_WC) &&
444 r600_can_dma_copy_buffer(rctx, 0, box->x, box->width)) ||
445 (rbuffer->flags & RADEON_FLAG_SPARSE)) {
446 struct r600_resource *staging;
447
448 assert(!(usage & TC_TRANSFER_MAP_THREADED_UNSYNC));
449 staging = (struct r600_resource*) pipe_buffer_create(
450 ctx->screen, 0, PIPE_USAGE_STAGING,
451 box->width + (box->x % R600_MAP_BUFFER_ALIGNMENT));
452 if (staging) {
453 /* Copy the VRAM buffer to the staging buffer. */
454 rctx->dma_copy(ctx, &staging->b.b, 0,
455 box->x % R600_MAP_BUFFER_ALIGNMENT,
456 0, 0, resource, 0, box);
457
458 data = r600_buffer_map_sync_with_rings(rctx, staging,
459 usage & ~PIPE_MAP_UNSYNCHRONIZED);
460 if (!data) {
461 r600_resource_reference(&staging, NULL);
462 return NULL;
463 }
464 data += box->x % R600_MAP_BUFFER_ALIGNMENT;
465
466 return r600_buffer_get_transfer(ctx, resource, usage, box,
467 ptransfer, data, staging, 0);
468 } else if (rbuffer->flags & RADEON_FLAG_SPARSE) {
469 return NULL;
470 }
471 }
472
473 data = r600_buffer_map_sync_with_rings(rctx, rbuffer, usage);
474 if (!data) {
475 return NULL;
476 }
477 data += box->x;
478
479 return r600_buffer_get_transfer(ctx, resource, usage, box,
480 ptransfer, data, NULL, 0);
481 }
482
r600_buffer_do_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * box)483 static void r600_buffer_do_flush_region(struct pipe_context *ctx,
484 struct pipe_transfer *transfer,
485 const struct pipe_box *box)
486 {
487 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
488 struct r600_resource *rbuffer = r600_resource(transfer->resource);
489
490 if (rtransfer->staging) {
491 struct pipe_resource *dst, *src;
492 unsigned soffset;
493 struct pipe_box dma_box;
494
495 dst = transfer->resource;
496 src = &rtransfer->staging->b.b;
497 soffset = rtransfer->b.b.offset + box->x % R600_MAP_BUFFER_ALIGNMENT;
498
499 u_box_1d(soffset, box->width, &dma_box);
500
501 /* Copy the staging buffer into the original one. */
502 ctx->resource_copy_region(ctx, dst, 0, box->x, 0, 0, src, 0, &dma_box);
503 }
504
505 util_range_add(&rbuffer->b.b, &rbuffer->valid_buffer_range, box->x,
506 box->x + box->width);
507 }
508
r600_buffer_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * rel_box)509 void r600_buffer_flush_region(struct pipe_context *ctx,
510 struct pipe_transfer *transfer,
511 const struct pipe_box *rel_box)
512 {
513 unsigned required_usage = PIPE_MAP_WRITE |
514 PIPE_MAP_FLUSH_EXPLICIT;
515
516 if (r600_resource(transfer->resource)->compute_global_bo)
517 return;
518
519 if ((transfer->usage & required_usage) == required_usage) {
520 struct pipe_box box;
521
522 u_box_1d(transfer->box.x + rel_box->x, rel_box->width, &box);
523 r600_buffer_do_flush_region(ctx, transfer, &box);
524 }
525 }
526
r600_buffer_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)527 void r600_buffer_transfer_unmap(struct pipe_context *ctx,
528 struct pipe_transfer *transfer)
529 {
530 struct r600_common_context *rctx = (struct r600_common_context*)ctx;
531 struct r600_transfer *rtransfer = (struct r600_transfer*)transfer;
532
533 if (r600_resource(transfer->resource)->compute_global_bo) {
534 r600_compute_global_transfer_unmap(ctx, transfer);
535 return;
536 }
537
538 if (transfer->usage & PIPE_MAP_WRITE &&
539 !(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT))
540 r600_buffer_do_flush_region(ctx, transfer, &transfer->box);
541
542 r600_resource_reference(&rtransfer->staging, NULL);
543 assert(rtransfer->b.staging == NULL); /* for threaded context only */
544 pipe_resource_reference(&transfer->resource, NULL);
545
546 /* Don't use pool_transfers_unsync. We are always in the driver
547 * thread. */
548 slab_free(&rctx->pool_transfers, transfer);
549 }
550
r600_buffer_subdata(struct pipe_context * ctx,struct pipe_resource * buffer,unsigned usage,unsigned offset,unsigned size,const void * data)551 void r600_buffer_subdata(struct pipe_context *ctx,
552 struct pipe_resource *buffer,
553 unsigned usage, unsigned offset,
554 unsigned size, const void *data)
555 {
556 struct pipe_transfer *transfer = NULL;
557 struct pipe_box box;
558 uint8_t *map = NULL;
559
560 usage |= PIPE_MAP_WRITE;
561
562 if (!(usage & PIPE_MAP_DIRECTLY))
563 usage |= PIPE_MAP_DISCARD_RANGE;
564
565 u_box_1d(offset, size, &box);
566 map = r600_buffer_transfer_map(ctx, buffer, 0, usage, &box, &transfer);
567 if (!map)
568 return;
569
570 memcpy(map, data, size);
571 r600_buffer_transfer_unmap(ctx, transfer);
572 }
573
574 static struct r600_resource *
r600_alloc_buffer_struct(struct pipe_screen * screen,const struct pipe_resource * templ)575 r600_alloc_buffer_struct(struct pipe_screen *screen,
576 const struct pipe_resource *templ)
577 {
578 struct r600_resource *rbuffer;
579
580 rbuffer = MALLOC_STRUCT(r600_resource);
581
582 rbuffer->b.b = *templ;
583 rbuffer->b.b.next = NULL;
584 pipe_reference_init(&rbuffer->b.b.reference, 1);
585 rbuffer->b.b.screen = screen;
586
587 threaded_resource_init(&rbuffer->b.b);
588
589 rbuffer->buf = NULL;
590 rbuffer->bind_history = 0;
591 rbuffer->immed_buffer = NULL;
592 rbuffer->compute_global_bo = false;
593 util_range_init(&rbuffer->valid_buffer_range);
594 return rbuffer;
595 }
596
r600_buffer_create(struct pipe_screen * screen,const struct pipe_resource * templ,unsigned alignment)597 struct pipe_resource *r600_buffer_create(struct pipe_screen *screen,
598 const struct pipe_resource *templ,
599 unsigned alignment)
600 {
601 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
602 struct r600_resource *rbuffer = r600_alloc_buffer_struct(screen, templ);
603
604 r600_init_resource_fields(rscreen, rbuffer, templ->width0, alignment);
605
606 if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
607 rbuffer->flags |= RADEON_FLAG_SPARSE;
608
609 if (!r600_alloc_resource(rscreen, rbuffer)) {
610 FREE(rbuffer);
611 return NULL;
612 }
613 return &rbuffer->b.b;
614 }
615
r600_aligned_buffer_create(struct pipe_screen * screen,unsigned flags,unsigned usage,unsigned size,unsigned alignment)616 struct pipe_resource *r600_aligned_buffer_create(struct pipe_screen *screen,
617 unsigned flags,
618 unsigned usage,
619 unsigned size,
620 unsigned alignment)
621 {
622 struct pipe_resource buffer;
623
624 memset(&buffer, 0, sizeof buffer);
625 buffer.target = PIPE_BUFFER;
626 buffer.format = PIPE_FORMAT_R8_UNORM;
627 buffer.bind = 0;
628 buffer.usage = usage;
629 buffer.flags = flags;
630 buffer.width0 = size;
631 buffer.height0 = 1;
632 buffer.depth0 = 1;
633 buffer.array_size = 1;
634 return r600_buffer_create(screen, &buffer, alignment);
635 }
636
637 struct pipe_resource *
r600_buffer_from_user_memory(struct pipe_screen * screen,const struct pipe_resource * templ,void * user_memory)638 r600_buffer_from_user_memory(struct pipe_screen *screen,
639 const struct pipe_resource *templ,
640 void *user_memory)
641 {
642 struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
643 struct radeon_winsys *ws = rscreen->ws;
644 struct r600_resource *rbuffer = r600_alloc_buffer_struct(screen, templ);
645
646 rbuffer->domains = RADEON_DOMAIN_GTT;
647 rbuffer->flags = 0;
648 rbuffer->b.is_user_ptr = true;
649 util_range_add(&rbuffer->b.b, &rbuffer->valid_buffer_range, 0, templ->width0);
650 util_range_add(&rbuffer->b.b, &rbuffer->b.valid_buffer_range, 0, templ->width0);
651
652 /* Convert a user pointer to a buffer. */
653 rbuffer->buf = ws->buffer_from_ptr(ws, user_memory, templ->width0);
654 if (!rbuffer->buf) {
655 FREE(rbuffer);
656 return NULL;
657 }
658
659 if (rscreen->info.r600_has_virtual_memory)
660 rbuffer->gpu_address =
661 ws->buffer_get_virtual_address(rbuffer->buf);
662 else
663 rbuffer->gpu_address = 0;
664
665 rbuffer->vram_usage = 0;
666 rbuffer->gart_usage = templ->width0;
667
668 return &rbuffer->b.b;
669 }
670