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