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"
41 * Compute min and max elements for glDraw[Range]Elements() calls.
43 static void get_minmax_index( GLuint count, GLuint type,
44 const GLvoid *indices,
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.
285 static void bind_array_obj( GLcontext *ctx )
287 struct vbo_context *vbo = vbo_context(ctx);
288 struct vbo_exec_context *exec = &vbo->exec;
289 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
292 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
293 * rather than as individual named arrays. Then this function can
296 exec->array.legacy_array[VERT_ATTRIB_POS] = &arrayObj->Vertex;
297 exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &arrayObj->Weight;
298 exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &arrayObj->Normal;
299 exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &arrayObj->Color;
300 exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &arrayObj->SecondaryColor;
301 exec->array.legacy_array[VERT_ATTRIB_FOG] = &arrayObj->FogCoord;
302 exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &arrayObj->Index;
303 if (arrayObj->PointSize.Enabled) {
304 /* this aliases COLOR_INDEX */
305 exec->array.legacy_array[VERT_ATTRIB_POINT_SIZE] = &arrayObj->PointSize;
307 exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &arrayObj->EdgeFlag;
309 for (i = 0; i < Elements(arrayObj->TexCoord); i++)
310 exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &arrayObj->TexCoord[i];
312 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
313 assert(i < Elements(exec->array.generic_array));
314 exec->array.generic_array[i] = &arrayObj->VertexAttrib[i];
317 exec->array.array_obj = arrayObj->Name;
321 static void recalculate_input_bindings( GLcontext *ctx )
323 struct vbo_context *vbo = vbo_context(ctx);
324 struct vbo_exec_context *exec = &vbo->exec;
325 const struct gl_client_array **inputs = &exec->array.inputs[0];
326 GLbitfield const_inputs = 0x0;
329 exec->array.program_mode = get_program_mode(ctx);
330 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
332 switch (exec->array.program_mode) {
334 /* When no vertex program is active (or the vertex program is generated
335 * from fixed-function state). We put the material values into the
336 * generic slots. This is the only situation where material values
337 * are available as per-vertex attributes.
339 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
340 if (exec->array.legacy_array[i]->Enabled)
341 inputs[i] = exec->array.legacy_array[i];
343 inputs[i] = &vbo->legacy_currval[i];
344 const_inputs |= 1 << i;
348 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
349 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i];
350 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
353 /* Could use just about anything, just to fill in the empty
356 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX - VERT_ATTRIB_GENERIC0; i++) {
357 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
358 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
363 /* NV_vertex_program - attribute arrays alias and override
364 * conventional, legacy arrays. No materials, and the generic
367 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
368 if (exec->array.generic_array[i]->Enabled)
369 inputs[i] = exec->array.generic_array[i];
370 else if (exec->array.legacy_array[i]->Enabled)
371 inputs[i] = exec->array.legacy_array[i];
373 inputs[i] = &vbo->legacy_currval[i];
374 const_inputs |= 1 << i;
378 /* Could use just about anything, just to fill in the empty
381 for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++) {
382 inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0];
383 const_inputs |= 1 << i;
388 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
389 * attribute array aliases and overrides the legacy position array.
391 * Otherwise, legacy attributes available in the legacy slots,
392 * generic attributes in the generic slots and materials are not
393 * available as per-vertex attributes.
395 if (exec->array.generic_array[0]->Enabled)
396 inputs[0] = exec->array.generic_array[0];
397 else if (exec->array.legacy_array[0]->Enabled)
398 inputs[0] = exec->array.legacy_array[0];
400 inputs[0] = &vbo->legacy_currval[0];
401 const_inputs |= 1 << 0;
404 for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
405 if (exec->array.legacy_array[i]->Enabled)
406 inputs[i] = exec->array.legacy_array[i];
408 inputs[i] = &vbo->legacy_currval[i];
409 const_inputs |= 1 << i;
413 for (i = 0; i < MAX_VERTEX_GENERIC_ATTRIBS; i++) {
414 if (exec->array.generic_array[i]->Enabled)
415 inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
417 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
418 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i);
425 _mesa_set_varying_vp_inputs( ctx, ~const_inputs );
429 static void bind_arrays( GLcontext *ctx )
432 if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
434 recalculate_input_bindings(ctx);
436 else if (exec->array.program_mode != get_program_mode(ctx) ||
437 exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
439 recalculate_input_bindings(ctx);
443 recalculate_input_bindings(ctx);
449 /***********************************************************************
453 static void GLAPIENTRY
454 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
456 GET_CURRENT_CONTEXT(ctx);
457 struct vbo_context *vbo = vbo_context(ctx);
458 struct vbo_exec_context *exec = &vbo->exec;
459 struct _mesa_prim prim[1];
461 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
464 FLUSH_CURRENT( ctx, 0 );
467 _mesa_update_state( ctx );
469 if (!vbo_validate_shaders(ctx)) {
470 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawArrays(bad shader)");
475 check_draw_arrays_data(ctx, start, count);
477 (void) check_draw_arrays_data;
483 /* Again... because we may have changed the bitmask of per-vertex varying
484 * attributes. If we regenerate the fixed-function vertex program now
485 * we may be able to prune down the number of vertex attributes which we
486 * need in the shader.
489 _mesa_update_state( ctx );
496 prim[0].start = start;
497 prim[0].count = count;
500 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL,
501 start, start + count - 1 );
504 print_draw_arrays(ctx, exec, mode, start, count);
506 (void) print_draw_arrays;
511 static void GLAPIENTRY
512 vbo_exec_DrawRangeElements(GLenum mode,
513 GLuint start, GLuint end,
514 GLsizei count, GLenum type, const GLvoid *indices)
516 GET_CURRENT_CONTEXT(ctx);
517 struct vbo_context *vbo = vbo_context(ctx);
518 struct vbo_exec_context *exec = &vbo->exec;
519 struct _mesa_index_buffer ib;
520 struct _mesa_prim prim[1];
522 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count, type, indices ))
525 if (end >= ctx->Array.ArrayObj->_MaxElement) {
526 /* the max element is out of bounds of one or more enabled arrays */
527 _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, count %d, "
528 "type 0x%x) index=%u is out of bounds (max=%u)",
529 start, end, count, type, end,
530 ctx->Array.ArrayObj->_MaxElement - 1);
532 _mesa_print_arrays(ctx);
537 check_draw_elements_data(ctx, count, type, indices);
539 (void) check_draw_elements_data;
542 FLUSH_CURRENT( ctx, 0 );
545 _mesa_update_state( ctx );
547 if (!vbo_validate_shaders(ctx)) {
548 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawRangeElements(bad shader)");
555 _mesa_update_state( ctx );
559 ib.obj = ctx->Array.ElementArrayBufferObj;
568 prim[0].count = count;
571 /* Need to give special consideration to rendering a range of
572 * indices starting somewhere above zero. Typically the
573 * application is issuing multiple DrawRangeElements() to draw
574 * successive primitives layed out linearly in the vertex arrays.
575 * Unless the vertex arrays are all in a VBO (or locked as with
576 * CVA), the OpenGL semantics imply that we need to re-read or
577 * re-upload the vertex data on each draw call.
579 * In the case of hardware tnl, we want to avoid starting the
580 * upload at zero, as it will mean every draw call uploads an
581 * increasing amount of not-used vertex data. Worse - in the
582 * software tnl module, all those vertices might be transformed and
583 * lit but never rendered.
585 * If we just upload or transform the vertices in start..end,
586 * however, the indices will be incorrect.
588 * At this level, we don't know exactly what the requirements of
589 * the backend are going to be, though it will likely boil down to
592 * 1) Do nothing, everything is in a VBO and is processed once
595 * 2) Adjust the indices and vertex arrays so that start becomes
598 * Rather than doing anything here, I'll provide a helper function
599 * for the latter case elsewhere.
602 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, start, end );
606 static void GLAPIENTRY
607 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
609 GET_CURRENT_CONTEXT(ctx);
610 GLuint min_index = 0;
611 GLuint max_index = 0;
613 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
616 if (!vbo_validate_shaders(ctx)) {
617 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawElements(bad shader)");
621 if (ctx->Array.ElementArrayBufferObj->Name) {
622 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
623 GL_ELEMENT_ARRAY_BUFFER_ARB,
625 ctx->Array.ElementArrayBufferObj);
627 get_minmax_index(count, type, ADD_POINTERS(map, indices), &min_index, &max_index);
629 ctx->Driver.UnmapBuffer(ctx,
630 GL_ELEMENT_ARRAY_BUFFER_ARB,
631 ctx->Array.ElementArrayBufferObj);
634 get_minmax_index(count, type, indices, &min_index, &max_index);
637 vbo_exec_DrawRangeElements(mode, min_index, max_index, count, type, indices);
641 /***********************************************************************
645 void vbo_exec_array_init( struct vbo_exec_context *exec )
648 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
649 exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
650 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
652 exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
653 exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
654 exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
659 void vbo_exec_array_destroy( struct vbo_exec_context *exec )
665 /* This API entrypoint is not ordinarily used */
667 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
669 vbo_exec_DrawArrays(mode, first, count);
673 /* This API entrypoint is not ordinarily used */
675 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
676 const GLvoid *indices)
678 vbo_exec_DrawElements(mode, count, type, indices);
682 /* This API entrypoint is not ordinarily used */
684 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
685 GLenum type, const GLvoid *indices)
687 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);