2 * Copyright 2013 Advanced Micro Devices, Inc.
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:
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 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.
28 #include "util/u_memory.h"
29 #include "util/u_upload_mgr.h"
33 boolean
r600_rings_is_buffer_referenced(struct r600_common_context
*ctx
,
34 struct radeon_winsys_cs_handle
*buf
,
35 enum radeon_bo_usage usage
)
37 if (ctx
->ws
->cs_is_buffer_referenced(ctx
->rings
.gfx
.cs
, buf
, usage
)) {
40 if (ctx
->rings
.dma
.cs
&& ctx
->rings
.dma
.cs
->cdw
&&
41 ctx
->ws
->cs_is_buffer_referenced(ctx
->rings
.dma
.cs
, buf
, usage
)) {
47 void *r600_buffer_map_sync_with_rings(struct r600_common_context
*ctx
,
48 struct r600_resource
*resource
,
51 enum radeon_bo_usage rusage
= RADEON_USAGE_READWRITE
;
54 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
55 return ctx
->ws
->buffer_map(resource
->cs_buf
, NULL
, usage
);
58 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
59 /* have to wait for the last write */
60 rusage
= RADEON_USAGE_WRITE
;
63 if (ctx
->rings
.gfx
.cs
->cdw
!= ctx
->initial_gfx_cs_size
&&
64 ctx
->ws
->cs_is_buffer_referenced(ctx
->rings
.gfx
.cs
,
65 resource
->cs_buf
, rusage
)) {
66 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
67 ctx
->rings
.gfx
.flush(ctx
, RADEON_FLUSH_ASYNC
, NULL
);
70 ctx
->rings
.gfx
.flush(ctx
, 0, NULL
);
74 if (ctx
->rings
.dma
.cs
&&
75 ctx
->rings
.dma
.cs
->cdw
&&
76 ctx
->ws
->cs_is_buffer_referenced(ctx
->rings
.dma
.cs
,
77 resource
->cs_buf
, rusage
)) {
78 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
79 ctx
->rings
.dma
.flush(ctx
, RADEON_FLUSH_ASYNC
, NULL
);
82 ctx
->rings
.dma
.flush(ctx
, 0, NULL
);
87 if (busy
|| ctx
->ws
->buffer_is_busy(resource
->buf
, rusage
)) {
88 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
91 /* We will be wait for the GPU. Wait for any offloaded
92 * CS flush to complete to avoid busy-waiting in the winsys. */
93 ctx
->ws
->cs_sync_flush(ctx
->rings
.gfx
.cs
);
94 if (ctx
->rings
.dma
.cs
)
95 ctx
->ws
->cs_sync_flush(ctx
->rings
.dma
.cs
);
99 /* Setting the CS to NULL will prevent doing checks we have done already. */
100 return ctx
->ws
->buffer_map(resource
->cs_buf
, NULL
, usage
);
103 bool r600_init_resource(struct r600_common_screen
*rscreen
,
104 struct r600_resource
*res
,
105 unsigned size
, unsigned alignment
,
106 bool use_reusable_pool
)
108 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
109 struct pb_buffer
*old_buf
, *new_buf
;
111 switch (res
->b
.b
.usage
) {
112 case PIPE_USAGE_STAGING
:
113 case PIPE_USAGE_DYNAMIC
:
114 case PIPE_USAGE_STREAM
:
115 /* Transfers are likely to occur more often with these resources. */
116 res
->domains
= RADEON_DOMAIN_GTT
;
118 case PIPE_USAGE_DEFAULT
:
119 case PIPE_USAGE_IMMUTABLE
:
121 /* Not listing GTT here improves performance in some apps. */
122 res
->domains
= RADEON_DOMAIN_VRAM
;
126 /* Use GTT for all persistent mappings, because they are
127 * always cached and coherent. */
128 if (res
->b
.b
.target
== PIPE_BUFFER
&&
129 res
->b
.b
.flags
& (PIPE_RESOURCE_FLAG_MAP_PERSISTENT
|
130 PIPE_RESOURCE_FLAG_MAP_COHERENT
)) {
131 res
->domains
= RADEON_DOMAIN_GTT
;
134 /* Tiled textures are unmappable. Always put them in VRAM. */
135 if (res
->b
.b
.target
!= PIPE_BUFFER
&&
136 rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
) {
137 res
->domains
= RADEON_DOMAIN_VRAM
;
140 /* Allocate a new resource. */
141 new_buf
= rscreen
->ws
->buffer_create(rscreen
->ws
, size
, alignment
,
148 /* Replace the pointer such that if res->buf wasn't NULL, it won't be
149 * NULL. This should prevent crashes with multiple contexts using
150 * the same buffer where one of the contexts invalidates it while
151 * the others are using it. */
153 res
->cs_buf
= rscreen
->ws
->buffer_get_cs_handle(new_buf
); /* should be atomic */
154 res
->buf
= new_buf
; /* should be atomic */
155 pb_reference(&old_buf
, NULL
);
157 util_range_set_empty(&res
->valid_buffer_range
);
159 if (rscreen
->debug_flags
& DBG_VM
&& res
->b
.b
.target
== PIPE_BUFFER
) {
160 fprintf(stderr
, "VM start=0x%"PRIu64
" end=0x%"PRIu64
" | Buffer %u bytes\n",
161 r600_resource_va(&rscreen
->b
, &res
->b
.b
),
162 r600_resource_va(&rscreen
->b
, &res
->b
.b
) + res
->buf
->size
,
168 static void r600_buffer_destroy(struct pipe_screen
*screen
,
169 struct pipe_resource
*buf
)
171 struct r600_resource
*rbuffer
= r600_resource(buf
);
173 util_range_destroy(&rbuffer
->valid_buffer_range
);
174 pb_reference(&rbuffer
->buf
, NULL
);
178 static void *r600_buffer_get_transfer(struct pipe_context
*ctx
,
179 struct pipe_resource
*resource
,
182 const struct pipe_box
*box
,
183 struct pipe_transfer
**ptransfer
,
184 void *data
, struct r600_resource
*staging
,
187 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
188 struct r600_transfer
*transfer
= util_slab_alloc(&rctx
->pool_transfers
);
190 transfer
->transfer
.resource
= resource
;
191 transfer
->transfer
.level
= level
;
192 transfer
->transfer
.usage
= usage
;
193 transfer
->transfer
.box
= *box
;
194 transfer
->transfer
.stride
= 0;
195 transfer
->transfer
.layer_stride
= 0;
196 transfer
->offset
= offset
;
197 transfer
->staging
= staging
;
198 *ptransfer
= &transfer
->transfer
;
202 static bool r600_can_dma_copy_buffer(struct r600_common_context
*rctx
,
203 unsigned dstx
, unsigned srcx
, unsigned size
)
205 bool dword_aligned
= !(dstx
% 4) && !(srcx
% 4) && !(size
% 4);
207 return rctx
->screen
->has_cp_dma
||
208 (dword_aligned
&& (rctx
->rings
.dma
.cs
||
209 rctx
->screen
->has_streamout
));
213 static void *r600_buffer_transfer_map(struct pipe_context
*ctx
,
214 struct pipe_resource
*resource
,
217 const struct pipe_box
*box
,
218 struct pipe_transfer
**ptransfer
)
220 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
221 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)ctx
->screen
;
222 struct r600_resource
*rbuffer
= r600_resource(resource
);
225 assert(box
->x
+ box
->width
<= resource
->width0
);
227 /* See if the buffer range being mapped has never been initialized,
228 * in which case it can be mapped unsynchronized. */
229 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
230 usage
& PIPE_TRANSFER_WRITE
&&
231 !util_ranges_intersect(&rbuffer
->valid_buffer_range
, box
->x
, box
->x
+ box
->width
)) {
232 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
235 /* If discarding the entire range, discard the whole resource instead. */
236 if (usage
& PIPE_TRANSFER_DISCARD_RANGE
&&
237 box
->x
== 0 && box
->width
== resource
->width0
) {
238 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
241 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
&&
242 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
243 assert(usage
& PIPE_TRANSFER_WRITE
);
245 /* Check if mapping this buffer would cause waiting for the GPU. */
246 if (r600_rings_is_buffer_referenced(rctx
, rbuffer
->cs_buf
, RADEON_USAGE_READWRITE
) ||
247 rctx
->ws
->buffer_is_busy(rbuffer
->buf
, RADEON_USAGE_READWRITE
)) {
248 rctx
->invalidate_buffer(&rctx
->b
, &rbuffer
->b
.b
);
250 /* At this point, the buffer is always idle. */
251 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
253 else if ((usage
& PIPE_TRANSFER_DISCARD_RANGE
) &&
254 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
255 !(rscreen
->debug_flags
& DBG_NO_DISCARD_RANGE
) &&
256 r600_can_dma_copy_buffer(rctx
, box
->x
, 0, box
->width
)) {
257 assert(usage
& PIPE_TRANSFER_WRITE
);
259 /* Check if mapping this buffer would cause waiting for the GPU. */
260 if (r600_rings_is_buffer_referenced(rctx
, rbuffer
->cs_buf
, RADEON_USAGE_READWRITE
) ||
261 rctx
->ws
->buffer_is_busy(rbuffer
->buf
, RADEON_USAGE_READWRITE
)) {
262 /* Do a wait-free write-only transfer using a temporary buffer. */
264 struct r600_resource
*staging
= NULL
;
266 u_upload_alloc(rctx
->uploader
, 0, box
->width
+ (box
->x
% R600_MAP_BUFFER_ALIGNMENT
),
267 &offset
, (struct pipe_resource
**)&staging
, (void**)&data
);
270 data
+= box
->x
% R600_MAP_BUFFER_ALIGNMENT
;
271 return r600_buffer_get_transfer(ctx
, resource
, level
, usage
, box
,
272 ptransfer
, data
, staging
, offset
);
274 return NULL
; /* error, shouldn't occur though */
277 /* At this point, the buffer is always idle (we checked it above). */
278 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
280 /* Using a staging buffer in GTT for larger reads is much faster. */
281 else if ((usage
& PIPE_TRANSFER_READ
) &&
282 !(usage
& PIPE_TRANSFER_WRITE
) &&
283 rbuffer
->domains
== RADEON_DOMAIN_VRAM
&&
284 r600_can_dma_copy_buffer(rctx
, 0, box
->x
, box
->width
)) {
286 struct r600_resource
*staging
= NULL
;
288 u_upload_alloc(rctx
->uploader
, 0,
289 box
->width
+ (box
->x
% R600_MAP_BUFFER_ALIGNMENT
),
290 &offset
, (struct pipe_resource
**)&staging
, (void**)&data
);
293 data
+= box
->x
% R600_MAP_BUFFER_ALIGNMENT
;
295 /* Copy the VRAM buffer to the staging buffer. */
296 rctx
->dma_copy(ctx
, &staging
->b
.b
, 0,
297 offset
+ box
->x
% R600_MAP_BUFFER_ALIGNMENT
,
298 0, 0, resource
, level
, box
);
300 /* Just do the synchronization. The buffer is mapped already. */
301 r600_buffer_map_sync_with_rings(rctx
, staging
, PIPE_TRANSFER_READ
);
303 return r600_buffer_get_transfer(ctx
, resource
, level
, usage
, box
,
304 ptransfer
, data
, staging
, offset
);
308 data
= r600_buffer_map_sync_with_rings(rctx
, rbuffer
, usage
);
314 return r600_buffer_get_transfer(ctx
, resource
, level
, usage
, box
,
315 ptransfer
, data
, NULL
, 0);
318 static void r600_buffer_transfer_unmap(struct pipe_context
*ctx
,
319 struct pipe_transfer
*transfer
)
321 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
322 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
323 struct r600_resource
*rbuffer
= r600_resource(transfer
->resource
);
325 if (rtransfer
->staging
) {
326 if (rtransfer
->transfer
.usage
& PIPE_TRANSFER_WRITE
) {
327 struct pipe_resource
*dst
, *src
;
328 unsigned soffset
, doffset
, size
;
331 dst
= transfer
->resource
;
332 src
= &rtransfer
->staging
->b
.b
;
333 size
= transfer
->box
.width
;
334 doffset
= transfer
->box
.x
;
335 soffset
= rtransfer
->offset
+ transfer
->box
.x
% R600_MAP_BUFFER_ALIGNMENT
;
337 u_box_1d(soffset
, size
, &box
);
339 /* Copy the staging buffer into the original one. */
340 rctx
->dma_copy(ctx
, dst
, 0, doffset
, 0, 0, src
, 0, &box
);
342 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
345 if (transfer
->usage
& PIPE_TRANSFER_WRITE
) {
346 util_range_add(&rbuffer
->valid_buffer_range
, transfer
->box
.x
,
347 transfer
->box
.x
+ transfer
->box
.width
);
349 util_slab_free(&rctx
->pool_transfers
, transfer
);
352 static const struct u_resource_vtbl r600_buffer_vtbl
=
354 NULL
, /* get_handle */
355 r600_buffer_destroy
, /* resource_destroy */
356 r600_buffer_transfer_map
, /* transfer_map */
357 NULL
, /* transfer_flush_region */
358 r600_buffer_transfer_unmap
, /* transfer_unmap */
359 NULL
/* transfer_inline_write */
362 struct pipe_resource
*r600_buffer_create(struct pipe_screen
*screen
,
363 const struct pipe_resource
*templ
,
366 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
367 struct r600_resource
*rbuffer
;
369 rbuffer
= MALLOC_STRUCT(r600_resource
);
371 rbuffer
->b
.b
= *templ
;
372 pipe_reference_init(&rbuffer
->b
.b
.reference
, 1);
373 rbuffer
->b
.b
.screen
= screen
;
374 rbuffer
->b
.vtbl
= &r600_buffer_vtbl
;
376 util_range_init(&rbuffer
->valid_buffer_range
);
378 if (!r600_init_resource(rscreen
, rbuffer
, templ
->width0
, alignment
, TRUE
)) {
382 return &rbuffer
->b
.b
;