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.
39 #include "main/dispatch.h"
45 #define FB_TEXTURE 0X08
50 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
52 GET_CURRENT_CONTEXT(ctx
);
54 if (ctx
->RenderMode
==GL_FEEDBACK
) {
55 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
59 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
62 if (!buffer
&& size
> 0) {
63 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
64 ctx
->Feedback
.BufferSize
= 0;
70 ctx
->Feedback
._Mask
= 0;
73 ctx
->Feedback
._Mask
= FB_3D
;
76 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
);
78 case GL_3D_COLOR_TEXTURE
:
79 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
| FB_TEXTURE
);
81 case GL_4D_COLOR_TEXTURE
:
82 ctx
->Feedback
._Mask
= (FB_3D
| FB_4D
| FB_COLOR
| FB_TEXTURE
);
85 _mesa_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
89 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
); /* Always flush */
90 ctx
->Feedback
.Type
= type
;
91 ctx
->Feedback
.BufferSize
= size
;
92 ctx
->Feedback
.Buffer
= buffer
;
93 ctx
->Feedback
.Count
= 0; /* Becaues of this. */
98 _mesa_PassThrough( GLfloat token
)
100 GET_CURRENT_CONTEXT(ctx
);
102 if (ctx
->RenderMode
==GL_FEEDBACK
) {
103 FLUSH_VERTICES(ctx
, 0);
104 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
105 _mesa_feedback_token( ctx
, token
);
111 * Put a vertex into the feedback buffer.
114 _mesa_feedback_vertex(struct gl_context
*ctx
,
115 const GLfloat win
[4],
116 const GLfloat color
[4],
117 const GLfloat texcoord
[4])
119 _mesa_feedback_token( ctx
, win
[0] );
120 _mesa_feedback_token( ctx
, win
[1] );
121 if (ctx
->Feedback
._Mask
& FB_3D
) {
122 _mesa_feedback_token( ctx
, win
[2] );
124 if (ctx
->Feedback
._Mask
& FB_4D
) {
125 _mesa_feedback_token( ctx
, win
[3] );
127 if (ctx
->Feedback
._Mask
& FB_COLOR
) {
128 _mesa_feedback_token( ctx
, color
[0] );
129 _mesa_feedback_token( ctx
, color
[1] );
130 _mesa_feedback_token( ctx
, color
[2] );
131 _mesa_feedback_token( ctx
, color
[3] );
133 if (ctx
->Feedback
._Mask
& FB_TEXTURE
) {
134 _mesa_feedback_token( ctx
, texcoord
[0] );
135 _mesa_feedback_token( ctx
, texcoord
[1] );
136 _mesa_feedback_token( ctx
, texcoord
[2] );
137 _mesa_feedback_token( ctx
, texcoord
[3] );
142 /**********************************************************************/
143 /** \name Selection */
147 * Establish a buffer for selection mode values.
149 * \param size buffer size.
150 * \param buffer buffer.
152 * \sa glSelectBuffer().
154 * \note this function can't be put in a display list.
156 * Verifies we're not in selection mode, flushes the vertices and initialize
157 * the fields in __struct gl_contextRec::Select with the given buffer.
160 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
162 GET_CURRENT_CONTEXT(ctx
);
165 _mesa_error(ctx
, GL_INVALID_VALUE
, "glSelectBuffer(size)");
169 if (ctx
->RenderMode
==GL_SELECT
) {
170 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
171 return; /* KW: added return */
174 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
175 ctx
->Select
.Buffer
= buffer
;
176 ctx
->Select
.BufferSize
= size
;
177 ctx
->Select
.BufferCount
= 0;
178 ctx
->Select
.HitFlag
= GL_FALSE
;
179 ctx
->Select
.HitMinZ
= 1.0;
180 ctx
->Select
.HitMaxZ
= 0.0;
185 * Write a value of a record into the selection buffer.
187 * \param ctx GL context.
188 * \param value value.
190 * Verifies there is free space in the buffer to write the value and
191 * increments the pointer.
194 write_record(struct gl_context
*ctx
, GLuint value
)
196 if (ctx
->Select
.BufferCount
< ctx
->Select
.BufferSize
) {
197 ctx
->Select
.Buffer
[ctx
->Select
.BufferCount
] = value
;
199 ctx
->Select
.BufferCount
++;
204 * Update the hit flag and the maximum and minimum depth values.
206 * \param ctx GL context.
209 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
210 * gl_selection::HitMaxZ.
213 _mesa_update_hitflag(struct gl_context
*ctx
, GLfloat z
)
215 ctx
->Select
.HitFlag
= GL_TRUE
;
216 if (z
< ctx
->Select
.HitMinZ
) {
217 ctx
->Select
.HitMinZ
= z
;
219 if (z
> ctx
->Select
.HitMaxZ
) {
220 ctx
->Select
.HitMaxZ
= z
;
226 * Write the hit record.
228 * \param ctx GL context.
230 * Write the hit record, i.e., the number of names in the stack, the minimum and
231 * maximum depth values and the number of names in the name stack at the time
232 * of the event. Resets the hit flag.
237 write_hit_record(struct gl_context
*ctx
)
240 GLuint zmin
, zmax
, zscale
= (~0u);
242 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
243 /* 2^32-1 and round to nearest unsigned integer. */
245 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
246 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
247 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
249 write_record( ctx
, ctx
->Select
.NameStackDepth
);
250 write_record( ctx
, zmin
);
251 write_record( ctx
, zmax
);
252 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
253 write_record( ctx
, ctx
->Select
.NameStack
[i
] );
257 ctx
->Select
.HitFlag
= GL_FALSE
;
258 ctx
->Select
.HitMinZ
= 1.0;
259 ctx
->Select
.HitMaxZ
= -1.0;
264 * Initialize the name stack.
266 * Verifies we are in select mode and resets the name stack depth and resets
267 * the hit record data in gl_selection. Marks new render mode in
268 * __struct gl_contextRec::NewState.
271 _mesa_InitNames( void )
273 GET_CURRENT_CONTEXT(ctx
);
274 FLUSH_VERTICES(ctx
, 0);
276 /* Record the hit before the HitFlag is wiped out again. */
277 if (ctx
->RenderMode
== GL_SELECT
) {
278 if (ctx
->Select
.HitFlag
) {
279 write_hit_record( ctx
);
282 ctx
->Select
.NameStackDepth
= 0;
283 ctx
->Select
.HitFlag
= GL_FALSE
;
284 ctx
->Select
.HitMinZ
= 1.0;
285 ctx
->Select
.HitMaxZ
= 0.0;
286 ctx
->NewState
|= _NEW_RENDERMODE
;
291 * Load the top-most name of the name stack.
295 * Verifies we are in selection mode and that the name stack is not empty.
296 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
297 * and replace the top-most name in the stack.
299 * sa __struct gl_contextRec::Select.
302 _mesa_LoadName( GLuint name
)
304 GET_CURRENT_CONTEXT(ctx
);
306 if (ctx
->RenderMode
!= GL_SELECT
) {
309 if (ctx
->Select
.NameStackDepth
== 0) {
310 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
314 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
316 if (ctx
->Select
.HitFlag
) {
317 write_hit_record( ctx
);
319 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
320 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
323 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
329 * Push a name into the name stack.
333 * Verifies we are in selection mode and that the name stack is not full.
334 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
335 * and adds the name to the top of the name stack.
337 * sa __struct gl_contextRec::Select.
340 _mesa_PushName( GLuint name
)
342 GET_CURRENT_CONTEXT(ctx
);
344 if (ctx
->RenderMode
!= GL_SELECT
) {
348 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
349 if (ctx
->Select
.HitFlag
) {
350 write_hit_record( ctx
);
352 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
353 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
356 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
361 * Pop a name into the name stack.
363 * Verifies we are in selection mode and that the name stack is not empty.
364 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
365 * and removes top-most name in the name stack.
367 * sa __struct gl_contextRec::Select.
370 _mesa_PopName( void )
372 GET_CURRENT_CONTEXT(ctx
);
374 if (ctx
->RenderMode
!= GL_SELECT
) {
378 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
379 if (ctx
->Select
.HitFlag
) {
380 write_hit_record( ctx
);
382 if (ctx
->Select
.NameStackDepth
== 0) {
383 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
386 ctx
->Select
.NameStackDepth
--;
392 /**********************************************************************/
393 /** \name Render Mode */
397 * Set rasterization mode.
399 * \param mode rasterization mode.
401 * \note this function can't be put in a display list.
403 * \sa glRenderMode().
405 * Flushes the vertices and do the necessary cleanup according to the previous
406 * rasterization mode, such as writing the hit record or resent the select
407 * buffer index when exiting the select mode. Updates
408 * __struct gl_contextRec::RenderMode and notifies the driver via the
409 * dd_function_table::RenderMode callback.
412 _mesa_RenderMode( GLenum mode
)
414 GET_CURRENT_CONTEXT(ctx
);
416 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
418 if (MESA_VERBOSE
& VERBOSE_API
)
419 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode
));
421 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
423 switch (ctx
->RenderMode
) {
428 if (ctx
->Select
.HitFlag
) {
429 write_hit_record( ctx
);
431 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
434 _mesa_warning(ctx
, "Feedback buffer overflow");
439 result
= ctx
->Select
.Hits
;
441 ctx
->Select
.BufferCount
= 0;
442 ctx
->Select
.Hits
= 0;
443 ctx
->Select
.NameStackDepth
= 0;
446 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
451 result
= ctx
->Feedback
.Count
;
453 ctx
->Feedback
.Count
= 0;
456 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
464 if (ctx
->Select
.BufferSize
==0) {
465 /* haven't called glSelectBuffer yet */
466 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
470 if (ctx
->Feedback
.BufferSize
==0) {
471 /* haven't called glFeedbackBuffer yet */
472 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
476 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
480 ctx
->RenderMode
= mode
;
481 if (ctx
->Driver
.RenderMode
)
482 ctx
->Driver
.RenderMode( ctx
, mode
);
490 /**********************************************************************/
491 /** \name Initialization */
495 * Initialize context feedback data.
497 void _mesa_init_feedback( struct gl_context
* ctx
)
500 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
501 ctx
->Feedback
.Buffer
= NULL
;
502 ctx
->Feedback
.BufferSize
= 0;
503 ctx
->Feedback
.Count
= 0;
505 /* Selection/picking */
506 ctx
->Select
.Buffer
= NULL
;
507 ctx
->Select
.BufferSize
= 0;
508 ctx
->Select
.BufferCount
= 0;
509 ctx
->Select
.Hits
= 0;
510 ctx
->Select
.NameStackDepth
= 0;
513 ctx
->RenderMode
= GL_RENDER
;