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 "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/arrayobj.h"
37 #include "main/bufferobj.h"
39 #include "st_context.h"
40 #include "st_cb_bufferobjects.h"
43 #include "pipe/p_context.h"
44 #include "pipe/p_defines.h"
45 #include "util/u_inlines.h"
49 * There is some duplication between mesa's bufferobjects and our
50 * bufmgr buffers. Both have an integer handle and a hashtable to
51 * lookup an opaque structure. It would be nice if the handles and
52 * internal structure where somehow shared.
54 static struct gl_buffer_object
*
55 st_bufferobj_alloc(struct gl_context
*ctx
, GLuint name
, GLenum target
)
57 struct st_buffer_object
*st_obj
= ST_CALLOC_STRUCT(st_buffer_object
);
62 _mesa_initialize_buffer_object(ctx
, &st_obj
->Base
, name
, target
);
70 * Deallocate/free a vertex/pixel buffer object.
71 * Called via glDeleteBuffersARB().
74 st_bufferobj_free(struct gl_context
*ctx
, struct gl_buffer_object
*obj
)
76 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
78 assert(obj
->RefCount
== 0);
79 _mesa_buffer_unmap_all_mappings(ctx
, obj
);
82 pipe_resource_reference(&st_obj
->buffer
, NULL
);
84 free(st_obj
->Base
.Label
);
91 * Replace data in a subrange of buffer object. If the data range
92 * specified by size + offset extends beyond the end of the buffer or
93 * if data is NULL, no copy is performed.
94 * Called via glBufferSubDataARB().
97 st_bufferobj_subdata(struct gl_context
*ctx
,
100 const GLvoid
* data
, struct gl_buffer_object
*obj
)
102 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
104 /* we may be called from VBO code, so double-check params here */
107 ASSERT(offset
+ size
<= obj
->Size
);
113 * According to ARB_vertex_buffer_object specification, if data is null,
114 * then the contents of the buffer object's data store is undefined. We just
115 * ignore, and leave it unchanged.
120 if (!st_obj
->buffer
) {
121 /* we probably ran out of memory during buffer allocation */
125 /* Now that transfers are per-context, we don't have to figure out
126 * flushing here. Usually drivers won't need to flush in this case
127 * even if the buffer is currently referenced by hardware - they
128 * just queue the upload as dma rather than mapping the underlying
131 pipe_buffer_write(st_context(ctx
)->pipe
,
138 * Called via glGetBufferSubDataARB().
141 st_bufferobj_get_subdata(struct gl_context
*ctx
,
144 GLvoid
* data
, struct gl_buffer_object
*obj
)
146 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
148 /* we may be called from VBO code, so double-check params here */
151 ASSERT(offset
+ size
<= obj
->Size
);
156 if (!st_obj
->buffer
) {
157 /* we probably ran out of memory during buffer allocation */
161 pipe_buffer_read(st_context(ctx
)->pipe
, st_obj
->buffer
,
167 * Allocate space for and store data in a buffer object. Any data that was
168 * previously stored in the buffer object is lost. If data is NULL,
169 * memory will be allocated, but no copy will occur.
170 * Called via ctx->Driver.BufferData().
171 * \return GL_TRUE for success, GL_FALSE if out of memory
174 st_bufferobj_data(struct gl_context
*ctx
,
179 GLbitfield storageFlags
,
180 struct gl_buffer_object
*obj
)
182 struct st_context
*st
= st_context(ctx
);
183 struct pipe_context
*pipe
= st
->pipe
;
184 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
185 unsigned bind
, pipe_usage
, pipe_flags
= 0;
187 if (size
&& data
&& st_obj
->buffer
&&
188 st_obj
->Base
.Size
== size
&&
189 st_obj
->Base
.Usage
== usage
&&
190 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.
197 u_box_1d(0, size
, &box
);
198 pipe
->transfer_inline_write(pipe
, st_obj
->buffer
, 0,
199 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
,
204 st_obj
->Base
.Size
= size
;
205 st_obj
->Base
.Usage
= usage
;
206 st_obj
->Base
.StorageFlags
= storageFlags
;
209 case GL_PIXEL_PACK_BUFFER_ARB
:
210 case GL_PIXEL_UNPACK_BUFFER_ARB
:
211 bind
= PIPE_BIND_RENDER_TARGET
| PIPE_BIND_SAMPLER_VIEW
;
213 case GL_ARRAY_BUFFER_ARB
:
214 bind
= PIPE_BIND_VERTEX_BUFFER
;
216 case GL_ELEMENT_ARRAY_BUFFER_ARB
:
217 bind
= PIPE_BIND_INDEX_BUFFER
;
219 case GL_TEXTURE_BUFFER
:
220 bind
= PIPE_BIND_SAMPLER_VIEW
;
222 case GL_TRANSFORM_FEEDBACK_BUFFER
:
223 bind
= PIPE_BIND_STREAM_OUTPUT
;
225 case GL_UNIFORM_BUFFER
:
226 bind
= PIPE_BIND_CONSTANT_BUFFER
;
233 if (st_obj
->Base
.Immutable
) {
235 if (storageFlags
& GL_CLIENT_STORAGE_BIT
)
236 pipe_usage
= PIPE_USAGE_STAGING
;
238 pipe_usage
= PIPE_USAGE_DEFAULT
;
247 pipe_usage
= PIPE_USAGE_DEFAULT
;
249 case GL_DYNAMIC_DRAW
:
250 case GL_DYNAMIC_READ
:
251 case GL_DYNAMIC_COPY
:
252 pipe_usage
= PIPE_USAGE_DYNAMIC
;
257 pipe_usage
= PIPE_USAGE_STREAM
;
263 if (storageFlags
& GL_MAP_PERSISTENT_BIT
)
264 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_PERSISTENT
;
265 if (storageFlags
& GL_MAP_COHERENT_BIT
)
266 pipe_flags
|= PIPE_RESOURCE_FLAG_MAP_COHERENT
;
268 pipe_resource_reference( &st_obj
->buffer
, NULL
);
270 if (ST_DEBUG
& DEBUG_BUFFER
) {
271 debug_printf("Create buffer size %td bind 0x%x\n", size
, bind
);
275 struct pipe_resource buffer
;
277 memset(&buffer
, 0, sizeof buffer
);
278 buffer
.target
= PIPE_BUFFER
;
279 buffer
.format
= PIPE_FORMAT_R8_UNORM
; /* want TYPELESS or similar */
281 buffer
.usage
= pipe_usage
;
282 buffer
.flags
= pipe_flags
;
283 buffer
.width0
= size
;
286 buffer
.array_size
= 1;
288 st_obj
->buffer
= pipe
->screen
->resource_create(pipe
->screen
, &buffer
);
290 if (!st_obj
->buffer
) {
292 st_obj
->Base
.Size
= 0;
297 pipe_buffer_write(pipe
, st_obj
->buffer
, 0, size
, data
);
300 /* BufferData may change an array or uniform buffer, need to update it */
301 st
->dirty
.st
|= ST_NEW_VERTEX_ARRAYS
| ST_NEW_UNIFORM_BUFFER
;
308 * Called via glMapBufferRange().
311 st_bufferobj_map_range(struct gl_context
*ctx
,
312 GLintptr offset
, GLsizeiptr length
, GLbitfield access
,
313 struct gl_buffer_object
*obj
,
314 gl_map_buffer_index index
)
316 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
317 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
318 enum pipe_transfer_usage flags
= 0x0;
320 if (access
& GL_MAP_WRITE_BIT
)
321 flags
|= PIPE_TRANSFER_WRITE
;
323 if (access
& GL_MAP_READ_BIT
)
324 flags
|= PIPE_TRANSFER_READ
;
326 if (access
& GL_MAP_FLUSH_EXPLICIT_BIT
)
327 flags
|= PIPE_TRANSFER_FLUSH_EXPLICIT
;
329 if (access
& GL_MAP_INVALIDATE_BUFFER_BIT
) {
330 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
332 else if (access
& GL_MAP_INVALIDATE_RANGE_BIT
) {
333 if (offset
== 0 && length
== obj
->Size
)
334 flags
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
336 flags
|= PIPE_TRANSFER_DISCARD_RANGE
;
339 if (access
& GL_MAP_UNSYNCHRONIZED_BIT
)
340 flags
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
342 if (access
& GL_MAP_PERSISTENT_BIT
)
343 flags
|= PIPE_TRANSFER_PERSISTENT
;
345 if (access
& GL_MAP_COHERENT_BIT
)
346 flags
|= PIPE_TRANSFER_COHERENT
;
348 /* ... other flags ...
351 if (access
& MESA_MAP_NOWAIT_BIT
)
352 flags
|= PIPE_TRANSFER_DONTBLOCK
;
356 assert(offset
< obj
->Size
);
357 assert(offset
+ length
<= obj
->Size
);
359 obj
->Mappings
[index
].Pointer
= pipe_buffer_map_range(pipe
,
363 &st_obj
->transfer
[index
]);
364 if (obj
->Mappings
[index
].Pointer
) {
365 obj
->Mappings
[index
].Offset
= offset
;
366 obj
->Mappings
[index
].Length
= length
;
367 obj
->Mappings
[index
].AccessFlags
= access
;
370 st_obj
->transfer
[index
] = NULL
;
373 return obj
->Mappings
[index
].Pointer
;
378 st_bufferobj_flush_mapped_range(struct gl_context
*ctx
,
379 GLintptr offset
, GLsizeiptr length
,
380 struct gl_buffer_object
*obj
,
381 gl_map_buffer_index index
)
383 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
384 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
386 /* Subrange is relative to mapped range */
389 assert(offset
+ length
<= obj
->Mappings
[index
].Length
);
390 assert(obj
->Mappings
[index
].Pointer
);
395 pipe_buffer_flush_mapped_range(pipe
, st_obj
->transfer
[index
],
396 obj
->Mappings
[index
].Offset
+ offset
,
402 * Called via glUnmapBufferARB().
405 st_bufferobj_unmap(struct gl_context
*ctx
, struct gl_buffer_object
*obj
,
406 gl_map_buffer_index index
)
408 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
409 struct st_buffer_object
*st_obj
= st_buffer_object(obj
);
411 if (obj
->Mappings
[index
].Length
)
412 pipe_buffer_unmap(pipe
, st_obj
->transfer
[index
]);
414 st_obj
->transfer
[index
] = NULL
;
415 obj
->Mappings
[index
].Pointer
= NULL
;
416 obj
->Mappings
[index
].Offset
= 0;
417 obj
->Mappings
[index
].Length
= 0;
423 * Called via glCopyBufferSubData().
426 st_copy_buffer_subdata(struct gl_context
*ctx
,
427 struct gl_buffer_object
*src
,
428 struct gl_buffer_object
*dst
,
429 GLintptr readOffset
, GLintptr writeOffset
,
432 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
433 struct st_buffer_object
*srcObj
= st_buffer_object(src
);
434 struct st_buffer_object
*dstObj
= st_buffer_object(dst
);
440 /* buffer should not already be mapped */
441 assert(!_mesa_check_disallowed_mapping(src
));
442 assert(!_mesa_check_disallowed_mapping(dst
));
444 u_box_1d(readOffset
, size
, &box
);
446 pipe
->resource_copy_region(pipe
, dstObj
->buffer
, 0, writeOffset
, 0, 0,
447 srcObj
->buffer
, 0, &box
);
451 * Called via glClearBufferSubData().
454 st_clear_buffer_subdata(struct gl_context
*ctx
,
455 GLintptr offset
, GLsizeiptr size
,
456 const GLvoid
*clearValue
,
457 GLsizeiptr clearValueSize
,
458 struct gl_buffer_object
*bufObj
)
460 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
461 struct st_buffer_object
*buf
= st_buffer_object(bufObj
);
462 static const char zeros
[16] = {0};
464 if (!pipe
->clear_buffer
) {
465 _mesa_buffer_clear_subdata(ctx
, offset
, size
,
466 clearValue
, clearValueSize
, bufObj
);
473 pipe
->clear_buffer(pipe
, buf
->buffer
, offset
, size
,
474 clearValue
, clearValueSize
);
478 /* TODO: if buffer wasn't created with appropriate usage flags, need
479 * to recreate it now and copy contents -- or possibly create a
480 * gallium entrypoint to extend the usage flags and let the driver
481 * decide if a copy is necessary.
484 st_bufferobj_validate_usage(struct st_context
*st
,
485 struct st_buffer_object
*obj
,
492 st_init_bufferobject_functions(struct dd_function_table
*functions
)
494 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
495 _mesa_init_buffer_object_functions(functions
);
497 functions
->NewBufferObject
= st_bufferobj_alloc
;
498 functions
->DeleteBuffer
= st_bufferobj_free
;
499 functions
->BufferData
= st_bufferobj_data
;
500 functions
->BufferSubData
= st_bufferobj_subdata
;
501 functions
->GetBufferSubData
= st_bufferobj_get_subdata
;
502 functions
->MapBufferRange
= st_bufferobj_map_range
;
503 functions
->FlushMappedBufferRange
= st_bufferobj_flush_mapped_range
;
504 functions
->UnmapBuffer
= st_bufferobj_unmap
;
505 functions
->CopyBufferSubData
= st_copy_buffer_subdata
;
506 functions
->ClearBufferSubData
= st_clear_buffer_subdata
;
508 /* For GL_APPLE_vertex_array_object */
509 functions
->NewArrayObject
= _mesa_new_vao
;
510 functions
->DeleteArrayObject
= _mesa_delete_vao
;