1 /* $Id: feedback.c,v 1.27 2002/10/24 23:57:20 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 #define FB_TEXTURE 0X10
48 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
50 GET_CURRENT_CONTEXT(ctx
);
51 ASSERT_OUTSIDE_BEGIN_END(ctx
);
53 if (ctx
->RenderMode
==GL_FEEDBACK
) {
54 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
58 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
62 _mesa_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
63 ctx
->Feedback
.BufferSize
= 0;
69 ctx
->Feedback
._Mask
= 0;
72 ctx
->Feedback
._Mask
= FB_3D
;
75 ctx
->Feedback
._Mask
= (FB_3D
|
76 (ctx
->Visual
.rgbMode
? FB_COLOR
: FB_INDEX
));
78 case GL_3D_COLOR_TEXTURE
:
79 ctx
->Feedback
._Mask
= (FB_3D
|
80 (ctx
->Visual
.rgbMode
? FB_COLOR
: FB_INDEX
) |
83 case GL_4D_COLOR_TEXTURE
:
84 ctx
->Feedback
._Mask
= (FB_3D
| FB_4D
|
85 (ctx
->Visual
.rgbMode
? FB_COLOR
: FB_INDEX
) |
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. */
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 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
110 FEEDBACK_TOKEN( ctx
, token
);
117 * Put a vertex into the feedback buffer.
119 void _mesa_feedback_vertex( GLcontext
*ctx
,
120 const GLfloat win
[4],
121 const GLfloat color
[4],
123 const GLfloat texcoord
[4] )
125 FEEDBACK_TOKEN( ctx
, win
[0] );
126 FEEDBACK_TOKEN( ctx
, win
[1] );
127 if (ctx
->Feedback
._Mask
& FB_3D
) {
128 FEEDBACK_TOKEN( ctx
, win
[2] );
130 if (ctx
->Feedback
._Mask
& FB_4D
) {
131 FEEDBACK_TOKEN( ctx
, win
[3] );
133 if (ctx
->Feedback
._Mask
& FB_INDEX
) {
134 FEEDBACK_TOKEN( ctx
, (GLfloat
) index
);
136 if (ctx
->Feedback
._Mask
& FB_COLOR
) {
137 FEEDBACK_TOKEN( ctx
, color
[0] );
138 FEEDBACK_TOKEN( ctx
, color
[1] );
139 FEEDBACK_TOKEN( ctx
, color
[2] );
140 FEEDBACK_TOKEN( ctx
, color
[3] );
142 if (ctx
->Feedback
._Mask
& FB_TEXTURE
) {
143 FEEDBACK_TOKEN( ctx
, texcoord
[0] );
144 FEEDBACK_TOKEN( ctx
, texcoord
[1] );
145 FEEDBACK_TOKEN( ctx
, texcoord
[2] );
146 FEEDBACK_TOKEN( ctx
, texcoord
[3] );
151 /**********************************************************************/
153 /**********************************************************************/
157 * NOTE: this function can't be put in a display list.
160 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
162 GET_CURRENT_CONTEXT(ctx
);
163 ASSERT_OUTSIDE_BEGIN_END(ctx
);
165 if (ctx
->RenderMode
==GL_SELECT
) {
166 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
167 return; /* KW: added return */
170 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
); /* why bother? */
171 ctx
->Select
.Buffer
= buffer
;
172 ctx
->Select
.BufferSize
= size
;
173 ctx
->Select
.BufferCount
= 0;
174 ctx
->Select
.HitFlag
= GL_FALSE
;
175 ctx
->Select
.HitMinZ
= 1.0;
176 ctx
->Select
.HitMaxZ
= 0.0;
180 #define WRITE_RECORD( CTX, V ) \
181 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
182 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
184 CTX->Select.BufferCount++;
188 void _mesa_update_hitflag( GLcontext
*ctx
, GLfloat z
)
190 ctx
->Select
.HitFlag
= GL_TRUE
;
191 if (z
< ctx
->Select
.HitMinZ
) {
192 ctx
->Select
.HitMinZ
= z
;
194 if (z
> ctx
->Select
.HitMaxZ
) {
195 ctx
->Select
.HitMaxZ
= z
;
200 static void write_hit_record( GLcontext
*ctx
)
203 GLuint zmin
, zmax
, zscale
= (~0u);
205 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
206 /* 2^32-1 and round to nearest unsigned integer. */
208 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
209 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
210 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
212 WRITE_RECORD( ctx
, ctx
->Select
.NameStackDepth
);
213 WRITE_RECORD( ctx
, zmin
);
214 WRITE_RECORD( ctx
, zmax
);
215 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
216 WRITE_RECORD( ctx
, ctx
->Select
.NameStack
[i
] );
220 ctx
->Select
.HitFlag
= GL_FALSE
;
221 ctx
->Select
.HitMinZ
= 1.0;
222 ctx
->Select
.HitMaxZ
= -1.0;
228 _mesa_InitNames( void )
230 GET_CURRENT_CONTEXT(ctx
);
231 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
);
233 /* Record the hit before the HitFlag is wiped out again. */
234 if (ctx
->RenderMode
== GL_SELECT
) {
235 if (ctx
->Select
.HitFlag
) {
236 write_hit_record( ctx
);
239 ctx
->Select
.NameStackDepth
= 0;
240 ctx
->Select
.HitFlag
= GL_FALSE
;
241 ctx
->Select
.HitMinZ
= 1.0;
242 ctx
->Select
.HitMaxZ
= 0.0;
243 ctx
->NewState
|= _NEW_RENDERMODE
;
249 _mesa_LoadName( GLuint name
)
251 GET_CURRENT_CONTEXT(ctx
);
252 ASSERT_OUTSIDE_BEGIN_END(ctx
);
254 if (ctx
->RenderMode
!= GL_SELECT
) {
257 if (ctx
->Select
.NameStackDepth
== 0) {
258 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
262 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
264 if (ctx
->Select
.HitFlag
) {
265 write_hit_record( ctx
);
267 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
268 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
271 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
277 _mesa_PushName( GLuint name
)
279 GET_CURRENT_CONTEXT(ctx
);
280 ASSERT_OUTSIDE_BEGIN_END(ctx
);
282 if (ctx
->RenderMode
!= GL_SELECT
) {
286 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
287 if (ctx
->Select
.HitFlag
) {
288 write_hit_record( ctx
);
290 if (ctx
->Select
.NameStackDepth
>= MAX_NAME_STACK_DEPTH
) {
291 _mesa_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
294 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
300 _mesa_PopName( void )
302 GET_CURRENT_CONTEXT(ctx
);
303 ASSERT_OUTSIDE_BEGIN_END(ctx
);
305 if (ctx
->RenderMode
!= GL_SELECT
) {
309 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
310 if (ctx
->Select
.HitFlag
) {
311 write_hit_record( ctx
);
313 if (ctx
->Select
.NameStackDepth
== 0) {
314 _mesa_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
317 ctx
->Select
.NameStackDepth
--;
322 /**********************************************************************/
324 /**********************************************************************/
329 * NOTE: this function can't be put in a display list.
332 _mesa_RenderMode( GLenum mode
)
334 GET_CURRENT_CONTEXT(ctx
);
336 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
338 if (MESA_VERBOSE
& VERBOSE_API
)
339 _mesa_debug(ctx
, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode
));
341 FLUSH_VERTICES(ctx
, _NEW_RENDERMODE
);
343 switch (ctx
->RenderMode
) {
348 if (ctx
->Select
.HitFlag
) {
349 write_hit_record( ctx
);
351 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
354 _mesa_warning(ctx
, "Feedback buffer overflow");
359 result
= ctx
->Select
.Hits
;
361 ctx
->Select
.BufferCount
= 0;
362 ctx
->Select
.Hits
= 0;
363 ctx
->Select
.NameStackDepth
= 0;
366 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
371 result
= ctx
->Feedback
.Count
;
373 ctx
->Feedback
.Count
= 0;
376 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
384 if (ctx
->Select
.BufferSize
==0) {
385 /* haven't called glSelectBuffer yet */
386 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
390 if (ctx
->Feedback
.BufferSize
==0) {
391 /* haven't called glFeedbackBuffer yet */
392 _mesa_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
396 _mesa_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
400 ctx
->RenderMode
= mode
;
401 if (ctx
->Driver
.RenderMode
)
402 ctx
->Driver
.RenderMode( ctx
, mode
);