mesa/st: don't calculate unused output_flags data either
[mesa.git] / src / mesa / state_tracker / st_program.c
index d2da20ae424710f01e238b8e1916ca85537f2a8d..5450b59a2d9cb8e37af30d5ce659f42fd129ed47 100644 (file)
@@ -42,6 +42,7 @@
 #include "draw/draw_context.h"
 #include "tgsi/tgsi_dump.h"
 
+#include "st_debug.h"
 #include "st_context.h"
 #include "st_atom.h"
 #include "st_program.h"
 #include "cso_cache/cso_context.h"
 
 
-#define ST_MAX_SHADER_TOKENS (8 * 1024)
-
-
-#define TGSI_DEBUG 0
-
-
 /**
  * Translate a Mesa vertex shader into a TGSI shader.
  * \param outputMapping  to map vertex program output registers (VERT_RESULT_x)
@@ -70,35 +65,16 @@ st_translate_vertex_program(struct st_context *st,
                             const ubyte *outputSemanticIndex)
 {
    struct pipe_context *pipe = st->pipe;
-   struct tgsi_token *tokens;
    GLuint defaultOutputMapping[VERT_RESULT_MAX];
-   struct pipe_shader_state vs;
    GLuint attr, i;
    GLuint num_generic = 0;
-   GLuint num_tokens;
 
-   ubyte vs_input_semantic_name[PIPE_MAX_SHADER_INPUTS];
-   ubyte vs_input_semantic_index[PIPE_MAX_SHADER_INPUTS];
    uint vs_num_inputs = 0;
 
    ubyte vs_output_semantic_name[PIPE_MAX_SHADER_OUTPUTS];
    ubyte vs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
    uint vs_num_outputs = 0;
 
-   GLbitfield input_flags[MAX_PROGRAM_INPUTS];
-   GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
-
-   tokens =  (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
-   if(!tokens) {
-      /* FIXME: propagate error to the caller */
-      assert(0);
-      return;
-   }
-
-   memset(&vs, 0, sizeof(vs));
-   memset(input_flags, 0, sizeof(input_flags));
-   memset(output_flags, 0, sizeof(output_flags));
-
    if (stvp->Base.IsPositionInvariant)
       _mesa_insert_mvp_code(st->ctx, &stvp->Base);
 
@@ -108,79 +84,10 @@ st_translate_vertex_program(struct st_context *st,
     */
    for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
       if (stvp->Base.Base.InputsRead & (1 << attr)) {
-         const GLuint slot = vs_num_inputs;
+         stvp->input_to_index[attr] = vs_num_inputs;
+         stvp->index_to_input[vs_num_inputs] = attr;
 
          vs_num_inputs++;
-
-         stvp->input_to_index[attr] = slot;
-         stvp->index_to_input[slot] = attr;
-
-         switch (attr) {
-         case VERT_ATTRIB_POS:
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_POSITION;
-            vs_input_semantic_index[slot] = 0;
-            break;
-         case VERT_ATTRIB_WEIGHT:
-            /* fall-through */
-         case VERT_ATTRIB_NORMAL:
-            /* just label as a generic */
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
-            vs_input_semantic_index[slot] = 0;
-            break;
-         case VERT_ATTRIB_COLOR0:
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
-            vs_input_semantic_index[slot] = 0;
-            break;
-         case VERT_ATTRIB_COLOR1:
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_COLOR;
-            vs_input_semantic_index[slot] = 1;
-            break;
-         case VERT_ATTRIB_FOG:
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_FOG;
-            vs_input_semantic_index[slot] = 0;
-            break;
-         case VERT_ATTRIB_POINT_SIZE:
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_PSIZE;
-            vs_input_semantic_index[slot] = 0;
-            break;
-         case VERT_ATTRIB_TEX0:
-         case VERT_ATTRIB_TEX1:
-         case VERT_ATTRIB_TEX2:
-         case VERT_ATTRIB_TEX3:
-         case VERT_ATTRIB_TEX4:
-         case VERT_ATTRIB_TEX5:
-         case VERT_ATTRIB_TEX6:
-         case VERT_ATTRIB_TEX7:
-            assert(slot < Elements(vs_input_semantic_name));
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
-            vs_input_semantic_index[slot] = num_generic++;
-            break;
-         case VERT_ATTRIB_GENERIC0:
-         case VERT_ATTRIB_GENERIC1:
-         case VERT_ATTRIB_GENERIC2:
-         case VERT_ATTRIB_GENERIC3:
-         case VERT_ATTRIB_GENERIC4:
-         case VERT_ATTRIB_GENERIC5:
-         case VERT_ATTRIB_GENERIC6:
-         case VERT_ATTRIB_GENERIC7:
-         case VERT_ATTRIB_GENERIC8:
-         case VERT_ATTRIB_GENERIC9:
-         case VERT_ATTRIB_GENERIC10:
-         case VERT_ATTRIB_GENERIC11:
-         case VERT_ATTRIB_GENERIC12:
-         case VERT_ATTRIB_GENERIC13:
-         case VERT_ATTRIB_GENERIC14:
-         case VERT_ATTRIB_GENERIC15:
-            assert(attr < VERT_ATTRIB_MAX);
-            assert(slot < Elements(vs_input_semantic_name));
-            vs_input_semantic_name[slot] = TGSI_SEMANTIC_GENERIC;
-            vs_input_semantic_index[slot] = num_generic++;
-            break;
-         default:
-            assert(0);
-         }
-
-         input_flags[slot] = stvp->Base.Base.InputFlags[attr];
       }
    }
 
