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"
60 _mesa_update_allow_draw_out_of_order(struct gl_context
*ctx
)
62 /* Out-of-order drawing is useful when vertex array draws and immediate
63 * mode are interleaved.
65 * Example with 3 draws:
74 * Out-of-order drawing changes the execution order like this:
81 * If out-of-order draws are enabled, immediate mode vertices are not
82 * flushed before glDrawElements, resulting in fewer draws and lower CPU
83 * overhead. This helps workstation applications.
85 * This is a simplified version of out-of-order determination to catch
88 * RadeonSI has a complete and more complicated out-of-order determination
89 * for driver-internal reasons.
91 /* Only the compatibility profile with immediate mode needs this. */
92 if (ctx
->API
!= API_OPENGL_COMPAT
|| !ctx
->Const
.AllowDrawOutOfOrder
)
95 /* If all of these are NULL, GLSL is disabled. */
96 struct gl_program
*vs
=
97 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
98 struct gl_program
*tcs
=
99 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
100 struct gl_program
*tes
=
101 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
102 struct gl_program
*gs
=
103 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
104 struct gl_program
*fs
=
105 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
106 GLenum16 depth_func
= ctx
->Depth
.Func
;
108 /* Z fighting and any primitives with equal Z shouldn't be reordered
109 * with LESS/LEQUAL/GREATER/GEQUAL functions.
111 * When drawing 2 primitive with equal Z:
112 * - with LEQUAL/GEQUAL, the last primitive wins the Z test.
113 * - with LESS/GREATER, the first primitive wins the Z test.
115 * Here we ignore that on the basis that such cases don't occur in real
116 * apps, and we they do occur, they occur with blending where out-of-order
117 * drawing is always disabled.
119 bool previous_state
= ctx
->_AllowDrawOutOfOrder
;
120 ctx
->_AllowDrawOutOfOrder
=
122 ctx
->DrawBuffer
->Visual
.depthBits
&&
125 (depth_func
== GL_NEVER
||
126 depth_func
== GL_LESS
||
127 depth_func
== GL_LEQUAL
||
128 depth_func
== GL_GREATER
||
129 depth_func
== GL_GEQUAL
) &&
130 (!ctx
->DrawBuffer
->Visual
.stencilBits
||
131 !ctx
->Stencil
.Enabled
) &&
132 (!ctx
->Color
.ColorMask
||
133 (!ctx
->Color
.BlendEnabled
&&
134 (!ctx
->Color
.ColorLogicOpEnabled
||
135 ctx
->Color
._LogicOp
== COLOR_LOGICOP_COPY
))) &&
136 (!vs
|| !vs
->info
.writes_memory
) &&
137 (!tes
|| !tes
->info
.writes_memory
) &&
138 (!tcs
|| !tcs
->info
.writes_memory
) &&
139 (!gs
|| !gs
->info
.writes_memory
) &&
140 (!fs
|| !fs
->info
.writes_memory
|| !fs
->info
.fs
.early_fragment_tests
);
142 /* If we are disabling out-of-order drawing, we need to flush queued
145 if (previous_state
&& !ctx
->_AllowDrawOutOfOrder
)
146 FLUSH_VERTICES(ctx
, 0);
151 * Update the ctx->*Program._Current pointers to point to the
152 * current/active programs.
154 * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
155 * programs or programs derived from fixed-function state.
157 * This function needs to be called after texture state validation in case
158 * we're generating a fragment program from fixed-function texture state.
160 * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
161 * or fragment program is being used.
164 update_program(struct gl_context
*ctx
)
166 struct gl_program
*vsProg
=
167 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
];
168 struct gl_program
*tcsProg
=
169 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_CTRL
];
170 struct gl_program
*tesProg
=
171 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_TESS_EVAL
];
172 struct gl_program
*gsProg
=
173 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_GEOMETRY
];
174 struct gl_program
*fsProg
=
175 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
];
176 struct gl_program
*csProg
=
177 ctx
->_Shader
->CurrentProgram
[MESA_SHADER_COMPUTE
];
178 const struct gl_program
*prevVP
= ctx
->VertexProgram
._Current
;
179 const struct gl_program
*prevFP
= ctx
->FragmentProgram
._Current
;
180 const struct gl_program
*prevGP
= ctx
->GeometryProgram
._Current
;
181 const struct gl_program
*prevTCP
= ctx
->TessCtrlProgram
._Current
;
182 const struct gl_program
*prevTEP
= ctx
->TessEvalProgram
._Current
;
183 const struct gl_program
*prevCP
= ctx
->ComputeProgram
._Current
;
186 * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
187 * pointers to the programs that should be used for rendering. If either
188 * is NULL, use fixed-function code paths.
190 * These programs may come from several sources. The priority is as
192 * 1. OpenGL 2.0/ARB vertex/fragment shaders
193 * 2. ARB/NV vertex/fragment programs
194 * 3. ATI fragment shader
195 * 4. Programs derived from fixed-function state.
197 * Note: it's possible for a vertex shader to get used with a fragment
198 * program (and vice versa) here, but in practice that shouldn't ever
199 * come up, or matter.
203 /* Use GLSL fragment shader */
204 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
, fsProg
);
205 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
208 else if (_mesa_arb_fragment_program_enabled(ctx
)) {
209 /* Use user-defined fragment program */
210 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
,
211 ctx
->FragmentProgram
.Current
);
212 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
215 else if (_mesa_ati_fragment_shader_enabled(ctx
) &&
216 ctx
->ATIFragmentShader
.Current
->Program
) {
217 /* Use the enabled ATI fragment shader's associated program */
218 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
,
219 ctx
->ATIFragmentShader
.Current
->Program
);
220 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
223 else if (ctx
->FragmentProgram
._MaintainTexEnvProgram
) {
224 /* Use fragment program generated from fixed-function state */
225 struct gl_shader_program
*f
= _mesa_get_fixed_func_fragment_program(ctx
);
227 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
,
228 f
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
);
229 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
230 f
->_LinkedShaders
[MESA_SHADER_FRAGMENT
]->Program
);
233 /* No fragment program */
234 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._Current
, NULL
);
235 _mesa_reference_program(ctx
, &ctx
->FragmentProgram
._TexEnvProgram
,
240 /* Use GLSL geometry shader */
241 _mesa_reference_program(ctx
, &ctx
->GeometryProgram
._Current
, gsProg
);
243 /* No geometry program */
244 _mesa_reference_program(ctx
, &ctx
->GeometryProgram
._Current
, NULL
);
248 /* Use GLSL tessellation evaluation shader */
249 _mesa_reference_program(ctx
, &ctx
->TessEvalProgram
._Current
, tesProg
);
252 /* No tessellation evaluation program */
253 _mesa_reference_program(ctx
, &ctx
->TessEvalProgram
._Current
, NULL
);
257 /* Use GLSL tessellation control shader */
258 _mesa_reference_program(ctx
, &ctx
->TessCtrlProgram
._Current
, tcsProg
);
261 /* No tessellation control program */
262 _mesa_reference_program(ctx
, &ctx
->TessCtrlProgram
._Current
, NULL
);
265 /* Examine vertex program after fragment program as
266 * _mesa_get_fixed_func_vertex_program() needs to know active
270 /* Use GLSL vertex shader */
271 assert(VP_MODE_SHADER
== ctx
->VertexProgram
._VPMode
);
272 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._Current
, vsProg
);
274 else if (_mesa_arb_vertex_program_enabled(ctx
)) {
275 /* Use user-defined vertex program */
276 assert(VP_MODE_SHADER
== ctx
->VertexProgram
._VPMode
);
277 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._Current
,
278 ctx
->VertexProgram
.Current
);
280 else if (ctx
->VertexProgram
._MaintainTnlProgram
) {
281 /* Use vertex program generated from fixed-function state */
282 assert(VP_MODE_FF
== ctx
->VertexProgram
._VPMode
);
283 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._Current
,
284 _mesa_get_fixed_func_vertex_program(ctx
));
285 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._TnlProgram
,
286 ctx
->VertexProgram
._Current
);
289 /* no vertex program */
290 assert(VP_MODE_FF
== ctx
->VertexProgram
._VPMode
);
291 _mesa_reference_program(ctx
, &ctx
->VertexProgram
._Current
, NULL
);
295 /* Use GLSL compute shader */
296 _mesa_reference_program(ctx
, &ctx
->ComputeProgram
._Current
, csProg
);
298 /* no compute program */
299 _mesa_reference_program(ctx
, &ctx
->ComputeProgram
._Current
, NULL
);
302 /* Let the driver know what's happening:
304 if (ctx
->FragmentProgram
._Current
!= prevFP
||
305 ctx
->VertexProgram
._Current
!= prevVP
||
306 ctx
->GeometryProgram
._Current
!= prevGP
||
307 ctx
->TessEvalProgram
._Current
!= prevTEP
||
308 ctx
->TessCtrlProgram
._Current
!= prevTCP
||
309 ctx
->ComputeProgram
._Current
!= prevCP
)
317 update_single_program_constants(struct gl_context
*ctx
,
318 struct gl_program
*prog
,
319 gl_shader_stage stage
)
322 const struct gl_program_parameter_list
*params
= prog
->Parameters
;
323 if (params
&& params
->StateFlags
& ctx
->NewState
) {
324 if (ctx
->DriverFlags
.NewShaderConstants
[stage
])
325 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewShaderConstants
[stage
];
327 return _NEW_PROGRAM_CONSTANTS
;
335 * This updates fixed-func state constants such as gl_ModelViewMatrix.
336 * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
339 update_program_constants(struct gl_context
*ctx
)
341 GLbitfield new_state
=
342 update_single_program_constants(ctx
, ctx
->VertexProgram
._Current
,
343 MESA_SHADER_VERTEX
) |
344 update_single_program_constants(ctx
, ctx
->FragmentProgram
._Current
,
345 MESA_SHADER_FRAGMENT
);
347 if (ctx
->API
== API_OPENGL_COMPAT
&&
348 ctx
->Const
.GLSLVersionCompat
>= 150) {
350 update_single_program_constants(ctx
, ctx
->GeometryProgram
._Current
,
351 MESA_SHADER_GEOMETRY
);
353 if (_mesa_has_ARB_tessellation_shader(ctx
)) {
355 update_single_program_constants(ctx
, ctx
->TessCtrlProgram
._Current
,
356 MESA_SHADER_TESS_CTRL
) |
357 update_single_program_constants(ctx
, ctx
->TessEvalProgram
._Current
,
358 MESA_SHADER_TESS_EVAL
);
367 update_fixed_func_program_usage(struct gl_context
*ctx
)
369 ctx
->FragmentProgram
._UsesTexEnvProgram
=
370 ctx
->FragmentProgram
._MaintainTexEnvProgram
&&
371 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_FRAGMENT
] && /* GLSL*/
372 !_mesa_arb_fragment_program_enabled(ctx
) &&
373 !(_mesa_ati_fragment_shader_enabled(ctx
) &&
374 ctx
->ATIFragmentShader
.Current
->Program
);
376 ctx
->VertexProgram
._UsesTnlProgram
=
377 ctx
->VertexProgram
._MaintainTnlProgram
&&
378 !ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
] && /* GLSL */
379 !_mesa_arb_vertex_program_enabled(ctx
);
384 * Compute derived GL state.
385 * If __struct gl_contextRec::NewState is non-zero then this function \b must
386 * be called before rendering anything.
388 * Calls dd_function_table::UpdateState to perform any internal state
389 * management necessary.
391 * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
392 * _mesa_update_buffer_bounds(),
393 * _mesa_update_lighting() and _mesa_update_tnl_spaces().
396 _mesa_update_state_locked( struct gl_context
*ctx
)
398 GLbitfield new_state
= ctx
->NewState
;
399 GLbitfield new_prog_state
= 0x0;
400 const GLbitfield computed_states
= ~(_NEW_CURRENT_ATTRIB
| _NEW_LINE
);
402 /* we can skip a bunch of state validation checks if the dirty
403 * state matches one or more bits in 'computed_states'.
405 if ((new_state
& computed_states
) == 0)
408 if (MESA_VERBOSE
& VERBOSE_STATE
)
409 _mesa_print_state("_mesa_update_state", new_state
);
411 if (new_state
& _NEW_BUFFERS
)
412 _mesa_update_framebuffer(ctx
, ctx
->ReadBuffer
, ctx
->DrawBuffer
);
414 /* Handle Core and Compatibility contexts separately. */
415 if (ctx
->API
== API_OPENGL_COMPAT
||
416 ctx
->API
== API_OPENGLES
) {
417 GLbitfield prog_flags
= _NEW_PROGRAM
;
419 if (new_state
& _NEW_PROGRAM
)
420 update_fixed_func_program_usage(ctx
);
422 /* Determine which states affect fixed-func vertex/fragment program. */
423 if (ctx
->FragmentProgram
._UsesTexEnvProgram
) {
424 prog_flags
|= (_NEW_BUFFERS
| _NEW_TEXTURE_OBJECT
| _NEW_FOG
|
425 _NEW_VARYING_VP_INPUTS
| _NEW_LIGHT
| _NEW_POINT
|
426 _NEW_RENDERMODE
| _NEW_COLOR
| _NEW_TEXTURE_STATE
);
429 if (ctx
->VertexProgram
._UsesTnlProgram
) {
430 prog_flags
|= (_NEW_VARYING_VP_INPUTS
| _NEW_TEXTURE_OBJECT
|
431 _NEW_TEXTURE_MATRIX
| _NEW_TRANSFORM
| _NEW_POINT
|
432 _NEW_FOG
| _NEW_LIGHT
| _NEW_TEXTURE_STATE
|
433 _MESA_NEW_NEED_EYE_COORDS
);
437 * Now update derived state info
439 if (new_state
& (_NEW_MODELVIEW
|_NEW_PROJECTION
))
440 _mesa_update_modelview_project( ctx
, new_state
);
442 if (new_state
& _NEW_TEXTURE_MATRIX
)
443 _mesa_update_texture_matrices(ctx
);
445 if (new_state
& (_NEW_TEXTURE_OBJECT
| _NEW_TEXTURE_STATE
| _NEW_PROGRAM
))
446 _mesa_update_texture_state(ctx
);
448 if (new_state
& _NEW_LIGHT
)
449 _mesa_update_lighting(ctx
);
451 if (new_state
& _NEW_PIXEL
)
452 _mesa_update_pixel( ctx
);
454 /* ctx->_NeedEyeCoords is now up to date.
456 * If the truth value of this variable has changed, update for the
457 * new lighting space and recompute the positions of lights and the
460 * If the lighting space hasn't changed, may still need to recompute
461 * light positions & normal transforms for other reasons.
463 if (new_state
& _MESA_NEW_NEED_EYE_COORDS
)
464 _mesa_update_tnl_spaces( ctx
, new_state
);
466 if (new_state
& prog_flags
) {
467 /* When we generate programs from fixed-function vertex/fragment state
468 * this call may generate/bind a new program. If so, we need to
469 * propogate the _NEW_PROGRAM flag to the driver.
471 new_prog_state
|= update_program(ctx
);
474 /* GL Core and GLES 2/3 contexts */
475 if (new_state
& (_NEW_TEXTURE_OBJECT
| _NEW_PROGRAM
))
476 _mesa_update_texture_state(ctx
);
478 if (new_state
& _NEW_PROGRAM
)
483 new_prog_state
|= update_program_constants(ctx
);
485 ctx
->NewState
|= new_prog_state
;
488 * Give the driver a chance to act upon the new_state flags.
489 * The driver might plug in different span functions, for example.
490 * Also, this is where the driver can invalidate the state of any
491 * active modules (such as swrast_setup, swrast, tnl, etc).
493 ctx
->Driver
.UpdateState(ctx
);
498 /* This is the usual entrypoint for state updates:
501 _mesa_update_state( struct gl_context
*ctx
)
503 _mesa_lock_context_textures(ctx
);
504 _mesa_update_state_locked(ctx
);
505 _mesa_unlock_context_textures(ctx
);
512 * Want to figure out which fragment program inputs are actually
513 * constant/current values from ctx->Current. These should be
514 * referenced as a tracked state variable rather than a fragment
515 * program input, to save the overhead of putting a constant value in
516 * every submitted vertex, transferring it to hardware, interpolating
517 * it across the triangle, etc...
519 * When there is a VP bound, just use vp->outputs. But when we're
520 * generating vp from fixed function state, basically want to
523 * vp_out_2_fp_in( vp_in_2_vp_out( varying_inputs ) |
524 * potential_vp_outputs )
526 * Where potential_vp_outputs is calculated by looking at enabled
529 * The generated fragment program should then only declare inputs that
530 * may vary or otherwise differ from the ctx->Current values.
531 * Otherwise, the fp should track them as state values instead.
534 set_varying_vp_inputs(struct gl_context
*ctx
, GLbitfield varying_inputs
)
537 * The gl_context::varying_vp_inputs value is only used when in
540 if (VP_MODE_FF
!= ctx
->VertexProgram
._VPMode
)
543 /* Only fixed-func generated programs ever uses varying_vp_inputs. */
544 if (!ctx
->VertexProgram
._MaintainTnlProgram
&&
545 !ctx
->FragmentProgram
._MaintainTexEnvProgram
)
548 if (ctx
->varying_vp_inputs
!= varying_inputs
) {
549 ctx
->varying_vp_inputs
= varying_inputs
;
550 ctx
->NewState
|= _NEW_VARYING_VP_INPUTS
;
556 * Used by drivers to tell core Mesa that the driver is going to
557 * install/ use its own vertex program. In particular, this will
558 * prevent generated fragment programs from using state vars instead
559 * of ordinary varyings/inputs.
562 _mesa_set_vp_override(struct gl_context
*ctx
, GLboolean flag
)
564 if (ctx
->VertexProgram
._Overriden
!= flag
) {
565 ctx
->VertexProgram
._Overriden
= flag
;
567 /* Set one of the bits which will trigger fragment program
570 ctx
->NewState
|= _NEW_PROGRAM
;
576 set_vertex_processing_mode(struct gl_context
*ctx
, gl_vertex_processing_mode m
)
578 if (ctx
->VertexProgram
._VPMode
== m
)
581 /* On change we may get new maps into the current values */
582 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
584 /* Finally memorize the value */
585 ctx
->VertexProgram
._VPMode
= m
;
587 /* Since we only track the varying inputs while being in fixed function
588 * vertex processing mode, we may need to recheck for the
589 * _NEW_VARYING_VP_INPUTS bit.
591 set_varying_vp_inputs(ctx
, ctx
->Array
._DrawVAOEnabledAttribs
);
596 * Update ctx->VertexProgram._VPMode.
597 * This is to distinguish whether we're running
598 * a vertex program/shader,
599 * a fixed-function TNL program or
600 * a fixed function vertex transformation without any program.
603 _mesa_update_vertex_processing_mode(struct gl_context
*ctx
)
605 if (ctx
->_Shader
->CurrentProgram
[MESA_SHADER_VERTEX
])
606 set_vertex_processing_mode(ctx
, VP_MODE_SHADER
);
607 else if (_mesa_arb_vertex_program_enabled(ctx
))
608 set_vertex_processing_mode(ctx
, VP_MODE_SHADER
);
610 set_vertex_processing_mode(ctx
, VP_MODE_FF
);
615 * Set the _DrawVAO and the net enabled arrays.
616 * The vao->_Enabled bitmask is transformed due to position/generic0
617 * as stored in vao->_AttributeMapMode. Then the filter bitmask is applied
618 * to filter out arrays unwanted for the currently executed draw operation.
619 * For example, the generic attributes are masked out form the _DrawVAO's
620 * enabled arrays when a fixed function array draw is executed.
623 _mesa_set_draw_vao(struct gl_context
*ctx
, struct gl_vertex_array_object
*vao
,
626 struct gl_vertex_array_object
**ptr
= &ctx
->Array
._DrawVAO
;
627 bool new_array
= false;
629 _mesa_reference_vao_(ctx
, ptr
, vao
);
634 if (vao
->NewArrays
) {
635 _mesa_update_vao_derived_arrays(ctx
, vao
);
641 /* May shuffle the position and generic0 bits around, filter out unwanted */
642 const GLbitfield enabled
= filter
& _mesa_get_vao_vp_inputs(vao
);
643 if (ctx
->Array
._DrawVAOEnabledAttribs
!= enabled
)
647 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewArray
;
649 ctx
->Array
._DrawVAOEnabledAttribs
= enabled
;
650 set_varying_vp_inputs(ctx
, enabled
);