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