1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Implementation of fenced buffers.
32 * \author José Fonseca <jrfonseca-at-tungstengraphics-dot-com>
33 * \author Thomas Hellström <thomas-at-tungstengraphics-dot-com>
37 #include "pipe/p_config.h"
39 #if defined(PIPE_OS_LINUX)
44 #include "pipe/p_compiler.h"
45 #include "pipe/p_error.h"
46 #include "pipe/p_debug.h"
47 #include "pipe/p_winsys.h"
48 #include "pipe/p_thread.h"
49 #include "util/u_memory.h"
50 #include "util/u_double_list.h"
52 #include "pb_buffer.h"
53 #include "pb_buffer_fenced.h"
58 * Convenience macro (type safe).
60 #define SUPER(__derived) (&(__derived)->base)
62 #define PIPE_BUFFER_USAGE_CPU_READ_WRITE \
63 ( PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE )
64 #define PIPE_BUFFER_USAGE_GPU_READ_WRITE \
65 ( PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE )
66 #define PIPE_BUFFER_USAGE_WRITE \
67 ( PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_GPU_WRITE )
70 struct fenced_buffer_list
74 struct pipe_winsys
*winsys
;
77 struct list_head delayed
;
81 struct list_head unfenced
;
87 * Wrapper around a pipe buffer which adds fencing and reference counting.
91 struct pb_buffer base
;
93 struct pb_buffer
*buffer
;
95 /* FIXME: protect access with mutex */
98 * A bitmask of PIPE_BUFFER_USAGE_CPU/GPU_READ/WRITE describing the current
104 struct pipe_fence_handle
*fence
;
106 struct list_head head
;
107 struct fenced_buffer_list
*list
;
111 static INLINE
struct fenced_buffer
*
112 fenced_buffer(struct pb_buffer
*buf
)
115 assert(buf
->vtbl
== &fenced_buffer_vtbl
);
116 return (struct fenced_buffer
*)buf
;
121 _fenced_buffer_add(struct fenced_buffer
*fenced_buf
)
123 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
125 assert(fenced_buf
->base
.base
.refcount
);
126 assert(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
127 assert(fenced_buf
->fence
);
130 LIST_DEL(&fenced_buf
->head
);
131 assert(fenced_list
->numUnfenced
);
132 --fenced_list
->numUnfenced
;
134 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->delayed
);
135 ++fenced_list
->numDelayed
;
140 * Actually destroy the buffer.
143 _fenced_buffer_destroy(struct fenced_buffer
*fenced_buf
)
145 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
147 assert(!fenced_buf
->base
.base
.refcount
);
148 assert(!fenced_buf
->fence
);
150 assert(fenced_buf
->head
.prev
);
151 assert(fenced_buf
->head
.next
);
152 LIST_DEL(&fenced_buf
->head
);
153 assert(fenced_list
->numUnfenced
);
154 --fenced_list
->numUnfenced
;
158 pb_reference(&fenced_buf
->buffer
, NULL
);
164 _fenced_buffer_remove(struct fenced_buffer_list
*fenced_list
,
165 struct fenced_buffer
*fenced_buf
)
167 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
169 assert(fenced_buf
->fence
);
170 assert(fenced_buf
->list
== fenced_list
);
172 winsys
->fence_reference(winsys
, &fenced_buf
->fence
, NULL
);
173 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
175 assert(fenced_buf
->head
.prev
);
176 assert(fenced_buf
->head
.next
);
178 LIST_DEL(&fenced_buf
->head
);
179 assert(fenced_list
->numDelayed
);
180 --fenced_list
->numDelayed
;
183 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->unfenced
);
184 ++fenced_list
->numUnfenced
;
187 if(!fenced_buf
->base
.base
.refcount
)
188 _fenced_buffer_destroy(fenced_buf
);
192 static INLINE
enum pipe_error
193 _fenced_buffer_finish(struct fenced_buffer
*fenced_buf
)
195 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
196 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
199 debug_warning("waiting for GPU");
202 assert(fenced_buf
->fence
);
203 if(fenced_buf
->fence
) {
204 if(winsys
->fence_finish(winsys
, fenced_buf
->fence
, 0) != 0) {
207 /* Remove from the fenced list */
208 /* TODO: remove consequents */
209 _fenced_buffer_remove(fenced_list
, fenced_buf
);
212 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
218 * Free as many fenced buffers from the list head as possible.
221 _fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
224 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
225 struct list_head
*curr
, *next
;
226 struct fenced_buffer
*fenced_buf
;
227 struct pipe_fence_handle
*prev_fence
= NULL
;
229 curr
= fenced_list
->delayed
.next
;
231 while(curr
!= &fenced_list
->delayed
) {
232 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
234 if(fenced_buf
->fence
!= prev_fence
) {
237 signaled
= winsys
->fence_finish(winsys
, fenced_buf
->fence
, 0);
239 signaled
= winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0);
242 prev_fence
= fenced_buf
->fence
;
245 assert(winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0) == 0);
248 _fenced_buffer_remove(fenced_list
, fenced_buf
);
257 fenced_buffer_destroy(struct pb_buffer
*buf
)
259 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
260 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
262 pipe_mutex_lock(fenced_list
->mutex
);
263 assert(fenced_buf
->base
.base
.refcount
== 0);
264 if (fenced_buf
->fence
) {
265 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
266 if(winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0) == 0) {
267 struct list_head
*curr
, *prev
;
268 curr
= &fenced_buf
->head
;
271 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
272 assert(winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0) == 0);
273 _fenced_buffer_remove(fenced_list
, fenced_buf
);
276 } while (curr
!= &fenced_list
->delayed
);
279 /* delay destruction */
283 _fenced_buffer_destroy(fenced_buf
);
285 pipe_mutex_unlock(fenced_list
->mutex
);
290 fenced_buffer_map(struct pb_buffer
*buf
,
293 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
296 assert(!(flags
& ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
297 flags
&= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
299 /* Check for GPU read/write access */
300 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
301 /* Wait for the GPU to finish writing */
302 _fenced_buffer_finish(fenced_buf
);
306 /* Check for CPU write access (read is OK) */
307 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
308 /* this is legal -- just for debugging */
309 debug_warning("concurrent CPU writes");
313 map
= pb_map(fenced_buf
->buffer
, flags
);
315 ++fenced_buf
->mapcount
;
316 fenced_buf
->flags
|= flags
;
324 fenced_buffer_unmap(struct pb_buffer
*buf
)
326 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
327 assert(fenced_buf
->mapcount
);
328 if(fenced_buf
->mapcount
) {
329 pb_unmap(fenced_buf
->buffer
);
330 --fenced_buf
->mapcount
;
331 if(!fenced_buf
->mapcount
)
332 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
338 fenced_buffer_get_base_buffer(struct pb_buffer
*buf
,
339 struct pb_buffer
**base_buf
,
342 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
343 pb_get_base_buffer(fenced_buf
->buffer
, base_buf
, offset
);
348 fenced_buffer_vtbl
= {
349 fenced_buffer_destroy
,
352 fenced_buffer_get_base_buffer
357 fenced_buffer_create(struct fenced_buffer_list
*fenced_list
,
358 struct pb_buffer
*buffer
)
360 struct fenced_buffer
*buf
;
365 buf
= CALLOC_STRUCT(fenced_buffer
);
367 pb_reference(&buffer
, NULL
);
371 buf
->base
.base
.refcount
= 1;
372 buf
->base
.base
.alignment
= buffer
->base
.alignment
;
373 buf
->base
.base
.usage
= buffer
->base
.usage
;
374 buf
->base
.base
.size
= buffer
->base
.size
;
376 buf
->base
.vtbl
= &fenced_buffer_vtbl
;
377 buf
->buffer
= buffer
;
378 buf
->list
= fenced_list
;
381 pipe_mutex_lock(fenced_list
->mutex
);
382 LIST_ADDTAIL(&buf
->head
, &fenced_list
->unfenced
);
383 ++fenced_list
->numUnfenced
;
384 pipe_mutex_unlock(fenced_list
->mutex
);
392 buffer_fence(struct pb_buffer
*buf
,
393 struct pipe_fence_handle
*fence
)
395 struct fenced_buffer
*fenced_buf
;
396 struct fenced_buffer_list
*fenced_list
;
397 struct pipe_winsys
*winsys
;
398 /* FIXME: receive this as a parameter */
399 unsigned flags
= fence
? PIPE_BUFFER_USAGE_GPU_READ_WRITE
: 0;
401 /* This is a public function, so be extra cautious with the buffer passed,
402 * as happens frequently to receive null buffers, or pointer to buffers
403 * other than fenced buffers. */
407 assert(buf
->vtbl
== &fenced_buffer_vtbl
);
408 if(buf
->vtbl
!= &fenced_buffer_vtbl
)
411 fenced_buf
= fenced_buffer(buf
);
412 fenced_list
= fenced_buf
->list
;
413 winsys
= fenced_list
->winsys
;
415 if(!fence
|| fence
== fenced_buf
->fence
) {
416 /* Handle the same fence case specially, not only because it is a fast
417 * path, but mostly to avoid serializing two writes with the same fence,
418 * as that would bring the hardware down to synchronous operation without
421 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
425 pipe_mutex_lock(fenced_list
->mutex
);
426 if (fenced_buf
->fence
)
427 _fenced_buffer_remove(fenced_list
, fenced_buf
);
429 winsys
->fence_reference(winsys
, &fenced_buf
->fence
, fence
);
430 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
431 _fenced_buffer_add(fenced_buf
);
433 pipe_mutex_unlock(fenced_list
->mutex
);
437 struct fenced_buffer_list
*
438 fenced_buffer_list_create(struct pipe_winsys
*winsys
)
440 struct fenced_buffer_list
*fenced_list
;
442 fenced_list
= CALLOC_STRUCT(fenced_buffer_list
);
446 fenced_list
->winsys
= winsys
;
448 LIST_INITHEAD(&fenced_list
->delayed
);
449 fenced_list
->numDelayed
= 0;
452 LIST_INITHEAD(&fenced_list
->unfenced
);
453 fenced_list
->numUnfenced
= 0;
456 pipe_mutex_init(fenced_list
->mutex
);
463 fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
466 pipe_mutex_lock(fenced_list
->mutex
);
467 _fenced_buffer_list_check_free(fenced_list
, wait
);
468 pipe_mutex_unlock(fenced_list
->mutex
);
474 fenced_buffer_list_dump(struct fenced_buffer_list
*fenced_list
)
476 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
477 struct list_head
*curr
, *next
;
478 struct fenced_buffer
*fenced_buf
;
479 struct pipe_fence_handle
*prev_fence
= NULL
;
481 pipe_mutex_lock(fenced_list
->mutex
);
483 debug_printf("%10s %7s %10s %s\n",
484 "buffer", "refcount", "fence", "signalled");
486 curr
= fenced_list
->unfenced
.next
;
488 while(curr
!= &fenced_list
->unfenced
) {
489 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
490 assert(!fenced_buf
->fence
);
491 debug_printf("%10p %7u\n",
493 fenced_buf
->base
.base
.refcount
);
498 curr
= fenced_list
->delayed
.next
;
500 while(curr
!= &fenced_list
->delayed
) {
502 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
503 signaled
= winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0);
504 debug_printf("%10p %7u %10p %s\n",
506 fenced_buf
->base
.base
.refcount
,
508 signaled
== 0 ? "y" : "n");
513 pipe_mutex_unlock(fenced_list
->mutex
);
519 fenced_buffer_list_destroy(struct fenced_buffer_list
*fenced_list
)
521 pipe_mutex_lock(fenced_list
->mutex
);
523 /* Wait on outstanding fences */
524 while (fenced_list
->numDelayed
) {
525 pipe_mutex_unlock(fenced_list
->mutex
);
526 #if defined(PIPE_OS_LINUX)
529 _fenced_buffer_list_check_free(fenced_list
, 1);
530 pipe_mutex_lock(fenced_list
->mutex
);
534 //assert(!fenced_list->numUnfenced);
537 pipe_mutex_unlock(fenced_list
->mutex
);