st/nine: Align stack for entry points
[mesa.git] / src / gallium / state_trackers / nine / surface9.c
index aa79112531980479ba36b1c27e40f0b53154c786..4c4234bfe277bb9fb7c6f30253425dec97341fb9 100644 (file)
 
 #include "surface9.h"
 #include "device9.h"
-#include "basetexture9.h" /* for marking dirty */
+
+/* for marking dirty */
+#include "basetexture9.h"
+#include "texture9.h"
+#include "cubetexture9.h"
 
 #include "nine_helpers.h"
 #include "nine_pipe.h"
@@ -38,6 +42,8 @@
 
 #define DBG_CHANNEL DBG_SURFACE
 
+#define is_ATI1_ATI2(format) (format == PIPE_FORMAT_RGTC1_UNORM || format == PIPE_FORMAT_RGTC2_UNORM)
+
 HRESULT
 NineSurface9_ctor( struct NineSurface9 *This,
                    struct NineUnknownParams *pParams,
@@ -50,6 +56,9 @@ NineSurface9_ctor( struct NineSurface9 *This,
                    D3DSURFACE_DESC *pDesc )
 {
     HRESULT hr;
+    union pipe_color_union rgba = {0};
+    struct pipe_surface *surf;
+    struct pipe_context *pipe = pParams->device->pipe;
 
     DBG("This=%p pDevice=%p pResource=%p Level=%u Layer=%u pDesc=%p\n",
         This, pParams->device, pResource, Level, Layer, pDesc);
@@ -60,14 +69,20 @@ NineSurface9_ctor( struct NineSurface9 *This,
     user_assert(!(pDesc->Usage & D3DUSAGE_DYNAMIC) ||
                 (pDesc->Pool != D3DPOOL_MANAGED), D3DERR_INVALIDCALL);
 
-    assert(pResource ||
-           pDesc->Pool != D3DPOOL_DEFAULT || pDesc->Format == D3DFMT_NULL);
+    assert(pResource || (user_buffer && pDesc->Pool != D3DPOOL_DEFAULT) ||
+           (!pContainer && pDesc->Pool != D3DPOOL_DEFAULT) ||
+           pDesc->Format == D3DFMT_NULL);
 
     assert(!pResource || !user_buffer);
+    assert(!user_buffer || pDesc->Pool != D3DPOOL_DEFAULT);
+    /* The only way we can have !pContainer is being created
+     * from create_zs_or_rt_surface with params 0 0 0 */
+    assert(pContainer || (Level == 0 && Layer == 0 && TextureType == 0));
+
+    This->data = (uint8_t *)user_buffer;
 
     This->base.info.screen = pParams->device->screen;
     This->base.info.target = PIPE_TEXTURE_2D;
-    This->base.info.format = d3d9_to_pipe_format(pDesc->Format);
     This->base.info.width0 = pDesc->Width;
     This->base.info.height0 = pDesc->Height;
     This->base.info.depth0 = 1;
@@ -77,21 +92,36 @@ NineSurface9_ctor( struct NineSurface9 *This,
     This->base.info.usage = PIPE_USAGE_DEFAULT;
     This->base.info.bind = PIPE_BIND_SAMPLER_VIEW;
     This->base.info.flags = 0;
+    This->base.info.format = d3d9_to_pipe_format_checked(This->base.info.screen,
+                                                         pDesc->Format,
+                                                         This->base.info.target,
+                                                         This->base.info.nr_samples,
+                                                         This->base.info.bind,
+                                                         FALSE,
+                                                         pDesc->Pool == D3DPOOL_SCRATCH);
 
     if (pDesc->Usage & D3DUSAGE_RENDERTARGET)
         This->base.info.bind |= PIPE_BIND_RENDER_TARGET;
     if (pDesc->Usage & D3DUSAGE_DEPTHSTENCIL)
         This->base.info.bind |= PIPE_BIND_DEPTH_STENCIL;
 
-    if (pDesc->Pool == D3DPOOL_SYSTEMMEM) {
-        This->base.info.usage = PIPE_USAGE_STAGING;
-        This->data = (uint8_t *)user_buffer; /* this is *pSharedHandle */
-        assert(!pResource);
-    } else {
-        if (pResource && (pDesc->Usage & D3DUSAGE_DYNAMIC))
-            pResource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
+    /* Ram buffer with no parent. Has to allocate the resource itself */
+    if (!pResource && !pContainer) {
+        assert(!user_buffer);
+        This->data = align_malloc(
+            nine_format_get_level_alloc_size(This->base.info.format,
+                                             pDesc->Width,
+                                             pDesc->Height,
+                                             0), 32);
+        if (!This->data)
+            return E_OUTOFMEMORY;
     }
 
+    assert(pDesc->Pool != D3DPOOL_SYSTEMMEM || !pResource);
+
+    if (pResource && (pDesc->Usage & D3DUSAGE_DYNAMIC))
+        pResource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
+
     hr = NineResource9_ctor(&This->base, pParams, pResource, FALSE, D3DRTYPE_SURFACE,
                             pDesc->Pool, pDesc->Usage);
     if (FAILED(hr))
@@ -106,23 +136,15 @@ NineSurface9_ctor( struct NineSurface9 *This,
     This->layer = Layer;
     This->desc = *pDesc;
 
-    This->stride = util_format_get_stride(This->base.info.format, pDesc->Width);
-    This->stride = align(This->stride, 4);
+    This->stride = nine_format_get_stride(This->base.info.format, pDesc->Width);
 
-    if (!pResource && !This->data) {
-        const unsigned size = This->stride *
-            util_format_get_nblocksy(This->base.info.format, This->desc.Height);
+    if (pResource && NineSurface9_IsOffscreenPlain(This))
+        pResource->flags |= NINE_RESOURCE_FLAG_LOCKABLE;
 
-        DBG("(%p(This=%p),level=%u) Allocating 0x%x bytes of system memory.\n",
-            This->base.base.container, This, This->level, size);
-
-        This->data = (uint8_t *)MALLOC(size);
-        if (!This->data)
-            return E_OUTOFMEMORY;
-        This->manage_data = TRUE;
-    } else {
-        if (pResource && NineSurface9_IsOffscreenPlain(This))
-            pResource->flags |= NINE_RESOURCE_FLAG_LOCKABLE; /* why setting this flag there ? too late ? should be before NineResource9_ctor call perhaps ? */
+    /* TODO: investigate what else exactly needs to be cleared */
+    if (This->base.resource && (pDesc->Usage & D3DUSAGE_RENDERTARGET)) {
+        surf = NineSurface9_GetSurface(This, 0);
+        pipe->clear_render_target(pipe, surf, &rgba, 0, 0, pDesc->Width, pDesc->Height);
     }
 
     NineSurface9_Dump(This);
@@ -135,14 +157,13 @@ NineSurface9_dtor( struct NineSurface9 *This )
 {
     if (This->transfer)
         NineSurface9_UnlockRect(This);
-    NineSurface9_ClearDirtyRects(This);
 
     pipe_surface_reference(&This->surface[0], NULL);
     pipe_surface_reference(&This->surface[1], NULL);
 
-    /* release allocated system memory for non-D3DPOOL_DEFAULT resources */
-    if (This->manage_data && This->data)
-        FREE(This->data);
+    /* Release system memory when we have to manage it (no parent) */
+    if (!This->base.base.container && This->data)
+        align_free(This->data);
     NineResource9_dtor(&This->base);
 }
 
@@ -150,14 +171,21 @@ struct pipe_surface *
 NineSurface9_CreatePipeSurface( struct NineSurface9 *This, const int sRGB )
 {
     struct pipe_context *pipe = This->pipe;
+    struct pipe_screen *screen = pipe->screen;
     struct pipe_resource *resource = This->base.resource;
     struct pipe_surface templ;
+    enum pipe_format srgb_format;
 
-    assert(This->desc.Pool == D3DPOOL_DEFAULT ||
-           This->desc.Pool == D3DPOOL_MANAGED);
+    assert(This->desc.Pool == D3DPOOL_DEFAULT);
     assert(resource);
 
-    templ.format = sRGB ? util_format_srgb(resource->format) : resource->format;
+    srgb_format = util_format_srgb(resource->format);
+    if (sRGB && srgb_format != PIPE_FORMAT_NONE &&
+        screen->is_format_supported(screen, srgb_format,
+                                    resource->target, 0, resource->bind))
+        templ.format = srgb_format;
+    else
+        templ.format = resource->format;
     templ.u.tex.level = This->level;
     templ.u.tex.first_layer = This->layer;
     templ.u.tex.last_layer = This->layer;
@@ -197,7 +225,7 @@ NineSurface9_Dump( struct NineSurface9 *This )
 }
 #endif /* DEBUG */
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineSurface9_GetContainer( struct NineSurface9 *This,
                            REFIID riid,
                            void **ppContainer )
@@ -211,7 +239,7 @@ NineSurface9_GetContainer( struct NineSurface9 *This,
     return hr;
 }
 
-static INLINE void
+void
 NineSurface9_MarkContainerDirty( struct NineSurface9 *This )
 {
     if (This->texture) {
@@ -221,7 +249,7 @@ NineSurface9_MarkContainerDirty( struct NineSurface9 *This )
         assert(This->texture == D3DRTYPE_TEXTURE ||
                This->texture == D3DRTYPE_CUBETEXTURE);
         if (This->base.pool == D3DPOOL_MANAGED)
-            tex->dirty = TRUE;
+            tex->managed.dirty = TRUE;
         else
         if (This->base.usage & D3DUSAGE_AUTOGENMIPMAP)
             tex->dirty_mip = TRUE;
@@ -230,7 +258,7 @@ NineSurface9_MarkContainerDirty( struct NineSurface9 *This )
     }
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineSurface9_GetDesc( struct NineSurface9 *This,
                       D3DSURFACE_DESC *pDesc )
 {
@@ -239,57 +267,42 @@ NineSurface9_GetDesc( struct NineSurface9 *This,
     return D3D_OK;
 }
 
-/* Wine just keeps a single directy rect and expands it to cover all
- * the dirty rects ever added.
- * We'll keep 2, and expand the one that fits better, just for fun.
- */
-INLINE void
+/* Add the dirty rects to the source texture */
+inline void
 NineSurface9_AddDirtyRect( struct NineSurface9 *This,
                            const struct pipe_box *box )
 {
-    float area[2];
-    struct u_rect rect, cover_a, cover_b;
+    RECT dirty_rect;
 
-    if (!box) {
-        This->dirty_rects[0].x0 = 0;
-        This->dirty_rects[0].y0 = 0;
-        This->dirty_rects[0].x1 = This->desc.Width;
-        This->dirty_rects[0].y1 = This->desc.Height;
+    DBG("This=%p box=%p\n", This, box);
 
-        memset(&This->dirty_rects[1], 0, sizeof(This->dirty_rects[1]));
-        return;
-    }
-    rect.x0 = box->x;
-    rect.y0 = box->y;
-    rect.x1 = box->x + box->width;
-    rect.y1 = box->y + box->height;
+    assert (This->base.pool != D3DPOOL_MANAGED ||
+            This->texture == D3DRTYPE_CUBETEXTURE ||
+            This->texture == D3DRTYPE_TEXTURE);
 
-    if (This->dirty_rects[0].x1 == 0) {
-        This->dirty_rects[0] = rect;
+    if (This->base.pool == D3DPOOL_DEFAULT)
         return;
-    }
 
-    u_rect_union(&cover_a, &This->dirty_rects[0], &rect);
-    area[0] = u_rect_area(&cover_a);
+    /* Add a dirty rect to level 0 of the parent texture */
+    dirty_rect.left = box->x << This->level_actual;
+    dirty_rect.right = dirty_rect.left + (box->width << This->level_actual);
+    dirty_rect.top = box->y << This->level_actual;
+    dirty_rect.bottom = dirty_rect.top + (box->height << This->level_actual);
 
-    if (This->dirty_rects[1].x1 == 0) {
-        area[1] = u_rect_area(&This->dirty_rects[0]);
-        if (area[0] > (area[1] * 1.25f))
-            This->dirty_rects[1] = rect;
-        else
-            This->dirty_rects[0] = cover_a;
-    } else {
-        u_rect_union(&cover_b, &This->dirty_rects[1], &rect);
-        area[1] = u_rect_area(&cover_b);
+    if (This->texture == D3DRTYPE_TEXTURE) {
+        struct NineTexture9 *tex =
+            NineTexture9(This->base.base.container);
 
-        if (area[0] > area[1])
-            This->dirty_rects[1] = cover_b;
-        else
-            This->dirty_rects[0] = cover_a;
+        NineTexture9_AddDirtyRect(tex, &dirty_rect);
+    } else if (This->texture == D3DRTYPE_CUBETEXTURE) {
+        struct NineCubeTexture9 *ctex =
+            NineCubeTexture9(This->base.base.container);
+
+        NineCubeTexture9_AddDirtyRect(ctex, This->layer, &dirty_rect);
     }
 }
 
-static INLINE uint8_t *
+static inline uint8_t *
 NineSurface9_GetSystemMemPointer(struct NineSurface9 *This, int x, int y)
 {
     unsigned x_offset = util_format_get_stride(This->base.info.format, x);
@@ -300,7 +313,7 @@ NineSurface9_GetSystemMemPointer(struct NineSurface9 *This, int x, int y)
     return This->data + (y * This->stride + x_offset);
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineSurface9_LockRect( struct NineSurface9 *This,
                        D3DLOCKED_RECT *pLockedRect,
                        const RECT *pRect,
@@ -317,6 +330,13 @@ NineSurface9_LockRect( struct NineSurface9 *This,
         nine_D3DLOCK_to_str(Flags));
     NineSurface9_Dump(This);
 
+    /* check if it's already locked */
+    user_assert(This->lock_count == 0, D3DERR_INVALIDCALL);
+
+    /* set pBits to NULL after lock_count check */
+    user_assert(pLockedRect, E_POINTER);
+    pLockedRect->pBits = NULL;
+
 #ifdef NINE_STRICT
     user_assert(This->base.pool != D3DPOOL_DEFAULT ||
                 (resource && (resource->flags & NINE_RESOURCE_FLAG_LOCKABLE)),
@@ -331,19 +351,17 @@ NineSurface9_LockRect( struct NineSurface9 *This,
     user_assert(!((Flags & D3DLOCK_DISCARD) && (Flags & D3DLOCK_READONLY)),
                 D3DERR_INVALIDCALL);
 
-    /* check if it's already locked */
-    user_assert(This->lock_count == 0, D3DERR_INVALIDCALL);
-    user_assert(pLockedRect, E_POINTER);
-
     user_assert(This->desc.MultiSampleType == D3DMULTISAMPLE_NONE,
                 D3DERR_INVALIDCALL);
 
-    if (pRect && This->base.pool == D3DPOOL_DEFAULT &&
+    if (pRect && This->desc.Pool == D3DPOOL_DEFAULT &&
         util_format_is_compressed(This->base.info.format)) {
         const unsigned w = util_format_get_blockwidth(This->base.info.format);
         const unsigned h = util_format_get_blockheight(This->base.info.format);
-        user_assert(!(pRect->left % w) && !(pRect->right % w) &&
-                    !(pRect->top % h) && !(pRect->bottom % h),
+        user_assert((pRect->left == 0 && pRect->right == This->desc.Width &&
+                     pRect->top == 0 && pRect->bottom == This->desc.Height) ||
+                    (!(pRect->left % w) && !(pRect->right % w) &&
+                    !(pRect->top % h) && !(pRect->bottom % h)),
                     D3DERR_INVALIDCALL);
     }
 
@@ -357,13 +375,9 @@ NineSurface9_LockRect( struct NineSurface9 *This,
         usage |= PIPE_TRANSFER_DONTBLOCK;
 
     if (pRect) {
+        /* Windows XP accepts invalid locking rectangles, Windows 7 rejects
+         * them. Use Windows XP behaviour for now. */
         rect_to_pipe_box(&box, pRect);
-        if (u_box_clip_2d(&box, &box, This->desc.Width,
-                          This->desc.Height) < 0) {
-            DBG("pRect clipped by Width=%u Height=%u\n",
-                This->desc.Width, This->desc.Height);
-            return D3DERR_INVALIDCALL;
-        }
     } else {
         u_box_origin_2d(This->desc.Width, This->desc.Height, &box);
     }
@@ -372,10 +386,19 @@ NineSurface9_LockRect( struct NineSurface9 *This,
 
     if (This->data) {
         DBG("returning system memory\n");
-
-        pLockedRect->Pitch = This->stride;
-        pLockedRect->pBits = NineSurface9_GetSystemMemPointer(This,
-                                                              box.x, box.y);
+        /* ATI1 and ATI2 need special handling, because of d3d9 bug.
+         * We must advertise to the application as if it is uncompressed
+         * and bpp 8, and the app has a workaround to work with the fact
+         * that it is actually compressed. */
+        if (is_ATI1_ATI2(This->base.info.format)) {
+            pLockedRect->Pitch = This->desc.Width;
+            pLockedRect->pBits = This->data + box.y * This->desc.Width + box.x;
+        } else {
+            pLockedRect->Pitch = This->stride;
+            pLockedRect->pBits = NineSurface9_GetSystemMemPointer(This,
+                                                                  box.x,
+                                                                  box.y);
+        }
     } else {
         DBG("mapping pipe_resource %p (level=%u usage=%x)\n",
             resource, This->level, usage);
@@ -394,15 +417,14 @@ NineSurface9_LockRect( struct NineSurface9 *This,
 
     if (!(Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY))) {
         NineSurface9_MarkContainerDirty(This);
-        if (This->base.pool == D3DPOOL_MANAGED)
-            NineSurface9_AddDirtyRect(This, &box);
+        NineSurface9_AddDirtyRect(This, &box);
     }
 
     ++This->lock_count;
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineSurface9_UnlockRect( struct NineSurface9 *This )
 {
     DBG("This=%p lock_count=%u\n", This, This->lock_count);
@@ -415,14 +437,14 @@ NineSurface9_UnlockRect( struct NineSurface9 *This )
     return D3D_OK;
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineSurface9_GetDC( struct NineSurface9 *This,
                     HDC *phdc )
 {
     STUB(D3DERR_INVALIDCALL);
 }
 
-HRESULT WINAPI
+HRESULT NINE_WINAPI
 NineSurface9_ReleaseDC( struct NineSurface9 *This,
                         HDC hdc )
 {
@@ -449,161 +471,125 @@ IDirect3DSurface9Vtbl NineSurface9_vtable = {
     (void *)NineSurface9_ReleaseDC
 };
 
-
-static INLINE boolean
-NineSurface9_IsDirty(struct NineSurface9 *This)
-{
-    return This->dirty_rects[0].x1 != 0;
-}
-
-HRESULT
-NineSurface9_CopySurface( struct NineSurface9 *This,
-                          struct NineSurface9 *From,
-                          const POINT *pDestPoint,
-                          const RECT *pSourceRect )
+/* When this function is called, we have already checked
+ * The copy regions fit the surfaces */
+void
+NineSurface9_CopyMemToDefault( struct NineSurface9 *This,
+                               struct NineSurface9 *From,
+                               const POINT *pDestPoint,
+                               const RECT *pSourceRect )
 {
     struct pipe_context *pipe = This->pipe;
     struct pipe_resource *r_dst = This->base.resource;
-    struct pipe_resource *r_src = From->base.resource;
-    struct pipe_transfer *transfer;
-    struct pipe_box src_box;
     struct pipe_box dst_box;
-    uint8_t *p_dst;
     const uint8_t *p_src;
+    int src_x, src_y, dst_x, dst_y, copy_width, copy_height;
 
-    user_assert(This->desc.Format == From->desc.Format, D3DERR_INVALIDCALL);
-
-    dst_box.x = pDestPoint ? pDestPoint->x : 0;
-    dst_box.y = pDestPoint ? pDestPoint->y : 0;
-
-    user_assert(dst_box.x >= 0 &&
-                dst_box.y >= 0, D3DERR_INVALIDCALL);
-
-    dst_box.z = This->layer;
-    src_box.z = From->layer;
+    assert(This->base.pool == D3DPOOL_DEFAULT &&
+           From->base.pool == D3DPOOL_SYSTEMMEM);
 
-    dst_box.depth = 1;
-    src_box.depth = 1;
+    if (pDestPoint) {
+        dst_x = pDestPoint->x;
+        dst_y = pDestPoint->y;
+    } else {
+        dst_x = 0;
+        dst_y = 0;
+    }
 
     if (pSourceRect) {
-        /* make sure it doesn't range outside the source surface */
-        user_assert(pSourceRect->left >= 0 &&
-                    pSourceRect->right <= From->desc.Width &&
-                    pSourceRect->top >= 0 &&
-                    pSourceRect->bottom <= From->desc.Height,
-                    D3DERR_INVALIDCALL);
-        if (rect_to_pipe_box_xy_only_clamp(&src_box, pSourceRect))
-            return D3D_OK;
+        src_x = pSourceRect->left;
+        src_y = pSourceRect->top;
+        copy_width = pSourceRect->right - pSourceRect->left;
+        copy_height = pSourceRect->bottom - pSourceRect->top;
     } else {
-        src_box.x = 0;
-        src_box.y = 0;
-        src_box.width = From->desc.Width;
-        src_box.height = From->desc.Height;
+        src_x = 0;
+        src_y = 0;
+        copy_width = From->desc.Width;
+        copy_height = From->desc.Height;
     }
 
-    /* limits */
-    dst_box.width = This->desc.Width - dst_box.x;
-    dst_box.height = This->desc.Height - dst_box.y;
+    u_box_2d_zslice(dst_x, dst_y, This->layer,
+                    copy_width, copy_height, &dst_box);
 
-    user_assert(src_box.width <= dst_box.width &&
-                src_box.height <= dst_box.height, D3DERR_INVALIDCALL);
+    p_src = NineSurface9_GetSystemMemPointer(From, src_x, src_y);
 
-    dst_box.width = src_box.width;
-    dst_box.height = src_box.height;
+    pipe->transfer_inline_write(pipe, r_dst, This->level,
+                                0, /* WRITE|DISCARD are implicit */
+                                &dst_box, p_src, From->stride, 0);
 
-    /* Don't copy to device memory of managed resources.
-     * We don't want to download it back again later.
-     */
-    if (This->base.pool == D3DPOOL_MANAGED)
-        r_dst = NULL;
+    NineSurface9_MarkContainerDirty(This);
+}
 
-    /* Don't copy from stale device memory of managed resources.
-     * Also, don't copy between system and device if we don't have to.
-     */
-    if (From->base.pool == D3DPOOL_MANAGED) {
-        if (!r_dst || NineSurface9_IsDirty(From))
-            r_src = NULL;
-    }
+void
+NineSurface9_CopyDefaultToMem( struct NineSurface9 *This,
+                               struct NineSurface9 *From )
+{
+    struct pipe_context *pipe = This->pipe;
+    struct pipe_resource *r_src = From->base.resource;
+    struct pipe_transfer *transfer;
+    struct pipe_box src_box;
+    uint8_t *p_dst;
+    const uint8_t *p_src;
 
-    if (r_dst && r_src) {
-        pipe->resource_copy_region(pipe,
-                                   r_dst, This->level,
-                                   dst_box.x, dst_box.y, dst_box.z,
-                                   r_src, From->level,
-                                   &src_box);
-    } else
-    if (r_dst) {
-        p_src = NineSurface9_GetSystemMemPointer(From, src_box.x, src_box.y);
-
-        pipe->transfer_inline_write(pipe, r_dst, This->level,
-                                    0, /* WRITE|DISCARD are implicit */
-                                    &dst_box, p_src, From->stride, 0);
-    } else
-    if (r_src) {
-        p_dst = NineSurface9_GetSystemMemPointer(This, 0, 0);
-
-        p_src = pipe->transfer_map(pipe, r_src, From->level,
-                                   PIPE_TRANSFER_READ,
-                                   &src_box, &transfer);
-        if (!p_src)
-            return D3DERR_DRIVERINTERNALERROR;
-
-        util_copy_rect(p_dst, This->base.info.format,
-                       This->stride, dst_box.x, dst_box.y,
-                       dst_box.width, dst_box.height,
-                       p_src,
-                       transfer->stride, src_box.x, src_box.y);
-
-        pipe->transfer_unmap(pipe, transfer);
-    } else {
-        p_dst = NineSurface9_GetSystemMemPointer(This, 0, 0);
-        p_src = NineSurface9_GetSystemMemPointer(From, 0, 0);
-
-        util_copy_rect(p_dst, This->base.info.format,
-                       This->stride, dst_box.x, dst_box.y,
-                       dst_box.width, dst_box.height,
-                       p_src,
-                       From->stride, src_box.x, src_box.y);
-    }
+    assert(This->base.pool == D3DPOOL_SYSTEMMEM &&
+           From->base.pool == D3DPOOL_DEFAULT);
 
-    if (This->base.pool == D3DPOOL_DEFAULT ||
-        This->base.pool == D3DPOOL_MANAGED)
-        NineSurface9_MarkContainerDirty(This);
-    if (!r_dst && This->base.resource)
-        NineSurface9_AddDirtyRect(This, &dst_box);
+    assert(This->desc.Width == From->desc.Width);
+    assert(This->desc.Height == From->desc.Height);
 
-    return D3D_OK;
+    u_box_origin_2d(This->desc.Width, This->desc.Height, &src_box);
+    src_box.z = From->layer;
+
+    p_src = pipe->transfer_map(pipe, r_src, From->level,
+                               PIPE_TRANSFER_READ,
+                               &src_box, &transfer);
+    p_dst = NineSurface9_GetSystemMemPointer(This, 0, 0);
+
+    assert (p_src && p_dst);
+
+    util_copy_rect(p_dst, This->base.info.format,
+                   This->stride, 0, 0,
+                   This->desc.Width, This->desc.Height,
+                   p_src,
+                   transfer->stride, 0, 0);
+
+    pipe->transfer_unmap(pipe, transfer);
 }
 
+
 /* Gladly, rendering to a MANAGED surface is not permitted, so we will
  * never have to do the reverse, i.e. download the surface.
  */
 HRESULT
-NineSurface9_UploadSelf( struct NineSurface9 *This )
+NineSurface9_UploadSelf( struct NineSurface9 *This,
+                         const struct pipe_box *damaged )
 {
     struct pipe_context *pipe = This->pipe;
     struct pipe_resource *res = This->base.resource;
     uint8_t *ptr;
-    unsigned i;
+    struct pipe_box box;
 
-    assert(This->base.pool == D3DPOOL_MANAGED);
+    DBG("This=%p damaged=%p\n", This, damaged);
 
-    if (!NineSurface9_IsDirty(This))
-        return D3D_OK;
+    assert(This->base.pool == D3DPOOL_MANAGED);
 
-    for (i = 0; i < Elements(This->dirty_rects); ++i) {
-        struct pipe_box box;
-        nine_u_rect_to_pipe_box(&box, &This->dirty_rects[i], This->layer);
+    if (damaged) {
+        box = *damaged;
+        box.z = This->layer;
+        box.depth = 1;
+    } else {
+        box.x = 0;
+        box.y = 0;
+        box.z = This->layer;
+        box.width = This->desc.Width;
+        box.height = This->desc.Height;
+        box.depth = 1;
+    }
 
-        if (box.width == 0)
-            break;
-        ptr = NineSurface9_GetSystemMemPointer(This, box.x, box.y);
+    ptr = NineSurface9_GetSystemMemPointer(This, box.x, box.y);
 
-        pipe->transfer_inline_write(pipe, res, This->level,
-                                    0,
-                                    &box, ptr, This->stride, 0);
-    }
-    NineSurface9_ClearDirtyRects(This);
+    pipe->transfer_inline_write(pipe, res, This->level, 0,
+                                &box, ptr, This->stride, 0);
 
     return D3D_OK;
 }
@@ -621,10 +607,10 @@ NineSurface9_SetResourceResize( struct NineSurface9 *This,
 
     This->desc.Width = This->base.info.width0 = resource->width0;
     This->desc.Height = This->base.info.height0 = resource->height0;
+    This->desc.MultiSampleType = This->base.info.nr_samples = resource->nr_samples;
 
-    This->stride = util_format_get_stride(This->base.info.format,
+    This->stride = nine_format_get_stride(This->base.info.format,
                                           This->desc.Width);
-    This->stride = align(This->stride, 4);
 
     pipe_surface_reference(&This->surface[0], NULL);
     pipe_surface_reference(&This->surface[1], NULL);