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