[Mesa-dev] [PATCH 1/2] svga: refactor some shader code

Brian Paul brianp at vmware.com
Fri Jan 31 16:24:18 PST 2014


Put common code in new svga_shader.c file.  Considate separate vertex/
fragment shader ID generation.
---
 src/gallium/drivers/svga/Makefile.sources |    1 +
 src/gallium/drivers/svga/svga_context.c   |   19 ++----
 src/gallium/drivers/svga/svga_context.h   |    3 +-
 src/gallium/drivers/svga/svga_pipe_fs.c   |   14 +---
 src/gallium/drivers/svga/svga_pipe_vs.c   |   14 +---
 src/gallium/drivers/svga/svga_shader.c    |  101 +++++++++++++++++++++++++++++
 src/gallium/drivers/svga/svga_shader.h    |   44 +++++++++++++
 src/gallium/drivers/svga/svga_state_fs.c  |   20 ++----
 src/gallium/drivers/svga/svga_state_vs.c  |   20 ++----
 src/gallium/drivers/svga/svga_tgsi.c      |    8 ---
 src/gallium/drivers/svga/svga_tgsi.h      |    3 -
 11 files changed, 171 insertions(+), 76 deletions(-)
 create mode 100644 src/gallium/drivers/svga/svga_shader.c
 create mode 100644 src/gallium/drivers/svga/svga_shader.h

diff --git a/src/gallium/drivers/svga/Makefile.sources b/src/gallium/drivers/svga/Makefile.sources
index de4622b..525cb91 100644
--- a/src/gallium/drivers/svga/Makefile.sources
+++ b/src/gallium/drivers/svga/Makefile.sources
@@ -24,6 +24,7 @@ C_SOURCES := \
 	svga_pipe_vs.c \
 	svga_screen.c \
 	svga_screen_cache.c \
+	svga_shader.c \
 	svga_state.c \
 	svga_state_need_swtnl.c \
 	svga_state_constants.c \
diff --git a/src/gallium/drivers/svga/svga_context.c b/src/gallium/drivers/svga/svga_context.c
index 21fe73a..c32051a 100644
--- a/src/gallium/drivers/svga/svga_context.c
+++ b/src/gallium/drivers/svga/svga_context.c
@@ -68,8 +68,7 @@ static void svga_destroy( struct pipe_context *pipe )
    
    svga_destroy_swtnl( svga );
 
-   util_bitmask_destroy( svga->vs_bm );
-   util_bitmask_destroy( svga->fs_bm );
+   util_bitmask_destroy( svga->shader_id_bm );
 
    for(shader = 0; shader < PIPE_SHADER_TYPES; ++shader)
       pipe_resource_reference( &svga->curr.cb[shader], NULL );
@@ -124,13 +123,9 @@ struct pipe_context *svga_context_create( struct pipe_screen *screen,
    svga->debug.no_line_width = debug_get_option_no_line_width();
    svga->debug.force_hw_line_stipple = debug_get_option_force_hw_line_stipple();
 
-   svga->fs_bm = util_bitmask_create();
-   if (svga->fs_bm == NULL)
-      goto no_fs_bm;
-
-   svga->vs_bm = util_bitmask_create();
-   if (svga->vs_bm == NULL)
-      goto no_vs_bm;
+   svga->shader_id_bm = util_bitmask_create();
+   if (svga->shader_id_bm == NULL)
+      goto no_shader_bm;
 
    svga->hwtnl = svga_hwtnl_create(svga);
    if (svga->hwtnl == NULL)
@@ -164,10 +159,8 @@ no_state:
 no_swtnl:
    svga_hwtnl_destroy( svga->hwtnl );
 no_hwtnl:
-   util_bitmask_destroy( svga->vs_bm );
-no_vs_bm:
-   util_bitmask_destroy( svga->fs_bm );
-no_fs_bm:
+   util_bitmask_destroy( svga->shader_id_bm );
+no_shader_bm:
    svga->swc->destroy(svga->swc);
 no_swc:
    FREE(svga);
diff --git a/src/gallium/drivers/svga/svga_context.h b/src/gallium/drivers/svga/svga_context.h
index f75fac5..71d4014 100644
--- a/src/gallium/drivers/svga/svga_context.h
+++ b/src/gallium/drivers/svga/svga_context.h
@@ -347,8 +347,7 @@ struct svga_context
    } swtnl;
 
    /* Bitmask of used shader IDs */
