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