python: Fix method renaming.
[mesa.git] / src / gallium / state_trackers / g3dvl / vl_r16snorm_mc_buf.c
index 650528ed8fa1a331058b3bc5cde232618d442ba8..eb8270ecad3fdf44cd006b4c285755d19658f790 100644 (file)
@@ -1,15 +1,14 @@
 #define VL_INTERNAL
 #include "vl_r16snorm_mc_buf.h"
 #include <assert.h>
-#include <stdlib.h>
 #include <pipe/p_context.h>
-#include <pipe/p_winsys.h>
 #include <pipe/p_screen.h>
 #include <pipe/p_state.h>
 #include <pipe/p_inlines.h>
 #include <tgsi/tgsi_parse.h>
 #include <tgsi/tgsi_build.h>
 #include <util/u_math.h>
+#include <util/u_memory.h>
 #include "vl_render.h"
 #include "vl_shader_build.h"
 #include "vl_surface.h"
 #include "vl_types.h"
 #include "vl_defs.h"
 
-/*
- * TODO: Dynamically determine number of buf sets to use, based on
- * video size and available mem, since we can easily run out of memory
- * for high res videos.
- * Note: Destroying previous frame's buffers and creating new ones
- * doesn't work, since the buffer are not actually destroyed until their
- * fence is signalled, and if we render fast enough we will create faster
- * than we destroy.
- */
-#define NUM_BUF_SETS 4 /* Number of rotating buffer sets to use */
+const unsigned int DEFAULT_BUF_ALIGNMENT = 1;
 
 enum vlMacroBlockTypeEx
 {
@@ -52,36 +42,79 @@ struct vlFragmentShaderConsts
        struct vlVertex4f div;
 };
 
+struct vlMacroBlockVertexStream0
+{
+       struct vlVertex2f pos;
+       struct vlVertex2f luma_tc;
+       struct vlVertex2f cb_tc;
+       struct vlVertex2f cr_tc;
+};
+
 struct vlR16SnormBufferedMC
 {
        struct vlRender                         base;
 
-       unsigned int                            picture_width, picture_height;
+       unsigned int                            picture_width;
+       unsigned int                            picture_height;
        enum vlFormat                           picture_format;
+       unsigned int                            macroblocks_per_picture;
 
-       unsigned int                            cur_buf;
        struct vlSurface                        *buffered_surface;
-       struct vlSurface                        *past_surface, *future_surface;
+       struct vlSurface                        *past_surface;
+       struct vlSurface                        *future_surface;
        struct vlVertex2f                       surface_tex_inv_size;
        struct vlVertex2f                       zero_block[3];
        unsigned int                            num_macroblocks;
        struct vlMpeg2MacroBlock                *macroblocks;
+       struct pipe_transfer                    *tex_transfer[3];
+       short                                   *texels[3];
 
        struct pipe_context                     *pipe;
        struct pipe_viewport_state              viewport;
        struct pipe_framebuffer_state           render_target;
-       struct pipe_sampler_state               *samplers[5];
-       struct pipe_texture                     *textures[NUM_BUF_SETS][5];
-       struct pipe_surface                     *tex_surface[3];
-       short                                   *texels[3];
+
+       union
+       {
+               void                                    *all[5];
+               struct
+               {
+                       void                            *y;
+                       void                            *cb;
+                       void                            *cr;
+                       void                            *ref[2];
+               };
+       } samplers;
+
+       union
+       {
+               struct pipe_texture                     *all[5];
+               struct
+               {
+                       struct pipe_texture             *y;
+                       struct pipe_texture             *cb;
+                       struct pipe_texture             *cr;
+                       struct pipe_texture             *ref[2];
+               };
+       } textures;
+
+       union
+       {
+               struct pipe_vertex_buffer               all[3];
+               struct
+               {
+                       struct pipe_vertex_buffer       ycbcr;
+                       struct pipe_vertex_buffer       ref[2];
+               };
+       } vertex_bufs;
+
        void                                    *i_vs, *p_vs[2], *b_vs[2];
        void                                    *i_fs, *p_fs[2], *b_fs[2];
-       struct pipe_vertex_buffer               vertex_bufs[NUM_BUF_SETS][3];
        struct pipe_vertex_element              vertex_elems[8];
-       struct pipe_constant_buffer             vs_const_buf, fs_const_buf;
+       struct pipe_constant_buffer             vs_const_buf;
+       struct pipe_constant_buffer             fs_const_buf;
 };
 
-static int vlBegin
+static inline int vlBegin
 (
        struct vlRender *render
 )
