d3dx9: Improve the scoring for fallback formats.
[wine/multimedia.git] / dlls / wined3d / resource.c
blob0ab55dc3f9e57b5e4f7869676b6d62d7d7225f97
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);
29 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
31 static DWORD resource_access_from_pool(enum wined3d_pool pool)
33 switch (pool)
35 case WINED3D_POOL_DEFAULT:
36 return WINED3D_RESOURCE_ACCESS_GPU;
38 case WINED3D_POOL_MANAGED:
39 return WINED3D_RESOURCE_ACCESS_GPU | WINED3D_RESOURCE_ACCESS_CPU;
41 case WINED3D_POOL_SCRATCH:
42 case WINED3D_POOL_SYSTEM_MEM:
43 return WINED3D_RESOURCE_ACCESS_CPU;
45 default:
46 FIXME("Unhandled pool %#x.\n", pool);
47 return 0;
51 static void resource_check_usage(DWORD usage)
53 static const DWORD handled = WINED3DUSAGE_RENDERTARGET
54 | WINED3DUSAGE_DEPTHSTENCIL
55 | WINED3DUSAGE_WRITEONLY
56 | WINED3DUSAGE_DYNAMIC
57 | WINED3DUSAGE_AUTOGENMIPMAP
58 | WINED3DUSAGE_STATICDECL
59 | WINED3DUSAGE_OVERLAY
60 | WINED3DUSAGE_TEXTURE;
62 /* WINED3DUSAGE_WRITEONLY is supposed to result in write-combined mappings
63 * being returned. OpenGL doesn't give us explicit control over that, but
64 * the hints and access flags we set for typical access patterns on
65 * dynamic resources should in theory have the same effect on the OpenGL
66 * driver. */
68 if (usage & ~handled)
69 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
70 if ((usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY)) == WINED3DUSAGE_DYNAMIC)
71 WARN_(d3d_perf)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
74 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
75 enum wined3d_resource_type type, const struct wined3d_format *format,
76 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
77 DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
78 void *parent, const struct wined3d_parent_ops *parent_ops,
79 const struct wined3d_resource_ops *resource_ops)
81 const struct wined3d *d3d = device->wined3d;
83 resource_check_usage(usage);
84 if (pool != WINED3D_POOL_SCRATCH)
86 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags & WINED3DFMT_FLAG_RENDERTARGET))
87 return WINED3DERR_INVALIDCALL;
88 if ((usage & WINED3DUSAGE_DEPTHSTENCIL) && !(format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
89 return WINED3DERR_INVALIDCALL;
90 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags & WINED3DFMT_FLAG_TEXTURE))
91 return WINED3DERR_INVALIDCALL;
94 resource->ref = 1;
95 resource->device = device;
96 resource->type = type;
97 resource->format = format;
98 resource->multisample_type = multisample_type;
99 resource->multisample_quality = multisample_quality;
100 resource->usage = usage;
101 resource->pool = pool;
102 resource->access_flags = resource_access_from_pool(pool);
103 if (usage & WINED3DUSAGE_DYNAMIC)
104 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
105 resource->width = width;
106 resource->height = height;
107 resource->depth = depth;
108 resource->size = size;
109 resource->priority = 0;
110 resource->parent = parent;
111 resource->parent_ops = parent_ops;
112 resource->resource_ops = resource_ops;
113 resource->map_binding = WINED3D_LOCATION_SYSMEM;
115 if (size)
117 if (!wined3d_resource_allocate_sysmem(resource))
119 ERR("Failed to allocate system memory.\n");
120 return E_OUTOFMEMORY;
123 else
125 resource->heap_memory = NULL;
128 /* Check that we have enough video ram left */
129 if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
131 if (size > wined3d_device_get_available_texture_mem(device))
133 ERR("Out of adapter memory\n");
134 wined3d_resource_free_sysmem(resource);
135 return WINED3DERR_OUTOFVIDEOMEMORY;
137 adapter_adjust_memory(device->adapter, size);
140 device_resource_add(device, resource);
142 return WINED3D_OK;
145 void resource_cleanup(struct wined3d_resource *resource)
147 const struct wined3d *d3d = resource->device->wined3d;
149 TRACE("Cleaning up resource %p.\n", resource);
151 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
153 TRACE("Decrementing device memory pool by %u.\n", resource->size);
154 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
157 wined3d_resource_free_sysmem(resource);
159 device_resource_released(resource->device, resource);
162 void resource_unload(struct wined3d_resource *resource)
164 if (resource->map_count)
165 ERR("Resource %p is being unloaded while mapped.\n", resource);
167 context_resource_unloaded(resource->device,
168 resource, resource->type);
171 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
173 DWORD prev;
175 if (resource->pool != WINED3D_POOL_MANAGED)
177 WARN("Called on non-managed resource %p, ignoring.\n", resource);
178 return 0;
181 prev = resource->priority;
182 resource->priority = priority;
183 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
184 return prev;
187 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
189 TRACE("resource %p, returning %u.\n", resource, resource->priority);
190 return resource->priority;
193 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
195 return resource->parent;
198 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
200 resource->parent = parent;
203 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
205 desc->resource_type = resource->type;
206 desc->format = resource->format->id;
207 desc->multisample_type = resource->multisample_type;
208 desc->multisample_quality = resource->multisample_quality;
209 desc->usage = resource->usage;
210 desc->pool = resource->pool;
211 desc->width = resource->width;
212 desc->height = resource->height;
213 desc->depth = resource->depth;
214 desc->size = resource->size;
217 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
219 void **p;
220 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
221 void *mem;
223 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
224 return FALSE;
226 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
227 *p = mem;
229 resource->heap_memory = ++p;
231 return TRUE;
234 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
236 void **p = resource->heap_memory;
238 if (!p)
239 return;
241 HeapFree(GetProcessHeap(), 0, *(--p));
242 resource->heap_memory = NULL;
245 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
247 /* Not all flags make sense together, but Windows never returns an error.
248 * Catch the cases that could cause issues. */
249 if (flags & WINED3D_MAP_READONLY)
251 if (flags & WINED3D_MAP_DISCARD)
253 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
254 return 0;
256 if (flags & WINED3D_MAP_NOOVERWRITE)
258 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
259 return 0;
262 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
263 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
265 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
266 return 0;
268 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
269 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
271 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
272 return 0;
275 return flags;
278 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
280 GLbitfield ret = 0;
282 if (!(d3d_flags & WINED3D_MAP_READONLY))
283 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
284 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
285 ret |= GL_MAP_READ_BIT;
287 if (d3d_flags & WINED3D_MAP_DISCARD)
288 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
289 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
290 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
292 return ret;
295 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
297 if (d3d_flags & WINED3D_MAP_READONLY)
298 return GL_READ_ONLY_ARB;
299 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
300 return GL_WRITE_ONLY_ARB;
301 return GL_READ_WRITE_ARB;
304 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
306 struct wined3d_swapchain *swapchain;
308 /* Only texture resources can be onscreen. */
309 if (resource->type != WINED3D_RTYPE_TEXTURE)
310 return TRUE;
312 /* Not on a swapchain - must be offscreen */
313 if (!(swapchain = wined3d_texture_from_resource(resource)->swapchain))
314 return TRUE;
316 /* The front buffer is always onscreen */
317 if (resource == &swapchain->front_buffer->resource)
318 return FALSE;
320 /* If the swapchain is rendered to an FBO, the backbuffer is
321 * offscreen, otherwise onscreen */
322 return swapchain->render_to_fbo;
325 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
327 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
328 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
329 else if (resource->multisample_type)
330 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
331 else
332 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;