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