1 /* $Id: m_translate.c,v 1.5 2001/02/20 18:28:52 keithw 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) (GLfloat)( 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) /* unused */
171 #define TRX_4F(f,n) /* unused */
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.
180 #define INIT init_trans_3_GLubyte_raw
181 #define DEST_4UB trans_3_GLubyte_4ub_raw
182 #define DEST_4US trans_3_GLubyte_4us_raw
183 #include "m_trans_tmp.h"
187 #define INIT init_trans_1_GLubyte_raw
188 #define DEST_1UI trans_1_GLubyte_1ui_raw
189 #define DEST_1UB trans_1_GLubyte_1ub_raw
190 #include "m_trans_tmp.h"
204 #define SRC_IDX TYPE_IDX(GL_SHORT)
205 #define TRX_3F(f,n) SHORT_TO_FLOAT( PTR_ELT(f,n) )
206 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
207 #define TRX_UB(ub, f,n) ub = SHORT_TO_UBYTE(PTR_ELT(f,n))
208 #define TRX_US(us, f,n) us = SHORT_TO_USHORT(PTR_ELT(f,n))
209 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
213 #define INIT init_trans_4_GLshort_raw
214 #define DEST_4F trans_4_GLshort_4f_raw
215 #define DEST_4UB trans_4_GLshort_4ub_raw
216 #define DEST_4US trans_4_GLshort_4us_raw
217 #include "m_trans_tmp.h"
220 #define INIT init_trans_3_GLshort_raw
221 #define DEST_4F trans_3_GLshort_4f_raw
222 #define DEST_4UB trans_3_GLshort_4ub_raw
223 #define DEST_4US trans_3_GLshort_4us_raw
224 #define DEST_3F trans_3_GLshort_3f_raw
225 #include "m_trans_tmp.h"
228 #define INIT init_trans_2_GLshort_raw
229 #define DEST_4F trans_2_GLshort_4f_raw
230 #include "m_trans_tmp.h"
233 #define INIT init_trans_1_GLshort_raw
234 #define DEST_4F trans_1_GLshort_4f_raw
235 #define DEST_1UB trans_1_GLshort_1ub_raw
236 #define DEST_1UI trans_1_GLshort_1ui_raw
237 #include "m_trans_tmp.h"
252 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_SHORT)
253 #define TRX_3F(f,n) USHORT_TO_FLOAT( PTR_ELT(f,n) )
254 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
255 #define TRX_UB(ub,f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 8)
256 #define TRX_US(us,f,n) us = (GLushort) (PTR_ELT(f,n) >> 8)
257 #define TRX_UI(f,n) (GLuint) PTR_ELT(f,n)
261 #define INIT init_trans_4_GLushort_raw
262 #define DEST_4F trans_4_GLushort_4f_raw
263 #define DEST_4UB trans_4_GLushort_4ub_raw
264 #define DEST_4US trans_4_GLushort_4us_raw
265 #include "m_trans_tmp.h"
268 #define INIT init_trans_3_GLushort_raw
269 #define DEST_4F trans_3_GLushort_4f_raw
270 #define DEST_4UB trans_3_GLushort_4ub_raw
271 #define DEST_4US trans_3_GLushort_4us_raw
272 #define DEST_3F trans_3_GLushort_3f_raw
273 #include "m_trans_tmp.h"
276 #define INIT init_trans_2_GLushort_raw
277 #define DEST_4F trans_2_GLushort_4f_raw
278 #include "m_trans_tmp.h"
281 #define INIT init_trans_1_GLushort_raw
282 #define DEST_4F trans_1_GLushort_4f_raw
283 #define DEST_1UB trans_1_GLushort_1ub_raw
284 #define DEST_1UI trans_1_GLushort_1ui_raw
285 #include "m_trans_tmp.h"
299 #define SRC_IDX TYPE_IDX(GL_INT)
300 #define TRX_3F(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
301 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
302 #define TRX_UB(ub, f,n) ub = INT_TO_UBYTE(PTR_ELT(f,n))
303 #define TRX_US(us, f,n) us = INT_TO_USHORT(PTR_ELT(f,n))
304 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
308 #define INIT init_trans_4_GLint_raw
309 #define DEST_4F trans_4_GLint_4f_raw
310 #define DEST_4UB trans_4_GLint_4ub_raw
311 #define DEST_4US trans_4_GLint_4us_raw
312 #include "m_trans_tmp.h"
315 #define INIT init_trans_3_GLint_raw
316 #define DEST_4F trans_3_GLint_4f_raw
317 #define DEST_4UB trans_3_GLint_4ub_raw
318 #define DEST_4US trans_3_GLint_4us_raw
319 #define DEST_3F trans_3_GLint_3f_raw
320 #include "m_trans_tmp.h"
323 #define INIT init_trans_2_GLint_raw
324 #define DEST_4F trans_2_GLint_4f_raw
325 #include "m_trans_tmp.h"
328 #define INIT init_trans_1_GLint_raw
329 #define DEST_4F trans_1_GLint_4f_raw
330 #define DEST_1UB trans_1_GLint_1ub_raw
331 #define DEST_1UI trans_1_GLint_1ui_raw
332 #include "m_trans_tmp.h"
347 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_INT)
348 #define TRX_3F(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
349 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
350 #define TRX_UB(ub, f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 24)
351 #define TRX_US(us, f,n) us = (GLshort) (PTR_ELT(f,n) >> 16)
352 #define TRX_UI(f,n) PTR_ELT(f,n)
356 #define INIT init_trans_4_GLuint_raw
357 #define DEST_4F trans_4_GLuint_4f_raw
358 #define DEST_4UB trans_4_GLuint_4ub_raw
359 #define DEST_4US trans_4_GLuint_4us_raw
360 #include "m_trans_tmp.h"
363 #define INIT init_trans_3_GLuint_raw
364 #define DEST_4F trans_3_GLuint_4f_raw
365 #define DEST_4UB trans_3_GLuint_4ub_raw
366 #define DEST_4US trans_3_GLuint_4us_raw
367 #define DEST_3F trans_3_GLuint_3f_raw
368 #include "m_trans_tmp.h"
371 #define INIT init_trans_2_GLuint_raw
372 #define DEST_4F trans_2_GLuint_4f_raw
373 #include "m_trans_tmp.h"
376 #define INIT init_trans_1_GLuint_raw
377 #define DEST_4F trans_1_GLuint_4f_raw
378 #define DEST_1UB trans_1_GLuint_1ub_raw
379 #define DEST_1UI trans_1_GLuint_1ui_raw
380 #include "m_trans_tmp.h"
394 #define SRC_IDX TYPE_IDX(GL_DOUBLE)
395 #define TRX_3F(f,n) PTR_ELT(f,n)
396 #define TRX_4F(f,n) PTR_ELT(f,n)
397 #define TRX_UB(ub,f,n) UNCLAMPED_FLOAT_TO_UBYTE(ub, PTR_ELT(f,n))
398 #define TRX_US(us,f,n) UNCLAMPED_FLOAT_TO_USHORT(us, PTR_ELT(f,n))
399 #define TRX_UI(f,n) (GLuint) (GLint) PTR_ELT(f,n)
400 #define TRX_1F(f,n) PTR_ELT(f,n)
404 #define INIT init_trans_4_GLdouble_raw
405 #define DEST_4F trans_4_GLdouble_4f_raw
406 #define DEST_4UB trans_4_GLdouble_4ub_raw
407 #define DEST_4US trans_4_GLdouble_4us_raw
408 #include "m_trans_tmp.h"
411 #define INIT init_trans_3_GLdouble_raw
412 #define DEST_4F trans_3_GLdouble_4f_raw
413 #define DEST_4UB trans_3_GLdouble_4ub_raw
414 #define DEST_4US trans_3_GLdouble_4us_raw
415 #define DEST_3F trans_3_GLdouble_3f_raw
416 #include "m_trans_tmp.h"
419 #define INIT init_trans_2_GLdouble_raw
420 #define DEST_4F trans_2_GLdouble_4f_raw
421 #include "m_trans_tmp.h"
424 #define INIT init_trans_1_GLdouble_raw
425 #define DEST_4F trans_1_GLdouble_4f_raw
426 #define DEST_1UB trans_1_GLdouble_1ub_raw
427 #define DEST_1UI trans_1_GLdouble_1ui_raw
428 #define DEST_1F trans_1_GLdouble_1f_raw
429 #include "m_trans_tmp.h"
437 #define SRC_IDX TYPE_IDX(GL_FLOAT)
439 #define INIT init_trans_4_GLfloat_raw
440 #define DEST_4UB trans_4_GLfloat_4ub_raw
441 #define DEST_4US trans_4_GLfloat_4us_raw
442 #define DEST_4F trans_4_GLfloat_4f_raw
443 #include "m_trans_tmp.h"
446 #define INIT init_trans_3_GLfloat_raw
447 #define DEST_4F trans_3_GLfloat_4f_raw
448 #define DEST_4UB trans_3_GLfloat_4ub_raw
449 #define DEST_4US trans_3_GLfloat_4us_raw
450 #define DEST_3F trans_3_GLfloat_3f_raw
451 #include "m_trans_tmp.h"
454 #define INIT init_trans_2_GLfloat_raw
455 #define DEST_4F trans_2_GLfloat_4f_raw
456 #include "m_trans_tmp.h"
459 #define INIT init_trans_1_GLfloat_raw
460 #define DEST_4F trans_1_GLfloat_4f_raw
461 #define DEST_1UB trans_1_GLfloat_1ub_raw
462 #define DEST_1UI trans_1_GLfloat_1ui_raw
463 #define DEST_1F trans_1_GLfloat_1f_raw
465 #include "m_trans_tmp.h"
476 static void trans_4_GLubyte_4ub_raw(GLubyte (*t
)[4],
481 const GLubyte
*f
= (GLubyte
*) Ptr
+ SRC_START
* stride
;
484 if (((((long) f
| (long) stride
)) & 3L) == 0L) {
487 for (i
= DST_START
; i
< n
; i
++, f
+= stride
) {
488 COPY_4UBV( t
[i
], f
);
491 for (i
= DST_START
; i
< n
; i
++, f
+= stride
) {
501 static void init_translate_raw(void)
503 MEMSET( TAB(_1ui
), 0, sizeof(TAB(_1ui
)) );
504 MEMSET( TAB(_1ub
), 0, sizeof(TAB(_1ub
)) );
505 MEMSET( TAB(_3f
), 0, sizeof(TAB(_3f
)) );
506 MEMSET( TAB(_4ub
), 0, sizeof(TAB(_4ub
)) );
507 MEMSET( TAB(_4us
), 0, sizeof(TAB(_4us
)) );
508 MEMSET( TAB(_4f
), 0, sizeof(TAB(_4f
)) );
510 TAB(_4ub
)[4][TYPE_IDX(GL_UNSIGNED_BYTE
)] = trans_4_GLubyte_4ub_raw
;
512 init_trans_4_GLbyte_raw();
513 init_trans_3_GLbyte_raw();
514 init_trans_2_GLbyte_raw();
515 init_trans_1_GLbyte_raw();
516 init_trans_1_GLubyte_raw();
517 init_trans_3_GLubyte_raw();
518 init_trans_4_GLshort_raw();
519 init_trans_3_GLshort_raw();
520 init_trans_2_GLshort_raw();
521 init_trans_1_GLshort_raw();
522 init_trans_4_GLushort_raw();
523 init_trans_3_GLushort_raw();
524 init_trans_2_GLushort_raw();
525 init_trans_1_GLushort_raw();
526 init_trans_4_GLint_raw();
527 init_trans_3_GLint_raw();
528 init_trans_2_GLint_raw();
529 init_trans_1_GLint_raw();
530 init_trans_4_GLuint_raw();
531 init_trans_3_GLuint_raw();
532 init_trans_2_GLuint_raw();
533 init_trans_1_GLuint_raw();
534 init_trans_4_GLdouble_raw();
535 init_trans_3_GLdouble_raw();
536 init_trans_2_GLdouble_raw();
537 init_trans_1_GLdouble_raw();
538 init_trans_4_GLfloat_raw();
539 init_trans_3_GLfloat_raw();
540 init_trans_2_GLfloat_raw();
541 init_trans_1_GLfloat_raw();
558 void _math_init_translate( void )
560 init_translate_raw();
565 void _math_trans_1f(GLfloat
*to
,
572 _math_trans_1f_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
575 void _math_trans_1ui(GLuint
*to
,
582 _math_trans_1ui_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
585 void _math_trans_1ub(GLubyte
*to
,
592 _math_trans_1ub_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
596 void _math_trans_4ub(GLubyte (*to
)[4],
604 _math_trans_4ub_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
607 void _math_trans_4chan( GLchan (*to
)[4],
615 #if CHAN_TYPE == GL_UNSIGNED_BYTE
616 _math_trans_4ub( to
, ptr
, stride
, type
, size
, start
, n
);
617 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
618 _math_trans_4us( to
, ptr
, stride
, type
, size
, start
, n
);
619 #elif CHAN_TYPE == GL_FLOAT
620 _math_trans_4f( to
, ptr
, stride
, type
, size
, start
, n
);
624 void _math_trans_4us(GLushort (*to
)[4],
632 _math_trans_4us_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
635 void _math_trans_4f(GLfloat (*to
)[4],
643 _math_trans_4f_tab
[size
][TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);
646 void _math_trans_3f(GLfloat (*to
)[3],
653 _math_trans_3f_tab
[TYPE_IDX(type
)]( to
, ptr
, stride
, start
, n
);