Merge branch 'mesa_7_5_branch'
[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 "glapi/dispatch.h"
37
38 #include "vbo_context.h"
39
40
41 /**
42 * Compute min and max elements for glDraw[Range]Elements() calls.
43 */
44 static void
45 get_minmax_index(GLuint count, GLuint type, const GLvoid *indices,
46 GLuint *min_index, GLuint *max_index)
47 {
48 GLuint i;
49
50 switch(type) {
51 case GL_UNSIGNED_INT: {
52 const GLuint *ui_indices = (const GLuint *)indices;
53 GLuint max_ui = ui_indices[count-1];
54 GLuint min_ui = ui_indices[0];
55 for (i = 0; i < count; i++) {
56 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
57 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
58 }
59 *min_index = min_ui;
60 *max_index = max_ui;
61 break;
62 }
63 case GL_UNSIGNED_SHORT: {
64 const GLushort *us_indices = (const GLushort *)indices;
65 GLuint max_us = us_indices[count-1];
66 GLuint min_us = us_indices[0];
67 for (i = 0; i < count; i++) {
68 if (us_indices[i] > max_us) max_us = us_indices[i];
69 if (us_indices[i] < min_us) min_us = us_indices[i];
70 }
71 *min_index = min_us;
72 *max_index = max_us;
73 break;
74 }
75 case GL_UNSIGNED_BYTE: {
76 const GLubyte *ub_indices = (const GLubyte *)indices;
77 GLuint max_ub = ub_indices[count-1];
78 GLuint min_ub = ub_indices[0];
79 for (i = 0; i < count; i++) {
80 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
81 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
82 }
83 *min_index = min_ub;
84 *max_index = max_ub;
85 break;
86 }
87 default:
88 assert(0);
89 break;
90 }
91 }
92
93
94 /**
95 * Check that element 'j' of the array has reasonable data.
96 * Map VBO if needed.
97 */
98 static void
99 check_array_data(GLcontext *ctx, struct gl_client_array *array,
100 GLuint attrib, GLuint j)
101 {
102 if (array->Enabled) {
103 const void *data = array->Ptr;
104 if (array->BufferObj->Name) {
105 if (!array->BufferObj->Pointer) {
106 /* need to map now */
107 array->BufferObj->Pointer = ctx->Driver.MapBuffer(ctx,
108 GL_ARRAY_BUFFER_ARB,
109 GL_READ_ONLY,
110 array->BufferObj);
111 }
112 data = ADD_POINTERS(data, array->BufferObj->Pointer);
113 }
114 switch (array->Type) {
115 case GL_FLOAT:
116 {
117 GLfloat *f = (GLfloat *) ((GLubyte *) data + array->StrideB * j);
118 GLuint k;
119 for (k = 0; k < array->Size; k++) {
120 if (IS_INF_OR_NAN(f[k]) ||
121 f[k] >= 1.0e20 || f[k] <= -1.0e10) {
122 _mesa_printf("Bad array data:\n");
123 _mesa_printf(" Element[%u].%u = %f\n", j, k, f[k]);
124 _mesa_printf(" Array %u at %p\n", attrib, (void* ) array);
125 _mesa_printf(" Type 0x%x, Size %d, Stride %d\n",
126 array->Type, array->Size, array->Stride);
127 _mesa_printf(" Address/offset %p in Buffer Object %u\n",
128 array->Ptr, array->BufferObj->Name);
129 f[k] = 1.0; /* XXX replace the bad value! */
130 }
131 //assert(!IS_INF_OR_NAN(f[k]));
132 }
133 }
134 break;
135 default:
136 ;
137 }
138 }
139 }
140
141
142 /**
143 * Unmap the buffer object referenced by given array, if mapped.
144 */
145 static void
146 unmap_array_buffer(GLcontext *ctx, struct gl_client_array *array)
147 {
148 if (array->Enabled &&
149 array->BufferObj->Name &&
150 array->BufferObj->Pointer) {
151 ctx->Driver.UnmapBuffer(ctx,
152 GL_ARRAY_BUFFER_ARB,
153 array->BufferObj);
154 }
155 }
156
157
158 /**
159 * Examine the array's data for NaNs, etc.
160 */
161 static void
162 check_draw_elements_data(GLcontext *ctx, GLsizei count, GLenum elemType,
163 const void *elements)
164 {
165 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
166 const void *elemMap;
167 GLint i, k;
168
169 if (ctx->Array.ElementArrayBufferObj->Name) {
170 elemMap = ctx->Driver.MapBuffer(ctx,
171 GL_ELEMENT_ARRAY_BUFFER_ARB,
172 GL_READ_ONLY,
173 ctx->Array.ElementArrayBufferObj);
174 elements = ADD_POINTERS(elements, elemMap);
175 }
176
177 for (i = 0; i < count; i++) {
178 GLuint j;
179
180 /* j = element[i] */
181 switch (elemType) {
182 case GL_UNSIGNED_BYTE:
183 j = ((const GLubyte *) elements)[i];
184 break;
185 case GL_UNSIGNED_SHORT:
186 j = ((const GLushort *) elements)[i];
187 break;
188 case GL_UNSIGNED_INT:
189 j = ((const GLuint *) elements)[i];
190 break;
191 default:
192 assert(0);
193 }
194
195 /* check element j of each enabled array */
196 check_array_data(ctx, &arrayObj->Vertex, VERT_ATTRIB_POS, j);
197 check_array_data(ctx, &arrayObj->Normal, VERT_ATTRIB_NORMAL, j);
198 check_array_data(ctx, &arrayObj->Color, VERT_ATTRIB_COLOR0, j);
199 check_array_data(ctx, &arrayObj->SecondaryColor, VERT_ATTRIB_COLOR1, j);
200 for (k = 0; k < Elements(arrayObj->TexCoord); k++) {
201 check_array_data(ctx, &arrayObj->TexCoord[k], VERT_ATTRIB_TEX0 + k, j);
202 }
203 for (k = 0; k < Elements(arrayObj->VertexAttrib); k++) {
204 check_array_data(ctx, &arrayObj->VertexAttrib[k], VERT_ATTRIB_GENERIC0 + k, j);
205 }
206 }
207
208 if (ctx->Array.ElementArrayBufferObj->Name) {
209 ctx->Driver.UnmapBuffer(ctx,
210 GL_ELEMENT_ARRAY_BUFFER_ARB,
211 ctx->Array.ElementArrayBufferObj);
212 }
213
214 unmap_array_buffer(ctx, &arrayObj->Vertex);
215 unmap_array_buffer(ctx, &arrayObj->Normal);
216 unmap_array_buffer(ctx, &arrayObj->Color);
217 for (k = 0; k < Elements(arrayObj->TexCoord); k++) {
218 unmap_array_buffer(ctx, &arrayObj->TexCoord[k]);
219 }
220 for (k = 0; k < Elements(arrayObj->VertexAttrib); k++) {
221 unmap_array_buffer(ctx, &arrayObj->VertexAttrib[k]);
222 }
223 }
224
225
226 /**
227 * Check array data, looking for NaNs, etc.
228 */
229 static void
230 check_draw_arrays_data(GLcontext *ctx, GLint start, GLsizei count)
231 {
232 /* TO DO */
233 }
234
235
236 /**
237 * Print info/data for glDrawArrays().
238 */
239 static void
240 print_draw_arrays(GLcontext *ctx, struct vbo_exec_context *exec,
241 GLenum mode, GLint start, GLsizei count)
242 {
243 int i;
244
245 _mesa_printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
246 mode, start, count);
247
248 for (i = 0; i < 32; i++) {
249 GLuint bufName = exec->array.inputs[i]->BufferObj->Name;
250 GLint stride = exec->array.inputs[i]->Stride;
251 _mesa_printf("attr %2d: size %d stride %d enabled %d "
252 "ptr %p Bufobj %u\n",
253 i,
254 exec->array.inputs[i]->Size,
255 stride,
256 /*exec->array.inputs[i]->Enabled,*/
257 exec->array.legacy_array[i]->Enabled,
258 exec->array.inputs[i]->Ptr,
259 bufName);
260
261 if (bufName) {
262 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, bufName);
263 GLubyte *p = ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER_ARB,
264 GL_READ_ONLY_ARB, buf);
265 int offset = (int) (GLintptr) exec->array.inputs[i]->Ptr;
266 float *f = (float *) (p + offset);
267 int *k = (int *) f;
268 int i;
269 int n = (count * stride) / 4;
270 if (n > 32)
271 n = 32;
272 _mesa_printf(" Data at offset %d:\n", offset);
273 for (i = 0; i < n; i++) {
274 _mesa_printf(" float[%d] = 0x%08x %f\n", i, k[i], f[i]);
275 }
276 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, buf);
277 }
278 }
279 }
280
281
282 /**
283 * Just translate the arrayobj into a sane layout.
284 */
285 static void
286 bind_array_obj(GLcontext *ctx)
287 {
288 struct vbo_context *vbo = vbo_context(ctx);
289 struct vbo_exec_context *exec = &vbo->exec;
290 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
291 GLuint i;
292
293 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
294 * rather than as individual named arrays. Then this function can
295 * go away.
296 */
297 exec->array.legacy_array[VERT_ATTRIB_POS] = &arrayObj->Vertex;
298 exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &arrayObj->Weight;
299 exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &arrayObj->Normal;
300 exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &arrayObj->Color;
301 exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &arrayObj->SecondaryColor;
302 exec->array.legacy_array[VERT_ATTRIB_FOG] = &arrayObj->FogCoord;
303 exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &arrayObj->Index;
304 if (arrayObj->PointSize.Enabled) {
305 /* this aliases COLOR_INDEX */
306 exec->array.legacy_array[VERT_ATTRIB_POINT_SIZE] = &arrayObj->PointSize;
307 }
308 exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &arrayObj->EdgeFlag;
309
310 for (i = 0; i < Elements(arrayObj->TexCoord); i++)
311 exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &arrayObj->TexCoord[i];
312
313 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
314 assert(i < Elements(exec->array.generic_array));
315 exec->array.generic_array[i] = &arrayObj->VertexAttrib[i];
316 }
317
318 exec->array.array_obj = arrayObj->Name;
319 }
320
321
322 static void
323 recalculate_input_bindings(GLcontext *ctx)
324 {
325 struct vbo_context *vbo = vbo_context(ctx);
326 struct vbo_exec_context *exec = &vbo->exec;
327 const struct gl_client_array **inputs = &exec->array.inputs[0];
328 GLbitfield const_inputs = 0x0;
329 GLuint i;
330
331 exec->array.program_mode = get_program_mode(ctx);
332 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
333
334 switch (exec->array.program_mode) {
335 case VP_NONE:
336 /* When no vertex program is active (or the vertex program is generated
337 * from fixed-function state). We put the material values into the
338 * generic slots. This is the only situation where material values
339 * are available as per-vertex attributes.
340 */
341 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
342 if (exec->array.legacy_array[i]->Enabled)
343 inputs[i] = exec->array.legacy_array[i];
344 else {
345 inputs[i] = &vbo->legacy_currval[i];
346 const_inputs |= 1 << i;
347 }
348 }
349
350 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
351 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i];
352 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
353 }
354
355 /* Could use just about anything, just to fill in the empty
356 * slots:
357 */
358 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX - VERT_ATTRIB_GENERIC0; i++) {
359 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
360 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
361 }
362 break;
363
364 case VP_NV:
365 /* NV_vertex_program - attribute arrays alias and override
366 * conventional, legacy arrays. No materials, and the generic
367 * slots are vacant.
368 */
369 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
370 if (exec->array.generic_array[i]->Enabled)
371 inputs[i] = exec->array.generic_array[i];
372 else if (exec->array.legacy_array[i]->Enabled)
373 inputs[i] = exec->array.legacy_array[i];
374 else {
375 inputs[i] = &vbo->legacy_currval[i];
376 const_inputs |= 1 << i;
377 }
378 }
379
380 /* Could use just about anything, just to fill in the empty
381 * slots:
382 */
383 for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++) {
384 inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0];
385 const_inputs |= 1 << i;
386 }
387 break;
388
389 case VP_ARB:
390 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
391 * attribute array aliases and overrides the legacy position array.
392 *
393 * Otherwise, legacy attributes available in the legacy slots,
394 * generic attributes in the generic slots and materials are not
395 * available as per-vertex attributes.
396 */
397 if (exec->array.generic_array[0]->Enabled)
398 inputs[0] = exec->array.generic_array[0];
399 else if (exec->array.legacy_array[0]->Enabled)
400 inputs[0] = exec->array.legacy_array[0];
401 else {
402 inputs[0] = &vbo->legacy_currval[0];
403 const_inputs |= 1 << 0;
404 }
405
406 for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
407 if (exec->array.legacy_array[i]->Enabled)
408 inputs[i] = exec->array.legacy_array[i];
409 else {
410 inputs[i] = &vbo->legacy_currval[i];
411 const_inputs |= 1 << i;
412 }
413 }
414
415 for (i = 0; i < MAX_VERTEX_GENERIC_ATTRIBS; i++) {
416 if (exec->array.generic_array[i]->Enabled)
417 inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
418 else {
419 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
420 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
421 }
422
423 }
424 break;
425 }
426
427 _mesa_set_varying_vp_inputs( ctx, ~const_inputs );
428 }
429
430
431 static void
432 bind_arrays(GLcontext *ctx)
433 {
434 #if 0
435 if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
436 bind_array_obj(ctx);
437 recalculate_input_bindings(ctx);
438 }
439 else if (exec->array.program_mode != get_program_mode(ctx) ||
440 exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
441
442 recalculate_input_bindings(ctx);
443 }
444 #else
445 bind_array_obj(ctx);
446 recalculate_input_bindings(ctx);
447 #endif
448 }
449
450
451
452 /***********************************************************************
453 * API functions.
454 */
455
456 static void GLAPIENTRY
457 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
458 {
459 GET_CURRENT_CONTEXT(ctx);
460 struct vbo_context *vbo = vbo_context(ctx);
461 struct vbo_exec_context *exec = &vbo->exec;
462 struct _mesa_prim prim[1];
463
464 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
465 return;
466
467 FLUSH_CURRENT( ctx, 0 );
468
469 if (ctx->NewState)
470 _mesa_update_state( ctx );
471
472 if (!vbo_validate_shaders(ctx)) {
473 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawArrays(bad shader)");
474 return;
475 }
476
477 #if 0
478 check_draw_arrays_data(ctx, start, count);
479 #else
480 (void) check_draw_arrays_data;
481 #endif
482
483 bind_arrays( ctx );
484
485 /* Again... because we may have changed the bitmask of per-vertex varying
486 * attributes. If we regenerate the fixed-function vertex program now
487 * we may be able to prune down the number of vertex attributes which we
488 * need in the shader.
489 */
490 if (ctx->NewState)
491 _mesa_update_state( ctx );
492
493 prim[0].begin = 1;
494 prim[0].end = 1;
495 prim[0].weak = 0;
496 prim[0].pad = 0;
497 prim[0].mode = mode;
498 prim[0].start = start;
499 prim[0].count = count;
500 prim[0].indexed = 0;
501
502 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL,
503 start, start + count - 1 );
504
505 #if 0
506 print_draw_arrays(ctx, exec, mode, start, count);
507 #else
508 (void) print_draw_arrays;
509 #endif
510 }
511
512
513 /**
514 * Map GL_ELEMENT_ARRAY_BUFFER and print contents.
515 */
516 static void
517 dump_element_buffer(GLcontext *ctx, GLenum type)
518 {
519 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
520 GL_ELEMENT_ARRAY_BUFFER_ARB,
521 GL_READ_ONLY,
522 ctx->Array.ElementArrayBufferObj);
523 switch (type) {
524 case GL_UNSIGNED_BYTE:
525 {
526 const GLubyte *us = (const GLubyte *) map;
527 GLuint i;
528 for (i = 0; i < ctx->Array.ElementArrayBufferObj->Size; i++) {
529 _mesa_printf("%02x ", us[i]);
530 if (i % 32 == 31)
531 _mesa_printf("\n");
532 }
533 _mesa_printf("\n");
534 }
535 break;
536 case GL_UNSIGNED_SHORT:
537 {
538 const GLushort *us = (const GLushort *) map;
539 GLuint i;
540 for (i = 0; i < ctx->Array.ElementArrayBufferObj->Size / 2; i++) {
541 _mesa_printf("%04x ", us[i]);
542 if (i % 16 == 15)
543 _mesa_printf("\n");
544 }
545 _mesa_printf("\n");
546 }
547 break;
548 case GL_UNSIGNED_INT:
549 {
550 const GLuint *us = (const GLuint *) map;
551 GLuint i;
552 for (i = 0; i < ctx->Array.ElementArrayBufferObj->Size / 4; i++) {
553 _mesa_printf("%08x ", us[i]);
554 if (i % 8 == 7)
555 _mesa_printf("\n");
556 }
557 _mesa_printf("\n");
558 }
559 break;
560 default:
561 ;
562 }
563
564 ctx->Driver.UnmapBuffer(ctx,
565 GL_ELEMENT_ARRAY_BUFFER_ARB,
566 ctx->Array.ElementArrayBufferObj);
567 }
568
569
570 static void GLAPIENTRY
571 vbo_exec_DrawRangeElements(GLenum mode,
572 GLuint start, GLuint end,
573 GLsizei count, GLenum type, const GLvoid *indices)
574 {
575 GET_CURRENT_CONTEXT(ctx);
576 struct vbo_context *vbo = vbo_context(ctx);
577 struct vbo_exec_context *exec = &vbo->exec;
578 struct _mesa_index_buffer ib;
579 struct _mesa_prim prim[1];
580
581 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
582 type, indices ))
583 return;
584
585 if (end >= ctx->Array.ArrayObj->_MaxElement) {
586 /* the max element is out of bounds of one or more enabled arrays */
587 _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, count %d, "
588 "type 0x%x, indices=%p)\n"
589 "\tindex=%u is out of bounds (max=%u) "
590 "Element Buffer %u (size %d)",
591 start, end, count, type, indices, end,
592 ctx->Array.ArrayObj->_MaxElement - 1,
593 ctx->Array.ElementArrayBufferObj->Name,
594 ctx->Array.ElementArrayBufferObj->Size);
595
596 if (0)
597 dump_element_buffer(ctx, type);
598
599 if (0)
600 _mesa_print_arrays(ctx);
601 return;
602 }
603 else if (0) {
604 _mesa_printf("glDraw[Range]Elements"
605 "(start %u, end %u, type 0x%x, count %d) ElemBuf %u\n",
606 start, end, type, count,
607 ctx->Array.ElementArrayBufferObj->Name);
608 }
609
610 #if 0
611 check_draw_elements_data(ctx, count, type, indices);
612 #else
613 (void) check_draw_elements_data;
614 #endif
615
616 FLUSH_CURRENT( ctx, 0 );
617
618 if (ctx->NewState)
619 _mesa_update_state( ctx );
620
621 if (!vbo_validate_shaders(ctx)) {
622 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawRangeElements(bad shader)");
623 return;
624 }
625
626 bind_arrays( ctx );
627
628 if (ctx->NewState)
629 _mesa_update_state( ctx );
630
631 ib.count = count;
632 ib.type = type;
633 ib.obj = ctx->Array.ElementArrayBufferObj;
634 ib.ptr = indices;
635
636 prim[0].begin = 1;
637 prim[0].end = 1;
638 prim[0].weak = 0;
639 prim[0].pad = 0;
640 prim[0].mode = mode;
641 prim[0].start = 0;
642 prim[0].count = count;
643 prim[0].indexed = 1;
644
645 /* Need to give special consideration to rendering a range of
646 * indices starting somewhere above zero. Typically the
647 * application is issuing multiple DrawRangeElements() to draw
648 * successive primitives layed out linearly in the vertex arrays.
649 * Unless the vertex arrays are all in a VBO (or locked as with
650 * CVA), the OpenGL semantics imply that we need to re-read or
651 * re-upload the vertex data on each draw call.
652 *
653 * In the case of hardware tnl, we want to avoid starting the
654 * upload at zero, as it will mean every draw call uploads an
655 * increasing amount of not-used vertex data. Worse - in the
656 * software tnl module, all those vertices might be transformed and
657 * lit but never rendered.
658 *
659 * If we just upload or transform the vertices in start..end,
660 * however, the indices will be incorrect.
661 *
662 * At this level, we don't know exactly what the requirements of
663 * the backend are going to be, though it will likely boil down to
664 * either:
665 *
666 * 1) Do nothing, everything is in a VBO and is processed once
667 * only.
668 *
669 * 2) Adjust the indices and vertex arrays so that start becomes
670 * zero.
671 *
672 * Rather than doing anything here, I'll provide a helper function
673 * for the latter case elsewhere.
674 */
675
676 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, start, end );
677 }
678
679
680 static void GLAPIENTRY
681 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
682 const GLvoid *indices)
683 {
684 GET_CURRENT_CONTEXT(ctx);
685 GLuint min_index = 0;
686 GLuint max_index = 0;
687
688 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
689 return;
690
691 if (!vbo_validate_shaders(ctx)) {
692 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawElements(bad shader)");
693 return;
694 }
695
696 if (ctx->Array.ElementArrayBufferObj->Name) {
697 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
698 GL_ELEMENT_ARRAY_BUFFER_ARB,
699 GL_READ_ONLY,
700 ctx->Array.ElementArrayBufferObj);
701
702 get_minmax_index(count, type, ADD_POINTERS(map, indices),
703 &min_index, &max_index);
704
705 ctx->Driver.UnmapBuffer(ctx,
706 GL_ELEMENT_ARRAY_BUFFER_ARB,
707 ctx->Array.ElementArrayBufferObj);
708 }
709 else {
710 get_minmax_index(count, type, indices, &min_index, &max_index);
711 }
712
713 vbo_exec_DrawRangeElements(mode, min_index, max_index, count, type, indices);
714 }
715
716
717 /***********************************************************************
718 * Initialization
719 */
720
721 void
722 vbo_exec_array_init( struct vbo_exec_context *exec )
723 {
724 #if 1
725 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
726 exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
727 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
728 #else
729 exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
730 exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
731 exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
732 #endif
733 }
734
735
736 void
737 vbo_exec_array_destroy( struct vbo_exec_context *exec )
738 {
739 /* nothing to do */
740 }
741
742
743 /* This API entrypoint is not ordinarily used */
744 void GLAPIENTRY
745 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
746 {
747 vbo_exec_DrawArrays(mode, first, count);
748 }
749
750
751 /* This API entrypoint is not ordinarily used */
752 void GLAPIENTRY
753 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
754 const GLvoid *indices)
755 {
756 vbo_exec_DrawElements(mode, count, type, indices);
757 }
758
759
760 /* This API entrypoint is not ordinarily used */
761 void GLAPIENTRY
762 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
763 GLenum type, const GLvoid *indices)
764 {
765 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
766 }