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
&& type
!= WINED3D_RTYPE_BUFFER
)
86 if ((usage
& WINED3DUSAGE_RENDERTARGET
) && !(format
->flags
& WINED3DFMT_FLAG_RENDERTARGET
))
88 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format
->id
));
89 return WINED3DERR_INVALIDCALL
;
91 if ((usage
& WINED3DUSAGE_DEPTHSTENCIL
) && !(format
->flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
93 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format
->id
));
94 return WINED3DERR_INVALIDCALL
;
96 if ((usage
& WINED3DUSAGE_TEXTURE
) && !(format
->flags
& WINED3DFMT_FLAG_TEXTURE
))
98 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format
->id
));
99 return WINED3DERR_INVALIDCALL
;
104 resource
->device
= device
;
105 resource
->type
= type
;
106 resource
->format
= format
;
107 resource
->multisample_type
= multisample_type
;
108 resource
->multisample_quality
= multisample_quality
;
109 resource
->usage
= usage
;
110 resource
->pool
= pool
;
111 resource
->access_flags
= resource_access_from_pool(pool
);
112 if (usage
& WINED3DUSAGE_DYNAMIC
)
113 resource
->access_flags
|= WINED3D_RESOURCE_ACCESS_CPU
;
114 resource
->width
= width
;
115 resource
->height
= height
;
116 resource
->depth
= depth
;
117 resource
->size
= size
;
118 resource
->priority
= 0;
119 resource
->parent
= parent
;
120 resource
->parent_ops
= parent_ops
;
121 resource
->resource_ops
= resource_ops
;
122 resource
->map_binding
= WINED3D_LOCATION_SYSMEM
;
126 if (!wined3d_resource_allocate_sysmem(resource
))
128 ERR("Failed to allocate system memory.\n");
129 return E_OUTOFMEMORY
;
134 resource
->heap_memory
= NULL
;
137 /* Check that we have enough video ram left */
138 if (pool
== WINED3D_POOL_DEFAULT
&& d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
140 if (size
> wined3d_device_get_available_texture_mem(device
))
142 ERR("Out of adapter memory\n");
143 wined3d_resource_free_sysmem(resource
);
144 return WINED3DERR_OUTOFVIDEOMEMORY
;
146 adapter_adjust_memory(device
->adapter
, size
);
149 device_resource_add(device
, resource
);
154 void resource_cleanup(struct wined3d_resource
*resource
)
156 const struct wined3d
*d3d
= resource
->device
->wined3d
;
158 TRACE("Cleaning up resource %p.\n", resource
);
160 if (resource
->pool
== WINED3D_POOL_DEFAULT
&& d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
162 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
163 adapter_adjust_memory(resource
->device
->adapter
, (INT64
)0 - resource
->size
);
166 wined3d_resource_free_sysmem(resource
);
168 device_resource_released(resource
->device
, resource
);
171 void resource_unload(struct wined3d_resource
*resource
)
173 if (resource
->map_count
)
174 ERR("Resource %p is being unloaded while mapped.\n", resource
);
176 context_resource_unloaded(resource
->device
,
177 resource
, resource
->type
);
180 DWORD CDECL
wined3d_resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
184 if (resource
->pool
!= WINED3D_POOL_MANAGED
)
186 WARN("Called on non-managed resource %p, ignoring.\n", resource
);
190 prev
= resource
->priority
;
191 resource
->priority
= priority
;
192 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
196 DWORD CDECL
wined3d_resource_get_priority(const struct wined3d_resource
*resource
)
198 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
199 return resource
->priority
;
202 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
204 return resource
->parent
;
207 void CDECL
wined3d_resource_set_parent(struct wined3d_resource
*resource
, void *parent
)
209 resource
->parent
= parent
;
212 void CDECL
wined3d_resource_get_desc(const struct wined3d_resource
*resource
, struct wined3d_resource_desc
*desc
)
214 desc
->resource_type
= resource
->type
;
215 desc
->format
= resource
->format
->id
;
216 desc
->multisample_type
= resource
->multisample_type
;
217 desc
->multisample_quality
= resource
->multisample_quality
;
218 desc
->usage
= resource
->usage
;
219 desc
->pool
= resource
->pool
;
220 desc
->width
= resource
->width
;
221 desc
->height
= resource
->height
;
222 desc
->depth
= resource
->depth
;
223 desc
->size
= resource
->size
;
226 BOOL
wined3d_resource_allocate_sysmem(struct wined3d_resource
*resource
)
229 SIZE_T align
= RESOURCE_ALIGNMENT
- 1 + sizeof(*p
);
232 if (!(mem
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, resource
->size
+ align
)))
235 p
= (void **)(((ULONG_PTR
)mem
+ align
) & ~(RESOURCE_ALIGNMENT
- 1)) - 1;
238 resource
->heap_memory
= ++p
;
243 void wined3d_resource_free_sysmem(struct wined3d_resource
*resource
)
245 void **p
= resource
->heap_memory
;
250 HeapFree(GetProcessHeap(), 0, *(--p
));
251 resource
->heap_memory
= NULL
;
254 DWORD
wined3d_resource_sanitize_map_flags(const struct wined3d_resource
*resource
, DWORD flags
)
256 /* Not all flags make sense together, but Windows never returns an error.
257 * Catch the cases that could cause issues. */
258 if (flags
& WINED3D_MAP_READONLY
)
260 if (flags
& WINED3D_MAP_DISCARD
)
262 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
265 if (flags
& WINED3D_MAP_NOOVERWRITE
)
267 WARN("WINED3D_MAP_READONLY combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
271 else if ((flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
272 == (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
274 WARN("WINED3D_MAP_DISCARD and WINED3D_MAP_NOOVERWRITE used together, ignoring.\n");
277 else if (flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
)
278 && !(resource
->usage
& WINED3DUSAGE_DYNAMIC
))
280 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
287 GLbitfield
wined3d_resource_gl_map_flags(DWORD d3d_flags
)
291 if (!(d3d_flags
& WINED3D_MAP_READONLY
))
292 ret
|= GL_MAP_WRITE_BIT
| GL_MAP_FLUSH_EXPLICIT_BIT
;
293 if (!(d3d_flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
)))
294 ret
|= GL_MAP_READ_BIT
;
296 if (d3d_flags
& WINED3D_MAP_DISCARD
)
297 ret
|= GL_MAP_INVALIDATE_BUFFER_BIT
;
298 if (d3d_flags
& WINED3D_MAP_NOOVERWRITE
)
299 ret
|= GL_MAP_UNSYNCHRONIZED_BIT
;
304 GLenum
wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags
)
306 if (d3d_flags
& WINED3D_MAP_READONLY
)
307 return GL_READ_ONLY_ARB
;
308 if (d3d_flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
309 return GL_WRITE_ONLY_ARB
;
310 return GL_READ_WRITE_ARB
;
313 BOOL
wined3d_resource_is_offscreen(struct wined3d_resource
*resource
)
315 struct wined3d_swapchain
*swapchain
;
317 /* Only texture resources can be onscreen. */
318 if (resource
->type
!= WINED3D_RTYPE_TEXTURE
)
321 /* Not on a swapchain - must be offscreen */
322 if (!(swapchain
= wined3d_texture_from_resource(resource
)->swapchain
))
325 /* The front buffer is always onscreen */
326 if (resource
== &swapchain
->front_buffer
->resource
)
329 /* If the swapchain is rendered to an FBO, the backbuffer is
330 * offscreen, otherwise onscreen */
331 return swapchain
->render_to_fbo
;
334 void wined3d_resource_update_draw_binding(struct wined3d_resource
*resource
)
336 if (!wined3d_resource_is_offscreen(resource
) || wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
337 resource
->draw_binding
= WINED3D_LOCATION_DRAWABLE
;
338 else if (resource
->multisample_type
)
339 resource
->draw_binding
= WINED3D_LOCATION_RB_MULTISAMPLE
;
341 resource
->draw_binding
= WINED3D_LOCATION_TEXTURE_RGB
;