gallium: no need to keep a copy of shader tokens in state tracker
authorKeith Whitwell <keithw@vmware.com>
Fri, 13 Mar 2009 16:22:35 +0000 (16:22 +0000)
committerKeith Whitwell <keithw@vmware.com>
Fri, 13 Mar 2009 16:24:22 +0000 (16:24 +0000)
Any driver who needs a copy of the shader tokens must organize to
do so itself.  This has been the case for a long time, but there
was still defensive code in the state tracker, which is now removed.

Any bugs resulting from this need to be fixed in the offending driver...

src/gallium/auxiliary/util/u_blit.c
src/gallium/auxiliary/util/u_gen_mipmap.c
src/gallium/auxiliary/util/u_simple_shaders.c
src/gallium/auxiliary/util/u_simple_shaders.h
src/gallium/drivers/softpipe/sp_fs_sse.c
src/gallium/drivers/softpipe/sp_state.h
src/gallium/drivers/softpipe/sp_state_fs.c
src/mesa/state_tracker/st_atom_shader.c
src/mesa/state_tracker/st_cb_bitmap.c
src/mesa/state_tracker/st_cb_clear.c
src/mesa/state_tracker/st_context.h

index 3b3777b8730eef9166e6ccc656eea2797ecdc4ce..98bb6a2ce707f3eea47dee543d869789df683273 100644 (file)
@@ -60,8 +60,6 @@ struct blit_state
    struct pipe_sampler_state sampler;
    struct pipe_viewport_state viewport;
 
-   struct pipe_shader_state vert_shader;
-   struct pipe_shader_state frag_shader;
    void *vs;
    void *fs;
 
@@ -134,12 +132,11 @@ util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
                                       TGSI_SEMANTIC_GENERIC };
       const uint semantic_indexes[] = { 0, 0 };
       ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
-                                                    semantic_indexes,
-                                                    &ctx->vert_shader);
+                                                    semantic_indexes);
    }
 
    /* fragment shader */
-   ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader);
+   ctx->fs = util_make_fragment_tex_shader(pipe);
    ctx->vbuf = NULL;
 
    /* init vertex data that doesn't change */
@@ -164,9 +161,6 @@ util_destroy_blit(struct blit_state *ctx)
    pipe->delete_vs_state(pipe, ctx->vs);
    pipe->delete_fs_state(pipe, ctx->fs);
 
-   FREE((void*) ctx->vert_shader.tokens);
-   FREE((void*) ctx->frag_shader.tokens);
-
    pipe_buffer_reference(&ctx->vbuf, NULL);
 
    FREE(ctx);
index 2b675e71b2305ea39cb0b18dbeb3a14d53dbfe46..847b4a6075e3559793c59fd6591853ee4ee8c5d1 100644 (file)
@@ -64,8 +64,6 @@ struct gen_mipmap_state
    struct pipe_sampler_state sampler;
    struct pipe_viewport_state viewport;
 
-   struct pipe_shader_state vert_shader;
-   struct pipe_shader_state frag_shader;
    void *vs;
    void *fs;
 
@@ -1322,12 +1320,11 @@ util_create_gen_mipmap(struct pipe_context *pipe,
                                       TGSI_SEMANTIC_GENERIC };
       const uint semantic_indexes[] = { 0, 0 };
       ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
-                                                    semantic_indexes,
-                                                    &ctx->vert_shader);
+                                                    semantic_indexes);
    }
 
    /* fragment shader */
-   ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader);
+   ctx->fs = util_make_fragment_tex_shader(pipe);
 
    /* vertex data that doesn't change */
    for (i = 0; i < 4; i++) {
@@ -1412,9 +1409,6 @@ util_destroy_gen_mipmap(struct gen_mipmap_state *ctx)
    pipe->delete_vs_state(pipe, ctx->vs);
    pipe->delete_fs_state(pipe, ctx->fs);
 
-   FREE((void*) ctx->vert_shader.tokens);
-   FREE((void*) ctx->frag_shader.tokens);
-
    pipe_buffer_reference(&ctx->vbuf, NULL);
 
    FREE(ctx);
index 8da18d67156b04c134fa0a3df83ba7b3aace85b9..e519c354d257de372b2794bb26e26d7798a913ea 100644 (file)
@@ -55,12 +55,11 @@ void *
 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
                                     uint num_attribs,
                                     const uint *semantic_names,
-                                    const uint *semantic_indexes,
-                                    struct pipe_shader_state *shader)
+                                    const uint *semantic_indexes)
                                     
 {
-   uint maxTokens = 100;
-   struct tgsi_token *tokens;
+   struct pipe_shader_state shader;
+   struct tgsi_token tokens[100];
    struct tgsi_header *header;
    struct tgsi_processor *processor;
    struct tgsi_full_declaration decl;
@@ -68,8 +67,6 @@ util_make_vertex_passthrough_shader(struct pipe_context *pipe,
    const uint procType = TGSI_PROCESSOR_VERTEX;
    uint ti, i;
 
-   tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
-
    /* shader header
     */
    *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
@@ -96,7 +93,7 @@ util_make_vertex_passthrough_shader(struct pipe_context *pipe,
       ti += tgsi_build_full_declaration(&decl,
                                         &tokens[ti],
                                         header,
-                                        maxTokens - ti);
+                                        Elements(tokens) - ti);
    }
 
    /* declare outputs */
@@ -111,7 +108,7 @@ util_make_vertex_passthrough_shader(struct pipe_context *pipe,
       ti += tgsi_build_full_declaration(&decl,
                                         &tokens[ti],
                                         header,
-                                        maxTokens - ti);
+                                        Elements(tokens) - ti);
    }
 
    /* emit MOV instructions */
