2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 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 * Keith Whitwell <keith@tungstengraphics.com>
37 #include "math/m_xform.h"
39 #include "t_context.h"
40 #include "t_pipeline.h"
43 /***********************************************************************
44 * Automatic texture coordinate generation (texgen) code.
48 struct texgen_stage_data
;
50 typedef void (*texgen_func
)( GLcontext
*ctx
,
51 struct texgen_stage_data
*store
,
55 struct texgen_stage_data
{
57 /* Per-texunit derived state.
59 GLuint TexgenSize
[MAX_TEXTURE_COORD_UNITS
];
60 GLuint TexgenHoles
[MAX_TEXTURE_COORD_UNITS
];
61 texgen_func TexgenFunc
[MAX_TEXTURE_COORD_UNITS
];
63 /* Temporary values used in texgen.
68 /* Buffered outputs of the stage.
70 GLvector4f texcoord
[MAX_TEXTURE_COORD_UNITS
];
74 #define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->privatePtr)
78 static GLuint all_bits
[5] = {
86 #define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4)
88 #define TEXGEN_NEED_M (TEXGEN_SPHERE_MAP)
89 #define TEXGEN_NEED_F (TEXGEN_SPHERE_MAP | \
90 TEXGEN_REFLECTION_MAP_NV)
94 static void build_m3( GLfloat f
[][3], GLfloat m
[],
95 const GLvector4f
*normal
,
96 const GLvector4f
*eye
)
98 GLuint stride
= eye
->stride
;
99 GLfloat
*coord
= (GLfloat
*)eye
->start
;
100 GLuint count
= eye
->count
;
101 const GLfloat
*norm
= normal
->start
;
104 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
105 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
108 two_nu
= 2.0F
* DOT3(norm
,u
);
109 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
110 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
111 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
112 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
114 m
[i
] = 0.5F
* _mesa_inv_sqrtf(m
[i
]);
121 static void build_m2( GLfloat f
[][3], GLfloat m
[],
122 const GLvector4f
*normal
,
123 const GLvector4f
*eye
)
125 GLuint stride
= eye
->stride
;
126 GLfloat
*coord
= eye
->start
;
127 GLuint count
= eye
->count
;
129 GLfloat
*norm
= normal
->start
;
132 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
133 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
137 two_nu
= 2.0F
* DOT3(norm
,u
);
138 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
139 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
140 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
141 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
143 m
[i
] = 0.5F
* _mesa_inv_sqrtf(m
[i
]);
150 typedef void (*build_m_func
)( GLfloat f
[][3],
152 const GLvector4f
*normal
,
153 const GLvector4f
*eye
);
156 static build_m_func build_m_tab
[5] = {
165 /* This is unusual in that we respect the stride of the output vector
166 * (f). This allows us to pass in either a texcoord vector4f, or a
167 * temporary vector3f.
169 static void build_f3( GLfloat
*f
,
171 const GLvector4f
*normal
,
172 const GLvector4f
*eye
)
174 GLuint stride
= eye
->stride
;
175 GLfloat
*coord
= eye
->start
;
176 GLuint count
= eye
->count
;
178 GLfloat
*norm
= normal
->start
;
181 for (i
=0;i
<count
;i
++) {
182 GLfloat u
[3], two_nu
;
185 two_nu
= 2.0F
* DOT3(norm
,u
);
186 f
[0] = u
[0] - norm
[0] * two_nu
;
187 f
[1] = u
[1] - norm
[1] * two_nu
;
188 f
[2] = u
[2] - norm
[2] * two_nu
;
189 STRIDE_F(coord
,stride
);
191 STRIDE_F(norm
, normal
->stride
);
196 static void build_f2( GLfloat
*f
,
198 const GLvector4f
*normal
,
199 const GLvector4f
*eye
)
201 GLuint stride
= eye
->stride
;
202 GLfloat
*coord
= eye
->start
;
203 GLuint count
= eye
->count
;
204 GLfloat
*norm
= normal
->start
;
207 for (i
=0;i
<count
;i
++) {
209 GLfloat u
[3], two_nu
;
213 two_nu
= 2.0F
* DOT3(norm
,u
);
214 f
[0] = u
[0] - norm
[0] * two_nu
;
215 f
[1] = u
[1] - norm
[1] * two_nu
;
216 f
[2] = u
[2] - norm
[2] * two_nu
;
218 STRIDE_F(coord
,stride
);
220 STRIDE_F(norm
, normal
->stride
);
224 typedef void (*build_f_func
)( GLfloat
*f
,
226 const GLvector4f
*normal_vec
,
227 const GLvector4f
*eye
);
231 /* Just treat 4-vectors as 3-vectors.
233 static build_f_func build_f_tab
[5] = {
242 /* Special case texgen functions.
244 static void texgen_reflection_map_nv( GLcontext
*ctx
,
245 struct texgen_stage_data
*store
,
248 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
249 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
250 GLvector4f
*out
= &store
->texcoord
[unit
];
252 build_f_tab
[VB
->EyePtr
->size
]( out
->start
,
258 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
259 out
->count
= in
->count
;
260 out
->size
= MAX2(in
->size
, 3);
262 _mesa_copy_tab
[0x8]( out
, in
);
265 out
->flags
|= VEC_SIZE_3
;
267 out
->count
= in
->count
;
274 static void texgen_normal_map_nv( GLcontext
*ctx
,
275 struct texgen_stage_data
*store
,
278 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
279 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
280 GLvector4f
*out
= &store
->texcoord
[unit
];
281 GLvector4f
*normal
= VB
->NormalPtr
;
282 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->start
;
283 GLuint count
= VB
->Count
;
285 const GLfloat
*norm
= normal
->start
;
287 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
288 texcoord
[i
][0] = norm
[0];
289 texcoord
[i
][1] = norm
[1];
290 texcoord
[i
][2] = norm
[2];
295 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
296 out
->count
= in
->count
;
297 out
->size
= MAX2(in
->size
, 3);
299 _mesa_copy_tab
[0x8]( out
, in
);
302 out
->flags
|= VEC_SIZE_3
;
304 out
->count
= in
->count
;
309 static void texgen_sphere_map( GLcontext
*ctx
,
310 struct texgen_stage_data
*store
,
313 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
314 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
315 GLvector4f
*out
= &store
->texcoord
[unit
];
316 GLfloat (*texcoord
)[4] = (GLfloat (*)[4]) out
->start
;
317 GLuint count
= VB
->Count
;
319 GLfloat (*f
)[3] = store
->tmp_f
;
320 GLfloat
*m
= store
->tmp_m
;
322 (build_m_tab
[VB
->EyePtr
->size
])( store
->tmp_f
,
327 for (i
=0;i
<count
;i
++) {
328 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
329 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
333 out
->size
= MAX2(in
->size
,2);
334 out
->count
= in
->count
;
335 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_2
;
337 _mesa_copy_tab
[all_bits
[in
->size
] & ~0x3]( out
, in
);
340 out
->flags
|= VEC_SIZE_2
;
341 out
->count
= in
->count
;
347 static void texgen( GLcontext
*ctx
,
348 struct texgen_stage_data
*store
,
351 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
352 struct vertex_buffer
*VB
= &tnl
->vb
;
353 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
354 GLvector4f
*out
= &store
->texcoord
[unit
];
355 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
356 const GLvector4f
*obj
= VB
->ObjPtr
;
357 const GLvector4f
*eye
= VB
->EyePtr
;
358 const GLvector4f
*normal
= VB
->NormalPtr
;
359 const GLfloat
*m
= store
->tmp_m
;
360 const GLuint count
= VB
->Count
;
361 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->data
;
362 GLfloat (*f
)[3] = store
->tmp_f
;
365 if (texUnit
->_GenFlags
& TEXGEN_NEED_M
) {
366 build_m_tab
[eye
->size
]( store
->tmp_f
, store
->tmp_m
, normal
, eye
);
367 } else if (texUnit
->_GenFlags
& TEXGEN_NEED_F
) {
368 build_f_tab
[eye
->size
]( (GLfloat
*)store
->tmp_f
, 3, normal
, eye
);
374 in
->count
= VB
->Count
;
376 out
->size
= store
->TexgenSize
[unit
];
377 out
->flags
|= texUnit
->TexGenEnabled
;
378 out
->count
= VB
->Count
;
379 holes
= store
->TexgenHoles
[unit
];
382 GLuint copy
= (all_bits
[in
->size
] & ~texUnit
->TexGenEnabled
);
384 _mesa_copy_tab
[copy
]( out
, in
);
386 out
->size
= MAX2(in
->size
, store
->TexgenSize
[unit
]);
387 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | texUnit
->TexGenEnabled
;
388 out
->count
= in
->count
;
390 holes
= ~all_bits
[in
->size
] & store
->TexgenHoles
[unit
];
394 if (holes
& VEC_DIRTY_3
) _mesa_vector4f_clean_elem(out
, count
, 3);
395 if (holes
& VEC_DIRTY_2
) _mesa_vector4f_clean_elem(out
, count
, 2);
396 if (holes
& VEC_DIRTY_1
) _mesa_vector4f_clean_elem(out
, count
, 1);
397 if (holes
& VEC_DIRTY_0
) _mesa_vector4f_clean_elem(out
, count
, 0);
400 if (texUnit
->TexGenEnabled
& S_BIT
) {
402 switch (texUnit
->GenModeS
) {
403 case GL_OBJECT_LINEAR
:
404 _mesa_dotprod_tab
[obj
->size
]( (GLfloat
*)out
->data
,
405 sizeof(out
->data
[0]), obj
,
406 texUnit
->ObjectPlaneS
);
409 _mesa_dotprod_tab
[eye
->size
]( (GLfloat
*)out
->data
,
410 sizeof(out
->data
[0]), eye
,
411 texUnit
->EyePlaneS
);
414 for (i
= 0; i
< count
; i
++)
415 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
417 case GL_REFLECTION_MAP_NV
:
418 for (i
=0;i
<count
;i
++)
419 texcoord
[i
][0] = f
[i
][0];
421 case GL_NORMAL_MAP_NV
: {
422 const GLfloat
*norm
= normal
->start
;
423 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
424 texcoord
[i
][0] = norm
[0];
429 _mesa_problem(ctx
, "Bad S texgen");
433 if (texUnit
->TexGenEnabled
& T_BIT
) {
435 switch (texUnit
->GenModeT
) {
436 case GL_OBJECT_LINEAR
:
437 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][1]),
438 sizeof(out
->data
[0]), obj
,
439 texUnit
->ObjectPlaneT
);
442 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][1]),
443 sizeof(out
->data
[0]), eye
,
444 texUnit
->EyePlaneT
);
447 for (i
= 0; i
< count
; i
++)
448 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
450 case GL_REFLECTION_MAP_NV
:
451 for (i
=0;i
<count
;i
++)
452 texcoord
[i
][1] = f
[i
][1];
454 case GL_NORMAL_MAP_NV
: {
455 const GLfloat
*norm
= normal
->start
;
456 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
457 texcoord
[i
][1] = norm
[1];
462 _mesa_problem(ctx
, "Bad T texgen");
466 if (texUnit
->TexGenEnabled
& R_BIT
) {
468 switch (texUnit
->GenModeR
) {
469 case GL_OBJECT_LINEAR
:
470 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][2]),
471 sizeof(out
->data
[0]), obj
,
472 texUnit
->ObjectPlaneR
);
475 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][2]),
476 sizeof(out
->data
[0]), eye
,
477 texUnit
->EyePlaneR
);
479 case GL_REFLECTION_MAP_NV
:
480 for (i
=0;i
<count
;i
++)
481 texcoord
[i
][2] = f
[i
][2];
483 case GL_NORMAL_MAP_NV
: {
484 const GLfloat
*norm
= normal
->start
;
485 for (i
=0;i
<count
;i
++,STRIDE_F(norm
, normal
->stride
)) {
486 texcoord
[i
][2] = norm
[2];
491 _mesa_problem(ctx
, "Bad R texgen");
495 if (texUnit
->TexGenEnabled
& Q_BIT
) {
496 switch (texUnit
->GenModeQ
) {
497 case GL_OBJECT_LINEAR
:
498 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][3]),
499 sizeof(out
->data
[0]), obj
,
500 texUnit
->ObjectPlaneQ
);
503 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][3]),
504 sizeof(out
->data
[0]), eye
,
505 texUnit
->EyePlaneQ
);
508 _mesa_problem(ctx
, "Bad Q texgen");
515 static GLboolean
run_texgen_stage( GLcontext
*ctx
,
516 struct tnl_pipeline_stage
*stage
)
518 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
519 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA( stage
);
522 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
523 if (ctx
->Texture
._TexGenEnabled
& ENABLE_TEXGEN(i
)) {
524 if (stage
->changed_inputs
& (_TNL_BIT_POS
| _TNL_BIT_NORMAL
| _TNL_BIT_TEX(i
)))
525 store
->TexgenFunc
[i
]( ctx
, store
, i
);
527 VB
->TexCoordPtr
[i
] = &store
->texcoord
[i
];
536 static GLboolean
run_validate_texgen_stage( GLcontext
*ctx
,
537 struct tnl_pipeline_stage
*stage
)
539 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
542 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
543 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
545 if (texUnit
->TexGenEnabled
) {
548 if (texUnit
->TexGenEnabled
& Q_BIT
)
550 else if (texUnit
->TexGenEnabled
& R_BIT
)
552 else if (texUnit
->TexGenEnabled
& T_BIT
)
557 store
->TexgenSize
[i
] = sz
;
558 store
->TexgenHoles
[i
] = (all_bits
[sz
] & ~texUnit
->TexGenEnabled
);
559 store
->TexgenFunc
[i
] = texgen
; /* general solution */
561 /* look for special texgen cases */
562 if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
|R_BIT
)) {
563 if (texUnit
->_GenFlags
== TEXGEN_REFLECTION_MAP_NV
) {
564 store
->TexgenFunc
[i
] = texgen_reflection_map_nv
;
566 else if (texUnit
->_GenFlags
== TEXGEN_NORMAL_MAP_NV
) {
567 store
->TexgenFunc
[i
] = texgen_normal_map_nv
;
570 else if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
) &&
571 texUnit
->_GenFlags
== TEXGEN_SPHERE_MAP
) {
572 store
->TexgenFunc
[i
] = texgen_sphere_map
;
577 stage
->run
= run_texgen_stage
;
578 return stage
->run( ctx
, stage
);
582 static void check_texgen( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
587 if (ctx
->Texture
._TexGenEnabled
&& !ctx
->VertexProgram
.Enabled
) {
591 if (ctx
->Texture
._GenFlags
& (TEXGEN_OBJ_LINEAR
| TEXGEN_NEED_EYE_COORD
))
592 inputs
|= _TNL_BIT_POS
;
594 if (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
)
595 inputs
|= _TNL_BIT_NORMAL
;
597 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
598 if (ctx
->Texture
._TexGenEnabled
& ENABLE_TEXGEN(i
))
600 outputs
|= _TNL_BIT_TEX(i
);
602 /* Need the original input in case it contains a Q coord:
605 inputs
|= _TNL_BIT_TEX(i
);
607 /* Something for Feedback? */
610 if (stage
->privatePtr
)
611 stage
->run
= run_validate_texgen_stage
;
613 stage
->inputs
= inputs
;
614 stage
->outputs
= outputs
;
621 /* Called the first time stage->run() is invoked.
623 static GLboolean
alloc_texgen_data( GLcontext
*ctx
,
624 struct tnl_pipeline_stage
*stage
)
626 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
627 struct texgen_stage_data
*store
;
630 stage
->privatePtr
= CALLOC(sizeof(*store
));
631 store
= TEXGEN_STAGE_DATA(stage
);
635 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
636 _mesa_vector4f_alloc( &store
->texcoord
[i
], 0, VB
->Size
, 32 );
638 store
->tmp_f
= (GLfloat (*)[3]) MALLOC(VB
->Size
* sizeof(GLfloat
) * 3);
639 store
->tmp_m
= (GLfloat
*) MALLOC(VB
->Size
* sizeof(GLfloat
));
641 /* Now validate and run the stage.
643 stage
->run
= run_validate_texgen_stage
;
644 return stage
->run( ctx
, stage
);
648 static void free_texgen_data( struct tnl_pipeline_stage
*stage
)
651 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
655 for (i
= 0 ; i
< MAX_TEXTURE_COORD_UNITS
; i
++)
656 if (store
->texcoord
[i
].data
)
657 _mesa_vector4f_free( &store
->texcoord
[i
] );
660 if (store
->tmp_f
) FREE( store
->tmp_f
);
661 if (store
->tmp_m
) FREE( store
->tmp_m
);
663 stage
->privatePtr
= NULL
;
669 const struct tnl_pipeline_stage _tnl_texgen_stage
=
672 _NEW_TEXTURE
, /* when to call check() */
673 _NEW_TEXTURE
, /* when to invalidate stored data */
674 GL_FALSE
, /* active? */
677 0, /* changed_inputs */
678 NULL
, /* private data */
679 free_texgen_data
, /* destructor */
680 check_texgen
, /* check */
681 alloc_texgen_data
/* run -- initially set to alloc data */