Checkpoint: rework shader input/output register mapping.
authorBrian <brian.paul@tungstengraphics.com>
Wed, 19 Sep 2007 01:37:36 +0000 (19:37 -0600)
committerBrian <brian.paul@tungstengraphics.com>
Wed, 19 Sep 2007 01:38:35 +0000 (19:38 -0600)
This is a step toward removing TGSI_ATTRIB_ tokens.
Basically, when translating Mesa programs to TGSI programs, pass in input and
output register re-maps, plus interpolation info.
There's some known breakage (cubemap.c) so more to be done...

12 files changed:
src/mesa/pipe/draw/draw_vertex_fetch.c
src/mesa/pipe/draw/draw_vertex_shader.c
src/mesa/pipe/p_state.h
src/mesa/pipe/softpipe/sp_prim_setup.c
src/mesa/pipe/tgsi/mesa/mesa_to_tgsi.c
src/mesa/pipe/tgsi/mesa/mesa_to_tgsi.h
src/mesa/state_tracker/st_atom_fs.c
src/mesa/state_tracker/st_atom_vs.c
src/mesa/state_tracker/st_cb_clear.c
src/mesa/state_tracker/st_cb_drawpixels.c
src/mesa/state_tracker/st_draw.c
src/mesa/state_tracker/st_program.h

index 0dbbdf1..ce402d6 100644 (file)
@@ -67,6 +67,10 @@ fetch_attrib4(const void *ptr, unsigned format, float attrib[4])
    }
 }
 
