summaryrefslogtreecommitdiffstats
path: root/src/mesa/tnl/t_vb_light.c
blob: 53f23ec4e10742a16741f3373a030dbb07c45e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* $Id: t_vb_light.c,v 1.9 2001/02/20 18:28:52 keithw Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.5
 *
 * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */



#include "glheader.h"
#include "colormac.h"
#include "light.h"
#include "macros.h"
#include "mem.h"
#include "mmath.h"
#include "simple_list.h"
#include "mtypes.h"

#include "t_context.h"
#include "t_pipeline.h"

#define LIGHT_FLAGS         0x1	/* must be first */
#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 {
   GLvector4chan LitColor[2];
   GLvector4chan LitSecondary[2];
   GLvector1ui LitIndex[2];
   light_func *light_func_tab;
};

#define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->private))

/* Tables for all the shading functions.
 */
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;

   /* Make sure we can talk about elements 0..2 in the vector we are
    * lighting.
    */
   if (stage->changed_inputs & (VERT_EYE|VERT_OBJ)) {
      if (input->size <= 2) {
	 if (input->flags & VEC_NOT_WRITEABLE) {
	    ASSERT(VB->importable_data & VERT_OBJ);

	    VB->import_data( ctx, VERT_OBJ, VEC_NOT_WRITEABLE );
	    input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;

	    ASSERT((input->flags & VEC_NOT_WRITEABLE) == 0);
	 }

	 gl_vector4f_clean_elem(input, VB->Count, 2);
      }
   }
      
   if (VB->Flag)
      ind = LIGHT_FLAGS;
   else 
      ind = 0;

   /* The individual tabs know about replaying side-effects vs. full
    * re-execution.
    */
   store->light_func_tab[ind]( ctx, VB, stage, input );

   return GL_TRUE;
}


/* Called in place of do_lighting when the light table may have changed.
 */
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];

   /* This and the above should only be done on _NEW_LIGHT:
    */
   gl_validate_all_lighting_tables( ctx );

   /* Now run the stage...
    */
   stage->run = run_lighting;
   return stage->run( ctx, stage );
}

/* Called the first time stage->run is called.  In effect, don't
 * allocate data until the first time the stage is run.
 */
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->private = MALLOC(sizeof(*store));
   store = LIGHT_STAGE_DATA(stage);
   if (!store)
      return GL_FALSE;

   /* Do onetime init.
    */
   init_lighting();

   gl_vector4chan_alloc( &store->LitColor[0], 0, size, 32 );
   gl_vector4chan_alloc( &store->LitColor[1], 0, size, 32 );
   gl_vector4chan_alloc( &store->LitSecondary[0], 0, size, 32 );
   gl_vector4chan_alloc( &store->LitSecondary[1], 0, size, 32 );
   gl_vector1ui_alloc( &store->LitIndex[0], 0, size, 32 );
   gl_vector1ui_alloc( &store->LitIndex[1], 0, size, 32 );

   /* Now validate the stage derived data...
    */
   stage->run = run_validate_lighting;
   return stage->run( ctx, stage );
}



/*
 * Check if lighting is enabled.  If so, configure the pipeline stage's
 * type, inputs, and outputs.
 */
static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
   stage->active = ctx->Light.Enabled;
   if (stage->active) {
      if (stage->private)
	 stage->run = run_validate_lighting;
      stage->inputs = VERT_NORM|VERT_MATERIAL;
      if (ctx->Light._NeedVertices) 
	 stage->inputs |= VERT_EYE; /* effectively, even when lighting in obj */
      if (ctx->Light.ColorMaterialEnabled) 
	 stage->inputs |= VERT_RGBA;

      stage->outputs = VERT_RGBA;
      if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
	 stage->outputs |= VERT_SPEC_RGB;
   }
}


static void dtr( struct gl_pipeline_stage *stage )
{
   struct light_stage_data *store = LIGHT_STAGE_DATA(stage);

   if (store) {
      gl_vector4chan_free( &store->LitColor[0] );      
      gl_vector4chan_free( &store->LitColor[1] );      
      gl_vector4chan_free( &store->LitSecondary[0] );  
      gl_vector4chan_free( &store->LitSecondary[1] );  
      gl_vector1ui_free( &store->LitIndex[0] );      
      gl_vector1ui_free( &store->LitIndex[1] );      
      FREE( store );
      stage->private = 0;
   }
}

const struct gl_pipeline_stage _tnl_lighting_stage = 
{ 
   "lighting",
   _NEW_LIGHT,			/* recheck */
   _NEW_LIGHT|_NEW_MODELVIEW,	/* recalc -- modelview dependency
				 * otherwise not captured by inputs
				 * (which may be VERT_OBJ) */
   0,0,0,			/* active, inputs, outputs */
   0,0,				/* changed_inputs, private_data */
   dtr,				/* destroy */
   check_lighting,		/* check */
   run_init_lighting		/* run -- initially set to ctr */
};