@@ -154,7 +187,7 @@ static inline int vlGrabBlocks
        assert(mc);
        assert(blocks);
 
-       tex_pitch = mc->tex_surface[0]->stride / mc->tex_surface[0]->block.size;
+       tex_pitch = mc->tex_transfer[0]->stride / mc->tex_transfer[0]->block.size;
        texels = mc->texels[0] + mbpy * tex_pitch + mbpx;
 
        for (y = 0; y < 2; ++y)
@@ -202,7 +235,7 @@ static inline int vlGrabBlocks
 
        for (tb = 0; tb < 2; ++tb)
        {
-               tex_pitch = mc->tex_surface[tb + 1]->stride / mc->tex_surface[tb + 1]->block.size;
+               tex_pitch = mc->tex_transfer[tb + 1]->stride / mc->tex_transfer[tb + 1]->block.size;
                texels = mc->texels[tb + 1] + mbpy * tex_pitch + mbpx;
 
                if ((coded_block_pattern >> (1 - tb)) & 1)
@@ -263,6 +296,7 @@ static inline int vlGrabMacroBlock
 {
        assert(mc);
        assert(macroblock);
+       assert(mc->num_macroblocks < mc->macroblocks_per_picture);
 
        mc->macroblocks[mc->num_macroblocks].mbx = macroblock->mbx;
        mc->macroblocks[mc->num_macroblocks].mby = macroblock->mby;
@@ -296,6 +330,7 @@ static inline int vlGrabMacroBlock
 }
 
 #define SET_BLOCK(vb, cbp, mbx, mby, unitx, unity, ofsx, ofsy, hx, hy, lm, cbm, crm, zb)                                       \
+       do {                                                                                                                    \
        (vb)[0].pos.x = (mbx) * (unitx) + (ofsx);               (vb)[0].pos.y = (mby) * (unity) + (ofsy);                       \
        (vb)[1].pos.x = (mbx) * (unitx) + (ofsx);               (vb)[1].pos.y = (mby) * (unity) + (ofsy) + (hy);                \
        (vb)[2].pos.x = (mbx) * (unitx) + (ofsx) + (hx);        (vb)[2].pos.y = (mby) * (unity) + (ofsy);                       \
@@ -358,13 +393,16 @@ static inline int vlGrabMacroBlock
                (vb)[3].cr_tc.x = (zb)[2].x + (hx);     (vb)[3].cr_tc.y = (zb)[2].y;                                            \
                (vb)[4].cr_tc.x = (zb)[2].x;            (vb)[4].cr_tc.y = (zb)[2].y + (hy);                                     \
                (vb)[5].cr_tc.x = (zb)[2].x + (hx);     (vb)[5].cr_tc.y = (zb)[2].y + (hy);                                     \
-       }
+       }                                                                                                                       \
+       } while (0)
 
-static inline int vlGrabMacroBlockVB
+static inline int vlGenMacroblockVerts
 (
        struct vlR16SnormBufferedMC *mc,
        struct vlMpeg2MacroBlock *macroblock,
-       unsigned int pos
+       unsigned int pos,
+       struct vlMacroBlockVertexStream0 *ycbcr_vb,
+       struct vlVertex2f **ref_vb
 )
 {
        struct vlVertex2f       mo_vec[2];
@@ -372,6 +410,8 @@ static inline int vlGrabMacroBlockVB
 
        assert(mc);
        assert(macroblock);
+       assert(ycbcr_vb);
+       assert(pos < mc->macroblocks_per_picture);
 
        switch (macroblock->mb_type)
        {
@@ -379,12 +419,9 @@ static inline int vlGrabMacroBlockVB
                {
                        struct vlVertex2f *vb;
 
-                       vb = (struct vlVertex2f*)mc->pipe->winsys->buffer_map
-                       (
-                               mc->pipe->winsys,
-                               mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS][2].buffer,
-                               PIPE_BUFFER_USAGE_CPU_WRITE
-                       ) + pos * 2 * 24;
+                       assert(ref_vb && ref_vb[1]);
+
+                       vb = ref_vb[1] + pos * 2 * 24;
 
                        mo_vec[0].x = macroblock->PMV[0][1][0] * 0.5f * mc->surface_tex_inv_size.x;
                        mo_vec[0].y = macroblock->PMV[0][1][1] * 0.5f * mc->surface_tex_inv_size.y;
@@ -411,8 +448,6 @@ static inline int vlGrabMacroBlockVB
                                }
                        }
 
