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