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"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_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(struct gl_context
*ctx
, GLuint name
, GLenum target
)
57 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
62 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
, target
);
70 * Deallocate/free a vertex/pixel buffer object.
71 * Called via glDeleteBuffersARB().
74 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
76 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
78 assert(obj
->RefCount
== 0);
79 assert(st_obj
->transfer
== NULL
);
82 pipe_resource_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(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 if (!st_obj
->buffer
) {
120 /* we probably ran out of memory during buffer allocation */
124 /* Now that transfers are per-context, we don't have to figure out
125 * flushing here. Usually drivers won't need to flush in this case
126 * even if the buffer is currently referenced by hardware - they
127 * just queue the upload as dma rather than mapping the underlying
130 pipe_buffer_write(st_context(ctx
)->pipe
,
137 * Called via glGetBufferSubDataARB().
140 st_bufferobj_get_subdata(struct gl_context
*ctx
,
143 GLvoid
* data
, struct gl_buffer_object
*obj
)
145 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
147 /* we may be called from VBO code, so double-check params here */
150 ASSERT(offset
+ size
<= obj
->Size
);
155 if (!st_obj
->buffer
) {
156 /* we probably ran out of memory during buffer allocation */
160 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
166 * Allocate space for and store data in a buffer object. Any data that was
167 * previously stored in the buffer object is lost. If data is NULL,
168 * memory will be allocated, but no copy will occur.
169 * Called via ctx->Driver.BufferData().
170 * \return GL_TRUE for success, GL_FALSE if out of memory
173 st_bufferobj_data(struct gl_context
*ctx
,
178 struct gl_buffer_object
*obj
)
180 struct st_context
*st
= st_context(ctx
);
181 struct pipe_context
*pipe
= st
->pipe
;
182 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
183 unsigned bind
, pipe_usage
;
185 if (st_obj
->Base
.Size
== size
&& st_obj
->Base
.Usage
== usage
&& data
) {
186 /* Just discard the old contents and write new data.
187 * This should be the same as creating a new buffer, but we avoid
188 * a lot of validation in Mesa.
192 u_box_1d(0, size
, &box
);
193 pipe
->transfer_inline_write(pipe
, st_obj
->buffer
, 0,
194 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
199 st_obj
->Base
.Size
= size
;
200 st_obj
->Base
.Usage
= usage
;
203 case GL_PIXEL_PACK_BUFFER_ARB
:
204 case GL_PIXEL_UNPACK_BUFFER_ARB
:
205 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
207 case GL_ARRAY_BUFFER_ARB
:
208 bind
= PIPE_BIND_VERTEX_BUFFER
;
210 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
211 bind
= PIPE_BIND_INDEX_BUFFER
;
213 case GL_TEXTURE_BUFFER
:
214 bind
= PIPE_BIND_SAMPLER_VIEW
;
216 case GL_TRANSFORM_FEEDBACK_BUFFER
:
217 bind
= PIPE_BIND_STREAM_OUTPUT
;
219 case GL_UNIFORM_BUFFER
:
220 bind
= PIPE_BIND_CONSTANT_BUFFER
;
230 pipe_usage
= PIPE_USAGE_STATIC
;
232 case GL_DYNAMIC_DRAW
:
233 case GL_DYNAMIC_READ
:
234 case GL_DYNAMIC_COPY
:
235 pipe_usage
= PIPE_USAGE_DYNAMIC
;
240 pipe_usage
= PIPE_USAGE_STREAM
;
243 pipe_usage
= PIPE_USAGE_DEFAULT
;
246 pipe_resource_reference( &st_obj
->buffer
, NULL
);
248 if (ST_DEBUG
& DEBUG_BUFFER
) {
249 debug_printf("Create buffer size %td bind 0x%x\n", size
, bind
);
253 st_obj
->buffer
= pipe_buffer_create(pipe
->screen
, bind
,
256 if (!st_obj
->buffer
) {
258 st_obj
->Base
.Size
= 0;
263 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
272 * Called via glMapBufferRange().
275 st_bufferobj_map_range(struct gl_context
*ctx
,
276 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
277 struct gl_buffer_object
*obj
)
279 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
280 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
281 enum pipe_transfer_usage flags
= 0x0;
283 if (access
& GL_MAP_WRITE_BIT
)
284 flags
|= PIPE_TRANSFER_WRITE
;
286 if (access
& GL_MAP_READ_BIT
)
287 flags
|= PIPE_TRANSFER_READ
;
289 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
290 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
292 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
293 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
295 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
296 if (offset
== 0 && length
== obj
->Size
)
297 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
299 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
302 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
303 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
305 /* ... other flags ...
308 if (access
& MESA_MAP_NOWAIT_BIT
)
309 flags
|= PIPE_TRANSFER_DONTBLOCK
;
313 assert(offset
< obj
->Size
);
314 assert(offset
+ length
<= obj
->Size
);
316 obj
->Pointer
= pipe_buffer_map_range(pipe
,
322 obj
->Offset
= offset
;
323 obj
->Length
= length
;
324 obj
->AccessFlags
= access
;
327 st_obj
->transfer
= NULL
;
335 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
336 GLintptr offset
, GLsizeiptr length
,
337 struct gl_buffer_object
*obj
)
339 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
340 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
342 /* Subrange is relative to mapped range */
345 assert(offset
+ length
<= obj
->Length
);
346 assert(obj
->Pointer
);
351 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
,
352 obj
->Offset
+ offset
, length
);
357 * Called via glUnmapBufferARB().
360 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
362 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
363 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
366 pipe_buffer_unmap(pipe
, st_obj
->transfer
);
368 st_obj
->transfer
= NULL
;
377 * Called via glCopyBufferSubData().
380 st_copy_buffer_subdata(struct gl_context
*ctx
,
381 struct gl_buffer_object
*src
,
382 struct gl_buffer_object
*dst
,
383 GLintptr readOffset
, GLintptr writeOffset
,
386 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
387 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
388 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
394 /* buffer should not already be mapped */
395 assert(!src
->Pointer
);
396 assert(!dst
->Pointer
);
398 u_box_1d(readOffset
, size
, &box
);
400 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
401 srcObj
->buffer
, 0, &box
);
405 /* TODO: if buffer wasn't created with appropriate usage flags, need
406 * to recreate it now and copy contents -- or possibly create a
407 * gallium entrypoint to extend the usage flags and let the driver
408 * decide if a copy is necessary.
411 st_bufferobj_validate_usage(struct st_context
*st
,
412 struct st_buffer_object
*obj
,
419 st_init_bufferobject_functions(struct dd_function_table
*functions
)
421 functions
->NewBufferObject
= st_bufferobj_alloc
;
422 functions
->DeleteBuffer
= st_bufferobj_free
;
423 functions
->BufferData
= st_bufferobj_data
;
424 functions
->BufferSubData
= st_bufferobj_subdata
;
425 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
426 functions
->MapBufferRange
= st_bufferobj_map_range
;
427 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
428 functions
->UnmapBuffer
= st_bufferobj_unmap
;
429 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
431 /* For GL_APPLE_vertex_array_object */
432 functions
->NewArrayObject
= _mesa_new_array_object
;
433 functions
->DeleteArrayObject
= _mesa_delete_array_object
;