gallium/util: factor out primitive-restart rewriting logic
[mesa.git] / src / gallium / auxiliary / pipebuffer / pb_buffer.h
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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 VMWARE 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.
25 *
26 **************************************************************************/
27
28 /**
29 * \file
30 * Generic code for buffers.
31 *
32 * Behind a pipe buffle handle there can be DMA buffers, client (or user)
33 * buffers, regular malloced buffers, etc. This file provides an abstract base
34 * buffer handle that allows the driver to cope with all those kinds of buffers
35 * in a more flexible way.
36 *
37 * There is no obligation of a winsys driver to use this library. And a pipe
38 * driver should be completly agnostic about it.
39 *
40 * \author Jose Fonseca <jfonseca@vmware.com>
41 */
42
43 #ifndef PB_BUFFER_H_
44 #define PB_BUFFER_H_
45
46
47 #include "pipe/p_compiler.h"
48 #include "util/u_debug.h"
49 #include "util/u_inlines.h"
50 #include "pipe/p_defines.h"
51
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57
58 struct pb_vtbl;
59 struct pb_validate;
60 struct pipe_fence_handle;
61
62 enum pb_usage_flags {
63 PB_USAGE_CPU_READ = (1 << 0),
64 PB_USAGE_CPU_WRITE = (1 << 1),
65 PB_USAGE_GPU_READ = (1 << 2),
66 PB_USAGE_GPU_WRITE = (1 << 3),
67 PB_USAGE_DONTBLOCK = (1 << 9),
68 PB_USAGE_UNSYNCHRONIZED = (1 << 10),
69 /* Persistent mappings may remain across a flush. Note that contrary
70 * to OpenGL persistent maps, there is no requirement at the pipebuffer
71 * api level to explicitly enforce coherency by barriers or range flushes.
72 */
73 PB_USAGE_PERSISTENT = (1 << 13)
74 };
75
76 /* For error checking elsewhere */
77 #define PB_USAGE_ALL (PB_USAGE_CPU_READ | \
78 PB_USAGE_CPU_WRITE | \
79 PB_USAGE_GPU_READ | \
80 PB_USAGE_GPU_WRITE | \
81 PB_USAGE_DONTBLOCK | \
82 PB_USAGE_UNSYNCHRONIZED | \
83 PB_USAGE_PERSISTENT)
84
85 #define PB_USAGE_CPU_READ_WRITE (PB_USAGE_CPU_READ | PB_USAGE_CPU_WRITE)
86 #define PB_USAGE_GPU_READ_WRITE (PB_USAGE_GPU_READ | PB_USAGE_GPU_WRITE)
87 #define PB_USAGE_WRITE (PB_USAGE_CPU_WRITE | PB_USAGE_GPU_WRITE)
88
89
90 /**
91 * Buffer description.
92 *
93 * Used when allocating the buffer.
94 */
95 struct pb_desc
96 {
97 unsigned alignment;
98 enum pb_usage_flags usage;
99 };
100
101
102 /**
103 * 64-bit type for GPU buffer sizes and offsets.
104 */
105 typedef uint64_t pb_size;
106
107
108 /**
109 * Base class for all pb_* buffers.
110 */
111 struct pb_buffer
112 {
113 struct pipe_reference reference;
114 unsigned alignment;
115 pb_size size;
116 enum pb_usage_flags usage;
117
118 /**
119 * Pointer to the virtual function table.
120 *
121 * Avoid accessing this table directly. Use the inline functions below
122 * instead to avoid mistakes.
123 */
124 const struct pb_vtbl *vtbl;
125 };
126
127
128 /**
129 * Virtual function table for the buffer storage operations.
130 *
131 * Note that creation is not done through this table.
132 */
133 struct pb_vtbl
134 {
135 void (*destroy)(struct pb_buffer *buf);
136
137 /**
138 * Map the entire data store of a buffer object into the client's address.
139 * flags is bitmask of PB_USAGE_CPU_READ/WRITE.
140 */
141 void *(*map)(struct pb_buffer *buf,
142 enum pb_usage_flags flags, void *flush_ctx);
143
144 void (*unmap)(struct pb_buffer *buf);
145
146 enum pipe_error (*validate)(struct pb_buffer *buf,
147 struct pb_validate *vl,
148 enum pb_usage_flags flags);
149
150 void (*fence)(struct pb_buffer *buf,
151 struct pipe_fence_handle *fence);
152
153 /**
154 * Get the base buffer and the offset.
155 *
156 * A buffer can be subdivided in smaller buffers. This method should return
157 * the underlaying buffer, and the relative offset.
158 *
159 * Buffers without an underlaying base buffer should return themselves, with
160 * a zero offset.
161 *
162 * Note that this will increase the reference count of the base buffer.
163 */
164 void (*get_base_buffer)(struct pb_buffer *buf,
165 struct pb_buffer **base_buf,
166 pb_size *offset);
167 };
168
169
170
171 /* Accessor functions for pb->vtbl:
172 */
173 static inline void *
174 pb_map(struct pb_buffer *buf, enum pb_usage_flags flags, void *flush_ctx)
175 {
176 assert(buf);
177 if (!buf)
178 return NULL;
179 assert(pipe_is_referenced(&buf->reference));
180 return buf->vtbl->map(buf, flags, flush_ctx);
181 }
182
183
184 static inline void
185 pb_unmap(struct pb_buffer *buf)
186 {
187 assert(buf);
188 if (!buf)
189 return;
190 assert(pipe_is_referenced(&buf->reference));
191 buf->vtbl->unmap(buf);
192 }
193
194
195 static inline void
196 pb_get_base_buffer(struct pb_buffer *buf,
197 struct pb_buffer **base_buf,
198 pb_size *offset)
199 {
200 assert(buf);
201 if (!buf) {
202 base_buf = NULL;
203 offset = 0;
204 return;
205 }
206 assert(pipe_is_referenced(&buf->reference));
207 assert(buf->vtbl->get_base_buffer);
208 buf->vtbl->get_base_buffer(buf, base_buf, offset);
209 assert(*base_buf);
210 assert(*offset < (*base_buf)->size);
211 }
212
213
214 static inline enum pipe_error
215 pb_validate(struct pb_buffer *buf, struct pb_validate *vl,
216 enum pb_usage_flags flags)
217 {
218 assert(buf);
219 if (!buf)
220 return PIPE_ERROR;
221 assert(buf->vtbl->validate);
222 return buf->vtbl->validate(buf, vl, flags);
223 }
224
225
226 static inline void
227 pb_fence(struct pb_buffer *buf, struct pipe_fence_handle *fence)
228 {
229 assert(buf);
230 if (!buf)
231 return;
232 assert(buf->vtbl->fence);
233 buf->vtbl->fence(buf, fence);
234 }
235
236
237 static inline void
238 pb_destroy(struct pb_buffer *buf)
239 {
240 assert(buf);
241 if (!buf)
242 return;
243 assert(!pipe_is_referenced(&buf->reference));
244 buf->vtbl->destroy(buf);
245 }
246
247
248 static inline void
249 pb_reference(struct pb_buffer **dst,
250 struct pb_buffer *src)
251 {
252 struct pb_buffer *old = *dst;
253
254 if (pipe_reference(&(*dst)->reference, &src->reference))
255 pb_destroy(old);
256 *dst = src;
257 }
258
259
260 /**
261 * Utility function to check whether the provided alignment is consistent with
262 * the requested or not.
263 */
264 static inline boolean
265 pb_check_alignment(pb_size requested, pb_size provided)
266 {
267 if (!requested)
268 return TRUE;
269 if (requested > provided)
270 return FALSE;
271 if (provided % requested != 0)
272 return FALSE;
273 return TRUE;
274 }
275
276
277 /**
278 * Utility function to check whether the provided alignment is consistent with
279 * the requested or not.
280 */
281 static inline boolean
282 pb_check_usage(unsigned requested, unsigned provided)
283 {
284 return (requested & provided) == requested ? TRUE : FALSE;
285 }
286
287
288 /**
289 * Malloc-based buffer to store data that can't be used by the graphics
290 * hardware.
291 */
292 struct pb_buffer *
293 pb_malloc_buffer_create(pb_size size,
294 const struct pb_desc *desc);
295
296
297 #ifdef __cplusplus
298 }
299 #endif
300
301 #endif /*PB_BUFFER_H_*/