From 5ec237841e439cace6e1d5978552794692c43fd6 Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Sun, 21 Feb 2016 23:34:42 +0100
Subject: Revert "wined3d: Handle slice pitch and alignment as well in
 wined3d_format_calculate_pitch()."

This reverts commit a0beaa4006b0261587204fdecc08ba0bd941567d.
---
 dlls/wined3d/directx.c         |  6 +-----
 dlls/wined3d/surface.c         | 29 +++++++++++++------------
 dlls/wined3d/texture.c         | 25 +++++++++++++++++++--
 dlls/wined3d/utils.c           | 49 +++++++++++++++++++++---------------------
 dlls/wined3d/wined3d_private.h |  3 +--
 5 files changed, 64 insertions(+), 48 deletions(-)

diff --git a/dlls/wined3d/directx.c b/dlls/wined3d/directx.c
index 5f995ce..329da15 100644
--- a/dlls/wined3d/directx.c
+++ b/dlls/wined3d/directx.c
@@ -4756,7 +4756,6 @@ UINT CDECL wined3d_calculate_format_pitch(const struct wined3d *wined3d, UINT ad
         enum wined3d_format_id format_id, UINT width)
 {
     const struct wined3d_gl_info *gl_info;
-    unsigned int row_pitch, slice_pitch;
 
     TRACE("wined3d %p, adapter_idx %u, format_id %s, width %u.\n",
             wined3d, adapter_idx, debug_d3dformat(format_id), width);
@@ -4765,10 +4764,7 @@ UINT CDECL wined3d_calculate_format_pitch(const struct wined3d *wined3d, UINT ad
         return ~0u;
 
     gl_info = &wined3d->adapters[adapter_idx].gl_info;
-    wined3d_format_calculate_pitch(wined3d_get_format(gl_info, format_id),
-            1, width, 1, &row_pitch, &slice_pitch);
-
-    return row_pitch;
+    return wined3d_format_calculate_pitch(wined3d_get_format(gl_info, format_id), width);
 }
 
 HRESULT CDECL wined3d_check_device_format_conversion(const struct wined3d *wined3d, UINT adapter_idx,
diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index 658b600..82a2f83 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -1917,8 +1917,9 @@ HRESULT wined3d_surface_update_desc(struct wined3d_surface *surface, const struc
     else
     {
         /* User memory surfaces don't have the regular surface alignment. */
-        wined3d_format_calculate_pitch(texture_resource->format, 1, width, height,
-                &surface->container->row_pitch, &surface->resource.size);
+        surface->resource.size = wined3d_format_calculate_size(texture_resource->format,
+                1, width, height, 1);
+        surface->container->row_pitch = wined3d_format_calculate_pitch(texture_resource->format, width);
     }
 
     /* The format might be changed to a format that needs conversion.
@@ -3806,7 +3807,7 @@ static HRESULT surface_load_drawable(struct wined3d_surface *surface,
 static HRESULT surface_load_texture(struct wined3d_surface *surface,
         struct wined3d_context *context, BOOL srgb)
 {
-    unsigned int width, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
+    unsigned int width, src_row_pitch, src_slice_pitch, dst_pitch;
     const RECT src_rect = {0, 0, surface->resource.width, surface->resource.height};
     const struct wined3d_gl_info *gl_info = context->gl_info;
     struct wined3d_device *device = surface->resource.device;
@@ -3926,17 +3927,17 @@ static HRESULT surface_load_texture(struct wined3d_surface *surface,
         UINT height = surface->resource.height;
 
         format.byte_count = format.conv_byte_count;
-        wined3d_format_calculate_pitch(&format, 1, width, height, &dst_row_pitch, &dst_slice_pitch);
+        dst_pitch = wined3d_format_calculate_pitch(&format, width);
 
-        if (!(mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch)))
+        if (!(mem = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height)))
         {
-            ERR("Out of memory (%u).\n", dst_slice_pitch);
+            ERR("Out of memory (%u).\n", dst_pitch * height);
             context_release(context);
             return E_OUTOFMEMORY;
         }
         format.convert(data.addr, mem, src_row_pitch, src_slice_pitch,
-                dst_row_pitch, dst_slice_pitch, width, height, 1);
-        src_row_pitch = dst_row_pitch;
+                dst_pitch, dst_pitch * height, width, height, 1);
+        src_row_pitch = dst_pitch;
         data.addr = mem;
     }
     else if (conversion)
@@ -3945,20 +3946,20 @@ static HRESULT surface_load_texture(struct wined3d_surface *surface,
         struct wined3d_palette *palette = NULL;
         UINT height = surface->resource.height;
 
-        wined3d_format_calculate_pitch(&format, device->surface_alignment,
-                width, height, &dst_row_pitch, &dst_slice_pitch);
+        dst_pitch = wined3d_format_calculate_pitch(&format, width);
+        dst_pitch = (dst_pitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
 
-        if (!(mem = HeapAlloc(GetProcessHeap(), 0, dst_slice_pitch)))
+        if (!(mem = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height)))
         {
-            ERR("Out of memory (%u).\n", dst_slice_pitch);
+            ERR("Out of memory (%u).\n", dst_pitch * height);
             context_release(context);
             return E_OUTOFMEMORY;
         }
         if (texture->swapchain && texture->swapchain->palette)
             palette = texture->swapchain->palette;
-        conversion->convert(data.addr, src_row_pitch, mem, dst_row_pitch,
+        conversion->convert(data.addr, src_row_pitch, mem, dst_pitch,
                 width, height, palette, &texture->async.gl_color_key);
-        src_row_pitch = dst_row_pitch;
+        src_row_pitch = dst_pitch;
         data.addr = mem;
     }
 
diff --git a/dlls/wined3d/texture.c b/dlls/wined3d/texture.c
index 487b2ef..8fe6251 100644
--- a/dlls/wined3d/texture.c
+++ b/dlls/wined3d/texture.c
@@ -496,6 +496,8 @@ void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
         unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
 {
     const struct wined3d_resource *resource = &texture->resource;
+    const struct wined3d_format *format = resource->format;
+    unsigned int alignment = resource->device->surface_alignment;
     unsigned int width = max(1, texture->resource.width >> level);
     unsigned int height = max(1, texture->resource.height >> level);
 
@@ -506,8 +508,27 @@ void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
         return;
     }
 
-    wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
-            width, height, row_pitch, slice_pitch);
+    if (resource->format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BLOCKS)
+    {
+        unsigned int row_block_count = (width + format->block_width - 1) / format->block_width;
+        unsigned int slice_block_count = (height + format->block_height - 1) / format->block_height;
+        *row_pitch = row_block_count * format->block_byte_count;
+        *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
+        *slice_pitch = *row_pitch * slice_block_count;
+    }
+    else
+    {
+        *row_pitch = format->byte_count * width;  /* Bytes / row */
+        *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
+        *slice_pitch = *row_pitch * height;
+    }
+
+    if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
+    {
+        /* The D3D format requirements make sure that the resulting format is an integer again */
+        *slice_pitch *= format->height_scale.numerator;
+        *slice_pitch /= format->height_scale.denominator;
+    }
 }
 
 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
diff --git a/dlls/wined3d/utils.c b/dlls/wined3d/utils.c
index 734b265..0504368 100644
--- a/dlls/wined3d/utils.c
+++ b/dlls/wined3d/utils.c
@@ -3199,47 +3199,46 @@ const struct wined3d_format *wined3d_get_format(const struct wined3d_gl_info *gl
     return &gl_info->formats[idx];
 }
 
-void wined3d_format_calculate_pitch(const struct wined3d_format *format, unsigned int alignment,
-        unsigned int width, unsigned int height, unsigned int *row_pitch, unsigned int *slice_pitch)
+UINT wined3d_format_calculate_pitch(const struct wined3d_format *format, UINT width)
 {
     /* For block based formats, pitch means the amount of bytes to the next
      * row of blocks rather than the next row of pixels. */
     if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BLOCKS)
+        return format->block_byte_count * ((width + format->block_width - 1) / format->block_width);
+
+    return format->byte_count * width;
+}
+
+UINT wined3d_format_calculate_size(const struct wined3d_format *format, UINT alignment,
+        UINT width, UINT height, UINT depth)
+{
+    UINT pitch = wined3d_format_calculate_pitch(format, width);
+    UINT size;
+
+    if (format->id == WINED3DFMT_UNKNOWN)
+    {
+        size = 0;
+    }
+    else if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BLOCKS)
     {
-        unsigned int row_block_count = (width + format->block_width - 1) / format->block_width;
-        unsigned int slice_block_count = (height + format->block_height - 1) / format->block_height;
-        *row_pitch = row_block_count * format->block_byte_count;
-        *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
-        *slice_pitch = *row_pitch * slice_block_count;
+        UINT row_count = (height + format->block_height - 1) / format->block_height;
+        size = row_count * ((pitch + alignment - 1) & ~(alignment - 1));
     }
     else
     {
-        *row_pitch = format->byte_count * width;  /* Bytes / row */
-        *row_pitch = (*row_pitch + alignment - 1) & ~(alignment - 1);
-        *slice_pitch = *row_pitch * height;
+        size = height * ((pitch + alignment - 1) & ~(alignment - 1));
     }
 
     if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
     {
         /* The D3D format requirements make sure that the resulting format is an integer again */
-        *slice_pitch *= format->height_scale.numerator;
-        *slice_pitch /= format->height_scale.denominator;
+        size *= format->height_scale.numerator;
+        size /= format->height_scale.denominator;
     }
 
