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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
29 * Selection and feedback modes functions.
40 #include "main/dispatch.h"
46 #define FB_TEXTURE 0X08
51 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
53 GET_CURRENT_CONTEXT(ctx
);
55 if (ctx
->RenderMode
==GL_FEEDBACK
) {
56 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
60 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
63 if (!buffer
&& size
> 0) {
64 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
65 ctx
->Feedback
.BufferSize
= 0;
71 ctx
->Feedback
._Mask
= 0;
74 ctx
->Feedback
._Mask
= FB_3D
;
77 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
);
79 case GL_3D_COLOR_TEXTURE
:
80 ctx
->Feedback
._Mask
= (FB_3D
| FB_COLOR
| FB_TEXTURE
);
82 case GL_4D_COLOR_TEXTURE
:
83 ctx
->Feedback
._Mask
= (FB_3D
| FB_4D
| FB_COLOR
| FB_TEXTURE
);
86 _mesa_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
90 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
); /* Always flush */
91 ctx
->Feedback
.Type
= type
;
92 ctx
->Feedback
.BufferSize
= size
;
93 ctx
->Feedback
.Buffer
= buffer
;
94 ctx
->Feedback
.Count
= 0; /* Becaues of this. */
99 _mesa_PassThrough( GLfloat token
)
101 GET_CURRENT_CONTEXT(ctx
);
103 if (ctx
->RenderMode
==GL_FEEDBACK
) {
104 FLUSH_VERTICES(ctx
, 0);
105 _mesa_feedback_token( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
106 _mesa_feedback_token( ctx
, token
);
112 * Put a vertex into the feedback buffer.
115 _mesa_feedback_vertex(struct gl_context
*ctx
,
116 const GLfloat win
[4],
117 const GLfloat color
[4],
118 const GLfloat texcoord
[4])
120 _mesa_feedback_token( ctx
, win
[0] );
121 _mesa_feedback_token( ctx
, win
[1] );
122 if (ctx
->Feedback
._Mask
& FB_3D
) {
123 _mesa_feedback_token( ctx
, win
[2] );
125 if (ctx
->Feedback
._Mask
& FB_4D
) {
126 _mesa_feedback_token( ctx
, win
[3] );
128 if (ctx
->Feedback
._Mask
& FB_COLOR
) {
129 _mesa_feedback_token( ctx
, color
[0] );
130 _mesa_feedback_token( ctx
, color
[1] );
131 _mesa_feedback_token( ctx
, color
[2] );
132 _mesa_feedback_token( ctx
, color
[3] );
134 if (ctx
->Feedback
._Mask
& FB_TEXTURE
) {
135 _mesa_feedback_token( ctx
, texcoord
[0] );
136 _mesa_feedback_token( ctx
, texcoord
[1] );
137 _mesa_feedback_token( ctx
, texcoord
[2] );
138 _mesa_feedback_token( ctx
, texcoord
[3] );
143 /**********************************************************************/
144 /** \name Selection */
148 * Establish a buffer for selection mode values.
150 * \param size buffer size.
151 * \param buffer buffer.
153 * \sa glSelectBuffer().
155 * \note this function can't be put in a display list.
157 * Verifies we're not in selection mode, flushes the vertices and initialize
158 * the fields in __struct gl_contextRec::Select with the given buffer.
161 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
163 GET_CURRENT_CONTEXT(ctx
);
166 _mesa_error(ctx
, GL_INVALID_VALUE
, "glSelectBuffer(size)");
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(struct gl_context
*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(struct gl_context
*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(struct gl_context
*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 * __struct gl_contextRec::NewState.
272 _mesa_InitNames( void )
274 GET_CURRENT_CONTEXT(ctx
);
275 FLUSH_VERTICES(ctx
, 0);
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 __struct gl_contextRec::Select.
303 _mesa_LoadName( GLuint name
)
305 GET_CURRENT_CONTEXT(ctx
);
307 if (ctx
->RenderMode
!= GL_SELECT
) {
310 if (ctx
->Select
.NameStackDepth
== 0) {
311 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
315 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
317 if (ctx
->Select
.HitFlag
) {
318 write_hit_record( ctx
);
320 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
321 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
324 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
330 * Push a name into the name stack.
334 * Verifies we are in selection mode and that the name stack is not full.
335 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
336 * and adds the name to the top of the name stack.
338 * sa __struct gl_contextRec::Select.
341 _mesa_PushName( GLuint name
)
343 GET_CURRENT_CONTEXT(ctx
);
345 if (ctx
->RenderMode
!= GL_SELECT
) {
349 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
350 if (ctx
->Select
.HitFlag
) {
351 write_hit_record( ctx
);
353 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
354 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
357 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
362 * Pop a name into the name stack.
364 * Verifies we are in selection mode and that the name stack is not empty.
365 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
366 * and removes top-most name in the name stack.
368 * sa __struct gl_contextRec::Select.
371 _mesa_PopName( void )
373 GET_CURRENT_CONTEXT(ctx
);
375 if (ctx
->RenderMode
!= GL_SELECT
) {
379 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
380 if (ctx
->Select
.HitFlag
) {
381 write_hit_record( ctx
);
383 if (ctx
->Select
.NameStackDepth
== 0) {
384 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
387 ctx
->Select
.NameStackDepth
--;
393 /**********************************************************************/
394 /** \name Render Mode */
398 * Set rasterization mode.
400 * \param mode rasterization mode.
402 * \note this function can't be put in a display list.
404 * \sa glRenderMode().
406 * Flushes the vertices and do the necessary cleanup according to the previous
407 * rasterization mode, such as writing the hit record or resent the select
408 * buffer index when exiting the select mode. Updates
409 * __struct gl_contextRec::RenderMode and notifies the driver via the
410 * dd_function_table::RenderMode callback.
413 _mesa_RenderMode( GLenum mode
)
415 GET_CURRENT_CONTEXT(ctx
);
417 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
419 if (MESA_VERBOSE
& VERBOSE_API
)
420 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode
));
422 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
424 switch (ctx
->RenderMode
) {
429 if (ctx
->Select
.HitFlag
) {
430 write_hit_record( ctx
);
432 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
435 _mesa_warning(ctx
, "Feedback buffer overflow");
440 result
= ctx
->Select
.Hits
;
442 ctx
->Select
.BufferCount
= 0;
443 ctx
->Select
.Hits
= 0;
444 ctx
->Select
.NameStackDepth
= 0;
447 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
452 result
= ctx
->Feedback
.Count
;
454 ctx
->Feedback
.Count
= 0;
457 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
465 if (ctx
->Select
.BufferSize
==0) {
466 /* haven't called glSelectBuffer yet */
467 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
471 if (ctx
->Feedback
.BufferSize
==0) {
472 /* haven't called glFeedbackBuffer yet */
473 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
477 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
481 ctx
->RenderMode
= mode
;
482 if (ctx
->Driver
.RenderMode
)
483 ctx
->Driver
.RenderMode( ctx
, mode
);
491 /**********************************************************************/
492 /** \name Initialization */
496 * Initialize context feedback data.
498 void _mesa_init_feedback( struct gl_context
* ctx
)
501 ctx
->Feedback
.Type
= GL_2D
; /* TODO: verify */
502 ctx
->Feedback
.Buffer
= NULL
;
503 ctx
->Feedback
.BufferSize
= 0;
504 ctx
->Feedback
.Count
= 0;
506 /* Selection/picking */
507 ctx
->Select
.Buffer
= NULL
;
508 ctx
->Select
.BufferSize
= 0;
509 ctx
->Select
.BufferCount
= 0;
510 ctx
->Select
.Hits
= 0;
511 ctx
->Select
.NameStackDepth
= 0;
514 ctx
->RenderMode
= GL_RENDER
;