-                       mc->pipe->winsys->buffer_unmap(mc->pipe->winsys, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS][2].buffer);
-
                        /* fall-through */
                }
                case vlMacroBlockTypeFwdPredicted:
@@ -420,12 +455,9 @@ static inline int vlGrabMacroBlockVB
                {
                        struct vlVertex2f *vb;
 
-                       vb = (struct vlVertex2f*)mc->pipe->winsys->buffer_map
-                       (
-                               mc->pipe->winsys,
-                               mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS][1].buffer,
-                               PIPE_BUFFER_USAGE_CPU_WRITE
-                       ) + pos * 2 * 24;
+                       assert(ref_vb && ref_vb[0]);
+
+                       vb = ref_vb[0] + pos * 2 * 24;
 
                        if (macroblock->mb_type == vlMacroBlockTypeBkwdPredicted)
                        {
@@ -469,8 +501,6 @@ static inline int vlGrabMacroBlockVB
                                }
                        }
 
-                       mc->pipe->winsys->buffer_unmap(mc->pipe->winsys, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS][1].buffer);
-
                        /* fall-through */
                }
                case vlMacroBlockTypeIntra:
@@ -486,20 +516,9 @@ static inline int vlGrabMacroBlockVB
                                mc->surface_tex_inv_size.y * (VL_MACROBLOCK_HEIGHT / 2)
                        };
 
-                       struct vlMacroBlockVertexStream0
-                       {
-                               struct vlVertex2f pos;
-                               struct vlVertex2f luma_tc;
-                               struct vlVertex2f cb_tc;
-                               struct vlVertex2f cr_tc;
-                       } *vb;
+                       struct vlMacroBlockVertexStream0 *vb;
 
-                       vb = (struct vlMacroBlockVertexStream0*)mc->pipe->winsys->buffer_map
-                       (
-                               mc->pipe->winsys,
-                               mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS][0].buffer,
-                               PIPE_BUFFER_USAGE_CPU_WRITE
-                       ) + pos * 24;
+                       vb = ycbcr_vb + pos * 24;
 
                        SET_BLOCK
                        (
@@ -533,8 +552,6 @@ static inline int vlGrabMacroBlockVB
                                4, 2, 1, mc->zero_block
                        );
 
-                       mc->pipe->winsys->buffer_unmap(mc->pipe->winsys, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS][0].buffer);
-
                        break;
                }
                default:
@@ -555,9 +572,6 @@ static int vlFlush
        unsigned int                    num_macroblocks[vlNumMacroBlockExTypes] = {0};
        unsigned int                    offset[vlNumMacroBlockExTypes];
        unsigned int                    vb_start = 0;
-       unsigned int                    mbw;
-       unsigned int                    mbh;
-       unsigned int                    num_mb_per_frame;
        unsigned int                    i;
 
        assert(render);
@@ -567,13 +581,11 @@ static int vlFlush
        if (!mc->buffered_surface)
                return 0;
 
-       mbw = align(mc->picture_width, VL_MACROBLOCK_WIDTH) / VL_MACROBLOCK_WIDTH;
-       mbh = align(mc->picture_height, VL_MACROBLOCK_HEIGHT) / VL_MACROBLOCK_HEIGHT;
-       num_mb_per_frame = mbw * mbh;
-
-       if (mc->num_macroblocks < num_mb_per_frame)
+       if (mc->num_macroblocks < mc->macroblocks_per_picture)
                return 0;
 
+       assert(mc->num_macroblocks <= mc->macroblocks_per_picture);
+
        pipe = mc->pipe;
 
        for (i = 0; i < mc->num_macroblocks; ++i)
@@ -588,19 +600,43 @@ static int vlFlush
        for (i = 1; i < vlNumMacroBlockExTypes; ++i)
                offset[i] = offset[i - 1] + num_macroblocks[i - 1];
 
