1 /* $Id: feedback.c,v 1.10 2000/05/09 23:54:09 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
47 #define FB_TEXTURE 0X10
52 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
54 GET_CURRENT_CONTEXT(ctx
);
55 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
, "glFeedbackBuffer" );
57 if (ctx
->RenderMode
==GL_FEEDBACK
) {
58 gl_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
63 gl_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
67 gl_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
68 ctx
->Feedback
.BufferSize
= 0;
74 ctx
->Feedback
.Mask
= 0;
75 ctx
->Feedback
.Type
= type
;
78 ctx
->Feedback
.Mask
= FB_3D
;
79 ctx
->Feedback
.Type
= type
;
82 ctx
->Feedback
.Mask
= FB_3D
83 | (ctx
->Visual
->RGBAflag
? FB_COLOR
: FB_INDEX
);
84 ctx
->Feedback
.Type
= type
;
86 case GL_3D_COLOR_TEXTURE
:
87 ctx
->Feedback
.Mask
= FB_3D
88 | (ctx
->Visual
->RGBAflag
? FB_COLOR
: FB_INDEX
)
90 ctx
->Feedback
.Type
= type
;
92 case GL_4D_COLOR_TEXTURE
:
93 ctx
->Feedback
.Mask
= FB_3D
| FB_4D
94 | (ctx
->Visual
->RGBAflag
? FB_COLOR
: FB_INDEX
)
96 ctx
->Feedback
.Type
= type
;
99 ctx
->Feedback
.Mask
= 0;
100 gl_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
103 ctx
->Feedback
.BufferSize
= size
;
104 ctx
->Feedback
.Buffer
= buffer
;
105 ctx
->Feedback
.Count
= 0;
111 _mesa_PassThrough( GLfloat token
)
113 GET_CURRENT_CONTEXT(ctx
);
114 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPassThrough");
116 if (ctx
->RenderMode
==GL_FEEDBACK
) {
117 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
118 FEEDBACK_TOKEN( ctx
, token
);
125 * Put a vertex into the feedback buffer.
127 void gl_feedback_vertex( GLcontext
*ctx
,
128 const GLfloat win
[4],
129 const GLfloat color
[4],
131 const GLfloat texcoord
[4] )
133 FEEDBACK_TOKEN( ctx
, win
[0] );
134 FEEDBACK_TOKEN( ctx
, win
[1] );
135 if (ctx
->Feedback
.Mask
& FB_3D
) {
136 FEEDBACK_TOKEN( ctx
, win
[2] );
138 if (ctx
->Feedback
.Mask
& FB_4D
) {
139 FEEDBACK_TOKEN( ctx
, win
[3] );
141 if (ctx
->Feedback
.Mask
& FB_INDEX
) {
142 FEEDBACK_TOKEN( ctx
, (GLfloat
) index
);
144 if (ctx
->Feedback
.Mask
& FB_COLOR
) {
145 FEEDBACK_TOKEN( ctx
, color
[0] );
146 FEEDBACK_TOKEN( ctx
, color
[1] );
147 FEEDBACK_TOKEN( ctx
, color
[2] );
148 FEEDBACK_TOKEN( ctx
, color
[3] );
150 if (ctx
->Feedback
.Mask
& FB_TEXTURE
) {
151 FEEDBACK_TOKEN( ctx
, texcoord
[0] );
152 FEEDBACK_TOKEN( ctx
, texcoord
[1] );
153 FEEDBACK_TOKEN( ctx
, texcoord
[2] );
154 FEEDBACK_TOKEN( ctx
, texcoord
[3] );
160 static void feedback_vertex( GLcontext
*ctx
, GLuint v
, GLuint pv
)
165 GLuint texUnit
= ctx
->Texture
.CurrentTransformUnit
;
166 const struct vertex_buffer
*VB
= ctx
->VB
;
169 win
[0] = VB
->Win
.data
[v
][0];
170 win
[1] = VB
->Win
.data
[v
][1];
171 win
[2] = VB
->Win
.data
[v
][2] / ctx
->Visual
->DepthMaxF
;
172 win
[3] = 1.0 / VB
->Win
.data
[v
][3];
174 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
)
177 UBYTE_RGBA_TO_FLOAT_RGBA( color
, VB
->ColorPtr
->data
[pv
] );
179 if (VB
->TexCoordPtr
[texUnit
]->size
== 4 &&
180 VB
->TexCoordPtr
[texUnit
]->data
[v
][3] != 0.0) {
181 GLfloat invq
= 1.0F
/ VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
182 tc
[0] = VB
->TexCoordPtr
[texUnit
]->data
[v
][0] * invq
;
183 tc
[1] = VB
->TexCoordPtr
[texUnit
]->data
[v
][1] * invq
;
184 tc
[2] = VB
->TexCoordPtr
[texUnit
]->data
[v
][2] * invq
;
185 tc
[3] = VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
188 ASSIGN_4V(tc
, 0,0,0,1);
190 VB
->TexCoordPtr
[texUnit
]->size
,
191 VB
->TexCoordPtr
[texUnit
]->data
[v
]);
195 index
= VB
->IndexPtr
->data
[v
];
199 gl_feedback_vertex( ctx
, win
, color
, index
, tc
);
205 * Put triangle in feedback buffer.
207 void gl_feedback_triangle( GLcontext
*ctx
,
208 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
210 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
211 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POLYGON_TOKEN
);
212 FEEDBACK_TOKEN( ctx
, (GLfloat
) 3 ); /* three vertices */
214 feedback_vertex( ctx
, v0
, pv
);
215 feedback_vertex( ctx
, v1
, pv
);
216 feedback_vertex( ctx
, v2
, pv
);
221 void gl_feedback_line( GLcontext
*ctx
, GLuint v1
, GLuint v2
, GLuint pv
)
223 GLenum token
= GL_LINE_TOKEN
;
225 if (ctx
->StippleCounter
==0)
226 token
= GL_LINE_RESET_TOKEN
;
228 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) token
);
230 feedback_vertex( ctx
, v1
, pv
);
231 feedback_vertex( ctx
, v2
, pv
);
233 ctx
->StippleCounter
++;
237 void gl_feedback_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
239 const struct vertex_buffer
*VB
= ctx
->VB
;
242 for (i
=first
;i
<=last
;i
++) {
243 if (VB
->ClipMask
[i
]==0) {
244 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POINT_TOKEN
);
245 feedback_vertex( ctx
, i
, i
);
254 /**********************************************************************/
256 /**********************************************************************/
260 * NOTE: this function can't be put in a display list.
263 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
265 GET_CURRENT_CONTEXT(ctx
);
266 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glSelectBuffer");
267 if (ctx
->RenderMode
==GL_SELECT
) {
268 gl_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
270 ctx
->Select
.Buffer
= buffer
;
271 ctx
->Select
.BufferSize
= size
;
272 ctx
->Select
.BufferCount
= 0;
274 ctx
->Select
.HitFlag
= GL_FALSE
;
275 ctx
->Select
.HitMinZ
= 1.0;
276 ctx
->Select
.HitMaxZ
= 0.0;
280 #define WRITE_RECORD( CTX, V ) \
281 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
282 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
284 CTX->Select.BufferCount++;
288 void gl_update_hitflag( GLcontext
*ctx
, GLfloat z
)
290 ctx
->Select
.HitFlag
= GL_TRUE
;
291 if (z
< ctx
->Select
.HitMinZ
) {
292 ctx
->Select
.HitMinZ
= z
;
294 if (z
> ctx
->Select
.HitMaxZ
) {
295 ctx
->Select
.HitMaxZ
= z
;
299 void gl_select_triangle( GLcontext
*ctx
,
300 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
302 const struct vertex_buffer
*VB
= ctx
->VB
;
304 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
305 const GLfloat zs
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
306 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][2] * zs
);
307 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][2] * zs
);
308 gl_update_hitflag( ctx
, VB
->Win
.data
[v2
][2] * zs
);
313 void gl_select_line( GLcontext
*ctx
,
314 GLuint v0
, GLuint v1
, GLuint pv
)
316 const struct vertex_buffer
*VB
= ctx
->VB
;
317 const GLfloat zs
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
318 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][2] * zs
);
319 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][2] * zs
);
323 void gl_select_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
325 struct vertex_buffer
*VB
= ctx
->VB
;
326 const GLfloat zs
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
329 for (i
=first
;i
<=last
;i
++) {
330 if (VB
->ClipMask
[i
]==0) {
331 gl_update_hitflag( ctx
, VB
->Win
.data
[i
][2] * zs
);
337 static void write_hit_record( GLcontext
*ctx
)
340 GLuint zmin
, zmax
, zscale
= (~0u);
342 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
343 /* 2^32-1 and round to nearest unsigned integer. */
345 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
346 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
347 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
349 WRITE_RECORD( ctx
, ctx
->Select
.NameStackDepth
);
350 WRITE_RECORD( ctx
, zmin
);
351 WRITE_RECORD( ctx
, zmax
);
352 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
353 WRITE_RECORD( ctx
, ctx
->Select
.NameStack
[i
] );
357 ctx
->Select
.HitFlag
= GL_FALSE
;
358 ctx
->Select
.HitMinZ
= 1.0;
359 ctx
->Select
.HitMaxZ
= -1.0;
365 _mesa_InitNames( void )
367 GET_CURRENT_CONTEXT(ctx
);
368 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glInitNames");
369 /* Record the hit before the HitFlag is wiped out again. */
370 if (ctx
->RenderMode
== GL_SELECT
) {
371 if (ctx
->Select
.HitFlag
) {
372 write_hit_record( ctx
);
375 ctx
->Select
.NameStackDepth
= 0;
376 ctx
->Select
.HitFlag
= GL_FALSE
;
377 ctx
->Select
.HitMinZ
= 1.0;
378 ctx
->Select
.HitMaxZ
= 0.0;
384 _mesa_LoadName( GLuint name
)
386 GET_CURRENT_CONTEXT(ctx
);
387 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLoadName");
388 if (ctx
->RenderMode
!= GL_SELECT
) {
391 if (ctx
->Select
.NameStackDepth
== 0) {
392 gl_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
395 if (ctx
->Select
.HitFlag
) {
396 write_hit_record( ctx
);
398 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
399 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
402 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
408 _mesa_PushName( GLuint name
)
410 GET_CURRENT_CONTEXT(ctx
);
411 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushName");
412 if (ctx
->RenderMode
!= GL_SELECT
) {
415 if (ctx
->Select
.HitFlag
) {
416 write_hit_record( ctx
);
418 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
419 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
422 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
429 _mesa_PopName( void )
431 GET_CURRENT_CONTEXT(ctx
);
432 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopName");
433 if (ctx
->RenderMode
!= GL_SELECT
) {
436 if (ctx
->Select
.HitFlag
) {
437 write_hit_record( ctx
);
439 if (ctx
->Select
.NameStackDepth
> 0) {
440 ctx
->Select
.NameStackDepth
--;
443 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
449 /**********************************************************************/
451 /**********************************************************************/
456 * NOTE: this function can't be put in a display list.
459 _mesa_RenderMode( GLenum mode
)
461 GET_CURRENT_CONTEXT(ctx
);
464 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glRenderMode", 0);
466 if (MESA_VERBOSE
& VERBOSE_API
)
467 fprintf(stderr
, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode
));
469 ctx
->TriangleCaps
&= ~(DD_FEEDBACK
|DD_SELECT
);
471 switch (ctx
->RenderMode
) {
476 if (ctx
->Select
.HitFlag
) {
477 write_hit_record( ctx
);
479 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
482 gl_warning(ctx
, "Feedback buffer overflow");
487 result
= ctx
->Select
.Hits
;
489 ctx
->Select
.BufferCount
= 0;
490 ctx
->Select
.Hits
= 0;
491 ctx
->Select
.NameStackDepth
= 0;
494 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
499 result
= ctx
->Feedback
.Count
;
501 ctx
->Feedback
.Count
= 0;
504 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
512 ctx
->TriangleCaps
|= DD_SELECT
;
513 if (ctx
->Select
.BufferSize
==0) {
514 /* haven't called glSelectBuffer yet */
515 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
519 ctx
->TriangleCaps
|= DD_FEEDBACK
;
520 if (ctx
->Feedback
.BufferSize
==0) {
521 /* haven't called glFeedbackBuffer yet */
522 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
526 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
530 ctx
->RenderMode
= mode
;
531 ctx
->NewState
|= NEW_ALL
;