wininet/tests: Fix a couple of test failures on Internet Explorer 11.
[wine/wine-gecko.git] / dlls / wined3d / resource.c
blob999dc5c99fb56b432c6cd9dde968ed29bd0c6d9a
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 static DWORD resource_access_from_pool(enum wined3d_pool pool)
32 switch (pool)
34 case WINED3D_POOL_DEFAULT:
35 return WINED3D_RESOURCE_ACCESS_GPU;
37 case WINED3D_POOL_MANAGED:
38 return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;
40 case WINED3D_POOL_SCRATCH:
41 case WINED3D_POOL_SYSTEM_MEM:
42 return WINED3D_RESOURCE_ACCESS_CPU;
44 default:
45 FIXME("Unhandled pool %#x.\n", pool);
46 return 0;
50 static void resource_check_usage(DWORD usage)
52 static const DWORD handled = WINED3DUSAGE_RENDERTARGET
53 | WINED3DUSAGE_DEPTHSTENCIL
54 | WINED3DUSAGE_DYNAMIC
55 | WINED3DUSAGE_AUTOGENMIPMAP
56 | WINED3DUSAGE_STATICDECL
57 | WINED3DUSAGE_OVERLAY
58 | WINED3DUSAGE_TEXTURE;
60 if (usage & ~handled)
61 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
64 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
65 enum wined3d_resource_type type, const struct wined3d_format *format,
66 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
67 DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
68 void *parent, const struct wined3d_parent_ops *parent_ops,
69 const struct wined3d_resource_ops *resource_ops)
71 const struct wined3d *d3d = device->wined3d;
73 resource_check_usage(usage);
74 if (pool != WINED3D_POOL_SCRATCH)
76 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags & WINED3DFMT_FLAG_RENDERTARGET))
77 return WINED3DERR_INVALIDCALL;
78 if ((usage & WINED3DUSAGE_DEPTHSTENCIL) && !(format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
79 return WINED3DERR_INVALIDCALL;
80 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags & WINED3DFMT_FLAG_TEXTURE))
81 return WINED3DERR_INVALIDCALL;
84 resource->ref = 1;
85 resource->device = device;
86 resource->type = type;
87 resource->format = format;
88 resource->multisample_type = multisample_type;
89 resource->multisample_quality = multisample_quality;
90 resource->usage = usage;
91 resource->pool = pool;
92 resource->access_flags = resource_access_from_pool(pool);
93 if (usage & WINED3DUSAGE_DYNAMIC)
94 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
95 resource->width = width;
96 resource->height = height;
97 resource->depth = depth;
98 resource->size = size;
99 resource->priority = 0;
100 resource->parent = parent;
101 resource->parent_ops = parent_ops;
102 resource->resource_ops = resource_ops;
104 if (size)
106 if (!wined3d_resource_allocate_sysmem(resource))
108 ERR("Failed to allocate system memory.\n");
109 return E_OUTOFMEMORY;
112 else
114 resource->heap_memory = NULL;
117 /* Check that we have enough video ram left */
118 if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
120 if (size > wined3d_device_get_available_texture_mem(device))
122 ERR("Out of adapter memory\n");
123 wined3d_resource_free_sysmem(resource);
124 return WINED3DERR_OUTOFVIDEOMEMORY;
126 adapter_adjust_memory(device->adapter, size);
129 device_resource_add(device, resource);
131 return WINED3D_OK;
134 void resource_cleanup(struct wined3d_resource *resource)
136 const struct wined3d *d3d = resource->device->wined3d;
138 TRACE("Cleaning up resource %p.\n", resource);
140 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
142 TRACE("Decrementing device memory pool by %u.\n", resource->size);
143 adapter_adjust_memory(resource->device->adapter, 0 - resource->size);
146 wined3d_resource_free_sysmem(resource);
148 device_resource_released(resource->device, resource);
151 void resource_unload(struct wined3d_resource *resource)
153 if (resource->map_count)
154 ERR("Resource %p is being unloaded while mapped.\n", resource);
156 context_resource_unloaded(resource->device,
157 resource, resource->type);
160 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
162 DWORD prev = resource->priority;
163 resource->priority = priority;
164 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
165 return prev;
168 DWORD resource_get_priority(const struct wined3d_resource *resource)
170 TRACE("resource %p, returning %u.\n", resource, resource->priority);
171 return resource->priority;
174 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
176 return resource->parent;
179 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
181 resource->parent = parent;
184 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
186 desc->resource_type = resource->type;
187 desc->format = resource->format->id;
188 desc->multisample_type = resource->multisample_type;
189 desc->multisample_quality = resource->multisample_quality;
190 desc->usage = resource->usage;
191 desc->pool = resource->pool;
192 desc->width = resource->width;
193 desc->height = resource->height;
194 desc->depth = resource->depth;
195 desc->size = resource->size;
198 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
200 void **p;
201 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
202 void *mem;
204 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
205 return FALSE;
207 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
208 *p = mem;
210 resource->heap_memory = ++p;
212 return TRUE;
215 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
217 void **p = resource->heap_memory;
219 if (!p)
220 return;
222 HeapFree(GetProcessHeap(), 0, *(--p));
223 resource->heap_memory = NULL;
226 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
228 /* Not all flags make sense together, but Windows never returns an error.
229 * Catch the cases that could cause issues. */
230 if (flags & WINED3D_MAP_READONLY)
232 if (flags & WINED3D_MAP_DISCARD)
234 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
235 return 0;
237 if (flags & WINED3D_MAP_NOOVERWRITE)
239 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
240 return 0;
243 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
244 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
246 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
247 return 0;
249 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
250 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
252 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
253 return 0;
256 return flags;
259 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
261 GLbitfield ret = 0;
263 if (!(d3d_flags & WINED3D_MAP_READONLY))
264 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
265 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
266 ret |= GL_MAP_READ_BIT;
268 if (d3d_flags & WINED3D_MAP_DISCARD)
269 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
270 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
271 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
273 return ret;
276 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
278 if (d3d_flags & WINED3D_MAP_READONLY)
279 return GL_READ_ONLY_ARB;
280 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
281 return GL_WRITE_ONLY_ARB;
282 return GL_READ_WRITE_ARB;