[Mesa-dev] [PATCHv2 12/13] i965: refactor do_wm_prog

Chia-I Wu olvaffe at gmail.com
Wed Jul 9 00:47:53 PDT 2014


Split do_wm_prog into

  brw_wm_init_compile
  brw_wm_do_compile
  brw_wm_upload_compile
  brw_wm_clear_complile

Add struct brw_wm_compile to be passed around them.

Signed-off-by: Chia-I Wu <olv at lunarg.com>
---
 src/mesa/drivers/dri/i965/brw_wm.c | 119 ++++++++++++++++++++++++-------------
 src/mesa/drivers/dri/i965/brw_wm.h |  30 ++++++++++
 2 files changed, 107 insertions(+), 42 deletions(-)

diff --git a/src/mesa/drivers/dri/i965/brw_wm.c b/src/mesa/drivers/dri/i965/brw_wm.c
index d716e6f..6849963 100644
--- a/src/mesa/drivers/dri/i965/brw_wm.c
+++ b/src/mesa/drivers/dri/i965/brw_wm.c
@@ -135,27 +135,30 @@ brw_wm_prog_data_compare(const void *in_a, const void *in_b)
    return true;
 }
 
-/**
- * All Mesa program -> GPU code generation goes through this function.
- * Depending on the instructions used (i.e. flow control instructions)
- * we'll use one of two code generators.
- */
-bool do_wm_prog(struct brw_context *brw,
-		struct gl_shader_program *prog,
-		struct brw_fragment_program *fp,
-		struct brw_wm_prog_key *key)
+void
+brw_wm_init_compile(struct brw_context *brw,
+		    struct gl_shader_program *prog,
+		    struct brw_fragment_program *fp,
+		    const struct brw_wm_prog_key *key,
+		    struct brw_wm_compile *c)
+{
+   memset(c, 0, sizeof(*c));
+
+   c->shader_prog = prog;
+   c->fp = fp;
+   c->key = key;
+   c->mem_ctx = ralloc_context(NULL);
+}
+
+bool
+brw_wm_do_compile(struct brw_context *brw,
+                  struct brw_wm_compile *c)
 {
    struct gl_context *ctx = &brw->ctx;
-   void *mem_ctx = ralloc_context(NULL);
-   struct brw_wm_prog_data prog_data;
-   const GLuint *program;
    struct gl_shader *fs = NULL;
-   GLuint program_size;
 
-   if (prog)
-      fs = prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
-
-   memset(&prog_data, 0, sizeof(prog_data));
+   if (c->shader_prog)
+      fs = c->shader_prog->_LinkedShaders[MESA_SHADER_FRAGMENT];
 
    /* Allocate the references to the uniforms that will end up in the
     * prog_data associated with the compiled program, and which will be freed
@@ -165,42 +168,74 @@ bool do_wm_prog(struct brw_context *brw,
    if (fs) {
       param_count = fs->num_uniform_components;
    } else {
-      param_count = fp->program.Base.Parameters->NumParameters * 4;
+      param_count = c->fp->program.Base.Parameters->NumParameters * 4;
    }
    /* The backend also sometimes adds params for texture size. */
    param_count += 2 * ctx->Const.Program[MESA_SHADER_FRAGMENT].MaxTextureImageUnits;
-   prog_data.base.param = rzalloc_array(NULL, const float *, param_count);
-   prog_data.base.pull_param =
-   rzalloc_array(NULL, const float *, param_count);
-   prog_data.base.nr_params = param_count;
-
-   prog_data.barycentric_interp_modes =
-      brw_compute_barycentric_interp_modes(brw, key->flat_shade,
-                                           key->persample_shading,
-                                           &fp->program);
-
-   program = brw_wm_fs_emit(brw, mem_ctx, key, &prog_data,
-                            &fp->program, prog, &program_size);
-   if (program == NULL) {
-      ralloc_free(mem_ctx);
+   c->prog_data.base.param = rzalloc_array(NULL, const float *, param_count);
+   c->prog_data.base.pull_param =
+      rzalloc_array(NULL, const float *, param_count);
+   c->prog_data.base.nr_params = param_count;
+
+   c->prog_data.barycentric_interp_modes =
+      brw_compute_barycentric_interp_modes(brw, c->key->flat_shade,
+                                           c->key->persample_shading,
+                                           &c->fp->program);
+
+   c->program = brw_wm_fs_emit(brw, c->mem_ctx, c->key, &c->prog_data,
+         &c->fp->program, c->shader_prog, &c->program_size);
+   if (c->program == NULL)
       return false;
-   }
-
-   if (prog_data.total_scratch) {
-      brw_get_scratch_bo(brw, &brw->wm.base.scratch_bo,
-			 prog_data.total_scratch * brw->max_wm_threads);
-   }
 
    if (unlikely(INTEL_DEBUG & DEBUG_WM))
       fprintf(stderr, "\n");
 
+   return true;
+}
+
+void
+brw_wm_upload_compile(struct brw_context *brw,
+                      const struct brw_wm_compile *c)
+{
+   if (c->prog_data.total_scratch) {
+      brw_get_scratch_bo(brw, &brw->wm.base.scratch_bo,
+			 c->prog_data.total_scratch * brw->max_wm_threads);
+   }
+
    brw_upload_cache(&brw->cache, BRW_WM_PROG,
-		    key, sizeof(struct brw_wm_prog_key),
-		    program, program_size,
-		    &prog_data, sizeof(prog_data),
+		    c->key, sizeof(struct brw_wm_prog_key),
+		    c->program, c->program_size,
+		    &c->prog_data, sizeof(c->prog_data),
 		    &brw->wm.base.prog_offset, &brw->wm.prog_data);
+}
+
+void
+brw_wm_clear_compile(struct brw_context *brw,
+                     struct brw_wm_compile *c)
+{
+   ralloc_free(c->mem_ctx);
+}
+
+/**
+ * All Mesa program -> GPU code generation goes through this function.
+ * Depending on the instructions used (i.e. flow control instructions)
+ * we'll use one of two code generators.
+ */
+bool do_wm_prog(struct brw_context *brw,
+		struct gl_shader_program *prog,
+		struct brw_fragment_program *fp,
+		struct brw_wm_prog_key *key)
+{
+   struct brw_wm_compile c;
+
+   brw_wm_init_compile(brw, prog, fp, key, &c);
+   if (!brw_wm_do_compile(brw, &c)) {
+      brw_wm_clear_compile(brw, &c);
+      return false;
+   }
 
-   ralloc_free(mem_ctx);
+   brw_wm_upload_compile(brw, &c);
+   brw_wm_clear_compile(brw, &c);
 
    return true;
 }