-       for (i = 0; i < mc->num_macroblocks; ++i)
        {
-               enum vlMacroBlockTypeEx mb_type_ex = vlGetMacroBlockTypeEx(&mc->macroblocks[i]);
+               struct vlMacroBlockVertexStream0        *ycbcr_vb;
+               struct vlVertex2f                       *ref_vb[2];
 
-               vlGrabMacroBlockVB(mc, &mc->macroblocks[i], offset[mb_type_ex]);
+               ycbcr_vb = (struct vlMacroBlockVertexStream0*)pipe_buffer_map
+               (
+                       pipe->screen,
+                       mc->vertex_bufs.ycbcr.buffer,
+                       PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
+               );
 
-               offset[mb_type_ex]++;
+               for (i = 0; i < 2; ++i)
+                       ref_vb[i] = (struct vlVertex2f*)pipe_buffer_map
+                       (
+                               pipe->screen,
+                               mc->vertex_bufs.ref[i].buffer,
+                               PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
+                       );
+
+               for (i = 0; i < mc->num_macroblocks; ++i)
+               {
+                       enum vlMacroBlockTypeEx mb_type_ex = vlGetMacroBlockTypeEx(&mc->macroblocks[i]);
+
+                       vlGenMacroblockVerts(mc, &mc->macroblocks[i], offset[mb_type_ex], ycbcr_vb, ref_vb);
+
+                       offset[mb_type_ex]++;
+               }
+
+               pipe_buffer_unmap(pipe->screen, mc->vertex_bufs.ycbcr.buffer);
+               for (i = 0; i < 2; ++i)
+                       pipe_buffer_unmap(pipe->screen, mc->vertex_bufs.ref[i].buffer);
        }
-       
+
        for (i = 0; i < 3; ++i)
        {
-               pipe_surface_unmap(mc->tex_surface[i]);
-               mc->pipe->screen->tex_surface_release(mc->pipe->screen, &mc->tex_surface[i]);
+               pipe->screen->transfer_unmap(pipe->screen, mc->tex_transfer[i]);
+               pipe->screen->tex_transfer_destroy(mc->tex_transfer[i]);
        }
 
        mc->render_target.cbufs[0] = pipe->screen->get_tex_surface
@@ -612,26 +648,26 @@ static int vlFlush
 
        pipe->set_framebuffer_state(pipe, &mc->render_target);
        pipe->set_viewport_state(pipe, &mc->viewport);
-       vs_consts = pipe->winsys->buffer_map
+       vs_consts = pipe_buffer_map
        (
-               pipe->winsys,
+               pipe->screen,
                mc->vs_const_buf.buffer,
-               PIPE_BUFFER_USAGE_CPU_WRITE
+               PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
        );
 
        vs_consts->denorm.x = mc->buffered_surface->texture->width[0];
        vs_consts->denorm.y = mc->buffered_surface->texture->height[0];
 
-       pipe->winsys->buffer_unmap(pipe->winsys, mc->vs_const_buf.buffer);
+       pipe_buffer_unmap(pipe->screen, mc->vs_const_buf.buffer);
        pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, &mc->vs_const_buf);
        pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, &mc->fs_const_buf);
 
        if (num_macroblocks[vlMacroBlockExTypeIntra] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 1, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 1, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 4, mc->vertex_elems);
-               pipe->set_sampler_textures(pipe, 3, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 3, (void**)mc->samplers);
+               pipe->set_sampler_textures(pipe, 3, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 3, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->i_vs);
                pipe->bind_fs_state(pipe, mc->i_fs);
 
@@ -641,11 +677,11 @@ static int vlFlush
 
        if (num_macroblocks[vlMacroBlockExTypeFwdPredictedFrame] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 6, mc->vertex_elems);
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][3] = mc->past_surface->texture;
-               pipe->set_sampler_textures(pipe, 4, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 4, (void**)mc->samplers);
+               mc->textures.ref[0] = mc->past_surface->texture;
+               pipe->set_sampler_textures(pipe, 4, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 4, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->p_vs[0]);
                pipe->bind_fs_state(pipe, mc->p_fs[0]);
 
@@ -655,11 +691,11 @@ static int vlFlush
 
        if (num_macroblocks[vlMacroBlockExTypeFwdPredictedField] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 6, mc->vertex_elems);
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][3] = mc->past_surface->texture;
-               pipe->set_sampler_textures(pipe, 4, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 4, (void**)mc->samplers);
+               mc->textures.ref[0] = mc->past_surface->texture;
+               pipe->set_sampler_textures(pipe, 4, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 4, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->p_vs[1]);
                pipe->bind_fs_state(pipe, mc->p_fs[1]);
 
@@ -669,11 +705,11 @@ static int vlFlush
 
        if (num_macroblocks[vlMacroBlockExTypeBkwdPredictedFrame] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 6, mc->vertex_elems);
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][3] = mc->future_surface->texture;
-               pipe->set_sampler_textures(pipe, 4, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 4, (void**)mc->samplers);
+               mc->textures.ref[0] = mc->future_surface->texture;
+               pipe->set_sampler_textures(pipe, 4, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 4, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->p_vs[0]);
                pipe->bind_fs_state(pipe, mc->p_fs[0]);
 
