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