2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \brief Translate vectors of numbers between various types.
27 * \author Keith Whitwell.
32 #include "mtypes.h" /* GLchan hack */
35 #include "m_translate.h"
39 typedef void (*trans_1f_func
)(GLfloat
*to
,
45 typedef void (*trans_1ui_func
)(GLuint
*to
,
51 typedef void (*trans_1ub_func
)(GLubyte
*to
,
57 typedef void (*trans_4ub_func
)(GLubyte (*to
)[4],
63 typedef void (*trans_4us_func
)(GLushort (*to
)[4],
69 typedef void (*trans_4f_func
)(GLfloat (*to
)[4],
75 typedef void (*trans_3fn_func
)(GLfloat (*to
)[3],
84 #define TYPE_IDX(t) ((t) & 0xf)
85 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
88 /* This macro is used on other systems, so undefine it for this module */
92 static trans_1f_func _math_trans_1f_tab
[MAX_TYPES
];
93 static trans_1ui_func _math_trans_1ui_tab
[MAX_TYPES
];
94 static trans_1ub_func _math_trans_1ub_tab
[MAX_TYPES
];
95 static trans_3fn_func _math_trans_3fn_tab
[MAX_TYPES
];
96 static trans_4ub_func _math_trans_4ub_tab
[5][MAX_TYPES
];
97 static trans_4us_func _math_trans_4us_tab
[5][MAX_TYPES
];
98 static trans_4f_func _math_trans_4f_tab
[5][MAX_TYPES
];
99 static trans_4f_func _math_trans_4fn_tab
[5][MAX_TYPES
];
102 #define PTR_ELT(ptr, elt) (((SRC *)ptr)[elt])
105 #define TAB(x) _math_trans##x##_tab
106 #define ARGS GLuint start, GLuint n
107 #define SRC_START start
109 #define STRIDE stride
110 #define NEXT_F f += stride
118 * Translate from GL_BYTE.
121 #define SRC_IDX TYPE_IDX(GL_BYTE)
122 #define TRX_3FN(f,n) BYTE_TO_FLOAT( PTR_ELT(f,n) )
124 #define TRX_4F(f,n) BYTE_TO_FLOAT( PTR_ELT(f,n) )
126 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
128 #define TRX_4FN(f,n) BYTE_TO_FLOAT( PTR_ELT(f,n) )
129 #define TRX_UB(ub, f,n) ub = BYTE_TO_UBYTE( PTR_ELT(f,n) )
130 #define TRX_US(ch, f,n) ch = BYTE_TO_USHORT( PTR_ELT(f,n) )
131 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
135 #define INIT init_trans_4_GLbyte_raw
136 #define DEST_4F trans_4_GLbyte_4f_raw
137 #define DEST_4FN trans_4_GLbyte_4fn_raw
138 #define DEST_4UB trans_4_GLbyte_4ub_raw
139 #define DEST_4US trans_4_GLbyte_4us_raw
140 #include "m_trans_tmp.h"
143 #define INIT init_trans_3_GLbyte_raw
144 #define DEST_4F trans_3_GLbyte_4f_raw
145 #define DEST_4FN trans_3_GLbyte_4fn_raw
146 #define DEST_4UB trans_3_GLbyte_4ub_raw
147 #define DEST_4US trans_3_GLbyte_4us_raw
148 #define DEST_3FN trans_3_GLbyte_3fn_raw
149 #include "m_trans_tmp.h"
152 #define INIT init_trans_2_GLbyte_raw
153 #define DEST_4F trans_2_GLbyte_4f_raw
154 #define DEST_4FN trans_2_GLbyte_4fn_raw
155 #include "m_trans_tmp.h"
158 #define INIT init_trans_1_GLbyte_raw
159 #define DEST_4F trans_1_GLbyte_4f_raw
160 #define DEST_4FN trans_1_GLbyte_4fn_raw
161 #define DEST_1UB trans_1_GLbyte_1ub_raw
162 #define DEST_1UI trans_1_GLbyte_1ui_raw
163 #include "m_trans_tmp.h"
176 * Translate from GL_UNSIGNED_BYTE.
179 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_BYTE)
180 #define TRX_3FN(f,n) UBYTE_TO_FLOAT(PTR_ELT(f,n))
181 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
182 #define TRX_4FN(f,n) UBYTE_TO_FLOAT(PTR_ELT(f,n))
183 #define TRX_UB(ub, f,n) ub = PTR_ELT(f,n)
184 #define TRX_US(us, f,n) us = UBYTE_TO_USHORT(PTR_ELT(f,n))
185 #define TRX_UI(f,n) (GLuint)PTR_ELT(f,n)
187 /* 4ub->4ub handled in special case below.
190 #define INIT init_trans_4_GLubyte_raw
191 #define DEST_4F trans_4_GLubyte_4f_raw
192 #define DEST_4FN trans_4_GLubyte_4fn_raw
193 #define DEST_4US trans_4_GLubyte_4us_raw
194 #include "m_trans_tmp.h"
198 #define INIT init_trans_3_GLubyte_raw
199 #define DEST_4UB trans_3_GLubyte_4ub_raw
200 #define DEST_4US trans_3_GLubyte_4us_raw
201 #define DEST_3FN trans_3_GLubyte_3fn_raw
202 #define DEST_4F trans_3_GLubyte_4f_raw
203 #define DEST_4FN trans_3_GLubyte_4fn_raw
204 #include "m_trans_tmp.h"
208 #define INIT init_trans_1_GLubyte_raw
209 #define DEST_1UI trans_1_GLubyte_1ui_raw
210 #define DEST_1UB trans_1_GLubyte_1ub_raw
211 #include "m_trans_tmp.h"
226 #define SRC_IDX TYPE_IDX(GL_SHORT)
227 #define TRX_3FN(f,n) SHORT_TO_FLOAT( PTR_ELT(f,n) )
228 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
229 #define TRX_4FN(f,n) SHORT_TO_FLOAT( PTR_ELT(f,n) )
230 #define TRX_UB(ub, f,n) ub = SHORT_TO_UBYTE(PTR_ELT(f,n))
231 #define TRX_US(us, f,n) us = SHORT_TO_USHORT(PTR_ELT(f,n))
232 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
236 #define INIT init_trans_4_GLshort_raw
237 #define DEST_4F trans_4_GLshort_4f_raw
238 #define DEST_4FN trans_4_GLshort_4fn_raw
239 #define DEST_4UB trans_4_GLshort_4ub_raw
240 #define DEST_4US trans_4_GLshort_4us_raw
241 #include "m_trans_tmp.h"
244 #define INIT init_trans_3_GLshort_raw
245 #define DEST_4F trans_3_GLshort_4f_raw
246 #define DEST_4FN trans_3_GLshort_4fn_raw
247 #define DEST_4UB trans_3_GLshort_4ub_raw
248 #define DEST_4US trans_3_GLshort_4us_raw
249 #define DEST_3FN trans_3_GLshort_3fn_raw
250 #include "m_trans_tmp.h"
253 #define INIT init_trans_2_GLshort_raw
254 #define DEST_4F trans_2_GLshort_4f_raw
255 #define DEST_4FN trans_2_GLshort_4fn_raw
256 #include "m_trans_tmp.h"
259 #define INIT init_trans_1_GLshort_raw
260 #define DEST_4F trans_1_GLshort_4f_raw
261 #define DEST_4FN trans_1_GLshort_4fn_raw
262 #define DEST_1UB trans_1_GLshort_1ub_raw
263 #define DEST_1UI trans_1_GLshort_1ui_raw
264 #include "m_trans_tmp.h"
280 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_SHORT)
281 #define TRX_3FN(f,n) USHORT_TO_FLOAT( PTR_ELT(f,n) )
282 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
283 #define TRX_4FN(f,n) USHORT_TO_FLOAT( PTR_ELT(f,n) )
284 #define TRX_UB(ub,f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 8)
285 #define TRX_US(us,f,n) us = PTR_ELT(f,n)
286 #define TRX_UI(f,n) (GLuint) PTR_ELT(f,n)
290 #define INIT init_trans_4_GLushort_raw
291 #define DEST_4F trans_4_GLushort_4f_raw
292 #define DEST_4FN trans_4_GLushort_4fn_raw
293 #define DEST_4UB trans_4_GLushort_4ub_raw
294 #define DEST_4US trans_4_GLushort_4us_raw
295 #include "m_trans_tmp.h"
298 #define INIT init_trans_3_GLushort_raw
299 #define DEST_4F trans_3_GLushort_4f_raw
300 #define DEST_4FN trans_3_GLushort_4fn_raw
301 #define DEST_4UB trans_3_GLushort_4ub_raw
302 #define DEST_4US trans_3_GLushort_4us_raw
303 #define DEST_3FN trans_3_GLushort_3fn_raw
304 #include "m_trans_tmp.h"
307 #define INIT init_trans_2_GLushort_raw
308 #define DEST_4F trans_2_GLushort_4f_raw
309 #define DEST_4FN trans_2_GLushort_4fn_raw
310 #include "m_trans_tmp.h"
313 #define INIT init_trans_1_GLushort_raw
314 #define DEST_4F trans_1_GLushort_4f_raw
315 #define DEST_4FN trans_1_GLushort_4fn_raw
316 #define DEST_1UB trans_1_GLushort_1ub_raw
317 #define DEST_1UI trans_1_GLushort_1ui_raw
318 #include "m_trans_tmp.h"
333 #define SRC_IDX TYPE_IDX(GL_INT)
334 #define TRX_3FN(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
335 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
336 #define TRX_4FN(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
337 #define TRX_UB(ub, f,n) ub = INT_TO_UBYTE(PTR_ELT(f,n))
338 #define TRX_US(us, f,n) us = INT_TO_USHORT(PTR_ELT(f,n))
339 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
343 #define INIT init_trans_4_GLint_raw
344 #define DEST_4F trans_4_GLint_4f_raw
345 #define DEST_4FN trans_4_GLint_4fn_raw
346 #define DEST_4UB trans_4_GLint_4ub_raw
347 #define DEST_4US trans_4_GLint_4us_raw
348 #include "m_trans_tmp.h"
351 #define INIT init_trans_3_GLint_raw
352 #define DEST_4F trans_3_GLint_4f_raw
353 #define DEST_4FN trans_3_GLint_4fn_raw
354 #define DEST_4UB trans_3_GLint_4ub_raw
355 #define DEST_4US trans_3_GLint_4us_raw
356 #define DEST_3FN trans_3_GLint_3fn_raw
357 #include "m_trans_tmp.h"
360 #define INIT init_trans_2_GLint_raw
361 #define DEST_4F trans_2_GLint_4f_raw
362 #define DEST_4FN trans_2_GLint_4fn_raw
363 #include "m_trans_tmp.h"
366 #define INIT init_trans_1_GLint_raw
367 #define DEST_4F trans_1_GLint_4f_raw
368 #define DEST_4FN trans_1_GLint_4fn_raw
369 #define DEST_1UB trans_1_GLint_1ub_raw
370 #define DEST_1UI trans_1_GLint_1ui_raw
371 #include "m_trans_tmp.h"
387 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_INT)
388 #define TRX_3FN(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
389 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
390 #define TRX_4FN(f,n) UINT_TO_FLOAT( PTR_ELT(f,n) )
391 #define TRX_UB(ub, f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 24)
392 #define TRX_US(us, f,n) us = (GLshort) (PTR_ELT(f,n) >> 16)
393 #define TRX_UI(f,n) PTR_ELT(f,n)
397 #define INIT init_trans_4_GLuint_raw
398 #define DEST_4F trans_4_GLuint_4f_raw
399 #define DEST_4FN trans_4_GLuint_4fn_raw
400 #define DEST_4UB trans_4_GLuint_4ub_raw
401 #define DEST_4US trans_4_GLuint_4us_raw
402 #include "m_trans_tmp.h"
405 #define INIT init_trans_3_GLuint_raw
406 #define DEST_4F trans_3_GLuint_4f_raw
407 #define DEST_4FN trans_3_GLuint_4fn_raw
408 #define DEST_4UB trans_3_GLuint_4ub_raw
409 #define DEST_4US trans_3_GLuint_4us_raw
410 #define DEST_3FN trans_3_GLuint_3fn_raw
411 #include "m_trans_tmp.h"
414 #define INIT init_trans_2_GLuint_raw
415 #define DEST_4F trans_2_GLuint_4f_raw
416 #define DEST_4FN trans_2_GLuint_4fn_raw
417 #include "m_trans_tmp.h"
420 #define INIT init_trans_1_GLuint_raw
421 #define DEST_4F trans_1_GLuint_4f_raw
422 #define DEST_4FN trans_1_GLuint_4fn_raw
423 #define DEST_1UB trans_1_GLuint_1ub_raw
424 #define DEST_1UI trans_1_GLuint_1ui_raw
425 #include "m_trans_tmp.h"
440 #define SRC_IDX TYPE_IDX(GL_DOUBLE)
441 #define TRX_3FN(f,n) (GLfloat) PTR_ELT(f,n)
442 #define TRX_4F(f,n) (GLfloat) PTR_ELT(f,n)
443 #define TRX_4FN(f,n) (GLfloat) PTR_ELT(f,n)
444 #define TRX_UB(ub,f,n) UNCLAMPED_FLOAT_TO_UBYTE(ub, PTR_ELT(f,n))
445 #define TRX_US(us,f,n) UNCLAMPED_FLOAT_TO_USHORT(us, PTR_ELT(f,n))
446 #define TRX_UI(f,n) (GLuint) (GLint) PTR_ELT(f,n)
447 #define TRX_1F(f,n) (GLfloat) PTR_ELT(f,n)
451 #define INIT init_trans_4_GLdouble_raw
452 #define DEST_4F trans_4_GLdouble_4f_raw
453 #define DEST_4FN trans_4_GLdouble_4fn_raw
454 #define DEST_4UB trans_4_GLdouble_4ub_raw
455 #define DEST_4US trans_4_GLdouble_4us_raw
456 #include "m_trans_tmp.h"
459 #define INIT init_trans_3_GLdouble_raw
460 #define DEST_4F trans_3_GLdouble_4f_raw
461 #define DEST_4FN trans_3_GLdouble_4fn_raw
462 #define DEST_4UB trans_3_GLdouble_4ub_raw
463 #define DEST_4US trans_3_GLdouble_4us_raw
464 #define DEST_3FN trans_3_GLdouble_3fn_raw
465 #include "m_trans_tmp.h"
468 #define INIT init_trans_2_GLdouble_raw
469 #define DEST_4F trans_2_GLdouble_4f_raw
470 #define DEST_4FN trans_2_GLdouble_4fn_raw
471 #include "m_trans_tmp.h"
474 #define INIT init_trans_1_GLdouble_raw
475 #define DEST_4F trans_1_GLdouble_4f_raw
476 #define DEST_4FN trans_1_GLdouble_4fn_raw
477 #define DEST_1UB trans_1_GLdouble_1ub_raw
478 #define DEST_1UI trans_1_GLdouble_1ui_raw
479 #define DEST_1F trans_1_GLdouble_1f_raw
480 #include "m_trans_tmp.h"
488 #define SRC_IDX TYPE_IDX(GL_FLOAT)
490 #define INIT init_trans_4_GLfloat_raw
491 #define DEST_4UB trans_4_GLfloat_4ub_raw
492 #define DEST_4US trans_4_GLfloat_4us_raw
493 #define DEST_4F trans_4_GLfloat_4f_raw
494 #define DEST_4FN trans_4_GLfloat_4fn_raw
495 #include "m_trans_tmp.h"
498 #define INIT init_trans_3_GLfloat_raw
499 #define DEST_4F trans_3_GLfloat_4f_raw
500 #define DEST_4FN trans_3_GLfloat_4fn_raw
501 #define DEST_4UB trans_3_GLfloat_4ub_raw
502 #define DEST_4US trans_3_GLfloat_4us_raw
503 #define DEST_3FN trans_3_GLfloat_3fn_raw
504 #include "m_trans_tmp.h"
507 #define INIT init_trans_2_GLfloat_raw
508 #define DEST_4F trans_2_GLfloat_4f_raw
509 #define DEST_4FN trans_2_GLfloat_4fn_raw
510 #include "m_trans_tmp.h"
513 #define INIT init_trans_1_GLfloat_raw
514 #define DEST_4F trans_1_GLfloat_4f_raw
515 #define DEST_4FN trans_1_GLfloat_4fn_raw
516 #define DEST_1UB trans_1_GLfloat_1ub_raw
517 #define DEST_1UI trans_1_GLfloat_1ui_raw
518 #define DEST_1F trans_1_GLfloat_1f_raw
520 #include "m_trans_tmp.h"
532 static void trans_4_GLubyte_4ub_raw(GLubyte (*t
)[4],
537 const GLubyte
*f
= (GLubyte
*) Ptr
+ SRC_START
* stride
;
540 if (((((uintptr_t) f
| (uintptr_t) stride
)) & 3L) == 0L) {
543 for (i
= DST_START
; i
< n
; i
++, f
+= stride
) {
544 COPY_4UBV( t
[i
], f
);
547 for (i
= DST_START
; i
< n
; i
++, f
+= stride
) {
557 static void init_translate_raw(void)
559 MEMSET( TAB(_1ui
), 0, sizeof(TAB(_1ui
)) );
560 MEMSET( TAB(_1ub
), 0, sizeof(TAB(_1ub
)) );
561 MEMSET( TAB(_3fn
), 0, sizeof(TAB(_3fn
)) );
562 MEMSET( TAB(_4ub
), 0, sizeof(TAB(_4ub
)) );
563 MEMSET( TAB(_4us
), 0, sizeof(TAB(_4us
)) );
564 MEMSET( TAB(_4f
), 0, sizeof(TAB(_4f
)) );
565 MEMSET( TAB(_4fn
), 0, sizeof(TAB(_4fn
)) );
567 init_trans_4_GLbyte_raw();
568 init_trans_3_GLbyte_raw();
569 init_trans_2_GLbyte_raw();
570 init_trans_1_GLbyte_raw();
571 init_trans_1_GLubyte_raw();
572 init_trans_3_GLubyte_raw();
573 init_trans_4_GLubyte_raw();
574 init_trans_4_GLshort_raw();
575 init_trans_3_GLshort_raw();
576 init_trans_2_GLshort_raw();
577 init_trans_1_GLshort_raw();
578 init_trans_4_GLushort_raw();
579 init_trans_3_GLushort_raw();
580 init_trans_2_GLushort_raw();
581 init_trans_1_GLushort_raw();
582 init_trans_4_GLint_raw();
583 init_trans_3_GLint_raw();
584 init_trans_2_GLint_raw();
585 init_trans_1_GLint_raw();
586 init_trans_4_GLuint_raw();
587 init_trans_3_GLuint_raw();
588 init_trans_2_GLuint_raw();
589 init_trans_1_GLuint_raw();
590 init_trans_4_GLdouble_raw();
591 init_trans_3_GLdouble_raw();
592 init_trans_2_GLdouble_raw();
593 init_trans_1_GLdouble_raw();
594 init_trans_4_GLfloat_raw();
595 init_trans_3_GLfloat_raw();
596 init_trans_2_GLfloat_raw();
597 init_trans_1_GLfloat_raw();
599 TAB(_4ub
)[4][TYPE_IDX(GL_UNSIGNED_BYTE
)] = trans_4_GLubyte_4ub_raw
;
618 void _math_init_translate( void )
620 init_translate_raw();
625 * Translate vector of values to GLfloat [1].
627 void _math_trans_1f(GLfloat
*to
,
634 _math_trans_1f_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
638 * Translate vector of values to GLuint [1].
640 void _math_trans_1ui(GLuint
*to
,
647 _math_trans_1ui_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
651 * Translate vector of values to GLubyte [1].
653 void _math_trans_1ub(GLubyte
*to
,
660 _math_trans_1ub_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
665 * Translate vector of values to GLubyte [4].
667 void _math_trans_4ub(GLubyte (*to
)[4],
675 _math_trans_4ub_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
679 * Translate vector of values to GLchan [4].
681 void _math_trans_4chan( GLchan (*to
)[4],
689 #if CHAN_TYPE == GL_UNSIGNED_BYTE
690 _math_trans_4ub( to
, ptr
, stride
, type
, size
, start
, n
);
691 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
692 _math_trans_4us( to
, ptr
, stride
, type
, size
, start
, n
);
693 #elif CHAN_TYPE == GL_FLOAT
694 _math_trans_4fn( to
, ptr
, stride
, type
, size
, start
, n
);
699 * Translate vector of values to GLushort [4].
701 void _math_trans_4us(GLushort (*to
)[4],
709 _math_trans_4us_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
713 * Translate vector of values to GLfloat [4].
715 void _math_trans_4f(GLfloat (*to
)[4],
723 _math_trans_4f_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
727 * Translate vector of values to GLfloat[4], normalized to [-1, 1].
729 void _math_trans_4fn(GLfloat (*to
)[4],
737 _math_trans_4fn_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
741 * Translate vector of values to GLfloat[3], normalized to [-1, 1].
743 void _math_trans_3fn(GLfloat (*to
)[3],
750 _math_trans_3fn_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);