@@ -683,11 +719,11 @@ static int vlFlush
 
        if (num_macroblocks[vlMacroBlockExTypeBkwdPredictedField] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 2, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 6, mc->vertex_elems);
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][3] = mc->future_surface->texture;
-               pipe->set_sampler_textures(pipe, 4, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 4, (void**)mc->samplers);
+               mc->textures.ref[0] = mc->future_surface->texture;
+               pipe->set_sampler_textures(pipe, 4, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 4, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->p_vs[1]);
                pipe->bind_fs_state(pipe, mc->p_fs[1]);
 
@@ -697,12 +733,12 @@ static int vlFlush
 
        if (num_macroblocks[vlMacroBlockExTypeBiPredictedFrame] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 3, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 3, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 8, mc->vertex_elems);
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][3] = mc->past_surface->texture;
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][4] = mc->future_surface->texture;
-               pipe->set_sampler_textures(pipe, 5, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 5, (void**)mc->samplers);
+               mc->textures.ref[0] = mc->past_surface->texture;
+               mc->textures.ref[1] = mc->future_surface->texture;
+               pipe->set_sampler_textures(pipe, 5, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 5, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->b_vs[0]);
                pipe->bind_fs_state(pipe, mc->b_fs[0]);
 
@@ -712,12 +748,12 @@ static int vlFlush
 
        if (num_macroblocks[vlMacroBlockExTypeBiPredictedField] > 0)
        {
-               pipe->set_vertex_buffers(pipe, 3, mc->vertex_bufs[mc->cur_buf % NUM_BUF_SETS]);
+               pipe->set_vertex_buffers(pipe, 3, mc->vertex_bufs.all);
                pipe->set_vertex_elements(pipe, 8, mc->vertex_elems);
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][3] = mc->past_surface->texture;
-               mc->textures[mc->cur_buf % NUM_BUF_SETS][4] = mc->future_surface->texture;
-               pipe->set_sampler_textures(pipe, 5, mc->textures[mc->cur_buf % NUM_BUF_SETS]);
-               pipe->bind_sampler_states(pipe, 5, (void**)mc->samplers);
+               mc->textures.ref[0] = mc->past_surface->texture;
+               mc->textures.ref[1] = mc->future_surface->texture;
+               pipe->set_sampler_textures(pipe, 5, mc->textures.all);
+               pipe->bind_sampler_states(pipe, 5, mc->samplers.all);
                pipe->bind_vs_state(pipe, mc->b_vs[1]);
                pipe->bind_fs_state(pipe, mc->b_fs[1]);
 
@@ -726,13 +762,13 @@ static int vlFlush
        }
 
        pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, &mc->buffered_surface->render_fence);
+       pipe_surface_reference(&mc->render_target.cbufs[0], NULL);
 
        for (i = 0; i < 3; ++i)
                mc->zero_block[i].x = -1.0f;
 
        mc->buffered_surface = NULL;
        mc->num_macroblocks = 0;
-       mc->cur_buf++;
 
        return 0;
 }
@@ -745,6 +781,7 @@ static int vlRenderMacroBlocksMpeg2R16SnormBuffered
 )
 {
        struct vlR16SnormBufferedMC     *mc;
+       bool                            new_surface = false;
        unsigned int                    i;
 
        assert(render);
@@ -756,43 +793,32 @@ static int vlRenderMacroBlocksMpeg2R16SnormBuffered
                if (mc->buffered_surface != surface)
                {
                        vlFlush(&mc->base);
-                       mc->buffered_surface = surface;
-                       mc->past_surface = batch->past_surface;
-                       mc->future_surface = batch->future_surface;
-                       mc->surface_tex_inv_size.x = 1.0f / surface->texture->width[0];
-                       mc->surface_tex_inv_size.y = 1.0f / surface->texture->height[0];
-                       
-                       for (i = 0; i < 3; ++i)
-                       {
-                               mc->tex_surface[i] = mc->pipe->screen->get_tex_surface
-                               (
-                                       mc->pipe->screen,
-                                       mc->textures[mc->cur_buf % NUM_BUF_SETS][i],
-                                       0, 0, 0, PIPE_BUFFER_USAGE_CPU_WRITE
-                               );
-
-                               mc->texels[i] = pipe_surface_map(mc->tex_surface[i], PIPE_BUFFER_USAGE_CPU_WRITE);
-                       }
+                       new_surface = true;
                }
        }
        else
