mesa: refactor: move _mesa_init_exec_table() into new api_exec.c file
[mesa.git] / src / mesa / main / state.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
4 *
5 * Copyright (C) 1999-2008 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 * \file state.c
28 * State management.
29 *
30 * This file manages recalculation of derived values in GLcontext.
31 */
32
33
34 #include "glheader.h"
35 #include "mtypes.h"
36 #include "context.h"
37 #include "debug.h"
38 #include "macros.h"
39 #include "ffvertex_prog.h"
40 #include "framebuffer.h"
41 #include "light.h"
42 #include "matrix.h"
43 #include "pixel.h"
44 #include "shader/program.h"
45 #include "state.h"
46 #include "stencil.h"
47 #include "texenvprogram.h"
48 #include "texobj.h"
49 #include "texstate.h"
50
51
52 /**
53 * Update state dependent on vertex arrays.
54 */
55 static void
56 update_arrays( GLcontext *ctx )
57 {
58 GLuint i, min;
59
60 /* find min of _MaxElement values for all enabled arrays */
61
62 /* 0 */
63 if (ctx->VertexProgram._Current
64 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled) {
65 min = ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_POS]._MaxElement;
66 }
67 else if (ctx->Array.ArrayObj->Vertex.Enabled) {
68 min = ctx->Array.ArrayObj->Vertex._MaxElement;
69 }
70 else {
71 /* can't draw anything without vertex positions! */
72 min = 0;
73 }
74
75 /* 1 */
76 if (ctx->VertexProgram._Enabled
77 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT].Enabled) {
78 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT]._MaxElement);
79 }
80 /* no conventional vertex weight array */
81
82 /* 2 */
83 if (ctx->VertexProgram._Enabled
84 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_NORMAL].Enabled) {
85 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_NORMAL]._MaxElement);
86 }
87 else if (ctx->Array.ArrayObj->Normal.Enabled) {
88 min = MIN2(min, ctx->Array.ArrayObj->Normal._MaxElement);
89 }
90
91 /* 3 */
92 if (ctx->VertexProgram._Enabled
93 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_COLOR0].Enabled) {
94 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_COLOR0]._MaxElement);
95 }
96 else if (ctx->Array.ArrayObj->Color.Enabled) {
97 min = MIN2(min, ctx->Array.ArrayObj->Color._MaxElement);
98 }
99
100 /* 4 */
101 if (ctx->VertexProgram._Enabled
102 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_COLOR1].Enabled) {
103 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_COLOR1]._MaxElement);
104 }
105 else if (ctx->Array.ArrayObj->SecondaryColor.Enabled) {
106 min = MIN2(min, ctx->Array.ArrayObj->SecondaryColor._MaxElement);
107 }
108
109 /* 5 */
110 if (ctx->VertexProgram._Enabled
111 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_FOG].Enabled) {
112 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_FOG]._MaxElement);
113 }
114 else if (ctx->Array.ArrayObj->FogCoord.Enabled) {
115 min = MIN2(min, ctx->Array.ArrayObj->FogCoord._MaxElement);
116 }
117
118 /* 6 */
119 if (ctx->VertexProgram._Enabled
120 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX].Enabled) {
121 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX]._MaxElement);
122 }
123 else if (ctx->Array.ArrayObj->Index.Enabled) {
124 min = MIN2(min, ctx->Array.ArrayObj->Index._MaxElement);
125 }
126
127
128 /* 7 */
129 if (ctx->VertexProgram._Enabled
130 && ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG].Enabled) {
131 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG]._MaxElement);
132 }
133
134 /* 8..15 */
135 for (i = VERT_ATTRIB_TEX0; i <= VERT_ATTRIB_TEX7; i++) {
136 if (ctx->VertexProgram._Enabled
137 && ctx->Array.ArrayObj->VertexAttrib[i].Enabled) {
138 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[i]._MaxElement);
139 }
140 else if (i - VERT_ATTRIB_TEX0 < ctx->Const.MaxTextureCoordUnits
141 && ctx->Array.ArrayObj->TexCoord[i - VERT_ATTRIB_TEX0].Enabled) {
142 min = MIN2(min, ctx->Array.ArrayObj->TexCoord[i - VERT_ATTRIB_TEX0]._MaxElement);
143 }
144 }
145
146 /* 16..31 */
147 if (ctx->VertexProgram._Current) {
148 for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++) {
149 if (ctx->Array.ArrayObj->VertexAttrib[i].Enabled) {
150 min = MIN2(min, ctx->Array.ArrayObj->VertexAttrib[i]._MaxElement);
151 }
152 }
153 }
154
155 if (ctx->Array.ArrayObj->EdgeFlag.Enabled) {
156 min = MIN2(min, ctx->Array.ArrayObj->EdgeFlag._MaxElement);
157 }
158
159 /* _MaxElement is one past the last legal array element */
160 ctx->Array._MaxElement = min;
161 }
162
163
164 static void
165 update_program(GLcontext *ctx)
166 {
167 const struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
168 const struct gl_vertex_program *prevVP = ctx->VertexProgram._Current;
169 const struct gl_fragment_program *prevFP = ctx->FragmentProgram._Current;
170
171 /* These _Enabled flags indicate if the program is enabled AND valid. */
172 ctx->VertexProgram._Enabled = ctx->VertexProgram.Enabled
173 && ctx->VertexProgram.Current->Base.Instructions;
174 ctx->FragmentProgram._Enabled = ctx->FragmentProgram.Enabled
175 && ctx->FragmentProgram.Current->Base.Instructions;
176 ctx->ATIFragmentShader._Enabled = ctx->ATIFragmentShader.Enabled
177 && ctx->ATIFragmentShader.Current->Instructions;
178
179 /*
180 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
181 * pointers to the programs that should be enabled/used. These will only
182 * be NULL if we need to use the fixed-function code.
183 *
184 * These programs may come from several sources. The priority is as
185 * follows:
186 * 1. OpenGL 2.0/ARB vertex/fragment shaders
187 * 2. ARB/NV vertex/fragment programs
188 * 3. Programs derived from fixed-function state.
189 *
190 * Note: it's possible for a vertex shader to get used with a fragment
191 * program (and vice versa) here, but in practice that shouldn't ever
192 * come up, or matter.
193 */
194
195 /**
196 ** Fragment program
197 **/
198 #if 1
199 /* XXX get rid of this someday? */
200 ctx->FragmentProgram._Active = GL_FALSE;
201 #endif
202 if (shProg && shProg->LinkStatus && shProg->FragmentProgram) {
203 /* user-defined fragment shader */
204 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
205 shProg->FragmentProgram);
206 }
207 else if (ctx->FragmentProgram._Enabled) {
208 /* use user-defined fragment program */
209 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
210 ctx->FragmentProgram.Current);
211 }
212 else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
213 /* fragment program generated from fixed-function state */
214 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
215 _mesa_get_fixed_func_fragment_program(ctx));
216 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
217 ctx->FragmentProgram._Current);
218
219 /* XXX get rid of this confusing stuff someday? */
220 ctx->FragmentProgram._Active = ctx->FragmentProgram._Enabled;
221 if (ctx->FragmentProgram._UseTexEnvProgram)
222 ctx->FragmentProgram._Active = GL_TRUE;
223 }
224 else {
225 /* no fragment program */
226 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
227 }
228
229 if (ctx->FragmentProgram._Current != prevFP && ctx->Driver.BindProgram) {
230 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
231 (struct gl_program *) ctx->FragmentProgram._Current);
232 }
233
234 /**
235 ** Vertex program
236 **/
237 #if 1
238 /* XXX get rid of this someday? */
239 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, NULL);
240 #endif
241 if (shProg && shProg->LinkStatus && shProg->VertexProgram) {
242 /* user-defined vertex shader */
243 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
244 shProg->VertexProgram);
245 }
246 else if (ctx->VertexProgram._Enabled) {
247 /* use user-defined vertex program */
248 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
249 ctx->VertexProgram.Current);
250 }
251 else if (ctx->VertexProgram._MaintainTnlProgram) {
252 /* vertex program generated from fixed-function state */
253 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
254 _mesa_get_fixed_func_vertex_program(ctx));
255 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram,
256 ctx->VertexProgram._Current);
257 }
258 else {
259 /* no vertex program / used fixed-function code */
260 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
261 }
262
263 if (ctx->VertexProgram._Current != prevVP && ctx->Driver.BindProgram) {
264 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
265 (struct gl_program *) ctx->VertexProgram._Current);
266 }
267 }
268
269
270 static void
271 update_viewport_matrix(GLcontext *ctx)
272 {
273 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
274
275 ASSERT(depthMax > 0);
276
277 /* Compute scale and bias values. This is really driver-specific
278 * and should be maintained elsewhere if at all.
279 * NOTE: RasterPos uses this.
280 */
281 _math_matrix_viewport(&ctx->Viewport._WindowMap,
282 ctx->Viewport.X, ctx->Viewport.Y,
283 ctx->Viewport.Width, ctx->Viewport.Height,
284 ctx->Viewport.Near, ctx->Viewport.Far,
285 depthMax);
286 }
287
288
289 /**
290 * Update derived color/blend/logicop state.
291 */
292 static void
293 update_color(GLcontext *ctx)
294 {
295 /* This is needed to support 1.1's RGB logic ops AND
296 * 1.0's blending logicops.
297 */
298 ctx->Color._LogicOpEnabled = RGBA_LOGICOP_ENABLED(ctx);
299 }
300
301
302
303 /**
304 * Update the ctx->_TriangleCaps bitfield.
305 * XXX that bitfield should really go away someday!
306 * This function must be called after other update_*() functions since
307 * there are dependencies on some other derived values.
308 */
309 static void
310 update_tricaps(GLcontext *ctx, GLbitfield new_state)
311 {
312 ctx->_TriangleCaps = 0;
313
314 /*
315 * Points
316 */
317 if (1/*new_state & _NEW_POINT*/) {
318 if (ctx->Point.SmoothFlag)
319 ctx->_TriangleCaps |= DD_POINT_SMOOTH;
320 if (ctx->Point.Size != 1.0F)
321 ctx->_TriangleCaps |= DD_POINT_SIZE;
322 if (ctx->Point._Attenuated)
323 ctx->_TriangleCaps |= DD_POINT_ATTEN;
324 }
325
326 /*
327 * Lines
328 */
329 if (1/*new_state & _NEW_LINE*/) {
330 if (ctx->Line.SmoothFlag)
331 ctx->_TriangleCaps |= DD_LINE_SMOOTH;
332 if (ctx->Line.StippleFlag)
333 ctx->_TriangleCaps |= DD_LINE_STIPPLE;
334 if (ctx->Line.Width != 1.0)
335 ctx->_TriangleCaps |= DD_LINE_WIDTH;
336 }
337
338 /*
339 * Polygons
340 */
341 if (1/*new_state & _NEW_POLYGON*/) {
342 if (ctx->Polygon.SmoothFlag)
343 ctx->_TriangleCaps |= DD_TRI_SMOOTH;
344 if (ctx->Polygon.StippleFlag)
345 ctx->_TriangleCaps |= DD_TRI_STIPPLE;
346 if (ctx->Polygon.FrontMode != GL_FILL
347 || ctx->Polygon.BackMode != GL_FILL)
348 ctx->_TriangleCaps |= DD_TRI_UNFILLED;
349 if (ctx->Polygon.CullFlag
350 && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
351 ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
352 if (ctx->Polygon.OffsetPoint ||
353 ctx->Polygon.OffsetLine ||
354 ctx->Polygon.OffsetFill)
355 ctx->_TriangleCaps |= DD_TRI_OFFSET;
356 }
357
358 /*
359 * Lighting and shading
360 */
361 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
362 ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
363 if (ctx->Light.ShadeModel == GL_FLAT)
364 ctx->_TriangleCaps |= DD_FLATSHADE;
365 if (NEED_SECONDARY_COLOR(ctx))
366 ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
367
368 /*
369 * Stencil
370 */
371 if (ctx->Stencil._TestTwoSide)
372 ctx->_TriangleCaps |= DD_TRI_TWOSTENCIL;
373 }
374
375
376 /**
377 * Compute derived GL state.
378 * If __GLcontextRec::NewState is non-zero then this function \b must
379 * be called before rendering anything.
380 *
381 * Calls dd_function_table::UpdateState to perform any internal state
382 * management necessary.
383 *
384 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
385 * _mesa_update_buffer_bounds(),
386 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
387 */
388 void
389 _mesa_update_state_locked( GLcontext *ctx )
390 {
391 GLbitfield new_state = ctx->NewState;
392 GLbitfield prog_flags = _NEW_PROGRAM;
393
394 if (MESA_VERBOSE & VERBOSE_STATE)
395 _mesa_print_state("_mesa_update_state", new_state);
396
397 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
398 _mesa_update_modelview_project( ctx, new_state );
399
400 if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
401 _mesa_update_texture( ctx, new_state );
402
403 if (new_state & _NEW_BUFFERS)
404 _mesa_update_framebuffer(ctx);
405
406 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
407 _mesa_update_draw_buffer_bounds( ctx );
408
409 if (new_state & _NEW_LIGHT)
410 _mesa_update_lighting( ctx );
411
412 if (new_state & _NEW_STENCIL)
413 _mesa_update_stencil( ctx );
414
415 if (new_state & _IMAGE_NEW_TRANSFER_STATE)
416 _mesa_update_pixel( ctx, new_state );
417
418 if (new_state & (_NEW_ARRAY | _NEW_PROGRAM))
419 update_arrays( ctx );
420
421 if (new_state & (_NEW_BUFFERS | _NEW_VIEWPORT))
422 update_viewport_matrix(ctx);
423
424 if (new_state & _NEW_COLOR)
425 update_color( ctx );
426
427 if (new_state & (_NEW_POINT | _NEW_LINE | _NEW_POLYGON | _NEW_LIGHT
428 | _NEW_STENCIL | _DD_NEW_SEPARATE_SPECULAR))
429 update_tricaps( ctx, new_state );
430
431 /* ctx->_NeedEyeCoords is now up to date.
432 *
433 * If the truth value of this variable has changed, update for the
434 * new lighting space and recompute the positions of lights and the
435 * normal transform.
436 *
437 * If the lighting space hasn't changed, may still need to recompute
438 * light positions & normal transforms for other reasons.
439 */
440 if (new_state & _MESA_NEW_NEED_EYE_COORDS)
441 _mesa_update_tnl_spaces( ctx, new_state );
442
443 if (ctx->FragmentProgram._MaintainTexEnvProgram) {
444 prog_flags |= (_NEW_TEXTURE | _NEW_FOG | _DD_NEW_SEPARATE_SPECULAR);
445 }
446 if (ctx->VertexProgram._MaintainTnlProgram) {
447 prog_flags |= (_NEW_TEXTURE | _NEW_TEXTURE_MATRIX |
448 _NEW_TRANSFORM | _NEW_POINT |
449 _NEW_FOG | _NEW_LIGHT |
450 _MESA_NEW_NEED_EYE_COORDS);
451 }
452 if (new_state & prog_flags)
453 update_program( ctx );
454
455
456
457 /*
458 * Give the driver a chance to act upon the new_state flags.
459 * The driver might plug in different span functions, for example.
460 * Also, this is where the driver can invalidate the state of any
461 * active modules (such as swrast_setup, swrast, tnl, etc).
462 *
463 * Set ctx->NewState to zero to avoid recursion if
464 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
465 */
466 new_state = ctx->NewState;
467 ctx->NewState = 0;
468 ctx->Driver.UpdateState(ctx, new_state);
469 ctx->Array.NewState = 0;
470 }
471
472
473 /* This is the usual entrypoint for state updates:
474 */
475 void
476 _mesa_update_state( GLcontext *ctx )
477 {
478 _mesa_lock_context_textures(ctx);
479 _mesa_update_state_locked(ctx);
480 _mesa_unlock_context_textures(ctx);
481 }