1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <inttypes.h> /* for PRId64 macro */
36 #include "main/imports.h"
37 #include "main/mtypes.h"
38 #include "main/arrayobj.h"
39 #include "main/bufferobj.h"
41 #include "st_context.h"
42 #include "st_cb_bufferobjects.h"
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "util/u_inlines.h"
51 * There is some duplication between mesa's bufferobjects and our
52 * bufmgr buffers. Both have an integer handle and a hashtable to
53 * lookup an opaque structure. It would be nice if the handles and
54 * internal structure where somehow shared.
56 static struct gl_buffer_object
*
57 st_bufferobj_alloc(struct gl_context
*ctx
, GLuint name
, GLenum target
)
59 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
64 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
, target
);
72 * Deallocate/free a vertex/pixel buffer object.
73 * Called via glDeleteBuffersARB().
76 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
78 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
80 assert(obj
->RefCount
== 0);
81 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
84 pipe_resource_reference(&st_obj
->buffer
, NULL
);
86 free(st_obj
->Base
.Label
);
93 * Replace data in a subrange of buffer object. If the data range
94 * specified by size + offset extends beyond the end of the buffer or
95 * if data is NULL, no copy is performed.
96 * Called via glBufferSubDataARB().
99 st_bufferobj_subdata(struct gl_context
*ctx
,
102 const GLvoid
* data
, struct gl_buffer_object
*obj
)
104 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
106 /* we may be called from VBO code, so double-check params here */
109 ASSERT(offset
+ size
<= obj
->Size
);
115 * According to ARB_vertex_buffer_object specification, if data is null,
116 * then the contents of the buffer object's data store is undefined. We just
117 * ignore, and leave it unchanged.
122 if (!st_obj
->buffer
) {
123 /* we probably ran out of memory during buffer allocation */
127 /* Now that transfers are per-context, we don't have to figure out
128 * flushing here. Usually drivers won't need to flush in this case
129 * even if the buffer is currently referenced by hardware - they
130 * just queue the upload as dma rather than mapping the underlying
133 pipe_buffer_write(st_context(ctx
)->pipe
,
140 * Called via glGetBufferSubDataARB().
143 st_bufferobj_get_subdata(struct gl_context
*ctx
,
146 GLvoid
* data
, struct gl_buffer_object
*obj
)
148 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
150 /* we may be called from VBO code, so double-check params here */
153 ASSERT(offset
+ size
<= obj
->Size
);
158 if (!st_obj
->buffer
) {
159 /* we probably ran out of memory during buffer allocation */
163 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
169 * Allocate space for and store data in a buffer object. Any data that was
170 * previously stored in the buffer object is lost. If data is NULL,
171 * memory will be allocated, but no copy will occur.
172 * Called via ctx->Driver.BufferData().
173 * \return GL_TRUE for success, GL_FALSE if out of memory
176 st_bufferobj_data(struct gl_context
*ctx
,
181 GLbitfield storageFlags
,
182 struct gl_buffer_object
*obj
)
184 struct st_context
*st
= st_context(ctx
);
185 struct pipe_context
*pipe
= st
->pipe
;
186 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
187 unsigned bind
, pipe_usage
, pipe_flags
= 0;
189 if (size
&& data
&& st_obj
->buffer
&&
190 st_obj
->Base
.Size
== size
&&
191 st_obj
->Base
.Usage
== usage
&&
192 st_obj
->Base
.StorageFlags
== storageFlags
) {
193 /* Just discard the old contents and write new data.
194 * This should be the same as creating a new buffer, but we avoid
195 * a lot of validation in Mesa.
199 u_box_1d(0, size
, &box
);
200 pipe
->transfer_inline_write(pipe
, st_obj
->buffer
, 0,
201 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
206 st_obj
->Base
.Size
= size
;
207 st_obj
->Base
.Usage
= usage
;
208 st_obj
->Base
.StorageFlags
= storageFlags
;
211 case GL_PIXEL_PACK_BUFFER_ARB
:
212 case GL_PIXEL_UNPACK_BUFFER_ARB
:
213 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
215 case GL_ARRAY_BUFFER_ARB
:
216 bind
= PIPE_BIND_VERTEX_BUFFER
;
218 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
219 bind
= PIPE_BIND_INDEX_BUFFER
;
221 case GL_TEXTURE_BUFFER
:
222 bind
= PIPE_BIND_SAMPLER_VIEW
;
224 case GL_TRANSFORM_FEEDBACK_BUFFER
:
225 bind
= PIPE_BIND_STREAM_OUTPUT
;
227 case GL_UNIFORM_BUFFER
:
228 bind
= PIPE_BIND_CONSTANT_BUFFER
;
230 case GL_DRAW_INDIRECT_BUFFER
:
231 bind
= PIPE_BIND_COMMAND_ARGS_BUFFER
;
238 if (st_obj
->Base
.Immutable
) {
240 if (storageFlags
& GL_CLIENT_STORAGE_BIT
)
241 pipe_usage
= PIPE_USAGE_STAGING
;
243 pipe_usage
= PIPE_USAGE_DEFAULT
;
252 pipe_usage
= PIPE_USAGE_DEFAULT
;
254 case GL_DYNAMIC_DRAW
:
255 case GL_DYNAMIC_READ
:
256 case GL_DYNAMIC_COPY
:
257 pipe_usage
= PIPE_USAGE_DYNAMIC
;
262 pipe_usage
= PIPE_USAGE_STREAM
;
268 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
269 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
270 if (storageFlags
& GL_MAP_COHERENT_BIT
)
271 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
273 pipe_resource_reference( &st_obj
->buffer
, NULL
);
275 if (ST_DEBUG
& DEBUG_BUFFER
) {
276 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
277 (int64_t) size
, bind
);
281 struct pipe_resource buffer
;
283 memset(&buffer
, 0, sizeof buffer
);
284 buffer
.target
= PIPE_BUFFER
;
285 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
287 buffer
.usage
= pipe_usage
;
288 buffer
.flags
= pipe_flags
;
289 buffer
.width0
= size
;
292 buffer
.array_size
= 1;
294 st_obj
->buffer
= pipe
->screen
->resource_create(pipe
->screen
, &buffer
);
296 if (!st_obj
->buffer
) {
298 st_obj
->Base
.Size
= 0;
303 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
306 /* BufferData may change an array or uniform buffer, need to update it */
307 st
->dirty
.st
|= ST_NEW_VERTEX_ARRAYS
| ST_NEW_UNIFORM_BUFFER
;
314 * Called via glMapBufferRange().
317 st_bufferobj_map_range(struct gl_context
*ctx
,
318 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
319 struct gl_buffer_object
*obj
,
320 gl_map_buffer_index index
)
322 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
323 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
324 enum pipe_transfer_usage flags
= 0x0;
326 if (access
& GL_MAP_WRITE_BIT
)
327 flags
|= PIPE_TRANSFER_WRITE
;
329 if (access
& GL_MAP_READ_BIT
)
330 flags
|= PIPE_TRANSFER_READ
;
332 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
333 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
335 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
336 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
338 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
339 if (offset
== 0 && length
== obj
->Size
)
340 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
342 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
345 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
346 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
348 if (access
& GL_MAP_PERSISTENT_BIT
)
349 flags
|= PIPE_TRANSFER_PERSISTENT
;
351 if (access
& GL_MAP_COHERENT_BIT
)
352 flags
|= PIPE_TRANSFER_COHERENT
;
354 /* ... other flags ...
357 if (access
& MESA_MAP_NOWAIT_BIT
)
358 flags
|= PIPE_TRANSFER_DONTBLOCK
;
362 assert(offset
< obj
->Size
);
363 assert(offset
+ length
<= obj
->Size
);
365 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
369 &st_obj
->transfer
[index
]);
370 if (obj
->Mappings
[index
].Pointer
) {
371 obj
->Mappings
[index
].Offset
= offset
;
372 obj
->Mappings
[index
].Length
= length
;
373 obj
->Mappings
[index
].AccessFlags
= access
;
376 st_obj
->transfer
[index
] = NULL
;
379 return obj
->Mappings
[index
].Pointer
;
384 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
385 GLintptr offset
, GLsizeiptr length
,
386 struct gl_buffer_object
*obj
,
387 gl_map_buffer_index index
)
389 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
390 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
392 /* Subrange is relative to mapped range */
395 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
396 assert(obj
->Mappings
[index
].Pointer
);
401 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
402 obj
->Mappings
[index
].Offset
+ offset
,
408 * Called via glUnmapBufferARB().
411 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
412 gl_map_buffer_index index
)
414 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
415 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
417 if (obj
->Mappings
[index
].Length
)
418 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
420 st_obj
->transfer
[index
] = NULL
;
421 obj
->Mappings
[index
].Pointer
= NULL
;
422 obj
->Mappings
[index
].Offset
= 0;
423 obj
->Mappings
[index
].Length
= 0;
429 * Called via glCopyBufferSubData().
432 st_copy_buffer_subdata(struct gl_context
*ctx
,
433 struct gl_buffer_object
*src
,
434 struct gl_buffer_object
*dst
,
435 GLintptr readOffset
, GLintptr writeOffset
,
438 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
439 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
440 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
446 /* buffer should not already be mapped */
447 assert(!_mesa_check_disallowed_mapping(src
));
448 assert(!_mesa_check_disallowed_mapping(dst
));
450 u_box_1d(readOffset
, size
, &box
);
452 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
453 srcObj
->buffer
, 0, &box
);
457 * Called via glClearBufferSubData().
460 st_clear_buffer_subdata(struct gl_context
*ctx
,
461 GLintptr offset
, GLsizeiptr size
,
462 const GLvoid
*clearValue
,
463 GLsizeiptr clearValueSize
,
464 struct gl_buffer_object
*bufObj
)
466 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
467 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
468 static const char zeros
[16] = {0};
470 if (!pipe
->clear_buffer
) {
471 _mesa_buffer_clear_subdata(ctx
, offset
, size
,
472 clearValue
, clearValueSize
, bufObj
);
479 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
480 clearValue
, clearValueSize
);
484 /* TODO: if buffer wasn't created with appropriate usage flags, need
485 * to recreate it now and copy contents -- or possibly create a
486 * gallium entrypoint to extend the usage flags and let the driver
487 * decide if a copy is necessary.
490 st_bufferobj_validate_usage(struct st_context
*st
,
491 struct st_buffer_object
*obj
,
498 st_init_bufferobject_functions(struct dd_function_table
*functions
)
500 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
501 _mesa_init_buffer_object_functions(functions
);
503 functions
->NewBufferObject
= st_bufferobj_alloc
;
504 functions
->DeleteBuffer
= st_bufferobj_free
;
505 functions
->BufferData
= st_bufferobj_data
;
506 functions
->BufferSubData
= st_bufferobj_subdata
;
507 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
508 functions
->MapBufferRange
= st_bufferobj_map_range
;
509 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
510 functions
->UnmapBuffer
= st_bufferobj_unmap
;
511 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
512 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
514 /* For GL_APPLE_vertex_array_object */
515 functions
->NewArrayObject
= _mesa_new_vao
;
516 functions
->DeleteArrayObject
= _mesa_delete_vao
;