[g3dvl] start to cleanup the mess
[mesa.git] / src / gallium / auxiliary / vl / vl_idct.c
index 99887277a3f5c22f1cb9e818159f53f28c9a2691..f191a898eddfa1eea486dd09710c1051d46f6a6f 100644 (file)
@@ -26,6 +26,7 @@
  **************************************************************************/
 
 #include "vl_idct.h"
+#include "vl_vertex_buffers.h"
 #include "util/u_draw.h"
 #include <assert.h>
 #include <pipe/p_context.h>
 
 #define SCALE_FACTOR_16_TO_9 (32768.0f / 256.0f)
 
-#define STAGE1_SCALE 4.0f
-#define STAGE2_SCALE (SCALE_FACTOR_16_TO_9 / STAGE1_SCALE)
-
-struct vertex_shader_consts
-{
-   struct vertex4f norm;
-};
-
-enum VS_INPUT
-{
-   VS_I_RECT,
-   VS_I_VPOS,
-
-   NUM_VS_INPUTS
-};
+#define NR_RENDER_TARGETS 4
 
 enum VS_OUTPUT
 {
    VS_O_VPOS,
-   VS_O_BLOCK,
-   VS_O_TEX,
-   VS_O_START
+   VS_O_L_ADDR0,
+   VS_O_L_ADDR1,
+   VS_O_R_ADDR0,
+   VS_O_R_ADDR1
 };
 
 static const float const_matrix[8][8] = {
@@ -76,200 +64,254 @@ static const float const_matrix[8][8] = {
    {  0.0975451f, -0.2777850f,  0.4157350f, -0.4903930f,  0.4903930f, -0.4157350f,  0.277786f, -0.0975458f }
 };
 
-/* vertices for a quad covering a block */
-static const struct vertex2f const_quad[4] = {
-   {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
-};
+static void
+calc_addr(struct ureg_program *shader, struct ureg_dst addr[2],
+          struct ureg_src tc, struct ureg_src start, bool right_side,
+          bool transposed, float size)
+{
+   unsigned wm_start = (right_side == transposed) ? TGSI_WRITEMASK_X : TGSI_WRITEMASK_Y;
+   unsigned sw_start = right_side ? TGSI_SWIZZLE_Y : TGSI_SWIZZLE_X;
+
+   unsigned wm_tc = (right_side == transposed) ? TGSI_WRITEMASK_Y : TGSI_WRITEMASK_X;
+   unsigned sw_tc = right_side ? TGSI_SWIZZLE_X : TGSI_SWIZZLE_Y;
+
+   /*
+    * addr[0..1].(start) = right_side ? start.x : tc.x
+    * addr[0..1].(tc) = right_side ? tc.y : start.y
+    * addr[0..1].z = tc.z
+    * addr[1].(start) += 1.0f / scale
+    */
+   ureg_MOV(shader, ureg_writemask(addr[0], wm_start), ureg_scalar(start, sw_start));
+   ureg_MOV(shader, ureg_writemask(addr[0], wm_tc), ureg_scalar(tc, sw_tc));
+   ureg_MOV(shader, ureg_writemask(addr[0], TGSI_WRITEMASK_Z), tc);
+
+   ureg_ADD(shader, ureg_writemask(addr[1], wm_start), ureg_scalar(start, sw_start), ureg_imm1f(shader, 1.0f / size));
+   ureg_MOV(shader, ureg_writemask(addr[1], wm_tc), ureg_scalar(tc, sw_tc));
+   ureg_MOV(shader, ureg_writemask(addr[1], TGSI_WRITEMASK_Z), tc);
+}
 
 static void *
-create_vert_shader(struct vl_idct *idct)
+create_vert_shader(struct vl_idct *idct, bool matrix_stage, int color_swizzle)
 {
    struct ureg_program *shader;
-   struct ureg_src scale;
-   struct ureg_src vrect, vpos;
-   struct ureg_dst t_vpos;
-   struct ureg_dst o_vpos, o_block, o_tex, o_start;
+   struct ureg_src vrect, vpos, vblock, eb[4];
+   struct ureg_src scale, blocks_xy, t_eb;
+   struct ureg_dst t_tex, t_start;
+   struct ureg_dst o_vpos, o_l_addr[2], o_r_addr[2];
+   unsigned label;
 
    shader = ureg_create(TGSI_PROCESSOR_VERTEX);
    if (!shader)
       return NULL;
 
-   scale = ureg_imm2f(shader,
-      (float)BLOCK_WIDTH / idct->destination->width0, 
-      (float)BLOCK_HEIGHT / idct->destination->height0);
-
-   t_vpos = ureg_DECL_temporary(shader);
+   t_tex = ureg_DECL_temporary(shader);
+   t_start = ureg_DECL_temporary(shader);
 
    vrect = ureg_DECL_vs_input(shader, VS_I_RECT);
    vpos = ureg_DECL_vs_input(shader, VS_I_VPOS);
+   vblock = ureg_swizzle(vrect, TGSI_SWIZZLE_Z, TGSI_SWIZZLE_W, TGSI_SWIZZLE_X, TGSI_SWIZZLE_X);
 
    o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS);
-   o_block = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK);
-   o_tex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX);
-   o_start = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_START);
+
+   eb[0] = ureg_DECL_vs_input(shader, VS_I_EB_0_0);
+   eb[1] = ureg_DECL_vs_input(shader, VS_I_EB_1_0);
+   eb[2] = ureg_DECL_vs_input(shader, VS_I_EB_0_1);
+   eb[3] = ureg_DECL_vs_input(shader, VS_I_EB_1_1);
+
+   o_l_addr[0] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR0);
+   o_l_addr[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR1);
+
+   o_r_addr[0] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR0);
+   o_r_addr[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR1);
 
    /*
-    * t_vpos = vpos + vrect
-    * o_vpos.xy = t_vpos * scale
-    * o_vpos.zw = vpos
+    * scale = (BLOCK_WIDTH, BLOCK_HEIGHT) / (dst.width, dst.height)
+    * blocks_xy = (blocks_x, blocks_y)
     *
-    * o_block = vrect
-    * o_tex = t_pos
-    * o_start = vpos * scale
+    * ar = vblock.y * blocks.x + vblock.x
+    * if eb[ar].(color_swizzle)
+    *    o_vpos.xy = -1
+    * else
+    *    t_tex = vpos * blocks_xy + vblock
+    *    t_start = t_tex * scale
+    *    t_tex = t_tex + vrect
+    *    o_vpos.xy = t_tex * scale
+    *
+    *    o_l_addr = calc_addr(...)
+    *    o_r_addr = calc_addr(...)
+    * endif
+    * o_vpos.zw = vpos
     *
     */
