Refactor code that converts between gl_vert_result and gl_frag_attrib.
authorPaul Berry <stereotype441@gmail.com>
Tue, 30 Aug 2011 18:46:29 +0000 (11:46 -0700)
committerPaul Berry <stereotype441@gmail.com>
Tue, 6 Sep 2011 18:02:32 +0000 (11:02 -0700)
Previously, this conversion was duplicated in several places in the
i965 driver.  This patch moves it to a common location in mtypes.h,
near the declaration of gl_vert_result and gl_frag_attrib.

I've also added comments to remind us that we may need to revisit the
conversion code when adding elements to gl_vert_result and
gl_frag_attrib.

Reviewed-by: Eric Anholt <eric@anholt.net>
src/mesa/drivers/dri/i965/brw_fs.cpp
src/mesa/drivers/dri/i965/brw_vs_constval.c
src/mesa/drivers/dri/i965/brw_wm_pass2.c
src/mesa/drivers/dri/i965/gen6_sf_state.c
src/mesa/main/mtypes.h

index 8b85f3b..9332373 100644 (file)
@@ -672,14 +672,7 @@ fs_visitor::calculate_urb_setup()
       /* FINISHME: The sf doesn't map VS->FS inputs for us very well. */
       for (unsigned int i = 0; i < VERT_RESULT_MAX; i++) {
         if (c->key.vp_outputs_written & BITFIELD64_BIT(i)) {
-           int fp_index;
-
-           if (i >= VERT_RESULT_VAR0)
-              fp_index = i - (VERT_RESULT_VAR0 - FRAG_ATTRIB_VAR0);
-           else if (i <= VERT_RESULT_TEX7)
-              fp_index = i;
-           else
-              fp_index = -1;
+           int fp_index = vert_result_to_frag_attrib(i);
 
            if (fp_index >= 0)
               urb_setup[fp_index] = urb_next++;
@@ -1828,17 +1821,12 @@ brw_fs_precompile(struct gl_context *ctx, struct gl_shader_program *prog)
 
    key.vp_outputs_written |= BITFIELD64_BIT(FRAG_ATTRIB_WPOS);
    for (int i = 0; i < FRAG_ATTRIB_MAX; i++) {
-      int vp_index = -1;
-
       if (!(fp->Base.InputsRead & BITFIELD64_BIT(i)))
         continue;
 
       key.proj_attrib_mask |= 1 << i;
 
-      if (i <= FRAG_ATTRIB_TEX7)
-        vp_index = i;
-      else if (i >= FRAG_ATTRIB_VAR0)
-        vp_index = i - FRAG_ATTRIB_VAR0 + VERT_RESULT_VAR0;
+      int vp_index = vert_result_to_frag_attrib(i);
 
       if (vp_index >= 0)
         key.vp_outputs_written |= BITFIELD64_BIT(vp_index);
index 47cc0a7..4d9d4b7 100644 (file)
@@ -144,14 +144,10 @@ static void calc_sizes( struct tracker *t )
     * which describes the fragment program input sizes.
     */
    for (vertRes = VERT_RESULT_TEX0; vertRes < VERT_RESULT_MAX; vertRes++) {
-      GLint fragAttrib;
 
       /* map vertex program output index to fragment program input index */
-      if (vertRes <= VERT_RESULT_TEX7)
-         fragAttrib = FRAG_ATTRIB_TEX0 + vertRes - VERT_RESULT_TEX0;
-      else if (vertRes >= VERT_RESULT_VAR0)
-         fragAttrib = FRAG_ATTRIB_VAR0 + vertRes - VERT_RESULT_VAR0;
-      else
+      GLint fragAttrib = vert_result_to_frag_attrib(vertRes);
+      if (fragAttrib < 0)
          continue;
       assert(fragAttrib >= FRAG_ATTRIB_TEX0);
       assert(fragAttrib <= FRAG_ATTRIB_MAX);
index 8c2b9e7..f1d70f7 100644 (file)
@@ -94,14 +94,7 @@ static void init_registers( struct brw_wm_compile *c )
    } else {
       for (j = 0; j < VERT_RESULT_MAX; j++) {
         if (c->key.vp_outputs_written & BITFIELD64_BIT(j)) {
-           int fp_index;
-
-           if (j >= VERT_RESULT_VAR0)
-              fp_index = j - (VERT_RESULT_VAR0 - FRAG_ATTRIB_VAR0);
-           else if (j <= VERT_RESULT_TEX7)
-              fp_index = j;
-           else
-              fp_index = -1;
+           int fp_index = vert_result_to_frag_attrib(j);
 
            nr_interp_regs++;
            if (fp_index >= 0)
index 5bb731d..714914a 100644 (file)
 uint32_t
 get_attr_override(struct brw_context *brw, int fs_attr, int two_side_color)
 {
-   int attr_index = 0, i, vs_attr;
+   int attr_index = 0, i;
    int bfc = 0;
+   int vs_attr = frag_attrib_to_vert_result(fs_attr);
 
-   if (fs_attr <= FRAG_ATTRIB_TEX7)
-      vs_attr = fs_attr;
-   else if (fs_attr == FRAG_ATTRIB_FACE)
-      vs_attr = 0; /* XXX */
-   else if (fs_attr == FRAG_ATTRIB_PNTC)
-      vs_attr = 0; /* XXX */
-   else {
-      assert(fs_attr >= FRAG_ATTRIB_VAR0);
-      vs_attr = fs_attr - FRAG_ATTRIB_VAR0 + VERT_RESULT_VAR0;
-   }
+   if (vs_attr < 0)
+      vs_attr = 0;
 
    /* Find the source index (0 = first attribute after the 4D position)
     * for this output attribute.  attr is currently a VERT_RESULT_* but should
index ad59797..bf96fdf 100644 (file)
@@ -215,7 +215,9 @@ typedef enum
 
 
 /**
- * Indexes for vertex program result attributes
+ * Indexes for vertex program result attributes.  Note that
+ * vert_result_to_frag_attrib() and frag_attrib_to_vert_result() make
+ * assumptions about the layout of this enum.
  */
 typedef enum
 {
@@ -313,7 +315,9 @@ typedef enum
 
 
 /**
- * Indexes for fragment program input attributes.
+ * Indexes for fragment program input attributes.  Note that
+ * vert_result_to_frag_attrib() and frag_attrib_to_vert_result() make
+ * assumptions about the layout of this enum.
  */
 typedef enum
 {
@@ -336,6 +340,43 @@ typedef enum
 } gl_frag_attrib;
 
 /**
+ * Convert from a gl_vert_result value to the corresponding gl_frag_attrib.
+ *
+ * VERT_RESULT_HPOS is converted to FRAG_ATTRIB_WPOS.
+ *
+ * gl_vert_result values which have no corresponding gl_frag_attrib
+ * (VERT_RESULT_PSIZ, VERT_RESULT_BFC0, VERT_RESULT_BFC1, and
+ * VERT_RESULT_EDGE) are converted to a value of -1.
+ */
+static inline int vert_result_to_frag_attrib(int vert_result)
+{
+   if (vert_result >= VERT_RESULT_VAR0)
+      return vert_result - VERT_RESULT_VAR0 + FRAG_ATTRIB_VAR0;
+   else if (vert_result <= VERT_RESULT_TEX7)
+      return vert_result;
+   else
+      return -1;
+}
+
+/**
+ * Convert from a gl_frag_attrib value to the corresponding gl_vert_result.
+ *
+ * FRAG_ATTRIB_WPOS is converted to VERT_RESULT_HPOS.
+ *
+ * gl_frag_attrib values which have no corresponding gl_vert_result
+ * (FRAG_ATTRIB_FACE and FRAG_ATTRIB_PNTC) are converted to a value of -1.
+ */
+static inline int frag_attrib_to_vert_result(int frag_attrib)
+{
+   if (frag_attrib <= FRAG_ATTRIB_TEX7)
+      return frag_attrib;
+   else if (frag_attrib >= FRAG_ATTRIB_VAR0)
+      return frag_attrib - FRAG_ATTRIB_VAR0 + VERT_RESULT_VAR0;
+   else
+      return -1;
+}
+
+/**
  * Bitflags for fragment program input attributes.
  */
 /*@{*/