wined3d: Get rid of WINED3DUSAGE_TEXTURE.
[wine.git] / dlls / wined3d / resource.c
blob05fbafbfb35775b40944570b873207ed00dd847c
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_WRITEONLY
34 | WINED3DUSAGE_DYNAMIC
35 | WINED3DUSAGE_STATICDECL
36 | WINED3DUSAGE_OVERLAY
37 | WINED3DUSAGE_SCRATCH
38 | WINED3DUSAGE_PRIVATE
39 | WINED3DUSAGE_LEGACY_CUBEMAP
40 | ~WINED3DUSAGE_MASK;
42 /* WINED3DUSAGE_WRITEONLY is supposed to result in write-combined mappings
43 * being returned. OpenGL doesn't give us explicit control over that, but
44 * the hints and access flags we set for typical access patterns on
45 * dynamic resources should in theory have the same effect on the OpenGL
46 * driver. */
48 if (usage & ~handled)
49 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
50 if ((usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY)) == WINED3DUSAGE_DYNAMIC)
51 WARN_(d3d_perf)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
54 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
55 enum wined3d_resource_type type, const struct wined3d_format *format,
56 enum wined3d_multisample_type multisample_type, unsigned int multisample_quality, unsigned int usage,
57 unsigned int bind_flags, unsigned int access, unsigned int width, unsigned int height, unsigned int depth,
58 unsigned int size, void *parent, const struct wined3d_parent_ops *parent_ops,
59 const struct wined3d_resource_ops *resource_ops)
61 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
62 enum wined3d_gl_resource_type base_type = WINED3D_GL_RES_TYPE_COUNT;
63 enum wined3d_gl_resource_type gl_type = WINED3D_GL_RES_TYPE_COUNT;
64 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
65 BOOL tex_2d_ok = FALSE;
66 unsigned int i;
68 static const struct
70 enum wined3d_resource_type type;
71 DWORD cube_usage;
72 enum wined3d_gl_resource_type gl_type;
74 resource_types[] =
76 {WINED3D_RTYPE_BUFFER, 0, WINED3D_GL_RES_TYPE_BUFFER},
77 {WINED3D_RTYPE_TEXTURE_1D, 0, WINED3D_GL_RES_TYPE_TEX_1D},
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 (type != WINED3D_RTYPE_BUFFER)
109 if ((bind_flags & WINED3D_BIND_RENDER_TARGET)
110 && !(format->flags[gl_type] & WINED3DFMT_FLAG_RENDERTARGET))
112 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format->id));
113 continue;
115 if ((bind_flags & WINED3D_BIND_DEPTH_STENCIL)
116 && !(format->flags[gl_type] & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
118 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format->id));
119 continue;
121 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
122 && bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_DEPTH_STENCIL)
123 && !(format->flags[gl_type] & WINED3DFMT_FLAG_FBO_ATTACHABLE))
125 WARN("Render target or depth stencil is not FBO attachable.\n");
126 continue;
128 if ((bind_flags & WINED3D_BIND_SHADER_RESOURCE)
129 && !(format->flags[gl_type] & WINED3DFMT_FLAG_TEXTURE))
131 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format->id));
132 continue;
135 if (((width & (width - 1)) || (height & (height - 1)))
136 && !d3d_info->texture_npot
137 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
138 && gl_type == WINED3D_GL_RES_TYPE_TEX_2D)
140 TRACE("Skipping 2D texture type to try texture rectangle.\n");
141 tex_2d_ok = TRUE;
142 continue;
144 break;
147 if (base_type != WINED3D_GL_RES_TYPE_COUNT && i == ARRAY_SIZE(resource_types))
149 if (tex_2d_ok)
151 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
152 * Use 2D textures, the texture code will pad to a power of 2 size. */
153 gl_type = WINED3D_GL_RES_TYPE_TEX_2D;
155 else if (usage & WINED3DUSAGE_SCRATCH)
157 /* Needed for proper format information. */
158 gl_type = base_type;
160 else
162 WARN("Did not find a suitable GL resource type for resource type %s.\n",
163 debug_d3dresourcetype(type));
164 return WINED3DERR_INVALIDCALL;
168 if (base_type != WINED3D_GL_RES_TYPE_COUNT
169 && (format->flags[base_type] & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY))
170 == WINED3DFMT_FLAG_BLOCKS)
172 UINT width_mask = format->block_width - 1;
173 UINT height_mask = format->block_height - 1;
174 if (width & width_mask || height & height_mask)
175 return WINED3DERR_INVALIDCALL;
178 resource->ref = 1;
179 resource->device = device;
180 resource->type = type;
181 resource->gl_type = gl_type;
182 resource->format = format;
183 if (gl_type < WINED3D_GL_RES_TYPE_COUNT)
184 resource->format_flags = format->flags[gl_type];
185 resource->multisample_type = multisample_type;
186 resource->multisample_quality = multisample_quality;
187 resource->usage = usage;
188 resource->bind_flags = bind_flags;
189 resource->access = access;
190 resource->width = width;
191 resource->height = height;
192 resource->depth = depth;
193 resource->size = size;
194 resource->priority = 0;
195 resource->parent = parent;
196 resource->parent_ops = parent_ops;
197 resource->resource_ops = resource_ops;
198 resource->map_binding = WINED3D_LOCATION_SYSMEM;
199 resource->heap_memory = NULL;
201 if (!(usage & WINED3DUSAGE_PRIVATE))
203 /* Check that we have enough video ram left */
204 if (!(access & WINED3D_RESOURCE_ACCESS_CPU) && device->wined3d->flags & WINED3D_VIDMEM_ACCOUNTING)
206 if (size > wined3d_device_get_available_texture_mem(device))
208 ERR("Out of adapter memory.\n");
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);
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->bind_flags = resource->bind_flags;
295 desc->access = resource->access;
296 desc->width = resource->width;
297 desc->height = resource->height;
298 desc->depth = resource->depth;
299 desc->size = resource->size;
302 static DWORD wined3d_resource_sanitise_map_flags(const struct wined3d_resource *resource, DWORD flags)
304 /* Not all flags make sense together, but Windows never returns an error.
305 * Catch the cases that could cause issues. */
306 if (flags & WINED3D_MAP_READ)
308 if (flags & WINED3D_MAP_DISCARD)
310 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
311 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
313 if (flags & WINED3D_MAP_NOOVERWRITE)
315 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
316 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
319 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
321 if (!(resource->usage & WINED3DUSAGE_DYNAMIC))
323 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
324 return flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE);
326 if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
327 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
329 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
330 flags &= ~WINED3D_MAP_DISCARD;
334 return flags;
337 HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
338 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
340 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
341 resource, sub_resource_idx, map_desc, debug_box(box), flags);
343 if (!(flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE)))
345 WARN("No read/write flags specified.\n");
346 return E_INVALIDARG;
349 if ((flags & WINED3D_MAP_READ) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_R))
351 WARN("Resource does not have MAP_R access.\n");
352 return E_INVALIDARG;
355 if ((flags & WINED3D_MAP_WRITE) && !(resource->access & WINED3D_RESOURCE_ACCESS_MAP_W))
357 WARN("Resource does not have MAP_W access.\n");
358 return E_INVALIDARG;
361 flags = wined3d_resource_sanitise_map_flags(resource, flags);
362 wined3d_resource_wait_idle(resource);
364 return wined3d_cs_map(resource->device->cs, resource, sub_resource_idx, map_desc, box, flags);
367 HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
369 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
371 return wined3d_cs_unmap(resource->device->cs, resource, sub_resource_idx);
374 void CDECL wined3d_resource_preload(struct wined3d_resource *resource)
376 wined3d_cs_emit_preload_resource(resource->device->cs, resource);
379 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
381 void **p;
382 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
383 void *mem;
385 if (!(mem = heap_alloc_zero(resource->size + align)))
387 ERR("Failed to allocate system memory.\n");
388 return FALSE;
391 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
392 *p = mem;
394 resource->heap_memory = ++p;
396 return TRUE;
399 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
401 void **p = resource->heap_memory;
403 if (!p)
404 return;
406 heap_free(*(--p));
407 resource->heap_memory = NULL;
410 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
412 GLbitfield ret = 0;
414 if (d3d_flags & WINED3D_MAP_WRITE)
415 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
416 if (d3d_flags & WINED3D_MAP_READ)
417 ret |= GL_MAP_READ_BIT;
419 if (d3d_flags & WINED3D_MAP_DISCARD)
420 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
421 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
422 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
424 return ret;
427 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
429 switch (d3d_flags & (WINED3D_MAP_READ | WINED3D_MAP_WRITE))
431 case WINED3D_MAP_READ:
432 return GL_READ_ONLY_ARB;
434 case WINED3D_MAP_WRITE:
435 return GL_WRITE_ONLY_ARB;
437 default:
438 return GL_READ_WRITE_ARB;
442 BOOL wined3d_resource_is_offscreen(struct wined3d_resource *resource)
444 struct wined3d_swapchain *swapchain;
446 /* Only 2D texture resources can be onscreen. */
447 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
448 return TRUE;
450 /* Not on a swapchain - must be offscreen */
451 if (!(swapchain = texture_from_resource(resource)->swapchain))
452 return TRUE;
454 /* The front buffer is always onscreen */
455 if (resource == &swapchain->front_buffer->resource)
456 return FALSE;
458 /* If the swapchain is rendered to an FBO, the backbuffer is
459 * offscreen, otherwise onscreen */
460 return swapchain->render_to_fbo;
463 void wined3d_resource_update_draw_binding(struct wined3d_resource *resource)
465 if (!wined3d_resource_is_offscreen(resource) || wined3d_settings.offscreen_rendering_mode != ORM_FBO)
467 resource->draw_binding = WINED3D_LOCATION_DRAWABLE;
469 else if (resource->multisample_type)
471 const struct wined3d_gl_info *gl_info = &resource->device->adapter->gl_info;
472 if (gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
473 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;
474 else
475 resource->draw_binding = WINED3D_LOCATION_RB_MULTISAMPLE;
477 else if (resource->gl_type == WINED3D_GL_RES_TYPE_RB)
479 resource->draw_binding = WINED3D_LOCATION_RB_RESOLVED;
481 else
483 resource->draw_binding = WINED3D_LOCATION_TEXTURE_RGB;
487 const struct wined3d_format *wined3d_resource_get_decompress_format(const struct wined3d_resource *resource)
489 const struct wined3d_adapter *adapter = resource->device->adapter;
490 if (resource->format_flags & (WINED3DFMT_FLAG_SRGB_READ | WINED3DFMT_FLAG_SRGB_WRITE)
491 && !(adapter->d3d_info.wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL))
492 return wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM_SRGB, resource->bind_flags);
493 return wined3d_get_format(adapter, WINED3DFMT_B8G8R8A8_UNORM, resource->bind_flags);