+
+/**
+ * Fetch vertex attributes for 'count' vertices.
+ */
 void draw_vertex_fetch( struct draw_context *draw,
                        struct tgsi_exec_machine *machine,
                        const unsigned *elts,
@@ -74,27 +78,26 @@ void draw_vertex_fetch( struct draw_context *draw,
 {
    unsigned j;
 
-
-   /* load machine inputs */
+   /* loop over vertices */
    for (j = 0; j < count; j++) {
-      unsigned attr;
-      for (attr = 0; attr < 16; attr++) {
-         if (draw->vertex_shader.inputs_read & (1 << attr)) {
-            unsigned buf = draw->vertex_element[attr].vertex_buffer_index;
-            const void *src
-               = (const void *) ((const ubyte *) draw->mapped_vbuffer[buf]
-                                 + draw->vertex_buffer[buf].buffer_offset
-                                 + draw->vertex_element[attr].src_offset
-                                 + elts[j] * draw->vertex_buffer[buf].pitch);
-            float p[4];
+      uint attr;
+      /* loop over vertex attributes (vertex shader inputs) */
+      for (attr = 0; attr < draw->vertex_shader.num_inputs; attr++) {
+
+         unsigned buf = draw->vertex_element[attr].vertex_buffer_index;
+         const void *src
+            = (const void *) ((const ubyte *) draw->mapped_vbuffer[buf]
+                              + draw->vertex_buffer[buf].buffer_offset
+                              + draw->vertex_element[attr].src_offset
+                              + elts[j] * draw->vertex_buffer[buf].pitch);
+         float p[4];
 
-            fetch_attrib4(src, draw->vertex_element[attr].src_format, p);
+         fetch_attrib4(src, draw->vertex_element[attr].src_format, p);
 
-            machine->Inputs[attr].xyzw[0].f[j] = p[0]; /*X*/
-            machine->Inputs[attr].xyzw[1].f[j] = p[1]; /*Y*/
-            machine->Inputs[attr].xyzw[2].f[j] = p[2]; /*Z*/
-            machine->Inputs[attr].xyzw[3].f[j] = p[3]; /*W*/
-         }
+         machine->Inputs[attr].xyzw[0].f[j] = p[0]; /*X*/
+         machine->Inputs[attr].xyzw[1].f[j] = p[1]; /*Y*/
+         machine->Inputs[attr].xyzw[2].f[j] = p[2]; /*Z*/
+         machine->Inputs[attr].xyzw[3].f[j] = p[3]; /*W*/
       }
    }
 }
index 8effc74..cb6c605 100644 (file)
@@ -114,7 +114,6 @@ run_vertex_program(struct draw_context *draw,
 
    draw_vertex_fetch( draw, &machine, elts, count );
 
-
    /* run shader */
    if( draw->vertex_shader.executable != NULL ) {
 #if defined(USE_X86_ASM) || defined(SLANG_X86)
@@ -159,14 +158,23 @@ run_vertex_program(struct draw_context *draw,
       vOut[j]->data[0][2] = z * scale[2] + trans[2];
       vOut[j]->data[0][3] = w;
 
-      /* remaining attributes are packed into sequential post-transform
+      /* Remaining attributes are packed into sequential post-transform
        * vertex attrib slots.
+       * Skip 0 since we just did it above.
+       * Subtract two because of the VERTEX_HEADER, CLIP_POS attribs.
        */
-      for (slot = 1; slot < draw->vertex_info.num_attribs; slot++) {
+      for (slot = 1; slot < draw->vertex_info.num_attribs - 2; slot++) {
          vOut[j]->data[slot][0] = machine.Outputs[slot].xyzw[0].f[j];
          vOut[j]->data[slot][1] = machine.Outputs[slot].xyzw[1].f[j];
          vOut[j]->data[slot][2] = machine.Outputs[slot].xyzw[2].f[j];
          vOut[j]->data[slot][3] = machine.Outputs[slot].xyzw[3].f[j];
+         /*
+         printf("output %d: %f %f %f %f\n", slot,
+                vOut[j]->data[slot][0],
+                vOut[j]->data[slot][1],
+                vOut[j]->data[slot][2],
+                vOut[j]->data[slot][3]);
+         */
       }
    } /* loop over vertices */
 }
index 048feed..d2cc76a 100644 (file)
@@ -144,6 +144,9 @@ struct pipe_shader_state {
    unsigned outputs_written;               /**< TGSI_ATTRIB_ bits */
    const struct tgsi_token *tokens;
    void *executable;
+
+   uint num_inputs;
+   uint num_outputs;
 };
 
 struct pipe_depth_stencil_state
index d0baf07..8a4be79 100644 (file)
@@ -448,6 +448,14 @@ static void tri_persp_coeff( struct setup_stage *setup,
    float a = setup->ebot.dy * majda - botda * setup->emaj.dy;
    float b = setup->emaj.dx * botda - majda * setup->ebot.dx;
       
+   /*
+   printf("tri persp %d,%d: %f %f %f\n", slot, i,
+          setup->vmin->data[slot][i],
+          setup->vmid->data[slot][i],
+          setup->vmax->data[slot][i]
+          );
+   */
+
    assert(slot < TGSI_ATTRIB_MAX);
    assert(i <= 3);
 
index 2820706..1f8d937 100644 (file)
@@ -211,6 +211,7 @@ tgsi_mesa_translate_fragment_output(GLuint attrib)
 }\r
 \r
 \r
+#if 01\r
 uint\r
 tgsi_mesa_translate_vertex_input_mask(GLbitfield mask)\r
 {\r
@@ -224,7 +225,7 @@ tgsi_mesa_translate_vertex_input_mask(GLbitfield mask)
    }\r
    return tgsiMask;\r
 }\r
-\r
+#endif\r
 \r
 uint\r
 tgsi_mesa_translate_vertex_output_mask(GLbitfield mask)\r
@@ -318,7 +319,9 @@ map_register_file_index(
    GLuint processor,\r
    GLuint file,\r
    GLuint index,\r
-   GLbitfield usage_bitmask )\r
+   GLbitfield usage_bitmask,\r
+   const GLuint inputMapping[],\r
+   const GLuint outputMapping[])\r
 {\r
    GLuint mapped_index;\r
    GLuint i;\r
@@ -337,6 +340,12 @@ map_register_file_index(
        * etc.\r
        */\r
       assert( index < 32 );\r
+      if (inputMapping) {\r
+         printf("New map %d input %d to %d\n", processor, index,\r
+                inputMapping[index]);\r
+         return inputMapping[index];\r
+      }\r
+\r
       assert( usage_bitmask & (1 << index) );\r
       mapped_index = 0;\r
       for( i = 0; i < index; i++ ) {\r
@@ -344,7 +353,7 @@ map_register_file_index(
             mapped_index++;\r
          }\r
       }\r
-      printf("Map input %d to %d\n", index, mapped_index);\r
+      printf("Map %d input %d to %d\n", processor, index, mapped_index);\r
       break;\r
 \r
    case TGSI_FILE_OUTPUT:\r
@@ -372,6 +381,8 @@ map_register_file_index(
                mapped_index++;\r
             }\r
          }\r
+         printf("Map VP output from %d to %d\n", index, mapped_index);\r
+         assert(outputMapping[index] == mapped_index);\r
       }\r
       break;\r
 \r
@@ -443,6 +454,8 @@ compile_instruction(
    struct tgsi_full_instruction *fullinst,\r
    GLuint inputs_read,\r
    GLuint outputs_written,\r
+   const GLuint inputMapping[],\r
+   const GLuint outputMapping[],\r
    GLuint preamble_size,\r
    GLuint processor )\r
 {\r
@@ -462,7 +475,9 @@ compile_instruction(
       processor,\r
       fulldst->DstRegister.File,\r
       inst->DstReg.Index,\r
-      outputs_written\r
+      outputs_written,\r
+      NULL,\r
+      outputMapping\r
       );\r
    fulldst->DstRegister.WriteMask = convert_writemask( inst->DstReg.WriteMask );\r
 \r
@@ -475,7 +490,9 @@ compile_instruction(
          processor,\r
          fullsrc->SrcRegister.File,\r
          inst->SrcReg[i].Index,\r
-         inputs_read );\r
+         inputs_read,\r
+         inputMapping,\r
+         outputMapping );\r
 \r
       for( j = 0; j < 4; j++ ) {\r
          GLuint swz;\r
@@ -789,9 +806,20 @@ make_frag_output_decl(
    return decl;\r
 }\r
 \r
+\r
+/**\r
+ * Convert Mesa fragment program to TGSI format.\r
+ * \param inputMapping  array to map original Mesa fragment program inputs\r
+ *                      registers to TGSI generic input indexes\r
+ * \param interpMode  array[FRAG_ATTRIB_x] of TGSI_INTERPOLATE_LINEAR/PERSP.\r
+ *\r
+ */\r
 GLboolean\r
 tgsi_mesa_compile_fp_program(\r
    const struct gl_fragment_program *program,\r
+   const GLuint inputMapping[],\r
+   const GLuint interpMode[],\r
+   const GLuint outputMapping[],\r
    struct tgsi_token *tokens,\r
    GLuint maxTokens )\r
 {\r
@@ -800,8 +828,10 @@ tgsi_mesa_compile_fp_program(
    struct tgsi_processor *processor;\r
    struct tgsi_full_declaration fulldecl;\r
    struct tgsi_full_instruction fullinst;\r
+   /*\r
    struct tgsi_full_dst_register *fulldst;\r
    struct tgsi_full_src_register *fullsrc;\r
+   */\r
    GLuint inputs_read;\r
    GLboolean reads_wpos;\r
    GLuint preamble_size = 0;\r
@@ -822,7 +852,7 @@ tgsi_mesa_compile_fp_program(
    /*\r
     * Declare input attributes. Note that we do not interpolate fragment position.\r
     */\r
-\r
+   reads_wpos = 1;\r
    /* Fragment position. */\r
    if( reads_wpos ) {\r
       fulldecl = make_frag_input_decl(\r
@@ -853,20 +883,16 @@ tgsi_mesa_compile_fp_program(
    for( i = 1; i < 32; i++ ) {\r
       if( inputs_read & (1 << i) ) {\r
          count++;\r
+         fulldecl = make_frag_input_decl(count,\r
+                                         count,\r
+                                         interpMode[i],\r
+                                         TGSI_WRITEMASK_XYZW );\r
+         ti += tgsi_build_full_declaration(&fulldecl,\r
+                                           &tokens[ti],\r
+                                           header,\r
+                                           maxTokens - ti );\r
       }\r
-   }\r
-   if( count > 0 ) {\r
-      fulldecl = make_frag_input_decl(\r
-         1,\r
-         1 + count - 1,\r
-         TGSI_INTERPOLATE_PERSPECTIVE,\r
-         TGSI_WRITEMASK_XYZW );\r
-      ti += tgsi_build_full_declaration(\r
-         &fulldecl,\r
-         &tokens[ti],\r
-         header,\r
-         maxTokens - ti );\r
-   }\r
+   }         \r
 \r
    /*\r
     * Declare output attributes.\r
@@ -932,6 +958,8 @@ tgsi_mesa_compile_fp_program(
             &fullinst,\r
             inputs_read,\r
             ~0, /*outputs_written*/\r
+            inputMapping,\r
+            outputMapping,\r
             preamble_size,\r
             TGSI_PROCESSOR_FRAGMENT ) ) {\r
          assert( i == program->Base.NumInstructions - 1 );\r
@@ -955,8 +983,10 @@ tgsi_mesa_compile_fp_program(
 GLboolean\r
 tgsi_mesa_compile_vp_program(\r
    const struct gl_vertex_program *program,\r
+   const GLuint inputMapping[],\r
+   const GLuint outputMapping[],\r
    struct tgsi_token *tokens,\r
-   GLuint maxTokens )\r
+   GLuint maxTokens)\r
 {\r
    GLuint i, ti;\r
    struct tgsi_header *header;\r
@@ -983,6 +1013,8 @@ tgsi_mesa_compile_vp_program(
             &fullinst,\r
             inputs_read,\r
             outputs_written,\r
+            inputMapping,\r
+            outputMapping,\r
             0,\r
             TGSI_PROCESSOR_VERTEX ) ) {\r
          assert( i == program->Base.NumInstructions - 1 );\r
index fda3fa3..017cfce 100644 (file)
@@ -10,12 +10,17 @@ struct tgsi_token;
 GLboolean\r
 tgsi_mesa_compile_fp_program(\r
    const struct gl_fragment_program *program,\r
+   const GLuint inputMapping[],\r
+   const GLuint interpMode[],\r
+   const GLuint outputMapping[],\r
    struct tgsi_token *tokens,\r
    GLuint maxTokens );\r
 \r
 GLboolean\r
 tgsi_mesa_compile_vp_program(\r
    const struct gl_vertex_program *program,\r
+   const GLuint inputMapping[],\r
+   const GLuint outputMapping[],\r
    struct tgsi_token *tokens,\r
    GLuint maxTokens );\r
 \r
index dc3e525..3df2c67 100644 (file)
 #include "st_atom.h"
 #include "st_program.h"
 
-#define TGSI_DEBUG 0
+#define TGSI_DEBUG 1
 
 static void compile_fs( struct st_context *st )
 {
+   /* Map FRAG_RESULT_COLR to output 1, map FRAG_RESULT_DEPR to output 0 */
+   static const GLuint outputMapping[2] = {1, 0};
    struct st_fragment_program *fp = st->fp;
    struct pipe_shader_state fs;
    struct pipe_shader_state *cached;
+   GLuint interpMode[16];  /* XXX size? */
+   GLuint i;
+
+   for (i = 0; i < 16; i++) {
+      if (fp->Base.Base.InputsRead & (1 << i)) {
+         if (i == FRAG_ATTRIB_COL0 || i == FRAG_ATTRIB_COL1) {
+            interpMode[i] = TGSI_INTERPOLATE_LINEAR;
+         }
+         else {
+            interpMode[i] = TGSI_INTERPOLATE_PERSPECTIVE;
+         }
+      }
+   }
 
    /* XXX: fix static allocation of tokens:
     */
-   tgsi_mesa_compile_fp_program( &fp->Base, fp->tokens, ST_FP_MAX_TOKENS );
+   tgsi_mesa_compile_fp_program( &fp->Base, NULL, interpMode,
+                                 outputMapping,
+                                 fp->tokens, ST_FP_MAX_TOKENS );
 
    memset(&fs, 0, sizeof(fs));
    fs.inputs_read
@@ -64,7 +81,7 @@ static void compile_fs( struct st_context *st )
    fp->fsx = cached;
 
    if (TGSI_DEBUG)
-      tgsi_dump( fp->tokens, TGSI_DUMP_VERBOSE );
+      tgsi_dump( fp->tokens, 0/*TGSI_DUMP_VERBOSE*/ );
 
    fp->dirty = 0;
 }
index 8f1df80..8de19e4 100644 (file)
 
 
 
-/* translate shader to TGSI format 
-*/
+/**
+ * Translate Mesa shader to TGSI format 
+ */
 static void compile_vs( struct st_context *st )
 {
    struct st_vertex_program *vp = st->vp;
    struct pipe_shader_state vs;
    struct pipe_shader_state *cached;
+   GLuint i;
+
+   memset(&vs, 0, sizeof(vs));
+
+   /*
+    * Determine how many inputs there are.
+    * Also, compute two look-up tables that map between Mesa VERT_ATTRIB_x
+    * values and TGSI generic input indexes.
+    */
+   for (i = 0; i < MAX_VERTEX_PROGRAM_ATTRIBS; i++) {
+      if (vp->Base.Base.InputsRead & (1 << i)) {
+         vp->input_to_index[i] = vs.num_inputs;
+         vp->index_to_input[vs.num_inputs] = i;
+         vs.num_inputs++;
+      }
+   }
+
+   /*
+    * Determine output register mapping.
+    */
+   for (i = 0; i < VERT_RESULT_MAX; i++) {
+      if (vp->Base.Base.OutputsWritten & (1 << i)) {
+         vp->output_to_index[i] = vs.num_outputs;
+         vp->index_to_output[vs.num_outputs] = i;
+         vs.num_outputs++;
+      }
+   }
+
+
    /* XXX: fix static allocation of tokens:
     */
-   tgsi_mesa_compile_vp_program( &vp->Base, vp->tokens, ST_FP_MAX_TOKENS );
+   tgsi_mesa_compile_vp_program( &vp->Base,
+                                 vp->input_to_index,
+                                 vp->output_to_index,
+                                 vp->tokens, ST_FP_MAX_TOKENS );
 
-   memset(&vs, 0, sizeof(vs));
+#if 01
    vs.inputs_read
       = tgsi_mesa_translate_vertex_input_mask(vp->Base.Base.InputsRead);
+#endif
    vs.outputs_written
       = tgsi_mesa_translate_vertex_output_mask(vp->Base.Base.OutputsWritten);
+
    vs.tokens = &vp->tokens[0];
 
    cached = st_cached_shader_state(st, &vs);
+
    vp->vs = cached;
 
    if (TGSI_DEBUG)
@@ -121,7 +157,7 @@ static void update_vs( struct st_context *st )
 const struct st_tracked_state st_update_vs = {
    .name = "st_update_vs",
    .dirty = {
-      .mesa  = 0
+      .mesa  = _NEW_PROGRAM
       .st   = ST_NEW_VERTEX_PROGRAM,
    },
    .update = update_vs
index 2ea4986..65cac9d 100644 (file)
@@ -119,12 +119,19 @@ is_depth_stencil_format(GLuint pipeFormat)
  * Create a simple fragment shader that just passes through the fragment color.
  */
 static struct st_fragment_program *
-make_color_shader(struct st_context *st)
+make_frag_shader(struct st_context *st)
 {
+   static const GLuint outputMapping[] = { 1, 0 };
    GLcontext *ctx = st->ctx;
    struct st_fragment_program *stfp;
    struct gl_program *p;
    GLboolean b;
+   GLuint interpMode[16];
+   GLuint i;
+
+   /* XXX temporary */
+   for (i = 0; i < 16; i++)
+      interpMode[i] = TGSI_INTERPOLATE_LINEAR;
 
    p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
    if (!p)
@@ -151,7 +158,8 @@ make_color_shader(struct st_context *st)
 
    stfp = (struct st_fragment_program *) p;
    /* compile into tgsi format */
-   b = tgsi_mesa_compile_fp_program(&stfp->Base,
+   b = tgsi_mesa_compile_fp_program(&stfp->Base, NULL, interpMode,
+                                    outputMapping,
                                     stfp->tokens, ST_FP_MAX_TOKENS);
    assert(b);
 
@@ -166,6 +174,11 @@ make_color_shader(struct st_context *st)
 static struct st_vertex_program *
 make_vertex_shader(struct st_context *st)
 {
+   /* Map VERT_ATTRIB_POS to 0, VERT_ATTRIB_COLOR0 to 1 */
+   static const GLuint inputMapping[4] = { 0, 0, 0, 1 };
+   /* Map VERT_RESULT_HPOS to 0, VERT_RESULT_COL0 to 1 */
+   static const GLuint outputMapping[2] = { 0, 1 };
+
    GLcontext *ctx = st->ctx;
    struct st_vertex_program *stvp;
    struct gl_program *p;
@@ -204,6 +217,8 @@ make_vertex_shader(struct st_context *st)
    stvp = (struct st_vertex_program *) p;
    /* compile into tgsi format */
    b = tgsi_mesa_compile_vp_program(&stvp->Base,
+                                    inputMapping,
+                                    outputMapping,
                                     stvp->tokens, ST_FP_MAX_TOKENS);
    assert(b);
 
@@ -349,7 +364,7 @@ clear_with_quad(GLcontext *ctx,
       struct pipe_shader_state fs;
       const struct pipe_shader_state *cached;
       if (!stfp) {
-         stfp = make_color_shader(st);
+         stfp = make_frag_shader(st);
       }
       memset(&fs, 0, sizeof(fs));
       fs.inputs_read = tgsi_mesa_translate_fragment_input_mask(stfp->Base.Base.InputsRead);
index 37e4063..731c060 100644 (file)
 static struct st_fragment_program *
 make_fragment_shader(struct st_context *st)
 {
+   static const GLuint outputMapping[2] = { 1, 0 };
    GLcontext *ctx = st->ctx;
    struct st_fragment_program *stfp;
    struct gl_program *p;
    GLboolean b;
+   GLuint interpMode[16];
+   GLuint i;
+
+   /* XXX temporary */
+   for (i = 0; i < 16; i++)
+      interpMode[i] = TGSI_INTERPOLATE_LINEAR;
 
    p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
    if (!p)
@@ -88,7 +95,8 @@ make_fragment_shader(struct st_context *st)
 
    stfp = (struct st_fragment_program *) p;
    /* compile into tgsi format */
-   b = tgsi_mesa_compile_fp_program(&stfp->Base,
+   b = tgsi_mesa_compile_fp_program(&stfp->Base, NULL, interpMode,
+                                    outputMapping,
                                     stfp->tokens, ST_FP_MAX_TOKENS);
    assert(b);
 
@@ -103,6 +111,8 @@ make_fragment_shader(struct st_context *st)
 static struct st_vertex_program *
 make_vertex_shader(struct st_context *st)
 {
+   /* Map VERT_RESULT_HPOS to 0, VERT_RESULT_TEX0 to 1 */
+   static const GLuint outputMapping[] = { 0, 0, 0, 0, 1 };
    GLcontext *ctx = st->ctx;
    struct st_vertex_program *stvp;
    struct gl_program *p;
@@ -140,7 +150,8 @@ make_vertex_shader(struct st_context *st)
 
    stvp = (struct st_vertex_program *) p;
    /* compile into tgsi format */
-   b = tgsi_mesa_compile_vp_program(&stvp->Base,
+   b = tgsi_mesa_compile_vp_program(&stvp->Base, NULL,
+                                    outputMapping,
                                     stvp->tokens, ST_FP_MAX_TOKENS);
    assert(b);
 
index ac63a38..bff0ad7 100644 (file)
 
 #include "tnl/t_vp_build.h"
 
-#include "st_context.h"
 #include "st_atom.h"
-#include "st_draw.h"
+#include "st_context.h"
 #include "st_cb_bufferobjects.h"
+#include "st_draw.h"
+#include "st_program.h"
+
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_winsys.h"
@@ -185,18 +187,22 @@ st_draw_vbo(GLcontext *ctx,
             GLuint max_index)
 {
    struct pipe_context *pipe = ctx->st->pipe;
-   GLuint attr, i;
-   GLbitfield attrsNeeded;
+   const struct st_vertex_program *vp = ctx->st->vp;
+   const struct pipe_shader_state *vs;
    const unsigned attr0_offset = (unsigned) arrays[0]->Ptr;
+   GLboolean needDefaultAttribs = GL_FALSE;
+   GLuint attr;
 
    st_validate_state(ctx->st);
-   update_default_attribs_buffer(ctx);
 
-   /* this must be after state validation */
-   attrsNeeded = ctx->st->state.vs->inputs_read;
+   /* must do this after state validation! */
+   vs = ctx->st->state.vs;
+
+   /* loop over TGSI shader inputs */
+   for (attr = 0; attr < vs->num_inputs; attr++) {
+      const GLuint mesaAttr = vp->index_to_input[attr];
+      struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
 
-   /* tell pipe about the vertex array element/attributes */
-   for (attr = 0; attr < 16; attr++) {
       struct pipe_vertex_buffer vbuffer;
       struct pipe_vertex_element velement;
 
@@ -206,43 +212,40 @@ st_draw_vbo(GLcontext *ctx,
       velement.vertex_buffer_index = 0;
       velement.src_format = 0;
 
-      if (attrsNeeded & (1 << attr)) {
-         const GLuint mesaAttr = tgsi_attrib_to_mesa_attrib(attr);
-         struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
-
-         if (bufobj && bufobj->Name) {
-            struct st_buffer_object *stobj = st_buffer_object(bufobj);
-            /* Recall that for VBOs, the gl_client_array->Ptr field is
-             * really an offset from the start of the VBO, not a pointer.
-             */
-            unsigned offset = (unsigned) arrays[mesaAttr]->Ptr;
-
-            assert(stobj->buffer);
-
-            vbuffer.buffer = stobj->buffer;
-            vbuffer.buffer_offset = attr0_offset;  /* in bytes */
-            vbuffer.pitch = arrays[mesaAttr]->StrideB; /* in bytes */
-            vbuffer.max_index = 0;  /* need this? */
-
-            velement.src_offset = offset - attr0_offset; /* bytes */
-            velement.vertex_buffer_index = attr;
-            velement.dst_offset = 0; /* need this? */
-            velement.src_format = pipe_vertex_format(arrays[mesaAttr]->Type,
-                                                     arrays[mesaAttr]->Size);
-            assert(velement.src_format);
-         }
-         else {
-            /* use the default attribute buffer */
-            vbuffer.buffer = ctx->st->default_attrib_buffer;
-            vbuffer.buffer_offset = 0;
-            vbuffer.pitch = 0; /* must be zero! */
-            vbuffer.max_index = 1;
-
-            velement.src_offset = attr * 4 * sizeof(GLfloat);
-            velement.vertex_buffer_index = attr;
-            velement.dst_offset = 0;
-            velement.src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
-         }
+      if (bufobj && bufobj->Name) {
+         struct st_buffer_object *stobj = st_buffer_object(bufobj);
+         /* Recall that for VBOs, the gl_client_array->Ptr field is
+          * really an offset from the start of the VBO, not a pointer.
+          */
+         unsigned offset = (unsigned) arrays[mesaAttr]->Ptr;
+
+         assert(stobj->buffer);
+
+         vbuffer.buffer = stobj->buffer;
+         vbuffer.buffer_offset = attr0_offset;  /* in bytes */
+         vbuffer.pitch = arrays[mesaAttr]->StrideB; /* in bytes */
+         vbuffer.max_index = 0;  /* need this? */
+
+         velement.src_offset = offset - attr0_offset; /* bytes */
+         velement.vertex_buffer_index = attr;
+         velement.dst_offset = 0; /* need this? */
+         velement.src_format = pipe_vertex_format(arrays[mesaAttr]->Type,
+                                                  arrays[mesaAttr]->Size);
+         assert(velement.src_format);
+      }
+      else {
+         /* use the default attribute buffer */
+         needDefaultAttribs = GL_TRUE;
+
+         vbuffer.buffer = ctx->st->default_attrib_buffer;
+         vbuffer.buffer_offset = 0;
+         vbuffer.pitch = 0; /* must be zero! */
+         vbuffer.max_index = 1;
+
+         velement.src_offset = mesaAttr * 4 * sizeof(GLfloat);
+         velement.vertex_buffer_index = attr;
+         velement.dst_offset = 0;
+         velement.src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
       }
 
       if (attr == 0)
@@ -252,12 +255,17 @@ st_draw_vbo(GLcontext *ctx,
       pipe->set_vertex_element(pipe, attr, &velement);
    }
 
+   if (needDefaultAttribs) {
+      update_default_attribs_buffer(ctx);
+   }
+
+
    /* do actual drawing */
    if (ib) {
       /* indexed primitive */
       struct gl_buffer_object *bufobj = ib->obj;
       struct pipe_buffer_handle *bh = NULL;
-      unsigned indexSize;
+      unsigned indexSize, i;
 
       if (bufobj && bufobj->Name) {
          /* elements/indexes are in a real VBO */
@@ -285,6 +293,7 @@ st_draw_vbo(GLcontext *ctx,
    }
    else {
       /* non-indexed */
+      GLuint i;
       for (i = 0; i < nr_prims; i++) {
          pipe->draw_arrays(pipe, prims[i].mode, prims[i].start, prims[i].count);
       }
index a2f114b..68ceba4 100644 (file)
@@ -61,13 +61,20 @@ struct st_fragment_program
 
 struct st_vertex_program
 {
-   struct gl_vertex_program Base;
-   GLboolean error;             /* If program is malformed for any reason. */
+   struct gl_vertex_program Base;  /**< The Mesa vertex program */
+   GLboolean error;        /**< Set if program is malformed for any reason. */
 
-   GLuint    id;               /* String id, for tracking
-                                * ProgramStringNotify changes.
-                                */
+   GLuint    id; /**< String id, for tracking ProgramStringNotify changes. */
+
+   /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
+   GLuint input_to_index[MAX_VERTEX_PROGRAM_ATTRIBS];
+   /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
+   GLuint index_to_input[MAX_VERTEX_PROGRAM_ATTRIBS];
+
+   GLuint output_to_index[MAX_VERTEX_PROGRAM_ATTRIBS];
+   GLuint index_to_output[MAX_VERTEX_PROGRAM_ATTRIBS];
 
+   /** The program in TGSI format */
    struct tgsi_token tokens[ST_FP_MAX_TOKENS];
    GLboolean dirty;