c2a52aaa538d20b8ad48ce7b4f7858414aa4c706
1 /* $Id: feedback.c,v 1.6 1999/11/11 01:22:26 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 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 gl_do_feedback_vertex( GLcontext
*ctx
, GLuint v
, GLuint pv
)
165 GLuint texUnit
= ctx
->Texture
.CurrentTransformUnit
;
166 struct vertex_buffer
*VB
= ctx
->VB
;
168 win
[0] = VB
->Win
.data
[v
][0];
169 win
[1] = VB
->Win
.data
[v
][1];
170 win
[2] = VB
->Win
.data
[v
][2] / DEPTH_SCALE
;
171 win
[3] = 1.0 / VB
->Win
.data
[v
][3];
173 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) pv
= v
;
175 UBYTE_RGBA_TO_FLOAT_RGBA( color
, VB
->ColorPtr
->data
[pv
] );
177 if (VB
->TexCoordPtr
[texUnit
]->size
== 4 &&
178 VB
->TexCoordPtr
[texUnit
]->data
[v
][3]!=0.0)
180 GLfloat invq
= 1.0F
/ VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
181 tc
[0] = VB
->TexCoordPtr
[texUnit
]->data
[v
][0] * invq
;
182 tc
[1] = VB
->TexCoordPtr
[texUnit
]->data
[v
][1] * invq
;
183 tc
[2] = VB
->TexCoordPtr
[texUnit
]->data
[v
][2] * invq
;
184 tc
[3] = VB
->TexCoordPtr
[texUnit
]->data
[v
][3];
186 ASSIGN_4V(tc
, 0,0,0,1);
188 VB
->TexCoordPtr
[texUnit
]->size
,
189 VB
->TexCoordPtr
[texUnit
]->data
[v
]);
192 gl_feedback_vertex( ctx
, win
, color
, VB
->IndexPtr
->data
[v
], tc
);
198 * Put triangle in feedback buffer.
200 void gl_feedback_triangle( GLcontext
*ctx
,
201 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
203 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
204 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POLYGON_TOKEN
);
205 FEEDBACK_TOKEN( ctx
, (GLfloat
) 3 ); /* three vertices */
207 gl_do_feedback_vertex( ctx
, v0
, pv
);
208 gl_do_feedback_vertex( ctx
, v1
, pv
);
209 gl_do_feedback_vertex( ctx
, v2
, pv
);
214 void gl_feedback_line( GLcontext
*ctx
, GLuint v1
, GLuint v2
, GLuint pv
)
216 GLenum token
= GL_LINE_TOKEN
;
218 if (ctx
->StippleCounter
==0)
219 token
= GL_LINE_RESET_TOKEN
;
221 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) token
);
223 gl_do_feedback_vertex( ctx
, v1
, pv
);
224 gl_do_feedback_vertex( ctx
, v2
, pv
);
226 ctx
->StippleCounter
++;
230 void gl_feedback_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
232 struct vertex_buffer
*VB
= ctx
->VB
;
235 for (i
=first
;i
<=last
;i
++)
236 if (VB
->ClipMask
[i
]==0) {
237 FEEDBACK_TOKEN( ctx
, (GLfloat
) (GLint
) GL_POINT_TOKEN
);
238 gl_do_feedback_vertex( ctx
, i
, i
);
246 /**********************************************************************/
248 /**********************************************************************/
252 * NOTE: this function can't be put in a display list.
255 _mesa_SelectBuffer( GLsizei size
, GLuint
*buffer
)
257 GET_CURRENT_CONTEXT(ctx
);
258 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glSelectBuffer");
259 if (ctx
->RenderMode
==GL_SELECT
) {
260 gl_error( ctx
, GL_INVALID_OPERATION
, "glSelectBuffer" );
262 ctx
->Select
.Buffer
= buffer
;
263 ctx
->Select
.BufferSize
= size
;
264 ctx
->Select
.BufferCount
= 0;
266 ctx
->Select
.HitFlag
= GL_FALSE
;
267 ctx
->Select
.HitMinZ
= 1.0;
268 ctx
->Select
.HitMaxZ
= 0.0;
272 #define WRITE_RECORD( CTX, V ) \
273 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
274 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
276 CTX->Select.BufferCount++;
280 void gl_update_hitflag( GLcontext
*ctx
, GLfloat z
)
282 ctx
->Select
.HitFlag
= GL_TRUE
;
283 if (z
< ctx
->Select
.HitMinZ
) {
284 ctx
->Select
.HitMinZ
= z
;
286 if (z
> ctx
->Select
.HitMaxZ
) {
287 ctx
->Select
.HitMaxZ
= z
;
291 void gl_select_triangle( GLcontext
*ctx
,
292 GLuint v0
, GLuint v1
, GLuint v2
, GLuint pv
)
294 struct vertex_buffer
*VB
= ctx
->VB
;
296 if (gl_cull_triangle( ctx
, v0
, v1
, v2
, 0 )) {
297 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][3] / DEPTH_SCALE
);
298 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][3] / DEPTH_SCALE
);
299 gl_update_hitflag( ctx
, VB
->Win
.data
[v2
][3] / DEPTH_SCALE
);
304 void gl_select_line( GLcontext
*ctx
,
305 GLuint v0
, GLuint v1
, GLuint pv
)
307 struct vertex_buffer
*VB
= ctx
->VB
;
309 gl_update_hitflag( ctx
, VB
->Win
.data
[v0
][3] / DEPTH_SCALE
);
310 gl_update_hitflag( ctx
, VB
->Win
.data
[v1
][3] / DEPTH_SCALE
);
313 void gl_select_points( GLcontext
*ctx
, GLuint first
, GLuint last
)
315 struct vertex_buffer
*VB
= ctx
->VB
;
318 for (i
=first
;i
<=last
;i
++)
319 if (VB
->ClipMask
[i
]==0)
320 gl_update_hitflag( ctx
, VB
->Win
.data
[i
][3] / DEPTH_SCALE
);
324 static void write_hit_record( GLcontext
*ctx
)
327 GLuint zmin
, zmax
, zscale
= (~0u);
329 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
330 /* 2^32-1 and round to nearest unsigned integer. */
332 assert( ctx
!= NULL
); /* this line magically fixes a SunOS 5.x/gcc bug */
333 zmin
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMinZ
);
334 zmax
= (GLuint
) ((GLfloat
) zscale
* ctx
->Select
.HitMaxZ
);
336 WRITE_RECORD( ctx
, ctx
->Select
.NameStackDepth
);
337 WRITE_RECORD( ctx
, zmin
);
338 WRITE_RECORD( ctx
, zmax
);
339 for (i
=0;i
<ctx
->Select
.NameStackDepth
;i
++) {
340 WRITE_RECORD( ctx
, ctx
->Select
.NameStack
[i
] );
344 ctx
->Select
.HitFlag
= GL_FALSE
;
345 ctx
->Select
.HitMinZ
= 1.0;
346 ctx
->Select
.HitMaxZ
= -1.0;
352 _mesa_InitNames( void )
354 GET_CURRENT_CONTEXT(ctx
);
355 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glInitNames");
356 /* Record the hit before the HitFlag is wiped out again. */
357 if (ctx
->RenderMode
==GL_SELECT
) {
358 if (ctx
->Select
.HitFlag
) {
359 write_hit_record( ctx
);
362 ctx
->Select
.NameStackDepth
= 0;
363 ctx
->Select
.HitFlag
= GL_FALSE
;
364 ctx
->Select
.HitMinZ
= 1.0;
365 ctx
->Select
.HitMaxZ
= 0.0;
371 _mesa_LoadName( GLuint name
)
373 GET_CURRENT_CONTEXT(ctx
);
374 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glLoadName");
375 if (ctx
->RenderMode
!=GL_SELECT
) {
378 if (ctx
->Select
.NameStackDepth
==0) {
379 gl_error( ctx
, GL_INVALID_OPERATION
, "glLoadName" );
382 if (ctx
->Select
.HitFlag
) {
383 write_hit_record( ctx
);
385 if (ctx
->Select
.NameStackDepth
<MAX_NAME_STACK_DEPTH
) {
386 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
-1] = name
;
389 ctx
->Select
.NameStack
[MAX_NAME_STACK_DEPTH
-1] = name
;
395 _mesa_PushName( GLuint name
)
397 GET_CURRENT_CONTEXT(ctx
);
398 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPushName");
399 if (ctx
->RenderMode
!=GL_SELECT
) {
402 if (ctx
->Select
.HitFlag
) {
403 write_hit_record( ctx
);
405 if (ctx
->Select
.NameStackDepth
<MAX_NAME_STACK_DEPTH
) {
406 ctx
->Select
.NameStack
[ctx
->Select
.NameStackDepth
++] = name
;
409 gl_error( ctx
, GL_STACK_OVERFLOW
, "glPushName" );
416 _mesa_PopName( void )
418 GET_CURRENT_CONTEXT(ctx
);
419 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glPopName");
420 if (ctx
->RenderMode
!=GL_SELECT
) {
423 if (ctx
->Select
.HitFlag
) {
424 write_hit_record( ctx
);
426 if (ctx
->Select
.NameStackDepth
>0) {
427 ctx
->Select
.NameStackDepth
--;
430 gl_error( ctx
, GL_STACK_UNDERFLOW
, "glPopName" );
436 /**********************************************************************/
438 /**********************************************************************/
443 * NOTE: this function can't be put in a display list.
446 _mesa_RenderMode( GLenum mode
)
448 GET_CURRENT_CONTEXT(ctx
);
451 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx
, "glRenderMode", 0);
453 if (MESA_VERBOSE
& VERBOSE_API
)
454 fprintf(stderr
, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode
));
456 ctx
->TriangleCaps
&= ~(DD_FEEDBACK
|DD_SELECT
);
458 switch (ctx
->RenderMode
) {
463 if (ctx
->Select
.HitFlag
) {
464 write_hit_record( ctx
);
466 if (ctx
->Select
.BufferCount
> ctx
->Select
.BufferSize
) {
469 gl_warning(ctx
, "Feedback buffer overflow");
474 result
= ctx
->Select
.Hits
;
476 ctx
->Select
.BufferCount
= 0;
477 ctx
->Select
.Hits
= 0;
478 ctx
->Select
.NameStackDepth
= 0;
481 if (ctx
->Feedback
.Count
> ctx
->Feedback
.BufferSize
) {
486 result
= ctx
->Feedback
.Count
;
488 ctx
->Feedback
.Count
= 0;
491 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
499 ctx
->TriangleCaps
|= DD_SELECT
;
500 if (ctx
->Select
.BufferSize
==0) {
501 /* haven't called glSelectBuffer yet */
502 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
506 ctx
->TriangleCaps
|= DD_FEEDBACK
;
507 if (ctx
->Feedback
.BufferSize
==0) {
508 /* haven't called glFeedbackBuffer yet */
509 gl_error( ctx
, GL_INVALID_OPERATION
, "glRenderMode" );
513 gl_error( ctx
, GL_INVALID_ENUM
, "glRenderMode" );
518 ctx
->RenderMode
= mode
;
519 ctx
->NewState
|= NEW_ALL
;