compiler/glsl: explicitly store NumUniformBlocks
[mesa.git] / src / mesa / main / state.c
index 3ca6f58c6a645faf2096f991b4679264969ce928..2f89cecd8abd3d04f682c7f85a13daea89e24ed1 100644 (file)
 #include "blend.h"
 
 
+void
+_mesa_update_allow_draw_out_of_order(struct gl_context *ctx)
+{
+   /* Out-of-order drawing is useful when vertex array draws and immediate
+    * mode are interleaved.
+    *
+    * Example with 3 draws:
+    *   glBegin();
+    *      glVertex();
+    *   glEnd();
+    *   glDrawElements();
+    *   glBegin();
+    *      glVertex();
+    *   glEnd();
+    *
+    * Out-of-order drawing changes the execution order like this:
+    *   glDrawElements();
+    *   glBegin();
+    *      glVertex();
+    *      glVertex();
+    *   glEnd();
+    *
+    * If out-of-order draws are enabled, immediate mode vertices are not
+    * flushed before glDrawElements, resulting in fewer draws and lower CPU
+    * overhead. This helps workstation applications.
+    *
+    * This is a simplified version of out-of-order determination to catch
+    * common cases.
+    *
+    * RadeonSI has a complete and more complicated out-of-order determination
+    * for driver-internal reasons.
+    */
+   /* Only the compatibility profile with immediate mode needs this. */
+   if (ctx->API != API_OPENGL_COMPAT || !ctx->Const.AllowDrawOutOfOrder)
+      return;
+
+   /* If all of these are NULL, GLSL is disabled. */
+   struct gl_program *vs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX];
+   struct gl_program *tcs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_CTRL];
+   struct gl_program *tes =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_TESS_EVAL];
+   struct gl_program *gs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_GEOMETRY];
+   struct gl_program *fs =
+      ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT];
+   GLenum16 depth_func = ctx->Depth.Func;
+
+   /* Z fighting and any primitives with equal Z shouldn't be reordered
+    * with LESS/LEQUAL/GREATER/GEQUAL functions.
+    *
+    * When drawing 2 primitive with equal Z:
+    * - with LEQUAL/GEQUAL, the last primitive wins the Z test.
+    * - with LESS/GREATER, the first primitive wins the Z test.
+    *
+    * Here we ignore that on the basis that such cases don't occur in real
+    * apps, and we they do occur, they occur with blending where out-of-order
+    * drawing is always disabled.
+    */
+   bool previous_state = ctx->_AllowDrawOutOfOrder;
+   ctx->_AllowDrawOutOfOrder =
+         ctx->DrawBuffer &&
+         ctx->DrawBuffer->Visual.depthBits &&
+         ctx->Depth.Test &&
+         ctx->Depth.Mask &&
+         (depth_func == GL_NEVER ||
+          depth_func == GL_LESS ||
+          depth_func == GL_LEQUAL ||
+          depth_func == GL_GREATER ||
+          depth_func == GL_GEQUAL) &&
+         (!ctx->DrawBuffer->Visual.stencilBits ||
+          !ctx->Stencil.Enabled) &&
+         (!ctx->Color.ColorMask ||
+          (!ctx->Color.BlendEnabled &&
+           (!ctx->Color.ColorLogicOpEnabled ||
+            ctx->Color._LogicOp == COLOR_LOGICOP_COPY))) &&
+         (!vs || !vs->info.writes_memory) &&
+         (!tes || !tes->info.writes_memory) &&
+         (!tcs || !tcs->info.writes_memory) &&
+         (!gs || !gs->info.writes_memory) &&
+         (!fs || !fs->info.writes_memory || !fs->info.fs.early_fragment_tests);
+
+   /* If we are disabling out-of-order drawing, we need to flush queued
+    * vertices.
+    */
+   if (previous_state && !ctx->_AllowDrawOutOfOrder)
+      FLUSH_VERTICES(ctx, 0);
+}
+
+
 /**
  * Update the ctx->*Program._Current pointers to point to the
  * current/active programs.
@@ -272,6 +363,23 @@ update_program_constants(struct gl_context *ctx)
 }
 
 
+static void
+update_fixed_func_program_usage(struct gl_context *ctx)
+{
+   ctx->FragmentProgram._UsesTexEnvProgram =
+      ctx->FragmentProgram._MaintainTexEnvProgram &&
+      !ctx->_Shader->CurrentProgram[MESA_SHADER_FRAGMENT] && /* GLSL*/
+      !_mesa_arb_fragment_program_enabled(ctx) &&
+      !(_mesa_ati_fragment_shader_enabled(ctx) &&
+        ctx->ATIFragmentShader.Current->Program);
+
+   ctx->VertexProgram._UsesTnlProgram =
+      ctx->VertexProgram._MaintainTnlProgram &&
+      !ctx->_Shader->CurrentProgram[MESA_SHADER_VERTEX] && /* GLSL */
+      !_mesa_arb_vertex_program_enabled(ctx);
+}
+
+
 /**
  * Compute derived GL state.
  * If __struct gl_contextRec::NewState is non-zero then this function \b must
@@ -308,14 +416,17 @@ _mesa_update_state_locked( struct gl_context *ctx )
        ctx->API == API_OPENGLES) {
       GLbitfield prog_flags = _NEW_PROGRAM;
 
-      /* Determine which state flags effect vertex/fragment program state */
-      if (ctx->FragmentProgram._MaintainTexEnvProgram) {
+      if (new_state & _NEW_PROGRAM)
+         update_fixed_func_program_usage(ctx);
+
+      /* Determine which states affect fixed-func vertex/fragment program. */
+      if (ctx->FragmentProgram._UsesTexEnvProgram) {
          prog_flags |= (_NEW_BUFFERS | _NEW_TEXTURE_OBJECT | _NEW_FOG |
                         _NEW_VARYING_VP_INPUTS | _NEW_LIGHT | _NEW_POINT |
-                        _NEW_RENDERMODE | _NEW_PROGRAM | _NEW_FRAG_CLAMP |
-                        _NEW_COLOR | _NEW_TEXTURE_STATE);
+                        _NEW_RENDERMODE | _NEW_COLOR | _NEW_TEXTURE_STATE);
       }