-    TRACE("Returning row pitch %u, slice pitch %u.\n", *row_pitch, *slice_pitch);
-}
-
-UINT wined3d_format_calculate_size(const struct wined3d_format *format, UINT alignment,
-        UINT width, UINT height, UINT depth)
-{
-    unsigned int row_pitch, slice_pitch;
+    size *= depth;
 
-    if (format->id == WINED3DFMT_UNKNOWN)
-        return 0;
-
-    wined3d_format_calculate_pitch(format, alignment, width, height, &row_pitch, &slice_pitch);
-
-    return slice_pitch * depth;
+    return size;
 }
 
 /*****************************************************************************
diff --git a/dlls/wined3d/wined3d_private.h b/dlls/wined3d/wined3d_private.h
index 05a48fd..40e0f29 100644
--- a/dlls/wined3d/wined3d_private.h
+++ b/dlls/wined3d/wined3d_private.h
@@ -3347,8 +3347,7 @@ struct wined3d_format
 
 const struct wined3d_format *wined3d_get_format(const struct wined3d_gl_info *gl_info,
         enum wined3d_format_id format_id) DECLSPEC_HIDDEN;
-void wined3d_format_calculate_pitch(const struct wined3d_format *format, unsigned int alignment,
-        unsigned int width, unsigned int height, unsigned int *row_pitch, unsigned int *slice_pitch) DECLSPEC_HIDDEN;
+UINT wined3d_format_calculate_pitch(const struct wined3d_format *format, UINT width) DECLSPEC_HIDDEN;
 UINT wined3d_format_calculate_size(const struct wined3d_format *format,
         UINT alignment, UINT width, UINT height, UINT depth) DECLSPEC_HIDDEN;
 DWORD wined3d_format_convert_from_float(const struct wined3d_format *format,
-- 
2.7.1

