vbo_exec_array.c revision 8e3f6c0f96eb22198ec436990acc85d44aca7d8e
1/************************************************************************** 2 * 3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "main/glheader.h" 29#include "main/context.h" 30#include "main/state.h" 31#include "main/api_validate.h" 32#include "main/api_noop.h" 33#include "main/varray.h" 34#include "main/bufferobj.h" 35#include "glapi/dispatch.h" 36 37#include "vbo_context.h" 38 39/** 40 * Compute min and max elements for glDraw[Range]Elements() calls. 41 */ 42static void get_minmax_index( GLuint count, GLuint type, 43 const GLvoid *indices, 44 GLuint *min_index, 45 GLuint *max_index) 46{ 47 GLuint i; 48 49 switch(type) { 50 case GL_UNSIGNED_INT: { 51 const GLuint *ui_indices = (const GLuint *)indices; 52 GLuint max_ui = ui_indices[count-1]; 53 GLuint min_ui = ui_indices[0]; 54 for (i = 0; i < count; i++) { 55 if (ui_indices[i] > max_ui) max_ui = ui_indices[i]; 56 if (ui_indices[i] < min_ui) min_ui = ui_indices[i]; 57 } 58 *min_index = min_ui; 59 *max_index = max_ui; 60 break; 61 } 62 case GL_UNSIGNED_SHORT: { 63 const GLushort *us_indices = (const GLushort *)indices; 64 GLuint max_us = us_indices[count-1]; 65 GLuint min_us = us_indices[0]; 66 for (i = 0; i < count; i++) { 67 if (us_indices[i] > max_us) max_us = us_indices[i]; 68 if (us_indices[i] < min_us) min_us = us_indices[i]; 69 } 70 *min_index = min_us; 71 *max_index = max_us; 72 break; 73 } 74 case GL_UNSIGNED_BYTE: { 75 const GLubyte *ub_indices = (const GLubyte *)indices; 76 GLuint max_ub = ub_indices[count-1]; 77 GLuint min_ub = ub_indices[0]; 78 for (i = 0; i < count; i++) { 79 if (ub_indices[i] > max_ub) max_ub = ub_indices[i]; 80 if (ub_indices[i] < min_ub) min_ub = ub_indices[i]; 81 } 82 *min_index = min_ub; 83 *max_index = max_ub; 84 break; 85 } 86 default: 87 assert(0); 88 break; 89 } 90} 91 92 93/** 94 * Just translate the arrayobj into a sane layout. 95 */ 96static void bind_array_obj( GLcontext *ctx ) 97{ 98 struct vbo_context *vbo = vbo_context(ctx); 99 struct vbo_exec_context *exec = &vbo->exec; 100 struct gl_array_object *arrayObj = ctx->Array.ArrayObj; 101 GLuint i; 102 103 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array 104 * rather than as individual named arrays. Then this function can 105 * go away. 106 */ 107 exec->array.legacy_array[VERT_ATTRIB_POS] = &arrayObj->Vertex; 108 exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &arrayObj->Weight; 109 exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &arrayObj->Normal; 110 exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &arrayObj->Color; 111 exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &arrayObj->SecondaryColor; 112 exec->array.legacy_array[VERT_ATTRIB_FOG] = &arrayObj->FogCoord; 113 exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &arrayObj->Index; 114 if (arrayObj->PointSize.Enabled) { 115 /* this aliases COLOR_INDEX */ 116 exec->array.legacy_array[VERT_ATTRIB_POINT_SIZE] = &arrayObj->PointSize; 117 } 118 exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &arrayObj->EdgeFlag; 119 120 for (i = 0; i < Elements(arrayObj->TexCoord); i++) 121 exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &arrayObj->TexCoord[i]; 122 123 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) { 124 assert(i < Elements(exec->array.generic_array)); 125 exec->array.generic_array[i] = &arrayObj->VertexAttrib[i]; 126 } 127 128 exec->array.array_obj = arrayObj->Name; 129} 130 131 132static void recalculate_input_bindings( GLcontext *ctx ) 133{ 134 struct vbo_context *vbo = vbo_context(ctx); 135 struct vbo_exec_context *exec = &vbo->exec; 136 const struct gl_client_array **inputs = &exec->array.inputs[0]; 137 GLbitfield const_inputs = 0x0; 138 GLuint i; 139 140 exec->array.program_mode = get_program_mode(ctx); 141 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled; 142 143 switch (exec->array.program_mode) { 144 case VP_NONE: 145 /* When no vertex program is active (or the vertex program is generated 146 * from fixed-function state). We put the material values into the 147 * generic slots. This is the only situation where material values 148 * are available as per-vertex attributes. 149 */ 150 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) { 151 if (exec->array.legacy_array[i]->Enabled) 152 inputs[i] = exec->array.legacy_array[i]; 153 else { 154 inputs[i] = &vbo->legacy_currval[i]; 155 const_inputs |= 1 << i; 156 } 157 } 158 159 for (i = 0; i < MAT_ATTRIB_MAX; i++) { 160 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i]; 161 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i); 162 } 163 164 /* Could use just about anything, just to fill in the empty 165 * slots: 166 */ 167 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX - VERT_ATTRIB_GENERIC0; i++) { 168 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i]; 169 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i); 170 } 171 break; 172 173 case VP_NV: 174 /* NV_vertex_program - attribute arrays alias and override 175 * conventional, legacy arrays. No materials, and the generic 176 * slots are vacant. 177 */ 178 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) { 179 if (exec->array.generic_array[i]->Enabled) 180 inputs[i] = exec->array.generic_array[i]; 181 else if (exec->array.legacy_array[i]->Enabled) 182 inputs[i] = exec->array.legacy_array[i]; 183 else { 184 inputs[i] = &vbo->legacy_currval[i]; 185 const_inputs |= 1 << i; 186 } 187 } 188 189 /* Could use just about anything, just to fill in the empty 190 * slots: 191 */ 192 for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++) { 193 inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0]; 194 const_inputs |= 1 << i; 195 } 196 break; 197 198 case VP_ARB: 199 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0] 200 * attribute array aliases and overrides the legacy position array. 201 * 202 * Otherwise, legacy attributes available in the legacy slots, 203 * generic attributes in the generic slots and materials are not 204 * available as per-vertex attributes. 205 */ 206 if (exec->array.generic_array[0]->Enabled) 207 inputs[0] = exec->array.generic_array[0]; 208 else if (exec->array.legacy_array[0]->Enabled) 209 inputs[0] = exec->array.legacy_array[0]; 210 else { 211 inputs[0] = &vbo->legacy_currval[0]; 212 const_inputs |= 1 << 0; 213 } 214 215 for (i = 1; i <= VERT_ATTRIB_TEX7; i++) { 216 if (exec->array.legacy_array[i]->Enabled) 217 inputs[i] = exec->array.legacy_array[i]; 218 else { 219 inputs[i] = &vbo->legacy_currval[i]; 220 const_inputs |= 1 << i; 221 } 222 } 223 224 for (i = 0; i < MAX_VERTEX_GENERIC_ATTRIBS; i++) { 225 if (exec->array.generic_array[i]->Enabled) 226 inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i]; 227 else { 228 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i]; 229 const_inputs |= 1 << (VERT_ATTRIB_GENERIC0 + i); 230 } 231 232 } 233 break; 234 } 235 236 _mesa_set_varying_vp_inputs( ctx, ~const_inputs ); 237} 238 239 240static void bind_arrays( GLcontext *ctx ) 241{ 242#if 0 243 if (ctx->Array.ArrayObj.Name != exec->array.array_obj) { 244 bind_array_obj(ctx); 245 recalculate_input_bindings(ctx); 246 } 247 else if (exec->array.program_mode != get_program_mode(ctx) || 248 exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) { 249 250 recalculate_input_bindings(ctx); 251 } 252#else 253 bind_array_obj(ctx); 254 recalculate_input_bindings(ctx); 255#endif 256} 257 258 259 260/*********************************************************************** 261 * API functions. 262 */ 263 264static void GLAPIENTRY 265vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count) 266{ 267 GET_CURRENT_CONTEXT(ctx); 268 struct vbo_context *vbo = vbo_context(ctx); 269 struct vbo_exec_context *exec = &vbo->exec; 270 struct _mesa_prim prim[1]; 271 272 if (!_mesa_validate_DrawArrays( ctx, mode, start, count )) 273 return; 274 275 FLUSH_CURRENT( ctx, 0 ); 276 277 if (ctx->NewState) 278 _mesa_update_state( ctx ); 279 280 if (!vbo_validate_shaders(ctx)) { 281 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawArrays(bad shader)"); 282 return; 283 } 284 285 bind_arrays( ctx ); 286 287 /* Again... because we may have changed the bitmask of per-vertex varying 288 * attributes. If we regenerate the fixed-function vertex program now 289 * we may be able to prune down the number of vertex attributes which we 290 * need in the shader. 291 */ 292 if (ctx->NewState) 293 _mesa_update_state( ctx ); 294 295 prim[0].begin = 1; 296 prim[0].end = 1; 297 prim[0].weak = 0; 298 prim[0].pad = 0; 299 prim[0].mode = mode; 300 prim[0].start = start; 301 prim[0].count = count; 302 prim[0].indexed = 0; 303 304 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL, 305 start, start + count - 1 ); 306 307#if 0 308 { 309 int i; 310 311 _mesa_printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n", 312 mode, start, count); 313 314 for (i = 0; i < 32; i++) { 315 GLuint bufName = exec->array.inputs[i]->BufferObj->Name; 316 GLint stride = exec->array.inputs[i]->Stride; 317 _mesa_printf("attr %2d: size %d stride %d enabled %d " 318 "ptr %p Bufobj %u\n", 319 i, 320 exec->array.inputs[i]->Size, 321 stride, 322 /*exec->array.inputs[i]->Enabled,*/ 323 exec->array.legacy_array[i]->Enabled, 324 exec->array.inputs[i]->Ptr, 325 bufName); 326 327 if (bufName) { 328 struct gl_buffer_object *buf = _mesa_lookup_bufferobj(ctx, bufName); 329 GLubyte *p = ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER_ARB, 330 GL_READ_ONLY_ARB, buf); 331 int offset = (int) exec->array.inputs[i]->Ptr; 332 float *f = (float *) (p + offset); 333 int *k = (int *) f; 334 int i; 335 int n = (count * stride) / 4; 336 if (n > 32) 337 n = 32; 338 _mesa_printf(" Data at offset %d:\n", offset); 339 for (i = 0; i < n; i++) { 340 _mesa_printf(" float[%d] = 0x%08x %f\n", i, k[i], f[i]); 341 } 342 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, buf); 343 } 344 } 345 } 346#endif 347} 348 349 350static void GLAPIENTRY 351vbo_exec_DrawRangeElements(GLenum mode, 352 GLuint start, GLuint end, 353 GLsizei count, GLenum type, const GLvoid *indices) 354{ 355 GET_CURRENT_CONTEXT(ctx); 356 struct vbo_context *vbo = vbo_context(ctx); 357 struct vbo_exec_context *exec = &vbo->exec; 358 struct _mesa_index_buffer ib; 359 struct _mesa_prim prim[1]; 360 361 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count, type, indices )) 362 return; 363 364 if (end >= ctx->Array.ArrayObj->_MaxElement) { 365 /* the max element is out of bounds of one or more enabled arrays */ 366 _mesa_warning(ctx, "glDraw[Range]Elements(start %u, end %u, count %d, " 367 "type 0x%x) index=%u is out of bounds (max=%u)", 368 start, end, count, type, end); 369 if (0) 370 _mesa_print_arrays(ctx); 371 return; 372 } 373 374 FLUSH_CURRENT( ctx, 0 ); 375 376 if (ctx->NewState) 377 _mesa_update_state( ctx ); 378 379 if (!vbo_validate_shaders(ctx)) { 380 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawRangeElements(bad shader)"); 381 return; 382 } 383 384 bind_arrays( ctx ); 385 386 if (ctx->NewState) 387 _mesa_update_state( ctx ); 388 389 ib.count = count; 390 ib.type = type; 391 ib.obj = ctx->Array.ElementArrayBufferObj; 392 ib.ptr = indices; 393 394 prim[0].begin = 1; 395 prim[0].end = 1; 396 prim[0].weak = 0; 397 prim[0].pad = 0; 398 prim[0].mode = mode; 399 prim[0].start = 0; 400 prim[0].count = count; 401 prim[0].indexed = 1; 402 403 /* Need to give special consideration to rendering a range of 404 * indices starting somewhere above zero. Typically the 405 * application is issuing multiple DrawRangeElements() to draw 406 * successive primitives layed out linearly in the vertex arrays. 407 * Unless the vertex arrays are all in a VBO (or locked as with 408 * CVA), the OpenGL semantics imply that we need to re-read or 409 * re-upload the vertex data on each draw call. 410 * 411 * In the case of hardware tnl, we want to avoid starting the 412 * upload at zero, as it will mean every draw call uploads an 413 * increasing amount of not-used vertex data. Worse - in the 414 * software tnl module, all those vertices might be transformed and 415 * lit but never rendered. 416 * 417 * If we just upload or transform the vertices in start..end, 418 * however, the indices will be incorrect. 419 * 420 * At this level, we don't know exactly what the requirements of 421 * the backend are going to be, though it will likely boil down to 422 * either: 423 * 424 * 1) Do nothing, everything is in a VBO and is processed once 425 * only. 426 * 427 * 2) Adjust the indices and vertex arrays so that start becomes 428 * zero. 429 * 430 * Rather than doing anything here, I'll provide a helper function 431 * for the latter case elsewhere. 432 */ 433 434 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, start, end ); 435} 436 437 438static void GLAPIENTRY 439vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) 440{ 441 GET_CURRENT_CONTEXT(ctx); 442 GLuint min_index = 0; 443 GLuint max_index = 0; 444 445 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices )) 446 return; 447 448 if (!vbo_validate_shaders(ctx)) { 449 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawElements(bad shader)"); 450 return; 451 } 452 453 if (ctx->Array.ElementArrayBufferObj->Name) { 454 const GLvoid *map = ctx->Driver.MapBuffer(ctx, 455 GL_ELEMENT_ARRAY_BUFFER_ARB, 456 GL_READ_ONLY, 457 ctx->Array.ElementArrayBufferObj); 458 459 get_minmax_index(count, type, ADD_POINTERS(map, indices), &min_index, &max_index); 460 461 ctx->Driver.UnmapBuffer(ctx, 462 GL_ELEMENT_ARRAY_BUFFER_ARB, 463 ctx->Array.ElementArrayBufferObj); 464 } 465 else { 466 get_minmax_index(count, type, indices, &min_index, &max_index); 467 } 468 469 vbo_exec_DrawRangeElements(mode, min_index, max_index, count, type, indices); 470} 471 472 473/*********************************************************************** 474 * Initialization 475 */ 476 477void vbo_exec_array_init( struct vbo_exec_context *exec ) 478{ 479#if 1 480 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays; 481 exec->vtxfmt.DrawElements = vbo_exec_DrawElements; 482 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements; 483#else 484 exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays; 485 exec->vtxfmt.DrawElements = _mesa_noop_DrawElements; 486 exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements; 487#endif 488} 489 490 491void vbo_exec_array_destroy( struct vbo_exec_context *exec ) 492{ 493 /* nothing to do */ 494} 495 496 497/* This API entrypoint is not ordinarily used */ 498void GLAPIENTRY 499_mesa_DrawArrays(GLenum mode, GLint first, GLsizei count) 500{ 501 vbo_exec_DrawArrays(mode, first, count); 502} 503 504 505/* This API entrypoint is not ordinarily used */ 506void GLAPIENTRY 507_mesa_DrawElements(GLenum mode, GLsizei count, GLenum type, 508 const GLvoid *indices) 509{ 510 vbo_exec_DrawElements(mode, count, type, indices); 511} 512 513 514/* This API entrypoint is not ordinarily used */ 515void GLAPIENTRY 516_mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, 517 GLenum type, const GLvoid *indices) 518{ 519 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices); 520} 521