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