-   struct util_bitmask *fs_bm;
-   struct util_bitmask *vs_bm;
+   struct util_bitmask *shader_id_bm;
 
    struct {
       unsigned dirty[SVGA_STATE_MAX];
diff --git a/src/gallium/drivers/svga/svga_pipe_fs.c b/src/gallium/drivers/svga/svga_pipe_fs.c
index 7bdcd8e..75299c5 100644
--- a/src/gallium/drivers/svga/svga_pipe_fs.c
+++ b/src/gallium/drivers/svga/svga_pipe_fs.c
@@ -35,6 +35,7 @@
 #include "svga_hw_reg.h"
 #include "svga_cmd.h"
 #include "svga_debug.h"
+#include "svga_shader.h"
 
 
 static void *
@@ -98,17 +99,8 @@ svga_delete_fs_state(struct pipe_context *pipe, void *shader)
    for (variant = fs->base.variants; variant; variant = tmp) {
       tmp = variant->next;
 
-      ret = SVGA3D_DestroyShader(svga->swc, variant->id, SVGA3D_SHADERTYPE_PS);
-      if (ret != PIPE_OK) {
-         svga_context_flush(svga, NULL);
-         ret = SVGA3D_DestroyShader(svga->swc, variant->id,
-                                    SVGA3D_SHADERTYPE_PS);
-         assert(ret == PIPE_OK);
-      }
-
-      util_bitmask_clear(svga->fs_bm, variant->id);
-
-      svga_destroy_shader_variant(variant);
+      ret = svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_PS, variant);
+      (void) ret;  /* PIPE_ERROR_ not handled yet */
 
       /*
        * Remove stale references to this variant to ensure a new variant on the
diff --git a/src/gallium/drivers/svga/svga_pipe_vs.c b/src/gallium/drivers/svga/svga_pipe_vs.c
index fd132eb..c3ac663 100644
--- a/src/gallium/drivers/svga/svga_pipe_vs.c
+++ b/src/gallium/drivers/svga/svga_pipe_vs.c
@@ -36,6 +36,7 @@
 #include "svga_hw_reg.h"
 #include "svga_cmd.h"
 #include "svga_debug.h"
+#include "svga_shader.h"
 
 
 /**
@@ -158,17 +159,8 @@ svga_delete_vs_state(struct pipe_context *pipe, void *shader)
    for (variant = vs->base.variants; variant; variant = tmp) {
       tmp = variant->next;
 
-      ret = SVGA3D_DestroyShader(svga->swc, variant->id, SVGA3D_SHADERTYPE_VS);
-      if (ret != PIPE_OK) {
-         svga_context_flush(svga, NULL);
-         ret = SVGA3D_DestroyShader(svga->swc, variant->id,
-                                    SVGA3D_SHADERTYPE_VS);
-         assert(ret == PIPE_OK);
-      }
-
-      util_bitmask_clear(svga->vs_bm, variant->id);
-
-      svga_destroy_shader_variant(variant);
+      ret = svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
+      (void) ret;  /* PIPE_ERROR_ not handled yet */
 
       /*
        * Remove stale references to this variant to ensure a new variant on the
diff --git a/src/gallium/drivers/svga/svga_shader.c b/src/gallium/drivers/svga/svga_shader.c
new file mode 100644
index 0000000..88877b2
--- /dev/null
+++ b/src/gallium/drivers/svga/svga_shader.c
@@ -0,0 +1,101 @@
+/**********************************************************
+ * Copyright 2008-2012 VMware, Inc.  All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ **********************************************************/
+
+#include "util/u_bitmask.h"
+#include "util/u_memory.h"
+#include "svga_context.h"
+#include "svga_cmd.h"
+#include "svga_shader.h"
+
+
+
+/**
+ * Issue the SVGA3D commands to define a new shader.
+ * \param result  contains the shader tokens, etc.  The result->id field will
+ *                be set here.
+ */
+enum pipe_error
+svga_define_shader(struct svga_context *svga,
+                   SVGA3dShaderType type,
+                   struct svga_shader_variant *variant)
+{
+   unsigned codeLen = variant->nr_tokens * sizeof(variant->tokens[0]);
+
+   {
+      enum pipe_error ret;
+
+      /* Allocate an integer ID for the shader */
+      variant->id = util_bitmask_add(svga->shader_id_bm);
+      if (variant->id == UTIL_BITMASK_INVALID_INDEX) {
+         return PIPE_ERROR_OUT_OF_MEMORY;
+      }
+
+      /* Issue SVGA3D device command to define the shader */
+      ret = SVGA3D_DefineShader(svga->swc,
+                                variant->id,
+                                type,
+                                variant->tokens,
+                                codeLen);
+      if (ret != PIPE_OK) {
+         /* free the ID */
+         assert(variant->id != UTIL_BITMASK_INVALID_INDEX);
+         util_bitmask_clear(svga->shader_id_bm, variant->id);
+         variant->id = UTIL_BITMASK_INVALID_INDEX;
+         return ret;
+      }
+   }
+
+   return PIPE_OK;
+}
+
+
+
+enum pipe_error
+svga_destroy_shader_variant(struct svga_context *svga,
+                            SVGA3dShaderType type,
+                            struct svga_shader_variant *variant)
+{
+   enum pipe_error ret = PIPE_OK;
+
+   /* first try */
+   if (variant->id != UTIL_BITMASK_INVALID_INDEX) {
+      ret = SVGA3D_DestroyShader(svga->swc, variant->id, type);
+
+      if (ret != PIPE_OK) {
+         /* flush and try again */
+         svga_context_flush(svga, NULL);
+
+         ret = SVGA3D_DestroyShader(svga->swc, variant->id, type);
+         assert(ret == PIPE_OK);
+      }
+
+      util_bitmask_clear(svga->shader_id_bm, variant->id);
+   }
+
+   FREE((unsigned *)variant->tokens);
+   FREE(variant);
+
+   return ret;
+}
diff --git a/src/gallium/drivers/svga/svga_shader.h b/src/gallium/drivers/svga/svga_shader.h
new file mode 100644
index 0000000..348dc0c
--- /dev/null
+++ b/src/gallium/drivers/svga/svga_shader.h
@@ -0,0 +1,44 @@
+/**********************************************************
+ * Copyright 2008-2012 VMware, Inc.  All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ **********************************************************/
+
+#ifndef SVGA_SHADER_H
+#define SVGA_SHADER_H
+
+#include "svga3d_reg.h"
+
+struct svga_shader_variant;
+
+enum pipe_error
+svga_define_shader(struct svga_context *svga,
+                   SVGA3dShaderType type,
+                   struct svga_shader_variant *variant);
+
+enum pipe_error
+svga_destroy_shader_variant(struct svga_context *svga,
+                            SVGA3dShaderType type,
+                            struct svga_shader_variant *variant);
+
+
+#endif /* SVGA_SHADER_H */
diff --git a/src/gallium/drivers/svga/svga_state_fs.c b/src/gallium/drivers/svga/svga_state_fs.c
index 1afdae9..860a0c8 100644
--- a/src/gallium/drivers/svga/svga_state_fs.c
+++ b/src/gallium/drivers/svga/svga_state_fs.c
@@ -33,6 +33,7 @@
 #include "svga_context.h"
 #include "svga_state.h"
 #include "svga_cmd.h"
+#include "svga_shader.h"
 #include "svga_resource_texture.h"
 #include "svga_tgsi.h"
 
@@ -134,30 +135,21 @@ compile_fs(struct svga_context *svga,
       }
    }
 
