1 /* $Id: feedback.c,v 1.9 2000/03/03 17:47:39 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 printf("FB %g\n", win
[2]);
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 UBYTE_RGBA_TO_FLOAT_RGBA( color
, VB
->ColorPtr
->data
[pv
] );
180 if (VB
->TexCoordPtr
[texUnit
]->size
== 4 &&
181 VB
->TexCoordPtr
[texUnit
]->data
[v
][3] != 0.0) {
182 GLfloat invq
= 1.0F
/ VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
183 tc
[0] = VB
->TexCoordPtr
[texUnit
]->data
[v
][0] * invq
;
184 tc
[1] = VB
->TexCoordPtr
[texUnit
]->data
[v
][1] * invq
;
185 tc
[2] = VB
->TexCoordPtr
[texUnit
]->data
[v
][2] * invq
;
186 tc
[3] = VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
189 ASSIGN_4V(tc
, 0,0,0,1);
191 VB
->TexCoordPtr
[texUnit
]->size
,
192 VB
->TexCoordPtr
[texUnit
]->data
[v
]);
196 index
= VB
->IndexPtr
->data
[v
];
200 gl_feedback_vertex( ctx
, win
, color
, index
, tc
);
206 * Put triangle in feedback buffer.
208 void gl_feedback_triangle( GLcontext
*ctx
,
209 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
211 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
212 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POLYGON_TOKEN
);
213 FEEDBACK_TOKEN( ctx
, (GLfloat
) 3 ); /* three vertices */
215 feedback_vertex( ctx
, v0
, pv
);
216 feedback_vertex( ctx
, v1
, pv
);
217 feedback_vertex( ctx
, v2
, pv
);
222 void gl_feedback_line( GLcontext
*ctx
, GLuint v1
, GLuint v2
, GLuint pv
)
224 GLenum token
= GL_LINE_TOKEN
;
226 if (ctx
->StippleCounter
==0)
227 token
= GL_LINE_RESET_TOKEN
;
229 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) token
);
231 feedback_vertex( ctx
, v1
, pv
);
232 feedback_vertex( ctx
, v2
, pv
);
234 ctx
->StippleCounter
++;
238 void gl_feedback_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
240 const struct vertex_buffer
*VB
= ctx
->VB
;
243 for (i
=first
;i
<=last
;i
++) {
244 if (VB
->ClipMask
[i
]==0) {
245 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POINT_TOKEN
);
246 feedback_vertex( ctx
, i
, i
);
255 /**********************************************************************/
257 /**********************************************************************/
261 * NOTE: this function can't be put in a display list.
264 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
266 GET_CURRENT_CONTEXT(ctx
);
267 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glSelectBuffer");
268 if (ctx
->RenderMode
==GL_SELECT
) {
269 gl_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
271 ctx
->Select
.Buffer
= buffer
;
272 ctx
->Select
.BufferSize
= size
;
273 ctx
->Select
.BufferCount
= 0;
275 ctx
->Select
.HitFlag
= GL_FALSE
;
276 ctx
->Select
.HitMinZ
= 1.0;
277 ctx
->Select
.HitMaxZ
= 0.0;
281 #define WRITE_RECORD( CTX, V ) \
282 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
283 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
285 CTX->Select.BufferCount++;
289 void gl_update_hitflag( GLcontext
*ctx
, GLfloat z
)
291 ctx
->Select
.HitFlag
= GL_TRUE
;
292 if (z
< ctx
->Select
.HitMinZ
) {
293 ctx
->Select
.HitMinZ
= z
;
295 if (z
> ctx
->Select
.HitMaxZ
) {
296 ctx
->Select
.HitMaxZ
= z
;
300 void gl_select_triangle( GLcontext
*ctx
,
301 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
303 const struct vertex_buffer
*VB
= ctx
->VB
;
305 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
306 const GLfloat zs
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
307 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][2] * zs
);
308 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][2] * zs
);
309 gl_update_hitflag( ctx
, VB
->Win
.data
[v2
][2] * zs
);
314 void gl_select_line( GLcontext
*ctx
,
315 GLuint v0
, GLuint v1
, GLuint pv
)
317 const struct vertex_buffer
*VB
= ctx
->VB
;
318 const GLfloat zs
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
319 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][2] * zs
);
320 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][2] * zs
);
324 void gl_select_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
326 struct vertex_buffer
*VB
= ctx
->VB
;
327 const GLfloat zs
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
330 for (i
=first
;i
<=last
;i
++) {
331 if (VB
->ClipMask
[i
]==0) {
332 gl_update_hitflag( ctx
, VB
->Win
.data
[i
][2] * zs
);
338 static void write_hit_record( GLcontext
*ctx
)
341 GLuint zmin
, zmax
, zscale
= (~0u);
343 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
344 /* 2^32-1 and round to nearest unsigned integer. */
346 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
347 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
348 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
350 WRITE_RECORD( ctx
, ctx
->Select
.NameStackDepth
);
351 WRITE_RECORD( ctx
, zmin
);
352 WRITE_RECORD( ctx
, zmax
);
353 for (i
= 0; i
< ctx
->Select
.NameStackDepth
; i
++) {
354 WRITE_RECORD( ctx
, ctx
->Select
.NameStack
[i
] );
358 ctx
->Select
.HitFlag
= GL_FALSE
;
359 ctx
->Select
.HitMinZ
= 1.0;
360 ctx
->Select
.HitMaxZ
= -1.0;
366 _mesa_InitNames( void )
368 GET_CURRENT_CONTEXT(ctx
);
369 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glInitNames");
370 /* Record the hit before the HitFlag is wiped out again. */
371 if (ctx
->RenderMode
== GL_SELECT
) {
372 if (ctx
->Select
.HitFlag
) {
373 write_hit_record( ctx
);
376 ctx
->Select
.NameStackDepth
= 0;
377 ctx
->Select
.HitFlag
= GL_FALSE
;
378 ctx
->Select
.HitMinZ
= 1.0;
379 ctx
->Select
.HitMaxZ
= 0.0;
385 _mesa_LoadName( GLuint name
)
387 GET_CURRENT_CONTEXT(ctx
);
388 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLoadName");
389 if (ctx
->RenderMode
!= GL_SELECT
) {
392 if (ctx
->Select
.NameStackDepth
== 0) {
393 gl_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
396 if (ctx
->Select
.HitFlag
) {
397 write_hit_record( ctx
);
399 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
400 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
403 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
409 _mesa_PushName( GLuint name
)
411 GET_CURRENT_CONTEXT(ctx
);
412 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushName");
413 if (ctx
->RenderMode
!= GL_SELECT
) {
416 if (ctx
->Select
.HitFlag
) {
417 write_hit_record( ctx
);
419 if (ctx
->Select
.NameStackDepth
< MAX_NAME_STACK_DEPTH
) {
420 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
423 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
430 _mesa_PopName( void )
432 GET_CURRENT_CONTEXT(ctx
);
433 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopName");
434 if (ctx
->RenderMode
!= GL_SELECT
) {
437 if (ctx
->Select
.HitFlag
) {
438 write_hit_record( ctx
);
440 if (ctx
->Select
.NameStackDepth
> 0) {
441 ctx
->Select
.NameStackDepth
--;
444 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
450 /**********************************************************************/
452 /**********************************************************************/
457 * NOTE: this function can't be put in a display list.
460 _mesa_RenderMode( GLenum mode
)
462 GET_CURRENT_CONTEXT(ctx
);
465 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glRenderMode", 0);
467 if (MESA_VERBOSE
& VERBOSE_API
)
468 fprintf(stderr
, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode
));
470 ctx
->TriangleCaps
&= ~(DD_FEEDBACK
|DD_SELECT
);
472 switch (ctx
->RenderMode
) {
477 if (ctx
->Select
.HitFlag
) {
478 write_hit_record( ctx
);
480 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
483 gl_warning(ctx
, "Feedback buffer overflow");
488 result
= ctx
->Select
.Hits
;
490 ctx
->Select
.BufferCount
= 0;
491 ctx
->Select
.Hits
= 0;
492 ctx
->Select
.NameStackDepth
= 0;
495 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
500 result
= ctx
->Feedback
.Count
;
502 ctx
->Feedback
.Count
= 0;
505 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
513 ctx
->TriangleCaps
|= DD_SELECT
;
514 if (ctx
->Select
.BufferSize
==0) {
515 /* haven't called glSelectBuffer yet */
516 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
520 ctx
->TriangleCaps
|= DD_FEEDBACK
;
521 if (ctx
->Feedback
.BufferSize
==0) {
522 /* haven't called glFeedbackBuffer yet */
523 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
527 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
531 ctx
->RenderMode
= mode
;
532 ctx
->NewState
|= NEW_ALL
;