st/mesa: Use PIPE_USAGE_STAGING for GL_STATIC/DYNAMIC/STREAM_READ buffers
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Functions for pixel buffer objects and vertex/element buffer objects.
31 */
32
33
34 #include <inttypes.h> /* for PRId64 macro */
35
36 #include "main/imports.h"
37 #include "main/mtypes.h"
38 #include "main/arrayobj.h"
39 #include "main/bufferobj.h"
40
41 #include "st_context.h"
42 #include "st_cb_bufferobjects.h"
43 #include "st_debug.h"
44
45 #include "pipe/p_context.h"
46 #include "pipe/p_defines.h"
47 #include "util/u_inlines.h"
48
49
50 /**
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.
55 */
56 static struct gl_buffer_object *
57 st_bufferobj_alloc(struct gl_context *ctx, GLuint name, GLenum target)
58 {
59 struct st_buffer_object *st_obj = ST_CALLOC_STRUCT(st_buffer_object);
60
61 if (!st_obj)
62 return NULL;
63
64 _mesa_initialize_buffer_object(ctx, &st_obj->Base, name, target);
65
66 return &st_obj->Base;
67 }
68
69
70
71 /**
72 * Deallocate/free a vertex/pixel buffer object.
73 * Called via glDeleteBuffersARB().
74 */
75 static void
76 st_bufferobj_free(struct gl_context *ctx, struct gl_buffer_object *obj)
77 {
78 struct st_buffer_object *st_obj = st_buffer_object(obj);
79
80 assert(obj->RefCount == 0);
81 _mesa_buffer_unmap_all_mappings(ctx, obj);
82
83 if (st_obj->buffer)
84 pipe_resource_reference(&st_obj->buffer, NULL);
85
86 free(st_obj->Base.Label);
87 free(st_obj);
88 }
89
90
91
92 /**
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().
97 */
98 static void
99 st_bufferobj_subdata(struct gl_context *ctx,
100 GLintptrARB offset,
101 GLsizeiptrARB size,
102 const GLvoid * data, struct gl_buffer_object *obj)
103 {
104 struct st_buffer_object *st_obj = st_buffer_object(obj);
105
106 /* we may be called from VBO code, so double-check params here */
107 ASSERT(offset >= 0);
108 ASSERT(size >= 0);
109 ASSERT(offset + size <= obj->Size);
110
111 if (!size)
112 return;
113
114 /*
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.
118 */
119 if (!data)
120 return;
121
122 if (!st_obj->buffer) {
123 /* we probably ran out of memory during buffer allocation */
124 return;
125 }
126
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
131 * buffer directly.
132 */
133 pipe_buffer_write(st_context(ctx)->pipe,
134 st_obj->buffer,
135 offset, size, data);
136 }
137
138
139 /**
140 * Called via glGetBufferSubDataARB().
141 */
142 static void
143 st_bufferobj_get_subdata(struct gl_context *ctx,
144 GLintptrARB offset,
145 GLsizeiptrARB size,
146 GLvoid * data, struct gl_buffer_object *obj)
147 {
148 struct st_buffer_object *st_obj = st_buffer_object(obj);
149
150 /* we may be called from VBO code, so double-check params here */
151 ASSERT(offset >= 0);
152 ASSERT(size >= 0);
153 ASSERT(offset + size <= obj->Size);
154
155 if (!size)
156 return;
157
158 if (!st_obj->buffer) {
159 /* we probably ran out of memory during buffer allocation */
160 return;
161 }
162
163 pipe_buffer_read(st_context(ctx)->pipe, st_obj->buffer,
164 offset, size, data);
165 }
166
167
168 /**
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
174 */
175 static GLboolean
176 st_bufferobj_data(struct gl_context *ctx,
177 GLenum target,
178 GLsizeiptrARB size,
179 const GLvoid * data,
180 GLenum usage,
181 GLbitfield storageFlags,
182 struct gl_buffer_object *obj)
183 {
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;
188
189 if (size && data && st_obj->buffer &&
190 st_obj->Base.Size == size &&
191 st_obj->Base.Usage == usage &&
192 st_obj->Base.StorageFlags == storageFlags) {
193 /* Just discard the old contents and write new data.
194 * This should be the same as creating a new buffer, but we avoid
195 * a lot of validation in Mesa.
196 */
197 struct pipe_box box;
198
199 u_box_1d(0, size, &box);
200 pipe->transfer_inline_write(pipe, st_obj->buffer, 0,
201 PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE,
202 &box, data, 0, 0);
203 return GL_TRUE;
204 }
205
206 st_obj->Base.Size = size;
207 st_obj->Base.Usage = usage;
208 st_obj->Base.StorageFlags = storageFlags;
209
210 switch (target) {
211 case GL_PIXEL_PACK_BUFFER_ARB:
212 case GL_PIXEL_UNPACK_BUFFER_ARB:
213 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
214 break;
215 case GL_ARRAY_BUFFER_ARB:
216 bind = PIPE_BIND_VERTEX_BUFFER;
217 break;
218 case GL_ELEMENT_ARRAY_BUFFER_ARB:
219 bind = PIPE_BIND_INDEX_BUFFER;
220 break;
221 case GL_TEXTURE_BUFFER:
222 bind = PIPE_BIND_SAMPLER_VIEW;
223 break;
224 case GL_TRANSFORM_FEEDBACK_BUFFER:
225 bind = PIPE_BIND_STREAM_OUTPUT;
226 break;
227 case GL_UNIFORM_BUFFER:
228 bind = PIPE_BIND_CONSTANT_BUFFER;
229 break;
230 case GL_DRAW_INDIRECT_BUFFER:
231 bind = PIPE_BIND_COMMAND_ARGS_BUFFER;
232 break;
233 default:
234 bind = 0;
235 }
236
237 /* Set usage. */
238 if (st_obj->Base.Immutable) {
239 /* BufferStorage */
240 if (storageFlags & GL_CLIENT_STORAGE_BIT)
241 pipe_usage = PIPE_USAGE_STAGING;
242 else
243 pipe_usage = PIPE_USAGE_DEFAULT;
244 }
245 else {
246 /* BufferData */
247 switch (usage) {
248 case GL_STATIC_DRAW:
249 case GL_STATIC_COPY:
250 default:
251 pipe_usage = PIPE_USAGE_DEFAULT;
252 break;
253 case GL_DYNAMIC_DRAW:
254 case GL_DYNAMIC_COPY:
255 pipe_usage = PIPE_USAGE_DYNAMIC;
256 break;
257 case GL_STREAM_DRAW:
258 case GL_STREAM_COPY:
259 pipe_usage = PIPE_USAGE_STREAM;
260 break;
261 case GL_STATIC_READ:
262 case GL_DYNAMIC_READ:
263 case GL_STREAM_READ:
264 pipe_usage = PIPE_USAGE_STAGING;
265 break;
266 }
267 }
268
269 /* Set flags. */
270 if (storageFlags & GL_MAP_PERSISTENT_BIT)
271 pipe_flags |= PIPE_RESOURCE_FLAG_MAP_PERSISTENT;
272 if (storageFlags & GL_MAP_COHERENT_BIT)
273 pipe_flags |= PIPE_RESOURCE_FLAG_MAP_COHERENT;
274
275 pipe_resource_reference( &st_obj->buffer, NULL );
276
277 if (ST_DEBUG & DEBUG_BUFFER) {
278 debug_printf("Create buffer size %" PRId64 " bind 0x%x\n",
279 (int64_t) size, bind);
280 }
281
282 if (size != 0) {
283 struct pipe_resource buffer;
284
285 memset(&buffer, 0, sizeof buffer);
286 buffer.target = PIPE_BUFFER;
287 buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
288 buffer.bind = bind;
289 buffer.usage = pipe_usage;
290 buffer.flags = pipe_flags;
291 buffer.width0 = size;
292 buffer.height0 = 1;
293 buffer.depth0 = 1;
294 buffer.array_size = 1;
295
296 st_obj->buffer = pipe->screen->resource_create(pipe->screen, &buffer);
297
298 if (!st_obj->buffer) {
299 /* out of memory */
300 st_obj->Base.Size = 0;
301 return GL_FALSE;
302 }
303
304 if (data)
305 pipe_buffer_write(pipe, st_obj->buffer, 0, size, data);
306 }
307
308 /* BufferData may change an array or uniform buffer, need to update it */
309 st->dirty.st |= ST_NEW_VERTEX_ARRAYS | ST_NEW_UNIFORM_BUFFER;
310
311 return GL_TRUE;
312 }
313
314
315 /**
316 * Called via glMapBufferRange().
317 */
318 static void *
319 st_bufferobj_map_range(struct gl_context *ctx,
320 GLintptr offset, GLsizeiptr length, GLbitfield access,
321 struct gl_buffer_object *obj,
322 gl_map_buffer_index index)
323 {
324 struct pipe_context *pipe = st_context(ctx)->pipe;
325 struct st_buffer_object *st_obj = st_buffer_object(obj);
326 enum pipe_transfer_usage flags = 0x0;
327
328 if (access & GL_MAP_WRITE_BIT)
329 flags |= PIPE_TRANSFER_WRITE;
330
331 if (access & GL_MAP_READ_BIT)
332 flags |= PIPE_TRANSFER_READ;
333
334 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
335 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
336
337 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
338 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
339 }
340 else if (access & GL_MAP_INVALIDATE_RANGE_BIT) {
341 if (offset == 0 && length == obj->Size)
342 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
343 else
344 flags |= PIPE_TRANSFER_DISCARD_RANGE;
345 }
346
347 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
348 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
349
350 if (access & GL_MAP_PERSISTENT_BIT)
351 flags |= PIPE_TRANSFER_PERSISTENT;
352
353 if (access & GL_MAP_COHERENT_BIT)
354 flags |= PIPE_TRANSFER_COHERENT;
355
356 /* ... other flags ...
357 */
358
359 if (access & MESA_MAP_NOWAIT_BIT)
360 flags |= PIPE_TRANSFER_DONTBLOCK;
361
362 assert(offset >= 0);
363 assert(length >= 0);
364 assert(offset < obj->Size);
365 assert(offset + length <= obj->Size);
366
367 obj->Mappings[index].Pointer = pipe_buffer_map_range(pipe,
368 st_obj->buffer,
369 offset, length,
370 flags,
371 &st_obj->transfer[index]);
372 if (obj->Mappings[index].Pointer) {
373 obj->Mappings[index].Offset = offset;
374 obj->Mappings[index].Length = length;
375 obj->Mappings[index].AccessFlags = access;
376 }
377 else {
378 st_obj->transfer[index] = NULL;
379 }
380
381 return obj->Mappings[index].Pointer;
382 }
383
384
385 static void
386 st_bufferobj_flush_mapped_range(struct gl_context *ctx,
387 GLintptr offset, GLsizeiptr length,
388 struct gl_buffer_object *obj,
389 gl_map_buffer_index index)
390 {
391 struct pipe_context *pipe = st_context(ctx)->pipe;
392 struct st_buffer_object *st_obj = st_buffer_object(obj);
393
394 /* Subrange is relative to mapped range */
395 assert(offset >= 0);
396 assert(length >= 0);
397 assert(offset + length <= obj->Mappings[index].Length);
398 assert(obj->Mappings[index].Pointer);
399
400 if (!length)
401 return;
402
403 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer[index],
404 obj->Mappings[index].Offset + offset,
405 length);
406 }
407
408
409 /**
410 * Called via glUnmapBufferARB().
411 */
412 static GLboolean
413 st_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj,
414 gl_map_buffer_index index)
415 {
416 struct pipe_context *pipe = st_context(ctx)->pipe;
417 struct st_buffer_object *st_obj = st_buffer_object(obj);
418
419 if (obj->Mappings[index].Length)
420 pipe_buffer_unmap(pipe, st_obj->transfer[index]);
421
422 st_obj->transfer[index] = NULL;
423 obj->Mappings[index].Pointer = NULL;
424 obj->Mappings[index].Offset = 0;
425 obj->Mappings[index].Length = 0;
426 return GL_TRUE;
427 }
428
429
430 /**
431 * Called via glCopyBufferSubData().
432 */
433 static void
434 st_copy_buffer_subdata(struct gl_context *ctx,
435 struct gl_buffer_object *src,
436 struct gl_buffer_object *dst,
437 GLintptr readOffset, GLintptr writeOffset,
438 GLsizeiptr size)
439 {
440 struct pipe_context *pipe = st_context(ctx)->pipe;
441 struct st_buffer_object *srcObj = st_buffer_object(src);
442 struct st_buffer_object *dstObj = st_buffer_object(dst);
443 struct pipe_box box;
444
445 if (!size)
446 return;
447
448 /* buffer should not already be mapped */
449 assert(!_mesa_check_disallowed_mapping(src));
450 assert(!_mesa_check_disallowed_mapping(dst));
451
452 u_box_1d(readOffset, size, &box);
453
454 pipe->resource_copy_region(pipe, dstObj->buffer, 0, writeOffset, 0, 0,
455 srcObj->buffer, 0, &box);
456 }
457
458 /**
459 * Called via glClearBufferSubData().
460 */
461 static void
462 st_clear_buffer_subdata(struct gl_context *ctx,
463 GLintptr offset, GLsizeiptr size,
464 const GLvoid *clearValue,
465 GLsizeiptr clearValueSize,
466 struct gl_buffer_object *bufObj)
467 {
468 struct pipe_context *pipe = st_context(ctx)->pipe;
469 struct st_buffer_object *buf = st_buffer_object(bufObj);
470 static const char zeros[16] = {0};
471
472 if (!pipe->clear_buffer) {
473 _mesa_buffer_clear_subdata(ctx, offset, size,
474 clearValue, clearValueSize, bufObj);
475 return;
476 }
477
478 if (!clearValue)
479 clearValue = zeros;
480
481 pipe->clear_buffer(pipe, buf->buffer, offset, size,
482 clearValue, clearValueSize);
483 }
484
485
486 /* TODO: if buffer wasn't created with appropriate usage flags, need
487 * to recreate it now and copy contents -- or possibly create a
488 * gallium entrypoint to extend the usage flags and let the driver
489 * decide if a copy is necessary.
490 */
491 void
492 st_bufferobj_validate_usage(struct st_context *st,
493 struct st_buffer_object *obj,
494 unsigned usage)
495 {
496 }
497
498
499 void
500 st_init_bufferobject_functions(struct dd_function_table *functions)
501 {
502 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
503 _mesa_init_buffer_object_functions(functions);
504
505 functions->NewBufferObject = st_bufferobj_alloc;
506 functions->DeleteBuffer = st_bufferobj_free;
507 functions->BufferData = st_bufferobj_data;
508 functions->BufferSubData = st_bufferobj_subdata;
509 functions->GetBufferSubData = st_bufferobj_get_subdata;
510 functions->MapBufferRange = st_bufferobj_map_range;
511 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
512 functions->UnmapBuffer = st_bufferobj_unmap;
513 functions->CopyBufferSubData = st_copy_buffer_subdata;
514 functions->ClearBufferSubData = st_clear_buffer_subdata;
515
516 /* For GL_APPLE_vertex_array_object */
517 functions->NewArrayObject = _mesa_new_vao;
518 functions->DeleteArrayObject = _mesa_delete_vao;
519 }