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