ntdll: Call NtRaiseException directly from RtlRaiseException on x86-64.
[wine.git] / dlls / wined3d / resource.c
blob40890cc99e80bae454f8808b9f53c1ffefd8605a
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_PRIVATE
61 | WINED3DUSAGE_LEGACY_CUBEMAP
62 | WINED3DUSAGE_TEXTURE;
64 /* WINED3DUSAGE_WRITEONLY is supposed to result in write-combined mappings
65 * being returned. OpenGL doesn't give us explicit control over that, but
66 * the hints and access flags we set for typical access patterns on
67 * dynamic resources should in theory have the same effect on the OpenGL
68 * driver. */
70 if (usage & ~handled)
71 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
72 if ((usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY)) == WINED3DUSAGE_DYNAMIC)
73 WARN_(d3d_perf)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
76 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
77 enum wined3d_resource_type type, const struct wined3d_format *format,
78 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
79 DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
80 void *parent, const struct wined3d_parent_ops *parent_ops,
81 const struct wined3d_resource_ops *resource_ops)
83 enum wined3d_gl_resource_type base_type = WINED3D_GL_RES_TYPE_COUNT;
84 enum wined3d_gl_resource_type gl_type = WINED3D_GL_RES_TYPE_COUNT;
85 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
86 BOOL tex_2d_ok = FALSE;
87 unsigned int i;
89 static const struct
91 enum wined3d_resource_type type;
92 DWORD cube_usage;
93 enum wined3d_gl_resource_type gl_type;
95 resource_types[] =
97 {WINED3D_RTYPE_BUFFER, 0, WINED3D_GL_RES_TYPE_BUFFER},
98 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_TEX_2D},
99 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_TEX_RECT},
100 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_RB},
101 {WINED3D_RTYPE_TEXTURE_2D, WINED3DUSAGE_LEGACY_CUBEMAP, WINED3D_GL_RES_TYPE_TEX_CUBE},
102 {WINED3D_RTYPE_TEXTURE_3D, 0, WINED3D_GL_RES_TYPE_TEX_3D},
105 resource_check_usage(usage);
107 for (i = 0; i < ARRAY_SIZE(resource_types); ++i)
109 if (resource_types[i].type != type
110 || resource_types[i].cube_usage != (usage & WINED3DUSAGE_LEGACY_CUBEMAP))
111 continue;
113 gl_type = resource_types[i].gl_type;
114 if (base_type == WINED3D_GL_RES_TYPE_COUNT)
115 base_type = gl_type;
117 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags[gl_type] & WINED3DFMT_FLAG_RENDERTARGET))
119 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format->id));
120 continue;
122 if ((usage & WINED3DUSAGE_DEPTHSTENCIL)
123 && !(format->flags[gl_type] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
125 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format->id));
126 continue;
128 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
129 && usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)
130 && !(format->flags[gl_type] & WINED3DFMT_FLAG_FBO_ATTACHABLE))
132 WARN("Render target or depth stencil is not FBO attachable.\n");
133 continue;
135 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags[gl_type] & WINED3DFMT_FLAG_TEXTURE))
137 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format->id));
138 continue;
140 if (((width & (width - 1)) || (height & (height - 1)))
141 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
142 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
143 && gl_type == WINED3D_GL_RES_TYPE_TEX_2D)
145 TRACE("Skipping 2D texture type to try texture rectangle.\n");
146 tex_2d_ok = TRUE;
147 continue;
149 break;
152 if (base_type != WINED3D_GL_RES_TYPE_COUNT && i == ARRAY_SIZE(resource_types))
154 if (tex_2d_ok)
156 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
157 * Use 2D textures, the texture code will pad to a power of 2 size. */
158 gl_type = WINED3D_GL_RES_TYPE_TEX_2D;
160 else if (pool == WINED3D_POOL_SCRATCH)
162 /* Needed for proper format information. */
163 gl_type = base_type;
165 else
167 WARN("Did not find a suitable GL resource type for resource type %s.\n",
168 debug_d3dresourcetype(type));
169 return WINED3DERR_INVALIDCALL;
173 if (base_type != WINED3D_GL_RES_TYPE_COUNT
174 && (format->flags[base_type] & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY))
175 == WINED3DFMT_FLAG_BLOCKS)
177 UINT width_mask = format->block_width - 1;
178 UINT height_mask = format->block_height - 1;
179 if (width & width_mask || height & height_mask)
180 return WINED3DERR_INVALIDCALL;
183 resource->ref = 1;
184 resource->device = device;
185 resource->type = type;
186 resource->gl_type = gl_type;
187 resource->format = format;
188 if (gl_type < WINED3D_GL_RES_TYPE_COUNT)
189 resource->format_flags = format->flags[gl_type];
190 resource->multisample_type = multisample_type;
191 resource->multisample_quality = multisample_quality;
192 resource->usage = usage;
193 resource->pool = pool;
194 resource->access_flags = resource_access_from_pool(pool);
195 if (usage & WINED3DUSAGE_DYNAMIC)
196 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
197 resource->width = width;
198 resource->height = height;
199 resource->depth = depth;
200 resource->size = size;
201 resource->priority = 0;
202 resource->parent = parent;
203 resource->parent_ops = parent_ops;
204 resource->resource_ops = resource_ops;
205 resource->map_binding = WINED3D_LOCATION_SYSMEM;
207 if (size)
209 if (!wined3d_resource_allocate_sysmem(resource))
211 ERR("Failed to allocate system memory.\n");
212 return E_OUTOFMEMORY;
215 else
217 resource->heap_memory = NULL;
220 if (!(usage & WINED3DUSAGE_PRIVATE))
222 /* Check that we have enough video ram left */
223 if (pool == WINED3D_POOL_DEFAULT && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
225 if (size > wined3d_device_get_available_texture_mem(device))
227 ERR("Out of adapter memory\n");
228 wined3d_resource_free_sysmem(resource);
229 return WINED3DERR_OUTOFVIDEOMEMORY;
231 adapter_adjust_memory(device->adapter, size);
234 device_resource_add(device, resource);
237 return WINED3D_OK;
240 static void wined3d_resource_destroy_object(void *object)
242 struct wined3d_resource *resource = object;
244 wined3d_resource_free_sysmem(resource);
245 context_resource_released(resource->device, resource, resource->type);
246 wined3d_resource_release(resource);
249 void resource_cleanup(struct wined3d_resource *resource)
251 const struct wined3d *d3d = resource->device->wined3d;
253 TRACE("Cleaning up resource %p.\n", resource);
255 if (!(resource->usage & WINED3DUSAGE_PRIVATE))
257 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
259 TRACE("Decrementing device memory pool by %u.\n", resource->size);
260 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
263 device_resource_released(resource->device, resource);
265 wined3d_resource_acquire(resource);
266 wined3d_cs_destroy_object(resource->device->cs, wined3d_resource_destroy_object, resource);
269 void resource_unload(struct wined3d_resource *resource)
271 if (resource->map_count)
272 ERR("Resource %p is being unloaded while mapped.\n", resource);
275 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
277 DWORD prev;
279 if (resource->pool != WINED3D_POOL_MANAGED)
281 WARN("Called on non-managed resource %p, ignoring.\n", resource);
282 return 0;
285 prev = resource->priority;
286 resource->priority = priority;
287 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
288 return prev;
291 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
293 TRACE("resource %p, returning %u.\n", resource, resource->priority);
294 return resource->priority;
297 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
299 return resource->parent;
302 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
304 resource->parent = parent;
307 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
309 desc->resource_type = resource->type;
310 desc->format = resource->format->id;
311 desc->multisample_type = resource->multisample_type;
312 desc->multisample_quality = resource->multisample_quality;
313 desc->usage = resource->usage;
314 desc->pool = resource->pool;
315 desc->width = resource->width;
316 desc->height = resource->height;
317 desc->depth = resource->depth;
318 desc->size = resource->size;
321 static DWORD wined3d_resource_sanitise_map_flags(const struct wined3d_resource *resource, DWORD flags)
323 /* Not all flags make sense together, but Windows never returns an error.
324 * Catch the cases that could cause issues. */
325 if (flags & WINED3D_MAP_READONLY)
327 if (flags & WINED3D_MAP_DISCARD)
329 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
330 return 0;
332 if (flags & WINED3D_MAP_NOOVERWRITE)
334 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
335 return 0;
338 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
339 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
341 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
342 return 0;
344 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
345 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
347 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
348 return 0;
351 return flags;
354 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
355 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
357 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
358 resource, sub_resource_idx, map_desc, debug_box(box), flags);
360 flags = wined3d_resource_sanitise_map_flags(resource, flags);
361 wined3d_resource_wait_idle(resource);
363 return wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
366 HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
368 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
370 return wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
373 void CDECL wined3d_resource_preload(struct wined3d_resource *resource)
375 wined3d_cs_emit_preload_resource(resource->device->cs, resource);
378 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
380 void **p;
381 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
382 void *mem;
384 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
385 return FALSE;
387 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
388 *p = mem;
390 resource->heap_memory = ++p;
392 return TRUE;
395 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
397 void **p = resource->heap_memory;
399 if (!p)
400 return;
402 HeapFree(GetProcessHeap(), 0, *(--p));
403 resource->heap_memory = NULL;
406 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
408 GLbitfield ret = 0;
410 if (!(d3d_flags & WINED3D_MAP_READONLY))
411 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
412 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
413 ret |= GL_MAP_READ_BIT;
415 if (d3d_flags & WINED3D_MAP_DISCARD)
416 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
417 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
418 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
420 return ret;
423 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
425 if (d3d_flags & WINED3D_MAP_READONLY)
426 return GL_READ_ONLY_ARB;
427 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
428 return GL_WRITE_ONLY_ARB;
429 return GL_READ_WRITE_ARB;
432 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
434 struct wined3d_swapchain *swapchain;
436 /* Only 2D texture resources can be onscreen. */
437 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
438 return TRUE;
440 /* Not on a swapchain - must be offscreen */
441 if (!(swapchain = texture_from_resource(resource)->swapchain))
442 return TRUE;
444 /* The front buffer is always onscreen */
445 if (resource == &swapchain->front_buffer->resource)
446 return FALSE;
448 /* If the swapchain is rendered to an FBO, the backbuffer is
449 * offscreen, otherwise onscreen */
450 return swapchain->render_to_fbo;
453 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
455 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
456 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
457 else if (resource->multisample_type)
458 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
459 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
460 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
461 else
462 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;