2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
28 * Selection and feedback modes functions.
38 #include "mfeatures.h"
40 #include "main/dispatch.h"
49 #define FB_TEXTURE 0X08
53 static void GLAPIENTRY
54 _mesa_FeedbackBuffer( GLsizei size, GLenum type, GLfloat *buffer )
56 GET_CURRENT_CONTEXT(ctx);
57 ASSERT_OUTSIDE_BEGIN_END(ctx);
59 if (ctx->RenderMode==GL_FEEDBACK) {
60 _mesa_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
64 _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
67 if (!buffer && size > 0) {
68 _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
69 ctx->Feedback.BufferSize = 0;
75 ctx->Feedback._Mask = 0;
78 ctx->Feedback._Mask = FB_3D;
81 ctx->Feedback._Mask = (FB_3D | FB_COLOR);
83 case GL_3D_COLOR_TEXTURE:
84 ctx->Feedback._Mask = (FB_3D | FB_COLOR | FB_TEXTURE);
86 case GL_4D_COLOR_TEXTURE:
87 ctx->Feedback._Mask = (FB_3D | FB_4D | FB_COLOR | FB_TEXTURE);
90 _mesa_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
94 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); /* Always flush */
95 ctx->Feedback.Type = type;
96 ctx->Feedback.BufferSize = size;
97 ctx->Feedback.Buffer = buffer;
98 ctx->Feedback.Count = 0; /* Becaues of this. */
102 static void GLAPIENTRY
103 _mesa_PassThrough( GLfloat token )
105 GET_CURRENT_CONTEXT(ctx);
106 ASSERT_OUTSIDE_BEGIN_END(ctx);
108 if (ctx->RenderMode==GL_FEEDBACK) {
109 FLUSH_VERTICES(ctx, 0);
110 _mesa_feedback_token( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN );
111 _mesa_feedback_token( ctx, token );
117 * Put a vertex into the feedback buffer.
120 _mesa_feedback_vertex(struct gl_context *ctx,
121 const GLfloat win[4],
122 const GLfloat color[4],
123 const GLfloat texcoord[4])
125 _mesa_feedback_token( ctx, win[0] );
126 _mesa_feedback_token( ctx, win[1] );
127 if (ctx->Feedback._Mask & FB_3D) {
128 _mesa_feedback_token( ctx, win[2] );
130 if (ctx->Feedback._Mask & FB_4D) {
131 _mesa_feedback_token( ctx, win[3] );
133 if (ctx->Feedback._Mask & FB_COLOR) {
134 _mesa_feedback_token( ctx, color[0] );
135 _mesa_feedback_token( ctx, color[1] );
136 _mesa_feedback_token( ctx, color[2] );
137 _mesa_feedback_token( ctx, color[3] );
139 if (ctx->Feedback._Mask & FB_TEXTURE) {
140 _mesa_feedback_token( ctx, texcoord[0] );
141 _mesa_feedback_token( ctx, texcoord[1] );
142 _mesa_feedback_token( ctx, texcoord[2] );
143 _mesa_feedback_token( ctx, texcoord[3] );
148 /**********************************************************************/
149 /** \name Selection */
153 * Establish a buffer for selection mode values.
155 * \param size buffer size.
156 * \param buffer buffer.
158 * \sa glSelectBuffer().
160 * \note this function can't be put in a display list.
162 * Verifies we're not in selection mode, flushes the vertices and initialize
163 * the fields in __struct gl_contextRec::Select with the given buffer.
165 static void GLAPIENTRY
166 _mesa_SelectBuffer( GLsizei size, GLuint *buffer )
168 GET_CURRENT_CONTEXT(ctx);
169 ASSERT_OUTSIDE_BEGIN_END(ctx);
172 _mesa_error(ctx, GL_INVALID_VALUE, "glSelectBuffer(size)");
176 if (ctx->RenderMode==GL_SELECT) {
177 _mesa_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
178 return; /* KW: added return */
181 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
182 ctx->Select.Buffer = buffer;
183 ctx->Select.BufferSize = size;
184 ctx->Select.BufferCount = 0;
185 ctx->Select.HitFlag = GL_FALSE;
186 ctx->Select.HitMinZ = 1.0;
187 ctx->Select.HitMaxZ = 0.0;
192 * Write a value of a record into the selection buffer.
194 * \param ctx GL context.
195 * \param value value.
197 * Verifies there is free space in the buffer to write the value and
198 * increments the pointer.
201 write_record(struct gl_context *ctx, GLuint value)
203 if (ctx->Select.BufferCount < ctx->Select.BufferSize) {
204 ctx->Select.Buffer[ctx->Select.BufferCount] = value;
206 ctx->Select.BufferCount++;
211 * Update the hit flag and the maximum and minimum depth values.
213 * \param ctx GL context.
216 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
217 * gl_selection::HitMaxZ.
220 _mesa_update_hitflag(struct gl_context *ctx, GLfloat z)
222 ctx->Select.HitFlag = GL_TRUE;
223 if (z < ctx->Select.HitMinZ) {
224 ctx->Select.HitMinZ = z;
226 if (z > ctx->Select.HitMaxZ) {
227 ctx->Select.HitMaxZ = z;
233 * Write the hit record.
235 * \param ctx GL context.
237 * Write the hit record, i.e., the number of names in the stack, the minimum and
238 * maximum depth values and the number of names in the name stack at the time
239 * of the event. Resets the hit flag.
244 write_hit_record(struct gl_context *ctx)
247 GLuint zmin, zmax, zscale = (~0u);
249 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
250 /* 2^32-1 and round to nearest unsigned integer. */
252 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
253 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
254 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
256 write_record( ctx, ctx->Select.NameStackDepth );
257 write_record( ctx, zmin );
258 write_record( ctx, zmax );
259 for (i = 0; i < ctx->Select.NameStackDepth; i++) {
260 write_record( ctx, ctx->Select.NameStack[i] );
264 ctx->Select.HitFlag = GL_FALSE;
265 ctx->Select.HitMinZ = 1.0;
266 ctx->Select.HitMaxZ = -1.0;
271 * Initialize the name stack.
273 * Verifies we are in select mode and resets the name stack depth and resets
274 * the hit record data in gl_selection. Marks new render mode in
275 * __struct gl_contextRec::NewState.
277 static void GLAPIENTRY
278 _mesa_InitNames( void )
280 GET_CURRENT_CONTEXT(ctx);
281 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
283 /* Record the hit before the HitFlag is wiped out again. */
284 if (ctx->RenderMode == GL_SELECT) {
285 if (ctx->Select.HitFlag) {
286 write_hit_record( ctx );
289 ctx->Select.NameStackDepth = 0;
290 ctx->Select.HitFlag = GL_FALSE;
291 ctx->Select.HitMinZ = 1.0;
292 ctx->Select.HitMaxZ = 0.0;
293 ctx->NewState |= _NEW_RENDERMODE;
298 * Load the top-most name of the name stack.
302 * Verifies we are in selection mode and that the name stack is not empty.
303 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
304 * and replace the top-most name in the stack.
306 * sa __struct gl_contextRec::Select.
308 static void GLAPIENTRY
309 _mesa_LoadName( GLuint name )
311 GET_CURRENT_CONTEXT(ctx);
312 ASSERT_OUTSIDE_BEGIN_END(ctx);
314 if (ctx->RenderMode != GL_SELECT) {
317 if (ctx->Select.NameStackDepth == 0) {
318 _mesa_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
322 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
324 if (ctx->Select.HitFlag) {
325 write_hit_record( ctx );
327 if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
328 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
331 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
337 * Push a name into the name stack.
341 * Verifies we are in selection mode and that the name stack is not full.
342 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
343 * and adds the name to the top of the name stack.
345 * sa __struct gl_contextRec::Select.
347 static void GLAPIENTRY
348 _mesa_PushName( GLuint name )
350 GET_CURRENT_CONTEXT(ctx);
351 ASSERT_OUTSIDE_BEGIN_END(ctx);
353 if (ctx->RenderMode != GL_SELECT) {
357 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
358 if (ctx->Select.HitFlag) {
359 write_hit_record( ctx );
361 if (ctx->Select.NameStackDepth >= MAX_NAME_STACK_DEPTH) {
362 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
365 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
370 * Pop a name into the name stack.
372 * Verifies we are in selection mode and that the name stack is not empty.
373 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
374 * and removes top-most name in the name stack.
376 * sa __struct gl_contextRec::Select.
378 static void GLAPIENTRY
379 _mesa_PopName( void )
381 GET_CURRENT_CONTEXT(ctx);
382 ASSERT_OUTSIDE_BEGIN_END(ctx);
384 if (ctx->RenderMode != GL_SELECT) {
388 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
389 if (ctx->Select.HitFlag) {
390 write_hit_record( ctx );
392 if (ctx->Select.NameStackDepth == 0) {
393 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
396 ctx->Select.NameStackDepth--;
402 /**********************************************************************/
403 /** \name Render Mode */
407 * Set rasterization mode.
409 * \param mode rasterization mode.
411 * \note this function can't be put in a display list.
413 * \sa glRenderMode().
415 * Flushes the vertices and do the necessary cleanup according to the previous
416 * rasterization mode, such as writing the hit record or resent the select
417 * buffer index when exiting the select mode. Updates
418 * __struct gl_contextRec::RenderMode and notifies the driver via the
419 * dd_function_table::RenderMode callback.
422 _mesa_RenderMode( GLenum mode )
424 GET_CURRENT_CONTEXT(ctx);
426 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
428 if (MESA_VERBOSE & VERBOSE_API)
429 _mesa_debug(ctx, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode));
431 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
433 switch (ctx->RenderMode) {
438 if (ctx->Select.HitFlag) {
439 write_hit_record( ctx );
441 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
444 _mesa_warning(ctx, "Feedback buffer overflow");
449 result = ctx->Select.Hits;
451 ctx->Select.BufferCount = 0;
452 ctx->Select.Hits = 0;
453 ctx->Select.NameStackDepth = 0;
457 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
462 result = ctx->Feedback.Count;
464 ctx->Feedback.Count = 0;
468 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
476 if (ctx->Select.BufferSize==0) {
477 /* haven't called glSelectBuffer yet */
478 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
483 if (ctx->Feedback.BufferSize==0) {
484 /* haven't called glFeedbackBuffer yet */
485 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
490 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
494 ctx->RenderMode = mode;
495 if (ctx->Driver.RenderMode)
496 ctx->Driver.RenderMode( ctx, mode );
505 _mesa_init_feedback_dispatch(struct _glapi_table *disp)
507 SET_InitNames(disp, _mesa_InitNames);
508 SET_FeedbackBuffer(disp, _mesa_FeedbackBuffer);
509 SET_LoadName(disp, _mesa_LoadName);
510 SET_PassThrough(disp, _mesa_PassThrough);
511 SET_PopName(disp, _mesa_PopName);
512 SET_PushName(disp, _mesa_PushName);
513 SET_SelectBuffer(disp, _mesa_SelectBuffer);
514 SET_RenderMode(disp, _mesa_RenderMode);
518 #endif /* FEATURE_feedback */
521 /**********************************************************************/
522 /** \name Initialization */
526 * Initialize context feedback data.
528 void _mesa_init_feedback( struct gl_context * ctx )
531 ctx->Feedback.Type = GL_2D; /* TODO: verify */
532 ctx->Feedback.Buffer = NULL;
533 ctx->Feedback.BufferSize = 0;
534 ctx->Feedback.Count = 0;
536 /* Selection/picking */
537 ctx->Select.Buffer = NULL;
538 ctx->Select.BufferSize = 0;
539 ctx->Select.BufferCount = 0;
540 ctx->Select.Hits = 0;
541 ctx->Select.NameStackDepth = 0;
544 ctx->RenderMode = GL_RENDER;