b55a085cc7c7cb936facea486ee55e9584dd8c88
[profile/ivi/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_inlines.h"
40 #include "st_context.h"
41 #include "st_cb_bufferobjects.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(GLcontext *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(&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(GLcontext *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
80    if (st_obj->buffer) 
81       pipe_buffer_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(GLcontext *ctx,
96                      GLenum target,
97                      GLintptrARB offset,
98                      GLsizeiptrARB size,
99                      const GLvoid * data, struct gl_buffer_object *obj)
100 {
101    struct st_buffer_object *st_obj = st_buffer_object(obj);
102
103    /* we may be called from VBO code, so double-check params here */
104    ASSERT(offset >= 0);
105    ASSERT(size >= 0);
106    ASSERT(offset + size <= obj->Size);
107
108    if (!size)
109       return;
110
111    /*
112     * According to ARB_vertex_buffer_object specification, if data is null,
113     * then the contents of the buffer object's data store is undefined. We just
114     * ignore, and leave it unchanged.
115     */
116    if (!data)
117       return;
118
119    st_cond_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer,
120                                    offset, size, data);
121 }
122
123
124 /**
125  * Called via glGetBufferSubDataARB().
126  */
127 static void
128 st_bufferobj_get_subdata(GLcontext *ctx,
129                          GLenum target,
130                          GLintptrARB offset,
131                          GLsizeiptrARB size,
132                          GLvoid * data, struct gl_buffer_object *obj)
133 {
134    struct st_buffer_object *st_obj = st_buffer_object(obj);
135
136    /* we may be called from VBO code, so double-check params here */
137    ASSERT(offset >= 0);
138    ASSERT(size >= 0);
139    ASSERT(offset + size <= obj->Size);
140
141    if (!size)
142       return;
143
144    st_cond_flush_pipe_buffer_read(st_context(ctx), st_obj->buffer,
145                                   offset, size, data);
146 }
147
148
149 /**
150  * Allocate space for and store data in a buffer object.  Any data that was
151  * previously stored in the buffer object is lost.  If data is NULL,
152  * memory will be allocated, but no copy will occur.
153  * Called via ctx->Driver.BufferData().
154  * \return GL_TRUE for success, GL_FALSE if out of memory
155  */
156 static GLboolean
157 st_bufferobj_data(GLcontext *ctx,
158                   GLenum target,
159                   GLsizeiptrARB size,
160                   const GLvoid * data,
161                   GLenum usage, 
162                   struct gl_buffer_object *obj)
163 {
164    struct st_context *st = st_context(ctx);
165    struct pipe_context *pipe = st->pipe;
166    struct st_buffer_object *st_obj = st_buffer_object(obj);
167    unsigned buffer_usage;
168
169    st_obj->Base.Size = size;
170    st_obj->Base.Usage = usage;
171    
172    switch(target) {
173    case GL_PIXEL_PACK_BUFFER_ARB:
174    case GL_PIXEL_UNPACK_BUFFER_ARB:
175       buffer_usage = PIPE_BUFFER_USAGE_PIXEL;
176       break;
177    case GL_ARRAY_BUFFER_ARB:
178       buffer_usage = PIPE_BUFFER_USAGE_VERTEX;
179       break;
180    case GL_ELEMENT_ARRAY_BUFFER_ARB:
181       buffer_usage = PIPE_BUFFER_USAGE_INDEX;
182       break;
183    default:
184       buffer_usage = 0;
185    }
186
187    pipe_buffer_reference( &st_obj->buffer, NULL );
188
189    if (size != 0) {
190       st_obj->buffer = pipe_buffer_create(pipe->screen, 32, buffer_usage, size);
191
192       if (!st_obj->buffer) {
193          return GL_FALSE;
194       }
195
196       if (data)
197          st_no_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer, 0,
198                                        size, data);
199       return GL_TRUE;
200    }
201
202    return GL_TRUE;
203 }
204
205
206 /**
207  * Called via glMapBufferARB().
208  */
209 static void *
210 st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
211                  struct gl_buffer_object *obj)
212 {
213    struct st_buffer_object *st_obj = st_buffer_object(obj);
214    uint flags;
215
216    switch (access) {
217    case GL_WRITE_ONLY:
218       flags = PIPE_BUFFER_USAGE_CPU_WRITE;
219       break;
220    case GL_READ_ONLY:
221       flags = PIPE_BUFFER_USAGE_CPU_READ;
222       break;
223    case GL_READ_WRITE:
224       /* fall-through */
225    default:
226       flags = PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
227       break;      
228    }
229
230    obj->Pointer = st_cond_flush_pipe_buffer_map(st_context(ctx),
231                                                 st_obj->buffer,
232                                                 flags);
233    if (obj->Pointer) {
234       obj->Offset = 0;
235       obj->Length = obj->Size;
236    }
237    return obj->Pointer;
238 }
239
240
241 /**
242  * Dummy data whose's pointer is used for zero length ranges.
243  */
244 static long
245 st_bufferobj_zero_length_range = 0;
246
247
248 /**
249  * Called via glMapBufferRange().
250  */
251 static void *
252 st_bufferobj_map_range(GLcontext *ctx, GLenum target, 
253                        GLintptr offset, GLsizeiptr length, GLbitfield access,
254                        struct gl_buffer_object *obj)
255 {
256    struct pipe_context *pipe = st_context(ctx)->pipe;
257    struct st_buffer_object *st_obj = st_buffer_object(obj);
258    uint flags = 0x0;
259
260    if (access & GL_MAP_WRITE_BIT)
261       flags |= PIPE_BUFFER_USAGE_CPU_WRITE;
262
263    if (access & GL_MAP_READ_BIT)
264       flags |= PIPE_BUFFER_USAGE_CPU_READ;
265
266    if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
267       flags |= PIPE_BUFFER_USAGE_FLUSH_EXPLICIT;
268    
269    if (access & GL_MAP_UNSYNCHRONIZED_BIT)
270       flags |= PIPE_BUFFER_USAGE_UNSYNCHRONIZED;
271
272    /* ... other flags ...
273     */
274
275    if (access & MESA_MAP_NOWAIT_BIT)
276       flags |= PIPE_BUFFER_USAGE_DONTBLOCK;
277
278    assert(offset >= 0);
279    assert(length >= 0);
280    assert(offset < obj->Size);
281    assert(offset + length <= obj->Size);
282
283    /*
284     * We go out of way here to hide the degenerate yet valid case of zero
285     * length range from the pipe driver.
286     */
287    if (!length) {
288       obj->Pointer = &st_bufferobj_zero_length_range;
289    }
290    else {
291       obj->Pointer = pipe_buffer_map_range(pipe->screen, st_obj->buffer, offset, length, flags);
292       if (obj->Pointer) {
293          obj->Pointer = (ubyte *) obj->Pointer + offset;
294       }
295    }
296    
297    if (obj->Pointer) {
298       obj->Offset = offset;
299       obj->Length = length;
300       obj->AccessFlags = access;
301    }
302
303    return obj->Pointer;
304 }
305
306
307 static void
308 st_bufferobj_flush_mapped_range(GLcontext *ctx, GLenum target, 
309                                 GLintptr offset, GLsizeiptr length,
310                                 struct gl_buffer_object *obj)
311 {
312    struct pipe_context *pipe = st_context(ctx)->pipe;
313    struct st_buffer_object *st_obj = st_buffer_object(obj);
314
315    /* Subrange is relative to mapped range */
316    assert(offset >= 0);
317    assert(length >= 0);
318    assert(offset + length <= obj->Length);
319    
320    if (!length)
321       return;
322
323    pipe_buffer_flush_mapped_range(pipe->screen, st_obj->buffer, 
324                                   obj->Offset + offset, length);
325 }
326
327
328 /**
329  * Called via glUnmapBufferARB().
330  */
331 static GLboolean
332 st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
333 {
334    struct pipe_context *pipe = st_context(ctx)->pipe;
335    struct st_buffer_object *st_obj = st_buffer_object(obj);
336
337    if(obj->Length)
338       pipe_buffer_unmap(pipe->screen, st_obj->buffer);
339
340    obj->Pointer = NULL;
341    obj->Offset = 0;
342    obj->Length = 0;
343    return GL_TRUE;
344 }
345
346
347 /**
348  * Called via glCopyBufferSubData().
349  */
350 static void
351 st_copy_buffer_subdata(GLcontext *ctx,
352                        struct gl_buffer_object *src,
353                        struct gl_buffer_object *dst,
354                        GLintptr readOffset, GLintptr writeOffset,
355                        GLsizeiptr size)
356 {
357    struct pipe_context *pipe = st_context(ctx)->pipe;
358    struct st_buffer_object *srcObj = st_buffer_object(src);
359    struct st_buffer_object *dstObj = st_buffer_object(dst);
360    ubyte *srcPtr, *dstPtr;
361
362    if(!size)
363       return;
364
365    /* buffer should not already be mapped */
366    assert(!src->Pointer);
367    assert(!dst->Pointer);
368
369    srcPtr = (ubyte *) pipe_buffer_map_range(pipe->screen,
370                                             srcObj->buffer,
371                                             readOffset, size,
372                                             PIPE_BUFFER_USAGE_CPU_READ);
373
374    dstPtr = (ubyte *) pipe_buffer_map_range(pipe->screen,
375                                             dstObj->buffer,
376                                             writeOffset, size,
377                                             PIPE_BUFFER_USAGE_CPU_WRITE);
378
379    if (srcPtr && dstPtr)
380       memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
381
382    pipe_buffer_unmap(pipe->screen, srcObj->buffer);
383    pipe_buffer_unmap(pipe->screen, dstObj->buffer);
384 }
385
386
387 void
388 st_init_bufferobject_functions(struct dd_function_table *functions)
389 {
390    functions->NewBufferObject = st_bufferobj_alloc;
391    functions->DeleteBuffer = st_bufferobj_free;
392    functions->BufferData = st_bufferobj_data;
393    functions->BufferSubData = st_bufferobj_subdata;
394    functions->GetBufferSubData = st_bufferobj_get_subdata;
395    functions->MapBuffer = st_bufferobj_map;
396    functions->MapBufferRange = st_bufferobj_map_range;
397    functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
398    functions->UnmapBuffer = st_bufferobj_unmap;
399    functions->CopyBufferSubData = st_copy_buffer_subdata;
400
401    /* For GL_APPLE_vertex_array_object */
402    functions->NewArrayObject = _mesa_new_array_object;
403    functions->DeleteArrayObject = _mesa_delete_array_object;
404 }