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 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/mtypes.h"
31 #include "main/arrayobj.h"
32 #include "main/bufferobj.h"
34 #include "st_inlines.h"
35 #include "st_context.h"
36 #include "st_cb_bufferobjects.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_inlines.h"
44 /* Pixel buffers and Vertex/index buffers are handled through these
45 * mesa callbacks. Framebuffer/Renderbuffer objects are
46 * created/managed elsewhere.
52 * There is some duplication between mesa's bufferobjects and our
53 * bufmgr buffers. Both have an integer handle and a hashtable to
54 * lookup an opaque structure. It would be nice if the handles and
55 * internal structure where somehow shared.
57 static struct gl_buffer_object
*
58 st_bufferobj_alloc(GLcontext
*ctx
, GLuint name
, GLenum target
)
60 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
65 _mesa_initialize_buffer_object(&st_obj
->Base
, name
, target
);
73 * Deallocate/free a vertex/pixel buffer object.
74 * Called via glDeleteBuffersARB().
77 st_bufferobj_free(GLcontext
*ctx
, struct gl_buffer_object
*obj
)
79 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
82 pipe_buffer_reference(&st_obj
->buffer
, NULL
);
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().
96 st_bufferobj_subdata(GLcontext
*ctx
,
100 const GLvoid
* data
, struct gl_buffer_object
*obj
)
102 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
104 if (offset
>= st_obj
->size
|| size
> (st_obj
->size
- offset
))
107 st_cond_flush_pipe_buffer_write(st_context(ctx
), st_obj
->buffer
,
113 * Called via glGetBufferSubDataARB().
116 st_bufferobj_get_subdata(GLcontext
*ctx
,
120 GLvoid
* data
, struct gl_buffer_object
*obj
)
122 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
124 if (offset
>= st_obj
->size
|| size
> (st_obj
->size
- offset
))
127 st_cond_flush_pipe_buffer_read(st_context(ctx
), st_obj
->buffer
,
133 * Allocate space for and store data in a buffer object. Any data that was
134 * previously stored in the buffer object is lost. If data is NULL,
135 * memory will be allocated, but no copy will occur.
136 * Called via glBufferDataARB().
139 st_bufferobj_data(GLcontext
*ctx
,
144 struct gl_buffer_object
*obj
)
146 struct st_context
*st
= st_context(ctx
);
147 struct pipe_context
*pipe
= st
->pipe
;
148 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
149 unsigned buffer_usage
;
151 st_obj
->Base
.Size
= size
;
152 st_obj
->Base
.Usage
= usage
;
155 case GL_PIXEL_PACK_BUFFER_ARB
:
156 case GL_PIXEL_UNPACK_BUFFER_ARB
:
157 buffer_usage
= PIPE_BUFFER_USAGE_PIXEL
;
159 case GL_ARRAY_BUFFER_ARB
:
160 buffer_usage
= PIPE_BUFFER_USAGE_VERTEX
;
162 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
163 buffer_usage
= PIPE_BUFFER_USAGE_INDEX
;
169 pipe_buffer_reference( &st_obj
->buffer
, NULL
);
171 st_obj
->buffer
= pipe_buffer_create( pipe
->screen
, 32, buffer_usage
, size
);
173 if (!st_obj
->buffer
) {
174 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferDataARB");
181 st_no_flush_pipe_buffer_write(st_context(ctx
), st_obj
->buffer
, 0,
187 * Called via glMapBufferARB().
190 st_bufferobj_map(GLcontext
*ctx
, GLenum target
, GLenum access
,
191 struct gl_buffer_object
*obj
)
193 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
198 flags
= PIPE_BUFFER_USAGE_CPU_WRITE
;
201 flags
= PIPE_BUFFER_USAGE_CPU_READ
;
206 flags
= PIPE_BUFFER_USAGE_CPU_READ
| PIPE_BUFFER_USAGE_CPU_WRITE
;
210 obj
->Pointer
= st_cond_flush_pipe_buffer_map(st_context(ctx
),
215 obj
->Length
= obj
->Size
;
223 * Called via glMapBufferRange().
226 st_bufferobj_map_range(GLcontext
*ctx
, GLenum target
,
227 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
228 struct gl_buffer_object
*obj
)
230 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
231 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
235 if (access
& GL_MAP_WRITE_BIT
)
236 flags
|= PIPE_BUFFER_USAGE_CPU_WRITE
;
238 if (access
& GL_MAP_READ_BIT
)
239 flags
|= PIPE_BUFFER_USAGE_CPU_READ
;
241 /* ... other flags ...
244 if (access
& MESA_MAP_NOWAIT_BIT
)
245 flags
|= PIPE_BUFFER_USAGE_DONTBLOCK
;
249 assert(offset
< obj
->Size
);
250 assert(offset
+ length
<= obj
->Size
);
252 map
= obj
->Pointer
= pipe_buffer_map_range(pipe
->screen
, st_obj
->buffer
, offset
, length
, flags
);
255 obj
->Length
= obj
->Size
;
264 st_bufferobj_flush_mapped_range(GLcontext
*ctx
, GLenum target
,
265 GLintptr offset
, GLsizeiptr length
,
266 struct gl_buffer_object
*obj
)
268 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
269 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
271 /* Subrange is relative to mapped range */
274 assert(offset
< obj
->Length
);
275 assert(offset
+ length
<= obj
->Length
);
277 pipe_buffer_flush_mapped_range(pipe
->screen
, st_obj
->buffer
,
278 obj
->Offset
+ offset
, length
);
283 * Called via glUnmapBufferARB().
286 st_bufferobj_unmap(GLcontext
*ctx
, GLenum target
, struct gl_buffer_object
*obj
)
288 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
289 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
291 pipe_buffer_unmap(pipe
->screen
, st_obj
->buffer
);
300 st_init_bufferobject_functions(struct dd_function_table
*functions
)
302 functions
->NewBufferObject
= st_bufferobj_alloc
;
303 functions
->DeleteBuffer
= st_bufferobj_free
;
304 functions
->BufferData
= st_bufferobj_data
;
305 functions
->BufferSubData
= st_bufferobj_subdata
;
306 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
307 functions
->MapBuffer
= st_bufferobj_map
;
308 functions
->MapBufferRange
= st_bufferobj_map_range
;
309 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
310 functions
->UnmapBuffer
= st_bufferobj_unmap
;
312 /* For GL_APPLE_vertex_array_object */
313 functions
->NewArrayObject
= _mesa_new_array_object
;
314 functions
->DeleteArrayObject
= _mesa_delete_array_object
;