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