wined3d: Introduce a wined3d_state_get_ffp_texture() helper.
[wine.git] / dlls / wined3d / texture.c
blob3dfecc949d19f5b4a98a161ff1f5cc9af2fb5bf8
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wined3d_private.h"
24 #include "wined3d_gl.h"
25 #include "wined3d_vk.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static const uint32_t wined3d_texture_sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_BUFFER;
34 static const struct wined3d_texture_ops texture_gl_ops;
36 struct wined3d_texture_idx
38 struct wined3d_texture *texture;
39 unsigned int sub_resource_idx;
42 struct wined3d_rect_f
44 float l;
45 float t;
46 float r;
47 float b;
50 bool wined3d_texture_validate_sub_resource_idx(const struct wined3d_texture *texture, unsigned int sub_resource_idx)
52 if (sub_resource_idx < texture->level_count * texture->layer_count)
53 return true;
55 WARN("Invalid sub-resource index %u.\n", sub_resource_idx);
56 return false;
59 BOOL wined3d_texture_can_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
61 if (!d3d_info->pbo || texture->resource.format->conv_byte_count || texture->resource.pin_sysmem
62 || (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED))
63 return FALSE;
65 return TRUE;
68 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
70 if (!wined3d_texture_can_use_pbo(texture, d3d_info))
71 return FALSE;
73 /* Use a PBO for dynamic textures and read-only staging textures. */
74 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
75 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
76 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
79 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
80 const struct wined3d_gl_info *gl_info)
82 /* We don't expect to create texture views for textures with height-scaled formats.
83 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
84 return gl_info->supported[ARB_TEXTURE_STORAGE]
85 && !(texture->resource.format_attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE);
88 /* Front buffer coordinates are always full screen coordinates, but our GL
89 * drawable is limited to the window's client area. The sysmem and texture
90 * copies do have the full screen size. Note that GL has a bottom-left
91 * origin, while D3D has a top-left origin. */
92 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
94 unsigned int drawable_height;
95 POINT offset = {0, 0};
96 RECT windowsize;
98 if (!texture->swapchain)
99 return;
101 if (texture == texture->swapchain->front_buffer)
103 ScreenToClient(window, &offset);
104 OffsetRect(rect, offset.x, offset.y);
107 GetClientRect(window, &windowsize);
108 drawable_height = windowsize.bottom - windowsize.top;
110 rect->top = drawable_height - rect->top;
111 rect->bottom = drawable_height - rect->bottom;
114 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
116 const struct wined3d_swapchain *swapchain = texture->swapchain;
118 TRACE("texture %p.\n", texture);
120 if (!swapchain)
122 ERR("Texture %p is not part of a swapchain.\n", texture);
123 return GL_NONE;
126 if (texture == swapchain->front_buffer)
128 TRACE("Returning GL_FRONT.\n");
129 return GL_FRONT;
132 if (texture == swapchain->back_buffers[0])
134 TRACE("Returning GL_BACK.\n");
135 return GL_BACK;
138 FIXME("Higher back buffer, returning GL_BACK.\n");
139 return GL_BACK;
142 static uint32_t wined3d_resource_access_from_location(uint32_t location)
144 switch (location)
146 case WINED3D_LOCATION_DISCARDED:
147 return 0;
149 case WINED3D_LOCATION_SYSMEM:
150 return WINED3D_RESOURCE_ACCESS_CPU;
152 case WINED3D_LOCATION_BUFFER:
153 case WINED3D_LOCATION_DRAWABLE:
154 case WINED3D_LOCATION_TEXTURE_RGB:
155 case WINED3D_LOCATION_TEXTURE_SRGB:
156 case WINED3D_LOCATION_RB_MULTISAMPLE:
157 case WINED3D_LOCATION_RB_RESOLVED:
158 return WINED3D_RESOURCE_ACCESS_GPU;
160 default:
161 FIXME("Unhandled location %#x.\n", location);
162 return 0;
166 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
168 f->l = ((r->left * 2.0f) / w) - 1.0f;
169 f->t = ((r->top * 2.0f) / h) - 1.0f;
170 f->r = ((r->right * 2.0f) / w) - 1.0f;
171 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
174 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
175 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
177 struct wined3d_vec3 *coords = info->texcoords;
178 struct wined3d_rect_f f;
179 unsigned int level;
180 GLenum target;
181 GLsizei w, h;
183 level = sub_resource_idx % texture_gl->t.level_count;
184 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
185 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
186 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
188 switch (target)
190 default:
191 FIXME("Unsupported texture target %#x.\n", target);
192 /* Fall back to GL_TEXTURE_2D */
193 case GL_TEXTURE_2D:
194 info->bind_target = GL_TEXTURE_2D;
195 coords[0].x = (float)rect->left / w;
196 coords[0].y = (float)rect->top / h;
197 coords[0].z = 0.0f;
199 coords[1].x = (float)rect->right / w;
200 coords[1].y = (float)rect->top / h;
201 coords[1].z = 0.0f;
203 coords[2].x = (float)rect->left / w;
204 coords[2].y = (float)rect->bottom / h;
205 coords[2].z = 0.0f;
207 coords[3].x = (float)rect->right / w;
208 coords[3].y = (float)rect->bottom / h;
209 coords[3].z = 0.0f;
210 break;
212 case GL_TEXTURE_RECTANGLE_ARB:
213 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
214 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
215 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
216 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
217 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
218 break;
220 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
221 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
222 cube_coords_float(rect, w, h, &f);
224 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
225 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
226 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
227 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
228 break;
230 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
231 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
232 cube_coords_float(rect, w, h, &f);
234 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
235 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
236 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
237 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
238 break;
240 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
241 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
242 cube_coords_float(rect, w, h, &f);
244 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
245 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
246 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
247 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
248 break;
250 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
251 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
252 cube_coords_float(rect, w, h, &f);
254 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
255 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
256 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
257 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
258 break;
260 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
261 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
262 cube_coords_float(rect, w, h, &f);
264 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
265 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
266 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
267 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
268 break;
270 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
271 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
272 cube_coords_float(rect, w, h, &f);
274 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
275 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
276 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
277 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
278 break;
282 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
283 const struct wined3d_resource *src_resource, DWORD src_location,
284 const struct wined3d_resource *dst_resource, DWORD dst_location)
286 const struct wined3d_format *src_format = src_resource->format;
287 const struct wined3d_format *dst_format = dst_resource->format;
288 bool src_ds, dst_ds;
290 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
291 return false;
293 if ((src_resource->format_attrs | dst_resource->format_attrs) & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
294 return false;
296 /* Source and/or destination need to be on the GL side. */
297 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
298 return false;
300 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
301 return false;
303 /* We can't copy between depth/stencil and colour attachments. One notable
304 * way we can end up here is when copying between typeless resources with
305 * formats like R16_TYPELESS, which can end up using either a
306 * depth/stencil or a colour format on the OpenGL side, depending on the
307 * resource's bind flags. */
308 src_ds = src_format->depth_size || src_format->stencil_size;
309 dst_ds = dst_format->depth_size || dst_format->stencil_size;
310 if (src_ds != dst_ds)
311 return false;
313 switch (blit_op)
315 case WINED3D_BLIT_OP_COLOR_BLIT:
316 if (!((src_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
317 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
318 return false;
319 if (!((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
320 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
321 return false;
322 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
323 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
324 return false;
325 break;
327 case WINED3D_BLIT_OP_DEPTH_BLIT:
328 if (!(src_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
329 return false;
330 if (!(dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_DEPTH_STENCIL))
331 return false;
332 /* Accept pure swizzle fixups for depth formats. In general we
333 * ignore the stencil component (if present) at the moment and the
334 * swizzle is not relevant with just the depth component. */
335 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
336 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
337 return false;
338 break;
340 default:
341 return false;
344 return true;
347 /* Blit between surface locations. Onscreen on different swapchains is not supported.
348 * Depth / stencil is not supported. Context activation is done by the caller. */
349 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
350 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
351 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
352 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
353 const RECT *dst_rect, const struct wined3d_format *resolve_format)
355 struct wined3d_texture *required_texture, *restore_texture = NULL, *dst_save_texture = dst_texture;
356 unsigned int restore_idx, dst_save_sub_resource_idx = dst_sub_resource_idx;
357 struct wined3d_texture *src_staging_texture = NULL;
358 const struct wined3d_gl_info *gl_info;
359 struct wined3d_context_gl *context_gl;
360 bool resolve, scaled_resolve;
361 GLenum gl_filter;
362 GLenum buffer;
363 RECT s, d;
365 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
366 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, resolve format %p.\n",
367 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
368 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
369 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect), resolve_format);
371 resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location);
372 scaled_resolve = resolve
373 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
374 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
376 if (filter == WINED3D_TEXF_LINEAR)
377 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
378 else
379 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
381 if (resolve)
383 GLint resolve_internal, src_internal, dst_internal;
384 enum wined3d_format_id resolve_format_id;
386 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
387 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
388 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
389 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
391 if (resolve_format)
393 resolve_internal = wined3d_format_gl(resolve_format)->internal;
394 resolve_format_id = resolve_format->id;
396 else if (!wined3d_format_is_typeless(src_texture->resource.format))
398 resolve_internal = src_internal;
399 resolve_format_id = src_texture->resource.format->id;
401 else
403 resolve_internal = dst_internal;
404 resolve_format_id = dst_texture->resource.format->id;
407 /* In case of typeless resolve the texture type may not match the resolve type.
408 * To handle that, allocate intermediate texture(s) to resolve from/to.
409 * A possible performance improvement would be to resolve using a shader instead. */
410 if (src_internal != resolve_internal)
412 struct wined3d_resource_desc desc;
413 unsigned src_level;
414 HRESULT hr;
416 src_level = src_sub_resource_idx % src_texture->level_count;
417 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
418 desc.format = resolve_format_id;
419 desc.multisample_type = src_texture->resource.multisample_type;
420 desc.multisample_quality = src_texture->resource.multisample_quality;
421 desc.usage = WINED3DUSAGE_PRIVATE;
422 desc.bind_flags = 0;
423 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
424 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
425 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
426 desc.depth = 1;
427 desc.size = 0;
429 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
430 &src_staging_texture);
431 if (FAILED(hr))
433 ERR("Failed to create staging texture, hr %#lx.\n", hr);
434 goto done;
437 if (src_location == WINED3D_LOCATION_DRAWABLE)
438 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the source of a typeless resolve.\n");
440 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
441 src_texture, src_sub_resource_idx, src_location, src_rect,
442 src_staging_texture, 0, src_location, src_rect,
443 NULL, WINED3D_TEXF_NONE, NULL);
445 src_texture = src_staging_texture;
446 src_sub_resource_idx = 0;
449 if (dst_internal != resolve_internal)
451 struct wined3d_resource_desc desc;
452 unsigned dst_level;
453 HRESULT hr;
455 dst_level = dst_sub_resource_idx % dst_texture->level_count;
456 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
457 desc.format = resolve_format_id;
458 desc.multisample_type = dst_texture->resource.multisample_type;
459 desc.multisample_quality = dst_texture->resource.multisample_quality;
460 desc.usage = WINED3DUSAGE_PRIVATE;
461 desc.bind_flags = 0;
462 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
463 desc.width = wined3d_texture_get_level_width(dst_texture, dst_level);
464 desc.height = wined3d_texture_get_level_height(dst_texture, dst_level);
465 desc.depth = 1;
466 desc.size = 0;
468 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
469 &dst_texture);
470 if (FAILED(hr))
472 ERR("Failed to create staging texture, hr %#lx.\n", hr);
473 goto done;
476 wined3d_texture_load_location(dst_texture, 0, context, dst_location);
477 dst_sub_resource_idx = 0;
481 /* Make sure the locations are up-to-date. Loading the destination
482 * surface isn't required if the entire surface is overwritten. (And is
483 * in fact harmful if we're being called by surface_load_location() with
484 * the purpose of loading the destination surface.) */
485 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
486 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
487 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
488 else
489 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
491 /* Acquire a context for the front-buffer, even though we may be blitting
492 * to/from a back-buffer. Since context_acquire() doesn't take the
493 * resource location into account, it may consider the back-buffer to be
494 * offscreen. */
495 if (src_location == WINED3D_LOCATION_DRAWABLE)
496 required_texture = src_texture->swapchain->front_buffer;
497 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
498 required_texture = dst_texture->swapchain->front_buffer;
499 else
500 required_texture = NULL;
502 restore_texture = context->current_rt.texture;
503 restore_idx = context->current_rt.sub_resource_idx;
504 if (restore_texture != required_texture)
505 context = context_acquire(device, required_texture, 0);
506 else
507 restore_texture = NULL;
509 context_gl = wined3d_context_gl(context);
510 if (!context_gl->valid)
512 context_release(context);
513 WARN("Invalid context, skipping blit.\n");
514 restore_texture = NULL;
515 goto done;
518 gl_info = context_gl->gl_info;
520 if (src_location == WINED3D_LOCATION_DRAWABLE)
522 TRACE("Source texture %p is onscreen.\n", src_texture);
523 buffer = wined3d_texture_get_gl_buffer(src_texture);
524 s = *src_rect;
525 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
526 src_rect = &s;
528 else
530 TRACE("Source texture %p is offscreen.\n", src_texture);
531 buffer = GL_COLOR_ATTACHMENT0;
534 wined3d_context_gl_apply_fbo_state_explicit(context_gl, GL_READ_FRAMEBUFFER,
535 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
536 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
537 checkGLcall("glReadBuffer()");
538 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
540 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
542 if (dst_location == WINED3D_LOCATION_DRAWABLE)
544 d = *dst_rect;
545 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
546 dst_rect = &d;
549 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
550 context_invalidate_state(context, STATE_BLEND);
552 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
553 context_invalidate_state(context, STATE_RASTERIZER);
555 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
556 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
557 checkGLcall("glBlitFramebuffer()");
559 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
560 gl_info->gl_ops.gl.p_glFlush();
562 if (dst_texture != dst_save_texture)
564 if (dst_location == WINED3D_LOCATION_DRAWABLE)
565 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the destination of a typeless resolve.\n");
567 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
568 dst_texture, 0, dst_location, dst_rect,
569 dst_save_texture, dst_save_sub_resource_idx, dst_location, dst_rect,
570 NULL, WINED3D_TEXF_NONE, NULL);
573 done:
574 if (dst_texture != dst_save_texture)
575 wined3d_texture_decref(dst_texture);
577 if (src_staging_texture)
578 wined3d_texture_decref(src_staging_texture);
580 if (restore_texture)
581 context_restore(context, restore_texture, restore_idx);
584 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
585 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
586 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
587 DWORD dst_location, const RECT *dst_rect)
589 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
590 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
591 GLbitfield src_mask, dst_mask;
592 GLbitfield gl_mask;
594 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
595 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
596 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
597 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
599 src_mask = 0;
600 if (src_texture->resource.format->depth_size)
601 src_mask |= GL_DEPTH_BUFFER_BIT;
602 if (src_texture->resource.format->stencil_size)
603 src_mask |= GL_STENCIL_BUFFER_BIT;
605 dst_mask = 0;
606 if (dst_texture->resource.format->depth_size)
607 dst_mask |= GL_DEPTH_BUFFER_BIT;
608 if (dst_texture->resource.format->stencil_size)
609 dst_mask |= GL_STENCIL_BUFFER_BIT;
611 if (src_mask != dst_mask)
613 ERR("Incompatible formats %s and %s.\n",
614 debug_d3dformat(src_texture->resource.format->id),
615 debug_d3dformat(dst_texture->resource.format->id));
616 return;
619 if (!src_mask)
621 ERR("Not a depth / stencil format: %s.\n",
622 debug_d3dformat(src_texture->resource.format->id));
623 return;
625 gl_mask = src_mask;
627 /* Make sure the locations are up-to-date. Loading the destination
628 * surface isn't required if the entire surface is overwritten. */
629 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
630 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
631 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
632 else
633 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
635 wined3d_context_gl_apply_fbo_state_explicit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
636 &src_texture->resource, src_sub_resource_idx, src_location);
637 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
639 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
641 if (gl_mask & GL_DEPTH_BUFFER_BIT)
643 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
644 context_invalidate_state(context, STATE_DEPTH_STENCIL);
646 if (gl_mask & GL_STENCIL_BUFFER_BIT)
648 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
649 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
650 gl_info->gl_ops.gl.p_glStencilMask(~0U);
651 context_invalidate_state(context, STATE_DEPTH_STENCIL);
654 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
655 context_invalidate_state(context, STATE_RASTERIZER);
657 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
658 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
659 checkGLcall("glBlitFramebuffer()");
662 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
664 struct wined3d_texture_sub_resource *sub_resource;
665 unsigned int i, sub_count;
667 if ((texture->flags & WINED3D_TEXTURE_CONVERTED)
668 || texture->resource.pin_sysmem
669 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
671 TRACE("Not evicting system memory for texture %p.\n", texture);
672 return;
675 TRACE("Evicting system memory for texture %p.\n", texture);
677 sub_count = texture->level_count * texture->layer_count;
678 for (i = 0; i < sub_count; ++i)
680 sub_resource = &texture->sub_resources[i];
681 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
682 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
683 i, texture);
684 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
686 wined3d_resource_free_sysmem(&texture->resource);
689 void wined3d_texture_validate_location(struct wined3d_texture *texture,
690 unsigned int sub_resource_idx, uint32_t location)
692 struct wined3d_texture_sub_resource *sub_resource;
693 DWORD previous_locations;
695 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
696 texture, sub_resource_idx, wined3d_debug_location(location));
698 sub_resource = &texture->sub_resources[sub_resource_idx];
699 previous_locations = sub_resource->locations;
700 sub_resource->locations |= location;
701 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
702 && !--texture->sysmem_count)
703 wined3d_texture_evict_sysmem(texture);
705 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
708 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
710 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
713 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
714 unsigned int sub_resource_idx, uint32_t location)
716 struct wined3d_texture_sub_resource *sub_resource;
717 DWORD previous_locations;
719 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
720 texture, sub_resource_idx, wined3d_debug_location(location));
722 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
723 wined3d_texture_set_dirty(texture);
725 sub_resource = &texture->sub_resources[sub_resource_idx];
726 previous_locations = sub_resource->locations;
727 sub_resource->locations &= ~location;
728 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
729 ++texture->sysmem_count;
731 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
733 if (!sub_resource->locations)
734 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
735 sub_resource_idx, texture);
738 void wined3d_texture_get_bo_address(const struct wined3d_texture *texture,
739 unsigned int sub_resource_idx, struct wined3d_bo_address *data, uint32_t location)
741 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
743 if (location == WINED3D_LOCATION_BUFFER)
745 data->addr = NULL;
746 data->buffer_object = sub_resource->bo;
748 else
750 assert(location == WINED3D_LOCATION_SYSMEM);
751 if (sub_resource->user_memory)
753 data->addr = sub_resource->user_memory;
755 else
757 data->addr = texture->resource.heap_memory;
758 data->addr += sub_resource->offset;
760 data->buffer_object = 0;
764 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
766 unsigned int i;
768 TRACE("texture %p\n", texture);
770 if (!texture->dirty_regions)
771 return;
773 for (i = 0; i < texture->layer_count; ++i)
775 texture->dirty_regions[i].box_count = 0;
779 /* Context activation is done by the caller. Context may be NULL in
780 * WINED3D_NO3D mode. */
781 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
782 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
784 DWORD current = texture->sub_resources[sub_resource_idx].locations;
785 BOOL ret;
787 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
788 texture, sub_resource_idx, context, wined3d_debug_location(location));
790 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
792 if (current & location)
794 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
795 return TRUE;
798 if (WARN_ON(d3d))
800 uint32_t required_access = wined3d_resource_access_from_location(location);
801 if ((texture->resource.access & required_access) != required_access)
802 WARN("Operation requires %#x access, but texture only has %#x.\n",
803 required_access, texture->resource.access);
806 if (current & WINED3D_LOCATION_DISCARDED)
808 TRACE("Sub-resource previously discarded, nothing to do.\n");
809 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
810 return FALSE;
811 wined3d_texture_validate_location(texture, sub_resource_idx, location);
812 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
813 return TRUE;
816 if (!current)
818 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
819 sub_resource_idx, texture);
820 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
821 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
824 if ((location & wined3d_texture_sysmem_locations)
825 && (current & (wined3d_texture_sysmem_locations | WINED3D_LOCATION_CLEARED)))
827 struct wined3d_bo_address source, destination;
828 struct wined3d_range range;
830 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
831 return FALSE;
832 wined3d_texture_get_bo_address(texture, sub_resource_idx, &destination, location);
833 range.offset = 0;
834 range.size = texture->sub_resources[sub_resource_idx].size;
835 if (current & WINED3D_LOCATION_CLEARED)
837 unsigned int level_idx = sub_resource_idx % texture->level_count;
838 struct wined3d_map_desc map;
839 struct wined3d_box box;
840 struct wined3d_color c;
842 if (texture->resource.format->caps[WINED3D_GL_RES_TYPE_TEX_2D]
843 & WINED3D_FORMAT_CAP_DEPTH_STENCIL)
845 c.r = texture->sub_resources[sub_resource_idx].clear_value.depth;
846 c.g = texture->sub_resources[sub_resource_idx].clear_value.stencil;
847 c.b = c.a = 0.0f;
849 else
851 c = texture->sub_resources[sub_resource_idx].clear_value.colour;
854 wined3d_texture_get_pitch(texture, level_idx, &map.row_pitch, &map.slice_pitch);
855 if (destination.buffer_object)
856 map.data = wined3d_context_map_bo_address(context, &destination, range.size,
857 WINED3D_MAP_WRITE | WINED3D_MAP_DISCARD);
858 else
859 map.data = destination.addr;
861 wined3d_texture_get_level_box(texture, level_idx, &box);
862 wined3d_resource_memory_colour_fill(&texture->resource, &map, &c, &box, true);
864 if (destination.buffer_object)
865 wined3d_context_unmap_bo_address(context, &destination, 1, &range);
867 else
869 wined3d_texture_get_bo_address(texture, sub_resource_idx,
870 &source, (current & wined3d_texture_sysmem_locations));
871 wined3d_context_copy_bo_address(context, &destination, &source, 1, &range,
872 WINED3D_MAP_WRITE | WINED3D_MAP_DISCARD);
874 ret = TRUE;
876 else
878 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
881 if (ret)
882 wined3d_texture_validate_location(texture, sub_resource_idx, location);
884 return ret;
887 static void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
888 struct wined3d_context *context, struct wined3d_bo_address *data)
890 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
891 DWORD locations = sub_resource->locations;
893 TRACE("texture %p, context %p, sub_resource_idx %u, data %p, locations %s.\n",
894 texture, context, sub_resource_idx, data, wined3d_debug_location(locations));
896 if (locations & (WINED3D_LOCATION_DISCARDED | WINED3D_LOCATION_CLEARED))
898 locations = (wined3d_texture_use_pbo(texture, context->d3d_info) ? WINED3D_LOCATION_BUFFER : WINED3D_LOCATION_SYSMEM);
899 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, locations))
901 data->buffer_object = 0;
902 data->addr = NULL;
903 return;
906 if (locations & WINED3D_LOCATION_BUFFER)
908 wined3d_texture_get_bo_address(texture, sub_resource_idx, data, WINED3D_LOCATION_BUFFER);
909 return;
912 if (locations & WINED3D_LOCATION_SYSMEM)
914 wined3d_texture_get_bo_address(texture, sub_resource_idx, data, WINED3D_LOCATION_SYSMEM);
915 return;
918 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
919 data->addr = NULL;
920 data->buffer_object = 0;
923 /* Context activation is done by the caller. */
924 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
925 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
927 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
928 struct wined3d_bo_gl *bo_gl = wined3d_bo_gl(sub_resource->bo);
930 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
932 wined3d_context_gl_destroy_bo(context_gl, bo_gl);
933 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
934 sub_resource->bo = NULL;
935 heap_free(bo_gl);
938 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
939 struct wined3d_context *context, unsigned int location)
941 texture->texture_ops->texture_unload_location(texture, context, location);
944 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
946 unsigned int sub_count = texture->level_count * texture->layer_count;
947 struct wined3d_device *device = texture->resource.device;
948 DWORD map_binding = texture->update_map_binding;
949 struct wined3d_context *context;
950 unsigned int i;
952 context = context_acquire(device, NULL, 0);
954 for (i = 0; i < sub_count; ++i)
956 if (texture->sub_resources[i].locations == texture->resource.map_binding
957 && !wined3d_texture_load_location(texture, i, context, map_binding))
958 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
961 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
962 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
964 context_release(context);
966 texture->resource.map_binding = map_binding;
967 texture->update_map_binding = 0;
970 static void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
972 texture->update_map_binding = map_binding;
973 if (!texture->resource.map_count)
974 wined3d_texture_update_map_binding(texture);
977 /* A GL context is provided by the caller */
978 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
979 struct gl_texture *tex)
981 context_gl_resource_released(device, tex->name, FALSE);
982 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
983 tex->name = 0;
986 /* Context activation is done by the caller. */
987 /* The caller is responsible for binding the correct texture. */
988 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
989 GLenum gl_internal_format, const struct wined3d_format_gl *format,
990 const struct wined3d_gl_info *gl_info)
992 unsigned int level, level_count, layer, layer_count;
993 GLsizei width, height, depth;
994 GLenum target;
996 level_count = texture_gl->t.level_count;
997 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
998 layer_count = 1;
999 else
1000 layer_count = texture_gl->t.layer_count;
1002 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1003 checkGLcall("glBindBuffer");
1005 for (layer = 0; layer < layer_count; ++layer)
1007 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
1009 for (level = 0; level < level_count; ++level)
1011 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1012 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1013 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
1015 height *= format->f.height_scale.numerator;
1016 height /= format->f.height_scale.denominator;
1019 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
1020 texture_gl, layer, level, target, width, height);
1022 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
1024 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
1025 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
1026 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
1027 format->format, format->type, NULL));
1028 checkGLcall("glTexImage3D");
1030 else if (target == GL_TEXTURE_1D)
1032 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
1033 width, 0, format->format, format->type, NULL);
1035 else
1037 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
1038 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
1039 format->format, format->type, NULL);
1040 checkGLcall("glTexImage2D");
1046 /* Context activation is done by the caller. */
1047 /* The caller is responsible for binding the correct texture. */
1048 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
1049 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
1051 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
1052 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
1053 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
1054 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
1055 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
1057 switch (texture_gl->target)
1059 case GL_TEXTURE_3D:
1060 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
1061 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
1062 break;
1063 case GL_TEXTURE_2D_ARRAY:
1064 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
1065 gl_internal_format, width, height, texture_gl->t.layer_count));
1066 break;
1067 case GL_TEXTURE_2D_MULTISAMPLE:
1068 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
1069 gl_internal_format, width, height, standard_pattern));
1070 break;
1071 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1072 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
1073 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
1074 break;
1075 case GL_TEXTURE_1D_ARRAY:
1076 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1077 gl_internal_format, width, texture_gl->t.layer_count));
1078 break;
1079 case GL_TEXTURE_1D:
1080 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
1081 break;
1082 default:
1083 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1084 gl_internal_format, width, height));
1085 break;
1088 checkGLcall("allocate immutable storage");
1091 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
1092 unsigned int layer, const struct wined3d_box *box)
1094 struct wined3d_dirty_regions *regions;
1095 unsigned int count;
1097 if (!texture->dirty_regions)
1098 return;
1100 regions = &texture->dirty_regions[layer];
1101 count = regions->box_count + 1;
1102 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
1103 || (!box->left && !box->top && !box->front
1104 && box->right == texture->resource.width
1105 && box->bottom == texture->resource.height
1106 && box->back == texture->resource.depth))
1108 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
1109 return;
1112 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
1114 ERR("Failed to grow boxes array, marking entire texture dirty.\n");
1115 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
1116 return;
1119 regions->boxes[regions->box_count++] = *box;
1122 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
1124 unsigned int sub_count = texture->level_count * texture->layer_count;
1125 struct wined3d_texture_sub_resource *sub_resource;
1126 unsigned int i;
1128 for (i = 0; i < sub_count; ++i)
1130 sub_resource = &texture->sub_resources[i];
1131 if (sub_resource->parent)
1133 TRACE("sub-resource %u.\n", i);
1134 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
1135 sub_resource->parent = NULL;
1140 static void wined3d_texture_create_dc(void *object)
1142 const struct wined3d_texture_idx *idx = object;
1143 struct wined3d_context *context = NULL;
1144 unsigned int sub_resource_idx, level;
1145 const struct wined3d_format *format;
1146 unsigned int row_pitch, slice_pitch;
1147 struct wined3d_texture *texture;
1148 struct wined3d_dc_info *dc_info;
1149 struct wined3d_bo_address data;
1150 D3DKMT_CREATEDCFROMMEMORY desc;
1151 struct wined3d_device *device;
1152 NTSTATUS status;
1154 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1156 texture = idx->texture;
1157 sub_resource_idx = idx->sub_resource_idx;
1158 level = sub_resource_idx % texture->level_count;
1159 device = texture->resource.device;
1161 format = texture->resource.format;
1162 if (!format->ddi_format)
1164 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1165 return;
1168 if (!texture->dc_info)
1170 unsigned int sub_count = texture->level_count * texture->layer_count;
1172 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
1174 ERR("Failed to allocate DC info.\n");
1175 return;
1179 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
1181 context = context_acquire(device, NULL, 0);
1182 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1185 if (texture->dirty_regions)
1186 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, NULL);
1188 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1189 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
1190 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1191 if (data.buffer_object)
1193 if (!context)
1194 context = context_acquire(device, NULL, 0);
1195 desc.pMemory = wined3d_context_map_bo_address(context, &data,
1196 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
1198 else
1200 desc.pMemory = data.addr;
1203 if (context)
1204 context_release(context);
1206 desc.Format = format->ddi_format;
1207 desc.Width = wined3d_texture_get_level_width(texture, level);
1208 desc.Height = wined3d_texture_get_level_height(texture, level);
1209 desc.Pitch = row_pitch;
1210 desc.hDeviceDc = CreateCompatibleDC(NULL);
1211 desc.pColorTable = NULL;
1213 status = D3DKMTCreateDCFromMemory(&desc);
1214 DeleteDC(desc.hDeviceDc);
1215 if (status)
1217 WARN("Failed to create DC, status %#lx.\n", status);
1218 return;
1221 dc_info = &texture->dc_info[sub_resource_idx];
1222 dc_info->dc = desc.hDc;
1223 dc_info->bitmap = desc.hBitmap;
1225 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1228 static void wined3d_texture_destroy_dc(void *object)
1230 const struct wined3d_texture_idx *idx = object;
1231 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1232 struct wined3d_context *context;
1233 struct wined3d_texture *texture;
1234 struct wined3d_dc_info *dc_info;
1235 struct wined3d_bo_address data;
1236 unsigned int sub_resource_idx;
1237 struct wined3d_device *device;
1238 struct wined3d_range range;
1239 NTSTATUS status;
1241 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1243 texture = idx->texture;
1244 sub_resource_idx = idx->sub_resource_idx;
1245 device = texture->resource.device;
1246 dc_info = &texture->dc_info[sub_resource_idx];
1248 if (!dc_info->dc)
1250 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1251 return;
1254 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1256 destroy_desc.hDc = dc_info->dc;
1257 destroy_desc.hBitmap = dc_info->bitmap;
1258 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1259 ERR("Failed to destroy dc, status %#lx.\n", status);
1260 dc_info->dc = NULL;
1261 dc_info->bitmap = NULL;
1263 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1264 if (data.buffer_object)
1266 context = context_acquire(device, NULL, 0);
1267 range.offset = 0;
1268 range.size = texture->sub_resources[sub_resource_idx].size;
1269 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1270 context_release(context);
1274 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1276 texture->swapchain = swapchain;
1277 wined3d_resource_update_draw_binding(&texture->resource);
1280 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1282 static const GLenum swizzle_source[] =
1284 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1285 GL_ONE, /* CHANNEL_SOURCE_ONE */
1286 GL_RED, /* CHANNEL_SOURCE_X */
1287 GL_GREEN, /* CHANNEL_SOURCE_Y */
1288 GL_BLUE, /* CHANNEL_SOURCE_Z */
1289 GL_ALPHA, /* CHANNEL_SOURCE_W */
1292 swizzle[0] = swizzle_source[fixup.x_source];
1293 swizzle[1] = swizzle_source[fixup.y_source];
1294 swizzle[2] = swizzle_source[fixup.z_source];
1295 swizzle[3] = swizzle_source[fixup.w_source];
1298 /* Context activation is done by the caller. */
1299 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1300 struct wined3d_context_gl *context_gl, BOOL srgb)
1302 const struct wined3d_format *format = texture_gl->t.resource.format;
1303 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1304 const struct color_fixup_desc fixup = format->color_fixup;
1305 struct gl_texture *gl_tex;
1306 GLenum target;
1308 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1310 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1311 srgb = FALSE;
1313 /* sRGB mode cache for preload() calls outside drawprim. */
1314 if (srgb)
1315 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1316 else
1317 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1319 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1320 target = texture_gl->target;
1322 if (gl_tex->name)
1324 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1325 return;
1328 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1329 checkGLcall("glGenTextures");
1330 TRACE("Generated texture %d.\n", gl_tex->name);
1332 if (!gl_tex->name)
1334 ERR("Failed to generate a texture name.\n");
1335 return;
1338 /* Initialise the state of the texture object to the OpenGL defaults, not
1339 * the wined3d defaults. */
1340 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1341 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1342 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1343 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1344 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1345 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1346 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1347 gl_tex->sampler_desc.lod_bias = 0.0f;
1348 gl_tex->sampler_desc.min_lod = -1000.0f;
1349 gl_tex->sampler_desc.max_lod = 1000.0f;
1350 gl_tex->sampler_desc.max_anisotropy = 1;
1351 gl_tex->sampler_desc.compare = FALSE;
1352 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1353 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1354 gl_tex->sampler_desc.srgb_decode = TRUE;
1355 else
1356 gl_tex->sampler_desc.srgb_decode = srgb;
1357 gl_tex->sampler_desc.mip_base_level = 0;
1358 wined3d_texture_set_dirty(&texture_gl->t);
1360 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1362 /* For a new texture we have to set the texture levels after binding the
1363 * texture. Beware that texture rectangles do not support mipmapping, but
1364 * set the maxmiplevel if we're relying on the partial
1365 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1366 * (I.e., do not care about cond_np2 here, just look for
1367 * GL_TEXTURE_RECTANGLE_ARB.) */
1368 if (target != GL_TEXTURE_RECTANGLE_ARB)
1370 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1371 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1372 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1375 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1377 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1378 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1379 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1380 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1383 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1385 /* Conditional non power of two textures use a different clamping
1386 * default. If we're using the GL_WINE_normalized_texrect partial
1387 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1388 * has the address mode set to repeat - something that prevents us
1389 * from hitting the accelerated codepath. Thus manually set the GL
1390 * state. The same applies to filtering. Even if the texture has only
1391 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1392 * fallback on macos. */
1393 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1394 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1395 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1396 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1397 checkGLcall("glTexParameteri");
1398 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1399 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1400 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1401 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1402 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1405 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1407 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1408 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1411 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1413 GLint swizzle[4];
1415 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1416 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1417 checkGLcall("set format swizzle");
1421 /* Context activation is done by the caller. */
1422 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1423 struct wined3d_context_gl *context_gl, BOOL srgb)
1425 /* We don't need a specific texture unit, but after binding the texture
1426 * the current unit is dirty. Read the unit back instead of switching to
1427 * 0, this avoids messing around with the state manager's GL states. The
1428 * current texture unit should always be a valid one.
1430 * To be more specific, this is tricky because we can implicitly be
1431 * called from sampler() in state.c. This means we can't touch anything
1432 * other than whatever happens to be the currently active texture, or we
1433 * would risk marking already applied sampler states dirty again. */
1434 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1436 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1437 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1438 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1440 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1441 * a shader. */
1442 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1443 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1445 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1448 /* Context activation is done by the caller (state handler). */
1449 /* This function relies on the correct texture being bound and loaded. */
1450 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1451 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1453 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1454 GLenum target = texture_gl->target;
1455 struct gl_texture *gl_tex;
1456 DWORD state;
1458 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1460 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1462 state = sampler_desc->address_u;
1463 if (state != gl_tex->sampler_desc.address_u)
1465 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1466 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1467 gl_tex->sampler_desc.address_u = state;
1470 state = sampler_desc->address_v;
1471 if (state != gl_tex->sampler_desc.address_v)
1473 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1474 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1475 gl_tex->sampler_desc.address_v = state;
1478 state = sampler_desc->address_w;
1479 if (state != gl_tex->sampler_desc.address_w)
1481 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1482 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1483 gl_tex->sampler_desc.address_w = state;
1486 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1487 sizeof(gl_tex->sampler_desc.border_color)))
1489 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1490 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1491 sizeof(gl_tex->sampler_desc.border_color));
1494 state = sampler_desc->mag_filter;
1495 if (state != gl_tex->sampler_desc.mag_filter)
1497 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1498 gl_tex->sampler_desc.mag_filter = state;
1501 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1502 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1504 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1505 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1506 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1507 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1510 state = sampler_desc->max_anisotropy;
1511 if (state != gl_tex->sampler_desc.max_anisotropy)
1513 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1514 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1515 else
1516 WARN("Anisotropic filtering not supported.\n");
1517 gl_tex->sampler_desc.max_anisotropy = state;
1520 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1521 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1522 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1524 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1525 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1526 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1529 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1531 if (sampler_desc->compare)
1532 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1533 else
1534 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1535 gl_tex->sampler_desc.compare = sampler_desc->compare;
1538 checkGLcall("Texture parameter application");
1540 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1542 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1543 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1544 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1548 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1550 unsigned int refcount;
1552 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1554 refcount = InterlockedIncrement(&texture->resource.ref);
1555 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1557 return refcount;
1560 static void wined3d_texture_destroy_object(void *object)
1562 struct wined3d_texture *texture = object;
1563 struct wined3d_resource *resource;
1564 struct wined3d_dc_info *dc_info;
1565 unsigned int sub_count;
1566 unsigned int i;
1568 TRACE("texture %p.\n", texture);
1570 resource = &texture->resource;
1571 sub_count = texture->level_count * texture->layer_count;
1573 if ((dc_info = texture->dc_info))
1575 for (i = 0; i < sub_count; ++i)
1577 if (dc_info[i].dc)
1579 struct wined3d_texture_idx texture_idx = {texture, i};
1581 wined3d_texture_destroy_dc(&texture_idx);
1584 heap_free(dc_info);
1587 if (texture->overlay_info)
1589 for (i = 0; i < sub_count; ++i)
1591 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1592 struct wined3d_overlay_info *overlay, *cur;
1594 list_remove(&info->entry);
1595 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1597 list_remove(&overlay->entry);
1600 heap_free(texture->overlay_info);
1603 if (texture->dirty_regions)
1605 for (i = 0; i < texture->layer_count; ++i)
1607 heap_free(texture->dirty_regions[i].boxes);
1609 heap_free(texture->dirty_regions);
1612 /* Discard the contents of resources with CPU access, to avoid downloading
1613 * them to SYSMEM on unload. */
1614 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
1616 for (i = 0; i < sub_count; ++i)
1618 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1619 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1622 resource->resource_ops->resource_unload(resource);
1625 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1627 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1628 resource_cleanup(&texture->resource);
1631 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1633 wined3d_texture_sub_resources_destroyed(texture);
1634 wined3d_texture_cleanup(texture);
1635 wined3d_resource_wait_idle(&texture->resource);
1638 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1640 unsigned int i, sub_resource_count, refcount;
1642 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1644 refcount = InterlockedDecrement(&texture->resource.ref);
1645 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1647 if (!refcount)
1649 bool in_cs_thread = GetCurrentThreadId() == texture->resource.device->cs->thread_id;
1651 /* This is called from the CS thread to destroy temporary textures. */
1652 if (!in_cs_thread)
1653 wined3d_mutex_lock();
1654 /* Wait for the texture to become idle if it's using user memory,
1655 * since the application is allowed to free that memory once the
1656 * texture is destroyed. Note that this implies that
1657 * the destroy handler can't access that memory either. */
1658 sub_resource_count = texture->layer_count * texture->level_count;
1659 for (i = 0; i < sub_resource_count; ++i)
1661 if (texture->sub_resources[i].user_memory)
1663 wined3d_resource_wait_idle(&texture->resource);
1664 break;
1667 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1668 if (!in_cs_thread)
1669 wined3d_mutex_unlock();
1672 return refcount;
1675 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1677 TRACE("texture %p.\n", texture);
1679 return &texture->resource;
1682 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1684 return c1->color_space_low_value == c2->color_space_low_value
1685 && c1->color_space_high_value == c2->color_space_high_value;
1688 /* Context activation is done by the caller */
1689 void wined3d_texture_load(struct wined3d_texture *texture,
1690 struct wined3d_context *context, BOOL srgb)
1692 UINT sub_count = texture->level_count * texture->layer_count;
1693 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1694 DWORD flag;
1695 UINT i;
1697 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1699 if (!needs_separate_srgb_gl_texture(context, texture))
1700 srgb = FALSE;
1702 if (srgb)
1703 flag = WINED3D_TEXTURE_SRGB_VALID;
1704 else
1705 flag = WINED3D_TEXTURE_RGB_VALID;
1707 if (!d3d_info->ffp_fragment_caps.color_key
1708 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1709 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1710 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1711 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1713 unsigned int i;
1715 TRACE("Reloading because of color key value change.\n");
1716 for (i = 0; i < sub_count; i++)
1718 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1719 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1720 else
1721 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1724 texture->async.gl_color_key = texture->async.src_blt_color_key;
1727 if (texture->flags & flag)
1729 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1730 return;
1733 /* Reload the surfaces if the texture is marked dirty. */
1734 for (i = 0; i < sub_count; ++i)
1736 if (!wined3d_texture_load_location(texture, i, context,
1737 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1738 ERR("Failed to load location (srgb %#x).\n", srgb);
1740 texture->flags |= flag;
1743 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1745 TRACE("texture %p.\n", texture);
1747 return texture->resource.parent;
1750 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1751 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1753 const struct wined3d_resource *resource = &texture->resource;
1754 unsigned int width = wined3d_texture_get_level_width(texture, level);
1755 unsigned int height = wined3d_texture_get_level_height(texture, level);
1757 if (texture->row_pitch)
1759 *row_pitch = texture->row_pitch;
1760 *slice_pitch = texture->slice_pitch;
1761 return;
1764 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1765 width, height, row_pitch, slice_pitch);
1768 unsigned int CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1770 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1772 return texture->lod;
1775 UINT CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1777 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1779 return texture->level_count;
1782 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1783 uint32_t flags, const struct wined3d_color_key *color_key)
1785 struct wined3d_device *device = texture->resource.device;
1786 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1787 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1789 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1791 if (flags & ~all_flags)
1793 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1794 return WINED3DERR_INVALIDCALL;
1797 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1799 return WINED3D_OK;
1802 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1803 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1804 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1805 /* Context activation is done by the caller. */
1806 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1807 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1809 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1810 struct wined3d_renderbuffer_entry *entry;
1811 unsigned int src_width, src_height;
1812 unsigned int width, height;
1813 GLuint renderbuffer = 0;
1815 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1816 return;
1818 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1820 struct wined3d_texture *rt_texture;
1821 unsigned int rt_level;
1823 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1825 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1826 return;
1828 rt_texture = wined3d_texture_from_resource(rt->resource);
1829 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1831 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1832 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1834 else
1836 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1837 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1840 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1841 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1843 /* A depth stencil smaller than the render target is not valid */
1844 if (width > src_width || height > src_height)
1845 return;
1847 /* Remove any renderbuffer set if the sizes match */
1848 if (width == src_width && height == src_height)
1850 texture_gl->current_renderbuffer = NULL;
1851 return;
1854 /* Look if we've already got a renderbuffer of the correct dimensions */
1855 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1857 if (entry->width == width && entry->height == height)
1859 renderbuffer = entry->id;
1860 texture_gl->current_renderbuffer = entry;
1861 break;
1865 if (!renderbuffer)
1867 const struct wined3d_format_gl *format_gl;
1869 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1870 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1871 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1872 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1874 entry = heap_alloc(sizeof(*entry));
1875 entry->width = width;
1876 entry->height = height;
1877 entry->id = renderbuffer;
1878 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1880 texture_gl->current_renderbuffer = entry;
1883 checkGLcall("set compatible renderbuffer");
1886 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture,
1887 unsigned int sub_resource_idx, void *mem, unsigned int pitch)
1889 unsigned int current_row_pitch, current_slice_pitch, width, height;
1890 struct wined3d_texture_sub_resource *sub_resource;
1891 unsigned int i, level, sub_resource_count;
1892 const struct wined3d_format *format;
1893 struct wined3d_device *device;
1894 unsigned int slice_pitch;
1895 bool update_memory_only;
1896 bool create_dib = false;
1898 TRACE("texture %p, sub_resource_idx %u, mem %p, pitch %u.\n", texture, sub_resource_idx, mem, pitch);
1900 device = texture->resource.device;
1901 format = texture->resource.format;
1902 level = sub_resource_idx % texture->level_count;
1903 sub_resource_count = texture->level_count * texture->layer_count;
1905 width = wined3d_texture_get_level_width(texture, level);
1906 height = wined3d_texture_get_level_height(texture, level);
1907 if (pitch)
1908 slice_pitch = height * pitch;
1909 else
1910 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1912 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1913 update_memory_only = (pitch == current_row_pitch && slice_pitch == current_slice_pitch);
1915 if (sub_resource_count > 1 && !update_memory_only)
1917 FIXME("Texture has multiple sub-resources, not supported.\n");
1918 return WINED3DERR_INVALIDCALL;
1921 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1923 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1924 return WINED3DERR_INVALIDCALL;
1927 if (texture->resource.map_count)
1929 WARN("Texture is mapped.\n");
1930 return WINED3DERR_INVALIDCALL;
1933 /* We have no way of supporting a pitch that is not a multiple of the pixel
1934 * byte width short of uploading the texture row-by-row.
1935 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1936 * for user-memory textures (it always expects packed data) while DirectDraw
1937 * requires a 4-byte aligned pitch and doesn't support texture formats
1938 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1939 * This check is here to verify that the assumption holds. */
1940 if (pitch % format->byte_count)
1942 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1943 return WINED3DERR_INVALIDCALL;
1946 if (device->d3d_initialized)
1947 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1948 wined3d_resource_wait_idle(&texture->resource);
1950 if (texture->dc_info && texture->dc_info[0].dc)
1952 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1954 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1955 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1956 create_dib = true;
1959 texture->sub_resources[sub_resource_idx].user_memory = mem;
1961 if (update_memory_only)
1963 for (i = 0; i < sub_resource_count; ++i)
1964 if (!texture->sub_resources[i].user_memory)
1965 break;
1967 if (i == sub_resource_count)
1968 wined3d_resource_free_sysmem(&texture->resource);
1970 else
1972 wined3d_resource_free_sysmem(&texture->resource);
1974 sub_resource = &texture->sub_resources[sub_resource_idx];
1976 texture->row_pitch = pitch;
1977 texture->slice_pitch = slice_pitch;
1979 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
1980 && texture->resource.usage & WINED3DUSAGE_VIDMEM_ACCOUNTING)
1981 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1982 texture->resource.size = texture->slice_pitch;
1983 sub_resource->size = texture->slice_pitch;
1984 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1987 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1988 ERR("Failed to allocate resource memory.\n");
1990 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1991 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1993 if (create_dib)
1995 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1997 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1998 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2001 return WINED3D_OK;
2004 /* Context activation is done by the caller. */
2005 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
2006 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
2008 struct wined3d_texture_sub_resource *sub_resource;
2009 struct wined3d_bo_gl *bo;
2011 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2013 if (sub_resource->bo)
2014 return;
2016 if (!(bo = heap_alloc(sizeof(*bo))))
2017 return;
2019 if (!wined3d_device_gl_create_bo(wined3d_device_gl(texture_gl->t.resource.device),
2020 context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, GL_STREAM_DRAW, true,
2021 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
2023 heap_free(bo);
2024 return;
2027 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
2028 sub_resource->bo = &bo->b;
2031 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
2033 unsigned int sub_count = texture->level_count * texture->layer_count;
2034 unsigned int i;
2036 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
2037 | WINED3D_TEXTURE_CONVERTED);
2038 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2039 for (i = 0; i < sub_count; ++i)
2041 wined3d_texture_invalidate_location(texture, i,
2042 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
2046 /* Context activation is done by the caller. */
2047 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
2048 struct wined3d_context_gl *context_gl, BOOL srgb)
2050 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
2051 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
2052 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2053 struct wined3d_resource *resource = &texture_gl->t.resource;
2054 const struct wined3d_device *device = resource->device;
2055 const struct wined3d_format *format = resource->format;
2056 const struct wined3d_color_key_conversion *conversion;
2057 const struct wined3d_format_gl *format_gl;
2058 GLenum internal;
2060 TRACE("texture_gl %p, context_gl %p, srgb %d, format %s.\n",
2061 texture_gl, context_gl, srgb, debug_d3dformat(format->id));
2063 if (!d3d_info->ffp_fragment_caps.color_key
2064 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
2065 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
2067 wined3d_texture_force_reload(&texture_gl->t);
2069 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
2070 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2073 if (texture_gl->t.flags & alloc_flag)
2074 return;
2076 if (resource->format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2078 TRACE("WINED3D_FORMAT_CAP_DECOMPRESS set.\n");
2079 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2080 format = wined3d_resource_get_decompress_format(resource);
2082 else if (format->conv_byte_count)
2084 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2086 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
2088 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2089 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
2090 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2092 format_gl = wined3d_format_gl(format);
2094 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2096 internal = wined3d_gl_get_internal_format(resource, format_gl, srgb);
2097 if (!internal)
2098 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2100 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2102 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2103 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2104 else
2105 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2106 texture_gl->t.flags |= alloc_flag;
2109 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2110 const struct wined3d_gl_info *gl_info, BOOL multisample)
2112 const struct wined3d_format_gl *format_gl;
2114 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2115 if (multisample)
2117 DWORD samples;
2119 if (texture_gl->rb_multisample)
2120 return;
2122 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2124 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2125 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2126 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2127 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2128 checkGLcall("glRenderbufferStorageMultisample()");
2129 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2131 else
2133 if (texture_gl->rb_resolved)
2134 return;
2136 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2137 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2138 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2139 texture_gl->t.resource.width, texture_gl->t.resource.height);
2140 checkGLcall("glRenderbufferStorage()");
2141 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2145 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2146 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2148 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2151 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2152 unsigned int sub_resource_idx)
2154 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2156 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
2157 return NULL;
2158 return &texture->sub_resources[sub_resource_idx];
2161 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2162 UINT layer, const struct wined3d_box *dirty_region)
2164 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2166 if (layer >= texture->layer_count)
2168 WARN("Invalid layer %u specified.\n", layer);
2169 return WINED3DERR_INVALIDCALL;
2172 if (dirty_region && FAILED(wined3d_resource_check_box_dimensions(&texture->resource, 0, dirty_region)))
2174 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2175 return WINED3DERR_INVALIDCALL;
2178 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2179 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2181 return WINED3D_OK;
2184 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2185 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2186 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2187 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2188 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2190 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2192 if (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2194 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2195 unsigned int dst_row_pitch, dst_slice_pitch;
2197 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2199 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2200 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2201 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2202 update_d, internal, dst_slice_pitch, addr);
2204 if (target == GL_TEXTURE_1D)
2206 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2207 update_w, internal, dst_row_pitch, addr));
2209 else
2211 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2213 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2214 * can't use the unpack row length like for glTexSubImage2D. */
2215 if (dst_row_pitch != src_row_pitch)
2217 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2218 update_h = src_format->block_height;
2219 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2220 &dst_row_pitch, &dst_slice_pitch);
2223 if (dst_slice_pitch != src_slice_pitch)
2225 slice_count = update_d;
2226 update_d = 1;
2229 for (slice = 0; slice < slice_count; ++slice)
2231 for (row = 0, y = dst_y; row < row_count; ++row)
2233 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2235 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2237 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2238 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2240 else
2242 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2243 update_h, internal, dst_slice_pitch, upload_addr));
2246 y += src_format->block_height;
2250 checkGLcall("Upload compressed texture data");
2252 else
2254 unsigned int y, y_count, z, z_count;
2255 bool unpacking_rows = false;
2257 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2258 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2259 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2260 update_d, format_gl->format, format_gl->type, addr);
2262 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2264 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2265 y_count = 1;
2266 unpacking_rows = true;
2268 else
2270 y_count = update_h;
2271 update_h = 1;
2274 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2276 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2277 z_count = 1;
2279 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2281 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2282 src_slice_pitch / (update_w * src_format->byte_count));
2283 z_count = 1;
2285 else
2287 z_count = update_d;
2288 update_d = 1;
2291 for (z = 0; z < z_count; ++z)
2293 for (y = 0; y < y_count; ++y)
2295 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2296 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2298 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2299 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2301 else if (target == GL_TEXTURE_1D)
2303 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2304 update_w, format_gl->format, format_gl->type, upload_addr);
2306 else
2308 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2309 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2313 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2314 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2315 checkGLcall("Upload texture data");
2319 static const struct d3dfmt_alpha_fixup
2321 enum wined3d_format_id format_id, conv_format_id;
2323 formats_src_alpha_fixup[] =
2325 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2326 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2327 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2330 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2331 const struct wined3d_format *dst_format)
2333 unsigned int i;
2335 if (!(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED) && !dst_format->alpha_size)
2336 return WINED3DFMT_UNKNOWN;
2338 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2340 if (formats_src_alpha_fixup[i].format_id == format_id)
2341 return formats_src_alpha_fixup[i].conv_format_id;
2344 return WINED3DFMT_UNKNOWN;
2347 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2348 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2349 unsigned int width, unsigned int height)
2351 unsigned int byte_count, alpha_mask;
2352 unsigned int x, y;
2354 byte_count = format->byte_count;
2355 alpha_mask = wined3d_mask_from_size(format->alpha_size) << format->alpha_offset;
2357 switch (byte_count)
2359 case 2:
2360 for (y = 0; y < height; ++y)
2362 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2363 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2365 for (x = 0; x < width; ++x)
2367 dst_row[x] = src_row[x] | alpha_mask;
2370 break;
2372 case 4:
2373 for (y = 0; y < height; ++y)
2375 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2376 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2378 for (x = 0; x < width; ++x)
2380 dst_row[x] = src_row[x] | alpha_mask;
2383 break;
2385 default:
2386 ERR("Unsupported byte count %u.\n", byte_count);
2387 break;
2391 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2392 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2393 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2394 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2395 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2397 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2398 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2399 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2400 unsigned int update_w = src_box->right - src_box->left;
2401 unsigned int update_h = src_box->bottom - src_box->top;
2402 unsigned int update_d = src_box->back - src_box->front;
2403 struct wined3d_bo_address bo;
2404 unsigned int level;
2405 BOOL srgb = FALSE;
2406 BOOL decompress;
2407 GLenum target;
2409 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2410 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2411 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2412 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2413 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2415 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2417 srgb = TRUE;
2419 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2421 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2422 return;
2425 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2427 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2429 WARN("Uploading a texture that is currently mapped, pinning sysmem.\n");
2430 dst_texture->resource.pin_sysmem = 1;
2433 if (src_format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
2435 update_h *= src_format->height_scale.numerator;
2436 update_h /= src_format->height_scale.denominator;
2439 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2440 level = dst_sub_resource_idx % dst_texture->level_count;
2442 switch (target)
2444 case GL_TEXTURE_1D_ARRAY:
2445 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2446 update_h = 1;
2447 break;
2448 case GL_TEXTURE_2D_ARRAY:
2449 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2450 update_d = 1;
2451 break;
2452 case GL_TEXTURE_2D_MULTISAMPLE:
2453 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2454 FIXME("Not supported for multisample textures.\n");
2455 return;
2458 bo.buffer_object = src_bo_addr->buffer_object;
2459 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2460 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2462 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2463 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2465 else
2467 bo.addr += src_box->top * src_row_pitch;
2468 bo.addr += src_box->left * src_format->byte_count;
2471 decompress = (dst_texture->resource.format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2472 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2474 if (src_format->upload || decompress
2475 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2476 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2478 const struct wined3d_format *compressed_format = src_format;
2479 unsigned int dst_row_pitch, dst_slice_pitch;
2480 struct wined3d_format_gl f;
2481 void *converted_mem;
2482 unsigned int z;
2483 BYTE *src_mem;
2485 if (decompress)
2487 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2489 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2491 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2492 assert(!!src_format);
2494 else
2496 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2497 ERR("Converting a block-based format.\n");
2499 f = *wined3d_format_gl(src_format);
2500 f.f.byte_count = src_format->conv_byte_count;
2501 src_format = &f.f;
2504 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2506 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2508 ERR("Failed to allocate upload buffer.\n");
2509 return;
2512 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2514 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2515 checkGLcall("glBindBuffer");
2517 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2519 if (decompress)
2520 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2521 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2522 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2523 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2524 update_w, update_h);
2525 else
2526 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2527 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2529 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_slice_pitch, dst_x,
2530 dst_y, dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2533 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2534 heap_free(converted_mem);
2536 else
2538 const uint8_t *offset = bo.addr;
2540 if (bo.buffer_object)
2542 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, wined3d_bo_gl(bo.buffer_object)->id));
2543 checkGLcall("glBindBuffer");
2544 offset += bo.buffer_object->buffer_offset;
2546 else
2548 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2549 checkGLcall("glBindBuffer");
2552 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, src_slice_pitch, dst_x,
2553 dst_y, dst_z, update_w, update_h, update_d, offset, srgb, dst_texture, gl_info);
2555 if (bo.buffer_object)
2557 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2558 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(bo.buffer_object));
2559 checkGLcall("glBindBuffer");
2563 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2565 struct wined3d_device *device = dst_texture->resource.device;
2566 unsigned int i;
2568 for (i = 0; i < device->context_count; ++i)
2570 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2575 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2576 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2578 struct wined3d_bo_gl *bo = wined3d_bo_gl(data->buffer_object);
2579 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2580 struct wined3d_texture_sub_resource *sub_resource;
2581 unsigned int dst_row_pitch, dst_slice_pitch;
2582 unsigned int src_row_pitch, src_slice_pitch;
2583 const struct wined3d_format_gl *format_gl;
2584 BYTE *temporary_mem = NULL;
2585 unsigned int level;
2586 GLenum target;
2587 void *mem;
2589 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2591 /* Only support read back of converted P8 textures. */
2592 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2593 && !format_gl->f.download)
2595 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2596 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2597 return;
2600 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2601 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2602 level = sub_resource_idx % texture_gl->t.level_count;
2604 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2606 if (format_gl->f.download)
2608 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2609 return;
2612 /* NP2 emulation is not allowed on array textures. */
2613 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2614 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2616 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2618 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2620 ERR("Out of memory.\n");
2621 return;
2625 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2627 if (format_gl->f.download)
2629 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2630 return;
2633 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2634 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2635 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2636 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2637 &src_row_pitch, &src_slice_pitch);
2638 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2640 ERR("Out of memory.\n");
2641 return;
2644 if (bo)
2645 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2646 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2647 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2650 if (format_gl->f.download)
2652 struct wined3d_format f;
2654 if (bo)
2655 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2657 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2658 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2660 f = format_gl->f;
2661 f.byte_count = format_gl->f.conv_byte_count;
2662 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2663 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2664 wined3d_texture_get_level_width(&texture_gl->t, level),
2665 wined3d_texture_get_level_height(&texture_gl->t, level),
2666 &src_row_pitch, &src_slice_pitch);
2668 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2670 ERR("Failed to allocate memory.\n");
2671 return;
2675 if (temporary_mem)
2677 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2678 checkGLcall("glBindBuffer");
2679 mem = temporary_mem;
2681 else if (bo)
2683 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2684 checkGLcall("glBindBuffer");
2685 mem = (uint8_t *)data->addr + bo->b.buffer_offset;
2687 else
2689 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2690 checkGLcall("glBindBuffer");
2691 mem = data->addr;
2694 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2696 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2697 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2699 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2700 checkGLcall("glGetCompressedTexImage");
2702 else
2704 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2705 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2707 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2708 checkGLcall("glGetTexImage");
2711 if (format_gl->f.download)
2713 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2714 wined3d_texture_get_level_width(&texture_gl->t, level),
2715 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2717 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2719 const BYTE *src_data;
2720 unsigned int h, y;
2721 BYTE *dst_data;
2722 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2723 * pitches, preventing us from using the texture pitch to box NPOT
2724 * textures. Instead, we repack the texture's CPU copy so that its
2725 * pitch equals bpp * width instead of bpp * pow2width.
2727 * Instead of boxing the texture:
2729 * │<── texture width ──>│ pow2 width ──>│
2730 * ├─────────────────────┼───────────────┼─
2731 * │111111111111111111111│ │ʌ
2732 * │222222222222222222222│ ││
2733 * │333333333333333333333│ padding │texture height
2734 * │444444444444444444444│ ││
2735 * │555555555555555555555│ │v
2736 * ├─────────────────────┘ ├─
2737 * │ │pow2 height
2738 * │ padding padding ││
2739 * │ │v
2740 * └─────────────────────────────────────┴─
2742 * we're repacking the data to the expected texture width
2744 * │<── texture width ──>│ pow2 width ──>│
2745 * ├─────────────────────┴───────────────┼─
2746 * │1111111111111111111112222222222222222│ʌ
2747 * │2222233333333333333333333344444444444││
2748 * │4444444444555555555555555555555 │texture height
2749 * │ ││
2750 * │ padding padding │v
2751 * │ ├─
2752 * │ │pow2 height
2753 * │ padding padding ││
2754 * │ │v
2755 * └─────────────────────────────────────┴─
2757 * == is the same as
2759 * │<── texture width ──>│
2760 * ├─────────────────────┼─
2761 * │111111111111111111111│ʌ
2762 * │222222222222222222222││
2763 * │333333333333333333333│texture height
2764 * │444444444444444444444││
2765 * │555555555555555555555│v
2766 * └─────────────────────┴─
2768 * This also means that any references to surface memory should work
2769 * with the data as if it were a standard texture with a NPOT width
2770 * instead of a texture boxed up to be a power-of-two texture. */
2771 src_data = mem;
2772 dst_data = data->addr;
2773 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2774 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2775 for (y = 0; y < h; ++y)
2777 memcpy(dst_data, src_data, dst_row_pitch);
2778 src_data += src_row_pitch;
2779 dst_data += dst_row_pitch;
2782 else if (temporary_mem)
2784 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2785 void *src_data = temporary_mem + layer * sub_resource->size;
2786 if (bo)
2788 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2789 checkGLcall("glBindBuffer");
2790 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER,
2791 (GLintptr)data->addr + bo->b.buffer_offset, sub_resource->size, src_data));
2792 checkGLcall("glBufferSubData");
2794 else
2796 memcpy(data->addr, src_data, sub_resource->size);
2800 if (bo)
2802 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2803 wined3d_context_gl_reference_bo(context_gl, bo);
2804 checkGLcall("glBindBuffer");
2807 heap_free(temporary_mem);
2810 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2811 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2812 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2813 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2814 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2816 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2817 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2818 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2819 unsigned int src_level, src_width, src_height, src_depth;
2820 unsigned int src_row_pitch, src_slice_pitch;
2821 const struct wined3d_format_gl *format_gl;
2822 uint8_t *offset = dst_bo_addr->addr;
2823 struct wined3d_bo *dst_bo;
2824 BOOL srgb = FALSE;
2825 GLenum target;
2827 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2828 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2829 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2830 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2831 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2833 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2835 srgb = TRUE;
2837 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2839 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2840 return;
2843 src_level = src_sub_resource_idx % src_texture->level_count;
2844 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2845 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2846 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2847 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2848 || src_box->front || src_box->back != src_depth)
2850 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2851 return;
2854 if (dst_x || dst_y || dst_z)
2856 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2857 return;
2860 if (dst_format->id != src_texture->resource.format->id)
2862 FIXME("Unhandled format conversion (%s -> %s).\n",
2863 debug_d3dformat(src_texture->resource.format->id),
2864 debug_d3dformat(dst_format->id));
2865 return;
2868 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2869 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2871 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2872 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2873 return;
2876 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2878 format_gl = wined3d_format_gl(src_texture->resource.format);
2879 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2881 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2882 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2883 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2884 || target == GL_TEXTURE_1D_ARRAY)
2886 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2887 return;
2890 if (format_gl->f.conv_byte_count)
2892 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2893 debug_d3dresourcetype(src_texture->resource.type),
2894 debug_d3dformat(format_gl->f.id));
2895 return;
2898 if ((dst_bo = dst_bo_addr->buffer_object))
2900 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, wined3d_bo_gl(dst_bo)->id));
2901 checkGLcall("glBindBuffer");
2902 offset += dst_bo->buffer_offset;
2904 else
2906 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2907 checkGLcall("glBindBuffer");
2910 if (src_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2912 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2913 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
2915 GL_EXTCALL(glGetCompressedTexImage(target, src_level, offset));
2916 checkGLcall("glGetCompressedTexImage");
2918 else
2920 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2921 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
2923 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, offset);
2924 checkGLcall("glGetTexImage");
2927 if (dst_bo)
2929 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2930 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(dst_bo));
2931 checkGLcall("glBindBuffer");
2935 /* Context activation is done by the caller. */
2936 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2937 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2939 struct wined3d_texture_sub_resource *sub_resource;
2941 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2943 /* We cannot download data from multisample textures directly. */
2944 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB)
2945 || sub_resource->locations & WINED3D_LOCATION_RB_MULTISAMPLE)
2946 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2948 if (sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED)
2950 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2951 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2952 return TRUE;
2955 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2957 unsigned int row_pitch, slice_pitch, level;
2958 struct wined3d_bo_address data;
2959 struct wined3d_box src_box;
2960 unsigned int src_location;
2962 level = sub_resource_idx % texture_gl->t.level_count;
2963 wined3d_texture_get_bo_address(&texture_gl->t, sub_resource_idx, &data, dst_location);
2964 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2965 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
2966 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
2967 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
2968 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
2969 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
2971 ++texture_gl->t.download_count;
2972 return TRUE;
2975 if (sub_resource->locations & WINED3D_LOCATION_DRAWABLE)
2977 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2978 texture_gl->t.resource.draw_binding, dst_location);
2979 return TRUE;
2982 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
2983 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
2985 return FALSE;
2988 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
2989 unsigned int sub_resource_idx, struct wined3d_context *context)
2991 struct wined3d_device *device;
2992 unsigned int level;
2993 RECT r;
2995 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
2997 DWORD current = texture->sub_resources[sub_resource_idx].locations;
2998 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
2999 wined3d_debug_location(current));
3000 return FALSE;
3003 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3004 && wined3d_resource_is_offscreen(&texture->resource))
3006 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
3007 return FALSE;
3010 device = texture->resource.device;
3011 level = sub_resource_idx % texture->level_count;
3012 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
3013 wined3d_texture_get_level_height(texture, level));
3014 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3015 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
3016 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
3017 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
3018 NULL, WINED3D_TEXF_POINT, NULL);
3020 return TRUE;
3023 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
3024 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
3026 unsigned int level = sub_resource_idx % texture->level_count;
3027 const RECT rect = {0, 0,
3028 wined3d_texture_get_level_width(texture, level),
3029 wined3d_texture_get_level_height(texture, level)};
3030 struct wined3d_texture_sub_resource *sub_resource;
3031 DWORD src_location, locations;
3033 sub_resource = &texture->sub_resources[sub_resource_idx];
3034 locations = sub_resource->locations;
3035 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3037 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3038 wined3d_debug_location(locations));
3039 return FALSE;
3042 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
3043 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
3044 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
3045 src_location = WINED3D_LOCATION_RB_RESOLVED;
3046 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
3047 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
3048 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
3049 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3050 else if (locations & WINED3D_LOCATION_DRAWABLE)
3051 src_location = WINED3D_LOCATION_DRAWABLE;
3052 else /* texture2d_blt_fbo() will load the source location if necessary. */
3053 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3055 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
3056 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect, NULL);
3058 return TRUE;
3061 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
3062 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
3064 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
3065 struct wined3d_device *device = texture_gl->t.resource.device;
3066 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3067 const struct wined3d_color_key_conversion *conversion;
3068 struct wined3d_texture_sub_resource *sub_resource;
3069 const struct wined3d_format *format;
3070 struct wined3d_bo_address data;
3071 BYTE *src_mem, *dst_mem = NULL;
3072 struct wined3d_box src_box;
3073 DWORD dst_location;
3074 BOOL depth;
3076 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
3077 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3079 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
3080 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
3081 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3083 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3085 return TRUE;
3088 level = sub_resource_idx % texture_gl->t.level_count;
3089 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3091 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3092 && (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)
3093 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3094 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3095 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3097 RECT src_rect;
3099 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3100 if (srgb)
3101 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3102 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3103 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect, NULL);
3104 else
3105 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3106 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3107 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect, NULL);
3109 return TRUE;
3112 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3113 && (!srgb || (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)))
3115 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3116 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3117 RECT src_rect;
3119 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3120 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3121 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3122 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3123 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3124 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect, NULL);
3126 return TRUE;
3129 /* Upload from system memory */
3131 if (srgb)
3133 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3134 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3135 == WINED3D_LOCATION_TEXTURE_RGB)
3137 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3138 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3139 &context_gl->c, texture_gl->t.resource.map_binding);
3142 else
3144 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3145 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3146 == WINED3D_LOCATION_TEXTURE_SRGB)
3148 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3149 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3150 &context_gl->c, texture_gl->t.resource.map_binding);
3154 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3156 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3157 /* Lets hope we get it from somewhere... */
3158 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3161 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3163 format = texture_gl->t.resource.format;
3164 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3165 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3167 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3168 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3169 * getting called. */
3170 if (conversion && sub_resource->bo)
3172 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3174 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3175 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3178 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &context_gl->c, &data);
3179 if (conversion)
3181 width = src_box.right - src_box.left;
3182 height = src_box.bottom - src_box.top;
3183 wined3d_format_calculate_pitch(format, device->surface_alignment,
3184 width, height, &dst_row_pitch, &dst_slice_pitch);
3186 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3187 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3189 ERR("Out of memory (%u).\n", dst_slice_pitch);
3190 return FALSE;
3192 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3193 width, height, &texture_gl->t.async.gl_color_key);
3194 src_row_pitch = dst_row_pitch;
3195 src_slice_pitch = dst_slice_pitch;
3196 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3198 data.buffer_object = 0;
3199 data.addr = dst_mem;
3202 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3203 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3205 heap_free(dst_mem);
3207 return TRUE;
3210 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3211 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3213 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3214 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3216 switch (location)
3218 case WINED3D_LOCATION_SYSMEM:
3219 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3220 : wined3d_resource_prepare_sysmem(&texture->resource);
3222 case WINED3D_LOCATION_BUFFER:
3223 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3224 return TRUE;
3226 case WINED3D_LOCATION_TEXTURE_RGB:
3227 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3228 return TRUE;
3230 case WINED3D_LOCATION_TEXTURE_SRGB:
3231 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3232 return TRUE;
3234 case WINED3D_LOCATION_DRAWABLE:
3235 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3236 ERR("Texture %p does not have a drawable.\n", texture);
3237 return TRUE;
3239 case WINED3D_LOCATION_RB_MULTISAMPLE:
3240 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3241 return TRUE;
3243 case WINED3D_LOCATION_RB_RESOLVED:
3244 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3245 return TRUE;
3247 default:
3248 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3249 return FALSE;
3253 static bool use_ffp_clear(const struct wined3d_texture *texture, unsigned int location)
3255 if (location == WINED3D_LOCATION_DRAWABLE)
3256 return true;
3258 /* If we are not using FBOs (and not rendering to the drawable), always
3259 * upload. The upload should always succeed in this case; we cannot have
3260 * ARB_texture_multisample without ARB_framebuffer_object. */
3261 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3262 return false;
3264 if (location == WINED3D_LOCATION_TEXTURE_RGB
3265 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))
3266 return false;
3267 if (location == WINED3D_LOCATION_TEXTURE_SRGB
3268 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB))
3269 return false;
3271 return location & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED
3272 | WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
3275 static bool wined3d_texture_gl_clear(struct wined3d_texture *texture,
3276 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, unsigned int location)
3278 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3279 const struct wined3d_format *format = texture->resource.format;
3280 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3281 struct wined3d_bo_address addr;
3283 /* The code that delays clears is Vulkan-specific, so here we should only
3284 * encounter WINED3D_LOCATION_CLEARED on newly created resources and thus
3285 * a zero clear value. */
3286 if (!format->depth_size && !format->stencil_size)
3288 if (sub_resource->clear_value.colour.r || sub_resource->clear_value.colour.g
3289 || sub_resource->clear_value.colour.b || sub_resource->clear_value.colour.a)
3291 ERR("Unexpected color clear value r=%08e, g=%08e, b=%08e, a=%08e.\n",
3292 sub_resource->clear_value.colour.r, sub_resource->clear_value.colour.g,
3293 sub_resource->clear_value.colour.b, sub_resource->clear_value.colour.a);
3296 else
3298 if (format->depth_size && sub_resource->clear_value.depth)
3299 ERR("Unexpected depth clear value %08e.\n", sub_resource->clear_value.depth);
3300 if (format->stencil_size && sub_resource->clear_value.stencil)
3301 ERR("Unexpected stencil clear value %x.\n", sub_resource->clear_value.stencil);
3304 if (use_ffp_clear(texture, location))
3306 GLbitfield clear_mask = 0;
3308 context_gl_apply_texture_draw_state(context_gl, texture, sub_resource_idx, location);
3310 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3311 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3313 if (format->depth_size)
3315 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
3316 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3318 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
3319 GL_EXTCALL(glClearDepthf(0.0f));
3320 else
3321 gl_info->gl_ops.gl.p_glClearDepth(0.0);
3322 clear_mask |= GL_DEPTH_BUFFER_BIT;
3325 if (format->stencil_size)
3327 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
3328 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3329 gl_info->gl_ops.gl.p_glStencilMask(~0u);
3330 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3331 gl_info->gl_ops.gl.p_glClearStencil(0);
3332 clear_mask |= GL_STENCIL_BUFFER_BIT;
3335 if (!format->depth_size && !format->stencil_size)
3337 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3338 context_invalidate_state(&context_gl->c, STATE_BLEND);
3339 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
3340 clear_mask |= GL_COLOR_BUFFER_BIT;
3343 gl_info->gl_ops.gl.p_glClear(clear_mask);
3344 checkGLcall("clear texture");
3346 wined3d_texture_validate_location(texture, sub_resource_idx, location);
3347 return true;
3350 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM))
3351 return false;
3352 wined3d_texture_get_bo_address(texture, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
3353 memset(addr.addr, 0, sub_resource->size);
3354 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
3355 return true;
3358 /* Context activation is done by the caller. */
3359 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3360 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
3362 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3363 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3364 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3366 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3367 texture, sub_resource_idx, context, wined3d_debug_location(location));
3369 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3370 return FALSE;
3372 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
3374 if (!wined3d_texture_gl_clear(texture, sub_resource_idx, context_gl, location))
3375 return FALSE;
3377 if (sub_resource->locations & location)
3378 return TRUE;
3381 switch (location)
3383 case WINED3D_LOCATION_SYSMEM:
3384 case WINED3D_LOCATION_BUFFER:
3385 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3387 case WINED3D_LOCATION_DRAWABLE:
3388 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3390 case WINED3D_LOCATION_RB_RESOLVED:
3391 case WINED3D_LOCATION_RB_MULTISAMPLE:
3392 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3394 case WINED3D_LOCATION_TEXTURE_RGB:
3395 case WINED3D_LOCATION_TEXTURE_SRGB:
3396 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3397 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3399 default:
3400 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3401 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3402 return FALSE;
3406 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3407 struct wined3d_context *context, unsigned int location)
3409 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3410 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3411 struct wined3d_renderbuffer_entry *entry, *entry2;
3412 unsigned int i, sub_count;
3414 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3416 switch (location)
3418 case WINED3D_LOCATION_BUFFER:
3419 sub_count = texture->level_count * texture->layer_count;
3420 for (i = 0; i < sub_count; ++i)
3422 if (texture_gl->t.sub_resources[i].bo)
3423 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3425 break;
3427 case WINED3D_LOCATION_TEXTURE_RGB:
3428 if (texture_gl->texture_rgb.name)
3429 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3430 break;
3432 case WINED3D_LOCATION_TEXTURE_SRGB:
3433 if (texture_gl->texture_srgb.name)
3434 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3435 break;
3437 case WINED3D_LOCATION_RB_MULTISAMPLE:
3438 if (texture_gl->rb_multisample)
3440 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3441 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3442 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3443 texture_gl->rb_multisample = 0;
3445 break;
3447 case WINED3D_LOCATION_RB_RESOLVED:
3448 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3449 struct wined3d_renderbuffer_entry, entry)
3451 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3452 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3453 list_remove(&entry->entry);
3454 heap_free(entry);
3456 list_init(&texture_gl->renderbuffers);
3457 texture_gl->current_renderbuffer = NULL;
3459 if (texture_gl->rb_resolved)
3461 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3462 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3463 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3464 texture_gl->rb_resolved = 0;
3466 break;
3468 default:
3469 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3470 break;
3474 static const struct wined3d_texture_ops texture_gl_ops =
3476 wined3d_texture_gl_prepare_location,
3477 wined3d_texture_gl_load_location,
3478 wined3d_texture_gl_unload_location,
3479 wined3d_texture_gl_upload_data,
3480 wined3d_texture_gl_download_data,
3483 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3485 return texture_from_resource(resource);
3488 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3490 return wined3d_texture_incref(texture_from_resource(resource));
3493 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3495 return wined3d_texture_decref(texture_from_resource(resource));
3498 static void texture_resource_preload(struct wined3d_resource *resource)
3500 struct wined3d_texture *texture = texture_from_resource(resource);
3501 struct wined3d_context *context;
3503 context = context_acquire(resource->device, NULL, 0);
3504 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3505 context_release(context);
3508 static void texture_resource_unload(struct wined3d_resource *resource)
3510 struct wined3d_texture *texture = texture_from_resource(resource);
3511 struct wined3d_device *device = resource->device;
3512 const struct wined3d_state *state = &device->cs->state;
3513 unsigned int location = resource->map_binding;
3514 struct wined3d_context *context;
3515 unsigned int sub_count, i;
3517 TRACE("resource %p.\n", resource);
3519 /* D3D is not initialised, so no GPU locations should currently exist.
3520 * Moreover, we may not be able to acquire a valid context. */
3521 if (!device->d3d_initialized)
3522 return;
3524 context = context_acquire(device, NULL, 0);
3526 if (location == WINED3D_LOCATION_BUFFER)
3527 location = WINED3D_LOCATION_SYSMEM;
3529 sub_count = texture->level_count * texture->layer_count;
3530 for (i = 0; i < sub_count; ++i)
3532 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3533 && wined3d_texture_load_location(texture, i, context, location))
3535 wined3d_texture_invalidate_location(texture, i, ~location);
3537 else
3539 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3540 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3541 debug_d3dresourcetype(resource->type), resource, i,
3542 wined3d_debug_resource_access(resource->access));
3543 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3544 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3548 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3549 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3550 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3551 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3552 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3554 context_release(context);
3556 wined3d_texture_force_reload(texture);
3558 for (i = 0; i < ARRAY_SIZE(state->textures); ++i)
3560 if (state->textures[i] == texture)
3561 device_invalidate_state(device, STATE_SAMPLER(i));
3564 wined3d_texture_set_dirty(texture);
3566 resource_unload(&texture->resource);
3569 static HRESULT texture_resource_sub_resource_get_desc(struct wined3d_resource *resource,
3570 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3572 const struct wined3d_texture *texture = texture_from_resource(resource);
3574 return wined3d_texture_get_sub_resource_desc(texture, sub_resource_idx, desc);
3577 static void texture_resource_sub_resource_get_map_pitch(struct wined3d_resource *resource,
3578 unsigned int sub_resource_idx, unsigned int *row_pitch, unsigned int *slice_pitch)
3580 const struct wined3d_texture *texture = texture_from_resource(resource);
3581 unsigned int level = sub_resource_idx % texture->level_count;
3583 if (resource->format_attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3585 *row_pitch = wined3d_texture_get_level_width(texture, level) * resource->format->byte_count;
3586 *slice_pitch = wined3d_texture_get_level_height(texture, level) * (*row_pitch);
3588 else
3590 wined3d_texture_get_pitch(texture, level, row_pitch, slice_pitch);
3594 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3595 void **map_ptr, const struct wined3d_box *box, uint32_t flags)
3597 struct wined3d_texture_sub_resource *sub_resource;
3598 struct wined3d_device *device = resource->device;
3599 struct wined3d_context *context;
3600 struct wined3d_texture *texture;
3601 struct wined3d_bo_address data;
3602 unsigned int texture_level;
3603 BYTE *base_memory;
3604 BOOL ret = TRUE;
3606 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
3607 resource, sub_resource_idx, map_ptr, debug_box(box), flags);
3609 texture = texture_from_resource(resource);
3610 sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx);
3612 texture_level = sub_resource_idx % texture->level_count;
3614 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3616 WARN("DC is in use.\n");
3617 return WINED3DERR_INVALIDCALL;
3620 if (sub_resource->map_count)
3622 WARN("Sub-resource is already mapped.\n");
3623 return WINED3DERR_INVALIDCALL;
3626 context = context_acquire(device, NULL, 0);
3628 if (flags & WINED3D_MAP_DISCARD)
3630 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3631 wined3d_debug_location(resource->map_binding));
3632 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3633 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3635 else
3637 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3638 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3639 if (!texture_level)
3641 unsigned int i;
3643 for (i = 0; i < texture->level_count; ++i)
3645 if (!(ret = wined3d_texture_load_location(texture, sub_resource_idx + i, context, resource->map_binding)))
3646 break;
3649 else
3651 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3655 if (!ret)
3657 ERR("Failed to prepare location.\n");
3658 context_release(context);
3659 return E_OUTOFMEMORY;
3662 /* We only record dirty regions for the top-most level. */
3663 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3664 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3665 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3667 if (flags & WINED3D_MAP_WRITE)
3669 if (!texture_level)
3671 unsigned int i;
3673 for (i = 0; i < texture->level_count; ++i)
3674 wined3d_texture_invalidate_location(texture, sub_resource_idx + i, ~resource->map_binding);
3676 else
3678 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3682 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, resource->map_binding);
3683 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3684 sub_resource->map_flags = flags;
3685 TRACE("Base memory pointer %p.\n", base_memory);
3687 context_release(context);
3689 *map_ptr = resource_offset_map_pointer(resource, sub_resource_idx, base_memory, box);
3691 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3693 RECT *r = &texture->swapchain->front_buffer_update;
3695 SetRect(r, box->left, box->top, box->right, box->bottom);
3696 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3699 ++resource->map_count;
3700 ++sub_resource->map_count;
3702 TRACE("Returning memory %p.\n", *map_ptr);
3704 return WINED3D_OK;
3707 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3709 struct wined3d_texture_sub_resource *sub_resource;
3710 struct wined3d_device *device = resource->device;
3711 struct wined3d_context *context;
3712 struct wined3d_texture *texture;
3713 struct wined3d_bo_address data;
3714 struct wined3d_range range;
3716 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3718 texture = texture_from_resource(resource);
3719 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3720 return E_INVALIDARG;
3722 if (!sub_resource->map_count)
3724 WARN("Trying to unmap unmapped sub-resource.\n");
3725 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3726 return WINED3D_OK;
3727 return WINEDDERR_NOTLOCKED;
3730 context = context_acquire(device, NULL, 0);
3732 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
3733 range.offset = 0;
3734 range.size = sub_resource->size;
3735 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3737 context_release(context);
3739 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3741 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3742 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3745 --sub_resource->map_count;
3746 if (!--resource->map_count && texture->update_map_binding)
3747 wined3d_texture_update_map_binding(texture);
3749 return WINED3D_OK;
3752 static const struct wined3d_resource_ops texture_resource_ops =
3754 texture_resource_incref,
3755 texture_resource_decref,
3756 texture_resource_preload,
3757 texture_resource_unload,
3758 texture_resource_sub_resource_get_desc,
3759 texture_resource_sub_resource_get_map_pitch,
3760 texture_resource_sub_resource_map,
3761 texture_resource_sub_resource_unmap,
3764 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3765 unsigned int layer_count, unsigned int level_count, uint32_t flags, struct wined3d_device *device,
3766 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3767 const struct wined3d_texture_ops *texture_ops)
3769 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3770 struct wined3d_device_parent *device_parent = device->device_parent;
3771 unsigned int sub_count, i, j, size, offset = 0;
3772 unsigned int pow2_width, pow2_height;
3773 const struct wined3d_format *format;
3774 HRESULT hr;
3776 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3777 "usage %s, bind_flags %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3778 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3779 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format), desc->multisample_type,
3780 desc->multisample_quality, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
3781 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3782 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3784 if (!desc->width || !desc->height || !desc->depth)
3785 return WINED3DERR_INVALIDCALL;
3787 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3789 ERR("Invalid layer count for volume texture.\n");
3790 return E_INVALIDARG;
3793 texture->sub_resources = sub_resources;
3795 /* TODO: It should only be possible to create textures for formats
3796 * that are reported as supported. */
3797 if (WINED3DFMT_UNKNOWN >= desc->format)
3799 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3800 return WINED3DERR_INVALIDCALL;
3802 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3804 if ((desc->usage & WINED3DUSAGE_DYNAMIC) && (desc->usage & (WINED3DUSAGE_MANAGED | WINED3DUSAGE_SCRATCH)))
3806 WARN("Attempted to create a dynamic texture with usage %s.\n", debug_d3dusage(desc->usage));
3807 return WINED3DERR_INVALIDCALL;
3810 pow2_width = desc->width;
3811 pow2_height = desc->height;
3812 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3813 && !d3d_info->texture_npot)
3815 /* level_count == 0 returns an error as well. */
3816 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3818 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3820 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3821 "texture without unconditional NPOT support.\n");
3822 return WINED3DERR_INVALIDCALL;
3825 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3827 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3829 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3831 /* TODO: Add support for non-power-of-two compressed textures. */
3832 if (format->attrs & (WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_HEIGHT_SCALE))
3834 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3835 desc->width, desc->height);
3836 return WINED3DERR_NOTAVAILABLE;
3839 /* Find the nearest pow2 match. */
3840 pow2_width = pow2_height = 1;
3841 while (pow2_width < desc->width)
3842 pow2_width <<= 1;
3843 while (pow2_height < desc->height)
3844 pow2_height <<= 1;
3845 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3848 texture->pow2_width = pow2_width;
3849 texture->pow2_height = pow2_height;
3851 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3852 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3854 /* One of four options:
3855 * 1: Do the same as we do with NPOT and scale the texture. (Any
3856 * texture ops would require the texture to be scaled which is
3857 * potentially slow.)
3858 * 2: Set the texture to the maximum size (bad idea).
3859 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3860 * 4: Create the surface, but allow it to be used only for DirectDraw
3861 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3862 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3863 * the render target. */
3864 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3866 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3867 return WINED3DERR_NOTAVAILABLE;
3870 /* We should never use this surface in combination with OpenGL. */
3871 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3874 for (i = 0; i < layer_count; ++i)
3876 for (j = 0; j < level_count; ++j)
3878 unsigned int idx = i * level_count + j;
3880 size = wined3d_format_calculate_size(format, device->surface_alignment,
3881 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3882 texture->sub_resources[idx].offset = offset;
3883 texture->sub_resources[idx].size = size;
3884 offset += size;
3886 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3889 if (!offset)
3890 return WINED3DERR_INVALIDCALL;
3892 /* Ensure the last mip-level is at least large enough to hold a single
3893 * compressed block. It is questionable how useful these mip-levels are to
3894 * the application with "broken pitch" formats, but we want to avoid
3895 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3896 if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3898 unsigned int min_size;
3900 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3901 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3902 if (min_size > offset)
3903 offset = min_size;
3906 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3907 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3908 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3910 static unsigned int once;
3912 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3913 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3914 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3915 && !(format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE)
3916 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3917 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3919 WARN("Failed to initialize resource, returning %#lx\n", hr);
3920 return hr;
3922 wined3d_resource_update_draw_binding(&texture->resource);
3924 texture->texture_ops = texture_ops;
3926 texture->layer_count = layer_count;
3927 texture->level_count = level_count;
3928 texture->lod = 0;
3929 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_DOWNLOADABLE;
3930 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3932 texture->flags |= WINED3D_TEXTURE_GET_DC_LENIENT;
3933 texture->resource.pin_sysmem = 1;
3935 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3936 texture->flags |= WINED3D_TEXTURE_GET_DC;
3937 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3938 texture->flags |= WINED3D_TEXTURE_DISCARD;
3939 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3941 if (!(texture->resource.format_caps & WINED3D_FORMAT_CAP_GEN_MIPMAP))
3942 WARN("Format doesn't support mipmaps generation, "
3943 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3944 else
3945 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3948 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS)
3950 if (!(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3952 wined3d_texture_cleanup_sync(texture);
3953 return E_OUTOFMEMORY;
3955 for (i = 0; i < texture->layer_count; ++i)
3956 wined3d_texture_dirty_region_add(texture, i, NULL);
3959 /* Precalculated scaling for 'faked' non power of two texture coords. */
3960 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3962 texture->pow2_matrix[0] = (float)desc->width;
3963 texture->pow2_matrix[5] = (float)desc->height;
3964 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3966 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3968 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3969 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3970 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3972 else
3974 texture->pow2_matrix[0] = 1.0f;
3975 texture->pow2_matrix[5] = 1.0f;
3977 texture->pow2_matrix[10] = 1.0f;
3978 texture->pow2_matrix[15] = 1.0f;
3979 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3981 if (wined3d_texture_use_pbo(texture, d3d_info))
3982 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3984 sub_count = level_count * layer_count;
3985 if (sub_count / layer_count != level_count)
3987 wined3d_texture_cleanup_sync(texture);
3988 return E_OUTOFMEMORY;
3991 if (desc->usage & WINED3DUSAGE_OVERLAY)
3993 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3995 wined3d_texture_cleanup_sync(texture);
3996 return E_OUTOFMEMORY;
3999 for (i = 0; i < sub_count; ++i)
4001 list_init(&texture->overlay_info[i].entry);
4002 list_init(&texture->overlay_info[i].overlays);
4006 /* Generate all sub-resources. */
4007 for (i = 0; i < sub_count; ++i)
4009 struct wined3d_texture_sub_resource *sub_resource;
4011 sub_resource = &texture->sub_resources[i];
4012 sub_resource->locations = WINED3D_LOCATION_CLEARED;
4014 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
4015 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
4017 WARN("Failed to create sub-resource parent, hr %#lx.\n", hr);
4018 sub_resource->parent = NULL;
4019 wined3d_texture_cleanup_sync(texture);
4020 return hr;
4023 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
4025 TRACE("Created sub-resource %u (level %u, layer %u).\n",
4026 i, i % texture->level_count, i / texture->level_count);
4028 if (desc->usage & WINED3DUSAGE_OWNDC)
4030 struct wined3d_texture_idx texture_idx = {texture, i};
4032 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4033 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4034 if (!texture->dc_info || !texture->dc_info[i].dc)
4036 wined3d_texture_cleanup_sync(texture);
4037 return WINED3DERR_INVALIDCALL;
4042 return WINED3D_OK;
4045 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
4046 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
4047 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
4048 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
4050 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
4051 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
4052 HRESULT hr;
4054 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
4055 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
4056 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
4057 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
4059 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx)
4060 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4061 return WINED3DERR_INVALIDCALL;
4063 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx)
4064 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4065 return WINED3DERR_INVALIDCALL;
4067 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
4068 && filter != WINED3D_TEXF_LINEAR)
4069 return WINED3DERR_INVALIDCALL;
4071 if (FAILED(hr = wined3d_resource_check_box_dimensions(&dst_texture->resource, dst_sub_resource_idx, &dst_box)))
4072 return hr;
4074 if (FAILED(hr = wined3d_resource_check_box_dimensions(&src_texture->resource, src_sub_resource_idx, &src_box)))
4075 return hr;
4077 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
4078 || src_texture->sub_resources[src_sub_resource_idx].map_count)
4080 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
4081 return WINEDDERR_SURFACEBUSY;
4084 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
4085 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
4087 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
4088 return WINED3DERR_INVALIDCALL;
4091 if (dst_texture->resource.device != src_texture->resource.device)
4093 FIXME("Rejecting cross-device blit.\n");
4094 return E_NOTIMPL;
4097 wined3d_device_context_emit_blt_sub_resource(context, &dst_texture->resource, dst_sub_resource_idx,
4098 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
4100 if (dst_texture->dirty_regions)
4101 wined3d_texture_add_dirty_region(dst_texture, dst_sub_resource_idx, &dst_box);
4103 return WINED3D_OK;
4106 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
4107 unsigned int sub_resource_idx, LONG *x, LONG *y)
4109 struct wined3d_overlay_info *overlay;
4111 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
4113 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4114 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4115 return WINEDDERR_NOTAOVERLAYSURFACE;
4117 overlay = &texture->overlay_info[sub_resource_idx];
4118 if (!overlay->dst_texture)
4120 TRACE("Overlay not visible.\n");
4121 *x = 0;
4122 *y = 0;
4123 return WINEDDERR_OVERLAYNOTVISIBLE;
4126 *x = overlay->dst_rect.left;
4127 *y = overlay->dst_rect.top;
4129 TRACE("Returning position %ld, %ld.\n", *x, *y);
4131 return WINED3D_OK;
4134 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
4135 unsigned int sub_resource_idx, LONG x, LONG y)
4137 struct wined3d_overlay_info *overlay;
4138 LONG w, h;
4140 TRACE("texture %p, sub_resource_idx %u, x %ld, y %ld.\n", texture, sub_resource_idx, x, y);
4142 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4143 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4144 return WINEDDERR_NOTAOVERLAYSURFACE;
4146 overlay = &texture->overlay_info[sub_resource_idx];
4147 w = overlay->dst_rect.right - overlay->dst_rect.left;
4148 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
4149 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
4151 return WINED3D_OK;
4154 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4155 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4156 const RECT *dst_rect, uint32_t flags)
4158 struct wined3d_overlay_info *overlay;
4159 unsigned int level, dst_level;
4161 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
4162 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
4163 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
4164 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
4166 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4167 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4168 return WINEDDERR_NOTAOVERLAYSURFACE;
4170 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4171 || !wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
4172 return WINED3DERR_INVALIDCALL;
4174 overlay = &texture->overlay_info[sub_resource_idx];
4176 level = sub_resource_idx % texture->level_count;
4177 if (src_rect)
4178 overlay->src_rect = *src_rect;
4179 else
4180 SetRect(&overlay->src_rect, 0, 0,
4181 wined3d_texture_get_level_width(texture, level),
4182 wined3d_texture_get_level_height(texture, level));
4184 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4185 if (dst_rect)
4186 overlay->dst_rect = *dst_rect;
4187 else
4188 SetRect(&overlay->dst_rect, 0, 0,
4189 wined3d_texture_get_level_width(dst_texture, dst_level),
4190 wined3d_texture_get_level_height(dst_texture, dst_level));
4192 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4193 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4195 overlay->dst_texture = NULL;
4196 list_remove(&overlay->entry);
4199 if (flags & WINEDDOVER_SHOW)
4201 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4203 overlay->dst_texture = dst_texture;
4204 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4205 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4208 else if (flags & WINEDDOVER_HIDE)
4210 /* Tests show that the rectangles are erased on hide. */
4211 SetRectEmpty(&overlay->src_rect);
4212 SetRectEmpty(&overlay->dst_rect);
4213 overlay->dst_texture = NULL;
4216 return WINED3D_OK;
4219 struct wined3d_swapchain * CDECL wined3d_texture_get_swapchain(struct wined3d_texture *texture)
4221 return texture->swapchain;
4224 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4226 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4228 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4229 return NULL;
4231 return texture->sub_resources[sub_resource_idx].parent;
4234 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4235 unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops)
4237 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4239 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4240 return;
4242 texture->sub_resources[sub_resource_idx].parent = parent;
4243 texture->sub_resources[sub_resource_idx].parent_ops = parent_ops;
4246 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4247 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4249 const struct wined3d_resource *resource;
4250 unsigned int level_idx;
4252 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4254 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4255 return WINED3DERR_INVALIDCALL;
4257 resource = &texture->resource;
4258 desc->format = resource->format->id;
4259 desc->multisample_type = resource->multisample_type;
4260 desc->multisample_quality = resource->multisample_quality;
4261 desc->usage = resource->usage;
4262 desc->bind_flags = resource->bind_flags;
4263 desc->access = resource->access;
4265 level_idx = sub_resource_idx % texture->level_count;
4266 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4267 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4268 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4269 desc->size = texture->sub_resources[sub_resource_idx].size;
4271 return WINED3D_OK;
4274 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4275 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4276 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4278 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device->adapter)->gl_info;
4279 HRESULT hr;
4281 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4282 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4283 texture_gl, device, desc, layer_count,
4284 level_count, flags, parent, parent_ops);
4286 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4287 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4289 WARN("OpenGL implementation does not support array textures.\n");
4290 return WINED3DERR_INVALIDCALL;
4293 switch (desc->resource_type)
4295 case WINED3D_RTYPE_TEXTURE_1D:
4296 if (layer_count > 1)
4297 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4298 else
4299 texture_gl->target = GL_TEXTURE_1D;
4300 break;
4302 case WINED3D_RTYPE_TEXTURE_2D:
4303 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4305 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4307 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4309 if (layer_count > 1)
4310 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4311 else
4312 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4314 else
4316 if (layer_count > 1)
4317 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4318 else
4319 texture_gl->target = GL_TEXTURE_2D;
4321 break;
4323 case WINED3D_RTYPE_TEXTURE_3D:
4324 if (!gl_info->supported[EXT_TEXTURE3D])
4326 WARN("OpenGL implementation does not support 3D textures.\n");
4327 return WINED3DERR_INVALIDCALL;
4329 texture_gl->target = GL_TEXTURE_3D;
4330 break;
4332 default:
4333 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4334 return WINED3DERR_INVALIDCALL;
4337 list_init(&texture_gl->renderbuffers);
4339 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4340 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4341 return hr;
4343 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4344 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4346 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4347 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4349 return WINED3D_OK;
4352 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4353 UINT layer_count, UINT level_count, uint32_t flags, const struct wined3d_sub_resource_data *data,
4354 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4356 unsigned int sub_count = level_count * layer_count;
4357 unsigned int i;
4358 HRESULT hr;
4360 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4361 "parent %p, parent_ops %p, texture %p.\n",
4362 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4364 if (!layer_count)
4366 WARN("Invalid layer count.\n");
4367 return E_INVALIDARG;
4369 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4371 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4372 layer_count = 6;
4375 if (!level_count)
4377 WARN("Invalid level count.\n");
4378 return WINED3DERR_INVALIDCALL;
4381 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4383 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4385 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4386 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4388 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4389 desc->multisample_quality);
4390 return WINED3DERR_NOTAVAILABLE;
4392 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4393 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4394 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4396 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4397 desc->multisample_quality);
4398 return WINED3DERR_NOTAVAILABLE;
4402 if (data)
4404 for (i = 0; i < sub_count; ++i)
4406 if (data[i].data)
4407 continue;
4409 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4410 return E_INVALIDARG;
4414 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4415 layer_count, level_count, flags, parent, parent_ops, texture)))
4416 return hr;
4418 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4419 * in this case. */
4420 if (data)
4422 struct wined3d_box box;
4424 for (i = 0; i < sub_count; ++i)
4426 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4427 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4428 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4432 TRACE("Created texture %p.\n", *texture);
4434 return WINED3D_OK;
4437 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4439 struct wined3d_device *device = texture->resource.device;
4440 struct wined3d_texture_sub_resource *sub_resource;
4441 struct wined3d_dc_info *dc_info;
4443 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4445 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4447 WARN("Texture does not support GetDC\n");
4448 /* Don't touch the DC */
4449 return WINED3DERR_INVALIDCALL;
4452 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4453 return WINED3DERR_INVALIDCALL;
4455 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4457 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4458 return WINED3DERR_INVALIDCALL;
4461 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4462 return WINED3DERR_INVALIDCALL;
4464 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4466 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4468 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4469 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4470 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4471 return WINED3DERR_INVALIDCALL;
4474 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4475 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4476 ++texture->resource.map_count;
4477 ++sub_resource->map_count;
4479 *dc = dc_info[sub_resource_idx].dc;
4480 TRACE("Returning dc %p.\n", *dc);
4482 return WINED3D_OK;
4485 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4487 struct wined3d_device *device = texture->resource.device;
4488 struct wined3d_texture_sub_resource *sub_resource;
4489 struct wined3d_dc_info *dc_info;
4491 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4493 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4494 return WINED3DERR_INVALIDCALL;
4496 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4498 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4499 return WINED3DERR_INVALIDCALL;
4502 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4503 return WINED3DERR_INVALIDCALL;
4505 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4507 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4508 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4509 return WINED3DERR_INVALIDCALL;
4512 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4514 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4516 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4517 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4520 --sub_resource->map_count;
4521 if (!--texture->resource.map_count && texture->update_map_binding)
4522 wined3d_texture_update_map_binding(texture);
4523 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4524 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4526 return WINED3D_OK;
4529 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4530 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4531 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4533 unsigned int src_row_pitch, src_slice_pitch;
4534 unsigned int update_w, update_h, update_d;
4535 unsigned int src_level, dst_level;
4536 struct wined3d_context *context;
4537 struct wined3d_bo_address data;
4539 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4540 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4541 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4542 src_texture, src_sub_resource_idx, debug_box(src_box));
4544 context = context_acquire(dst_texture->resource.device, NULL, 0);
4546 /* Only load the sub-resource for partial updates. For newly allocated
4547 * textures the texture wouldn't be the current location, and we'd upload
4548 * zeroes just to overwrite them again. */
4549 update_w = src_box->right - src_box->left;
4550 update_h = src_box->bottom - src_box->top;
4551 update_d = src_box->back - src_box->front;
4552 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4553 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4554 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4555 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4556 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4557 else
4558 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4560 src_level = src_sub_resource_idx % src_texture->level_count;
4561 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, context, &data);
4562 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4564 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4565 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4566 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4568 context_release(context);
4570 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4571 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4574 /* Partial downloads are not supported. */
4575 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4576 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4578 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4579 unsigned int dst_location = dst_texture->resource.map_binding;
4580 struct wined3d_context *context;
4581 struct wined3d_bo_address data;
4582 struct wined3d_box src_box;
4583 unsigned int src_location;
4585 context = context_acquire(src_texture->resource.device, NULL, 0);
4587 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4588 wined3d_texture_get_bo_address(dst_texture, dst_sub_resource_idx, &data, dst_location);
4590 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4591 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4592 else
4593 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4594 src_level = src_sub_resource_idx % src_texture->level_count;
4595 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4597 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4598 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4600 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4601 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4603 context_release(context);
4605 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4606 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4609 static void wined3d_texture_set_bo(struct wined3d_texture *texture,
4610 unsigned sub_resource_idx, struct wined3d_context *context, struct wined3d_bo *bo)
4612 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
4613 struct wined3d_bo *prev_bo = sub_resource->bo;
4615 TRACE("texture %p, sub_resource_idx %u, context %p, bo %p.\n", texture, sub_resource_idx, context, bo);
4617 if (prev_bo)
4619 struct wined3d_bo_user *bo_user;
4621 LIST_FOR_EACH_ENTRY(bo_user, &prev_bo->users, struct wined3d_bo_user, entry)
4622 bo_user->valid = false;
4623 list_init(&prev_bo->users);
4625 assert(list_empty(&bo->users));
4627 wined3d_context_destroy_bo(context, prev_bo);
4628 heap_free(prev_bo);
4631 sub_resource->bo = bo;
4634 void wined3d_texture_update_sub_resource(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4635 struct wined3d_context *context, const struct upload_bo *upload_bo, const struct wined3d_box *box,
4636 unsigned int row_pitch, unsigned int slice_pitch)
4638 unsigned int level = sub_resource_idx % texture->level_count;
4639 unsigned int width = wined3d_texture_get_level_width(texture, level);
4640 unsigned int height = wined3d_texture_get_level_height(texture, level);
4641 unsigned int depth = wined3d_texture_get_level_depth(texture, level);
4642 struct wined3d_box src_box;
4644 if (upload_bo->flags & UPLOAD_BO_RENAME_ON_UNMAP)
4646 wined3d_texture_set_bo(texture, sub_resource_idx, context, upload_bo->addr.buffer_object);
4647 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
4648 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_BUFFER);
4649 /* Try to free address space if we are not mapping persistently. */
4650 if (upload_bo->addr.buffer_object->map_ptr)
4651 wined3d_context_unmap_bo_address(context, (const struct wined3d_bo_address *)&upload_bo->addr, 0, NULL);
4654 /* Only load the sub-resource for partial updates. */
4655 if (!box->left && !box->top && !box->front
4656 && box->right == width && box->bottom == height && box->back == depth)
4657 wined3d_texture_prepare_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4658 else
4659 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4661 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
4662 texture->texture_ops->texture_upload_data(context, &upload_bo->addr, texture->resource.format,
4663 &src_box, row_pitch, slice_pitch, texture, sub_resource_idx,
4664 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
4666 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4667 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4670 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4671 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4672 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4673 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4674 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4676 FIXME("Not implemented.\n");
4679 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4680 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4681 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4682 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4683 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4685 FIXME("Not implemented.\n");
4688 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4689 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4691 if (location == WINED3D_LOCATION_SYSMEM)
4692 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4693 : wined3d_resource_prepare_sysmem(&texture->resource);
4695 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4696 return FALSE;
4699 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4700 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
4702 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4703 texture, sub_resource_idx, context, wined3d_debug_location(location));
4705 if (location == WINED3D_LOCATION_SYSMEM)
4706 return TRUE;
4708 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4710 return FALSE;
4713 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4714 struct wined3d_context *context, unsigned int location)
4716 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4719 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4721 wined3d_texture_no3d_prepare_location,
4722 wined3d_texture_no3d_load_location,
4723 wined3d_texture_no3d_unload_location,
4724 wined3d_texture_no3d_upload_data,
4725 wined3d_texture_no3d_download_data,
4728 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4729 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4730 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4732 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4733 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4734 texture_no3d, device, desc, layer_count,
4735 level_count, flags, parent, parent_ops);
4737 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4738 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4741 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4743 static const VkComponentSwizzle swizzle_source[] =
4745 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4746 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4747 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4748 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4749 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4750 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4753 mapping->r = swizzle_source[fixup.x_source];
4754 mapping->g = swizzle_source[fixup.y_source];
4755 mapping->b = swizzle_source[fixup.z_source];
4756 mapping->a = swizzle_source[fixup.w_source];
4759 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4760 struct wined3d_context_vk *context_vk)
4762 const struct wined3d_format_vk *format_vk;
4763 const struct wined3d_vk_info *vk_info;
4764 struct wined3d_device_vk *device_vk;
4765 VkImageViewCreateInfo create_info;
4766 struct color_fixup_desc fixup;
4767 uint32_t flags = 0;
4768 VkResult vr;
4770 if (texture_vk->default_image_info.imageView)
4771 return &texture_vk->default_image_info;
4773 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4774 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4775 vk_info = context_vk->vk_info;
4777 if (texture_vk->t.layer_count > 1)
4778 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4780 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4781 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4782 create_info.pNext = NULL;
4783 create_info.flags = 0;
4784 create_info.image = texture_vk->image.vk_image;
4785 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4786 create_info.format = format_vk->vk_format;
4787 fixup = format_vk->f.color_fixup;
4788 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4790 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4791 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4792 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4793 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4795 else
4797 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4799 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4800 create_info.subresourceRange.baseMipLevel = 0;
4801 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4802 create_info.subresourceRange.baseArrayLayer = 0;
4803 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4804 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4805 NULL, &texture_vk->default_image_info.imageView))) < 0)
4807 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4808 return NULL;
4811 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4813 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4815 /* The default image view is used for SRVs, UAVs and RTVs when the d3d view encompasses the entire
4816 * resource. Any UAV capable resource will always use VK_IMAGE_LAYOUT_GENERAL, so we can use the
4817 * same image info for SRVs and UAVs. For render targets wined3d_rendertarget_view_vk_get_image_view
4818 * only cares about the VkImageView, not entire image info. So using SHADER_READ_ONLY_OPTIMAL works,
4819 * but relies on what the callers of the function do and don't do with the descriptor we return.
4821 * Note that VkWriteDescriptorSet for SRV/UAV use takes a VkDescriptorImageInfo *, so we need a
4822 * place to store the VkDescriptorImageInfo. So returning onlky a VkImageView from this function
4823 * would bring its own problems. */
4824 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
4825 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
4826 else
4827 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
4829 return &texture_vk->default_image_info;
4832 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4833 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4834 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4835 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4836 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4838 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4839 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4840 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4841 struct wined3d_texture_sub_resource *sub_resource;
4842 unsigned int src_width, src_height, src_depth;
4843 struct wined3d_bo_address staging_bo_addr;
4844 VkPipelineStageFlags bo_stage_flags = 0;
4845 const struct wined3d_vk_info *vk_info;
4846 VkCommandBuffer vk_command_buffer;
4847 VkBufferMemoryBarrier vk_barrier;
4848 VkImageSubresourceRange vk_range;
4849 struct wined3d_bo_vk staging_bo;
4850 VkImageAspectFlags aspect_mask;
4851 struct wined3d_bo_vk *src_bo;
4852 struct wined3d_range range;
4853 VkBufferImageCopy region;
4854 size_t src_offset;
4855 void *map_ptr;
4857 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4858 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4859 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4860 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4861 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4863 if (src_format->id != dst_texture->resource.format->id)
4865 FIXME("Unhandled format conversion (%s -> %s).\n",
4866 debug_d3dformat(src_format->id),
4867 debug_d3dformat(dst_texture->resource.format->id));
4868 return;
4871 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4872 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4873 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4874 src_row_pitch = dst_row_pitch = 0;
4875 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4876 src_slice_pitch = dst_slice_pitch = 0;
4878 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4880 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4881 return;
4884 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4886 FIXME("Not supported for multisample textures.\n");
4887 return;
4890 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4891 if (wined3d_popcount(aspect_mask) > 1)
4893 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4894 return;
4897 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4898 vk_info = context_vk->vk_info;
4900 src_width = src_box->right - src_box->left;
4901 src_height = src_box->bottom - src_box->top;
4902 src_depth = src_box->back - src_box->front;
4904 src_offset = src_box->front * src_slice_pitch
4905 + (src_box->top / src_format->block_height) * src_row_pitch
4906 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4908 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4910 ERR("Failed to get command buffer.\n");
4911 return;
4914 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
4915 wined3d_context_vk_end_current_render_pass(context_vk);
4916 if (!src_bo_addr->buffer_object)
4918 unsigned int staging_row_pitch, staging_slice_pitch, staging_size;
4920 wined3d_format_calculate_pitch(src_format, context->device->surface_alignment, src_width, src_height,
4921 &staging_row_pitch, &staging_slice_pitch);
4922 staging_size = staging_slice_pitch * src_depth;
4924 if (!wined3d_context_vk_create_bo(context_vk, staging_size,
4925 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4927 ERR("Failed to create staging bo.\n");
4928 return;
4931 staging_bo_addr.buffer_object = &staging_bo.b;
4932 staging_bo_addr.addr = NULL;
4933 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4934 staging_size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4936 ERR("Failed to map staging bo.\n");
4937 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4938 return;
4941 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
4942 map_ptr, staging_row_pitch, staging_slice_pitch, src_width, src_height, src_depth);
4944 range.offset = 0;
4945 range.size = staging_size;
4946 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4948 src_bo = &staging_bo;
4950 src_offset = 0;
4951 src_row_pitch = staging_row_pitch;
4952 src_slice_pitch = staging_slice_pitch;
4954 else
4956 src_bo = wined3d_bo_vk(src_bo_addr->buffer_object);
4958 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
4959 vk_barrier.pNext = NULL;
4960 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(src_bo->usage) & ~WINED3D_READ_ONLY_ACCESS_FLAGS;
4961 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
4962 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4963 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4964 vk_barrier.buffer = src_bo->vk_buffer;
4965 vk_barrier.offset = src_bo->b.buffer_offset + (size_t)src_bo_addr->addr;
4966 vk_barrier.size = sub_resource->size;
4968 src_offset += (size_t)src_bo_addr->addr;
4970 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(src_bo->usage);
4971 if (vk_barrier.srcAccessMask)
4972 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
4973 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
4976 vk_range.aspectMask = aspect_mask;
4977 vk_range.baseMipLevel = dst_level;
4978 vk_range.levelCount = 1;
4979 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4980 vk_range.layerCount = 1;
4982 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4983 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4984 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4985 VK_ACCESS_TRANSFER_WRITE_BIT,
4986 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4987 dst_texture_vk->image.vk_image, &vk_range);
4989 region.bufferOffset = src_bo->b.buffer_offset + src_offset;
4990 region.bufferRowLength = (src_row_pitch / src_format->block_byte_count) * src_format->block_width;
4991 if (src_row_pitch)
4992 region.bufferImageHeight = (src_slice_pitch / src_row_pitch) * src_format->block_height;
4993 else
4994 region.bufferImageHeight = 1;
4995 region.imageSubresource.aspectMask = vk_range.aspectMask;
4996 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
4997 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
4998 region.imageSubresource.layerCount = vk_range.layerCount;
4999 region.imageOffset.x = dst_x;
5000 region.imageOffset.y = dst_y;
5001 region.imageOffset.z = dst_z;
5002 region.imageExtent.width = src_width;
5003 region.imageExtent.height = src_height;
5004 region.imageExtent.depth = src_depth;
5006 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, src_bo->vk_buffer,
5007 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
5009 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5010 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5011 VK_ACCESS_TRANSFER_WRITE_BIT,
5012 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5013 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
5014 dst_texture_vk->image.vk_image, &vk_range);
5015 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
5016 wined3d_context_vk_reference_bo(context_vk, src_bo);
5018 if (src_bo == &staging_bo)
5020 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5022 else if (vk_barrier.srcAccessMask)
5024 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5025 bo_stage_flags, 0, 0, NULL, 0, NULL, 0, NULL));
5029 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
5030 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
5031 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
5032 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
5033 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
5035 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
5036 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5037 unsigned int src_level, src_width, src_height, src_depth;
5038 struct wined3d_texture_sub_resource *sub_resource;
5039 unsigned int src_row_pitch, src_slice_pitch;
5040 struct wined3d_bo_address staging_bo_addr;
5041 VkPipelineStageFlags bo_stage_flags = 0;
5042 const struct wined3d_vk_info *vk_info;
5043 VkCommandBuffer vk_command_buffer;
5044 VkImageSubresourceRange vk_range;
5045 VkBufferMemoryBarrier vk_barrier;
5046 struct wined3d_bo_vk staging_bo;
5047 VkImageAspectFlags aspect_mask;
5048 struct wined3d_bo_vk *dst_bo;
5049 VkBufferImageCopy region;
5050 size_t dst_offset = 0;
5051 void *map_ptr;
5053 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
5054 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
5055 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5056 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
5057 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
5059 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
5061 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
5062 return;
5065 src_level = src_sub_resource_idx % src_texture->level_count;
5066 src_width = wined3d_texture_get_level_width(src_texture, src_level);
5067 src_height = wined3d_texture_get_level_height(src_texture, src_level);
5068 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
5069 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
5070 || src_box->front || src_box->back != src_depth)
5072 FIXME("Unhandled source box %s.\n", debug_box(src_box));
5073 return;
5076 if (dst_format->id != src_texture->resource.format->id)
5078 FIXME("Unhandled format conversion (%s -> %s).\n",
5079 debug_d3dformat(src_texture->resource.format->id),
5080 debug_d3dformat(dst_format->id));
5081 return;
5084 if (dst_x || dst_y || dst_z)
5086 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
5087 return;
5090 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
5092 FIXME("Not supported for multisample textures.\n");
5093 return;
5096 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
5097 if (wined3d_popcount(aspect_mask) > 1)
5099 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
5100 return;
5103 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
5104 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
5105 src_row_pitch = dst_row_pitch = 0;
5106 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
5107 src_slice_pitch = dst_slice_pitch = 0;
5109 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
5110 vk_info = context_vk->vk_info;
5111 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5113 ERR("Failed to get command buffer.\n");
5114 return;
5117 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyImageToBuffer() calls below. */
5118 wined3d_context_vk_end_current_render_pass(context_vk);
5120 if (!dst_bo_addr->buffer_object)
5122 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5123 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5125 ERR("Failed to create staging bo.\n");
5126 return;
5129 dst_bo = &staging_bo;
5131 else
5133 dst_bo = wined3d_bo_vk(dst_bo_addr->buffer_object);
5135 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5136 vk_barrier.pNext = NULL;
5137 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(dst_bo->usage);
5138 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5139 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5140 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5141 vk_barrier.buffer = dst_bo->vk_buffer;
5142 vk_barrier.offset = dst_bo->b.buffer_offset + (size_t)dst_bo_addr->addr;
5143 vk_barrier.size = sub_resource->size;
5145 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(dst_bo->usage);
5146 dst_offset = (size_t)dst_bo_addr->addr;
5148 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5149 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5152 vk_range.aspectMask = aspect_mask;
5153 vk_range.baseMipLevel = src_level;
5154 vk_range.levelCount = 1;
5155 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
5156 vk_range.layerCount = 1;
5158 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5159 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5160 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5161 VK_ACCESS_TRANSFER_READ_BIT,
5162 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5163 src_texture_vk->image.vk_image, &vk_range);
5165 region.bufferOffset = dst_bo->b.buffer_offset + dst_offset;
5166 region.bufferRowLength = 0;
5167 region.bufferImageHeight = 0;
5168 region.imageSubresource.aspectMask = vk_range.aspectMask;
5169 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5170 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5171 region.imageSubresource.layerCount = vk_range.layerCount;
5172 region.imageOffset.x = 0;
5173 region.imageOffset.y = 0;
5174 region.imageOffset.z = 0;
5175 region.imageExtent.width = src_width;
5176 region.imageExtent.height = src_height;
5177 region.imageExtent.depth = src_depth;
5179 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
5180 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_bo->vk_buffer, 1, &region));
5182 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5183 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5184 VK_ACCESS_TRANSFER_READ_BIT,
5185 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5186 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
5187 src_texture_vk->image.vk_image, &vk_range);
5189 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
5190 wined3d_context_vk_reference_bo(context_vk, dst_bo);
5192 if (dst_bo == &staging_bo)
5194 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5195 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
5197 staging_bo_addr.buffer_object = &staging_bo.b;
5198 staging_bo_addr.addr = NULL;
5199 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5200 sub_resource->size, WINED3D_MAP_READ)))
5202 ERR("Failed to map staging bo.\n");
5203 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5204 return;
5207 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
5208 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
5209 src_box->bottom - src_box->top, src_box->back - src_box->front);
5211 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
5212 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5214 else
5216 vk_barrier.dstAccessMask = vk_barrier.srcAccessMask;
5217 vk_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5219 if (dst_bo->host_synced)
5221 vk_barrier.dstAccessMask |= VK_ACCESS_HOST_READ_BIT;
5222 bo_stage_flags |= VK_PIPELINE_STAGE_HOST_BIT;
5225 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5226 bo_stage_flags, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5227 /* Start the download so we don't stall waiting for the result. */
5228 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5232 static bool wined3d_texture_vk_clear(struct wined3d_texture_vk *texture_vk,
5233 unsigned int sub_resource_idx, struct wined3d_context *context)
5235 struct wined3d_texture_sub_resource *sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5236 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5237 const struct wined3d_format *format = texture_vk->t.resource.format;
5238 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
5239 VkClearDepthStencilValue depth_value;
5240 VkCommandBuffer vk_command_buffer;
5241 VkImageSubresourceRange vk_range;
5242 VkClearColorValue colour_value;
5243 VkImageAspectFlags aspect_mask;
5244 VkImage vk_image;
5246 if (texture_vk->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5248 struct wined3d_bo_address addr;
5249 struct wined3d_color *c = &sub_resource->clear_value.colour;
5251 if (c->r || c->g || c-> b || c->a)
5252 FIXME("Compressed resource %p is cleared to a non-zero color.\n", &texture_vk->t);
5254 if (!wined3d_texture_prepare_location(&texture_vk->t, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM))
5255 return false;
5256 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
5257 memset(addr.addr, 0, sub_resource->size);
5258 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
5259 return true;
5262 vk_image = texture_vk->image.vk_image;
5264 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5266 ERR("Failed to get command buffer.\n");
5267 return false;
5270 aspect_mask = vk_aspect_mask_from_format(format);
5272 vk_range.aspectMask = aspect_mask;
5273 vk_range.baseMipLevel = sub_resource_idx % texture_vk->t.level_count;
5274 vk_range.levelCount = 1;
5275 vk_range.baseArrayLayer = sub_resource_idx / texture_vk->t.level_count;
5276 vk_range.layerCount = 1;
5278 wined3d_context_vk_end_current_render_pass(context_vk);
5280 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5281 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5282 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags), VK_ACCESS_TRANSFER_WRITE_BIT,
5283 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_image, &vk_range);
5285 if (format->depth_size || format->stencil_size)
5287 depth_value.depth = sub_resource->clear_value.depth;
5288 depth_value.stencil = sub_resource->clear_value.stencil;
5289 VK_CALL(vkCmdClearDepthStencilImage(vk_command_buffer, vk_image,
5290 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depth_value, 1, &vk_range));
5292 else
5294 wined3d_format_colour_to_vk(format, &sub_resource->clear_value.colour, &colour_value);
5295 VK_CALL(vkCmdClearColorImage(vk_command_buffer, vk_image,
5296 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour_value, 1, &vk_range));
5299 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5300 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5301 VK_ACCESS_TRANSFER_WRITE_BIT, vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
5302 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texture_vk->layout, vk_image, &vk_range);
5303 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5305 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
5306 return true;
5309 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
5310 unsigned int sub_resource_idx, struct wined3d_context *context)
5312 struct wined3d_texture_sub_resource *sub_resource;
5313 unsigned int level, row_pitch, slice_pitch;
5314 struct wined3d_bo_address data;
5315 struct wined3d_box src_box;
5317 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5319 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
5321 if (!wined3d_texture_vk_clear(texture_vk, sub_resource_idx, context))
5322 return FALSE;
5324 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
5325 return TRUE;
5328 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
5330 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5331 return FALSE;
5334 level = sub_resource_idx % texture_vk->t.level_count;
5335 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, context, &data);
5336 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5337 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5338 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
5339 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
5340 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
5342 return TRUE;
5345 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
5346 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5348 struct wined3d_texture_sub_resource *sub_resource;
5349 unsigned int level, row_pitch, slice_pitch;
5350 struct wined3d_bo_address data;
5351 struct wined3d_box src_box;
5353 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5354 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
5356 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5357 return FALSE;
5360 level = sub_resource_idx % texture_vk->t.level_count;
5361 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &data, location);
5362 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5363 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5364 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
5365 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
5367 return TRUE;
5370 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
5371 struct wined3d_context_vk *context_vk)
5373 const struct wined3d_format_vk *format_vk;
5374 struct wined3d_resource *resource;
5375 VkCommandBuffer vk_command_buffer;
5376 VkImageSubresourceRange vk_range;
5377 VkImageUsageFlags vk_usage;
5378 VkImageType vk_image_type;
5379 unsigned int flags = 0;
5381 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
5382 return TRUE;
5384 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5386 ERR("Failed to get command buffer.\n");
5387 return FALSE;
5390 resource = &texture_vk->t.resource;
5391 format_vk = wined3d_format_vk(resource->format);
5393 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain
5394 || (texture_vk->t.resource.bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
5396 /* For UAVs, we need this in case a clear necessitates creation of a new view
5397 * with a different format. */
5398 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
5401 switch (resource->type)
5403 case WINED3D_RTYPE_TEXTURE_1D:
5404 vk_image_type = VK_IMAGE_TYPE_1D;
5405 break;
5406 case WINED3D_RTYPE_TEXTURE_2D:
5407 vk_image_type = VK_IMAGE_TYPE_2D;
5408 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
5409 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
5410 break;
5411 case WINED3D_RTYPE_TEXTURE_3D:
5412 vk_image_type = VK_IMAGE_TYPE_3D;
5413 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
5414 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
5415 break;
5416 default:
5417 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
5418 vk_image_type = VK_IMAGE_TYPE_2D;
5419 break;
5422 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5423 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5424 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5425 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5426 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5427 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5428 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5429 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5430 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5432 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5433 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5434 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5435 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5436 else if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5437 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5438 else if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5439 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5440 else
5442 FIXME("unexpected bind flags %s, using VK_IMAGE_LAYOUT_GENERAL\n", wined3d_debug_bind_flags(resource->bind_flags));
5443 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5446 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5447 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5448 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5450 return FALSE;
5453 /* We can't use a zero src access mask without synchronization2. Set the last-used bind mask to something
5454 * non-zero to avoid this. */
5455 texture_vk->bind_mask = resource->bind_flags;
5457 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5458 vk_range.baseMipLevel = 0;
5459 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5460 vk_range.baseArrayLayer = 0;
5461 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5463 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5464 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5465 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5466 0, 0,
5467 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5468 texture_vk->image.vk_image, &vk_range);
5470 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5472 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5473 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5475 return TRUE;
5478 static BOOL wined3d_texture_vk_prepare_buffer_object(struct wined3d_texture_vk *texture_vk,
5479 unsigned int sub_resource_idx, struct wined3d_context_vk *context_vk)
5481 struct wined3d_texture_sub_resource *sub_resource;
5482 struct wined3d_bo_vk *bo;
5484 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5485 if (sub_resource->bo)
5486 return TRUE;
5488 if (!(bo = heap_alloc(sizeof(*bo))))
5489 return FALSE;
5491 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5492 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
5493 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bo))
5495 heap_free(bo);
5496 return FALSE;
5499 /* Texture buffer objects receive a barrier to HOST_READ in wined3d_texture_vk_download_data(),
5500 * so they don't need it when they are mapped for reading. */
5501 bo->host_synced = true;
5502 sub_resource->bo = &bo->b;
5503 TRACE("Created buffer object %p for texture %p, sub-resource %u.\n", bo, texture_vk, sub_resource_idx);
5504 return TRUE;
5507 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5508 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5510 switch (location)
5512 case WINED3D_LOCATION_SYSMEM:
5513 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5514 : wined3d_resource_prepare_sysmem(&texture->resource);
5516 case WINED3D_LOCATION_TEXTURE_RGB:
5517 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5519 case WINED3D_LOCATION_BUFFER:
5520 return wined3d_texture_vk_prepare_buffer_object(wined3d_texture_vk(texture), sub_resource_idx,
5521 wined3d_context_vk(context));
5523 default:
5524 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5525 return FALSE;
5529 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5530 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
5532 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5533 return FALSE;
5535 switch (location)
5537 case WINED3D_LOCATION_TEXTURE_RGB:
5538 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5540 case WINED3D_LOCATION_SYSMEM:
5541 case WINED3D_LOCATION_BUFFER:
5542 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context, location);
5544 default:
5545 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5546 return FALSE;
5550 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5551 struct wined3d_context *context, unsigned int location)
5553 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5554 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5555 unsigned int i, sub_count;
5557 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5559 switch (location)
5561 case WINED3D_LOCATION_TEXTURE_RGB:
5562 if (texture_vk->default_image_info.imageView)
5564 wined3d_context_vk_destroy_vk_image_view(context_vk,
5565 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5566 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5569 if (texture_vk->image.vk_image)
5570 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5571 break;
5573 case WINED3D_LOCATION_BUFFER:
5574 sub_count = texture->level_count * texture->layer_count;
5575 for (i = 0; i < sub_count; ++i)
5577 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
5579 if (sub_resource->bo)
5581 struct wined3d_bo_vk *bo_vk = wined3d_bo_vk(sub_resource->bo);
5583 wined3d_context_vk_destroy_bo(context_vk, bo_vk);
5584 heap_free(bo_vk);
5585 sub_resource->bo = NULL;
5588 break;
5590 case WINED3D_LOCATION_TEXTURE_SRGB:
5591 case WINED3D_LOCATION_RB_MULTISAMPLE:
5592 case WINED3D_LOCATION_RB_RESOLVED:
5593 break;
5595 default:
5596 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5597 break;
5601 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5603 wined3d_texture_vk_prepare_location,
5604 wined3d_texture_vk_load_location,
5605 wined3d_texture_vk_unload_location,
5606 wined3d_texture_vk_upload_data,
5607 wined3d_texture_vk_download_data,
5610 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5611 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5612 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5614 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5615 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5616 texture_vk, device, desc, layer_count,
5617 level_count, flags, parent, parent_ops);
5619 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5620 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5623 enum VkImageLayout wined3d_layout_from_bind_mask(const struct wined3d_texture_vk *texture_vk, const uint32_t bind_mask)
5625 assert(wined3d_popcount(bind_mask) == 1);
5627 /* We want to avoid switching between LAYOUT_GENERAL and other layouts. In Radeon GPUs (and presumably
5628 * others), this will trigger decompressing and recompressing the texture. We also hardcode the layout
5629 * into views when they are created. */
5630 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
5631 return VK_IMAGE_LAYOUT_GENERAL;
5633 switch (bind_mask)
5635 case WINED3D_BIND_RENDER_TARGET:
5636 return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5638 case WINED3D_BIND_DEPTH_STENCIL:
5639 return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5641 case WINED3D_BIND_SHADER_RESOURCE:
5642 return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5644 default:
5645 ERR("Unexpected bind mask %s.\n", wined3d_debug_bind_flags(bind_mask));
5646 return VK_IMAGE_LAYOUT_GENERAL;
5650 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5651 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5653 enum VkImageLayout new_layout;
5654 uint32_t src_bind_mask = 0;
5656 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5657 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5659 new_layout = wined3d_layout_from_bind_mask(texture_vk, bind_mask);
5661 /* A layout transition is potentially a read-write operation, so even if we
5662 * prepare the texture to e.g. read only shader resource mode, we have to wait
5663 * for past operations to finish. */
5664 if (bind_mask & ~WINED3D_READ_ONLY_BIND_MASK || new_layout != texture_vk->layout)
5666 src_bind_mask = texture_vk->bind_mask & WINED3D_READ_ONLY_BIND_MASK;
5667 if (!src_bind_mask)
5668 src_bind_mask = texture_vk->bind_mask;
5670 texture_vk->bind_mask = bind_mask;
5672 else if ((texture_vk->bind_mask & bind_mask) != bind_mask)
5674 src_bind_mask = texture_vk->bind_mask & ~WINED3D_READ_ONLY_BIND_MASK;
5675 texture_vk->bind_mask |= bind_mask;
5678 if (src_bind_mask)
5680 VkImageSubresourceRange vk_range;
5682 TRACE(" %s(%x) -> %s(%x).\n",
5683 wined3d_debug_bind_flags(src_bind_mask), texture_vk->layout,
5684 wined3d_debug_bind_flags(bind_mask), new_layout);
5686 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5687 vk_range.baseMipLevel = 0;
5688 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5689 vk_range.baseArrayLayer = 0;
5690 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5692 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5693 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask),
5694 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5695 vk_access_mask_from_bind_flags(src_bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5696 texture_vk->layout, new_layout, texture_vk->image.vk_image, &vk_range);
5698 texture_vk->layout = new_layout;
5702 /* This is called when a texture is used as render target and shader resource
5703 * or depth stencil and shader resource at the same time. This can either be
5704 * read-only simultaneos use as depth stencil, but also for rendering to one
5705 * subresource while reading from another. Without tracking of barriers and
5706 * layouts per subresource VK_IMAGE_LAYOUT_GENERAL is the only thing we can do. */
5707 void wined3d_texture_vk_make_generic(struct wined3d_texture_vk *texture_vk,
5708 struct wined3d_context_vk *context_vk)
5710 VkImageSubresourceRange vk_range;
5712 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
5713 return;
5715 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5716 vk_range.baseMipLevel = 0;
5717 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5718 vk_range.baseArrayLayer = 0;
5719 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5721 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5722 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5723 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5724 0, 0,
5725 texture_vk->layout, VK_IMAGE_LAYOUT_GENERAL, texture_vk->image.vk_image, &vk_range);
5727 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5728 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
5731 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5733 struct wined3d_blitter *next;
5735 if ((next = blitter->next))
5736 next->ops->blitter_destroy(next, context);
5738 heap_free(blitter);
5741 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5742 const struct wined3d_resource *src_resource, DWORD src_location,
5743 const struct wined3d_resource *dst_resource, DWORD dst_location)
5745 const struct wined3d_format *src_format = src_resource->format;
5746 const struct wined3d_format *dst_format = dst_resource->format;
5747 bool decompress;
5749 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5750 return false;
5752 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5753 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
5754 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5756 TRACE("Source or destination resource is not GPU accessible.\n");
5757 return false;
5760 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5762 if (dst_format->depth_size || dst_format->stencil_size)
5763 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5764 else
5765 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5768 switch (blit_op)
5770 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5771 if (context->d3d_info->ffp_fragment_caps.color_key)
5773 TRACE("Colour keying requires converted textures.\n");
5774 return false;
5776 case WINED3D_BLIT_OP_COLOR_BLIT:
5777 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5778 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5779 return false;
5781 if (TRACE_ON(d3d))
5783 TRACE("Checking support for fixup:\n");
5784 dump_color_fixup_desc(src_format->color_fixup);
5787 /* We only support identity conversions. */
5788 if (!is_identity_fixup(src_format->color_fixup)
5789 || !is_identity_fixup(dst_format->color_fixup))
5791 if (dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5793 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
5794 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5795 else if (context->device->shader_backend == &none_shader_backend)
5796 WARN("Claiming fixup support because of no shader backend.\n");
5797 return true;
5799 else
5801 TRACE("Fixups are not supported.\n");
5802 return false;
5806 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5808 if (dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5810 if (context->device->shader_backend == &none_shader_backend)
5811 WARN("Claiming !render_target support because of no shader backend.\n");
5812 return true;
5814 else
5816 TRACE("Can only blit to render targets.\n");
5817 return false;
5820 return true;
5822 default:
5823 TRACE("Unsupported blit operation %#x.\n", blit_op);
5824 return false;
5828 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5830 unsigned int height = rtv->height;
5831 unsigned int width = rtv->width;
5833 /* partial draw rect */
5834 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5835 return false;
5837 /* partial clear rect */
5838 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5839 || clear_rect->right < width || clear_rect->bottom < height))
5840 return false;
5842 return true;
5845 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5846 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5847 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5849 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5850 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5851 const struct wined3d_state *state = &device->cs->state;
5852 struct wined3d_texture *depth_stencil = NULL;
5853 unsigned int drawable_width, drawable_height;
5854 const struct wined3d_gl_info *gl_info;
5855 struct wined3d_context_gl *context_gl;
5856 struct wined3d_texture *target = NULL;
5857 struct wined3d_color colour_srgb;
5858 struct wined3d_context *context;
5859 GLbitfield clear_mask = 0;
5860 bool render_offscreen;
5861 unsigned int i;
5863 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5865 target = texture_from_resource(rtv->resource);
5866 context = context_acquire(device, target, rtv->sub_resource_idx);
5868 else
5870 context = context_acquire(device, NULL, 0);
5872 context_gl = wined3d_context_gl(context);
5874 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5875 depth_stencil = texture_from_resource(dsv->resource);
5877 if (!context_gl->valid)
5879 context_release(context);
5880 WARN("Invalid context, skipping clear.\n");
5881 return;
5883 gl_info = context_gl->gl_info;
5885 /* When we're clearing parts of the drawable, make sure that the target
5886 * surface is well up to date in the drawable. After the clear we'll mark
5887 * the drawable up to date, so we have to make sure that this is true for
5888 * the cleared parts, and the untouched parts.
5890 * If we're clearing the whole target there is no need to copy it into the
5891 * drawable, it will be overwritten anyway. If we're not clearing the
5892 * colour buffer we don't have to copy either since we're not going to set
5893 * the drawable up to date. We have to check all settings that limit the
5894 * clear area though. Do not bother checking all this if the destination
5895 * surface is in the drawable anyway. */
5896 for (i = 0; i < rt_count; ++i)
5898 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5900 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5902 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5903 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5904 else
5905 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5909 if (target)
5911 render_offscreen = context->render_offscreen;
5912 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5914 else
5916 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5918 render_offscreen = true;
5919 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5920 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5923 if (depth_stencil)
5925 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5927 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5928 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5929 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5930 else
5931 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5933 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5935 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5936 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5940 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5942 context_release(context);
5943 WARN("Failed to apply clear state, skipping clear.\n");
5944 return;
5947 /* Only set the values up once, as they are not changing. */
5948 if (flags & WINED3DCLEAR_STENCIL)
5950 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5951 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5952 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5953 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5954 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5955 checkGLcall("glClearStencil");
5956 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5959 if (flags & WINED3DCLEAR_ZBUFFER)
5961 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5962 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5963 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5964 GL_EXTCALL(glClearDepthf(depth));
5965 else
5966 gl_info->gl_ops.gl.p_glClearDepth(depth);
5967 checkGLcall("glClearDepth");
5968 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5971 if (flags & WINED3DCLEAR_TARGET)
5973 for (i = 0; i < rt_count; ++i)
5975 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5977 if (!rtv)
5978 continue;
5980 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5982 FIXME("Not supported on buffer resources.\n");
5983 continue;
5986 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
5987 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
5990 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5992 if (rt_count > 1)
5993 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5994 "support, this might cause graphical issues.\n");
5996 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5997 colour = &colour_srgb;
6000 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6001 context_invalidate_state(context, STATE_BLEND);
6002 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
6003 checkGLcall("glClearColor");
6004 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
6007 if (!rect_count)
6009 if (render_offscreen)
6011 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
6012 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6014 else
6016 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
6017 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6019 gl_info->gl_ops.gl.p_glClear(clear_mask);
6021 else
6023 RECT current_rect;
6025 /* Now process each rect in turn. */
6026 for (i = 0; i < rect_count; ++i)
6028 /* Note that GL uses lower left, width/height. */
6029 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
6031 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
6032 wine_dbgstr_rect(&clear_rect[i]),
6033 wine_dbgstr_rect(&current_rect));
6035 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
6036 * silently. The rectangle is not cleared, no error is returned,
6037 * but further rectangles are still cleared if they are valid. */
6038 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
6040 TRACE("Rectangle with negative dimensions, ignoring.\n");
6041 continue;
6044 if (render_offscreen)
6046 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
6047 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6049 else
6051 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
6052 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6054 gl_info->gl_ops.gl.p_glClear(clear_mask);
6057 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
6058 checkGLcall("clear");
6060 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
6061 gl_info->gl_ops.gl.p_glFlush();
6063 context_release(context);
6066 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
6068 struct wined3d_resource *resource;
6069 struct wined3d_texture *texture;
6070 DWORD locations;
6072 resource = view->resource;
6073 if (resource->type == WINED3D_RTYPE_BUFFER)
6074 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
6076 texture = texture_from_resource(resource);
6077 locations = texture->sub_resources[view->sub_resource_idx].locations;
6078 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
6079 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6080 || texture->resource.pin_sysmem;
6082 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6083 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
6086 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6087 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6088 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6090 struct wined3d_rendertarget_view *view, *previous = NULL;
6091 bool have_identical_size = TRUE;
6092 struct wined3d_fb_state tmp_fb;
6093 unsigned int next_rt_count = 0;
6094 struct wined3d_blitter *next;
6095 DWORD next_flags = 0;
6096 unsigned int i;
6098 if (flags & WINED3DCLEAR_TARGET)
6100 for (i = 0; i < rt_count; ++i)
6102 if (!(view = fb->render_targets[i]))
6103 continue;
6105 if (blitter_use_cpu_clear(view)
6106 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
6107 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
6108 || !(view->format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))))
6110 next_flags |= WINED3DCLEAR_TARGET;
6111 flags &= ~WINED3DCLEAR_TARGET;
6112 next_rt_count = rt_count;
6113 rt_count = 0;
6114 break;
6117 /* FIXME: We should reject colour fills on formats with fixups,
6118 * but this would break P8 colour fills for example. */
6122 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6123 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6124 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6125 && blitter_use_cpu_clear(view))
6127 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6128 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6131 if (flags)
6133 for (i = 0; i < rt_count; ++i)
6135 if (!(view = fb->render_targets[i]))
6136 continue;
6138 if (previous && (previous->width != view->width || previous->height != view->height))
6139 have_identical_size = false;
6140 previous = view;
6142 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6144 view = fb->depth_stencil;
6146 if (previous && (previous->width != view->width || previous->height != view->height))
6147 have_identical_size = false;
6150 if (have_identical_size)
6152 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
6153 clear_rects, draw_rect, flags, colour, depth, stencil);
6155 else
6157 for (i = 0; i < rt_count; ++i)
6159 if (!(view = fb->render_targets[i]))
6160 continue;
6162 tmp_fb.render_targets[0] = view;
6163 tmp_fb.depth_stencil = NULL;
6164 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
6165 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6167 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6169 tmp_fb.render_targets[0] = NULL;
6170 tmp_fb.depth_stencil = fb->depth_stencil;
6171 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
6172 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6177 if (next_flags && (next = blitter->next))
6178 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6179 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6182 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6183 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6184 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6185 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6186 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6187 const struct wined3d_format *resolve_format)
6189 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6190 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6191 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6192 struct wined3d_resource *src_resource, *dst_resource;
6193 struct wined3d_texture *staging_texture = NULL;
6194 struct wined3d_color_key old_blt_key;
6195 struct wined3d_device *device;
6196 struct wined3d_blitter *next;
6197 DWORD old_colour_key_flags;
6198 RECT r;
6200 src_resource = &src_texture->resource;
6201 dst_resource = &dst_texture->resource;
6202 device = dst_resource->device;
6204 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
6206 if ((next = blitter->next))
6207 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6208 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6209 resolve_format);
6212 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
6213 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
6215 old_blt_key = src_texture->async.src_blt_color_key;
6216 old_colour_key_flags = src_texture->async.color_key_flags;
6217 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
6219 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
6221 struct wined3d_resource_desc desc;
6222 struct wined3d_box upload_box;
6223 unsigned int src_level;
6224 HRESULT hr;
6226 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
6228 src_level = src_sub_resource_idx % src_texture->level_count;
6229 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6230 desc.format = src_texture->resource.format->id;
6231 desc.multisample_type = src_texture->resource.multisample_type;
6232 desc.multisample_quality = src_texture->resource.multisample_quality;
6233 desc.usage = WINED3DUSAGE_PRIVATE;
6234 desc.bind_flags = 0;
6235 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6236 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
6237 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
6238 desc.depth = 1;
6239 desc.size = 0;
6241 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
6242 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
6244 ERR("Failed to create staging texture, hr %#lx.\n", hr);
6245 return dst_location;
6248 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
6249 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
6250 src_texture, src_sub_resource_idx, &upload_box);
6252 src_texture = staging_texture;
6253 src_texture_gl = wined3d_texture_gl(src_texture);
6254 src_sub_resource_idx = 0;
6256 else
6258 /* Make sure the surface is up-to-date. This should probably use
6259 * surface_load_location() and worry about the destination surface
6260 * too, unless we're overwriting it completely. */
6261 wined3d_texture_load(src_texture, context, FALSE);
6264 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
6265 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
6266 else
6267 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
6269 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
6270 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
6272 if (dst_location == WINED3D_LOCATION_DRAWABLE)
6274 r = *dst_rect;
6275 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
6276 dst_rect = &r;
6279 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
6280 checkGLcall("glEnable(target)");
6282 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6284 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
6285 checkGLcall("glEnable(GL_ALPHA_TEST)");
6288 if (colour_key)
6290 /* For P8 surfaces, the alpha component contains the palette index.
6291 * Which means that the colourkey is one of the palette entries. In
6292 * other cases pixels that should be masked away have alpha set to 0. */
6293 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
6294 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
6295 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
6296 else
6297 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
6298 checkGLcall("glAlphaFunc");
6301 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
6302 src_sub_resource_idx, src_rect, dst_rect, filter);
6304 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6306 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
6307 checkGLcall("glDisable(GL_ALPHA_TEST)");
6310 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6311 checkGLcall("glDisable(GL_TEXTURE_2D)");
6312 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6314 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6315 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6317 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6319 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6320 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6323 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
6324 gl_info->gl_ops.gl.p_glFlush();
6326 /* Restore the colour key parameters */
6327 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
6328 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
6330 if (staging_texture)
6331 wined3d_texture_decref(staging_texture);
6333 return dst_location;
6336 static const struct wined3d_blitter_ops ffp_blitter_ops =
6338 ffp_blitter_destroy,
6339 ffp_blitter_clear,
6340 ffp_blitter_blit,
6343 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6345 struct wined3d_blitter *blitter;
6347 if (!(blitter = heap_alloc(sizeof(*blitter))))
6348 return;
6350 TRACE("Created blitter %p.\n", blitter);
6352 blitter->ops = &ffp_blitter_ops;
6353 blitter->next = *next;
6354 *next = blitter;
6357 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6359 struct wined3d_blitter *next;
6361 if ((next = blitter->next))
6362 next->ops->blitter_destroy(next, context);
6364 heap_free(blitter);
6367 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6368 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6369 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6371 struct wined3d_blitter *next;
6373 if ((next = blitter->next))
6374 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6375 clear_rects, draw_rect, flags, colour, depth, stencil);
6378 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6379 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6380 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6381 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6382 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6383 const struct wined3d_format *resolve_format)
6385 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6386 struct wined3d_resource *src_resource, *dst_resource;
6387 enum wined3d_blit_op blit_op = op;
6388 struct wined3d_device *device;
6389 struct wined3d_blitter *next;
6391 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6392 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6393 "colour_key %p, filter %s, resolve_format %p.\n",
6394 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6395 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6396 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6398 src_resource = &src_texture->resource;
6399 dst_resource = &dst_texture->resource;
6401 device = dst_resource->device;
6403 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
6405 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
6406 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
6407 else
6408 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
6411 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
6412 src_resource, src_location, dst_resource, dst_location))
6414 if (!(next = blitter->next))
6416 ERR("No blitter to handle blit op %#x.\n", op);
6417 return dst_location;
6420 TRACE("Forwarding to blitter %p.\n", next);
6421 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6422 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6423 resolve_format);
6426 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
6428 TRACE("Colour blit.\n");
6429 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
6430 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
6431 return dst_location;
6434 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
6436 TRACE("Depth/stencil blit.\n");
6437 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
6438 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
6439 return dst_location;
6442 ERR("This blitter does not implement blit op %#x.\n", blit_op);
6443 return dst_location;
6446 static const struct wined3d_blitter_ops fbo_blitter_ops =
6448 fbo_blitter_destroy,
6449 fbo_blitter_clear,
6450 fbo_blitter_blit,
6453 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6455 struct wined3d_blitter *blitter;
6457 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
6458 return;
6460 if (!(blitter = heap_alloc(sizeof(*blitter))))
6461 return;
6463 TRACE("Created blitter %p.\n", blitter);
6465 blitter->ops = &fbo_blitter_ops;
6466 blitter->next = *next;
6467 *next = blitter;
6470 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6472 struct wined3d_blitter *next;
6474 if ((next = blitter->next))
6475 next->ops->blitter_destroy(next, context);
6477 heap_free(blitter);
6480 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6481 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6482 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6484 struct wined3d_blitter *next;
6486 if (!(next = blitter->next))
6488 ERR("No blitter to handle clear.\n");
6489 return;
6492 TRACE("Forwarding to blitter %p.\n", next);
6493 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6494 clear_rects, draw_rect, flags, colour, depth, stencil);
6497 static bool gl_formats_compatible(struct wined3d_texture *src_texture, DWORD src_location,
6498 struct wined3d_texture *dst_texture, DWORD dst_location)
6500 GLuint src_internal, dst_internal;
6501 bool src_ds, dst_ds;
6503 src_ds = src_texture->resource.format->depth_size || src_texture->resource.format->stencil_size;
6504 dst_ds = dst_texture->resource.format->depth_size || dst_texture->resource.format->stencil_size;
6505 if (src_ds == dst_ds)
6506 return true;
6507 /* Also check the internal format because, e.g. WINED3DFMT_D24_UNORM_S8_UINT has nonzero depth and stencil
6508 * sizes as does WINED3DFMT_R24G8_TYPELESS when bound with flag WINED3D_BIND_DEPTH_STENCIL, but these share
6509 * the same internal format with WINED3DFMT_R24_UNORM_X8_TYPELESS. */
6510 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
6511 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
6512 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
6513 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
6514 return src_internal == dst_internal;
6517 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6518 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6519 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6520 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6521 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6522 const struct wined3d_format *resolve_format)
6524 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6525 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6526 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6527 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6528 unsigned int src_level, src_layer, dst_level, dst_layer;
6529 struct wined3d_blitter *next;
6530 GLuint src_name, dst_name;
6531 DWORD location;
6533 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6534 * be better off using the FBO blitter directly, since we'd need to use it
6535 * to copy the resource contents to the texture anyway.
6537 * We also can't copy between depth/stencil and colour resources, since
6538 * the formats are considered incompatible in OpenGL. */
6539 if (op != WINED3D_BLIT_OP_RAW_BLIT || !gl_formats_compatible(src_texture, src_location, dst_texture, dst_location)
6540 || ((src_texture->resource.format_attrs | dst_texture->resource.format_attrs)
6541 & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
6542 || (src_texture->resource.format->id == dst_texture->resource.format->id
6543 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6544 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6546 if (!(next = blitter->next))
6548 ERR("No blitter to handle blit op %#x.\n", op);
6549 return dst_location;
6552 TRACE("Forwarding to blitter %p.\n", next);
6553 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6554 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6555 resolve_format);
6558 TRACE("Blit using ARB_copy_image.\n");
6560 src_level = src_sub_resource_idx % src_texture->level_count;
6561 src_layer = src_sub_resource_idx / src_texture->level_count;
6563 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6564 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6566 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6567 if (!location)
6568 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6569 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6570 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6571 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6572 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6573 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6575 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6576 if (!location)
6577 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6578 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6579 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6581 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6582 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6584 else
6586 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6587 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6589 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6590 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6592 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6593 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6594 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6595 src_rect->bottom - src_rect->top, 1));
6596 checkGLcall("copy image data");
6598 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6599 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6600 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6601 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6603 return dst_location | location;
6606 static const struct wined3d_blitter_ops raw_blitter_ops =
6608 raw_blitter_destroy,
6609 raw_blitter_clear,
6610 raw_blitter_blit,
6613 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6615 struct wined3d_blitter *blitter;
6617 if (!gl_info->supported[ARB_COPY_IMAGE])
6618 return;
6620 if (!(blitter = heap_alloc(sizeof(*blitter))))
6621 return;
6623 TRACE("Created blitter %p.\n", blitter);
6625 blitter->ops = &raw_blitter_ops;
6626 blitter->next = *next;
6627 *next = blitter;
6630 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6632 struct wined3d_blitter *next;
6634 TRACE("blitter %p, context %p.\n", blitter, context);
6636 if ((next = blitter->next))
6637 next->ops->blitter_destroy(next, context);
6639 heap_free(blitter);
6642 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6643 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6644 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6646 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6647 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6648 unsigned int i, attachment_count, delay_count = 0;
6649 struct wined3d_rendertarget_view_vk *rtv_vk;
6650 struct wined3d_rendertarget_view *view;
6651 const struct wined3d_vk_info *vk_info;
6652 struct wined3d_device_vk *device_vk;
6653 VkCommandBuffer vk_command_buffer;
6654 VkRenderPassBeginInfo begin_desc;
6655 VkFramebufferCreateInfo fb_desc;
6656 VkFramebuffer vk_framebuffer;
6657 VkRenderPass vk_render_pass;
6658 bool depth_stencil = false;
6659 unsigned int layer_count;
6660 VkClearColorValue *c;
6661 VkResult vr;
6662 RECT r;
6664 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6665 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6666 context_vk, rt_count, fb, rect_count, clear_rects,
6667 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6669 device_vk = wined3d_device_vk(context_vk->c.device);
6670 vk_info = context_vk->vk_info;
6672 if (!(flags & WINED3DCLEAR_TARGET))
6673 rt_count = 0;
6675 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6677 if (!(view = fb->render_targets[i]))
6678 continue;
6680 /* Don't delay typeless clears because the data written into the resource depends on the
6681 * view format. Except all-zero clears, those should result in zeros in either case.
6683 * We could store the clear format along with the clear value, but then we'd have to
6684 * create a matching RTV at draw time, which would need its own render pass, thus mooting
6685 * the point of the delayed clear. (Unless we are lucky enough that the application
6686 * draws with the same RTV as it clears.) */
6687 if (is_full_clear(view, draw_rect, clear_rects)
6688 && (!wined3d_format_is_typeless(view->resource->format) || (!colour->r && !colour->g
6689 && !colour->b && !colour->a)))
6691 struct wined3d_texture *texture = texture_from_resource(view->resource);
6692 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6693 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6694 texture->sub_resources[view->sub_resource_idx].clear_value.colour = *colour;
6695 delay_count++;
6696 continue;
6698 else
6700 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6702 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6703 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6705 rtv_vk = wined3d_rendertarget_view_vk(view);
6706 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6707 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6709 c = &clear_values[attachment_count].color;
6710 wined3d_format_colour_to_vk(view->format, colour, c);
6712 if (view->layer_count > layer_count)
6713 layer_count = view->layer_count;
6715 ++attachment_count;
6718 if (!attachment_count)
6719 flags &= ~WINED3DCLEAR_TARGET;
6721 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6723 DWORD full_flags = 0;
6725 /* Vulkan can clear only depth or stencil, but at the moment we can't put the depth and
6726 * stencil parts in separate locations. It isn't easy to do either, as such a half-cleared
6727 * texture would need to be handled not just as a DS target but also when used as a shader
6728 * resource or accessed on sysmem. */
6729 if (view->format->depth_size)
6730 full_flags = WINED3DCLEAR_ZBUFFER;
6731 if (view->format->stencil_size)
6732 full_flags |= WINED3DCLEAR_STENCIL;
6734 if (!is_full_clear(view, draw_rect, clear_rects) || (flags & full_flags) != full_flags
6735 || (wined3d_format_is_typeless(view->resource->format) && (depth || stencil)))
6737 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6738 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6739 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6741 rtv_vk = wined3d_rendertarget_view_vk(view);
6742 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6743 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6745 clear_values[attachment_count].depthStencil.depth = depth;
6746 clear_values[attachment_count].depthStencil.stencil = stencil;
6748 if (view->layer_count > layer_count)
6749 layer_count = view->layer_count;
6751 depth_stencil = true;
6752 ++attachment_count;
6754 else
6756 struct wined3d_texture *texture = texture_from_resource(view->resource);
6757 texture->sub_resources[view->sub_resource_idx].clear_value.depth = depth;
6758 texture->sub_resources[view->sub_resource_idx].clear_value.stencil = stencil;
6759 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6760 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6761 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6762 delay_count++;
6766 if (!attachment_count)
6768 TRACE("The clear has been delayed until draw time.\n");
6769 return;
6772 TRACE("Doing an immediate clear of %u attachments.\n", attachment_count);
6773 if (delay_count)
6774 TRACE_(d3d_perf)("Partial clear: %u immediate, %u delayed.\n", attachment_count, delay_count);
6776 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6777 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6779 ERR("Failed to get render pass.\n");
6780 return;
6783 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6785 ERR("Failed to get command buffer.\n");
6786 return;
6789 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6790 fb_desc.pNext = NULL;
6791 fb_desc.flags = 0;
6792 fb_desc.renderPass = vk_render_pass;
6793 fb_desc.attachmentCount = attachment_count;
6794 fb_desc.pAttachments = views;
6795 fb_desc.width = draw_rect->right - draw_rect->left;
6796 fb_desc.height = draw_rect->bottom - draw_rect->top;
6797 fb_desc.layers = layer_count;
6798 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6800 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6801 return;
6804 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6805 begin_desc.pNext = NULL;
6806 begin_desc.renderPass = vk_render_pass;
6807 begin_desc.framebuffer = vk_framebuffer;
6808 begin_desc.clearValueCount = attachment_count;
6809 begin_desc.pClearValues = clear_values;
6811 wined3d_context_vk_end_current_render_pass(context_vk);
6813 for (i = 0; i < rect_count; ++i)
6815 r.left = max(clear_rects[i].left, draw_rect->left);
6816 r.top = max(clear_rects[i].top, draw_rect->top);
6817 r.right = min(clear_rects[i].right, draw_rect->right);
6818 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6820 if (r.left >= r.right || r.top >= r.bottom)
6821 continue;
6823 begin_desc.renderArea.offset.x = r.left;
6824 begin_desc.renderArea.offset.y = r.top;
6825 begin_desc.renderArea.extent.width = r.right - r.left;
6826 begin_desc.renderArea.extent.height = r.bottom - r.top;
6827 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6828 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6831 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6833 for (i = 0; i < rt_count; ++i)
6835 if (!(view = fb->render_targets[i]))
6836 continue;
6838 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6841 if (depth_stencil)
6843 view = fb->depth_stencil;
6844 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6848 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6849 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6850 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6852 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6853 struct wined3d_rendertarget_view *view, *previous = NULL;
6854 struct wined3d_context_vk *context_vk;
6855 bool have_identical_size = true;
6856 struct wined3d_fb_state tmp_fb;
6857 unsigned int next_rt_count = 0;
6858 struct wined3d_blitter *next;
6859 uint32_t next_flags = 0;
6860 unsigned int i;
6862 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6863 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6864 blitter, device, rt_count, fb, rect_count, clear_rects,
6865 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6867 if (!rect_count)
6869 rect_count = 1;
6870 clear_rects = draw_rect;
6873 if (flags & WINED3DCLEAR_TARGET)
6875 for (i = 0; i < rt_count; ++i)
6877 if (!(view = fb->render_targets[i]))
6878 continue;
6880 if (blitter_use_cpu_clear(view))
6882 next_flags |= WINED3DCLEAR_TARGET;
6883 flags &= ~WINED3DCLEAR_TARGET;
6884 next_rt_count = rt_count;
6885 rt_count = 0;
6886 break;
6891 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6892 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6893 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6894 && blitter_use_cpu_clear(view))
6896 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6897 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6900 if (flags)
6902 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6904 for (i = 0; i < rt_count; ++i)
6906 if (!(view = fb->render_targets[i]))
6907 continue;
6909 if (previous && (previous->width != view->width || previous->height != view->height))
6910 have_identical_size = false;
6911 previous = view;
6913 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6915 view = fb->depth_stencil;
6917 if (previous && (previous->width != view->width || previous->height != view->height))
6918 have_identical_size = false;
6921 if (have_identical_size)
6923 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6924 clear_rects, draw_rect, flags, colour, depth, stencil);
6926 else
6928 for (i = 0; i < rt_count; ++i)
6930 if (!(view = fb->render_targets[i]))
6931 continue;
6933 tmp_fb.render_targets[0] = view;
6934 tmp_fb.depth_stencil = NULL;
6935 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6936 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6938 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6940 tmp_fb.render_targets[0] = NULL;
6941 tmp_fb.depth_stencil = fb->depth_stencil;
6942 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6943 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6947 context_release(&context_vk->c);
6950 if (!next_flags)
6951 return;
6953 if (!(next = blitter->next))
6955 ERR("No blitter to handle clear.\n");
6956 return;
6959 TRACE("Forwarding to blitter %p.\n", next);
6960 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6961 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6964 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6965 const struct wined3d_resource *src_resource, const RECT *src_rect,
6966 const struct wined3d_resource *dst_resource, const RECT *dst_rect, const struct wined3d_format *resolve_format)
6968 const struct wined3d_format *src_format = src_resource->format;
6969 const struct wined3d_format *dst_format = dst_resource->format;
6971 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6973 TRACE("Destination resource does not have GPU access.\n");
6974 return false;
6977 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6979 TRACE("Source resource does not have GPU access.\n");
6980 return false;
6983 if (dst_format->id != src_format->id)
6985 if (!is_identity_fixup(dst_format->color_fixup))
6987 TRACE("Destination fixups are not supported.\n");
6988 return false;
6991 if (!is_identity_fixup(src_format->color_fixup))
6993 TRACE("Source fixups are not supported.\n");
6994 return false;
6997 if (op != WINED3D_BLIT_OP_RAW_BLIT
6998 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format
6999 && ((!wined3d_format_is_typeless(src_format) && !wined3d_format_is_typeless(dst_format))
7000 || !resolve_format))
7002 TRACE("Format conversion not supported.\n");
7003 return false;
7007 if (wined3d_resource_get_sample_count(dst_resource) > 1)
7009 TRACE("Multi-sample destination resource not supported.\n");
7010 return false;
7013 if (op == WINED3D_BLIT_OP_RAW_BLIT)
7014 return true;
7016 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
7018 TRACE("Unsupported blit operation %#x.\n", op);
7019 return false;
7022 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
7023 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
7025 TRACE("Scaling not supported.\n");
7026 return false;
7029 return true;
7032 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
7033 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
7034 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
7035 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
7036 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
7037 const struct wined3d_format *resolve_format)
7039 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
7040 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
7041 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
7042 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
7043 VkImageSubresourceRange vk_src_range, vk_dst_range;
7044 VkImageLayout src_layout, dst_layout;
7045 VkCommandBuffer vk_command_buffer;
7046 struct wined3d_blitter *next;
7047 unsigned src_sample_count;
7048 bool resolve = false;
7050 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
7051 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
7052 "colour_key %p, filter %s, resolve format %p.\n",
7053 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
7054 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
7055 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
7057 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect,
7058 resolve_format))
7059 goto next;
7061 src_sample_count = wined3d_resource_get_sample_count(&src_texture_vk->t.resource);
7062 if (src_sample_count > 1)
7063 resolve = true;
7065 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
7066 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
7067 vk_src_range.levelCount = 1;
7068 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
7069 vk_src_range.layerCount = 1;
7071 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
7072 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
7073 vk_dst_range.levelCount = 1;
7074 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
7075 vk_dst_range.layerCount = 1;
7077 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7078 ERR("Failed to load the source sub-resource.\n");
7080 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
7082 if (!wined3d_texture_prepare_location(dst_texture,
7083 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7085 ERR("Failed to prepare the destination sub-resource.\n");
7086 goto next;
7089 else
7091 if (!wined3d_texture_load_location(dst_texture,
7092 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7094 ERR("Failed to load the destination sub-resource.\n");
7095 goto next;
7099 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
7101 ERR("Failed to get command buffer.\n");
7102 goto next;
7105 if (src_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7106 src_layout = VK_IMAGE_LAYOUT_GENERAL;
7107 else
7108 src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7110 if (dst_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7111 dst_layout = VK_IMAGE_LAYOUT_GENERAL;
7112 else
7113 dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7115 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7116 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7117 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7118 VK_ACCESS_TRANSFER_READ_BIT, src_texture_vk->layout, src_layout,
7119 src_texture_vk->image.vk_image, &vk_src_range);
7120 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7121 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7122 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7123 VK_ACCESS_TRANSFER_WRITE_BIT, dst_texture_vk->layout, dst_layout,
7124 dst_texture_vk->image.vk_image, &vk_dst_range);
7126 if (resolve)
7128 const struct wined3d_format_vk *src_format_vk = wined3d_format_vk(src_texture->resource.format);
7129 const struct wined3d_format_vk *dst_format_vk = wined3d_format_vk(dst_texture->resource.format);
7130 const unsigned int usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
7131 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
7132 VkImageLayout resolve_src_layout, resolve_dst_layout;
7133 VkImage src_vk_image, dst_vk_image;
7134 VkImageSubresourceRange vk_range;
7135 VkImageResolve resolve_region;
7136 VkImageType vk_image_type;
7137 VkImageCopy copy_region;
7138 VkFormat vk_format;
7140 if (resolve_format)
7142 vk_format = wined3d_format_vk(resolve_format)->vk_format;
7144 else if (!wined3d_format_is_typeless(src_texture->resource.format))
7146 vk_format = src_format_vk->vk_format;
7148 else
7150 vk_format = dst_format_vk->vk_format;
7153 switch (src_texture->resource.type)
7155 case WINED3D_RTYPE_TEXTURE_1D:
7156 vk_image_type = VK_IMAGE_TYPE_1D;
7157 break;
7158 case WINED3D_RTYPE_TEXTURE_2D:
7159 vk_image_type = VK_IMAGE_TYPE_2D;
7160 break;
7161 case WINED3D_RTYPE_TEXTURE_3D:
7162 vk_image_type = VK_IMAGE_TYPE_3D;
7163 break;
7164 default:
7165 ERR("Unexpected resource type: %s\n", debug_d3dresourcetype(src_texture->resource.type));
7166 goto barrier_next;
7169 vk_range.baseMipLevel = 0;
7170 vk_range.levelCount = 1;
7171 vk_range.baseArrayLayer = 0;
7172 vk_range.layerCount = 1;
7174 resolve_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7175 resolve_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7176 resolve_region.extent.width = src_rect->right - src_rect->left;
7177 resolve_region.extent.height = src_rect->bottom - src_rect->top;
7178 resolve_region.extent.depth = 1;
7180 /* In case of typeless resolve the texture type may not match the resolve type.
7181 * To handle that, allocate intermediate texture(s) to resolve from/to.
7182 * A possible performance improvement would be to resolve using a shader instead. */
7183 if (src_format_vk->vk_format != vk_format)
7185 struct wined3d_image_vk src_image;
7187 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7188 resolve_region.extent.width, resolve_region.extent.height, 1,
7189 src_sample_count, 1, 1, 0, &src_image))
7190 goto barrier_next;
7192 wined3d_context_vk_reference_image(context_vk, &src_image);
7193 src_vk_image = src_image.vk_image;
7194 wined3d_context_vk_destroy_image(context_vk, &src_image);
7196 vk_range.aspectMask = vk_src_range.aspectMask;
7198 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7199 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7200 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
7201 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, src_vk_image, &vk_range);
7203 copy_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7204 copy_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7205 copy_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7206 copy_region.srcSubresource.layerCount = 1;
7207 copy_region.srcOffset.x = src_rect->left;
7208 copy_region.srcOffset.y = src_rect->top;
7209 copy_region.srcOffset.z = 0;
7210 copy_region.dstSubresource.aspectMask = vk_src_range.aspectMask;
7211 copy_region.dstSubresource.mipLevel = 0;
7212 copy_region.dstSubresource.baseArrayLayer = 0;
7213 copy_region.dstSubresource.layerCount = 1;
7214 copy_region.dstOffset.x = 0;
7215 copy_region.dstOffset.y = 0;
7216 copy_region.dstOffset.z = 0;
7217 copy_region.extent.width = resolve_region.extent.width;
7218 copy_region.extent.height = resolve_region.extent.height;
7219 copy_region.extent.depth = 1;
7221 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image,
7222 src_layout, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
7223 1, &copy_region));
7225 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7226 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7227 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7228 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7229 src_vk_image, &vk_range);
7230 resolve_src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7232 resolve_region.srcSubresource.mipLevel = 0;
7233 resolve_region.srcSubresource.baseArrayLayer = 0;
7234 resolve_region.srcSubresource.layerCount = 1;
7235 resolve_region.srcOffset.x = 0;
7236 resolve_region.srcOffset.y = 0;
7237 resolve_region.srcOffset.z = 0;
7239 else
7241 src_vk_image = src_texture_vk->image.vk_image;
7242 resolve_src_layout = src_layout;
7244 resolve_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7245 resolve_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7246 resolve_region.srcSubresource.layerCount = 1;
7247 resolve_region.srcOffset.x = src_rect->left;
7248 resolve_region.srcOffset.y = src_rect->top;
7249 resolve_region.srcOffset.z = 0;
7252 if (dst_format_vk->vk_format != vk_format)
7254 struct wined3d_image_vk dst_image;
7256 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7257 resolve_region.extent.width, resolve_region.extent.height, 1,
7258 VK_SAMPLE_COUNT_1_BIT, 1, 1, 0, &dst_image))
7259 goto barrier_next;
7261 wined3d_context_vk_reference_image(context_vk, &dst_image);
7262 dst_vk_image = dst_image.vk_image;
7263 wined3d_context_vk_destroy_image(context_vk, &dst_image);
7265 vk_range.aspectMask = vk_dst_range.aspectMask;
7266 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7267 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
7268 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_vk_image, &vk_range);
7269 resolve_dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7271 resolve_region.dstSubresource.mipLevel = 0;
7272 resolve_region.dstSubresource.baseArrayLayer = 0;
7273 resolve_region.dstSubresource.layerCount = 1;
7274 resolve_region.dstOffset.x = 0;
7275 resolve_region.dstOffset.y = 0;
7276 resolve_region.dstOffset.z = 0;
7278 else
7280 dst_vk_image = dst_texture_vk->image.vk_image;
7281 resolve_dst_layout = dst_layout;
7283 resolve_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7284 resolve_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7285 resolve_region.dstSubresource.layerCount = 1;
7286 resolve_region.dstOffset.x = dst_rect->left;
7287 resolve_region.dstOffset.y = dst_rect->top;
7288 resolve_region.dstOffset.z = 0;
7291 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_vk_image, resolve_src_layout,
7292 dst_vk_image, resolve_dst_layout, 1, &resolve_region));
7294 if (dst_vk_image != dst_texture_vk->image.vk_image)
7296 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7297 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7298 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7299 resolve_dst_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7300 dst_vk_image, &vk_range);
7302 copy_region.srcSubresource.aspectMask = vk_dst_range.aspectMask;
7303 copy_region.srcSubresource.mipLevel = 0;
7304 copy_region.srcSubresource.baseArrayLayer = 0;
7305 copy_region.srcSubresource.layerCount = 1;
7306 copy_region.srcOffset.x = 0;
7307 copy_region.srcOffset.y = 0;
7308 copy_region.srcOffset.z = 0;
7309 copy_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7310 copy_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7311 copy_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7312 copy_region.dstSubresource.layerCount = 1;
7313 copy_region.dstOffset.x = dst_rect->left;
7314 copy_region.dstOffset.y = dst_rect->top;
7315 copy_region.dstOffset.z = 0;
7316 copy_region.extent.width = resolve_region.extent.width;
7317 copy_region.extent.height = resolve_region.extent.height;
7318 copy_region.extent.depth = 1;
7320 VK_CALL(vkCmdCopyImage(vk_command_buffer, dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7321 dst_texture_vk->image.vk_image, dst_layout, 1, &copy_region));
7324 else
7326 VkImageCopy region;
7328 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7329 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7330 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7331 region.srcSubresource.layerCount = vk_src_range.layerCount;
7332 region.srcOffset.x = src_rect->left;
7333 region.srcOffset.y = src_rect->top;
7334 region.srcOffset.z = 0;
7335 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7336 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7337 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7338 region.dstSubresource.layerCount = vk_dst_range.layerCount;
7339 region.dstOffset.x = dst_rect->left;
7340 region.dstOffset.y = dst_rect->top;
7341 region.dstOffset.z = 0;
7342 region.extent.width = src_rect->right - src_rect->left;
7343 region.extent.height = src_rect->bottom - src_rect->top;
7344 region.extent.depth = 1;
7346 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, src_layout,
7347 dst_texture_vk->image.vk_image, dst_layout, 1, &region));
7350 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7351 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7352 VK_ACCESS_TRANSFER_WRITE_BIT,
7353 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7354 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7355 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7356 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7357 VK_ACCESS_TRANSFER_READ_BIT,
7358 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7359 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7361 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
7362 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
7363 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
7364 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
7366 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
7367 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
7369 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
7371 barrier_next:
7372 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7373 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7374 VK_ACCESS_TRANSFER_WRITE_BIT,
7375 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7376 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7377 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7378 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7379 VK_ACCESS_TRANSFER_READ_BIT,
7380 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7381 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7383 next:
7384 if (!(next = blitter->next))
7386 ERR("No blitter to handle blit op %#x.\n", op);
7387 return dst_location;
7390 TRACE("Forwarding to blitter %p.\n", next);
7391 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
7392 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
7395 static const struct wined3d_blitter_ops vk_blitter_ops =
7397 .blitter_destroy = vk_blitter_destroy,
7398 .blitter_clear = vk_blitter_clear,
7399 .blitter_blit = vk_blitter_blit,
7402 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
7404 struct wined3d_blitter *blitter;
7406 if (!(blitter = heap_alloc(sizeof(*blitter))))
7407 return;
7409 TRACE("Created blitter %p.\n", blitter);
7411 blitter->ops = &vk_blitter_ops;
7412 blitter->next = *next;
7413 *next = blitter;