@@ -128,7 +125,7 @@ util_make_vertex_passthrough_shader(struct pipe_context *pipe,
       ti += tgsi_build_full_instruction(&inst,
                                         &tokens[ti],
                                         header,
-                                        maxTokens - ti );
+                                        Elements(tokens) - ti );
    }
 
    /* END instruction */
@@ -139,16 +136,15 @@ util_make_vertex_passthrough_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_instruction(&inst,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti );
+                                     Elements(tokens) - ti );
 
 #if 0 /*debug*/
    tgsi_dump(tokens, 0);
 #endif
 
-   shader->tokens = tokens;
-   /*shader->num_tokens = ti;*/
+   shader.tokens = tokens;
 
-   return pipe->create_vs_state(pipe, shader);
+   return pipe->create_vs_state(pipe, &shader);
 }
 
 
@@ -160,11 +156,10 @@ util_make_vertex_passthrough_shader(struct pipe_context *pipe,
  *  END;
  */
 void *
-util_make_fragment_tex_shader(struct pipe_context *pipe,
-                              struct pipe_shader_state *shader)
+util_make_fragment_tex_shader(struct pipe_context *pipe)
 {
-   uint maxTokens = 100;
-   struct tgsi_token *tokens;
+   struct pipe_shader_state shader;
+   struct tgsi_token tokens[100];
    struct tgsi_header *header;
    struct tgsi_processor *processor;
    struct tgsi_full_declaration decl;
@@ -172,8 +167,6 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
    const uint procType = TGSI_PROCESSOR_FRAGMENT;
    uint ti;
 
-   tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
-
    /* shader header
     */
    *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
@@ -199,7 +192,7 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_declaration(&decl,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti);
+                                     Elements(tokens) - ti);
 
    /* declare color[0] output */
    decl = tgsi_default_full_declaration();
@@ -212,7 +205,7 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_declaration(&decl,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti);
+                                     Elements(tokens) - ti);
 
    /* declare sampler */
    decl = tgsi_default_full_declaration();
@@ -222,7 +215,7 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_declaration(&decl,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti);
+                                     Elements(tokens) - ti);
 
    /* TEX instruction */
    inst = tgsi_default_full_instruction();
@@ -239,7 +232,7 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_instruction(&inst,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti );
+                                     Elements(tokens) - ti );
 
    /* END instruction */
    inst = tgsi_default_full_instruction();
@@ -249,16 +242,15 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_instruction(&inst,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti );
+                                     Elements(tokens) - ti );
 
 #if 0 /*debug*/
    tgsi_dump(tokens, 0);
 #endif
 
-   shader->tokens = tokens;
-   /*shader->num_tokens = ti;*/
+   shader.tokens = tokens;
 
-   return pipe->create_fs_state(pipe, shader);
+   return pipe->create_fs_state(pipe, &shader);
 }
 
 
