1 /**************************************************************************
3 * Copyright 2008 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 **************************************************************************/
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
36 #include "pipe/p_compiler.h"
37 #include "pipe/p_error.h"
38 #include "util/u_memory.h"
39 #include "pipe/p_debug.h"
41 #include "pb_buffer.h"
42 #include "pb_buffer_fenced.h"
43 #include "pb_validate.h"
46 #define PB_VALIDATE_INITIAL_SIZE 1 /* 512 */
49 struct pb_validate_entry
51 struct pb_buffer
*buf
;
58 struct pb_validate_entry
*entries
;
65 pb_validate_add_buffer(struct pb_validate
*vl
,
66 struct pb_buffer
*buf
,
73 assert(flags
& PIPE_BUFFER_USAGE_GPU_READ_WRITE
);
74 assert(!(flags
& ~PIPE_BUFFER_USAGE_GPU_READ_WRITE
));
75 flags
&= PIPE_BUFFER_USAGE_GPU_READ_WRITE
;
77 /* We only need to store one reference for each buffer, so avoid storing
78 * consecutive references for the same buffer. It might not be the most
79 * common pattern, but it is easy to implement.
81 if(vl
->used
&& vl
->entries
[vl
->used
- 1].buf
== buf
) {
82 vl
->entries
[vl
->used
- 1].flags
|= flags
;
87 if(vl
->used
== vl
->size
) {
89 struct pb_validate_entry
*new_entries
;
91 new_size
= vl
->size
* 2;
93 return PIPE_ERROR_OUT_OF_MEMORY
;
95 new_entries
= (struct pb_validate_entry
*)REALLOC(vl
->entries
,
96 vl
->size
*sizeof(struct pb_validate_entry
),
97 new_size
*sizeof(struct pb_validate_entry
));
99 return PIPE_ERROR_OUT_OF_MEMORY
;
101 memset(new_entries
+ vl
->size
, 0, (new_size
- vl
->size
)*sizeof(struct pb_validate_entry
));
104 vl
->entries
= new_entries
;
107 assert(!vl
->entries
[vl
->used
].buf
);
108 pb_reference(&vl
->entries
[vl
->used
].buf
, buf
);
109 vl
->entries
[vl
->used
].flags
= flags
;
117 pb_validate_foreach(struct pb_validate
*vl
,
118 enum pipe_error (*callback
)(struct pb_buffer
*buf
, void *data
),
122 for(i
= 0; i
< vl
->used
; ++i
) {
124 ret
= callback(vl
->entries
[i
].buf
, data
);
133 pb_validate_validate(struct pb_validate
*vl
)
137 for(i
= 0; i
< vl
->used
; ++i
) {
139 ret
= pb_validate(vl
->entries
[i
].buf
, vl
, vl
->entries
[i
].flags
);
142 pb_validate(vl
->entries
[i
].buf
, NULL
, 0);
152 pb_validate_fence(struct pb_validate
*vl
,
153 struct pipe_fence_handle
*fence
)
156 for(i
= 0; i
< vl
->used
; ++i
) {
157 pb_fence(vl
->entries
[i
].buf
, fence
);
158 pb_reference(&vl
->entries
[i
].buf
, NULL
);
165 pb_validate_destroy(struct pb_validate
*vl
)
168 for(i
= 0; i
< vl
->used
; ++i
)
169 pb_reference(&vl
->entries
[i
].buf
, NULL
);
178 struct pb_validate
*vl
;
180 vl
= CALLOC_STRUCT(pb_validate
);
184 vl
->size
= PB_VALIDATE_INITIAL_SIZE
;
185 vl
->entries
= (struct pb_validate_entry
*)CALLOC(vl
->size
, sizeof(struct pb_validate_entry
));