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"
47 #include "pipe/p_context.h"
48 #include "pipe/p_defines.h"
49 #include "util/u_inlines.h"
53 * There is some duplication between mesa's bufferobjects and our
54 * bufmgr buffers. Both have an integer handle and a hashtable to
55 * lookup an opaque structure. It would be nice if the handles and
56 * internal structure where somehow shared.
58 static struct gl_buffer_object
*
59 st_bufferobj_alloc(struct gl_context
*ctx
, GLuint name
)
61 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
66 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
);
74 * Deallocate/free a vertex/pixel buffer object.
75 * Called via glDeleteBuffersARB().
78 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
80 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
82 assert(obj
->RefCount
== 0);
83 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
86 pipe_resource_reference(&st_obj
->buffer
, NULL
);
88 _mesa_delete_buffer_object(ctx
, obj
);
94 * Replace data in a subrange of buffer object. If the data range
95 * specified by size + offset extends beyond the end of the buffer or
96 * if data is NULL, no copy is performed.
97 * Called via glBufferSubDataARB().
100 st_bufferobj_subdata(struct gl_context
*ctx
,
103 const void * data
, struct gl_buffer_object
*obj
)
105 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
107 /* we may be called from VBO code, so double-check params here */
110 assert(offset
+ size
<= obj
->Size
);
116 * According to ARB_vertex_buffer_object specification, if data is null,
117 * then the contents of the buffer object's data store is undefined. We just
118 * ignore, and leave it unchanged.
123 if (!st_obj
->buffer
) {
124 /* we probably ran out of memory during buffer allocation */
128 /* Now that transfers are per-context, we don't have to figure out
129 * flushing here. Usually drivers won't need to flush in this case
130 * even if the buffer is currently referenced by hardware - they
131 * just queue the upload as dma rather than mapping the underlying
134 pipe_buffer_write(st_context(ctx
)->pipe
,
141 * Called via glGetBufferSubDataARB().
144 st_bufferobj_get_subdata(struct gl_context
*ctx
,
147 void * data
, struct gl_buffer_object
*obj
)
149 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
151 /* we may be called from VBO code, so double-check params here */
154 assert(offset
+ size
<= obj
->Size
);
159 if (!st_obj
->buffer
) {
160 /* we probably ran out of memory during buffer allocation */
164 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
170 * Return bitmask of PIPE_BIND_x flags corresponding a GL buffer target.
173 buffer_target_to_bind_flags(GLenum target
)
176 case GL_PIXEL_PACK_BUFFER_ARB
:
177 case GL_PIXEL_UNPACK_BUFFER_ARB
:
178 return PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
179 case GL_ARRAY_BUFFER_ARB
:
180 return PIPE_BIND_VERTEX_BUFFER
;
181 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
182 return PIPE_BIND_INDEX_BUFFER
;
183 case GL_TEXTURE_BUFFER
:
184 return PIPE_BIND_SAMPLER_VIEW
;
185 case GL_TRANSFORM_FEEDBACK_BUFFER
:
186 return PIPE_BIND_STREAM_OUTPUT
;
187 case GL_UNIFORM_BUFFER
:
188 return PIPE_BIND_CONSTANT_BUFFER
;
189 case GL_DRAW_INDIRECT_BUFFER
:
190 case GL_PARAMETER_BUFFER_ARB
:
191 return PIPE_BIND_COMMAND_ARGS_BUFFER
;
192 case GL_ATOMIC_COUNTER_BUFFER
:
193 case GL_SHADER_STORAGE_BUFFER
:
194 return PIPE_BIND_SHADER_BUFFER
;
195 case GL_QUERY_BUFFER
:
196 return PIPE_BIND_QUERY_BUFFER
;
204 * Return bitmask of PIPE_RESOURCE_x flags corresponding to GL_MAP_x flags.
207 storage_flags_to_buffer_flags(GLbitfield storageFlags
)
210 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
211 flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
212 if (storageFlags
& GL_MAP_COHERENT_BIT
)
213 flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
214 if (storageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)
215 flags
|= PIPE_RESOURCE_FLAG_SPARSE
;
221 * From a buffer object's target, immutability flag, storage flags and
222 * usage hint, return a pipe_resource_usage value (PIPE_USAGE_DYNAMIC,
225 static const enum pipe_resource_usage
226 buffer_usage(GLenum target
, GLboolean immutable
,
227 GLbitfield storageFlags
, GLenum usage
)
231 if (storageFlags
& GL_CLIENT_STORAGE_BIT
) {
232 if (storageFlags
& GL_MAP_READ_BIT
)
233 return PIPE_USAGE_STAGING
;
235 return PIPE_USAGE_STREAM
;
237 return PIPE_USAGE_DEFAULT
;
243 case GL_DYNAMIC_DRAW
:
244 case GL_DYNAMIC_COPY
:
245 return PIPE_USAGE_DYNAMIC
;
248 /* XXX: Remove this test and fall-through when we have PBO unpacking
249 * acceleration. Right now, PBO unpacking is done by the CPU, so we
250 * have to make sure CPU reads are fast.
252 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
253 return PIPE_USAGE_STREAM
;
257 case GL_DYNAMIC_READ
:
259 return PIPE_USAGE_STAGING
;
263 return PIPE_USAGE_DEFAULT
;
269 static ALWAYS_INLINE GLboolean
270 bufferobj_data(struct gl_context
*ctx
,
274 struct gl_memory_object
*memObj
,
277 GLbitfield storageFlags
,
278 struct gl_buffer_object
*obj
)
280 struct st_context
*st
= st_context(ctx
);
281 struct pipe_context
*pipe
= st
->pipe
;
282 struct pipe_screen
*screen
= pipe
->screen
;
283 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
284 struct st_memory_object
*st_mem_obj
= st_memory_object(memObj
);
286 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
287 size
&& st_obj
->buffer
&&
288 st_obj
->Base
.Size
== size
&&
289 st_obj
->Base
.Usage
== usage
&&
290 st_obj
->Base
.StorageFlags
== storageFlags
) {
292 /* Just discard the old contents and write new data.
293 * This should be the same as creating a new buffer, but we avoid
294 * a lot of validation in Mesa.
296 pipe
->buffer_subdata(pipe
, st_obj
->buffer
,
297 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
300 } else if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
)) {
301 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
306 st_obj
->Base
.Size
= size
;
307 st_obj
->Base
.Usage
= usage
;
308 st_obj
->Base
.StorageFlags
= storageFlags
;
310 pipe_resource_reference( &st_obj
->buffer
, NULL
);
312 const unsigned bindings
= buffer_target_to_bind_flags(target
);
314 if (ST_DEBUG
& DEBUG_BUFFER
) {
315 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
316 (int64_t) size
, bindings
);
320 struct pipe_resource buffer
;
322 memset(&buffer
, 0, sizeof buffer
);
323 buffer
.target
= PIPE_BUFFER
;
324 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
325 buffer
.bind
= bindings
;
327 buffer_usage(target
, st_obj
->Base
.Immutable
, storageFlags
, usage
);
328 buffer
.flags
= storage_flags_to_buffer_flags(storageFlags
);
329 buffer
.width0
= size
;
332 buffer
.array_size
= 1;
335 st_obj
->buffer
= screen
->resource_from_memobj(screen
, &buffer
,
339 else if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
341 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
344 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
346 if (st_obj
->buffer
&& data
)
347 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
350 if (!st_obj
->buffer
) {
352 st_obj
->Base
.Size
= 0;
357 /* The current buffer may be bound, so we have to revalidate all atoms that
360 if (st_obj
->Base
.UsageHistory
& USAGE_ARRAY_BUFFER
)
361 ctx
->NewDriverState
|= ST_NEW_VERTEX_ARRAYS
;
362 /* if (st_obj->Base.UsageHistory & USAGE_ELEMENT_ARRAY_BUFFER) */
363 /* ctx->NewDriverState |= TODO: Handle indices as gallium state; */
364 if (st_obj
->Base
.UsageHistory
& USAGE_UNIFORM_BUFFER
)
365 ctx
->NewDriverState
|= ST_NEW_UNIFORM_BUFFER
;
366 if (st_obj
->Base
.UsageHistory
& USAGE_SHADER_STORAGE_BUFFER
)
367 ctx
->NewDriverState
|= ST_NEW_STORAGE_BUFFER
;
368 if (st_obj
->Base
.UsageHistory
& USAGE_TEXTURE_BUFFER
)
369 ctx
->NewDriverState
|= ST_NEW_SAMPLER_VIEWS
| ST_NEW_IMAGE_UNITS
;
370 if (st_obj
->Base
.UsageHistory
& USAGE_ATOMIC_COUNTER_BUFFER
)
371 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewAtomicBuffer
;
377 * Allocate space for and store data in a buffer object. Any data that was
378 * previously stored in the buffer object is lost. If data is NULL,
379 * memory will be allocated, but no copy will occur.
380 * Called via ctx->Driver.BufferData().
381 * \return GL_TRUE for success, GL_FALSE if out of memory
384 st_bufferobj_data(struct gl_context
*ctx
,
389 GLbitfield storageFlags
,
390 struct gl_buffer_object
*obj
)
392 return bufferobj_data(ctx
, target
, size
, data
, NULL
, 0, usage
, storageFlags
, obj
);
396 st_bufferobj_data_mem(struct gl_context
*ctx
,
399 struct gl_memory_object
*memObj
,
402 struct gl_buffer_object
*bufObj
)
404 return bufferobj_data(ctx
, target
, size
, NULL
, memObj
, offset
, usage
, 0, bufObj
);
408 * Called via glInvalidateBuffer(Sub)Data.
411 st_bufferobj_invalidate(struct gl_context
*ctx
,
412 struct gl_buffer_object
*obj
,
416 struct st_context
*st
= st_context(ctx
);
417 struct pipe_context
*pipe
= st
->pipe
;
418 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
420 /* We ignore partial invalidates. */
421 if (offset
!= 0 || size
!= obj
->Size
)
424 /* Nothing to invalidate. */
428 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
433 * Convert GLbitfield of GL_MAP_x flags to gallium pipe_transfer_usage flags.
434 * \param wholeBuffer is the whole buffer being mapped?
436 enum pipe_transfer_usage
437 st_access_flags_to_transfer_flags(GLbitfield access
, bool wholeBuffer
)
439 enum pipe_transfer_usage flags
= 0;
441 if (access
& GL_MAP_WRITE_BIT
)
442 flags
|= PIPE_TRANSFER_WRITE
;
444 if (access
& GL_MAP_READ_BIT
)
445 flags
|= PIPE_TRANSFER_READ
;
447 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
448 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
450 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
451 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
453 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
455 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
457 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
460 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
461 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
463 if (access
& GL_MAP_PERSISTENT_BIT
)
464 flags
|= PIPE_TRANSFER_PERSISTENT
;
466 if (access
& GL_MAP_COHERENT_BIT
)
467 flags
|= PIPE_TRANSFER_COHERENT
;
469 /* ... other flags ...
472 if (access
& MESA_MAP_NOWAIT_BIT
)
473 flags
|= PIPE_TRANSFER_DONTBLOCK
;
480 * Called via glMapBufferRange().
483 st_bufferobj_map_range(struct gl_context
*ctx
,
484 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
485 struct gl_buffer_object
*obj
,
486 gl_map_buffer_index index
)
488 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
489 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
493 assert(offset
< obj
->Size
);
494 assert(offset
+ length
<= obj
->Size
);
496 const enum pipe_transfer_usage transfer_flags
=
497 st_access_flags_to_transfer_flags(access
,
498 offset
== 0 && length
== obj
->Size
);
500 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
504 &st_obj
->transfer
[index
]);
505 if (obj
->Mappings
[index
].Pointer
) {
506 obj
->Mappings
[index
].Offset
= offset
;
507 obj
->Mappings
[index
].Length
= length
;
508 obj
->Mappings
[index
].AccessFlags
= access
;
511 st_obj
->transfer
[index
] = NULL
;
514 return obj
->Mappings
[index
].Pointer
;
519 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
520 GLintptr offset
, GLsizeiptr length
,
521 struct gl_buffer_object
*obj
,
522 gl_map_buffer_index index
)
524 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
525 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
527 /* Subrange is relative to mapped range */
530 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
531 assert(obj
->Mappings
[index
].Pointer
);
536 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
537 obj
->Mappings
[index
].Offset
+ offset
,
543 * Called via glUnmapBufferARB().
546 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
547 gl_map_buffer_index index
)
549 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
550 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
552 if (obj
->Mappings
[index
].Length
)
553 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
555 st_obj
->transfer
[index
] = NULL
;
556 obj
->Mappings
[index
].Pointer
= NULL
;
557 obj
->Mappings
[index
].Offset
= 0;
558 obj
->Mappings
[index
].Length
= 0;
564 * Called via glCopyBufferSubData().
567 st_copy_buffer_subdata(struct gl_context
*ctx
,
568 struct gl_buffer_object
*src
,
569 struct gl_buffer_object
*dst
,
570 GLintptr readOffset
, GLintptr writeOffset
,
573 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
574 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
575 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
581 /* buffer should not already be mapped */
582 assert(!_mesa_check_disallowed_mapping(src
));
583 assert(!_mesa_check_disallowed_mapping(dst
));
585 u_box_1d(readOffset
, size
, &box
);
587 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
588 srcObj
->buffer
, 0, &box
);
592 * Called via glClearBufferSubData().
595 st_clear_buffer_subdata(struct gl_context
*ctx
,
596 GLintptr offset
, GLsizeiptr size
,
597 const void *clearValue
,
598 GLsizeiptr clearValueSize
,
599 struct gl_buffer_object
*bufObj
)
601 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
602 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
603 static const char zeros
[16] = {0};
605 if (!pipe
->clear_buffer
) {
606 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
607 clearValue
, clearValueSize
, bufObj
);
614 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
615 clearValue
, clearValueSize
);
619 st_bufferobj_page_commitment(struct gl_context
*ctx
,
620 struct gl_buffer_object
*bufferObj
,
621 GLintptr offset
, GLsizeiptr size
,
624 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
625 struct st_buffer_object
*buf
= st_buffer_object(bufferObj
);
628 u_box_1d(offset
, size
, &box
);
630 if (!pipe
->resource_commit(pipe
, buf
->buffer
, 0, &box
, commit
)) {
631 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferPageCommitmentARB(out of memory)");
637 st_init_bufferobject_functions(struct pipe_screen
*screen
,
638 struct dd_function_table
*functions
)
640 functions
->NewBufferObject
= st_bufferobj_alloc
;
641 functions
->DeleteBuffer
= st_bufferobj_free
;
642 functions
->BufferData
= st_bufferobj_data
;
643 functions
->BufferDataMem
= st_bufferobj_data_mem
;
644 functions
->BufferSubData
= st_bufferobj_subdata
;
645 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
646 functions
->MapBufferRange
= st_bufferobj_map_range
;
647 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
648 functions
->UnmapBuffer
= st_bufferobj_unmap
;
649 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
650 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
651 functions
->BufferPageCommitment
= st_bufferobj_page_commitment
;
653 if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
))
654 functions
->InvalidateBufferSubData
= st_bufferobj_invalidate
;