gallium: new, unified pipe_context::set_sampler_views() function
[mesa.git] / src / gallium / auxiliary / vl / vl_idct.c
index 11f935afd73346954d61e99e0dacc24674217410..79adb045dad4ed6cf21dee3447c28c72e8a047b3 100644 (file)
 
 #include <assert.h>
 
-#include <pipe/p_context.h>
-#include <pipe/p_screen.h>
+#include "pipe/p_context.h"
+#include "pipe/p_screen.h"
 
-#include <util/u_draw.h>
-#include <util/u_sampler.h>
+#include "util/u_draw.h"
+#include "util/u_sampler.h"
+#include "util/u_memory.h"
 
-#include <tgsi/tgsi_ureg.h>
+#include "tgsi/tgsi_ureg.h"
 
 #include "vl_defines.h"
 #include "vl_types.h"
@@ -42,8 +43,8 @@
 
 enum VS_OUTPUT
 {
-   VS_O_VPOS,
-   VS_O_L_ADDR0,
+   VS_O_VPOS = 0,
+   VS_O_L_ADDR0 = 0,
    VS_O_L_ADDR1,
    VS_O_R_ADDR0,
    VS_O_R_ADDR1
@@ -86,11 +87,9 @@ calc_addr(struct ureg_program *shader, struct ureg_dst addr[2],
     */
    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
@@ -113,10 +112,11 @@ increment_addr(struct ureg_program *shader, struct ureg_dst daddr[2],
 }
 
 static void
-fetch_four(struct ureg_program *shader, struct ureg_dst m[2], struct ureg_src addr[2], struct ureg_src sampler)
+fetch_four(struct ureg_program *shader, struct ureg_dst m[2], struct ureg_src addr[2],
+           struct ureg_src sampler, bool resource3d)
 {
-   ureg_TEX(shader, m[0], TGSI_TEXTURE_3D, addr[0], sampler);
-   ureg_TEX(shader, m[1], TGSI_TEXTURE_3D, addr[1], sampler);
+   ureg_TEX(shader, m[0], resource3d ? TGSI_TEXTURE_3D : TGSI_TEXTURE_2D, addr[0], sampler);
+   ureg_TEX(shader, m[1], resource3d ? TGSI_TEXTURE_3D : TGSI_TEXTURE_2D, addr[1], sampler);
 }
 
 static void
@@ -139,6 +139,122 @@ matrix_mul(struct ureg_program *shader, struct ureg_dst dst, struct ureg_dst l[2
    ureg_release_temporary(shader, tmp);
 }
 
+static void *
+create_mismatch_vert_shader(struct vl_idct *idct)
+{
+   struct ureg_program *shader;
+   struct ureg_src vpos;
+   struct ureg_src scale;
+   struct ureg_dst t_tex;
+   struct ureg_dst o_vpos, o_addr[2];
+
+   shader = ureg_create(TGSI_PROCESSOR_VERTEX);
+   if (!shader)
+      return NULL;
+
+   vpos = ureg_DECL_vs_input(shader, VS_I_VPOS);
+
+   t_tex = ureg_DECL_temporary(shader);
+
+   o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS);
+
+   o_addr[0] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR0);
+   o_addr[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR1);
+
+   /*
+    * scale = (VL_BLOCK_WIDTH, VL_BLOCK_HEIGHT) / (dst.width, dst.height)
+    *
+    * t_vpos = vpos + 7 / VL_BLOCK_WIDTH
+    * o_vpos.xy = t_vpos * scale
+    *
+    * o_addr = calc_addr(...)
+    *
+    */
+
+   scale = ureg_imm2f(shader,
+      (float)VL_BLOCK_WIDTH / idct->buffer_width,
+      (float)VL_BLOCK_HEIGHT / idct->buffer_height);
+
+   ureg_MAD(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), vpos, scale, scale);
+   ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), ureg_imm1f(shader, 1.0f));
+
+   ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), vpos, scale);
+   calc_addr(shader, o_addr, ureg_src(t_tex), ureg_src(t_tex), false, false, idct->buffer_width / 4);
+
+   ureg_release_temporary(shader, t_tex);
+
+   ureg_END(shader);
+
+   return ureg_create_shader_and_destroy(shader, idct->pipe);
+}
+
+static void *
+create_mismatch_frag_shader(struct vl_idct *idct)
+{
+   struct ureg_program *shader;
+
+   struct ureg_src addr[2];
+
+   struct ureg_dst m[8][2];
+   struct ureg_dst fragment;
+
+   unsigned i;
+
+   shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
+   if (!shader)
+      return NULL;
+
+   addr[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR0, TGSI_INTERPOLATE_LINEAR);
+   addr[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_L_ADDR1, TGSI_INTERPOLATE_LINEAR);
+
+   fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
+
+   for (i = 0; i < 8; ++i) {
+      m[i][0] = ureg_DECL_temporary(shader);
+      m[i][1] = ureg_DECL_temporary(shader);
+   }
+
+   for (i = 0; i < 8; ++i) {
+      increment_addr(shader, m[i], addr, false, false, i, idct->buffer_height);
+   }
+
+   for (i = 0; i < 8; ++i) {
+      struct ureg_src s_addr[2];
+      s_addr[0] = ureg_src(m[i][0]);
+      s_addr[1] = ureg_src(m[i][1]);
+      fetch_four(shader, m[i], s_addr, ureg_DECL_sampler(shader, 0), false);
+   }
+
+   for (i = 1; i < 8; ++i) {
+      ureg_ADD(shader, m[0][0], ureg_src(m[0][0]), ureg_src(m[i][0]));
+      ureg_ADD(shader, m[0][1], ureg_src(m[0][1]), ureg_src(m[i][1]));
+   }
+
+   ureg_ADD(shader, m[0][0], ureg_src(m[0][0]), ureg_src(m[0][1]));
+   ureg_DP4(shader, m[0][0], ureg_abs(ureg_src(m[0][0])), ureg_imm1f(shader, 1 << 14));
+
+   ureg_MUL(shader, ureg_writemask(m[0][0], TGSI_WRITEMASK_W), ureg_abs(ureg_src(m[7][1])), ureg_imm1f(shader, 1 << 14));
+   ureg_FRC(shader, m[0][0], ureg_src(m[0][0]));
+   ureg_SGT(shader, m[0][0], ureg_imm1f(shader, 0.5f), ureg_abs(ureg_src(m[0][0])));
+
+   ureg_CMP(shader, ureg_writemask(m[0][0], TGSI_WRITEMASK_W), ureg_negate(ureg_src(m[0][0])),
+            ureg_imm1f(shader, 1.0f / (1 << 15)), ureg_imm1f(shader, -1.0f / (1 << 15)));
+   ureg_MUL(shader, ureg_writemask(m[0][0], TGSI_WRITEMASK_W), ureg_src(m[0][0]),
+            ureg_scalar(ureg_src(m[0][0]), TGSI_SWIZZLE_X));
+
+   ureg_MOV(shader, ureg_writemask(fragment, TGSI_WRITEMASK_XYZ), ureg_src(m[7][1]));
+   ureg_ADD(shader, ureg_writemask(fragment, TGSI_WRITEMASK_W), ureg_src(m[0][0]), ureg_src(m[7][1]));
+
+   for (i = 0; i < 8; ++i) {
+      ureg_release_temporary(shader, m[i][0]);
+      ureg_release_temporary(shader, m[i][1]);
+   }
+
+   ureg_END(shader);
+
+   return ureg_create_shader_and_destroy(shader, idct->pipe);
+}
+
 static void *
 create_stage1_vert_shader(struct vl_idct *idct)
 {
@@ -167,7 +283,7 @@ create_stage1_vert_shader(struct vl_idct *idct)
    o_r_addr[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_R_ADDR1);
 
    /*
-    * scale = (BLOCK_WIDTH, BLOCK_HEIGHT) / (dst.width, dst.height)
+    * scale = (VL_BLOCK_WIDTH, VL_BLOCK_HEIGHT) / (dst.width, dst.height)
     *
     * t_vpos = vpos + vrect
     * o_vpos.xy = t_vpos * scale
@@ -179,8 +295,8 @@ create_stage1_vert_shader(struct vl_idct *idct)
     */
 
    scale = ureg_imm2f(shader,
-      (float)BLOCK_WIDTH / idct->buffer_width,
-      (float)BLOCK_HEIGHT / idct->buffer_height);
+      (float)VL_BLOCK_WIDTH / idct->buffer_width,
+      (float)VL_BLOCK_HEIGHT / idct->buffer_height);
 
    ureg_ADD(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), vpos, vrect);
    ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_XY), ureg_src(t_tex), scale);
