2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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.
27 #include "main/glheader.h"
28 #include "main/colormac.h"
29 #include "main/light.h"
30 #include "main/macros.h"
31 #include "main/imports.h"
32 #include "main/simple_list.h"
33 #include "main/mtypes.h"
35 #include "math/m_translate.h"
37 #include "t_context.h"
38 #include "t_pipeline.h"
40 #define LIGHT_TWOSIDE 0x1
41 #define LIGHT_MATERIAL 0x2
42 #define MAX_LIGHT_FUNC 0x4
44 typedef void (*light_func
)( struct gl_context
*ctx
,
45 struct vertex_buffer
*VB
,
46 struct tnl_pipeline_stage
*stage
,
50 * Information for updating current material attributes from vertex color,
51 * for GL_COLOR_MATERIAL.
53 struct material_cursor
{
54 const GLfloat
*ptr
; /* points to src vertex color (in VB array) */
55 GLuint stride
; /* stride to next vertex color (bytes) */
56 GLfloat
*current
; /* points to material attribute to update */
57 GLuint size
; /* vertex/color size: 1, 2, 3 or 4 */
61 * Data private to this pipeline stage.
63 struct light_stage_data
{
65 GLvector4f LitColor
[2];
66 GLvector4f LitSecondary
[2];
67 light_func
*light_func_tab
;
69 struct material_cursor mat
[MAT_ATTRIB_MAX
];
75 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
80 * In the case of colormaterial, the effected material attributes
81 * should already have been bound to point to the incoming color data,
82 * prior to running the pipeline.
83 * This function copies the vertex's color to the material attributes
84 * which are tracking glColor.
85 * It's called per-vertex in the lighting loop.
88 update_materials(struct gl_context
*ctx
, struct light_stage_data
*store
)
92 for (i
= 0 ; i
< store
->mat_count
; i
++) {
93 /* update the material */
94 COPY_CLEAN_4V(store
->mat
[i
].current
, store
->mat
[i
].size
, store
->mat
[i
].ptr
);
95 /* increment src vertex color pointer */
96 STRIDE_F(store
->mat
[i
].ptr
, store
->mat
[i
].stride
);
99 /* recompute derived light/material values */
100 _mesa_update_material( ctx
, store
->mat_bitmask
);
101 /* XXX we should only call this if we're tracking/changing the specular
104 _mesa_validate_all_lighting_tables( ctx
);
109 * Prepare things prior to running the lighting stage.
110 * Return number of material attributes which will track vertex color.
113 prepare_materials(struct gl_context
*ctx
,
114 struct vertex_buffer
*VB
, struct light_stage_data
*store
)
118 store
->mat_count
= 0;
119 store
->mat_bitmask
= 0;
121 /* Examine the ColorMaterialBitmask to determine which materials
122 * track vertex color. Override the material attribute's pointer
123 * with the color pointer for each one.
125 if (ctx
->Light
.ColorMaterialEnabled
) {
126 const GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
127 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
128 if (bitmask
& (1<<i
))
129 VB
->AttribPtr
[_TNL_ATTRIB_MAT_FRONT_AMBIENT
+ i
] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
];
132 /* Now, for each material attribute that's tracking vertex color, save
133 * some values (ptr, stride, size, current) that we'll need in
134 * update_materials(), above, that'll actually copy the vertex color to
135 * the material attribute(s).
137 for (i
= _TNL_FIRST_MAT
; i
<= _TNL_LAST_MAT
; i
++) {
138 if (VB
->AttribPtr
[i
]->stride
) {
139 const GLuint j
= store
->mat_count
++;
140 const GLuint attr
= i
- _TNL_ATTRIB_MAT_FRONT_AMBIENT
;
141 store
->mat
[j
].ptr
= VB
->AttribPtr
[i
]->start
;
142 store
->mat
[j
].stride
= VB
->AttribPtr
[i
]->stride
;
143 store
->mat
[j
].size
= VB
->AttribPtr
[i
]->size
;
144 store
->mat
[j
].current
= ctx
->Light
.Material
.Attrib
[attr
];
145 store
->mat_bitmask
|= (1<<attr
);
149 /* FIXME: Is this already done?
151 _mesa_update_material( ctx
, ~0 );
152 _mesa_validate_all_lighting_tables( ctx
);
154 return store
->mat_count
;
157 /* Tables for all the shading functions.
159 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
160 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
161 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
162 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
166 #include "t_vb_lighttmp.h"
168 #define TAG(x) x##_twoside
169 #define IDX (LIGHT_TWOSIDE)
170 #include "t_vb_lighttmp.h"
172 #define TAG(x) x##_material
173 #define IDX (LIGHT_MATERIAL)
174 #include "t_vb_lighttmp.h"
176 #define TAG(x) x##_twoside_material
177 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
178 #include "t_vb_lighttmp.h"
181 static void init_lighting_tables( void )
187 init_light_tab_twoside();
188 init_light_tab_material();
189 init_light_tab_twoside_material();
195 static GLboolean
run_lighting( struct gl_context
*ctx
,
196 struct tnl_pipeline_stage
*stage
)
198 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
199 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
200 struct vertex_buffer
*VB
= &tnl
->vb
;
201 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->AttribPtr
[_TNL_ATTRIB_POS
];
204 if (!ctx
->Light
.Enabled
|| ctx
->VertexProgram
._Current
)
207 /* Make sure we can talk about position x,y and z:
209 if (input
->size
<= 2 && input
== VB
->AttribPtr
[_TNL_ATTRIB_POS
]) {
211 _math_trans_4f( store
->Input
.data
,
212 VB
->AttribPtr
[_TNL_ATTRIB_POS
]->data
,
213 VB
->AttribPtr
[_TNL_ATTRIB_POS
]->stride
,
215 VB
->AttribPtr
[_TNL_ATTRIB_POS
]->size
,
219 if (input
->size
<= 2) {
222 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 2);
225 if (input
->size
<= 1) {
228 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 1);
231 input
= &store
->Input
;
236 if (prepare_materials( ctx
, VB
, store
))
237 idx
|= LIGHT_MATERIAL
;
239 if (ctx
->Light
.Model
.TwoSide
)
240 idx
|= LIGHT_TWOSIDE
;
242 /* The individual functions know about replaying side-effects
243 * vs. full re-execution.
245 store
->light_func_tab
[idx
]( ctx
, VB
, stage
, input
);
251 /* Called in place of do_lighting when the light table may have changed.
253 static void validate_lighting( struct gl_context
*ctx
,
254 struct tnl_pipeline_stage
*stage
)
258 if (!ctx
->Light
.Enabled
|| ctx
->VertexProgram
._Current
)
261 if (ctx
->Light
._NeedVertices
) {
262 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
263 tab
= _tnl_light_spec_tab
;
265 tab
= _tnl_light_tab
;
268 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
269 tab
= _tnl_light_fast_single_tab
;
271 tab
= _tnl_light_fast_tab
;
275 LIGHT_STAGE_DATA(stage
)->light_func_tab
= tab
;
277 /* This and the above should only be done on _NEW_LIGHT:
279 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
284 /* Called the first time stage->run is called. In effect, don't
285 * allocate data until the first time the stage is run.
287 static GLboolean
init_lighting( struct gl_context
*ctx
,
288 struct tnl_pipeline_stage
*stage
)
290 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
291 struct light_stage_data
*store
;
292 GLuint size
= tnl
->vb
.Size
;
294 stage
->privatePtr
= MALLOC(sizeof(*store
));
295 store
= LIGHT_STAGE_DATA(stage
);
301 init_lighting_tables();
303 _mesa_vector4f_alloc( &store
->Input
, 0, size
, 32 );
304 _mesa_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
305 _mesa_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
306 _mesa_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
307 _mesa_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
309 store
->LitColor
[0].size
= 4;
310 store
->LitColor
[1].size
= 4;
311 store
->LitSecondary
[0].size
= 3;
312 store
->LitSecondary
[1].size
= 3;
320 static void dtr( struct tnl_pipeline_stage
*stage
)
322 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
325 _mesa_vector4f_free( &store
->Input
);
326 _mesa_vector4f_free( &store
->LitColor
[0] );
327 _mesa_vector4f_free( &store
->LitColor
[1] );
328 _mesa_vector4f_free( &store
->LitSecondary
[0] );
329 _mesa_vector4f_free( &store
->LitSecondary
[1] );
331 stage
->privatePtr
= NULL
;
335 const struct tnl_pipeline_stage _tnl_lighting_stage
=
337 "lighting", /* name */
338 NULL
, /* private_data */