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 "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
33 DWORD flags
; /* DDSPD_* */
44 static DWORD
resource_access_from_pool(WINED3DPOOL pool
)
48 case WINED3DPOOL_DEFAULT
:
49 return WINED3D_RESOURCE_ACCESS_GPU
;
51 case WINED3DPOOL_MANAGED
:
52 return WINED3D_RESOURCE_ACCESS_GPU
| WINED3D_RESOURCE_ACCESS_CPU
;
54 case WINED3DPOOL_SYSTEMMEM
:
55 return WINED3D_RESOURCE_ACCESS_CPU
;
57 case WINED3DPOOL_SCRATCH
:
58 return WINED3D_RESOURCE_ACCESS_SCRATCH
;
61 FIXME("Unhandled pool %#x.\n", pool
);
66 static void resource_check_usage(DWORD usage
)
68 static const DWORD handled
= WINED3DUSAGE_RENDERTARGET
69 | WINED3DUSAGE_DEPTHSTENCIL
70 | WINED3DUSAGE_DYNAMIC
71 | WINED3DUSAGE_AUTOGENMIPMAP
72 | WINED3DUSAGE_STATICDECL
73 | WINED3DUSAGE_OVERLAY
;
76 FIXME("Unhandled usage flags %#x.\n", usage
& ~handled
);
79 HRESULT
resource_init(struct wined3d_resource
*resource
, struct wined3d_device
*device
,
80 WINED3DRESOURCETYPE resource_type
, const struct wined3d_format
*format
,
81 WINED3DMULTISAMPLE_TYPE multisample_type
, UINT multisample_quality
,
82 DWORD usage
, WINED3DPOOL pool
, UINT width
, UINT height
, UINT depth
, UINT size
,
83 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
84 const struct wined3d_resource_ops
*resource_ops
)
87 resource
->device
= device
;
88 resource
->resourceType
= resource_type
;
89 resource
->format
= format
;
90 resource
->multisample_type
= multisample_type
;
91 resource
->multisample_quality
= multisample_quality
;
92 resource
->usage
= usage
;
93 resource
->pool
= pool
;
94 resource
->access_flags
= resource_access_from_pool(pool
);
95 if (usage
& WINED3DUSAGE_DYNAMIC
)
96 resource
->access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
97 resource
->width
= width
;
98 resource
->height
= height
;
99 resource
->depth
= depth
;
100 resource
->size
= size
;
101 resource
->priority
= 0;
102 resource
->parent
= parent
;
103 resource
->parent_ops
= parent_ops
;
104 resource
->resource_ops
= resource_ops
;
105 list_init(&resource
->privateData
);
107 resource_check_usage(usage
);
111 resource
->heapMemory
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, size
+ RESOURCE_ALIGNMENT
);
112 if (!resource
->heapMemory
)
114 ERR("Out of memory!\n");
115 return WINED3DERR_OUTOFVIDEOMEMORY
;
120 resource
->heapMemory
= NULL
;
122 resource
->allocatedMemory
= (BYTE
*)(((ULONG_PTR
)resource
->heapMemory
123 + (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1));
125 /* Check that we have enough video ram left */
126 if (pool
== WINED3DPOOL_DEFAULT
)
128 if (size
> wined3d_device_get_available_texture_mem(device
))
130 ERR("Out of adapter memory\n");
131 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
132 return WINED3DERR_OUTOFVIDEOMEMORY
;
134 adapter_adjust_memory(device
->adapter
, size
);
137 device_resource_add(device
, resource
);
142 void resource_cleanup(struct wined3d_resource
*resource
)
144 struct private_data
*data
;
145 struct list
*e1
, *e2
;
148 TRACE("Cleaning up resource %p.\n", resource
);
150 if (resource
->pool
== WINED3DPOOL_DEFAULT
)
152 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
153 adapter_adjust_memory(resource
->device
->adapter
, 0 - resource
->size
);
156 LIST_FOR_EACH_SAFE(e1
, e2
, &resource
->privateData
)
158 data
= LIST_ENTRY(e1
, struct private_data
, entry
);
159 hr
= wined3d_resource_free_private_data(resource
, &data
->tag
);
161 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource
, hr
);
164 HeapFree(GetProcessHeap(), 0, resource
->heapMemory
);
165 resource
->allocatedMemory
= 0;
166 resource
->heapMemory
= 0;
168 if (resource
->device
)
169 device_resource_released(resource
->device
, resource
);
172 void resource_unload(struct wined3d_resource
*resource
)
174 context_resource_unloaded(resource
->device
,
175 resource
, resource
->resourceType
);
178 static struct private_data
*resource_find_private_data(const struct wined3d_resource
*resource
, REFGUID tag
)
180 struct private_data
*data
;
183 TRACE("Searching for private data %s\n", debugstr_guid(tag
));
184 LIST_FOR_EACH(entry
, &resource
->privateData
)
186 data
= LIST_ENTRY(entry
, struct private_data
, entry
);
187 if (IsEqualGUID(&data
->tag
, tag
)) {
188 TRACE("Found %p\n", data
);
192 TRACE("Not found\n");
196 HRESULT CDECL
wined3d_resource_set_private_data(struct wined3d_resource
*resource
, REFGUID guid
,
197 const void *data
, DWORD data_size
, DWORD flags
)
199 struct private_data
*d
;
201 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
202 resource
, debugstr_guid(guid
), data
, data_size
, flags
);
204 wined3d_resource_free_private_data(resource
, guid
);
206 d
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*d
));
207 if (!d
) return E_OUTOFMEMORY
;
212 if (flags
& WINED3DSPD_IUNKNOWN
)
214 if (data_size
!= sizeof(IUnknown
*))
216 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size
);
217 HeapFree(GetProcessHeap(), 0, d
);
218 return WINED3DERR_INVALIDCALL
;
220 d
->ptr
.object
= (IUnknown
*)data
;
221 d
->size
= sizeof(IUnknown
*);
222 IUnknown_AddRef(d
->ptr
.object
);
226 d
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
229 HeapFree(GetProcessHeap(), 0, d
);
230 return E_OUTOFMEMORY
;
233 memcpy(d
->ptr
.data
, data
, data_size
);
235 list_add_tail(&resource
->privateData
, &d
->entry
);
240 HRESULT CDECL
wined3d_resource_get_private_data(const struct wined3d_resource
*resource
, REFGUID guid
,
241 void *data
, DWORD
*data_size
)
243 const struct private_data
*d
;
245 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
246 resource
, debugstr_guid(guid
), data
, data_size
);
248 d
= resource_find_private_data(resource
, guid
);
249 if (!d
) return WINED3DERR_NOTFOUND
;
251 if (*data_size
< d
->size
)
253 *data_size
= d
->size
;
254 return WINED3DERR_MOREDATA
;
257 if (d
->flags
& WINED3DSPD_IUNKNOWN
)
259 *(IUnknown
**)data
= d
->ptr
.object
;
260 if (resource
->device
->wined3d
->dxVersion
!= 7)
262 /* D3D8 and D3D9 addref the private data, DDraw does not. This
263 * can't be handled in ddraw because it doesn't know if the
264 * pointer returned is an IUnknown * or just a blob. */
265 IUnknown_AddRef(d
->ptr
.object
);
270 memcpy(data
, d
->ptr
.data
, d
->size
);
275 HRESULT CDECL
wined3d_resource_free_private_data(struct wined3d_resource
*resource
, REFGUID guid
)
277 struct private_data
*data
;
279 TRACE("resource %p, guid %s.\n", resource
, debugstr_guid(guid
));
281 data
= resource_find_private_data(resource
, guid
);
282 if (!data
) return WINED3DERR_NOTFOUND
;
284 if (data
->flags
& WINED3DSPD_IUNKNOWN
)
286 if (data
->ptr
.object
)
287 IUnknown_Release(data
->ptr
.object
);
291 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
293 list_remove(&data
->entry
);
295 HeapFree(GetProcessHeap(), 0, data
);
300 DWORD
resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
302 DWORD prev
= resource
->priority
;
303 resource
->priority
= priority
;
304 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
308 DWORD
resource_get_priority(const struct wined3d_resource
*resource
)
310 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
311 return resource
->priority
;
314 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
316 return resource
->parent
;
319 void CDECL
wined3d_resource_get_desc(const struct wined3d_resource
*resource
, struct wined3d_resource_desc
*desc
)
321 desc
->resource_type
= resource
->resourceType
;
322 desc
->format
= resource
->format
->id
;
323 desc
->multisample_type
= resource
->multisample_type
;
324 desc
->multisample_quality
= resource
->multisample_quality
;
325 desc
->usage
= resource
->usage
;
326 desc
->pool
= resource
->pool
;
327 desc
->width
= resource
->width
;
328 desc
->height
= resource
->height
;
329 desc
->depth
= resource
->depth
;
330 desc
->size
= resource
->size
;