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