@@ -188,13 +304,10 @@ create_stage1_vert_shader(struct vl_idct *idct)
    ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_tex));
    ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), ureg_imm1f(shader, 1.0f));
 
-   ureg_MUL(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_Z),
-      ureg_scalar(vrect, TGSI_SWIZZLE_X),
-      ureg_imm1f(shader, BLOCK_WIDTH / idct->nr_of_render_targets));
    ureg_MUL(shader, ureg_writemask(t_start, TGSI_WRITEMASK_XY), vpos, scale);
 
    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);
+   calc_addr(shader, o_r_addr, vrect, ureg_imm1f(shader, 0.0f), true, true, VL_BLOCK_WIDTH / 4);
 
    ureg_release_temporary(shader, t_tex);
    ureg_release_temporary(shader, t_start);
@@ -212,7 +325,7 @@ create_stage1_frag_shader(struct vl_idct *idct)
    struct ureg_src l_addr[2], r_addr[2];
 
    struct ureg_dst l[4][2], r[2];
-   struct ureg_dst fragment[idct->nr_of_render_targets];
+   struct ureg_dst *fragment;
 
    int i, j;
 
@@ -220,6 +333,8 @@ create_stage1_frag_shader(struct vl_idct *idct)
    if (!shader)
       return NULL;
 