@@ -269,11 +261,10 @@ util_make_fragment_tex_shader(struct pipe_context *pipe,
  * Make simple fragment color pass-through shader.
  */
 void *
-util_make_fragment_passthrough_shader(struct pipe_context *pipe,
-                                      struct pipe_shader_state *shader)
+util_make_fragment_passthrough_shader(struct pipe_context *pipe)
 {
-   uint maxTokens = 40;
-   struct tgsi_token *tokens;
+   struct pipe_shader_state shader;
+   struct tgsi_token tokens[40];
    struct tgsi_header *header;
    struct tgsi_processor *processor;
    struct tgsi_full_declaration decl;
@@ -281,8 +272,6 @@ util_make_fragment_passthrough_shader(struct pipe_context *pipe,
    const uint procType = TGSI_PROCESSOR_FRAGMENT;
    uint ti;
 
-   tokens = (struct tgsi_token *) MALLOC(maxTokens * sizeof(tokens[0]));
-
    /* shader header
     */
    *(struct tgsi_version *) &tokens[0] = tgsi_build_version();
@@ -306,7 +295,7 @@ util_make_fragment_passthrough_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_declaration(&decl,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti);
+                                     Elements(tokens) - ti);
 
    /* declare output */
    decl = tgsi_default_full_declaration();
@@ -319,7 +308,7 @@ util_make_fragment_passthrough_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_declaration(&decl,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti);
+                                     Elements(tokens) - ti);
 
 
    /* MOVE out[0], in[0]; */
@@ -334,7 +323,7 @@ util_make_fragment_passthrough_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_instruction(&inst,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti );
+                                     Elements(tokens) - ti );
 
    /* END instruction */
    inst = tgsi_default_full_instruction();
@@ -344,24 +333,17 @@ util_make_fragment_passthrough_shader(struct pipe_context *pipe,
    ti += tgsi_build_full_instruction(&inst,
                                      &tokens[ti],
                                      header,
-                                     maxTokens - ti );
+                                     Elements(tokens) - ti );
 
-   assert(ti < maxTokens);
+   assert(ti < Elements(tokens));
 
 #if 0 /*debug*/
    tgsi_dump(tokens, 0);
 #endif
 
-   shader->tokens = tokens;
-   /*shader->num_tokens = ti;*/
+   shader.tokens = tokens;
 
-   return pipe->create_fs_state(pipe, shader);
+   return pipe->create_fs_state(pipe, &shader);
 }
 
 
-void
-util_free_shader(struct pipe_shader_state *shader)
-{
-   FREE((struct tgsi_token *)shader->tokens);
-   shader->tokens = NULL;
-}
index 99b8d9067d6c57189ebc677fef9a8435e06b3bc5..6f8d96af9bcbf17526ce8032b20f915e373f64b8 100644 (file)
@@ -46,22 +46,15 @@ extern void *
 util_make_vertex_passthrough_shader(struct pipe_context *pipe,
                                     uint num_attribs,
                                     const uint *semantic_names,
-                                    const uint *semantic_indexes,
-                                    struct pipe_shader_state *shader);
+                                    const uint *semantic_indexes);
 
 
 extern void *
-util_make_fragment_tex_shader(struct pipe_context *pipe,
-                              struct pipe_shader_state *shader);
+util_make_fragment_tex_shader(struct pipe_context *pipe);
 
 
 extern void *
-util_make_fragment_passthrough_shader(struct pipe_context *pipe,
-                                      struct pipe_shader_state *shader);
-
-
-extern void
-util_free_shader(struct pipe_shader_state *shader);
+util_make_fragment_passthrough_shader(struct pipe_context *pipe);
 
 
 #ifdef __cplusplus
index abd37547a98eb0a0617ca38c0d8093a9dd76c272..366abe2ed49fab78ee0e9c5f5e1f403308496103 100644 (file)
@@ -145,7 +145,7 @@ softpipe_create_fs_sse(struct softpipe_context *softpipe,
       return NULL;
    }
 
-   shader->base.shader = *templ;
+   shader->base.shader.tokens = NULL; /* don't hold reference to templ->tokens */
    shader->base.prepare = fs_sse_prepare;
    shader->base.run = fs_sse_run;
    shader->base.delete = fs_sse_delete;
