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