-   ureg_ADD(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), vpos, vrect);
-   ureg_MUL(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos), scale);
-   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos));
-   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos);
 
-   ureg_MOV(shader, ureg_writemask(o_block, TGSI_WRITEMASK_XY), vrect);
-   ureg_MOV(shader, ureg_writemask(o_tex, TGSI_WRITEMASK_XY), ureg_src(t_vpos));
-   ureg_MUL(shader, ureg_writemask(o_start, TGSI_WRITEMASK_XY), vpos, scale);
+   scale = ureg_imm2f(shader,
+      (float)BLOCK_WIDTH / idct->buffer_width,
+      (float)BLOCK_HEIGHT / idct->buffer_height);
 
-   ureg_release_temporary(shader, t_vpos);
+   blocks_xy = ureg_imm2f(shader, idct->blocks_x, idct->blocks_y);
 
-   ureg_END(shader);
+   if (idct->blocks_x > 1 || idct->blocks_y > 1) {
+      struct ureg_dst ar = ureg_DECL_address(shader);
 
-   return ureg_create_shader_and_destroy(shader, idct->pipe);
-}
+      ureg_MAD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_X),
+               ureg_scalar(vblock, TGSI_SWIZZLE_Y), blocks_xy, vblock);
 
-static void
-fetch_one(struct ureg_program *shader, struct ureg_dst m[2],
-          struct ureg_src tc, struct ureg_src sampler,
-          struct ureg_src start, bool right_side, float size)
-{
-   struct ureg_dst t_tc, tmp;
-   unsigned i, j;
+      ureg_ARL(shader, ureg_writemask(ar, TGSI_WRITEMASK_X), ureg_src(t_tex));
+      t_eb = ureg_src_indirect(eb[0], ureg_src(ar));
+   } else {
+      t_eb = eb[0];
+   }
 
-   t_tc = ureg_DECL_temporary(shader);
-   tmp = ureg_DECL_temporary(shader);
+   ureg_IF(shader, ureg_scalar(t_eb, color_swizzle), &label);
 
-   m[0] = ureg_DECL_temporary(shader);
-   m[1] = ureg_DECL_temporary(shader);
+      ureg_MOV(shader, o_vpos, ureg_imm1f(shader, -1.0f));
 
-   /*
-    * t_tc.x = right_side ? start.x : tc.x
-    * t_tc.y = right_side ? tc.y : start.y
-    * m[0..1].xyzw = tex(t_tc++, sampler)
-    */
-   if(right_side) {
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(tc, TGSI_SWIZZLE_X));
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(start, TGSI_SWIZZLE_Y));
-   } else {
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(start, TGSI_SWIZZLE_X));
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(tc, TGSI_SWIZZLE_Y));
-   }
-   for(i = 0; i < 2; ++i) {
-      for(j = 0; j < 4; ++j) {
-         /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */
-         ureg_TEX(shader, tmp, TGSI_TEXTURE_2D, ureg_src(t_tc), sampler);
-         ureg_MOV(shader, ureg_writemask(m[i], TGSI_WRITEMASK_X << j), ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_X));
-
-         if(i != 1 || j != 3) /* skip the last add */
-            ureg_ADD(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X << right_side),
-               ureg_src(t_tc), ureg_imm1f(shader, 1.0f / size));
+   ureg_fixup_label(shader, label, ureg_get_instruction_number(shader));
+   ureg_ELSE(shader, &label);
+
+      ureg_MAD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), vpos, blocks_xy, vblock);
+      ureg_MUL(shader, ureg_writemask(t_start, TGSI_WRITEMASK_XY), ureg_src(t_tex), scale);
+
+      ureg_ADD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), ureg_src(t_tex), vrect);
+
+      ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), ureg_src(t_tex), scale);
+      ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_Z),
+         ureg_scalar(vrect, TGSI_SWIZZLE_X),
+         ureg_imm1f(shader, BLOCK_WIDTH / NR_RENDER_TARGETS));
+
+      ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_tex));
+
+      if(matrix_stage) {
+         calc_addr(shader, o_l_addr, ureg_src(t_tex), ureg_src(t_start), false, false, idct->buffer_width / 4);
+         calc_addr(shader, o_r_addr, vrect, ureg_imm1f(shader, 0.0f), true, true, BLOCK_WIDTH / 4);
+      } else {
+         calc_addr(shader, o_l_addr, vrect, ureg_imm1f(shader, 0.0f), false, false, BLOCK_WIDTH / 4);
+         calc_addr(shader, o_r_addr, ureg_src(t_tex), ureg_src(t_start), true, false, idct->buffer_height / 4);
       }
-   }
 
-   ureg_release_temporary(shader, t_tc);
-   ureg_release_temporary(shader, tmp);
+   ureg_fixup_label(shader, label, ureg_get_instruction_number(shader));
+   ureg_ENDIF(shader);
+
+   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos);
+
+   ureg_release_temporary(shader, t_tex);
+   ureg_release_temporary(shader, t_start);
+
+   ureg_END(shader);
+
+   return ureg_create_shader_and_destroy(shader, idct->pipe);
 }
 
 static void
