1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009 VMware, Inc.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
27 **************************************************************************/
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"
38 #include "vbo_context.h"
42 * Compute min and max elements for glDraw[Range]Elements() calls.
45 get_minmax_index(GLuint count, GLuint type, const GLvoid *indices,
46 GLuint *min_index, GLuint *max_index)
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];
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];
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];
95 * Check that element 'j' of the array has reasonable data.
99 check_array_data(GLcontext *ctx, struct gl_client_array *array,
100 GLuint attrib, GLuint j)
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,
112 data = ADD_POINTERS(data, array->BufferObj->Pointer);
114 switch (array->Type) {
117 GLfloat *f = (GLfloat *) ((GLubyte *) data + array->StrideB * j);
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! */
131 //assert(!IS_INF_OR_NAN(f[k]));
143 * Unmap the buffer object referenced by given array, if mapped.
146 unmap_array_buffer(GLcontext *ctx, struct gl_client_array *array)
148 if (array->Enabled &&
149 array->BufferObj->Name &&
150 array->BufferObj->Pointer) {
151 ctx->Driver.UnmapBuffer(ctx,
159 * Examine the array's data for NaNs, etc.
162 check_draw_elements_data(GLcontext *ctx, GLsizei count, GLenum elemType,
163 const void *elements)
165 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
169 if (ctx->Array.ElementArrayBufferObj->Name) {
170 elemMap = ctx->Driver.MapBuffer(ctx,
171 GL_ELEMENT_ARRAY_BUFFER_ARB,
173 ctx->Array.ElementArrayBufferObj);
174 elements = ADD_POINTERS(elements, elemMap);
177 for (i = 0; i < count; i++) {
182 case GL_UNSIGNED_BYTE:
183 j = ((const GLubyte *) elements)[i];
185 case GL_UNSIGNED_SHORT:
186 j = ((const GLushort *) elements)[i];
188 case GL_UNSIGNED_INT:
189 j = ((const GLuint *) elements)[i];
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);
203 for (k = 0; k < Elements(arrayObj->VertexAttrib); k++) {
204 check_array_data(ctx, &arrayObj->VertexAttrib[k], VERT_ATTRIB_GENERIC0 + k, j);
208 if (ctx->Array.ElementArrayBufferObj->Name) {
209 ctx->Driver.UnmapBuffer(ctx,
210 GL_ELEMENT_ARRAY_BUFFER_ARB,
211 ctx->Array.ElementArrayBufferObj);
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]);
220 for (k = 0; k < Elements(arrayObj->VertexAttrib); k++) {
221 unmap_array_buffer(ctx, &arrayObj->VertexAttrib[k]);
227 * Check array data, looking for NaNs, etc.
230 check_draw_arrays_data(GLcontext *ctx, GLint start, GLsizei count)
237 * Print info/data for glDrawArrays().
240 print_draw_arrays(GLcontext *ctx, struct vbo_exec_context *exec,
241 GLenum mode, GLint start, GLsizei count)
245 _mesa_printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
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",
254 exec->array.inputs[i]->Size,
256 /*exec->array.inputs[i]->Enabled,*/
257 exec->array.legacy_array[i]->Enabled,
258 exec->array.inputs[i]->Ptr,
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);
269 int n = (count * stride) / 4;
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]);
276 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, buf);
283 * Just translate the arrayobj into a sane layout.
286 bind_array_obj(GLcontext *ctx)
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;
293 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
294 * rather than as individual named arrays. Then this function can
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;
308 exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &arrayObj->EdgeFlag;
310 for (i = 0; i < Elements(arrayObj->TexCoord); i++)
311 exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &arrayObj->TexCoord[i];
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];
318 exec->array.array_obj = arrayObj->Name;
323 recalculate_input_bindings(GLcontext *ctx)
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;
331 exec->array.program_mode = get_program_mode(ctx);
332 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
334 switch (exec->array.program_mode) {
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.
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];
345 inputs[i] = &vbo->legacy_currval[i];
346 const_inputs |= 1 << i;
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);
355 /* Could use just about anything, just to fill in the empty
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);
365 /* NV_vertex_program - attribute arrays alias and override
366 * conventional, legacy arrays. No materials, and the generic
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];
375 inputs[i] = &vbo->legacy_currval[i];
376 const_inputs |= 1 << i;
380 /* Could use just about anything, just to fill in the empty
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;
390 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
391 * attribute array aliases and overrides the legacy position array.
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.
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];
402 inputs[0] = &vbo->legacy_currval[0];
403 const_inputs |= 1 << 0;
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];
410 inputs[i] = &vbo->legacy_currval[i];
411 const_inputs |= 1 << i;
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];
419 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
420 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
427 _mesa_set_varying_vp_inputs( ctx, ~const_inputs );
432 bind_arrays(GLcontext *ctx)
435 if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
437 recalculate_input_bindings(ctx);
439 else if (exec->array.program_mode != get_program_mode(ctx) ||
440 exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
442 recalculate_input_bindings(ctx);
446 recalculate_input_bindings(ctx);
452 /***********************************************************************
456 static void GLAPIENTRY
457 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
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];
464 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
467 FLUSH_CURRENT( ctx, 0 );
470 _mesa_update_state( ctx );
472 if (!vbo_validate_shaders(ctx)) {
473 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawArrays(bad shader)");
478 check_draw_arrays_data(ctx, start, count);
480 (void) check_draw_arrays_data;
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.
491 _mesa_update_state( ctx );
498 prim[0].start = start;
499 prim[0].count = count;
502 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL,
503 start, start + count - 1 );
506 print_draw_arrays(ctx, exec, mode, start, count);
508 (void) print_draw_arrays;
513 static void GLAPIENTRY
514 vbo_exec_DrawRangeElements(GLenum mode,
515 GLuint start, GLuint end,
516 GLsizei count, GLenum type, const GLvoid *indices)
518 GET_CURRENT_CONTEXT(ctx);
519 struct vbo_context *vbo = vbo_context(ctx);
520 struct vbo_exec_context *exec = &vbo->exec;
521 struct _mesa_index_buffer ib;
522 struct _mesa_prim prim[1];
524 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count,
528 if (end >= ctx->Array.ArrayObj->_MaxElement) {
529 /* the max element is out of bounds of one or more enabled arrays */
530 _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, count %d, "
531 "type 0x%x) index=%u is out of bounds (max=%u)",
532 start, end, count, type, end,
533 ctx->Array.ArrayObj->_MaxElement - 1);
535 _mesa_print_arrays(ctx);
540 check_draw_elements_data(ctx, count, type, indices);
542 (void) check_draw_elements_data;
545 FLUSH_CURRENT( ctx, 0 );
548 _mesa_update_state( ctx );
550 if (!vbo_validate_shaders(ctx)) {
551 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawRangeElements(bad shader)");
558 _mesa_update_state( ctx );
562 ib.obj = ctx->Array.ElementArrayBufferObj;
571 prim[0].count = count;
574 /* Need to give special consideration to rendering a range of
575 * indices starting somewhere above zero. Typically the
576 * application is issuing multiple DrawRangeElements() to draw
577 * successive primitives layed out linearly in the vertex arrays.
578 * Unless the vertex arrays are all in a VBO (or locked as with
579 * CVA), the OpenGL semantics imply that we need to re-read or
580 * re-upload the vertex data on each draw call.
582 * In the case of hardware tnl, we want to avoid starting the
583 * upload at zero, as it will mean every draw call uploads an
584 * increasing amount of not-used vertex data. Worse - in the
585 * software tnl module, all those vertices might be transformed and
586 * lit but never rendered.
588 * If we just upload or transform the vertices in start..end,
589 * however, the indices will be incorrect.
591 * At this level, we don't know exactly what the requirements of
592 * the backend are going to be, though it will likely boil down to
595 * 1) Do nothing, everything is in a VBO and is processed once
598 * 2) Adjust the indices and vertex arrays so that start becomes
601 * Rather than doing anything here, I'll provide a helper function
602 * for the latter case elsewhere.
605 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, start, end );
609 static void GLAPIENTRY
610 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type,
611 const GLvoid *indices)
613 GET_CURRENT_CONTEXT(ctx);
614 GLuint min_index = 0;
615 GLuint max_index = 0;
617 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
620 if (!vbo_validate_shaders(ctx)) {
621 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawElements(bad shader)");
625 if (ctx->Array.ElementArrayBufferObj->Name) {
626 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
627 GL_ELEMENT_ARRAY_BUFFER_ARB,
629 ctx->Array.ElementArrayBufferObj);
631 get_minmax_index(count, type, ADD_POINTERS(map, indices),
632 &min_index, &max_index);
634 ctx->Driver.UnmapBuffer(ctx,
635 GL_ELEMENT_ARRAY_BUFFER_ARB,
636 ctx->Array.ElementArrayBufferObj);
639 get_minmax_index(count, type, indices, &min_index, &max_index);
642 vbo_exec_DrawRangeElements(mode, min_index, max_index, count, type, indices);
646 /***********************************************************************
651 vbo_exec_array_init( struct vbo_exec_context *exec )
654 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
655 exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
656 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
658 exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
659 exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
660 exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
666 vbo_exec_array_destroy( struct vbo_exec_context *exec )
672 /* This API entrypoint is not ordinarily used */
674 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
676 vbo_exec_DrawArrays(mode, first, count);
680 /* This API entrypoint is not ordinarily used */
682 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
683 const GLvoid *indices)
685 vbo_exec_DrawElements(mode, count, type, indices);
689 /* This API entrypoint is not ordinarily used */
691 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
692 GLenum type, const GLvoid *indices)
694 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);