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 /* Serialize writes */
300 if((fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_WRITE
) ||
301 ((fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_READ
) && (flags
& PIPE_BUFFER_USAGE_CPU_WRITE
))) {
302 /* Wait for the GPU to finish */
303 _fenced_buffer_finish(fenced_buf
);
307 /* Check for CPU write access (read is OK) */
308 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
309 /* this is legal -- just for debugging */
310 debug_warning("concurrent CPU writes");
314 map
= pb_map(fenced_buf
->buffer
, flags
);
316 ++fenced_buf
->mapcount
;
317 fenced_buf
->flags
|= flags
;
325 fenced_buffer_unmap(struct pb_buffer
*buf
)
327 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
328 assert(fenced_buf
->mapcount
);
329 if(fenced_buf
->mapcount
) {
330 pb_unmap(fenced_buf
->buffer
);
331 --fenced_buf
->mapcount
;
332 if(!fenced_buf
->mapcount
)
333 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
339 fenced_buffer_get_base_buffer(struct pb_buffer
*buf
,
340 struct pb_buffer
**base_buf
,
343 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
344 pb_get_base_buffer(fenced_buf
->buffer
, base_buf
, offset
);
349 fenced_buffer_vtbl
= {
350 fenced_buffer_destroy
,
353 fenced_buffer_get_base_buffer
358 fenced_buffer_create(struct fenced_buffer_list
*fenced_list
,
359 struct pb_buffer
*buffer
)
361 struct fenced_buffer
*buf
;
366 buf
= CALLOC_STRUCT(fenced_buffer
);
368 pb_reference(&buffer
, NULL
);
372 buf
->base
.base
.refcount
= 1;
373 buf
->base
.base
.alignment
= buffer
->base
.alignment
;
374 buf
->base
.base
.usage
= buffer
->base
.usage
;
375 buf
->base
.base
.size
= buffer
->base
.size
;
377 buf
->base
.vtbl
= &fenced_buffer_vtbl
;
378 buf
->buffer
= buffer
;
379 buf
->list
= fenced_list
;
382 pipe_mutex_lock(fenced_list
->mutex
);
383 LIST_ADDTAIL(&buf
->head
, &fenced_list
->unfenced
);
384 ++fenced_list
->numUnfenced
;
385 pipe_mutex_unlock(fenced_list
->mutex
);
393 buffer_fence(struct pb_buffer
*buf
,
394 struct pipe_fence_handle
*fence
)
396 struct fenced_buffer
*fenced_buf
;
397 struct fenced_buffer_list
*fenced_list
;
398 struct pipe_winsys
*winsys
;
399 /* FIXME: receive this as a parameter */
400 unsigned flags
= fence
? PIPE_BUFFER_USAGE_GPU_READ_WRITE
: 0;
402 /* This is a public function, so be extra cautious with the buffer passed,
403 * as happens frequently to receive null buffers, or pointer to buffers
404 * other than fenced buffers. */
408 assert(buf
->vtbl
== &fenced_buffer_vtbl
);
409 if(buf
->vtbl
!= &fenced_buffer_vtbl
)
412 fenced_buf
= fenced_buffer(buf
);
413 fenced_list
= fenced_buf
->list
;
414 winsys
= fenced_list
->winsys
;
416 if(!fence
|| fence
== fenced_buf
->fence
) {
417 /* Handle the same fence case specially, not only because it is a fast
418 * path, but mostly to avoid serializing two writes with the same fence,
419 * as that would bring the hardware down to synchronous operation without
422 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
426 pipe_mutex_lock(fenced_list
->mutex
);
427 if (fenced_buf
->fence
)
428 _fenced_buffer_remove(fenced_list
, fenced_buf
);
430 winsys
->fence_reference(winsys
, &fenced_buf
->fence
, fence
);
431 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
432 _fenced_buffer_add(fenced_buf
);
434 pipe_mutex_unlock(fenced_list
->mutex
);
438 struct fenced_buffer_list
*
439 fenced_buffer_list_create(struct pipe_winsys
*winsys
)
441 struct fenced_buffer_list
*fenced_list
;
443 fenced_list
= CALLOC_STRUCT(fenced_buffer_list
);
447 fenced_list
->winsys
= winsys
;
449 LIST_INITHEAD(&fenced_list
->delayed
);
450 fenced_list
->numDelayed
= 0;
453 LIST_INITHEAD(&fenced_list
->unfenced
);
454 fenced_list
->numUnfenced
= 0;
457 pipe_mutex_init(fenced_list
->mutex
);
464 fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
467 pipe_mutex_lock(fenced_list
->mutex
);
468 _fenced_buffer_list_check_free(fenced_list
, wait
);
469 pipe_mutex_unlock(fenced_list
->mutex
);
475 fenced_buffer_list_dump(struct fenced_buffer_list
*fenced_list
)
477 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
478 struct list_head
*curr
, *next
;
479 struct fenced_buffer
*fenced_buf
;
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
);