+   fragment = MALLOC(idct->nr_of_render_targets * sizeof(struct ureg_dst));
+
    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);
 
@@ -242,15 +357,20 @@ create_stage1_frag_shader(struct vl_idct *idct)
    }
 
    for (i = 0; i < 4; ++i) {
-      struct ureg_src s_addr[2] = { ureg_src(l[i][0]), ureg_src(l[i][1]) };
-      fetch_four(shader, l[i], s_addr, ureg_DECL_sampler(shader, 1));
+      struct ureg_src s_addr[2];
+      s_addr[0] = ureg_src(l[i][0]);
+      s_addr[1] = ureg_src(l[i][1]);
+      fetch_four(shader, l[i], s_addr, ureg_DECL_sampler(shader, 0), false);
    }
 
    for (i = 0; i < idct->nr_of_render_targets; ++i) {
-      increment_addr(shader, r, r_addr, true, true, i - (signed)idct->nr_of_render_targets / 2, BLOCK_HEIGHT);
+      struct ureg_src s_addr[2];
 
-      struct ureg_src s_addr[2] = { ureg_src(r[0]), ureg_src(r[1]) };
-      fetch_four(shader, r, s_addr, ureg_DECL_sampler(shader, 0));
+      increment_addr(shader, r, r_addr, true, true, i - (signed)idct->nr_of_render_targets / 2, VL_BLOCK_HEIGHT);
+
+      s_addr[0] = ureg_src(r[0]);
+      s_addr[1] = ureg_src(r[1]);
+      fetch_four(shader, r, s_addr, ureg_DECL_sampler(shader, 1), false);
 
       for (j = 0; j < 4; ++j) {
          matrix_mul(shader, ureg_writemask(fragment[i], TGSI_WRITEMASK_X << j), l[j], r);
@@ -266,6 +386,8 @@ create_stage1_frag_shader(struct vl_idct *idct)
 
    ureg_END(shader);
 
+   FREE(fragment);
+
    return ureg_create_shader_and_destroy(shader, idct->pipe);
 }
 
@@ -292,16 +414,19 @@ vl_idct_stage2_vert_shader(struct vl_idct *idct, struct ureg_program *shader,
    o_r_addr[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, first_output + VS_O_R_ADDR1);
 
    scale = ureg_imm2f(shader,
-      (float)BLOCK_WIDTH / idct->buffer_width,
-      (float)BLOCK_HEIGHT / idct->buffer_height);
+      (float)VL_BLOCK_WIDTH / idct->buffer_width,
+      (float)VL_BLOCK_HEIGHT / idct->buffer_height);
 
    ureg_MUL(shader, ureg_writemask(tex, TGSI_WRITEMASK_Z),
       ureg_scalar(vrect, TGSI_SWIZZLE_X),
-      ureg_imm1f(shader, BLOCK_WIDTH / idct->nr_of_render_targets));
+      ureg_imm1f(shader, VL_BLOCK_WIDTH / idct->nr_of_render_targets));
    ureg_MUL(shader, ureg_writemask(t_start, TGSI_WRITEMASK_XY), vpos, scale);
 
