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