@@ -203,7 +110,6 @@ st_translate_vertex_program(struct st_context *st,
       assert(i < Elements(vs_output_semantic_name));
       vs_output_semantic_name[i] = TGSI_SEMANTIC_GENERIC;
       vs_output_semantic_index[i] = 0;
-      output_flags[i] = 0x0;
    }
 
    num_generic = 0;
@@ -289,9 +195,6 @@ st_translate_vertex_program(struct st_context *st,
                vs_output_semantic_index[slot] = num_generic++;
             }
          }
-
-         assert(slot < Elements(output_flags));
-         output_flags[slot] = stvp->Base.Base.OutputFlags[attr];
       }
    }
 
@@ -330,7 +233,7 @@ st_translate_vertex_program(struct st_context *st,
 
    /* free old shader state, if any */
    if (stvp->state.tokens) {
-      _mesa_free((void *) stvp->state.tokens);
+      st_free_tokens(stvp->state.tokens);
       stvp->state.tokens = NULL;
    }
    if (stvp->driver_shader) {
@@ -338,43 +241,34 @@ st_translate_vertex_program(struct st_context *st,
       stvp->driver_shader = NULL;
    }
 
-   /* XXX: fix static allocation of tokens:
-    */
-   num_tokens = st_translate_mesa_program(st->ctx,
-                                          TGSI_PROCESSOR_VERTEX,
-                                          &stvp->Base.Base,
-                                          /* inputs */
-                                          vs_num_inputs,
-                                          stvp->input_to_index,
-                                          vs_input_semantic_name,
-                                          vs_input_semantic_index,
-                                          NULL,
-                                          input_flags,
-                                          /* outputs */
-                                          vs_num_outputs,
-                                          outputMapping,
-                                          vs_output_semantic_name,
-                                          vs_output_semantic_index,
-                                          output_flags,
-                                          /* tokenized result */
-                                          tokens, ST_MAX_SHADER_TOKENS);
-
-   assert(num_tokens < ST_MAX_SHADER_TOKENS);
-
-   vs.tokens = (struct tgsi_token *)
-      _mesa_realloc(tokens,
-                    ST_MAX_SHADER_TOKENS * sizeof *tokens,
-                    num_tokens * sizeof *tokens);
+   stvp->state.tokens = 
+      st_translate_mesa_program(st->ctx,
+                                TGSI_PROCESSOR_VERTEX,
+                                &stvp->Base.Base,
+                                /* inputs */
+                                vs_num_inputs,
+                                stvp->input_to_index,
+                                NULL, /* input semantic name */
+                                NULL, /* input semantic index */
+                                NULL,
+                                /* outputs */
+                                vs_num_outputs,
+                                outputMapping,
+                                vs_output_semantic_name,
+                                vs_output_semantic_index );
 
    stvp->num_inputs = vs_num_inputs;
-   stvp->state = vs; /* struct copy */
-   stvp->driver_shader = pipe->create_vs_state(pipe, &vs);
+   stvp->driver_shader = pipe->create_vs_state(pipe, &stvp->state);
 
-   if (0)
+   if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
       _mesa_print_program(&stvp->Base.Base);
+      debug_printf("\n");
+   }
 
-   if (TGSI_DEBUG)
-      tgsi_dump( vs.tokens, 0 );
+   if (ST_DEBUG & DEBUG_TGSI) {
+      tgsi_dump( stvp->state.tokens, 0 );
+      debug_printf("\n");
+   }
 }
 
 
