xref: /aosp_15_r20/external/mesa3d/src/mesa/main/state.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 
26 /**
27  * \file state.c
28  * State management.
29  *
30  * This file manages recalculation of derived values in struct gl_context.
31  */
32 
33 
34 #include "util/glheader.h"
35 #include "mtypes.h"
36 #include "arrayobj.h"
37 #include "context.h"
38 #include "debug.h"
39 #include "macros.h"
40 #include "ffvertex_prog.h"
41 #include "framebuffer.h"
42 #include "light.h"
43 #include "matrix.h"
44 #include "pixel.h"
45 #include "program/program.h"
46 #include "program/prog_parameter.h"
47 #include "shaderobj.h"
48 #include "state.h"
49 #include "stencil.h"
50 #include "texenvprogram.h"
51 #include "texobj.h"
52 #include "texstate.h"
53 #include "varray.h"
54 #include "vbo/vbo.h"
55 #include "viewport.h"
56 #include "blend.h"
57 
58 #include "state_tracker/st_context.h"
59 #include "state_tracker/st_util.h"
60 
61 void
_mesa_update_allow_draw_out_of_order(struct gl_context * ctx)62 _mesa_update_allow_draw_out_of_order(struct gl_context *ctx)
63 {
64    /* Out-of-order drawing is useful when vertex array draws and immediate
65     * mode are interleaved.
66     *
67     * Example with 3 draws:
68     *   glBegin();
69     *      glVertex();
70     *   glEnd();
71     *   glDrawElements();
72     *   glBegin();
73     *      glVertex();
74     *   glEnd();
75     *
76     * Out-of-order drawing changes the execution order like this:
77     *   glDrawElements();
78     *   glBegin();
79     *      glVertex();
80     *      glVertex();
81     *   glEnd();
82     *
83     * If out-of-order draws are enabled, immediate mode vertices are not
84     * flushed before glDrawElements, resulting in fewer draws and lower CPU
85     * overhead. This helps workstation applications.
86     *
87     * This is a simplified version of out-of-order determination to catch
88     * common cases.
89     *
90     * RadeonSI has a complete and more complicated out-of-order determination
91     * for driver-internal reasons.
92     */
93    /* Only the compatibility profile with immediate mode needs this. */
94    if (!ctx->Const.AllowDrawOutOfOrder)
95       return;
96 
97    assert(_mesa_is_desktop_gl_compat(ctx));
98 
99    /* If all of these are NULL, GLSL is disabled. */
100    struct gl_program *vs =
101       ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
102    struct gl_program *tcs =
103       ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
104    struct gl_program *tes =
105       ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
106    struct gl_program *gs =
107       ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
108    struct gl_program *fs =
109       ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
110    GLenum16 depth_func = ctx->Depth.Func;
111 
112    /* Z fighting and any primitives with equal Z shouldn't be reordered
113     * with LESS/LEQUAL/GREATER/GEQUAL functions.
114     *
115     * When drawing 2 primitive with equal Z:
116     * - with LEQUAL/GEQUAL, the last primitive wins the Z test.
117     * - with LESS/GREATER, the first primitive wins the Z test.
118     *
119     * Here we ignore that on the basis that such cases don't occur in real
120     * apps, and we they do occur, they occur with blending where out-of-order
121     * drawing is always disabled.
122     */
123    bool previous_state = ctx->_AllowDrawOutOfOrder;
124    ctx->_AllowDrawOutOfOrder =
125          ctx->DrawBuffer &&
126          ctx->DrawBuffer->Visual.depthBits &&
127          ctx->Depth.Test &&
128          ctx->Depth.Mask &&
129          (depth_func == GL_NEVER ||
130           depth_func == GL_LESS ||
131           depth_func == GL_LEQUAL ||
132           depth_func == GL_GREATER ||
133           depth_func == GL_GEQUAL) &&
134          (!ctx->DrawBuffer->Visual.stencilBits ||
135           !ctx->Stencil.Enabled) &&
136          (!ctx->Color.ColorMask ||
137           (!ctx->Color.BlendEnabled &&
138            (!ctx->Color.ColorLogicOpEnabled ||
139             ctx->Color._LogicOp == COLOR_LOGICOP_COPY))) &&
140          (!vs || !vs->info.writes_memory) &&
141          (!tes || !tes->info.writes_memory) &&
142          (!tcs || !tcs->info.writes_memory) &&
143          (!gs || !gs->info.writes_memory) &&
144          (!fs || !fs->info.writes_memory || !fs->info.fs.early_fragment_tests);
145 
146    /* If we are disabling out-of-order drawing, we need to flush queued
147     * vertices.
148     */
149    if (previous_state && !ctx->_AllowDrawOutOfOrder)
150       FLUSH_VERTICES(ctx, 0, 0);
151 }
152 
153 uint64_t
_mesa_get_active_states(struct gl_context * ctx)154 _mesa_get_active_states(struct gl_context *ctx)
155 {
156    struct gl_program *vp = ctx->VertexProgram._Current;
157    struct gl_program *tcp = ctx->TessCtrlProgram._Current;
158    struct gl_program *tep = ctx->TessEvalProgram._Current;
159    struct gl_program *gp = ctx->GeometryProgram._Current;
160    struct gl_program *fp = ctx->FragmentProgram._Current;
161    struct gl_program *cp = ctx->ComputeProgram._Current;
162    uint64_t active_shader_states = 0;
163 
164    if (vp)
165       active_shader_states |= vp->affected_states;
166    if (tcp)
167       active_shader_states |= tcp->affected_states;
168    if (tep)
169       active_shader_states |= tep->affected_states;
170    if (gp)
171       active_shader_states |= gp->affected_states;
172    if (fp)
173       active_shader_states |= fp->affected_states;
174    if (cp)
175       active_shader_states |= cp->affected_states;
176 
177    /* Mark non-shader-resource shader states as "always active". */
178    return active_shader_states | ~ST_ALL_SHADER_RESOURCES;
179 }
180 
181 /**
182  * Update the ctx->*Program._Current pointers to point to the
183  * current/active programs.
184  *
185  * Programs may come from 3 sources: GLSL shaders, ARB/NV_vertex/fragment
186  * programs or programs derived from fixed-function state.
187  *
188  * This function needs to be called after texture state validation in case
189  * we're generating a fragment program from fixed-function texture state.
190  *
191  * \return bitfield which will indicate _NEW_PROGRAM state if a new vertex
192  * or fragment program is being used.
193  */
194 static GLbitfield
update_program(struct gl_context * ctx)195 update_program(struct gl_context *ctx)
196 {
197    struct gl_program *vsProg =
198       ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
199    struct gl_program *tcsProg =
200       ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
201    struct gl_program *tesProg =
202       ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
203    struct gl_program *gsProg =
204       ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
205    struct gl_program *fsProg =
206       ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
207    struct gl_program *csProg =
208       ctx->_Shader->CurrentProgram[MESA_SHADER_COMPUTE];
209    const struct gl_program *prevVP = ctx->VertexProgram._Current;
210    const struct gl_program *prevFP = ctx->FragmentProgram._Current;
211    const struct gl_program *prevGP = ctx->GeometryProgram._Current;
212    const struct gl_program *prevTCP = ctx->TessCtrlProgram._Current;
213    const struct gl_program *prevTEP = ctx->TessEvalProgram._Current;
214    const struct gl_program *prevCP = ctx->ComputeProgram._Current;
215    uint64_t prev_vp_affected_states = prevVP ? prevVP->affected_states : 0;
216    uint64_t prev_tcp_affected_states = prevTCP ? prevTCP->affected_states : 0;
217    uint64_t prev_tep_affected_states = prevTEP ? prevTEP->affected_states : 0;
218    uint64_t prev_gp_affected_states = prevGP ? prevGP->affected_states : 0;
219    uint64_t prev_fp_affected_states = prevFP ? prevFP->affected_states : 0;
220    uint64_t prev_cp_affected_states = prevCP ? prevCP->affected_states : 0;
221 
222    /*
223     * Set the ctx->VertexProgram._Current and ctx->FragmentProgram._Current
224     * pointers to the programs that should be used for rendering.  If either
225     * is NULL, use fixed-function code paths.
226     *
227     * These programs may come from several sources.  The priority is as
228     * follows:
229     *   1. OpenGL 2.0/ARB vertex/fragment shaders
230     *   2. ARB/NV vertex/fragment programs
231     *   3. ATI fragment shader
232     *   4. Programs derived from fixed-function state.
233     *
234     * Note: it's possible for a vertex shader to get used with a fragment
235     * program (and vice versa) here, but in practice that shouldn't ever
236     * come up, or matter.
237     */
238 
239    if (fsProg) {
240       /* Use GLSL fragment shader */
241       _mesa_reference_program(ctx, &ctx->FragmentProgram._Current, fsProg);
242       _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
243                               NULL);
244    } else if (_mesa_arb_fragment_program_enabled(ctx)) {
245       /* Use user-defined fragment program */
246       _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
247                               ctx->FragmentProgram.Current);
248       _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
249 			      NULL);
250    } else if (_mesa_ati_fragment_shader_enabled(ctx) &&
251             ctx->ATIFragmentShader.Current->Program) {
252        /* Use the enabled ATI fragment shader's associated program */
253       _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
254                               ctx->ATIFragmentShader.Current->Program);
255       _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
256                               NULL);
257    } else {
258       /* Use fragment program generated from fixed-function state */
259       _mesa_reference_program(ctx, &ctx->FragmentProgram._Current,
260                               _mesa_get_fixed_func_fragment_program(ctx));
261       _mesa_reference_program(ctx, &ctx->FragmentProgram._TexEnvProgram,
262                               ctx->FragmentProgram._Current);
263    }
264 
265    /* Examine vertex program after fragment program as
266     * _mesa_get_fixed_func_vertex_program() needs to know active
267     * fragprog inputs.
268     */
269    if (vsProg) {
270       /* Use GLSL vertex shader */
271       assert(VP_MODE_SHADER == ctx->VertexProgram._VPMode);
272       _mesa_reference_program(ctx, &ctx->VertexProgram._Current, vsProg);
273    } else if (_mesa_arb_vertex_program_enabled(ctx)) {
274       /* Use user-defined vertex program */
275       assert(VP_MODE_SHADER == ctx->VertexProgram._VPMode);
276       _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
277                               ctx->VertexProgram.Current);
278    } else {
279       /* Use vertex program generated from fixed-function state */
280       assert(VP_MODE_FF == ctx->VertexProgram._VPMode);
281       _mesa_reference_program(ctx, &ctx->VertexProgram._Current,
282                               _mesa_get_fixed_func_vertex_program(ctx));
283       _mesa_reference_program(ctx, &ctx->VertexProgram._TnlProgram,
284                               ctx->VertexProgram._Current);
285    }
286 
287    /* Bind or unbind these shaders. (NULL = unbind) */
288    _mesa_reference_program(ctx, &ctx->GeometryProgram._Current, gsProg);
289    _mesa_reference_program(ctx, &ctx->TessEvalProgram._Current, tesProg);
290    _mesa_reference_program(ctx, &ctx->TessCtrlProgram._Current, tcsProg);
291    _mesa_reference_program(ctx, &ctx->ComputeProgram._Current, csProg);
292 
293    bool vp_changed = ctx->VertexProgram._Current != prevVP;
294    bool tcp_changed = ctx->TessCtrlProgram._Current != prevTCP;
295    bool tep_changed = ctx->TessEvalProgram._Current != prevTEP;
296    bool gp_changed = ctx->GeometryProgram._Current != prevGP;
297    bool fp_changed = ctx->FragmentProgram._Current != prevFP;
298    bool cp_changed = ctx->ComputeProgram._Current != prevCP;
299 
300    /* Set NewDriverState depending on which shaders have changed. */
301    uint64_t dirty = 0;
302 
303    /* Flag states used by both new and old shaders to rebind shader resources
304     * (because shaders pack them and reorder them) and to unbind shader
305     * resources properly when transitioning to shaders that don't use them.
306     */
307    if (vp_changed) {
308       ctx->Array.NewVertexElements = true;
309       dirty |= prev_vp_affected_states;
310       if (ctx->VertexProgram._Current)
311          dirty |= ST_NEW_VERTEX_PROGRAM(ctx, ctx->VertexProgram._Current);
312    }
313 
314    if (tcp_changed) {
315       dirty |= prev_tcp_affected_states;
316       if (ctx->TessCtrlProgram._Current)
317          dirty |= ctx->TessCtrlProgram._Current->affected_states;
318    }
319 
320    if (tep_changed) {
321       dirty |= prev_tep_affected_states;
322       if (ctx->TessEvalProgram._Current)
323          dirty |= ctx->TessEvalProgram._Current->affected_states;
324    }
325 
326    if (gp_changed) {
327       dirty |= prev_gp_affected_states;
328       if (ctx->GeometryProgram._Current)
329          dirty |= ctx->GeometryProgram._Current->affected_states;
330    }
331 
332    if (fp_changed) {
333       dirty |= prev_fp_affected_states;
334       if (ctx->FragmentProgram._Current)
335          dirty |= ctx->FragmentProgram._Current->affected_states;
336 
337       if (!ctx->st->needs_texcoord_semantic)
338          dirty |= ST_NEW_RASTERIZER;
339    }
340 
341    if (cp_changed) {
342       dirty |= prev_cp_affected_states;
343       if (ctx->ComputeProgram._Current)
344          dirty |= ctx->ComputeProgram._Current->affected_states;
345    }
346 
347    struct gl_program *last_vertex_stage;
348    bool last_vertex_stage_dirty;
349 
350    if (ctx->GeometryProgram._Current) {
351       last_vertex_stage = ctx->GeometryProgram._Current;
352       last_vertex_stage_dirty = gp_changed;
353    } else if (ctx->TessEvalProgram._Current) {
354       last_vertex_stage = ctx->TessEvalProgram._Current;
355       last_vertex_stage_dirty = gp_changed | tep_changed;
356    } else {
357       last_vertex_stage = ctx->VertexProgram._Current;
358       last_vertex_stage_dirty = gp_changed | tep_changed | vp_changed;
359    }
360 
361    /* Find out the number of viewports. This determines how many scissors
362     * and viewport states we need to update.
363     */
364    struct st_context *st = ctx->st;
365    unsigned num_viewports = 1;
366 
367    if (last_vertex_stage &&
368        last_vertex_stage->info.outputs_written & (
369              VARYING_BIT_VIEWPORT | VARYING_BIT_VIEWPORT_MASK))
370       num_viewports = ctx->Const.MaxViewports;
371 
372    if (st->state.num_viewports != num_viewports) {
373       st->state.num_viewports = num_viewports;
374       dirty |= ST_NEW_VIEWPORT;
375 
376       if (ctx->Scissor.EnableFlags & u_bit_consecutive(0, num_viewports))
377          dirty |= ST_NEW_SCISSOR;
378    }
379 
380    if (st->lower_point_size && last_vertex_stage_dirty &&
381        !ctx->VertexProgram.PointSizeEnabled && !ctx->PointSizeIsSet) {
382       if (ctx->GeometryProgram._Current) {
383          ctx->NewDriverState |= ST_NEW_GS_CONSTANTS;
384       } else if (ctx->TessEvalProgram._Current) {
385          ctx->NewDriverState |= ST_NEW_TES_CONSTANTS;
386       } else {
387          ctx->NewDriverState |= ST_NEW_VS_CONSTANTS;
388       }
389    }
390 
391    ctx->NewDriverState |= dirty;
392 
393    /* Let the driver know what's happening: */
394    if (fp_changed || vp_changed || gp_changed || tep_changed ||
395        tcp_changed || cp_changed) {
396       /* This will mask out unused shader resources. */
397       st->active_states = _mesa_get_active_states(ctx);
398 
399       /* Some drivers need to clean up previous states too */
400       if (st->validate_all_dirty_states)
401          st->active_states |= dirty;
402 
403       return _NEW_PROGRAM;
404    }
405 
406    return 0;
407 }
408 
409 
410 static GLbitfield
update_single_program_constants(struct gl_context * ctx,struct gl_program * prog,gl_shader_stage stage)411 update_single_program_constants(struct gl_context *ctx,
412                                 struct gl_program *prog,
413                                 gl_shader_stage stage)
414 {
415    if (prog) {
416       const struct gl_program_parameter_list *params = prog->Parameters;
417       if (params && params->StateFlags & ctx->NewState) {
418          if (ctx->DriverFlags.NewShaderConstants[stage])
419             ctx->NewDriverState |= ctx->DriverFlags.NewShaderConstants[stage];
420          else
421             return _NEW_PROGRAM_CONSTANTS;
422       }
423    }
424    return 0;
425 }
426 
427 
428 /**
429  * This updates fixed-func state constants such as gl_ModelViewMatrix.
430  * Examine shader constants and return either _NEW_PROGRAM_CONSTANTS or 0.
431  */
432 static GLbitfield
update_program_constants(struct gl_context * ctx)433 update_program_constants(struct gl_context *ctx)
434 {
435    GLbitfield new_state =
436       update_single_program_constants(ctx, ctx->VertexProgram._Current,
437                                       MESA_SHADER_VERTEX) |
438       update_single_program_constants(ctx, ctx->FragmentProgram._Current,
439                                       MESA_SHADER_FRAGMENT);
440 
441    if (_mesa_is_desktop_gl_compat(ctx) &&
442        ctx->Const.GLSLVersionCompat >= 150) {
443       new_state |=
444          update_single_program_constants(ctx, ctx->GeometryProgram._Current,
445                                          MESA_SHADER_GEOMETRY);
446 
447       if (_mesa_has_ARB_tessellation_shader(ctx)) {
448          new_state |=
449             update_single_program_constants(ctx, ctx->TessCtrlProgram._Current,
450                                             MESA_SHADER_TESS_CTRL) |
451             update_single_program_constants(ctx, ctx->TessEvalProgram._Current,
452                                             MESA_SHADER_TESS_EVAL);
453       }
454    }
455 
456    return new_state;
457 }
458 
459 
460 static void
update_fixed_func_program_usage(struct gl_context * ctx)461 update_fixed_func_program_usage(struct gl_context *ctx)
462 {
463    ctx->FragmentProgram._UsesTexEnvProgram =
464       !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] && /* GLSL*/
465       !_mesa_arb_fragment_program_enabled(ctx) &&
466       !(_mesa_ati_fragment_shader_enabled(ctx) &&
467         ctx->ATIFragmentShader.Current->Program);
468 
469    ctx->VertexProgram._UsesTnlProgram =
470       !ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] && /* GLSL */
471       !_mesa_arb_vertex_program_enabled(ctx);
472 }
473 
474 
475 /**
476  * Compute derived GL state.
477  * If __struct gl_contextRec::NewState is non-zero then this function \b must
478  * be called before rendering anything.
479  *
480  * Calls dd_function_table::UpdateState to perform any internal state
481  * management necessary.
482  *
483  * \sa _mesa_update_modelview_project(), _mesa_update_texture(),
484  * _mesa_update_buffer_bounds(),
485  * _mesa_update_lighting() and _mesa_update_tnl_spaces().
486  */
487 void
_mesa_update_state_locked(struct gl_context * ctx)488 _mesa_update_state_locked( struct gl_context *ctx )
489 {
490    GLbitfield new_state = ctx->NewState;
491    GLbitfield new_prog_state = 0x0;
492    const GLbitfield checked_states =
493       _NEW_BUFFERS | _NEW_MODELVIEW | _NEW_PROJECTION | _NEW_TEXTURE_MATRIX |
494       _NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE | _NEW_PROGRAM |
495       _NEW_LIGHT_CONSTANTS | _NEW_POINT | _NEW_FF_VERT_PROGRAM |
496       _NEW_FF_FRAG_PROGRAM | _NEW_TNL_SPACES;
497 
498    /* we can skip a bunch of state validation checks if the dirty
499     * state matches one or more bits in 'computed_states'.
500     */
501    if (!(new_state & checked_states))
502       goto out;
503 
504    if (MESA_VERBOSE & VERBOSE_STATE)
505       _mesa_print_state("_mesa_update_state", new_state);
506 
507    if (new_state & _NEW_BUFFERS)
508       _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer);
509 
510    /* Handle Core and Compatibility contexts separately. */
511    if (_mesa_is_desktop_gl_compat(ctx) ||
512        _mesa_is_gles1(ctx)) {
513       /* Update derived state. */
514       if (new_state & (_NEW_MODELVIEW|_NEW_PROJECTION))
515          _mesa_update_modelview_project( ctx, new_state );
516 
517       if (new_state & _NEW_TEXTURE_MATRIX)
518          new_state |= _mesa_update_texture_matrices(ctx);
519 
520       if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_TEXTURE_STATE | _NEW_PROGRAM))
521          new_state |= _mesa_update_texture_state(ctx);
522 
523       if (new_state & _NEW_LIGHT_CONSTANTS)
524          new_state |= _mesa_update_lighting(ctx);
525 
526       /* ctx->_NeedEyeCoords is determined here.
527        *
528        * If the truth value of this variable has changed, update for the
529        * new lighting space and recompute the positions of lights and the
530        * normal transform.
531        *
532        * If the lighting space hasn't changed, may still need to recompute
533        * light positions & normal transforms for other reasons.
534        */
535       if (new_state & (_NEW_TNL_SPACES | _NEW_LIGHT_CONSTANTS |
536                        _NEW_MODELVIEW)) {
537          if (_mesa_update_tnl_spaces(ctx, new_state))
538             new_state |= _NEW_FF_VERT_PROGRAM;
539       }
540 
541       if (new_state & _NEW_PROGRAM)
542          update_fixed_func_program_usage(ctx);
543 
544       /* Determine which states affect fixed-func vertex/fragment program. */
545       GLbitfield prog_flags = _NEW_PROGRAM;
546 
547       if (ctx->FragmentProgram._UsesTexEnvProgram) {
548          prog_flags |= _NEW_BUFFERS | _NEW_TEXTURE_OBJECT |
549                        _NEW_FF_FRAG_PROGRAM | _NEW_TEXTURE_STATE;
550       }
551 
552       if (ctx->VertexProgram._UsesTnlProgram)
553          prog_flags |= _NEW_FF_VERT_PROGRAM;
554 
555       if (new_state & prog_flags) {
556          /* When we generate programs from fixed-function vertex/fragment state
557           * this call may generate/bind a new program.  If so, we need to
558           * propogate the _NEW_PROGRAM flag to the driver.
559           */
560          new_prog_state |= update_program(ctx);
561       }
562    } else {
563       /* GL Core and GLES 2/3 contexts */
564       if (new_state & (_NEW_TEXTURE_OBJECT | _NEW_PROGRAM))
565          _mesa_update_texture_state(ctx);
566 
567       if (new_state & _NEW_PROGRAM)
568          update_program(ctx);
569    }
570 
571  out:
572    new_prog_state |= update_program_constants(ctx);
573 
574    ctx->NewState |= new_prog_state;
575 
576    /*
577     * Give the driver a chance to act upon the new_state flags.
578     * The driver might plug in different span functions, for example.
579     * Also, this is where the driver can invalidate the state of any
580     * active modules (such as swrast_setup, swrast, tnl, etc).
581     */
582    st_invalidate_state(ctx);
583    ctx->NewState = 0;
584 }
585 
586 
587 /* This is the usual entrypoint for state updates:
588  */
589 void
_mesa_update_state(struct gl_context * ctx)590 _mesa_update_state( struct gl_context *ctx )
591 {
592    _mesa_lock_context_textures(ctx);
593    _mesa_update_state_locked(ctx);
594    _mesa_unlock_context_textures(ctx);
595 }
596 
597 /* This is the usual entrypoint for state updates in glClear calls:
598  */
599 void
_mesa_update_clear_state(struct gl_context * ctx)600 _mesa_update_clear_state( struct gl_context *ctx )
601 {
602    GLbitfield new_state = ctx->NewState;
603 
604    if (MESA_VERBOSE & VERBOSE_STATE)
605       _mesa_print_state("_mesa_update_clear_state", new_state);
606 
607    if (new_state & _NEW_BUFFERS) {
608       _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer);
609 
610       st_invalidate_buffers(st_context(ctx));
611       ctx->NewState &= ~_NEW_BUFFERS;
612    }
613 }
614 
615 /**
616  * Used by drivers to tell core Mesa that the driver is going to
617  * install/ use its own vertex program.  In particular, this will
618  * prevent generated fragment programs from using state vars instead
619  * of ordinary varyings/inputs.
620  */
621 void
_mesa_set_vp_override(struct gl_context * ctx,GLboolean flag)622 _mesa_set_vp_override(struct gl_context *ctx, GLboolean flag)
623 {
624    if (ctx->VertexProgram._Overriden != flag) {
625       ctx->VertexProgram._Overriden = flag;
626 
627       /* Set one of the bits which will trigger fragment program
628        * regeneration:
629        */
630       ctx->NewState |= _NEW_PROGRAM;
631    }
632 }
633 
634 
635 static void
set_vertex_processing_mode(struct gl_context * ctx,gl_vertex_processing_mode m)636 set_vertex_processing_mode(struct gl_context *ctx, gl_vertex_processing_mode m)
637 {
638    if (ctx->VertexProgram._VPMode == m)
639       return;
640 
641    /* On change we may get new maps into the current values */
642    ctx->NewDriverState |= ST_NEW_VERTEX_ARRAYS;
643    ctx->Array.NewVertexElements = true;
644 
645    /* Finally memorize the value */
646    ctx->VertexProgram._VPMode = m;
647 
648    /* The gl_context::VertexProgram._VaryingInputs value is only used when in
649     * VP_MODE_FF mode and the fixed-func pipeline is emulated by shaders.
650     */
651    ctx->VertexProgram._VPModeOptimizesConstantAttribs =
652       m == VP_MODE_FF;
653 
654    /* Set a filter mask for the net enabled vao arrays.
655     * This is to mask out arrays that would otherwise supersede required current
656     * values for the fixed function shaders for example.
657     */
658    switch (m) {
659    case VP_MODE_FF:
660       /* When no vertex program is active (or the vertex program is generated
661        * from fixed-function state).  We put the material values into the
662        * generic slots.  Since the vao has no material arrays, mute these
663        * slots from the enabled arrays so that the current material values
664        * are pulled instead of the vao arrays.
665        */
666       ctx->VertexProgram._VPModeInputFilter = VERT_BIT_FF_ALL;
667       break;
668 
669    case VP_MODE_SHADER:
670       /* There are no shaders in OpenGL ES 1.x, so this code path should be
671        * impossible to reach.  The meta code is careful to not use shaders in
672        * ES1.
673        */
674       assert(ctx->API != API_OPENGLES);
675 
676       /* Other parts of the code assume that inputs[VERT_ATTRIB_POS] through
677        * inputs[VERT_ATTRIB_GENERIC0-1] will be non-NULL.  However, in OpenGL
678        * ES 2.0+ or OpenGL core profile, none of these arrays should ever
679        * be enabled.
680        */
681       if (_mesa_is_desktop_gl_compat(ctx))
682          ctx->VertexProgram._VPModeInputFilter = VERT_BIT_ALL;
683       else
684          ctx->VertexProgram._VPModeInputFilter = VERT_BIT_GENERIC_ALL;
685       break;
686 
687    default:
688       assert(0);
689    }
690 
691    _mesa_set_varying_vp_inputs(ctx, ctx->VertexProgram._VPModeInputFilter &
692                                ctx->Array._DrawVAO->_EnabledWithMapMode);
693 }
694 
695 
696 /**
697  * Update ctx->VertexProgram._VPMode.
698  * This is to distinguish whether we're running
699  *   a vertex program/shader,
700  *   a fixed-function TNL program or
701  *   a fixed function vertex transformation without any program.
702  */
703 void
_mesa_update_vertex_processing_mode(struct gl_context * ctx)704 _mesa_update_vertex_processing_mode(struct gl_context *ctx)
705 {
706    if (ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX])
707       set_vertex_processing_mode(ctx, VP_MODE_SHADER);
708    else if (_mesa_arb_vertex_program_enabled(ctx))
709       set_vertex_processing_mode(ctx, VP_MODE_SHADER);
710    else
711       set_vertex_processing_mode(ctx, VP_MODE_FF);
712 }
713 
714 
715 void
_mesa_reset_vertex_processing_mode(struct gl_context * ctx)716 _mesa_reset_vertex_processing_mode(struct gl_context *ctx)
717 {
718    ctx->VertexProgram._VPMode = -1; /* force the update */
719    _mesa_update_vertex_processing_mode(ctx);
720 }
721