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