[WINED3D]
[reactos.git] / reactos / dll / directx / wine / wined3d / resource.c
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
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27
28 struct private_data
29 {
30 struct list entry;
31
32 GUID tag;
33 DWORD flags; /* DDSPD_* */
34
35 union
36 {
37 void *data;
38 IUnknown *object;
39 } ptr;
40
41 DWORD size;
42 };
43
44 static DWORD resource_access_from_pool(enum wined3d_pool pool)
45 {
46 switch (pool)
47 {
48 case WINED3D_POOL_DEFAULT:
49 return WINED3D_RESOURCE_ACCESS_GPU;
50
51 case WINED3D_POOL_MANAGED:
52 return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;
53
54 case WINED3D_POOL_SCRATCH:
55 case WINED3D_POOL_SYSTEM_MEM:
56 return WINED3D_RESOURCE_ACCESS_CPU;
57
58 default:
59 FIXME("Unhandled pool %#x.\n", pool);
60 return 0;
61 }
62 }
63
64 static void resource_check_usage(DWORD usage)
65 {
66 static const DWORD handled = WINED3DUSAGE_RENDERTARGET
67 | WINED3DUSAGE_DEPTHSTENCIL
68 | WINED3DUSAGE_DYNAMIC
69 | WINED3DUSAGE_AUTOGENMIPMAP
70 | WINED3DUSAGE_STATICDECL
71 | WINED3DUSAGE_OVERLAY;
72
73 if (usage & ~handled)
74 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
75 }
76
77 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
78 enum wined3d_resource_type type, const struct wined3d_format *format,
79 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
80 DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
81 void *parent, const struct wined3d_parent_ops *parent_ops,
82 const struct wined3d_resource_ops *resource_ops)
83 {
84 const struct wined3d *d3d = device->wined3d;
85
86 resource->ref = 1;
87 resource->device = device;
88 resource->type = 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);
106
107 resource_check_usage(usage);
108
109 if (size)
110 {
111 resource->heap_memory = wined3d_resource_allocate_sysmem(size);
112 if (!resource->heap_memory)
113 {
114 ERR("Out of memory!\n");
115 return WINED3DERR_OUTOFVIDEOMEMORY;
116 }
117 }
118 else
119 {
120 resource->heap_memory = NULL;
121 }
122 resource->allocatedMemory = resource->heap_memory;
123
124 /* Check that we have enough video ram left */
125 if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
126 {
127 if (size > wined3d_device_get_available_texture_mem(device))
128 {
129 ERR("Out of adapter memory\n");
130 wined3d_resource_free_sysmem(resource->heap_memory);
131 return WINED3DERR_OUTOFVIDEOMEMORY;
132 }
133 adapter_adjust_memory(device->adapter, size);
134 }
135
136 device_resource_add(device, resource);
137
138 return WINED3D_OK;
139 }
140
141 void resource_cleanup(struct wined3d_resource *resource)
142 {
143 const struct wined3d *d3d = resource->device->wined3d;
144 struct private_data *data;
145 struct list *e1, *e2;
146 HRESULT hr;
147
148 TRACE("Cleaning up resource %p.\n", resource);
149
150 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
151 {
152 TRACE("Decrementing device memory pool by %u.\n", resource->size);
153 adapter_adjust_memory(resource->device->adapter, 0 - resource->size);
154 }
155
156 LIST_FOR_EACH_SAFE(e1, e2, &resource->privateData)
157 {
158 data = LIST_ENTRY(e1, struct private_data, entry);
159 hr = wined3d_resource_free_private_data(resource, &data->tag);
160 if (FAILED(hr))
161 ERR("Failed to free private data when destroying resource %p, hr = %#x.\n", resource, hr);
162 }
163
164 wined3d_resource_free_sysmem(resource->heap_memory);
165 resource->allocatedMemory = NULL;
166 resource->heap_memory = NULL;
167
168 device_resource_released(resource->device, resource);
169 }
170
171 void resource_unload(struct wined3d_resource *resource)
172 {
173 if (resource->map_count)
174 ERR("Resource %p is being unloaded while mapped.\n", resource);
175
176 context_resource_unloaded(resource->device,
177 resource, resource->type);
178 }
179
180 static struct private_data *resource_find_private_data(const struct wined3d_resource *resource, REFGUID tag)
181 {
182 struct private_data *data;
183 struct list *entry;
184
185 TRACE("Searching for private data %s\n", debugstr_guid(tag));
186 LIST_FOR_EACH(entry, &resource->privateData)
187 {
188 data = LIST_ENTRY(entry, struct private_data, entry);
189 if (IsEqualGUID(&data->tag, tag)) {
190 TRACE("Found %p\n", data);
191 return data;
192 }
193 }
194 TRACE("Not found\n");
195 return NULL;
196 }
197
198 HRESULT CDECL wined3d_resource_set_private_data(struct wined3d_resource *resource, REFGUID guid,
199 const void *data, DWORD data_size, DWORD flags)
200 {
201 struct private_data *d;
202
203 TRACE("resource %p, riid %s, data %p, data_size %u, flags %#x.\n",
204 resource, debugstr_guid(guid), data, data_size, flags);
205
206 wined3d_resource_free_private_data(resource, guid);
207
208 d = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d));
209 if (!d) return E_OUTOFMEMORY;
210
211 d->tag = *guid;
212 d->flags = flags;
213
214 if (flags & WINED3DSPD_IUNKNOWN)
215 {
216 if (data_size != sizeof(IUnknown *))
217 {
218 WARN("IUnknown data with size %u, returning WINED3DERR_INVALIDCALL.\n", data_size);
219 HeapFree(GetProcessHeap(), 0, d);
220 return WINED3DERR_INVALIDCALL;
221 }
222 d->ptr.object = (IUnknown *)data;
223 d->size = sizeof(IUnknown *);
224 IUnknown_AddRef(d->ptr.object);
225 }
226 else
227 {
228 d->ptr.data = HeapAlloc(GetProcessHeap(), 0, data_size);
229 if (!d->ptr.data)
230 {
231 HeapFree(GetProcessHeap(), 0, d);
232 return E_OUTOFMEMORY;
233 }
234 d->size = data_size;
235 memcpy(d->ptr.data, data, data_size);
236 }
237 list_add_tail(&resource->privateData, &d->entry);
238
239 return WINED3D_OK;
240 }
241
242 HRESULT CDECL wined3d_resource_get_private_data(const struct wined3d_resource *resource, REFGUID guid,
243 void *data, DWORD *data_size)
244 {
245 const struct private_data *d;
246
247 TRACE("resource %p, guid %s, data %p, data_size %p.\n",
248 resource, debugstr_guid(guid), data, data_size);
249
250 d = resource_find_private_data(resource, guid);
251 if (!d) return WINED3DERR_NOTFOUND;
252
253 if (*data_size < d->size)
254 {
255 *data_size = d->size;
256 return WINED3DERR_MOREDATA;
257 }
258
259 if (d->flags & WINED3DSPD_IUNKNOWN)
260 {
261 *(IUnknown **)data = d->ptr.object;
262 if (resource->device->wined3d->dxVersion != 7)
263 {
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);
268 }
269 }
270 else
271 {
272 memcpy(data, d->ptr.data, d->size);
273 }
274
275 return WINED3D_OK;
276 }
277 HRESULT CDECL wined3d_resource_free_private_data(struct wined3d_resource *resource, REFGUID guid)
278 {
279 struct private_data *data;
280
281 TRACE("resource %p, guid %s.\n", resource, debugstr_guid(guid));
282
283 data = resource_find_private_data(resource, guid);
284 if (!data) return WINED3DERR_NOTFOUND;
285
286 if (data->flags & WINED3DSPD_IUNKNOWN)
287 {
288 if (data->ptr.object)
289 IUnknown_Release(data->ptr.object);
290 }
291 else
292 {
293 HeapFree(GetProcessHeap(), 0, data->ptr.data);
294 }
295 list_remove(&data->entry);
296
297 HeapFree(GetProcessHeap(), 0, data);
298
299 return WINED3D_OK;
300 }
301
302 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
303 {
304 DWORD prev = resource->priority;
305 resource->priority = priority;
306 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
307 return prev;
308 }
309
310 DWORD resource_get_priority(const struct wined3d_resource *resource)
311 {
312 TRACE("resource %p, returning %u.\n", resource, resource->priority);
313 return resource->priority;
314 }
315
316 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
317 {
318 return resource->parent;
319 }
320
321 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
322 {
323 desc->resource_type = resource->type;
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;
333 }
334
335 void *wined3d_resource_allocate_sysmem(SIZE_T size)
336 {
337 void **p;
338 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
339 void *mem;
340
341 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + align)))
342 return NULL;
343
344 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
345 *p = mem;
346
347 return ++p;
348 }
349
350 void wined3d_resource_free_sysmem(void *mem)
351 {
352 void **p = mem;
353
354 if (!mem)
355 return;
356
357 HeapFree(GetProcessHeap(), 0, *(--p));
358 }
359
360 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
361 {
362 /* Not all flags make sense together, but Windows never returns an error.
363 * Catch the cases that could cause issues. */
364 if (flags & WINED3D_MAP_READONLY)
365 {
366 if (flags & WINED3D_MAP_DISCARD)
367 {
368 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
369 return 0;
370 }
371 if (flags & WINED3D_MAP_NOOVERWRITE)
372 {
373 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
374 return 0;
375 }
376 }
377 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
378 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
379 {
380 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
381 return 0;
382 }
383 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
384 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
385 {
386 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
387 return 0;
388 }
389
390 return flags;
391 }
392
393 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
394 {
395 GLbitfield ret = 0;
396
397 if (!(d3d_flags & WINED3D_MAP_READONLY))
398 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
399 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
400 ret |= GL_MAP_READ_BIT;
401
402 if (d3d_flags & WINED3D_MAP_DISCARD)
403 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
404 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
405 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
406
407 return ret;
408 }