Consistent copyright info (version number, date) across all files.
[mesa.git] / src / mesa / main / feedback.c
1 /* $Id: feedback.c,v 1.23 2001/03/12 00:48:37 gareth Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #ifdef PC_HEADER
29 #include "all.h"
30 #else
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 "mmath.h"
38 #include "mtypes.h"
39 #endif
40
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
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
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
155 /**********************************************************************/
156 /* Selection */
157 /**********************************************************************/
158
159
160 /*
161 * NOTE: this function can't be put in a display list.
162 */
163 void
164 _mesa_SelectBuffer( GLsizei size, GLuint *buffer )
165 {
166 GET_CURRENT_CONTEXT(ctx);
167 ASSERT_OUTSIDE_BEGIN_END(ctx);
168
169 if (ctx->RenderMode==GL_SELECT) {
170 _mesa_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
171 return; /* KW: added return */
172 }
173
174 FLUSH_VERTICES(ctx, _NEW_RENDERMODE); /* why bother? */
175 ctx->Select.Buffer = buffer;
176 ctx->Select.BufferSize = size;
177 ctx->Select.BufferCount = 0;
178 ctx->Select.HitFlag = GL_FALSE;
179 ctx->Select.HitMinZ = 1.0;
180 ctx->Select.HitMaxZ = 0.0;
181 }
182
183
184 #define WRITE_RECORD( CTX, V ) \
185 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
186 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
187 } \
188 CTX->Select.BufferCount++;
189
190
191
192 void _mesa_update_hitflag( GLcontext *ctx, GLfloat z )
193 {
194 ctx->Select.HitFlag = GL_TRUE;
195 if (z < ctx->Select.HitMinZ) {
196 ctx->Select.HitMinZ = z;
197 }
198 if (z > ctx->Select.HitMaxZ) {
199 ctx->Select.HitMaxZ = z;
200 }
201 }
202
203
204 static void write_hit_record( GLcontext *ctx )
205 {
206 GLuint i;
207 GLuint zmin, zmax, zscale = (~0u);
208
209 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
210 /* 2^32-1 and round to nearest unsigned integer. */
211
212 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
213 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
214 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
215
216 WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
217 WRITE_RECORD( ctx, zmin );
218 WRITE_RECORD( ctx, zmax );
219 for (i = 0; i < ctx->Select.NameStackDepth; i++) {
220 WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
221 }
222
223 ctx->Select.Hits++;
224 ctx->Select.HitFlag = GL_FALSE;
225 ctx->Select.HitMinZ = 1.0;
226 ctx->Select.HitMaxZ = -1.0;
227 }
228
229
230
231 void
232 _mesa_InitNames( void )
233 {
234 GET_CURRENT_CONTEXT(ctx);
235 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
236
237 /* Record the hit before the HitFlag is wiped out again. */
238 if (ctx->RenderMode == GL_SELECT) {
239 if (ctx->Select.HitFlag) {
240 write_hit_record( ctx );
241 }
242 }
243 ctx->Select.NameStackDepth = 0;
244 ctx->Select.HitFlag = GL_FALSE;
245 ctx->Select.HitMinZ = 1.0;
246 ctx->Select.HitMaxZ = 0.0;
247 ctx->NewState |= _NEW_RENDERMODE;
248 }
249
250
251
252 void
253 _mesa_LoadName( GLuint name )
254 {
255 GET_CURRENT_CONTEXT(ctx);
256 ASSERT_OUTSIDE_BEGIN_END(ctx);
257
258 if (ctx->RenderMode != GL_SELECT) {
259 return;
260 }
261 if (ctx->Select.NameStackDepth == 0) {
262 _mesa_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
263 return;
264 }
265
266 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
267
268 if (ctx->Select.HitFlag) {
269 write_hit_record( ctx );
270 }
271 if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
272 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
273 }
274 else {
275 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
276 }
277 }
278
279
280 void
281 _mesa_PushName( GLuint name )
282 {
283 GET_CURRENT_CONTEXT(ctx);
284 ASSERT_OUTSIDE_BEGIN_END(ctx);
285
286 if (ctx->RenderMode != GL_SELECT) {
287 return;
288 }
289
290 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
291 if (ctx->Select.HitFlag) {
292 write_hit_record( ctx );
293 }
294 if (ctx->Select.NameStackDepth >= MAX_NAME_STACK_DEPTH) {
295 _mesa_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
296 }
297 else
298 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
299 }
300
301
302
303 void
304 _mesa_PopName( void )
305 {
306 GET_CURRENT_CONTEXT(ctx);
307 ASSERT_OUTSIDE_BEGIN_END(ctx);
308
309 if (ctx->RenderMode != GL_SELECT) {
310 return;
311 }
312
313 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
314 if (ctx->Select.HitFlag) {
315 write_hit_record( ctx );
316 }
317 if (ctx->Select.NameStackDepth == 0) {
318 _mesa_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
319 }
320 else
321 ctx->Select.NameStackDepth--;
322 }
323
324
325
326 /**********************************************************************/
327 /* Render Mode */
328 /**********************************************************************/
329
330
331
332 /*
333 * NOTE: this function can't be put in a display list.
334 */
335 GLint
336 _mesa_RenderMode( GLenum mode )
337 {
338 GET_CURRENT_CONTEXT(ctx);
339 GLint result;
340 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
341
342 if (MESA_VERBOSE & VERBOSE_API)
343 fprintf(stderr, "glRenderMode %s\n", _mesa_lookup_enum_by_nr(mode));
344
345 FLUSH_VERTICES(ctx, _NEW_RENDERMODE);
346 ctx->_TriangleCaps &= ~(DD_FEEDBACK|DD_SELECT);
347
348 switch (ctx->RenderMode) {
349 case GL_RENDER:
350 result = 0;
351 break;
352 case GL_SELECT:
353 if (ctx->Select.HitFlag) {
354 write_hit_record( ctx );
355 }
356 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
357 /* overflow */
358 #ifdef DEBUG
359 _mesa_warning(ctx, "Feedback buffer overflow");
360 #endif
361 result = -1;
362 }
363 else {
364 result = ctx->Select.Hits;
365 }
366 ctx->Select.BufferCount = 0;
367 ctx->Select.Hits = 0;
368 ctx->Select.NameStackDepth = 0;
369 break;
370 case GL_FEEDBACK:
371 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
372 /* overflow */
373 result = -1;
374 }
375 else {
376 result = ctx->Feedback.Count;
377 }
378 ctx->Feedback.Count = 0;
379 break;
380 default:
381 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
382 return 0;
383 }
384
385 switch (mode) {
386 case GL_RENDER:
387 break;
388 case GL_SELECT:
389 ctx->_TriangleCaps |= DD_SELECT;
390 if (ctx->Select.BufferSize==0) {
391 /* haven't called glSelectBuffer yet */
392 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
393 }
394 break;
395 case GL_FEEDBACK:
396 ctx->_TriangleCaps |= DD_FEEDBACK;
397 if (ctx->Feedback.BufferSize==0) {
398 /* haven't called glFeedbackBuffer yet */
399 _mesa_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
400 }
401 break;
402 default:
403 _mesa_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
404 return 0;
405 }
406
407 ctx->RenderMode = mode;
408 if (ctx->Driver.RenderMode)
409 ctx->Driver.RenderMode( ctx, mode );
410
411 return result;
412 }