-   variant->id = util_bitmask_add(svga->fs_bm);
-   if(variant->id == UTIL_BITMASK_INVALID_INDEX) {
-      ret = PIPE_ERROR_OUT_OF_MEMORY;
-      goto fail;
-   }
-
-   ret = SVGA3D_DefineShader(svga->swc, 
-                             variant->id,
-                             SVGA3D_SHADERTYPE_PS,
-                             variant->tokens, 
-                             variant->nr_tokens * sizeof variant->tokens[0]);
+   ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_PS, variant);
    if (ret != PIPE_OK)
       goto fail;
 
    *out_variant = variant;
+
+   /* insert variants at head of linked list */
    variant->next = fs->base.variants;
    fs->base.variants = variant;
+
    return PIPE_OK;
 
 fail:
    if (variant) {
-      if (variant->id != UTIL_BITMASK_INVALID_INDEX)
-         util_bitmask_clear( svga->fs_bm, variant->id );
-      svga_destroy_shader_variant( variant );
+      svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_PS, variant);
    }
    return ret;
 }
diff --git a/src/gallium/drivers/svga/svga_state_vs.c b/src/gallium/drivers/svga/svga_state_vs.c
index 208bdd3..aaef17e 100644
--- a/src/gallium/drivers/svga/svga_state_vs.c
+++ b/src/gallium/drivers/svga/svga_state_vs.c
@@ -35,6 +35,7 @@
 #include "svga_context.h"
 #include "svga_state.h"
 #include "svga_cmd.h"