-fetch_four(struct ureg_program *shader, struct ureg_dst m[2],
-           struct ureg_src tc, struct ureg_src sampler,
-           struct ureg_src start, bool right_side, float size)
+increment_addr(struct ureg_program *shader, struct ureg_dst daddr[2],
+               struct ureg_src saddr[2], bool right_side, bool transposed,
+               int pos, float size)
 {
-   struct ureg_dst t_tc;
-
-   t_tc = ureg_DECL_temporary(shader);
-   m[0] = ureg_DECL_temporary(shader);
-   m[1] = ureg_DECL_temporary(shader);
+   unsigned wm_start = (right_side == transposed) ? TGSI_WRITEMASK_X : TGSI_WRITEMASK_Y;
+   unsigned wm_tc = (right_side == transposed) ? TGSI_WRITEMASK_Y : TGSI_WRITEMASK_X;
 
    /*
-    * t_tc.x = right_side ? start.x : tc.x
-    * t_tc.y = right_side ? tc.y : start.y
-    * m[0..1] = tex(t_tc++, sampler)
+    * daddr[0..1].(start) = saddr[0..1].(start)
+    * daddr[0..1].(tc) = saddr[0..1].(tc)
     */
-   if(right_side) {
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(start, TGSI_SWIZZLE_Y));
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(tc, TGSI_SWIZZLE_X));
-   } else {
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(start, TGSI_SWIZZLE_X));
-      ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(tc, TGSI_SWIZZLE_Y));
-   }
 
-   ureg_TEX(shader, m[0], TGSI_TEXTURE_2D, ureg_src(t_tc), sampler);
-   ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_imm1f(shader, 4.0f / size));
-   ureg_TEX(shader, m[1], TGSI_TEXTURE_2D, ureg_src(t_tc), sampler);
+   ureg_MOV(shader, ureg_writemask(daddr[0], wm_start), saddr[0]);
+   ureg_ADD(shader, ureg_writemask(daddr[0], wm_tc), saddr[0], ureg_imm1f(shader, pos / size));
+   ureg_MOV(shader, ureg_writemask(daddr[1], wm_start), saddr[1]);
+   ureg_ADD(shader, ureg_writemask(daddr[1], wm_tc), saddr[1], ureg_imm1f(shader, pos / size));
+}
 
-   ureg_release_temporary(shader, t_tc);
+static void
+fetch_four(struct ureg_program *shader, struct ureg_dst m[2], struct ureg_src addr[2], struct ureg_src sampler)
+{
+   ureg_TEX(shader, m[0], TGSI_TEXTURE_3D, addr[0], sampler);
+   ureg_TEX(shader, m[1], TGSI_TEXTURE_3D, addr[1], sampler);
 }
 
-static struct ureg_dst
-matrix_mul(struct ureg_program *shader, struct ureg_dst m[2][2])
+static void
+matrix_mul(struct ureg_program *shader, struct ureg_dst dst, struct ureg_dst l[2], struct ureg_dst r[2])
 {
-   struct ureg_dst dst, tmp[2];
-   unsigned i;
+   struct ureg_dst tmp;
 
-   dst = ureg_DECL_temporary(shader);
-   for(i = 0; i < 2; ++i) {
-      tmp[i] = ureg_DECL_temporary(shader);
-   }
+   tmp = ureg_DECL_temporary(shader);
 
    /*
-    * tmp[0..1] = dot4(m[0][0..1], m[1][0..1])
-    * dst = tmp[0] + tmp[1]
+    * tmp.xy = dot4(m[0][0..1], m[1][0..1])
+    * dst = tmp.x + tmp.y
     */
-   ureg_DP4(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(m[0][0]), ureg_src(m[1][0]));
-   ureg_DP4(shader, ureg_writemask(tmp[1], TGSI_WRITEMASK_X), ureg_src(m[0][1]), ureg_src(m[1][1]));
-   ureg_ADD(shader, ureg_writemask(dst, TGSI_WRITEMASK_X), ureg_src(tmp[0]), ureg_src(tmp[1]));
-
-   for(i = 0; i < 2; ++i) {
-      ureg_release_temporary(shader, tmp[i]);
-   }
+   ureg_DP4(shader, ureg_writemask(tmp, TGSI_WRITEMASK_X), ureg_src(l[0]), ureg_src(r[0]));
+   ureg_DP4(shader, ureg_writemask(tmp, TGSI_WRITEMASK_Y), ureg_src(l[1]), ureg_src(r[1]));
+   ureg_ADD(shader, dst,
+      ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_X),
+      ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_Y));
 
-   return dst;
+   ureg_release_temporary(shader, tmp);
 }
 
 static void *
-create_transpose_frag_shader(struct vl_idct *idct)
+create_matrix_frag_shader(struct vl_idct *idct)
 {
    struct ureg_program *shader;
 
-   struct ureg_src tc[2], sampler[2];
-   struct ureg_src start[2];
+   struct ureg_src l_addr[2], r_addr[2];
 
-   struct ureg_dst m[2][2];
-   struct ureg_dst tmp, fragment;
+   struct ureg_dst l[4][2], r[2];
+   struct ureg_dst fragment[NR_RENDER_TARGETS];
+
+   unsigned i, j;
 
    shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
    if (!shader)
       return NULL;
 
-   tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR);
-   tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR);
+   l_addr[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR0, TGSI_INTERPOLATE_LINEAR);
+   l_addr[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR1, TGSI_INTERPOLATE_LINEAR);
+
+   r_addr[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR0, TGSI_INTERPOLATE_LINEAR);
+   r_addr[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR1, TGSI_INTERPOLATE_LINEAR);
+
+   for (i = 0; i < NR_RENDER_TARGETS; ++i)
+       fragment[i] = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, i);
 
-   sampler[0] = ureg_DECL_sampler(shader, 0);
-   sampler[1] = ureg_DECL_sampler(shader, 1);
+   for (i = 0; i < 4; ++i) {
+      l[i][0] = ureg_DECL_temporary(shader);
+      l[i][1] = ureg_DECL_temporary(shader);
+   }
 
