wined3d: Don't enumerate sub-resources in wined3d_device_reset().
[wine.git] / dlls / wined3d / resource.c
blobbe7dfb47781ef9a3a71cef5b8f8f9783dee4bed3
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_LEGACY_CUBEMAP
61 | WINED3DUSAGE_TEXTURE;
63 /* WINED3DUSAGE_WRITEONLY is supposed to result in write-combined mappings
64 * being returned. OpenGL doesn't give us explicit control over that, but
65 * the hints and access flags we set for typical access patterns on
66 * dynamic resources should in theory have the same effect on the OpenGL
67 * driver. */
69 if (usage & ~handled)
70 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
71 if ((usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY)) == WINED3DUSAGE_DYNAMIC)
72 WARN_(d3d_perf)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
75 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
76 enum wined3d_resource_type type, const struct wined3d_format *format,
77 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
78 DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
79 void *parent, const struct wined3d_parent_ops *parent_ops,
80 const struct wined3d_resource_ops *resource_ops)
82 enum wined3d_gl_resource_type base_type = WINED3D_GL_RES_TYPE_COUNT;
83 enum wined3d_gl_resource_type gl_type = WINED3D_GL_RES_TYPE_COUNT;
84 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
85 BOOL tex_2d_ok = FALSE;
86 unsigned int i;
88 static const struct
90 enum wined3d_resource_type type;
91 DWORD cube_usage;
92 enum wined3d_gl_resource_type gl_type;
94 resource_types[] =
96 {WINED3D_RTYPE_BUFFER, 0, WINED3D_GL_RES_TYPE_BUFFER},
97 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_TEX_2D},
98 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_TEX_RECT},
99 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_RB},
100 {WINED3D_RTYPE_TEXTURE_2D, WINED3DUSAGE_LEGACY_CUBEMAP, WINED3D_GL_RES_TYPE_TEX_CUBE},
101 {WINED3D_RTYPE_TEXTURE_3D, 0, WINED3D_GL_RES_TYPE_TEX_3D},
104 resource_check_usage(usage);
106 for (i = 0; i < ARRAY_SIZE(resource_types); ++i)
108 if (resource_types[i].type != type
109 || resource_types[i].cube_usage != (usage & WINED3DUSAGE_LEGACY_CUBEMAP))
110 continue;
112 gl_type = resource_types[i].gl_type;
113 if (base_type == WINED3D_GL_RES_TYPE_COUNT)
114 base_type = gl_type;
116 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags[gl_type] & WINED3DFMT_FLAG_RENDERTARGET))
118 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format->id));
119 continue;
121 if ((usage & WINED3DUSAGE_DEPTHSTENCIL)
122 && !(format->flags[gl_type] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
124 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format->id));
125 continue;
127 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
128 && usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)
129 && !(format->flags[gl_type] & WINED3DFMT_FLAG_FBO_ATTACHABLE))
131 WARN("Render target or depth stencil is not FBO attachable.\n");
132 continue;
134 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags[gl_type] & WINED3DFMT_FLAG_TEXTURE))
136 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format->id));
137 continue;
139 if (((width & (width - 1)) || (height & (height - 1)))
140 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
141 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
142 && gl_type == WINED3D_GL_RES_TYPE_TEX_2D)
144 TRACE("Skipping 2D texture type to try texture rectangle.\n");
145 tex_2d_ok = TRUE;
146 continue;
148 break;
151 if (base_type != WINED3D_GL_RES_TYPE_COUNT && i == ARRAY_SIZE(resource_types))
153 if (tex_2d_ok)
155 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
156 * Use 2D textures, the texture code will pad to a power of 2 size. */
157 gl_type = WINED3D_GL_RES_TYPE_TEX_2D;
159 else if (pool == WINED3D_POOL_SCRATCH)
161 /* Needed for proper format information. */
162 gl_type = base_type;
164 else
166 WARN("Did not find a suitable GL resource type for resource type %s.\n",
167 debug_d3dresourcetype(type));
168 return WINED3DERR_INVALIDCALL;
172 if (base_type != WINED3D_GL_RES_TYPE_COUNT
173 && (format->flags[base_type] & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY))
174 == WINED3DFMT_FLAG_BLOCKS)
176 UINT width_mask = format->block_width - 1;
177 UINT height_mask = format->block_height - 1;
178 if (width & width_mask || height & height_mask)
179 return WINED3DERR_INVALIDCALL;
182 resource->ref = 1;
183 resource->device = device;
184 resource->type = type;
185 resource->gl_type = gl_type;
186 resource->format = format;
187 if (gl_type < WINED3D_GL_RES_TYPE_COUNT)
188 resource->format_flags = format->flags[gl_type];
189 resource->multisample_type = multisample_type;
190 resource->multisample_quality = multisample_quality;
191 resource->usage = usage;
192 resource->pool = pool;
193 resource->access_flags = resource_access_from_pool(pool);
194 if (usage & WINED3DUSAGE_DYNAMIC)
195 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
196 resource->width = width;
197 resource->height = height;
198 resource->depth = depth;
199 resource->size = size;
200 resource->priority = 0;
201 resource->parent = parent;
202 resource->parent_ops = parent_ops;
203 resource->resource_ops = resource_ops;
204 resource->map_binding = WINED3D_LOCATION_SYSMEM;
206 if (size)
208 if (!wined3d_resource_allocate_sysmem(resource))
210 ERR("Failed to allocate system memory.\n");
211 return E_OUTOFMEMORY;
214 else
216 resource->heap_memory = NULL;
219 /* Check that we have enough video ram left */
220 if (pool == WINED3D_POOL_DEFAULT && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
222 if (size > wined3d_device_get_available_texture_mem(device))
224 ERR("Out of adapter memory\n");
225 wined3d_resource_free_sysmem(resource);
226 return WINED3DERR_OUTOFVIDEOMEMORY;
228 adapter_adjust_memory(device->adapter, size);
231 device_resource_add(device, resource);
233 return WINED3D_OK;
236 void resource_cleanup(struct wined3d_resource *resource)
238 const struct wined3d *d3d = resource->device->wined3d;
240 TRACE("Cleaning up resource %p.\n", resource);
242 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
244 TRACE("Decrementing device memory pool by %u.\n", resource->size);
245 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
248 wined3d_resource_free_sysmem(resource);
250 device_resource_released(resource->device, resource);
253 void resource_unload(struct wined3d_resource *resource)
255 if (resource->map_count)
256 ERR("Resource %p is being unloaded while mapped.\n", resource);
259 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
261 DWORD prev;
263 if (resource->pool != WINED3D_POOL_MANAGED)
265 WARN("Called on non-managed resource %p, ignoring.\n", resource);
266 return 0;
269 prev = resource->priority;
270 resource->priority = priority;
271 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
272 return prev;
275 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
277 TRACE("resource %p, returning %u.\n", resource, resource->priority);
278 return resource->priority;
281 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
283 return resource->parent;
286 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
288 resource->parent = parent;
291 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
293 desc->resource_type = resource->type;
294 desc->format = resource->format->id;
295 desc->multisample_type = resource->multisample_type;
296 desc->multisample_quality = resource->multisample_quality;
297 desc->usage = resource->usage;
298 desc->pool = resource->pool;
299 desc->width = resource->width;
300 desc->height = resource->height;
301 desc->depth = resource->depth;
302 desc->size = resource->size;
305 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
306 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
308 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
309 resource, sub_resource_idx, map_desc, debug_box(box), flags);
311 return resource->resource_ops->resource_sub_resource_map(resource, sub_resource_idx, map_desc, box, flags);
314 HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
316 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
318 return resource->resource_ops->resource_sub_resource_unmap(resource, sub_resource_idx);
321 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
323 void **p;
324 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
325 void *mem;
327 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
328 return FALSE;
330 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
331 *p = mem;
333 resource->heap_memory = ++p;
335 return TRUE;
338 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
340 void **p = resource->heap_memory;
342 if (!p)
343 return;
345 HeapFree(GetProcessHeap(), 0, *(--p));
346 resource->heap_memory = NULL;
349 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
351 /* Not all flags make sense together, but Windows never returns an error.
352 * Catch the cases that could cause issues. */
353 if (flags & WINED3D_MAP_READONLY)
355 if (flags & WINED3D_MAP_DISCARD)
357 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
358 return 0;
360 if (flags & WINED3D_MAP_NOOVERWRITE)
362 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
363 return 0;
366 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
367 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
369 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
370 return 0;
372 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
373 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
375 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
376 return 0;
379 return flags;
382 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
384 GLbitfield ret = 0;
386 if (!(d3d_flags & WINED3D_MAP_READONLY))
387 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
388 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
389 ret |= GL_MAP_READ_BIT;
391 if (d3d_flags & WINED3D_MAP_DISCARD)
392 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
393 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
394 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
396 return ret;
399 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
401 if (d3d_flags & WINED3D_MAP_READONLY)
402 return GL_READ_ONLY_ARB;
403 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
404 return GL_WRITE_ONLY_ARB;
405 return GL_READ_WRITE_ARB;
408 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
410 struct wined3d_swapchain *swapchain;
412 /* Only 2D texture resources can be onscreen. */
413 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
414 return TRUE;
416 /* Not on a swapchain - must be offscreen */
417 if (!(swapchain = texture_from_resource(resource)->swapchain))
418 return TRUE;
420 /* The front buffer is always onscreen */
421 if (resource == &swapchain->front_buffer->resource)
422 return FALSE;
424 /* If the swapchain is rendered to an FBO, the backbuffer is
425 * offscreen, otherwise onscreen */
426 return swapchain->render_to_fbo;
429 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
431 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
432 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
433 else if (resource->multisample_type)
434 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
435 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
436 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
437 else
438 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;