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
7 * Copyright 2006-2008, 2013 Stefan Dösinger for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/port.h"
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
35 DWORD flags
; /* DDSPD_* */
46 static DWORD
resource_access_from_pool(enum wined3d_pool pool
)
50 case WINED3D_POOL_DEFAULT
:
51 return WINED3D_RESOURCE_ACCESS_GPU
;
53 case WINED3D_POOL_MANAGED
:
54 return WINED3D_RESOURCE_ACCESS_GPU
| WINED3D_RESOURCE_ACCESS_CPU
;
56 case WINED3D_POOL_SCRATCH
:
57 case WINED3D_POOL_SYSTEM_MEM
:
58 return WINED3D_RESOURCE_ACCESS_CPU
;
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
74 | WINED3DUSAGE_TEXTURE
;
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
)
87 const struct wined3d
*d3d
= device
->wined3d
;
89 resource_check_usage(usage
);
90 if (pool
!= WINED3D_POOL_SCRATCH
)
92 if ((usage
& WINED3DUSAGE_RENDERTARGET
) && !(format
->flags
& WINED3DFMT_FLAG_RENDERTARGET
))
93 return WINED3DERR_INVALIDCALL
;
94 if ((usage
& WINED3DUSAGE_DEPTHSTENCIL
) && !(format
->flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
95 return WINED3DERR_INVALIDCALL
;
96 if ((usage
& WINED3DUSAGE_TEXTURE
) && !(format
->flags
& WINED3DFMT_FLAG_TEXTURE
))
97 return WINED3DERR_INVALIDCALL
;
101 resource
->device
= device
;
102 resource
->type
= type
;
103 resource
->format
= format
;
104 resource
->multisample_type
= multisample_type
;
105 resource
->multisample_quality
= multisample_quality
;
106 resource
->usage
= usage
;
107 resource
->pool
= pool
;
108 resource
->access_flags
= resource_access_from_pool(pool
);
109 if (usage
& WINED3DUSAGE_DYNAMIC
)
110 resource
->access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
111 resource
->width
= width
;
112 resource
->height
= height
;
113 resource
->depth
= depth
;
114 resource
->size
= size
;
115 resource
->priority
= 0;
116 resource
->parent
= parent
;
117 resource
->parent_ops
= parent_ops
;
118 resource
->resource_ops
= resource_ops
;
119 list_init(&resource
->privateData
);
123 if (!wined3d_resource_allocate_sysmem(resource
))
125 ERR("Failed to allocate system memory.\n");
126 return E_OUTOFMEMORY
;
131 resource
->heap_memory
= NULL
;
133 resource
->allocatedMemory
= resource
->heap_memory
;
135 /* Check that we have enough video ram left */
136 if (pool
== WINED3D_POOL_DEFAULT
&& d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
138 if (size
> wined3d_device_get_available_texture_mem(device
))
140 ERR("Out of adapter memory\n");
141 wined3d_resource_free_sysmem(resource
);
142 return WINED3DERR_OUTOFVIDEOMEMORY
;
144 adapter_adjust_memory(device
->adapter
, size
);
147 device_resource_add(device
, resource
);
152 void resource_cleanup(struct wined3d_resource
*resource
)
154 const struct wined3d
*d3d
= resource
->device
->wined3d
;
155 struct private_data
*data
;
156 struct list
*e1
, *e2
;
159 TRACE("Cleaning up resource %p.\n", resource
);
161 if (resource
->pool
== WINED3D_POOL_DEFAULT
&& d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
163 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
164 adapter_adjust_memory(resource
->device
->adapter
, 0 - resource
->size
);
167 LIST_FOR_EACH_SAFE(e1
, e2
, &resource
->privateData
)
169 data
= LIST_ENTRY(e1
, struct private_data
, entry
);
170 hr
= wined3d_resource_free_private_data(resource
, &data
->tag
);
172 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource
, hr
);
175 wined3d_resource_free_sysmem(resource
);
176 resource
->allocatedMemory
= NULL
;
178 device_resource_released(resource
->device
, resource
);
181 void resource_unload(struct wined3d_resource
*resource
)
183 if (resource
->map_count
)
184 ERR("Resource %p is being unloaded while mapped.\n", resource
);
186 context_resource_unloaded(resource
->device
,
187 resource
, resource
->type
);
190 static struct private_data
*resource_find_private_data(const struct wined3d_resource
*resource
, REFGUID tag
)
192 struct private_data
*data
;
195 TRACE("Searching for private data %s\n", debugstr_guid(tag
));
196 LIST_FOR_EACH(entry
, &resource
->privateData
)
198 data
= LIST_ENTRY(entry
, struct private_data
, entry
);
199 if (IsEqualGUID(&data
->tag
, tag
)) {
200 TRACE("Found %p\n", data
);
204 TRACE("Not found\n");
208 HRESULT CDECL
wined3d_resource_set_private_data(struct wined3d_resource
*resource
, REFGUID guid
,
209 const void *data
, DWORD data_size
, DWORD flags
)
211 struct private_data
*d
;
213 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
214 resource
, debugstr_guid(guid
), data
, data_size
, flags
);
216 wined3d_resource_free_private_data(resource
, guid
);
218 d
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*d
));
219 if (!d
) return E_OUTOFMEMORY
;
224 if (flags
& WINED3DSPD_IUNKNOWN
)
226 if (data_size
!= sizeof(IUnknown
*))
228 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size
);
229 HeapFree(GetProcessHeap(), 0, d
);
230 return WINED3DERR_INVALIDCALL
;
232 d
->ptr
.object
= (IUnknown
*)data
;
233 d
->size
= sizeof(IUnknown
*);
234 IUnknown_AddRef(d
->ptr
.object
);
238 d
->ptr
.data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
241 HeapFree(GetProcessHeap(), 0, d
);
242 return E_OUTOFMEMORY
;
245 memcpy(d
->ptr
.data
, data
, data_size
);
247 list_add_tail(&resource
->privateData
, &d
->entry
);
252 HRESULT CDECL
wined3d_resource_get_private_data(const struct wined3d_resource
*resource
, REFGUID guid
,
253 void *data
, DWORD
*data_size
)
255 const struct private_data
*d
;
257 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
258 resource
, debugstr_guid(guid
), data
, data_size
);
260 d
= resource_find_private_data(resource
, guid
);
261 if (!d
) return WINED3DERR_NOTFOUND
;
263 if (*data_size
< d
->size
)
265 *data_size
= d
->size
;
266 return WINED3DERR_MOREDATA
;
269 if (d
->flags
& WINED3DSPD_IUNKNOWN
)
271 *(IUnknown
**)data
= d
->ptr
.object
;
272 if (resource
->device
->wined3d
->dxVersion
!= 7)
274 /* D3D8 and D3D9 addref the private data, DDraw does not. This
275 * can't be handled in ddraw because it doesn't know if the
276 * pointer returned is an IUnknown * or just a blob. */
277 IUnknown_AddRef(d
->ptr
.object
);
282 memcpy(data
, d
->ptr
.data
, d
->size
);
287 HRESULT CDECL
wined3d_resource_free_private_data(struct wined3d_resource
*resource
, REFGUID guid
)
289 struct private_data
*data
;
291 TRACE("resource %p, guid %s.\n", resource
, debugstr_guid(guid
));
293 data
= resource_find_private_data(resource
, guid
);
294 if (!data
) return WINED3DERR_NOTFOUND
;
296 if (data
->flags
& WINED3DSPD_IUNKNOWN
)
298 if (data
->ptr
.object
)
299 IUnknown_Release(data
->ptr
.object
);
303 HeapFree(GetProcessHeap(), 0, data
->ptr
.data
);
305 list_remove(&data
->entry
);
307 HeapFree(GetProcessHeap(), 0, data
);
312 DWORD
resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
314 DWORD prev
= resource
->priority
;
315 resource
->priority
= priority
;
316 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
320 DWORD
resource_get_priority(const struct wined3d_resource
*resource
)
322 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
323 return resource
->priority
;
326 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
328 return resource
->parent
;
331 void CDECL
wined3d_resource_get_desc(const struct wined3d_resource
*resource
, struct wined3d_resource_desc
*desc
)
333 desc
->resource_type
= resource
->type
;
334 desc
->format
= resource
->format
->id
;
335 desc
->multisample_type
= resource
->multisample_type
;
336 desc
->multisample_quality
= resource
->multisample_quality
;
337 desc
->usage
= resource
->usage
;
338 desc
->pool
= resource
->pool
;
339 desc
->width
= resource
->width
;
340 desc
->height
= resource
->height
;
341 desc
->depth
= resource
->depth
;
342 desc
->size
= resource
->size
;
345 BOOL
wined3d_resource_allocate_sysmem(struct wined3d_resource
*resource
)
348 SIZE_T align
= RESOURCE_ALIGNMENT
- 1 + sizeof(*p
);
351 if (!(mem
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, resource
->size
+ align
)))
354 p
= (void **)(((ULONG_PTR
)mem
+ align
) & ~(RESOURCE_ALIGNMENT
- 1)) - 1;
357 resource
->heap_memory
= ++p
;
362 void wined3d_resource_free_sysmem(struct wined3d_resource
*resource
)
364 void **p
= resource
->heap_memory
;
369 HeapFree(GetProcessHeap(), 0, *(--p
));
370 resource
->heap_memory
= NULL
;
373 DWORD
wined3d_resource_sanitize_map_flags(const struct wined3d_resource
*resource
, DWORD flags
)
375 /* Not all flags make sense together, but Windows never returns an error.
376 * Catch the cases that could cause issues. */
377 if (flags
& WINED3D_MAP_READONLY
)
379 if (flags
& WINED3D_MAP_DISCARD
)
381 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
384 if (flags
& WINED3D_MAP_NOOVERWRITE
)
386 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
390 else if ((flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
391 == (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
393 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
396 else if (flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
)
397 && !(resource
->usage
& WINED3DUSAGE_DYNAMIC
))
399 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
406 GLbitfield
wined3d_resource_gl_map_flags(DWORD d3d_flags
)
410 if (!(d3d_flags
& WINED3D_MAP_READONLY
))
411 ret
|= GL_MAP_WRITE_BIT
| GL_MAP_FLUSH_EXPLICIT_BIT
;
412 if (!(d3d_flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
)))
413 ret
|= GL_MAP_READ_BIT
;
415 if (d3d_flags
& WINED3D_MAP_DISCARD
)
416 ret
|= GL_MAP_INVALIDATE_BUFFER_BIT
;
417 if (d3d_flags
& WINED3D_MAP_NOOVERWRITE
)
418 ret
|= GL_MAP_UNSYNCHRONIZED_BIT
;
423 GLenum
wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags
)
425 if (d3d_flags
& WINED3D_MAP_READONLY
)
426 return GL_READ_ONLY_ARB
;
427 if (d3d_flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
428 return GL_WRITE_ONLY_ARB
;
429 return GL_READ_WRITE_ARB
;