-   start[0] = ureg_imm1f(shader, 0.0f);
-   start[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT);
+   r[0] = ureg_DECL_temporary(shader);
+   r[1] = ureg_DECL_temporary(shader);
 
-   fetch_four(shader, m[0], tc[0], sampler[0], start[0], false, BLOCK_WIDTH);
-   fetch_one(shader, m[1], tc[1], sampler[1], start[1], true, idct->destination->height0);
+   for (i = 1; i < 4; ++i) {
+      increment_addr(shader, l[i], l_addr, false, false, i, idct->buffer_height);
+   }
 
-   fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
+   for (i = 0; i < 4; ++i) {
+      struct ureg_src s_addr[2];
+      s_addr[0] = i == 0 ? l_addr[0] : ureg_src(l[i][0]);
+      s_addr[1] = i == 0 ? l_addr[1] : ureg_src(l[i][1]);
+      fetch_four(shader, l[i], s_addr, ureg_DECL_sampler(shader, 1));
+   }
 
-   tmp = matrix_mul(shader, m);
-   ureg_MUL(shader, fragment, ureg_src(tmp), ureg_imm1f(shader, STAGE2_SCALE));
+   for (i = 0; i < NR_RENDER_TARGETS; ++i) {
+      if(i > 0)
+         increment_addr(shader, r, r_addr, true, true, i, BLOCK_HEIGHT);
 
-   ureg_release_temporary(shader, tmp);
-   ureg_release_temporary(shader, m[0][0]);
-   ureg_release_temporary(shader, m[0][1]);
-   ureg_release_temporary(shader, m[1][0]);
-   ureg_release_temporary(shader, m[1][1]);
+      struct ureg_src s_addr[2] = { ureg_src(r[0]), ureg_src(r[1]) };
+      s_addr[0] = i == 0 ? r_addr[0] : ureg_src(r[0]);
+      s_addr[1] = i == 0 ? r_addr[1] : ureg_src(r[1]);
+      fetch_four(shader, r, s_addr, ureg_DECL_sampler(shader, 0));
+
+      for (j = 0; j < 4; ++j) {
+         matrix_mul(shader, ureg_writemask(fragment[i], TGSI_WRITEMASK_X << j), l[j], r);
+      }
+   }
+
+   for (i = 0; i < 4; ++i) {
+      ureg_release_temporary(shader, l[i][0]);
+      ureg_release_temporary(shader, l[i][1]);
+   }
+   ureg_release_temporary(shader, r[0]);
+   ureg_release_temporary(shader, r[1]);
 
    ureg_END(shader);
 
@@ -277,305 +319,177 @@ create_transpose_frag_shader(struct vl_idct *idct)
 }
 
 static void *
-create_matrix_frag_shader(struct vl_idct *idct)
+create_transpose_frag_shader(struct vl_idct *idct)
 {
    struct ureg_program *shader;
 
-   struct ureg_src tc[2], sampler[2];
-   struct ureg_src start[2];
+   struct ureg_src l_addr[2], r_addr[2];
 
-   struct ureg_dst m[2][2];
-   struct ureg_dst tmp, fragment;
+   struct ureg_dst l[2], r[2];
+   struct ureg_dst fragment;
 
    shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
    if (!shader)
       return NULL;
 
-   tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR);
-   tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR);
+   l_addr[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR0, TGSI_INTERPOLATE_LINEAR);
+   l_addr[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR1, TGSI_INTERPOLATE_LINEAR);
 
-   sampler[0] = ureg_DECL_sampler(shader, 1);
-   sampler[1] = ureg_DECL_sampler(shader, 0);
+   r_addr[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR0, TGSI_INTERPOLATE_LINEAR);
+   r_addr[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR1, TGSI_INTERPOLATE_LINEAR);
 
-   start[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT);
-   start[1] = ureg_imm1f(shader, 0.0f);
+   l[0] = ureg_DECL_temporary(shader);
+   l[1] = ureg_DECL_temporary(shader);
+   r[0] = ureg_DECL_temporary(shader);
+   r[1] = ureg_DECL_temporary(shader);
 
-   fetch_four(shader, m[0], tc[0], sampler[0], start[0], false, idct->destination->width0);
-   fetch_four(shader, m[1], tc[1], sampler[1], start[1], true, BLOCK_HEIGHT);
+   fetch_four(shader, l, l_addr, ureg_DECL_sampler(shader, 0));
+   fetch_four(shader, r, r_addr, ureg_DECL_sampler(shader, 1));
 
    fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
 
-   tmp = matrix_mul(shader, m);
-   ureg_MUL(shader, fragment, ureg_src(tmp), ureg_imm1f(shader, STAGE1_SCALE));
+   matrix_mul(shader, ureg_writemask(fragment, TGSI_WRITEMASK_X), l, r);
 
-   ureg_release_temporary(shader, tmp);
-   ureg_release_temporary(shader, m[0][0]);
-   ureg_release_temporary(shader, m[0][1]);
-   ureg_release_temporary(shader, m[1][0]);
-   ureg_release_temporary(shader, m[1][1]);
+   ureg_release_temporary(shader, l[0]);
+   ureg_release_temporary(shader, l[1]);
+   ureg_release_temporary(shader, r[0]);
+   ureg_release_temporary(shader, r[1]);
 
    ureg_END(shader);
 
    return ureg_create_shader_and_destroy(shader, idct->pipe);
 }
 
-static void *
-create_empty_block_frag_shader(struct vl_idct *idct)
+static bool
+init_shaders(struct vl_idct *idct, int color_swizzle)
 {
-   struct ureg_program *shader;
-   struct ureg_dst fragment;
-
-   shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
-   if (!shader)
-      return NULL;
-
-   fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
-
-   ureg_MOV(shader, fragment, ureg_imm1f(shader, 0.0f));
+   idct->matrix_vs = create_vert_shader(idct, true, color_swizzle);
+   idct->matrix_fs = create_matrix_frag_shader(idct);
 
-   ureg_END(shader);
+   idct->transpose_vs = create_vert_shader(idct, false, color_swizzle);
+   idct->transpose_fs = create_transpose_frag_shader(idct);
 
-   return ureg_create_shader_and_destroy(shader, idct->pipe);
+   return
+      idct->matrix_vs != NULL &&
+      idct->matrix_fs != NULL &&
+      idct->transpose_vs != NULL &&
+      idct->transpose_fs != NULL;
 }
 
 static void
