wined3d: Add a function for allocating aligned resource memory.

This commit is contained in:
Stefan Dösinger 2013-08-21 15:15:48 +02:00 committed by Alexandre Julliard
parent 9d75a517c2
commit 4dd4a6913b
5 changed files with 93 additions and 69 deletions

View File

@ -3,7 +3,7 @@
* Copyright 2002-2005 Raphael Junqueira * Copyright 2002-2005 Raphael Junqueira
* Copyright 2004 Christian Costa * Copyright 2004 Christian Costa
* Copyright 2005 Oliver Stieber * Copyright 2005 Oliver Stieber
* Copyright 2007-2010 Stefan Dösinger for CodeWeavers * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
* Copyright 2009-2010 Henri Verbeet for CodeWeavers * Copyright 2009-2010 Henri Verbeet for CodeWeavers
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -189,9 +189,9 @@ static void buffer_create_buffer_object(struct wined3d_buffer *This, const struc
} }
else else
{ {
HeapFree(GetProcessHeap(), 0, This->resource.heapMemory); wined3d_resource_free_sysmem(This->resource.heap_memory);
This->resource.allocatedMemory = NULL; This->resource.allocatedMemory = NULL;
This->resource.heapMemory = NULL; This->resource.heap_memory = NULL;
} }
return; return;
@ -492,8 +492,8 @@ BYTE *buffer_get_sysmem(struct wined3d_buffer *This, const struct wined3d_gl_inf
/* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */ /* AllocatedMemory exists if the buffer is double buffered or has no buffer object at all */
if(This->resource.allocatedMemory) return This->resource.allocatedMemory; if(This->resource.allocatedMemory) return This->resource.allocatedMemory;
This->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + RESOURCE_ALIGNMENT); This->resource.heap_memory = wined3d_resource_allocate_sysmem(This->resource.size);
This->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1)); This->resource.allocatedMemory = This->resource.heap_memory;
if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB) if (This->buffer_type_hint == GL_ELEMENT_ARRAY_BUFFER_ARB)
device_invalidate_state(This->resource.device, STATE_INDEXBUFFER); device_invalidate_state(This->resource.device, STATE_INDEXBUFFER);

View File

@ -4,6 +4,7 @@
* Copyright 2004 Christian Costa * Copyright 2004 Christian Costa
* Copyright 2005 Oliver Stieber * Copyright 2005 Oliver Stieber
* Copyright 2009-2010 Henri Verbeet for CodeWeavers * Copyright 2009-2010 Henri Verbeet for CodeWeavers
* Copyright 2006-2008, 2013 Stefan Dösinger for CodeWeavers
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -111,8 +112,8 @@ HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *
if (size) if (size)
{ {
resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT); resource->heap_memory = wined3d_resource_allocate_sysmem(size);
if (!resource->heapMemory) if (!resource->heap_memory)
{ {
ERR("Out of memory!\n"); ERR("Out of memory!\n");
return WINED3DERR_OUTOFVIDEOMEMORY; return WINED3DERR_OUTOFVIDEOMEMORY;
@ -120,10 +121,9 @@ HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *
} }
else else
{ {
resource->heapMemory = NULL; resource->heap_memory = NULL;
} }
resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory resource->allocatedMemory = resource->heap_memory;
+ (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
/* Check that we have enough video ram left */ /* Check that we have enough video ram left */
if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING) if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
@ -131,7 +131,7 @@ HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *
if (size > wined3d_device_get_available_texture_mem(device)) if (size > wined3d_device_get_available_texture_mem(device))
{ {
ERR("Out of adapter memory\n"); ERR("Out of adapter memory\n");
HeapFree(GetProcessHeap(), 0, resource->heapMemory); wined3d_resource_free_sysmem(resource->heap_memory);
return WINED3DERR_OUTOFVIDEOMEMORY; return WINED3DERR_OUTOFVIDEOMEMORY;
} }
adapter_adjust_memory(device->adapter, size); adapter_adjust_memory(device->adapter, size);
@ -165,9 +165,9 @@ void resource_cleanup(struct wined3d_resource *resource)
ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr); ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
} }
HeapFree(GetProcessHeap(), 0, resource->heapMemory); wined3d_resource_free_sysmem(resource->heap_memory);
resource->allocatedMemory = 0; resource->allocatedMemory = NULL;
resource->heapMemory = 0; resource->heap_memory = NULL;
device_resource_released(resource->device, resource); device_resource_released(resource->device, resource);
} }
@ -335,3 +335,28 @@ void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, st
desc->depth = resource->depth; desc->depth = resource->depth;
desc->size = resource->size; desc->size = resource->size;
} }
void *wined3d_resource_allocate_sysmem(SIZE_T size)
{
void **p;
SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
void *mem;
if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + align)))
return NULL;
p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
*p = mem;
return ++p;
}
void wined3d_resource_free_sysmem(void *mem)
{
void **p = mem;
if (!mem)
return;
HeapFree(GetProcessHeap(), 0, *(--p));
}