-   calc_addr(shader, o_l_addr, vrect, ureg_imm1f(shader, 0.0f), false, false, BLOCK_WIDTH / 4);
+   calc_addr(shader, o_l_addr, vrect, ureg_imm1f(shader, 0.0f), false, false, VL_BLOCK_WIDTH / 4);
    calc_addr(shader, o_r_addr, ureg_src(tex), ureg_src(t_start), true, false, idct->buffer_height / 4);
+
+   ureg_MOV(shader, ureg_writemask(o_r_addr[0], TGSI_WRITEMASK_Z), ureg_src(tex));
+   ureg_MOV(shader, ureg_writemask(o_r_addr[1], TGSI_WRITEMASK_Z), ureg_src(tex));
 }
 
 void
@@ -325,8 +450,8 @@ vl_idct_stage2_frag_shader(struct vl_idct *idct, struct ureg_program *shader,
    r[0] = ureg_DECL_temporary(shader);
    r[1] = ureg_DECL_temporary(shader);
 
-   fetch_four(shader, l, l_addr, ureg_DECL_sampler(shader, 0));
-   fetch_four(shader, r, r_addr, ureg_DECL_sampler(shader, 1));
+   fetch_four(shader, l, l_addr, ureg_DECL_sampler(shader, 1), false);
+   fetch_four(shader, r, r_addr, ureg_DECL_sampler(shader, 0), true);
 
    matrix_mul(shader, fragment, l, r);
 
@@ -339,6 +464,14 @@ vl_idct_stage2_frag_shader(struct vl_idct *idct, struct ureg_program *shader,
 static bool
 init_shaders(struct vl_idct *idct)
 {
+   idct->vs_mismatch = create_mismatch_vert_shader(idct);
+   if (!idct->vs_mismatch)
+      goto error_vs_mismatch;
+
+   idct->fs_mismatch = create_mismatch_frag_shader(idct);
+   if (!idct->fs_mismatch)
+      goto error_fs_mismatch;
+
    idct->vs = create_stage1_vert_shader(idct);
    if (!idct->vs)
       goto error_vs;
@@ -353,12 +486,20 @@ error_fs:
    idct->pipe->delete_vs_state(idct->pipe, idct->vs);
 
 error_vs:
+   idct->pipe->delete_vs_state(idct->pipe, idct->vs_mismatch);
+
+error_fs_mismatch:
+   idct->pipe->delete_vs_state(idct->pipe, idct->fs);
+
+error_vs_mismatch:
    return false;
 }
 
 static void
 cleanup_shaders(struct vl_idct *idct)
 {
+   idct->pipe->delete_vs_state(idct->pipe, idct->vs_mismatch);
+   idct->pipe->delete_fs_state(idct->pipe, idct->fs_mismatch);
    idct->pipe->delete_vs_state(idct->pipe, idct->vs);
    idct->pipe->delete_fs_state(idct->pipe, idct->fs);
 }
@@ -374,7 +515,10 @@ init_state(struct vl_idct *idct)
    assert(idct);
 
    memset(&rs_state, 0, sizeof(rs_state));
-   rs_state.gl_rasterization_rules = true;
+   rs_state.point_size = 1;
+   rs_state.half_pixel_center = true;
+   rs_state.bottom_edge_rule = true;
+   rs_state.depth_clip = 1;
    idct->rs_state = idct->pipe->create_rasterizer_state(idct->pipe, &rs_state);
    if (!idct->rs_state)
       goto error_rs_state;
@@ -442,6 +586,44 @@ cleanup_state(struct vl_idct *idct)
    idct->pipe->delete_blend_state(idct->pipe, idct->blend);
 }
 
+static bool
+init_source(struct vl_idct *idct, struct vl_idct_buffer *buffer)
+{
+   struct pipe_resource *tex;
+   struct pipe_surface surf_templ;
+
+   assert(idct && buffer);
+
+   tex = buffer->sampler_views.individual.source->texture;
+
+   buffer->fb_state_mismatch.width = tex->width0;
+   buffer->fb_state_mismatch.height = tex->height0;
+   buffer->fb_state_mismatch.nr_cbufs = 1;
+
+   memset(&surf_templ, 0, sizeof(surf_templ));
+   surf_templ.format = tex->format;
+   surf_templ.u.tex.first_layer = 0;
+   surf_templ.u.tex.last_layer = 0;
+   buffer->fb_state_mismatch.cbufs[0] = idct->pipe->create_surface(idct->pipe, tex, &surf_templ);
+
+   buffer->viewport_mismatch.scale[0] = tex->width0;
+   buffer->viewport_mismatch.scale[1] = tex->height0;
+   buffer->viewport_mismatch.scale[2] = 1;
+   buffer->viewport_mismatch.scale[3] = 1;
+
+   return true;
+}
+
+static void
+cleanup_source(struct vl_idct_buffer *buffer)
+{
+   assert(buffer);
+
+   pipe_surface_reference(&buffer->fb_state_mismatch.cbufs[0], NULL);
+
+   pipe_sampler_view_reference(&buffer->sampler_views.individual.source, NULL);
+}
+
 static bool
 init_intermediate(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 {
@@ -461,7 +643,6 @@ init_intermediate(struct vl_idct *idct, struct vl_idct_buffer *buffer)
       surf_templ.format = tex->format;
       surf_templ.u.tex.first_layer = i;
       surf_templ.u.tex.last_layer = i;
-      surf_templ.usage = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
       buffer->fb_state.cbufs[i] = idct->pipe->create_surface(
          idct->pipe, tex, &surf_templ);
 
@@ -471,6 +652,8 @@ init_intermediate(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 
    buffer->viewport.scale[0] = tex->width0;
    buffer->viewport.scale[1] = tex->height0;
+   buffer->viewport.scale[2] = 1;
+   buffer->viewport.scale[3] = 1;
 
    return true;
 
@@ -482,13 +665,13 @@ error_surfaces:
 }
 
 static void
-cleanup_intermediate(struct vl_idct *idct, struct vl_idct_buffer *buffer)
+cleanup_intermediate(struct vl_idct_buffer *buffer)
 {
    unsigned i;
 
-   assert(idct && buffer);
+   assert(buffer);
 
-   for(i = 0; i < idct->nr_of_render_targets; ++i)
+   for(i = 0; i < PIPE_MAX_COLOR_BUFS; ++i)
       pipe_surface_reference(&buffer->fb_state.cbufs[i], NULL);
 
    pipe_sampler_view_reference(&buffer->sampler_views.individual.intermediate, NULL);
@@ -506,8 +689,8 @@ vl_idct_upload_matrix(struct pipe_context *pipe, float scale)
    struct pipe_box rect =
    {
       0, 0, 0,
-      BLOCK_WIDTH / 4,
-      BLOCK_HEIGHT,
+      VL_BLOCK_WIDTH / 4,
+      VL_BLOCK_HEIGHT,
       1
    };
 
@@ -529,28 +712,21 @@ vl_idct_upload_matrix(struct pipe_context *pipe, float scale)
    if (!matrix)
       goto error_matrix;
 
-   buf_transfer = pipe->get_transfer
-   (
-      pipe, matrix,
-      0, PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
-      &rect
-   );
-   if (!buf_transfer)
-      goto error_transfer;
-
-   pitch = buf_transfer->stride / sizeof(float);
-
-   f = pipe->transfer_map(pipe, buf_transfer);
+   f = pipe->transfer_map(pipe, matrix, 0,
+                                     PIPE_TRANSFER_WRITE |
+                                     PIPE_TRANSFER_DISCARD_RANGE,
+                                     &rect, &buf_transfer);
    if (!f)
       goto error_map;
 
-   for(i = 0; i < BLOCK_HEIGHT; ++i)
-      for(j = 0; j < BLOCK_WIDTH; ++j)
+   pitch = buf_transfer->stride / sizeof(float);
+
+   for(i = 0; i < VL_BLOCK_HEIGHT; ++i)
+      for(j = 0; j < VL_BLOCK_WIDTH; ++j)
          // transpose and scale
          f[i * pitch + j] = ((const float (*)[8])const_matrix)[j][i] * scale;
 
    pipe->transfer_unmap(pipe, buf_transfer);
-   pipe->transfer_destroy(pipe, buf_transfer);
 
    memset(&sv_templ, 0, sizeof(sv_templ));
    u_sampler_view_default_template(&sv_templ, matrix, matrix->format);
@@ -562,9 +738,6 @@ vl_idct_upload_matrix(struct pipe_context *pipe, float scale)
    return sv;
 
 error_map:
-   pipe->transfer_destroy(pipe, buf_transfer);
-
-error_transfer:
    pipe_resource_reference(&matrix, NULL);
 
 error_matrix:
@@ -577,7 +750,8 @@ bool vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe,
                   struct pipe_sampler_view *matrix,
                   struct pipe_sampler_view *transpose)
 {
-   assert(idct && pipe && matrix);
+   assert(idct && pipe);
+   assert(matrix && transpose);
 
    idct->pipe = pipe;
    idct->buffer_width = buffer_width;
@@ -605,18 +779,16 @@ vl_idct_cleanup(struct vl_idct *idct)
    cleanup_state(idct);
 
    pipe_sampler_view_reference(&idct->matrix, NULL);
+   pipe_sampler_view_reference(&idct->transpose, NULL);
 }
 
 bool
 vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer,
                     struct pipe_sampler_view *source,
-                    struct pipe_sampler_view *intermediate,
-                    struct pipe_surface *destination)
+                    struct pipe_sampler_view *intermediate)
 {
-   assert(buffer);
-   assert(idct);
-   assert(source);
-   assert(destination);
+   assert(buffer && idct);
+   assert(source && intermediate);
 
    memset(buffer, 0, sizeof(struct vl_idct_buffer));
 
@@ -625,46 +797,51 @@ vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer,
    pipe_sampler_view_reference(&buffer->sampler_views.individual.transpose, idct->transpose);
    pipe_sampler_view_reference(&buffer->sampler_views.individual.intermediate, intermediate);
 
-   if (!init_intermediate(idct, buffer))
+   if (!init_source(idct, buffer))
       return false;
 
-   buffer->viewport.scale[2] = 1;
-   buffer->viewport.scale[3] = 1;
-   buffer->viewport.translate[0] = 0;
-   buffer->viewport.translate[1] = 0;
-   buffer->viewport.translate[2] = 0;
-   buffer->viewport.translate[3] = 0;
+   if (!init_intermediate(idct, buffer))
+      return false;
 
    return true;
 }
 
 void
-vl_idct_cleanup_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer)
+vl_idct_cleanup_buffer(struct vl_idct_buffer *buffer)
 {
-   unsigned i;
+   assert(buffer);
 
-   assert(idct && buffer);
+   cleanup_source(buffer);
+   cleanup_intermediate(buffer);
 
-   for(i = 0; i < idct->nr_of_render_targets; ++i)
-      pipe_surface_reference(&buffer->fb_state.cbufs[i], NULL);
-
-   cleanup_intermediate(idct, buffer);
+   pipe_sampler_view_reference(&buffer->sampler_views.individual.matrix, NULL);
+   pipe_sampler_view_reference(&buffer->sampler_views.individual.transpose, NULL);
 }
 
 void
 vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer, unsigned num_instances)
 {
-   assert(idct);
    assert(buffer);
 
    idct->pipe->bind_rasterizer_state(idct->pipe, idct->rs_state);
    idct->pipe->bind_blend_state(idct->pipe, idct->blend);
-   idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers);
+
+   idct->pipe->bind_sampler_states(idct->pipe, PIPE_SHADER_FRAGMENT,
+                                   0, 2, idct->samplers);
+
+   idct->pipe->set_sampler_views(idct->pipe, PIPE_SHADER_FRAGMENT, 0, 2,
+                                 buffer->sampler_views.stage[0]);
+
+   /* mismatch control */
+   idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state_mismatch);
+   idct->pipe->set_viewport_states(idct->pipe, 0, 1, &buffer->viewport_mismatch);
+   idct->pipe->bind_vs_state(idct->pipe, idct->vs_mismatch);
+   idct->pipe->bind_fs_state(idct->pipe, idct->fs_mismatch);
+   util_draw_arrays_instanced(idct->pipe, PIPE_PRIM_POINTS, 0, 1, 0, num_instances);
 
    /* first stage */
    idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state);
