2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
30 * This file manages recalculation of derived values in struct gl_context.
39 #include "framebuffer.h"
43 #include "program/program.h"
44 #include "program/prog_parameter.h"
45 #include "shaderobj.h"
54 update_separate_specular(struct gl_context
*ctx
)
56 if (_mesa_need_secondary_color(ctx
))
57 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
59 ctx
->_TriangleCaps
&= ~DD_SEPARATE_SPECULAR
;
64 * Helper for update_arrays().
65 * \return min(current min, array->_MaxElement).
68 update_min(GLuint min
, struct gl_client_array
*array
)
70 _mesa_update_array_max_element(array
);
71 return MIN2(min
, array
->_MaxElement
);
76 * Update ctx->Array._MaxElement (the max legal index into all enabled arrays).
77 * Need to do this upon new array state or new buffer object state.
80 update_arrays( struct gl_context
*ctx
)
82 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
85 /* find min of _MaxElement values for all enabled arrays.
86 * Note that the generic arrays always take precedence over
91 if (ctx
->VertexProgram
._Current
92 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
) {
93 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
]);
95 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
) {
96 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]);
100 if (ctx
->VertexProgram
._Enabled
101 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC1
].Enabled
) {
102 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC1
]);
104 /* no conventional vertex weight array */
107 if (ctx
->VertexProgram
._Enabled
108 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC2
].Enabled
) {
109 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC2
]);
111 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
) {
112 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]);
116 if (ctx
->VertexProgram
._Enabled
117 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC3
].Enabled
) {
118 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC3
]);
120 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
) {
121 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]);
125 if (ctx
->VertexProgram
._Enabled
126 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC4
].Enabled
) {
127 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC4
]);
129 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
].Enabled
) {
130 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
]);
134 if (ctx
->VertexProgram
._Enabled
135 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC5
].Enabled
) {
136 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC5
]);
138 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
].Enabled
) {
139 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
]);
143 if (ctx
->VertexProgram
._Enabled
144 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC6
].Enabled
) {
145 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC6
]);
147 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
].Enabled
) {
148 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
]);
152 if (ctx
->VertexProgram
._Enabled
153 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC7
].Enabled
) {
154 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC7
]);
158 for (i
= 0; i
< VERT_ATTRIB_TEX_MAX
; i
++) {
159 if (ctx
->VertexProgram
._Enabled
160 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC8
+ i
].Enabled
) {
161 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC8
+ i
]);
163 else if (i
< ctx
->Const
.MaxTextureCoordUnits
164 && arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX(i
)].Enabled
) {
165 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX(i
)]);
170 if (ctx
->VertexProgram
._Current
) {
171 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
172 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
) {
173 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)]);
178 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
].Enabled
) {
179 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
]);
182 /* _MaxElement is one past the last legal array element */
183 arrayObj
->_MaxElement
= min
;
188 * Update the following fields:
189 * ctx->VertexProgram._Enabled
190 * ctx->FragmentProgram._Enabled
191 * ctx->ATIFragmentShader._Enabled
192 * This needs to be done before texture state validation.
195 update_program_enables(struct gl_context
*ctx
)
197 /* These _Enabled flags indicate if the user-defined ARB/NV vertex/fragment
198 * program is enabled AND valid. Similarly for ATI fragment shaders.
199 * GLSL shaders not relevant here.
201 ctx
->VertexProgram
._Enabled
= ctx
->VertexProgram
.Enabled
202 && ctx
->VertexProgram
.Current
->Base
.Instructions
;
203 ctx
->FragmentProgram
._Enabled
= ctx
->FragmentProgram
.Enabled
204 && ctx
->FragmentProgram
.Current
->Base
.Instructions
;
209 * Update the ctx->Vertex/Geometry/FragmentProgram._Current pointers to point
210 * to the current/active programs. Then call ctx->Driver.BindProgram() to
211 * tell the driver which programs to use.
213 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
214 * programs or programs derived from fixed-function state.
216 * This function needs to be called after texture state validation in case
217 * we're generating a fragment program from fixed-function texture state.
219 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
220 * or fragment program is being used.
223 update_program(struct gl_context
*ctx
)
225 const struct gl_shader_program
*vsProg
= ctx
->Shader
.CurrentVertexProgram
;
226 struct gl_shader_program
*fsProg
= ctx
->Shader
.CurrentFragmentProgram
;
227 const struct gl_vertex_program
*prevVP
= ctx
->VertexProgram
._Current
;
228 const struct gl_fragment_program
*prevFP
= ctx
->FragmentProgram
._Current
;
229 GLbitfield new_state
= 0x0;
232 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
233 * pointers to the programs that should be used for rendering. If either
234 * is NULL, use fixed-function code paths.
236 * These programs may come from several sources. The priority is as
238 * 1. OpenGL 2.0/ARB vertex/fragment shaders
239 * 2. ARB/NV vertex/fragment programs
240 * 3. Programs derived from fixed-function state.
242 * Note: it's possible for a vertex shader to get used with a fragment
243 * program (and vice versa) here, but in practice that shouldn't ever
244 * come up, or matter.
247 if (fsProg
&& fsProg
->LinkStatus
248 && fsProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
249 /* Use GLSL fragment shader */
250 _mesa_reference_shader_program(ctx
,
251 &ctx
->Shader
._CurrentFragmentProgram
,
253 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
254 (struct gl_fragment_program
*)
255 fsProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
);
257 else if (ctx
->FragmentProgram
._Enabled
) {
258 /* Use user-defined fragment program */
259 _mesa_reference_shader_program(ctx
,
260 &ctx
->Shader
._CurrentFragmentProgram
,
262 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
263 ctx
->FragmentProgram
.Current
);
266 /* No fragment program */
267 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
270 /* Examine vertex program after fragment program as
271 * _mesa_get_fixed_func_vertex_program() needs to know active
274 if (vsProg
&& vsProg
->LinkStatus
275 && vsProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
276 /* Use GLSL vertex shader */
277 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
278 (struct gl_vertex_program
*)
279 vsProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
);
281 else if (ctx
->VertexProgram
._Enabled
) {
282 /* Use user-defined vertex program */
283 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
284 ctx
->VertexProgram
.Current
);
287 /* no vertex program */
288 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
291 /* Let the driver know what's happening:
293 if (ctx
->FragmentProgram
._Current
!= prevFP
) {
294 new_state
|= _NEW_PROGRAM
;
295 if (ctx
->Driver
.BindProgram
) {
296 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
297 (struct gl_program
*) ctx
->FragmentProgram
._Current
);
301 if (ctx
->VertexProgram
._Current
!= prevVP
) {
302 new_state
|= _NEW_PROGRAM
;
303 if (ctx
->Driver
.BindProgram
) {
304 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
305 (struct gl_program
*) ctx
->VertexProgram
._Current
);
314 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
317 update_program_constants(struct gl_context
*ctx
)
319 GLbitfield new_state
= 0x0;
321 if (ctx
->FragmentProgram
._Current
) {
322 const struct gl_program_parameter_list
*params
=
323 ctx
->FragmentProgram
._Current
->Base
.Parameters
;
324 if (params
&& params
->StateFlags
& ctx
->NewState
) {
325 new_state
|= _NEW_PROGRAM_CONSTANTS
;
329 if (ctx
->VertexProgram
._Current
) {
330 const struct gl_program_parameter_list
*params
=
331 ctx
->VertexProgram
._Current
->Base
.Parameters
;
332 if (params
&& params
->StateFlags
& ctx
->NewState
) {
333 new_state
|= _NEW_PROGRAM_CONSTANTS
;
344 update_viewport_matrix(struct gl_context
*ctx
)
346 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
348 ASSERT(depthMax
> 0);
350 /* Compute scale and bias values. This is really driver-specific
351 * and should be maintained elsewhere if at all.
352 * NOTE: RasterPos uses this.
354 _math_matrix_viewport(&ctx
->Viewport
._WindowMap
,
355 ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
356 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
,
357 ctx
->Viewport
.Near
, ctx
->Viewport
.Far
,
363 * Update the ctx->VertexProgram._TwoSideEnabled flag.
366 update_twoside(struct gl_context
*ctx
)
368 if (ctx
->Shader
.CurrentVertexProgram
||
369 ctx
->VertexProgram
._Enabled
) {
370 ctx
->VertexProgram
._TwoSideEnabled
= ctx
->VertexProgram
.TwoSideEnabled
;
372 ctx
->VertexProgram
._TwoSideEnabled
= (ctx
->Light
.Enabled
&&
373 ctx
->Light
.Model
.TwoSide
);
379 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
380 * in ctx->_TriangleCaps if needed.
383 update_polygon(struct gl_context
*ctx
)
385 ctx
->_TriangleCaps
&= ~(DD_TRI_CULL_FRONT_BACK
| DD_TRI_OFFSET
);
387 if (ctx
->Polygon
.CullFlag
&& ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
388 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
390 if ( ctx
->Polygon
.OffsetPoint
391 || ctx
->Polygon
.OffsetLine
392 || ctx
->Polygon
.OffsetFill
)
393 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
398 * Update the ctx->_TriangleCaps bitfield.
399 * XXX that bitfield should really go away someday!
400 * This function must be called after other update_*() functions since
401 * there are dependencies on some other derived values.
405 update_tricaps(struct gl_context
*ctx
, GLbitfield new_state
)
407 ctx
->_TriangleCaps
= 0;
412 if (1/*new_state & _NEW_POINT*/) {
413 if (ctx
->Point
.SmoothFlag
)
414 ctx
->_TriangleCaps
|= DD_POINT_SMOOTH
;
415 if (ctx
->Point
._Attenuated
)
416 ctx
->_TriangleCaps
|= DD_POINT_ATTEN
;
422 if (1/*new_state & _NEW_LINE*/) {
423 if (ctx
->Line
.SmoothFlag
)
424 ctx
->_TriangleCaps
|= DD_LINE_SMOOTH
;
425 if (ctx
->Line
.StippleFlag
)
426 ctx
->_TriangleCaps
|= DD_LINE_STIPPLE
;
432 if (1/*new_state & _NEW_POLYGON*/) {
433 if (ctx
->Polygon
.SmoothFlag
)
434 ctx
->_TriangleCaps
|= DD_TRI_SMOOTH
;
435 if (ctx
->Polygon
.StippleFlag
)
436 ctx
->_TriangleCaps
|= DD_TRI_STIPPLE
;
437 if (ctx
->Polygon
.FrontMode
!= GL_FILL
438 || ctx
->Polygon
.BackMode
!= GL_FILL
)
439 ctx
->_TriangleCaps
|= DD_TRI_UNFILLED
;
440 if (ctx
->Polygon
.CullFlag
441 && ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
442 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
443 if (ctx
->Polygon
.OffsetPoint
||
444 ctx
->Polygon
.OffsetLine
||
445 ctx
->Polygon
.OffsetFill
)
446 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
450 * Lighting and shading
452 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
453 ctx
->_TriangleCaps
|= DD_TRI_LIGHT_TWOSIDE
;
454 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
455 ctx
->_TriangleCaps
|= DD_FLATSHADE
;
456 if (_mesa_need_secondary_color(ctx
))
457 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
462 if (ctx
->Stencil
._TestTwoSide
)
463 ctx
->_TriangleCaps
|= DD_TRI_TWOSTENCIL
;
469 * Compute derived GL state.
470 * If __struct gl_contextRec::NewState is non-zero then this function \b must
471 * be called before rendering anything.
473 * Calls dd_function_table::UpdateState to perform any internal state
474 * management necessary.
476 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
477 * _mesa_update_buffer_bounds(),
478 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
481 _mesa_update_state_locked( struct gl_context
*ctx
)
483 GLbitfield new_state
= ctx
->NewState
;
484 GLbitfield prog_flags
= _NEW_PROGRAM
;
485 GLbitfield new_prog_state
= 0x0;
487 if (new_state
== _NEW_CURRENT_ATTRIB
)
490 if (MESA_VERBOSE
& VERBOSE_STATE
)
491 _mesa_print_state("_mesa_update_state", new_state
);
494 * Now update derived state info
497 if (new_state
& prog_flags
)
498 update_program_enables( ctx
);
500 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
501 _mesa_update_modelview_project( ctx
, new_state
);
503 if (new_state
& (_NEW_PROGRAM
|_NEW_TEXTURE
|_NEW_TEXTURE_MATRIX
))
504 _mesa_update_texture( ctx
, new_state
);
506 if (new_state
& _NEW_BUFFERS
)
507 _mesa_update_framebuffer(ctx
);
509 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
510 _mesa_update_draw_buffer_bounds( ctx
);
512 if (new_state
& _NEW_POLYGON
)
513 update_polygon( ctx
);
515 if (new_state
& _NEW_LIGHT
)
516 _mesa_update_lighting( ctx
);
518 if (new_state
& (_NEW_LIGHT
| _NEW_PROGRAM
))
519 update_twoside( ctx
);
521 if (new_state
& (_NEW_STENCIL
| _NEW_BUFFERS
))
522 _mesa_update_stencil( ctx
);
524 if (new_state
& _NEW_PIXEL
)
525 _mesa_update_pixel( ctx
, new_state
);
527 if (new_state
& _DD_NEW_SEPARATE_SPECULAR
)
528 update_separate_specular( ctx
);
530 if (new_state
& (_NEW_BUFFERS
| _NEW_VIEWPORT
))
531 update_viewport_matrix(ctx
);
534 if (new_state
& (_NEW_POINT
| _NEW_LINE
| _NEW_POLYGON
| _NEW_LIGHT
535 | _NEW_STENCIL
| _DD_NEW_SEPARATE_SPECULAR
))
536 update_tricaps( ctx
, new_state
);
539 /* ctx->_NeedEyeCoords is now up to date.
541 * If the truth value of this variable has changed, update for the
542 * new lighting space and recompute the positions of lights and the
545 * If the lighting space hasn't changed, may still need to recompute
546 * light positions & normal transforms for other reasons.
548 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
549 _mesa_update_tnl_spaces( ctx
, new_state
);
551 if (new_state
& prog_flags
) {
552 /* When we generate programs from fixed-function vertex/fragment state
553 * this call may generate/bind a new program. If so, we need to
554 * propogate the _NEW_PROGRAM flag to the driver.
556 new_prog_state
|= update_program( ctx
);
559 if (new_state
& (_NEW_ARRAY
| _NEW_PROGRAM
| _NEW_BUFFER_OBJECT
))
560 update_arrays( ctx
);
563 new_prog_state
|= update_program_constants(ctx
);
566 * Give the driver a chance to act upon the new_state flags.
567 * The driver might plug in different span functions, for example.
568 * Also, this is where the driver can invalidate the state of any
569 * active modules (such as swrast_setup, swrast, tnl, etc).
571 * Set ctx->NewState to zero to avoid recursion if
572 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
574 new_state
= ctx
->NewState
| new_prog_state
;
576 ctx
->Driver
.UpdateState(ctx
, new_state
);
577 ctx
->Array
.NewState
= 0;
578 if (!ctx
->Array
.RebindArrays
)
579 ctx
->Array
.RebindArrays
= (new_state
& (_NEW_ARRAY
| _NEW_PROGRAM
)) != 0;
583 /* This is the usual entrypoint for state updates:
586 _mesa_update_state( struct gl_context
*ctx
)
588 _mesa_lock_context_textures(ctx
);
589 _mesa_update_state_locked(ctx
);
590 _mesa_unlock_context_textures(ctx
);
597 * Want to figure out which fragment program inputs are actually
598 * constant/current values from ctx->Current. These should be
599 * referenced as a tracked state variable rather than a fragment
600 * program input, to save the overhead of putting a constant value in
601 * every submitted vertex, transferring it to hardware, interpolating
602 * it across the triangle, etc...
604 * When there is a VP bound, just use vp->outputs. But when we're
605 * generating vp from fixed function state, basically want to
608 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
609 * potential_vp_outputs )
611 * Where potential_vp_outputs is calculated by looking at enabled
614 * The generated fragment program should then only declare inputs that
615 * may vary or otherwise differ from the ctx->Current values.
616 * Otherwise, the fp should track them as state values instead.
619 _mesa_set_varying_vp_inputs( struct gl_context
*ctx
,
620 GLbitfield64 varying_inputs
)
622 if (ctx
->varying_vp_inputs
!= varying_inputs
) {
623 ctx
->varying_vp_inputs
= varying_inputs
;
624 ctx
->NewState
|= _NEW_ARRAY
;
625 /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
631 * Used by drivers to tell core Mesa that the driver is going to
632 * install/ use its own vertex program. In particular, this will
633 * prevent generated fragment programs from using state vars instead
634 * of ordinary varyings/inputs.
637 _mesa_set_vp_override(struct gl_context
*ctx
, GLboolean flag
)
639 if (ctx
->VertexProgram
._Overriden
!= flag
) {
640 ctx
->VertexProgram
._Overriden
= flag
;
642 /* Set one of the bits which will trigger fragment program
645 ctx
->NewState
|= _NEW_PROGRAM
;