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_inlines.h"
40 #include "st_context.h"
41 #include "st_cb_bufferobjects.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "pipe/p_inlines.h"
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.
54 static struct gl_buffer_object
*
55 st_bufferobj_alloc(GLcontext
*ctx
, GLuint name
, GLenum target
)
57 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
62 _mesa_initialize_buffer_object(&st_obj
->Base
, name
, target
);
70 * Deallocate/free a vertex/pixel buffer object.
71 * Called via glDeleteBuffersARB().
74 st_bufferobj_free(GLcontext
*ctx
, struct gl_buffer_object
*obj
)
76 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
79 pipe_buffer_reference(&st_obj
->buffer
, NULL
);
87 * Replace data in a subrange of buffer object. If the data range
88 * specified by size + offset extends beyond the end of the buffer or
89 * if data is NULL, no copy is performed.
90 * Called via glBufferSubDataARB().
93 st_bufferobj_subdata(GLcontext
*ctx
,
97 const GLvoid
* data
, struct gl_buffer_object
*obj
)
99 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
101 /* we may be called from VBO code, so double-check params here */
104 ASSERT(offset
+ size
<= obj
->Size
);
106 st_cond_flush_pipe_buffer_write(st_context(ctx
), st_obj
->buffer
,
112 * Called via glGetBufferSubDataARB().
115 st_bufferobj_get_subdata(GLcontext
*ctx
,
119 GLvoid
* data
, struct gl_buffer_object
*obj
)
121 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
123 /* we may be called from VBO code, so double-check params here */
126 ASSERT(offset
+ size
<= obj
->Size
);
128 st_cond_flush_pipe_buffer_read(st_context(ctx
), st_obj
->buffer
,
134 * Allocate space for and store data in a buffer object. Any data that was
135 * previously stored in the buffer object is lost. If data is NULL,
136 * memory will be allocated, but no copy will occur.
137 * Called via ctx->Driver.BufferData().
138 * \return GL_TRUE for success, GL_FALSE if out of memory
141 st_bufferobj_data(GLcontext
*ctx
,
146 struct gl_buffer_object
*obj
)
148 struct st_context
*st
= st_context(ctx
);
149 struct pipe_context
*pipe
= st
->pipe
;
150 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
151 unsigned buffer_usage
;
153 st_obj
->Base
.Size
= size
;
154 st_obj
->Base
.Usage
= usage
;
157 case GL_PIXEL_PACK_BUFFER_ARB
:
158 case GL_PIXEL_UNPACK_BUFFER_ARB
:
159 buffer_usage
= PIPE_BUFFER_USAGE_PIXEL
;
161 case GL_ARRAY_BUFFER_ARB
:
162 buffer_usage
= PIPE_BUFFER_USAGE_VERTEX
;
164 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
165 buffer_usage
= PIPE_BUFFER_USAGE_INDEX
;
171 pipe_buffer_reference( &st_obj
->buffer
, NULL
);
174 st_obj
->buffer
= pipe_buffer_create(pipe
->screen
, 32, buffer_usage
, size
);
176 if (!st_obj
->buffer
) {
181 st_no_flush_pipe_buffer_write(st_context(ctx
), st_obj
->buffer
, 0,
191 * Called via glMapBufferARB().
194 st_bufferobj_map(GLcontext
*ctx
, GLenum target
, GLenum access
,
195 struct gl_buffer_object
*obj
)
197 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
202 flags
= PIPE_BUFFER_USAGE_CPU_WRITE
;
205 flags
= PIPE_BUFFER_USAGE_CPU_READ
;
210 flags
= PIPE_BUFFER_USAGE_CPU_READ
| PIPE_BUFFER_USAGE_CPU_WRITE
;
214 obj
->Pointer
= st_cond_flush_pipe_buffer_map(st_context(ctx
),
219 obj
->Length
= obj
->Size
;
226 * Called via glMapBufferRange().
229 st_bufferobj_map_range(GLcontext
*ctx
, GLenum target
,
230 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
231 struct gl_buffer_object
*obj
)
233 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
234 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
237 if (access
& GL_MAP_WRITE_BIT
)
238 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
240 if (access
& GL_MAP_READ_BIT
)
241 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
243 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
244 flags
|= PIPE_BUFFER_USAGE_FLUSH_EXPLICIT
;
246 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
247 flags
|= PIPE_BUFFER_USAGE_UNSYNCHRONIZED
;
249 /* ... other flags ...
252 if (access
& MESA_MAP_NOWAIT_BIT
)
253 flags
|= PIPE_BUFFER_USAGE_DONTBLOCK
;
257 assert(offset
< obj
->Size
);
258 assert(offset
+ length
<= obj
->Size
);
260 obj
->Pointer
= pipe_buffer_map_range(pipe
->screen
, st_obj
->buffer
, offset
, length
, flags
);
262 obj
->Pointer
= (ubyte
*) obj
->Pointer
+ offset
;
263 obj
->Offset
= offset
;
264 obj
->Length
= length
;
265 obj
->AccessFlags
= access
;
273 st_bufferobj_flush_mapped_range(GLcontext
*ctx
, GLenum target
,
274 GLintptr offset
, GLsizeiptr length
,
275 struct gl_buffer_object
*obj
)
277 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
278 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
280 /* Subrange is relative to mapped range */
283 assert(offset
+ length
<= obj
->Length
);
285 pipe_buffer_flush_mapped_range(pipe
->screen
, st_obj
->buffer
,
286 obj
->Offset
+ offset
, length
);
291 * Called via glUnmapBufferARB().
294 st_bufferobj_unmap(GLcontext
*ctx
, GLenum target
, struct gl_buffer_object
*obj
)
296 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
297 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
299 pipe_buffer_unmap(pipe
->screen
, st_obj
->buffer
);
308 * Called via glCopyBufferSubData().
311 st_copy_buffer_subdata(GLcontext
*ctx
,
312 struct gl_buffer_object
*src
,
313 struct gl_buffer_object
*dst
,
314 GLintptr readOffset
, GLintptr writeOffset
,
317 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
318 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
319 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
320 ubyte
*srcPtr
, *dstPtr
;
322 /* buffer should not already be mapped */
323 assert(!src
->Pointer
);
324 assert(!dst
->Pointer
);
326 srcPtr
= (ubyte
*) pipe_buffer_map_range(pipe
->screen
,
329 PIPE_BUFFER_USAGE_CPU_READ
);
331 dstPtr
= (ubyte
*) pipe_buffer_map_range(pipe
->screen
,
334 PIPE_BUFFER_USAGE_CPU_WRITE
);
336 if (srcPtr
&& dstPtr
)
337 _mesa_memcpy(dstPtr
+ writeOffset
, srcPtr
+ readOffset
, size
);
339 pipe_buffer_unmap(pipe
->screen
, srcObj
->buffer
);
340 pipe_buffer_unmap(pipe
->screen
, dstObj
->buffer
);
345 st_init_bufferobject_functions(struct dd_function_table
*functions
)
347 functions
->NewBufferObject
= st_bufferobj_alloc
;
348 functions
->DeleteBuffer
= st_bufferobj_free
;
349 functions
->BufferData
= st_bufferobj_data
;
350 functions
->BufferSubData
= st_bufferobj_subdata
;
351 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
352 functions
->MapBuffer
= st_bufferobj_map
;
353 functions
->MapBufferRange
= st_bufferobj_map_range
;
354 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
355 functions
->UnmapBuffer
= st_bufferobj_unmap
;
356 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
358 /* For GL_APPLE_vertex_array_object */
359 functions
->NewArrayObject
= _mesa_new_array_object
;
360 functions
->DeleteArrayObject
= _mesa_delete_array_object
;