+               new_surface = true;
+
+       if (new_surface)
        {
                mc->buffered_surface = surface;
                mc->past_surface = batch->past_surface;
                mc->future_surface = batch->future_surface;
                mc->surface_tex_inv_size.x = 1.0f / surface->texture->width[0];
                mc->surface_tex_inv_size.y = 1.0f / surface->texture->height[0];
-               
+
                for (i = 0; i < 3; ++i)
                {
-                       mc->tex_surface[i] = mc->pipe->screen->get_tex_surface
+                       mc->tex_transfer[i] = mc->pipe->screen->get_tex_transfer
                        (
                                mc->pipe->screen,
-                               mc->textures[mc->cur_buf % NUM_BUF_SETS][i],
-                               0, 0, 0, PIPE_BUFFER_USAGE_CPU_WRITE
+                               mc->textures.all[i],
+                               0, 0, 0, PIPE_TRANSFER_WRITE, 0, 0,
+                               surface->texture->width[0],
+                               surface->texture->height[0]
                        );
 
-                       mc->texels[i] = pipe_surface_map(mc->tex_surface[i], PIPE_BUFFER_USAGE_CPU_WRITE);
+                       mc->texels[i] = mc->pipe->screen->transfer_map(mc->pipe->screen, mc->tex_transfer[i]);
                }
        }
 
@@ -802,7 +828,7 @@ static int vlRenderMacroBlocksMpeg2R16SnormBuffered
        return 0;
 }
 
-static int vlEnd
+static inline int vlEnd
 (
        struct vlRender *render
 )
@@ -819,7 +845,7 @@ static int vlDestroy
 {
        struct vlR16SnormBufferedMC     *mc;
        struct pipe_context             *pipe;
-       unsigned int                    h, i;
+       unsigned int                    i;
 
        assert(render);
 
@@ -827,19 +853,14 @@ static int vlDestroy
        pipe = mc->pipe;
 
        for (i = 0; i < 5; ++i)
-               pipe->delete_sampler_state(pipe, mc->samplers[i]);
+               pipe->delete_sampler_state(pipe, mc->samplers.all[i]);
 
-       for (h = 0; h < NUM_BUF_SETS; ++h)
-                       for (i = 0; i < 3; ++i)
-                               pipe->winsys->buffer_destroy(pipe->winsys, mc->vertex_bufs[h][i].buffer);
+       for (i = 0; i < 3; ++i)
+               pipe_buffer_reference(&mc->vertex_bufs.all[i].buffer, NULL);
 
        /* Textures 3 & 4 are not created directly, no need to release them here */
-       for (i = 0; i < NUM_BUF_SETS; ++i)
-       {
-               pipe_texture_release(&mc->textures[i][0]);
-               pipe_texture_release(&mc->textures[i][1]);
-               pipe_texture_release(&mc->textures[i][2]);
-       }
+       for (i = 0; i < 3; ++i)
+               pipe_texture_reference(&mc->textures.all[i], NULL);
 
        pipe->delete_vs_state(pipe, mc->i_vs);
        pipe->delete_fs_state(pipe, mc->i_fs);
@@ -852,11 +873,11 @@ static int vlDestroy
                pipe->delete_fs_state(pipe, mc->b_fs[i]);
        }
 
-       pipe->winsys->buffer_destroy(pipe->winsys, mc->vs_const_buf.buffer);
-       pipe->winsys->buffer_destroy(pipe->winsys, mc->fs_const_buf.buffer);
+       pipe_buffer_reference(&mc->vs_const_buf.buffer, NULL);
+       pipe_buffer_reference(&mc->fs_const_buf.buffer, NULL);
 
-       free(mc->macroblocks);
-       free(mc);
+       FREE(mc->macroblocks);
+       FREE(mc);
 
        return 0;
 }
