59a4e161bff632b3f866bb360e1a0a8189ca4e5d
[mesa.git] / src / mesa / main / state.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.3
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 struct gl_context.
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 "program/program.h"
45 #include "program/prog_parameter.h"
46 #include "state.h"
47 #include "stencil.h"
48 #include "texenvprogram.h"
49 #include "texobj.h"
50 #include "texstate.h"
51 #include "varray.h"
52
53
54 static void
55 update_separate_specular(struct gl_context *ctx)
56 {
57 if (_mesa_need_secondary_color(ctx))
58 ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
59 else
60 ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
61 }
62
63
64 /**
65 * Helper for update_arrays().
66 * \return min(current min, array->_MaxElement).
67 */
68 static GLuint
69 update_min(GLuint min, struct gl_client_array *array)
70 {
71 _mesa_update_array_max_element(array);
72 return MIN2(min, array->_MaxElement);
73 }
74
75
76 /**
77 * Update ctx->Array._MaxElement (the max legal index into all enabled arrays).
78 * Need to do this upon new array state or new buffer object state.
79 */
80 static void
81 update_arrays( struct gl_context *ctx )
82 {
83 struct gl_array_object *arrayObj = ctx->Array.ArrayObj;
84 GLuint i, min = ~0;
85
86 /* find min of _MaxElement values for all enabled arrays */
87
88 /* 0 */
89 if (ctx->VertexProgram._Current
90 && arrayObj->VertexAttrib[VERT_ATTRIB_POS].Enabled) {
91 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_POS]);
92 }
93 else if (arrayObj->Vertex.Enabled) {
94 min = update_min(min, &arrayObj->Vertex);
95 }
96
97 /* 1 */
98 if (ctx->VertexProgram._Enabled
99 && arrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT].Enabled) {
100 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_WEIGHT]);
101 }
102 /* no conventional vertex weight array */
103
104 /* 2 */
105 if (ctx->VertexProgram._Enabled
106 && arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL].Enabled) {
107 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_NORMAL]);
108 }
109 else if (arrayObj->Normal.Enabled) {
110 min = update_min(min, &arrayObj->Normal);
111 }
112
113 /* 3 */
114 if (ctx->VertexProgram._Enabled
115 && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0].Enabled) {
116 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR0]);
117 }
118 else if (arrayObj->Color.Enabled) {
119 min = update_min(min, &arrayObj->Color);
120 }
121
122 /* 4 */
123 if (ctx->VertexProgram._Enabled
124 && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR1].Enabled) {
125 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR1]);
126 }
127 else if (arrayObj->SecondaryColor.Enabled) {
128 min = update_min(min, &arrayObj->SecondaryColor);
129 }
130
131 /* 5 */
132 if (ctx->VertexProgram._Enabled
133 && arrayObj->VertexAttrib[VERT_ATTRIB_FOG].Enabled) {
134 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_FOG]);
135 }
136 else if (arrayObj->FogCoord.Enabled) {
137 min = update_min(min, &arrayObj->FogCoord);
138 }
139
140 /* 6 */
141 if (ctx->VertexProgram._Enabled
142 && arrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX].Enabled) {
143 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_COLOR_INDEX]);
144 }
145 else if (arrayObj->Index.Enabled) {
146 min = update_min(min, &arrayObj->Index);
147 }
148
149 /* 7 */
150 if (ctx->VertexProgram._Enabled
151 && arrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG].Enabled) {
152 min = update_min(min, &arrayObj->VertexAttrib[VERT_ATTRIB_EDGEFLAG]);
153 }
154
155 /* 8..15 */
156 for (i = VERT_ATTRIB_TEX0; i <= VERT_ATTRIB_TEX7; i++) {
157 if (ctx->VertexProgram._Enabled
158 && arrayObj->VertexAttrib[i].Enabled) {
159 min = update_min(min, &arrayObj->VertexAttrib[i]);
160 }
161 else if (i - VERT_ATTRIB_TEX0 < ctx->Const.MaxTextureCoordUnits
162 && arrayObj->TexCoord[i - VERT_ATTRIB_TEX0].Enabled) {
163 min = update_min(min, &arrayObj->TexCoord[i - VERT_ATTRIB_TEX0]);
164 }
165 }
166
167 /* 16..31 */
168 if (ctx->VertexProgram._Current) {
169 for (i = 0; i < Elements(arrayObj->VertexAttrib); i++) {
170 if (arrayObj->VertexAttrib[i].Enabled) {
171 min = update_min(min, &arrayObj->VertexAttrib[i]);
172 }
173 }
174 }
175
176 if (arrayObj->EdgeFlag.Enabled) {
177 min = update_min(min, &arrayObj->EdgeFlag);
178 }
179
180 /* _MaxElement is one past the last legal array element */
181 arrayObj->_MaxElement = min;
182 }
183
184
185 /**
186 * Update the following fields:
187 * ctx->VertexProgram._Enabled
188 * ctx->FragmentProgram._Enabled
189 * ctx->ATIFragmentShader._Enabled
190 * This needs to be done before texture state validation.
191 */
192 static void
193 update_program_enables(struct gl_context *ctx)
194 {
195 /* These _Enabled flags indicate if the program is enabled AND valid. */
196 ctx->VertexProgram._Enabled = ctx->VertexProgram.Enabled
197 && ctx->VertexProgram.Current->Base.Instructions;
198 ctx->FragmentProgram._Enabled = ctx->FragmentProgram.Enabled
199 && ctx->FragmentProgram.Current->Base.Instructions;
200 ctx->ATIFragmentShader._Enabled = ctx->ATIFragmentShader.Enabled
201 && ctx->ATIFragmentShader.Current->Instructions[0];
202 }
203
204
205 /**
206 * Update the ctx->Vertex/Geometry/FragmentProgram._Current pointers to point
207 * to the current/active programs. Then call ctx->Driver.BindProgram() to
208 * tell the driver which programs to use.
209 *
210 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
211 * programs or programs derived from fixed-function state.
212 *
213 * This function needs to be called after texture state validation in case
214 * we're generating a fragment program from fixed-function texture state.
215 *
216 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
217 * or fragment program is being used.
218 */
219 static GLbitfield
220 update_program(struct gl_context *ctx)
221 {
222 const struct gl_shader_program *vsProg = ctx->Shader.CurrentVertexProgram;
223 const struct gl_shader_program *gsProg = ctx->Shader.CurrentGeometryProgram;
224 const struct gl_shader_program *fsProg = ctx->Shader.CurrentFragmentProgram;
225 const struct gl_vertex_program *prevVP = ctx->VertexProgram._Current;
226 const struct gl_fragment_program *prevFP = ctx->FragmentProgram._Current;
227 const struct gl_geometry_program *prevGP = ctx->GeometryProgram._Current;
228 GLbitfield new_state = 0x0;
229
230 /*
231 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
232 * pointers to the programs that should be used for rendering. If either
233 * is NULL, use fixed-function code paths.
234 *
235 * These programs may come from several sources. The priority is as
236 * follows:
237 * 1. OpenGL 2.0/ARB vertex/fragment shaders
238 * 2. ARB/NV vertex/fragment programs
239 * 3. Programs derived from fixed-function state.
240 *
241 * Note: it's possible for a vertex shader to get used with a fragment
242 * program (and vice versa) here, but in practice that shouldn't ever
243 * come up, or matter.
244 */
245
246 if (fsProg && fsProg->LinkStatus && fsProg->FragmentProgram) {
247 /* Use GLSL fragment shader */
248 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
249 fsProg->FragmentProgram);
250 }
251 else if (ctx->FragmentProgram._Enabled) {
252 /* Use user-defined fragment program */
253 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
254 ctx->FragmentProgram.Current);
255 }
256 else if (ctx->FragmentProgram._MaintainTexEnvProgram) {
257 /* Use fragment program generated from fixed-function state */
258 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current,
259 _mesa_get_fixed_func_fragment_program(ctx));
260 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._TexEnvProgram,
261 ctx->FragmentProgram._Current);
262 }
263 else {
264 /* No fragment program */
265 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram._Current, NULL);
266 }
267
268 if (gsProg && gsProg->LinkStatus && gsProg->GeometryProgram) {
269 /* Use GLSL geometry shader */
270 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current,
271 gsProg->GeometryProgram);
272 } else {
273 /* No geometry program */
274 _mesa_reference_geomprog(ctx, &ctx->GeometryProgram._Current, NULL);
275 }
276
277 /* Examine vertex program after fragment program as
278 * _mesa_get_fixed_func_vertex_program() needs to know active
279 * fragprog inputs.
280 */
281 if (vsProg && vsProg->LinkStatus && vsProg->VertexProgram) {
282 /* Use GLSL vertex shader */
283 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
284 vsProg->VertexProgram);
285 }
286 else if (ctx->VertexProgram._Enabled) {
287 /* Use user-defined vertex program */
288 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
289 ctx->VertexProgram.Current);
290 }
291 else if (ctx->VertexProgram._MaintainTnlProgram) {
292 /* Use vertex program generated from fixed-function state */
293 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current,
294 _mesa_get_fixed_func_vertex_program(ctx));
295 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram,
296 ctx->VertexProgram._Current);
297 }
298 else {
299 /* no vertex program */
300 _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, NULL);
301 }
302
303 /* Let the driver know what's happening:
304 */
305 if (ctx->FragmentProgram._Current != prevFP) {
306 new_state |= _NEW_PROGRAM;
307 if (ctx->Driver.BindProgram) {
308 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
309 (struct gl_program *) ctx->FragmentProgram._Current);
310 }
311 }
312
313 if (ctx->GeometryProgram._Current != prevGP) {
314 new_state |= _NEW_PROGRAM;
315 if (ctx->Driver.BindProgram) {
316 ctx->Driver.BindProgram(ctx, MESA_GEOMETRY_PROGRAM,
317 (struct gl_program *) ctx->GeometryProgram._Current);
318 }
319 }
320
321 if (ctx->VertexProgram._Current != prevVP) {
322 new_state |= _NEW_PROGRAM;
323 if (ctx->Driver.BindProgram) {
324 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
325 (struct gl_program *) ctx->VertexProgram._Current);
326 }
327 }
328
329 return new_state;
330 }
331
332
333 /**
334 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
335 */
336 static GLbitfield
337 update_program_constants(struct gl_context *ctx)
338 {
339 GLbitfield new_state = 0x0;
340
341 if (ctx->FragmentProgram._Current) {
342 const struct gl_program_parameter_list *params =
343 ctx->FragmentProgram._Current->Base.Parameters;
344 if (params && params->StateFlags & ctx->NewState) {
345 new_state |= _NEW_PROGRAM_CONSTANTS;
346 }
347 }
348
349 if (ctx->GeometryProgram._Current) {
350 const struct gl_program_parameter_list *params =
351 ctx->GeometryProgram._Current->Base.Parameters;
352 /*FIXME: StateFlags is always 0 because we have unnamed constant
353 * not state changes */
354 if (params /*&& params->StateFlags & ctx->NewState*/) {
355 new_state |= _NEW_PROGRAM_CONSTANTS;
356 }
357 }
358
359 if (ctx->VertexProgram._Current) {
360 const struct gl_program_parameter_list *params =
361 ctx->VertexProgram._Current->Base.Parameters;
362 if (params && params->StateFlags & ctx->NewState) {
363 new_state |= _NEW_PROGRAM_CONSTANTS;
364 }
365 }
366
367 return new_state;
368 }
369
370
371
372
373 static void
374 update_viewport_matrix(struct gl_context *ctx)
375 {
376 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF;
377
378 ASSERT(depthMax > 0);
379
380 /* Compute scale and bias values. This is really driver-specific
381 * and should be maintained elsewhere if at all.
382 * NOTE: RasterPos uses this.
383 */
384 _math_matrix_viewport(&ctx->Viewport._WindowMap,
385 ctx->Viewport.X, ctx->Viewport.Y,
386 ctx->Viewport.Width, ctx->Viewport.Height,
387 ctx->Viewport.Near, ctx->Viewport.Far,
388 depthMax);
389 }
390
391
392 /**
393 * Update derived multisample state.
394 */
395 static void
396 update_multisample(struct gl_context *ctx)
397 {
398 ctx->Multisample._Enabled = GL_FALSE;
399 if (ctx->Multisample.Enabled &&
400 ctx->DrawBuffer &&
401 ctx->DrawBuffer->Visual.sampleBuffers)
402 ctx->Multisample._Enabled = GL_TRUE;
403 }
404
405
406 /**
407 * Update derived color/blend/logicop state.
408 */
409 static void
410 update_color(struct gl_context *ctx)
411 {
412 /* This is needed to support 1.1's RGB logic ops AND
413 * 1.0's blending logicops.
414 */
415 ctx->Color._LogicOpEnabled = _mesa_rgba_logicop_enabled(ctx);
416 }
417
418 static void
419 update_clamp_fragment_color(struct gl_context *ctx)
420 {
421 if(ctx->Color.ClampFragmentColor == GL_FIXED_ONLY_ARB)
422 ctx->Color._ClampFragmentColor = !ctx->DrawBuffer || !ctx->DrawBuffer->Visual.floatMode;
423 else
424 ctx->Color._ClampFragmentColor = ctx->Color.ClampFragmentColor;
425 }
426
427 static void
428 update_clamp_vertex_color(struct gl_context *ctx)
429 {
430 if(ctx->Light.ClampVertexColor == GL_FIXED_ONLY_ARB)
431 ctx->Light._ClampVertexColor = !ctx->DrawBuffer || !ctx->DrawBuffer->Visual.floatMode;
432 else
433 ctx->Light._ClampVertexColor = ctx->Light.ClampVertexColor;
434 }
435
436 static void
437 update_clamp_read_color(struct gl_context *ctx)
438 {
439 if(ctx->Color.ClampReadColor == GL_FIXED_ONLY_ARB)
440 ctx->Color._ClampReadColor = !ctx->ReadBuffer || !ctx->ReadBuffer->Visual.floatMode;
441 else
442 ctx->Color._ClampReadColor = ctx->Color.ClampReadColor;
443 }
444
445
446
447
448 /*
449 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
450 * in ctx->_TriangleCaps if needed.
451 */
452 static void
453 update_polygon(struct gl_context *ctx)
454 {
455 ctx->_TriangleCaps &= ~(DD_TRI_CULL_FRONT_BACK | DD_TRI_OFFSET);
456
457 if (ctx->Polygon.CullFlag && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
458 ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
459
460 if ( ctx->Polygon.OffsetPoint
461 || ctx->Polygon.OffsetLine
462 || ctx->Polygon.OffsetFill)
463 ctx->_TriangleCaps |= DD_TRI_OFFSET;
464 }
465
466
467 /**
468 * Update the ctx->_TriangleCaps bitfield.
469 * XXX that bitfield should really go away someday!
470 * This function must be called after other update_*() functions since
471 * there are dependencies on some other derived values.
472 */
473 #if 0
474 static void
475 update_tricaps(struct gl_context *ctx, GLbitfield new_state)
476 {
477 ctx->_TriangleCaps = 0;
478
479 /*
480 * Points
481 */
482 if (1/*new_state & _NEW_POINT*/) {
483 if (ctx->Point.SmoothFlag)
484 ctx->_TriangleCaps |= DD_POINT_SMOOTH;
485 if (ctx->Point._Attenuated)
486 ctx->_TriangleCaps |= DD_POINT_ATTEN;
487 }
488
489 /*
490 * Lines
491 */
492 if (1/*new_state & _NEW_LINE*/) {
493 if (ctx->Line.SmoothFlag)
494 ctx->_TriangleCaps |= DD_LINE_SMOOTH;
495 if (ctx->Line.StippleFlag)
496 ctx->_TriangleCaps |= DD_LINE_STIPPLE;
497 }
498
499 /*
500 * Polygons
501 */
502 if (1/*new_state & _NEW_POLYGON*/) {
503 if (ctx->Polygon.SmoothFlag)
504 ctx->_TriangleCaps |= DD_TRI_SMOOTH;
505 if (ctx->Polygon.StippleFlag)
506 ctx->_TriangleCaps |= DD_TRI_STIPPLE;
507 if (ctx->Polygon.FrontMode != GL_FILL
508 || ctx->Polygon.BackMode != GL_FILL)
509 ctx->_TriangleCaps |= DD_TRI_UNFILLED;
510 if (ctx->Polygon.CullFlag
511 && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)
512 ctx->_TriangleCaps |= DD_TRI_CULL_FRONT_BACK;
513 if (ctx->Polygon.OffsetPoint ||
514 ctx->Polygon.OffsetLine ||
515 ctx->Polygon.OffsetFill)
516 ctx->_TriangleCaps |= DD_TRI_OFFSET;
517 }
518
519 /*
520 * Lighting and shading
521 */
522 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
523 ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
524 if (ctx->Light.ShadeModel == GL_FLAT)
525 ctx->_TriangleCaps |= DD_FLATSHADE;
526 if (_mesa_need_secondary_color(ctx))
527 ctx->_TriangleCaps |= DD_SEPARATE_SPECULAR;
528
529 /*
530 * Stencil
531 */
532 if (ctx->Stencil._TestTwoSide)
533 ctx->_TriangleCaps |= DD_TRI_TWOSTENCIL;
534 }
535 #endif
536
537
538 /**
539 * Compute derived GL state.
540 * If __struct gl_contextRec::NewState is non-zero then this function \b must
541 * be called before rendering anything.
542 *
543 * Calls dd_function_table::UpdateState to perform any internal state
544 * management necessary.
545 *
546 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
547 * _mesa_update_buffer_bounds(),
548 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
549 */
550 void
551 _mesa_update_state_locked( struct gl_context *ctx )
552 {
553 GLbitfield new_state = ctx->NewState;
554 GLbitfield prog_flags = _NEW_PROGRAM;
555 GLbitfield new_prog_state = 0x0;
556
557 if (new_state == _NEW_CURRENT_ATTRIB)
558 goto out;
559
560 if (MESA_VERBOSE & VERBOSE_STATE)
561 _mesa_print_state("_mesa_update_state", new_state);
562
563 /* Determine which state flags effect vertex/fragment program state */
564 if (ctx->FragmentProgram._MaintainTexEnvProgram) {
565 prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE | _NEW_FOG |
566 _NEW_ARRAY | _NEW_LIGHT | _NEW_POINT | _NEW_RENDERMODE |
567 _NEW_PROGRAM | _NEW_FRAG_CLAMP);
568 }
569 if (ctx->VertexProgram._MaintainTnlProgram) {
570 prog_flags |= (_NEW_ARRAY | _NEW_TEXTURE | _NEW_TEXTURE_MATRIX |
571 _NEW_TRANSFORM | _NEW_POINT |
572 _NEW_FOG | _NEW_LIGHT |
573 _MESA_NEW_NEED_EYE_COORDS);
574 }
575
576 /*
577 * Now update derived state info
578 */
579
580 if (new_state & prog_flags)
581 update_program_enables( ctx );
582
583 if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
584 _mesa_update_modelview_project( ctx, new_state );
585
586 if (new_state & (_NEW_PROGRAM|_NEW_TEXTURE|_NEW_TEXTURE_MATRIX))
587 _mesa_update_texture( ctx, new_state );
588
589 if (new_state & _NEW_BUFFERS)
590 _mesa_update_framebuffer(ctx);
591
592 if (new_state & (_NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT))
593 _mesa_update_draw_buffer_bounds( ctx );
594
595 if (new_state & _NEW_POLYGON)
596 update_polygon( ctx );
597
598 if (new_state & _NEW_LIGHT)
599 _mesa_update_lighting( ctx );
600
601 if (new_state & (_NEW_LIGHT | _NEW_BUFFERS))
602 update_clamp_vertex_color(ctx);
603
604 if (new_state & (_NEW_STENCIL | _NEW_BUFFERS))
605 _mesa_update_stencil( ctx );
606
607 if (new_state & _NEW_PIXEL)
608 _mesa_update_pixel( ctx, new_state );
609
610 if (new_state & _DD_NEW_SEPARATE_SPECULAR)
611 update_separate_specular( ctx );
612
613 if (new_state & (_NEW_BUFFERS | _NEW_VIEWPORT))
614 update_viewport_matrix(ctx);
615
616 if (new_state & _NEW_MULTISAMPLE)
617 update_multisample( ctx );
618
619 if (new_state & _NEW_COLOR)
620 update_color( ctx );
621
622 if (new_state & (_NEW_COLOR | _NEW_BUFFERS))
623 update_clamp_read_color(ctx);
624
625 if(new_state & (_NEW_FRAG_CLAMP | _NEW_BUFFERS))
626 update_clamp_fragment_color(ctx);
627
628 #if 0
629 if (new_state & (_NEW_POINT | _NEW_LINE | _NEW_POLYGON | _NEW_LIGHT
630 | _NEW_STENCIL | _DD_NEW_SEPARATE_SPECULAR))
631 update_tricaps( ctx, new_state );
632 #endif
633
634 /* ctx->_NeedEyeCoords is now up to date.
635 *
636 * If the truth value of this variable has changed, update for the
637 * new lighting space and recompute the positions of lights and the
638 * normal transform.
639 *
640 * If the lighting space hasn't changed, may still need to recompute
641 * light positions & normal transforms for other reasons.
642 */
643 if (new_state & _MESA_NEW_NEED_EYE_COORDS)
644 _mesa_update_tnl_spaces( ctx, new_state );
645
646 if (new_state & prog_flags) {
647 /* When we generate programs from fixed-function vertex/fragment state
648 * this call may generate/bind a new program. If so, we need to
649 * propogate the _NEW_PROGRAM flag to the driver.
650 */
651 new_prog_state |= update_program( ctx );
652 }
653
654 if (new_state & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT))
655 update_arrays( ctx );
656
657 out:
658 new_prog_state |= update_program_constants(ctx);
659
660 /*
661 * Give the driver a chance to act upon the new_state flags.
662 * The driver might plug in different span functions, for example.
663 * Also, this is where the driver can invalidate the state of any
664 * active modules (such as swrast_setup, swrast, tnl, etc).
665 *
666 * Set ctx->NewState to zero to avoid recursion if
667 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
668 */
669 new_state = ctx->NewState | new_prog_state;
670 ctx->NewState = 0;
671 ctx->Driver.UpdateState(ctx, new_state);
672 ctx->Array.NewState = 0;
673 if (!ctx->Array.RebindArrays)
674 ctx->Array.RebindArrays = (new_state & (_NEW_ARRAY | _NEW_PROGRAM)) != 0;
675 }
676
677
678 /* This is the usual entrypoint for state updates:
679 */
680 void
681 _mesa_update_state( struct gl_context *ctx )
682 {
683 _mesa_lock_context_textures(ctx);
684 _mesa_update_state_locked(ctx);
685 _mesa_unlock_context_textures(ctx);
686 }
687
688
689
690
691 /**
692 * Want to figure out which fragment program inputs are actually
693 * constant/current values from ctx->Current. These should be
694 * referenced as a tracked state variable rather than a fragment
695 * program input, to save the overhead of putting a constant value in
696 * every submitted vertex, transferring it to hardware, interpolating
697 * it across the triangle, etc...
698 *
699 * When there is a VP bound, just use vp->outputs. But when we're
700 * generating vp from fixed function state, basically want to
701 * calculate:
702 *
703 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
704 * potential_vp_outputs )
705 *
706 * Where potential_vp_outputs is calculated by looking at enabled
707 * texgen, etc.
708 *
709 * The generated fragment program should then only declare inputs that
710 * may vary or otherwise differ from the ctx->Current values.
711 * Otherwise, the fp should track them as state values instead.
712 */
713 void
714 _mesa_set_varying_vp_inputs( struct gl_context *ctx,
715 GLbitfield varying_inputs )
716 {
717 if (ctx->varying_vp_inputs != varying_inputs) {
718 ctx->varying_vp_inputs = varying_inputs;
719 ctx->NewState |= _NEW_ARRAY;
720 /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
721 }
722 }
723
724
725 /**
726 * Used by drivers to tell core Mesa that the driver is going to
727 * install/ use its own vertex program. In particular, this will
728 * prevent generated fragment programs from using state vars instead
729 * of ordinary varyings/inputs.
730 */
731 void
732 _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
733 {
734 if (ctx->VertexProgram._Overriden != flag) {
735 ctx->VertexProgram._Overriden = flag;
736
737 /* Set one of the bits which will trigger fragment program
738 * regeneration:
739 */
740 ctx->NewState |= _NEW_PROGRAM;
741 }
742 }