[Mesa-dev] [PATCH 05/12] sso: replace Shader binding point with _Shader

Gregory Hainaut gregory.hainaut at gmail.com
Fri May 3 10:44:09 PDT 2013


To avoid NULL pointer check a default pipeline object is installed in _Shader when no
program is current

The spec say that UseProgram/UseShaderProgramEXT/ActiveProgramEXT got an higher
priority over the pipeline object. When default program is uninstall, the pipeline is
used if any was bound.

Note: A careful rename need to be done now...

V2: formating improvement
---
 src/mesa/main/mtypes.h      |    5 +++
 src/mesa/main/pipelineobj.c |    8 ++++
 src/mesa/main/shaderapi.c   |   87 +++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index f979cd0..adf518b 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -2434,6 +2434,9 @@ struct gl_pipeline_shader_state
    /** Currently bound pipeline object. See _mesa_BindProgramPipeline() */
    struct gl_pipeline_object *Current;
 
+   /* Default Object to ensure that _Shader is never NULL */
+   struct gl_pipeline_object *Default;
+
    /** Pipeline objects */
    struct _mesa_HashTable *Objects;
 };
@@ -3546,6 +3549,8 @@ struct gl_context
 
    struct gl_pipeline_shader_state Pipeline; /**< GLSL pipeline shader object state */
    struct gl_pipeline_object Shader; /**< GLSL shader object state */
+   struct gl_pipeline_object *_Shader; /**< Points to ::Shader or ::Pipeline.Current
+                                         or ::Pipeline.Default */
    struct gl_shader_compiler_options ShaderCompilerOptions[MESA_SHADER_TYPES];
 
    struct gl_query_state Query;  /**< occlusion, timer queries */
diff --git a/src/mesa/main/pipelineobj.c b/src/mesa/main/pipelineobj.c
index ffbeeae..5add307 100644
--- a/src/mesa/main/pipelineobj.c
+++ b/src/mesa/main/pipelineobj.c
@@ -95,6 +95,10 @@ _mesa_init_pipeline(struct gl_context *ctx)
    ctx->Pipeline.Objects = _mesa_NewHashTable();
 
    ctx->Pipeline.Current = NULL;
+
+   /* Install a default Pipeline */
+   ctx->Pipeline.Default = _mesa_new_pipeline_object(ctx, 0);
+   _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
 }
 
 
@@ -118,6 +122,10 @@ _mesa_free_pipeline_data(struct gl_context *ctx)
 {
    _mesa_HashDeleteAll(ctx->Pipeline.Objects, delete_pipelineobj_cb, ctx);
    _mesa_DeleteHashTable(ctx->Pipeline.Objects);
+
+   _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
+   _mesa_delete_pipeline_object(ctx, ctx->Pipeline.Default);
+
 }
 
 /**
diff --git a/src/mesa/main/shaderapi.c b/src/mesa/main/shaderapi.c
index 46072ba..c25f49f 100644
--- a/src/mesa/main/shaderapi.c
+++ b/src/mesa/main/shaderapi.c
@@ -43,6 +43,7 @@
 #include "main/hash.h"
 #include "main/mfeatures.h"
 #include "main/mtypes.h"
+#include "main/pipelineobj.h"
 #include "main/shaderapi.h"
 #include "main/shaderobj.h"
 #include "main/transformfeedback.h"
@@ -138,6 +139,8 @@ _mesa_free_shader_state(struct gl_context *ctx)
    _mesa_reference_shader_program(ctx, &ctx->Shader.ActiveProgram, NULL);
 
    /* Extended for ARB_separate_shader_objects */
+   _mesa_reference_pipeline_object(ctx, &ctx->_Shader, NULL);
+
    assert(ctx->Shader.RefCount == 1);
    _glthread_DESTROY_MUTEX(ctx->Shader.Mutex);
 }
@@ -1455,7 +1458,29 @@ _mesa_UseProgram(GLhandleARB program)
       shProg = NULL;
    }
 
