feedback.c revision 7117f1b4
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
51void 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
105void 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 */
123void _mesa_feedback_vertex( GLcontext *ctx,
124                            const GLfloat win[4],
125                            const GLfloat color[4],
126                            GLfloat index,
127                            const GLfloat texcoord[4] )
128{
129#if 0
130   {
131      /* snap window x, y to fractional pixel position */
132      const GLint snapMask = ~((FIXED_ONE / (1 << SUB_PIXEL_BITS)) - 1);
133      GLfixed x, y;
134      x = FloatToFixed(win[0]) & snapMask;
135      y = FloatToFixed(win[1]) & snapMask;
136      FEEDBACK_TOKEN(ctx, FixedToFloat(x));
137      FEEDBACK_TOKEN(ctx, FixedToFloat(y) );
138   }
139#else
140   FEEDBACK_TOKEN( ctx, win[0] );
141   FEEDBACK_TOKEN( ctx, win[1] );
142#endif
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#endif
167
168
169/**********************************************************************/
170/** \name Selection */
171/*@{*/
172
173/**
174 * Establish a buffer for selection mode values.
175 *
176 * \param size buffer size.
177 * \param buffer buffer.
178 *
179 * \sa glSelectBuffer().
180 *
181 * \note this function can't be put in a display list.
182 *
183 * Verifies we're not in selection mode, flushes the vertices and initialize
184 * the fields in __GLcontextRec::Select with the given buffer.
185 */
186void GLAPIENTRY
187_mesa_SelectBuffer( GLsizei size, GLuint *buffer )
188{
189   GET_CURRENT_CONTEXT(ctx);
190   ASSERT_OUTSIDE_BEGIN_END(ctx);
191
192   if (ctx->RenderMode==GL_SELECT) {
193      _mesa_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
194      return;			/* KW: added return */
195   }
196
197   FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
198   ctx->Select.Buffer = buffer;
199   ctx->Select.BufferSize = size;
200   ctx->Select.BufferCount = 0;
201   ctx->Select.HitFlag = GL_FALSE;
202   ctx->Select.HitMinZ = 1.0;
203   ctx->Select.HitMaxZ = 0.0;
204}
205
206
207/**
208 * Write a value of a record into the selection buffer.
209 *
210 * \param CTX GL context.
211 * \param V value.
212 *
213 * Verifies there is free space in the buffer to write the value and
214 * increments the pointer.
215 */
216#define WRITE_RECORD( CTX, V )					\
217	if (CTX->Select.BufferCount < CTX->Select.BufferSize) {	\
218	   CTX->Select.Buffer[CTX->Select.BufferCount] = (V);	\
219	}							\
220	CTX->Select.BufferCount++;
221
222
223/**
224 * Update the hit flag and the maximum and minimum depth values.
225 *
226 * \param ctx GL context.
227 * \param z depth.
228 *
229 * Sets gl_selection::HitFlag and updates gl_selection::HitMinZ and
230 * gl_selection::HitMaxZ.
231 */
232void _mesa_update_hitflag( GLcontext *ctx, GLfloat z )
233{
234   ctx->Select.HitFlag = GL_TRUE;
235   if (z < ctx->Select.HitMinZ) {
236      ctx->Select.HitMinZ = z;
237   }
238   if (z > ctx->Select.HitMaxZ) {
239      ctx->Select.HitMaxZ = z;
240   }
241}
242
243
244/**
245 * Write the hit record.
246 *
247 * \param ctx GL context.
248 *
249 * Write the hit record, i.e., the number of names in the stack, the minimum and
250 * maximum depth values and the number of names in the name stack at the time
251 * of the event. Resets the hit flag.
252 *
253 * \sa gl_selection.
254 */
255static void write_hit_record( GLcontext *ctx )
256{
257   GLuint i;
258   GLuint zmin, zmax, zscale = (~0u);
259
260   /* HitMinZ and HitMaxZ are in [0,1].  Multiply these values by */
261   /* 2^32-1 and round to nearest unsigned integer. */
262
263   assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
264   zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
265   zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
266
267   WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
268   WRITE_RECORD( ctx, zmin );
269   WRITE_RECORD( ctx, zmax );
270   for (i = 0; i < ctx->Select.NameStackDepth; i++) {
271      WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
272   }
273
274   ctx->Select.Hits++;
275   ctx->Select.HitFlag = GL_FALSE;
276   ctx->Select.HitMinZ = 1.0;
277   ctx->Select.HitMaxZ = -1.0;
278}
279
280
281/**
282 * Initialize the name stack.
283 *
284 * Verifies we are in select mode and resets the name stack depth and resets
285 * the hit record data in gl_selection. Marks new render mode in
286 * __GLcontextRec::NewState.
287 */
288void GLAPIENTRY
289_mesa_InitNames( void )
290{
291   GET_CURRENT_CONTEXT(ctx);
292   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
293
294   /* Record the hit before the HitFlag is wiped out again. */
295   if (ctx->RenderMode == GL_SELECT) {
296      if (ctx->Select.HitFlag) {
297         write_hit_record( ctx );
298      }
299   }
300   ctx->Select.NameStackDepth = 0;
301   ctx->Select.HitFlag = GL_FALSE;
302   ctx->Select.HitMinZ = 1.0;
303   ctx->Select.HitMaxZ = 0.0;
304   ctx->NewState |= _NEW_RENDERMODE;
305}
306
307
308/**
309 * Load the top-most name of the name stack.
310 *
311 * \param name name.
312 *
313 * Verifies we are in selection mode and that the name stack is not empty.
314 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
315 * and replace the top-most name in the stack.
316 *
317 * sa __GLcontextRec::Select.
318 */
319void GLAPIENTRY
320_mesa_LoadName( GLuint name )
321{
322   GET_CURRENT_CONTEXT(ctx);
323   ASSERT_OUTSIDE_BEGIN_END(ctx);
324
325   if (ctx->RenderMode != GL_SELECT) {
326      return;
327   }
328   if (ctx->Select.NameStackDepth == 0) {
329      _mesa_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
330      return;
331   }
332
333   FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
334
335   if (ctx->Select.HitFlag) {
336      write_hit_record( ctx );
337   }
338   if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
339      ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
340   }
341   else {
342      ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
343   }
344}
345
346
347/**
348 * Push a name into the name stack.
349 *
350 * \param name name.
351 *
352 * Verifies we are in selection mode and that the name stack is not full.
353 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
354 * and adds the name to the top of the name stack.
355 *
356 * sa __GLcontextRec::Select.
357 */
358void GLAPIENTRY
359_mesa_PushName( GLuint name )
360{
361   GET_CURRENT_CONTEXT(ctx);
362   ASSERT_OUTSIDE_BEGIN_END(ctx);
363
364   if (ctx->RenderMode != GL_SELECT) {
365      return;
366   }
367
368   FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
369   if (ctx->Select.HitFlag) {
370      write_hit_record( ctx );
371   }
372   if (ctx->Select.NameStackDepth >= MAX_NAME_STACK_DEPTH) {
373      _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
374   }
375   else
376      ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
377}
378
379
380/**
381 * Pop a name into the name stack.
382 *
383 * Verifies we are in selection mode and that the name stack is not empty.
384 * Flushes vertices. If there is a hit flag writes it (via write_hit_record()),
385 * and removes top-most name in the name stack.
386 *
387 * sa __GLcontextRec::Select.
388 */
389void GLAPIENTRY
390_mesa_PopName( void )
391{
392   GET_CURRENT_CONTEXT(ctx);
393   ASSERT_OUTSIDE_BEGIN_END(ctx);
394
395   if (ctx->RenderMode != GL_SELECT) {
396      return;
397   }
398
399   FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
400   if (ctx->Select.HitFlag) {
401      write_hit_record( ctx );
402   }
403   if (ctx->Select.NameStackDepth == 0) {
404      _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
405   }
406   else
407      ctx->Select.NameStackDepth--;
408}
409
410/*@}*/
411
412
413/**********************************************************************/
414/** \name Render Mode */
415/*@{*/
416
417/**
418 * Set rasterization mode.
419 *
420 * \param mode rasterization mode.
421 *
422 * \note this function can't be put in a display list.
423 *
424 * \sa glRenderMode().
425 *
426 * Flushes the vertices and do the necessary cleanup according to the previous
427 * rasterization mode, such as writing the hit record or resent the select
428 * buffer index when exiting the select mode. Updates
429 * __GLcontextRec::RenderMode and notifies the driver via the
430 * dd_function_table::RenderMode callback.
431 */
432GLint GLAPIENTRY
433_mesa_RenderMode( GLenum mode )
434{
435   GET_CURRENT_CONTEXT(ctx);
436   GLint result;
437   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
438
439   if (MESA_VERBOSE & VERBOSE_API)
440      _mesa_debug(ctx, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode));
441
442   FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
443
444   switch (ctx->RenderMode) {
445      case GL_RENDER:
446	 result = 0;
447	 break;
448      case GL_SELECT:
449	 if (ctx->Select.HitFlag) {
450	    write_hit_record( ctx );
451	 }
452	 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
453	    /* overflow */
454#ifdef DEBUG
455            _mesa_warning(ctx, "Feedback buffer overflow");
456#endif
457	    result = -1;
458	 }
459	 else {
460	    result = ctx->Select.Hits;
461	 }
462	 ctx->Select.BufferCount = 0;
463	 ctx->Select.Hits = 0;
464	 ctx->Select.NameStackDepth = 0;
465	 break;
466#if _HAVE_FULL_GL
467      case GL_FEEDBACK:
468	 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
469	    /* overflow */
470	    result = -1;
471	 }
472	 else {
473	    result = ctx->Feedback.Count;
474	 }
475	 ctx->Feedback.Count = 0;
476	 break;
477#endif
478      default:
479	 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
480	 return 0;
481   }
482
483   switch (mode) {
484      case GL_RENDER:
485         break;
486      case GL_SELECT:
487	 if (ctx->Select.BufferSize==0) {
488	    /* haven't called glSelectBuffer yet */
489	    _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
490	 }
491	 break;
492#if _HAVE_FULL_GL
493      case GL_FEEDBACK:
494	 if (ctx->Feedback.BufferSize==0) {
495	    /* haven't called glFeedbackBuffer yet */
496	    _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
497	 }
498	 break;
499#endif
500      default:
501	 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
502	 return 0;
503   }
504
505   ctx->RenderMode = mode;
506   if (ctx->Driver.RenderMode)
507      ctx->Driver.RenderMode( ctx, mode );
508
509   return result;
510}
511
512/*@}*/
513
514
515/**********************************************************************/
516/** \name Initialization */
517/*@{*/
518
519/**
520 * Initialize context feedback data.
521 */
522void _mesa_init_feedback( GLcontext * ctx )
523{
524   /* Feedback */
525   ctx->Feedback.Type = GL_2D;   /* TODO: verify */
526   ctx->Feedback.Buffer = NULL;
527   ctx->Feedback.BufferSize = 0;
528   ctx->Feedback.Count = 0;
529
530   /* Selection/picking */
531   ctx->Select.Buffer = NULL;
532   ctx->Select.BufferSize = 0;
533   ctx->Select.BufferCount = 0;
534   ctx->Select.Hits = 0;
535   ctx->Select.NameStackDepth = 0;
536
537   /* Miscellaneous */
538   ctx->RenderMode = GL_RENDER;
539}
540
541/*@}*/
542