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 free(st_obj
->Base
.Label
);
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 GLvoid
* 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 GLvoid
* 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
,
169 * Allocate space for and store data in a buffer object. Any data that was
170 * previously stored in the buffer object is lost. If data is NULL,
171 * memory will be allocated, but no copy will occur.
172 * Called via ctx->Driver.BufferData().
173 * \return GL_TRUE for success, GL_FALSE if out of memory
176 st_bufferobj_data(struct gl_context
*ctx
,
181 GLbitfield storageFlags
,
182 struct gl_buffer_object
*obj
)
184 struct st_context
*st
= st_context(ctx
);
185 struct pipe_context
*pipe
= st
->pipe
;
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
&& data
&& st_obj
->buffer
&&
191 st_obj
->Base
.Size
== size
&&
192 st_obj
->Base
.Usage
== usage
&&
193 st_obj
->Base
.StorageFlags
== storageFlags
) {
194 /* Just discard the old contents and write new data.
195 * This should be the same as creating a new buffer, but we avoid
196 * a lot of validation in Mesa.
200 u_box_1d(0, size
, &box
);
201 pipe
->transfer_inline_write(pipe
, st_obj
->buffer
, 0,
202 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
207 st_obj
->Base
.Size
= size
;
208 st_obj
->Base
.Usage
= usage
;
209 st_obj
->Base
.StorageFlags
= storageFlags
;
212 case GL_PIXEL_PACK_BUFFER_ARB
:
213 case GL_PIXEL_UNPACK_BUFFER_ARB
:
214 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
216 case GL_ARRAY_BUFFER_ARB
:
217 bind
= PIPE_BIND_VERTEX_BUFFER
;
219 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
220 bind
= PIPE_BIND_INDEX_BUFFER
;
222 case GL_TEXTURE_BUFFER
:
223 bind
= PIPE_BIND_SAMPLER_VIEW
;
225 case GL_TRANSFORM_FEEDBACK_BUFFER
:
226 bind
= PIPE_BIND_STREAM_OUTPUT
;
228 case GL_UNIFORM_BUFFER
:
229 bind
= PIPE_BIND_CONSTANT_BUFFER
;
231 case GL_DRAW_INDIRECT_BUFFER
:
232 bind
= PIPE_BIND_COMMAND_ARGS_BUFFER
;
239 if (st_obj
->Base
.Immutable
) {
241 if (storageFlags
& GL_CLIENT_STORAGE_BIT
)
242 pipe_usage
= PIPE_USAGE_STAGING
;
244 pipe_usage
= PIPE_USAGE_DEFAULT
;
252 pipe_usage
= PIPE_USAGE_DEFAULT
;
254 case GL_DYNAMIC_DRAW
:
255 case GL_DYNAMIC_COPY
:
256 pipe_usage
= PIPE_USAGE_DYNAMIC
;
260 /* XXX: Remove this test and fall-through when we have PBO unpacking
261 * acceleration. Right now, PBO unpacking is done by the CPU, so we
262 * have to make sure CPU reads are fast.
264 if (target
!= GL_PIXEL_UNPACK_BUFFER_ARB
) {
265 pipe_usage
= PIPE_USAGE_STREAM
;
270 case GL_DYNAMIC_READ
:
272 pipe_usage
= PIPE_USAGE_STAGING
;
278 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
279 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
280 if (storageFlags
& GL_MAP_COHERENT_BIT
)
281 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
283 pipe_resource_reference( &st_obj
->buffer
, NULL
);
285 if (ST_DEBUG
& DEBUG_BUFFER
) {
286 debug_printf("Create buffer size %" PRId64
" bind 0x%x\n",
287 (int64_t) size
, bind
);
291 struct pipe_screen
*screen
= pipe
->screen
;
292 struct pipe_resource buffer
;
294 memset(&buffer
, 0, sizeof buffer
);
295 buffer
.target
= PIPE_BUFFER
;
296 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
298 buffer
.usage
= pipe_usage
;
299 buffer
.flags
= pipe_flags
;
300 buffer
.width0
= size
;
303 buffer
.array_size
= 1;
305 if (target
== GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD
) {
307 screen
->resource_from_user_memory(screen
, &buffer
, (void*)data
);
310 st_obj
->buffer
= screen
->resource_create(screen
, &buffer
);
312 if (st_obj
->buffer
&& data
)
313 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
316 if (!st_obj
->buffer
) {
318 st_obj
->Base
.Size
= 0;
323 /* BufferData may change an array or uniform buffer, need to update it */
324 st
->dirty
.st
|= ST_NEW_VERTEX_ARRAYS
| ST_NEW_UNIFORM_BUFFER
;
331 * Called via glMapBufferRange().
334 st_bufferobj_map_range(struct gl_context
*ctx
,
335 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
336 struct gl_buffer_object
*obj
,
337 gl_map_buffer_index index
)
339 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
340 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
341 enum pipe_transfer_usage flags
= 0x0;
343 if (access
& GL_MAP_WRITE_BIT
)
344 flags
|= PIPE_TRANSFER_WRITE
;
346 if (access
& GL_MAP_READ_BIT
)
347 flags
|= PIPE_TRANSFER_READ
;
349 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
350 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
352 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
353 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
355 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
356 if (offset
== 0 && length
== obj
->Size
)
357 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
359 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
362 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
363 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
365 if (access
& GL_MAP_PERSISTENT_BIT
)
366 flags
|= PIPE_TRANSFER_PERSISTENT
;
368 if (access
& GL_MAP_COHERENT_BIT
)
369 flags
|= PIPE_TRANSFER_COHERENT
;
371 /* ... other flags ...
374 if (access
& MESA_MAP_NOWAIT_BIT
)
375 flags
|= PIPE_TRANSFER_DONTBLOCK
;
379 assert(offset
< obj
->Size
);
380 assert(offset
+ length
<= obj
->Size
);
382 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
386 &st_obj
->transfer
[index
]);
387 if (obj
->Mappings
[index
].Pointer
) {
388 obj
->Mappings
[index
].Offset
= offset
;
389 obj
->Mappings
[index
].Length
= length
;
390 obj
->Mappings
[index
].AccessFlags
= access
;
393 st_obj
->transfer
[index
] = NULL
;
396 return obj
->Mappings
[index
].Pointer
;
401 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
402 GLintptr offset
, GLsizeiptr length
,
403 struct gl_buffer_object
*obj
,
404 gl_map_buffer_index index
)
406 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
407 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
409 /* Subrange is relative to mapped range */
412 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
413 assert(obj
->Mappings
[index
].Pointer
);
418 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
419 obj
->Mappings
[index
].Offset
+ offset
,
425 * Called via glUnmapBufferARB().
428 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
429 gl_map_buffer_index index
)
431 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
432 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
434 if (obj
->Mappings
[index
].Length
)
435 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
437 st_obj
->transfer
[index
] = NULL
;
438 obj
->Mappings
[index
].Pointer
= NULL
;
439 obj
->Mappings
[index
].Offset
= 0;
440 obj
->Mappings
[index
].Length
= 0;
446 * Called via glCopyBufferSubData().
449 st_copy_buffer_subdata(struct gl_context
*ctx
,
450 struct gl_buffer_object
*src
,
451 struct gl_buffer_object
*dst
,
452 GLintptr readOffset
, GLintptr writeOffset
,
455 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
456 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
457 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
463 /* buffer should not already be mapped */
464 assert(!_mesa_check_disallowed_mapping(src
));
465 assert(!_mesa_check_disallowed_mapping(dst
));
467 u_box_1d(readOffset
, size
, &box
);
469 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
470 srcObj
->buffer
, 0, &box
);
474 * Called via glClearBufferSubData().
477 st_clear_buffer_subdata(struct gl_context
*ctx
,
478 GLintptr offset
, GLsizeiptr size
,
479 const GLvoid
*clearValue
,
480 GLsizeiptr clearValueSize
,
481 struct gl_buffer_object
*bufObj
)
483 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
484 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
485 static const char zeros
[16] = {0};
487 if (!pipe
->clear_buffer
) {
488 _mesa_ClearBufferSubData_sw(ctx
, offset
, size
,
489 clearValue
, clearValueSize
, bufObj
);
496 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
497 clearValue
, clearValueSize
);
501 /* TODO: if buffer wasn't created with appropriate usage flags, need
502 * to recreate it now and copy contents -- or possibly create a
503 * gallium entrypoint to extend the usage flags and let the driver
504 * decide if a copy is necessary.
507 st_bufferobj_validate_usage(struct st_context
*st
,
508 struct st_buffer_object
*obj
,
515 st_init_bufferobject_functions(struct dd_function_table
*functions
)
517 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
518 _mesa_init_buffer_object_functions(functions
);
520 functions
->NewBufferObject
= st_bufferobj_alloc
;
521 functions
->DeleteBuffer
= st_bufferobj_free
;
522 functions
->BufferData
= st_bufferobj_data
;
523 functions
->BufferSubData
= st_bufferobj_subdata
;
524 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
525 functions
->MapBufferRange
= st_bufferobj_map_range
;
526 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
527 functions
->UnmapBuffer
= st_bufferobj_unmap
;
528 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
529 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;