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 void resource_check_usage(DWORD usage
, unsigned int access
)
33 static const DWORD handled
= WINED3DUSAGE_DYNAMIC
34 | WINED3DUSAGE_STATICDECL
35 | WINED3DUSAGE_OVERLAY
36 | WINED3DUSAGE_SCRATCH
37 | WINED3DUSAGE_PRIVATE
38 | WINED3DUSAGE_LEGACY_CUBEMAP
41 /* Write-only CPU access is supposed to result in write-combined mappings
42 * being returned. OpenGL doesn't give us explicit control over that, but
43 * the hints and access flags we set for typical access patterns on
44 * dynamic resources should in theory have the same effect on the OpenGL
48 FIXME("Unhandled usage flags %#x.\n", usage
& ~handled
);
49 if (usage
& WINED3DUSAGE_DYNAMIC
&& access
& WINED3D_RESOURCE_ACCESS_MAP_R
)
50 WARN_(d3d_perf
)("WINED3DUSAGE_DYNAMIC used with WINED3D_RESOURCE_ACCESS_MAP_R.\n");
53 HRESULT
resource_init(struct wined3d_resource
*resource
, struct wined3d_device
*device
,
54 enum wined3d_resource_type type
, const struct wined3d_format
*format
,
55 enum wined3d_multisample_type multisample_type
, unsigned int multisample_quality
, unsigned int usage
,
56 unsigned int bind_flags
, unsigned int access
, unsigned int width
, unsigned int height
, unsigned int depth
,
57 unsigned int size
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
58 const struct wined3d_resource_ops
*resource_ops
)
60 const struct wined3d_d3d_info
*d3d_info
= &device
->adapter
->d3d_info
;
61 enum wined3d_gl_resource_type base_type
= WINED3D_GL_RES_TYPE_COUNT
;
62 enum wined3d_gl_resource_type gl_type
= WINED3D_GL_RES_TYPE_COUNT
;
63 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
64 BOOL tex_2d_ok
= FALSE
;
69 enum wined3d_resource_type type
;
71 enum wined3d_gl_resource_type gl_type
;
75 {WINED3D_RTYPE_BUFFER
, 0, WINED3D_GL_RES_TYPE_BUFFER
},
76 {WINED3D_RTYPE_TEXTURE_1D
, 0, WINED3D_GL_RES_TYPE_TEX_1D
},
77 {WINED3D_RTYPE_TEXTURE_2D
, 0, WINED3D_GL_RES_TYPE_TEX_2D
},
78 {WINED3D_RTYPE_TEXTURE_2D
, 0, WINED3D_GL_RES_TYPE_TEX_RECT
},
79 {WINED3D_RTYPE_TEXTURE_2D
, 0, WINED3D_GL_RES_TYPE_RB
},
80 {WINED3D_RTYPE_TEXTURE_2D
, WINED3DUSAGE_LEGACY_CUBEMAP
, WINED3D_GL_RES_TYPE_TEX_CUBE
},
81 {WINED3D_RTYPE_TEXTURE_3D
, 0, WINED3D_GL_RES_TYPE_TEX_3D
},
84 resource_check_usage(usage
, access
);
86 if (usage
& WINED3DUSAGE_SCRATCH
&& access
& WINED3D_RESOURCE_ACCESS_GPU
)
88 ERR("Trying to create a scratch resource with access flags %s.\n",
89 wined3d_debug_resource_access(access
));
90 return WINED3DERR_INVALIDCALL
;
93 if (bind_flags
& (WINED3D_BIND_RENDER_TARGET
| WINED3D_BIND_DEPTH_STENCIL
))
95 if ((access
& (WINED3D_RESOURCE_ACCESS_CPU
| WINED3D_RESOURCE_ACCESS_GPU
)) != WINED3D_RESOURCE_ACCESS_GPU
)
97 WARN("Bind flags %s are incompatible with resource access %s.\n",
98 wined3d_debug_bind_flags(bind_flags
), wined3d_debug_resource_access(access
));
99 return WINED3DERR_INVALIDCALL
;
102 /* Dynamic usage is incompatible with GPU writes. */
103 if (usage
& WINED3DUSAGE_DYNAMIC
)
105 WARN("Bind flags %s are incompatible with resource usage %s.\n",
106 wined3d_debug_bind_flags(bind_flags
), debug_d3dusage(usage
));
107 return WINED3DERR_INVALIDCALL
;
112 ERR("Attempting to create a zero-sized resource.\n");
114 for (i
= 0; i
< ARRAY_SIZE(resource_types
); ++i
)
116 if (resource_types
[i
].type
!= type
117 || resource_types
[i
].cube_usage
!= (usage
& WINED3DUSAGE_LEGACY_CUBEMAP
))
120 gl_type
= resource_types
[i
].gl_type
;
121 if (base_type
== WINED3D_GL_RES_TYPE_COUNT
)
124 if (type
!= WINED3D_RTYPE_BUFFER
)
126 if ((bind_flags
& WINED3D_BIND_RENDER_TARGET
)
127 && !(format
->flags
[gl_type
] & WINED3DFMT_FLAG_RENDERTARGET
))
129 WARN("Format %s cannot be used for render targets.\n", debug_d3dformat(format
->id
));
132 if ((bind_flags
& WINED3D_BIND_DEPTH_STENCIL
)
133 && !(format
->flags
[gl_type
] & WINED3DFMT_FLAG_DEPTH_STENCIL
))
135 WARN("Format %s cannot be used for depth/stencil buffers.\n", debug_d3dformat(format
->id
));
138 if ((bind_flags
& WINED3D_BIND_SHADER_RESOURCE
)
139 && !(format
->flags
[gl_type
] & WINED3DFMT_FLAG_TEXTURE
))
141 WARN("Format %s cannot be used for texturing.\n", debug_d3dformat(format
->id
));
145 if (((width
& (width
- 1)) || (height
& (height
- 1)))
146 && !d3d_info
->texture_npot
147 && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
]
148 && gl_type
== WINED3D_GL_RES_TYPE_TEX_2D
)
150 TRACE("Skipping 2D texture type to try texture rectangle.\n");
157 if (base_type
!= WINED3D_GL_RES_TYPE_COUNT
&& i
== ARRAY_SIZE(resource_types
))
161 /* Non power of 2 texture and rectangle textures or renderbuffers do not work.
162 * Use 2D textures, the texture code will pad to a power of 2 size. */
163 gl_type
= WINED3D_GL_RES_TYPE_TEX_2D
;
165 else if (usage
& WINED3DUSAGE_SCRATCH
)
167 /* Needed for proper format information. */
172 WARN("Did not find a suitable GL resource type for resource type %s.\n",
173 debug_d3dresourcetype(type
));
174 return WINED3DERR_INVALIDCALL
;
178 if (base_type
!= WINED3D_GL_RES_TYPE_COUNT
179 && (format
->flags
[base_type
] & (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BLOCKS_NO_VERIFY
))
180 == WINED3DFMT_FLAG_BLOCKS
)
182 UINT width_mask
= format
->block_width
- 1;
183 UINT height_mask
= format
->block_height
- 1;
184 if (width
& width_mask
|| height
& height_mask
)
185 return WINED3DERR_INVALIDCALL
;
189 resource
->device
= device
;
190 resource
->type
= type
;
191 resource
->gl_type
= gl_type
;
192 resource
->format
= format
;
193 if (gl_type
< WINED3D_GL_RES_TYPE_COUNT
)
194 resource
->format_flags
= format
->flags
[gl_type
];
195 resource
->multisample_type
= multisample_type
;
196 resource
->multisample_quality
= multisample_quality
;
197 resource
->usage
= usage
;
198 resource
->bind_flags
= bind_flags
;
199 if (resource
->format_flags
& WINED3DFMT_FLAG_MAPPABLE
)
200 access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
201 resource
->access
= access
;
202 resource
->width
= width
;
203 resource
->height
= height
;
204 resource
->depth
= depth
;
205 resource
->size
= size
;
206 resource
->priority
= 0;
207 resource
->parent
= parent
;
208 resource
->parent_ops
= parent_ops
;
209 resource
->resource_ops
= resource_ops
;
210 resource
->map_binding
= WINED3D_LOCATION_SYSMEM
;
211 resource
->heap_memory
= NULL
;
213 if (!(usage
& WINED3DUSAGE_PRIVATE
))
215 /* Check that we have enough video ram left */
216 if (!(access
& WINED3D_RESOURCE_ACCESS_CPU
) && device
->wined3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
218 if (size
> wined3d_device_get_available_texture_mem(device
))
220 ERR("Out of adapter memory.\n");
221 return WINED3DERR_OUTOFVIDEOMEMORY
;
223 adapter_adjust_memory(device
->adapter
, size
);
226 device_resource_add(device
, resource
);
232 static void wined3d_resource_destroy_object(void *object
)
234 struct wined3d_resource
*resource
= object
;
236 wined3d_resource_free_sysmem(resource
);
237 context_resource_released(resource
->device
, resource
);
238 wined3d_resource_release(resource
);
241 void resource_cleanup(struct wined3d_resource
*resource
)
243 const struct wined3d
*d3d
= resource
->device
->wined3d
;
245 TRACE("Cleaning up resource %p.\n", resource
);
247 if (!(resource
->usage
& WINED3DUSAGE_PRIVATE
))
249 if (!(resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
) && d3d
->flags
& WINED3D_VIDMEM_ACCOUNTING
)
251 TRACE("Decrementing device memory pool by %u.\n", resource
->size
);
252 adapter_adjust_memory(resource
->device
->adapter
, (INT64
)0 - resource
->size
);
255 device_resource_released(resource
->device
, resource
);
257 wined3d_resource_acquire(resource
);
258 wined3d_cs_destroy_object(resource
->device
->cs
, wined3d_resource_destroy_object
, resource
);
261 void resource_unload(struct wined3d_resource
*resource
)
263 if (resource
->map_count
)
264 ERR("Resource %p is being unloaded while mapped.\n", resource
);
267 DWORD CDECL
wined3d_resource_set_priority(struct wined3d_resource
*resource
, DWORD priority
)
271 if (!wined3d_resource_access_is_managed(resource
->access
))
273 WARN("Called on non-managed resource %p, ignoring.\n", resource
);
277 prev
= resource
->priority
;
278 resource
->priority
= priority
;
279 TRACE("resource %p, new priority %u, returning old priority %u.\n", resource
, priority
, prev
);
283 DWORD CDECL
wined3d_resource_get_priority(const struct wined3d_resource
*resource
)
285 TRACE("resource %p, returning %u.\n", resource
, resource
->priority
);
286 return resource
->priority
;
289 void * CDECL
wined3d_resource_get_parent(const struct wined3d_resource
*resource
)
291 return resource
->parent
;
294 void CDECL
wined3d_resource_set_parent(struct wined3d_resource
*resource
, void *parent
)
296 resource
->parent
= parent
;
299 void CDECL
wined3d_resource_get_desc(const struct wined3d_resource
*resource
, struct wined3d_resource_desc
*desc
)
301 desc
->resource_type
= resource
->type
;
302 desc
->format
= resource
->format
->id
;
303 desc
->multisample_type
= resource
->multisample_type
;
304 desc
->multisample_quality
= resource
->multisample_quality
;
305 desc
->usage
= resource
->usage
;
306 desc
->bind_flags
= resource
->bind_flags
;
307 desc
->access
= resource
->access
;
308 desc
->width
= resource
->width
;
309 desc
->height
= resource
->height
;
310 desc
->depth
= resource
->depth
;
311 desc
->size
= resource
->size
;
314 static DWORD
wined3d_resource_sanitise_map_flags(const struct wined3d_resource
*resource
, DWORD flags
)
316 /* Not all flags make sense together, but Windows never returns an error.
317 * Catch the cases that could cause issues. */
318 if (flags
& WINED3D_MAP_READ
)
320 if (flags
& WINED3D_MAP_DISCARD
)
322 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_DISCARD, ignoring flags.\n");
323 return flags
& (WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
325 if (flags
& WINED3D_MAP_NOOVERWRITE
)
327 WARN("WINED3D_MAP_READ combined with WINED3D_MAP_NOOVERWRITE, ignoring flags.\n");
328 return flags
& (WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
331 else if (flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
333 if (!(resource
->usage
& WINED3DUSAGE_DYNAMIC
))
335 WARN("DISCARD or NOOVERWRITE map on non-dynamic buffer, ignoring.\n");
336 return flags
& (WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
338 if ((flags
& (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
339 == (WINED3D_MAP_DISCARD
| WINED3D_MAP_NOOVERWRITE
))
341 WARN("WINED3D_MAP_NOOVERWRITE used with WINED3D_MAP_DISCARD, ignoring WINED3D_MAP_DISCARD.\n");
342 flags
&= ~WINED3D_MAP_DISCARD
;
349 HRESULT CDECL
wined3d_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
350 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
352 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
353 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
355 if (!(flags
& (WINED3D_MAP_READ
| WINED3D_MAP_WRITE
)))
357 WARN("No read/write flags specified.\n");
361 if ((flags
& WINED3D_MAP_READ
) && !(resource
->access
& WINED3D_RESOURCE_ACCESS_MAP_R
))
363 WARN("Resource does not have MAP_R access.\n");
367 if ((flags
& WINED3D_MAP_WRITE
) && !(resource
->access
& WINED3D_RESOURCE_ACCESS_MAP_W
))
369 WARN("Resource does not have MAP_W access.\n");
373 flags
= wined3d_resource_sanitise_map_flags(resource
, flags
);
374 wined3d_resource_wait_idle(resource
);
376 return wined3d_cs_map(resource
->device
->cs
, resource
, sub_resource_idx
, map_desc
, box
, flags
);
379 HRESULT CDECL
wined3d_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
381 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
383 return wined3d_cs_unmap(resource
->device
->cs
, resource
, sub_resource_idx
);
386 void CDECL
wined3d_resource_preload(struct wined3d_resource
*resource
)
388 wined3d_cs_emit_preload_resource(resource
->device
->cs
, resource
);
391 static BOOL
wined3d_resource_allocate_sysmem(struct wined3d_resource
*resource
)
394 SIZE_T align
= RESOURCE_ALIGNMENT
- 1 + sizeof(*p
);
397 if (!(mem
= heap_alloc_zero(resource
->size
+ align
)))
399 ERR("Failed to allocate system memory.\n");
403 p
= (void **)(((ULONG_PTR
)mem
+ align
) & ~(RESOURCE_ALIGNMENT
- 1)) - 1;
406 resource
->heap_memory
= ++p
;
411 BOOL
wined3d_resource_prepare_sysmem(struct wined3d_resource
*resource
)
413 if (resource
->heap_memory
)
416 return wined3d_resource_allocate_sysmem(resource
);
419 void wined3d_resource_free_sysmem(struct wined3d_resource
*resource
)
421 void **p
= resource
->heap_memory
;
427 resource
->heap_memory
= NULL
;
430 GLbitfield
wined3d_resource_gl_map_flags(DWORD d3d_flags
)
434 if (d3d_flags
& WINED3D_MAP_WRITE
)
435 ret
|= GL_MAP_WRITE_BIT
| GL_MAP_FLUSH_EXPLICIT_BIT
;
436 if (d3d_flags
& WINED3D_MAP_READ
)
437 ret
|= GL_MAP_READ_BIT
;
439 if (d3d_flags
& WINED3D_MAP_DISCARD
)
440 ret
|= GL_MAP_INVALIDATE_BUFFER_BIT
;
441 if (d3d_flags
& WINED3D_MAP_NOOVERWRITE
)
442 ret
|= GL_MAP_UNSYNCHRONIZED_BIT
;
447 GLenum
wined3d_resource_gl_legacy_map_flags(DWORD d3d_flags
)
449 switch (d3d_flags
& (WINED3D_MAP_READ
| WINED3D_MAP_WRITE
))
451 case WINED3D_MAP_READ
:
452 return GL_READ_ONLY_ARB
;
454 case WINED3D_MAP_WRITE
:
455 return GL_WRITE_ONLY_ARB
;
458 return GL_READ_WRITE_ARB
;
462 BOOL
wined3d_resource_is_offscreen(struct wined3d_resource
*resource
)
464 struct wined3d_swapchain
*swapchain
;
466 /* Only 2D texture resources can be onscreen. */
467 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
470 /* Not on a swapchain - must be offscreen */
471 if (!(swapchain
= texture_from_resource(resource
)->swapchain
))
474 /* The front buffer is always onscreen */
475 if (resource
== &swapchain
->front_buffer
->resource
)
478 /* If the swapchain is rendered to an FBO, the backbuffer is
479 * offscreen, otherwise onscreen */
480 return swapchain
->render_to_fbo
;
483 void wined3d_resource_update_draw_binding(struct wined3d_resource
*resource
)
485 const struct wined3d_d3d_info
*d3d_info
= &resource
->device
->adapter
->d3d_info
;
487 if (!wined3d_resource_is_offscreen(resource
) || wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
)
489 resource
->draw_binding
= WINED3D_LOCATION_DRAWABLE
;
491 else if (resource
->multisample_type
)
493 resource
->draw_binding
= d3d_info
->multisample_draw_location
;
495 else if (resource
->gl_type
== WINED3D_GL_RES_TYPE_RB
)
497 resource
->draw_binding
= WINED3D_LOCATION_RB_RESOLVED
;
501 resource
->draw_binding
= WINED3D_LOCATION_TEXTURE_RGB
;
505 const struct wined3d_format
*wined3d_resource_get_decompress_format(const struct wined3d_resource
*resource
)
507 const struct wined3d_adapter
*adapter
= resource
->device
->adapter
;
508 if (resource
->format_flags
& (WINED3DFMT_FLAG_SRGB_READ
| WINED3DFMT_FLAG_SRGB_WRITE
)
509 && !(adapter
->d3d_info
.wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
))
510 return wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM_SRGB
, resource
->bind_flags
);
511 return wined3d_get_format(adapter
, WINED3DFMT_B8G8R8A8_UNORM
, resource
->bind_flags
);
514 unsigned int wined3d_resource_get_sample_count(const struct wined3d_resource
*resource
)
516 const struct wined3d_format
*format
= resource
->format
;
518 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
519 * feature through type == MULTISAMPLE_XX and quality != 0. This could
520 * be mapped to GL_NV_framebuffer_multisample_coverage.
522 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
523 * (EQAA), but it does not have an equivalent OpenGL extension. */
525 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
526 * levels as the count of advertised multisample types for the texture
528 if (resource
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
530 unsigned int i
, count
= 0;
532 for (i
= 0; i
< sizeof(format
->multisample_types
) * CHAR_BIT
; ++i
)
534 if (format
->multisample_types
& 1u << i
)
536 if (resource
->multisample_quality
== count
++)
543 return resource
->multisample_type
;
546 VkAccessFlags
vk_access_mask_from_bind_flags(uint32_t bind_flags
)
548 VkAccessFlags flags
= 0;
550 if (bind_flags
& WINED3D_BIND_VERTEX_BUFFER
)
551 flags
|= VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT
;
552 if (bind_flags
& WINED3D_BIND_INDEX_BUFFER
)
553 flags
|= VK_ACCESS_INDEX_READ_BIT
;
554 if (bind_flags
& WINED3D_BIND_CONSTANT_BUFFER
)
555 flags
|= VK_ACCESS_UNIFORM_READ_BIT
;
556 if (bind_flags
& WINED3D_BIND_SHADER_RESOURCE
)
557 flags
|= VK_ACCESS_SHADER_READ_BIT
;
558 if (bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
)
559 flags
|= VK_ACCESS_SHADER_READ_BIT
| VK_ACCESS_SHADER_WRITE_BIT
;
560 if (bind_flags
& WINED3D_BIND_INDIRECT_BUFFER
)
561 flags
|= VK_ACCESS_INDIRECT_COMMAND_READ_BIT
;
562 if (bind_flags
& WINED3D_BIND_RENDER_TARGET
)
563 flags
|= VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
| VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT
;
564 if (bind_flags
& WINED3D_BIND_DEPTH_STENCIL
)
565 flags
|= VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT
| VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
;
566 if (bind_flags
& WINED3D_BIND_STREAM_OUTPUT
)
567 FIXME("Ignoring some bind flags %#x.\n", bind_flags
);