wined3d: Do not write a FIXME about WINED3DUSAGE_WRITEONLY.
[wine.git] / dlls / wined3d / resource.c
blob3a54dd8a3f1dd284d82d780a9a952db009d72455
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 if (usage & ~handled)
63 FIXME("Unhandled usage flags %#x.\n", usage & ~handled);
64 if ((usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY)) == WINED3DUSAGE_DYNAMIC)
65 WARN_(d3d_perf)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
68 HRESULT resource_init(struct wined3d_resource *resource, struct wined3d_device *device,
69 enum wined3d_resource_type type, const struct wined3d_format *format,
70 enum wined3d_multisample_type multisample_type, UINT multisample_quality,
71 DWORD usage, enum wined3d_pool pool, UINT width, UINT height, UINT depth, UINT size,
72 void *parent, const struct wined3d_parent_ops *parent_ops,
73 const struct wined3d_resource_ops *resource_ops)
75 const struct wined3d *d3d = device->wined3d;
77 resource_check_usage(usage);
78 if (pool != WINED3D_POOL_SCRATCH)
80 if ((usage & WINED3DUSAGE_RENDERTARGET) && !(format->flags & WINED3DFMT_FLAG_RENDERTARGET))
81 return WINED3DERR_INVALIDCALL;
82 if ((usage & WINED3DUSAGE_DEPTHSTENCIL) && !(format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
83 return WINED3DERR_INVALIDCALL;
84 if ((usage & WINED3DUSAGE_TEXTURE) && !(format->flags & WINED3DFMT_FLAG_TEXTURE))
85 return WINED3DERR_INVALIDCALL;
88 resource->ref = 1;
89 resource->device = device;
90 resource->type = type;
91 resource->format = format;
92 resource->multisample_type = multisample_type;
93 resource->multisample_quality = multisample_quality;
94 resource->usage = usage;
95 resource->pool = pool;
96 resource->access_flags = resource_access_from_pool(pool);
97 if (usage & WINED3DUSAGE_DYNAMIC)
98 resource->access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
99 resource->width = width;
100 resource->height = height;
101 resource->depth = depth;
102 resource->size = size;
103 resource->priority = 0;
104 resource->parent = parent;
105 resource->parent_ops = parent_ops;
106 resource->resource_ops = resource_ops;
108 if (size)
110 if (!wined3d_resource_allocate_sysmem(resource))
112 ERR("Failed to allocate system memory.\n");
113 return E_OUTOFMEMORY;
116 else
118 resource->heap_memory = NULL;
121 /* Check that we have enough video ram left */
122 if (pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
124 if (size > wined3d_device_get_available_texture_mem(device))
126 ERR("Out of adapter memory\n");
127 wined3d_resource_free_sysmem(resource);
128 return WINED3DERR_OUTOFVIDEOMEMORY;
130 adapter_adjust_memory(device->adapter, size);
133 device_resource_add(device, resource);
135 return WINED3D_OK;
138 void resource_cleanup(struct wined3d_resource *resource)
140 const struct wined3d *d3d = resource->device->wined3d;
142 TRACE("Cleaning up resource %p.\n", resource);
144 if (resource->pool == WINED3D_POOL_DEFAULT && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
146 TRACE("Decrementing device memory pool by %u.\n", resource->size);
147 adapter_adjust_memory(resource->device->adapter, 0 - resource->size);
150 wined3d_resource_free_sysmem(resource);
152 device_resource_released(resource->device, resource);
155 void resource_unload(struct wined3d_resource *resource)
157 if (resource->map_count)
158 ERR("Resource %p is being unloaded while mapped.\n", resource);
160 context_resource_unloaded(resource->device,
161 resource, resource->type);
164 DWORD resource_set_priority(struct wined3d_resource *resource, DWORD priority)
166 DWORD prev = resource->priority;
167 resource->priority = priority;
168 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource, priority, prev);
169 return prev;
172 DWORD resource_get_priority(const struct wined3d_resource *resource)
174 TRACE("resource %p, returning %u.\n", resource, resource->priority);
175 return resource->priority;
178 void * CDECL wined3d_resource_get_parent(const struct wined3d_resource *resource)
180 return resource->parent;
183 void CDECL wined3d_resource_set_parent(struct wined3d_resource *resource, void *parent)
185 resource->parent = parent;
188 void CDECL wined3d_resource_get_desc(const struct wined3d_resource *resource, struct wined3d_resource_desc *desc)
190 desc->resource_type = resource->type;
191 desc->format = resource->format->id;
192 desc->multisample_type = resource->multisample_type;
193 desc->multisample_quality = resource->multisample_quality;
194 desc->usage = resource->usage;
195 desc->pool = resource->pool;
196 desc->width = resource->width;
197 desc->height = resource->height;
198 desc->depth = resource->depth;
199 desc->size = resource->size;
202 BOOL wined3d_resource_allocate_sysmem(struct wined3d_resource *resource)
204 void **p;
205 SIZE_T align = RESOURCE_ALIGNMENT - 1 + sizeof(*p);
206 void *mem;
208 if (!(mem = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, resource->size + align)))
209 return FALSE;
211 p = (void **)(((ULONG_PTR)mem + align) & ~(RESOURCE_ALIGNMENT - 1)) - 1;
212 *p = mem;
214 resource->heap_memory = ++p;
216 return TRUE;
219 void wined3d_resource_free_sysmem(struct wined3d_resource *resource)
221 void **p = resource->heap_memory;
223 if (!p)
224 return;
226 HeapFree(GetProcessHeap(), 0, *(--p));
227 resource->heap_memory = NULL;
230 DWORD wined3d_resource_sanitize_map_flags(const struct wined3d_resource *resource, DWORD flags)
232 /* Not all flags make sense together, but Windows never returns an error.
233 * Catch the cases that could cause issues. */
234 if (flags & WINED3D_MAP_READONLY)
236 if (flags & WINED3D_MAP_DISCARD)
238 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
239 return 0;
241 if (flags & WINED3D_MAP_NOOVERWRITE)
243 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
244 return 0;
247 else if ((flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
248 == (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
250 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
251 return 0;
253 else if (flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)
254 && !(resource->usage & WINED3DUSAGE_DYNAMIC))
256 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
257 return 0;
260 return flags;
263 GLbitfield wined3d_resource_gl_map_flags(DWORD d3d_flags)
265 GLbitfield ret = 0;
267 if (!(d3d_flags & WINED3D_MAP_READONLY))
268 ret |= GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT;
269 if (!(d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE)))
270 ret |= GL_MAP_READ_BIT;
272 if (d3d_flags & WINED3D_MAP_DISCARD)
273 ret |= GL_MAP_INVALIDATE_BUFFER_BIT;
274 if (d3d_flags & WINED3D_MAP_NOOVERWRITE)
275 ret |= GL_MAP_UNSYNCHRONIZED_BIT;
277 return ret;
280 GLenum wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags)
282 if (d3d_flags & WINED3D_MAP_READONLY)
283 return GL_READ_ONLY_ARB;
284 if (d3d_flags & (WINED3D_MAP_DISCARD | WINED3D_MAP_NOOVERWRITE))
285 return GL_WRITE_ONLY_ARB;
286 return GL_READ_WRITE_ARB;