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"
43 #include "st_cb_memoryobjects.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "util/u_inlines.h"
52 * There is some duplication between mesa's bufferobjects and our
53 * bufmgr buffers. Both have an integer handle and a hashtable to
54 * lookup an opaque structure. It would be nice if the handles and
55 * internal structure where somehow shared.
57 static struct gl_buffer_object
*
58 st_bufferobj_alloc(struct gl_context
*ctx
, GLuint name
)
60 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
65 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
);
73 * Deallocate/free a vertex/pixel buffer object.
74 * Called via glDeleteBuffersARB().
77 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
79 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
81 assert(obj
->RefCount
== 0);
82 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
85 pipe_resource_reference(&st_obj
->buffer
, NULL
);
87 _mesa_delete_buffer_object(ctx
, obj
);
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 void * 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 void * 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
,
167 static ALWAYS_INLINE GLboolean
168 bufferobj_data(struct gl_context
*ctx
,
172 struct gl_memory_object
*memObj
,
175 GLbitfield storageFlags
,
176 struct gl_buffer_object
*obj
)
178 struct st_context
*st
= st_context(ctx
);
179 struct pipe_context
*pipe
= st
->pipe
;
180 struct pipe_screen
*screen
= pipe
->screen
;
181 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
182 struct st_memory_object
*st_mem_obj
= st_memory_object(memObj
);
183 unsigned bind
, pipe_usage
, pipe_flags
= 0;
185 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
186 size
&& st_obj
->buffer
&&
187 st_obj
->Base
.Size
== size
&&
188 st_obj
->Base
.Usage
== usage
&&
189 st_obj
->Base
.StorageFlags
== storageFlags
) {
191 /* Just discard the old contents and write new data.
192 * This should be the same as creating a new buffer, but we avoid
193 * a lot of validation in Mesa.
195 pipe
->buffer_subdata(pipe
, st_obj
->buffer
,
196 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
199 } else if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
)) {
200 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
205 st_obj
->Base
.Size
= size
;
206 st_obj
->Base
.Usage
= usage
;
207 st_obj
->Base
.StorageFlags
= storageFlags
;
210 case GL_PIXEL_PACK_BUFFER_ARB
:
211 case GL_PIXEL_UNPACK_BUFFER_ARB
:
212 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
214 case GL_ARRAY_BUFFER_ARB
:
215 bind
= PIPE_BIND_VERTEX_BUFFER
;
217 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
218 bind
= PIPE_BIND_INDEX_BUFFER
;
220 case GL_TEXTURE_BUFFER
:
221 bind
= PIPE_BIND_SAMPLER_VIEW
;
223 case GL_TRANSFORM_FEEDBACK_BUFFER
:
224 bind
= PIPE_BIND_STREAM_OUTPUT
;
226 case GL_UNIFORM_BUFFER
:
227 bind
= PIPE_BIND_CONSTANT_BUFFER
;
229 case GL_DRAW_INDIRECT_BUFFER
:
230 case GL_PARAMETER_BUFFER_ARB
:
231 bind
= PIPE_BIND_COMMAND_ARGS_BUFFER
;
233 case GL_ATOMIC_COUNTER_BUFFER
:
234 case GL_SHADER_STORAGE_BUFFER
:
235 bind
= PIPE_BIND_SHADER_BUFFER
;
237 case GL_QUERY_BUFFER
:
238 bind
= PIPE_BIND_QUERY_BUFFER
;
245 if (st_obj
->Base
.Immutable
) {
247 if (storageFlags
& GL_CLIENT_STORAGE_BIT
) {
248 if (storageFlags
& GL_MAP_READ_BIT
)
249 pipe_usage
= PIPE_USAGE_STAGING
;
251 pipe_usage
= PIPE_USAGE_STREAM
;
253 pipe_usage
= PIPE_USAGE_DEFAULT
;
262 pipe_usage
= PIPE_USAGE_DEFAULT
;
264 case GL_DYNAMIC_DRAW
:
265 case GL_DYNAMIC_COPY
:
266 pipe_usage
= PIPE_USAGE_DYNAMIC
;
270 /* XXX: Remove this test and fall-through when we have PBO unpacking
271 * acceleration. Right now, PBO unpacking is done by the CPU, so we
272 * have to make sure CPU reads are fast.
274 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
275 pipe_usage
= PIPE_USAGE_STREAM
;
280 case GL_DYNAMIC_READ
:
282 pipe_usage
= PIPE_USAGE_STAGING
;
288 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
289 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
290 if (storageFlags
& GL_MAP_COHERENT_BIT
)
291 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
292 if (storageFlags
& GL_SPARSE_STORAGE_BIT_ARB
)
293 pipe_flags
|= PIPE_RESOURCE_FLAG_SPARSE
;
295 pipe_resource_reference( &st_obj
->buffer
, NULL
);
297 if (ST_DEBUG
& DEBUG_BUFFER
) {
298 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
299 (int64_t) size
, bind
);
303 struct pipe_resource buffer
;
305 memset(&buffer
, 0, sizeof buffer
);
306 buffer
.target
= PIPE_BUFFER
;
307 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
309 buffer
.usage
= pipe_usage
;
310 buffer
.flags
= pipe_flags
;
311 buffer
.width0
= size
;
314 buffer
.array_size
= 1;
317 st_obj
->buffer
= screen
->resource_from_memobj(screen
, &buffer
,
321 else if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
323 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
326 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
328 if (st_obj
->buffer
&& data
)
329 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
332 if (!st_obj
->buffer
) {
334 st_obj
->Base
.Size
= 0;
339 /* The current buffer may be bound, so we have to revalidate all atoms that
342 /* TODO: Add arrays to usage history */
343 ctx
->NewDriverState
|= ST_NEW_VERTEX_ARRAYS
;
344 if (st_obj
->Base
.UsageHistory
& USAGE_UNIFORM_BUFFER
)
345 ctx
->NewDriverState
|= ST_NEW_UNIFORM_BUFFER
;
346 if (st_obj
->Base
.UsageHistory
& USAGE_SHADER_STORAGE_BUFFER
)
347 ctx
->NewDriverState
|= ST_NEW_STORAGE_BUFFER
;
348 if (st_obj
->Base
.UsageHistory
& USAGE_TEXTURE_BUFFER
)
349 ctx
->NewDriverState
|= ST_NEW_SAMPLER_VIEWS
| ST_NEW_IMAGE_UNITS
;
350 if (st_obj
->Base
.UsageHistory
& USAGE_ATOMIC_COUNTER_BUFFER
)
351 ctx
->NewDriverState
|= ST_NEW_ATOMIC_BUFFER
;
357 * Allocate space for and store data in a buffer object. Any data that was
358 * previously stored in the buffer object is lost. If data is NULL,
359 * memory will be allocated, but no copy will occur.
360 * Called via ctx->Driver.BufferData().
361 * \return GL_TRUE for success, GL_FALSE if out of memory
364 st_bufferobj_data(struct gl_context
*ctx
,
369 GLbitfield storageFlags
,
370 struct gl_buffer_object
*obj
)
372 return bufferobj_data(ctx
, target
, size
, data
, NULL
, 0, usage
, storageFlags
, obj
);
376 st_bufferobj_data_mem(struct gl_context
*ctx
,
379 struct gl_memory_object
*memObj
,
382 struct gl_buffer_object
*bufObj
)
384 return bufferobj_data(ctx
, target
, size
, NULL
, memObj
, offset
, usage
, 0, bufObj
);
388 * Called via glInvalidateBuffer(Sub)Data.
391 st_bufferobj_invalidate(struct gl_context
*ctx
,
392 struct gl_buffer_object
*obj
,
396 struct st_context
*st
= st_context(ctx
);
397 struct pipe_context
*pipe
= st
->pipe
;
398 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
400 /* We ignore partial invalidates. */
401 if (offset
!= 0 || size
!= obj
->Size
)
404 /* Nothing to invalidate. */
408 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
413 * Called via glMapBufferRange().
416 st_bufferobj_map_range(struct gl_context
*ctx
,
417 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
418 struct gl_buffer_object
*obj
,
419 gl_map_buffer_index index
)
421 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
422 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
423 enum pipe_transfer_usage flags
= 0x0;
425 if (access
& GL_MAP_WRITE_BIT
)
426 flags
|= PIPE_TRANSFER_WRITE
;
428 if (access
& GL_MAP_READ_BIT
)
429 flags
|= PIPE_TRANSFER_READ
;
431 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
432 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
434 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
435 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
437 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
438 if (offset
== 0 && length
== obj
->Size
)
439 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
441 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
444 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
445 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
447 if (access
& GL_MAP_PERSISTENT_BIT
)
448 flags
|= PIPE_TRANSFER_PERSISTENT
;
450 if (access
& GL_MAP_COHERENT_BIT
)
451 flags
|= PIPE_TRANSFER_COHERENT
;
453 /* ... other flags ...
456 if (access
& MESA_MAP_NOWAIT_BIT
)
457 flags
|= PIPE_TRANSFER_DONTBLOCK
;
461 assert(offset
< obj
->Size
);
462 assert(offset
+ length
<= obj
->Size
);
464 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
468 &st_obj
->transfer
[index
]);
469 if (obj
->Mappings
[index
].Pointer
) {
470 obj
->Mappings
[index
].Offset
= offset
;
471 obj
->Mappings
[index
].Length
= length
;
472 obj
->Mappings
[index
].AccessFlags
= access
;
475 st_obj
->transfer
[index
] = NULL
;
478 return obj
->Mappings
[index
].Pointer
;
483 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
484 GLintptr offset
, GLsizeiptr length
,
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
);
491 /* Subrange is relative to mapped range */
494 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
495 assert(obj
->Mappings
[index
].Pointer
);
500 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
501 obj
->Mappings
[index
].Offset
+ offset
,
507 * Called via glUnmapBufferARB().
510 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
511 gl_map_buffer_index index
)
513 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
514 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
516 if (obj
->Mappings
[index
].Length
)
517 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
519 st_obj
->transfer
[index
] = NULL
;
520 obj
->Mappings
[index
].Pointer
= NULL
;
521 obj
->Mappings
[index
].Offset
= 0;
522 obj
->Mappings
[index
].Length
= 0;
528 * Called via glCopyBufferSubData().
531 st_copy_buffer_subdata(struct gl_context
*ctx
,
532 struct gl_buffer_object
*src
,
533 struct gl_buffer_object
*dst
,
534 GLintptr readOffset
, GLintptr writeOffset
,
537 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
538 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
539 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
545 /* buffer should not already be mapped */
546 assert(!_mesa_check_disallowed_mapping(src
));
547 assert(!_mesa_check_disallowed_mapping(dst
));
549 u_box_1d(readOffset
, size
, &box
);
551 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
552 srcObj
->buffer
, 0, &box
);
556 * Called via glClearBufferSubData().
559 st_clear_buffer_subdata(struct gl_context
*ctx
,
560 GLintptr offset
, GLsizeiptr size
,
561 const void *clearValue
,
562 GLsizeiptr clearValueSize
,
563 struct gl_buffer_object
*bufObj
)
565 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
566 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
567 static const char zeros
[16] = {0};
569 if (!pipe
->clear_buffer
) {
570 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
571 clearValue
, clearValueSize
, bufObj
);
578 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
579 clearValue
, clearValueSize
);
583 st_bufferobj_page_commitment(struct gl_context
*ctx
,
584 struct gl_buffer_object
*bufferObj
,
585 GLintptr offset
, GLsizeiptr size
,
588 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
589 struct st_buffer_object
*buf
= st_buffer_object(bufferObj
);
592 u_box_1d(offset
, size
, &box
);
594 if (!pipe
->resource_commit(pipe
, buf
->buffer
, 0, &box
, commit
)) {
595 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glBufferPageCommitmentARB(out of memory)");
601 st_init_bufferobject_functions(struct pipe_screen
*screen
,
602 struct dd_function_table
*functions
)
604 functions
->NewBufferObject
= st_bufferobj_alloc
;
605 functions
->DeleteBuffer
= st_bufferobj_free
;
606 functions
->BufferData
= st_bufferobj_data
;
607 functions
->BufferDataMem
= st_bufferobj_data_mem
;
608 functions
->BufferSubData
= st_bufferobj_subdata
;
609 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
610 functions
->MapBufferRange
= st_bufferobj_map_range
;
611 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
612 functions
->UnmapBuffer
= st_bufferobj_unmap
;
613 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
614 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
615 functions
->BufferPageCommitment
= st_bufferobj_page_commitment
;
617 if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
))
618 functions
->InvalidateBufferSubData
= st_bufferobj_invalidate
;