diff --git a/src/mesa/drivers/dri/i965/brw_wm.h b/src/mesa/drivers/dri/i965/brw_wm.h
index 7458301..b1a6e8f 100644
--- a/src/mesa/drivers/dri/i965/brw_wm.h
+++ b/src/mesa/drivers/dri/i965/brw_wm.h
@@ -80,6 +80,36 @@ struct brw_wm_prog_key {
    struct brw_sampler_prog_key_data tex;
 };
 
+struct brw_wm_compile {
+   struct gl_shader_program *shader_prog;
+   struct brw_fragment_program *fp;
+   const struct brw_wm_prog_key *key;
+
+   struct brw_wm_prog_data prog_data;
+   void *mem_ctx;
+   const unsigned *program;
+   unsigned program_size;
+};
+
+void
+brw_wm_init_compile(struct brw_context *brw,
+		    struct gl_shader_program *prog,
+		    struct brw_fragment_program *fp,
+		    const struct brw_wm_prog_key *key,
+		    struct brw_wm_compile *c);
+
+bool
+brw_wm_do_compile(struct brw_context *brw,
+                  struct brw_wm_compile *c);
+
+void
+brw_wm_upload_compile(struct brw_context *brw,
+                      const struct brw_wm_compile *c);
+
+void
+brw_wm_clear_compile(struct brw_context *brw,
+                     struct brw_wm_compile *c);
+
 /**
  * Compile a fragment shader.
  *
-- 
2.0.0.rc2



More information about the mesa-dev mailing list