glx: Don't destroy DRI2 drawables for legacy glx drawables
[mesa.git] / src / mesa / vbo / vbo_exec_array.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/api_validate.h"
33 #include "main/varray.h"
34 #include "main/bufferobj.h"
35 #include "main/enums.h"
36 #include "main/macros.h"
37
38 #include "vbo_context.h"
39
40
41 /**
42 * Compute min and max elements by scanning the index buffer for
43 * glDraw[Range]Elements() calls.
44 */
45 void
46 vbo_get_minmax_index(GLcontext *ctx,
47 const struct _mesa_prim *prim,
48 const struct _mesa_index_buffer *ib,
49 GLuint *min_index, GLuint *max_index)
50 {
51 GLuint i;
52 GLuint count = prim->count;
53 const void *indices;
54
55 if (_mesa_is_bufferobj(ib->obj)) {
56 const GLvoid *map =
57 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB,
58 GL_READ_ONLY, ib->obj);
59 indices = ADD_POINTERS(map, ib->ptr);
60 } else {
61 indices = ib->ptr;
62 }
63
64 switch (ib->type) {
65 case GL_UNSIGNED_INT: {
66 const GLuint *ui_indices = (const GLuint *)indices;
67 GLuint max_ui = ui_indices[count-1];
68 GLuint min_ui = ui_indices[0];
69 for (i = 0; i < count; i++) {
70 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
71 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
72 }
73 *min_index = min_ui;
74 *max_index = max_ui;
75 break;
76 }
77 case GL_UNSIGNED_SHORT: {
78 const GLushort *us_indices = (const GLushort *)indices;
79 GLuint max_us = us_indices[count-1];
80 GLuint min_us = us_indices[0];
81 for (i = 0; i < count; i++) {
82 if (us_indices[i] > max_us) max_us = us_indices[i];
83 if (us_indices[i] < min_us) min_us = us_indices[i];
84 }
85 *min_index = min_us;
86 *max_index = max_us;
87 break;
88 }
89 case GL_UNSIGNED_BYTE: {
90 const GLubyte *ub_indices = (const GLubyte *)indices;
91 GLuint max_ub = ub_indices[count-1];
92 GLuint min_ub = ub_indices[0];
93 for (i = 0; i < count; i++) {
94 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
95 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
96 }
97 *min_index = min_ub;
98 *max_index = max_ub;
99 break;
100 }
101 default:
102 assert(0);
103 break;
104 }
105
106 if (_mesa_is_bufferobj(ib->obj)) {
107 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, ib->obj);
108 }
109 }
110
111
112 /**
113 * Check that element 'j' of the array has reasonable data.
114 * Map VBO if needed.
115 * For debugging purposes; not normally used.
116 */
117 static void
118 check_array_data(GLcontext *ctx, struct gl_client_array *array,
119 GLuint attrib, GLuint j)
120 {
121 if (array->Enabled) {
122 const void *data = array->Ptr;
123 if (_mesa_is_bufferobj(array->BufferObj)) {
124 if (!array->BufferObj->Pointer) {
125 /* need to map now */
126 array->BufferObj->Pointer =
127 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
128 GL_READ_ONLY, array->BufferObj);
129 }
130 data = ADD_POINTERS(data, array->BufferObj->Pointer);
131 }
132 switch (array->Type) {
133 case GL_FLOAT:
134 {
135 GLfloat *f = (GLfloat *) ((GLubyte *) data + array->StrideB * j);
136 GLint k;
137 for (k = 0; k < array->Size; k++) {
138 if (IS_INF_OR_NAN(f[k]) ||
139 f[k] >= 1.0e20 || f[k] <= -1.0e10) {
140 printf("Bad array data:\n");
141 printf(" Element[%u].%u = %f\n", j, k, f[k]);
142 printf(" Array %u at %p\n", attrib, (void* ) array);
143 printf(" Type 0x%x, Size %d, Stride %d\n",
144 array->Type, array->Size, array->Stride);
145 printf(" Address/offset %p in Buffer Object %u\n",
146 array->Ptr, array->BufferObj->Name);
147 f[k] = 1.0; /* XXX replace the bad value! */
148 }
149 /*assert(!IS_INF_OR_NAN(f[k]));*/
150 }
151 }
152 break;
153 default:
154 ;
155 }
156 }
157 }
158
159
160 /**
161 * Unmap the buffer object referenced by given array, if mapped.
162 */
163 static void
164 unmap_array_buffer(GLcontext *ctx, struct gl_client_array *array)
165 {
166 if (array->Enabled &&
167 _mesa_is_bufferobj(array->BufferObj) &&
168 _mesa_bufferobj_mapped(array->BufferObj)) {
169 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, array->BufferObj);
170 }
171 }
172
173
174 /**
175 * Examine the array's data for NaNs, etc.
176 * For debug purposes; not normally used.
177 */
178 static void
179 check_draw_elements_data(GLcontext *ctx, GLsizei count, GLenum elemType,
180 const void *elements, GLint basevertex)
181 {
182 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
183 const void *elemMap;
184 GLint i, k;
185
186 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
187 elemMap = ctx->Driver.MapBuffer(ctx,
188 GL_ELEMENT_ARRAY_BUFFER_ARB,
189 GL_READ_ONLY,
190 ctx->Array.ElementArrayBufferObj);
191 elements = ADD_POINTERS(elements, elemMap);
192 }
193
194 for (i = 0; i < count; i++) {
195 GLuint j;
196
197 /* j = element[i] */
198 switch (elemType) {
199 case GL_UNSIGNED_BYTE:
200 j = ((const GLubyte *) elements)[i];
201 break;
202 case GL_UNSIGNED_SHORT:
203 j = ((const GLushort *) elements)[i];
204 break;
205 case GL_UNSIGNED_INT:
206 j = ((const GLuint *) elements)[i];
207 break;
208 default:
209 assert(0);
210 }
211
212 /* check element j of each enabled array */
213 check_array_data(ctx, &arrayObj->Vertex, VERT_ATTRIB_POS, j);
214 check_array_data(ctx, &arrayObj->Normal, VERT_ATTRIB_NORMAL, j);
215 check_array_data(ctx, &arrayObj->Color, VERT_ATTRIB_COLOR0, j);
216 check_array_data(ctx, &arrayObj->SecondaryColor, VERT_ATTRIB_COLOR1, j);
217 for (k = 0; k < Elements(arrayObj->TexCoord); k++) {
218 check_array_data(ctx, &arrayObj->TexCoord[k], VERT_ATTRIB_TEX0 + k, j);
219 }
220 for (k = 0; k < Elements(arrayObj->VertexAttrib); k++) {
221 check_array_data(ctx, &arrayObj->VertexAttrib[k],
222 VERT_ATTRIB_GENERIC0 + k, j);
223 }
224 }
225
226 if (_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj)) {
227 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB,
228 ctx->Array.ElementArrayBufferObj);
229 }
230
231 unmap_array_buffer(ctx, &arrayObj->Vertex);
232 unmap_array_buffer(ctx, &arrayObj->Normal);
233 unmap_array_buffer(ctx, &arrayObj->Color);
234 for (k = 0; k < Elements(arrayObj->TexCoord); k++) {
235 unmap_array_buffer(ctx, &arrayObj->TexCoord[k]);
236 }
237 for (k = 0; k < Elements(arrayObj->VertexAttrib); k++) {
238 unmap_array_buffer(ctx, &arrayObj->VertexAttrib[k]);
239 }
240 }
241
242
243 /**
244 * Check array data, looking for NaNs, etc.
245 */
246 static void
247 check_draw_arrays_data(GLcontext *ctx, GLint start, GLsizei count)
248 {
249 /* TO DO */
250 }
251
252
253 /**
254 * Print info/data for glDrawArrays(), for debugging.
255 */
256 static void
257 print_draw_arrays(GLcontext *ctx, struct vbo_exec_context *exec,
258 GLenum mode, GLint start, GLsizei count)
259 {
260 int i;
261
262 printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
263 mode, start, count);
264
265 for (i = 0; i < 32; i++) {
266 GLuint bufName = exec->array.inputs[i]->BufferObj->Name;
267 GLint stride = exec->array.inputs[i]->Stride;
268 printf("attr %2d: size %d stride %d enabled %d "
269 "ptr %p Bufobj %u\n",
270 i,
271 exec->array.inputs[i]->Size,
272 stride,
273 /*exec->array.inputs[i]->Enabled,*/
274 exec->array.legacy_array[i]->Enabled,
275 exec->array.inputs[i]->Ptr,
276 bufName);
277
278 if (bufName) {
279 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, bufName);
280 GLubyte *p = ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
281 GL_READ_ONLY_ARB, buf);
282 int offset = (int) (GLintptr) exec->array.inputs[i]->Ptr;
283 float *f = (float *) (p + offset);
284 int *k = (int *) f;
285 int i;
286 int n = (count * stride) / 4;
287 if (n > 32)
288 n = 32;
289 printf(" Data at offset %d:\n", offset);
290 for (i = 0; i < n; i++) {
291 printf(" float[%d] = 0x%08x %f\n", i, k[i], f[i]);
292 }
293 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, buf);
294 }
295 }
296 }
297
298
299 /**
300 * Bind the VBO executor to the current vertex array object prior
301 * to drawing.
302 *
303 * Just translate the arrayobj into a sane layout.
304 */
305 static void
306 bind_array_obj(GLcontext *ctx)
307 {
308 struct vbo_context *vbo = vbo_context(ctx);
309 struct vbo_exec_context *exec = &vbo->exec;
310 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
311 GLuint i;
312
313 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
314 * rather than as individual named arrays. Then this function can
315 * go away.
316 */
317 exec->array.legacy_array[VERT_ATTRIB_POS] = &arrayObj->Vertex;
318 exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &arrayObj->Weight;
319 exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &arrayObj->Normal;
320 exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &arrayObj->Color;
321 exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &arrayObj->SecondaryColor;
322 exec->array.legacy_array[VERT_ATTRIB_FOG] = &arrayObj->FogCoord;
323 exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &arrayObj->Index;
324 if (arrayObj->PointSize.Enabled) {
325 /* this aliases COLOR_INDEX */
326 exec->array.legacy_array[VERT_ATTRIB_POINT_SIZE] = &arrayObj->PointSize;
327 }
328 exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &arrayObj->EdgeFlag;
329
330 for (i = 0; i < Elements(arrayObj->TexCoord); i++)
331 exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &arrayObj->TexCoord[i];
332
333 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
334 assert(i < Elements(exec->array.generic_array));
335 exec->array.generic_array[i] = &arrayObj->VertexAttrib[i];
336 }
337
338 exec->array.array_obj = arrayObj->Name;
339 }
340
341
342 /**
343 * Set the vbo->exec->inputs[] pointers to point to the enabled
344 * vertex arrays. This depends on the current vertex program/shader
345 * being executed because of whether or not generic vertex arrays
346 * alias the conventional vertex arrays.
347 * For arrays that aren't enabled, we set the input[attrib] pointer
348 * to point at a zero-stride current value "array".
349 */
350 static void
351 recalculate_input_bindings(GLcontext *ctx)
352 {
353 struct vbo_context *vbo = vbo_context(ctx);
354 struct vbo_exec_context *exec = &vbo->exec;
355 const struct gl_client_array **inputs = &exec->array.inputs[0];
356 GLbitfield const_inputs = 0x0;
357 GLuint i;
358
359 exec->array.program_mode = get_program_mode(ctx);
360 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
361
362 switch (exec->array.program_mode) {
363 case VP_NONE:
364 /* When no vertex program is active (or the vertex program is generated
365 * from fixed-function state). We put the material values into the
366 * generic slots. This is the only situation where material values
367 * are available as per-vertex attributes.
368 */
369 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
370 if (exec->array.legacy_array[i]->Enabled)
371 inputs[i] = exec->array.legacy_array[i];
372 else {
373 inputs[i] = &vbo->legacy_currval[i];
374 const_inputs |= 1 << i;
375 }
376 }
377
378 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
379 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i];
380 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
381 }
382
383 /* Could use just about anything, just to fill in the empty
384 * slots:
385 */
386 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX - VERT_ATTRIB_GENERIC0; i++) {
387 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
388 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
389 }
390 break;
391
392 case VP_NV:
393 /* NV_vertex_program - attribute arrays alias and override
394 * conventional, legacy arrays. No materials, and the generic
395 * slots are vacant.
396 */
397 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
398 if (exec->array.generic_array[i]->Enabled)
399 inputs[i] = exec->array.generic_array[i];
400 else if (exec->array.legacy_array[i]->Enabled)
401 inputs[i] = exec->array.legacy_array[i];
402 else {
403 inputs[i] = &vbo->legacy_currval[i];
404 const_inputs |= 1 << i;
405 }
406 }
407
408 /* Could use just about anything, just to fill in the empty
409 * slots:
410 */
411 for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++) {
412 inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0];
413 const_inputs |= 1 << i;
414 }
415 break;
416
417 case VP_ARB:
418 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
419 * attribute array aliases and overrides the legacy position array.
420 *
421 * Otherwise, legacy attributes available in the legacy slots,
422 * generic attributes in the generic slots and materials are not
423 * available as per-vertex attributes.
424 */
425 if (exec->array.generic_array[0]->Enabled)
426 inputs[0] = exec->array.generic_array[0];
427 else if (exec->array.legacy_array[0]->Enabled)
428 inputs[0] = exec->array.legacy_array[0];
429 else {
430 inputs[0] = &vbo->legacy_currval[0];
431 const_inputs |= 1 << 0;
432 }
433
434 for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
435 if (exec->array.legacy_array[i]->Enabled)
436 inputs[i] = exec->array.legacy_array[i];
437 else {
438 inputs[i] = &vbo->legacy_currval[i];
439 const_inputs |= 1 << i;
440 }
441 }
442
443 for (i = 0; i < MAX_VERTEX_GENERIC_ATTRIBS; i++) {
444 if (exec->array.generic_array[i]->Enabled)
445 inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
446 else {
447 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
448 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
449 }
450
451 }
452 break;
453 }
454
455 _mesa_set_varying_vp_inputs( ctx, ~const_inputs );
456 }
457
458
459 /**
460 * Examine the enabled vertex arrays to set the exec->array.inputs[] values.
461 * These will point to the arrays to actually use for drawing. Some will
462 * be user-provided arrays, other will be zero-stride const-valued arrays.
463 * Note that this might set the _NEW_ARRAY dirty flag so state validation
464 * must be done after this call.
465 */
466 static void
467 bind_arrays(GLcontext *ctx)
468 {
469 bind_array_obj(ctx);
470 recalculate_input_bindings(ctx);
471 }
472
473
474
475 /**
476 * Called from glDrawArrays when in immediate mode (not display list mode).
477 */
478 static void GLAPIENTRY
479 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
480 {
481 GET_CURRENT_CONTEXT(ctx);
482 struct vbo_context *vbo = vbo_context(ctx);
483 struct vbo_exec_context *exec = &vbo->exec;
484 struct _mesa_prim prim[1];
485
486 if (MESA_VERBOSE & VERBOSE_DRAW)
487 _mesa_debug(ctx, "glDrawArrays(%s, %d, %d)\n",
488 _mesa_lookup_enum_by_nr(mode), start, count);
489
490 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
491 return;
492
493 vbo_exec_FlushVertices_internal( ctx, GL_FALSE );
494
495 FLUSH_CURRENT( ctx, 0 );
496
497 if (!_mesa_valid_to_render(ctx, "glDrawArrays")) {
498 return;
499 }
500
501 #if 0
502 check_draw_arrays_data(ctx, start, count);
503 #else
504 (void) check_draw_arrays_data;
505 #endif
506
507 bind_arrays( ctx );
508
509 /* Again... because we may have changed the bitmask of per-vertex varying
510 * attributes. If we regenerate the fixed-function vertex program now
511 * we may be able to prune down the number of vertex attributes which we
512 * need in the shader.
513 */
514 if (ctx->NewState)
515 _mesa_update_state( ctx );
516
517 prim[0].begin = 1;
518 prim[0].end = 1;
519 prim[0].weak = 0;
520 prim[0].pad = 0;
521 prim[0].mode = mode;
522 prim[0].start = start;
523 prim[0].count = count;
524 prim[0].indexed = 0;
525 prim[0].basevertex = 0;
526 prim[0].num_instances = 1;
527
528 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL,
529 GL_TRUE, start, start + count - 1 );
530
531 #if 0
532 print_draw_arrays(ctx, exec, mode, start, count);
533 #else
534 (void) print_draw_arrays;
535 #endif
536 }
537
538
539 /**
540 * Called from glDrawArraysInstanced when in immediate mode (not
541 * display list mode).
542 */
543 static void GLAPIENTRY
544 vbo_exec_DrawArraysInstanced(GLenum mode, GLint start, GLsizei count,
545 GLsizei primcount)
546 {
547 GET_CURRENT_CONTEXT(ctx);
548 struct vbo_context *vbo = vbo_context(ctx);
549 struct vbo_exec_context *exec = &vbo->exec;
550 struct _mesa_prim prim[1];
551
552 if (MESA_VERBOSE & VERBOSE_DRAW)
553 _mesa_debug(ctx, "glDrawArraysInstanced(%s, %d, %d, %d)\n",
554 _mesa_lookup_enum_by_nr(mode), start, count, primcount);
555
556 if (!_mesa_validate_DrawArraysInstanced(ctx, mode, start, count, primcount))
557 return;
558
559 FLUSH_CURRENT( ctx, 0 );
560
561 if (!_mesa_valid_to_render(ctx, "glDrawArraysInstanced")) {
562 return;
563 }
564
565 #if 0 /* debug */
566 check_draw_arrays_data(ctx, start, count);
567 #endif
568
569 bind_arrays( ctx );
570
571 /* Again... because we may have changed the bitmask of per-vertex varying
572 * attributes. If we regenerate the fixed-function vertex program now
573 * we may be able to prune down the number of vertex attributes which we
574 * need in the shader.
575 */
576 if (ctx->NewState)
577 _mesa_update_state( ctx );
578
579 prim[0].begin = 1;
580 prim[0].end = 1;
581 prim[0].weak = 0;
582 prim[0].pad = 0;
583 prim[0].mode = mode;
584 prim[0].start = start;
585 prim[0].count = count;
586 prim[0].indexed = 0;
587 prim[0].basevertex = 0;
588 prim[0].num_instances = primcount;
589
590 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL,
591 GL_TRUE, start, start + count - 1 );
592
593 #if 0 /* debug */
594 print_draw_arrays(ctx, exec, mode, start, count);
595 #endif
596 }
597
598
599 /**
600 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
601 * For debugging.
602 */
603 static void
604 dump_element_buffer(GLcontext *ctx, GLenum type)
605 {
606 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
607 GL_ELEMENT_ARRAY_BUFFER_ARB,
608 GL_READ_ONLY,
609 ctx->Array.ElementArrayBufferObj);
610 switch (type) {
611 case GL_UNSIGNED_BYTE:
612 {
613 const GLubyte *us = (const GLubyte *) map;
614 GLint i;
615 for (i = 0; i < ctx->Array.ElementArrayBufferObj->Size; i++) {
616 printf("%02x ", us[i]);
617 if (i % 32 == 31)
618 printf("\n");
619 }
620 printf("\n");
621 }
622 break;
623 case GL_UNSIGNED_SHORT:
624 {
625 const GLushort *us = (const GLushort *) map;
626 GLint i;
627 for (i = 0; i < ctx->Array.ElementArrayBufferObj->Size / 2; i++) {
628 printf("%04x ", us[i]);
629 if (i % 16 == 15)
630 printf("\n");
631 }
632 printf("\n");
633 }
634 break;
635 case GL_UNSIGNED_INT:
636 {
637 const GLuint *us = (const GLuint *) map;
638 GLint i;
639 for (i = 0; i < ctx->Array.ElementArrayBufferObj->Size / 4; i++) {
640 printf("%08x ", us[i]);
641 if (i % 8 == 7)
642 printf("\n");
643 }
644 printf("\n");
645 }
646 break;
647 default:
648 ;
649 }
650
651 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB,
652 ctx->Array.ElementArrayBufferObj);
653 }
654
655
656 /**
657 * Inner support for both _mesa_DrawElements and _mesa_DrawRangeElements.
658 * Do the rendering for a glDrawElements or glDrawRangeElements call after
659 * we've validated buffer bounds, etc.
660 */
661 static void
662 vbo_validated_drawrangeelements(GLcontext *ctx, GLenum mode,
663 GLboolean index_bounds_valid,
664 GLuint start, GLuint end,
665 GLsizei count, GLenum type,
666 const GLvoid *indices,
667 GLint basevertex, GLint primcount)
668 {
669 struct vbo_context *vbo = vbo_context(ctx);
670 struct vbo_exec_context *exec = &vbo->exec;
671 struct _mesa_index_buffer ib;
672 struct _mesa_prim prim[1];
673
674 FLUSH_CURRENT( ctx, 0 );
675
676 if (!_mesa_valid_to_render(ctx, "glDraw[Range]Elements")) {
677 return;
678 }
679
680 bind_arrays( ctx );
681
682 /* check for dirty state again */
683 if (ctx->NewState)
684 _mesa_update_state( ctx );
685
686 ib.count = count;
687 ib.type = type;
688 ib.obj = ctx->Array.ElementArrayBufferObj;
689 ib.ptr = indices;
690
691 prim[0].begin = 1;
692 prim[0].end = 1;
693 prim[0].weak = 0;
694 prim[0].pad = 0;
695 prim[0].mode = mode;
696 prim[0].start = 0;
697 prim[0].count = count;
698 prim[0].indexed = 1;
699 prim[0].basevertex = basevertex;
700 prim[0].num_instances = primcount;
701
702 /* Need to give special consideration to rendering a range of
703 * indices starting somewhere above zero. Typically the
704 * application is issuing multiple DrawRangeElements() to draw
705 * successive primitives layed out linearly in the vertex arrays.
706 * Unless the vertex arrays are all in a VBO (or locked as with
707 * CVA), the OpenGL semantics imply that we need to re-read or
708 * re-upload the vertex data on each draw call.
709 *
710 * In the case of hardware tnl, we want to avoid starting the
711 * upload at zero, as it will mean every draw call uploads an
712 * increasing amount of not-used vertex data. Worse - in the
713 * software tnl module, all those vertices might be transformed and
714 * lit but never rendered.
715 *
716 * If we just upload or transform the vertices in start..end,
717 * however, the indices will be incorrect.
718 *
719 * At this level, we don't know exactly what the requirements of
720 * the backend are going to be, though it will likely boil down to
721 * either:
722 *
723 * 1) Do nothing, everything is in a VBO and is processed once
724 * only.
725 *
726 * 2) Adjust the indices and vertex arrays so that start becomes
727 * zero.
728 *
729 * Rather than doing anything here, I'll provide a helper function
730 * for the latter case elsewhere.
731 */
732
733 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib,
734 index_bounds_valid, start, end );
735 }
736
737
738 /**
739 * Called by glDrawRangeElementsBaseVertex() in immediate mode.
740 */
741 static void GLAPIENTRY
742 vbo_exec_DrawRangeElementsBaseVertex(GLenum mode,
743 GLuint start, GLuint end,
744 GLsizei count, GLenum type,
745 const GLvoid *indices,
746 GLint basevertex)
747 {
748 static GLuint warnCount = 0;
749 GET_CURRENT_CONTEXT(ctx);
750
751 if (MESA_VERBOSE & VERBOSE_DRAW)
752 _mesa_debug(ctx,
753 "glDrawRangeElementsBaseVertex(%s, %u, %u, %d, %s, %p, %d)\n",
754 _mesa_lookup_enum_by_nr(mode), start, end, count,
755 _mesa_lookup_enum_by_nr(type), indices, basevertex);
756
757 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
758 type, indices, basevertex ))
759 return;
760
761 /* NOTE: It's important that 'end' is a reasonable value.
762 * in _tnl_draw_prims(), we use end to determine how many vertices
763 * to transform. If it's too large, we can unnecessarily split prims
764 * or we can read/write out of memory in several different places!
765 */
766
767 /* Catch/fix some potential user errors */
768 if (type == GL_UNSIGNED_BYTE) {
769 start = MIN2(start, 0xff);
770 end = MIN2(end, 0xff);
771 }
772 else if (type == GL_UNSIGNED_SHORT) {
773 start = MIN2(start, 0xffff);
774 end = MIN2(end, 0xffff);
775 }
776
777 if (end >= ctx->Array.ArrayObj->_MaxElement) {
778 /* the max element is out of bounds of one or more enabled arrays */
779 warnCount++;
780
781 if (warnCount < 10) {
782 _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, count %d, "
783 "type 0x%x, indices=%p)\n"
784 "\tend is out of bounds (max=%u) "
785 "Element Buffer %u (size %d)\n"
786 "\tThis should probably be fixed in the application.",
787 start, end, count, type, indices,
788 ctx->Array.ArrayObj->_MaxElement - 1,
789 ctx->Array.ElementArrayBufferObj->Name,
790 (int) ctx->Array.ElementArrayBufferObj->Size);
791 }
792
793 if (0)
794 dump_element_buffer(ctx, type);
795
796 if (0)
797 _mesa_print_arrays(ctx);
798
799 #ifdef DEBUG
800 /* 'end' was out of bounds, but now let's check the actual array
801 * indexes to see if any of them are out of bounds.
802 */
803 {
804 GLuint max = _mesa_max_buffer_index(ctx, count, type, indices,
805 ctx->Array.ElementArrayBufferObj);
806 if (max >= ctx->Array.ArrayObj->_MaxElement) {
807 if (warnCount < 10) {
808 _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, "
809 "count %d, type 0x%x, indices=%p)\n"
810 "\tindex=%u is out of bounds (max=%u) "
811 "Element Buffer %u (size %d)\n"
812 "\tSkipping the glDrawRangeElements() call",
813 start, end, count, type, indices, max,
814 ctx->Array.ArrayObj->_MaxElement - 1,
815 ctx->Array.ElementArrayBufferObj->Name,
816 (int) ctx->Array.ElementArrayBufferObj->Size);
817 }
818 }
819 /* XXX we could also find the min index and compare to 'start'
820 * to see if start is correct. But it's more likely to get the
821 * upper bound wrong.
822 */
823 }
824 #endif
825
826 /* Set 'end' to the max possible legal value */
827 assert(ctx->Array.ArrayObj->_MaxElement >= 1);
828 end = ctx->Array.ArrayObj->_MaxElement - 1;
829 }
830 else if (0) {
831 printf("glDraw[Range]Elements{,BaseVertex}"
832 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u, "
833 "base %d\n",
834 start, end, type, count,
835 ctx->Array.ElementArrayBufferObj->Name,
836 basevertex);
837 }
838
839 #if 0
840 check_draw_elements_data(ctx, count, type, indices);
841 #else
842 (void) check_draw_elements_data;
843 #endif
844
845 vbo_validated_drawrangeelements(ctx, mode, GL_TRUE, start, end,
846 count, type, indices, basevertex, 1);
847 }
848
849
850 /**
851 * Called by glDrawRangeElements() in immediate mode.
852 */
853 static void GLAPIENTRY
854 vbo_exec_DrawRangeElements(GLenum mode, GLuint start, GLuint end,
855 GLsizei count, GLenum type, const GLvoid *indices)
856 {
857 GET_CURRENT_CONTEXT(ctx);
858
859 if (MESA_VERBOSE & VERBOSE_DRAW)
860 _mesa_debug(ctx,
861 "glDrawRangeElements(%s, %u, %u, %d, %s, %p)\n",
862 _mesa_lookup_enum_by_nr(mode), start, end, count,
863 _mesa_lookup_enum_by_nr(type), indices);
864
865 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
866 indices, 0);
867 }
868
869
870 /**
871 * Called by glDrawElements() in immediate mode.
872 */
873 static void GLAPIENTRY
874 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
875 const GLvoid *indices)
876 {
877 GET_CURRENT_CONTEXT(ctx);
878
879 if (MESA_VERBOSE & VERBOSE_DRAW)
880 _mesa_debug(ctx, "glDrawElements(%s, %u, %s, %p)\n",
881 _mesa_lookup_enum_by_nr(mode), count,
882 _mesa_lookup_enum_by_nr(type), indices);
883
884 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices, 0 ))
885 return;
886
887 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
888 count, type, indices, 0, 1);
889 }
890
891
892 /**
893 * Called by glDrawElementsBaseVertex() in immediate mode.
894 */
895 static void GLAPIENTRY
896 vbo_exec_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
897 const GLvoid *indices, GLint basevertex)
898 {
899 GET_CURRENT_CONTEXT(ctx);
900
901 if (MESA_VERBOSE & VERBOSE_DRAW)
902 _mesa_debug(ctx, "glDrawElementsBaseVertex(%s, %d, %s, %p, %d)\n",
903 _mesa_lookup_enum_by_nr(mode), count,
904 _mesa_lookup_enum_by_nr(type), indices, basevertex);
905
906 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices,
907 basevertex ))
908 return;
909
910 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
911 count, type, indices, basevertex, 1);
912 }
913
914
915 /**
916 * Called by glDrawElementsInstanced() in immediate mode.
917 */
918 static void GLAPIENTRY
919 vbo_exec_DrawElementsInstanced(GLenum mode, GLsizei count, GLenum type,
920 const GLvoid *indices, GLsizei primcount)
921 {
922 GET_CURRENT_CONTEXT(ctx);
923
924 if (MESA_VERBOSE & VERBOSE_DRAW)
925 _mesa_debug(ctx, "glDrawElementsInstanced(%s, %d, %s, %p, %d)\n",
926 _mesa_lookup_enum_by_nr(mode), count,
927 _mesa_lookup_enum_by_nr(type), indices, primcount);
928
929 if (!_mesa_validate_DrawElementsInstanced(ctx, mode, count, type, indices,
930 primcount))
931 return;
932
933 vbo_validated_drawrangeelements(ctx, mode, GL_FALSE, ~0, ~0,
934 count, type, indices, 0, primcount);
935 }
936
937
938 /**
939 * Inner support for both _mesa_MultiDrawElements() and
940 * _mesa_MultiDrawRangeElements().
941 * This does the actual rendering after we've checked array indexes, etc.
942 */
943 static void
944 vbo_validated_multidrawelements(GLcontext *ctx, GLenum mode,
945 const GLsizei *count, GLenum type,
946 const GLvoid **indices, GLsizei primcount,
947 const GLint *basevertex)
948 {
949 struct vbo_context *vbo = vbo_context(ctx);
950 struct vbo_exec_context *exec = &vbo->exec;
951 struct _mesa_index_buffer ib;
952 struct _mesa_prim *prim;
953 unsigned int index_type_size = 0;
954 uintptr_t min_index_ptr, max_index_ptr;
955 GLboolean fallback = GL_FALSE;
956 int i;
957
958 if (primcount == 0)
959 return;
960
961 FLUSH_CURRENT( ctx, 0 );
962
963 if (!_mesa_valid_to_render(ctx, "glMultiDrawElements")) {
964 return;
965 }
966
967 prim = calloc(1, primcount * sizeof(*prim));
968 if (prim == NULL) {
969 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glMultiDrawElements");
970 return;
971 }
972
973 /* Decide if we can do this all as one set of primitives sharing the
974 * same index buffer, or if we have to reset the index pointer per
975 * primitive.
976 */
977 bind_arrays( ctx );
978
979 /* check for dirty state again */
980 if (ctx->NewState)
981 _mesa_update_state( ctx );
982
983 switch (type) {
984 case GL_UNSIGNED_INT:
985 index_type_size = 4;
986 break;
987 case GL_UNSIGNED_SHORT:
988 index_type_size = 2;
989 break;
990 case GL_UNSIGNED_BYTE:
991 index_type_size = 1;
992 break;
993 default:
994 assert(0);
995 }
996
997 min_index_ptr = (uintptr_t)indices[0];
998 max_index_ptr = 0;
999 for (i = 0; i < primcount; i++) {
1000 min_index_ptr = MIN2(min_index_ptr, (uintptr_t)indices[i]);
1001 max_index_ptr = MAX2(max_index_ptr, (uintptr_t)indices[i] +
1002 index_type_size * count[i]);
1003 }
1004
1005 /* Check if we can handle this thing as a bunch of index offsets from the
1006 * same index pointer. If we can't, then we have to fall back to doing
1007 * a draw_prims per primitive.
1008 */
1009 if (index_type_size != 1) {
1010 for (i = 0; i < primcount; i++) {
1011 if ((((uintptr_t)indices[i] - min_index_ptr) % index_type_size) != 0) {
1012 fallback = GL_TRUE;
1013 break;
1014 }
1015 }
1016 }
1017
1018 /* If the index buffer isn't in a VBO, then treating the application's
1019 * subranges of the index buffer as one large index buffer may lead to
1020 * us reading unmapped memory.
1021 */
1022 if (!_mesa_is_bufferobj(ctx->Array.ElementArrayBufferObj))
1023 fallback = GL_TRUE;
1024
1025 if (!fallback) {
1026 ib.count = (max_index_ptr - min_index_ptr) / index_type_size;
1027 ib.type = type;
1028 ib.obj = ctx->Array.ElementArrayBufferObj;
1029 ib.ptr = (void *)min_index_ptr;
1030
1031 for (i = 0; i < primcount; i++) {
1032 prim[i].begin = (i == 0);
1033 prim[i].end = (i == primcount - 1);
1034 prim[i].weak = 0;
1035 prim[i].pad = 0;
1036 prim[i].mode = mode;
1037 prim[i].start = ((uintptr_t)indices[i] - min_index_ptr) / index_type_size;
1038 prim[i].count = count[i];
1039 prim[i].indexed = 1;
1040 prim[i].num_instances = 1;
1041 if (basevertex != NULL)
1042 prim[i].basevertex = basevertex[i];
1043 else
1044 prim[i].basevertex = 0;
1045 }
1046
1047 vbo->draw_prims(ctx, exec->array.inputs, prim, primcount, &ib,
1048 GL_FALSE, ~0, ~0);
1049 } else {
1050 /* render one prim at a time */
1051 for (i = 0; i < primcount; i++) {
1052 ib.count = count[i];
1053 ib.type = type;
1054 ib.obj = ctx->Array.ElementArrayBufferObj;
1055 ib.ptr = indices[i];
1056
1057 prim[0].begin = 1;
1058 prim[0].end = 1;
1059 prim[0].weak = 0;
1060 prim[0].pad = 0;
1061 prim[0].mode = mode;
1062 prim[0].start = 0;
1063 prim[0].count = count[i];
1064 prim[0].indexed = 1;
1065 prim[0].num_instances = 1;
1066 if (basevertex != NULL)
1067 prim[0].basevertex = basevertex[i];
1068 else
1069 prim[0].basevertex = 0;
1070
1071 vbo->draw_prims(ctx, exec->array.inputs, prim, 1, &ib,
1072 GL_FALSE, ~0, ~0);
1073 }
1074 }
1075
1076 free(prim);
1077 }
1078
1079
1080 static void GLAPIENTRY
1081 vbo_exec_MultiDrawElements(GLenum mode,
1082 const GLsizei *count, GLenum type,
1083 const GLvoid **indices,
1084 GLsizei primcount)
1085 {
1086 GET_CURRENT_CONTEXT(ctx);
1087 GLint i;
1088
1089 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1090
1091 for (i = 0; i < primcount; i++) {
1092 if (!_mesa_validate_DrawElements(ctx, mode, count[i], type, indices[i],
1093 0))
1094 return;
1095 }
1096
1097 vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount,
1098 NULL);
1099 }
1100
1101
1102 static void GLAPIENTRY
1103 vbo_exec_MultiDrawElementsBaseVertex(GLenum mode,
1104 const GLsizei *count, GLenum type,
1105 const GLvoid **indices,
1106 GLsizei primcount,
1107 const GLsizei *basevertex)
1108 {
1109 GET_CURRENT_CONTEXT(ctx);
1110 GLint i;
1111
1112 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
1113
1114 for (i = 0; i < primcount; i++) {
1115 if (!_mesa_validate_DrawElements(ctx, mode, count[i], type, indices[i],
1116 basevertex[i]))
1117 return;
1118 }
1119
1120 vbo_validated_multidrawelements(ctx, mode, count, type, indices, primcount,
1121 basevertex);
1122 }
1123
1124
1125 /**
1126 * Plug in the immediate-mode vertex array drawing commands into the
1127 * givven vbo_exec_context object.
1128 */
1129 void
1130 vbo_exec_array_init( struct vbo_exec_context *exec )
1131 {
1132 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
1133 exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
1134 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
1135 exec->vtxfmt.MultiDrawElementsEXT = vbo_exec_MultiDrawElements;
1136 exec->vtxfmt.DrawElementsBaseVertex = vbo_exec_DrawElementsBaseVertex;
1137 exec->vtxfmt.DrawRangeElementsBaseVertex = vbo_exec_DrawRangeElementsBaseVertex;
1138 exec->vtxfmt.MultiDrawElementsBaseVertex = vbo_exec_MultiDrawElementsBaseVertex;
1139 exec->vtxfmt.DrawArraysInstanced = vbo_exec_DrawArraysInstanced;
1140 exec->vtxfmt.DrawElementsInstanced = vbo_exec_DrawElementsInstanced;
1141 }
1142
1143
1144 void
1145 vbo_exec_array_destroy( struct vbo_exec_context *exec )
1146 {
1147 /* nothing to do */
1148 }
1149
1150
1151
1152 /**
1153 * The following functions are only used for OpenGL ES 1/2 support.
1154 * And some aren't even supported (yet) in ES 1/2.
1155 */
1156
1157
1158 void GLAPIENTRY
1159 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
1160 {
1161 vbo_exec_DrawArrays(mode, first, count);
1162 }
1163
1164
1165 void GLAPIENTRY
1166 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
1167 const GLvoid *indices)
1168 {
1169 vbo_exec_DrawElements(mode, count, type, indices);
1170 }
1171
1172
1173 void GLAPIENTRY
1174 _mesa_DrawElementsBaseVertex(GLenum mode, GLsizei count, GLenum type,
1175 const GLvoid *indices, GLint basevertex)
1176 {
1177 vbo_exec_DrawElementsBaseVertex(mode, count, type, indices, basevertex);
1178 }
1179
1180
1181 void GLAPIENTRY
1182 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
1183 GLenum type, const GLvoid *indices)
1184 {
1185 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
1186 }
1187
1188
1189 void GLAPIENTRY
1190 _mesa_DrawRangeElementsBaseVertex(GLenum mode, GLuint start, GLuint end,
1191 GLsizei count, GLenum type,
1192 const GLvoid *indices, GLint basevertex)
1193 {
1194 vbo_exec_DrawRangeElementsBaseVertex(mode, start, end, count, type,
1195 indices, basevertex);
1196 }
1197
1198
1199 void GLAPIENTRY
1200 _mesa_MultiDrawElementsEXT(GLenum mode, const GLsizei *count, GLenum type,
1201 const GLvoid **indices, GLsizei primcount)
1202 {
1203 vbo_exec_MultiDrawElements(mode, count, type, indices, primcount);
1204 }
1205
1206
1207 void GLAPIENTRY
1208 _mesa_MultiDrawElementsBaseVertex(GLenum mode,
1209 const GLsizei *count, GLenum type,
1210 const GLvoid **indices, GLsizei primcount,
1211 const GLint *basevertex)
1212 {
1213 vbo_exec_MultiDrawElementsBaseVertex(mode, count, type, indices,
1214 primcount, basevertex);
1215 }