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
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
)
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
;
46 FIXME("Unhandled pool %#x.\n", pool
);
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 /* WINED3DUSAGE_WRITEONLY is supposed to result in write-combined mappings
63 * being returned. OpenGL doesn't give us explicit control over that, but
64 * the hints and access flags we set for typical access patterns on
65 * dynamic resources should in theory have the same effect on the OpenGL
69 FIXME("Unhandled usage flags %#x.\n", usage
& ~handled
);
70 if ((usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_WRITEONLY
)) == WINED3DUSAGE_DYNAMIC
)
71 WARN_(d3d_perf
)("WINED3DUSAGE_DYNAMIC used without WINED3DUSAGE_WRITEONLY.\n");
74 HRESULT
resource_init(struct wined3d_resource
*resource
, struct wined3d_device
*device
,
75 enum wined3d_resource_type type
, const struct wined3d_format
*format
,
76 enum wined3d_multisample_type multisample_type
, UINT multisample_quality
,
77 DWORD usage
, enum wined3d_pool pool
, UINT width
, UINT height
, UINT depth
, UINT size
,
78 void *parent
, const struct wined3d_parent_ops
*parent_ops
,
79 const struct wined3d_resource_ops
*resource_ops
)
81 const struct wined3d
*d3d
= device
->wined3d
;
83 resource_check_usage(usage
);
84 if (pool
!= WINED3D_POOL_SCRATCH
)
86 if ((usage
& WINED3DUSAGE_RENDERTARGET
) && !(format
->flags
& WINED3DFMT_FLAG_RENDERTARGET
))
87 return WINED3DERR_INVALIDCALL
;
88 if ((usage
& WINED3DUSAGE_DEPTHSTENCIL
) && !(format
->flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
89 return WINED3DERR_INVALIDCALL
;
90 if ((usage
& WINED3DUSAGE_TEXTURE
) && !(format
->flags
& WINED3DFMT_FLAG_TEXTURE
))
91 return WINED3DERR_INVALIDCALL
;
95 resource
->device
= device
;
96 resource
->type
= type
;
97 resource
->format
= format
;
98 resource
->multisample_type
= multisample_type
;
99 resource
->multisample_quality
= multisample_quality
;
100 resource
->usage
= usage
;
101 resource
->pool
= pool
;
102 resource
->access_flags
= resource_access_from_pool(pool
);
103 if (usage
& WINED3DUSAGE_DYNAMIC
)
104 resource
->access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
105 resource
->width
= width
;
106 resource
->height
= height
;
107 resource
->depth
= depth
;
108 resource
->size
= size
;
109 resource
->priority
= 0;
110 resource
->parent
= parent
;
111 resource
->parent_ops
= parent_ops
;
112 resource
->resource_ops
= resource_ops
;
116 if (!wined3d_resource_allocate_sysmem(resource
))
118 ERR("Failed to allocate system memory.\n");
119 return E_OUTOFMEMORY
;
124 resource
->heap_memory
= NULL
;
127 /* Check that we have enough video ram left */
128 if (pool
== WINED3D_POOL_DEFAULT
&& d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
130 if (size
> wined3d_device_get_available_texture_mem(device
))
132 ERR("Out of adapter memory\n");
133 wined3d_resource_free_sysmem(resource
);
134 return WINED3DERR_OUTOFVIDEOMEMORY
;
136 adapter_adjust_memory(device
->adapter
, size
);
139 device_resource_add(device
, resource
);
144 void resource_cleanup(struct wined3d_resource
*resource
)
146 const struct wined3d
*d3d
= resource
->device
->wined3d
;
148 TRACE("Cleaning up resource %p.\n", resource
);
150 if (resource
->pool
== WINED3D_POOL_DEFAULT
&& d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
152 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
153 adapter_adjust_memory(resource
->device
->adapter
, 0 - resource
->size
);
156 wined3d_resource_free_sysmem(resource
);
158 device_resource_released(resource
->device
, resource
);
161 void resource_unload(struct wined3d_resource
*resource
)
163 if (resource
->map_count
)
164 ERR("Resource %p is being unloaded while mapped.\n", resource
);
166 context_resource_unloaded(resource
->device
,
167 resource
, resource
->type
);
170 DWORD CDECL
wined3d_resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
174 if (resource
->pool
!= WINED3D_POOL_MANAGED
)
176 WARN("Called on non-managed resource %p, ignoring.\n", resource
);
180 prev
= resource
->priority
;
181 resource
->priority
= priority
;
182 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
186 DWORD CDECL
wined3d_resource_get_priority(const struct wined3d_resource
*resource
)
188 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
189 return resource
->priority
;
192 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
194 return resource
->parent
;
197 void CDECL
wined3d_resource_set_parent(struct wined3d_resource
*resource
, void *parent
)
199 resource
->parent
= parent
;
202 void CDECL
wined3d_resource_get_desc(const struct wined3d_resource
*resource
, struct wined3d_resource_desc
*desc
)
204 desc
->resource_type
= resource
->type
;
205 desc
->format
= resource
->format
->id
;
206 desc
->multisample_type
= resource
->multisample_type
;
207 desc
->multisample_quality
= resource
->multisample_quality
;
208 desc
->usage
= resource
->usage
;
209 desc
->pool
= resource
->pool
;
210 desc
->width
= resource
->width
;
211 desc
->height
= resource
->height
;
212 desc
->depth
= resource
->depth
;
213 desc
->size
= resource
->size
;
216 BOOL
wined3d_resource_allocate_sysmem(struct wined3d_resource
*resource
)
219 SIZE_T align
= RESOURCE_ALIGNMENT
- 1 + sizeof(*p
);
222 if (!(mem
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, resource
->size
+ align
)))
225 p
= (void **)(((ULONG_PTR
)mem
+ align
) & ~(RESOURCE_ALIGNMENT
- 1)) - 1;
228 resource
->heap_memory
= ++p
;
233 void wined3d_resource_free_sysmem(struct wined3d_resource
*resource
)
235 void **p
= resource
->heap_memory
;
240 HeapFree(GetProcessHeap(), 0, *(--p
));
241 resource
->heap_memory
= NULL
;
244 DWORD
wined3d_resource_sanitize_map_flags(const struct wined3d_resource
*resource
, DWORD flags
)
246 /* Not all flags make sense together, but Windows never returns an error.
247 * Catch the cases that could cause issues. */
248 if (flags
& WINED3D_MAP_READONLY
)
250 if (flags
& WINED3D_MAP_DISCARD
)
252 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
255 if (flags
& WINED3D_MAP_NOOVERWRITE
)
257 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
261 else if ((flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
262 == (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
264 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
267 else if (flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
)
268 && !(resource
->usage
& WINED3DUSAGE_DYNAMIC
))
270 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
277 GLbitfield
wined3d_resource_gl_map_flags(DWORD d3d_flags
)
281 if (!(d3d_flags
& WINED3D_MAP_READONLY
))
282 ret
|= GL_MAP_WRITE_BIT
| GL_MAP_FLUSH_EXPLICIT_BIT
;
283 if (!(d3d_flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
)))
284 ret
|= GL_MAP_READ_BIT
;
286 if (d3d_flags
& WINED3D_MAP_DISCARD
)
287 ret
|= GL_MAP_INVALIDATE_BUFFER_BIT
;
288 if (d3d_flags
& WINED3D_MAP_NOOVERWRITE
)
289 ret
|= GL_MAP_UNSYNCHRONIZED_BIT
;
294 GLenum
wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags
)
296 if (d3d_flags
& WINED3D_MAP_READONLY
)
297 return GL_READ_ONLY_ARB
;
298 if (d3d_flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
299 return GL_WRITE_ONLY_ARB
;
300 return GL_READ_WRITE_ARB
;
303 BOOL
wined3d_resource_is_offscreen(struct wined3d_resource
*resource
)
305 struct wined3d_swapchain
*swapchain
;
307 if (resource
->type
== WINED3D_RTYPE_SURFACE
)
308 resource
= &surface_from_resource(resource
)->container
->resource
;
310 /* Only texture resources can be onscreen. */
311 if (resource
->type
!= WINED3D_RTYPE_TEXTURE
)
314 /* Not on a swapchain - must be offscreen */
315 if (!(swapchain
= wined3d_texture_from_resource(resource
)->swapchain
))
318 /* The front buffer is always onscreen */
319 if (resource
== &swapchain
->front_buffer
->resource
)
322 /* If the swapchain is rendered to an FBO, the backbuffer is
323 * offscreen, otherwise onscreen */
324 return swapchain
->render_to_fbo
;
327 void wined3d_resource_update_draw_binding(struct wined3d_resource
*resource
)
329 if (!wined3d_resource_is_offscreen(resource
) || wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
330 resource
->draw_binding
= WINED3D_LOCATION_DRAWABLE
;
331 else if (resource
->multisample_type
)
332 resource
->draw_binding
= WINED3D_LOCATION_RB_MULTISAMPLE
;
334 resource
->draw_binding
= WINED3D_LOCATION_TEXTURE_RGB
;