@@ -882,42 +903,39 @@ static int vlCreateDataBufs
 {
        const unsigned int      mbw = align(mc->picture_width, VL_MACROBLOCK_WIDTH) / VL_MACROBLOCK_WIDTH;
        const unsigned int      mbh = align(mc->picture_height, VL_MACROBLOCK_HEIGHT) / VL_MACROBLOCK_HEIGHT;
-       const unsigned int      num_mb_per_frame = mbw * mbh;
 
        struct pipe_context     *pipe;
-       unsigned int            h, i;
+       unsigned int            i;
 
        assert(mc);
 
        pipe = mc->pipe;
+       mc->macroblocks_per_picture = mbw * mbh;
 
        /* Create our vertex buffers */
-       for (h = 0; h < NUM_BUF_SETS; ++h)
+       mc->vertex_bufs.ycbcr.stride = sizeof(struct vlVertex2f) * 4;
+       mc->vertex_bufs.ycbcr.max_index = 24 * mc->macroblocks_per_picture - 1;
+       mc->vertex_bufs.ycbcr.buffer_offset = 0;
+       mc->vertex_bufs.ycbcr.buffer = pipe_buffer_create
+       (
+               pipe->screen,
+               DEFAULT_BUF_ALIGNMENT,
+               PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_DISCARD,
+               sizeof(struct vlVertex2f) * 4 * 24 * mc->macroblocks_per_picture
+       );
+
+       for (i = 1; i < 3; ++i)
        {
-               mc->vertex_bufs[h][0].pitch = sizeof(struct vlVertex2f) * 4;
-               mc->vertex_bufs[h][0].max_index = 24 * num_mb_per_frame - 1;
-               mc->vertex_bufs[h][0].buffer_offset = 0;
-               mc->vertex_bufs[h][0].buffer = pipe->winsys->buffer_create
+               mc->vertex_bufs.all[i].stride = sizeof(struct vlVertex2f) * 2;
+               mc->vertex_bufs.all[i].max_index = 24 * mc->macroblocks_per_picture - 1;
+               mc->vertex_bufs.all[i].buffer_offset = 0;
+               mc->vertex_bufs.all[i].buffer = pipe_buffer_create
                (
-                       pipe->winsys,
-                       1,
-                       PIPE_BUFFER_USAGE_VERTEX,
-                       sizeof(struct vlVertex2f) * 4 * 24 * num_mb_per_frame
+                       pipe->screen,
+                       DEFAULT_BUF_ALIGNMENT,
+                       PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_DISCARD,
+                       sizeof(struct vlVertex2f) * 2 * 24 * mc->macroblocks_per_picture
                );
-
-               for (i = 1; i < 3; ++i)
-               {
-                       mc->vertex_bufs[h][i].pitch = sizeof(struct vlVertex2f) * 2;
-                       mc->vertex_bufs[h][i].max_index = 24 * num_mb_per_frame - 1;
-                       mc->vertex_bufs[h][i].buffer_offset = 0;
-                       mc->vertex_bufs[h][i].buffer = pipe->winsys->buffer_create
-                       (
-                               pipe->winsys,
-                               1,
-                               PIPE_BUFFER_USAGE_VERTEX,
-                               sizeof(struct vlVertex2f) * 2 * 24 * num_mb_per_frame
-                       );
-               }
        }
 
        /* Position element */
@@ -969,34 +987,32 @@ static int vlCreateDataBufs
        mc->vertex_elems[7].src_format = PIPE_FORMAT_R32G32_FLOAT;
 
        /* Create our constant buffer */
-       mc->vs_const_buf.size = sizeof(struct vlVertexShaderConsts);
-       mc->vs_const_buf.buffer = pipe->winsys->buffer_create
+       mc->vs_const_buf.buffer = pipe_buffer_create
        (
-               pipe->winsys,
-               1,
-               PIPE_BUFFER_USAGE_CONSTANT,
-               mc->vs_const_buf.size
+               pipe->screen,
+               DEFAULT_BUF_ALIGNMENT,
+               PIPE_BUFFER_USAGE_CONSTANT | PIPE_BUFFER_USAGE_DISCARD,
+               sizeof(struct vlVertexShaderConsts)
        );
 
-       mc->fs_const_buf.size = sizeof(struct vlFragmentShaderConsts);
-       mc->fs_const_buf.buffer = pipe->winsys->buffer_create
+       mc->fs_const_buf.buffer = pipe_buffer_create
        (
-               pipe->winsys,
-               1,
+               pipe->screen,
+               DEFAULT_BUF_ALIGNMENT,
                PIPE_BUFFER_USAGE_CONSTANT,
-               mc->fs_const_buf.size
+               sizeof(struct vlFragmentShaderConsts)
        );
 
        memcpy
        (
-               pipe->winsys->buffer_map(pipe->winsys, mc->fs_const_buf.buffer, PIPE_BUFFER_USAGE_CPU_WRITE),
+               pipe_buffer_map(pipe->screen, mc->fs_const_buf.buffer, PIPE_BUFFER_USAGE_CPU_WRITE),
                &fs_consts,
                sizeof(struct vlFragmentShaderConsts)
        );
 
-       pipe->winsys->buffer_unmap(pipe->winsys, mc->fs_const_buf.buffer);
+       pipe_buffer_unmap(pipe->screen, mc->fs_const_buf.buffer);
 
-       mc->macroblocks = malloc(sizeof(struct vlMpeg2MacroBlock) * num_mb_per_frame);
+       mc->macroblocks = MALLOC(sizeof(struct vlMpeg2MacroBlock) * mc->macroblocks_per_picture);
 
        return 0;
 }