-   idct->pipe->set_viewport_state(idct->pipe, &buffer->viewport);
-   idct->pipe->set_fragment_sampler_views(idct->pipe, 2, buffer->sampler_views.stage[0]);
+   idct->pipe->set_viewport_states(idct->pipe, 0, 1, &buffer->viewport);
    idct->pipe->bind_vs_state(idct->pipe, idct->vs);
    idct->pipe->bind_fs_state(idct->pipe, idct->fs);
    util_draw_arrays_instanced(idct->pipe, PIPE_PRIM_QUADS, 0, 4, 0, num_instances);
@@ -673,12 +850,13 @@ vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer, unsigned num_
 void
 vl_idct_prepare_stage2(struct vl_idct *idct, struct vl_idct_buffer *buffer)
 {
-   assert(idct);
    assert(buffer);
 
    /* second stage */
    idct->pipe->bind_rasterizer_state(idct->pipe, idct->rs_state);
-   idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers);
-   idct->pipe->set_fragment_sampler_views(idct->pipe, 2, buffer->sampler_views.stage[1]);
+   idct->pipe->bind_sampler_states(idct->pipe, PIPE_SHADER_FRAGMENT,
+                                   0, 2, idct->samplers);
+   idct->pipe->set_sampler_views(idct->pipe, PIPE_SHADER_FRAGMENT,
+                                 0, 2, buffer->sampler_views.stage[1]);
 }