106f34300a86fd9081ef80a2bd2eee91f50249f7
[mesa.git] / src / mesa / main / feedback.c
1 /**
2 * \file feedback.c
3 * Selection and feedback modes functions.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 5.1
9 *
10 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "enums.h"
35 #include "feedback.h"
36 #include "macros.h"
37 #include "mtypes.h"
38
39
40 #if _HAVE_FULL_GL
41
42
43 #define FB_3D 0x01
44 #define FB_4D 0x02
45 #define FB_INDEX 0x04
46 #define FB_COLOR 0x08
47 #define FB_TEXTURE 0X10
48
49
50
51 void GLAPIENTRY
52 _mesa_FeedbackBuffer( GLsizei size, GLenum type, GLfloat *buffer )
53 {
54 GET_CURRENT_CONTEXT(ctx);
55 ASSERT_OUTSIDE_BEGIN_END(ctx);
56
57 if (ctx->RenderMode==GL_FEEDBACK) {
58 _mesa_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
59 return;
60 }
61 if (size<0) {
62 _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
63 return;
64 }
65 if (!buffer) {
66 _mesa_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
67 ctx->Feedback.BufferSize = 0;
68 return;
69 }
70
71 switch (type) {
72 case GL_2D:
73 ctx->Feedback._Mask = 0;
74 break;
75 case GL_3D:
76 ctx->Feedback._Mask = FB_3D;
77 break;
78 case GL_3D_COLOR:
79 ctx->Feedback._Mask = (FB_3D |
80 (ctx->Visual.rgbMode ? FB_COLOR : FB_INDEX));
81 break;
82 case GL_3D_COLOR_TEXTURE:
83 ctx->Feedback._Mask = (FB_3D |
84 (ctx->Visual.rgbMode ? FB_COLOR : FB_INDEX) |
85 FB_TEXTURE);
86 break;
87 case GL_4D_COLOR_TEXTURE:
88 ctx->Feedback._Mask = (FB_3D | FB_4D |
89 (ctx->Visual.rgbMode ? FB_COLOR : FB_INDEX) |
90 FB_TEXTURE);
91 break;
92 default:
93 _mesa_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
94 return;
95 }
96
97 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); /* Always flush */
98 ctx->Feedback.Type = type;
99 ctx->Feedback.BufferSize = size;
100 ctx->Feedback.Buffer = buffer;
101 ctx->Feedback.Count = 0; /* Becaues of this. */
102 }
103
104
105 void GLAPIENTRY
106 _mesa_PassThrough( GLfloat token )
107 {
108 GET_CURRENT_CONTEXT(ctx);
109 ASSERT_OUTSIDE_BEGIN_END(ctx);
110
111 if (ctx->RenderMode==GL_FEEDBACK) {
112 FLUSH_VERTICES(ctx, 0);
113 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN );
114 FEEDBACK_TOKEN( ctx, token );
115 }
116 }
117
118
119
120 /*
121 * Put a vertex into the feedback buffer.
122 */
123 void _mesa_feedback_vertex( GLcontext *ctx,
124 const GLfloat win[4],
125 const GLfloat color[4],
126 GLuint index,
127 const GLfloat texcoord[4] )
128 {
129 FEEDBACK_TOKEN( ctx, win[0] );
130 FEEDBACK_TOKEN( ctx, win[1] );
131 if (ctx->Feedback._Mask & FB_3D) {
132 FEEDBACK_TOKEN( ctx, win[2] );
133 }
134 if (ctx->Feedback._Mask & FB_4D) {
135 FEEDBACK_TOKEN( ctx, win[3] );
136 }
137 if (ctx->Feedback._Mask & FB_INDEX) {
138 FEEDBACK_TOKEN( ctx, (GLfloat) index );
139 }
140 if (ctx->Feedback._Mask & FB_COLOR) {
141 FEEDBACK_TOKEN( ctx, color[0] );
142 FEEDBACK_TOKEN( ctx, color[1] );
143 FEEDBACK_TOKEN( ctx, color[2] );
144 FEEDBACK_TOKEN( ctx, color[3] );
145 }
146 if (ctx->Feedback._Mask & FB_TEXTURE) {
147 FEEDBACK_TOKEN( ctx, texcoord[0] );
148 FEEDBACK_TOKEN( ctx, texcoord[1] );
149 FEEDBACK_TOKEN( ctx, texcoord[2] );
150 FEEDBACK_TOKEN( ctx, texcoord[3] );
151 }
152 }
153
154 #endif
155
156
157 /**********************************************************************/
158 /** \name Selection */
159 /*@{*/
160
161 /**
162 * Establish a buffer for selection mode values.
163 *
164 * \param size buffer size.
165 * \param buffer buffer.
166 *
167 * \sa glSelectBuffer().
168 *
169 * \note this function can't be put in a display list.
170 *
171 * Verifies we're not in selection mode, flushes the vertices and initialize
172 * the fields in __GLcontextRec::Select with the given buffer.
173 */
174 void GLAPIENTRY
175 _mesa_SelectBuffer( GLsizei size, GLuint *buffer )
176 {
177 GET_CURRENT_CONTEXT(ctx);
178 ASSERT_OUTSIDE_BEGIN_END(ctx);
179
180 if (ctx->RenderMode==GL_SELECT) {
181 _mesa_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
182 return; /* KW: added return */
183 }
184
185 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
186 ctx->Select.Buffer = buffer;
187 ctx->Select.BufferSize = size;
188 ctx->Select.BufferCount = 0;
189 ctx->Select.HitFlag = GL_FALSE;
190 ctx->Select.HitMinZ = 1.0;
191 ctx->Select.HitMaxZ = 0.0;
192 }
193
194
195 /**
196 * Write a value of a record into the selection buffer.
197 *
198 * \param CTX GL context.
199 * \param V value.
200 *
201 * Verifies there is free space in the buffer to write the value and
202 * increments the pointer.
203 */
204 #define WRITE_RECORD( CTX, V ) \
205 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
206 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
207 } \
208 CTX->Select.BufferCount++;
209
210
211 /**
212 * Update the hit flag and the maximum and minimum depth values.
213 *
214 * \param ctx GL context.
215 * \param z depth.
216 *
217 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
218 * gl_selection::HitMaxZ.
219 */
220 void _mesa_update_hitflag( GLcontext *ctx, GLfloat z )
221 {
222 ctx->Select.HitFlag = GL_TRUE;
223 if (z < ctx->Select.HitMinZ) {
224 ctx->Select.HitMinZ = z;
225 }
226 if (z > ctx->Select.HitMaxZ) {
227 ctx->Select.HitMaxZ = z;
228 }
229 }
230
231
232 /**
233 * Write the hit record.
234 *
235 * \param ctx GL context.
236 *
237 * Write the hit record, i.e., the number of names in the stack, the minimum and
238 * maximum depth values and the number of names in the name stack at the time
239 * of the event. Resets the hit flag.
240 *
241 * \sa gl_selection.
242 */
243 static void write_hit_record( GLcontext *ctx )
244 {
245 GLuint i;
246 GLuint zmin, zmax, zscale = (~0u);
247
248 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
249 /* 2^32-1 and round to nearest unsigned integer. */
250
251 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
252 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
253 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
254
255 WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
256 WRITE_RECORD( ctx, zmin );
257 WRITE_RECORD( ctx, zmax );
258 for (i = 0; i < ctx->Select.NameStackDepth; i++) {
259 WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
260 }
261
262 ctx->Select.Hits++;
263 ctx->Select.HitFlag = GL_FALSE;
264 ctx->Select.HitMinZ = 1.0;
265 ctx->Select.HitMaxZ = -1.0;
266 }
267
268
269 /**
270 * Initialize the name stack.
271 *
272 * Verifies we are in select mode and resets the name stack depth and resets
273 * the hit record data in gl_selection. Marks new render mode in
274 * __GLcontextRec::NewState.
275 */
276 void GLAPIENTRY
277 _mesa_InitNames( void )
278 {
279 GET_CURRENT_CONTEXT(ctx);
280 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
281
282 /* Record the hit before the HitFlag is wiped out again. */
283 if (ctx->RenderMode == GL_SELECT) {
284 if (ctx->Select.HitFlag) {
285 write_hit_record( ctx );
286 }
287 }
288 ctx->Select.NameStackDepth = 0;
289 ctx->Select.HitFlag = GL_FALSE;
290 ctx->Select.HitMinZ = 1.0;
291 ctx->Select.HitMaxZ = 0.0;
292 ctx->NewState |= _NEW_RENDERMODE;
293 }
294
295
296 /**
297 * Load the top-most name of the name stack.
298 *
299 * \param name name.
300 *
301 * Verifies we are in selection mode and that the name stack is not empty.
302 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
303 * and replace the top-most name in the stack.
304 *
305 * sa __GLcontextRec::Select.
306 */
307 void GLAPIENTRY
308 _mesa_LoadName( GLuint name )
309 {
310 GET_CURRENT_CONTEXT(ctx);
311 ASSERT_OUTSIDE_BEGIN_END(ctx);
312
313 if (ctx->RenderMode != GL_SELECT) {
314 return;
315 }
316 if (ctx->Select.NameStackDepth == 0) {
317 _mesa_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
318 return;
319 }
320
321 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
322
323 if (ctx->Select.HitFlag) {
324 write_hit_record( ctx );
325 }
326 if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
327 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
328 }
329 else {
330 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
331 }
332 }
333
334
335 /**
336 * Push a name into the name stack.
337 *
338 * \param name name.
339 *
340 * Verifies we are in selection mode and that the name stack is not full.
341 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
342 * and adds the name to the top of the name stack.
343 *
344 * sa __GLcontextRec::Select.
345 */
346 void GLAPIENTRY
347 _mesa_PushName( GLuint name )
348 {
349 GET_CURRENT_CONTEXT(ctx);
350 ASSERT_OUTSIDE_BEGIN_END(ctx);
351
352 if (ctx->RenderMode != GL_SELECT) {
353 return;
354 }
355
356 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
357 if (ctx->Select.HitFlag) {
358 write_hit_record( ctx );
359 }
360 if (ctx->Select.NameStackDepth >= MAX_NAME_STACK_DEPTH) {
361 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
362 }
363 else
364 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
365 }
366
367
368 /**
369 * Pop a name into the name stack.
370 *
371 * Verifies we are in selection mode and that the name stack is not empty.
372 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
373 * and removes top-most name in the name stack.
374 *
375 * sa __GLcontextRec::Select.
376 */
377 void GLAPIENTRY
378 _mesa_PopName( void )
379 {
380 GET_CURRENT_CONTEXT(ctx);
381 ASSERT_OUTSIDE_BEGIN_END(ctx);
382
383 if (ctx->RenderMode != GL_SELECT) {
384 return;
385 }
386
387 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
388 if (ctx->Select.HitFlag) {
389 write_hit_record( ctx );
390 }
391 if (ctx->Select.NameStackDepth == 0) {
392 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
393 }
394 else
395 ctx->Select.NameStackDepth--;
396 }
397
398 /*@}*/
399
400
401 /**********************************************************************/
402 /** \name Render Mode */
403 /*@{*/
404
405 /**
406 * Set rasterization mode.
407 *
408 * \param mode rasterization mode.
409 *
410 * \note this function can't be put in a display list.
411 *
412 * \sa glRenderMode().
413 *
414 * Flushes the vertices and do the necessary cleanup according to the previous
415 * rasterization mode, such as writing the hit record or resent the select
416 * buffer index when exiting the select mode. Updates
417 * __GLcontextRec::RenderMode and notifies the driver via the
418 * dd_function_table::RenderMode callback.
419 */
420 GLint GLAPIENTRY
421 _mesa_RenderMode( GLenum mode )
422 {
423 GET_CURRENT_CONTEXT(ctx);
424 GLint result;
425 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
426
427 if (MESA_VERBOSE & VERBOSE_API)
428 _mesa_debug(ctx, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode));
429
430 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
431
432 switch (ctx->RenderMode) {
433 case GL_RENDER:
434 result = 0;
435 break;
436 case GL_SELECT:
437 if (ctx->Select.HitFlag) {
438 write_hit_record( ctx );
439 }
440 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
441 /* overflow */
442 #ifdef DEBUG
443 _mesa_warning(ctx, "Feedback buffer overflow");
444 #endif
445 result = -1;
446 }
447 else {
448 result = ctx->Select.Hits;
449 }
450 ctx->Select.BufferCount = 0;
451 ctx->Select.Hits = 0;
452 ctx->Select.NameStackDepth = 0;
453 break;
454 #if _HAVE_FULL_GL
455 case GL_FEEDBACK:
456 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
457 /* overflow */
458 result = -1;
459 }
460 else {
461 result = ctx->Feedback.Count;
462 }
463 ctx->Feedback.Count = 0;
464 break;
465 #endif
466 default:
467 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
468 return 0;
469 }
470
471 switch (mode) {
472 case GL_RENDER:
473 break;
474 case GL_SELECT:
475 if (ctx->Select.BufferSize==0) {
476 /* haven't called glSelectBuffer yet */
477 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
478 }
479 break;
480 #if _HAVE_FULL_GL
481 case GL_FEEDBACK:
482 if (ctx->Feedback.BufferSize==0) {
483 /* haven't called glFeedbackBuffer yet */
484 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
485 }
486 break;
487 #endif
488 default:
489 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
490 return 0;
491 }
492
493 ctx->RenderMode = mode;
494 if (ctx->Driver.RenderMode)
495 ctx->Driver.RenderMode( ctx, mode );
496
497 return result;
498 }
499
500 /*@}*/
501
502
503 /**********************************************************************/
504 /** \name Initialization */
505 /*@{*/
506
507 /**
508 * Initialize context feedback data.
509 */
510 void _mesa_init_feedback( GLcontext * ctx )
511 {
512 /* Feedback */
513 ctx->Feedback.Type = GL_2D; /* TODO: verify */
514 ctx->Feedback.Buffer = NULL;
515 ctx->Feedback.BufferSize = 0;
516 ctx->Feedback.Count = 0;
517
518 /* Selection/picking */
519 ctx->Select.Buffer = NULL;
520 ctx->Select.BufferSize = 0;
521 ctx->Select.BufferCount = 0;
522 ctx->Select.Hits = 0;
523 ctx->Select.NameStackDepth = 0;
524
525 /* Miscellaneous */
526 ctx->RenderMode = GL_RENDER;
527 }
528
529 /*@}*/