2 * Copyright 2002-2004 Jason Edmeades
3 * Copyright 2003-2004 Raphael Junqueira
4 * Copyright 2004 Christian Costa
5 * Copyright 2005 Oliver Stieber
6 * Copyright 2009-2010 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
34 DWORD flags
; /* DDSPD_* */
45 static DWORD
resource_access_from_pool(enum wined3d_pool pool
)
49 case WINED3D_POOL_DEFAULT
:
50 return WINED3D_RESOURCE_ACCESS_GPU
;
52 case WINED3D_POOL_MANAGED
:
53 return WINED3D_RESOURCE_ACCESS_GPU
| WINED3D_RESOURCE_ACCESS_CPU
;
55 case WINED3D_POOL_SYSTEM_MEM
:
56 return WINED3D_RESOURCE_ACCESS_CPU
;
58 case WINED3D_POOL_SCRATCH
:
59 return WINED3D_RESOURCE_ACCESS_SCRATCH
;
62 FIXME("Unhandled pool %#x.\n", pool
);
67 static void resource_check_usage(DWORD usage
)
69 static const DWORD handled
= WINED3DUSAGE_RENDERTARGET
70 | WINED3DUSAGE_DEPTHSTENCIL
71 | WINED3DUSAGE_DYNAMIC
72 | WINED3DUSAGE_AUTOGENMIPMAP
73 | WINED3DUSAGE_STATICDECL
74 | WINED3DUSAGE_OVERLAY
;
77 FIXME("Unhandled usage flags %#x.\n", usage
& ~handled
);
80 HRESULT
resource_init(struct wined3d_resource
*resource
, struct wined3d_device
*device
,
81 enum wined3d_resource_type type
, const struct wined3d_format
*format
,
82 enum wined3d_multisample_type multisample_type
, UINT multisample_quality
,
83 DWORD usage
, enum wined3d_pool pool
, UINT width
, UINT height
, UINT depth
, UINT size
,
84 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
85 const struct wined3d_resource_ops
*resource_ops
)
88 resource
->device
= device
;
89 resource
->type
= type
;
90 resource
->format
= format
;
91 resource
->multisample_type
= multisample_type
;
92 resource
->multisample_quality
= multisample_quality
;
93 resource
->usage
= usage
;
94 resource
->pool
= pool
;
95 resource
->access_flags
= resource_access_from_pool(pool
);
96 if (usage
& WINED3DUSAGE_DYNAMIC
)
97 resource
->access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
98 resource
->width
= width
;
99 resource
->height
= height
;
100 resource
->depth
= depth
;
101 resource
->size
= size
;
102 resource
->priority
= 0;
103 resource
->parent
= parent
;
104 resource
->parent_ops
= parent_ops
;
105 resource
->resource_ops
= resource_ops
;
106 list_init(&resource
->privateData
);
108 resource_check_usage(usage
);
112 resource
->heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ RESOURCE_ALIGNMENT
);
113 if (!resource
->heapMemory
)
115 ERR("Out of memory!\n");
116 return WINED3DERR_OUTOFVIDEOMEMORY
;
121 resource
->heapMemory
= NULL
;
123 resource
->allocatedMemory
= (BYTE
*)(((ULONG_PTR
)resource
->heapMemory
124 + (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
126 /* Check that we have enough video ram left */
127 if (pool
== WINED3D_POOL_DEFAULT
)
129 if (size
> wined3d_device_get_available_texture_mem(device
))
131 ERR("Out of adapter memory\n");
132 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
133 return WINED3DERR_OUTOFVIDEOMEMORY
;
135 adapter_adjust_memory(device
->adapter
, size
);
138 device_resource_add(device
, resource
);
143 void resource_cleanup(struct wined3d_resource
*resource
)
145 struct private_data
*data
;
146 struct list
*e1
, *e2
;
149 TRACE("Cleaning up resource %p.\n", resource
);
151 if (resource
->pool
== WINED3D_POOL_DEFAULT
)
153 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
154 adapter_adjust_memory(resource
->device
->adapter
, 0 - resource
->size
);
157 LIST_FOR_EACH_SAFE(e1
, e2
, &resource
->privateData
)
159 data
= LIST_ENTRY(e1
, struct private_data
, entry
);
160 hr
= wined3d_resource_free_private_data(resource
, &data
->tag
);
162 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource
, hr
);
165 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
166 resource
->allocatedMemory
= 0;
167 resource
->heapMemory
= 0;
169 if (resource
->device
)
170 device_resource_released(resource
->device
, resource
);
173 void resource_unload(struct wined3d_resource
*resource
)
175 if (resource
->map_count
)
176 ERR("Resource %p is being unloaded while mapped.\n", resource
);
178 context_resource_unloaded(resource
->device
,
179 resource
, resource
->type
);
182 static struct private_data
*resource_find_private_data(const struct wined3d_resource
*resource
, REFGUID tag
)
184 struct private_data
*data
;
187 TRACE("Searching for private data %s\n", debugstr_guid(tag
));
188 LIST_FOR_EACH(entry
, &resource
->privateData
)
190 data
= LIST_ENTRY(entry
, struct private_data
, entry
);
191 if (IsEqualGUID(&data
->tag
, tag
)) {
192 TRACE("Found %p\n", data
);
196 TRACE("Not found\n");
200 HRESULT CDECL
wined3d_resource_set_private_data(struct wined3d_resource
*resource
, REFGUID guid
,
201 const void *data
, DWORD data_size
, DWORD flags
)
203 struct private_data
*d
;
205 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
206 resource
, debugstr_guid(guid
), data
, data_size
, flags
);
208 wined3d_resource_free_private_data(resource
, guid
);
210 d
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*d
));
211 if (!d
) return E_OUTOFMEMORY
;
216 if (flags
& WINED3DSPD_IUNKNOWN
)
218 if (data_size
!= sizeof(IUnknown
*))
220 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size
);
221 HeapFree(GetProcessHeap(), 0, d
);
222 return WINED3DERR_INVALIDCALL
;
224 d
->ptr
.object
= (IUnknown
*)data
;
225 d
->size
= sizeof(IUnknown
*);
226 IUnknown_AddRef(d
->ptr
.object
);
230 d
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
233 HeapFree(GetProcessHeap(), 0, d
);
234 return E_OUTOFMEMORY
;
237 memcpy(d
->ptr
.data
, data
, data_size
);
239 list_add_tail(&resource
->privateData
, &d
->entry
);
244 HRESULT CDECL
wined3d_resource_get_private_data(const struct wined3d_resource
*resource
, REFGUID guid
,
245 void *data
, DWORD
*data_size
)
247 const struct private_data
*d
;
249 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
250 resource
, debugstr_guid(guid
), data
, data_size
);
252 d
= resource_find_private_data(resource
, guid
);
253 if (!d
) return WINED3DERR_NOTFOUND
;
255 if (*data_size
< d
->size
)
257 *data_size
= d
->size
;
258 return WINED3DERR_MOREDATA
;
261 if (d
->flags
& WINED3DSPD_IUNKNOWN
)
263 *(IUnknown
**)data
= d
->ptr
.object
;
264 if (resource
->device
->wined3d
->dxVersion
!= 7)
266 /* D3D8 and D3D9 addref the private data, DDraw does not. This
267 * can't be handled in ddraw because it doesn't know if the
268 * pointer returned is an IUnknown * or just a blob. */
269 IUnknown_AddRef(d
->ptr
.object
);
274 memcpy(data
, d
->ptr
.data
, d
->size
);
279 HRESULT CDECL
wined3d_resource_free_private_data(struct wined3d_resource
*resource
, REFGUID guid
)
281 struct private_data
*data
;
283 TRACE("resource %p, guid %s.\n", resource
, debugstr_guid(guid
));
285 data
= resource_find_private_data(resource
, guid
);
286 if (!data
) return WINED3DERR_NOTFOUND
;
288 if (data
->flags
& WINED3DSPD_IUNKNOWN
)
290 if (data
->ptr
.object
)
291 IUnknown_Release(data
->ptr
.object
);
295 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
297 list_remove(&data
->entry
);
299 HeapFree(GetProcessHeap(), 0, data
);
304 DWORD
resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
306 DWORD prev
= resource
->priority
;
307 resource
->priority
= priority
;
308 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
312 DWORD
resource_get_priority(const struct wined3d_resource
*resource
)
314 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
315 return resource
->priority
;
318 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
320 return resource
->parent
;
323 void CDECL
wined3d_resource_get_desc(const struct wined3d_resource
*resource
, struct wined3d_resource_desc
*desc
)
325 desc
->resource_type
= resource
->type
;
326 desc
->format
= resource
->format
->id
;
327 desc
->multisample_type
= resource
->multisample_type
;
328 desc
->multisample_quality
= resource
->multisample_quality
;
329 desc
->usage
= resource
->usage
;
330 desc
->pool
= resource
->pool
;
331 desc
->width
= resource
->width
;
332 desc
->height
= resource
->height
;
333 desc
->depth
= resource
->depth
;
334 desc
->size
= resource
->size
;