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