Merge branch 'mesa_7_5_branch'
[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 "pipe/p_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 if (st_obj->buffer)
79 pipe_buffer_reference(&st_obj->buffer, NULL);
80
81 _mesa_free(st_obj);
82 }
83
84
85
86 /**
87 * Replace data in a subrange of buffer object. If the data range
88 * specified by size + offset extends beyond the end of the buffer or
89 * if data is NULL, no copy is performed.
90 * Called via glBufferSubDataARB().
91 */
92 static void
93 st_bufferobj_subdata(GLcontext *ctx,
94 GLenum target,
95 GLintptrARB offset,
96 GLsizeiptrARB size,
97 const GLvoid * data, struct gl_buffer_object *obj)
98 {
99 struct st_buffer_object *st_obj = st_buffer_object(obj);
100
101 /* we may be called from VBO code, so double-check params here */
102 ASSERT(offset >= 0);
103 ASSERT(size >= 0);
104 ASSERT(offset + size <= obj->Size);
105
106 st_cond_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer,
107 offset, size, data);
108 }
109
110
111 /**
112 * Called via glGetBufferSubDataARB().
113 */
114 static void
115 st_bufferobj_get_subdata(GLcontext *ctx,
116 GLenum target,
117 GLintptrARB offset,
118 GLsizeiptrARB size,
119 GLvoid * data, struct gl_buffer_object *obj)
120 {
121 struct st_buffer_object *st_obj = st_buffer_object(obj);
122
123 /* we may be called from VBO code, so double-check params here */
124 ASSERT(offset >= 0);
125 ASSERT(size >= 0);
126 ASSERT(offset + size <= obj->Size);
127
128 st_cond_flush_pipe_buffer_read(st_context(ctx), st_obj->buffer,
129 offset, size, data);
130 }
131
132
133 /**
134 * Allocate space for and store data in a buffer object. Any data that was
135 * previously stored in the buffer object is lost. If data is NULL,
136 * memory will be allocated, but no copy will occur.
137 * Called via glBufferDataARB().
138 */
139 static void
140 st_bufferobj_data(GLcontext *ctx,
141 GLenum target,
142 GLsizeiptrARB size,
143 const GLvoid * data,
144 GLenum usage,
145 struct gl_buffer_object *obj)
146 {
147 struct st_context *st = st_context(ctx);
148 struct pipe_context *pipe = st->pipe;
149 struct st_buffer_object *st_obj = st_buffer_object(obj);
150 unsigned buffer_usage;
151
152 st_obj->Base.Size = size;
153 st_obj->Base.Usage = usage;
154
155 switch(target) {
156 case GL_PIXEL_PACK_BUFFER_ARB:
157 case GL_PIXEL_UNPACK_BUFFER_ARB:
158 buffer_usage = PIPE_BUFFER_USAGE_PIXEL;
159 break;
160 case GL_ARRAY_BUFFER_ARB:
161 buffer_usage = PIPE_BUFFER_USAGE_VERTEX;
162 break;
163 case GL_ELEMENT_ARRAY_BUFFER_ARB:
164 buffer_usage = PIPE_BUFFER_USAGE_INDEX;
165 break;
166 default:
167 buffer_usage = 0;
168 }
169
170 pipe_buffer_reference( &st_obj->buffer, NULL );
171
172 st_obj->buffer = pipe_buffer_create( pipe->screen, 32, buffer_usage, size );
173
174 if (!st_obj->buffer) {
175 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBufferDataARB");
176 return;
177 }
178
179 if (data)
180 st_no_flush_pipe_buffer_write(st_context(ctx), st_obj->buffer, 0,
181 size, data);
182 }
183
184
185 /**
186 * Called via glMapBufferARB().
187 */
188 static void *
189 st_bufferobj_map(GLcontext *ctx, GLenum target, GLenum access,
190 struct gl_buffer_object *obj)
191 {
192 struct st_buffer_object *st_obj = st_buffer_object(obj);
193 uint flags;
194
195 switch (access) {
196 case GL_WRITE_ONLY:
197 flags = PIPE_BUFFER_USAGE_CPU_WRITE;
198 break;
199 case GL_READ_ONLY:
200 flags = PIPE_BUFFER_USAGE_CPU_READ;
201 break;
202 case GL_READ_WRITE:
203 /* fall-through */
204 default:
205 flags = PIPE_BUFFER_USAGE_CPU_READ | PIPE_BUFFER_USAGE_CPU_WRITE;
206 break;
207 }
208
209 obj->Pointer = st_cond_flush_pipe_buffer_map(st_context(ctx),
210 st_obj->buffer,
211 flags);
212 if (obj->Pointer) {
213 obj->Offset = 0;
214 obj->Length = obj->Size;
215 }
216 return obj->Pointer;
217 }
218
219
220 /**
221 * Called via glMapBufferRange().
222 */
223 static void *
224 st_bufferobj_map_range(GLcontext *ctx, GLenum target,
225 GLintptr offset, GLsizeiptr length, GLbitfield access,
226 struct gl_buffer_object *obj)
227 {
228 struct pipe_context *pipe = st_context(ctx)->pipe;
229 struct st_buffer_object *st_obj = st_buffer_object(obj);
230 uint flags = 0x0;
231 char *map;
232
233 if (access & GL_MAP_WRITE_BIT)
234 flags |= PIPE_BUFFER_USAGE_CPU_WRITE;
235
236 if (access & GL_MAP_READ_BIT)
237 flags |= PIPE_BUFFER_USAGE_CPU_READ;
238
239 /* ... other flags ...
240 */
241
242 if (access & MESA_MAP_NOWAIT_BIT)
243 flags |= PIPE_BUFFER_USAGE_DONTBLOCK;
244
245 assert(offset >= 0);
246 assert(length >= 0);
247 assert(offset < obj->Size);
248 assert(offset + length <= obj->Size);
249
250 map = obj->Pointer = pipe_buffer_map_range(pipe->screen, st_obj->buffer, offset, length, flags);
251 if(obj->Pointer) {
252 obj->Offset = offset;
253 obj->Length = length;
254 map += offset;
255 }
256
257 return map;
258 }
259
260
261 static void
262 st_bufferobj_flush_mapped_range(GLcontext *ctx, GLenum target,
263 GLintptr offset, GLsizeiptr length,
264 struct gl_buffer_object *obj)
265 {
266 struct pipe_context *pipe = st_context(ctx)->pipe;
267 struct st_buffer_object *st_obj = st_buffer_object(obj);
268
269 /* Subrange is relative to mapped range */
270 assert(offset >= 0);
271 assert(length >= 0);
272 assert(offset + length <= obj->Length);
273
274 pipe_buffer_flush_mapped_range(pipe->screen, st_obj->buffer,
275 obj->Offset + offset, length);
276 }
277
278
279 /**
280 * Called via glUnmapBufferARB().
281 */
282 static GLboolean
283 st_bufferobj_unmap(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
284 {
285 struct pipe_context *pipe = st_context(ctx)->pipe;
286 struct st_buffer_object *st_obj = st_buffer_object(obj);
287
288 pipe_buffer_unmap(pipe->screen, st_obj->buffer);
289 obj->Pointer = NULL;
290 obj->Offset = 0;
291 obj->Length = 0;
292 return GL_TRUE;
293 }
294
295
296 /**
297 * Called via glCopyBufferSubData().
298 */
299 static void
300 st_copy_buffer_subdata(GLcontext *ctx,
301 struct gl_buffer_object *src,
302 struct gl_buffer_object *dst,
303 GLintptr readOffset, GLintptr writeOffset,
304 GLsizeiptr size)
305 {
306 struct pipe_context *pipe = st_context(ctx)->pipe;
307 struct st_buffer_object *srcObj = st_buffer_object(src);
308 struct st_buffer_object *dstObj = st_buffer_object(dst);
309 ubyte *srcPtr, *dstPtr;
310
311 /* buffer should not already be mapped */
312 assert(!src->Pointer);
313 assert(!dst->Pointer);
314
315 srcPtr = (ubyte *) pipe_buffer_map_range(pipe->screen,
316 srcObj->buffer,
317 readOffset, size,
318 PIPE_BUFFER_USAGE_CPU_READ);
319
320 dstPtr = (ubyte *) pipe_buffer_map_range(pipe->screen,
321 dstObj->buffer,
322 writeOffset, size,
323 PIPE_BUFFER_USAGE_CPU_WRITE);
324
325 if (srcPtr && dstPtr)
326 _mesa_memcpy(dstPtr + writeOffset, srcPtr + readOffset, size);
327
328 pipe_buffer_unmap(pipe->screen, srcObj->buffer);
329 pipe_buffer_unmap(pipe->screen, dstObj->buffer);
330 }
331
332
333 void
334 st_init_bufferobject_functions(struct dd_function_table *functions)
335 {
336 functions->NewBufferObject = st_bufferobj_alloc;
337 functions->DeleteBuffer = st_bufferobj_free;
338 functions->BufferData = st_bufferobj_data;
339 functions->BufferSubData = st_bufferobj_subdata;
340 functions->GetBufferSubData = st_bufferobj_get_subdata;
341 functions->MapBuffer = st_bufferobj_map;
342 functions->MapBufferRange = st_bufferobj_map_range;
343 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
344 functions->UnmapBuffer = st_bufferobj_unmap;
345 functions->CopyBufferSubData = st_copy_buffer_subdata;
346
347 /* For GL_APPLE_vertex_array_object */
348 functions->NewArrayObject = _mesa_new_array_object;
349 functions->DeleteArrayObject = _mesa_delete_array_object;
350 }