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