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
)
59 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
64 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
);
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 _mesa_delete_buffer_object(ctx
, obj
);
92 * Replace data in a subrange of buffer object. If the data range
93 * specified by size + offset extends beyond the end of the buffer or
94 * if data is NULL, no copy is performed.
95 * Called via glBufferSubDataARB().
98 st_bufferobj_subdata(struct gl_context
*ctx
,
101 const void * data
, struct gl_buffer_object
*obj
)
103 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
105 /* we may be called from VBO code, so double-check params here */
108 assert(offset
+ size
<= obj
->Size
);
114 * According to ARB_vertex_buffer_object specification, if data is null,
115 * then the contents of the buffer object's data store is undefined. We just
116 * ignore, and leave it unchanged.
121 if (!st_obj
->buffer
) {
122 /* we probably ran out of memory during buffer allocation */
126 /* Now that transfers are per-context, we don't have to figure out
127 * flushing here. Usually drivers won't need to flush in this case
128 * even if the buffer is currently referenced by hardware - they
129 * just queue the upload as dma rather than mapping the underlying
132 pipe_buffer_write(st_context(ctx
)->pipe
,
139 * Called via glGetBufferSubDataARB().
142 st_bufferobj_get_subdata(struct gl_context
*ctx
,
145 void * data
, struct gl_buffer_object
*obj
)
147 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
149 /* we may be called from VBO code, so double-check params here */
152 assert(offset
+ size
<= obj
->Size
);
157 if (!st_obj
->buffer
) {
158 /* we probably ran out of memory during buffer allocation */
162 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
168 * Allocate space for and store data in a buffer object. Any data that was
169 * previously stored in the buffer object is lost. If data is NULL,
170 * memory will be allocated, but no copy will occur.
171 * Called via ctx->Driver.BufferData().
172 * \return GL_TRUE for success, GL_FALSE if out of memory
175 st_bufferobj_data(struct gl_context
*ctx
,
180 GLbitfield storageFlags
,
181 struct gl_buffer_object
*obj
)
183 struct st_context
*st
= st_context(ctx
);
184 struct pipe_context
*pipe
= st
->pipe
;
185 struct pipe_screen
*screen
= pipe
->screen
;
186 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
187 unsigned bind
, pipe_usage
, pipe_flags
= 0;
189 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
190 size
&& st_obj
->buffer
&&
191 st_obj
->Base
.Size
== size
&&
192 st_obj
->Base
.Usage
== usage
&&
193 st_obj
->Base
.StorageFlags
== storageFlags
) {
195 /* Just discard the old contents and write new data.
196 * This should be the same as creating a new buffer, but we avoid
197 * a lot of validation in Mesa.
199 pipe
->buffer_subdata(pipe
, st_obj
->buffer
,
200 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
203 } else if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
)) {
204 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
209 st_obj
->Base
.Size
= size
;
210 st_obj
->Base
.Usage
= usage
;
211 st_obj
->Base
.StorageFlags
= storageFlags
;
214 case GL_PIXEL_PACK_BUFFER_ARB
:
215 case GL_PIXEL_UNPACK_BUFFER_ARB
:
216 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
218 case GL_ARRAY_BUFFER_ARB
:
219 bind
= PIPE_BIND_VERTEX_BUFFER
;
221 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
222 bind
= PIPE_BIND_INDEX_BUFFER
;
224 case GL_TEXTURE_BUFFER
:
225 bind
= PIPE_BIND_SAMPLER_VIEW
;
227 case GL_TRANSFORM_FEEDBACK_BUFFER
:
228 bind
= PIPE_BIND_STREAM_OUTPUT
;
230 case GL_UNIFORM_BUFFER
:
231 bind
= PIPE_BIND_CONSTANT_BUFFER
;
233 case GL_DRAW_INDIRECT_BUFFER
:
234 case GL_PARAMETER_BUFFER_ARB
:
235 bind
= PIPE_BIND_COMMAND_ARGS_BUFFER
;
237 case GL_ATOMIC_COUNTER_BUFFER
:
238 case GL_SHADER_STORAGE_BUFFER
:
239 bind
= PIPE_BIND_SHADER_BUFFER
;
241 case GL_QUERY_BUFFER
:
242 bind
= PIPE_BIND_QUERY_BUFFER
;
249 if (st_obj
->Base
.Immutable
) {
251 if (storageFlags
& GL_CLIENT_STORAGE_BIT
) {
252 if (storageFlags
& GL_MAP_READ_BIT
)
253 pipe_usage
= PIPE_USAGE_STAGING
;
255 pipe_usage
= PIPE_USAGE_STREAM
;
257 pipe_usage
= PIPE_USAGE_DEFAULT
;
266 pipe_usage
= PIPE_USAGE_DEFAULT
;
268 case GL_DYNAMIC_DRAW
:
269 case GL_DYNAMIC_COPY
:
270 pipe_usage
= PIPE_USAGE_DYNAMIC
;
274 /* XXX: Remove this test and fall-through when we have PBO unpacking
275 * acceleration. Right now, PBO unpacking is done by the CPU, so we
276 * have to make sure CPU reads are fast.
278 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
279 pipe_usage
= PIPE_USAGE_STREAM
;
284 case GL_DYNAMIC_READ
:
286 pipe_usage
= PIPE_USAGE_STAGING
;
292 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
293 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
294 if (storageFlags
& GL_MAP_COHERENT_BIT
)
295 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
297 pipe_resource_reference( &st_obj
->buffer
, NULL
);
299 if (ST_DEBUG
& DEBUG_BUFFER
) {
300 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
301 (int64_t) size
, bind
);
305 struct pipe_resource buffer
;
307 memset(&buffer
, 0, sizeof buffer
);
308 buffer
.target
= PIPE_BUFFER
;
309 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
311 buffer
.usage
= pipe_usage
;
312 buffer
.flags
= pipe_flags
;
313 buffer
.width0
= size
;
316 buffer
.array_size
= 1;
318 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
320 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
323 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
325 if (st_obj
->buffer
&& data
)
326 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
329 if (!st_obj
->buffer
) {
331 st_obj
->Base
.Size
= 0;
336 /* The current buffer may be bound, so we have to revalidate all atoms that
339 /* TODO: Add arrays to usage history */
340 st
->dirty
|= ST_NEW_VERTEX_ARRAYS
;
341 if (st_obj
->Base
.UsageHistory
& USAGE_UNIFORM_BUFFER
)
342 st
->dirty
|= ST_NEW_UNIFORM_BUFFER
;
343 if (st_obj
->Base
.UsageHistory
& USAGE_SHADER_STORAGE_BUFFER
)
344 st
->dirty
|= ST_NEW_STORAGE_BUFFER
;
345 if (st_obj
->Base
.UsageHistory
& USAGE_TEXTURE_BUFFER
)
346 st
->dirty
|= ST_NEW_SAMPLER_VIEWS
| ST_NEW_IMAGE_UNITS
;
347 if (st_obj
->Base
.UsageHistory
& USAGE_ATOMIC_COUNTER_BUFFER
)
348 st
->dirty
|= ST_NEW_ATOMIC_BUFFER
;
355 * Called via glInvalidateBuffer(Sub)Data.
358 st_bufferobj_invalidate(struct gl_context
*ctx
,
359 struct gl_buffer_object
*obj
,
363 struct st_context
*st
= st_context(ctx
);
364 struct pipe_context
*pipe
= st
->pipe
;
365 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
367 /* We ignore partial invalidates. */
368 if (offset
!= 0 || size
!= obj
->Size
)
371 /* Nothing to invalidate. */
375 pipe
->invalidate_resource(pipe
, st_obj
->buffer
);
380 * Called via glMapBufferRange().
383 st_bufferobj_map_range(struct gl_context
*ctx
,
384 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
385 struct gl_buffer_object
*obj
,
386 gl_map_buffer_index index
)
388 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
389 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
390 enum pipe_transfer_usage flags
= 0x0;
392 if (access
& GL_MAP_WRITE_BIT
)
393 flags
|= PIPE_TRANSFER_WRITE
;
395 if (access
& GL_MAP_READ_BIT
)
396 flags
|= PIPE_TRANSFER_READ
;
398 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
399 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
401 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
402 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
404 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
405 if (offset
== 0 && length
== obj
->Size
)
406 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
408 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
411 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
412 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
414 if (access
& GL_MAP_PERSISTENT_BIT
)
415 flags
|= PIPE_TRANSFER_PERSISTENT
;
417 if (access
& GL_MAP_COHERENT_BIT
)
418 flags
|= PIPE_TRANSFER_COHERENT
;
420 /* ... other flags ...
423 if (access
& MESA_MAP_NOWAIT_BIT
)
424 flags
|= PIPE_TRANSFER_DONTBLOCK
;
428 assert(offset
< obj
->Size
);
429 assert(offset
+ length
<= obj
->Size
);
431 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
435 &st_obj
->transfer
[index
]);
436 if (obj
->Mappings
[index
].Pointer
) {
437 obj
->Mappings
[index
].Offset
= offset
;
438 obj
->Mappings
[index
].Length
= length
;
439 obj
->Mappings
[index
].AccessFlags
= access
;
442 st_obj
->transfer
[index
] = NULL
;
445 return obj
->Mappings
[index
].Pointer
;
450 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
451 GLintptr offset
, GLsizeiptr length
,
452 struct gl_buffer_object
*obj
,
453 gl_map_buffer_index index
)
455 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
456 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
458 /* Subrange is relative to mapped range */
461 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
462 assert(obj
->Mappings
[index
].Pointer
);
467 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
468 obj
->Mappings
[index
].Offset
+ offset
,
474 * Called via glUnmapBufferARB().
477 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
478 gl_map_buffer_index index
)
480 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
481 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
483 if (obj
->Mappings
[index
].Length
)
484 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
486 st_obj
->transfer
[index
] = NULL
;
487 obj
->Mappings
[index
].Pointer
= NULL
;
488 obj
->Mappings
[index
].Offset
= 0;
489 obj
->Mappings
[index
].Length
= 0;
495 * Called via glCopyBufferSubData().
498 st_copy_buffer_subdata(struct gl_context
*ctx
,
499 struct gl_buffer_object
*src
,
500 struct gl_buffer_object
*dst
,
501 GLintptr readOffset
, GLintptr writeOffset
,
504 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
505 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
506 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
512 /* buffer should not already be mapped */
513 assert(!_mesa_check_disallowed_mapping(src
));
514 assert(!_mesa_check_disallowed_mapping(dst
));
516 u_box_1d(readOffset
, size
, &box
);
518 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
519 srcObj
->buffer
, 0, &box
);
523 * Called via glClearBufferSubData().
526 st_clear_buffer_subdata(struct gl_context
*ctx
,
527 GLintptr offset
, GLsizeiptr size
,
528 const void *clearValue
,
529 GLsizeiptr clearValueSize
,
530 struct gl_buffer_object
*bufObj
)
532 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
533 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
534 static const char zeros
[16] = {0};
536 if (!pipe
->clear_buffer
) {
537 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
538 clearValue
, clearValueSize
, bufObj
);
545 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
546 clearValue
, clearValueSize
);
550 /* TODO: if buffer wasn't created with appropriate usage flags, need
551 * to recreate it now and copy contents -- or possibly create a
552 * gallium entrypoint to extend the usage flags and let the driver
553 * decide if a copy is necessary.
556 st_bufferobj_validate_usage(struct st_context
*st
,
557 struct st_buffer_object
*obj
,
564 st_init_bufferobject_functions(struct pipe_screen
*screen
,
565 struct dd_function_table
*functions
)
567 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
568 _mesa_init_buffer_object_functions(functions
);
570 functions
->NewBufferObject
= st_bufferobj_alloc
;
571 functions
->DeleteBuffer
= st_bufferobj_free
;
572 functions
->BufferData
= st_bufferobj_data
;
573 functions
->BufferSubData
= st_bufferobj_subdata
;
574 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
575 functions
->MapBufferRange
= st_bufferobj_map_range
;
576 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
577 functions
->UnmapBuffer
= st_bufferobj_unmap
;
578 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
579 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
581 if (screen
->get_param(screen
, PIPE_CAP_INVALIDATE_BUFFER
))
582 functions
->InvalidateBufferSubData
= st_bufferobj_invalidate
;