-   _mesa_use_program(ctx, shProg);
+   /*
+    * The executable code for an individual shader stage is taken from the
+    * current program for that stage.  If there is a current program object
+    * for any shader stage or for uniform updates established by UseProgram,
+    * UseShaderProgramEXT, or ActiveProgramEXT, the current program for that
+    * stage (if any) is considered current.  Otherwise, if there is a bound
+    * program pipeline object ...
+    */
+   if (program) {
+      /* Attach shader state to the binding point */
+      _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
+      /* Update the program */
+      _mesa_use_program(ctx, shProg);
+   } else {
+      /* Must be done first: detach the progam */
+      _mesa_use_program(ctx, shProg);
+      /* Unattach shader_state binding point */
+      _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
+      /* If a pipeline was bound, rebind it */
+      if (ctx->Pipeline.Current) {
+         _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
+      }
+   }
 }
 
 
@@ -1778,7 +1803,35 @@ _mesa_UseShaderProgramEXT(GLenum type, GLuint program)
       }
    }
 
-   _mesa_use_shader_program(ctx, type, shProg);
+   /*
+    * The executable code for an individual shader stage is taken from the
+    * current program for that stage.  If there is a current program object
+    * for any shader stage or for uniform updates established by UseProgram,
+    * UseShaderProgramEXT, or ActiveProgramEXT, the current program for that
+    * stage (if any) is considered current.  Otherwise, if there is a bound
+    * program pipeline object ...
+    */
+   if (program) {
+      /* Attach shader state to the binding point */
+      _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
+      /* Update the program */
+      _mesa_use_shader_program(ctx, type, shProg, &ctx->Shader);
+   } else {
+      /* Must be done first: detach the progam */
+      _mesa_use_shader_program(ctx, type, shProg, &ctx->Shader);
+
+      /* Nothing remains current */
+      if (!ctx->Shader.CurrentVertexProgram && !ctx->Shader.CurrentGeometryProgram &&
+          !ctx->Shader.CurrentFragmentProgram && !ctx->Shader.ActiveProgram) {
+
+         /* Unattach shader_state binding point */
+         _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
+         /* If a pipeline was bound, rebind it */
+         if (ctx->Pipeline.Current) {
+            _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
+         }
+      }
+   }
 }
 
 
@@ -1793,7 +1846,35 @@ _mesa_ActiveProgramEXT(GLuint program)
       ? _mesa_lookup_shader_program_err(ctx, program, "glActiveProgramEXT")
       : NULL;
 
-   _mesa_active_program(ctx, shProg, "glActiveProgramEXT");
+   /*
+    * The executable code for an individual shader stage is taken from the
+    * current program for that stage.  If there is a current program object
+    * for any shader stage or for uniform updates established by UseProgram,
+    * UseShaderProgramEXT, or ActiveProgramEXT, the current program for that
+    * stage (if any) is considered current.  Otherwise, if there is a bound
+    * program pipeline object ...
+    */
+   if (shProg != NULL) {
+      /* Attach shader state to the binding point */
+      _mesa_reference_pipeline_object(ctx, &ctx->_Shader, &ctx->Shader);
+      _mesa_active_program(ctx, shProg, "glActiveProgramEXT");
+   } else {
+      /* Must be done first: unset the current active progam */
+      _mesa_active_program(ctx, shProg, "glActiveProgramEXT");
+
+      /* Nothing remains current */
+      if (!ctx->Shader.CurrentVertexProgram && !ctx->Shader.CurrentGeometryProgram &&
+          !ctx->Shader.CurrentFragmentProgram && !ctx->Shader.ActiveProgram) {
+
+         /* Unattach shader_state binding point */
+         _mesa_reference_pipeline_object(ctx, &ctx->_Shader, ctx->Pipeline.Default);
+         /* If a pipeline was bound, rebind it */
+         if (ctx->Pipeline.Current) {
+            _mesa_BindProgramPipeline(ctx->Pipeline.Current->Name);
+         }
+      }
+   }
+
    return;
 }
 
-- 
1.7.10.4



More information about the mesa-dev mailing list