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 mtx_destroy(&st_obj
->Base
.Mutex
);
87 free(st_obj
->Base
.Label
);
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 GLvoid
* 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 GLvoid
* 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 * Allocate space for and store data in a buffer object. Any data that was
171 * previously stored in the buffer object is lost. If data is NULL,
172 * memory will be allocated, but no copy will occur.
173 * Called via ctx->Driver.BufferData().
174 * \return GL_TRUE for success, GL_FALSE if out of memory
177 st_bufferobj_data(struct gl_context
*ctx
,
182 GLbitfield storageFlags
,
183 struct gl_buffer_object
*obj
)
185 struct st_context
*st
= st_context(ctx
);
186 struct pipe_context
*pipe
= st
->pipe
;
187 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
188 unsigned bind
, pipe_usage
, pipe_flags
= 0;
190 if (target
!= GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
&&
191 size
&& data
&& st_obj
->buffer
&&
192 st_obj
->Base
.Size
== size
&&
193 st_obj
->Base
.Usage
== usage
&&
194 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.
201 u_box_1d(0, size
, &box
);
202 pipe
->transfer_inline_write(pipe
, st_obj
->buffer
, 0,
203 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
208 st_obj
->Base
.Size
= size
;
209 st_obj
->Base
.Usage
= usage
;
210 st_obj
->Base
.StorageFlags
= storageFlags
;
213 case GL_PIXEL_PACK_BUFFER_ARB
:
214 case GL_PIXEL_UNPACK_BUFFER_ARB
:
215 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
217 case GL_ARRAY_BUFFER_ARB
:
218 bind
= PIPE_BIND_VERTEX_BUFFER
;
220 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
221 bind
= PIPE_BIND_INDEX_BUFFER
;
223 case GL_TEXTURE_BUFFER
:
224 bind
= PIPE_BIND_SAMPLER_VIEW
;
226 case GL_TRANSFORM_FEEDBACK_BUFFER
:
227 bind
= PIPE_BIND_STREAM_OUTPUT
;
229 case GL_UNIFORM_BUFFER
:
230 bind
= PIPE_BIND_CONSTANT_BUFFER
;
232 case GL_DRAW_INDIRECT_BUFFER
:
233 bind
= PIPE_BIND_COMMAND_ARGS_BUFFER
;
240 if (st_obj
->Base
.Immutable
) {
242 if (storageFlags
& GL_CLIENT_STORAGE_BIT
)
243 pipe_usage
= PIPE_USAGE_STAGING
;
245 pipe_usage
= PIPE_USAGE_DEFAULT
;
253 pipe_usage
= PIPE_USAGE_DEFAULT
;
255 case GL_DYNAMIC_DRAW
:
256 case GL_DYNAMIC_COPY
:
257 pipe_usage
= PIPE_USAGE_DYNAMIC
;
261 /* XXX: Remove this test and fall-through when we have PBO unpacking
262 * acceleration. Right now, PBO unpacking is done by the CPU, so we
263 * have to make sure CPU reads are fast.
265 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
266 pipe_usage
= PIPE_USAGE_STREAM
;
271 case GL_DYNAMIC_READ
:
273 pipe_usage
= PIPE_USAGE_STAGING
;
279 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
280 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
281 if (storageFlags
& GL_MAP_COHERENT_BIT
)
282 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
284 pipe_resource_reference( &st_obj
->buffer
, NULL
);
286 if (ST_DEBUG
& DEBUG_BUFFER
) {
287 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
288 (int64_t) size
, bind
);
292 struct pipe_screen
*screen
= pipe
->screen
;
293 struct pipe_resource buffer
;
295 memset(&buffer
, 0, sizeof buffer
);
296 buffer
.target
= PIPE_BUFFER
;
297 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
299 buffer
.usage
= pipe_usage
;
300 buffer
.flags
= pipe_flags
;
301 buffer
.width0
= size
;
304 buffer
.array_size
= 1;
306 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
308 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
311 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
313 if (st_obj
->buffer
&& data
)
314 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
317 if (!st_obj
->buffer
) {
319 st_obj
->Base
.Size
= 0;
324 /* BufferData may change an array or uniform buffer, need to update it */
325 st
->dirty
.st
|= ST_NEW_VERTEX_ARRAYS
| ST_NEW_UNIFORM_BUFFER
;
332 * Called via glMapBufferRange().
335 st_bufferobj_map_range(struct gl_context
*ctx
,
336 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
337 struct gl_buffer_object
*obj
,
338 gl_map_buffer_index index
)
340 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
341 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
342 enum pipe_transfer_usage flags
= 0x0;
344 if (access
& GL_MAP_WRITE_BIT
)
345 flags
|= PIPE_TRANSFER_WRITE
;
347 if (access
& GL_MAP_READ_BIT
)
348 flags
|= PIPE_TRANSFER_READ
;
350 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
351 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
353 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
354 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
356 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
357 if (offset
== 0 && length
== obj
->Size
)
358 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
360 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
363 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
364 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
366 if (access
& GL_MAP_PERSISTENT_BIT
)
367 flags
|= PIPE_TRANSFER_PERSISTENT
;
369 if (access
& GL_MAP_COHERENT_BIT
)
370 flags
|= PIPE_TRANSFER_COHERENT
;
372 /* ... other flags ...
375 if (access
& MESA_MAP_NOWAIT_BIT
)
376 flags
|= PIPE_TRANSFER_DONTBLOCK
;
380 assert(offset
< obj
->Size
);
381 assert(offset
+ length
<= obj
->Size
);
383 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
387 &st_obj
->transfer
[index
]);
388 if (obj
->Mappings
[index
].Pointer
) {
389 obj
->Mappings
[index
].Offset
= offset
;
390 obj
->Mappings
[index
].Length
= length
;
391 obj
->Mappings
[index
].AccessFlags
= access
;
394 st_obj
->transfer
[index
] = NULL
;
397 return obj
->Mappings
[index
].Pointer
;
402 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
403 GLintptr offset
, GLsizeiptr length
,
404 struct gl_buffer_object
*obj
,
405 gl_map_buffer_index index
)
407 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
408 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
410 /* Subrange is relative to mapped range */
413 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
414 assert(obj
->Mappings
[index
].Pointer
);
419 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
420 obj
->Mappings
[index
].Offset
+ offset
,
426 * Called via glUnmapBufferARB().
429 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
430 gl_map_buffer_index index
)
432 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
433 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
435 if (obj
->Mappings
[index
].Length
)
436 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
438 st_obj
->transfer
[index
] = NULL
;
439 obj
->Mappings
[index
].Pointer
= NULL
;
440 obj
->Mappings
[index
].Offset
= 0;
441 obj
->Mappings
[index
].Length
= 0;
447 * Called via glCopyBufferSubData().
450 st_copy_buffer_subdata(struct gl_context
*ctx
,
451 struct gl_buffer_object
*src
,
452 struct gl_buffer_object
*dst
,
453 GLintptr readOffset
, GLintptr writeOffset
,
456 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
457 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
458 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
464 /* buffer should not already be mapped */
465 assert(!_mesa_check_disallowed_mapping(src
));
466 assert(!_mesa_check_disallowed_mapping(dst
));
468 u_box_1d(readOffset
, size
, &box
);
470 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
471 srcObj
->buffer
, 0, &box
);
475 * Called via glClearBufferSubData().
478 st_clear_buffer_subdata(struct gl_context
*ctx
,
479 GLintptr offset
, GLsizeiptr size
,
480 const GLvoid
*clearValue
,
481 GLsizeiptr clearValueSize
,
482 struct gl_buffer_object
*bufObj
)
484 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
485 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
486 static const char zeros
[16] = {0};
488 if (!pipe
->clear_buffer
) {
489 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
490 clearValue
, clearValueSize
, bufObj
);
497 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
498 clearValue
, clearValueSize
);
502 /* TODO: if buffer wasn't created with appropriate usage flags, need
503 * to recreate it now and copy contents -- or possibly create a
504 * gallium entrypoint to extend the usage flags and let the driver
505 * decide if a copy is necessary.
508 st_bufferobj_validate_usage(struct st_context
*st
,
509 struct st_buffer_object
*obj
,
516 st_init_bufferobject_functions(struct dd_function_table
*functions
)
518 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
519 _mesa_init_buffer_object_functions(functions
);
521 functions
->NewBufferObject
= st_bufferobj_alloc
;
522 functions
->DeleteBuffer
= st_bufferobj_free
;
523 functions
->BufferData
= st_bufferobj_data
;
524 functions
->BufferSubData
= st_bufferobj_subdata
;
525 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
526 functions
->MapBufferRange
= st_bufferobj_map_range
;
527 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
528 functions
->UnmapBuffer
= st_bufferobj_unmap
;
529 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
530 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;