index 6f558e6da5aacf8771262d6379f263f172855d6e..9776e978e3e8f9e40027b2cbfd08c1f81fc4d584 100644 (file)
@@ -85,7 +85,7 @@ struct sp_fragment_shader {
 
 /** Subclass of pipe_shader_state */
 struct sp_vertex_shader {
-   struct pipe_shader_state shader;  /* Note: this field not actually used */
+   struct pipe_shader_state shader;
    struct draw_vertex_shader *draw_data;
 };
 
index b63180baa537a118f68820da10f797a834376bec..4330c2039356f45e3027b71321fe27642c26cf40 100644 (file)
@@ -36,6 +36,7 @@
 #include "draw/draw_context.h"
 #include "tgsi/tgsi_dump.h"
 #include "tgsi/tgsi_scan.h"
+#include "tgsi/tgsi_parse.h"
 
 
 void *
@@ -97,17 +98,28 @@ softpipe_create_vs_state(struct pipe_context *pipe,
    struct sp_vertex_shader *state;
 
    state = CALLOC_STRUCT(sp_vertex_shader);
-   if (state == NULL ) {
-      return NULL;
-   }
+   if (state == NULL ) 
+      goto fail;
+
+   /* copy shader tokens, the ones passed in will go away.
+    */
+   state->shader.tokens = tgsi_dup_tokens(templ->tokens);
+   if (state->shader.tokens == NULL)
+      goto fail;
 
    state->draw_data = draw_create_vertex_shader(softpipe->draw, templ);
-   if (state->draw_data == NULL) {
-      FREE( state );
-      return NULL;
-   }
+   if (state->draw_data == NULL) 
+      goto fail;
 
    return state;
+
+fail:
+   if (state) {
+      FREE( (void *)state->shader.tokens );
+      FREE( state->draw_data );
+      FREE( state );
+   }
+   return NULL;
 }
 
 
index 486582fce89cdf143f97408a2d473cd6a4ca2ef2..fc1ff5be04a59cc6d43b8c0885ea8512cdbddef1 100644 (file)
@@ -307,14 +307,9 @@ st_free_translated_vertex_programs(struct st_context *st,
 static void *
 get_passthrough_fs(struct st_context *st)
 {
-   struct pipe_shader_state shader;
-
    if (!st->passthrough_fs) {
       st->passthrough_fs =
-         util_make_fragment_passthrough_shader(st->pipe, &shader);
-#if 0      /* We actually need to keep the tokens around at this time */
-      util_free_shader(&shader);
-#endif
+         util_make_fragment_passthrough_shader(st->pipe);
    }
 
    return st->passthrough_fs;
index 3f503f1253290060f32e59d550e3ea05d722d499..f9f1780ba8832e002134b926bd5ea58b9d49b76a 100644 (file)
@@ -740,8 +740,7 @@ st_Bitmap(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
       const uint semantic_indexes[] = { 0, 0, 0 };
       st->bitmap.vs = util_make_vertex_passthrough_shader(st->pipe, 3,
                                                           semantic_names,
-                                                          semantic_indexes,
-                                                          &st->bitmap.vert_shader);
+                                                          semantic_indexes);
    }
 
    if (UseBitmapCache && accum_bitmap(st, x, y, width, height, unpack, bitmap))
@@ -830,7 +829,6 @@ st_destroy_bitmap(struct st_context *st)
       cso_delete_vertex_shader(st->cso_context, st->bitmap.vs);
       st->bitmap.vs = NULL;
    }
-   util_free_shader(&st->bitmap.vert_shader);
 
    if (st->bitmap.vbuf) {
       pipe_buffer_reference(&st->bitmap.vbuf, NULL);
index 7d4948a64ead3c4b71822b77d5a4d62110fef4b5..8206733f76687e0cf6c5ab33b38c16d78d9b511c 100644 (file)
@@ -77,7 +77,7 @@ st_init_clear(struct st_context *st)
 
    /* fragment shader state: color pass-through program */
    st->clear.fs =
-      util_make_fragment_passthrough_shader(pipe, &st->clear.frag_shader);
+      util_make_fragment_passthrough_shader(pipe);
 
    /* vertex shader state: color/position pass-through */
    {
@@ -86,8 +86,7 @@ st_init_clear(struct st_context *st)
       const uint semantic_indexes[] = { 0, 0 };
       st->clear.vs = util_make_vertex_passthrough_shader(pipe, 2,
                                                          semantic_names,
-                                                         semantic_indexes,
-                                                         &st->clear.vert_shader);
+                                                         semantic_indexes);
    }
 }
 
@@ -95,16 +94,6 @@ st_init_clear(struct st_context *st)
 void
 st_destroy_clear(struct st_context *st)
 {
-   if (st->clear.vert_shader.tokens) {
-      util_free_shader(&st->clear.vert_shader);
-      st->clear.vert_shader.tokens = NULL;
-   }
-
-   if (st->clear.frag_shader.tokens) {
-      util_free_shader(&st->clear.frag_shader);
-      st->clear.frag_shader.tokens = NULL;
-   }
-
    if (st->clear.fs) {
       cso_delete_fragment_shader(st->cso_context, st->clear.fs);
       st->clear.fs = NULL;
index 3547925ad7f320a7382d23d9df6e85613276ee05..d7518ab689765461306d1f85ab02fb9cff27f77a 100644 (file)
@@ -149,7 +149,6 @@ struct st_context
    struct {
       struct pipe_rasterizer_state rasterizer;
       struct pipe_sampler_state sampler;
-      struct pipe_shader_state vert_shader;
       enum pipe_format tex_format;
       void *vs;
       float vertices[4][3][4];  /**< vertex pos + color + texcoord */
@@ -166,8 +165,6 @@ struct st_context
 
    /** for glClear */
    struct {
-      struct pipe_shader_state vert_shader;
-      struct pipe_shader_state frag_shader;
       struct pipe_rasterizer_state raster;
       struct pipe_viewport_state viewport;
       void *vs;