-      if (ctx->VertexProgram._MaintainTnlProgram) {
+
+      if (ctx->VertexProgram._UsesTnlProgram) {
          prog_flags |= (_NEW_VARYING_VP_INPUTS | _NEW_TEXTURE_OBJECT |
                         _NEW_TEXTURE_MATRIX | _NEW_TRANSFORM | _NEW_POINT |
                         _NEW_FOG | _NEW_LIGHT | _NEW_TEXTURE_STATE |
@@ -372,7 +483,6 @@ _mesa_update_state_locked( struct gl_context *ctx )
    new_prog_state |= update_program_constants(ctx);
 
    ctx->NewState |= new_prog_state;
-   vbo_exec_invalidate_state(ctx);
 
    /*
     * Give the driver a chance to act upon the new_state flags.
@@ -423,19 +533,21 @@ _mesa_update_state( struct gl_context *ctx )
 static void
 set_varying_vp_inputs(struct gl_context *ctx, GLbitfield varying_inputs)
 {
-   if (ctx->API != API_OPENGL_COMPAT &&
-       ctx->API != API_OPENGLES)
+   /*
+    * The gl_context::varying_vp_inputs value is only used when in
+    * VP_MODE_FF mode.
+    */
+   if (VP_MODE_FF != ctx->VertexProgram._VPMode)
+      return;
+
+   /* Only fixed-func generated programs ever uses varying_vp_inputs. */
+   if (!ctx->VertexProgram._MaintainTnlProgram &&
+       !ctx->FragmentProgram._MaintainTexEnvProgram)
       return;
 
    if (ctx->varying_vp_inputs != varying_inputs) {
       ctx->varying_vp_inputs = varying_inputs;
-
-      /* Only fixed-func generated programs ever use varying_vp_inputs. */
-      if (ctx->VertexProgram._MaintainTnlProgram ||
-          ctx->FragmentProgram._MaintainTexEnvProgram) {
-         ctx->NewState |= _NEW_VARYING_VP_INPUTS;
-      }
-      /*printf("%s %x\n", __func__, varying_inputs);*/
+      ctx->NewState |= _NEW_VARYING_VP_INPUTS;
    }
 }
 
@@ -471,6 +583,12 @@ set_vertex_processing_mode(struct gl_context *ctx, gl_vertex_processing_mode m)
 
    /* Finally memorize the value */
    ctx->VertexProgram._VPMode = m;
+
+   /* Since we only track the varying inputs while being in fixed function
+    * vertex processing mode, we may need to recheck for the
+    * _NEW_VARYING_VP_INPUTS bit.
+    */
+   set_varying_vp_inputs(ctx, ctx->Array._DrawVAOEnabledAttribs);
 }