+#include "svga_shader.h"
 #include "svga_tgsi.h"
 
 #include "svga_hw_reg.h"
@@ -128,30 +129,21 @@ compile_vs(struct svga_context *svga,
       }
    }
 
-   variant->id = util_bitmask_add(svga->vs_bm);
-   if(variant->id == UTIL_BITMASK_INVALID_INDEX) {
-      ret = PIPE_ERROR_OUT_OF_MEMORY;
-      goto fail;
-   }
-
-   ret = SVGA3D_DefineShader(svga->swc, 
-                             variant->id,
-                             SVGA3D_SHADERTYPE_VS,
-                             variant->tokens, 
-                             variant->nr_tokens * sizeof variant->tokens[0]);
+   ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_VS, variant);
    if (ret != PIPE_OK)
       goto fail;
 
    *out_variant = variant;
+
+   /* insert variants at head of linked list */
    variant->next = vs->base.variants;
    vs->base.variants = variant;
+
    return PIPE_OK;
 
 fail:
    if (variant) {
-      if (variant->id != UTIL_BITMASK_INVALID_INDEX)
-         util_bitmask_clear( svga->vs_bm, variant->id );
-      svga_destroy_shader_variant( variant );
+      svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
    }
    return ret;
 }
diff --git a/src/gallium/drivers/svga/svga_tgsi.c b/src/gallium/drivers/svga/svga_tgsi.c
index 368f131..9aafd85 100644
--- a/src/gallium/drivers/svga/svga_tgsi.c
+++ b/src/gallium/drivers/svga/svga_tgsi.c
@@ -381,11 +381,3 @@ svga_translate_vertex_program(const struct svga_vertex_shader *vs,
 
    return svga_tgsi_translate(&vs->base, &key, PIPE_SHADER_VERTEX);
 }
-
-
-void
-svga_destroy_shader_variant(struct svga_shader_variant *variant)
-{
-   FREE((unsigned *) variant->tokens);
-   FREE(variant);
-}
diff --git a/src/gallium/drivers/svga/svga_tgsi.h b/src/gallium/drivers/svga/svga_tgsi.h
index b32b708..9a0c7b8 100644
--- a/src/gallium/drivers/svga/svga_tgsi.h
+++ b/src/gallium/drivers/svga/svga_tgsi.h
@@ -154,9 +154,6 @@ svga_translate_vertex_program( const struct svga_vertex_shader *fs,
                                const struct svga_vs_compile_key *vkey );
 
 
-void
-svga_destroy_shader_variant(struct svga_shader_variant *variant);
-
 unsigned
 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);
 
-- 
1.7.10.4



More information about the mesa-dev mailing list