4251aae01a308a14ee15c164ddd1ec4f1370ba80
[mesa.git] / src / mesa / main / feedback.c
1 /* $Id: feedback.c,v 1.1 1999/08/19 00:55:41 jtg Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.1
6 *
7 * Copyright (C) 1999 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
29
30
31 #ifdef PC_HEADER
32 #include "all.h"
33 #else
34 #include <assert.h>
35 #include <stdio.h>
36 #include "context.h"
37 #include "enums.h"
38 #include "feedback.h"
39 #include "macros.h"
40 #include "types.h"
41 #ifdef XFree86Server
42 #include "GL/xf86glx.h"
43 #endif
44 #endif
45
46
47
48 #define FB_3D 0x01
49 #define FB_4D 0x02
50 #define FB_INDEX 0x04
51 #define FB_COLOR 0x08
52 #define FB_TEXTURE 0X10
53
54
55
56 void
57 gl_FeedbackBuffer( GLcontext *ctx, GLsizei size, GLenum type, GLfloat *buffer )
58 {
59 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx, "glFeedbackBuffer" );
60
61 if (ctx->RenderMode==GL_FEEDBACK) {
62 gl_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
63 return;
64 }
65
66 if (size<0) {
67 gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
68 return;
69 }
70 if (!buffer) {
71 gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
72 ctx->Feedback.BufferSize = 0;
73 return;
74 }
75
76 switch (type) {
77 case GL_2D:
78 ctx->Feedback.Mask = 0;
79 ctx->Feedback.Type = type;
80 break;
81 case GL_3D:
82 ctx->Feedback.Mask = FB_3D;
83 ctx->Feedback.Type = type;
84 break;
85 case GL_3D_COLOR:
86 ctx->Feedback.Mask = FB_3D
87 | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX);
88 ctx->Feedback.Type = type;
89 break;
90 case GL_3D_COLOR_TEXTURE:
91 ctx->Feedback.Mask = FB_3D
92 | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
93 | FB_TEXTURE;
94 ctx->Feedback.Type = type;
95 break;
96 case GL_4D_COLOR_TEXTURE:
97 ctx->Feedback.Mask = FB_3D | FB_4D
98 | (ctx->Visual->RGBAflag ? FB_COLOR : FB_INDEX)
99 | FB_TEXTURE;
100 ctx->Feedback.Type = type;
101 break;
102 default:
103 ctx->Feedback.Mask = 0;
104 gl_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
105 }
106
107 ctx->Feedback.BufferSize = size;
108 ctx->Feedback.Buffer = buffer;
109 ctx->Feedback.Count = 0;
110 }
111
112
113
114 void gl_PassThrough( GLcontext *ctx, GLfloat token )
115 {
116 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPassThrough");
117
118 if (ctx->RenderMode==GL_FEEDBACK) {
119 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN );
120 FEEDBACK_TOKEN( ctx, token );
121 }
122 }
123
124
125
126 /*
127 * Put a vertex into the feedback buffer.
128 */
129 void gl_feedback_vertex( GLcontext *ctx,
130 GLfloat x, GLfloat y, GLfloat z, GLfloat w,
131 const GLfloat color[4], GLfloat index,
132 const GLfloat texcoord[4] )
133 {
134 FEEDBACK_TOKEN( ctx, x );
135 FEEDBACK_TOKEN( ctx, y );
136 if (ctx->Feedback.Mask & FB_3D) {
137 FEEDBACK_TOKEN( ctx, z );
138 }
139 if (ctx->Feedback.Mask & FB_4D) {
140 FEEDBACK_TOKEN( ctx, w );
141 }
142 if (ctx->Feedback.Mask & FB_INDEX) {
143 FEEDBACK_TOKEN( ctx, index );
144 }
145 if (ctx->Feedback.Mask & FB_COLOR) {
146 FEEDBACK_TOKEN( ctx, color[0] );
147 FEEDBACK_TOKEN( ctx, color[1] );
148 FEEDBACK_TOKEN( ctx, color[2] );
149 FEEDBACK_TOKEN( ctx, color[3] );
150 }
151 if (ctx->Feedback.Mask & FB_TEXTURE) {
152 FEEDBACK_TOKEN( ctx, texcoord[0] );
153 FEEDBACK_TOKEN( ctx, texcoord[1] );
154 FEEDBACK_TOKEN( ctx, texcoord[2] );
155 FEEDBACK_TOKEN( ctx, texcoord[3] );
156 }
157 }
158
159
160
161 /**********************************************************************/
162 /* Selection */
163 /**********************************************************************/
164
165
166 /*
167 * NOTE: this function can't be put in a display list.
168 */
169 void gl_SelectBuffer( GLcontext *ctx, GLsizei size, GLuint *buffer )
170 {
171 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glSelectBuffer");
172 if (ctx->RenderMode==GL_SELECT) {
173 gl_error( ctx, GL_INVALID_OPERATION, "glSelectBuffer" );
174 }
175 ctx->Select.Buffer = buffer;
176 ctx->Select.BufferSize = size;
177 ctx->Select.BufferCount = 0;
178
179 ctx->Select.HitFlag = GL_FALSE;
180 ctx->Select.HitMinZ = 1.0;
181 ctx->Select.HitMaxZ = 0.0;
182 }
183
184
185 #define WRITE_RECORD( CTX, V ) \
186 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
187 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
188 } \
189 CTX->Select.BufferCount++;
190
191
192
193 void gl_update_hitflag( GLcontext *ctx, GLfloat z )
194 {
195 ctx->Select.HitFlag = GL_TRUE;
196 if (z < ctx->Select.HitMinZ) {
197 ctx->Select.HitMinZ = z;
198 }
199 if (z > ctx->Select.HitMaxZ) {
200 ctx->Select.HitMaxZ = z;
201 }
202 }
203
204
205
206 static void write_hit_record( GLcontext *ctx )
207 {
208 GLuint i;
209 GLuint zmin, zmax, zscale = (~0u);
210
211 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
212 /* 2^32-1 and round to nearest unsigned integer. */
213
214 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
215 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
216 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
217
218 WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
219 WRITE_RECORD( ctx, zmin );
220 WRITE_RECORD( ctx, zmax );
221 for (i=0;i<ctx->Select.NameStackDepth;i++) {
222 WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
223 }
224
225 ctx->Select.Hits++;
226 ctx->Select.HitFlag = GL_FALSE;
227 ctx->Select.HitMinZ = 1.0;
228 ctx->Select.HitMaxZ = -1.0;
229 }
230
231
232
233 void gl_InitNames( GLcontext *ctx )
234 {
235 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glInitNames");
236 /* Record the hit before the HitFlag is wiped out again. */
237 if (ctx->RenderMode==GL_SELECT) {
238 if (ctx->Select.HitFlag) {
239 write_hit_record( ctx );
240 }
241 }
242 ctx->Select.NameStackDepth = 0;
243 ctx->Select.HitFlag = GL_FALSE;
244 ctx->Select.HitMinZ = 1.0;
245 ctx->Select.HitMaxZ = 0.0;
246 }
247
248
249
250 void gl_LoadName( GLcontext *ctx, GLuint name )
251 {
252 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLoadName");
253 if (ctx->RenderMode!=GL_SELECT) {
254 return;
255 }
256 if (ctx->Select.NameStackDepth==0) {
257 gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
258 return;
259 }
260 if (ctx->Select.HitFlag) {
261 write_hit_record( ctx );
262 }
263 if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
264 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
265 }
266 else {
267 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
268 }
269 }
270
271
272 void gl_PushName( GLcontext *ctx, GLuint name )
273 {
274 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushName");
275 if (ctx->RenderMode!=GL_SELECT) {
276 return;
277 }
278 if (ctx->Select.HitFlag) {
279 write_hit_record( ctx );
280 }
281 if (ctx->Select.NameStackDepth<MAX_NAME_STACK_DEPTH) {
282 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
283 }
284 else {
285 gl_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
286 }
287 }
288
289
290
291 void gl_PopName( GLcontext *ctx )
292 {
293 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopName");
294 if (ctx->RenderMode!=GL_SELECT) {
295 return;
296 }
297 if (ctx->Select.HitFlag) {
298 write_hit_record( ctx );
299 }
300 if (ctx->Select.NameStackDepth>0) {
301 ctx->Select.NameStackDepth--;
302 }
303 else {
304 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
305 }
306 }
307
308
309
310 /**********************************************************************/
311 /* Render Mode */
312 /**********************************************************************/
313
314
315
316 /*
317 * NOTE: this function can't be put in a display list.
318 */
319 GLint gl_RenderMode( GLcontext *ctx, GLenum mode )
320 {
321 GLint result;
322
323 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glRenderMode", 0);
324
325 if (MESA_VERBOSE & VERBOSE_API)
326 fprintf(stderr, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode));
327
328 ctx->TriangleCaps &= ~(DD_FEEDBACK|DD_SELECT);
329
330 switch (ctx->RenderMode) {
331 case GL_RENDER:
332 result = 0;
333 break;
334 case GL_SELECT:
335 if (ctx->Select.HitFlag) {
336 write_hit_record( ctx );
337 }
338 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
339 /* overflow */
340 #ifdef DEBUG
341 gl_warning(ctx, "Feedback buffer overflow");
342 #endif
343 result = -1;
344 }
345 else {
346 result = ctx->Select.Hits;
347 }
348 ctx->Select.BufferCount = 0;
349 ctx->Select.Hits = 0;
350 ctx->Select.NameStackDepth = 0;
351 break;
352 case GL_FEEDBACK:
353 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
354 /* overflow */
355 result = -1;
356 }
357 else {
358 result = ctx->Feedback.Count;
359 }
360 ctx->Feedback.Count = 0;
361 break;
362 default:
363 gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
364 return 0;
365 }
366
367 switch (mode) {
368 case GL_RENDER:
369 break;
370 case GL_SELECT:
371 ctx->TriangleCaps |= DD_SELECT;
372 if (ctx->Select.BufferSize==0) {
373 /* haven't called glSelectBuffer yet */
374 gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
375 }
376 break;
377 case GL_FEEDBACK:
378 ctx->TriangleCaps |= DD_FEEDBACK;
379 if (ctx->Feedback.BufferSize==0) {
380 /* haven't called glFeedbackBuffer yet */
381 gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
382 }
383 break;
384 default:
385 gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
386 return 0;
387 }
388
389
390 ctx->RenderMode = mode;
391 ctx->NewState |= NEW_ALL;
392
393 return result;
394 }
395