#include "glheader.h"
#include "colormac.h"
#include "light.h"
#include "macros.h"
#include "imports.h"
#include "mmath.h"
#include "simple_list.h"
#include "mtypes.h"
#include "math/m_translate.h"
#include "t_context.h"
#include "t_pipeline.h"
#define LIGHT_FLAGS 0x1
#define LIGHT_TWOSIDE 0x2
#define LIGHT_COLORMATERIAL 0x4
#define MAX_LIGHT_FUNC 0x8
typedef void (*light_func)( GLcontext *ctx,
struct vertex_buffer *VB,
struct gl_pipeline_stage *stage,
GLvector4f *input );
struct light_stage_data {
struct gl_client_array FloatColor;
struct gl_client_array LitColor[2];
struct gl_client_array LitSecondary[2];
GLvector1ui LitIndex[2];
light_func *light_func_tab;
};
#define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
static void import_color_material( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
struct gl_client_array *to = &LIGHT_STAGE_DATA(stage)->FloatColor;
struct gl_client_array *from = VB->ColorPtr[0];
GLuint count = VB->Count;
if (!to->Ptr) {
to->Ptr = ALIGN_MALLOC( VB->Size * 4 * sizeof(GLfloat), 32 );
to->Type = GL_FLOAT;
}
if (!from->StrideB) {
to->StrideB = 0;
count = 1;
}
else
to->StrideB = 4 * sizeof(GLfloat);
_math_trans_4fc( (GLfloat (*)[4]) to->Ptr,
from->Ptr,
from->StrideB,
from->Type,
from->Size,
0,
count);
VB->ColorPtr[0] = to;
}
static light_func _tnl_light_tab[MAX_LIGHT_FUNC];
static light_func _tnl_light_fast_tab[MAX_LIGHT_FUNC];
static light_func _tnl_light_fast_single_tab[MAX_LIGHT_FUNC];
static light_func _tnl_light_spec_tab[MAX_LIGHT_FUNC];
static light_func _tnl_light_ci_tab[MAX_LIGHT_FUNC];
#define TAG(x) x
#define IDX (0)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_tw
#define IDX (LIGHT_TWOSIDE)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_fl
#define IDX (LIGHT_FLAGS)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_tw_fl
#define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_cm
#define IDX (LIGHT_COLORMATERIAL)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_tw_cm
#define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_fl_cm
#define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
#include "t_vb_lighttmp.h"
#define TAG(x) x##_tw_fl_cm
#define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
#include "t_vb_lighttmp.h"
static void init_lighting( void )
{
static int done;
if (!done) {
init_light_tab();
init_light_tab_tw();
init_light_tab_fl();
init_light_tab_tw_fl();
init_light_tab_cm();
init_light_tab_tw_cm();
init_light_tab_fl_cm();
init_light_tab_tw_fl_cm();
done = 1;
}
}
static GLboolean run_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct vertex_buffer *VB = &tnl->vb;
GLvector4f *input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
GLuint ind;
if (stage->changed_inputs & (VERT_BIT_EYE|VERT_BIT_POS)) {
if (input->size <= 2) {
if (input->flags & VEC_NOT_WRITEABLE) {
ASSERT(VB->importable_data & VERT_BIT_POS);
VB->import_data( ctx, VERT_BIT_POS, VEC_NOT_WRITEABLE );
input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
ASSERT((input->flags & VEC_NOT_WRITEABLE) == 0);
}
_mesa_vector4f_clean_elem(input, VB->Count, 2);
}
}
if (VB->Flag)
ind = LIGHT_FLAGS;
else
ind = 0;
store->light_func_tab[ind]( ctx, VB, stage, input );
return GL_TRUE;
}
static GLboolean run_validate_lighting( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
GLuint ind = 0;
light_func *tab;
if (ctx->Visual.rgbMode) {
if (ctx->Light._NeedVertices) {
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
tab = _tnl_light_spec_tab;
else
tab = _tnl_light_tab;
}
else {
if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
tab = _tnl_light_fast_single_tab;
else
tab = _tnl_light_fast_tab;
}
}
else
tab = _tnl_light_ci_tab;
if (ctx->Light.ColorMaterialEnabled)
ind |= LIGHT_COLORMATERIAL;
if (ctx->Light.Model.TwoSide)
ind |= LIGHT_TWOSIDE;
LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
stage->run = run_lighting;
return stage->run( ctx, stage );
}
static void alloc_4chan( struct gl_client_array *a, GLuint sz )
{
a->Ptr = ALIGN_MALLOC( sz * sizeof(GLchan) * 4, 32 );
a->Size = 4;
a->Type = CHAN_TYPE;
a->Stride = 0;
a->StrideB = sizeof(GLchan) * 4;
a->Enabled = 0;
a->Flags = 0;
}
static GLboolean run_init_lighting( GLcontext *ctx,
struct gl_pipeline_stage *stage )
{
TNLcontext *tnl = TNL_CONTEXT(ctx);
struct light_stage_data *store;
GLuint size = tnl->vb.Size;
stage->privatePtr = MALLOC(sizeof(*store));
store = LIGHT_STAGE_DATA(stage);
if (!store)
return GL_FALSE;
init_lighting();
store->FloatColor.Ptr = 0;
alloc_4chan( &store->LitColor[0], size );
alloc_4chan( &store->LitColor[1], size );
alloc_4chan( &store->LitSecondary[0], size );
alloc_4chan( &store->LitSecondary[1], size );
_mesa_vector1ui_alloc( &store->LitIndex[0], 0, size, 32 );
_mesa_vector1ui_alloc( &store->LitIndex[1], 0, size, 32 );
stage->run = run_validate_lighting;
return stage->run( ctx, stage );
}
static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
stage->active = ctx->Light.Enabled && !ctx->VertexProgram.Enabled;
if (stage->active) {
if (stage->privatePtr)
stage->run = run_validate_lighting;
stage->inputs = VERT_BIT_NORMAL|VERT_BIT_MATERIAL;
if (ctx->Light._NeedVertices)
stage->inputs |= VERT_BIT_EYE;
if (ctx->Light.ColorMaterialEnabled)
stage->inputs |= VERT_BIT_COLOR0;
stage->outputs = VERT_BIT_COLOR0;
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
stage->outputs |= VERT_BIT_COLOR1;
}
}
static void dtr( struct gl_pipeline_stage *stage )
{
struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
if (store) {
ALIGN_FREE( store->LitColor[0].Ptr );
ALIGN_FREE( store->LitColor[1].Ptr );
ALIGN_FREE( store->LitSecondary[0].Ptr );
ALIGN_FREE( store->LitSecondary[1].Ptr );
if (store->FloatColor.Ptr)
ALIGN_FREE( store->FloatColor.Ptr );
_mesa_vector1ui_free( &store->LitIndex[0] );
_mesa_vector1ui_free( &store->LitIndex[1] );
FREE( store );
stage->privatePtr = 0;
}
}
const struct gl_pipeline_stage _tnl_lighting_stage =
{
"lighting",
_NEW_LIGHT,
_NEW_LIGHT|_NEW_MODELVIEW,
GL_FALSE,
0,
0,
0,
NULL,
dtr,
check_lighting,
run_init_lighting
};