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.
38 #include "framebuffer.h"
42 #include "program/program.h"
43 #include "program/prog_parameter.h"
44 #include "shaderobj.h"
53 update_separate_specular(struct gl_context
*ctx
)
55 if (_mesa_need_secondary_color(ctx
))
56 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
58 ctx
->_TriangleCaps
&= ~DD_SEPARATE_SPECULAR
;
63 * Helper for update_arrays().
64 * \return min(current min, array->_MaxElement).
67 update_min(GLuint min
, struct gl_client_array
*array
)
69 _mesa_update_array_max_element(array
);
70 return MIN2(min
, array
->_MaxElement
);
75 * Update ctx->Array._MaxElement (the max legal index into all enabled arrays).
76 * Need to do this upon new array state or new buffer object state.
79 update_arrays( struct gl_context
*ctx
)
81 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
84 /* find min of _MaxElement values for all enabled arrays.
85 * Note that the generic arrays always take precedence over
90 if (ctx
->VertexProgram
._Current
91 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
].Enabled
) {
92 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC0
]);
94 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
) {
95 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]);
99 if (ctx
->VertexProgram
._Enabled
100 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC1
].Enabled
) {
101 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC1
]);
103 /* no conventional vertex weight array */
106 if (ctx
->VertexProgram
._Enabled
107 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC2
].Enabled
) {
108 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC2
]);
110 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
) {
111 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]);
115 if (ctx
->VertexProgram
._Enabled
116 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC3
].Enabled
) {
117 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC3
]);
119 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
) {
120 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]);
124 if (ctx
->VertexProgram
._Enabled
125 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC4
].Enabled
) {
126 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC4
]);
128 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
].Enabled
) {
129 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
]);
133 if (ctx
->VertexProgram
._Enabled
134 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC5
].Enabled
) {
135 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC5
]);
137 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
].Enabled
) {
138 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
]);
142 if (ctx
->VertexProgram
._Enabled
143 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC6
].Enabled
) {
144 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC6
]);
146 else if (arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
].Enabled
) {
147 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
]);
151 if (ctx
->VertexProgram
._Enabled
152 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC7
].Enabled
) {
153 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC7
]);
157 for (i
= 0; i
< VERT_ATTRIB_TEX_MAX
; i
++) {
158 if (ctx
->VertexProgram
._Enabled
159 && arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC8
+ i
].Enabled
) {
160 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC8
+ i
]);
162 else if (i
< ctx
->Const
.MaxTextureCoordUnits
163 && arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX(i
)].Enabled
) {
164 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_TEX(i
)]);
169 if (ctx
->VertexProgram
._Current
) {
170 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
171 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)].Enabled
) {
172 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_GENERIC(i
)]);
177 if (arrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
].Enabled
) {
178 min
= update_min(min
, &arrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
]);
181 /* _MaxElement is one past the last legal array element */
182 arrayObj
->_MaxElement
= min
;
187 * Update the following fields:
188 * ctx->VertexProgram._Enabled
189 * ctx->FragmentProgram._Enabled
190 * ctx->ATIFragmentShader._Enabled
191 * This needs to be done before texture state validation.
194 update_program_enables(struct gl_context
*ctx
)
196 /* These _Enabled flags indicate if the user-defined ARB/NV vertex/fragment
197 * program is enabled AND valid. Similarly for ATI fragment shaders.
198 * GLSL shaders not relevant here.
200 ctx
->VertexProgram
._Enabled
= ctx
->VertexProgram
.Enabled
201 && ctx
->VertexProgram
.Current
->Base
.Instructions
;
202 ctx
->FragmentProgram
._Enabled
= ctx
->FragmentProgram
.Enabled
203 && ctx
->FragmentProgram
.Current
->Base
.Instructions
;
208 * Update the ctx->Vertex/Geometry/FragmentProgram._Current pointers to point
209 * to the current/active programs. Then call ctx->Driver.BindProgram() to
210 * tell the driver which programs to use.
212 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
213 * programs or programs derived from fixed-function state.
215 * This function needs to be called after texture state validation in case
216 * we're generating a fragment program from fixed-function texture state.
218 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
219 * or fragment program is being used.
222 update_program(struct gl_context
*ctx
)
224 const struct gl_shader_program
*vsProg
= ctx
->Shader
.CurrentVertexProgram
;
225 struct gl_shader_program
*fsProg
= ctx
->Shader
.CurrentFragmentProgram
;
226 const struct gl_vertex_program
*prevVP
= ctx
->VertexProgram
._Current
;
227 const struct gl_fragment_program
*prevFP
= ctx
->FragmentProgram
._Current
;
228 GLbitfield new_state
= 0x0;
231 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
232 * pointers to the programs that should be used for rendering. If either
233 * is NULL, use fixed-function code paths.
235 * These programs may come from several sources. The priority is as
237 * 1. OpenGL 2.0/ARB vertex/fragment shaders
238 * 2. ARB/NV vertex/fragment programs
239 * 3. Programs derived from fixed-function state.
241 * Note: it's possible for a vertex shader to get used with a fragment
242 * program (and vice versa) here, but in practice that shouldn't ever
243 * come up, or matter.
246 if (fsProg
&& fsProg
->LinkStatus
247 && fsProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
248 /* Use GLSL fragment shader */
249 _mesa_reference_shader_program(ctx
,
250 &ctx
->Shader
._CurrentFragmentProgram
,
252 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
253 (struct gl_fragment_program
*)
254 fsProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
);
256 else if (ctx
->FragmentProgram
._Enabled
) {
257 /* Use user-defined fragment program */
258 _mesa_reference_shader_program(ctx
,
259 &ctx
->Shader
._CurrentFragmentProgram
,
261 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
262 ctx
->FragmentProgram
.Current
);
265 /* No fragment program */
266 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
269 /* Examine vertex program after fragment program as
270 * _mesa_get_fixed_func_vertex_program() needs to know active
273 if (vsProg
&& vsProg
->LinkStatus
274 && vsProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
275 /* Use GLSL vertex shader */
276 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
277 (struct gl_vertex_program
*)
278 vsProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
);
280 else if (ctx
->VertexProgram
._Enabled
) {
281 /* Use user-defined vertex program */
282 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
283 ctx
->VertexProgram
.Current
);
286 /* no vertex program */
287 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
290 /* Let the driver know what's happening:
292 if (ctx
->FragmentProgram
._Current
!= prevFP
) {
293 new_state
|= _NEW_PROGRAM
;
294 if (ctx
->Driver
.BindProgram
) {
295 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
296 (struct gl_program
*) ctx
->FragmentProgram
._Current
);
300 if (ctx
->VertexProgram
._Current
!= prevVP
) {
301 new_state
|= _NEW_PROGRAM
;
302 if (ctx
->Driver
.BindProgram
) {
303 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
304 (struct gl_program
*) ctx
->VertexProgram
._Current
);
313 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
316 update_program_constants(struct gl_context
*ctx
)
318 GLbitfield new_state
= 0x0;
320 if (ctx
->FragmentProgram
._Current
) {
321 const struct gl_program_parameter_list
*params
=
322 ctx
->FragmentProgram
._Current
->Base
.Parameters
;
323 if (params
&& params
->StateFlags
& ctx
->NewState
) {
324 new_state
|= _NEW_PROGRAM_CONSTANTS
;
328 if (ctx
->VertexProgram
._Current
) {
329 const struct gl_program_parameter_list
*params
=
330 ctx
->VertexProgram
._Current
->Base
.Parameters
;
331 if (params
&& params
->StateFlags
& ctx
->NewState
) {
332 new_state
|= _NEW_PROGRAM_CONSTANTS
;
343 update_viewport_matrix(struct gl_context
*ctx
)
345 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
347 ASSERT(depthMax
> 0);
349 /* Compute scale and bias values. This is really driver-specific
350 * and should be maintained elsewhere if at all.
351 * NOTE: RasterPos uses this.
353 _math_matrix_viewport(&ctx
->Viewport
._WindowMap
,
354 ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
355 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
,
356 ctx
->Viewport
.Near
, ctx
->Viewport
.Far
,
362 * Update the ctx->VertexProgram._TwoSideEnabled flag.
365 update_twoside(struct gl_context
*ctx
)
367 if (ctx
->Shader
.CurrentVertexProgram
||
368 ctx
->VertexProgram
._Enabled
) {
369 ctx
->VertexProgram
._TwoSideEnabled
= ctx
->VertexProgram
.TwoSideEnabled
;
371 ctx
->VertexProgram
._TwoSideEnabled
= (ctx
->Light
.Enabled
&&
372 ctx
->Light
.Model
.TwoSide
);
378 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
379 * in ctx->_TriangleCaps if needed.
382 update_polygon(struct gl_context
*ctx
)
384 ctx
->_TriangleCaps
&= ~(DD_TRI_CULL_FRONT_BACK
| DD_TRI_OFFSET
);
386 if (ctx
->Polygon
.CullFlag
&& ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
387 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
389 if ( ctx
->Polygon
.OffsetPoint
390 || ctx
->Polygon
.OffsetLine
391 || ctx
->Polygon
.OffsetFill
)
392 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
397 * Update the ctx->_TriangleCaps bitfield.
398 * XXX that bitfield should really go away someday!
399 * This function must be called after other update_*() functions since
400 * there are dependencies on some other derived values.
404 update_tricaps(struct gl_context
*ctx
, GLbitfield new_state
)
406 ctx
->_TriangleCaps
= 0;
411 if (1/*new_state & _NEW_POINT*/) {
412 if (ctx
->Point
.SmoothFlag
)
413 ctx
->_TriangleCaps
|= DD_POINT_SMOOTH
;
414 if (ctx
->Point
._Attenuated
)
415 ctx
->_TriangleCaps
|= DD_POINT_ATTEN
;
421 if (1/*new_state & _NEW_LINE*/) {
422 if (ctx
->Line
.SmoothFlag
)
423 ctx
->_TriangleCaps
|= DD_LINE_SMOOTH
;
424 if (ctx
->Line
.StippleFlag
)
425 ctx
->_TriangleCaps
|= DD_LINE_STIPPLE
;
431 if (1/*new_state & _NEW_POLYGON*/) {
432 if (ctx
->Polygon
.SmoothFlag
)
433 ctx
->_TriangleCaps
|= DD_TRI_SMOOTH
;
434 if (ctx
->Polygon
.StippleFlag
)
435 ctx
->_TriangleCaps
|= DD_TRI_STIPPLE
;
436 if (ctx
->Polygon
.FrontMode
!= GL_FILL
437 || ctx
->Polygon
.BackMode
!= GL_FILL
)
438 ctx
->_TriangleCaps
|= DD_TRI_UNFILLED
;
439 if (ctx
->Polygon
.CullFlag
440 && ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
441 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
442 if (ctx
->Polygon
.OffsetPoint
||
443 ctx
->Polygon
.OffsetLine
||
444 ctx
->Polygon
.OffsetFill
)
445 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
449 * Lighting and shading
451 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
452 ctx
->_TriangleCaps
|= DD_TRI_LIGHT_TWOSIDE
;
453 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
454 ctx
->_TriangleCaps
|= DD_FLATSHADE
;
455 if (_mesa_need_secondary_color(ctx
))
456 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
461 if (ctx
->Stencil
._TestTwoSide
)
462 ctx
->_TriangleCaps
|= DD_TRI_TWOSTENCIL
;
468 * Compute derived GL state.
469 * If __struct gl_contextRec::NewState is non-zero then this function \b must
470 * be called before rendering anything.
472 * Calls dd_function_table::UpdateState to perform any internal state
473 * management necessary.
475 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
476 * _mesa_update_buffer_bounds(),
477 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
480 _mesa_update_state_locked( struct gl_context
*ctx
)
482 GLbitfield new_state
= ctx
->NewState
;
483 GLbitfield prog_flags
= _NEW_PROGRAM
;
484 GLbitfield new_prog_state
= 0x0;
486 if (new_state
== _NEW_CURRENT_ATTRIB
)
490 * Now update derived state info
493 if (new_state
& prog_flags
)
494 update_program_enables( ctx
);
496 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
497 _mesa_update_modelview_project( ctx
, new_state
);
499 if (new_state
& (_NEW_PROGRAM
|_NEW_TEXTURE
|_NEW_TEXTURE_MATRIX
))
500 _mesa_update_texture( ctx
, new_state
);
502 if (new_state
& _NEW_BUFFERS
)
503 _mesa_update_framebuffer(ctx
);
505 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
506 _mesa_update_draw_buffer_bounds( ctx
);
508 if (new_state
& _NEW_POLYGON
)
509 update_polygon( ctx
);
511 if (new_state
& _NEW_LIGHT
)
512 _mesa_update_lighting( ctx
);
514 if (new_state
& (_NEW_LIGHT
| _NEW_PROGRAM
))
515 update_twoside( ctx
);
517 if (new_state
& (_NEW_STENCIL
| _NEW_BUFFERS
))
518 _mesa_update_stencil( ctx
);
520 if (new_state
& _NEW_PIXEL
)
521 _mesa_update_pixel( ctx
, new_state
);
523 if (new_state
& _DD_NEW_SEPARATE_SPECULAR
)
524 update_separate_specular( ctx
);
526 if (new_state
& (_NEW_BUFFERS
| _NEW_VIEWPORT
))
527 update_viewport_matrix(ctx
);
530 if (new_state
& (_NEW_POINT
| _NEW_LINE
| _NEW_POLYGON
| _NEW_LIGHT
531 | _NEW_STENCIL
| _DD_NEW_SEPARATE_SPECULAR
))
532 update_tricaps( ctx
, new_state
);
535 /* ctx->_NeedEyeCoords is now up to date.
537 * If the truth value of this variable has changed, update for the
538 * new lighting space and recompute the positions of lights and the
541 * If the lighting space hasn't changed, may still need to recompute
542 * light positions & normal transforms for other reasons.
544 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
545 _mesa_update_tnl_spaces( ctx
, new_state
);
547 if (new_state
& prog_flags
) {
548 /* When we generate programs from fixed-function vertex/fragment state
549 * this call may generate/bind a new program. If so, we need to
550 * propogate the _NEW_PROGRAM flag to the driver.
552 new_prog_state
|= update_program( ctx
);
555 if (new_state
& (_NEW_ARRAY
| _NEW_PROGRAM
| _NEW_BUFFER_OBJECT
))
556 update_arrays( ctx
);
559 new_prog_state
|= update_program_constants(ctx
);
562 * Give the driver a chance to act upon the new_state flags.
563 * The driver might plug in different span functions, for example.
564 * Also, this is where the driver can invalidate the state of any
565 * active modules (such as swrast_setup, swrast, tnl, etc).
567 * Set ctx->NewState to zero to avoid recursion if
568 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
570 new_state
= ctx
->NewState
| new_prog_state
;
572 ctx
->Driver
.UpdateState(ctx
, new_state
);
573 ctx
->Array
.NewState
= 0;
574 if (!ctx
->Array
.RebindArrays
)
575 ctx
->Array
.RebindArrays
= (new_state
& (_NEW_ARRAY
| _NEW_PROGRAM
)) != 0;
579 /* This is the usual entrypoint for state updates:
582 _mesa_update_state( struct gl_context
*ctx
)
584 _mesa_lock_context_textures(ctx
);
585 _mesa_update_state_locked(ctx
);
586 _mesa_unlock_context_textures(ctx
);
593 * Want to figure out which fragment program inputs are actually
594 * constant/current values from ctx->Current. These should be
595 * referenced as a tracked state variable rather than a fragment
596 * program input, to save the overhead of putting a constant value in
597 * every submitted vertex, transferring it to hardware, interpolating
598 * it across the triangle, etc...
600 * When there is a VP bound, just use vp->outputs. But when we're
601 * generating vp from fixed function state, basically want to
604 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
605 * potential_vp_outputs )
607 * Where potential_vp_outputs is calculated by looking at enabled
610 * The generated fragment program should then only declare inputs that
611 * may vary or otherwise differ from the ctx->Current values.
612 * Otherwise, the fp should track them as state values instead.
615 _mesa_set_varying_vp_inputs( struct gl_context
*ctx
,
616 GLbitfield64 varying_inputs
)
618 if (ctx
->varying_vp_inputs
!= varying_inputs
) {
619 ctx
->varying_vp_inputs
= varying_inputs
;
620 ctx
->NewState
|= _NEW_ARRAY
;
621 /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
627 * Used by drivers to tell core Mesa that the driver is going to
628 * install/ use its own vertex program. In particular, this will
629 * prevent generated fragment programs from using state vars instead
630 * of ordinary varyings/inputs.
633 _mesa_set_vp_override(struct gl_context
*ctx
, GLboolean flag
)
635 if (ctx
->VertexProgram
._Overriden
!= flag
) {
636 ctx
->VertexProgram
._Overriden
= flag
;
638 /* Set one of the bits which will trigger fragment program
641 ctx
->NewState
|= _NEW_PROGRAM
;