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