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