| 1 |
/* |
| 2 |
* Mesa 3-D graphics library |
| 3 |
* Version: 6.3 |
| 4 |
* |
| 5 |
* Copyright (C) 1999-2005 Brian Paul All Rights Reserved. |
| 6 |
* |
| 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: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included |
| 15 |
* in all copies or substantial portions of the Software. |
| 16 |
* |
| 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. |
| 23 |
*/ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include "glheader.h" |
| 28 |
#include "colormac.h" |
| 29 |
#include "light.h" |
| 30 |
#include "macros.h" |
| 31 |
#include "imports.h" |
| 32 |
#include "simple_list.h" |
| 33 |
#include "mtypes.h" |
| 34 |
|
| 35 |
#include "math/m_translate.h" |
| 36 |
|
| 37 |
#include "t_context.h" |
| 38 |
#include "t_pipeline.h" |
| 39 |
|
| 40 |
#define LIGHT_TWOSIDE 0x1 |
| 41 |
#define LIGHT_MATERIAL 0x2 |
| 42 |
#define MAX_LIGHT_FUNC 0x4 |
| 43 |
|
| 44 |
typedef void (*light_func)( GLcontext *ctx, |
| 45 |
struct vertex_buffer *VB, |
| 46 |
struct tnl_pipeline_stage *stage, |
| 47 |
GLvector4f *input ); |
| 48 |
|
| 49 |
/** |
| 50 |
* Information for updating current material attributes from vertex color, |
| 51 |
* for GL_COLOR_MATERIAL. |
| 52 |
*/ |
| 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 */ |
| 58 |
}; |
| 59 |
|
| 60 |
/** |
| 61 |
* Data private to this pipeline stage. |
| 62 |
*/ |
| 63 |
struct light_stage_data { |
| 64 |
GLvector4f Input; |
| 65 |
GLvector4f LitColor[2]; |
| 66 |
GLvector4f LitSecondary[2]; |
| 67 |
GLvector4f LitIndex[2]; |
| 68 |
light_func *light_func_tab; |
| 69 |
|
| 70 |
struct material_cursor mat[MAT_ATTRIB_MAX]; |
| 71 |
GLuint mat_count; |
| 72 |
GLuint mat_bitmask; |
| 73 |
}; |
| 74 |
|
| 75 |
|
| 76 |
#define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr)) |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* In the case of colormaterial, the effected material attributes |
| 82 |
* should already have been bound to point to the incoming color data, |
| 83 |
* prior to running the pipeline. |
| 84 |
* This function copies the vertex's color to the material attributes |
| 85 |
* which are tracking glColor. |
| 86 |
* It's called per-vertex in the lighting loop. |
| 87 |
*/ |
| 88 |
static void |
| 89 |
update_materials(GLcontext *ctx, struct light_stage_data *store) |
| 90 |
{ |
| 91 |
GLuint i; |
| 92 |
|
| 93 |
for (i = 0 ; i < store->mat_count ; i++) { |
| 94 |
/* update the material */ |
| 95 |
COPY_CLEAN_4V(store->mat[i].current, store->mat[i].size, store->mat[i].ptr); |
| 96 |
/* increment src vertex color pointer */ |
| 97 |
STRIDE_F(store->mat[i].ptr, store->mat[i].stride); |
| 98 |
} |
| 99 |
|
| 100 |
/* recompute derived light/material values */ |
| 101 |
_mesa_update_material( ctx, store->mat_bitmask ); |
| 102 |
/* XXX we should only call this if we're tracking/changing the specular |
| 103 |
* exponent. |
| 104 |
*/ |
| 105 |
_mesa_validate_all_lighting_tables( ctx ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Prepare things prior to running the lighting stage. |
| 111 |
* Return number of material attributes which will track vertex color. |
| 112 |
*/ |
| 113 |
static GLuint |
| 114 |
prepare_materials(GLcontext *ctx, |
| 115 |
struct vertex_buffer *VB, struct light_stage_data *store) |
| 116 |
{ |
| 117 |
GLuint i; |
| 118 |
|
| 119 |
store->mat_count = 0; |
| 120 |
store->mat_bitmask = 0; |
| 121 |
|
| 122 |
/* Examine the ColorMaterialBitmask to determine which materials |
| 123 |
* track vertex color. Override the material attribute's pointer |
| 124 |
* with the color pointer for each one. |
| 125 |
*/ |
| 126 |
if (ctx->Light.ColorMaterialEnabled) { |
| 127 |
const GLuint bitmask = ctx->Light.ColorMaterialBitmask; |
| 128 |
for (i = 0 ; i < MAT_ATTRIB_MAX ; i++) |
| 129 |
if (bitmask & (1<<i)) |
| 130 |
VB->AttribPtr[_TNL_ATTRIB_MAT_FRONT_AMBIENT + i] = VB->ColorPtr[0]; |
| 131 |
} |
| 132 |
|
| 133 |
/* Now, for each material attribute that's tracking vertex color, save |
| 134 |
* some values (ptr, stride, size, current) that we'll need in |
| 135 |
* update_materials(), above, that'll actually copy the vertex color to |
| 136 |
* the material attribute(s). |
| 137 |
*/ |
| 138 |
for (i = _TNL_ATTRIB_MAT_FRONT_AMBIENT ; i < _TNL_ATTRIB_INDEX ; i++) { |
| 139 |
if (VB->AttribPtr[i]->stride) { |
| 140 |
const GLuint j = store->mat_count++; |
| 141 |
const GLuint attr = i - _TNL_ATTRIB_MAT_FRONT_AMBIENT; |
| 142 |
store->mat[j].ptr = VB->AttribPtr[i]->start; |
| 143 |
store->mat[j].stride = VB->AttribPtr[i]->stride; |
| 144 |
store->mat[j].size = VB->AttribPtr[i]->size; |
| 145 |
store->mat[j].current = ctx->Light.Material.Attrib[attr]; |
| 146 |
store->mat_bitmask |= (1<<attr); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/* FIXME: Is this already done? |
| 151 |
*/ |
| 152 |
_mesa_update_material( ctx, ~0 ); |
| 153 |
_mesa_validate_all_lighting_tables( ctx ); |
| 154 |
|
| 155 |
return store->mat_count; |
| 156 |
} |
| 157 |
|
| 158 |
/* Tables for all the shading functions. |
| 159 |
*/ |
| 160 |
static light_func _tnl_light_tab[MAX_LIGHT_FUNC]; |
| 161 |
static light_func _tnl_light_fast_tab[MAX_LIGHT_FUNC]; |
| 162 |
static light_func _tnl_light_fast_single_tab[MAX_LIGHT_FUNC]; |
| 163 |
static light_func _tnl_light_spec_tab[MAX_LIGHT_FUNC]; |
| 164 |
static light_func _tnl_light_ci_tab[MAX_LIGHT_FUNC]; |
| 165 |
|
| 166 |
#define TAG(x) x |
| 167 |
#define IDX (0) |
| 168 |
#include "t_vb_lighttmp.h" |
| 169 |
|
| 170 |
#define TAG(x) x##_twoside |
| 171 |
#define IDX (LIGHT_TWOSIDE) |
| 172 |
#include "t_vb_lighttmp.h" |
| 173 |
|
| 174 |
#define TAG(x) x##_material |
| 175 |
#define IDX (LIGHT_MATERIAL) |
| 176 |
#include "t_vb_lighttmp.h" |
| 177 |
|
| 178 |
#define TAG(x) x##_twoside_material |
| 179 |
#define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL) |
| 180 |
#include "t_vb_lighttmp.h" |
| 181 |
|
| 182 |
|
| 183 |
static void init_lighting_tables( void ) |
| 184 |
{ |
| 185 |
static int done; |
| 186 |
|
| 187 |
if (!done) { |
| 188 |
init_light_tab(); |
| 189 |
init_light_tab_twoside(); |
| 190 |
init_light_tab_material(); |
| 191 |
init_light_tab_twoside_material(); |
| 192 |
done = 1; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
static GLboolean run_lighting( GLcontext *ctx, |
| 198 |
struct tnl_pipeline_stage *stage ) |
| 199 |
{ |
| 200 |
struct light_stage_data *store = LIGHT_STAGE_DATA(stage); |
| 201 |
TNLcontext *tnl = TNL_CONTEXT(ctx); |
| 202 |
struct vertex_buffer *VB = &tnl->vb; |
| 203 |
GLvector4f *input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr; |
| 204 |
GLuint idx; |
| 205 |
|
| 206 |
if (!ctx->Light.Enabled || ctx->VertexProgram._Enabled) |
| 207 |
return GL_TRUE; |
| 208 |
|
| 209 |
/* Make sure we can talk about position x,y and z: |
| 210 |
*/ |
| 211 |
if (input->size <= 2 && input == VB->ObjPtr) { |
| 212 |
|
| 213 |
_math_trans_4f( store->Input.data, |
| 214 |
VB->ObjPtr->data, |
| 215 |
VB->ObjPtr->stride, |
| 216 |
GL_FLOAT, |
| 217 |
VB->ObjPtr->size, |
| 218 |
0, |
| 219 |
VB->Count ); |
| 220 |
|
| 221 |
if (input->size <= 2) { |
| 222 |
/* Clean z. |
| 223 |
*/ |
| 224 |
_mesa_vector4f_clean_elem(&store->Input, VB->Count, 2); |
| 225 |
} |
| 226 |
|
| 227 |
if (input->size <= 1) { |
| 228 |
/* Clean y. |
| 229 |
*/ |
| 230 |
_mesa_vector4f_clean_elem(&store->Input, VB->Count, 1); |
| 231 |
} |
| 232 |
|
| 233 |
input = &store->Input; |
| 234 |
} |
| 235 |
|
| 236 |
idx = 0; |
| 237 |
|
| 238 |
if (prepare_materials( ctx, VB, store )) |
| 239 |
idx |= LIGHT_MATERIAL; |
| 240 |
|
| 241 |
if (ctx->Light.Model.TwoSide) |
| 242 |
idx |= LIGHT_TWOSIDE; |
| 243 |
|
| 244 |
/* The individual functions know about replaying side-effects |
| 245 |
* vs. full re-execution. |
| 246 |
*/ |
| 247 |
store->light_func_tab[idx]( ctx, VB, stage, input ); |
| 248 |
|
| 249 |
VB->AttribPtr[_TNL_ATTRIB_COLOR0] = VB->ColorPtr[0]; |
| 250 |
VB->AttribPtr[_TNL_ATTRIB_COLOR1] = VB->SecondaryColorPtr[0]; |
| 251 |
VB->AttribPtr[_TNL_ATTRIB_INDEX] = VB->IndexPtr[0]; |
| 252 |
|
| 253 |
return GL_TRUE; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/* Called in place of do_lighting when the light table may have changed. |
| 258 |
*/ |
| 259 |
static void validate_lighting( GLcontext *ctx, |
| 260 |
struct tnl_pipeline_stage *stage ) |
| 261 |
{ |
| 262 |
light_func *tab; |
| 263 |
|
| 264 |
if (!ctx->Light.Enabled || ctx->VertexProgram._Enabled) |
| 265 |
return; |
| 266 |
|
| 267 |
if (ctx->Visual.rgbMode) { |
| 268 |
if (ctx->Light._NeedVertices) { |
| 269 |
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR) |
| 270 |
tab = _tnl_light_spec_tab; |
| 271 |
else |
| 272 |
tab = _tnl_light_tab; |
| 273 |
} |
| 274 |
else { |
| 275 |
if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev) |
| 276 |
tab = _tnl_light_fast_single_tab; |
| 277 |
else |
| 278 |
tab = _tnl_light_fast_tab; |
| 279 |
} |
| 280 |
} |
| 281 |
else |
| 282 |
tab = _tnl_light_ci_tab; |
| 283 |
|
| 284 |
|
| 285 |
LIGHT_STAGE_DATA(stage)->light_func_tab = tab; |
| 286 |
|
| 287 |
/* This and the above should only be done on _NEW_LIGHT: |
| 288 |
*/ |
| 289 |
TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx ); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
/* Called the first time stage->run is called. In effect, don't |
| 295 |
* allocate data until the first time the stage is run. |
| 296 |
*/ |
| 297 |
static GLboolean init_lighting( GLcontext *ctx, |
| 298 |
struct tnl_pipeline_stage *stage ) |
| 299 |
{ |
| 300 |
TNLcontext *tnl = TNL_CONTEXT(ctx); |
| 301 |
struct light_stage_data *store; |
| 302 |
GLuint size = tnl->vb.Size; |
| 303 |
|
| 304 |
stage->privatePtr = MALLOC(sizeof(*store)); |
| 305 |
store = LIGHT_STAGE_DATA(stage); |
| 306 |
if (!store) |
| 307 |
return GL_FALSE; |
| 308 |
|
| 309 |
/* Do onetime init. |
| 310 |
*/ |
| 311 |
init_lighting_tables(); |
| 312 |
|
| 313 |
_mesa_vector4f_alloc( &store->Input, 0, size, 32 ); |
| 314 |
_mesa_vector4f_alloc( &store->LitColor[0], 0, size, 32 ); |
| 315 |
_mesa_vector4f_alloc( &store->LitColor[1], 0, size, 32 ); |
| 316 |
_mesa_vector4f_alloc( &store->LitSecondary[0], 0, size, 32 ); |
| 317 |
_mesa_vector4f_alloc( &store->LitSecondary[1], 0, size, 32 ); |
| 318 |
_mesa_vector4f_alloc( &store->LitIndex[0], 0, size, 32 ); |
| 319 |
_mesa_vector4f_alloc( &store->LitIndex[1], 0, size, 32 ); |
| 320 |
|
| 321 |
store->LitColor[0].size = 4; |
| 322 |
store->LitColor[1].size = 4; |
| 323 |
store->LitSecondary[0].size = 3; |
| 324 |
store->LitSecondary[1].size = 3; |
| 325 |
|
| 326 |
store->LitIndex[0].size = 1; |
| 327 |
store->LitIndex[0].stride = sizeof(GLfloat); |
| 328 |
store->LitIndex[1].size = 1; |
| 329 |
store->LitIndex[1].stride = sizeof(GLfloat); |
| 330 |
|
| 331 |
return GL_TRUE; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
static void dtr( struct tnl_pipeline_stage *stage ) |
| 338 |
{ |
| 339 |
struct light_stage_data *store = LIGHT_STAGE_DATA(stage); |
| 340 |
|
| 341 |
if (store) { |
| 342 |
_mesa_vector4f_free( &store->Input ); |
| 343 |
_mesa_vector4f_free( &store->LitColor[0] ); |
| 344 |
_mesa_vector4f_free( &store->LitColor[1] ); |
| 345 |
_mesa_vector4f_free( &store->LitSecondary[0] ); |
| 346 |
_mesa_vector4f_free( &store->LitSecondary[1] ); |
| 347 |
_mesa_vector4f_free( &store->LitIndex[0] ); |
| 348 |
_mesa_vector4f_free( &store->LitIndex[1] ); |
| 349 |
FREE( store ); |
| 350 |
stage->privatePtr = NULL; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
const struct tnl_pipeline_stage _tnl_lighting_stage = |
| 355 |
{ |
| 356 |
"lighting", /* name */ |
| 357 |
NULL, /* private_data */ |
| 358 |
init_lighting, |
| 359 |
dtr, /* destroy */ |
| 360 |
validate_lighting, |
| 361 |
run_lighting |
| 362 |
}; |