reg/tests: Test import with non-standard registry file headers.
[wine.git] / dlls / wined3d / resource.c
blob4e0f6a6c171c73c1a0e34ab2ee1a8f5ad86e334d
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 static void wined3d_resource_destroy_object(void *object)
238 struct wined3d_resource *resource = object;
240 wined3d_resource_free_sysmem(resource);
241 context_resource_released(resource->device, resource, resource->type);
242 wined3d_resource_release(resource);
245 void resource_cleanup(struct wined3d_resource *resource)
247 const struct wined3d *d3d = resource->device->wined3d;
249 TRACE("Cleaning up resource %p.\n", resource);
251 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
253 TRACE("Decrementing device memory pool by %u.\n", resource->size);
254 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
257 device_resource_released(resource->device, resource);
258 wined3d_resource_acquire(resource);
259 wined3d_cs_destroy_object(resource->device->cs, wined3d_resource_destroy_object, resource);
262 void resource_unload(struct wined3d_resource *resource)
264 if (resource->map_count)
265 ERR("Resource %p is being unloaded while mapped.\n", resource);
268 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
270 DWORD prev;
272 if (resource->pool != WINED3D_POOL_MANAGED)
274 WARN("Called on non-managed resource %p, ignoring.\n", resource);
275 return 0;
278 prev = resource->priority;
279 resource->priority = priority;
280 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
281 return prev;
284 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
286 TRACE("resource %p, returning %u.\n", resource, resource->priority);
287 return resource->priority;
290 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
292 return resource->parent;
295 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
297 resource->parent = parent;
300 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
302 desc->resource_type = resource->type;
303 desc->format = resource->format->id;
304 desc->multisample_type = resource->multisample_type;
305 desc->multisample_quality = resource->multisample_quality;
306 desc->usage = resource->usage;
307 desc->pool = resource->pool;
308 desc->width = resource->width;
309 desc->height = resource->height;
310 desc->depth = resource->depth;
311 desc->size = resource->size;
314 static DWORD wined3d_resource_sanitise_map_flags(const struct wined3d_resource *resource, DWORD flags)
316 /* Not all flags make sense together, but Windows never returns an error.
317 * Catch the cases that could cause issues. */
318 if (flags & WINED3D_MAP_READONLY)
320 if (flags & WINED3D_MAP_DISCARD)
322 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
323 return 0;
325 if (flags & WINED3D_MAP_NOOVERWRITE)
327 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
328 return 0;
331 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
332 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
334 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
335 return 0;
337 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
338 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
340 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
341 return 0;
344 return flags;
347 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
348 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
350 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
351 resource, sub_resource_idx, map_desc, debug_box(box), flags);
353 flags = wined3d_resource_sanitise_map_flags(resource, flags);
355 return wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
358 HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
360 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
362 return wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
365 void CDECL wined3d_resource_preload(struct wined3d_resource *resource)
367 wined3d_cs_emit_preload_resource(resource->device->cs, resource);
370 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
372 void **p;
373 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
374 void *mem;
376 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
377 return FALSE;
379 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
380 *p = mem;
382 resource->heap_memory = ++p;
384 return TRUE;
387 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
389 void **p = resource->heap_memory;
391 if (!p)
392 return;
394 HeapFree(GetProcessHeap(), 0, *(--p));
395 resource->heap_memory = NULL;
398 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
400 GLbitfield ret = 0;
402 if (!(d3d_flags & WINED3D_MAP_READONLY))
403 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
404 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
405 ret |= GL_MAP_READ_BIT;
407 if (d3d_flags & WINED3D_MAP_DISCARD)
408 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
409 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
410 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
412 return ret;
415 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
417 if (d3d_flags & WINED3D_MAP_READONLY)
418 return GL_READ_ONLY_ARB;
419 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
420 return GL_WRITE_ONLY_ARB;
421 return GL_READ_WRITE_ARB;
424 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
426 struct wined3d_swapchain *swapchain;
428 /* Only 2D texture resources can be onscreen. */
429 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
430 return TRUE;
432 /* Not on a swapchain - must be offscreen */
433 if (!(swapchain = texture_from_resource(resource)->swapchain))
434 return TRUE;
436 /* The front buffer is always onscreen */
437 if (resource == &swapchain->front_buffer->resource)
438 return FALSE;
440 /* If the swapchain is rendered to an FBO, the backbuffer is
441 * offscreen, otherwise onscreen */
442 return swapchain->render_to_fbo;
445 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
447 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
448 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
449 else if (resource->multisample_type)
450 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
451 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
452 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
453 else
454 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;