-xfer_buffers_map(struct vl_idct *idct)
+cleanup_shaders(struct vl_idct *idct)
 {
-   struct pipe_box rect =
-   {
-      0, 0, 0,
-      idct->destination->width0,
-      idct->destination->height0,
-      1
-   };
-
-   idct->tex_transfer = idct->pipe->get_transfer
-   (
-      idct->pipe, idct->textures.individual.source,
-      u_subresource(0, 0),
-      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
-      &rect
-   );
-
-   idct->texels = idct->pipe->transfer_map(idct->pipe, idct->tex_transfer);
-
-   idct->vectors = pipe_buffer_map
-   (
-      idct->pipe,
-      idct->vertex_bufs.individual.pos.buffer,
-      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
-      &idct->vec_transfer
-   );
+   idct->pipe->delete_vs_state(idct->pipe, idct->matrix_vs);
+   idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs);
+   idct->pipe->delete_vs_state(idct->pipe, idct->transpose_vs);
+   idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs);
 }
 
-static void
-xfer_buffers_unmap(struct vl_idct *idct)
+static bool
+init_state(struct vl_idct *idct)
 {
-   pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.pos.buffer, idct->vec_transfer);
+   struct pipe_sampler_state sampler;
+   struct pipe_rasterizer_state rs_state;
+   unsigned i;
 
-   idct->pipe->transfer_unmap(idct->pipe, idct->tex_transfer);
-   idct->pipe->transfer_destroy(idct->pipe, idct->tex_transfer);
-}
+   assert(idct);
 
-static bool
-init_shaders(struct vl_idct *idct)
-{
-   idct->vs = create_vert_shader(idct);
-   idct->transpose_fs = create_transpose_frag_shader(idct);
-   idct->matrix_fs = create_matrix_frag_shader(idct);
-   idct->eb_fs = create_empty_block_frag_shader(idct);
+   for (i = 0; i < 4; ++i) {
+      memset(&sampler, 0, sizeof(sampler));
+      sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
+      sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
+      sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
+      sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
+      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
+      sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
+      sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
+      sampler.compare_func = PIPE_FUNC_ALWAYS;
+      sampler.normalized_coords = 1;
+      /*sampler.shadow_ambient = ; */
+      /*sampler.lod_bias = ; */
+      sampler.min_lod = 0;
+      /*sampler.max_lod = ; */
+      /*sampler.border_color[0] = ; */
+      /*sampler.max_anisotropy = ; */
+      idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler);
+   }
 
-   return 
-      idct->vs != NULL &&
-      idct->transpose_fs != NULL &&
-      idct->matrix_fs != NULL &&
-      idct->eb_fs != NULL;
+   memset(&rs_state, 0, sizeof(rs_state));
+   /*rs_state.sprite_coord_enable */
+   rs_state.sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
+   rs_state.point_quad_rasterization = true;
+   rs_state.point_size = BLOCK_WIDTH;
+   rs_state.gl_rasterization_rules = false;
+   idct->rs_state = idct->pipe->create_rasterizer_state(idct->pipe, &rs_state);
+
+   return true;
 }
 
 static void
-cleanup_shaders(struct vl_idct *idct)
+cleanup_state(struct vl_idct *idct)
 {
-   idct->pipe->delete_vs_state(idct->pipe, idct->vs);
-   idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs);
-   idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs);
-   idct->pipe->delete_fs_state(idct->pipe, idct->eb_fs);
+   unsigned i;
+
+   for (i = 0; i < 4; ++i)
+      idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]);
+
+   idct->pipe->delete_rasterizer_state(idct->pipe, idct->rs_state);
 }
 
 static bool