@@ -383,7 +277,6 @@ st_translate_vertex_program(struct st_context *st,
  * Translate a Mesa fragment shader into a TGSI shader.
  * \param inputMapping  to map fragment program input registers to TGSI
  *                      input slots
- * \param tokensOut  destination for TGSI tokens
  * \return  pointer to cached pipe_shader object.
  */
 void
@@ -392,16 +285,13 @@ st_translate_fragment_program(struct st_context *st,
                               const GLuint inputMapping[])
 {
    struct pipe_context *pipe = st->pipe;
-   struct tgsi_token *tokens;
    GLuint outputMapping[FRAG_RESULT_MAX];
    GLuint defaultInputMapping[FRAG_ATTRIB_MAX];
-   struct pipe_shader_state fs;
    GLuint interpMode[16];  /* XXX size? */
    GLuint attr;
    const GLbitfield inputsRead = stfp->Base.Base.InputsRead;
    GLuint vslot = 0;
    GLuint num_generic = 0;
-   GLuint num_tokens;
 
    uint fs_num_inputs = 0;
 
@@ -409,20 +299,6 @@ st_translate_fragment_program(struct st_context *st,
    ubyte fs_output_semantic_index[PIPE_MAX_SHADER_OUTPUTS];
    uint fs_num_outputs = 0;
 
-   GLbitfield input_flags[MAX_PROGRAM_INPUTS];
-   GLbitfield output_flags[MAX_PROGRAM_OUTPUTS];
-
-   tokens =  (struct tgsi_token *)MALLOC(ST_MAX_SHADER_TOKENS * sizeof *tokens);
-   if(!tokens) {
-      /* FIXME: propagate error to the caller */
-      assert(0);
-      return;
-   }
-
-   memset(&fs, 0, sizeof(fs));
-   memset(input_flags, 0, sizeof(input_flags));
-   memset(output_flags, 0, sizeof(output_flags));
-
    /* which vertex output goes to the first fragment input: */
    if (inputsRead & FRAG_BIT_WPOS)
       vslot = 0;
@@ -492,8 +368,6 @@ st_translate_fragment_program(struct st_context *st,
             stfp->input_semantic_index[slot] = num_generic++;
             interpMode[slot] = TGSI_INTERPOLATE_PERSPECTIVE;
          }
-
-         input_flags[slot] = stfp->Base.Base.InputFlags[attr];
       }
    }
 
@@ -531,8 +405,6 @@ st_translate_fragment_program(struct st_context *st,
                break;
             }
 
-            output_flags[fs_num_outputs] = stfp->Base.Base.OutputFlags[attr];
-
             fs_num_outputs++;
          }
       }
@@ -541,42 +413,33 @@ st_translate_fragment_program(struct st_context *st,
    if (!inputMapping)
       inputMapping = defaultInputMapping;
 
-   /* XXX: fix static allocation of tokens:
-    */
-   num_tokens = st_translate_mesa_program(st->ctx,
-                                          TGSI_PROCESSOR_FRAGMENT,
-                                          &stfp->Base.Base,
-                                          /* inputs */
-                                          fs_num_inputs,
-                                          inputMapping,
-                                          stfp->input_semantic_name,
-                                          stfp->input_semantic_index,
-                                          interpMode,
-                                          input_flags,
-                                          /* outputs */
-                                          fs_num_outputs,
-                                          outputMapping,
-                                          fs_output_semantic_name,
-                                          fs_output_semantic_index,
-                                          output_flags,
-                                          /* tokenized result */
-                                          tokens, ST_MAX_SHADER_TOKENS);
-
-   assert(num_tokens < ST_MAX_SHADER_TOKENS);
-
-   fs.tokens = (struct tgsi_token *)
-      _mesa_realloc(tokens,
-                    ST_MAX_SHADER_TOKENS * sizeof *tokens,
-                    num_tokens * sizeof *tokens);
-
-   stfp->state = fs; /* struct copy */
-   stfp->driver_shader = pipe->create_fs_state(pipe, &fs);
-
-   if (0)
+   stfp->state.tokens = 
+      st_translate_mesa_program(st->ctx,
+                                TGSI_PROCESSOR_FRAGMENT,
+                                &stfp->Base.Base,
+                                /* inputs */
+                                fs_num_inputs,
+                                inputMapping,
+                                stfp->input_semantic_name,
+                                stfp->input_semantic_index,
+                                interpMode,
+                                /* outputs */
+                                fs_num_outputs,
+                                outputMapping,
+                                fs_output_semantic_name,
+                                fs_output_semantic_index );
+
+   stfp->driver_shader = pipe->create_fs_state(pipe, &stfp->state);
+
+   if ((ST_DEBUG & DEBUG_TGSI) && (ST_DEBUG & DEBUG_MESA)) {
       _mesa_print_program(&stfp->Base.Base);
+      debug_printf("\n");
+   }
 
-   if (TGSI_DEBUG)
-      tgsi_dump( fs.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
+   if (ST_DEBUG & DEBUG_TGSI) {
+      tgsi_dump( stfp->state.tokens, 0/*TGSI_DUMP_VERBOSE*/ );
+      debug_printf("\n");
+   }
 }