1 /* $Id: feedback.c,v 1.13 2000/10/28 20:41:14 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.
48 #define FB_TEXTURE 0X10
53 _mesa_FeedbackBuffer( GLsizei size
, GLenum type
, GLfloat
*buffer
)
55 GET_CURRENT_CONTEXT(ctx
);
56 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx
, "glFeedbackBuffer" );
58 if (ctx
->RenderMode
==GL_FEEDBACK
) {
59 gl_error( ctx
, GL_INVALID_OPERATION
, "glFeedbackBuffer" );
64 gl_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(size<0)" );
68 gl_error( ctx
, GL_INVALID_VALUE
, "glFeedbackBuffer(buffer==NULL)" );
69 ctx
->Feedback
.BufferSize
= 0;
75 ctx
->Feedback
.Mask
= 0;
76 ctx
->Feedback
.Type
= type
;
79 ctx
->Feedback
.Mask
= FB_3D
;
80 ctx
->Feedback
.Type
= type
;
83 ctx
->Feedback
.Mask
= FB_3D
84 | (ctx
->Visual
.RGBAflag
? FB_COLOR
: FB_INDEX
);
85 ctx
->Feedback
.Type
= type
;
87 case GL_3D_COLOR_TEXTURE
:
88 ctx
->Feedback
.Mask
= FB_3D
89 | (ctx
->Visual
.RGBAflag
? FB_COLOR
: FB_INDEX
)
91 ctx
->Feedback
.Type
= type
;
93 case GL_4D_COLOR_TEXTURE
:
94 ctx
->Feedback
.Mask
= FB_3D
| FB_4D
95 | (ctx
->Visual
.RGBAflag
? FB_COLOR
: FB_INDEX
)
97 ctx
->Feedback
.Type
= type
;
100 ctx
->Feedback
.Mask
= 0;
101 gl_error( ctx
, GL_INVALID_ENUM
, "glFeedbackBuffer" );
104 ctx
->Feedback
.BufferSize
= size
;
105 ctx
->Feedback
.Buffer
= buffer
;
106 ctx
->Feedback
.Count
= 0;
112 _mesa_PassThrough( GLfloat token
)
114 GET_CURRENT_CONTEXT(ctx
);
115 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPassThrough");
117 if (ctx
->RenderMode
==GL_FEEDBACK
) {
118 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_PASS_THROUGH_TOKEN
);
119 FEEDBACK_TOKEN( ctx
, token
);
126 * Put a vertex into the feedback buffer.
128 void gl_feedback_vertex( GLcontext
*ctx
,
129 const GLfloat win
[4],
130 const GLfloat color
[4],
132 const GLfloat texcoord
[4] )
134 FEEDBACK_TOKEN( ctx
, win
[0] );
135 FEEDBACK_TOKEN( ctx
, win
[1] );
136 if (ctx
->Feedback
.Mask
& FB_3D
) {
137 FEEDBACK_TOKEN( ctx
, win
[2] );
139 if (ctx
->Feedback
.Mask
& FB_4D
) {
140 FEEDBACK_TOKEN( ctx
, win
[3] );
142 if (ctx
->Feedback
.Mask
& FB_INDEX
) {
143 FEEDBACK_TOKEN( ctx
, (GLfloat
) index
);
145 if (ctx
->Feedback
.Mask
& FB_COLOR
) {
146 FEEDBACK_TOKEN( ctx
, color
[0] );
147 FEEDBACK_TOKEN( ctx
, color
[1] );
148 FEEDBACK_TOKEN( ctx
, color
[2] );
149 FEEDBACK_TOKEN( ctx
, color
[3] );
151 if (ctx
->Feedback
.Mask
& FB_TEXTURE
) {
152 FEEDBACK_TOKEN( ctx
, texcoord
[0] );
153 FEEDBACK_TOKEN( ctx
, texcoord
[1] );
154 FEEDBACK_TOKEN( ctx
, texcoord
[2] );
155 FEEDBACK_TOKEN( ctx
, texcoord
[3] );
161 static void feedback_vertex( GLcontext
*ctx
, GLuint v
, GLuint pv
)
166 GLuint texUnit
= ctx
->Texture
.CurrentTransformUnit
;
167 const struct vertex_buffer
*VB
= ctx
->VB
;
170 win
[0] = VB
->Win
.data
[v
][0];
171 win
[1] = VB
->Win
.data
[v
][1];
172 win
[2] = VB
->Win
.data
[v
][2] / ctx
->Visual
.DepthMaxF
;
173 win
[3] = 1.0 / VB
->Win
.data
[v
][3];
175 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
)
178 color
[0] = CHAN_TO_FLOAT(VB
->ColorPtr
->data
[pv
][0]);
179 color
[1] = CHAN_TO_FLOAT(VB
->ColorPtr
->data
[pv
][1]);
180 color
[2] = CHAN_TO_FLOAT(VB
->ColorPtr
->data
[pv
][2]);
181 color
[3] = CHAN_TO_FLOAT(VB
->ColorPtr
->data
[pv
][3]);
183 if (VB
->TexCoordPtr
[texUnit
]->size
== 4 &&
184 VB
->TexCoordPtr
[texUnit
]->data
[v
][3] != 0.0) {
185 GLfloat invq
= 1.0F
/ VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
186 tc
[0] = VB
->TexCoordPtr
[texUnit
]->data
[v
][0] * invq
;
187 tc
[1] = VB
->TexCoordPtr
[texUnit
]->data
[v
][1] * invq
;
188 tc
[2] = VB
->TexCoordPtr
[texUnit
]->data
[v
][2] * invq
;
189 tc
[3] = VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
192 ASSIGN_4V(tc
, 0,0,0,1);
194 VB
->TexCoordPtr
[texUnit
]->size
,
195 VB
->TexCoordPtr
[texUnit
]->data
[v
]);
199 index
= VB
->IndexPtr
->data
[v
];
203 gl_feedback_vertex( ctx
, win
, color
, index
, tc
);
209 * Put triangle in feedback buffer.
211 void gl_feedback_triangle( GLcontext
*ctx
,
212 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
214 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
215 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POLYGON_TOKEN
);
216 FEEDBACK_TOKEN( ctx
, (GLfloat
) 3 ); /* three vertices */
218 feedback_vertex( ctx
, v0
, pv
);
219 feedback_vertex( ctx
, v1
, pv
);
220 feedback_vertex( ctx
, v2
, pv
);
225 void gl_feedback_line( GLcontext
*ctx
, GLuint v1
, GLuint v2
, GLuint pv
)
227 GLenum token
= GL_LINE_TOKEN
;
229 if (ctx
->StippleCounter
==0)
230 token
= GL_LINE_RESET_TOKEN
;
232 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) token
);
234 feedback_vertex( ctx
, v1
, pv
);
235 feedback_vertex( ctx
, v2
, pv
);
237 ctx
->StippleCounter
++;
241 void gl_feedback_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
243 const struct vertex_buffer
*VB
= ctx
->VB
;
246 for (i
=first
;i
<=last
;i
++) {
247 if (VB
->ClipMask
[i
]==0) {
248 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POINT_TOKEN
);
249 feedback_vertex( ctx
, i
, i
);
258 /**********************************************************************/
260 /**********************************************************************/
264 * NOTE: this function can't be put in a display list.
267 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
269 GET_CURRENT_CONTEXT(ctx
);
270 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glSelectBuffer");
271 if (ctx
->RenderMode
==GL_SELECT
) {
272 gl_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
274 ctx
->Select
.Buffer
= buffer
;
275 ctx
->Select
.BufferSize
= size
;
276 ctx
->Select
.BufferCount
= 0;
278 ctx
->Select
.HitFlag
= GL_FALSE
;
279 ctx
->Select
.HitMinZ
= 1.0;
280 ctx
->Select
.HitMaxZ
= 0.0;
284 #define WRITE_RECORD( CTX, V ) \
285 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
286 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
288 CTX->Select.BufferCount++;
292 void gl_update_hitflag( GLcontext
*ctx
, GLfloat z
)
294 ctx
->Select
.HitFlag
= GL_TRUE
;
295 if (z
< ctx
->Select
.HitMinZ
) {
296 ctx
->Select
.HitMinZ
= z
;
298 if (z
> ctx
->Select
.HitMaxZ
) {
299 ctx
->Select
.HitMaxZ
= z
;
303 void gl_select_triangle( GLcontext
*ctx
,
304 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
306 const struct vertex_buffer
*VB
= ctx
->VB
;
308 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
309 const GLfloat zs
= 1.0F
/ ctx
->Visual
.DepthMaxF
;
310 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][2] * zs
);
311 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][2] * zs
);
312 gl_update_hitflag( ctx
, VB
->Win
.data
[v2
][2] * zs
);
317 void gl_select_line( GLcontext
*ctx
,
318 GLuint v0
, GLuint v1
, GLuint pv
)
320 const struct vertex_buffer
*VB
= ctx
->VB
;
321 const GLfloat zs
= 1.0F
/ ctx
->Visual
.DepthMaxF
;
322 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][2] * zs
);
323 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][2] * zs
);
327 void gl_select_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
329 struct vertex_buffer
*VB
= ctx
->VB
;
330 const GLfloat zs
= 1.0F
/ ctx
->Visual
.DepthMaxF
;
333 for (i
=first
;i
<=last
;i
++) {
334 if (VB
->ClipMask
[i
]==0) {
335 gl_update_hitflag( ctx
, VB
->Win
.data
[i
][2] * zs
);
341 static void write_hit_record( GLcontext
*ctx
)
344 GLuint zmin
, zmax
, zscale
= (~0u);
346 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
347 /* 2^32-1 and round to nearest unsigned integer. */
349 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
350 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
351 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
353 WRITE_RECORD( ctx
, ctx
->Select
.NameStackDepth
);
354 WRITE_RECORD( ctx
, zmin
);
355 WRITE_RECORD( ctx
, zmax
);
356 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
357 WRITE_RECORD( ctx
, ctx
->Select
.NameStack
[i
] );
361 ctx
->Select
.HitFlag
= GL_FALSE
;
362 ctx
->Select
.HitMinZ
= 1.0;
363 ctx
->Select
.HitMaxZ
= -1.0;
369 _mesa_InitNames( void )
371 GET_CURRENT_CONTEXT(ctx
);
372 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glInitNames");
373 /* Record the hit before the HitFlag is wiped out again. */
374 if (ctx
->RenderMode
== GL_SELECT
) {
375 if (ctx
->Select
.HitFlag
) {
376 write_hit_record( ctx
);
379 ctx
->Select
.NameStackDepth
= 0;
380 ctx
->Select
.HitFlag
= GL_FALSE
;
381 ctx
->Select
.HitMinZ
= 1.0;
382 ctx
->Select
.HitMaxZ
= 0.0;
388 _mesa_LoadName( GLuint name
)
390 GET_CURRENT_CONTEXT(ctx
);
391 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLoadName");
392 if (ctx
->RenderMode
!= GL_SELECT
) {
395 if (ctx
->Select
.NameStackDepth
== 0) {
396 gl_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
399 if (ctx
->Select
.HitFlag
) {
400 write_hit_record( ctx
);
402 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
403 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
406 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
412 _mesa_PushName( GLuint name
)
414 GET_CURRENT_CONTEXT(ctx
);
415 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushName");
416 if (ctx
->RenderMode
!= GL_SELECT
) {
419 if (ctx
->Select
.HitFlag
) {
420 write_hit_record( ctx
);
422 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
423 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
426 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
433 _mesa_PopName( void )
435 GET_CURRENT_CONTEXT(ctx
);
436 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopName");
437 if (ctx
->RenderMode
!= GL_SELECT
) {
440 if (ctx
->Select
.HitFlag
) {
441 write_hit_record( ctx
);
443 if (ctx
->Select
.NameStackDepth
> 0) {
444 ctx
->Select
.NameStackDepth
--;
447 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
453 /**********************************************************************/
455 /**********************************************************************/
460 * NOTE: this function can't be put in a display list.
463 _mesa_RenderMode( GLenum mode
)
465 GET_CURRENT_CONTEXT(ctx
);
468 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glRenderMode", 0);
470 if (MESA_VERBOSE
& VERBOSE_API
)
471 fprintf(stderr
, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode
));
473 ctx
->TriangleCaps
&= ~(DD_FEEDBACK
|DD_SELECT
);
475 switch (ctx
->RenderMode
) {
480 if (ctx
->Select
.HitFlag
) {
481 write_hit_record( ctx
);
483 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
486 _mesa_warning(ctx
, "Feedback buffer overflow");
491 result
= ctx
->Select
.Hits
;
493 ctx
->Select
.BufferCount
= 0;
494 ctx
->Select
.Hits
= 0;
495 ctx
->Select
.NameStackDepth
= 0;
498 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
503 result
= ctx
->Feedback
.Count
;
505 ctx
->Feedback
.Count
= 0;
508 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
516 ctx
->TriangleCaps
|= DD_SELECT
;
517 if (ctx
->Select
.BufferSize
==0) {
518 /* haven't called glSelectBuffer yet */
519 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
523 ctx
->TriangleCaps
|= DD_FEEDBACK
;
524 if (ctx
->Feedback
.BufferSize
==0) {
525 /* haven't called glFeedbackBuffer yet */
526 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
530 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
534 ctx
->RenderMode
= mode
;
535 ctx
->NewState
|= NEW_ALL
;