-init_buffers(struct vl_idct *idct)
+init_textures(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 {
    struct pipe_resource template;
    struct pipe_sampler_view sampler_view;
-   struct pipe_vertex_element vertex_elems[2];
    unsigned i;
 
-   idct->max_blocks =
-      align(idct->destination->width0, BLOCK_WIDTH) / BLOCK_WIDTH *
-      align(idct->destination->height0, BLOCK_HEIGHT) / BLOCK_HEIGHT *
-      idct->destination->depth0;
+   assert(idct && buffer);
 
+   /* create textures */
    memset(&template, 0, sizeof(struct pipe_resource));
-   template.target = PIPE_TEXTURE_2D;
-   template.format = PIPE_FORMAT_R32G32B32A32_FLOAT;
    template.last_level = 0;
-   template.width0 = 2;
-   template.height0 = 8;
-   template.depth0 = 1;
-   template.usage = PIPE_USAGE_IMMUTABLE;
    template.bind = PIPE_BIND_SAMPLER_VIEW;
    template.flags = 0;
 
+   template.target = PIPE_TEXTURE_2D;
    template.format = PIPE_FORMAT_R16G16B16A16_SNORM;
-   template.width0 = idct->destination->width0;
-   template.height0 = idct->destination->height0;
-   template.depth0 = idct->destination->depth0;
+   template.width0 = idct->buffer_width / 4;
+   template.height0 = idct->buffer_height;
+   template.depth0 = 1;
+   template.array_size = 1;
    template.usage = PIPE_USAGE_STREAM;
-   idct->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
+   buffer->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
 
-   template.format = idct->destination->format;
+   template.target = PIPE_TEXTURE_3D;
+   template.format = PIPE_FORMAT_R16G16B16A16_SNORM;
+   template.width0 = idct->buffer_width / NR_RENDER_TARGETS;
+   template.height0 = idct->buffer_height / 4;
+   template.depth0 = NR_RENDER_TARGETS;
    template.usage = PIPE_USAGE_STATIC;
-   idct->textures.individual.intermediate = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
+   buffer->textures.individual.intermediate = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
 
    for (i = 0; i < 4; ++i) {
-      if(idct->textures.all[i] == NULL)
+      if(buffer->textures.all[i] == NULL)
          return false; /* a texture failed to allocate */
 
-      u_sampler_view_default_template(&sampler_view, idct->textures.all[i], idct->textures.all[i]->format);
-      idct->sampler_views.all[i] = idct->pipe->create_sampler_view(idct->pipe, idct->textures.all[i], &sampler_view);
+      u_sampler_view_default_template(&sampler_view, buffer->textures.all[i], buffer->textures.all[i]->format);
+      buffer->sampler_views.all[i] = idct->pipe->create_sampler_view(idct->pipe, buffer->textures.all[i], &sampler_view);
    }
 
-   idct->vertex_bufs.individual.quad.stride = sizeof(struct vertex2f);
-   idct->vertex_bufs.individual.quad.max_index = 4 * idct->max_blocks - 1;
-   idct->vertex_bufs.individual.quad.buffer_offset = 0;
-   idct->vertex_bufs.individual.quad.buffer = pipe_buffer_create
-   (
-      idct->pipe->screen,
-      PIPE_BIND_VERTEX_BUFFER,
-      sizeof(struct vertex2f) * 4 * idct->max_blocks
-   );
-
-   if(idct->vertex_bufs.individual.quad.buffer == NULL)
-      return false;
-
-   idct->vertex_bufs.individual.pos.stride = sizeof(struct vertex2f);
-   idct->vertex_bufs.individual.pos.max_index = 4 * idct->max_blocks - 1;
-   idct->vertex_bufs.individual.pos.buffer_offset = 0;
-   idct->vertex_bufs.individual.pos.buffer = pipe_buffer_create
-   (
-      idct->pipe->screen,
-      PIPE_BIND_VERTEX_BUFFER,
-      sizeof(struct vertex2f) * 4 * idct->max_blocks
-   );
-
-   if(idct->vertex_bufs.individual.pos.buffer == NULL)
-      return false;
-
-   /* Rect element */
-   vertex_elems[0].src_offset = 0;
-   vertex_elems[0].instance_divisor = 0;
-   vertex_elems[0].vertex_buffer_index = 0;
-   vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;
-
-   /* Pos element */
-   vertex_elems[1].src_offset = 0;
-   vertex_elems[1].instance_divisor = 0;
-   vertex_elems[1].vertex_buffer_index = 1;
-   vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT;
-
-   idct->vertex_elems_state = idct->pipe->create_vertex_elements_state(idct->pipe, 2, vertex_elems);
-
    return true;
 }
 
 static void
-cleanup_buffers(struct vl_idct *idct)
+cleanup_textures(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 {
    unsigned i;
 
-   assert(idct);
+   assert(idct && buffer);
 
    for (i = 0; i < 4; ++i) {
-      pipe_sampler_view_reference(&idct->sampler_views.all[i], NULL);
-      pipe_resource_reference(&idct->textures.all[i], NULL);
+      pipe_sampler_view_reference(&buffer->sampler_views.all[i], NULL);
+      pipe_resource_reference(&buffer->textures.all[i], NULL);
    }
-
-   idct->pipe->delete_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
-   pipe_resource_reference(&idct->vertex_bufs.individual.quad.buffer, NULL);
-   pipe_resource_reference(&idct->vertex_bufs.individual.pos.buffer, NULL);
-}
-
-static void
-init_constants(struct vl_idct *idct)
-{
-   struct pipe_transfer *buf_transfer;
-   struct vertex2f *v;
-
-   unsigned i;
-
-   /* quad vectors */
-   v = pipe_buffer_map
-   (
-      idct->pipe,
-      idct->vertex_bufs.individual.quad.buffer,
-      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
-      &buf_transfer
-   );
-   for ( i = 0; i < idct->max_blocks; ++i)
-     memcpy(v + i * 4, &const_quad, sizeof(const_quad));
-   pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.quad.buffer, buf_transfer);
-}
-
-static void
-init_state(struct vl_idct *idct)
-{
-   struct pipe_sampler_state sampler;
-   unsigned i;
-
-   idct->num_blocks = 0;
-   idct->num_empty_blocks = 0;
-
-   idct->viewport.scale[0] = idct->destination->width0;
-   idct->viewport.scale[1] = idct->destination->height0;
-   idct->viewport.scale[2] = 1;
-   idct->viewport.scale[3] = 1;
-   idct->viewport.translate[0] = 0;
-   idct->viewport.translate[1] = 0;
-   idct->viewport.translate[2] = 0;
-   idct->viewport.translate[3] = 0;
-
-   idct->fb_state.width = idct->destination->width0;
-   idct->fb_state.height = idct->destination->height0;
-   idct->fb_state.nr_cbufs = 1;
-   idct->fb_state.zsbuf = NULL;
-
-   for (i = 0; i < 4; ++i) {
-      memset(&sampler, 0, sizeof(sampler));
-      sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
-      sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
-      sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
-      sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
-      sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
-      sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
-      sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
-      sampler.compare_func = PIPE_FUNC_ALWAYS;
-      sampler.normalized_coords = 1;
-      /*sampler.shadow_ambient = ; */
-      /*sampler.lod_bias = ; */
-      sampler.min_lod = 0;
-      /*sampler.max_lod = ; */
-      /*sampler.border_color[0] = ; */
-      /*sampler.max_anisotropy = ; */
-      idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler);
-   }
-}
-
-static void
-cleanup_state(struct vl_idct *idct)
-{
-   unsigned i;
-
-   for (i = 0; i < 4; ++i)
-      idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]);
 }
 
 struct pipe_resource *
@@ -589,7 +503,7 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    struct pipe_box rect =
    {
       0, 0, 0,
-      BLOCK_WIDTH,
+      BLOCK_WIDTH / 4,
       BLOCK_HEIGHT,
       1
    };
