st/mesa: add some simple buffer/draw debug code
[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 #include "st_debug.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(struct gl_context *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(ctx, &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(struct gl_context *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 assert(st_obj->transfer == NULL);
80
81 if (st_obj->buffer)
82 pipe_resource_reference(&st_obj->buffer, NULL);
83
84 free(st_obj);
85 }
86
87
88
89 /**
90 * Replace data in a subrange of buffer object. If the data range
91 * specified by size + offset extends beyond the end of the buffer or
92 * if data is NULL, no copy is performed.
93 * Called via glBufferSubDataARB().
94 */
95 static void
96 st_bufferobj_subdata(struct gl_context *ctx,
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 if (!st_obj->buffer) {
120 /* we probably ran out of memory during buffer allocation */
121 return;
122 }
123
124 /* Now that transfers are per-context, we don't have to figure out
125 * flushing here. Usually drivers won't need to flush in this case
126 * even if the buffer is currently referenced by hardware - they
127 * just queue the upload as dma rather than mapping the underlying
128 * buffer directly.
129 */
130 pipe_buffer_write(st_context(ctx)->pipe,
131 st_obj->buffer,
132 offset, size, data);
133 }
134
135
136 /**
137 * Called via glGetBufferSubDataARB().
138 */
139 static void
140 st_bufferobj_get_subdata(struct gl_context *ctx,
141 GLintptrARB offset,
142 GLsizeiptrARB size,
143 GLvoid * data, struct gl_buffer_object *obj)
144 {
145 struct st_buffer_object *st_obj = st_buffer_object(obj);
146
147 /* we may be called from VBO code, so double-check params here */
148 ASSERT(offset >= 0);
149 ASSERT(size >= 0);
150 ASSERT(offset + size <= obj->Size);
151
152 if (!size)
153 return;
154
155 if (!st_obj->buffer) {
156 /* we probably ran out of memory during buffer allocation */
157 return;
158 }
159
160 pipe_buffer_read(st_context(ctx)->pipe, st_obj->buffer,
161 offset, size, data);
162 }
163
164
165 /**
166 * Allocate space for and store data in a buffer object. Any data that was
167 * previously stored in the buffer object is lost. If data is NULL,
168 * memory will be allocated, but no copy will occur.
169 * Called via ctx->Driver.BufferData().
170 * \return GL_TRUE for success, GL_FALSE if out of memory
171 */
172 static GLboolean
173 st_bufferobj_data(struct gl_context *ctx,
174 GLenum target,
175 GLsizeiptrARB size,
176 const GLvoid * data,
177 GLenum usage,
178 struct gl_buffer_object *obj)
179 {
180 struct st_context *st = st_context(ctx);
181 struct pipe_context *pipe = st->pipe;
182 struct st_buffer_object *st_obj = st_buffer_object(obj);
183 unsigned bind, pipe_usage;
184
185 st_obj->Base.Size = size;
186 st_obj->Base.Usage = usage;
187
188 switch(target) {
189 case GL_PIXEL_PACK_BUFFER_ARB:
190 case GL_PIXEL_UNPACK_BUFFER_ARB:
191 bind = PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW;
192 break;
193 case GL_ARRAY_BUFFER_ARB:
194 bind = PIPE_BIND_VERTEX_BUFFER;
195 break;
196 case GL_ELEMENT_ARRAY_BUFFER_ARB:
197 bind = PIPE_BIND_INDEX_BUFFER;
198 break;
199 case GL_TEXTURE_BUFFER:
200 bind = PIPE_BIND_SAMPLER_VIEW;
201 break;
202 case GL_TRANSFORM_FEEDBACK_BUFFER:
203 bind = PIPE_BIND_STREAM_OUTPUT;
204 break;
205 case GL_UNIFORM_BUFFER:
206 bind = PIPE_BIND_CONSTANT_BUFFER;
207 break;
208 default:
209 bind = 0;
210 }
211
212 switch (usage) {
213 case GL_STATIC_DRAW:
214 case GL_STATIC_READ:
215 case GL_STATIC_COPY:
216 pipe_usage = PIPE_USAGE_STATIC;
217 break;
218 case GL_DYNAMIC_DRAW:
219 case GL_DYNAMIC_READ:
220 case GL_DYNAMIC_COPY:
221 pipe_usage = PIPE_USAGE_DYNAMIC;
222 break;
223 case GL_STREAM_DRAW:
224 case GL_STREAM_READ:
225 case GL_STREAM_COPY:
226 pipe_usage = PIPE_USAGE_STREAM;
227 break;
228 default:
229 pipe_usage = PIPE_USAGE_DEFAULT;
230 }
231
232 pipe_resource_reference( &st_obj->buffer, NULL );
233
234 if (ST_DEBUG & DEBUG_BUFFER) {
235 debug_printf("Create buffer size %lu bind 0x%x\n", size, bind);
236 }
237
238 if (size != 0) {
239 st_obj->buffer = pipe_buffer_create(pipe->screen, bind,
240 pipe_usage, size);
241
242 if (!st_obj->buffer) {
243 /* out of memory */
244 st_obj->Base.Size = 0;
245 return GL_FALSE;
246 }
247
248 if (data)
249 pipe_buffer_write(pipe, st_obj->buffer, 0, size, data);
250 return GL_TRUE;
251 }
252
253 return GL_TRUE;
254 }
255
256
257 /**
258 * Called via glMapBufferRange().
259 */
260 static void *
261 st_bufferobj_map_range(struct gl_context *ctx,
262 GLintptr offset, GLsizeiptr length, GLbitfield access,
263 struct gl_buffer_object *obj)
264 {
265 struct pipe_context *pipe = st_context(ctx)->pipe;
266 struct st_buffer_object *st_obj = st_buffer_object(obj);
267 enum pipe_transfer_usage flags = 0x0;
268
269 if (access & GL_MAP_WRITE_BIT)
270 flags |= PIPE_TRANSFER_WRITE;
271
272 if (access & GL_MAP_READ_BIT)
273 flags |= PIPE_TRANSFER_READ;
274
275 if (access & GL_MAP_FLUSH_EXPLICIT_BIT)
276 flags |= PIPE_TRANSFER_FLUSH_EXPLICIT;
277
278 if (access & GL_MAP_INVALIDATE_BUFFER_BIT) {
279 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
280 }
281 else if (access & GL_MAP_INVALIDATE_RANGE_BIT) {
282 if (offset == 0 && length == obj->Size)
283 flags |= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
284 else
285 flags |= PIPE_TRANSFER_DISCARD_RANGE;
286 }
287
288 if (access & GL_MAP_UNSYNCHRONIZED_BIT)
289 flags |= PIPE_TRANSFER_UNSYNCHRONIZED;
290
291 /* ... other flags ...
292 */
293
294 if (access & MESA_MAP_NOWAIT_BIT)
295 flags |= PIPE_TRANSFER_DONTBLOCK;
296
297 assert(offset >= 0);
298 assert(length >= 0);
299 assert(offset < obj->Size);
300 assert(offset + length <= obj->Size);
301
302 obj->Pointer = pipe_buffer_map_range(pipe,
303 st_obj->buffer,
304 offset, length,
305 flags,
306 &st_obj->transfer);
307 if (obj->Pointer) {
308 obj->Offset = offset;
309 obj->Length = length;
310 obj->AccessFlags = access;
311 }
312 else {
313 st_obj->transfer = NULL;
314 }
315
316 return obj->Pointer;
317 }
318
319
320 static void
321 st_bufferobj_flush_mapped_range(struct gl_context *ctx,
322 GLintptr offset, GLsizeiptr length,
323 struct gl_buffer_object *obj)
324 {
325 struct pipe_context *pipe = st_context(ctx)->pipe;
326 struct st_buffer_object *st_obj = st_buffer_object(obj);
327
328 /* Subrange is relative to mapped range */
329 assert(offset >= 0);
330 assert(length >= 0);
331 assert(offset + length <= obj->Length);
332 assert(obj->Pointer);
333
334 if (!length)
335 return;
336
337 pipe_buffer_flush_mapped_range(pipe, st_obj->transfer,
338 obj->Offset + offset, length);
339 }
340
341
342 /**
343 * Called via glUnmapBufferARB().
344 */
345 static GLboolean
346 st_bufferobj_unmap(struct gl_context *ctx, struct gl_buffer_object *obj)
347 {
348 struct pipe_context *pipe = st_context(ctx)->pipe;
349 struct st_buffer_object *st_obj = st_buffer_object(obj);
350
351 if (obj->Length)
352 pipe_buffer_unmap(pipe, st_obj->transfer);
353
354 st_obj->transfer = NULL;
355 obj->Pointer = NULL;
356 obj->Offset = 0;
357 obj->Length = 0;
358 return GL_TRUE;
359 }
360
361
362 /**
363 * Called via glCopyBufferSubData().
364 */
365 static void
366 st_copy_buffer_subdata(struct gl_context *ctx,
367 struct gl_buffer_object *src,
368 struct gl_buffer_object *dst,
369 GLintptr readOffset, GLintptr writeOffset,
370 GLsizeiptr size)
371 {
372 struct pipe_context *pipe = st_context(ctx)->pipe;
373 struct st_buffer_object *srcObj = st_buffer_object(src);
374 struct st_buffer_object *dstObj = st_buffer_object(dst);
375 struct pipe_box box;
376
377 if(!size)
378 return;
379
380 /* buffer should not already be mapped */
381 assert(!src->Pointer);
382 assert(!dst->Pointer);
383
384 u_box_1d(readOffset, size, &box);
385
386 pipe->resource_copy_region(pipe, dstObj->buffer, 0, writeOffset, 0, 0,
387 srcObj->buffer, 0, &box);
388 }
389
390
391 /* TODO: if buffer wasn't created with appropriate usage flags, need
392 * to recreate it now and copy contents -- or possibly create a
393 * gallium entrypoint to extend the usage flags and let the driver
394 * decide if a copy is necessary.
395 */
396 void
397 st_bufferobj_validate_usage(struct st_context *st,
398 struct st_buffer_object *obj,
399 unsigned usage)
400 {
401 }
402
403
404 void
405 st_init_bufferobject_functions(struct dd_function_table *functions)
406 {
407 functions->NewBufferObject = st_bufferobj_alloc;
408 functions->DeleteBuffer = st_bufferobj_free;
409 functions->BufferData = st_bufferobj_data;
410 functions->BufferSubData = st_bufferobj_subdata;
411 functions->GetBufferSubData = st_bufferobj_get_subdata;
412 functions->MapBufferRange = st_bufferobj_map_range;
413 functions->FlushMappedBufferRange = st_bufferobj_flush_mapped_range;
414 functions->UnmapBuffer = st_bufferobj_unmap;
415 functions->CopyBufferSubData = st_copy_buffer_subdata;
416
417 /* For GL_APPLE_vertex_array_object */
418 functions->NewArrayObject = _mesa_new_array_object;
419 functions->DeleteArrayObject = _mesa_delete_array_object;
420 }