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 "main/dispatch.h"
48 #define FB_TEXTURE 0X08
52 static void GLAPIENTRY
53 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
55 GET_CURRENT_CONTEXT(ctx
);
56 ASSERT_OUTSIDE_BEGIN_END(ctx
);
58 if (ctx
->RenderMode
==GL_FEEDBACK
) {
59 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
63 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
67 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
68 ctx
->Feedback
.BufferSize
= 0;
74 ctx
->Feedback
._Mask
= 0;
77 ctx
->Feedback
._Mask
= FB_3D
;
80 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
);
82 case GL_3D_COLOR_TEXTURE
:
83 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
| FB_TEXTURE
);
85 case GL_4D_COLOR_TEXTURE
:
86 ctx
->Feedback
._Mask
= (FB_3D
| FB_4D
| FB_COLOR
| FB_TEXTURE
);
89 _mesa_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
93 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
); /* Always flush */
94 ctx
->Feedback
.Type
= type
;
95 ctx
->Feedback
.BufferSize
= size
;
96 ctx
->Feedback
.Buffer
= buffer
;
97 ctx
->Feedback
.Count
= 0; /* Becaues of this. */
101 static void GLAPIENTRY
102 _mesa_PassThrough( GLfloat token
)
104 GET_CURRENT_CONTEXT(ctx
);
105 ASSERT_OUTSIDE_BEGIN_END(ctx
);
107 if (ctx
->RenderMode
==GL_FEEDBACK
) {
108 FLUSH_VERTICES(ctx
, 0);
109 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
110 _mesa_feedback_token( ctx
, token
);
116 * Put a vertex into the feedback buffer.
119 _mesa_feedback_vertex(GLcontext
*ctx
,
120 const GLfloat win
[4],
121 const GLfloat color
[4],
122 const GLfloat texcoord
[4])
124 _mesa_feedback_token( ctx
, win
[0] );
125 _mesa_feedback_token( ctx
, win
[1] );
126 if (ctx
->Feedback
._Mask
& FB_3D
) {
127 _mesa_feedback_token( ctx
, win
[2] );
129 if (ctx
->Feedback
._Mask
& FB_4D
) {
130 _mesa_feedback_token( ctx
, win
[3] );
132 if (ctx
->Feedback
._Mask
& FB_COLOR
) {
133 _mesa_feedback_token( ctx
, color
[0] );
134 _mesa_feedback_token( ctx
, color
[1] );
135 _mesa_feedback_token( ctx
, color
[2] );
136 _mesa_feedback_token( ctx
, color
[3] );
138 if (ctx
->Feedback
._Mask
& FB_TEXTURE
) {
139 _mesa_feedback_token( ctx
, texcoord
[0] );
140 _mesa_feedback_token( ctx
, texcoord
[1] );
141 _mesa_feedback_token( ctx
, texcoord
[2] );
142 _mesa_feedback_token( ctx
, texcoord
[3] );
147 /**********************************************************************/
148 /** \name Selection */
152 * Establish a buffer for selection mode values.
154 * \param size buffer size.
155 * \param buffer buffer.
157 * \sa glSelectBuffer().
159 * \note this function can't be put in a display list.
161 * Verifies we're not in selection mode, flushes the vertices and initialize
162 * the fields in __GLcontextRec::Select with the given buffer.
164 static void GLAPIENTRY
165 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
167 GET_CURRENT_CONTEXT(ctx
);
168 ASSERT_OUTSIDE_BEGIN_END(ctx
);
170 if (ctx
->RenderMode
==GL_SELECT
) {
171 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
172 return; /* KW: added return */
175 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
176 ctx
->Select
.Buffer
= buffer
;
177 ctx
->Select
.BufferSize
= size
;
178 ctx
->Select
.BufferCount
= 0;
179 ctx
->Select
.HitFlag
= GL_FALSE
;
180 ctx
->Select
.HitMinZ
= 1.0;
181 ctx
->Select
.HitMaxZ
= 0.0;
186 * Write a value of a record into the selection buffer.
188 * \param ctx GL context.
189 * \param value value.
191 * Verifies there is free space in the buffer to write the value and
192 * increments the pointer.
195 write_record(GLcontext
*ctx
, GLuint value
)
197 if (ctx
->Select
.BufferCount
< ctx
->Select
.BufferSize
) {
198 ctx
->Select
.Buffer
[ctx
->Select
.BufferCount
] = value
;
200 ctx
->Select
.BufferCount
++;
205 * Update the hit flag and the maximum and minimum depth values.
207 * \param ctx GL context.
210 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
211 * gl_selection::HitMaxZ.
214 _mesa_update_hitflag(GLcontext
*ctx
, GLfloat z
)
216 ctx
->Select
.HitFlag
= GL_TRUE
;
217 if (z
< ctx
->Select
.HitMinZ
) {
218 ctx
->Select
.HitMinZ
= z
;
220 if (z
> ctx
->Select
.HitMaxZ
) {
221 ctx
->Select
.HitMaxZ
= z
;
227 * Write the hit record.
229 * \param ctx GL context.
231 * Write the hit record, i.e., the number of names in the stack, the minimum and
232 * maximum depth values and the number of names in the name stack at the time
233 * of the event. Resets the hit flag.
238 write_hit_record(GLcontext
*ctx
)
241 GLuint zmin
, zmax
, zscale
= (~0u);
243 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
244 /* 2^32-1 and round to nearest unsigned integer. */
246 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
247 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
248 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
250 write_record( ctx
, ctx
->Select
.NameStackDepth
);
251 write_record( ctx
, zmin
);
252 write_record( ctx
, zmax
);
253 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
254 write_record( ctx
, ctx
->Select
.NameStack
[i
] );
258 ctx
->Select
.HitFlag
= GL_FALSE
;
259 ctx
->Select
.HitMinZ
= 1.0;
260 ctx
->Select
.HitMaxZ
= -1.0;
265 * Initialize the name stack.
267 * Verifies we are in select mode and resets the name stack depth and resets
268 * the hit record data in gl_selection. Marks new render mode in
269 * __GLcontextRec::NewState.
271 static void GLAPIENTRY
272 _mesa_InitNames( void )
274 GET_CURRENT_CONTEXT(ctx
);
275 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
277 /* Record the hit before the HitFlag is wiped out again. */
278 if (ctx
->RenderMode
== GL_SELECT
) {
279 if (ctx
->Select
.HitFlag
) {
280 write_hit_record( ctx
);
283 ctx
->Select
.NameStackDepth
= 0;
284 ctx
->Select
.HitFlag
= GL_FALSE
;
285 ctx
->Select
.HitMinZ
= 1.0;
286 ctx
->Select
.HitMaxZ
= 0.0;
287 ctx
->NewState
|= _NEW_RENDERMODE
;
292 * Load the top-most name of the name stack.
296 * Verifies we are in selection mode and that the name stack is not empty.
297 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
298 * and replace the top-most name in the stack.
300 * sa __GLcontextRec::Select.
302 static void GLAPIENTRY
303 _mesa_LoadName( GLuint name
)
305 GET_CURRENT_CONTEXT(ctx
);
306 ASSERT_OUTSIDE_BEGIN_END(ctx
);
308 if (ctx
->RenderMode
!= GL_SELECT
) {
311 if (ctx
->Select
.NameStackDepth
== 0) {
312 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
316 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
318 if (ctx
->Select
.HitFlag
) {
319 write_hit_record( ctx
);
321 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
322 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
325 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
331 * Push a name into the name stack.
335 * Verifies we are in selection mode and that the name stack is not full.
336 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
337 * and adds the name to the top of the name stack.
339 * sa __GLcontextRec::Select.
341 static void GLAPIENTRY
342 _mesa_PushName( GLuint name
)
344 GET_CURRENT_CONTEXT(ctx
);
345 ASSERT_OUTSIDE_BEGIN_END(ctx
);
347 if (ctx
->RenderMode
!= GL_SELECT
) {
351 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
352 if (ctx
->Select
.HitFlag
) {
353 write_hit_record( ctx
);
355 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
356 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
359 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
364 * Pop a name into the name stack.
366 * Verifies we are in selection mode and that the name stack is not empty.
367 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
368 * and removes top-most name in the name stack.
370 * sa __GLcontextRec::Select.
372 static void GLAPIENTRY
373 _mesa_PopName( void )
375 GET_CURRENT_CONTEXT(ctx
);
376 ASSERT_OUTSIDE_BEGIN_END(ctx
);
378 if (ctx
->RenderMode
!= GL_SELECT
) {
382 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
383 if (ctx
->Select
.HitFlag
) {
384 write_hit_record( ctx
);
386 if (ctx
->Select
.NameStackDepth
== 0) {
387 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
390 ctx
->Select
.NameStackDepth
--;
396 /**********************************************************************/
397 /** \name Render Mode */
401 * Set rasterization mode.
403 * \param mode rasterization mode.
405 * \note this function can't be put in a display list.
407 * \sa glRenderMode().
409 * Flushes the vertices and do the necessary cleanup according to the previous
410 * rasterization mode, such as writing the hit record or resent the select
411 * buffer index when exiting the select mode. Updates
412 * __GLcontextRec::RenderMode and notifies the driver via the
413 * dd_function_table::RenderMode callback.
415 static GLint GLAPIENTRY
416 _mesa_RenderMode( GLenum mode
)
418 GET_CURRENT_CONTEXT(ctx
);
420 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
422 if (MESA_VERBOSE
& VERBOSE_API
)
423 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode
));
425 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
427 switch (ctx
->RenderMode
) {
432 if (ctx
->Select
.HitFlag
) {
433 write_hit_record( ctx
);
435 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
438 _mesa_warning(ctx
, "Feedback buffer overflow");
443 result
= ctx
->Select
.Hits
;
445 ctx
->Select
.BufferCount
= 0;
446 ctx
->Select
.Hits
= 0;
447 ctx
->Select
.NameStackDepth
= 0;
451 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
456 result
= ctx
->Feedback
.Count
;
458 ctx
->Feedback
.Count
= 0;
462 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
470 if (ctx
->Select
.BufferSize
==0) {
471 /* haven't called glSelectBuffer yet */
472 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
477 if (ctx
->Feedback
.BufferSize
==0) {
478 /* haven't called glFeedbackBuffer yet */
479 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
484 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
488 ctx
->RenderMode
= mode
;
489 if (ctx
->Driver
.RenderMode
)
490 ctx
->Driver
.RenderMode( ctx
, mode
);
499 _mesa_init_feedback_dispatch(struct _glapi_table
*disp
)
501 SET_InitNames(disp
, _mesa_InitNames
);
502 SET_FeedbackBuffer(disp
, _mesa_FeedbackBuffer
);
503 SET_LoadName(disp
, _mesa_LoadName
);
504 SET_PassThrough(disp
, _mesa_PassThrough
);
505 SET_PopName(disp
, _mesa_PopName
);
506 SET_PushName(disp
, _mesa_PushName
);
507 SET_SelectBuffer(disp
, _mesa_SelectBuffer
);
508 SET_RenderMode(disp
, _mesa_RenderMode
);
512 #endif /* FEATURE_feedback */
515 /**********************************************************************/
516 /** \name Initialization */
520 * Initialize context feedback data.
522 void _mesa_init_feedback( GLcontext
* ctx
)
525 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
526 ctx
->Feedback
.Buffer
= NULL
;
527 ctx
->Feedback
.BufferSize
= 0;
528 ctx
->Feedback
.Count
= 0;
530 /* Selection/picking */
531 ctx
->Select
.Buffer
= NULL
;
532 ctx
->Select
.BufferSize
= 0;
533 ctx
->Select
.BufferCount
= 0;
534 ctx
->Select
.Hits
= 0;
535 ctx
->Select
.NameStackDepth
= 0;
538 ctx
->RenderMode
= GL_RENDER
;