e0cb979f2313dcfa8b01540857e0ebbcb2fe2ef0
[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_READ:
250 case GL_STATIC_COPY:
251 default:
252 pipe_usage = PIPE_USAGE_DEFAULT;
253 break;
254 case GL_DYNAMIC_DRAW:
255 case GL_DYNAMIC_READ:
256 case GL_DYNAMIC_COPY:
257 pipe_usage = PIPE_USAGE_DYNAMIC;
258 break;
259 case GL_STREAM_DRAW:
260 case GL_STREAM_READ:
261 case GL_STREAM_COPY:
262 pipe_usage = PIPE_USAGE_STREAM;
263 break;
264 }
265 }
266
267 /* Set flags. */
268 if (storageFlags & GL_MAP_PERSISTENT_BIT)
269 pipe_flags |= PIPE_RESOURCE_FLAG_MAP_PERSISTENT;
270 if (storageFlags & GL_MAP_COHERENT_BIT)
271 pipe_flags |= PIPE_RESOURCE_FLAG_MAP_COHERENT;
272
273 pipe_resource_reference( &st_obj->buffer, NULL );
274
275 if (ST_DEBUG & DEBUG_BUFFER) {
276 debug_printf("Create buffer size %" PRId64 " bind 0x%x\n",
277 (int64_t) size, bind);
278 }
279
280 if (size != 0) {
281 struct pipe_resource buffer;
282
283 memset(&buffer, 0, sizeof buffer);
284 buffer.target = PIPE_BUFFER;
285 buffer.format = PIPE_FORMAT_R8_UNORM; /* want TYPELESS or similar */
286 buffer.bind = bind;
287 buffer.usage = pipe_usage;
288 buffer.flags = pipe_flags;
289 buffer.width0 = size;
290 buffer.height0 = 1;
291 buffer.depth0 = 1;
292 buffer.array_size = 1;
293
294 st_obj->buffer = pipe->screen->resource_create(pipe->screen, &buffer);
295
296 if (!st_obj->buffer) {
297 /* out of memory */
298 st_obj->Base.Size = 0;
299 return GL_FALSE;
300 }
301
302 if (data)
303 pipe_buffer_write(pipe, st_obj->buffer, 0, size, data);
304 }
305
306 /* BufferData may change an array or uniform buffer, need to update it */
307 st->dirty.st |= ST_NEW_VERTEX_ARRAYS | ST_NEW_UNIFORM_BUFFER;
308
309 return GL_TRUE;
310 }
311
312
313 /**
314 * Called via glMapBufferRange().
315 */
316 static void *
317 st_bufferobj_map_range(struct gl_context *ctx,
318 GLintptr offset, GLsizeiptr length, GLbitfield access,
319 struct gl_buffer_object *obj,
320 gl_map_buffer_index index)
321 {
322 struct pipe_context *pipe = st_context(ctx)->pipe;
323 struct st_buffer_object *st_obj = st_buffer_object(obj);
324 enum pipe_transfer_usage flags = 0x0;
325
326 if (access & GL_MAP_WRITE_BIT)
327 flags |= PIPE_TRANSFER_WRITE;
328
329 if (access & GL_MAP_READ_BIT)
330 flags |= PIPE_TRANSFER_READ;
331
332 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
333 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
334
335 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
336 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
337 }
338 else if (access & GL_MAP_INVALIDATE_RANGE_BIT) {
339 if (offset == 0 && length == obj->Size)
340 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
341 else
342 flags |= PIPE_TRANSFER_DISCARD_RANGE;
343 }
344
345 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
346 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
347
348 if (access & GL_MAP_PERSISTENT_BIT)
349 flags |= PIPE_TRANSFER_PERSISTENT;
350
351 if (access & GL_MAP_COHERENT_BIT)
352 flags |= PIPE_TRANSFER_COHERENT;
353
354 /* ... other flags ...
355 */
356
357 if (access & MESA_MAP_NOWAIT_BIT)
358 flags |= PIPE_TRANSFER_DONTBLOCK;
359
360 assert(offset >= 0);
361 assert(length >= 0);
362 assert(offset < obj->Size);
363 assert(offset + length <= obj->Size);
364
365 obj->Mappings[index].Pointer = pipe_buffer_map_range(pipe,
366 st_obj->buffer,
367 offset, length,
368 flags,
369 &st_obj->transfer[index]);
370 if (obj->Mappings[index].Pointer) {
371 obj->Mappings[index].Offset = offset;
372 obj->Mappings[index].Length = length;
373 obj->Mappings[index].AccessFlags = access;
374 }
375 else {
376 st_obj->transfer[index] = NULL;
377 }
378
379 return obj->Mappings[index].Pointer;
380 }
381
382
383 static void
384 st_bufferobj_flush_mapped_range(struct gl_context *ctx,
385 GLintptr offset, GLsizeiptr length,
386 struct gl_buffer_object *obj,
387 gl_map_buffer_index index)
388 {
389 struct pipe_context *pipe = st_context(ctx)->pipe;
390 struct st_buffer_object *st_obj = st_buffer_object(obj);
391
392 /* Subrange is relative to mapped range */
393 assert(offset >= 0);
394 assert(length >= 0);
395 assert(offset + length <= obj->Mappings[index].Length);
396 assert(obj->Mappings[index].Pointer);
397
398 if (!length)
399 return;
400
401 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer[index],
402 obj->Mappings[index].Offset + offset,
403 length);
404 }
405
406
407 /**
408 * Called via glUnmapBufferARB().
409 */
410 static GLboolean
411 st_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj,
412 gl_map_buffer_index index)
413 {
414 struct pipe_context *pipe = st_context(ctx)->pipe;
415 struct st_buffer_object *st_obj = st_buffer_object(obj);
416
417 if (obj->Mappings[index].Length)
418 pipe_buffer_unmap(pipe, st_obj->transfer[index]);
419
420 st_obj->transfer[index] = NULL;
421 obj->Mappings[index].Pointer = NULL;
422 obj->Mappings[index].Offset = 0;
423 obj->Mappings[index].Length = 0;
424 return GL_TRUE;
425 }
426
427
428 /**
429 * Called via glCopyBufferSubData().
430 */
431 static void
432 st_copy_buffer_subdata(struct gl_context *ctx,
433 struct gl_buffer_object *src,
434 struct gl_buffer_object *dst,
435 GLintptr readOffset, GLintptr writeOffset,
436 GLsizeiptr size)
437 {
438 struct pipe_context *pipe = st_context(ctx)->pipe;
439 struct st_buffer_object *srcObj = st_buffer_object(src);
440 struct st_buffer_object *dstObj = st_buffer_object(dst);
441 struct pipe_box box;
442
443 if (!size)
444 return;
445
446 /* buffer should not already be mapped */
447 assert(!_mesa_check_disallowed_mapping(src));
448 assert(!_mesa_check_disallowed_mapping(dst));
449
450 u_box_1d(readOffset, size, &box);
451
452 pipe->resource_copy_region(pipe, dstObj->buffer, 0, writeOffset, 0, 0,
453 srcObj->buffer, 0, &box);
454 }
455
456 /**
457 * Called via glClearBufferSubData().
458 */
459 static void
460 st_clear_buffer_subdata(struct gl_context *ctx,
461 GLintptr offset, GLsizeiptr size,
462 const GLvoid *clearValue,
463 GLsizeiptr clearValueSize,
464 struct gl_buffer_object *bufObj)
465 {
466 struct pipe_context *pipe = st_context(ctx)->pipe;
467 struct st_buffer_object *buf = st_buffer_object(bufObj);
468 static const char zeros[16] = {0};
469
470 if (!pipe->clear_buffer) {
471 _mesa_buffer_clear_subdata(ctx, offset, size,
472 clearValue, clearValueSize, bufObj);
473 return;
474 }
475
476 if (!clearValue)
477 clearValue = zeros;
478
479 pipe->clear_buffer(pipe, buf->buffer, offset, size,
480 clearValue, clearValueSize);
481 }
482
483
484 /* TODO: if buffer wasn't created with appropriate usage flags, need
485 * to recreate it now and copy contents -- or possibly create a
486 * gallium entrypoint to extend the usage flags and let the driver
487 * decide if a copy is necessary.
488 */
489 void
490 st_bufferobj_validate_usage(struct st_context *st,
491 struct st_buffer_object *obj,
492 unsigned usage)
493 {
494 }
495
496
497 void
498 st_init_bufferobject_functions(struct dd_function_table *functions)
499 {
500 /* plug in default driver fallbacks (such as for ClearBufferSubData) */
501 _mesa_init_buffer_object_functions(functions);
502
503 functions->NewBufferObject = st_bufferobj_alloc;
504 functions->DeleteBuffer = st_bufferobj_free;
505 functions->BufferData = st_bufferobj_data;
506 functions->BufferSubData = st_bufferobj_subdata;
507 functions->GetBufferSubData = st_bufferobj_get_subdata;
508 functions->MapBufferRange = st_bufferobj_map_range;
509 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
510 functions->UnmapBuffer = st_bufferobj_unmap;
511 functions->CopyBufferSubData = st_copy_buffer_subdata;
512 functions->ClearBufferSubData = st_clear_buffer_subdata;
513
514 /* For GL_APPLE_vertex_array_object */
515 functions->NewArrayObject = _mesa_new_vao;
516 functions->DeleteArrayObject = _mesa_delete_vao;
517 }