2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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>
30 * Regarding GL_NV_texgen_reflection:
32 * Portions of this software may use or implement intellectual
33 * property owned and licensed by NVIDIA Corporation. NVIDIA disclaims
34 * any and all warranties with respect to such intellectual property,
35 * including any use thereof or modifications thereto.
45 #include "math/m_xform.h"
47 #include "t_context.h"
48 #include "t_pipeline.h"
51 /***********************************************************************
52 * Automatic texture coordinate generation (texgen) code.
56 struct texgen_stage_data
;
58 typedef void (*texgen_func
)( GLcontext
*ctx
,
59 struct texgen_stage_data
*store
,
63 struct texgen_stage_data
{
65 /* Per-texunit derived state.
67 GLuint TexgenSize
[MAX_TEXTURE_COORD_UNITS
];
68 GLuint TexgenHoles
[MAX_TEXTURE_COORD_UNITS
];
69 texgen_func TexgenFunc
[MAX_TEXTURE_COORD_UNITS
];
71 /* Temporary values used in texgen.
76 /* Buffered outputs of the stage.
78 GLvector4f texcoord
[MAX_TEXTURE_COORD_UNITS
];
82 #define TEXGEN_STAGE_DATA(stage) ((struct texgen_stage_data *)stage->privatePtr)
86 static GLuint all_bits
[5] = {
94 #define VEC_SIZE_FLAGS (VEC_SIZE_1|VEC_SIZE_2|VEC_SIZE_3|VEC_SIZE_4)
96 #define TEXGEN_NEED_M (TEXGEN_SPHERE_MAP)
97 #define TEXGEN_NEED_F (TEXGEN_SPHERE_MAP | \
98 TEXGEN_REFLECTION_MAP_NV)
102 static void build_m3( GLfloat f
[][3], GLfloat m
[],
103 const GLvector4f
*normal
,
104 const GLvector4f
*eye
)
106 GLuint stride
= eye
->stride
;
107 GLfloat
*coord
= (GLfloat
*)eye
->start
;
108 GLuint count
= eye
->count
;
109 const GLfloat
*norm
= normal
->start
;
112 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
113 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
116 two_nu
= 2.0F
* DOT3(norm
,u
);
117 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
118 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
119 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
120 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
122 m
[i
] = 0.5F
* _mesa_inv_sqrtf(m
[i
]);
129 static void build_m2( GLfloat f
[][3], GLfloat m
[],
130 const GLvector4f
*normal
,
131 const GLvector4f
*eye
)
133 GLuint stride
= eye
->stride
;
134 GLfloat
*coord
= eye
->start
;
135 GLuint count
= eye
->count
;
137 GLfloat
*norm
= normal
->start
;
140 for (i
=0;i
<count
;i
++,STRIDE_F(coord
,stride
),STRIDE_F(norm
,normal
->stride
)) {
141 GLfloat u
[3], two_nu
, fx
, fy
, fz
;
145 two_nu
= 2.0F
* DOT3(norm
,u
);
146 fx
= f
[i
][0] = u
[0] - norm
[0] * two_nu
;
147 fy
= f
[i
][1] = u
[1] - norm
[1] * two_nu
;
148 fz
= f
[i
][2] = u
[2] - norm
[2] * two_nu
;
149 m
[i
] = fx
* fx
+ fy
* fy
+ (fz
+ 1.0F
) * (fz
+ 1.0F
);
151 m
[i
] = 0.5F
* _mesa_inv_sqrtf(m
[i
]);
158 typedef void (*build_m_func
)( GLfloat f
[][3],
160 const GLvector4f
*normal
,
161 const GLvector4f
*eye
);
164 static build_m_func build_m_tab
[5] = {
173 /* This is unusual in that we respect the stride of the output vector
174 * (f). This allows us to pass in either a texcoord vector4f, or a
175 * temporary vector3f.
177 static void build_f3( GLfloat
*f
,
179 const GLvector4f
*normal
,
180 const GLvector4f
*eye
)
182 GLuint stride
= eye
->stride
;
183 GLfloat
*coord
= eye
->start
;
184 GLuint count
= eye
->count
;
186 GLfloat
*norm
= normal
->start
;
189 for (i
=0;i
<count
;i
++) {
190 GLfloat u
[3], two_nu
;
193 two_nu
= 2.0F
* DOT3(norm
,u
);
194 f
[0] = u
[0] - norm
[0] * two_nu
;
195 f
[1] = u
[1] - norm
[1] * two_nu
;
196 f
[2] = u
[2] - norm
[2] * two_nu
;
197 STRIDE_F(coord
,stride
);
199 STRIDE_F(norm
, normal
->stride
);
204 static void build_f2( GLfloat
*f
,
206 const GLvector4f
*normal
,
207 const GLvector4f
*eye
)
209 GLuint stride
= eye
->stride
;
210 GLfloat
*coord
= eye
->start
;
211 GLuint count
= eye
->count
;
212 GLfloat
*norm
= normal
->start
;
215 for (i
=0;i
<count
;i
++) {
217 GLfloat u
[3], two_nu
;
221 two_nu
= 2.0F
* DOT3(norm
,u
);
222 f
[0] = u
[0] - norm
[0] * two_nu
;
223 f
[1] = u
[1] - norm
[1] * two_nu
;
224 f
[2] = u
[2] - norm
[2] * two_nu
;
226 STRIDE_F(coord
,stride
);
228 STRIDE_F(norm
, normal
->stride
);
232 typedef void (*build_f_func
)( GLfloat
*f
,
234 const GLvector4f
*normal_vec
,
235 const GLvector4f
*eye
);
239 /* Just treat 4-vectors as 3-vectors.
241 static build_f_func build_f_tab
[5] = {
250 /* Special case texgen functions.
252 static void texgen_reflection_map_nv( GLcontext
*ctx
,
253 struct texgen_stage_data
*store
,
256 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
257 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
258 GLvector4f
*out
= &store
->texcoord
[unit
];
260 build_f_tab
[VB
->EyePtr
->size
]( out
->start
,
266 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
267 out
->count
= in
->count
;
268 out
->size
= MAX2(in
->size
, 3);
270 _mesa_copy_tab
[0x8]( out
, in
);
273 out
->flags
|= VEC_SIZE_3
;
275 out
->count
= in
->count
;
282 static void texgen_normal_map_nv( GLcontext
*ctx
,
283 struct texgen_stage_data
*store
,
286 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
287 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
288 GLvector4f
*out
= &store
->texcoord
[unit
];
289 GLvector4f
*normal
= VB
->NormalPtr
;
290 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->start
;
291 GLuint count
= VB
->Count
;
293 const GLfloat
*norm
= normal
->start
;
295 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
296 texcoord
[i
][0] = norm
[0];
297 texcoord
[i
][1] = norm
[1];
298 texcoord
[i
][2] = norm
[2];
303 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_3
;
304 out
->count
= in
->count
;
305 out
->size
= MAX2(in
->size
, 3);
307 _mesa_copy_tab
[0x8]( out
, in
);
310 out
->flags
|= VEC_SIZE_3
;
312 out
->count
= in
->count
;
317 static void texgen_sphere_map( GLcontext
*ctx
,
318 struct texgen_stage_data
*store
,
321 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
322 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
323 GLvector4f
*out
= &store
->texcoord
[unit
];
324 GLfloat (*texcoord
)[4] = (GLfloat (*)[4]) out
->start
;
325 GLuint count
= VB
->Count
;
327 GLfloat (*f
)[3] = store
->tmp_f
;
328 GLfloat
*m
= store
->tmp_m
;
330 (build_m_tab
[VB
->EyePtr
->size
])( store
->tmp_f
,
335 for (i
=0;i
<count
;i
++) {
336 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
337 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
341 out
->size
= MAX2(in
->size
,2);
342 out
->count
= in
->count
;
343 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | VEC_SIZE_2
;
345 _mesa_copy_tab
[all_bits
[in
->size
] & ~0x3]( out
, in
);
348 out
->flags
|= VEC_SIZE_2
;
349 out
->count
= in
->count
;
355 static void texgen( GLcontext
*ctx
,
356 struct texgen_stage_data
*store
,
359 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
360 struct vertex_buffer
*VB
= &tnl
->vb
;
361 GLvector4f
*in
= VB
->TexCoordPtr
[unit
];
362 GLvector4f
*out
= &store
->texcoord
[unit
];
363 const struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[unit
];
364 const GLvector4f
*obj
= VB
->ObjPtr
;
365 const GLvector4f
*eye
= VB
->EyePtr
;
366 const GLvector4f
*normal
= VB
->NormalPtr
;
367 const GLfloat
*m
= store
->tmp_m
;
368 const GLuint count
= VB
->Count
;
369 GLfloat (*texcoord
)[4] = (GLfloat (*)[4])out
->data
;
370 GLfloat (*f
)[3] = store
->tmp_f
;
373 if (texUnit
->_GenFlags
& TEXGEN_NEED_M
) {
374 build_m_tab
[eye
->size
]( store
->tmp_f
, store
->tmp_m
, normal
, eye
);
375 } else if (texUnit
->_GenFlags
& TEXGEN_NEED_F
) {
376 build_f_tab
[eye
->size
]( (GLfloat
*)store
->tmp_f
, 3, normal
, eye
);
382 in
->count
= VB
->Count
;
384 out
->size
= store
->TexgenSize
[unit
];
385 out
->flags
|= texUnit
->TexGenEnabled
;
386 out
->count
= VB
->Count
;
387 holes
= store
->TexgenHoles
[unit
];
390 GLuint copy
= (all_bits
[in
->size
] & ~texUnit
->TexGenEnabled
);
392 _mesa_copy_tab
[copy
]( out
, in
);
394 out
->size
= MAX2(in
->size
, store
->TexgenSize
[unit
]);
395 out
->flags
|= (in
->flags
& VEC_SIZE_FLAGS
) | texUnit
->TexGenEnabled
;
396 out
->count
= in
->count
;
398 holes
= ~all_bits
[in
->size
] & store
->TexgenHoles
[unit
];
402 if (holes
& VEC_DIRTY_3
) _mesa_vector4f_clean_elem(out
, count
, 3);
403 if (holes
& VEC_DIRTY_2
) _mesa_vector4f_clean_elem(out
, count
, 2);
404 if (holes
& VEC_DIRTY_1
) _mesa_vector4f_clean_elem(out
, count
, 1);
405 if (holes
& VEC_DIRTY_0
) _mesa_vector4f_clean_elem(out
, count
, 0);
408 if (texUnit
->TexGenEnabled
& S_BIT
) {
410 switch (texUnit
->GenModeS
) {
411 case GL_OBJECT_LINEAR
:
412 _mesa_dotprod_tab
[obj
->size
]( (GLfloat
*)out
->data
,
413 sizeof(out
->data
[0]), obj
,
414 texUnit
->ObjectPlaneS
);
417 _mesa_dotprod_tab
[eye
->size
]( (GLfloat
*)out
->data
,
418 sizeof(out
->data
[0]), eye
,
419 texUnit
->EyePlaneS
);
422 for (i
= 0; i
< count
; i
++)
423 texcoord
[i
][0] = f
[i
][0] * m
[i
] + 0.5F
;
425 case GL_REFLECTION_MAP_NV
:
426 for (i
=0;i
<count
;i
++)
427 texcoord
[i
][0] = f
[i
][0];
429 case GL_NORMAL_MAP_NV
: {
430 const GLfloat
*norm
= normal
->start
;
431 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
432 texcoord
[i
][0] = norm
[0];
437 _mesa_problem(ctx
, "Bad S texgen");
441 if (texUnit
->TexGenEnabled
& T_BIT
) {
443 switch (texUnit
->GenModeT
) {
444 case GL_OBJECT_LINEAR
:
445 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][1]),
446 sizeof(out
->data
[0]), obj
,
447 texUnit
->ObjectPlaneT
);
450 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][1]),
451 sizeof(out
->data
[0]), eye
,
452 texUnit
->EyePlaneT
);
455 for (i
= 0; i
< count
; i
++)
456 texcoord
[i
][1] = f
[i
][1] * m
[i
] + 0.5F
;
458 case GL_REFLECTION_MAP_NV
:
459 for (i
=0;i
<count
;i
++)
460 texcoord
[i
][1] = f
[i
][1];
462 case GL_NORMAL_MAP_NV
: {
463 const GLfloat
*norm
= normal
->start
;
464 for (i
=0;i
<count
;i
++, STRIDE_F(norm
, normal
->stride
)) {
465 texcoord
[i
][1] = norm
[1];
470 _mesa_problem(ctx
, "Bad T texgen");
474 if (texUnit
->TexGenEnabled
& R_BIT
) {
476 switch (texUnit
->GenModeR
) {
477 case GL_OBJECT_LINEAR
:
478 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][2]),
479 sizeof(out
->data
[0]), obj
,
480 texUnit
->ObjectPlaneR
);
483 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][2]),
484 sizeof(out
->data
[0]), eye
,
485 texUnit
->EyePlaneR
);
487 case GL_REFLECTION_MAP_NV
:
488 for (i
=0;i
<count
;i
++)
489 texcoord
[i
][2] = f
[i
][2];
491 case GL_NORMAL_MAP_NV
: {
492 const GLfloat
*norm
= normal
->start
;
493 for (i
=0;i
<count
;i
++,STRIDE_F(norm
, normal
->stride
)) {
494 texcoord
[i
][2] = norm
[2];
499 _mesa_problem(ctx
, "Bad R texgen");
503 if (texUnit
->TexGenEnabled
& Q_BIT
) {
504 switch (texUnit
->GenModeQ
) {
505 case GL_OBJECT_LINEAR
:
506 _mesa_dotprod_tab
[obj
->size
]( &(out
->data
[0][3]),
507 sizeof(out
->data
[0]), obj
,
508 texUnit
->ObjectPlaneQ
);
511 _mesa_dotprod_tab
[eye
->size
]( &(out
->data
[0][3]),
512 sizeof(out
->data
[0]), eye
,
513 texUnit
->EyePlaneQ
);
516 _mesa_problem(ctx
, "Bad Q texgen");
523 static GLboolean
run_texgen_stage( GLcontext
*ctx
,
524 struct tnl_pipeline_stage
*stage
)
526 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
527 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA( stage
);
530 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
531 if (ctx
->Texture
._TexGenEnabled
& ENABLE_TEXGEN(i
)) {
532 if (stage
->changed_inputs
& (_TNL_BIT_POS
| _TNL_BIT_NORMAL
| _TNL_BIT_TEX(i
)))
533 store
->TexgenFunc
[i
]( ctx
, store
, i
);
535 VB
->AttribPtr
[VERT_ATTRIB_TEX0
+i
] =
536 VB
->TexCoordPtr
[i
] = &store
->texcoord
[i
];
545 static GLboolean
run_validate_texgen_stage( GLcontext
*ctx
,
546 struct tnl_pipeline_stage
*stage
)
548 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
551 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
552 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[i
];
554 if (texUnit
->TexGenEnabled
) {
557 if (texUnit
->TexGenEnabled
& Q_BIT
)
559 else if (texUnit
->TexGenEnabled
& R_BIT
)
561 else if (texUnit
->TexGenEnabled
& T_BIT
)
566 store
->TexgenSize
[i
] = sz
;
567 store
->TexgenHoles
[i
] = (all_bits
[sz
] & ~texUnit
->TexGenEnabled
);
568 store
->TexgenFunc
[i
] = texgen
; /* general solution */
570 /* look for special texgen cases */
571 if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
|R_BIT
)) {
572 if (texUnit
->_GenFlags
== TEXGEN_REFLECTION_MAP_NV
) {
573 store
->TexgenFunc
[i
] = texgen_reflection_map_nv
;
575 else if (texUnit
->_GenFlags
== TEXGEN_NORMAL_MAP_NV
) {
576 store
->TexgenFunc
[i
] = texgen_normal_map_nv
;
579 else if (texUnit
->TexGenEnabled
== (S_BIT
|T_BIT
) &&
580 texUnit
->_GenFlags
== TEXGEN_SPHERE_MAP
) {
581 store
->TexgenFunc
[i
] = texgen_sphere_map
;
586 stage
->run
= run_texgen_stage
;
587 return stage
->run( ctx
, stage
);
591 static void check_texgen( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
596 if (ctx
->Texture
._TexGenEnabled
&& !ctx
->VertexProgram
._Enabled
) {
600 if (ctx
->Texture
._GenFlags
& (TEXGEN_OBJ_LINEAR
| TEXGEN_NEED_EYE_COORD
))
601 inputs
|= _TNL_BIT_POS
;
603 if (ctx
->Texture
._GenFlags
& TEXGEN_NEED_NORMALS
)
604 inputs
|= _TNL_BIT_NORMAL
;
606 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
607 if (ctx
->Texture
._TexGenEnabled
& ENABLE_TEXGEN(i
))
609 outputs
|= _TNL_BIT_TEX(i
);
611 /* Need the original input in case it contains a Q coord:
614 inputs
|= _TNL_BIT_TEX(i
);
616 /* Something for Feedback? */
619 if (stage
->privatePtr
)
620 stage
->run
= run_validate_texgen_stage
;
622 stage
->inputs
= inputs
;
623 stage
->outputs
= outputs
;
630 /* Called the first time stage->run() is invoked.
632 static GLboolean
alloc_texgen_data( GLcontext
*ctx
,
633 struct tnl_pipeline_stage
*stage
)
635 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
636 struct texgen_stage_data
*store
;
639 stage
->privatePtr
= CALLOC(sizeof(*store
));
640 store
= TEXGEN_STAGE_DATA(stage
);
644 for (i
= 0 ; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++)
645 _mesa_vector4f_alloc( &store
->texcoord
[i
], 0, VB
->Size
, 32 );
647 store
->tmp_f
= (GLfloat (*)[3]) MALLOC(VB
->Size
* sizeof(GLfloat
) * 3);
648 store
->tmp_m
= (GLfloat
*) MALLOC(VB
->Size
* sizeof(GLfloat
));
650 /* Now validate and run the stage.
652 stage
->run
= run_validate_texgen_stage
;
653 return stage
->run( ctx
, stage
);
657 static void free_texgen_data( struct tnl_pipeline_stage
*stage
)
660 struct texgen_stage_data
*store
= TEXGEN_STAGE_DATA(stage
);
664 for (i
= 0 ; i
< MAX_TEXTURE_COORD_UNITS
; i
++)
665 if (store
->texcoord
[i
].data
)
666 _mesa_vector4f_free( &store
->texcoord
[i
] );
669 if (store
->tmp_f
) FREE( store
->tmp_f
);
670 if (store
->tmp_m
) FREE( store
->tmp_m
);
672 stage
->privatePtr
= NULL
;
678 const struct tnl_pipeline_stage _tnl_texgen_stage
=
681 _NEW_TEXTURE
|_NEW_PROGRAM
, /* when to call check() */
682 _NEW_TEXTURE
, /* when to invalidate stored data */
683 GL_FALSE
, /* active? */
686 0, /* changed_inputs */
687 NULL
, /* private data */
688 free_texgen_data
, /* destructor */
689 check_texgen
, /* check */
690 alloc_texgen_data
/* run -- initially set to alloc data */