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
);
171 if (ctx
->RenderMode
==GL_SELECT
) {
172 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
173 return; /* KW: added return */
176 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
177 ctx
->Select
.Buffer
= buffer
;
178 ctx
->Select
.BufferSize
= size
;
179 ctx
->Select
.BufferCount
= 0;
180 ctx
->Select
.HitFlag
= GL_FALSE
;
181 ctx
->Select
.HitMinZ
= 1.0;
182 ctx
->Select
.HitMaxZ
= 0.0;
187 * Write a value of a record into the selection buffer.
189 * \param ctx GL context.
190 * \param value value.
192 * Verifies there is free space in the buffer to write the value and
193 * increments the pointer.
196 write_record(struct gl_context
*ctx
, GLuint value
)
198 if (ctx
->Select
.BufferCount
< ctx
->Select
.BufferSize
) {
199 ctx
->Select
.Buffer
[ctx
->Select
.BufferCount
] = value
;
201 ctx
->Select
.BufferCount
++;
206 * Update the hit flag and the maximum and minimum depth values.
208 * \param ctx GL context.
211 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
212 * gl_selection::HitMaxZ.
215 _mesa_update_hitflag(struct gl_context
*ctx
, GLfloat z
)
217 ctx
->Select
.HitFlag
= GL_TRUE
;
218 if (z
< ctx
->Select
.HitMinZ
) {
219 ctx
->Select
.HitMinZ
= z
;
221 if (z
> ctx
->Select
.HitMaxZ
) {
222 ctx
->Select
.HitMaxZ
= z
;
228 * Write the hit record.
230 * \param ctx GL context.
232 * Write the hit record, i.e., the number of names in the stack, the minimum and
233 * maximum depth values and the number of names in the name stack at the time
234 * of the event. Resets the hit flag.
239 write_hit_record(struct gl_context
*ctx
)
242 GLuint zmin
, zmax
, zscale
= (~0u);
244 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
245 /* 2^32-1 and round to nearest unsigned integer. */
247 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
248 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
249 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
251 write_record( ctx
, ctx
->Select
.NameStackDepth
);
252 write_record( ctx
, zmin
);
253 write_record( ctx
, zmax
);
254 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
255 write_record( ctx
, ctx
->Select
.NameStack
[i
] );
259 ctx
->Select
.HitFlag
= GL_FALSE
;
260 ctx
->Select
.HitMinZ
= 1.0;
261 ctx
->Select
.HitMaxZ
= -1.0;
266 * Initialize the name stack.
268 * Verifies we are in select mode and resets the name stack depth and resets
269 * the hit record data in gl_selection. Marks new render mode in
270 * __struct gl_contextRec::NewState.
272 static void GLAPIENTRY
273 _mesa_InitNames( void )
275 GET_CURRENT_CONTEXT(ctx
);
276 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
278 /* Record the hit before the HitFlag is wiped out again. */
279 if (ctx
->RenderMode
== GL_SELECT
) {
280 if (ctx
->Select
.HitFlag
) {
281 write_hit_record( ctx
);
284 ctx
->Select
.NameStackDepth
= 0;
285 ctx
->Select
.HitFlag
= GL_FALSE
;
286 ctx
->Select
.HitMinZ
= 1.0;
287 ctx
->Select
.HitMaxZ
= 0.0;
288 ctx
->NewState
|= _NEW_RENDERMODE
;
293 * Load the top-most name of the name stack.
297 * Verifies we are in selection mode and that the name stack is not empty.
298 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
299 * and replace the top-most name in the stack.
301 * sa __struct gl_contextRec::Select.
303 static void GLAPIENTRY
304 _mesa_LoadName( GLuint name
)
306 GET_CURRENT_CONTEXT(ctx
);
307 ASSERT_OUTSIDE_BEGIN_END(ctx
);
309 if (ctx
->RenderMode
!= GL_SELECT
) {
312 if (ctx
->Select
.NameStackDepth
== 0) {
313 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
317 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
319 if (ctx
->Select
.HitFlag
) {
320 write_hit_record( ctx
);
322 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
323 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
326 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
332 * Push a name into the name stack.
336 * Verifies we are in selection mode and that the name stack is not full.
337 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
338 * and adds the name to the top of the name stack.
340 * sa __struct gl_contextRec::Select.
342 static void GLAPIENTRY
343 _mesa_PushName( GLuint name
)
345 GET_CURRENT_CONTEXT(ctx
);
346 ASSERT_OUTSIDE_BEGIN_END(ctx
);
348 if (ctx
->RenderMode
!= GL_SELECT
) {
352 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
353 if (ctx
->Select
.HitFlag
) {
354 write_hit_record( ctx
);
356 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
357 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
360 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
365 * Pop a name into the name stack.
367 * Verifies we are in selection mode and that the name stack is not empty.
368 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
369 * and removes top-most name in the name stack.
371 * sa __struct gl_contextRec::Select.
373 static void GLAPIENTRY
374 _mesa_PopName( void )
376 GET_CURRENT_CONTEXT(ctx
);
377 ASSERT_OUTSIDE_BEGIN_END(ctx
);
379 if (ctx
->RenderMode
!= GL_SELECT
) {
383 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
384 if (ctx
->Select
.HitFlag
) {
385 write_hit_record( ctx
);
387 if (ctx
->Select
.NameStackDepth
== 0) {
388 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
391 ctx
->Select
.NameStackDepth
--;
397 /**********************************************************************/
398 /** \name Render Mode */
402 * Set rasterization mode.
404 * \param mode rasterization mode.
406 * \note this function can't be put in a display list.
408 * \sa glRenderMode().
410 * Flushes the vertices and do the necessary cleanup according to the previous
411 * rasterization mode, such as writing the hit record or resent the select
412 * buffer index when exiting the select mode. Updates
413 * __struct gl_contextRec::RenderMode and notifies the driver via the
414 * dd_function_table::RenderMode callback.
416 static GLint GLAPIENTRY
417 _mesa_RenderMode( GLenum mode
)
419 GET_CURRENT_CONTEXT(ctx
);
421 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
423 if (MESA_VERBOSE
& VERBOSE_API
)
424 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode
));
426 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
428 switch (ctx
->RenderMode
) {
433 if (ctx
->Select
.HitFlag
) {
434 write_hit_record( ctx
);
436 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
439 _mesa_warning(ctx
, "Feedback buffer overflow");
444 result
= ctx
->Select
.Hits
;
446 ctx
->Select
.BufferCount
= 0;
447 ctx
->Select
.Hits
= 0;
448 ctx
->Select
.NameStackDepth
= 0;
452 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
457 result
= ctx
->Feedback
.Count
;
459 ctx
->Feedback
.Count
= 0;
463 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
471 if (ctx
->Select
.BufferSize
==0) {
472 /* haven't called glSelectBuffer yet */
473 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
478 if (ctx
->Feedback
.BufferSize
==0) {
479 /* haven't called glFeedbackBuffer yet */
480 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
485 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
489 ctx
->RenderMode
= mode
;
490 if (ctx
->Driver
.RenderMode
)
491 ctx
->Driver
.RenderMode( ctx
, mode
);
500 _mesa_init_feedback_dispatch(struct _glapi_table
*disp
)
502 SET_InitNames(disp
, _mesa_InitNames
);
503 SET_FeedbackBuffer(disp
, _mesa_FeedbackBuffer
);
504 SET_LoadName(disp
, _mesa_LoadName
);
505 SET_PassThrough(disp
, _mesa_PassThrough
);
506 SET_PopName(disp
, _mesa_PopName
);
507 SET_PushName(disp
, _mesa_PushName
);
508 SET_SelectBuffer(disp
, _mesa_SelectBuffer
);
509 SET_RenderMode(disp
, _mesa_RenderMode
);
513 #endif /* FEATURE_feedback */
516 /**********************************************************************/
517 /** \name Initialization */
521 * Initialize context feedback data.
523 void _mesa_init_feedback( struct gl_context
* ctx
)
526 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
527 ctx
->Feedback
.Buffer
= NULL
;
528 ctx
->Feedback
.BufferSize
= 0;
529 ctx
->Feedback
.Count
= 0;
531 /* Selection/picking */
532 ctx
->Select
.Buffer
= NULL
;
533 ctx
->Select
.BufferSize
= 0;
534 ctx
->Select
.BufferCount
= 0;
535 ctx
->Select
.Hits
= 0;
536 ctx
->Select
.NameStackDepth
= 0;
539 ctx
->RenderMode
= GL_RENDER
;