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