2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
30 * This file manages recalculation of derived values in struct gl_context.
40 #include "ffvertex_prog.h"
41 #include "framebuffer.h"
45 #include "program/program.h"
46 #include "program/prog_parameter.h"
47 #include "shaderobj.h"
50 #include "texenvprogram.h"
54 #include "vbo/vbo_context.h"
60 * Update the ctx->*Program._Current pointers to point to the
61 * current/active programs.
63 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
64 * programs or programs derived from fixed-function state.
66 * This function needs to be called after texture state validation in case
67 * we're generating a fragment program from fixed-function texture state.
69 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
70 * or fragment program is being used.
73 update_program(struct gl_context
*ctx
)
75 struct gl_program
*vsProg
=
76 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
77 struct gl_program
*tcsProg
=
78 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
79 struct gl_program
*tesProg
=
80 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
81 struct gl_program
*gsProg
=
82 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
83 struct gl_program
*fsProg
=
84 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
85 struct gl_program
*csProg
=
86 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
87 const struct gl_program
*prevVP
= ctx
->VertexProgram
._Current
;
88 const struct gl_program
*prevFP
= ctx
->FragmentProgram
._Current
;
89 const struct gl_program
*prevGP
= ctx
->GeometryProgram
._Current
;
90 const struct gl_program
*prevTCP
= ctx
->TessCtrlProgram
._Current
;
91 const struct gl_program
*prevTEP
= ctx
->TessEvalProgram
._Current
;
92 const struct gl_program
*prevCP
= ctx
->ComputeProgram
._Current
;
95 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
96 * pointers to the programs that should be used for rendering. If either
97 * is NULL, use fixed-function code paths.
99 * These programs may come from several sources. The priority is as
101 * 1. OpenGL 2.0/ARB vertex/fragment shaders
102 * 2. ARB/NV vertex/fragment programs
103 * 3. ATI fragment shader
104 * 4. Programs derived from fixed-function state.
106 * Note: it's possible for a vertex shader to get used with a fragment
107 * program (and vice versa) here, but in practice that shouldn't ever
108 * come up, or matter.
112 /* Use GLSL fragment shader */
113 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
, fsProg
);
114 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
117 else if (_mesa_arb_fragment_program_enabled(ctx
)) {
118 /* Use user-defined fragment program */
119 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
,
120 ctx
->FragmentProgram
.Current
);
121 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
124 else if (_mesa_ati_fragment_shader_enabled(ctx
) &&
125 ctx
->ATIFragmentShader
.Current
->Program
) {
126 /* Use the enabled ATI fragment shader's associated program */
127 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
,
128 ctx
->ATIFragmentShader
.Current
->Program
);
129 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
132 else if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
133 /* Use fragment program generated from fixed-function state */
134 struct gl_shader_program
*f
= _mesa_get_fixed_func_fragment_program(ctx
);
136 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
,
137 f
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
);
138 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
139 f
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
);
142 /* No fragment program */
143 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
144 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
149 /* Use GLSL geometry shader */
150 _mesa_reference_program(ctx
, &ctx
->GeometryProgram
._Current
, gsProg
);
152 /* No geometry program */
153 _mesa_reference_program(ctx
, &ctx
->GeometryProgram
._Current
, NULL
);
157 /* Use GLSL tessellation evaluation shader */
158 _mesa_reference_program(ctx
, &ctx
->TessEvalProgram
._Current
, tesProg
);
161 /* No tessellation evaluation program */
162 _mesa_reference_program(ctx
, &ctx
->TessEvalProgram
._Current
, NULL
);
166 /* Use GLSL tessellation control shader */
167 _mesa_reference_program(ctx
, &ctx
->TessCtrlProgram
._Current
, tcsProg
);
170 /* No tessellation control program */
171 _mesa_reference_program(ctx
, &ctx
->TessCtrlProgram
._Current
, NULL
);
174 /* Examine vertex program after fragment program as
175 * _mesa_get_fixed_func_vertex_program() needs to know active
179 /* Use GLSL vertex shader */
180 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._Current
, vsProg
);
182 else if (_mesa_arb_vertex_program_enabled(ctx
)) {
183 /* Use user-defined vertex program */
184 _mesa_reference_program(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_program(ctx
, &ctx
->VertexProgram
._Current
,
190 _mesa_get_fixed_func_vertex_program(ctx
));
191 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._TnlProgram
,
192 ctx
->VertexProgram
._Current
);
195 /* no vertex program */
196 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
200 /* Use GLSL compute shader */
201 _mesa_reference_program(ctx
, &ctx
->ComputeProgram
._Current
, csProg
);
203 /* no compute program */
204 _mesa_reference_program(ctx
, &ctx
->ComputeProgram
._Current
, NULL
);
207 /* Let the driver know what's happening:
209 if (ctx
->FragmentProgram
._Current
!= prevFP
||
210 ctx
->VertexProgram
._Current
!= prevVP
||
211 ctx
->GeometryProgram
._Current
!= prevGP
||
212 ctx
->TessEvalProgram
._Current
!= prevTEP
||
213 ctx
->TessCtrlProgram
._Current
!= prevTCP
||
214 ctx
->ComputeProgram
._Current
!= prevCP
)
222 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
225 update_program_constants(struct gl_context
*ctx
)
227 GLbitfield new_state
= 0x0;
229 if (ctx
->FragmentProgram
._Current
) {
230 const struct gl_program_parameter_list
*params
=
231 ctx
->FragmentProgram
._Current
->Parameters
;
232 if (params
&& params
->StateFlags
& ctx
->NewState
) {
233 if (ctx
->DriverFlags
.NewShaderConstants
[MESA_SHADER_FRAGMENT
]) {
234 ctx
->NewDriverState
|=
235 ctx
->DriverFlags
.NewShaderConstants
[MESA_SHADER_FRAGMENT
];
237 new_state
|= _NEW_PROGRAM_CONSTANTS
;
242 /* Don't handle tessellation and geometry shaders here. They don't use
243 * any state constants.
246 if (ctx
->VertexProgram
._Current
) {
247 const struct gl_program_parameter_list
*params
=
248 ctx
->VertexProgram
._Current
->Parameters
;
249 if (params
&& params
->StateFlags
& ctx
->NewState
) {
250 if (ctx
->DriverFlags
.NewShaderConstants
[MESA_SHADER_VERTEX
]) {
251 ctx
->NewDriverState
|=
252 ctx
->DriverFlags
.NewShaderConstants
[MESA_SHADER_VERTEX
];
254 new_state
|= _NEW_PROGRAM_CONSTANTS
;
264 * Compute derived GL state.
265 * If __struct gl_contextRec::NewState is non-zero then this function \b must
266 * be called before rendering anything.
268 * Calls dd_function_table::UpdateState to perform any internal state
269 * management necessary.
271 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
272 * _mesa_update_buffer_bounds(),
273 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
276 _mesa_update_state_locked( struct gl_context
*ctx
)
278 GLbitfield new_state
= ctx
->NewState
;
279 GLbitfield new_prog_state
= 0x0;
280 const GLbitfield computed_states
= ~(_NEW_CURRENT_ATTRIB
| _NEW_LINE
);
282 /* we can skip a bunch of state validation checks if the dirty
283 * state matches one or more bits in 'computed_states'.
285 if ((new_state
& computed_states
) == 0)
288 if (MESA_VERBOSE
& VERBOSE_STATE
)
289 _mesa_print_state("_mesa_update_state", new_state
);
291 if (new_state
& _NEW_BUFFERS
)
292 _mesa_update_framebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
);
294 /* Handle Core and Compatibility contexts separately. */
295 if (ctx
->API
== API_OPENGL_COMPAT
||
296 ctx
->API
== API_OPENGLES
) {
297 GLbitfield prog_flags
= _NEW_PROGRAM
;
299 /* Determine which state flags effect vertex/fragment program state */
300 if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
301 prog_flags
|= (_NEW_BUFFERS
| _NEW_TEXTURE_OBJECT
| _NEW_FOG
|
302 _NEW_VARYING_VP_INPUTS
| _NEW_LIGHT
| _NEW_POINT
|
303 _NEW_RENDERMODE
| _NEW_PROGRAM
| _NEW_FRAG_CLAMP
|
304 _NEW_COLOR
| _NEW_TEXTURE_STATE
);
306 if (ctx
->VertexProgram
._MaintainTnlProgram
) {
307 prog_flags
|= (_NEW_VARYING_VP_INPUTS
| _NEW_TEXTURE_OBJECT
|
308 _NEW_TEXTURE_MATRIX
| _NEW_TRANSFORM
| _NEW_POINT
|
309 _NEW_FOG
| _NEW_LIGHT
| _NEW_TEXTURE_STATE
|
310 _MESA_NEW_NEED_EYE_COORDS
);
314 * Now update derived state info
316 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
317 _mesa_update_modelview_project( ctx
, new_state
);
319 if (new_state
& _NEW_TEXTURE_MATRIX
)
320 _mesa_update_texture_matrices(ctx
);
322 if (new_state
& (_NEW_TEXTURE_OBJECT
| _NEW_TEXTURE_STATE
| _NEW_PROGRAM
))
323 _mesa_update_texture_state(ctx
);
325 if (new_state
& _NEW_LIGHT
)
326 _mesa_update_lighting(ctx
);
328 if (new_state
& _NEW_PIXEL
)
329 _mesa_update_pixel( ctx
);
331 /* ctx->_NeedEyeCoords is now up to date.
333 * If the truth value of this variable has changed, update for the
334 * new lighting space and recompute the positions of lights and the
337 * If the lighting space hasn't changed, may still need to recompute
338 * light positions & normal transforms for other reasons.
340 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
341 _mesa_update_tnl_spaces( ctx
, new_state
);
343 if (new_state
& prog_flags
) {
344 /* When we generate programs from fixed-function vertex/fragment state
345 * this call may generate/bind a new program. If so, we need to
346 * propogate the _NEW_PROGRAM flag to the driver.
348 new_prog_state
|= update_program(ctx
);
351 /* GL Core and GLES 2/3 contexts */
352 if (new_state
& (_NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
))
353 _mesa_update_texture_state(ctx
);
355 if (new_state
& _NEW_PROGRAM
)
359 if (new_state
& _NEW_ARRAY
)
360 _mesa_update_vao_client_arrays(ctx
, ctx
->Array
.VAO
);
363 new_prog_state
|= update_program_constants(ctx
);
365 ctx
->NewState
|= new_prog_state
;
366 vbo_exec_invalidate_state(ctx
);
369 * Give the driver a chance to act upon the new_state flags.
370 * The driver might plug in different span functions, for example.
371 * Also, this is where the driver can invalidate the state of any
372 * active modules (such as swrast_setup, swrast, tnl, etc).
374 ctx
->Driver
.UpdateState(ctx
);
376 ctx
->Array
.VAO
->NewArrays
= 0x0;
380 /* This is the usual entrypoint for state updates:
383 _mesa_update_state( struct gl_context
*ctx
)
385 _mesa_lock_context_textures(ctx
);
386 _mesa_update_state_locked(ctx
);
387 _mesa_unlock_context_textures(ctx
);
394 * Want to figure out which fragment program inputs are actually
395 * constant/current values from ctx->Current. These should be
396 * referenced as a tracked state variable rather than a fragment
397 * program input, to save the overhead of putting a constant value in
398 * every submitted vertex, transferring it to hardware, interpolating
399 * it across the triangle, etc...
401 * When there is a VP bound, just use vp->outputs. But when we're
402 * generating vp from fixed function state, basically want to
405 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
406 * potential_vp_outputs )
408 * Where potential_vp_outputs is calculated by looking at enabled
411 * The generated fragment program should then only declare inputs that
412 * may vary or otherwise differ from the ctx->Current values.
413 * Otherwise, the fp should track them as state values instead.
416 _mesa_set_varying_vp_inputs( struct gl_context
*ctx
,
417 GLbitfield64 varying_inputs
)
419 if (ctx
->API
!= API_OPENGL_COMPAT
&&
420 ctx
->API
!= API_OPENGLES
)
423 if (ctx
->varying_vp_inputs
!= varying_inputs
) {
424 ctx
->varying_vp_inputs
= varying_inputs
;
426 /* Only the fixed-func generated programs need to use the flag
427 * and the fixed-func fragment program uses it only if there is also
428 * a fixed-func vertex program, so this only depends on the latter.
430 * It's okay to check the VP pointer here, because this is called after
431 * _mesa_update_state in the vbo module. */
432 if (ctx
->VertexProgram
._TnlProgram
||
433 ctx
->FragmentProgram
._TexEnvProgram
) {
434 ctx
->NewState
|= _NEW_VARYING_VP_INPUTS
;
436 /*printf("%s %x\n", __func__, varying_inputs);*/
442 * Used by drivers to tell core Mesa that the driver is going to
443 * install/ use its own vertex program. In particular, this will
444 * prevent generated fragment programs from using state vars instead
445 * of ordinary varyings/inputs.
448 _mesa_set_vp_override(struct gl_context
*ctx
, GLboolean flag
)
450 if (ctx
->VertexProgram
._Overriden
!= flag
) {
451 ctx
->VertexProgram
._Overriden
= flag
;
453 /* Set one of the bits which will trigger fragment program
456 ctx
->NewState
|= _NEW_PROGRAM
;