st/mesa: put ATI_texture_mirror_once in the right place
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Functions for pixel buffer objects and vertex/element buffer objects.
31 */
32
33
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/arrayobj.h"
37 #include "main/bufferobj.h"
38
39 #include "st_inlines.h"
40 #include "st_context.h"
41 #include "st_cb_bufferobjects.h"
42
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_inlines.h"
46
47
48 /**
49 * There is some duplication between mesa's bufferobjects and our
50 * bufmgr buffers. Both have an integer handle and a hashtable to
51 * lookup an opaque structure. It would be nice if the handles and
52 * internal structure where somehow shared.
53 */
54 static struct gl_buffer_object *
55 st_bufferobj_alloc(GLcontext *ctx, GLuint name, GLenum target)
56 {
57 struct st_buffer_object *st_obj = ST_CALLOC_STRUCT(st_buffer_object);
58
59 if (!st_obj)
60 return NULL;
61
62 _mesa_initialize_buffer_object(&st_obj->Base, name, target);
63
64 return &st_obj->Base;
65 }
66
67
68
69 /**
70 * Deallocate/free a vertex/pixel buffer object.
71 * Called via glDeleteBuffersARB().
72 */
73 static void
74 st_bufferobj_free(GLcontext *ctx, struct gl_buffer_object *obj)
75 {
76 struct st_buffer_object *st_obj = st_buffer_object(obj);
77
78 assert(obj->RefCount == 0);
79 assert(st_obj->transfer == NULL);
80
81 if (st_obj->buffer)
82 pipe_resource_reference(&st_obj->buffer, NULL);
83
84 free(st_obj);
85 }
86
87
88
89 /**
90 * Replace data in a subrange of buffer object. If the data range
91 * specified by size + offset extends beyond the end of the buffer or
92 * if data is NULL, no copy is performed.
93 * Called via glBufferSubDataARB().
94 */
95 static void
96 st_bufferobj_subdata(GLcontext *ctx,
97 GLenum target,
98 GLintptrARB offset,
99 GLsizeiptrARB size,
100 const GLvoid * data, struct gl_buffer_object *obj)
101 {
102 struct st_buffer_object *st_obj = st_buffer_object(obj);
103
104 /* we may be called from VBO code, so double-check params here */
105 ASSERT(offset >= 0);
106 ASSERT(size >= 0);
107 ASSERT(offset + size <= obj->Size);
108
109 if (!size)
110 return;
111
112 /*
113 * According to ARB_vertex_buffer_object specification, if data is null,
114 * then the contents of the buffer object's data store is undefined. We just
115 * ignore, and leave it unchanged.
116 */
117 if (!data)
118 return;
119
120 /* Now that transfers are per-context, we don't have to figure out
121 * flushing here. Usually drivers won't need to flush in this case
122 * even if the buffer is currently referenced by hardware - they
123 * just queue the upload as dma rather than mapping the underlying
124 * buffer directly.
125 */
126 pipe_buffer_write(st_context(ctx)->pipe,
127 st_obj->buffer,
128 offset, size, data);
129 }
130
131
132 /**
133 * Called via glGetBufferSubDataARB().
134 */
135 static void
136 st_bufferobj_get_subdata(GLcontext *ctx,
137 GLenum target,
138 GLintptrARB offset,
139 GLsizeiptrARB size,
140 GLvoid * data, struct gl_buffer_object *obj)
141 {
142 struct st_buffer_object *st_obj = st_buffer_object(obj);
143
144 /* we may be called from VBO code, so double-check params here */
145 ASSERT(offset >= 0);
146 ASSERT(size >= 0);
147 ASSERT(offset + size <= obj->Size);
148
149 if (!size)
150 return;
151
152 pipe_buffer_read(st_context(ctx)->pipe, st_obj->buffer,
153 offset, size, data);
154 }
155
156
157 /**
158 * Allocate space for and store data in a buffer object. Any data that was
159 * previously stored in the buffer object is lost. If data is NULL,
160 * memory will be allocated, but no copy will occur.
161 * Called via ctx->Driver.BufferData().
162 * \return GL_TRUE for success, GL_FALSE if out of memory
163 */
164 static GLboolean
165 st_bufferobj_data(GLcontext *ctx,
166 GLenum target,
167 GLsizeiptrARB size,
168 const GLvoid * data,
169 GLenum usage,
170 struct gl_buffer_object *obj)
171 {
172 struct st_context *st = st_context(ctx);
173 struct pipe_context *pipe = st->pipe;
174 struct st_buffer_object *st_obj = st_buffer_object(obj);
175 unsigned buffer_usage;
176
177 st_obj->Base.Size = size;
178 st_obj->Base.Usage = usage;
179
180 switch(target) {
181 case GL_PIXEL_PACK_BUFFER_ARB:
182 case GL_PIXEL_UNPACK_BUFFER_ARB:
183 buffer_usage = (PIPE_BIND_RENDER_TARGET |
184 PIPE_BIND_BLIT_SOURCE |
185 PIPE_BIND_BLIT_DESTINATION);
186 break;
187 case GL_ARRAY_BUFFER_ARB:
188 buffer_usage = PIPE_BIND_VERTEX_BUFFER;
189 break;
190 case GL_ELEMENT_ARRAY_BUFFER_ARB:
191 buffer_usage = PIPE_BIND_INDEX_BUFFER;
192 break;
193 default:
194 buffer_usage = 0;
195 }
196
197 pipe_resource_reference( &st_obj->buffer, NULL );
198
199 if (size != 0) {
200 st_obj->buffer = pipe_buffer_create(pipe->screen, buffer_usage, size);
201
202 if (!st_obj->buffer) {
203 return GL_FALSE;
204 }
205
206 if (data)
207 st_no_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer, 0,
208 size, data);
209 return GL_TRUE;
210 }
211
212 return GL_TRUE;
213 }
214
215
216 /**
217 * Called via glMapBufferARB().
218 */
219 static void *
220 st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
221 struct gl_buffer_object *obj)
222 {
223 struct st_buffer_object *st_obj = st_buffer_object(obj);
224 uint flags;
225
226 switch (access) {
227 case GL_WRITE_ONLY:
228 flags = PIPE_TRANSFER_WRITE;
229 break;
230 case GL_READ_ONLY:
231 flags = PIPE_TRANSFER_READ;
232 break;
233 case GL_READ_WRITE:
234 default:
235 flags = PIPE_TRANSFER_READ_WRITE;
236 break;
237 }
238
239 obj->Pointer = pipe_buffer_map(st_context(ctx)->pipe,
240 st_obj->buffer,
241 flags,
242 &st_obj->transfer);
243
244 if (obj->Pointer) {
245 obj->Offset = 0;
246 obj->Length = obj->Size;
247 }
248 return obj->Pointer;
249 }
250
251
252 /**
253 * Dummy data whose's pointer is used for zero length ranges.
254 */
255 static long
256 st_bufferobj_zero_length_range = 0;
257
258
259 /**
260 * Called via glMapBufferRange().
261 */
262 static void *
263 st_bufferobj_map_range(GLcontext *ctx, GLenum target,
264 GLintptr offset, GLsizeiptr length, GLbitfield access,
265 struct gl_buffer_object *obj)
266 {
267 struct pipe_context *pipe = st_context(ctx)->pipe;
268 struct st_buffer_object *st_obj = st_buffer_object(obj);
269 enum pipe_transfer_usage flags = 0x0;
270
271 if (access & GL_MAP_WRITE_BIT)
272 flags |= PIPE_TRANSFER_WRITE;
273
274 if (access & GL_MAP_READ_BIT)
275 flags |= PIPE_TRANSFER_READ;
276
277 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
278 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
279
280 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
281 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
282
283 /* ... other flags ...
284 */
285
286 if (access & MESA_MAP_NOWAIT_BIT)
287 flags |= PIPE_TRANSFER_DONTBLOCK;
288
289 assert(offset >= 0);
290 assert(length >= 0);
291 assert(offset < obj->Size);
292 assert(offset + length <= obj->Size);
293
294 /*
295 * We go out of way here to hide the degenerate yet valid case of zero
296 * length range from the pipe driver.
297 */
298 if (!length) {
299 obj->Pointer = &st_bufferobj_zero_length_range;
300 }
301 else {
302 obj->Pointer = pipe_buffer_map_range(pipe,
303 st_obj->buffer,
304 offset, length,
305 flags,
306 &st_obj->transfer);
307 if (obj->Pointer) {
308 obj->Pointer = (ubyte *) obj->Pointer + offset;
309 }
310 }
311
312 if (obj->Pointer) {
313 obj->Offset = offset;
314 obj->Length = length;
315 obj->AccessFlags = access;
316 }
317
318 return obj->Pointer;
319 }
320
321
322 static void
323 st_bufferobj_flush_mapped_range(GLcontext *ctx, GLenum target,
324 GLintptr offset, GLsizeiptr length,
325 struct gl_buffer_object *obj)
326 {
327 struct pipe_context *pipe = st_context(ctx)->pipe;
328 struct st_buffer_object *st_obj = st_buffer_object(obj);
329
330 /* Subrange is relative to mapped range */
331 assert(offset >= 0);
332 assert(length >= 0);
333 assert(offset + length <= obj->Length);
334 assert(obj->Pointer);
335
336 if (!length)
337 return;
338
339 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer,
340 obj->Offset + offset, length);
341 }
342
343
344 /**
345 * Called via glUnmapBufferARB().
346 */
347 static GLboolean
348 st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
349 {
350 struct pipe_context *pipe = st_context(ctx)->pipe;
351 struct st_buffer_object *st_obj = st_buffer_object(obj);
352
353 if (obj->Length)
354 pipe_buffer_unmap(pipe, st_obj->buffer, st_obj->transfer);
355
356 st_obj->transfer = NULL;
357 obj->Pointer = NULL;
358 obj->Offset = 0;
359 obj->Length = 0;
360 return GL_TRUE;
361 }
362
363
364 /**
365 * Called via glCopyBufferSubData().
366 */
367 static void
368 st_copy_buffer_subdata(GLcontext *ctx,
369 struct gl_buffer_object *src,
370 struct gl_buffer_object *dst,
371 GLintptr readOffset, GLintptr writeOffset,
372 GLsizeiptr size)
373 {
374 struct pipe_context *pipe = st_context(ctx)->pipe;
375 struct st_buffer_object *srcObj = st_buffer_object(src);
376 struct st_buffer_object *dstObj = st_buffer_object(dst);
377 struct pipe_transfer *src_transfer;
378 struct pipe_transfer *dst_transfer;
379 ubyte *srcPtr, *dstPtr;
380
381 if(!size)
382 return;
383
384 /* buffer should not already be mapped */
385 assert(!src->Pointer);
386 assert(!dst->Pointer);
387
388 srcPtr = (ubyte *) pipe_buffer_map_range(pipe,
389 srcObj->buffer,
390 readOffset, size,
391 PIPE_TRANSFER_READ,
392 &src_transfer);
393
394 dstPtr = (ubyte *) pipe_buffer_map_range(pipe,
395 dstObj->buffer,
396 writeOffset, size,
397 PIPE_TRANSFER_WRITE,
398 &dst_transfer);
399
400 if (srcPtr && dstPtr)
401 memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
402
403 pipe_buffer_unmap(pipe, srcObj->buffer, src_transfer);
404 pipe_buffer_unmap(pipe, dstObj->buffer, dst_transfer);
405 }
406
407
408 /* TODO: if buffer wasn't created with appropriate usage flags, need
409 * to recreate it now and copy contents -- or possibly create a
410 * gallium entrypoint to extend the usage flags and let the driver
411 * decide if a copy is necessary.
412 */
413 void
414 st_bufferobj_validate_usage(struct st_context *st,
415 struct st_buffer_object *obj,
416 unsigned usage)
417 {
418 }
419
420
421 void
422 st_init_bufferobject_functions(struct dd_function_table *functions)
423 {
424 functions->NewBufferObject = st_bufferobj_alloc;
425 functions->DeleteBuffer = st_bufferobj_free;
426 functions->BufferData = st_bufferobj_data;
427 functions->BufferSubData = st_bufferobj_subdata;
428 functions->GetBufferSubData = st_bufferobj_get_subdata;
429 functions->MapBuffer = st_bufferobj_map;
430 functions->MapBufferRange = st_bufferobj_map_range;
431 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
432 functions->UnmapBuffer = st_bufferobj_unmap;
433 functions->CopyBufferSubData = st_copy_buffer_subdata;
434
435 /* For GL_APPLE_vertex_array_object */
436 functions->NewArrayObject = _mesa_new_array_object;
437 functions->DeleteArrayObject = _mesa_delete_array_object;
438 }