TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / wined3d / resource.c
blobd466764b1771e50406228323d42e9a894a03ca14
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;
82 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
83 static const enum wined3d_gl_resource_type gl_resource_types[][4] =
85 /* 0 */ {WINED3D_GL_RES_TYPE_COUNT},
86 /* WINED3D_RTYPE_SURFACE */ {WINED3D_GL_RES_TYPE_COUNT},
87 /* WINED3D_RTYPE_VOLUME */ {WINED3D_GL_RES_TYPE_COUNT},
88 /* WINED3D_RTYPE_TEXTURE */ {WINED3D_GL_RES_TYPE_TEX_2D,
89 WINED3D_GL_RES_TYPE_TEX_RECT, WINED3D_GL_RES_TYPE_RB, WINED3D_GL_RES_TYPE_COUNT},
90 /* WINED3D_RTYPE_VOLUME_TEXTURE */ {WINED3D_GL_RES_TYPE_TEX_3D, WINED3D_GL_RES_TYPE_COUNT},
91 /* WINED3D_RTYPE_CUBE_TEXTURE */ {WINED3D_GL_RES_TYPE_TEX_CUBE, WINED3D_GL_RES_TYPE_COUNT},
92 /* WINED3D_RTYPE_BUFFER */ {WINED3D_GL_RES_TYPE_BUFFER, WINED3D_GL_RES_TYPE_COUNT},
94 enum wined3d_gl_resource_type gl_type = WINED3D_GL_RES_TYPE_COUNT;
95 enum wined3d_gl_resource_type base_type = gl_resource_types[type][0];
97 resource_check_usage(usage);
99 if (base_type != WINED3D_GL_RES_TYPE_COUNT)
101 unsigned int i;
102 BOOL tex_2d_ok = FALSE;
104 for (i = 0; (gl_type = gl_resource_types[type][i]) != WINED3D_GL_RES_TYPE_COUNT; i++)
106 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags[gl_type] & WINED3DFMT_FLAG_RENDERTARGET))
108 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format->id));
109 continue;
111 if ((usage & WINED3DUSAGE_DEPTHSTENCIL) &&
112 !(format->flags[gl_type] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
114 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format->id));
115 continue;
117 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
118 && usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)
119 && !(format->flags[gl_type] & WINED3DFMT_FLAG_FBO_ATTACHABLE))
121 WARN("Render target or depth stencil is not FBO attachable.\n");
122 continue;
124 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags[gl_type] & WINED3DFMT_FLAG_TEXTURE))
126 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format->id));
127 continue;
129 if (((width & (width - 1)) || (height & (height - 1)))
130 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
131 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
132 && gl_type == WINED3D_GL_RES_TYPE_TEX_2D)
134 TRACE("Skipping 2D texture type to try texture rectangle.\n");
135 tex_2d_ok = TRUE;
136 continue;
138 break;
141 if (gl_type == WINED3D_GL_RES_TYPE_COUNT)
143 if (tex_2d_ok)
145 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
146 * Use 2D textures, the texture code will pad to a power of 2 size. */
147 gl_type = WINED3D_GL_RES_TYPE_TEX_2D;
149 else if (pool == WINED3D_POOL_SCRATCH)
151 /* Needed for proper format information. */
152 gl_type = base_type;
154 else
156 WARN("Did not find a suitable GL resource type, resource type, d3d type %u.\n", type);
157 return WINED3DERR_INVALIDCALL;
162 if (base_type != WINED3D_GL_RES_TYPE_COUNT
163 && (format->flags[base_type] & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY))
164 == WINED3DFMT_FLAG_BLOCKS)
166 UINT width_mask = format->block_width - 1;
167 UINT height_mask = format->block_height - 1;
168 if (width & width_mask || height & height_mask)
169 return WINED3DERR_INVALIDCALL;
172 resource->ref = 1;
173 resource->device = device;
174 resource->type = type;
175 resource->gl_type = gl_type;
176 resource->format = format;
177 if (gl_type < WINED3D_GL_RES_TYPE_COUNT)
178 resource->format_flags = format->flags[gl_type];
179 resource->multisample_type = multisample_type;
180 resource->multisample_quality = multisample_quality;
181 resource->usage = usage;
182 resource->pool = pool;
183 resource->access_flags = resource_access_from_pool(pool);
184 if (usage & WINED3DUSAGE_DYNAMIC)
185 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
186 resource->width = width;
187 resource->height = height;
188 resource->depth = depth;
189 resource->size = size;
190 resource->priority = 0;
191 resource->parent = parent;
192 resource->parent_ops = parent_ops;
193 resource->resource_ops = resource_ops;
194 resource->map_binding = WINED3D_LOCATION_SYSMEM;
196 if (size)
198 if (!wined3d_resource_allocate_sysmem(resource))
200 ERR("Failed to allocate system memory.\n");
201 return E_OUTOFMEMORY;
204 else
206 resource->heap_memory = NULL;
209 /* Check that we have enough video ram left */
210 if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
212 if (size > wined3d_device_get_available_texture_mem(device))
214 ERR("Out of adapter memory\n");
215 wined3d_resource_free_sysmem(resource);
216 return WINED3DERR_OUTOFVIDEOMEMORY;
218 adapter_adjust_memory(device->adapter, size);
221 device_resource_add(device, resource);
223 return WINED3D_OK;
226 void resource_cleanup(struct wined3d_resource *resource)
228 const struct wined3d *d3d = resource->device->wined3d;
230 TRACE("Cleaning up resource %p.\n", resource);
232 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
234 TRACE("Decrementing device memory pool by %u.\n", resource->size);
235 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
238 wined3d_resource_free_sysmem(resource);
240 device_resource_released(resource->device, resource);
243 void resource_unload(struct wined3d_resource *resource)
245 if (resource->map_count)
246 ERR("Resource %p is being unloaded while mapped.\n", resource);
248 context_resource_unloaded(resource->device,
249 resource, resource->type);
252 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
254 DWORD prev;
256 if (resource->pool != WINED3D_POOL_MANAGED)
258 WARN("Called on non-managed resource %p, ignoring.\n", resource);
259 return 0;
262 prev = resource->priority;
263 resource->priority = priority;
264 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
265 return prev;
268 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
270 TRACE("resource %p, returning %u.\n", resource, resource->priority);
271 return resource->priority;
274 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
276 return resource->parent;
279 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
281 resource->parent = parent;
284 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
286 desc->resource_type = resource->type;
287 desc->format = resource->format->id;
288 desc->multisample_type = resource->multisample_type;
289 desc->multisample_quality = resource->multisample_quality;
290 desc->usage = resource->usage;
291 desc->pool = resource->pool;
292 desc->width = resource->width;
293 desc->height = resource->height;
294 desc->depth = resource->depth;
295 desc->size = resource->size;
298 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
299 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
301 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %p, flags %#x.\n",
302 resource, sub_resource_idx, map_desc, box, flags);
304 return resource->resource_ops->resource_sub_resource_map(resource, sub_resource_idx, map_desc, box, flags);
307 HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
309 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
311 return resource->resource_ops->resource_sub_resource_unmap(resource, sub_resource_idx);
314 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
316 void **p;
317 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
318 void *mem;
320 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
321 return FALSE;
323 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
324 *p = mem;
326 resource->heap_memory = ++p;
328 return TRUE;
331 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
333 void **p = resource->heap_memory;
335 if (!p)
336 return;
338 HeapFree(GetProcessHeap(), 0, *(--p));
339 resource->heap_memory = NULL;
342 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
344 /* Not all flags make sense together, but Windows never returns an error.
345 * Catch the cases that could cause issues. */
346 if (flags & WINED3D_MAP_READONLY)
348 if (flags & WINED3D_MAP_DISCARD)
350 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
351 return 0;
353 if (flags & WINED3D_MAP_NOOVERWRITE)
355 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
356 return 0;
359 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
360 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
362 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
363 return 0;
365 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
366 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
368 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
369 return 0;
372 return flags;
375 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
377 GLbitfield ret = 0;
379 if (!(d3d_flags & WINED3D_MAP_READONLY))
380 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
381 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
382 ret |= GL_MAP_READ_BIT;
384 if (d3d_flags & WINED3D_MAP_DISCARD)
385 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
386 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
387 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
389 return ret;
392 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
394 if (d3d_flags & WINED3D_MAP_READONLY)
395 return GL_READ_ONLY_ARB;
396 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
397 return GL_WRITE_ONLY_ARB;
398 return GL_READ_WRITE_ARB;
401 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
403 struct wined3d_swapchain *swapchain;
405 /* Only texture resources can be onscreen. */
406 if (resource->type != WINED3D_RTYPE_TEXTURE)
407 return TRUE;
409 /* Not on a swapchain - must be offscreen */
410 if (!(swapchain = wined3d_texture_from_resource(resource)->swapchain))
411 return TRUE;
413 /* The front buffer is always onscreen */
414 if (resource == &swapchain->front_buffer->resource)
415 return FALSE;
417 /* If the swapchain is rendered to an FBO, the backbuffer is
418 * offscreen, otherwise onscreen */
419 return swapchain->render_to_fbo;
422 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
424 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
425 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
426 else if (resource->multisample_type)
427 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
428 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
429 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
430 else
431 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;