fix GL_BACK color material bug
[mesa.git] / src / mesa / main / macros.h
1 /* $Id: macros.h,v 1.8 1999/11/22 18:57:56 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
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 * A collection of useful macros.
30 */
31
32
33 #ifndef MACROS_H
34 #define MACROS_H
35
36
37 #include "glheader.h"
38
39
40 #ifdef DEBUG
41 # define ASSERT(X) assert(X)
42 #else
43 # define ASSERT(X)
44 #endif
45
46
47 #if defined(__GNUC__)
48 #define INLINE __inline__
49 #elif defined(__MSC__)
50 #define INLINE __inline
51 #else
52 #define INLINE
53 #endif
54
55
56 /* Limits: */
57 #define MAX_GLUSHORT 0xffff
58 #define MAX_GLUINT 0xffffffff
59
60
61 /* Some compilers don't like some of Mesa's const usage */
62 #ifdef NO_CONST
63 # define CONST
64 #else
65 # define CONST const
66 #endif
67
68
69 /* Pi */
70 #ifndef M_PI
71 #define M_PI (3.1415926)
72 #endif
73
74
75 /* Degrees to radians conversion: */
76 #define DEG2RAD (M_PI/180.0)
77
78
79 #ifndef NULL
80 #define NULL 0
81 #endif
82
83
84
85 /*
86 * Bitmask helpers
87 */
88 #define SET_BITS(WORD, BITS) (WORD) |= (BITS)
89 #define CLEAR_BITS(WORD, BITS) (WORD) &= ~(BITS)
90 #define TEST_BITS(WORD, BITS) ((WORD) & (BITS))
91
92
93 /* Stepping a GLfloat pointer by a byte stride
94 */
95 #define STRIDE_F(p, i) (p = (GLfloat *)((GLubyte *)p + i))
96 #define STRIDE_UI(p, i) (p = (GLuint *)((GLubyte *)p + i))
97 #define STRIDE_T(p, t, i) (p = (t *)((GLubyte *)p + i))
98
99
100 #define ZERO_2V( DST ) (DST)[0] = (DST)[1] = 0
101 #define ZERO_3V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = 0
102 #define ZERO_4V( DST ) (DST)[0] = (DST)[1] = (DST)[2] = (DST)[3] = 0
103
104
105 /* Copy short vectors: */
106 #define COPY_2V( DST, SRC ) \
107 do { \
108 (DST)[0] = (SRC)[0]; \
109 (DST)[1] = (SRC)[1]; \
110 } while (0)
111
112
113 #define COPY_3V( DST, SRC ) \
114 do { \
115 (DST)[0] = (SRC)[0]; \
116 (DST)[1] = (SRC)[1]; \
117 (DST)[2] = (SRC)[2]; \
118 } while (0)
119
120 #define COPY_4V( DST, SRC ) \
121 do { \
122 (DST)[0] = (SRC)[0]; \
123 (DST)[1] = (SRC)[1]; \
124 (DST)[2] = (SRC)[2]; \
125 (DST)[3] = (SRC)[3]; \
126 } while (0)
127
128
129 #define COPY_2FV( DST, SRC ) \
130 do { \
131 const GLfloat *_tmp = (SRC); \
132 (DST)[0] = _tmp[0]; \
133 (DST)[1] = _tmp[1]; \
134 } while (0)
135
136
137 #define COPY_3FV( DST, SRC ) \
138 do { \
139 const GLfloat *_tmp = (SRC); \
140 (DST)[0] = _tmp[0]; \
141 (DST)[1] = _tmp[1]; \
142 (DST)[2] = _tmp[2]; \
143 } while (0)
144
145 #define COPY_4FV( DST, SRC ) \
146 do { \
147 const GLfloat *_tmp = (SRC); \
148 (DST)[0] = _tmp[0]; \
149 (DST)[1] = _tmp[1]; \
150 (DST)[2] = _tmp[2]; \
151 (DST)[3] = _tmp[3]; \
152 } while (0)
153
154
155
156 #define COPY_SZ_4V(DST, SZ, SRC) \
157 do { \
158 switch (SZ) { \
159 case 4: (DST)[3] = (SRC)[3]; \
160 case 3: (DST)[2] = (SRC)[2]; \
161 case 2: (DST)[1] = (SRC)[1]; \
162 case 1: (DST)[0] = (SRC)[0]; \
163 } \
164 } while(0)
165
166 #define SUB_4V( DST, SRCA, SRCB ) \
167 do { \
168 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
169 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
170 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
171 (DST)[3] = (SRCA)[3] - (SRCB)[3]; \
172 } while (0)
173
174 #define ADD_4V( DST, SRCA, SRCB ) \
175 do { \
176 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
177 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
178 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
179 (DST)[3] = (SRCA)[3] + (SRCB)[3]; \
180 } while (0)
181
182 #define SCALE_4V( DST, SRCA, SRCB ) \
183 do { \
184 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
185 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
186 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
187 (DST)[3] = (SRCA)[3] * (SRCB)[3]; \
188 } while (0)
189
190 #define ACC_4V( DST, SRC ) \
191 do { \
192 (DST)[0] += (SRC)[0]; \
193 (DST)[1] += (SRC)[1]; \
194 (DST)[2] += (SRC)[2]; \
195 (DST)[3] += (SRC)[3]; \
196 } while (0)
197
198 #define ACC_SCALE_4V( DST, SRCA, SRCB ) \
199 do { \
200 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
201 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
202 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
203 (DST)[3] += (SRCA)[3] * (SRCB)[3]; \
204 } while (0)
205
206 #define ACC_SCALE_SCALAR_4V( DST, S, SRCB ) \
207 do { \
208 (DST)[0] += S * (SRCB)[0]; \
209 (DST)[1] += S * (SRCB)[1]; \
210 (DST)[2] += S * (SRCB)[2]; \
211 (DST)[3] += S * (SRCB)[3]; \
212 } while (0)
213
214 #define SCALE_SCALAR_4V( DST, S, SRCB ) \
215 do { \
216 (DST)[0] = S * (SRCB)[0]; \
217 (DST)[1] = S * (SRCB)[1]; \
218 (DST)[2] = S * (SRCB)[2]; \
219 (DST)[3] = S * (SRCB)[3]; \
220 } while (0)
221
222
223 #define SELF_SCALE_SCALAR_4V( DST, S ) \
224 do { \
225 (DST)[0] *= S; \
226 (DST)[1] *= S; \
227 (DST)[2] *= S; \
228 (DST)[3] *= S; \
229 } while (0)
230
231
232 /*
233 * Similarly for 3-vectors.
234 */
235 #define SUB_3V( DST, SRCA, SRCB ) \
236 do { \
237 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
238 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
239 (DST)[2] = (SRCA)[2] - (SRCB)[2]; \
240 } while (0)
241
242 #define ADD_3V( DST, SRCA, SRCB ) \
243 do { \
244 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
245 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
246 (DST)[2] = (SRCA)[2] + (SRCB)[2]; \
247 } while (0)
248
249 #define SCALE_3V( DST, SRCA, SRCB ) \
250 do { \
251 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
252 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
253 (DST)[2] = (SRCA)[2] * (SRCB)[2]; \
254 } while (0)
255
256 #define ACC_3V( DST, SRC ) \
257 do { \
258 (DST)[0] += (SRC)[0]; \
259 (DST)[1] += (SRC)[1]; \
260 (DST)[2] += (SRC)[2]; \
261 } while (0)
262
263 #define ACC_SCALE_3V( DST, SRCA, SRCB ) \
264 do { \
265 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
266 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
267 (DST)[2] += (SRCA)[2] * (SRCB)[2]; \
268 } while (0)
269
270 #define SCALE_SCALAR_3V( DST, S, SRCB ) \
271 do { \
272 (DST)[0] = S * (SRCB)[0]; \
273 (DST)[1] = S * (SRCB)[1]; \
274 (DST)[2] = S * (SRCB)[2]; \
275 } while (0)
276
277 #define ACC_SCALE_SCALAR_3V( DST, S, SRCB ) \
278 do { \
279 (DST)[0] += S * (SRCB)[0]; \
280 (DST)[1] += S * (SRCB)[1]; \
281 (DST)[2] += S * (SRCB)[2]; \
282 } while (0)
283
284 #define SELF_SCALE_SCALAR_3V( DST, S ) \
285 do { \
286 (DST)[0] *= S; \
287 (DST)[1] *= S; \
288 (DST)[2] *= S; \
289 } while (0)
290
291 #define ACC_SCALAR_3V( DST, S ) \
292 do { \
293 (DST)[0] += S; \
294 (DST)[1] += S; \
295 (DST)[2] += S; \
296 } while (0)
297
298 /* And also for 2-vectors
299 */
300 #define SUB_2V( DST, SRCA, SRCB ) \
301 do { \
302 (DST)[0] = (SRCA)[0] - (SRCB)[0]; \
303 (DST)[1] = (SRCA)[1] - (SRCB)[1]; \
304 } while (0)
305
306 #define ADD_2V( DST, SRCA, SRCB ) \
307 do { \
308 (DST)[0] = (SRCA)[0] + (SRCB)[0]; \
309 (DST)[1] = (SRCA)[1] + (SRCB)[1]; \
310 } while (0)
311
312 #define SCALE_2V( DST, SRCA, SRCB ) \
313 do { \
314 (DST)[0] = (SRCA)[0] * (SRCB)[0]; \
315 (DST)[1] = (SRCA)[1] * (SRCB)[1]; \
316 } while (0)
317
318 #define ACC_2V( DST, SRC ) \
319 do { \
320 (DST)[0] += (SRC)[0]; \
321 (DST)[1] += (SRC)[1]; \
322 } while (0)
323
324 #define ACC_SCALE_2V( DST, SRCA, SRCB ) \
325 do { \
326 (DST)[0] += (SRCA)[0] * (SRCB)[0]; \
327 (DST)[1] += (SRCA)[1] * (SRCB)[1]; \
328 } while (0)
329
330 #define SCALE_SCALAR_2V( DST, S, SRCB ) \
331 do { \
332 (DST)[0] = S * (SRCB)[0]; \
333 (DST)[1] = S * (SRCB)[1]; \
334 } while (0)
335
336 #define ACC_SCALE_SCALAR_2V( DST, S, SRCB ) \
337 do { \
338 (DST)[0] += S * (SRCB)[0]; \
339 (DST)[1] += S * (SRCB)[1]; \
340 } while (0)
341
342 #define SELF_SCALE_SCALAR_2V( DST, S ) \
343 do { \
344 (DST)[0] *= S; \
345 (DST)[1] *= S; \
346 } while (0)
347
348 #define ACC_SCALAR_2V( DST, S ) \
349 do { \
350 (DST)[0] += S; \
351 (DST)[1] += S; \
352 } while (0)
353
354
355
356 /*
357 * Copy a vector of 4 GLubytes from SRC to DST.
358 */
359 #define COPY_4UBV(DST, SRC) \
360 do { \
361 if (sizeof(GLuint)==4*sizeof(GLubyte)) { \
362 *((GLuint*)(DST)) = *((GLuint*)(SRC)); \
363 } \
364 else { \
365 (DST)[0] = (SRC)[0]; \
366 (DST)[1] = (SRC)[1]; \
367 (DST)[2] = (SRC)[2]; \
368 (DST)[3] = (SRC)[3]; \
369 } \
370 } while (0)
371
372
373 /* Assign scalers to short vectors: */
374 #define ASSIGN_2V( V, V0, V1 ) \
375 do { V[0] = V0; V[1] = V1; } while(0)
376
377 #define ASSIGN_3V( V, V0, V1, V2 ) \
378 do { V[0] = V0; V[1] = V1; V[2] = V2; } while(0)
379
380 #define ASSIGN_4V( V, V0, V1, V2, V3 ) \
381 do { \
382 V[0] = V0; \
383 V[1] = V1; \
384 V[2] = V2; \
385 V[3] = V3; \
386 } while(0)
387
388
389
390
391 /* Absolute value (for Int, Float, Double): */
392 #define ABSI(X) ((X) < 0 ? -(X) : (X))
393 #define ABSF(X) ((X) < 0.0F ? -(X) : (X))
394 #define ABSD(X) ((X) < 0.0 ? -(X) : (X))
395
396
397
398 /* Round a floating-point value to the nearest integer: */
399 #define ROUNDF(X) ( (X)<0.0F ? ((GLint) ((X)-0.5F)) : ((GLint) ((X)+0.5F)) )
400
401
402 /* Compute ceiling of integer quotient of A divided by B: */
403 #define CEILING( A, B ) ( (A) % (B) == 0 ? (A)/(B) : (A)/(B)+1 )
404
405
406 /* Clamp X to [MIN,MAX]: */
407 #define CLAMP( X, MIN, MAX ) ( (X)<(MIN) ? (MIN) : ((X)>(MAX) ? (MAX) : (X)) )
408
409 /* Assign X to CLAMP(X, MIN, MAX) */
410 #define CLAMP_SELF(x, mn, mx) \
411 ( (x)<(mn) ? ((x) = (mn)) : ((x)>(mx) ? ((x)=(mx)) : (x)) )
412
413
414
415 /* Min of two values: */
416 #define MIN2( A, B ) ( (A)<(B) ? (A) : (B) )
417
418
419 /* MAX of two values: */
420 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
421
422 /* Dot product of two 2-element vectors */
423 #define DOT2( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] )
424
425 /* Dot product of two 3-element vectors */
426 #define DOT3( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + (a)[2]*(b)[2] )
427
428
429 /* Dot product of two 4-element vectors */
430 #define DOT4( a, b ) ( (a)[0]*(b)[0] + (a)[1]*(b)[1] + \
431 (a)[2]*(b)[2] + (a)[3]*(b)[3] )
432
433 #define DOT4V(v,a,b,c,d) (v[0]*a + v[1]*b + v[2]*c + v[3]*d)
434
435
436 #define CROSS3(n, u, v) \
437 do { \
438 (n)[0] = (u)[1]*(v)[2] - (u)[2]*(v)[1]; \
439 (n)[1] = (u)[2]*(v)[0] - (u)[0]*(v)[2]; \
440 (n)[2] = (u)[0]*(v)[1] - (u)[1]*(v)[0]; \
441 } while (0)
442
443
444 /*
445 * Integer / float conversion for colors, normals, etc.
446 */
447
448 #define BYTE_TO_UBYTE(b) (b < 0 ? 0 : (GLubyte) b)
449 #define SHORT_TO_UBYTE(s) (s < 0 ? 0 : (GLubyte) (s >> 7))
450 #define USHORT_TO_UBYTE(s) (GLubyte) (s >> 8)
451 #define INT_TO_UBYTE(i) (i < 0 ? 0 : (GLubyte) (i >> 23))
452 #define UINT_TO_UBYTE(i) (GLubyte) (i >> 24)
453
454 /* Convert GLubyte in [0,255] to GLfloat in [0.0,1.0] */
455 #define UBYTE_TO_FLOAT(B) ((GLfloat) (B) * (1.0F / 255.0F))
456
457 /* Convert GLfloat in [0.0,1.0] to GLubyte in [0,255] */
458 #define FLOAT_TO_UBYTE(X) ((GLubyte) (GLint) (((X)) * 255.0F))
459
460
461 /* Convert GLbyte in [-128,127] to GLfloat in [-1.0,1.0] */
462 #define BYTE_TO_FLOAT(B) ((2.0F * (B) + 1.0F) * (1.0F/255.0F))
463
464 /* Convert GLfloat in [-1.0,1.0] to GLbyte in [-128,127] */
465 #define FLOAT_TO_BYTE(X) ( (((GLint) (255.0F * (X))) - 1) / 2 )
466
467
468 /* Convert GLushort in [0,65536] to GLfloat in [0.0,1.0] */
469 #define USHORT_TO_FLOAT(S) ((GLfloat) (S) * (1.0F / 65535.0F))
470
471 /* Convert GLfloat in [0.0,1.0] to GLushort in [0,65536] */
472 #define FLOAT_TO_USHORT(X) ((GLushort) (GLint) ((X) * 65535.0F))
473
474
475 /* Convert GLshort in [-32768,32767] to GLfloat in [-1.0,1.0] */
476 #define SHORT_TO_FLOAT(S) ((2.0F * (S) + 1.0F) * (1.0F/65535.0F))
477
478 /* Convert GLfloat in [0.0,1.0] to GLshort in [-32768,32767] */
479 #define FLOAT_TO_SHORT(X) ( (((GLint) (65535.0F * (X))) - 1) / 2 )
480
481
482 /* Convert GLuint in [0,4294967295] to GLfloat in [0.0,1.0] */
483 #define UINT_TO_FLOAT(U) ((GLfloat) (U) * (1.0F / 4294967295.0F))
484
485 /* Convert GLfloat in [0.0,1.0] to GLuint in [0,4294967295] */
486 #define FLOAT_TO_UINT(X) ((GLuint) ((X) * 4294967295.0))
487
488
489 /* Convert GLint in [-2147483648,2147483647] to GLfloat in [-1.0,1.0] */
490 #define INT_TO_FLOAT(I) ((2.0F * (I) + 1.0F) * (1.0F/4294967294.0F))
491
492 /* Convert GLfloat in [-1.0,1.0] to GLint in [-2147483648,2147483647] */
493 /* causes overflow:
494 #define FLOAT_TO_INT(X) ( (((GLint) (4294967294.0F * (X))) - 1) / 2 )
495 */
496 /* a close approximation: */
497 #define FLOAT_TO_INT(X) ( (GLint) (2147483647.0 * (X)) )
498
499
500 #endif /*MACROS_H*/