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/errors.h"
37 #include "main/imports.h"
38 #include "main/mtypes.h"
39 #include "main/arrayobj.h"
40 #include "main/bufferobj.h"
42 #include "st_context.h"
43 #include "st_cb_bufferobjects.h"
44 #include "st_cb_memoryobjects.h"
48 #include "pipe/p_context.h"
49 #include "pipe/p_defines.h"
50 #include "util/u_inlines.h"
54 * There is some duplication between mesa's bufferobjects and our
55 * bufmgr buffers. Both have an integer handle and a hashtable to
56 * lookup an opaque structure. It would be nice if the handles and
57 * internal structure where somehow shared.
59 static struct gl_buffer_object
*
60 st_bufferobj_alloc(struct gl_context
*ctx
, GLuint name
)
62 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
67 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
);
75 * Deallocate/free a vertex/pixel buffer object.
76 * Called via glDeleteBuffersARB().
79 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
81 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
83 assert(obj
->RefCount
== 0);
84 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
87 pipe_resource_reference(&st_obj
->buffer
, NULL
);
89 _mesa_delete_buffer_object(ctx
, obj
);
95 * Replace data in a subrange of buffer object. If the data range
96 * specified by size + offset extends beyond the end of the buffer or
97 * if data is NULL, no copy is performed.
98 * Called via glBufferSubDataARB().
101 st_bufferobj_subdata(struct gl_context
*ctx
,
104 const void * data
, struct gl_buffer_object
*obj
)
106 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
108 /* we may be called from VBO code, so double-check params here */
111 assert(offset
+ size
<= obj
->Size
);
117 * According to ARB_vertex_buffer_object specification, if data is null,
118 * then the contents of the buffer object's data store is undefined. We just
119 * ignore, and leave it unchanged.
124 if (!st_obj
->buffer
) {
125 /* we probably ran out of memory during buffer allocation */
129 /* Now that transfers are per-context, we don't have to figure out
130 * flushing here. Usually drivers won't need to flush in this case
131 * even if the buffer is currently referenced by hardware - they
132 * just queue the upload as dma rather than mapping the underlying
135 pipe_buffer_write(st_context(ctx
)->pipe
,
142 * Called via glGetBufferSubDataARB().
145 st_bufferobj_get_subdata(struct gl_context
*ctx
,
148 void * data
, struct gl_buffer_object
*obj
)
150 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
152 /* we may be called from VBO code, so double-check params here */
155 assert(offset
+ size
<= obj
->Size
);
160 if (!st_obj
->buffer
) {
161 /* we probably ran out of memory during buffer allocation */
165 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
171 * Return bitmask of PIPE_BIND_x flags corresponding a GL buffer target.
174 buffer_target_to_bind_flags(GLenum target
)
177 case GL_PIXEL_PACK_BUFFER_ARB
:
178 case GL_PIXEL_UNPACK_BUFFER_ARB
:
179 return PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
180 case GL_ARRAY_BUFFER_ARB
:
181 return PIPE_BIND_VERTEX_BUFFER
;
182 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
183 return PIPE_BIND_INDEX_BUFFER
;
184 case GL_TEXTURE_BUFFER
:
185 return PIPE_BIND_SAMPLER_VIEW
;
186 case GL_TRANSFORM_FEEDBACK_BUFFER
:
187 return PIPE_BIND_STREAM_OUTPUT
;
188 case GL_UNIFORM_BUFFER
:
189 return PIPE_BIND_CONSTANT_BUFFER
;
190 case GL_DRAW_INDIRECT_BUFFER
:
191 case GL_PARAMETER_BUFFER_ARB
:
192 return PIPE_BIND_COMMAND_ARGS_BUFFER
;
193 case GL_ATOMIC_COUNTER_BUFFER
:
194 case GL_SHADER_STORAGE_BUFFER
:
195 return PIPE_BIND_SHADER_BUFFER
;
196 case GL_QUERY_BUFFER
:
197 return PIPE_BIND_QUERY_BUFFER
;
205 * Return bitmask of PIPE_RESOURCE_x flags corresponding to GL_MAP_x flags.
208 storage_flags_to_buffer_flags(GLbitfield storageFlags
)
211 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
212 flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
213 if (storageFlags
& GL_MAP_COHERENT_BIT
)
214 flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
215 if (storageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)
216 flags
|= PIPE_RESOURCE_FLAG_SPARSE
;
222 * From a buffer object's target, immutability flag, storage flags and
223 * usage hint, return a pipe_resource_usage value (PIPE_USAGE_DYNAMIC,
226 static enum pipe_resource_usage
227 buffer_usage(GLenum target
, GLboolean immutable
,
228 GLbitfield storageFlags
, GLenum usage
)
232 if (storageFlags
& GL_CLIENT_STORAGE_BIT
) {
233 if (storageFlags
& GL_MAP_READ_BIT
)
234 return PIPE_USAGE_STAGING
;
236 return PIPE_USAGE_STREAM
;
238 return PIPE_USAGE_DEFAULT
;
244 case GL_DYNAMIC_DRAW
:
245 case GL_DYNAMIC_COPY
:
246 return PIPE_USAGE_DYNAMIC
;
249 /* XXX: Remove this test and fall-through when we have PBO unpacking
250 * acceleration. Right now, PBO unpacking is done by the CPU, so we
251 * have to make sure CPU reads are fast.
253 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
254 return PIPE_USAGE_STREAM
;
258 case GL_DYNAMIC_READ
:
260 return PIPE_USAGE_STAGING
;
264 return PIPE_USAGE_DEFAULT
;
270 static ALWAYS_INLINE GLboolean
271 bufferobj_data(struct gl_context
*ctx
,
275 struct gl_memory_object
*memObj
,
278 GLbitfield storageFlags
,
279 struct gl_buffer_object
*obj
)
281 struct st_context
*st
= st_context(ctx
);
282 struct pipe_context
*pipe
= st
->pipe
;
283 struct pipe_screen
*screen
= pipe
->screen
;
284 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
285 struct st_memory_object
*st_mem_obj
= st_memory_object(memObj
);
287 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
288 size
&& st_obj
->buffer
&&
289 st_obj
->Base
.Size
== size
&&
290 st_obj
->Base
.Usage
== usage
&&
291 st_obj
->Base
.StorageFlags
== storageFlags
) {
293 /* Just discard the old contents and write new data.
294 * This should be the same as creating a new buffer, but we avoid
295 * a lot of validation in Mesa.
297 pipe
->buffer_subdata(pipe
, st_obj
->buffer
,
298 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
301 } else if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
)) {
302 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
307 st_obj
->Base
.Size
= size
;
308 st_obj
->Base
.Usage
= usage
;
309 st_obj
->Base
.StorageFlags
= storageFlags
;
311 pipe_resource_reference( &st_obj
->buffer
, NULL
);
313 const unsigned bindings
= buffer_target_to_bind_flags(target
);
315 if (ST_DEBUG
& DEBUG_BUFFER
) {
316 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
317 (int64_t) size
, bindings
);
321 struct pipe_resource buffer
;
323 memset(&buffer
, 0, sizeof buffer
);
324 buffer
.target
= PIPE_BUFFER
;
325 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
326 buffer
.bind
= bindings
;
328 buffer_usage(target
, st_obj
->Base
.Immutable
, storageFlags
, usage
);
329 buffer
.flags
= storage_flags_to_buffer_flags(storageFlags
);
330 buffer
.width0
= size
;
333 buffer
.array_size
= 1;
336 st_obj
->buffer
= screen
->resource_from_memobj(screen
, &buffer
,
340 else if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
342 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
345 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
347 if (st_obj
->buffer
&& data
)
348 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
351 if (!st_obj
->buffer
) {
353 st_obj
->Base
.Size
= 0;
358 /* The current buffer may be bound, so we have to revalidate all atoms that
361 if (st_obj
->Base
.UsageHistory
& USAGE_ARRAY_BUFFER
)
362 ctx
->NewDriverState
|= ST_NEW_VERTEX_ARRAYS
;
363 /* if (st_obj->Base.UsageHistory & USAGE_ELEMENT_ARRAY_BUFFER) */
364 /* ctx->NewDriverState |= TODO: Handle indices as gallium state; */
365 if (st_obj
->Base
.UsageHistory
& USAGE_UNIFORM_BUFFER
)
366 ctx
->NewDriverState
|= ST_NEW_UNIFORM_BUFFER
;
367 if (st_obj
->Base
.UsageHistory
& USAGE_SHADER_STORAGE_BUFFER
)
368 ctx
->NewDriverState
|= ST_NEW_STORAGE_BUFFER
;
369 if (st_obj
->Base
.UsageHistory
& USAGE_TEXTURE_BUFFER
)
370 ctx
->NewDriverState
|= ST_NEW_SAMPLER_VIEWS
| ST_NEW_IMAGE_UNITS
;
371 if (st_obj
->Base
.UsageHistory
& USAGE_ATOMIC_COUNTER_BUFFER
)
372 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
378 * Allocate space for and store data in a buffer object. Any data that was
379 * previously stored in the buffer object is lost. If data is NULL,
380 * memory will be allocated, but no copy will occur.
381 * Called via ctx->Driver.BufferData().
382 * \return GL_TRUE for success, GL_FALSE if out of memory
385 st_bufferobj_data(struct gl_context
*ctx
,
390 GLbitfield storageFlags
,
391 struct gl_buffer_object
*obj
)
393 return bufferobj_data(ctx
, target
, size
, data
, NULL
, 0, usage
, storageFlags
, obj
);
397 st_bufferobj_data_mem(struct gl_context
*ctx
,
400 struct gl_memory_object
*memObj
,
403 struct gl_buffer_object
*bufObj
)
405 return bufferobj_data(ctx
, target
, size
, NULL
, memObj
, offset
, usage
, 0, bufObj
);
409 * Called via glInvalidateBuffer(Sub)Data.
412 st_bufferobj_invalidate(struct gl_context
*ctx
,
413 struct gl_buffer_object
*obj
,
417 struct st_context
*st
= st_context(ctx
);
418 struct pipe_context
*pipe
= st
->pipe
;
419 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
421 /* We ignore partial invalidates. */
422 if (offset
!= 0 || size
!= obj
->Size
)
425 /* Nothing to invalidate. */
429 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
434 * Convert GLbitfield of GL_MAP_x flags to gallium pipe_transfer_usage flags.
435 * \param wholeBuffer is the whole buffer being mapped?
437 enum pipe_transfer_usage
438 st_access_flags_to_transfer_flags(GLbitfield access
, bool wholeBuffer
)
440 enum pipe_transfer_usage flags
= 0;
442 if (access
& GL_MAP_WRITE_BIT
)
443 flags
|= PIPE_TRANSFER_WRITE
;
445 if (access
& GL_MAP_READ_BIT
)
446 flags
|= PIPE_TRANSFER_READ
;
448 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
449 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
451 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
452 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
454 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
456 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
458 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
461 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
462 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
464 if (access
& GL_MAP_PERSISTENT_BIT
)
465 flags
|= PIPE_TRANSFER_PERSISTENT
;
467 if (access
& GL_MAP_COHERENT_BIT
)
468 flags
|= PIPE_TRANSFER_COHERENT
;
470 /* ... other flags ...
473 if (access
& MESA_MAP_NOWAIT_BIT
)
474 flags
|= PIPE_TRANSFER_DONTBLOCK
;
481 * Called via glMapBufferRange().
484 st_bufferobj_map_range(struct gl_context
*ctx
,
485 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
486 struct gl_buffer_object
*obj
,
487 gl_map_buffer_index index
)
489 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
490 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
494 assert(offset
< obj
->Size
);
495 assert(offset
+ length
<= obj
->Size
);
497 const enum pipe_transfer_usage transfer_flags
=
498 st_access_flags_to_transfer_flags(access
,
499 offset
== 0 && length
== obj
->Size
);
501 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
505 &st_obj
->transfer
[index
]);
506 if (obj
->Mappings
[index
].Pointer
) {
507 obj
->Mappings
[index
].Offset
= offset
;
508 obj
->Mappings
[index
].Length
= length
;
509 obj
->Mappings
[index
].AccessFlags
= access
;
512 st_obj
->transfer
[index
] = NULL
;
515 return obj
->Mappings
[index
].Pointer
;
520 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
521 GLintptr offset
, GLsizeiptr length
,
522 struct gl_buffer_object
*obj
,
523 gl_map_buffer_index index
)
525 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
526 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
528 /* Subrange is relative to mapped range */
531 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
532 assert(obj
->Mappings
[index
].Pointer
);
537 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
538 obj
->Mappings
[index
].Offset
+ offset
,
544 * Called via glUnmapBufferARB().
547 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
548 gl_map_buffer_index index
)
550 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
551 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
553 if (obj
->Mappings
[index
].Length
)
554 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
556 st_obj
->transfer
[index
] = NULL
;
557 obj
->Mappings
[index
].Pointer
= NULL
;
558 obj
->Mappings
[index
].Offset
= 0;
559 obj
->Mappings
[index
].Length
= 0;
565 * Called via glCopyBufferSubData().
568 st_copy_buffer_subdata(struct gl_context
*ctx
,
569 struct gl_buffer_object
*src
,
570 struct gl_buffer_object
*dst
,
571 GLintptr readOffset
, GLintptr writeOffset
,
574 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
575 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
576 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
582 /* buffer should not already be mapped */
583 assert(!_mesa_check_disallowed_mapping(src
));
584 assert(!_mesa_check_disallowed_mapping(dst
));
586 u_box_1d(readOffset
, size
, &box
);
588 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
589 srcObj
->buffer
, 0, &box
);
593 * Called via glClearBufferSubData().
596 st_clear_buffer_subdata(struct gl_context
*ctx
,
597 GLintptr offset
, GLsizeiptr size
,
598 const void *clearValue
,
599 GLsizeiptr clearValueSize
,
600 struct gl_buffer_object
*bufObj
)
602 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
603 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
604 static const char zeros
[16] = {0};
606 if (!pipe
->clear_buffer
) {
607 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
608 clearValue
, clearValueSize
, bufObj
);
615 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
616 clearValue
, clearValueSize
);
620 st_bufferobj_page_commitment(struct gl_context
*ctx
,
621 struct gl_buffer_object
*bufferObj
,
622 GLintptr offset
, GLsizeiptr size
,
625 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
626 struct st_buffer_object
*buf
= st_buffer_object(bufferObj
);
629 u_box_1d(offset
, size
, &box
);
631 if (!pipe
->resource_commit(pipe
, buf
->buffer
, 0, &box
, commit
)) {
632 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferPageCommitmentARB(out of memory)");
638 st_init_bufferobject_functions(struct pipe_screen
*screen
,
639 struct dd_function_table
*functions
)
641 functions
->NewBufferObject
= st_bufferobj_alloc
;
642 functions
->DeleteBuffer
= st_bufferobj_free
;
643 functions
->BufferData
= st_bufferobj_data
;
644 functions
->BufferDataMem
= st_bufferobj_data_mem
;
645 functions
->BufferSubData
= st_bufferobj_subdata
;
646 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
647 functions
->MapBufferRange
= st_bufferobj_map_range
;
648 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
649 functions
->UnmapBuffer
= st_bufferobj_unmap
;
650 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
651 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
652 functions
->BufferPageCommitment
= st_bufferobj_page_commitment
;
654 if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
))
655 functions
->InvalidateBufferSubData
= st_bufferobj_invalidate
;