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 * Functions for pixel buffer objects and vertex/element buffer objects.
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/arrayobj.h"
37 #include "main/bufferobj.h"
39 #include "st_context.h"
40 #include "st_cb_bufferobjects.h"
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "util/u_inlines.h"
48 * There is some duplication between mesa's bufferobjects and our
49 * bufmgr buffers. Both have an integer handle and a hashtable to
50 * lookup an opaque structure. It would be nice if the handles and
51 * internal structure where somehow shared.
53 static struct gl_buffer_object
*
54 st_bufferobj_alloc(struct gl_context
*ctx
, GLuint name
, GLenum target
)
56 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
61 _mesa_initialize_buffer_object(&st_obj
->Base
, name
, target
);
69 * Deallocate/free a vertex/pixel buffer object.
70 * Called via glDeleteBuffersARB().
73 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
75 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
77 assert(obj
->RefCount
== 0);
78 assert(st_obj
->transfer
== NULL
);
81 pipe_resource_reference(&st_obj
->buffer
, NULL
);
89 * Replace data in a subrange of buffer object. If the data range
90 * specified by size + offset extends beyond the end of the buffer or
91 * if data is NULL, no copy is performed.
92 * Called via glBufferSubDataARB().
95 st_bufferobj_subdata(struct gl_context
*ctx
,
99 const GLvoid
* data
, struct gl_buffer_object
*obj
)
101 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
103 /* we may be called from VBO code, so double-check params here */
106 ASSERT(offset
+ size
<= obj
->Size
);
112 * According to ARB_vertex_buffer_object specification, if data is null,
113 * then the contents of the buffer object's data store is undefined. We just
114 * ignore, and leave it unchanged.
119 /* Now that transfers are per-context, we don't have to figure out
120 * flushing here. Usually drivers won't need to flush in this case
121 * even if the buffer is currently referenced by hardware - they
122 * just queue the upload as dma rather than mapping the underlying
125 pipe_buffer_write(st_context(ctx
)->pipe
,
132 * Called via glGetBufferSubDataARB().
135 st_bufferobj_get_subdata(struct gl_context
*ctx
,
139 GLvoid
* data
, struct gl_buffer_object
*obj
)
141 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
143 /* we may be called from VBO code, so double-check params here */
146 ASSERT(offset
+ size
<= obj
->Size
);
151 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
157 * Allocate space for and store data in a buffer object. Any data that was
158 * previously stored in the buffer object is lost. If data is NULL,
159 * memory will be allocated, but no copy will occur.
160 * Called via ctx->Driver.BufferData().
161 * \return GL_TRUE for success, GL_FALSE if out of memory
164 st_bufferobj_data(struct gl_context
*ctx
,
169 struct gl_buffer_object
*obj
)
171 struct st_context
*st
= st_context(ctx
);
172 struct pipe_context
*pipe
= st
->pipe
;
173 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
174 unsigned bind
, pipe_usage
;
176 st_obj
->Base
.Size
= size
;
177 st_obj
->Base
.Usage
= usage
;
180 case GL_PIXEL_PACK_BUFFER_ARB
:
181 case GL_PIXEL_UNPACK_BUFFER_ARB
:
182 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
184 case GL_ARRAY_BUFFER_ARB
:
185 bind
= PIPE_BIND_VERTEX_BUFFER
;
187 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
188 bind
= PIPE_BIND_INDEX_BUFFER
;
198 pipe_usage
= PIPE_USAGE_STATIC
;
200 case GL_DYNAMIC_DRAW
:
201 case GL_DYNAMIC_READ
:
202 case GL_DYNAMIC_COPY
:
203 pipe_usage
= PIPE_USAGE_DYNAMIC
;
208 pipe_usage
= PIPE_USAGE_STREAM
;
211 pipe_usage
= PIPE_USAGE_DEFAULT
;
214 pipe_resource_reference( &st_obj
->buffer
, NULL
);
217 st_obj
->buffer
= pipe_buffer_create(pipe
->screen
, bind
,
220 if (!st_obj
->buffer
) {
225 pipe_buffer_write(st_context(ctx
)->pipe
, st_obj
->buffer
, 0,
235 * Dummy data whose's pointer is used for zero size buffers or ranges.
237 static long st_bufferobj_zero_length
= 0;
242 * Called via glMapBufferARB().
245 st_bufferobj_map(struct gl_context
*ctx
, GLenum target
, GLenum access
,
246 struct gl_buffer_object
*obj
)
248 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
253 flags
= PIPE_TRANSFER_WRITE
;
256 flags
= PIPE_TRANSFER_READ
;
260 flags
= PIPE_TRANSFER_READ_WRITE
;
264 /* Handle zero-size buffers here rather than in drivers */
265 if (obj
->Size
== 0) {
266 obj
->Pointer
= &st_bufferobj_zero_length
;
269 obj
->Pointer
= pipe_buffer_map(st_context(ctx
)->pipe
,
277 obj
->Length
= obj
->Size
;
284 * Called via glMapBufferRange().
287 st_bufferobj_map_range(struct gl_context
*ctx
, GLenum target
,
288 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
289 struct gl_buffer_object
*obj
)
291 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
292 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
293 enum pipe_transfer_usage flags
= 0x0;
295 if (access
& GL_MAP_WRITE_BIT
)
296 flags
|= PIPE_TRANSFER_WRITE
;
298 if (access
& GL_MAP_READ_BIT
)
299 flags
|= PIPE_TRANSFER_READ
;
301 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
302 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
304 if (access
& GL_MAP_INVALIDATE_RANGE_BIT
)
305 flags
|= PIPE_TRANSFER_DISCARD
;
307 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
)
308 flags
|= PIPE_TRANSFER_DISCARD
;
310 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
311 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
313 /* ... other flags ...
316 if (access
& MESA_MAP_NOWAIT_BIT
)
317 flags
|= PIPE_TRANSFER_DONTBLOCK
;
321 assert(offset
< obj
->Size
);
322 assert(offset
+ length
<= obj
->Size
);
325 * We go out of way here to hide the degenerate yet valid case of zero
326 * length range from the pipe driver.
329 obj
->Pointer
= &st_bufferobj_zero_length
;
332 obj
->Pointer
= pipe_buffer_map_range(pipe
,
338 obj
->Pointer
= (ubyte
*) obj
->Pointer
+ offset
;
343 obj
->Offset
= offset
;
344 obj
->Length
= length
;
345 obj
->AccessFlags
= access
;
353 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
, GLenum target
,
354 GLintptr offset
, GLsizeiptr length
,
355 struct gl_buffer_object
*obj
)
357 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
358 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
360 /* Subrange is relative to mapped range */
363 assert(offset
+ length
<= obj
->Length
);
364 assert(obj
->Pointer
);
369 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
,
370 obj
->Offset
+ offset
, length
);
375 * Called via glUnmapBufferARB().
378 st_bufferobj_unmap(struct gl_context
*ctx
, GLenum target
, struct gl_buffer_object
*obj
)
380 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
381 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
384 pipe_buffer_unmap(pipe
, st_obj
->transfer
);
386 st_obj
->transfer
= NULL
;
395 * Called via glCopyBufferSubData().
398 st_copy_buffer_subdata(struct gl_context
*ctx
,
399 struct gl_buffer_object
*src
,
400 struct gl_buffer_object
*dst
,
401 GLintptr readOffset
, GLintptr writeOffset
,
404 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
405 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
406 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
407 struct pipe_transfer
*src_transfer
;
408 struct pipe_transfer
*dst_transfer
;
409 ubyte
*srcPtr
, *dstPtr
;
414 /* buffer should not already be mapped */
415 assert(!src
->Pointer
);
416 assert(!dst
->Pointer
);
418 srcPtr
= (ubyte
*) pipe_buffer_map_range(pipe
,
424 dstPtr
= (ubyte
*) pipe_buffer_map_range(pipe
,
430 if (srcPtr
&& dstPtr
)
431 memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
433 pipe_buffer_unmap(pipe
, src_transfer
);
434 pipe_buffer_unmap(pipe
, dst_transfer
);
438 /* TODO: if buffer wasn't created with appropriate usage flags, need
439 * to recreate it now and copy contents -- or possibly create a
440 * gallium entrypoint to extend the usage flags and let the driver
441 * decide if a copy is necessary.
444 st_bufferobj_validate_usage(struct st_context
*st
,
445 struct st_buffer_object
*obj
,
452 st_init_bufferobject_functions(struct dd_function_table
*functions
)
454 functions
->NewBufferObject
= st_bufferobj_alloc
;
455 functions
->DeleteBuffer
= st_bufferobj_free
;
456 functions
->BufferData
= st_bufferobj_data
;
457 functions
->BufferSubData
= st_bufferobj_subdata
;
458 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
459 functions
->MapBuffer
= st_bufferobj_map
;
460 functions
->MapBufferRange
= st_bufferobj_map_range
;
461 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
462 functions
->UnmapBuffer
= st_bufferobj_unmap
;
463 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
465 /* For GL_APPLE_vertex_array_object */
466 functions
->NewArrayObject
= _mesa_new_array_object
;
467 functions
->DeleteArrayObject
= _mesa_delete_array_object
;