- Changes for new software rasterizer modules
[mesa.git] / src / mesa / main / feedback.c
1 /* $Id: feedback.c,v 1.16 2000/11/05 18:40:57 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 *
7 * Copyright (C) 1999-2000 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 "types.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_AND_FLUSH( ctx, "glFeedbackBuffer" );
56
57 if (ctx->RenderMode==GL_FEEDBACK) {
58 gl_error( ctx, GL_INVALID_OPERATION, "glFeedbackBuffer" );
59 return;
60 }
61
62 if (size<0) {
63 gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(size<0)" );
64 return;
65 }
66 if (!buffer) {
67 gl_error( ctx, GL_INVALID_VALUE, "glFeedbackBuffer(buffer==NULL)" );
68 ctx->Feedback.BufferSize = 0;
69 return;
70 }
71
72 switch (type) {
73 case GL_2D:
74 ctx->Feedback.Mask = 0;
75 ctx->Feedback.Type = type;
76 break;
77 case GL_3D:
78 ctx->Feedback.Mask = FB_3D;
79 ctx->Feedback.Type = type;
80 break;
81 case GL_3D_COLOR:
82 ctx->Feedback.Mask = FB_3D
83 | (ctx->Visual.RGBAflag ? FB_COLOR : FB_INDEX);
84 ctx->Feedback.Type = type;
85 break;
86 case GL_3D_COLOR_TEXTURE:
87 ctx->Feedback.Mask = FB_3D
88 | (ctx->Visual.RGBAflag ? FB_COLOR : FB_INDEX)
89 | FB_TEXTURE;
90 ctx->Feedback.Type = type;
91 break;
92 case GL_4D_COLOR_TEXTURE:
93 ctx->Feedback.Mask = FB_3D | FB_4D
94 | (ctx->Visual.RGBAflag ? FB_COLOR : FB_INDEX)
95 | FB_TEXTURE;
96 ctx->Feedback.Type = type;
97 break;
98 default:
99 ctx->Feedback.Mask = 0;
100 gl_error( ctx, GL_INVALID_ENUM, "glFeedbackBuffer" );
101 }
102
103 ctx->Feedback.BufferSize = size;
104 ctx->Feedback.Buffer = buffer;
105 ctx->Feedback.Count = 0;
106 ctx->NewState |= _NEW_FEEDBACK_SELECT;
107 }
108
109
110
111 void
112 _mesa_PassThrough( GLfloat token )
113 {
114 GET_CURRENT_CONTEXT(ctx);
115 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPassThrough");
116
117 if (ctx->RenderMode==GL_FEEDBACK) {
118 FEEDBACK_TOKEN( ctx, (GLfloat) (GLint) GL_PASS_THROUGH_TOKEN );
119 FEEDBACK_TOKEN( ctx, token );
120 }
121 }
122
123
124
125 /*
126 * Put a vertex into the feedback buffer.
127 */
128 void gl_feedback_vertex( GLcontext *ctx,
129 const GLfloat win[4],
130 const GLfloat color[4],
131 GLuint index,
132 const GLfloat texcoord[4] )
133 {
134 FEEDBACK_TOKEN( ctx, win[0] );
135 FEEDBACK_TOKEN( ctx, win[1] );
136 if (ctx->Feedback.Mask & FB_3D) {
137 FEEDBACK_TOKEN( ctx, win[2] );
138 }
139 if (ctx->Feedback.Mask & FB_4D) {
140 FEEDBACK_TOKEN( ctx, win[3] );
141 }
142 if (ctx->Feedback.Mask & FB_INDEX) {
143 FEEDBACK_TOKEN( ctx, (GLfloat) 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 /* Selection */
161 /**********************************************************************/
162
163
164 /*
165 * NOTE: this function can't be put in a display list.
166 */
167 void
168 _mesa_SelectBuffer( GLsizei size, GLuint *buffer )
169 {
170 GET_CURRENT_CONTEXT(ctx);
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 ctx->NewState |= _NEW_FEEDBACK_SELECT;
184 }
185
186
187 #define WRITE_RECORD( CTX, V ) \
188 if (CTX->Select.BufferCount < CTX->Select.BufferSize) { \
189 CTX->Select.Buffer[CTX->Select.BufferCount] = (V); \
190 } \
191 CTX->Select.BufferCount++;
192
193
194
195 void gl_update_hitflag( GLcontext *ctx, GLfloat z )
196 {
197 ctx->Select.HitFlag = GL_TRUE;
198 if (z < ctx->Select.HitMinZ) {
199 ctx->Select.HitMinZ = z;
200 }
201 if (z > ctx->Select.HitMaxZ) {
202 ctx->Select.HitMaxZ = z;
203 }
204 }
205
206
207 static void write_hit_record( GLcontext *ctx )
208 {
209 GLuint i;
210 GLuint zmin, zmax, zscale = (~0u);
211
212 /* HitMinZ and HitMaxZ are in [0,1]. Multiply these values by */
213 /* 2^32-1 and round to nearest unsigned integer. */
214
215 assert( ctx != NULL ); /* this line magically fixes a SunOS 5.x/gcc bug */
216 zmin = (GLuint) ((GLfloat) zscale * ctx->Select.HitMinZ);
217 zmax = (GLuint) ((GLfloat) zscale * ctx->Select.HitMaxZ);
218
219 WRITE_RECORD( ctx, ctx->Select.NameStackDepth );
220 WRITE_RECORD( ctx, zmin );
221 WRITE_RECORD( ctx, zmax );
222 for (i = 0; i < ctx->Select.NameStackDepth; i++) {
223 WRITE_RECORD( ctx, ctx->Select.NameStack[i] );
224 }
225
226 ctx->Select.Hits++;
227 ctx->Select.HitFlag = GL_FALSE;
228 ctx->Select.HitMinZ = 1.0;
229 ctx->Select.HitMaxZ = -1.0;
230 }
231
232
233
234 void
235 _mesa_InitNames( void )
236 {
237 GET_CURRENT_CONTEXT(ctx);
238 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glInitNames");
239 /* Record the hit before the HitFlag is wiped out again. */
240 if (ctx->RenderMode == GL_SELECT) {
241 if (ctx->Select.HitFlag) {
242 write_hit_record( ctx );
243 }
244 }
245 ctx->Select.NameStackDepth = 0;
246 ctx->Select.HitFlag = GL_FALSE;
247 ctx->Select.HitMinZ = 1.0;
248 ctx->Select.HitMaxZ = 0.0;
249 ctx->NewState |= _NEW_FEEDBACK_SELECT;
250 }
251
252
253
254 void
255 _mesa_LoadName( GLuint name )
256 {
257 GET_CURRENT_CONTEXT(ctx);
258 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLoadName");
259 if (ctx->RenderMode != GL_SELECT) {
260 return;
261 }
262 if (ctx->Select.NameStackDepth == 0) {
263 gl_error( ctx, GL_INVALID_OPERATION, "glLoadName" );
264 return;
265 }
266 if (ctx->Select.HitFlag) {
267 write_hit_record( ctx );
268 }
269 if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
270 ctx->Select.NameStack[ctx->Select.NameStackDepth-1] = name;
271 }
272 else {
273 ctx->Select.NameStack[MAX_NAME_STACK_DEPTH-1] = name;
274 }
275 ctx->NewState |= _NEW_FEEDBACK_SELECT;
276 }
277
278
279 void
280 _mesa_PushName( GLuint name )
281 {
282 GET_CURRENT_CONTEXT(ctx);
283 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPushName");
284 if (ctx->RenderMode != GL_SELECT) {
285 return;
286 }
287 if (ctx->Select.HitFlag) {
288 write_hit_record( ctx );
289 }
290 if (ctx->Select.NameStackDepth < MAX_NAME_STACK_DEPTH) {
291 ctx->Select.NameStack[ctx->Select.NameStackDepth++] = name;
292 }
293 else {
294 gl_error( ctx, GL_STACK_OVERFLOW, "glPushName" );
295 }
296 ctx->NewState |= _NEW_FEEDBACK_SELECT;
297 }
298
299
300
301 void
302 _mesa_PopName( void )
303 {
304 GET_CURRENT_CONTEXT(ctx);
305 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glPopName");
306 if (ctx->RenderMode != GL_SELECT) {
307 return;
308 }
309 if (ctx->Select.HitFlag) {
310 write_hit_record( ctx );
311 }
312 if (ctx->Select.NameStackDepth > 0) {
313 ctx->Select.NameStackDepth--;
314 }
315 else {
316 gl_error( ctx, GL_STACK_UNDERFLOW, "glPopName" );
317 }
318 ctx->NewState |= _NEW_FEEDBACK_SELECT;
319 }
320
321
322
323 /**********************************************************************/
324 /* Render Mode */
325 /**********************************************************************/
326
327
328
329 /*
330 * NOTE: this function can't be put in a display list.
331 */
332 GLint
333 _mesa_RenderMode( GLenum mode )
334 {
335 GET_CURRENT_CONTEXT(ctx);
336 GLint result;
337
338 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH_WITH_RETVAL(ctx, "glRenderMode", 0);
339
340 if (MESA_VERBOSE & VERBOSE_API)
341 fprintf(stderr, "glRenderMode %s\n", gl_lookup_enum_by_nr(mode));
342
343 ctx->_TriangleCaps &= ~(DD_FEEDBACK|DD_SELECT);
344
345 switch (ctx->RenderMode) {
346 case GL_RENDER:
347 result = 0;
348 break;
349 case GL_SELECT:
350 if (ctx->Select.HitFlag) {
351 write_hit_record( ctx );
352 }
353 if (ctx->Select.BufferCount > ctx->Select.BufferSize) {
354 /* overflow */
355 #ifdef DEBUG
356 _mesa_warning(ctx, "Feedback buffer overflow");
357 #endif
358 result = -1;
359 }
360 else {
361 result = ctx->Select.Hits;
362 }
363 ctx->Select.BufferCount = 0;
364 ctx->Select.Hits = 0;
365 ctx->Select.NameStackDepth = 0;
366 ctx->NewState |= _NEW_FEEDBACK_SELECT;
367 break;
368 case GL_FEEDBACK:
369 if (ctx->Feedback.Count > ctx->Feedback.BufferSize) {
370 /* overflow */
371 result = -1;
372 }
373 else {
374 result = ctx->Feedback.Count;
375 }
376 ctx->Feedback.Count = 0;
377 ctx->NewState |= _NEW_FEEDBACK_SELECT;
378 break;
379 default:
380 gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
381 return 0;
382 }
383
384 switch (mode) {
385 case GL_RENDER:
386 break;
387 case GL_SELECT:
388 ctx->_TriangleCaps |= DD_SELECT;
389 if (ctx->Select.BufferSize==0) {
390 /* haven't called glSelectBuffer yet */
391 gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
392 }
393 break;
394 case GL_FEEDBACK:
395 ctx->_TriangleCaps |= DD_FEEDBACK;
396 if (ctx->Feedback.BufferSize==0) {
397 /* haven't called glFeedbackBuffer yet */
398 gl_error( ctx, GL_INVALID_OPERATION, "glRenderMode" );
399 }
400 break;
401 default:
402 gl_error( ctx, GL_INVALID_ENUM, "glRenderMode" );
403 return 0;
404 }
405
406 ctx->RenderMode = mode;
407 ctx->NewState |= _NEW_RENDERMODE;
408
409 return result;
410 }
411