1 /* $Id: m_translate.c,v 1.9 2001/09/18 23:06:14 kschultz Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
28 * New (3.1) transformation code written by Keith Whitwell.
33 #include "mtypes.h" /* GLchan hack */
38 #include "m_translate.h"
42 typedef void (*trans_1f_func
)(GLfloat
*to
,
48 typedef void (*trans_1ui_func
)(GLuint
*to
,
54 typedef void (*trans_1ub_func
)(GLubyte
*to
,
60 typedef void (*trans_4ub_func
)(GLubyte (*to
)[4],
66 typedef void (*trans_4us_func
)(GLushort (*to
)[4],
72 typedef void (*trans_4f_func
)(GLfloat (*to
)[4],
78 typedef void (*trans_3f_func
)(GLfloat (*to
)[3],
87 #define TYPE_IDX(t) ((t) & 0xf)
88 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
91 /* This macro is used on other systems, so undefine it for this module */
95 static trans_1f_func _math_trans_1f_tab
[MAX_TYPES
];
96 static trans_1ui_func _math_trans_1ui_tab
[MAX_TYPES
];
97 static trans_1ub_func _math_trans_1ub_tab
[MAX_TYPES
];
98 static trans_3f_func _math_trans_3f_tab
[MAX_TYPES
];
99 static trans_4ub_func _math_trans_4ub_tab
[5][MAX_TYPES
];
100 static trans_4us_func _math_trans_4us_tab
[5][MAX_TYPES
];
101 static trans_4f_func _math_trans_4f_tab
[5][MAX_TYPES
];
104 #define PTR_ELT(ptr, elt) (((SRC *)ptr)[elt])
107 #define TAB(x) _math_trans##x##_tab
108 #define ARGS GLuint start, GLuint n
109 #define SRC_START start
111 #define STRIDE stride
112 #define NEXT_F f += stride
122 #define SRC_IDX TYPE_IDX(GL_BYTE)
123 #define TRX_3F(f,n) BYTE_TO_FLOAT( PTR_ELT(f,n) )
124 #define TRX_4F(f,n) BYTE_TO_FLOAT( PTR_ELT(f,n) )
125 #define TRX_UB(ub, f,n) ub = BYTE_TO_UBYTE( PTR_ELT(f,n) )
126 #define TRX_US(ch, f,n) ch = BYTE_TO_USHORT( PTR_ELT(f,n) )
127 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
131 #define INIT init_trans_4_GLbyte_raw
132 #define DEST_4F trans_4_GLbyte_4f_raw
133 #define DEST_4UB trans_4_GLbyte_4ub_raw
134 #define DEST_4US trans_4_GLbyte_4us_raw
135 #include "m_trans_tmp.h"
138 #define INIT init_trans_3_GLbyte_raw
139 #define DEST_4F trans_3_GLbyte_4f_raw
140 #define DEST_4UB trans_3_GLbyte_4ub_raw
141 #define DEST_4US trans_3_GLbyte_4us_raw
142 #define DEST_3F trans_3_GLbyte_3f_raw
143 #include "m_trans_tmp.h"
146 #define INIT init_trans_2_GLbyte_raw
147 #define DEST_4F trans_2_GLbyte_4f_raw
148 #include "m_trans_tmp.h"
151 #define INIT init_trans_1_GLbyte_raw
152 #define DEST_4F trans_1_GLbyte_4f_raw
153 #define DEST_1UB trans_1_GLbyte_1ub_raw
154 #define DEST_1UI trans_1_GLbyte_1ui_raw
155 #include "m_trans_tmp.h"
169 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_BYTE)
170 #define TRX_3F(f,n) UBYTE_TO_FLOAT(PTR_ELT(f,n))
171 #define TRX_4F(f,n) UBYTE_TO_FLOAT(PTR_ELT(f,n))
172 #define TRX_UB(ub, f,n) ub = PTR_ELT(f,n)
173 #define TRX_US(us, f,n) us = UBYTE_TO_USHORT(PTR_ELT(f,n))
174 #define TRX_UI(f,n) (GLuint)PTR_ELT(f,n)
176 /* 4ub->4ub handled in special case below.
179 #define INIT init_trans_4_GLubyte_raw
180 #define DEST_4F trans_4_GLubyte_4f_raw
181 #define DEST_4US trans_4_GLubyte_4us_raw
182 #include "m_trans_tmp.h"
186 #define INIT init_trans_3_GLubyte_raw
187 #define DEST_4UB trans_3_GLubyte_4ub_raw
188 #define DEST_4US trans_3_GLubyte_4us_raw
189 #define DEST_3F trans_3_GLubyte_3f_raw
190 #define DEST_4F trans_3_GLubyte_4f_raw
191 #include "m_trans_tmp.h"
195 #define INIT init_trans_1_GLubyte_raw
196 #define DEST_1UI trans_1_GLubyte_1ui_raw
197 #define DEST_1UB trans_1_GLubyte_1ub_raw
198 #include "m_trans_tmp.h"
212 #define SRC_IDX TYPE_IDX(GL_SHORT)
213 #define TRX_3F(f,n) SHORT_TO_FLOAT( PTR_ELT(f,n) )
214 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
215 #define TRX_UB(ub, f,n) ub = SHORT_TO_UBYTE(PTR_ELT(f,n))
216 #define TRX_US(us, f,n) us = SHORT_TO_USHORT(PTR_ELT(f,n))
217 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
221 #define INIT init_trans_4_GLshort_raw
222 #define DEST_4F trans_4_GLshort_4f_raw
223 #define DEST_4UB trans_4_GLshort_4ub_raw
224 #define DEST_4US trans_4_GLshort_4us_raw
225 #include "m_trans_tmp.h"
228 #define INIT init_trans_3_GLshort_raw
229 #define DEST_4F trans_3_GLshort_4f_raw
230 #define DEST_4UB trans_3_GLshort_4ub_raw
231 #define DEST_4US trans_3_GLshort_4us_raw
232 #define DEST_3F trans_3_GLshort_3f_raw
233 #include "m_trans_tmp.h"
236 #define INIT init_trans_2_GLshort_raw
237 #define DEST_4F trans_2_GLshort_4f_raw
238 #include "m_trans_tmp.h"
241 #define INIT init_trans_1_GLshort_raw
242 #define DEST_4F trans_1_GLshort_4f_raw
243 #define DEST_1UB trans_1_GLshort_1ub_raw
244 #define DEST_1UI trans_1_GLshort_1ui_raw
245 #include "m_trans_tmp.h"
260 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_SHORT)
261 #define TRX_3F(f,n) USHORT_TO_FLOAT( PTR_ELT(f,n) )
262 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
263 #define TRX_UB(ub,f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 8)
264 #define TRX_US(us,f,n) us = (GLushort) (PTR_ELT(f,n) >> 8)
265 #define TRX_UI(f,n) (GLuint) PTR_ELT(f,n)
269 #define INIT init_trans_4_GLushort_raw
270 #define DEST_4F trans_4_GLushort_4f_raw
271 #define DEST_4UB trans_4_GLushort_4ub_raw
272 #define DEST_4US trans_4_GLushort_4us_raw
273 #include "m_trans_tmp.h"
276 #define INIT init_trans_3_GLushort_raw
277 #define DEST_4F trans_3_GLushort_4f_raw
278 #define DEST_4UB trans_3_GLushort_4ub_raw
279 #define DEST_4US trans_3_GLushort_4us_raw
280 #define DEST_3F trans_3_GLushort_3f_raw
281 #include "m_trans_tmp.h"
284 #define INIT init_trans_2_GLushort_raw
285 #define DEST_4F trans_2_GLushort_4f_raw
286 #include "m_trans_tmp.h"
289 #define INIT init_trans_1_GLushort_raw
290 #define DEST_4F trans_1_GLushort_4f_raw
291 #define DEST_1UB trans_1_GLushort_1ub_raw
292 #define DEST_1UI trans_1_GLushort_1ui_raw
293 #include "m_trans_tmp.h"
307 #define SRC_IDX TYPE_IDX(GL_INT)
308 #define TRX_3F(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
309 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
310 #define TRX_UB(ub, f,n) ub = INT_TO_UBYTE(PTR_ELT(f,n))
311 #define TRX_US(us, f,n) us = INT_TO_USHORT(PTR_ELT(f,n))
312 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
316 #define INIT init_trans_4_GLint_raw
317 #define DEST_4F trans_4_GLint_4f_raw
318 #define DEST_4UB trans_4_GLint_4ub_raw
319 #define DEST_4US trans_4_GLint_4us_raw
320 #include "m_trans_tmp.h"
323 #define INIT init_trans_3_GLint_raw
324 #define DEST_4F trans_3_GLint_4f_raw
325 #define DEST_4UB trans_3_GLint_4ub_raw
326 #define DEST_4US trans_3_GLint_4us_raw
327 #define DEST_3F trans_3_GLint_3f_raw
328 #include "m_trans_tmp.h"
331 #define INIT init_trans_2_GLint_raw
332 #define DEST_4F trans_2_GLint_4f_raw
333 #include "m_trans_tmp.h"
336 #define INIT init_trans_1_GLint_raw
337 #define DEST_4F trans_1_GLint_4f_raw
338 #define DEST_1UB trans_1_GLint_1ub_raw
339 #define DEST_1UI trans_1_GLint_1ui_raw
340 #include "m_trans_tmp.h"
355 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_INT)
356 #define TRX_3F(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
357 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
358 #define TRX_UB(ub, f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 24)
359 #define TRX_US(us, f,n) us = (GLshort) (PTR_ELT(f,n) >> 16)
360 #define TRX_UI(f,n) PTR_ELT(f,n)
364 #define INIT init_trans_4_GLuint_raw
365 #define DEST_4F trans_4_GLuint_4f_raw
366 #define DEST_4UB trans_4_GLuint_4ub_raw
367 #define DEST_4US trans_4_GLuint_4us_raw
368 #include "m_trans_tmp.h"
371 #define INIT init_trans_3_GLuint_raw
372 #define DEST_4F trans_3_GLuint_4f_raw
373 #define DEST_4UB trans_3_GLuint_4ub_raw
374 #define DEST_4US trans_3_GLuint_4us_raw
375 #define DEST_3F trans_3_GLuint_3f_raw
376 #include "m_trans_tmp.h"
379 #define INIT init_trans_2_GLuint_raw
380 #define DEST_4F trans_2_GLuint_4f_raw
381 #include "m_trans_tmp.h"
384 #define INIT init_trans_1_GLuint_raw
385 #define DEST_4F trans_1_GLuint_4f_raw
386 #define DEST_1UB trans_1_GLuint_1ub_raw
387 #define DEST_1UI trans_1_GLuint_1ui_raw
388 #include "m_trans_tmp.h"
402 #define SRC_IDX TYPE_IDX(GL_DOUBLE)
403 #define TRX_3F(f,n) (GLfloat) PTR_ELT(f,n)
404 #define TRX_4F(f,n) (GLfloat) PTR_ELT(f,n)
405 #define TRX_UB(ub,f,n) UNCLAMPED_FLOAT_TO_UBYTE(ub, PTR_ELT(f,n))
406 #define TRX_US(us,f,n) UNCLAMPED_FLOAT_TO_USHORT(us, PTR_ELT(f,n))
407 #define TRX_UI(f,n) (GLuint) (GLint) PTR_ELT(f,n)
408 #define TRX_1F(f,n) (GLfloat) PTR_ELT(f,n)
412 #define INIT init_trans_4_GLdouble_raw
413 #define DEST_4F trans_4_GLdouble_4f_raw
414 #define DEST_4UB trans_4_GLdouble_4ub_raw
415 #define DEST_4US trans_4_GLdouble_4us_raw
416 #include "m_trans_tmp.h"
419 #define INIT init_trans_3_GLdouble_raw
420 #define DEST_4F trans_3_GLdouble_4f_raw
421 #define DEST_4UB trans_3_GLdouble_4ub_raw
422 #define DEST_4US trans_3_GLdouble_4us_raw
423 #define DEST_3F trans_3_GLdouble_3f_raw
424 #include "m_trans_tmp.h"
427 #define INIT init_trans_2_GLdouble_raw
428 #define DEST_4F trans_2_GLdouble_4f_raw
429 #include "m_trans_tmp.h"
432 #define INIT init_trans_1_GLdouble_raw
433 #define DEST_4F trans_1_GLdouble_4f_raw
434 #define DEST_1UB trans_1_GLdouble_1ub_raw
435 #define DEST_1UI trans_1_GLdouble_1ui_raw
436 #define DEST_1F trans_1_GLdouble_1f_raw
437 #include "m_trans_tmp.h"
445 #define SRC_IDX TYPE_IDX(GL_FLOAT)
447 #define INIT init_trans_4_GLfloat_raw
448 #define DEST_4UB trans_4_GLfloat_4ub_raw
449 #define DEST_4US trans_4_GLfloat_4us_raw
450 #define DEST_4F trans_4_GLfloat_4f_raw
451 #include "m_trans_tmp.h"
454 #define INIT init_trans_3_GLfloat_raw
455 #define DEST_4F trans_3_GLfloat_4f_raw
456 #define DEST_4UB trans_3_GLfloat_4ub_raw
457 #define DEST_4US trans_3_GLfloat_4us_raw
458 #define DEST_3F trans_3_GLfloat_3f_raw
459 #include "m_trans_tmp.h"
462 #define INIT init_trans_2_GLfloat_raw
463 #define DEST_4F trans_2_GLfloat_4f_raw
464 #include "m_trans_tmp.h"
467 #define INIT init_trans_1_GLfloat_raw
468 #define DEST_4F trans_1_GLfloat_4f_raw
469 #define DEST_1UB trans_1_GLfloat_1ub_raw
470 #define DEST_1UI trans_1_GLfloat_1ui_raw
471 #define DEST_1F trans_1_GLfloat_1f_raw
473 #include "m_trans_tmp.h"
484 static void trans_4_GLubyte_4ub_raw(GLubyte (*t
)[4],
489 const GLubyte
*f
= (GLubyte
*) Ptr
+ SRC_START
* stride
;
492 if (((((long) f
| (long) stride
)) & 3L) == 0L) {
495 for (i
= DST_START
; i
< n
; i
++, f
+= stride
) {
496 COPY_4UBV( t
[i
], f
);
499 for (i
= DST_START
; i
< n
; i
++, f
+= stride
) {
509 static void init_translate_raw(void)
511 MEMSET( TAB(_1ui
), 0, sizeof(TAB(_1ui
)) );
512 MEMSET( TAB(_1ub
), 0, sizeof(TAB(_1ub
)) );
513 MEMSET( TAB(_3f
), 0, sizeof(TAB(_3f
)) );
514 MEMSET( TAB(_4ub
), 0, sizeof(TAB(_4ub
)) );
515 MEMSET( TAB(_4us
), 0, sizeof(TAB(_4us
)) );
516 MEMSET( TAB(_4f
), 0, sizeof(TAB(_4f
)) );
518 init_trans_4_GLbyte_raw();
519 init_trans_3_GLbyte_raw();
520 init_trans_2_GLbyte_raw();
521 init_trans_1_GLbyte_raw();
522 init_trans_1_GLubyte_raw();
523 init_trans_3_GLubyte_raw();
524 init_trans_4_GLubyte_raw();
525 init_trans_4_GLshort_raw();
526 init_trans_3_GLshort_raw();
527 init_trans_2_GLshort_raw();
528 init_trans_1_GLshort_raw();
529 init_trans_4_GLushort_raw();
530 init_trans_3_GLushort_raw();
531 init_trans_2_GLushort_raw();
532 init_trans_1_GLushort_raw();
533 init_trans_4_GLint_raw();
534 init_trans_3_GLint_raw();
535 init_trans_2_GLint_raw();
536 init_trans_1_GLint_raw();
537 init_trans_4_GLuint_raw();
538 init_trans_3_GLuint_raw();
539 init_trans_2_GLuint_raw();
540 init_trans_1_GLuint_raw();
541 init_trans_4_GLdouble_raw();
542 init_trans_3_GLdouble_raw();
543 init_trans_2_GLdouble_raw();
544 init_trans_1_GLdouble_raw();
545 init_trans_4_GLfloat_raw();
546 init_trans_3_GLfloat_raw();
547 init_trans_2_GLfloat_raw();
548 init_trans_1_GLfloat_raw();
550 TAB(_4ub
)[4][TYPE_IDX(GL_UNSIGNED_BYTE
)] = trans_4_GLubyte_4ub_raw
;
567 void _math_init_translate( void )
569 init_translate_raw();
574 void _math_trans_1f(GLfloat
*to
,
581 _math_trans_1f_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
584 void _math_trans_1ui(GLuint
*to
,
591 _math_trans_1ui_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
594 void _math_trans_1ub(GLubyte
*to
,
601 _math_trans_1ub_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
605 void _math_trans_4ub(GLubyte (*to
)[4],
613 _math_trans_4ub_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
616 void _math_trans_4chan( GLchan (*to
)[4],
624 #if CHAN_TYPE == GL_UNSIGNED_BYTE
625 _math_trans_4ub( to
, ptr
, stride
, type
, size
, start
, n
);
626 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
627 _math_trans_4us( to
, ptr
, stride
, type
, size
, start
, n
);
628 #elif CHAN_TYPE == GL_FLOAT
629 _math_trans_4f( to
, ptr
, stride
, type
, size
, start
, n
);
633 void _math_trans_4us(GLushort (*to
)[4],
641 _math_trans_4us_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
644 void _math_trans_4f(GLfloat (*to
)[4],
652 _math_trans_4f_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
655 void _math_trans_3f(GLfloat (*to
)[3],
662 _math_trans_3f_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);