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.
39 #include "glapi/dispatch.h"
49 #define FB_TEXTURE 0X10
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)" );
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
|
82 (ctx
->Visual
.rgbMode
? FB_COLOR
: FB_INDEX
));
84 case GL_3D_COLOR_TEXTURE
:
85 ctx
->Feedback
._Mask
= (FB_3D
|
86 (ctx
->Visual
.rgbMode
? FB_COLOR
: FB_INDEX
) |
89 case GL_4D_COLOR_TEXTURE
:
90 ctx
->Feedback
._Mask
= (FB_3D
| FB_4D
|
91 (ctx
->Visual
.rgbMode
? FB_COLOR
: FB_INDEX
) |
95 _mesa_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
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. */
107 static void GLAPIENTRY
108 _mesa_PassThrough( GLfloat token
)
110 GET_CURRENT_CONTEXT(ctx
);
111 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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
);
122 * Put a vertex into the feedback buffer.
125 _mesa_feedback_vertex(GLcontext
*ctx
,
126 const GLfloat win
[4],
127 const GLfloat color
[4],
129 const GLfloat texcoord
[4])
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] );
136 if (ctx
->Feedback
._Mask
& FB_4D
) {
137 _mesa_feedback_token( ctx
, win
[3] );
139 if (ctx
->Feedback
._Mask
& FB_INDEX
) {
140 _mesa_feedback_token( ctx
, (GLfloat
) index
);
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] );
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] );
157 /**********************************************************************/
158 /** \name Selection */
162 * Establish a buffer for selection mode values.
164 * \param size buffer size.
165 * \param buffer buffer.
167 * \sa glSelectBuffer().
169 * \note this function can't be put in a display list.
171 * Verifies we're not in selection mode, flushes the vertices and initialize
172 * the fields in __GLcontextRec::Select with the given buffer.
174 static void GLAPIENTRY
175 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
177 GET_CURRENT_CONTEXT(ctx
);
178 ASSERT_OUTSIDE_BEGIN_END(ctx
);
180 if (ctx
->RenderMode
==GL_SELECT
) {
181 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
182 return; /* KW: added return */
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;
196 * Write a value of a record into the selection buffer.
198 * \param ctx GL context.
199 * \param value value.
201 * Verifies there is free space in the buffer to write the value and
202 * increments the pointer.
205 write_record(GLcontext
*ctx
, GLuint value
)
207 if (ctx
->Select
.BufferCount
< ctx
->Select
.BufferSize
) {
208 ctx
->Select
.Buffer
[ctx
->Select
.BufferCount
] = value
;
210 ctx
->Select
.BufferCount
++;
215 * Update the hit flag and the maximum and minimum depth values.
217 * \param ctx GL context.
220 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
221 * gl_selection::HitMaxZ.
224 _mesa_update_hitflag(GLcontext
*ctx
, GLfloat z
)
226 ctx
->Select
.HitFlag
= GL_TRUE
;
227 if (z
< ctx
->Select
.HitMinZ
) {
228 ctx
->Select
.HitMinZ
= z
;
230 if (z
> ctx
->Select
.HitMaxZ
) {
231 ctx
->Select
.HitMaxZ
= z
;
237 * Write the hit record.
239 * \param ctx GL context.
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.
248 write_hit_record(GLcontext
*ctx
)
251 GLuint zmin
, zmax
, zscale
= (~0u);
253 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
254 /* 2^32-1 and round to nearest unsigned integer. */
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
);
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
] );
268 ctx
->Select
.HitFlag
= GL_FALSE
;
269 ctx
->Select
.HitMinZ
= 1.0;
270 ctx
->Select
.HitMaxZ
= -1.0;
275 * Initialize the name stack.
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.
281 static void GLAPIENTRY
282 _mesa_InitNames( void )
284 GET_CURRENT_CONTEXT(ctx
);
285 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
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
);
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
;
302 * Load the top-most name of the name stack.
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.
310 * sa __GLcontextRec::Select.
312 static void GLAPIENTRY
313 _mesa_LoadName( GLuint name
)
315 GET_CURRENT_CONTEXT(ctx
);
316 ASSERT_OUTSIDE_BEGIN_END(ctx
);
318 if (ctx
->RenderMode
!= GL_SELECT
) {
321 if (ctx
->Select
.NameStackDepth
== 0) {
322 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
326 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
328 if (ctx
->Select
.HitFlag
) {
329 write_hit_record( ctx
);
331 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
332 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
335 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
341 * Push a name into the name stack.
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.
349 * sa __GLcontextRec::Select.
351 static void GLAPIENTRY
352 _mesa_PushName( GLuint name
)
354 GET_CURRENT_CONTEXT(ctx
);
355 ASSERT_OUTSIDE_BEGIN_END(ctx
);
357 if (ctx
->RenderMode
!= GL_SELECT
) {
361 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
362 if (ctx
->Select
.HitFlag
) {
363 write_hit_record( ctx
);
365 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
366 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
369 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
374 * Pop a name into the name stack.
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.
380 * sa __GLcontextRec::Select.
382 static void GLAPIENTRY
383 _mesa_PopName( void )
385 GET_CURRENT_CONTEXT(ctx
);
386 ASSERT_OUTSIDE_BEGIN_END(ctx
);
388 if (ctx
->RenderMode
!= GL_SELECT
) {
392 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
393 if (ctx
->Select
.HitFlag
) {
394 write_hit_record( ctx
);
396 if (ctx
->Select
.NameStackDepth
== 0) {
397 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
400 ctx
->Select
.NameStackDepth
--;
406 /**********************************************************************/
407 /** \name Render Mode */
411 * Set rasterization mode.
413 * \param mode rasterization mode.
415 * \note this function can't be put in a display list.
417 * \sa glRenderMode().
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.
425 static GLint GLAPIENTRY
426 _mesa_RenderMode( GLenum mode
)
428 GET_CURRENT_CONTEXT(ctx
);
430 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
432 if (MESA_VERBOSE
& VERBOSE_API
)
433 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode
));
435 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
437 switch (ctx
->RenderMode
) {
442 if (ctx
->Select
.HitFlag
) {
443 write_hit_record( ctx
);
445 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
448 _mesa_warning(ctx
, "Feedback buffer overflow");
453 result
= ctx
->Select
.Hits
;
455 ctx
->Select
.BufferCount
= 0;
456 ctx
->Select
.Hits
= 0;
457 ctx
->Select
.NameStackDepth
= 0;
461 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
466 result
= ctx
->Feedback
.Count
;
468 ctx
->Feedback
.Count
= 0;
472 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
480 if (ctx
->Select
.BufferSize
==0) {
481 /* haven't called glSelectBuffer yet */
482 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
487 if (ctx
->Feedback
.BufferSize
==0) {
488 /* haven't called glFeedbackBuffer yet */
489 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
494 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
498 ctx
->RenderMode
= mode
;
499 if (ctx
->Driver
.RenderMode
)
500 ctx
->Driver
.RenderMode( ctx
, mode
);
509 _mesa_init_feedback_dispatch(struct _glapi_table
*disp
)
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
);
522 #endif /* FEATURE_feedback */
525 /**********************************************************************/
526 /** \name Initialization */
530 * Initialize context feedback data.
532 void _mesa_init_feedback( GLcontext
* ctx
)
535 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
536 ctx
->Feedback
.Buffer
= NULL
;
537 ctx
->Feedback
.BufferSize
= 0;
538 ctx
->Feedback
.Count
= 0;
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;
548 ctx
->RenderMode
= GL_RENDER
;