@@ -1016,6 +1032,13 @@ static int vlInit
 
        pipe = mc->pipe;
 
+       mc->buffered_surface = NULL;
+       mc->past_surface = NULL;
+       mc->future_surface = NULL;
+       for (i = 0; i < 3; ++i)
+               mc->zero_block[i].x = -1.0f;
+       mc->num_macroblocks = 0;
+
        /* For MC we render to textures, which are rounded up to nearest POT */
        mc->viewport.scale[0] = vlRoundUpPOT(mc->picture_width);
        mc->viewport.scale[1] = vlRoundUpPOT(mc->picture_height);
@@ -1028,7 +1051,7 @@ static int vlInit
 
        mc->render_target.width = vlRoundUpPOT(mc->picture_width);
        mc->render_target.height = vlRoundUpPOT(mc->picture_height);
-       mc->render_target.num_cbufs = 1;
+       mc->render_target.nr_cbufs = 1;
        /* FB for MC stage is a vlSurface created by the user, set at render time */
        mc->render_target.zsbuf = NULL;
 
@@ -1057,7 +1080,7 @@ static int vlInit
                /*sampler.max_lod = ;*/
                /*sampler.border_color[i] = ;*/
                /*sampler.max_anisotropy = ;*/
-               mc->samplers[i] = pipe->create_sampler_state(pipe, &sampler);
+               mc->samplers.all[i] = pipe->create_sampler_state(pipe, &sampler);
        }
 
        memset(&template, 0, sizeof(struct pipe_texture));
@@ -1069,10 +1092,9 @@ static int vlInit
        template.depth[0] = 1;
        template.compressed = 0;
        pf_get_block(template.format, &template.block);
-       template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
+       template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_DYNAMIC;
 
-       for (i = 0; i < NUM_BUF_SETS; ++i)
-               mc->textures[i][0] = pipe->screen->texture_create(pipe->screen, &template);
+       mc->textures.y = pipe->screen->texture_create(pipe->screen, &template);
 
        if (mc->picture_format == vlFormatYCbCr420)
        {
@@ -1082,13 +1104,10 @@ static int vlInit
        else if (mc->picture_format == vlFormatYCbCr422)
                template.height[0] = vlRoundUpPOT(mc->picture_height / 2);
 
-       for (i = 0; i < NUM_BUF_SETS; ++i)
-       {
-               mc->textures[i][1] = pipe->screen->texture_create(pipe->screen, &template);
-               mc->textures[i][2] = pipe->screen->texture_create(pipe->screen, &template);
-       }
+       mc->textures.cb = pipe->screen->texture_create(pipe->screen, &template);
+       mc->textures.cr = pipe->screen->texture_create(pipe->screen, &template);
 
-       /* textures[3] & textures[4] are assigned from vlSurfaces for P and B macroblocks at render time */
+       /* textures.all[3] & textures.all[4] are assigned from vlSurfaces for P and B macroblocks at render time */
 
        vlCreateVertexShaderIMB(mc);
        vlCreateFragmentShaderIMB(mc);
@@ -1114,13 +1133,12 @@ int vlCreateR16SNormBufferedMC
        struct vlRender **render
 )
 {
-       struct vlR16SnormBufferedMC     *mc;
-       unsigned int                    i;
+       struct vlR16SnormBufferedMC *mc;
 
        assert(pipe);
        assert(render);
 
-       mc = calloc(1, sizeof(struct vlR16SnormBufferedMC));
+       mc = CALLOC_STRUCT(vlR16SnormBufferedMC);
 
        mc->base.vlBegin = &vlBegin;
        mc->base.vlRenderMacroBlocksMpeg2 = &vlRenderMacroBlocksMpeg2R16SnormBuffered;
@@ -1131,14 +1149,6 @@ int vlCreateR16SNormBufferedMC
        mc->picture_width = picture_width;
        mc->picture_height = picture_height;
 
-       mc->cur_buf = 0;
-       mc->buffered_surface = NULL;
-       mc->past_surface = NULL;
-       mc->future_surface = NULL;
-       for (i = 0; i < 3; ++i)
-               mc->zero_block[i].x = -1.0f;
-       mc->num_macroblocks = 0;
-
        vlInit(mc);
 
        *render = &mc->base;