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)
43 #include "pipe/p_compiler.h"
44 #include "pipe/p_error.h"
45 #include "pipe/p_debug.h"
46 #include "pipe/p_winsys.h"
47 #include "pipe/p_thread.h"
48 #include "pipe/p_util.h"
49 #include "util/u_double_list.h"
51 #include "pb_buffer.h"
52 #include "pb_buffer_fenced.h"
57 * Convenience macro (type safe).
59 #define SUPER(__derived) (&(__derived)->base)
61 #define PIPE_BUFFER_USAGE_CPU_READ_WRITE \
62 ( PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE )
63 #define PIPE_BUFFER_USAGE_GPU_READ_WRITE \
64 ( PIPE_BUFFER_USAGE_GPU_READ | PIPE_BUFFER_USAGE_GPU_WRITE )
65 #define PIPE_BUFFER_USAGE_WRITE \
66 ( PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_GPU_WRITE )
69 struct fenced_buffer_list
71 _glthread_Mutex mutex
;
73 struct pipe_winsys
*winsys
;
77 struct list_head delayed
;
82 * Wrapper around a pipe buffer which adds fencing and reference counting.
86 struct pb_buffer base
;
88 struct pb_buffer
*buffer
;
90 /* FIXME: protect access with mutex */
93 * A bitmask of PIPE_BUFFER_USAGE_CPU/GPU_READ/WRITE describing the current
99 struct pipe_fence_handle
*fence
;
101 struct list_head head
;
102 struct fenced_buffer_list
*list
;
106 static INLINE
struct fenced_buffer
*
107 fenced_buffer(struct pb_buffer
*buf
)
110 assert(buf
->vtbl
== &fenced_buffer_vtbl
);
111 return (struct fenced_buffer
*)buf
;
116 _fenced_buffer_add(struct fenced_buffer
*fenced_buf
)
118 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
120 assert(fenced_buf
->base
.base
.refcount
);
121 assert(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
122 assert(fenced_buf
->fence
);
124 assert(!fenced_buf
->head
.prev
);
125 assert(!fenced_buf
->head
.next
);
126 LIST_ADDTAIL(&fenced_buf
->head
, &fenced_list
->delayed
);
127 ++fenced_list
->numDelayed
;
132 * Actually destroy the buffer.
135 _fenced_buffer_destroy(struct fenced_buffer
*fenced_buf
)
137 assert(!fenced_buf
->base
.base
.refcount
);
138 assert(!fenced_buf
->fence
);
139 pb_reference(&fenced_buf
->buffer
, NULL
);
145 _fenced_buffer_remove(struct fenced_buffer
*fenced_buf
)
147 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
148 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
150 assert(fenced_buf
->fence
);
152 winsys
->fence_reference(winsys
, &fenced_buf
->fence
, NULL
);
153 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
155 assert(fenced_buf
->head
.prev
);
156 assert(fenced_buf
->head
.next
);
157 LIST_DEL(&fenced_buf
->head
);
159 fenced_buf
->head
.prev
= NULL
;
160 fenced_buf
->head
.next
= NULL
;
163 assert(fenced_list
->numDelayed
);
164 --fenced_list
->numDelayed
;
166 if(!fenced_buf
->base
.base
.refcount
)
167 _fenced_buffer_destroy(fenced_buf
);
171 static INLINE
enum pipe_error
172 _fenced_buffer_finish(struct fenced_buffer
*fenced_buf
)
174 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
175 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
178 debug_warning("waiting for GPU");
181 assert(fenced_buf
->fence
);
182 if(fenced_buf
->fence
) {
183 if(winsys
->fence_finish(winsys
, fenced_buf
->fence
, 0) != 0) {
186 /* Remove from the fenced list */
187 _fenced_buffer_remove(fenced_buf
); /* TODO: remove consequents */
190 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
196 * Free as many fenced buffers from the list head as possible.
199 _fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
202 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
203 struct list_head
*curr
, *next
;
204 struct fenced_buffer
*fenced_buf
;
205 struct pipe_fence_handle
*prev_fence
= NULL
;
207 curr
= fenced_list
->delayed
.next
;
209 while(curr
!= &fenced_list
->delayed
) {
210 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
212 if(fenced_buf
->fence
!= prev_fence
) {
215 signaled
= winsys
->fence_finish(winsys
, fenced_buf
->fence
, 0);
217 signaled
= winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0);
220 prev_fence
= fenced_buf
->fence
;
223 assert(winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0) == 0);
226 _fenced_buffer_remove(fenced_buf
);
235 fenced_buffer_destroy(struct pb_buffer
*buf
)
237 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
238 struct fenced_buffer_list
*fenced_list
= fenced_buf
->list
;
240 _glthread_LOCK_MUTEX(fenced_list
->mutex
);
241 assert(fenced_buf
->base
.base
.refcount
== 0);
242 if (fenced_buf
->fence
) {
243 struct pipe_winsys
*winsys
= fenced_list
->winsys
;
244 if(winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0) == 0) {
245 struct list_head
*curr
, *prev
;
246 curr
= &fenced_buf
->head
;
249 fenced_buf
= LIST_ENTRY(struct fenced_buffer
, curr
, head
);
250 assert(winsys
->fence_signalled(winsys
, fenced_buf
->fence
, 0) == 0);
251 _fenced_buffer_remove(fenced_buf
);
254 } while (curr
!= &fenced_list
->delayed
);
257 /* delay destruction */
261 _fenced_buffer_destroy(fenced_buf
);
263 _glthread_UNLOCK_MUTEX(fenced_list
->mutex
);
268 fenced_buffer_map(struct pb_buffer
*buf
,
271 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
274 assert(!(flags
& ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
));
275 flags
&= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
277 /* Check for GPU read/write access */
278 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_GPU_WRITE
) {
279 /* Wait for the GPU to finish writing */
280 _fenced_buffer_finish(fenced_buf
);
284 /* Check for CPU write access (read is OK) */
285 if(fenced_buf
->flags
& PIPE_BUFFER_USAGE_CPU_READ_WRITE
) {
286 /* this is legal -- just for debugging */
287 debug_warning("concurrent CPU writes");
291 map
= pb_map(fenced_buf
->buffer
, flags
);
293 ++fenced_buf
->mapcount
;
294 fenced_buf
->flags
|= flags
;
302 fenced_buffer_unmap(struct pb_buffer
*buf
)
304 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
305 assert(fenced_buf
->mapcount
);
306 if(fenced_buf
->mapcount
) {
307 pb_unmap(fenced_buf
->buffer
);
308 --fenced_buf
->mapcount
;
309 if(!fenced_buf
->mapcount
)
310 fenced_buf
->flags
&= ~PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
316 fenced_buffer_get_base_buffer(struct pb_buffer
*buf
,
317 struct pb_buffer
**base_buf
,
320 struct fenced_buffer
*fenced_buf
= fenced_buffer(buf
);
321 pb_get_base_buffer(fenced_buf
->buffer
, base_buf
, offset
);
326 fenced_buffer_vtbl
= {
327 fenced_buffer_destroy
,
330 fenced_buffer_get_base_buffer
335 fenced_buffer_create(struct fenced_buffer_list
*fenced_list
,
336 struct pb_buffer
*buffer
)
338 struct fenced_buffer
*buf
;
343 buf
= CALLOC_STRUCT(fenced_buffer
);
345 pb_reference(&buffer
, NULL
);
349 buf
->base
.base
.refcount
= 1;
350 buf
->base
.base
.alignment
= buffer
->base
.alignment
;
351 buf
->base
.base
.usage
= buffer
->base
.usage
;
352 buf
->base
.base
.size
= buffer
->base
.size
;
354 buf
->base
.vtbl
= &fenced_buffer_vtbl
;
355 buf
->buffer
= buffer
;
356 buf
->list
= fenced_list
;
363 buffer_fence(struct pb_buffer
*buf
,
364 struct pipe_fence_handle
*fence
)
366 struct fenced_buffer
*fenced_buf
;
367 struct fenced_buffer_list
*fenced_list
;
368 struct pipe_winsys
*winsys
;
369 /* FIXME: receive this as a parameter */
370 unsigned flags
= fence
? PIPE_BUFFER_USAGE_GPU_READ_WRITE
: 0;
372 /* This is a public function, so be extra cautious with the buffer passed,
373 * as happens frequently to receive null buffers, or pointer to buffers
374 * other than fenced buffers. */
378 assert(buf
->vtbl
== &fenced_buffer_vtbl
);
379 if(buf
->vtbl
!= &fenced_buffer_vtbl
)
382 fenced_buf
= fenced_buffer(buf
);
383 fenced_list
= fenced_buf
->list
;
384 winsys
= fenced_list
->winsys
;
386 if(!fence
|| fence
== fenced_buf
->fence
) {
387 /* Handle the same fence case specially, not only because it is a fast
388 * path, but mostly to avoid serializing two writes with the same fence,
389 * as that would bring the hardware down to synchronous operation without
392 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
396 _glthread_LOCK_MUTEX(fenced_list
->mutex
);
397 if (fenced_buf
->fence
)
398 _fenced_buffer_remove(fenced_buf
);
400 winsys
->fence_reference(winsys
, &fenced_buf
->fence
, fence
);
401 fenced_buf
->flags
|= flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
402 _fenced_buffer_add(fenced_buf
);
404 _glthread_UNLOCK_MUTEX(fenced_list
->mutex
);
408 struct fenced_buffer_list
*
409 fenced_buffer_list_create(struct pipe_winsys
*winsys
)
411 struct fenced_buffer_list
*fenced_list
;
413 fenced_list
= CALLOC_STRUCT(fenced_buffer_list
);
417 fenced_list
->winsys
= winsys
;
419 LIST_INITHEAD(&fenced_list
->delayed
);
421 fenced_list
->numDelayed
= 0;
423 _glthread_INIT_MUTEX(fenced_list
->mutex
);
430 fenced_buffer_list_check_free(struct fenced_buffer_list
*fenced_list
,
433 _glthread_LOCK_MUTEX(fenced_list
->mutex
);
434 _fenced_buffer_list_check_free(fenced_list
, wait
);
435 _glthread_UNLOCK_MUTEX(fenced_list
->mutex
);
440 fenced_buffer_list_destroy(struct fenced_buffer_list
*fenced_list
)
442 _glthread_LOCK_MUTEX(fenced_list
->mutex
);
444 /* Wait on outstanding fences */
445 while (fenced_list
->numDelayed
) {
446 _glthread_UNLOCK_MUTEX(fenced_list
->mutex
);
447 #if defined(PIPE_OS_LINUX)
450 _fenced_buffer_list_check_free(fenced_list
, 1);
451 _glthread_LOCK_MUTEX(fenced_list
->mutex
);
454 _glthread_UNLOCK_MUTEX(fenced_list
->mutex
);