Fixed includes & added a few hooks for the DRI.
[mesa.git] / src / mesa / main / feedback.c
1 /* $Id: feedback.c,v 1.4 1999/10/08 09:27:10 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28
29
30
31 #ifdef PC_HEADER
32 #include "all.h"
33 #else
34 #ifndef XFree86Server
35 #include <assert.h>
36 #include <stdio.h>
37 #else
38 #include "GL/xf86glx.h"
39 #endif
40 #include "context.h"
41 #include "enums.h"
42 #include "feedback.h"
43 #include "macros.h"
44 #include "mmath.h"
45 #include "types.h"
46 #include "triangle.h"
47 #ifdef XFree86Server
48 #include "GL/xf86glx.h"
49 #endif
50 #endif
51
52
53
54 #define FB_3D 0x01
55 #define FB_4D 0x02
56 #define FB_INDEX 0x04
57 #define FB_COLOR 0x08
58 #define FB_TEXTURE 0X10
59
60
61
62 void
63 gl_FeedbackBuffer( GLcontext *ctx, GLsizei size, GLenum type, GLfloat *buffer )
64 {
65 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx, "glFeedbackBuffer" );
66
67 if (ctx->RenderMode==GL_FEEDBACK) {
68 gl_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
69 return;
70 }
71
72 if (size<0) {
73 gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
74 return;
75 }
76 if (!buffer) {
77 gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
78 ctx->Feedback.BufferSize = 0;
79 return;
80 }
81
82 switch (type) {
83 case GL_2D:
84 ctx->Feedback.Mask = 0;
85 ctx->Feedback.Type = type;
86 break;
87 case GL_3D:
88 ctx->Feedback.Mask = FB_3D;
89 ctx->Feedback.Type = type;
90 break;
91 case GL_3D_COLOR:
92 ctx->Feedback.Mask = FB_3D
93 | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX);
94 ctx->Feedback.Type = type;
95 break;
96 case GL_3D_COLOR_TEXTURE:
97 ctx->Feedback.Mask = FB_3D
98 | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
99 | FB_TEXTURE;
100 ctx->Feedback.Type = type;
101 break;
102 case GL_4D_COLOR_TEXTURE:
103 ctx->Feedback.Mask = FB_3D | FB_4D
104 | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
105 | FB_TEXTURE;
106 ctx->Feedback.Type = type;
107 break;
108 default:
109 ctx->Feedback.Mask = 0;
110 gl_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
111 }
112
113 ctx->Feedback.BufferSize = size;
114 ctx->Feedback.Buffer = buffer;
115 ctx->Feedback.Count = 0;
116 }
117
118
119
120 void gl_PassThrough( GLcontext *ctx, GLfloat token )
121 {
122 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPassThrough");
123
124 if (ctx->RenderMode==GL_FEEDBACK) {
125 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN );
126 FEEDBACK_TOKEN( ctx, token );
127 }
128 }
129
130
131
132 /*
133 * Put a vertex into the feedback buffer.
134 */
135 void gl_feedback_vertex( GLcontext *ctx,
136 const GLfloat win[4],
137 const GLfloat color[4],
138 GLuint index,
139 const GLfloat texcoord[4] )
140 {
141 FEEDBACK_TOKEN( ctx, win[0] );
142 FEEDBACK_TOKEN( ctx, win[1] );
143 if (ctx->Feedback.Mask & FB_3D) {
144 FEEDBACK_TOKEN( ctx, win[2] );
145 }
146 if (ctx->Feedback.Mask & FB_4D) {
147 FEEDBACK_TOKEN( ctx, win[3] );
148 }
149 if (ctx->Feedback.Mask & FB_INDEX) {
150 FEEDBACK_TOKEN( ctx, (GLfloat) index );
151 }
152 if (ctx->Feedback.Mask & FB_COLOR) {
153 FEEDBACK_TOKEN( ctx, color[0] );
154 FEEDBACK_TOKEN( ctx, color[1] );
155 FEEDBACK_TOKEN( ctx, color[2] );
156 FEEDBACK_TOKEN( ctx, color[3] );
157 }
158 if (ctx->Feedback.Mask & FB_TEXTURE) {
159 FEEDBACK_TOKEN( ctx, texcoord[0] );
160 FEEDBACK_TOKEN( ctx, texcoord[1] );
161 FEEDBACK_TOKEN( ctx, texcoord[2] );
162 FEEDBACK_TOKEN( ctx, texcoord[3] );
163 }
164 }
165
166
167
168 static void gl_do_feedback_vertex( GLcontext *ctx, GLuint v, GLuint pv )
169 {
170 GLfloat win[4];
171 GLfloat color[4];
172 GLfloat tc[4];
173 GLuint texUnit = ctx->Texture.CurrentTransformUnit;
174 struct vertex_buffer *VB = ctx->VB;
175
176 win[0] = VB->Win.data[v][0];
177 win[1] = VB->Win.data[v][1];
178 win[2] = VB->Win.data[v][2] / DEPTH_SCALE;
179 win[3] = 1.0 / VB->Win.data[v][3];
180
181 if (ctx->Light.ShadeModel==GL_SMOOTH) pv = v;
182
183 UBYTE_RGBA_TO_FLOAT_RGBA( color, VB->ColorPtr->data[pv] );
184
185 if (VB->TexCoordPtr[texUnit]->size == 4 &&
186 VB->TexCoordPtr[texUnit]->data[v][3]!=0.0)
187 {
188 GLfloat invq = 1.0F / VB->TexCoordPtr[texUnit]->data[v][3];
189 tc[0] = VB->TexCoordPtr[texUnit]->data[v][0] * invq;
190 tc[1] = VB->TexCoordPtr[texUnit]->data[v][1] * invq;
191 tc[2] = VB->TexCoordPtr[texUnit]->data[v][2] * invq;
192 tc[3] = VB->TexCoordPtr[texUnit]->data[v][3];
193 } else {
194 ASSIGN_4V(tc, 0,0,0,1);
195 COPY_SZ_4V(tc,
196 VB->TexCoordPtr[texUnit]->size,
197 VB->TexCoordPtr[texUnit]->data[v]);
198 }
199
200 gl_feedback_vertex( ctx, win, color, VB->IndexPtr->data[v], tc );
201 }
202
203
204
205 /*
206 * Put triangle in feedback buffer.
207 */
208 void gl_feedback_triangle( GLcontext *ctx,
209 GLuint v0, GLuint v1, GLuint v2, GLuint pv )
210 {
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 */
214
215 gl_do_feedback_vertex( ctx, v0, pv );
216 gl_do_feedback_vertex( ctx, v1, pv );
217 gl_do_feedback_vertex( ctx, v2, pv );
218 }
219 }
220
221
222 void gl_feedback_line( GLcontext *ctx, GLuint v1, GLuint v2, GLuint pv )
223 {
224 GLenum token = GL_LINE_TOKEN;
225
226 if (ctx->StippleCounter==0)
227 token = GL_LINE_RESET_TOKEN;
228
229 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) token );
230
231 gl_do_feedback_vertex( ctx, v1, pv );
232 gl_do_feedback_vertex( ctx, v2, pv );
233
234 ctx->StippleCounter++;
235 }
236
237
238 void gl_feedback_points( GLcontext *ctx, GLuint first, GLuint last )
239 {
240 struct vertex_buffer *VB = ctx->VB;
241 GLuint i;
242
243 for (i=first;i<=last;i++)
244 if (VB->ClipMask[i]==0) {
245 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_POINT_TOKEN );
246 gl_do_feedback_vertex( ctx, i, i );
247 }
248 }
249
250
251
252
253
254 /**********************************************************************/
255 /* Selection */
256 /**********************************************************************/
257
258
259 /*
260 * NOTE: this function can't be put in a display list.
261 */
262 void gl_SelectBuffer( GLcontext *ctx, GLsizei size, GLuint *buffer )
263 {
264 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glSelectBuffer");
265 if (ctx->RenderMode==GL_SELECT) {
266 gl_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
267 }
268 ctx->Select.Buffer = buffer;
269 ctx->Select.BufferSize = size;
270 ctx->Select.BufferCount = 0;
271
272 ctx->Select.HitFlag = GL_FALSE;
273 ctx->Select.HitMinZ = 1.0;
274 ctx->Select.HitMaxZ = 0.0;
275 }
276
277
278 #define WRITE_RECORD( CTX, V ) \
279 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
280 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
281 } \
282 CTX->Select.BufferCount++;
283
284
285
286 void gl_update_hitflag( GLcontext *ctx, GLfloat z )
287 {
288 ctx->Select.HitFlag = GL_TRUE;
289 if (z < ctx->Select.HitMinZ) {
290 ctx->Select.HitMinZ = z;
291 }
292 if (z > ctx->Select.HitMaxZ) {
293 ctx->Select.HitMaxZ = z;
294 }
295 }
296
297 void gl_select_triangle( GLcontext *ctx,
298 GLuint v0, GLuint v1, GLuint v2, GLuint pv )
299 {
300 struct vertex_buffer *VB = ctx->VB;
301
302 if (gl_cull_triangle( ctx, v0, v1, v2, 0 )) {
303 gl_update_hitflag( ctx, VB->Win.data[v0][3] / DEPTH_SCALE );
304 gl_update_hitflag( ctx, VB->Win.data[v1][3] / DEPTH_SCALE );
305 gl_update_hitflag( ctx, VB->Win.data[v2][3] / DEPTH_SCALE );
306 }
307 }
308
309
310 void gl_select_line( GLcontext *ctx,
311 GLuint v0, GLuint v1, GLuint pv )
312 {
313 struct vertex_buffer *VB = ctx->VB;
314
315 gl_update_hitflag( ctx, VB->Win.data[v0][3] / DEPTH_SCALE );
316 gl_update_hitflag( ctx, VB->Win.data[v1][3] / DEPTH_SCALE );
317 }
318
319 void gl_select_points( GLcontext *ctx, GLuint first, GLuint last )
320 {
321 struct vertex_buffer *VB = ctx->VB;
322 GLuint i;
323
324 for (i=first;i<=last;i++)
325 if (VB->ClipMask[i]==0)
326 gl_update_hitflag( ctx, VB->Win.data[i][3] / DEPTH_SCALE);
327 }
328
329
330 static void write_hit_record( GLcontext *ctx )
331 {
332 GLuint i;
333 GLuint zmin, zmax, zscale = (~0u);
334
335 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
336 /* 2^32-1 and round to nearest unsigned integer. */
337
338 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
339 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
340 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
341
342 WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
343 WRITE_RECORD( ctx, zmin );
344 WRITE_RECORD( ctx, zmax );
345 for (i=0;i<ctx->Select.NameStackDepth;i++) {
346 WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
347 }
348
349 ctx->Select.Hits++;
350 ctx->Select.HitFlag = GL_FALSE;
351 ctx->Select.HitMinZ = 1.0;
352 ctx->Select.HitMaxZ = -1.0;
353 }
354
355
356
357 void gl_InitNames( GLcontext *ctx )
358 {
359 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glInitNames");
360 /* Record the hit before the HitFlag is wiped out again. */
361 if (ctx->RenderMode==GL_SELECT) {
362 if (ctx->Select.HitFlag) {
363 write_hit_record( ctx );
364 }
365 }
366 ctx->Select.NameStackDepth = 0;
367 ctx->Select.HitFlag = GL_FALSE;
368 ctx->Select.HitMinZ = 1.0;
369 ctx->Select.HitMaxZ = 0.0;
370 }
371
372
373
374 void gl_LoadName( GLcontext *ctx, GLuint name )
375 {
376 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLoadName");
377 if (ctx->RenderMode!=GL_SELECT) {
378 return;
379 }
380 if (ctx->Select.NameStackDepth==0) {
381 gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
382 return;
383 }
384 if (ctx->Select.HitFlag) {
385 write_hit_record( ctx );
386 }
387 if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
388 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
389 }
390 else {
391 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
392 }
393 }
394
395
396 void gl_PushName( GLcontext *ctx, GLuint name )
397 {
398 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushName");
399 if (ctx->RenderMode!=GL_SELECT) {
400 return;
401 }
402 if (ctx->Select.HitFlag) {
403 write_hit_record( ctx );
404 }
405 if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
406 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
407 }
408 else {
409 gl_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
410 }
411 }
412
413
414
415 void gl_PopName( GLcontext *ctx )
416 {
417 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopName");
418 if (ctx->RenderMode!=GL_SELECT) {
419 return;
420 }
421 if (ctx->Select.HitFlag) {
422 write_hit_record( ctx );
423 }
424 if (ctx->Select.NameStackDepth>0) {
425 ctx->Select.NameStackDepth--;
426 }
427 else {
428 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
429 }
430 }
431
432
433
434 /**********************************************************************/
435 /* Render Mode */
436 /**********************************************************************/
437
438
439
440 /*
441 * NOTE: this function can't be put in a display list.
442 */
443 GLint gl_RenderMode( GLcontext *ctx, GLenum mode )
444 {
445 GLint result;
446
447 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glRenderMode", 0);
448
449 if (MESA_VERBOSE & VERBOSE_API)
450 fprintf(stderr, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode));
451
452 ctx->TriangleCaps &= ~(DD_FEEDBACK|DD_SELECT);
453
454 switch (ctx->RenderMode) {
455 case GL_RENDER:
456 result = 0;
457 break;
458 case GL_SELECT:
459 if (ctx->Select.HitFlag) {
460 write_hit_record( ctx );
461 }
462 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
463 /* overflow */
464 #ifdef DEBUG
465 gl_warning(ctx, "Feedback buffer overflow");
466 #endif
467 result = -1;
468 }
469 else {
470 result = ctx->Select.Hits;
471 }
472 ctx->Select.BufferCount = 0;
473 ctx->Select.Hits = 0;
474 ctx->Select.NameStackDepth = 0;
475 break;
476 case GL_FEEDBACK:
477 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
478 /* overflow */
479 result = -1;
480 }
481 else {
482 result = ctx->Feedback.Count;
483 }
484 ctx->Feedback.Count = 0;
485 break;
486 default:
487 gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
488 return 0;
489 }
490
491 switch (mode) {
492 case GL_RENDER:
493 break;
494 case GL_SELECT:
495 ctx->TriangleCaps |= DD_SELECT;
496 if (ctx->Select.BufferSize==0) {
497 /* haven't called glSelectBuffer yet */
498 gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
499 }
500 break;
501 case GL_FEEDBACK:
502 ctx->TriangleCaps |= DD_FEEDBACK;
503 if (ctx->Feedback.BufferSize==0) {
504 /* haven't called glFeedbackBuffer yet */
505 gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
506 }
507 break;
508 default:
509 gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
510 return 0;
511 }
512
513
514 ctx->RenderMode = mode;
515 ctx->NewState |= NEW_ALL;
516
517 return result;
518 }
519