2 * IWineD3DResource Implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
8 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
35 DWORD flags; /* DDSPD_* */
46 static DWORD resource_access_from_pool(WINED3DPOOL pool)
50 case WINED3DPOOL_DEFAULT:
51 return WINED3D_RESOURCE_ACCESS_GPU;
53 case WINED3DPOOL_MANAGED:
54 return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;
56 case WINED3DPOOL_SYSTEMMEM:
57 return WINED3D_RESOURCE_ACCESS_CPU;
59 case WINED3DPOOL_SCRATCH:
60 return WINED3D_RESOURCE_ACCESS_SCRATCH;
63 FIXME("Unhandled pool %#x.\n", pool);
68 static void resource_check_usage(DWORD usage)
70 static const DWORD handled = WINED3DUSAGE_RENDERTARGET
71 | WINED3DUSAGE_DEPTHSTENCIL
72 | WINED3DUSAGE_DYNAMIC
73 | WINED3DUSAGE_AUTOGENMIPMAP
74 | WINED3DUSAGE_STATICDECL
75 | WINED3DUSAGE_OVERLAY;
78 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
81 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
82 WINED3DRESOURCETYPE resource_type, const struct wined3d_format *format,
83 WINED3DMULTISAMPLE_TYPE multisample_type, UINT multisample_quality,
84 DWORD usage, WINED3DPOOL pool, UINT width, UINT height, UINT depth, UINT size,
85 void *parent, const struct wined3d_parent_ops *parent_ops,
86 const struct wined3d_resource_ops *resource_ops)
89 resource->device = device;
90 resource->resourceType = resource_type;
91 resource->format = format;
92 resource->multisample_type = multisample_type;
93 resource->multisample_quality = multisample_quality;
94 resource->usage = usage;
95 resource->pool = pool;
96 resource->access_flags = resource_access_from_pool(pool);
97 if (usage & WINED3DUSAGE_DYNAMIC)
98 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
99 resource->width = width;
100 resource->height = height;
101 resource->depth = depth;
102 resource->size = size;
103 resource->priority = 0;
104 resource->parent = parent;
105 resource->parent_ops = parent_ops;
106 resource->resource_ops = resource_ops;
107 list_init(&resource->privateData);
109 resource_check_usage(usage);
113 resource->heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + RESOURCE_ALIGNMENT);
114 if (!resource->heapMemory)
116 ERR("Out of memory!\n");
117 return WINED3DERR_OUTOFVIDEOMEMORY;
122 resource->heapMemory = NULL;
124 resource->allocatedMemory = (BYTE *)(((ULONG_PTR)resource->heapMemory
125 + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
127 /* Check that we have enough video ram left */
128 if (pool == WINED3DPOOL_DEFAULT)
130 if (size > wined3d_device_get_available_texture_mem(device))
132 ERR("Out of adapter memory\n");
133 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
134 return WINED3DERR_OUTOFVIDEOMEMORY;
136 WineD3DAdapterChangeGLRam(device, size);
139 device_resource_add(device, resource);
144 void resource_cleanup(struct wined3d_resource *resource)
146 struct private_data *data;
147 struct list *e1, *e2;
150 TRACE("Cleaning up resource %p.\n", resource);
152 if (resource->pool == WINED3DPOOL_DEFAULT)
154 TRACE("Decrementing device memory pool by %u.\n", resource->size);
155 WineD3DAdapterChangeGLRam(resource->device, 0 - resource->size);
158 LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
160 data = LIST_ENTRY(e1, struct private_data, entry);
161 hr = wined3d_resource_free_private_data(resource, &data->tag);
163 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
166 HeapFree(GetProcessHeap(), 0, resource->heapMemory);
167 resource->allocatedMemory = 0;
168 resource->heapMemory = 0;
170 if (resource->device)
171 device_resource_released(resource->device, resource);
174 void resource_unload(struct wined3d_resource *resource)
176 context_resource_unloaded(resource->device,
177 resource, resource->resourceType);
180 static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
182 struct private_data *data;
185 TRACE("Searching for private data %s\n", debugstr_guid(tag));
186 LIST_FOR_EACH(entry, &resource->privateData)
188 data = LIST_ENTRY(entry, struct private_data, entry);
189 if (IsEqualGUID(&data->tag, tag)) {
190 TRACE("Found %p\n", data);
194 TRACE("Not found\n");
198 HRESULT CDECL wined3d_resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
199 const void *data, DWORD data_size, DWORD flags)
201 struct private_data *d;
203 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
204 resource, debugstr_guid(guid), data, data_size, flags);
206 wined3d_resource_free_private_data(resource, guid);
208 d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
209 if (!d) return E_OUTOFMEMORY;
214 if (flags & WINED3DSPD_IUNKNOWN)
216 if (data_size != sizeof(IUnknown *))
218 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
219 HeapFree(GetProcessHeap(), 0, d);
220 return WINED3DERR_INVALIDCALL;
222 d->ptr.object = (IUnknown *)data;
223 d->size = sizeof(IUnknown *);
224 IUnknown_AddRef(d->ptr.object);
228 d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
231 HeapFree(GetProcessHeap(), 0, d);
232 return E_OUTOFMEMORY;
235 memcpy(d->ptr.data, data, data_size);
237 list_add_tail(&resource->privateData, &d->entry);
242 HRESULT CDECL wined3d_resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid,
243 void *data, DWORD *data_size)
245 const struct private_data *d;
247 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
248 resource, debugstr_guid(guid), data, data_size);
250 d = resource_find_private_data(resource, guid);
251 if (!d) return WINED3DERR_NOTFOUND;
253 if (*data_size < d->size)
255 *data_size = d->size;
256 return WINED3DERR_MOREDATA;
259 if (d->flags & WINED3DSPD_IUNKNOWN)
261 *(IUnknown **)data = d->ptr.object;
262 if (resource->device->wined3d->dxVersion != 7)
264 /* D3D8 and D3D9 addref the private data, DDraw does not. This
265 * can't be handled in ddraw because it doesn't know if the
266 * pointer returned is an IUnknown * or just a blob. */
267 IUnknown_AddRef(d->ptr.object);
272 memcpy(data, d->ptr.data, d->size);
277 HRESULT CDECL wined3d_resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
279 struct private_data *data;
281 TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
283 data = resource_find_private_data(resource, guid);
284 if (!data) return WINED3DERR_NOTFOUND;
286 if (data->flags & WINED3DSPD_IUNKNOWN)
288 if (data->ptr.object)
289 IUnknown_Release(data->ptr.object);
293 HeapFree(GetProcessHeap(), 0, data->ptr.data);
295 list_remove(&data->entry);
297 HeapFree(GetProcessHeap(), 0, data);
302 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
304 DWORD prev = resource->priority;
305 resource->priority = priority;
306 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
310 DWORD resource_get_priority(const struct wined3d_resource *resource)
312 TRACE("resource %p, returning %u.\n", resource, resource->priority);
313 return resource->priority;
316 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
318 return resource->parent;
321 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
323 desc->resource_type = resource->resourceType;
324 desc->format = resource->format->id;
325 desc->multisample_type = resource->multisample_type;
326 desc->multisample_quality = resource->multisample_quality;
327 desc->usage = resource->usage;
328 desc->pool = resource->pool;
329 desc->width = resource->width;
330 desc->height = resource->height;
331 desc->depth = resource->depth;
332 desc->size = resource->size;