Major rework of tnl module
[mesa.git] / src / mesa / math / m_translate.c
1 /* $Id: m_translate.c,v 1.2 2000/12/26 05:09:31 keithw 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 * New (3.1) transformation code written by Keith Whitwell.
29 */
30
31
32 #include "glheader.h"
33 #include "colormac.h"
34 #include "mem.h"
35 #include "mmath.h"
36
37 #include "m_translate.h"
38
39
40
41 typedef void (*trans_1f_func)(GLfloat *to,
42 CONST void *ptr,
43 GLuint stride,
44 GLuint start,
45 GLuint n );
46
47 typedef void (*trans_1ui_func)(GLuint *to,
48 CONST void *ptr,
49 GLuint stride,
50 GLuint start,
51 GLuint n );
52
53 typedef void (*trans_1ub_func)(GLubyte *to,
54 CONST void *ptr,
55 GLuint stride,
56 GLuint start,
57 GLuint n );
58
59 typedef void (*trans_4ub_func)(GLubyte (*to)[4],
60 CONST void *ptr,
61 GLuint stride,
62 GLuint start,
63 GLuint n );
64
65 typedef void (*trans_4f_func)(GLfloat (*to)[4],
66 CONST void *ptr,
67 GLuint stride,
68 GLuint start,
69 GLuint n );
70
71 typedef void (*trans_3f_func)(GLfloat (*to)[3],
72 CONST void *ptr,
73 GLuint stride,
74 GLuint start,
75 GLuint n );
76
77
78
79
80 #define TYPE_IDX(t) ((t) & 0xf)
81 #define MAX_TYPES TYPE_IDX(GL_DOUBLE)+1 /* 0xa + 1 */
82
83
84 /* This macro is used on other systems, so undefine it for this module */
85
86 #undef CHECK
87
88 static trans_1f_func _math_trans_1f_tab[MAX_TYPES];
89 static trans_1ui_func _math_trans_1ui_tab[MAX_TYPES];
90 static trans_1ub_func _math_trans_1ub_tab[MAX_TYPES];
91 static trans_3f_func _math_trans_3f_tab[MAX_TYPES];
92 static trans_4ub_func _math_trans_4ub_tab[5][MAX_TYPES];
93 static trans_4f_func _math_trans_4f_tab[5][MAX_TYPES];
94
95
96 #define PTR_ELT(ptr, elt) (((SRC *)ptr)[elt])
97
98
99 #define TAB(x) _math_trans##x##_tab
100 #define ARGS GLuint start, GLuint n
101 #define SRC_START start
102 #define DST_START 0
103 #define STRIDE stride
104 #define NEXT_F f += stride
105 #define NEXT_F2
106 #define CHECK
107
108
109
110
111 /* GL_BYTE
112 */
113 #define SRC GLbyte
114 #define SRC_IDX TYPE_IDX(GL_BYTE)
115 #define TRX_3F(f,n) BYTE_TO_FLOAT( PTR_ELT(f,n) )
116 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
117 #define TRX_UB(ub, f,n) ub = BYTE_TO_UBYTE( PTR_ELT(f,n) )
118 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
119
120
121 #define SZ 4
122 #define INIT init_trans_4_GLbyte_raw
123 #define DEST_4F trans_4_GLbyte_4f_raw
124 #define DEST_4UB trans_4_GLbyte_4ub_raw
125 #include "m_trans_tmp.h"
126
127 #define SZ 3
128 #define INIT init_trans_3_GLbyte_raw
129 #define DEST_4F trans_3_GLbyte_4f_raw
130 #define DEST_4UB trans_3_GLbyte_4ub_raw
131 #define DEST_3F trans_3_GLbyte_3f_raw
132 #include "m_trans_tmp.h"
133
134 #define SZ 2
135 #define INIT init_trans_2_GLbyte_raw
136 #define DEST_4F trans_2_GLbyte_4f_raw
137 #include "m_trans_tmp.h"
138
139 #define SZ 1
140 #define INIT init_trans_1_GLbyte_raw
141 #define DEST_4F trans_1_GLbyte_4f_raw
142 #define DEST_1UB trans_1_GLbyte_1ub_raw
143 #define DEST_1UI trans_1_GLbyte_1ui_raw
144 #include "m_trans_tmp.h"
145
146 #undef SRC
147 #undef TRX_3F
148 #undef TRX_4F
149 #undef TRX_UB
150 #undef TRX_UI
151 #undef SRC_IDX
152
153 /* GL_UNSIGNED_BYTE
154 */
155 #define SRC GLubyte
156 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_BYTE)
157 #define TRX_3F(f,n) /* unused */
158 #define TRX_4F(f,n) /* unused */
159 #define TRX_UB(ub, f,n) ub = PTR_ELT(f,n)
160 #define TRX_UI(f,n) (GLuint)PTR_ELT(f,n)
161
162 /* 4ub->4ub handled in special case below.
163 */
164
165 #define SZ 3
166 #define INIT init_trans_3_GLubyte_raw
167 #define DEST_4UB trans_3_GLubyte_4ub_raw
168 #include "m_trans_tmp.h"
169
170
171 #define SZ 1
172 #define INIT init_trans_1_GLubyte_raw
173 #define DEST_1UI trans_1_GLubyte_1ui_raw
174 #define DEST_1UB trans_1_GLubyte_1ub_raw
175 #include "m_trans_tmp.h"
176
177 #undef SRC
178 #undef SRC_IDX
179 #undef TRX_3F
180 #undef TRX_4F
181 #undef TRX_UB
182 #undef TRX_UI
183
184
185 /* GL_SHORT
186 */
187 #define SRC GLshort
188 #define SRC_IDX TYPE_IDX(GL_SHORT)
189 #define TRX_3F(f,n) SHORT_TO_FLOAT( PTR_ELT(f,n) )
190 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
191 #define TRX_UB(ub, f,n) ub = SHORT_TO_UBYTE(PTR_ELT(f,n))
192 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
193
194
195 #define SZ 4
196 #define INIT init_trans_4_GLshort_raw
197 #define DEST_4F trans_4_GLshort_4f_raw
198 #define DEST_4UB trans_4_GLshort_4ub_raw
199 #include "m_trans_tmp.h"
200
201 #define SZ 3
202 #define INIT init_trans_3_GLshort_raw
203 #define DEST_4F trans_3_GLshort_4f_raw
204 #define DEST_4UB trans_3_GLshort_4ub_raw
205 #define DEST_3F trans_3_GLshort_3f_raw
206 #include "m_trans_tmp.h"
207
208 #define SZ 2
209 #define INIT init_trans_2_GLshort_raw
210 #define DEST_4F trans_2_GLshort_4f_raw
211 #include "m_trans_tmp.h"
212
213 #define SZ 1
214 #define INIT init_trans_1_GLshort_raw
215 #define DEST_4F trans_1_GLshort_4f_raw
216 #define DEST_1UB trans_1_GLshort_1ub_raw
217 #define DEST_1UI trans_1_GLshort_1ui_raw
218 #include "m_trans_tmp.h"
219
220
221 #undef SRC
222 #undef SRC_IDX
223 #undef TRX_3F
224 #undef TRX_4F
225 #undef TRX_UB
226 #undef TRX_UI
227
228
229 /* GL_UNSIGNED_SHORT
230 */
231 #define SRC GLushort
232 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_SHORT)
233 #define TRX_3F(f,n) USHORT_TO_FLOAT( PTR_ELT(f,n) )
234 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
235 #define TRX_UB(ub,f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 8)
236 #define TRX_UI(f,n) (GLuint) PTR_ELT(f,n)
237
238
239 #define SZ 4
240 #define INIT init_trans_4_GLushort_raw
241 #define DEST_4F trans_4_GLushort_4f_raw
242 #define DEST_4UB trans_4_GLushort_4ub_raw
243 #include "m_trans_tmp.h"
244
245 #define SZ 3
246 #define INIT init_trans_3_GLushort_raw
247 #define DEST_4F trans_3_GLushort_4f_raw
248 #define DEST_4UB trans_3_GLushort_4ub_raw
249 #define DEST_3F trans_3_GLushort_3f_raw
250 #include "m_trans_tmp.h"
251
252 #define SZ 2
253 #define INIT init_trans_2_GLushort_raw
254 #define DEST_4F trans_2_GLushort_4f_raw
255 #include "m_trans_tmp.h"
256
257 #define SZ 1
258 #define INIT init_trans_1_GLushort_raw
259 #define DEST_4F trans_1_GLushort_4f_raw
260 #define DEST_1UB trans_1_GLushort_1ub_raw
261 #define DEST_1UI trans_1_GLushort_1ui_raw
262 #include "m_trans_tmp.h"
263
264 #undef SRC
265 #undef SRC_IDX
266 #undef TRX_3F
267 #undef TRX_4F
268 #undef TRX_UB
269 #undef TRX_UI
270
271
272 /* GL_INT
273 */
274 #define SRC GLint
275 #define SRC_IDX TYPE_IDX(GL_INT)
276 #define TRX_3F(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
277 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
278 #define TRX_UB(ub, f,n) ub = INT_TO_UBYTE(PTR_ELT(f,n))
279 #define TRX_UI(f,n) (PTR_ELT(f,n) < 0 ? 0 : (GLuint) PTR_ELT(f,n))
280
281
282 #define SZ 4
283 #define INIT init_trans_4_GLint_raw
284 #define DEST_4F trans_4_GLint_4f_raw
285 #define DEST_4UB trans_4_GLint_4ub_raw
286 #include "m_trans_tmp.h"
287
288 #define SZ 3
289 #define INIT init_trans_3_GLint_raw
290 #define DEST_4F trans_3_GLint_4f_raw
291 #define DEST_4UB trans_3_GLint_4ub_raw
292 #define DEST_3F trans_3_GLint_3f_raw
293 #include "m_trans_tmp.h"
294
295 #define SZ 2
296 #define INIT init_trans_2_GLint_raw
297 #define DEST_4F trans_2_GLint_4f_raw
298 #include "m_trans_tmp.h"
299
300 #define SZ 1
301 #define INIT init_trans_1_GLint_raw
302 #define DEST_4F trans_1_GLint_4f_raw
303 #define DEST_1UB trans_1_GLint_1ub_raw
304 #define DEST_1UI trans_1_GLint_1ui_raw
305 #include "m_trans_tmp.h"
306
307
308 #undef SRC
309 #undef SRC_IDX
310 #undef TRX_3F
311 #undef TRX_4F
312 #undef TRX_UB
313 #undef TRX_UI
314
315
316 /* GL_UNSIGNED_INT
317 */
318 #define SRC GLuint
319 #define SRC_IDX TYPE_IDX(GL_UNSIGNED_INT)
320 #define TRX_3F(f,n) INT_TO_FLOAT( PTR_ELT(f,n) )
321 #define TRX_4F(f,n) (GLfloat)( PTR_ELT(f,n) )
322 #define TRX_UB(ub, f,n) ub = (GLubyte) (PTR_ELT(f,n) >> 24)
323 #define TRX_UI(f,n) PTR_ELT(f,n)
324
325
326 #define SZ 4
327 #define INIT init_trans_4_GLuint_raw
328 #define DEST_4F trans_4_GLuint_4f_raw
329 #define DEST_4UB trans_4_GLuint_4ub_raw
330 #include "m_trans_tmp.h"
331
332 #define SZ 3
333 #define INIT init_trans_3_GLuint_raw
334 #define DEST_4F trans_3_GLuint_4f_raw
335 #define DEST_4UB trans_3_GLuint_4ub_raw
336 #define DEST_3F trans_3_GLuint_3f_raw
337 #include "m_trans_tmp.h"
338
339 #define SZ 2
340 #define INIT init_trans_2_GLuint_raw
341 #define DEST_4F trans_2_GLuint_4f_raw
342 #include "m_trans_tmp.h"
343
344 #define SZ 1
345 #define INIT init_trans_1_GLuint_raw
346 #define DEST_4F trans_1_GLuint_4f_raw
347 #define DEST_1UB trans_1_GLuint_1ub_raw
348 #define DEST_1UI trans_1_GLuint_1ui_raw
349 #include "m_trans_tmp.h"
350
351 #undef SRC
352 #undef SRC_IDX
353 #undef TRX_3F
354 #undef TRX_4F
355 #undef TRX_UB
356 #undef TRX_UI
357
358
359 /* GL_DOUBLE
360 */
361 #define SRC GLdouble
362 #define SRC_IDX TYPE_IDX(GL_DOUBLE)
363 #define TRX_3F(f,n) PTR_ELT(f,n)
364 #define TRX_4F(f,n) PTR_ELT(f,n)
365 #define TRX_UB(ub,f,n) FLOAT_COLOR_TO_CHAN(ub, PTR_ELT(f,n))
366 #define TRX_UI(f,n) (GLuint) (GLint) PTR_ELT(f,n)
367 #define TRX_1F(f,n) PTR_ELT(f,n)
368
369
370 #define SZ 4
371 #define INIT init_trans_4_GLdouble_raw
372 #define DEST_4F trans_4_GLdouble_4f_raw
373 #define DEST_4UB trans_4_GLdouble_4ub_raw
374 #include "m_trans_tmp.h"
375
376 #define SZ 3
377 #define INIT init_trans_3_GLdouble_raw
378 #define DEST_4F trans_3_GLdouble_4f_raw
379 #define DEST_4UB trans_3_GLdouble_4ub_raw
380 #define DEST_3F trans_3_GLdouble_3f_raw
381 #include "m_trans_tmp.h"
382
383 #define SZ 2
384 #define INIT init_trans_2_GLdouble_raw
385 #define DEST_4F trans_2_GLdouble_4f_raw
386 #include "m_trans_tmp.h"
387
388 #define SZ 1
389 #define INIT init_trans_1_GLdouble_raw
390 #define DEST_4F trans_1_GLdouble_4f_raw
391 #define DEST_1UB trans_1_GLdouble_1ub_raw
392 #define DEST_1UI trans_1_GLdouble_1ui_raw
393 #define DEST_1F trans_1_GLdouble_1f_raw
394 #include "m_trans_tmp.h"
395
396 #undef SRC
397 #undef SRC_IDX
398
399 /* GL_FLOAT
400 */
401 #define SRC GLfloat
402 #define SRC_IDX TYPE_IDX(GL_FLOAT)
403 #define SZ 4
404 #define INIT init_trans_4_GLfloat_raw
405 #define DEST_4UB trans_4_GLfloat_4ub_raw
406 #define DEST_4F trans_4_GLfloat_4f_raw
407 #include "m_trans_tmp.h"
408
409 #define SZ 3
410 #define INIT init_trans_3_GLfloat_raw
411 #define DEST_4F trans_3_GLfloat_4f_raw
412 #define DEST_4UB trans_3_GLfloat_4ub_raw
413 #define DEST_3F trans_3_GLfloat_3f_raw
414 #include "m_trans_tmp.h"
415
416 #define SZ 2
417 #define INIT init_trans_2_GLfloat_raw
418 #define DEST_4F trans_2_GLfloat_4f_raw
419 #include "m_trans_tmp.h"
420
421 #define SZ 1
422 #define INIT init_trans_1_GLfloat_raw
423 #define DEST_4F trans_1_GLfloat_4f_raw
424 #define DEST_1UB trans_1_GLfloat_1ub_raw
425 #define DEST_1UI trans_1_GLfloat_1ui_raw
426 #define DEST_1F trans_1_GLfloat_1f_raw
427
428 #include "m_trans_tmp.h"
429
430 #undef SRC
431 #undef SRC_IDX
432 #undef TRX_3F
433 #undef TRX_4F
434 #undef TRX_UB
435 #undef TRX_UI
436
437
438 static void trans_4_GLubyte_4ub_raw (GLubyte (*t)[4],
439 CONST void *Ptr,
440 GLuint stride,
441 ARGS )
442 {
443 const GLubyte *f = (GLubyte *) Ptr + SRC_START * stride;
444 GLuint i;
445
446 if (((((long) f | (long) stride)) & 3L) == 0L) {
447 /* Aligned.
448 */
449 for (i = DST_START ; i < n ; i++, f += stride) {
450 COPY_4UBV( t[i], f );
451 }
452 } else {
453 for (i = DST_START ; i < n ; i++, f += stride) {
454 t[i][0] = f[0];
455 t[i][1] = f[1];
456 t[i][2] = f[2];
457 t[i][3] = f[3];
458 }
459 }
460 }
461
462
463 static void init_translate_raw(void)
464 {
465 MEMSET( TAB(_1ui), 0, sizeof(TAB(_1ui)) );
466 MEMSET( TAB(_1ub), 0, sizeof(TAB(_1ub)) );
467 MEMSET( TAB(_3f), 0, sizeof(TAB(_3f)) );
468 MEMSET( TAB(_4ub), 0, sizeof(TAB(_4ub)) );
469 MEMSET( TAB(_4f), 0, sizeof(TAB(_4f)) );
470
471 TAB(_4ub)[4][TYPE_IDX(GL_UNSIGNED_BYTE)] = trans_4_GLubyte_4ub_raw;
472
473 init_trans_4_GLbyte_raw();
474 init_trans_3_GLbyte_raw();
475 init_trans_2_GLbyte_raw();
476 init_trans_1_GLbyte_raw();
477 init_trans_1_GLubyte_raw();
478 init_trans_3_GLubyte_raw();
479 init_trans_4_GLshort_raw();
480 init_trans_3_GLshort_raw();
481 init_trans_2_GLshort_raw();
482 init_trans_1_GLshort_raw();
483 init_trans_4_GLushort_raw();
484 init_trans_3_GLushort_raw();
485 init_trans_2_GLushort_raw();
486 init_trans_1_GLushort_raw();
487 init_trans_4_GLint_raw();
488 init_trans_3_GLint_raw();
489 init_trans_2_GLint_raw();
490 init_trans_1_GLint_raw();
491 init_trans_4_GLuint_raw();
492 init_trans_3_GLuint_raw();
493 init_trans_2_GLuint_raw();
494 init_trans_1_GLuint_raw();
495 init_trans_4_GLdouble_raw();
496 init_trans_3_GLdouble_raw();
497 init_trans_2_GLdouble_raw();
498 init_trans_1_GLdouble_raw();
499 init_trans_4_GLfloat_raw();
500 init_trans_3_GLfloat_raw();
501 init_trans_2_GLfloat_raw();
502 init_trans_1_GLfloat_raw();
503 }
504
505
506 #undef TAB
507 #undef CLASS
508 #undef ARGS
509 #undef CHECK
510 #undef SRC_START
511 #undef DST_START
512 #undef NEXT_F
513 #undef NEXT_F2
514
515
516
517
518
519 void _math_init_translate( void )
520 {
521 init_translate_raw();
522 }
523
524
525
526 void _math_trans_1f(GLfloat *to,
527 CONST void *ptr,
528 GLuint stride,
529 GLenum type,
530 GLuint start,
531 GLuint n )
532 {
533 _math_trans_1f_tab[TYPE_IDX(type)]( to, ptr, stride, start, n );
534 }
535
536 void _math_trans_1ui(GLuint *to,
537 CONST void *ptr,
538 GLuint stride,
539 GLenum type,
540 GLuint start,
541 GLuint n )
542 {
543 _math_trans_1ui_tab[TYPE_IDX(type)]( to, ptr, stride, start, n );
544 }
545
546 void _math_trans_1ub(GLubyte *to,
547 CONST void *ptr,
548 GLuint stride,
549 GLenum type,
550 GLuint start,
551 GLuint n )
552 {
553 _math_trans_1ub_tab[TYPE_IDX(type)]( to, ptr, stride, start, n );
554 }
555
556
557 void _math_trans_4ub(GLubyte (*to)[4],
558 CONST void *ptr,
559 GLuint stride,
560 GLenum type,
561 GLuint size,
562 GLuint start,
563 GLuint n )
564 {
565 _math_trans_4ub_tab[size][TYPE_IDX(type)]( to, ptr, stride, start, n );
566 }
567
568 void _math_trans_4f(GLfloat (*to)[4],
569 CONST void *ptr,
570 GLuint stride,
571 GLenum type,
572 GLuint size,
573 GLuint start,
574 GLuint n )
575 {
576 _math_trans_4f_tab[size][TYPE_IDX(type)]( to, ptr, stride, start, n );
577 }
578
579 void _math_trans_3f(GLfloat (*to)[3],
580 CONST void *ptr,
581 GLuint stride,
582 GLenum type,
583 GLuint start,
584 GLuint n )
585 {
586 _math_trans_3f_tab[TYPE_IDX(type)]( to, ptr, stride, start, n );
587 }
588