View File

@ -6,7 +6,7 @@
* Copyright 2002-2003 Raphael Junqueira * Copyright 2002-2003 Raphael Junqueira
* Copyright 2004 Christian Costa * Copyright 2004 Christian Costa
* Copyright 2005 Oliver Stieber * Copyright 2005 Oliver Stieber
* Copyright 2006-2011 Stefan Dösinger for CodeWeavers * Copyright 2006-2011, 2013 Stefan Dösinger for CodeWeavers
* Copyright 2007-2008 Henri Verbeet * Copyright 2007-2008 Henri Verbeet
* Copyright 2006-2008 Roderick Colenbrander * Copyright 2006-2008 Roderick Colenbrander
* Copyright 2009-2011 Henri Verbeet for CodeWeavers * Copyright 2009-2011 Henri Verbeet for CodeWeavers
@ -572,8 +572,8 @@ static void surface_load_pbo(struct wined3d_surface *surface, const struct wined
/* We don't need the system memory anymore and we can't even use it for PBOs. */ /* We don't need the system memory anymore and we can't even use it for PBOs. */
if (!(surface->flags & SFLAG_CLIENT)) if (!(surface->flags & SFLAG_CLIENT))
{ {
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
} }
surface->resource.allocatedMemory = NULL; surface->resource.allocatedMemory = NULL;
surface->flags |= SFLAG_PBO; surface->flags |= SFLAG_PBO;
@ -592,11 +592,10 @@ static void surface_prepare_system_memory(struct wined3d_surface *surface)
{ {
/* Whatever surface we have, make sure that there is memory allocated /* Whatever surface we have, make sure that there is memory allocated
* for the downloaded copy, or a PBO to map. */ * for the downloaded copy, or a PBO to map. */
if (!surface->resource.heapMemory) if (!surface->resource.heap_memory)
surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), 0, surface->resource.size + RESOURCE_ALIGNMENT); surface->resource.heap_memory = wined3d_resource_allocate_sysmem(surface->resource.size);
surface->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)surface->resource.heapMemory surface->resource.allocatedMemory = surface->resource.heap_memory;
+ (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
if (surface->flags & SFLAG_INSYSMEM) if (surface->flags & SFLAG_INSYSMEM)
ERR("Surface without memory or PBO has SFLAG_INSYSMEM set.\n"); ERR("Surface without memory or PBO has SFLAG_INSYSMEM set.\n");
@ -608,9 +607,9 @@ static void surface_evict_sysmem(struct wined3d_surface *surface)
if (surface->resource.map_count || (surface->flags & SFLAG_DONOTFREE)) if (surface->resource.map_count || (surface->flags & SFLAG_DONOTFREE))
return; return;
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.allocatedMemory = NULL; surface->resource.allocatedMemory = NULL;
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
surface_modify_location(surface, SFLAG_INSYSMEM, FALSE); surface_modify_location(surface, SFLAG_INSYSMEM, FALSE);
} }
@ -1778,14 +1777,13 @@ static void surface_remove_pbo(struct wined3d_surface *surface, const struct win
} }
else else
{ {
if (!surface->resource.heapMemory) if (!surface->resource.heap_memory)
surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), 0, surface->resource.size + RESOURCE_ALIGNMENT); surface->resource.heap_memory = wined3d_resource_allocate_sysmem(surface->resource.size);
else if (!(surface->flags & SFLAG_CLIENT)) else if (!(surface->flags & SFLAG_CLIENT))
ERR("Surface %p has heapMemory %p and flags %#x.\n", ERR("Surface %p has heap_memory %p and flags %#x.\n",
surface, surface->resource.heapMemory, surface->flags); surface, surface->resource.heap_memory, surface->flags);
surface->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)surface->resource.heapMemory surface->resource.allocatedMemory = surface->resource.heap_memory;
+ (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
} }
GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo)); GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo));
@ -1804,10 +1802,10 @@ static BOOL surface_init_sysmem(struct wined3d_surface *surface)
{ {
if (!surface->resource.allocatedMemory) if (!surface->resource.allocatedMemory)
{ {
if (!surface->resource.heapMemory) if (!surface->resource.heap_memory)
{ {
if (!(surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, surface->resource.heap_memory = wined3d_resource_allocate_sysmem(surface->resource.size);
surface->resource.size + RESOURCE_ALIGNMENT))) if (!surface->resource.heap_memory)
{ {
ERR("Failed to allocate memory.\n"); ERR("Failed to allocate memory.\n");
return FALSE; return FALSE;
@ -1815,12 +1813,11 @@ static BOOL surface_init_sysmem(struct wined3d_surface *surface)
} }
else if (!(surface->flags & SFLAG_CLIENT)) else if (!(surface->flags & SFLAG_CLIENT))
{ {
ERR("Surface %p has heapMemory %p and flags %#x.\n", ERR("Surface %p has heap_memory %p and flags %#x.\n",
surface, surface->resource.heapMemory, surface->flags); surface, surface->resource.heap_memory, surface->flags);
} }
surface->resource.allocatedMemory = surface->resource.allocatedMemory = surface->resource.heap_memory;
(BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
} }
else else
{ {
@ -1963,8 +1960,8 @@ static HRESULT gdi_surface_private_setup(struct wined3d_surface *surface)
hr = surface_create_dib_section(surface); hr = surface_create_dib_section(surface);
if (SUCCEEDED(hr)) if (SUCCEEDED(hr))
{ {
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
surface->resource.allocatedMemory = surface->dib.bitmap_data; surface->resource.allocatedMemory = surface->dib.bitmap_data;
} }
@ -2023,8 +2020,8 @@ static void gdi_surface_map(struct wined3d_surface *surface, const RECT *rect, D
ERR("Failed to create dib section, hr %#x.\n", hr); ERR("Failed to create dib section, hr %#x.\n", hr);
return; return;
} }
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
surface->resource.allocatedMemory = surface->dib.bitmap_data; surface->resource.allocatedMemory = surface->dib.bitmap_data;
} }
} }
@ -2691,9 +2688,8 @@ static void surface_allocate_surface(struct wined3d_surface *surface, const stru
/* Point OpenGL to our allocated texture memory. Do not use /* Point OpenGL to our allocated texture memory. Do not use
* resource.allocatedMemory here because it might point into a * resource.allocatedMemory here because it might point into a
* PBO. Instead use heapMemory, but get the alignment right. */ * PBO. Instead use heap_memory. */
mem = (BYTE *)(((ULONG_PTR)surface->resource.heapMemory mem = surface->resource.heap_memory;
+ (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE); gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)"); checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
@ -3255,8 +3251,8 @@ HRESULT CDECL wined3d_surface_set_mem(struct wined3d_surface *surface, void *mem
} }
else if (!(surface->flags & SFLAG_USERPTR)) else if (!(surface->flags & SFLAG_USERPTR))
{ {
release = surface->resource.heapMemory; release = surface->resource.heap_memory;
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
} }
surface->resource.allocatedMemory = mem; surface->resource.allocatedMemory = mem;
surface->flags |= SFLAG_USERPTR; surface->flags |= SFLAG_USERPTR;
@ -3269,12 +3265,12 @@ HRESULT CDECL wined3d_surface_set_mem(struct wined3d_surface *surface, void *mem
surface_release_client_storage(surface); surface_release_client_storage(surface);
/* Now free the old memory if any. */ /* Now free the old memory if any. */
HeapFree(GetProcessHeap(), 0, release); wined3d_resource_free_sysmem(release);
} }
else if (surface->flags & SFLAG_USERPTR) else if (surface->flags & SFLAG_USERPTR)
{ {
/* HeapMemory should be NULL already. */ /* heap_memory should be NULL already. */
if (surface->resource.heapMemory) if (surface->resource.heap_memory)
ERR("User pointer surface has heap memory allocated.\n"); ERR("User pointer surface has heap memory allocated.\n");
if (!mem) if (!mem)
@ -3458,8 +3454,8 @@ HRESULT CDECL wined3d_surface_update_desc(struct wined3d_surface *surface,
surface->flags &= ~(SFLAG_LOCATIONS | SFLAG_USERPTR); surface->flags &= ~(SFLAG_LOCATIONS | SFLAG_USERPTR);
surface->resource.allocatedMemory = NULL; surface->resource.allocatedMemory = NULL;
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
surface->resource.width = width; surface->resource.width = width;
surface->resource.height = height; surface->resource.height = height;
@ -3955,8 +3951,8 @@ HRESULT CDECL wined3d_surface_getdc(struct wined3d_surface *surface, HDC *dc)
/* Use the DIB section from now on if we are not using a PBO. */ /* Use the DIB section from now on if we are not using a PBO. */
if (!(surface->flags & (SFLAG_PBO | SFLAG_PIN_SYSMEM))) if (!(surface->flags & (SFLAG_PBO | SFLAG_PIN_SYSMEM)))
{ {
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
surface->resource.allocatedMemory = surface->dib.bitmap_data; surface->resource.allocatedMemory = surface->dib.bitmap_data;
} }
} }
@ -4785,9 +4781,9 @@ void flip_surface(struct wined3d_surface *front, struct wined3d_surface *back)
front->resource.allocatedMemory = back->resource.allocatedMemory; front->resource.allocatedMemory = back->resource.allocatedMemory;
back->resource.allocatedMemory = tmp; back->resource.allocatedMemory = tmp;
tmp = front->resource.heapMemory; tmp = front->resource.heap_memory;
front->resource.heapMemory = back->resource.heapMemory; front->resource.heap_memory = back->resource.heap_memory;
back->resource.heapMemory = tmp; back->resource.heap_memory = tmp;
} }
/* Flip the PBO */ /* Flip the PBO */
@ -5456,9 +5452,9 @@ static HRESULT IWineD3DSurfaceImpl_BltOverride(struct wined3d_surface *dst_surfa
if (!dst_surface->resource.map_count && !(dst_surface->flags & SFLAG_DONOTFREE)) if (!dst_surface->resource.map_count && !(dst_surface->flags & SFLAG_DONOTFREE))
{ {
HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory); wined3d_resource_free_sysmem(dst_surface->resource.heap_memory);
dst_surface->resource.allocatedMemory = NULL; dst_surface->resource.allocatedMemory = NULL;
dst_surface->resource.heapMemory = NULL; dst_surface->resource.heap_memory = NULL;
} }
else else
{ {
@ -7224,8 +7220,8 @@ static HRESULT surface_init(struct wined3d_surface *surface, UINT alignment, UIN
if ((usage & WINED3DUSAGE_OWNDC) && !surface->hDC if ((usage & WINED3DUSAGE_OWNDC) && !surface->hDC
&& SUCCEEDED(surface_create_dib_section(surface))) && SUCCEEDED(surface_create_dib_section(surface)))
{ {
HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory); wined3d_resource_free_sysmem(surface->resource.heap_memory);
surface->resource.heapMemory = NULL; surface->resource.heap_memory = NULL;
surface->resource.allocatedMemory = surface->dib.bitmap_data; surface->resource.allocatedMemory = surface->dib.bitmap_data;
} }

View File

@ -737,10 +737,10 @@ static void swapchain_gdi_present(struct wined3d_swapchain *swapchain, const REC
front->resource.allocatedMemory = back->resource.allocatedMemory; front->resource.allocatedMemory = back->resource.allocatedMemory;
back->resource.allocatedMemory = tmp; back->resource.allocatedMemory = tmp;
if (front->resource.heapMemory) if (front->resource.heap_memory)
ERR("GDI Surface %p has heap memory allocated.\n", front); ERR("GDI Surface %p has heap memory allocated.\n", front);
if (back->resource.heapMemory) if (back->resource.heap_memory)
ERR("GDI Surface %p has heap memory allocated.\n", back); ERR("GDI Surface %p has heap memory allocated.\n", back);
} }

View File

@ -1926,6 +1926,9 @@ struct wined3d_resource_ops
void (*resource_unload)(struct wined3d_resource *resource); void (*resource_unload)(struct wined3d_resource *resource);
}; };
void *wined3d_resource_allocate_sysmem(SIZE_T size) DECLSPEC_HIDDEN;
void wined3d_resource_free_sysmem(void *mem) DECLSPEC_HIDDEN;
struct wined3d_resource struct wined3d_resource
{ {
LONG ref; LONG ref;
@ -1935,19 +1938,19 @@ struct wined3d_resource
enum wined3d_resource_type type; enum wined3d_resource_type type;
const struct wined3d_format *format; const struct wined3d_format *format;
enum wined3d_multisample_type multisample_type; enum wined3d_multisample_type multisample_type;
UINT multisample_quality; UINT multisample_quality;
DWORD usage; DWORD usage;
enum wined3d_pool pool; enum wined3d_pool pool;
DWORD access_flags; DWORD access_flags;
UINT width; UINT width;
UINT height; UINT height;
UINT depth; UINT depth;
UINT size; UINT size;
DWORD priority; DWORD priority;
BYTE *allocatedMemory; /* Pointer to the real data location */ BYTE *allocatedMemory; /* Pointer to the real data location */
BYTE *heapMemory; /* Pointer to the HeapAlloced block of memory */ void *heap_memory;
struct list privateData; struct list privateData;
struct list resource_list_entry; struct list resource_list_entry;
void *parent; void *parent;
const struct wined3d_parent_ops *parent_ops; const struct wined3d_parent_ops *parent_ops;