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