ole32/tests: Add a trailing '\n' to ok() calls.
[wine.git] / dlls / wined3d / resource.c
blobcf01f933727dd8b499a82890ca7c707863290a88
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 | WINED3D_RESOURCE_ACCESS_MAP;
41 case WINED3D_POOL_SYSTEM_MEM:
42 return WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP;
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_WRITEONLY
55 | WINED3DUSAGE_DYNAMIC
56 | WINED3DUSAGE_AUTOGENMIPMAP
57 | WINED3DUSAGE_STATICDECL
58 | WINED3DUSAGE_OVERLAY
59 | WINED3DUSAGE_SCRATCH
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 if (usage & WINED3DUSAGE_SCRATCH && pool != WINED3D_POOL_SYSTEM_MEM)
109 ERR("WINED3DUSAGE_SCRATCH used with pool %s.\n", debug_d3dpool(pool));
110 return WINED3DERR_INVALIDCALL;
113 for (i = 0; i < ARRAY_SIZE(resource_types); ++i)
115 if (resource_types[i].type != type
116 || resource_types[i].cube_usage != (usage & WINED3DUSAGE_LEGACY_CUBEMAP))
117 continue;
119 gl_type = resource_types[i].gl_type;
120 if (base_type == WINED3D_GL_RES_TYPE_COUNT)
121 base_type = gl_type;
123 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags[gl_type] & WINED3DFMT_FLAG_RENDERTARGET))
125 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format->id));
126 continue;
128 if ((usage & WINED3DUSAGE_DEPTHSTENCIL)
129 && !(format->flags[gl_type] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
131 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format->id));
132 continue;
134 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
135 && usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)
136 && !(format->flags[gl_type] & WINED3DFMT_FLAG_FBO_ATTACHABLE))
138 WARN("Render target or depth stencil is not FBO attachable.\n");
139 continue;
141 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags[gl_type] & WINED3DFMT_FLAG_TEXTURE))
143 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format->id));
144 continue;
146 if (((width & (width - 1)) || (height & (height - 1)))
147 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
148 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
149 && gl_type == WINED3D_GL_RES_TYPE_TEX_2D)
151 TRACE("Skipping 2D texture type to try texture rectangle.\n");
152 tex_2d_ok = TRUE;
153 continue;
155 break;
158 if (base_type != WINED3D_GL_RES_TYPE_COUNT && i == ARRAY_SIZE(resource_types))
160 if (tex_2d_ok)
162 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
163 * Use 2D textures, the texture code will pad to a power of 2 size. */
164 gl_type = WINED3D_GL_RES_TYPE_TEX_2D;
166 else if (usage & WINED3DUSAGE_SCRATCH)
168 /* Needed for proper format information. */
169 gl_type = base_type;
171 else
173 WARN("Did not find a suitable GL resource type for resource type %s.\n",
174 debug_d3dresourcetype(type));
175 return WINED3DERR_INVALIDCALL;
179 if (base_type != WINED3D_GL_RES_TYPE_COUNT
180 && (format->flags[base_type] & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY))
181 == WINED3DFMT_FLAG_BLOCKS)
183 UINT width_mask = format->block_width - 1;
184 UINT height_mask = format->block_height - 1;
185 if (width & width_mask || height & height_mask)
186 return WINED3DERR_INVALIDCALL;
189 resource->ref = 1;
190 resource->device = device;
191 resource->type = type;
192 resource->gl_type = gl_type;
193 resource->format = format;
194 if (gl_type < WINED3D_GL_RES_TYPE_COUNT)
195 resource->format_flags = format->flags[gl_type];
196 resource->multisample_type = multisample_type;
197 resource->multisample_quality = multisample_quality;
198 resource->usage = usage;
199 resource->pool = pool;
200 resource->access = resource_access_from_pool(pool);
201 if (usage & WINED3DUSAGE_DYNAMIC)
202 resource->access |= WINED3D_RESOURCE_ACCESS_MAP;
203 resource->width = width;
204 resource->height = height;
205 resource->depth = depth;
206 resource->size = size;
207 resource->priority = 0;
208 resource->parent = parent;
209 resource->parent_ops = parent_ops;
210 resource->resource_ops = resource_ops;
211 resource->map_binding = WINED3D_LOCATION_SYSMEM;
213 if (size)
215 if (!wined3d_resource_allocate_sysmem(resource))
217 ERR("Failed to allocate system memory.\n");
218 return E_OUTOFMEMORY;
221 else
223 resource->heap_memory = NULL;
226 if (!(usage & WINED3DUSAGE_PRIVATE))
228 /* Check that we have enough video ram left */
229 if (pool == WINED3D_POOL_DEFAULT && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
231 if (size > wined3d_device_get_available_texture_mem(device))
233 ERR("Out of adapter memory\n");
234 wined3d_resource_free_sysmem(resource);
235 return WINED3DERR_OUTOFVIDEOMEMORY;
237 adapter_adjust_memory(device->adapter, size);
240 device_resource_add(device, resource);
243 return WINED3D_OK;
246 static void wined3d_resource_destroy_object(void *object)
248 struct wined3d_resource *resource = object;
250 wined3d_resource_free_sysmem(resource);
251 context_resource_released(resource->device, resource, resource->type);
252 wined3d_resource_release(resource);
255 void resource_cleanup(struct wined3d_resource *resource)
257 const struct wined3d *d3d = resource->device->wined3d;
259 TRACE("Cleaning up resource %p.\n", resource);
261 if (!(resource->usage & WINED3DUSAGE_PRIVATE))
263 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
265 TRACE("Decrementing device memory pool by %u.\n", resource->size);
266 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
269 device_resource_released(resource->device, resource);
271 wined3d_resource_acquire(resource);
272 wined3d_cs_destroy_object(resource->device->cs, wined3d_resource_destroy_object, resource);
275 void resource_unload(struct wined3d_resource *resource)
277 if (resource->map_count)
278 ERR("Resource %p is being unloaded while mapped.\n", resource);
281 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
283 DWORD prev;
285 if (resource->pool != WINED3D_POOL_MANAGED)
287 WARN("Called on non-managed resource %p, ignoring.\n", resource);
288 return 0;
291 prev = resource->priority;
292 resource->priority = priority;
293 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
294 return prev;
297 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
299 TRACE("resource %p, returning %u.\n", resource, resource->priority);
300 return resource->priority;
303 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
305 return resource->parent;
308 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
310 resource->parent = parent;
313 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
315 desc->resource_type = resource->type;
316 desc->format = resource->format->id;
317 desc->multisample_type = resource->multisample_type;
318 desc->multisample_quality = resource->multisample_quality;
319 desc->usage = resource->usage;
320 desc->pool = resource->pool;
321 desc->width = resource->width;
322 desc->height = resource->height;
323 desc->depth = resource->depth;
324 desc->size = resource->size;
327 static DWORD wined3d_resource_sanitise_map_flags(const struct wined3d_resource *resource, DWORD flags)
329 /* Not all flags make sense together, but Windows never returns an error.
330 * Catch the cases that could cause issues. */
331 if (flags & WINED3D_MAP_READONLY)
333 if (flags & WINED3D_MAP_DISCARD)
335 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
336 return 0;
338 if (flags & WINED3D_MAP_NOOVERWRITE)
340 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
341 return 0;
344 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
346 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
348 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
349 return 0;
351 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
352 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
354 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
355 flags &= ~WINED3D_MAP_DISCARD;
359 return flags;
362 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
363 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
365 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
366 resource, sub_resource_idx, map_desc, debug_box(box), flags);
368 flags = wined3d_resource_sanitise_map_flags(resource, flags);
369 wined3d_resource_wait_idle(resource);
371 return wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
374 HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
376 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
378 return wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
381 void CDECL wined3d_resource_preload(struct wined3d_resource *resource)
383 wined3d_cs_emit_preload_resource(resource->device->cs, resource);
386 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
388 void **p;
389 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
390 void *mem;
392 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
393 return FALSE;
395 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
396 *p = mem;
398 resource->heap_memory = ++p;
400 return TRUE;
403 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
405 void **p = resource->heap_memory;
407 if (!p)
408 return;
410 HeapFree(GetProcessHeap(), 0, *(--p));
411 resource->heap_memory = NULL;
414 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
416 GLbitfield ret = 0;
418 if (!(d3d_flags & WINED3D_MAP_READONLY))
419 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
420 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
421 ret |= GL_MAP_READ_BIT;
423 if (d3d_flags & WINED3D_MAP_DISCARD)
424 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
425 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
426 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
428 return ret;
431 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
433 if (d3d_flags & WINED3D_MAP_READONLY)
434 return GL_READ_ONLY_ARB;
435 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
436 return GL_WRITE_ONLY_ARB;
437 return GL_READ_WRITE_ARB;
440 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
442 struct wined3d_swapchain *swapchain;
444 /* Only 2D texture resources can be onscreen. */
445 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
446 return TRUE;
448 /* Not on a swapchain - must be offscreen */
449 if (!(swapchain = texture_from_resource(resource)->swapchain))
450 return TRUE;
452 /* The front buffer is always onscreen */
453 if (resource == &swapchain->front_buffer->resource)
454 return FALSE;
456 /* If the swapchain is rendered to an FBO, the backbuffer is
457 * offscreen, otherwise onscreen */
458 return swapchain->render_to_fbo;
461 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
463 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
464 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
465 else if (resource->multisample_type)
466 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
467 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
468 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
469 else
470 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;