svga: refactor some shader code
authorBrian Paul <brianp@vmware.com>
Sat, 1 Feb 2014 00:23:11 +0000 (17:23 -0700)
committerBrian Paul <brianp@vmware.com>
Mon, 3 Feb 2014 19:40:13 +0000 (12:40 -0700)
Put common code in new svga_shader.c file.  Considate separate vertex/
fragment shader ID generation.

Reviewed-by: Jose Fonseca <jfonseca@vmware.com>
src/gallium/drivers/svga/Makefile.sources
src/gallium/drivers/svga/svga_context.c
src/gallium/drivers/svga/svga_context.h
src/gallium/drivers/svga/svga_pipe_fs.c
src/gallium/drivers/svga/svga_pipe_vs.c
src/gallium/drivers/svga/svga_shader.c [new file with mode: 0644]
src/gallium/drivers/svga/svga_shader.h [new file with mode: 0644]
src/gallium/drivers/svga/svga_state_fs.c
src/gallium/drivers/svga/svga_state_vs.c
src/gallium/drivers/svga/svga_tgsi.c
src/gallium/drivers/svga/svga_tgsi.h

index de4622b0a27624a94a1a111922eb5517157431ea..525cb91a4e0e9a54e575bd0fe2808052b0f64fd7 100644 (file)
@@ -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 \
index 21fe73ad152bff483b91d9c5eff42781a194ac85..c32051a41db8e9434d015741006812c4c6aa875f 100644 (file)
@@ -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);
index f75fac5a1ccc2f504db4cee7f745254c543802ea..71d4014170d680edd2d4026582bb33468712db29 100644 (file)
@@ -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];
index 7bdcd8eb4bb869dceb4fc966684e13173c3197c4..75299c50db714268ed2f06ae3b2cc02f75b8b307 100644 (file)
@@ -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
index fd132ebf28012ce63ac38628fc4b4a5dc6fb56d2..c3ac663b4a2ec4feb82655c1d8cfa71701d84b45 100644 (file)
@@ -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 (file)
index 0000000..88877b2
--- /dev/null
@@ -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 (file)
index 0000000..348dc0c
--- /dev/null
@@ -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 */
index 1afdae95e0f5c7f77f1ee3a8f941fc3d0e51a575..860a0c8e0cf9e98ac31421169b8d52f9109b3f8e 100644 (file)
@@ -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;
 }
index 208bdd3626ab212b512d16991e1d3ebd9f024f6b..aaef17ef35f7a9882e704372c3e1ca1ac233bd7f 100644 (file)
@@ -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;
 }
index 368f131c6b2b1668c221215d92a8a475d30294a4..9aafd8512643c3e0e9347b0c1d74486f4eb5ba0c 100644 (file)
@@ -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);
-}
index b32b7081e9a35099474bc23c36e4182339451230..9a0c7b8f41e27f9c8b0cefe58caacb2f988de798 100644 (file)
@@ -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);