feedback.c revision 4a49301e
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.5 4 * 5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions 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 MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26/** 27 * \file feedback.c 28 * Selection and feedback modes functions. 29 */ 30 31 32#include "glheader.h" 33#include "colormac.h" 34#include "context.h" 35#include "enums.h" 36#include "feedback.h" 37#include "macros.h" 38#include "mtypes.h" 39#include "glapi/dispatch.h" 40 41 42#if FEATURE_feedback 43 44 45#define FB_3D 0x01 46#define FB_4D 0x02 47#define FB_INDEX 0x04 48#define FB_COLOR 0x08 49#define FB_TEXTURE 0X10 50 51 52 53static void GLAPIENTRY 54_mesa_FeedbackBuffer( GLsizei size, GLenum type, GLfloat *buffer ) 55{ 56 GET_CURRENT_CONTEXT(ctx); 57 ASSERT_OUTSIDE_BEGIN_END(ctx); 58 59 if (ctx->RenderMode==GL_FEEDBACK) { 60 _mesa_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" ); 61 return; 62 } 63 if (size<0) { 64 _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" ); 65 return; 66 } 67 if (!buffer) { 68 _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" ); 69 ctx->Feedback.BufferSize = 0; 70 return; 71 } 72 73 switch (type) { 74 case GL_2D: 75 ctx->Feedback._Mask = 0; 76 break; 77 case GL_3D: 78 ctx->Feedback._Mask = FB_3D; 79 break; 80 case GL_3D_COLOR: 81 ctx->Feedback._Mask = (FB_3D | 82 (ctx->Visual.rgbMode ? FB_COLOR : FB_INDEX)); 83 break; 84 case GL_3D_COLOR_TEXTURE: 85 ctx->Feedback._Mask = (FB_3D | 86 (ctx->Visual.rgbMode ? FB_COLOR : FB_INDEX) | 87 FB_TEXTURE); 88 break; 89 case GL_4D_COLOR_TEXTURE: 90 ctx->Feedback._Mask = (FB_3D | FB_4D | 91 (ctx->Visual.rgbMode ? FB_COLOR : FB_INDEX) | 92 FB_TEXTURE); 93 break; 94 default: 95 _mesa_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" ); 96 return; 97 } 98 99 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); /* Always flush */ 100 ctx->Feedback.Type = type; 101 ctx->Feedback.BufferSize = size; 102 ctx->Feedback.Buffer = buffer; 103 ctx->Feedback.Count = 0; /* Becaues of this. */ 104} 105 106 107static void GLAPIENTRY 108_mesa_PassThrough( GLfloat token ) 109{ 110 GET_CURRENT_CONTEXT(ctx); 111 ASSERT_OUTSIDE_BEGIN_END(ctx); 112 113 if (ctx->RenderMode==GL_FEEDBACK) { 114 FLUSH_VERTICES(ctx, 0); 115 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN ); 116 _mesa_feedback_token( ctx, token ); 117 } 118} 119 120 121/** 122 * Put a vertex into the feedback buffer. 123 */ 124void 125_mesa_feedback_vertex(GLcontext *ctx, 126 const GLfloat win[4], 127 const GLfloat color[4], 128 GLfloat index, 129 const GLfloat texcoord[4]) 130{ 131 _mesa_feedback_token( ctx, win[0] ); 132 _mesa_feedback_token( ctx, win[1] ); 133 if (ctx->Feedback._Mask & FB_3D) { 134 _mesa_feedback_token( ctx, win[2] ); 135 } 136 if (ctx->Feedback._Mask & FB_4D) { 137 _mesa_feedback_token( ctx, win[3] ); 138 } 139 if (ctx->Feedback._Mask & FB_INDEX) { 140 _mesa_feedback_token( ctx, (GLfloat) index ); 141 } 142 if (ctx->Feedback._Mask & FB_COLOR) { 143 _mesa_feedback_token( ctx, color[0] ); 144 _mesa_feedback_token( ctx, color[1] ); 145 _mesa_feedback_token( ctx, color[2] ); 146 _mesa_feedback_token( ctx, color[3] ); 147 } 148 if (ctx->Feedback._Mask & FB_TEXTURE) { 149 _mesa_feedback_token( ctx, texcoord[0] ); 150 _mesa_feedback_token( ctx, texcoord[1] ); 151 _mesa_feedback_token( ctx, texcoord[2] ); 152 _mesa_feedback_token( ctx, texcoord[3] ); 153 } 154} 155 156 157/**********************************************************************/ 158/** \name Selection */ 159/*@{*/ 160 161/** 162 * Establish a buffer for selection mode values. 163 * 164 * \param size buffer size. 165 * \param buffer buffer. 166 * 167 * \sa glSelectBuffer(). 168 * 169 * \note this function can't be put in a display list. 170 * 171 * Verifies we're not in selection mode, flushes the vertices and initialize 172 * the fields in __GLcontextRec::Select with the given buffer. 173 */ 174static void GLAPIENTRY 175_mesa_SelectBuffer( GLsizei size, GLuint *buffer ) 176{ 177 GET_CURRENT_CONTEXT(ctx); 178 ASSERT_OUTSIDE_BEGIN_END(ctx); 179 180 if (ctx->RenderMode==GL_SELECT) { 181 _mesa_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" ); 182 return; /* KW: added return */ 183 } 184 185 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); 186 ctx->Select.Buffer = buffer; 187 ctx->Select.BufferSize = size; 188 ctx->Select.BufferCount = 0; 189 ctx->Select.HitFlag = GL_FALSE; 190 ctx->Select.HitMinZ = 1.0; 191 ctx->Select.HitMaxZ = 0.0; 192} 193 194 195/** 196 * Write a value of a record into the selection buffer. 197 * 198 * \param ctx GL context. 199 * \param value value. 200 * 201 * Verifies there is free space in the buffer to write the value and 202 * increments the pointer. 203 */ 204static INLINE void 205write_record(GLcontext *ctx, GLuint value) 206{ 207 if (ctx->Select.BufferCount < ctx->Select.BufferSize) { 208 ctx->Select.Buffer[ctx->Select.BufferCount] = value; 209 } 210 ctx->Select.BufferCount++; 211} 212 213 214/** 215 * Update the hit flag and the maximum and minimum depth values. 216 * 217 * \param ctx GL context. 218 * \param z depth. 219 * 220 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and 221 * gl_selection::HitMaxZ. 222 */ 223void 224_mesa_update_hitflag(GLcontext *ctx, GLfloat z) 225{ 226 ctx->Select.HitFlag = GL_TRUE; 227 if (z < ctx->Select.HitMinZ) { 228 ctx->Select.HitMinZ = z; 229 } 230 if (z > ctx->Select.HitMaxZ) { 231 ctx->Select.HitMaxZ = z; 232 } 233} 234 235 236/** 237 * Write the hit record. 238 * 239 * \param ctx GL context. 240 * 241 * Write the hit record, i.e., the number of names in the stack, the minimum and 242 * maximum depth values and the number of names in the name stack at the time 243 * of the event. Resets the hit flag. 244 * 245 * \sa gl_selection. 246 */ 247static void 248write_hit_record(GLcontext *ctx) 249{ 250 GLuint i; 251 GLuint zmin, zmax, zscale = (~0u); 252 253 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */ 254 /* 2^32-1 and round to nearest unsigned integer. */ 255 256 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */ 257 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ); 258 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ); 259 260 write_record( ctx, ctx->Select.NameStackDepth ); 261 write_record( ctx, zmin ); 262 write_record( ctx, zmax ); 263 for (i = 0; i < ctx->Select.NameStackDepth; i++) { 264 write_record( ctx, ctx->Select.NameStack[i] ); 265 } 266 267 ctx->Select.Hits++; 268 ctx->Select.HitFlag = GL_FALSE; 269 ctx->Select.HitMinZ = 1.0; 270 ctx->Select.HitMaxZ = -1.0; 271} 272 273 274/** 275 * Initialize the name stack. 276 * 277 * Verifies we are in select mode and resets the name stack depth and resets 278 * the hit record data in gl_selection. Marks new render mode in 279 * __GLcontextRec::NewState. 280 */ 281static void GLAPIENTRY 282_mesa_InitNames( void ) 283{ 284 GET_CURRENT_CONTEXT(ctx); 285 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); 286 287 /* Record the hit before the HitFlag is wiped out again. */ 288 if (ctx->RenderMode == GL_SELECT) { 289 if (ctx->Select.HitFlag) { 290 write_hit_record( ctx ); 291 } 292 } 293 ctx->Select.NameStackDepth = 0; 294 ctx->Select.HitFlag = GL_FALSE; 295 ctx->Select.HitMinZ = 1.0; 296 ctx->Select.HitMaxZ = 0.0; 297 ctx->NewState |= _NEW_RENDERMODE; 298} 299 300 301/** 302 * Load the top-most name of the name stack. 303 * 304 * \param name name. 305 * 306 * Verifies we are in selection mode and that the name stack is not empty. 307 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()), 308 * and replace the top-most name in the stack. 309 * 310 * sa __GLcontextRec::Select. 311 */ 312static void GLAPIENTRY 313_mesa_LoadName( GLuint name ) 314{ 315 GET_CURRENT_CONTEXT(ctx); 316 ASSERT_OUTSIDE_BEGIN_END(ctx); 317 318 if (ctx->RenderMode != GL_SELECT) { 319 return; 320 } 321 if (ctx->Select.NameStackDepth == 0) { 322 _mesa_error( ctx, GL_INVALID_OPERATION, "glLoadName" ); 323 return; 324 } 325 326 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); 327 328 if (ctx->Select.HitFlag) { 329 write_hit_record( ctx ); 330 } 331 if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) { 332 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name; 333 } 334 else { 335 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name; 336 } 337} 338 339 340/** 341 * Push a name into the name stack. 342 * 343 * \param name name. 344 * 345 * Verifies we are in selection mode and that the name stack is not full. 346 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()), 347 * and adds the name to the top of the name stack. 348 * 349 * sa __GLcontextRec::Select. 350 */ 351static void GLAPIENTRY 352_mesa_PushName( GLuint name ) 353{ 354 GET_CURRENT_CONTEXT(ctx); 355 ASSERT_OUTSIDE_BEGIN_END(ctx); 356 357 if (ctx->RenderMode != GL_SELECT) { 358 return; 359 } 360 361 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); 362 if (ctx->Select.HitFlag) { 363 write_hit_record( ctx ); 364 } 365 if (ctx->Select.NameStackDepth >= MAX_NAME_STACK_DEPTH) { 366 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushName" ); 367 } 368 else 369 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name; 370} 371 372 373/** 374 * Pop a name into the name stack. 375 * 376 * Verifies we are in selection mode and that the name stack is not empty. 377 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()), 378 * and removes top-most name in the name stack. 379 * 380 * sa __GLcontextRec::Select. 381 */ 382static void GLAPIENTRY 383_mesa_PopName( void ) 384{ 385 GET_CURRENT_CONTEXT(ctx); 386 ASSERT_OUTSIDE_BEGIN_END(ctx); 387 388 if (ctx->RenderMode != GL_SELECT) { 389 return; 390 } 391 392 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); 393 if (ctx->Select.HitFlag) { 394 write_hit_record( ctx ); 395 } 396 if (ctx->Select.NameStackDepth == 0) { 397 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopName" ); 398 } 399 else 400 ctx->Select.NameStackDepth--; 401} 402 403/*@}*/ 404 405 406/**********************************************************************/ 407/** \name Render Mode */ 408/*@{*/ 409 410/** 411 * Set rasterization mode. 412 * 413 * \param mode rasterization mode. 414 * 415 * \note this function can't be put in a display list. 416 * 417 * \sa glRenderMode(). 418 * 419 * Flushes the vertices and do the necessary cleanup according to the previous 420 * rasterization mode, such as writing the hit record or resent the select 421 * buffer index when exiting the select mode. Updates 422 * __GLcontextRec::RenderMode and notifies the driver via the 423 * dd_function_table::RenderMode callback. 424 */ 425static GLint GLAPIENTRY 426_mesa_RenderMode( GLenum mode ) 427{ 428 GET_CURRENT_CONTEXT(ctx); 429 GLint result; 430 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0); 431 432 if (MESA_VERBOSE & VERBOSE_API) 433 _mesa_debug(ctx, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode)); 434 435 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); 436 437 switch (ctx->RenderMode) { 438 case GL_RENDER: 439 result = 0; 440 break; 441 case GL_SELECT: 442 if (ctx->Select.HitFlag) { 443 write_hit_record( ctx ); 444 } 445 if (ctx->Select.BufferCount > ctx->Select.BufferSize) { 446 /* overflow */ 447#ifdef DEBUG 448 _mesa_warning(ctx, "Feedback buffer overflow"); 449#endif 450 result = -1; 451 } 452 else { 453 result = ctx->Select.Hits; 454 } 455 ctx->Select.BufferCount = 0; 456 ctx->Select.Hits = 0; 457 ctx->Select.NameStackDepth = 0; 458 break; 459#if _HAVE_FULL_GL 460 case GL_FEEDBACK: 461 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) { 462 /* overflow */ 463 result = -1; 464 } 465 else { 466 result = ctx->Feedback.Count; 467 } 468 ctx->Feedback.Count = 0; 469 break; 470#endif 471 default: 472 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" ); 473 return 0; 474 } 475 476 switch (mode) { 477 case GL_RENDER: 478 break; 479 case GL_SELECT: 480 if (ctx->Select.BufferSize==0) { 481 /* haven't called glSelectBuffer yet */ 482 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" ); 483 } 484 break; 485#if _HAVE_FULL_GL 486 case GL_FEEDBACK: 487 if (ctx->Feedback.BufferSize==0) { 488 /* haven't called glFeedbackBuffer yet */ 489 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" ); 490 } 491 break; 492#endif 493 default: 494 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" ); 495 return 0; 496 } 497 498 ctx->RenderMode = mode; 499 if (ctx->Driver.RenderMode) 500 ctx->Driver.RenderMode( ctx, mode ); 501 502 return result; 503} 504 505/*@}*/ 506 507 508void 509_mesa_init_feedback_dispatch(struct _glapi_table *disp) 510{ 511 SET_InitNames(disp, _mesa_InitNames); 512 SET_FeedbackBuffer(disp, _mesa_FeedbackBuffer); 513 SET_LoadName(disp, _mesa_LoadName); 514 SET_PassThrough(disp, _mesa_PassThrough); 515 SET_PopName(disp, _mesa_PopName); 516 SET_PushName(disp, _mesa_PushName); 517 SET_SelectBuffer(disp, _mesa_SelectBuffer); 518 SET_RenderMode(disp, _mesa_RenderMode); 519} 520 521 522#endif /* FEATURE_feedback */ 523 524 525/**********************************************************************/ 526/** \name Initialization */ 527/*@{*/ 528 529/** 530 * Initialize context feedback data. 531 */ 532void _mesa_init_feedback( GLcontext * ctx ) 533{ 534 /* Feedback */ 535 ctx->Feedback.Type = GL_2D; /* TODO: verify */ 536 ctx->Feedback.Buffer = NULL; 537 ctx->Feedback.BufferSize = 0; 538 ctx->Feedback.Count = 0; 539 540 /* Selection/picking */ 541 ctx->Select.Buffer = NULL; 542 ctx->Select.BufferSize = 0; 543 ctx->Select.BufferCount = 0; 544 ctx->Select.Hits = 0; 545 ctx->Select.NameStackDepth = 0; 546 547 /* Miscellaneous */ 548 ctx->RenderMode = GL_RENDER; 549} 550 551/*@}*/ 552