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 GLcontext.
39 #include "ffvertex_prog.h"
40 #include "framebuffer.h"
43 #if FEATURE_pixel_transfer
46 #include "shader/program.h"
49 #include "texenvprogram.h"
55 update_separate_specular(GLcontext
*ctx
)
57 if (NEED_SECONDARY_COLOR(ctx
))
58 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
60 ctx
->_TriangleCaps
&= ~DD_SEPARATE_SPECULAR
;
65 * Update state dependent on vertex arrays.
68 update_arrays( GLcontext
*ctx
)
72 /* find min of _MaxElement values for all enabled arrays */
75 if (ctx
->VertexProgram
._Current
76 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
].Enabled
) {
77 min
= ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_POS
]._MaxElement
;
79 else if (ctx
->Array
.ArrayObj
->Vertex
.Enabled
) {
80 min
= ctx
->Array
.ArrayObj
->Vertex
._MaxElement
;
83 /* can't draw anything without vertex positions! */
88 if (ctx
->VertexProgram
._Enabled
89 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_WEIGHT
].Enabled
) {
90 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_WEIGHT
]._MaxElement
);
92 /* no conventional vertex weight array */
95 if (ctx
->VertexProgram
._Enabled
96 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
].Enabled
) {
97 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_NORMAL
]._MaxElement
);
99 else if (ctx
->Array
.ArrayObj
->Normal
.Enabled
) {
100 min
= MIN2(min
, ctx
->Array
.ArrayObj
->Normal
._MaxElement
);
104 if (ctx
->VertexProgram
._Enabled
105 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
].Enabled
) {
106 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR0
]._MaxElement
);
108 else if (ctx
->Array
.ArrayObj
->Color
.Enabled
) {
109 min
= MIN2(min
, ctx
->Array
.ArrayObj
->Color
._MaxElement
);
113 if (ctx
->VertexProgram
._Enabled
114 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
].Enabled
) {
115 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR1
]._MaxElement
);
117 else if (ctx
->Array
.ArrayObj
->SecondaryColor
.Enabled
) {
118 min
= MIN2(min
, ctx
->Array
.ArrayObj
->SecondaryColor
._MaxElement
);
122 if (ctx
->VertexProgram
._Enabled
123 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
].Enabled
) {
124 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_FOG
]._MaxElement
);
126 else if (ctx
->Array
.ArrayObj
->FogCoord
.Enabled
) {
127 min
= MIN2(min
, ctx
->Array
.ArrayObj
->FogCoord
._MaxElement
);
131 if (ctx
->VertexProgram
._Enabled
132 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
].Enabled
) {
133 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_COLOR_INDEX
]._MaxElement
);
135 else if (ctx
->Array
.ArrayObj
->Index
.Enabled
) {
136 min
= MIN2(min
, ctx
->Array
.ArrayObj
->Index
._MaxElement
);
141 if (ctx
->VertexProgram
._Enabled
142 && ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
].Enabled
) {
143 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[VERT_ATTRIB_EDGEFLAG
]._MaxElement
);
147 for (i
= VERT_ATTRIB_TEX0
; i
<= VERT_ATTRIB_TEX7
; i
++) {
148 if (ctx
->VertexProgram
._Enabled
149 && ctx
->Array
.ArrayObj
->VertexAttrib
[i
].Enabled
) {
150 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[i
]._MaxElement
);
152 else if (i
- VERT_ATTRIB_TEX0
< ctx
->Const
.MaxTextureCoordUnits
153 && ctx
->Array
.ArrayObj
->TexCoord
[i
- VERT_ATTRIB_TEX0
].Enabled
) {
154 min
= MIN2(min
, ctx
->Array
.ArrayObj
->TexCoord
[i
- VERT_ATTRIB_TEX0
]._MaxElement
);
159 if (ctx
->VertexProgram
._Current
) {
160 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
161 if (ctx
->Array
.ArrayObj
->VertexAttrib
[i
].Enabled
) {
162 min
= MIN2(min
, ctx
->Array
.ArrayObj
->VertexAttrib
[i
]._MaxElement
);
167 if (ctx
->Array
.ArrayObj
->EdgeFlag
.Enabled
) {
168 min
= MIN2(min
, ctx
->Array
.ArrayObj
->EdgeFlag
._MaxElement
);
171 /* _MaxElement is one past the last legal array element */
172 ctx
->Array
._MaxElement
= min
;
177 * Update the following fields:
178 * ctx->VertexProgram._Enabled
179 * ctx->FragmentProgram._Enabled
180 * ctx->ATIFragmentShader._Enabled
181 * This needs to be done before texture state validation.
184 update_program_enables(GLcontext
*ctx
)
186 /* These _Enabled flags indicate if the program is enabled AND valid. */
187 ctx
->VertexProgram
._Enabled
= ctx
->VertexProgram
.Enabled
188 && ctx
->VertexProgram
.Current
->Base
.Instructions
;
189 ctx
->FragmentProgram
._Enabled
= ctx
->FragmentProgram
.Enabled
190 && ctx
->FragmentProgram
.Current
->Base
.Instructions
;
191 ctx
->ATIFragmentShader
._Enabled
= ctx
->ATIFragmentShader
.Enabled
192 && ctx
->ATIFragmentShader
.Current
->Instructions
[0];
197 * Update vertex/fragment program state. In particular, update these fields:
198 * ctx->VertexProgram._Current
199 * ctx->VertexProgram._TnlProgram,
200 * These point to the highest priority enabled vertex/fragment program or are
201 * NULL if fixed-function processing is to be done.
203 * This function needs to be called after texture state validation in case
204 * we're generating a fragment program from fixed-function texture state.
207 update_program(GLcontext
*ctx
)
209 const struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
210 const struct gl_vertex_program
*prevVP
= ctx
->VertexProgram
._Current
;
211 const struct gl_fragment_program
*prevFP
= ctx
->FragmentProgram
._Current
;
214 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
215 * pointers to the programs that should be used for rendering. If either
216 * is NULL, use fixed-function code paths.
218 * These programs may come from several sources. The priority is as
220 * 1. OpenGL 2.0/ARB vertex/fragment shaders
221 * 2. ARB/NV vertex/fragment programs
222 * 3. Programs derived from fixed-function state.
224 * Note: it's possible for a vertex shader to get used with a fragment
225 * program (and vice versa) here, but in practice that shouldn't ever
226 * come up, or matter.
229 if (shProg
&& shProg
->LinkStatus
&& shProg
->FragmentProgram
) {
230 /* Use shader programs */
231 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
232 shProg
->FragmentProgram
);
234 else if (ctx
->FragmentProgram
._Enabled
) {
235 /* use user-defined vertex program */
236 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
237 ctx
->FragmentProgram
.Current
);
239 else if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
240 /* Use fragment program generated from fixed-function state.
242 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
243 _mesa_get_fixed_func_fragment_program(ctx
));
244 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
245 ctx
->FragmentProgram
._Current
);
248 /* no fragment program */
249 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
252 /* Examine vertex program after fragment program as
253 * _mesa_get_fixed_func_vertex_program() needs to know active
256 if (shProg
&& shProg
->LinkStatus
&& shProg
->VertexProgram
) {
257 /* Use shader programs */
258 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
259 shProg
->VertexProgram
);
261 else if (ctx
->VertexProgram
._Enabled
) {
262 /* use user-defined vertex program */
263 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
264 ctx
->VertexProgram
.Current
);
266 else if (ctx
->VertexProgram
._MaintainTnlProgram
) {
267 /* Use vertex program generated from fixed-function state.
269 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
270 _mesa_get_fixed_func_vertex_program(ctx
));
271 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._TnlProgram
,
272 ctx
->VertexProgram
._Current
);
275 /* no vertex program */
276 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
279 /* Let the driver know what's happening:
281 if (ctx
->FragmentProgram
._Current
!= prevFP
&& ctx
->Driver
.BindProgram
) {
282 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
283 (struct gl_program
*) ctx
->FragmentProgram
._Current
);
286 if (ctx
->VertexProgram
._Current
!= prevVP
&& ctx
->Driver
.BindProgram
) {
287 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
288 (struct gl_program
*) ctx
->VertexProgram
._Current
);
294 update_viewport_matrix(GLcontext
*ctx
)
296 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
298 ASSERT(depthMax
> 0);
300 /* Compute scale and bias values. This is really driver-specific
301 * and should be maintained elsewhere if at all.
302 * NOTE: RasterPos uses this.
304 _math_matrix_viewport(&ctx
->Viewport
._WindowMap
,
305 ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
306 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
,
307 ctx
->Viewport
.Near
, ctx
->Viewport
.Far
,
313 * Update derived multisample state.
316 update_multisample(GLcontext
*ctx
)
318 ctx
->Multisample
._Enabled
= GL_FALSE
;
319 if (ctx
->Multisample
.Enabled
&&
321 ctx
->DrawBuffer
->Visual
.sampleBuffers
)
322 ctx
->Multisample
._Enabled
= GL_TRUE
;
327 * Update derived color/blend/logicop state.
330 update_color(GLcontext
*ctx
)
332 /* This is needed to support 1.1's RGB logic ops AND
333 * 1.0's blending logicops.
335 ctx
->Color
._LogicOpEnabled
= RGBA_LOGICOP_ENABLED(ctx
);
340 * Check polygon state and set DD_TRI_CULL_FRONT_BACK and/or DD_TRI_OFFSET
341 * in ctx->_TriangleCaps if needed.
344 update_polygon(GLcontext
*ctx
)
346 ctx
->_TriangleCaps
&= ~(DD_TRI_CULL_FRONT_BACK
| DD_TRI_OFFSET
);
348 if (ctx
->Polygon
.CullFlag
&& ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
349 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
351 if ( ctx
->Polygon
.OffsetPoint
352 || ctx
->Polygon
.OffsetLine
353 || ctx
->Polygon
.OffsetFill
)
354 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
359 * Update the ctx->_TriangleCaps bitfield.
360 * XXX that bitfield should really go away someday!
361 * This function must be called after other update_*() functions since
362 * there are dependencies on some other derived values.
366 update_tricaps(GLcontext
*ctx
, GLbitfield new_state
)
368 ctx
->_TriangleCaps
= 0;
373 if (1/*new_state & _NEW_POINT*/) {
374 if (ctx
->Point
.SmoothFlag
)
375 ctx
->_TriangleCaps
|= DD_POINT_SMOOTH
;
376 if (ctx
->Point
.Size
!= 1.0F
)
377 ctx
->_TriangleCaps
|= DD_POINT_SIZE
;
378 if (ctx
->Point
._Attenuated
)
379 ctx
->_TriangleCaps
|= DD_POINT_ATTEN
;
385 if (1/*new_state & _NEW_LINE*/) {
386 if (ctx
->Line
.SmoothFlag
)
387 ctx
->_TriangleCaps
|= DD_LINE_SMOOTH
;
388 if (ctx
->Line
.StippleFlag
)
389 ctx
->_TriangleCaps
|= DD_LINE_STIPPLE
;
390 if (ctx
->Line
.Width
!= 1.0)
391 ctx
->_TriangleCaps
|= DD_LINE_WIDTH
;
397 if (1/*new_state & _NEW_POLYGON*/) {
398 if (ctx
->Polygon
.SmoothFlag
)
399 ctx
->_TriangleCaps
|= DD_TRI_SMOOTH
;
400 if (ctx
->Polygon
.StippleFlag
)
401 ctx
->_TriangleCaps
|= DD_TRI_STIPPLE
;
402 if (ctx
->Polygon
.FrontMode
!= GL_FILL
403 || ctx
->Polygon
.BackMode
!= GL_FILL
)
404 ctx
->_TriangleCaps
|= DD_TRI_UNFILLED
;
405 if (ctx
->Polygon
.CullFlag
406 && ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
)
407 ctx
->_TriangleCaps
|= DD_TRI_CULL_FRONT_BACK
;
408 if (ctx
->Polygon
.OffsetPoint
||
409 ctx
->Polygon
.OffsetLine
||
410 ctx
->Polygon
.OffsetFill
)
411 ctx
->_TriangleCaps
|= DD_TRI_OFFSET
;
415 * Lighting and shading
417 if (ctx
->Light
.Enabled
&& ctx
->Light
.Model
.TwoSide
)
418 ctx
->_TriangleCaps
|= DD_TRI_LIGHT_TWOSIDE
;
419 if (ctx
->Light
.ShadeModel
== GL_FLAT
)
420 ctx
->_TriangleCaps
|= DD_FLATSHADE
;
421 if (NEED_SECONDARY_COLOR(ctx
))
422 ctx
->_TriangleCaps
|= DD_SEPARATE_SPECULAR
;
427 if (ctx
->Stencil
._TestTwoSide
)
428 ctx
->_TriangleCaps
|= DD_TRI_TWOSTENCIL
;
434 * Compute derived GL state.
435 * If __GLcontextRec::NewState is non-zero then this function \b must
436 * be called before rendering anything.
438 * Calls dd_function_table::UpdateState to perform any internal state
439 * management necessary.
441 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
442 * _mesa_update_buffer_bounds(),
443 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
446 _mesa_update_state_locked( GLcontext
*ctx
)
448 GLbitfield new_state
= ctx
->NewState
;
449 GLbitfield prog_flags
= _NEW_PROGRAM
;
451 if (new_state
== _NEW_CURRENT_ATTRIB
)
454 if (MESA_VERBOSE
& VERBOSE_STATE
)
455 _mesa_print_state("_mesa_update_state", new_state
);
457 /* Determine which state flags effect vertex/fragment program state */
458 if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
459 prog_flags
|= (_NEW_TEXTURE
| _NEW_FOG
| _DD_NEW_SEPARATE_SPECULAR
);
461 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
462 prog_flags
|= (_NEW_ARRAY
| _NEW_TEXTURE
| _NEW_TEXTURE_MATRIX
|
463 _NEW_TRANSFORM
| _NEW_POINT
|
464 _NEW_FOG
| _NEW_LIGHT
|
465 _MESA_NEW_NEED_EYE_COORDS
);
469 * Now update derived state info
472 if (new_state
& prog_flags
)
473 update_program_enables( ctx
);
475 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
476 _mesa_update_modelview_project( ctx
, new_state
);
478 if (new_state
& (_NEW_PROGRAM
|_NEW_TEXTURE
|_NEW_TEXTURE_MATRIX
))
479 _mesa_update_texture( ctx
, new_state
);
481 if (new_state
& _NEW_BUFFERS
)
482 _mesa_update_framebuffer(ctx
);
484 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
485 _mesa_update_draw_buffer_bounds( ctx
);
487 if (new_state
& _NEW_POLYGON
)
488 update_polygon( ctx
);
490 if (new_state
& _NEW_LIGHT
)
491 _mesa_update_lighting( ctx
);
493 if (new_state
& (_NEW_STENCIL
| _NEW_BUFFERS
))
494 _mesa_update_stencil( ctx
);
496 #if FEATURE_pixel_transfer
497 if (new_state
& _MESA_NEW_TRANSFER_STATE
)
498 _mesa_update_pixel( ctx
, new_state
);
501 if (new_state
& _DD_NEW_SEPARATE_SPECULAR
)
502 update_separate_specular( ctx
);
504 if (new_state
& (_NEW_ARRAY
| _NEW_PROGRAM
))
505 update_arrays( ctx
);
507 if (new_state
& (_NEW_BUFFERS
| _NEW_VIEWPORT
))
508 update_viewport_matrix(ctx
);
510 if (new_state
& _NEW_MULTISAMPLE
)
511 update_multisample( ctx
);
513 if (new_state
& _NEW_COLOR
)
517 if (new_state
& (_NEW_POINT
| _NEW_LINE
| _NEW_POLYGON
| _NEW_LIGHT
518 | _NEW_STENCIL
| _DD_NEW_SEPARATE_SPECULAR
))
519 update_tricaps( ctx
, new_state
);
522 /* ctx->_NeedEyeCoords is now up to date.
524 * If the truth value of this variable has changed, update for the
525 * new lighting space and recompute the positions of lights and the
528 * If the lighting space hasn't changed, may still need to recompute
529 * light positions & normal transforms for other reasons.
531 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
532 _mesa_update_tnl_spaces( ctx
, new_state
);
534 if (new_state
& prog_flags
)
535 update_program( ctx
);
538 * Give the driver a chance to act upon the new_state flags.
539 * The driver might plug in different span functions, for example.
540 * Also, this is where the driver can invalidate the state of any
541 * active modules (such as swrast_setup, swrast, tnl, etc).
543 * Set ctx->NewState to zero to avoid recursion if
544 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
547 new_state
= ctx
->NewState
;
549 ctx
->Driver
.UpdateState(ctx
, new_state
);
550 ctx
->Array
.NewState
= 0;
554 /* This is the usual entrypoint for state updates:
557 _mesa_update_state( GLcontext
*ctx
)
559 _mesa_lock_context_textures(ctx
);
560 _mesa_update_state_locked(ctx
);
561 _mesa_unlock_context_textures(ctx
);
568 * Want to figure out which fragment program inputs are actually
569 * constant/current values from ctx->Current. These should be
570 * referenced as a tracked state variable rather than a fragment
571 * program input, to save the overhead of putting a constant value in
572 * every submitted vertex, transferring it to hardware, interpolating
573 * it across the triangle, etc...
575 * When there is a VP bound, just use vp->outputs. But when we're
576 * generating vp from fixed function state, basically want to
579 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
580 * potential_vp_outputs )
582 * Where potential_vp_outputs is calculated by looking at enabled
585 * The generated fragment program should then only declare inputs that
586 * may vary or otherwise differ from the ctx->Current values.
587 * Otherwise, the fp should track them as state values instead.
590 _mesa_set_varying_vp_inputs( GLcontext
*ctx
,
591 GLbitfield varying_inputs
)
593 if (ctx
->varying_vp_inputs
!= varying_inputs
) {
594 ctx
->varying_vp_inputs
= varying_inputs
;
595 ctx
->NewState
|= _NEW_ARRAY
;
596 /*_mesa_printf("%s %x\n", __FUNCTION__, varying_inputs);*/
602 * Used by drivers to tell core Mesa that the driver is going to
603 * install/ use its own vertex program. In particular, this will
604 * prevent generated fragment programs from using state vars instead
605 * of ordinary varyings/inputs.
608 _mesa_set_vp_override(GLcontext
*ctx
, GLboolean flag
)
610 if (ctx
->VertexProgram
._Overriden
!= flag
) {
611 ctx
->VertexProgram
._Overriden
= flag
;
613 /* Set one of the bits which will trigger fragment program
616 ctx
->NewState
|= _NEW_ARRAY
;