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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
31 * This file manages recalculation of derived values in struct gl_context.
41 #include "ffvertex_prog.h"
42 #include "framebuffer.h"
46 #include "program/program.h"
47 #include "program/prog_parameter.h"
48 #include "shaderobj.h"
51 #include "texenvprogram.h"
59 * Update the following fields:
60 * ctx->VertexProgram._Enabled
61 * ctx->FragmentProgram._Enabled
62 * ctx->ATIFragmentShader._Enabled
63 * This needs to be done before texture state validation.
66 update_program_enables(struct gl_context
*ctx
)
68 /* These _Enabled flags indicate if the user-defined ARB/NV vertex/fragment
69 * program is enabled AND valid. Similarly for ATI fragment shaders.
70 * GLSL shaders not relevant here.
72 ctx
->VertexProgram
._Enabled
= ctx
->VertexProgram
.Enabled
73 && ctx
->VertexProgram
.Current
->Base
.Instructions
;
74 ctx
->FragmentProgram
._Enabled
= ctx
->FragmentProgram
.Enabled
75 && ctx
->FragmentProgram
.Current
->Base
.Instructions
;
76 ctx
->ATIFragmentShader
._Enabled
= ctx
->ATIFragmentShader
.Enabled
77 && ctx
->ATIFragmentShader
.Current
->Instructions
[0];
82 * Update the ctx->Vertex/Geometry/FragmentProgram._Current pointers to point
83 * to the current/active programs. Then call ctx->Driver.BindProgram() to
84 * tell the driver which programs to use.
86 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
87 * programs or programs derived from fixed-function state.
89 * This function needs to be called after texture state validation in case
90 * we're generating a fragment program from fixed-function texture state.
92 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
93 * or fragment program is being used.
96 update_program(struct gl_context
*ctx
)
98 const struct gl_shader_program
*vsProg
= ctx
->Shader
.CurrentVertexProgram
;
99 const struct gl_shader_program
*gsProg
= ctx
->Shader
.CurrentGeometryProgram
;
100 struct gl_shader_program
*fsProg
= ctx
->Shader
.CurrentFragmentProgram
;
101 const struct gl_vertex_program
*prevVP
= ctx
->VertexProgram
._Current
;
102 const struct gl_fragment_program
*prevFP
= ctx
->FragmentProgram
._Current
;
103 const struct gl_geometry_program
*prevGP
= ctx
->GeometryProgram
._Current
;
104 GLbitfield new_state
= 0x0;
107 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
108 * pointers to the programs that should be used for rendering. If either
109 * is NULL, use fixed-function code paths.
111 * These programs may come from several sources. The priority is as
113 * 1. OpenGL 2.0/ARB vertex/fragment shaders
114 * 2. ARB/NV vertex/fragment programs
115 * 3. Programs derived from fixed-function state.
117 * Note: it's possible for a vertex shader to get used with a fragment
118 * program (and vice versa) here, but in practice that shouldn't ever
119 * come up, or matter.
122 if (fsProg
&& fsProg
->LinkStatus
123 && fsProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]) {
124 /* Use GLSL fragment shader */
125 _mesa_reference_shader_program(ctx
,
126 &ctx
->Shader
._CurrentFragmentProgram
,
128 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
129 gl_fragment_program(fsProg
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
));
130 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
133 else if (ctx
->FragmentProgram
._Enabled
) {
134 /* Use user-defined fragment program */
135 _mesa_reference_shader_program(ctx
,
136 &ctx
->Shader
._CurrentFragmentProgram
,
138 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
139 ctx
->FragmentProgram
.Current
);
140 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
143 else if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
144 /* Use fragment program generated from fixed-function state */
145 struct gl_shader_program
*f
= _mesa_get_fixed_func_fragment_program(ctx
);
147 _mesa_reference_shader_program(ctx
,
148 &ctx
->Shader
._CurrentFragmentProgram
,
150 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
,
151 gl_fragment_program(f
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
));
152 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
153 gl_fragment_program(f
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
));
156 /* No fragment program */
157 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
158 _mesa_reference_fragprog(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
162 if (gsProg
&& gsProg
->LinkStatus
163 && gsProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]) {
164 /* Use GLSL geometry shader */
165 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
._Current
,
166 gl_geometry_program(gsProg
->_LinkedShaders
[MESA_SHADER_GEOMETRY
]->Program
));
168 /* No geometry program */
169 _mesa_reference_geomprog(ctx
, &ctx
->GeometryProgram
._Current
, NULL
);
172 /* Examine vertex program after fragment program as
173 * _mesa_get_fixed_func_vertex_program() needs to know active
176 if (vsProg
&& vsProg
->LinkStatus
177 && vsProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]) {
178 /* Use GLSL vertex shader */
179 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
180 gl_vertex_program(vsProg
->_LinkedShaders
[MESA_SHADER_VERTEX
]->Program
));
182 else if (ctx
->VertexProgram
._Enabled
) {
183 /* Use user-defined vertex program */
184 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
185 ctx
->VertexProgram
.Current
);
187 else if (ctx
->VertexProgram
._MaintainTnlProgram
) {
188 /* Use vertex program generated from fixed-function state */
189 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
,
190 _mesa_get_fixed_func_vertex_program(ctx
));
191 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._TnlProgram
,
192 ctx
->VertexProgram
._Current
);
195 /* no vertex program */
196 _mesa_reference_vertprog(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
199 /* Let the driver know what's happening:
201 if (ctx
->FragmentProgram
._Current
!= prevFP
) {
202 new_state
|= _NEW_PROGRAM
;
203 if (ctx
->Driver
.BindProgram
) {
204 ctx
->Driver
.BindProgram(ctx
, GL_FRAGMENT_PROGRAM_ARB
,
205 (struct gl_program
*) ctx
->FragmentProgram
._Current
);
209 if (ctx
->GeometryProgram
._Current
!= prevGP
) {
210 new_state
|= _NEW_PROGRAM
;
211 if (ctx
->Driver
.BindProgram
) {
212 ctx
->Driver
.BindProgram(ctx
, MESA_GEOMETRY_PROGRAM
,
213 (struct gl_program
*) ctx
->GeometryProgram
._Current
);
217 if (ctx
->VertexProgram
._Current
!= prevVP
) {
218 new_state
|= _NEW_PROGRAM
;
219 if (ctx
->Driver
.BindProgram
) {
220 ctx
->Driver
.BindProgram(ctx
, GL_VERTEX_PROGRAM_ARB
,
221 (struct gl_program
*) ctx
->VertexProgram
._Current
);
230 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
233 update_program_constants(struct gl_context
*ctx
)
235 GLbitfield new_state
= 0x0;
237 if (ctx
->FragmentProgram
._Current
) {
238 const struct gl_program_parameter_list
*params
=
239 ctx
->FragmentProgram
._Current
->Base
.Parameters
;
240 if (params
&& params
->StateFlags
& ctx
->NewState
) {
241 new_state
|= _NEW_PROGRAM_CONSTANTS
;
245 if (ctx
->GeometryProgram
._Current
) {
246 const struct gl_program_parameter_list
*params
=
247 ctx
->GeometryProgram
._Current
->Base
.Parameters
;
248 /*FIXME: StateFlags is always 0 because we have unnamed constant
249 * not state changes */
250 if (params
/*&& params->StateFlags & ctx->NewState*/) {
251 new_state
|= _NEW_PROGRAM_CONSTANTS
;
255 if (ctx
->VertexProgram
._Current
) {
256 const struct gl_program_parameter_list
*params
=
257 ctx
->VertexProgram
._Current
->Base
.Parameters
;
258 if (params
&& params
->StateFlags
& ctx
->NewState
) {
259 new_state
|= _NEW_PROGRAM_CONSTANTS
;
270 update_viewport_matrix(struct gl_context
*ctx
)
272 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
274 ASSERT(depthMax
> 0);
276 /* Compute scale and bias values. This is really driver-specific
277 * and should be maintained elsewhere if at all.
278 * NOTE: RasterPos uses this.
280 _math_matrix_viewport(&ctx
->Viewport
._WindowMap
,
281 ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
282 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
,
283 ctx
->Viewport
.Near
, ctx
->Viewport
.Far
,
289 * Update derived multisample state.
292 update_multisample(struct gl_context
*ctx
)
294 ctx
->Multisample
._Enabled
= GL_FALSE
;
295 if (ctx
->Multisample
.Enabled
&&
297 ctx
->DrawBuffer
->Visual
.sampleBuffers
)
298 ctx
->Multisample
._Enabled
= GL_TRUE
;
303 * Update the ctx->VertexProgram._TwoSideEnabled flag.
306 update_twoside(struct gl_context
*ctx
)
308 if (ctx
->Shader
.CurrentVertexProgram
||
309 ctx
->VertexProgram
._Enabled
) {
310 ctx
->VertexProgram
._TwoSideEnabled
= ctx
->VertexProgram
.TwoSideEnabled
;
312 ctx
->VertexProgram
._TwoSideEnabled
= (ctx
->Light
.Enabled
&&
313 ctx
->Light
.Model
.TwoSide
);
319 * Compute derived GL state.
320 * If __struct gl_contextRec::NewState is non-zero then this function \b must
321 * be called before rendering anything.
323 * Calls dd_function_table::UpdateState to perform any internal state
324 * management necessary.
326 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
327 * _mesa_update_buffer_bounds(),
328 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
331 _mesa_update_state_locked( struct gl_context
*ctx
)
333 GLbitfield new_state
= ctx
->NewState
;
334 GLbitfield prog_flags
= _NEW_PROGRAM
;
335 GLbitfield new_prog_state
= 0x0;
337 if (new_state
== _NEW_CURRENT_ATTRIB
)
340 if (MESA_VERBOSE
& VERBOSE_STATE
)
341 _mesa_print_state("_mesa_update_state", new_state
);
343 /* Determine which state flags effect vertex/fragment program state */
344 if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
345 prog_flags
|= (_NEW_BUFFERS
| _NEW_TEXTURE
| _NEW_FOG
|
346 _NEW_VARYING_VP_INPUTS
| _NEW_LIGHT
| _NEW_POINT
|
347 _NEW_RENDERMODE
| _NEW_PROGRAM
| _NEW_FRAG_CLAMP
|
350 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
351 prog_flags
|= (_NEW_VARYING_VP_INPUTS
| _NEW_TEXTURE
|
352 _NEW_TEXTURE_MATRIX
| _NEW_TRANSFORM
| _NEW_POINT
|
353 _NEW_FOG
| _NEW_LIGHT
|
354 _MESA_NEW_NEED_EYE_COORDS
);
358 * Now update derived state info
361 if (new_state
& prog_flags
)
362 update_program_enables( ctx
);
364 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
365 _mesa_update_modelview_project( ctx
, new_state
);
367 if (new_state
& (_NEW_PROGRAM
|_NEW_TEXTURE
|_NEW_TEXTURE_MATRIX
))
368 _mesa_update_texture( ctx
, new_state
);
370 if (new_state
& _NEW_BUFFERS
)
371 _mesa_update_framebuffer(ctx
);
373 if (new_state
& (_NEW_SCISSOR
| _NEW_BUFFERS
| _NEW_VIEWPORT
))
374 _mesa_update_draw_buffer_bounds( ctx
);
376 if (new_state
& _NEW_LIGHT
)
377 _mesa_update_lighting( ctx
);
379 if (new_state
& (_NEW_LIGHT
| _NEW_PROGRAM
))
380 update_twoside( ctx
);
382 if (new_state
& (_NEW_STENCIL
| _NEW_BUFFERS
))
383 _mesa_update_stencil( ctx
);
385 if (new_state
& _NEW_PIXEL
)
386 _mesa_update_pixel( ctx
, new_state
);
388 if (new_state
& (_NEW_BUFFERS
| _NEW_VIEWPORT
))
389 update_viewport_matrix(ctx
);
391 if (new_state
& (_NEW_MULTISAMPLE
| _NEW_BUFFERS
))
392 update_multisample( ctx
);
394 /* ctx->_NeedEyeCoords is now up to date.
396 * If the truth value of this variable has changed, update for the
397 * new lighting space and recompute the positions of lights and the
400 * If the lighting space hasn't changed, may still need to recompute
401 * light positions & normal transforms for other reasons.
403 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
404 _mesa_update_tnl_spaces( ctx
, new_state
);
406 if (new_state
& prog_flags
) {
407 /* When we generate programs from fixed-function vertex/fragment state
408 * this call may generate/bind a new program. If so, we need to
409 * propogate the _NEW_PROGRAM flag to the driver.
411 new_prog_state
|= update_program( ctx
);
414 if (ctx
->Const
.CheckArrayBounds
&&
415 new_state
& (_NEW_ARRAY
| _NEW_PROGRAM
| _NEW_BUFFER_OBJECT
)) {
416 _mesa_update_array_object_max_element(ctx
, ctx
->Array
.ArrayObj
);
420 new_prog_state
|= update_program_constants(ctx
);
423 * Give the driver a chance to act upon the new_state flags.
424 * The driver might plug in different span functions, for example.
425 * Also, this is where the driver can invalidate the state of any
426 * active modules (such as swrast_setup, swrast, tnl, etc).
428 * Set ctx->NewState to zero to avoid recursion if
429 * Driver.UpdateState() has to call FLUSH_VERTICES(). (fixed?)
431 new_state
= ctx
->NewState
| new_prog_state
;
433 ctx
->Driver
.UpdateState(ctx
, new_state
);
437 /* This is the usual entrypoint for state updates:
440 _mesa_update_state( struct gl_context
*ctx
)
442 _mesa_lock_context_textures(ctx
);
443 _mesa_update_state_locked(ctx
);
444 _mesa_unlock_context_textures(ctx
);
451 * Want to figure out which fragment program inputs are actually
452 * constant/current values from ctx->Current. These should be
453 * referenced as a tracked state variable rather than a fragment
454 * program input, to save the overhead of putting a constant value in
455 * every submitted vertex, transferring it to hardware, interpolating
456 * it across the triangle, etc...
458 * When there is a VP bound, just use vp->outputs. But when we're
459 * generating vp from fixed function state, basically want to
462 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
463 * potential_vp_outputs )
465 * Where potential_vp_outputs is calculated by looking at enabled
468 * The generated fragment program should then only declare inputs that
469 * may vary or otherwise differ from the ctx->Current values.
470 * Otherwise, the fp should track them as state values instead.
473 _mesa_set_varying_vp_inputs( struct gl_context
*ctx
,
474 GLbitfield64 varying_inputs
)
476 if (ctx
->varying_vp_inputs
!= varying_inputs
) {
477 ctx
->varying_vp_inputs
= varying_inputs
;
479 /* Only the fixed-func generated programs need to use the flag
480 * and the fixed-func fragment program uses it only if there is also
481 * a fixed-func vertex program, so this only depends on the latter.
483 * It's okay to check the VP pointer here, because this is called after
484 * _mesa_update_state in the vbo module. */
485 if (ctx
->VertexProgram
._TnlProgram
||
486 ctx
->FragmentProgram
._TexEnvProgram
) {
487 ctx
->NewState
|= _NEW_VARYING_VP_INPUTS
;
489 /*printf("%s %x\n", __FUNCTION__, varying_inputs);*/
495 * Used by drivers to tell core Mesa that the driver is going to
496 * install/ use its own vertex program. In particular, this will
497 * prevent generated fragment programs from using state vars instead
498 * of ordinary varyings/inputs.
501 _mesa_set_vp_override(struct gl_context
*ctx
, GLboolean flag
)
503 if (ctx
->VertexProgram
._Overriden
!= flag
) {
504 ctx
->VertexProgram
._Overriden
= flag
;
506 /* Set one of the bits which will trigger fragment program
509 ctx
->NewState
|= _NEW_PROGRAM
;