mesa: Remove target parameter from dd_function_table::MapBufferRange
[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 GLintptrARB offset,
136 GLsizeiptrARB size,
137 GLvoid * data, struct gl_buffer_object *obj)
138 {
139 struct st_buffer_object *st_obj = st_buffer_object(obj);
140
141 /* we may be called from VBO code, so double-check params here */
142 ASSERT(offset >= 0);
143 ASSERT(size >= 0);
144 ASSERT(offset + size <= obj->Size);
145
146 if (!size)
147 return;
148
149 pipe_buffer_read(st_context(ctx)->pipe, st_obj->buffer,
150 offset, size, data);
151 }
152
153
154 /**
155 * Allocate space for and store data in a buffer object. Any data that was
156 * previously stored in the buffer object is lost. If data is NULL,
157 * memory will be allocated, but no copy will occur.
158 * Called via ctx->Driver.BufferData().
159 * \return GL_TRUE for success, GL_FALSE if out of memory
160 */
161 static GLboolean
162 st_bufferobj_data(struct gl_context *ctx,
163 GLenum target,
164 GLsizeiptrARB size,
165 const GLvoid * data,
166 GLenum usage,
167 struct gl_buffer_object *obj)
168 {
169 struct st_context *st = st_context(ctx);
170 struct pipe_context *pipe = st->pipe;
171 struct st_buffer_object *st_obj = st_buffer_object(obj);
172 unsigned bind, pipe_usage;
173
174 st_obj->Base.Size = size;
175 st_obj->Base.Usage = usage;
176
177 switch(target) {
178 case GL_PIXEL_PACK_BUFFER_ARB:
179 case GL_PIXEL_UNPACK_BUFFER_ARB:
180 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
181 break;
182 case GL_ARRAY_BUFFER_ARB:
183 bind = PIPE_BIND_VERTEX_BUFFER;
184 break;
185 case GL_ELEMENT_ARRAY_BUFFER_ARB:
186 bind = PIPE_BIND_INDEX_BUFFER;
187 break;
188 default:
189 bind = 0;
190 }
191
192 switch (usage) {
193 case GL_STATIC_DRAW:
194 case GL_STATIC_READ:
195 case GL_STATIC_COPY:
196 pipe_usage = PIPE_USAGE_STATIC;
197 break;
198 case GL_DYNAMIC_DRAW:
199 case GL_DYNAMIC_READ:
200 case GL_DYNAMIC_COPY:
201 pipe_usage = PIPE_USAGE_DYNAMIC;
202 break;
203 case GL_STREAM_DRAW:
204 case GL_STREAM_READ:
205 case GL_STREAM_COPY:
206 pipe_usage = PIPE_USAGE_STREAM;
207 break;
208 default:
209 pipe_usage = PIPE_USAGE_DEFAULT;
210 }
211
212 pipe_resource_reference( &st_obj->buffer, NULL );
213
214 if (size != 0) {
215 st_obj->buffer = pipe_buffer_create(pipe->screen, bind,
216 pipe_usage, size);
217
218 if (!st_obj->buffer) {
219 return GL_FALSE;
220 }
221
222 if (data)
223 pipe_buffer_write(pipe, st_obj->buffer, 0, size, data);
224 return GL_TRUE;
225 }
226
227 return GL_TRUE;
228 }
229
230
231 /**
232 * Dummy data whose's pointer is used for zero size buffers or ranges.
233 */
234 static long st_bufferobj_zero_length = 0;
235
236
237
238 /**
239 * Called via glMapBufferARB().
240 */
241 static void *
242 st_bufferobj_map(struct gl_context *ctx, GLenum access,
243 struct gl_buffer_object *obj)
244 {
245 struct st_buffer_object *st_obj = st_buffer_object(obj);
246 uint flags;
247
248 switch (access) {
249 case GL_WRITE_ONLY:
250 flags = PIPE_TRANSFER_WRITE;
251 break;
252 case GL_READ_ONLY:
253 flags = PIPE_TRANSFER_READ;
254 break;
255 case GL_READ_WRITE:
256 default:
257 flags = PIPE_TRANSFER_READ_WRITE;
258 break;
259 }
260
261 /* Handle zero-size buffers here rather than in drivers */
262 if (obj->Size == 0) {
263 obj->Pointer = &st_bufferobj_zero_length;
264 }
265 else {
266 obj->Pointer = pipe_buffer_map(st_context(ctx)->pipe,
267 st_obj->buffer,
268 flags,
269 &st_obj->transfer);
270 }
271
272 if (obj->Pointer) {
273 obj->Offset = 0;
274 obj->Length = obj->Size;
275 }
276 return obj->Pointer;
277 }
278
279
280 /**
281 * Called via glMapBufferRange().
282 */
283 static void *
284 st_bufferobj_map_range(struct gl_context *ctx,
285 GLintptr offset, GLsizeiptr length, GLbitfield access,
286 struct gl_buffer_object *obj)
287 {
288 struct pipe_context *pipe = st_context(ctx)->pipe;
289 struct st_buffer_object *st_obj = st_buffer_object(obj);
290 enum pipe_transfer_usage flags = 0x0;
291
292 if (access & GL_MAP_WRITE_BIT)
293 flags |= PIPE_TRANSFER_WRITE;
294
295 if (access & GL_MAP_READ_BIT)
296 flags |= PIPE_TRANSFER_READ;
297
298 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
299 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
300
301 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
302 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
303 }
304 else if (access & GL_MAP_INVALIDATE_RANGE_BIT) {
305 if (offset == 0 && length == obj->Size)
306 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
307 else
308 flags |= PIPE_TRANSFER_DISCARD_RANGE;
309 }
310
311 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
312 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
313
314 /* ... other flags ...
315 */
316
317 if (access & MESA_MAP_NOWAIT_BIT)
318 flags |= PIPE_TRANSFER_DONTBLOCK;
319
320 assert(offset >= 0);
321 assert(length >= 0);
322 assert(offset < obj->Size);
323 assert(offset + length <= obj->Size);
324
325 /*
326 * We go out of way here to hide the degenerate yet valid case of zero
327 * length range from the pipe driver.
328 */
329 if (!length) {
330 obj->Pointer = &st_bufferobj_zero_length;
331 }
332 else {
333 obj->Pointer = pipe_buffer_map_range(pipe,
334 st_obj->buffer,
335 offset, length,
336 flags,
337 &st_obj->transfer);
338 if (obj->Pointer) {
339 obj->Pointer = (ubyte *) obj->Pointer + offset;
340 }
341 }
342
343 if (obj->Pointer) {
344 obj->Offset = offset;
345 obj->Length = length;
346 obj->AccessFlags = access;
347 }
348
349 return obj->Pointer;
350 }
351
352
353 static void
354 st_bufferobj_flush_mapped_range(struct gl_context *ctx, GLenum target,
355 GLintptr offset, GLsizeiptr length,
356 struct gl_buffer_object *obj)
357 {
358 struct pipe_context *pipe = st_context(ctx)->pipe;
359 struct st_buffer_object *st_obj = st_buffer_object(obj);
360
361 /* Subrange is relative to mapped range */
362 assert(offset >= 0);
363 assert(length >= 0);
364 assert(offset + length <= obj->Length);
365 assert(obj->Pointer);
366
367 if (!length)
368 return;
369
370 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer,
371 obj->Offset + offset, length);
372 }
373
374
375 /**
376 * Called via glUnmapBufferARB().
377 */
378 static GLboolean
379 st_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj)
380 {
381 struct pipe_context *pipe = st_context(ctx)->pipe;
382 struct st_buffer_object *st_obj = st_buffer_object(obj);
383
384 if (obj->Length)
385 pipe_buffer_unmap(pipe, st_obj->transfer);
386
387 st_obj->transfer = NULL;
388 obj->Pointer = NULL;
389 obj->Offset = 0;
390 obj->Length = 0;
391 return GL_TRUE;
392 }
393
394
395 /**
396 * Called via glCopyBufferSubData().
397 */
398 static void
399 st_copy_buffer_subdata(struct gl_context *ctx,
400 struct gl_buffer_object *src,
401 struct gl_buffer_object *dst,
402 GLintptr readOffset, GLintptr writeOffset,
403 GLsizeiptr size)
404 {
405 struct pipe_context *pipe = st_context(ctx)->pipe;
406 struct st_buffer_object *srcObj = st_buffer_object(src);
407 struct st_buffer_object *dstObj = st_buffer_object(dst);
408 struct pipe_box box;
409
410 if(!size)
411 return;
412
413 /* buffer should not already be mapped */
414 assert(!src->Pointer);
415 assert(!dst->Pointer);
416
417 u_box_1d(readOffset, size, &box);
418
419 pipe->resource_copy_region(pipe, dstObj->buffer, 0, writeOffset, 0, 0,
420 srcObj->buffer, 0, &box);
421 }
422
423
424 /* TODO: if buffer wasn't created with appropriate usage flags, need
425 * to recreate it now and copy contents -- or possibly create a
426 * gallium entrypoint to extend the usage flags and let the driver
427 * decide if a copy is necessary.
428 */
429 void
430 st_bufferobj_validate_usage(struct st_context *st,
431 struct st_buffer_object *obj,
432 unsigned usage)
433 {
434 }
435
436
437 void
438 st_init_bufferobject_functions(struct dd_function_table *functions)
439 {
440 functions->NewBufferObject = st_bufferobj_alloc;
441 functions->DeleteBuffer = st_bufferobj_free;
442 functions->BufferData = st_bufferobj_data;
443 functions->BufferSubData = st_bufferobj_subdata;
444 functions->GetBufferSubData = st_bufferobj_get_subdata;
445 functions->MapBuffer = st_bufferobj_map;
446 functions->MapBufferRange = st_bufferobj_map_range;
447 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
448 functions->UnmapBuffer = st_bufferobj_unmap;
449 functions->CopyBufferSubData = st_copy_buffer_subdata;
450
451 /* For GL_APPLE_vertex_array_object */
452 functions->NewArrayObject = _mesa_new_array_object;
453 functions->DeleteArrayObject = _mesa_delete_array_object;
454 }