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 * If the buffer is mapped, suppress implicit buffer range invalidation
136 * by using PIPE_TRANSFER_MAP_DIRECTLY.
138 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
140 pipe
->buffer_subdata(pipe
, st_obj
->buffer
,
141 _mesa_bufferobj_mapped(obj
, MAP_USER
) ?
142 PIPE_TRANSFER_MAP_DIRECTLY
: 0,
148 * Called via glGetBufferSubDataARB().
151 st_bufferobj_get_subdata(struct gl_context
*ctx
,
154 void * data
, struct gl_buffer_object
*obj
)
156 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
158 /* we may be called from VBO code, so double-check params here */
161 assert(offset
+ size
<= obj
->Size
);
166 if (!st_obj
->buffer
) {
167 /* we probably ran out of memory during buffer allocation */
171 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
177 * Return bitmask of PIPE_BIND_x flags corresponding a GL buffer target.
180 buffer_target_to_bind_flags(GLenum target
)
183 case GL_PIXEL_PACK_BUFFER_ARB
:
184 case GL_PIXEL_UNPACK_BUFFER_ARB
:
185 return PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
186 case GL_ARRAY_BUFFER_ARB
:
187 return PIPE_BIND_VERTEX_BUFFER
;
188 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
189 return PIPE_BIND_INDEX_BUFFER
;
190 case GL_TEXTURE_BUFFER
:
191 return PIPE_BIND_SAMPLER_VIEW
;
192 case GL_TRANSFORM_FEEDBACK_BUFFER
:
193 return PIPE_BIND_STREAM_OUTPUT
;
194 case GL_UNIFORM_BUFFER
:
195 return PIPE_BIND_CONSTANT_BUFFER
;
196 case GL_DRAW_INDIRECT_BUFFER
:
197 case GL_PARAMETER_BUFFER_ARB
:
198 return PIPE_BIND_COMMAND_ARGS_BUFFER
;
199 case GL_ATOMIC_COUNTER_BUFFER
:
200 case GL_SHADER_STORAGE_BUFFER
:
201 return PIPE_BIND_SHADER_BUFFER
;
202 case GL_QUERY_BUFFER
:
203 return PIPE_BIND_QUERY_BUFFER
;
211 * Return bitmask of PIPE_RESOURCE_x flags corresponding to GL_MAP_x flags.
214 storage_flags_to_buffer_flags(GLbitfield storageFlags
)
217 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
218 flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
219 if (storageFlags
& GL_MAP_COHERENT_BIT
)
220 flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
221 if (storageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)
222 flags
|= PIPE_RESOURCE_FLAG_SPARSE
;
228 * From a buffer object's target, immutability flag, storage flags and
229 * usage hint, return a pipe_resource_usage value (PIPE_USAGE_DYNAMIC,
232 static enum pipe_resource_usage
233 buffer_usage(GLenum target
, GLboolean immutable
,
234 GLbitfield storageFlags
, GLenum usage
)
238 if (storageFlags
& GL_CLIENT_STORAGE_BIT
) {
239 if (storageFlags
& GL_MAP_READ_BIT
)
240 return PIPE_USAGE_STAGING
;
242 return PIPE_USAGE_STREAM
;
244 return PIPE_USAGE_DEFAULT
;
250 case GL_DYNAMIC_DRAW
:
251 case GL_DYNAMIC_COPY
:
252 return PIPE_USAGE_DYNAMIC
;
255 /* XXX: Remove this test and fall-through when we have PBO unpacking
256 * acceleration. Right now, PBO unpacking is done by the CPU, so we
257 * have to make sure CPU reads are fast.
259 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
260 return PIPE_USAGE_STREAM
;
264 case GL_DYNAMIC_READ
:
266 return PIPE_USAGE_STAGING
;
270 return PIPE_USAGE_DEFAULT
;
276 static ALWAYS_INLINE GLboolean
277 bufferobj_data(struct gl_context
*ctx
,
281 struct gl_memory_object
*memObj
,
284 GLbitfield storageFlags
,
285 struct gl_buffer_object
*obj
)
287 struct st_context
*st
= st_context(ctx
);
288 struct pipe_context
*pipe
= st
->pipe
;
289 struct pipe_screen
*screen
= pipe
->screen
;
290 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
291 struct st_memory_object
*st_mem_obj
= st_memory_object(memObj
);
292 bool is_mapped
= _mesa_bufferobj_mapped(obj
, MAP_USER
);
294 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
295 size
&& st_obj
->buffer
&&
296 st_obj
->Base
.Size
== size
&&
297 st_obj
->Base
.Usage
== usage
&&
298 st_obj
->Base
.StorageFlags
== storageFlags
) {
300 /* Just discard the old contents and write new data.
301 * This should be the same as creating a new buffer, but we avoid
302 * a lot of validation in Mesa.
304 * If the buffer is mapped, we can't discard it.
306 * PIPE_TRANSFER_MAP_DIRECTLY supresses implicit buffer range
309 pipe
->buffer_subdata(pipe
, st_obj
->buffer
,
310 is_mapped
? PIPE_TRANSFER_MAP_DIRECTLY
:
311 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
314 } else if (is_mapped
) {
315 return GL_TRUE
; /* can't reallocate, nothing to do */
316 } else if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
)) {
317 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
322 st_obj
->Base
.Size
= size
;
323 st_obj
->Base
.Usage
= usage
;
324 st_obj
->Base
.StorageFlags
= storageFlags
;
326 pipe_resource_reference( &st_obj
->buffer
, NULL
);
328 const unsigned bindings
= buffer_target_to_bind_flags(target
);
330 if (ST_DEBUG
& DEBUG_BUFFER
) {
331 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
332 (int64_t) size
, bindings
);
336 struct pipe_resource buffer
;
338 memset(&buffer
, 0, sizeof buffer
);
339 buffer
.target
= PIPE_BUFFER
;
340 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
341 buffer
.bind
= bindings
;
343 buffer_usage(target
, st_obj
->Base
.Immutable
, storageFlags
, usage
);
344 buffer
.flags
= storage_flags_to_buffer_flags(storageFlags
);
345 buffer
.width0
= size
;
348 buffer
.array_size
= 1;
351 st_obj
->buffer
= screen
->resource_from_memobj(screen
, &buffer
,
355 else if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
357 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
360 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
362 if (st_obj
->buffer
&& data
)
363 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
366 if (!st_obj
->buffer
) {
368 st_obj
->Base
.Size
= 0;
373 /* The current buffer may be bound, so we have to revalidate all atoms that
376 if (st_obj
->Base
.UsageHistory
& USAGE_ARRAY_BUFFER
)
377 ctx
->NewDriverState
|= ST_NEW_VERTEX_ARRAYS
;
378 /* if (st_obj->Base.UsageHistory & USAGE_ELEMENT_ARRAY_BUFFER) */
379 /* ctx->NewDriverState |= TODO: Handle indices as gallium state; */
380 if (st_obj
->Base
.UsageHistory
& USAGE_UNIFORM_BUFFER
)
381 ctx
->NewDriverState
|= ST_NEW_UNIFORM_BUFFER
;
382 if (st_obj
->Base
.UsageHistory
& USAGE_SHADER_STORAGE_BUFFER
)
383 ctx
->NewDriverState
|= ST_NEW_STORAGE_BUFFER
;
384 if (st_obj
->Base
.UsageHistory
& USAGE_TEXTURE_BUFFER
)
385 ctx
->NewDriverState
|= ST_NEW_SAMPLER_VIEWS
| ST_NEW_IMAGE_UNITS
;
386 if (st_obj
->Base
.UsageHistory
& USAGE_ATOMIC_COUNTER_BUFFER
)
387 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
393 * Allocate space for and store data in a buffer object. Any data that was
394 * previously stored in the buffer object is lost. If data is NULL,
395 * memory will be allocated, but no copy will occur.
396 * Called via ctx->Driver.BufferData().
397 * \return GL_TRUE for success, GL_FALSE if out of memory
400 st_bufferobj_data(struct gl_context
*ctx
,
405 GLbitfield storageFlags
,
406 struct gl_buffer_object
*obj
)
408 return bufferobj_data(ctx
, target
, size
, data
, NULL
, 0, usage
, storageFlags
, obj
);
412 st_bufferobj_data_mem(struct gl_context
*ctx
,
415 struct gl_memory_object
*memObj
,
418 struct gl_buffer_object
*bufObj
)
420 return bufferobj_data(ctx
, target
, size
, NULL
, memObj
, offset
, usage
, 0, bufObj
);
424 * Called via glInvalidateBuffer(Sub)Data.
427 st_bufferobj_invalidate(struct gl_context
*ctx
,
428 struct gl_buffer_object
*obj
,
432 struct st_context
*st
= st_context(ctx
);
433 struct pipe_context
*pipe
= st
->pipe
;
434 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
436 /* We ignore partial invalidates. */
437 if (offset
!= 0 || size
!= obj
->Size
)
440 /* If the buffer is mapped, we can't invalidate it. */
441 if (!st_obj
->buffer
|| _mesa_bufferobj_mapped(obj
, MAP_USER
))
444 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
449 * Convert GLbitfield of GL_MAP_x flags to gallium pipe_transfer_usage flags.
450 * \param wholeBuffer is the whole buffer being mapped?
452 enum pipe_transfer_usage
453 st_access_flags_to_transfer_flags(GLbitfield access
, bool wholeBuffer
)
455 enum pipe_transfer_usage flags
= 0;
457 if (access
& GL_MAP_WRITE_BIT
)
458 flags
|= PIPE_TRANSFER_WRITE
;
460 if (access
& GL_MAP_READ_BIT
)
461 flags
|= PIPE_TRANSFER_READ
;
463 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
464 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
466 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
467 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
469 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
471 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
473 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
476 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
477 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
479 if (access
& GL_MAP_PERSISTENT_BIT
)
480 flags
|= PIPE_TRANSFER_PERSISTENT
;
482 if (access
& GL_MAP_COHERENT_BIT
)
483 flags
|= PIPE_TRANSFER_COHERENT
;
485 /* ... other flags ...
488 if (access
& MESA_MAP_NOWAIT_BIT
)
489 flags
|= PIPE_TRANSFER_DONTBLOCK
;
496 * Called via glMapBufferRange().
499 st_bufferobj_map_range(struct gl_context
*ctx
,
500 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
501 struct gl_buffer_object
*obj
,
502 gl_map_buffer_index index
)
504 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
505 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
509 assert(offset
< obj
->Size
);
510 assert(offset
+ length
<= obj
->Size
);
512 const enum pipe_transfer_usage transfer_flags
=
513 st_access_flags_to_transfer_flags(access
,
514 offset
== 0 && length
== obj
->Size
);
516 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
520 &st_obj
->transfer
[index
]);
521 if (obj
->Mappings
[index
].Pointer
) {
522 obj
->Mappings
[index
].Offset
= offset
;
523 obj
->Mappings
[index
].Length
= length
;
524 obj
->Mappings
[index
].AccessFlags
= access
;
527 st_obj
->transfer
[index
] = NULL
;
530 return obj
->Mappings
[index
].Pointer
;
535 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
536 GLintptr offset
, GLsizeiptr length
,
537 struct gl_buffer_object
*obj
,
538 gl_map_buffer_index index
)
540 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
541 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
543 /* Subrange is relative to mapped range */
546 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
547 assert(obj
->Mappings
[index
].Pointer
);
552 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
553 obj
->Mappings
[index
].Offset
+ offset
,
559 * Called via glUnmapBufferARB().
562 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
563 gl_map_buffer_index index
)
565 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
566 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
568 if (obj
->Mappings
[index
].Length
)
569 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
571 st_obj
->transfer
[index
] = NULL
;
572 obj
->Mappings
[index
].Pointer
= NULL
;
573 obj
->Mappings
[index
].Offset
= 0;
574 obj
->Mappings
[index
].Length
= 0;
580 * Called via glCopyBufferSubData().
583 st_copy_buffer_subdata(struct gl_context
*ctx
,
584 struct gl_buffer_object
*src
,
585 struct gl_buffer_object
*dst
,
586 GLintptr readOffset
, GLintptr writeOffset
,
589 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
590 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
591 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
597 /* buffer should not already be mapped */
598 assert(!_mesa_check_disallowed_mapping(src
));
599 assert(!_mesa_check_disallowed_mapping(dst
));
601 u_box_1d(readOffset
, size
, &box
);
603 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
604 srcObj
->buffer
, 0, &box
);
608 * Called via glClearBufferSubData().
611 st_clear_buffer_subdata(struct gl_context
*ctx
,
612 GLintptr offset
, GLsizeiptr size
,
613 const void *clearValue
,
614 GLsizeiptr clearValueSize
,
615 struct gl_buffer_object
*bufObj
)
617 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
618 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
619 static const char zeros
[16] = {0};
621 if (!pipe
->clear_buffer
) {
622 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
623 clearValue
, clearValueSize
, bufObj
);
630 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
631 clearValue
, clearValueSize
);
635 st_bufferobj_page_commitment(struct gl_context
*ctx
,
636 struct gl_buffer_object
*bufferObj
,
637 GLintptr offset
, GLsizeiptr size
,
640 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
641 struct st_buffer_object
*buf
= st_buffer_object(bufferObj
);
644 u_box_1d(offset
, size
, &box
);
646 if (!pipe
->resource_commit(pipe
, buf
->buffer
, 0, &box
, commit
)) {
647 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferPageCommitmentARB(out of memory)");
653 st_init_bufferobject_functions(struct pipe_screen
*screen
,
654 struct dd_function_table
*functions
)
656 functions
->NewBufferObject
= st_bufferobj_alloc
;
657 functions
->DeleteBuffer
= st_bufferobj_free
;
658 functions
->BufferData
= st_bufferobj_data
;
659 functions
->BufferDataMem
= st_bufferobj_data_mem
;
660 functions
->BufferSubData
= st_bufferobj_subdata
;
661 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
662 functions
->MapBufferRange
= st_bufferobj_map_range
;
663 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
664 functions
->UnmapBuffer
= st_bufferobj_unmap
;
665 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
666 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
667 functions
->BufferPageCommitment
= st_bufferobj_page_commitment
;
669 if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
))
670 functions
->InvalidateBufferSubData
= st_bufferobj_invalidate
;