wined3d: Resource size should always be non-zero in resource_init().
[wine.git] / dlls / wined3d / resource.c
blob8968c164ed570129a7d1d6fa7690aa2f8a41373a
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 void resource_check_usage(DWORD usage)
33 static const DWORD handled = WINED3DUSAGE_RENDERTARGET
34 | WINED3DUSAGE_DEPTHSTENCIL
35 | WINED3DUSAGE_WRITEONLY
36 | WINED3DUSAGE_DYNAMIC
37 | WINED3DUSAGE_STATICDECL
38 | WINED3DUSAGE_OVERLAY
39 | WINED3DUSAGE_SCRATCH
40 | WINED3DUSAGE_PRIVATE
41 | WINED3DUSAGE_LEGACY_CUBEMAP
42 | WINED3DUSAGE_TEXTURE;
44 /* WINED3DUSAGE_WRITEONLY is supposed to result in write-combined mappings
45 * being returned. OpenGL doesn't give us explicit control over that, but
46 * the hints and access flags we set for typical access patterns on
47 * dynamic resources should in theory have the same effect on the OpenGL
48 * driver. */
50 if (usage & ~handled)
51 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
52 if ((usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY)) == WINED3DUSAGE_DYNAMIC)
53 WARN_(d3d_perf)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
56 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
57 enum wined3d_resource_type type, const struct wined3d_format *format,
58 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality,
59 unsigned int usage, unsigned int access, unsigned int width, unsigned int height, unsigned int depth,
60 unsigned int size, void *parent, const struct wined3d_parent_ops *parent_ops,
61 const struct wined3d_resource_ops *resource_ops)
63 enum wined3d_gl_resource_type base_type = WINED3D_GL_RES_TYPE_COUNT;
64 enum wined3d_gl_resource_type gl_type = WINED3D_GL_RES_TYPE_COUNT;
65 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
66 BOOL tex_2d_ok = FALSE;
67 unsigned int i;
69 static const struct
71 enum wined3d_resource_type type;
72 DWORD cube_usage;
73 enum wined3d_gl_resource_type gl_type;
75 resource_types[] =
77 {WINED3D_RTYPE_BUFFER, 0, WINED3D_GL_RES_TYPE_BUFFER},
78 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_TEX_2D},
79 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_TEX_RECT},
80 {WINED3D_RTYPE_TEXTURE_2D, 0, WINED3D_GL_RES_TYPE_RB},
81 {WINED3D_RTYPE_TEXTURE_2D, WINED3DUSAGE_LEGACY_CUBEMAP, WINED3D_GL_RES_TYPE_TEX_CUBE},
82 {WINED3D_RTYPE_TEXTURE_3D, 0, WINED3D_GL_RES_TYPE_TEX_3D},
85 resource_check_usage(usage);
87 if (usage & WINED3DUSAGE_SCRATCH && access & WINED3D_RESOURCE_ACCESS_GPU)
89 ERR("Trying to create a scratch resource with access flags %s.\n",
90 wined3d_debug_resource_access(access));
91 return WINED3DERR_INVALIDCALL;
94 if (!size)
95 ERR("Attempting to create a zero-sized resource.\n");
97 for (i = 0; i < ARRAY_SIZE(resource_types); ++i)
99 if (resource_types[i].type != type
100 || resource_types[i].cube_usage != (usage & WINED3DUSAGE_LEGACY_CUBEMAP))
101 continue;
103 gl_type = resource_types[i].gl_type;
104 if (base_type == WINED3D_GL_RES_TYPE_COUNT)
105 base_type = gl_type;
107 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags[gl_type] & WINED3DFMT_FLAG_RENDERTARGET))
109 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format->id));
110 continue;
112 if ((usage & WINED3DUSAGE_DEPTHSTENCIL)
113 && !(format->flags[gl_type] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
115 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format->id));
116 continue;
118 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
119 && usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)
120 && !(format->flags[gl_type] & WINED3DFMT_FLAG_FBO_ATTACHABLE))
122 WARN("Render target or depth stencil is not FBO attachable.\n");
123 continue;
125 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags[gl_type] & WINED3DFMT_FLAG_TEXTURE))
127 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format->id));
128 continue;
130 if (((width & (width - 1)) || (height & (height - 1)))
131 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
132 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
133 && gl_type == WINED3D_GL_RES_TYPE_TEX_2D)
135 TRACE("Skipping 2D texture type to try texture rectangle.\n");
136 tex_2d_ok = TRUE;
137 continue;
139 break;
142 if (base_type != WINED3D_GL_RES_TYPE_COUNT && i == ARRAY_SIZE(resource_types))
144 if (tex_2d_ok)
146 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
147 * Use 2D textures, the texture code will pad to a power of 2 size. */
148 gl_type = WINED3D_GL_RES_TYPE_TEX_2D;
150 else if (usage & WINED3DUSAGE_SCRATCH)
152 /* Needed for proper format information. */
153 gl_type = base_type;
155 else
157 WARN("Did not find a suitable GL resource type for resource type %s.\n",
158 debug_d3dresourcetype(type));
159 return WINED3DERR_INVALIDCALL;
163 if (base_type != WINED3D_GL_RES_TYPE_COUNT
164 && (format->flags[base_type] & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY))
165 == WINED3DFMT_FLAG_BLOCKS)
167 UINT width_mask = format->block_width - 1;
168 UINT height_mask = format->block_height - 1;
169 if (width & width_mask || height & height_mask)
170 return WINED3DERR_INVALIDCALL;
173 resource->ref = 1;
174 resource->device = device;
175 resource->type = type;
176 resource->gl_type = gl_type;
177 resource->format = format;
178 if (gl_type < WINED3D_GL_RES_TYPE_COUNT)
179 resource->format_flags = format->flags[gl_type];
180 resource->multisample_type = multisample_type;
181 resource->multisample_quality = multisample_quality;
182 resource->usage = usage;
183 resource->access = access;
184 resource->width = width;
185 resource->height = height;
186 resource->depth = depth;
187 resource->size = size;
188 resource->priority = 0;
189 resource->parent = parent;
190 resource->parent_ops = parent_ops;
191 resource->resource_ops = resource_ops;
192 resource->map_binding = WINED3D_LOCATION_SYSMEM;
194 if (!wined3d_resource_allocate_sysmem(resource))
196 ERR("Failed to allocate system memory.\n");
197 return E_OUTOFMEMORY;
200 if (!(usage & WINED3DUSAGE_PRIVATE))
202 /* Check that we have enough video ram left */
203 if (!(access & WINED3D_RESOURCE_ACCESS_CPU) && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
205 if (size > wined3d_device_get_available_texture_mem(device))
207 ERR("Out of adapter memory\n");
208 wined3d_resource_free_sysmem(resource);
209 return WINED3DERR_OUTOFVIDEOMEMORY;
211 adapter_adjust_memory(device->adapter, size);
214 device_resource_add(device, resource);
217 return WINED3D_OK;
220 static void wined3d_resource_destroy_object(void *object)
222 struct wined3d_resource *resource = object;
224 wined3d_resource_free_sysmem(resource);
225 context_resource_released(resource->device, resource, resource->type);
226 wined3d_resource_release(resource);
229 void resource_cleanup(struct wined3d_resource *resource)
231 const struct wined3d *d3d = resource->device->wined3d;
233 TRACE("Cleaning up resource %p.\n", resource);
235 if (!(resource->usage & WINED3DUSAGE_PRIVATE))
237 if (!(resource->access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
239 TRACE("Decrementing device memory pool by %u.\n", resource->size);
240 adapter_adjust_memory(resource->device->adapter, (INT64)0 - resource->size);
243 device_resource_released(resource->device, resource);
245 wined3d_resource_acquire(resource);
246 wined3d_cs_destroy_object(resource->device->cs, wined3d_resource_destroy_object, resource);
249 void resource_unload(struct wined3d_resource *resource)
251 if (resource->map_count)
252 ERR("Resource %p is being unloaded while mapped.\n", resource);
255 DWORD CDECL wined3d_resource_set_priority(struct wined3d_resource *resource, DWORD priority)
257 DWORD prev;
259 if (!wined3d_resource_access_is_managed(resource->access))
261 WARN("Called on non-managed resource %p, ignoring.\n", resource);
262 return 0;
265 prev = resource->priority;
266 resource->priority = priority;
267 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
268 return prev;
271 DWORD CDECL wined3d_resource_get_priority(const struct wined3d_resource *resource)
273 TRACE("resource %p, returning %u.\n", resource, resource->priority);
274 return resource->priority;
277 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
279 return resource->parent;
282 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
284 resource->parent = parent;
287 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
289 desc->resource_type = resource->type;
290 desc->format = resource->format->id;
291 desc->multisample_type = resource->multisample_type;
292 desc->multisample_quality = resource->multisample_quality;
293 desc->usage = resource->usage;
294 desc->access = resource->access;
295 desc->width = resource->width;
296 desc->height = resource->height;
297 desc->depth = resource->depth;
298 desc->size = resource->size;
301 static DWORD wined3d_resource_sanitise_map_flags(const struct wined3d_resource *resource, DWORD flags)
303 /* Not all flags make sense together, but Windows never returns an error.
304 * Catch the cases that could cause issues. */
305 if (flags & WINED3D_MAP_READ)
307 if (flags & WINED3D_MAP_DISCARD)
309 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
310 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
312 if (flags & WINED3D_MAP_NOOVERWRITE)
314 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
315 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
318 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
320 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
322 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
323 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
325 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
326 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
328 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
329 flags &= ~WINED3D_MAP_DISCARD;
333 return flags;
336 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
337 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
339 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
340 resource, sub_resource_idx, map_desc, debug_box(box), flags);
342 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
344 WARN("No read/write flags specified.\n");
345 return E_INVALIDARG;
348 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
350 WARN("Resource does not have MAP_R access.\n");
351 return E_INVALIDARG;
354 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
356 WARN("Resource does not have MAP_W access.\n");
357 return E_INVALIDARG;
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 = heap_alloc_zero(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 heap_free(*(--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_WRITE)
411 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
412 if (d3d_flags & WINED3D_MAP_READ)
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 switch (d3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE))
427 case WINED3D_MAP_READ:
428 return GL_READ_ONLY_ARB;
430 case WINED3D_MAP_WRITE:
431 return GL_WRITE_ONLY_ARB;
433 default:
434 return GL_READ_WRITE_ARB;
438 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
440 struct wined3d_swapchain *swapchain;
442 /* Only 2D texture resources can be onscreen. */
443 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
444 return TRUE;
446 /* Not on a swapchain - must be offscreen */
447 if (!(swapchain = texture_from_resource(resource)->swapchain))
448 return TRUE;
450 /* The front buffer is always onscreen */
451 if (resource == &swapchain->front_buffer->resource)
452 return FALSE;
454 /* If the swapchain is rendered to an FBO, the backbuffer is
455 * offscreen, otherwise onscreen */
456 return swapchain->render_to_fbo;
459 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
461 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
463 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
465 else if (resource->multisample_type)
467 const struct wined3d_gl_info *gl_info = &resource->device->adapter->gl_info;
468 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
469 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;
470 else
471 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
473 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
475 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
477 else
479 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;