2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * Selection and feedback modes functions.
43 #define FB_TEXTURE 0X08
48 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
50 GET_CURRENT_CONTEXT(ctx
);
52 if (ctx
->RenderMode
==GL_FEEDBACK
) {
53 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
57 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
60 if (!buffer
&& size
> 0) {
61 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
62 ctx
->Feedback
.BufferSize
= 0;
68 ctx
->Feedback
._Mask
= 0;
71 ctx
->Feedback
._Mask
= FB_3D
;
74 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
);
76 case GL_3D_COLOR_TEXTURE
:
77 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
| FB_TEXTURE
);
79 case GL_4D_COLOR_TEXTURE
:
80 ctx
->Feedback
._Mask
= (FB_3D
| FB_4D
| FB_COLOR
| FB_TEXTURE
);
83 _mesa_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
87 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
); /* Always flush */
88 ctx
->Feedback
.Type
= type
;
89 ctx
->Feedback
.BufferSize
= size
;
90 ctx
->Feedback
.Buffer
= buffer
;
91 ctx
->Feedback
.Count
= 0; /* Because of this. */
96 _mesa_PassThrough( GLfloat token
)
98 GET_CURRENT_CONTEXT(ctx
);
100 if (ctx
->RenderMode
==GL_FEEDBACK
) {
101 FLUSH_VERTICES(ctx
, 0);
102 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
103 _mesa_feedback_token( ctx
, token
);
109 * Put a vertex into the feedback buffer.
112 _mesa_feedback_vertex(struct gl_context
*ctx
,
113 const GLfloat win
[4],
114 const GLfloat color
[4],
115 const GLfloat texcoord
[4])
117 _mesa_feedback_token( ctx
, win
[0] );
118 _mesa_feedback_token( ctx
, win
[1] );
119 if (ctx
->Feedback
._Mask
& FB_3D
) {
120 _mesa_feedback_token( ctx
, win
[2] );
122 if (ctx
->Feedback
._Mask
& FB_4D
) {
123 _mesa_feedback_token( ctx
, win
[3] );
125 if (ctx
->Feedback
._Mask
& FB_COLOR
) {
126 _mesa_feedback_token( ctx
, color
[0] );
127 _mesa_feedback_token( ctx
, color
[1] );
128 _mesa_feedback_token( ctx
, color
[2] );
129 _mesa_feedback_token( ctx
, color
[3] );
131 if (ctx
->Feedback
._Mask
& FB_TEXTURE
) {
132 _mesa_feedback_token( ctx
, texcoord
[0] );
133 _mesa_feedback_token( ctx
, texcoord
[1] );
134 _mesa_feedback_token( ctx
, texcoord
[2] );
135 _mesa_feedback_token( ctx
, texcoord
[3] );
140 /**********************************************************************/
141 /** \name Selection */
145 * Establish a buffer for selection mode values.
147 * \param size buffer size.
148 * \param buffer buffer.
150 * \sa glSelectBuffer().
152 * \note this function can't be put in a display list.
154 * Verifies we're not in selection mode, flushes the vertices and initialize
155 * the fields in __struct gl_contextRec::Select with the given buffer.
158 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
160 GET_CURRENT_CONTEXT(ctx
);
163 _mesa_error(ctx
, GL_INVALID_VALUE
, "glSelectBuffer(size)");
167 if (ctx
->RenderMode
==GL_SELECT
) {
168 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
169 return; /* KW: added return */
172 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
173 ctx
->Select
.Buffer
= buffer
;
174 ctx
->Select
.BufferSize
= size
;
175 ctx
->Select
.BufferCount
= 0;
176 ctx
->Select
.HitFlag
= GL_FALSE
;
177 ctx
->Select
.HitMinZ
= 1.0;
178 ctx
->Select
.HitMaxZ
= 0.0;
183 * Write a value of a record into the selection buffer.
185 * \param ctx GL context.
186 * \param value value.
188 * Verifies there is free space in the buffer to write the value and
189 * increments the pointer.
192 write_record(struct gl_context
*ctx
, GLuint value
)
194 if (ctx
->Select
.BufferCount
< ctx
->Select
.BufferSize
) {
195 ctx
->Select
.Buffer
[ctx
->Select
.BufferCount
] = value
;
197 ctx
->Select
.BufferCount
++;
202 * Update the hit flag and the maximum and minimum depth values.
204 * \param ctx GL context.
207 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
208 * gl_selection::HitMaxZ.
211 _mesa_update_hitflag(struct gl_context
*ctx
, GLfloat z
)
213 ctx
->Select
.HitFlag
= GL_TRUE
;
214 if (z
< ctx
->Select
.HitMinZ
) {
215 ctx
->Select
.HitMinZ
= z
;
217 if (z
> ctx
->Select
.HitMaxZ
) {
218 ctx
->Select
.HitMaxZ
= z
;
224 * Write the hit record.
226 * \param ctx GL context.
228 * Write the hit record, i.e., the number of names in the stack, the minimum and
229 * maximum depth values and the number of names in the name stack at the time
230 * of the event. Resets the hit flag.
235 write_hit_record(struct gl_context
*ctx
)
238 GLuint zmin
, zmax
, zscale
= (~0u);
240 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
241 /* 2^32-1 and round to nearest unsigned integer. */
243 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
244 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
245 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
247 write_record( ctx
, ctx
->Select
.NameStackDepth
);
248 write_record( ctx
, zmin
);
249 write_record( ctx
, zmax
);
250 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
251 write_record( ctx
, ctx
->Select
.NameStack
[i
] );
255 ctx
->Select
.HitFlag
= GL_FALSE
;
256 ctx
->Select
.HitMinZ
= 1.0;
257 ctx
->Select
.HitMaxZ
= -1.0;
262 * Initialize the name stack.
264 * Verifies we are in select mode and resets the name stack depth and resets
265 * the hit record data in gl_selection. Marks new render mode in
266 * __struct gl_contextRec::NewState.
269 _mesa_InitNames( void )
271 GET_CURRENT_CONTEXT(ctx
);
272 FLUSH_VERTICES(ctx
, 0);
274 /* Record the hit before the HitFlag is wiped out again. */
275 if (ctx
->RenderMode
== GL_SELECT
) {
276 if (ctx
->Select
.HitFlag
) {
277 write_hit_record( ctx
);
280 ctx
->Select
.NameStackDepth
= 0;
281 ctx
->Select
.HitFlag
= GL_FALSE
;
282 ctx
->Select
.HitMinZ
= 1.0;
283 ctx
->Select
.HitMaxZ
= 0.0;
284 ctx
->NewState
|= _NEW_RENDERMODE
;
289 * Load the top-most name of the name stack.
293 * Verifies we are in selection mode and that the name stack is not empty.
294 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
295 * and replace the top-most name in the stack.
297 * sa __struct gl_contextRec::Select.
300 _mesa_LoadName( GLuint name
)
302 GET_CURRENT_CONTEXT(ctx
);
304 if (ctx
->RenderMode
!= GL_SELECT
) {
307 if (ctx
->Select
.NameStackDepth
== 0) {
308 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
312 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
314 if (ctx
->Select
.HitFlag
) {
315 write_hit_record( ctx
);
317 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
318 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
321 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
327 * Push a name into the name stack.
331 * Verifies we are in selection mode and that the name stack is not full.
332 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
333 * and adds the name to the top of the name stack.
335 * sa __struct gl_contextRec::Select.
338 _mesa_PushName( GLuint name
)
340 GET_CURRENT_CONTEXT(ctx
);
342 if (ctx
->RenderMode
!= GL_SELECT
) {
346 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
347 if (ctx
->Select
.HitFlag
) {
348 write_hit_record( ctx
);
350 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
351 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
354 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
359 * Pop a name into the name stack.
361 * Verifies we are in selection mode and that the name stack is not empty.
362 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
363 * and removes top-most name in the name stack.
365 * sa __struct gl_contextRec::Select.
368 _mesa_PopName( void )
370 GET_CURRENT_CONTEXT(ctx
);
372 if (ctx
->RenderMode
!= GL_SELECT
) {
376 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
377 if (ctx
->Select
.HitFlag
) {
378 write_hit_record( ctx
);
380 if (ctx
->Select
.NameStackDepth
== 0) {
381 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
384 ctx
->Select
.NameStackDepth
--;
390 /**********************************************************************/
391 /** \name Render Mode */
395 * Set rasterization mode.
397 * \param mode rasterization mode.
399 * \note this function can't be put in a display list.
401 * \sa glRenderMode().
403 * Flushes the vertices and do the necessary cleanup according to the previous
404 * rasterization mode, such as writing the hit record or resent the select
405 * buffer index when exiting the select mode. Updates
406 * __struct gl_contextRec::RenderMode and notifies the driver via the
407 * dd_function_table::RenderMode callback.
410 _mesa_RenderMode( GLenum mode
)
412 GET_CURRENT_CONTEXT(ctx
);
414 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
416 if (MESA_VERBOSE
& VERBOSE_API
)
417 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_enum_to_string(mode
));
419 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
421 switch (ctx
->RenderMode
) {
426 if (ctx
->Select
.HitFlag
) {
427 write_hit_record( ctx
);
429 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
432 _mesa_warning(ctx
, "Feedback buffer overflow");
437 result
= ctx
->Select
.Hits
;
439 ctx
->Select
.BufferCount
= 0;
440 ctx
->Select
.Hits
= 0;
441 ctx
->Select
.NameStackDepth
= 0;
444 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
449 result
= ctx
->Feedback
.Count
;
451 ctx
->Feedback
.Count
= 0;
454 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
462 if (ctx
->Select
.BufferSize
==0) {
463 /* haven't called glSelectBuffer yet */
464 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
468 if (ctx
->Feedback
.BufferSize
==0) {
469 /* haven't called glFeedbackBuffer yet */
470 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
474 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
478 ctx
->RenderMode
= mode
;
479 if (ctx
->Driver
.RenderMode
)
480 ctx
->Driver
.RenderMode( ctx
, mode
);
488 /**********************************************************************/
489 /** \name Initialization */
493 * Initialize context feedback data.
495 void _mesa_init_feedback( struct gl_context
* ctx
)
498 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
499 ctx
->Feedback
.Buffer
= NULL
;
500 ctx
->Feedback
.BufferSize
= 0;
501 ctx
->Feedback
.Count
= 0;
503 /* Selection/picking */
504 ctx
->Select
.Buffer
= NULL
;
505 ctx
->Select
.BufferSize
= 0;
506 ctx
->Select
.BufferCount
= 0;
507 ctx
->Select
.Hits
= 0;
508 ctx
->Select
.NameStackDepth
= 0;
511 ctx
->RenderMode
= GL_RENDER
;