wbemdisp: Added WinMGMTS object stub implementation.
[wine/wine-gecko.git] / dlls / wined3d / resource.c
blobca322b27b615ccb42d46a730c861747897994e58
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 struct private_data
32 struct list entry;
34 GUID tag;
35 DWORD flags; /* DDSPD_* */
37 union
39 void *data;
40 IUnknown *object;
41 } ptr;
43 DWORD size;
46 static DWORD resource_access_from_pool(enum wined3d_pool pool)
48 switch (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;
60 default:
61 FIXME("Unhandled pool %#x.\n", pool);
62 return 0;
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;
75 if (usage & ~handled)
76 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
79 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
80 enum wined3d_resource_type type, const struct wined3d_format *format,
81 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
82 DWORD usage, enum wined3d_pool 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)
86 const struct wined3d *d3d = device->wined3d;
88 resource->ref = 1;
89 resource->device = device;
90 resource->type = 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);
111 if (size)
113 resource->heap_memory = wined3d_resource_allocate_sysmem(size);
114 if (!resource->heap_memory)
116 ERR("Out of memory!\n");
117 return WINED3DERR_OUTOFVIDEOMEMORY;
120 else
122 resource->heap_memory = NULL;
124 resource->allocatedMemory = resource->heap_memory;
126 /* Check that we have enough video ram left */
127 if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
129 if (size > wined3d_device_get_available_texture_mem(device))
131 ERR("Out of adapter memory\n");
132 wined3d_resource_free_sysmem(resource->heap_memory);
133 return WINED3DERR_OUTOFVIDEOMEMORY;
135 adapter_adjust_memory(device->adapter, size);
138 device_resource_add(device, resource);
140 return WINED3D_OK;
143 void resource_cleanup(struct wined3d_resource *resource)
145 const struct wined3d *d3d = resource->device->wined3d;
146 struct private_data *data;
147 struct list *e1, *e2;
148 HRESULT hr;
150 TRACE("Cleaning up resource %p.\n", resource);
152 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
154 TRACE("Decrementing device memory pool by %u.\n", resource->size);
155 adapter_adjust_memory(resource->device->adapter, 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);
162 if (FAILED(hr))
163 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
166 wined3d_resource_free_sysmem(resource->heap_memory);
167 resource->allocatedMemory = NULL;
168 resource->heap_memory = NULL;
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;
185 struct list *entry;
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);
193 return data;
196 TRACE("Not found\n");
197 return NULL;
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;
213 d->tag = *guid;
214 d->flags = flags;
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);
228 else
230 d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
231 if (!d->ptr.data)
233 HeapFree(GetProcessHeap(), 0, d);
234 return E_OUTOFMEMORY;
236 d->size = data_size;
237 memcpy(d->ptr.data, data, data_size);
239 list_add_tail(&resource->privateData, &d->entry);
241 return WINED3D_OK;
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);
272 else
274 memcpy(data, d->ptr.data, d->size);
277 return WINED3D_OK;
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);
293 else
295 HeapFree(GetProcessHeap(), 0, data->ptr.data);
297 list_remove(&data->entry);
299 HeapFree(GetProcessHeap(), 0, data);
301 return WINED3D_OK;
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);
309 return 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;
337 void *wined3d_resource_allocate_sysmem(SIZE_T size)
339 void **p;
340 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
341 void *mem;
343 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + align)))
344 return NULL;
346 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
347 *p = mem;
349 return ++p;
352 void wined3d_resource_free_sysmem(void *mem)
354 void **p = mem;
356 if (!mem)
357 return;
359 HeapFree(GetProcessHeap(), 0, *(--p));
362 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
364 /* Not all flags make sense together, but Windows never returns an error.
365 * Catch the cases that could cause issues. */
366 if (flags & WINED3D_MAP_READONLY)
368 if (flags & WINED3D_MAP_DISCARD)
370 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
371 return 0;
373 if (flags & WINED3D_MAP_NOOVERWRITE)
375 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
376 return 0;
379 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
380 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
382 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
383 return 0;
385 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
386 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
388 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
389 return 0;
392 return flags;
395 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
397 GLbitfield ret = 0;
399 if (!(d3d_flags & WINED3D_MAP_READONLY))
400 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
401 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
402 ret |= GL_MAP_READ_BIT;
404 if (d3d_flags & WINED3D_MAP_DISCARD)
405 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
406 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
407 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
409 return ret;