@@ -601,6 +515,7 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    template.width0 = 2;
    template.height0 = 8;
    template.depth0 = 1;
+   template.array_size = 1;
    template.usage = PIPE_USAGE_IMMUTABLE;
    template.bind = PIPE_BIND_SAMPLER_VIEW;
    template.flags = 0;
@@ -611,16 +526,16 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    buf_transfer = pipe->get_transfer
    (
       pipe, matrix,
-      u_subresource(0, 0),
-      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
+      0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
       &rect
    );
-   pitch = buf_transfer->stride / util_format_get_blocksize(buf_transfer->resource->format);
+   pitch = buf_transfer->stride / sizeof(float);
 
    f = pipe->transfer_map(pipe, buf_transfer);
    for(i = 0; i < BLOCK_HEIGHT; ++i)
       for(j = 0; j < BLOCK_WIDTH; ++j)
-         f[i * pitch * 4 + j] = const_matrix[j][i]; // transpose
+         // transpose and scale
+         f[i * pitch + j] = const_matrix[j][i] * sqrtf(SCALE_FACTOR_16_TO_9);
 
    pipe->transfer_unmap(pipe, buf_transfer);
    pipe->transfer_destroy(pipe, buf_transfer);
@@ -628,150 +543,201 @@ vl_idct_upload_matrix(struct pipe_context *pipe)
    return matrix;
 }
 
-bool
-vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, struct pipe_resource *dst, struct pipe_resource *matrix)
+bool vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe,
+                  unsigned buffer_width, unsigned buffer_height,
+                  unsigned blocks_x, unsigned blocks_y,
+                  int color_swizzle, struct pipe_resource *matrix)
 {
-   assert(idct && pipe && dst);
+   assert(idct && pipe && matrix);
 
    idct->pipe = pipe;
-   pipe_resource_reference(&idct->textures.individual.matrix, matrix);
-   pipe_resource_reference(&idct->textures.individual.transpose, matrix);
-   pipe_resource_reference(&idct->destination, dst);
-
-   init_state(idct);
+   idct->buffer_width = buffer_width;
+   idct->buffer_height = buffer_height;
+   idct->blocks_x = blocks_x;
+   idct->blocks_y = blocks_y;
+   pipe_resource_reference(&idct->matrix, matrix);
 
-   if(!init_shaders(idct))
+   if(!init_shaders(idct, color_swizzle))
       return false;
 
-   if(!init_buffers(idct)) {
+   if(!init_state(idct)) {
       cleanup_shaders(idct);
       return false;
    }
 
-   idct->surfaces.intermediate = idct->pipe->screen->get_tex_surface(
-      idct->pipe->screen, idct->textures.individual.intermediate, 0, 0, 0,
-      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
-
-   idct->surfaces.destination = idct->pipe->screen->get_tex_surface(
-      idct->pipe->screen, idct->destination, 0, 0, 0,
-      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
-
-   init_constants(idct);
-   xfer_buffers_map(idct);
-
    return true;
 }
 
 void
 vl_idct_cleanup(struct vl_idct *idct)
 {
-   idct->pipe->screen->tex_surface_destroy(idct->surfaces.destination);
-   idct->pipe->screen->tex_surface_destroy(idct->surfaces.intermediate);
-
    cleanup_shaders(idct);
-   cleanup_buffers(idct);
-
    cleanup_state(idct);
 
-   pipe_resource_reference(&idct->destination, NULL);
+   pipe_resource_reference(&idct->matrix, NULL);
 }
 
-void
-vl_idct_add_block(struct vl_idct *idct, unsigned x, unsigned y, short *block)
+bool
+vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer,
+                    struct pipe_resource *dst)
 {
-   struct vertex2f v, *v_dst;
-
-   unsigned tex_pitch;
-   short *texels;
+   struct pipe_surface template;
 
    unsigned i;
 
+   assert(buffer);
    assert(idct);
+   assert(dst);
 
-   if(block) {
-      tex_pitch = idct->tex_transfer->stride / util_format_get_blocksize(idct->tex_transfer->resource->format);
-      texels = idct->texels + (y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH) * 4;
+   pipe_resource_reference(&buffer->textures.individual.matrix, idct->matrix);
+   pipe_resource_reference(&buffer->textures.individual.transpose, idct->matrix);
+   pipe_resource_reference(&buffer->destination, dst);
 
-      for (i = 0; i < BLOCK_HEIGHT; ++i)
-         memcpy(texels + i * tex_pitch * 4, block + i * BLOCK_WIDTH, BLOCK_WIDTH * 2);
+   if (!init_textures(idct, buffer))
+      return false;
 
-      /* non empty blocks fills the vector buffer from left to right */
-      v_dst = idct->vectors + idct->num_blocks * 4;
+   /* init state */
+   buffer->viewport[0].scale[0] = buffer->textures.individual.intermediate->width0;
+   buffer->viewport[0].scale[1] = buffer->textures.individual.intermediate->height0;
+
+   buffer->viewport[1].scale[0] = buffer->destination->width0;
+   buffer->viewport[1].scale[1] = buffer->destination->height0;
+
+   buffer->fb_state[0].width = buffer->textures.individual.intermediate->width0;
+   buffer->fb_state[0].height = buffer->textures.individual.intermediate->height0;
+
+   buffer->fb_state[0].nr_cbufs = NR_RENDER_TARGETS;
+   for(i = 0; i < NR_RENDER_TARGETS; ++i) {
+      memset(&template, 0, sizeof(template));
+      template.format = buffer->textures.individual.intermediate->format;
+      template.u.tex.first_layer = i;
+      template.u.tex.last_layer = i;
+      template.usage = PIPE_BIND_RENDER_TARGET;
+      buffer->fb_state[0].cbufs[i] = idct->pipe->create_surface(
+         idct->pipe, buffer->textures.individual.intermediate,
+         &template);
+   }
 
-      idct->num_blocks++;
+   buffer->fb_state[1].width = buffer->destination->width0;
+   buffer->fb_state[1].height = buffer->destination->height0;
 
-   } else {
+   buffer->fb_state[1].nr_cbufs = 1;
 
-      /* while empty blocks fills the vector buffer from right to left */
-      v_dst = idct->vectors + (idct->max_blocks - idct->num_empty_blocks) * 4 - 4;
+   memset(&template, 0, sizeof(template));
+   template.format = buffer->destination->format;
+   template.usage = PIPE_BIND_RENDER_TARGET;
+   buffer->fb_state[1].cbufs[0] = idct->pipe->create_surface(
+      idct->pipe, buffer->destination, &template);
 
-      idct->num_empty_blocks++;
+   for(i = 0; i < 2; ++i) {
+      buffer->viewport[i].scale[2] = 1;
+      buffer->viewport[i].scale[3] = 1;
+      buffer->viewport[i].translate[0] = 0;
+      buffer->viewport[i].translate[1] = 0;
+      buffer->viewport[i].translate[2] = 0;
+      buffer->viewport[i].translate[3] = 0;
+
+      buffer->fb_state[i].zsbuf = NULL;
    }
 
-   v.x = x;
-   v.y = y;
+   return true;
+}
 
-   for (i = 0; i < 4; ++i) {
-      v_dst[i] = v;
+void
+vl_idct_cleanup_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer)
+{
+   unsigned i;
+
+   assert(buffer);
+
+   for(i = 0; i < NR_RENDER_TARGETS; ++i) {
+      idct->pipe->surface_destroy(idct->pipe, buffer->fb_state[0].cbufs[i]);
    }
+
+   idct->pipe->surface_destroy(idct->pipe, buffer->fb_state[1].cbufs[0]);
+
+   cleanup_textures(idct, buffer);
 }
 
 void
-vl_idct_flush(struct vl_idct *idct)
+vl_idct_map_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 {
-   xfer_buffers_unmap(idct);
+   assert(idct);
 
-   if(idct->num_blocks > 0) {
+   struct pipe_box rect =
+   {
+      0, 0, 0,
+      buffer->textures.individual.source->width0,
+      buffer->textures.individual.source->height0,
+      1
+   };
 
-      /* first stage */
-      idct->fb_state.cbufs[0] = idct->surfaces.intermediate;
-      idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state);
-      idct->pipe->set_viewport_state(idct->pipe, &idct->viewport);
+   buffer->tex_transfer = idct->pipe->get_transfer
+   (
+      idct->pipe, buffer->textures.individual.source,
+      0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
+      &rect
+   );
 
-      idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all);
-      idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
-      idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[0]);
-      idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]);
-      idct->pipe->bind_vs_state(idct->pipe, idct->vs);
-      idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs);
+   buffer->texels = idct->pipe->transfer_map(idct->pipe, buffer->tex_transfer);
+}
 
