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