mesa: Remove target parameter from dd_function_table::BufferSubData
[mesa.git] / src / mesa / state_tracker / st_cb_bufferobjects.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
42 #include "pipe/p_context.h"
43 #include "pipe/p_defines.h"
44 #include "util/u_inlines.h"
45
46
47 /**
48 * There is some duplication between mesa's bufferobjects and our
49 * bufmgr buffers. Both have an integer handle and a hashtable to
50 * lookup an opaque structure. It would be nice if the handles and
51 * internal structure where somehow shared.
52 */
53 static struct gl_buffer_object *
54 st_bufferobj_alloc(struct gl_context *ctx, GLuint name, GLenum target)
55 {
56 struct st_buffer_object *st_obj = ST_CALLOC_STRUCT(st_buffer_object);
57
58 if (!st_obj)
59 return NULL;
60
61 _mesa_initialize_buffer_object(&st_obj->Base, name, target);
62
63 return &st_obj->Base;
64 }
65
66
67
68 /**
69 * Deallocate/free a vertex/pixel buffer object.
70 * Called via glDeleteBuffersARB().
71 */
72 static void
73 st_bufferobj_free(struct gl_context *ctx, struct gl_buffer_object *obj)
74 {
75 struct st_buffer_object *st_obj = st_buffer_object(obj);
76
77 assert(obj->RefCount == 0);
78 assert(st_obj->transfer == NULL);
79
80 if (st_obj->buffer)
81 pipe_resource_reference(&st_obj->buffer, NULL);
82
83 free(st_obj);
84 }
85
86
87
88 /**
89 * Replace data in a subrange of buffer object. If the data range
90 * specified by size + offset extends beyond the end of the buffer or
91 * if data is NULL, no copy is performed.
92 * Called via glBufferSubDataARB().
93 */
94 static void
95 st_bufferobj_subdata(struct gl_context *ctx,
96 GLintptrARB offset,
97 GLsizeiptrARB size,
98 const GLvoid * data, struct gl_buffer_object *obj)
99 {
100 struct st_buffer_object *st_obj = st_buffer_object(obj);
101
102 /* we may be called from VBO code, so double-check params here */
103 ASSERT(offset >= 0);
104 ASSERT(size >= 0);
105 ASSERT(offset + size <= obj->Size);
106
107 if (!size)
108 return;
109
110 /*
111 * According to ARB_vertex_buffer_object specification, if data is null,
112 * then the contents of the buffer object's data store is undefined. We just
113 * ignore, and leave it unchanged.
114 */
115 if (!data)
116 return;
117
118 /* Now that transfers are per-context, we don't have to figure out
119 * flushing here. Usually drivers won't need to flush in this case
120 * even if the buffer is currently referenced by hardware - they
121 * just queue the upload as dma rather than mapping the underlying
122 * buffer directly.
123 */
124 pipe_buffer_write(st_context(ctx)->pipe,
125 st_obj->buffer,
126 offset, size, data);
127 }
128
129
130 /**
131 * Called via glGetBufferSubDataARB().
132 */
133 static void
134 st_bufferobj_get_subdata(struct gl_context *ctx,
135 GLenum target,
136 GLintptrARB offset,
137 GLsizeiptrARB size,
138 GLvoid * data, struct gl_buffer_object *obj)
139 {
140 struct st_buffer_object *st_obj = st_buffer_object(obj);
141
142 /* we may be called from VBO code, so double-check params here */
143 ASSERT(offset >= 0);
144 ASSERT(size >= 0);
145 ASSERT(offset + size <= obj->Size);
146
147 if (!size)
148 return;
149
150 pipe_buffer_read(st_context(ctx)->pipe, st_obj->buffer,
151 offset, size, data);
152 }
153
154
155 /**
156 * Allocate space for and store data in a buffer object. Any data that was
157 * previously stored in the buffer object is lost. If data is NULL,
158 * memory will be allocated, but no copy will occur.
159 * Called via ctx->Driver.BufferData().
160 * \return GL_TRUE for success, GL_FALSE if out of memory
161 */
162 static GLboolean
163 st_bufferobj_data(struct gl_context *ctx,
164 GLenum target,
165 GLsizeiptrARB size,
166 const GLvoid * data,
167 GLenum usage,
168 struct gl_buffer_object *obj)
169 {
170 struct st_context *st = st_context(ctx);
171 struct pipe_context *pipe = st->pipe;
172 struct st_buffer_object *st_obj = st_buffer_object(obj);
173 unsigned bind, pipe_usage;
174
175 st_obj->Base.Size = size;
176 st_obj->Base.Usage = usage;
177
178 switch(target) {
179 case GL_PIXEL_PACK_BUFFER_ARB:
180 case GL_PIXEL_UNPACK_BUFFER_ARB:
181 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
182 break;
183 case GL_ARRAY_BUFFER_ARB:
184 bind = PIPE_BIND_VERTEX_BUFFER;
185 break;
186 case GL_ELEMENT_ARRAY_BUFFER_ARB:
187 bind = PIPE_BIND_INDEX_BUFFER;
188 break;
189 default:
190 bind = 0;
191 }
192
193 switch (usage) {
194 case GL_STATIC_DRAW:
195 case GL_STATIC_READ:
196 case GL_STATIC_COPY:
197 pipe_usage = PIPE_USAGE_STATIC;
198 break;
199 case GL_DYNAMIC_DRAW:
200 case GL_DYNAMIC_READ:
201 case GL_DYNAMIC_COPY:
202 pipe_usage = PIPE_USAGE_DYNAMIC;
203 break;
204 case GL_STREAM_DRAW:
205 case GL_STREAM_READ:
206 case GL_STREAM_COPY:
207 pipe_usage = PIPE_USAGE_STREAM;
208 break;
209 default:
210 pipe_usage = PIPE_USAGE_DEFAULT;
211 }
212
213 pipe_resource_reference( &st_obj->buffer, NULL );
214
215 if (size != 0) {
216 st_obj->buffer = pipe_buffer_create(pipe->screen, bind,
217 pipe_usage, size);
218
219 if (!st_obj->buffer) {
220 return GL_FALSE;
221 }
222
223 if (data)
224 pipe_buffer_write(pipe, st_obj->buffer, 0, size, data);
225 return GL_TRUE;
226 }
227
228 return GL_TRUE;
229 }
230
231
232 /**
233 * Dummy data whose's pointer is used for zero size buffers or ranges.
234 */
235 static long st_bufferobj_zero_length = 0;
236
237
238
239 /**
240 * Called via glMapBufferARB().
241 */
242 static void *
243 st_bufferobj_map(struct gl_context *ctx, GLenum access,
244 struct gl_buffer_object *obj)
245 {
246 struct st_buffer_object *st_obj = st_buffer_object(obj);
247 uint flags;
248
249 switch (access) {
250 case GL_WRITE_ONLY:
251 flags = PIPE_TRANSFER_WRITE;
252 break;
253 case GL_READ_ONLY:
254 flags = PIPE_TRANSFER_READ;
255 break;
256 case GL_READ_WRITE:
257 default:
258 flags = PIPE_TRANSFER_READ_WRITE;
259 break;
260 }
261
262 /* Handle zero-size buffers here rather than in drivers */
263 if (obj->Size == 0) {
264 obj->Pointer = &st_bufferobj_zero_length;
265 }
266 else {
267 obj->Pointer = pipe_buffer_map(st_context(ctx)->pipe,
268 st_obj->buffer,
269 flags,
270 &st_obj->transfer);
271 }
272
273 if (obj->Pointer) {
274 obj->Offset = 0;
275 obj->Length = obj->Size;
276 }
277 return obj->Pointer;
278 }
279
280
281 /**
282 * Called via glMapBufferRange().
283 */
284 static void *
285 st_bufferobj_map_range(struct gl_context *ctx, GLenum target,
286 GLintptr offset, GLsizeiptr length, GLbitfield access,
287 struct gl_buffer_object *obj)
288 {
289 struct pipe_context *pipe = st_context(ctx)->pipe;
290 struct st_buffer_object *st_obj = st_buffer_object(obj);
291 enum pipe_transfer_usage flags = 0x0;
292
293 if (access & GL_MAP_WRITE_BIT)
294 flags |= PIPE_TRANSFER_WRITE;
295
296 if (access & GL_MAP_READ_BIT)
297 flags |= PIPE_TRANSFER_READ;
298
299 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
300 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
301
302 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
303 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
304 }
305 else if (access & GL_MAP_INVALIDATE_RANGE_BIT) {
306 if (offset == 0 && length == obj->Size)
307 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
308 else
309 flags |= PIPE_TRANSFER_DISCARD_RANGE;
310 }
311
312 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
313 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
314
315 /* ... other flags ...
316 */
317
318 if (access & MESA_MAP_NOWAIT_BIT)
319 flags |= PIPE_TRANSFER_DONTBLOCK;
320
321 assert(offset >= 0);
322 assert(length >= 0);
323 assert(offset < obj->Size);
324 assert(offset + length <= obj->Size);
325
326 /*
327 * We go out of way here to hide the degenerate yet valid case of zero
328 * length range from the pipe driver.
329 */
330 if (!length) {
331 obj->Pointer = &st_bufferobj_zero_length;
332 }
333 else {
334 obj->Pointer = pipe_buffer_map_range(pipe,
335 st_obj->buffer,
336 offset, length,
337 flags,
338 &st_obj->transfer);
339 if (obj->Pointer) {
340 obj->Pointer = (ubyte *) obj->Pointer + offset;
341 }
342 }
343
344 if (obj->Pointer) {
345 obj->Offset = offset;
346 obj->Length = length;
347 obj->AccessFlags = access;
348 }
349
350 return obj->Pointer;
351 }
352
353
354 static void
355 st_bufferobj_flush_mapped_range(struct gl_context *ctx, GLenum target,
356 GLintptr offset, GLsizeiptr length,
357 struct gl_buffer_object *obj)
358 {
359 struct pipe_context *pipe = st_context(ctx)->pipe;
360 struct st_buffer_object *st_obj = st_buffer_object(obj);
361
362 /* Subrange is relative to mapped range */
363 assert(offset >= 0);
364 assert(length >= 0);
365 assert(offset + length <= obj->Length);
366 assert(obj->Pointer);
367
368 if (!length)
369 return;
370
371 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer,
372 obj->Offset + offset, length);
373 }
374
375
376 /**
377 * Called via glUnmapBufferARB().
378 */
379 static GLboolean
380 st_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj)
381 {
382 struct pipe_context *pipe = st_context(ctx)->pipe;
383 struct st_buffer_object *st_obj = st_buffer_object(obj);
384
385 if (obj->Length)
386 pipe_buffer_unmap(pipe, st_obj->transfer);
387
388 st_obj->transfer = NULL;
389 obj->Pointer = NULL;
390 obj->Offset = 0;
391 obj->Length = 0;
392 return GL_TRUE;
393 }
394
395
396 /**
397 * Called via glCopyBufferSubData().
398 */
399 static void
400 st_copy_buffer_subdata(struct gl_context *ctx,
401 struct gl_buffer_object *src,
402 struct gl_buffer_object *dst,
403 GLintptr readOffset, GLintptr writeOffset,
404 GLsizeiptr size)
405 {
406 struct pipe_context *pipe = st_context(ctx)->pipe;
407 struct st_buffer_object *srcObj = st_buffer_object(src);
408 struct st_buffer_object *dstObj = st_buffer_object(dst);
409 struct pipe_box box;
410
411 if(!size)
412 return;
413
414 /* buffer should not already be mapped */
415 assert(!src->Pointer);
416 assert(!dst->Pointer);
417
418 u_box_1d(readOffset, size, &box);
419
420 pipe->resource_copy_region(pipe, dstObj->buffer, 0, writeOffset, 0, 0,
421 srcObj->buffer, 0, &box);
422 }
423
424
425 /* TODO: if buffer wasn't created with appropriate usage flags, need
426 * to recreate it now and copy contents -- or possibly create a
427 * gallium entrypoint to extend the usage flags and let the driver
428 * decide if a copy is necessary.
429 */
430 void
431 st_bufferobj_validate_usage(struct st_context *st,
432 struct st_buffer_object *obj,
433 unsigned usage)
434 {
435 }
436
437
438 void
439 st_init_bufferobject_functions(struct dd_function_table *functions)
440 {
441 functions->NewBufferObject = st_bufferobj_alloc;
442 functions->DeleteBuffer = st_bufferobj_free;
443 functions->BufferData = st_bufferobj_data;
444 functions->BufferSubData = st_bufferobj_subdata;
445 functions->GetBufferSubData = st_bufferobj_get_subdata;
446 functions->MapBuffer = st_bufferobj_map;
447 functions->MapBufferRange = st_bufferobj_map_range;
448 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
449 functions->UnmapBuffer = st_bufferobj_unmap;
450 functions->CopyBufferSubData = st_copy_buffer_subdata;
451
452 /* For GL_APPLE_vertex_array_object */
453 functions->NewArrayObject = _mesa_new_array_object;
454 functions->DeleteArrayObject = _mesa_delete_array_object;
455 }