-      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4);
+void
+vl_idct_add_block(struct vl_idct_buffer *buffer, unsigned x, unsigned y, short *block)
+{
+   //struct vertex2s v;
+   unsigned tex_pitch;
+   short *texels;
 
-      /* second stage */
-      idct->fb_state.cbufs[0] = idct->surfaces.destination;
-      idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state);
-      idct->pipe->set_viewport_state(idct->pipe, &idct->viewport);
+   unsigned i;
 
-      idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all);
-      idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
-      idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[1]);
-      idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]);
-      idct->pipe->bind_vs_state(idct->pipe, idct->vs);
-      idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs);
+   assert(buffer);
 
-      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4);
-   }
+   tex_pitch = buffer->tex_transfer->stride / sizeof(short);
+   texels = buffer->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH;
 
-   if(idct->num_empty_blocks > 0) {
+   for (i = 0; i < BLOCK_HEIGHT; ++i)
+      memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short));
+}
 
-      /* empty block handling */
-      idct->fb_state.cbufs[0] = idct->surfaces.destination;
-      idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state);
-      idct->pipe->set_viewport_state(idct->pipe, &idct->viewport);
+void
+vl_idct_unmap_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
+{
+   assert(idct && buffer);
 
-      idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all);
-      idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
-      idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all);
-      idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all);
-      idct->pipe->bind_vs_state(idct->pipe, idct->vs);
-      idct->pipe->bind_fs_state(idct->pipe, idct->eb_fs);
+   idct->pipe->transfer_unmap(idct->pipe, buffer->tex_transfer);
+   idct->pipe->transfer_destroy(idct->pipe, buffer->tex_transfer);
+}
 
-      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS,
-         (idct->max_blocks - idct->num_empty_blocks) * 4,
-         idct->num_empty_blocks * 4);
-   }
+void
+vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer, unsigned num_instances)
+{
+   unsigned num_verts;
+
+   assert(idct);
+   assert(buffer);
+
+   if(num_instances > 0) {
+      num_verts = idct->blocks_x * idct->blocks_y * 4;
 
-   idct->num_blocks = 0;
-   idct->num_empty_blocks = 0;
-   xfer_buffers_map(idct);
+      idct->pipe->bind_rasterizer_state(idct->pipe, idct->rs_state);
+
+      /* first stage */
+      idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state[0]);
+      idct->pipe->set_viewport_state(idct->pipe, &buffer->viewport[0]);
+      idct->pipe->set_fragment_sampler_views(idct->pipe, 2, buffer->sampler_views.stage[0]);
+      idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]);
+      idct->pipe->bind_vs_state(idct->pipe, idct->matrix_vs);
+      idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs);
+      util_draw_arrays_instanced(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts, 0, num_instances);
+
+      /* second stage */
+      idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state[1]);
+      idct->pipe->set_viewport_state(idct->pipe, &buffer->viewport[1]);
+      idct->pipe->set_fragment_sampler_views(idct->pipe, 2, buffer->sampler_views.stage[1]);
+      idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]);
+      idct->pipe->bind_vs_state(idct->pipe, idct->transpose_vs);
+      idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs);
+      util_draw_arrays_instanced(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts, 0, num_instances);
+   }
 }