wined3d: Remove WINED3D_TEXTURE_NORMALIZED_COORDS.
[wine.git] / dlls / wined3d / texture.c
blobbc7b1b63b9aefde146aa24bf824522f65d7cb269
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_blit(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_blit(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 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
1093 unsigned int sub_count = texture->level_count * texture->layer_count;
1094 struct wined3d_texture_sub_resource *sub_resource;
1095 unsigned int i;
1097 for (i = 0; i < sub_count; ++i)
1099 sub_resource = &texture->sub_resources[i];
1100 if (sub_resource->parent)
1102 TRACE("sub-resource %u.\n", i);
1103 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
1104 sub_resource->parent = NULL;
1109 static void wined3d_texture_create_dc(void *object)
1111 const struct wined3d_texture_idx *idx = object;
1112 struct wined3d_context *context = NULL;
1113 unsigned int sub_resource_idx, level;
1114 const struct wined3d_format *format;
1115 unsigned int row_pitch, slice_pitch;
1116 struct wined3d_texture *texture;
1117 struct wined3d_dc_info *dc_info;
1118 struct wined3d_bo_address data;
1119 D3DKMT_CREATEDCFROMMEMORY desc;
1120 struct wined3d_device *device;
1121 NTSTATUS status;
1123 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1125 texture = idx->texture;
1126 sub_resource_idx = idx->sub_resource_idx;
1127 level = sub_resource_idx % texture->level_count;
1128 device = texture->resource.device;
1130 format = texture->resource.format;
1131 if (!format->ddi_format)
1133 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1134 return;
1137 if (!texture->dc_info)
1139 unsigned int sub_count = texture->level_count * texture->layer_count;
1141 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
1143 ERR("Failed to allocate DC info.\n");
1144 return;
1148 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
1150 context = context_acquire(device, NULL, 0);
1151 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1153 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1154 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
1155 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1156 if (data.buffer_object)
1158 if (!context)
1159 context = context_acquire(device, NULL, 0);
1160 desc.pMemory = wined3d_context_map_bo_address(context, &data,
1161 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
1163 else
1165 desc.pMemory = data.addr;
1168 if (context)
1169 context_release(context);
1171 desc.Format = format->ddi_format;
1172 desc.Width = wined3d_texture_get_level_width(texture, level);
1173 desc.Height = wined3d_texture_get_level_height(texture, level);
1174 desc.Pitch = row_pitch;
1175 desc.hDeviceDc = CreateCompatibleDC(NULL);
1176 desc.pColorTable = NULL;
1178 status = D3DKMTCreateDCFromMemory(&desc);
1179 DeleteDC(desc.hDeviceDc);
1180 if (status)
1182 WARN("Failed to create DC, status %#lx.\n", status);
1183 return;
1186 dc_info = &texture->dc_info[sub_resource_idx];
1187 dc_info->dc = desc.hDc;
1188 dc_info->bitmap = desc.hBitmap;
1190 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1193 static void wined3d_texture_destroy_dc(void *object)
1195 const struct wined3d_texture_idx *idx = object;
1196 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1197 struct wined3d_context *context;
1198 struct wined3d_texture *texture;
1199 struct wined3d_dc_info *dc_info;
1200 struct wined3d_bo_address data;
1201 unsigned int sub_resource_idx;
1202 struct wined3d_device *device;
1203 struct wined3d_range range;
1204 NTSTATUS status;
1206 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1208 texture = idx->texture;
1209 sub_resource_idx = idx->sub_resource_idx;
1210 device = texture->resource.device;
1211 dc_info = &texture->dc_info[sub_resource_idx];
1213 if (!dc_info->dc)
1215 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1216 return;
1219 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1221 destroy_desc.hDc = dc_info->dc;
1222 destroy_desc.hBitmap = dc_info->bitmap;
1223 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1224 ERR("Failed to destroy dc, status %#lx.\n", status);
1225 dc_info->dc = NULL;
1226 dc_info->bitmap = NULL;
1228 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
1229 if (data.buffer_object)
1231 context = context_acquire(device, NULL, 0);
1232 range.offset = 0;
1233 range.size = texture->sub_resources[sub_resource_idx].size;
1234 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1235 context_release(context);
1239 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1241 texture->swapchain = swapchain;
1242 wined3d_resource_update_draw_binding(&texture->resource);
1245 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1247 static const GLenum swizzle_source[] =
1249 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1250 GL_ONE, /* CHANNEL_SOURCE_ONE */
1251 GL_RED, /* CHANNEL_SOURCE_X */
1252 GL_GREEN, /* CHANNEL_SOURCE_Y */
1253 GL_BLUE, /* CHANNEL_SOURCE_Z */
1254 GL_ALPHA, /* CHANNEL_SOURCE_W */
1257 swizzle[0] = swizzle_source[fixup.x_source];
1258 swizzle[1] = swizzle_source[fixup.y_source];
1259 swizzle[2] = swizzle_source[fixup.z_source];
1260 swizzle[3] = swizzle_source[fixup.w_source];
1263 /* Context activation is done by the caller. */
1264 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1265 struct wined3d_context_gl *context_gl, BOOL srgb)
1267 const struct wined3d_format *format = texture_gl->t.resource.format;
1268 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1269 const struct color_fixup_desc fixup = format->color_fixup;
1270 struct gl_texture *gl_tex;
1271 GLenum target;
1273 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1275 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1276 srgb = FALSE;
1278 /* sRGB mode cache for preload() calls outside drawprim. */
1279 if (srgb)
1280 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1281 else
1282 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1284 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1285 target = texture_gl->target;
1287 if (gl_tex->name)
1289 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1290 return;
1293 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1294 checkGLcall("glGenTextures");
1295 TRACE("Generated texture %d.\n", gl_tex->name);
1297 if (!gl_tex->name)
1299 ERR("Failed to generate a texture name.\n");
1300 return;
1303 /* Initialise the state of the texture object to the OpenGL defaults, not
1304 * the wined3d defaults. */
1305 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1306 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1307 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1308 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1309 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1310 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1311 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1312 gl_tex->sampler_desc.lod_bias = 0.0f;
1313 gl_tex->sampler_desc.min_lod = -1000.0f;
1314 gl_tex->sampler_desc.max_lod = 1000.0f;
1315 gl_tex->sampler_desc.max_anisotropy = 1;
1316 gl_tex->sampler_desc.compare = FALSE;
1317 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1318 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1319 gl_tex->sampler_desc.srgb_decode = TRUE;
1320 else
1321 gl_tex->sampler_desc.srgb_decode = srgb;
1322 gl_tex->base_level = 0;
1323 wined3d_texture_set_dirty(&texture_gl->t);
1325 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1327 /* For a new texture we have to set the texture levels after binding the
1328 * texture. Beware that texture rectangles do not support mipmapping, but
1329 * set the maxmiplevel if we're relying on the partial
1330 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1331 * (I.e., do not care about cond_np2 here, just look for
1332 * GL_TEXTURE_RECTANGLE_ARB.) */
1333 if (target != GL_TEXTURE_RECTANGLE_ARB)
1335 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1336 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1337 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1340 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1342 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1343 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1344 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1345 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1348 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1350 /* Conditional non power of two textures use a different clamping
1351 * default. If we're using the GL_WINE_normalized_texrect partial
1352 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1353 * has the address mode set to repeat - something that prevents us
1354 * from hitting the accelerated codepath. Thus manually set the GL
1355 * state. The same applies to filtering. Even if the texture has only
1356 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1357 * fallback on macos. */
1358 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1359 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1360 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1361 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1362 checkGLcall("glTexParameteri");
1363 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1364 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1365 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1366 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1367 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1370 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1372 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1373 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1376 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1378 GLint swizzle[4];
1380 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1381 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1382 checkGLcall("set format swizzle");
1386 /* Context activation is done by the caller. */
1387 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1388 struct wined3d_context_gl *context_gl, BOOL srgb)
1390 /* We don't need a specific texture unit, but after binding the texture
1391 * the current unit is dirty. Read the unit back instead of switching to
1392 * 0, this avoids messing around with the state manager's GL states. The
1393 * current texture unit should always be a valid one.
1395 * To be more specific, this is tricky because we can implicitly be
1396 * called from sampler() in state.c. This means we can't touch anything
1397 * other than whatever happens to be the currently active texture, or we
1398 * would risk marking already applied sampler states dirty again. */
1399 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1401 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1402 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1403 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1405 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1406 * a shader. */
1407 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1408 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1410 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1413 /* Context activation is done by the caller (state handler). */
1414 /* This function relies on the correct texture being bound and loaded. */
1415 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1416 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1418 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1419 GLenum target = texture_gl->target;
1420 struct gl_texture *gl_tex;
1421 DWORD state;
1423 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1425 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1427 state = sampler_desc->address_u;
1428 if (state != gl_tex->sampler_desc.address_u)
1430 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1431 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1432 gl_tex->sampler_desc.address_u = state;
1435 state = sampler_desc->address_v;
1436 if (state != gl_tex->sampler_desc.address_v)
1438 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1439 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1440 gl_tex->sampler_desc.address_v = state;
1443 state = sampler_desc->address_w;
1444 if (state != gl_tex->sampler_desc.address_w)
1446 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1447 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1448 gl_tex->sampler_desc.address_w = state;
1451 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1452 sizeof(gl_tex->sampler_desc.border_color)))
1454 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1455 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1456 sizeof(gl_tex->sampler_desc.border_color));
1459 state = sampler_desc->mag_filter;
1460 if (state != gl_tex->sampler_desc.mag_filter)
1462 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1463 gl_tex->sampler_desc.mag_filter = state;
1466 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1467 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1469 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1470 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1471 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1472 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1475 state = sampler_desc->max_anisotropy;
1476 if (state != gl_tex->sampler_desc.max_anisotropy)
1478 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1479 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1480 else
1481 WARN("Anisotropic filtering not supported.\n");
1482 gl_tex->sampler_desc.max_anisotropy = state;
1485 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1486 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1487 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1489 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1490 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1491 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1494 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1496 if (sampler_desc->compare)
1497 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1498 else
1499 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1500 gl_tex->sampler_desc.compare = sampler_desc->compare;
1503 checkGLcall("Texture parameter application");
1505 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1507 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1508 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1509 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1513 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1515 unsigned int refcount;
1517 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1519 refcount = InterlockedIncrement(&texture->resource.ref);
1520 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1522 return refcount;
1525 static void wined3d_texture_destroy_object(void *object)
1527 struct wined3d_texture *texture = object;
1528 struct wined3d_resource *resource;
1529 struct wined3d_dc_info *dc_info;
1530 unsigned int sub_count;
1531 unsigned int i;
1533 TRACE("texture %p.\n", texture);
1535 resource = &texture->resource;
1536 sub_count = texture->level_count * texture->layer_count;
1538 if ((dc_info = texture->dc_info))
1540 for (i = 0; i < sub_count; ++i)
1542 if (dc_info[i].dc)
1544 struct wined3d_texture_idx texture_idx = {texture, i};
1546 wined3d_texture_destroy_dc(&texture_idx);
1549 heap_free(dc_info);
1552 if (texture->overlay_info)
1554 for (i = 0; i < sub_count; ++i)
1556 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1557 struct wined3d_overlay_info *overlay, *cur;
1559 list_remove(&info->entry);
1560 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1562 list_remove(&overlay->entry);
1565 heap_free(texture->overlay_info);
1568 if (texture->dirty_regions)
1570 for (i = 0; i < texture->layer_count; ++i)
1572 heap_free(texture->dirty_regions[i].boxes);
1574 heap_free(texture->dirty_regions);
1577 /* Discard the contents of resources with CPU access, to avoid downloading
1578 * them to SYSMEM on unload. */
1579 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
1581 for (i = 0; i < sub_count; ++i)
1583 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1584 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1587 resource->resource_ops->resource_unload(resource);
1590 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1592 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1593 resource_cleanup(&texture->resource);
1596 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1598 wined3d_texture_sub_resources_destroyed(texture);
1599 wined3d_texture_cleanup(texture);
1600 wined3d_resource_wait_idle(&texture->resource);
1603 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1605 unsigned int i, sub_resource_count, refcount;
1607 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1609 refcount = InterlockedDecrement(&texture->resource.ref);
1610 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1612 if (!refcount)
1614 bool in_cs_thread = GetCurrentThreadId() == texture->resource.device->cs->thread_id;
1616 /* This is called from the CS thread to destroy temporary textures. */
1617 if (!in_cs_thread)
1618 wined3d_mutex_lock();
1619 /* Wait for the texture to become idle if it's using user memory,
1620 * since the application is allowed to free that memory once the
1621 * texture is destroyed. Note that this implies that
1622 * the destroy handler can't access that memory either. */
1623 sub_resource_count = texture->layer_count * texture->level_count;
1624 for (i = 0; i < sub_resource_count; ++i)
1626 if (texture->sub_resources[i].user_memory)
1628 wined3d_resource_wait_idle(&texture->resource);
1629 break;
1632 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1633 if (!in_cs_thread)
1634 wined3d_mutex_unlock();
1637 return refcount;
1640 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1642 TRACE("texture %p.\n", texture);
1644 return &texture->resource;
1647 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1649 return c1->color_space_low_value == c2->color_space_low_value
1650 && c1->color_space_high_value == c2->color_space_high_value;
1653 /* Context activation is done by the caller */
1654 void wined3d_texture_load(struct wined3d_texture *texture,
1655 struct wined3d_context *context, BOOL srgb)
1657 UINT sub_count = texture->level_count * texture->layer_count;
1658 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1659 DWORD flag;
1660 UINT i;
1662 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1664 if (!needs_separate_srgb_gl_texture(context, texture))
1665 srgb = FALSE;
1667 if (srgb)
1668 flag = WINED3D_TEXTURE_SRGB_VALID;
1669 else
1670 flag = WINED3D_TEXTURE_RGB_VALID;
1672 if (!d3d_info->shader_color_key
1673 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1674 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1675 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1676 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1678 unsigned int i;
1680 TRACE("Reloading because of color key value change.\n");
1681 for (i = 0; i < sub_count; i++)
1683 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1684 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1685 else
1686 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1689 texture->async.gl_color_key = texture->async.src_blt_color_key;
1692 if (texture->flags & flag)
1694 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1695 return;
1698 /* Reload the surfaces if the texture is marked dirty. */
1699 for (i = 0; i < sub_count; ++i)
1701 if (!wined3d_texture_load_location(texture, i, context,
1702 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1703 ERR("Failed to load location (srgb %#x).\n", srgb);
1705 texture->flags |= flag;
1708 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1710 TRACE("texture %p.\n", texture);
1712 return texture->resource.parent;
1715 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1716 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1718 const struct wined3d_resource *resource = &texture->resource;
1719 unsigned int width = wined3d_texture_get_level_width(texture, level);
1720 unsigned int height = wined3d_texture_get_level_height(texture, level);
1722 if (texture->row_pitch)
1724 *row_pitch = texture->row_pitch;
1725 *slice_pitch = texture->slice_pitch;
1726 return;
1729 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1730 width, height, row_pitch, slice_pitch);
1733 unsigned int CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, unsigned int lod)
1735 struct wined3d_resource *resource;
1736 unsigned int old = texture->lod;
1738 TRACE("texture %p, lod %u.\n", texture, lod);
1740 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1741 * textures. The call always returns 0, and GetLOD always returns 0. */
1742 resource = &texture->resource;
1743 if (!(resource->usage & WINED3DUSAGE_MANAGED))
1745 TRACE("Ignoring LOD on texture with resource access %s.\n",
1746 wined3d_debug_resource_access(resource->access));
1747 return 0;
1750 if (lod >= texture->level_count)
1751 lod = texture->level_count - 1;
1753 if (texture->lod != lod)
1755 struct wined3d_device *device = resource->device;
1757 wined3d_resource_wait_idle(resource);
1758 texture->lod = lod;
1760 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1761 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1762 if (resource->bind_count)
1763 wined3d_device_context_emit_set_sampler_state(&device->cs->c, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1764 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1767 return old;
1770 unsigned int CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1772 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1774 return texture->lod;
1777 UINT CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1779 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1781 return texture->level_count;
1784 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1785 uint32_t flags, const struct wined3d_color_key *color_key)
1787 struct wined3d_device *device = texture->resource.device;
1788 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1789 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1791 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1793 if (flags & ~all_flags)
1795 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1796 return WINED3DERR_INVALIDCALL;
1799 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1801 return WINED3D_OK;
1804 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1805 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1806 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1807 /* Context activation is done by the caller. */
1808 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1809 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1811 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1812 struct wined3d_renderbuffer_entry *entry;
1813 unsigned int src_width, src_height;
1814 unsigned int width, height;
1815 GLuint renderbuffer = 0;
1817 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1818 return;
1820 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1822 struct wined3d_texture *rt_texture;
1823 unsigned int rt_level;
1825 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1827 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1828 return;
1830 rt_texture = wined3d_texture_from_resource(rt->resource);
1831 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1833 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1834 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1836 else
1838 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1839 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1842 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1843 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1845 /* A depth stencil smaller than the render target is not valid */
1846 if (width > src_width || height > src_height)
1847 return;
1849 /* Remove any renderbuffer set if the sizes match */
1850 if (width == src_width && height == src_height)
1852 texture_gl->current_renderbuffer = NULL;
1853 return;
1856 /* Look if we've already got a renderbuffer of the correct dimensions */
1857 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1859 if (entry->width == width && entry->height == height)
1861 renderbuffer = entry->id;
1862 texture_gl->current_renderbuffer = entry;
1863 break;
1867 if (!renderbuffer)
1869 const struct wined3d_format_gl *format_gl;
1871 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1872 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1873 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1874 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1876 entry = heap_alloc(sizeof(*entry));
1877 entry->width = width;
1878 entry->height = height;
1879 entry->id = renderbuffer;
1880 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1882 texture_gl->current_renderbuffer = entry;
1885 checkGLcall("set compatible renderbuffer");
1888 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture,
1889 unsigned int sub_resource_idx, void *mem, unsigned int pitch)
1891 unsigned int current_row_pitch, current_slice_pitch, width, height;
1892 struct wined3d_texture_sub_resource *sub_resource;
1893 unsigned int i, level, sub_resource_count;
1894 const struct wined3d_format *format;
1895 struct wined3d_device *device;
1896 unsigned int slice_pitch;
1897 bool update_memory_only;
1898 bool create_dib = false;
1900 TRACE("texture %p, sub_resource_idx %u, mem %p, pitch %u.\n", texture, sub_resource_idx, mem, pitch);
1902 device = texture->resource.device;
1903 format = texture->resource.format;
1904 level = sub_resource_idx % texture->level_count;
1905 sub_resource_count = texture->level_count * texture->layer_count;
1907 width = wined3d_texture_get_level_width(texture, level);
1908 height = wined3d_texture_get_level_height(texture, level);
1909 if (pitch)
1910 slice_pitch = height * pitch;
1911 else
1912 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1914 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1915 update_memory_only = (pitch == current_row_pitch && slice_pitch == current_slice_pitch);
1917 if (sub_resource_count > 1 && !update_memory_only)
1919 FIXME("Texture has multiple sub-resources, not supported.\n");
1920 return WINED3DERR_INVALIDCALL;
1923 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1925 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1926 return WINED3DERR_INVALIDCALL;
1929 if (texture->resource.map_count)
1931 WARN("Texture is mapped.\n");
1932 return WINED3DERR_INVALIDCALL;
1935 /* We have no way of supporting a pitch that is not a multiple of the pixel
1936 * byte width short of uploading the texture row-by-row.
1937 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1938 * for user-memory textures (it always expects packed data) while DirectDraw
1939 * requires a 4-byte aligned pitch and doesn't support texture formats
1940 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1941 * This check is here to verify that the assumption holds. */
1942 if (pitch % format->byte_count)
1944 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1945 return WINED3DERR_INVALIDCALL;
1948 if (device->d3d_initialized)
1949 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1950 wined3d_resource_wait_idle(&texture->resource);
1952 if (texture->dc_info && texture->dc_info[0].dc)
1954 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1956 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1957 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1958 create_dib = true;
1961 texture->sub_resources[sub_resource_idx].user_memory = mem;
1963 if (update_memory_only)
1965 for (i = 0; i < sub_resource_count; ++i)
1966 if (!texture->sub_resources[i].user_memory)
1967 break;
1969 if (i == sub_resource_count)
1970 wined3d_resource_free_sysmem(&texture->resource);
1972 else
1974 wined3d_resource_free_sysmem(&texture->resource);
1976 sub_resource = &texture->sub_resources[sub_resource_idx];
1978 texture->row_pitch = pitch;
1979 texture->slice_pitch = slice_pitch;
1981 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
1982 && texture->resource.usage & WINED3DUSAGE_VIDMEM_ACCOUNTING)
1983 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1984 texture->resource.size = texture->slice_pitch;
1985 sub_resource->size = texture->slice_pitch;
1986 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1989 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1990 ERR("Failed to allocate resource memory.\n");
1992 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1993 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1995 if (create_dib)
1997 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1999 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
2000 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2003 return WINED3D_OK;
2006 /* Context activation is done by the caller. */
2007 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
2008 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
2010 struct wined3d_texture_sub_resource *sub_resource;
2011 struct wined3d_bo_gl *bo;
2013 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2015 if (sub_resource->bo)
2016 return;
2018 if (!(bo = heap_alloc(sizeof(*bo))))
2019 return;
2021 if (!wined3d_device_gl_create_bo(wined3d_device_gl(texture_gl->t.resource.device),
2022 context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, GL_STREAM_DRAW, true,
2023 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
2025 heap_free(bo);
2026 return;
2029 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
2030 sub_resource->bo = &bo->b;
2033 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
2035 unsigned int sub_count = texture->level_count * texture->layer_count;
2036 unsigned int i;
2038 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
2039 | WINED3D_TEXTURE_CONVERTED);
2040 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2041 for (i = 0; i < sub_count; ++i)
2043 wined3d_texture_invalidate_location(texture, i,
2044 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
2048 /* Context activation is done by the caller. */
2049 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
2050 struct wined3d_context_gl *context_gl, BOOL srgb)
2052 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
2053 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
2054 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2055 struct wined3d_resource *resource = &texture_gl->t.resource;
2056 const struct wined3d_device *device = resource->device;
2057 const struct wined3d_format *format = resource->format;
2058 const struct wined3d_color_key_conversion *conversion;
2059 const struct wined3d_format_gl *format_gl;
2060 GLenum internal;
2062 TRACE("texture_gl %p, context_gl %p, srgb %d, format %s.\n",
2063 texture_gl, context_gl, srgb, debug_d3dformat(format->id));
2065 if (!d3d_info->shader_color_key
2066 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
2067 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
2069 wined3d_texture_force_reload(&texture_gl->t);
2071 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
2072 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2075 if (texture_gl->t.flags & alloc_flag)
2076 return;
2078 if (resource->format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2080 TRACE("WINED3D_FORMAT_CAP_DECOMPRESS set.\n");
2081 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2082 format = wined3d_resource_get_decompress_format(resource);
2084 else if (format->conv_byte_count)
2086 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2088 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
2090 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2091 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
2092 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2094 format_gl = wined3d_format_gl(format);
2096 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2098 internal = wined3d_gl_get_internal_format(resource, format_gl, srgb);
2099 if (!internal)
2100 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2102 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2104 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2105 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2106 else
2107 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2108 texture_gl->t.flags |= alloc_flag;
2111 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2112 const struct wined3d_gl_info *gl_info, BOOL multisample)
2114 const struct wined3d_format_gl *format_gl;
2116 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2117 if (multisample)
2119 DWORD samples;
2121 if (texture_gl->rb_multisample)
2122 return;
2124 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2126 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2127 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2128 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2129 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2130 checkGLcall("glRenderbufferStorageMultisample()");
2131 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2133 else
2135 if (texture_gl->rb_resolved)
2136 return;
2138 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2139 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2140 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2141 texture_gl->t.resource.width, texture_gl->t.resource.height);
2142 checkGLcall("glRenderbufferStorage()");
2143 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2147 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2148 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2150 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2153 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2154 unsigned int sub_resource_idx)
2156 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2158 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
2159 return NULL;
2160 return &texture->sub_resources[sub_resource_idx];
2163 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2164 unsigned int layer, const struct wined3d_box *box)
2166 struct wined3d_dirty_regions *regions;
2167 unsigned int count;
2169 if (!texture->dirty_regions)
2170 return;
2172 regions = &texture->dirty_regions[layer];
2173 count = regions->box_count + 1;
2174 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2175 || (!box->left && !box->top && !box->front
2176 && box->right == texture->resource.width
2177 && box->bottom == texture->resource.height
2178 && box->back == texture->resource.depth))
2180 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2181 return;
2184 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2186 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2187 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2188 return;
2191 regions->boxes[regions->box_count++] = *box;
2194 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2195 UINT layer, const struct wined3d_box *dirty_region)
2197 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2199 if (layer >= texture->layer_count)
2201 WARN("Invalid layer %u specified.\n", layer);
2202 return WINED3DERR_INVALIDCALL;
2205 if (dirty_region && FAILED(wined3d_resource_check_box_dimensions(&texture->resource, 0, dirty_region)))
2207 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2208 return WINED3DERR_INVALIDCALL;
2211 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2212 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2214 return WINED3D_OK;
2217 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2218 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2219 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2220 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2221 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2223 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2225 if (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2227 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2228 unsigned int dst_row_pitch, dst_slice_pitch;
2230 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2232 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2233 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2234 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2235 update_d, internal, dst_slice_pitch, addr);
2237 if (target == GL_TEXTURE_1D)
2239 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2240 update_w, internal, dst_row_pitch, addr));
2242 else
2244 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2246 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2247 * can't use the unpack row length like for glTexSubImage2D. */
2248 if (dst_row_pitch != src_row_pitch)
2250 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2251 update_h = src_format->block_height;
2252 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2253 &dst_row_pitch, &dst_slice_pitch);
2256 if (dst_slice_pitch != src_slice_pitch)
2258 slice_count = update_d;
2259 update_d = 1;
2262 for (slice = 0; slice < slice_count; ++slice)
2264 for (row = 0, y = dst_y; row < row_count; ++row)
2266 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2268 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2270 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2271 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2273 else
2275 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2276 update_h, internal, dst_slice_pitch, upload_addr));
2279 y += src_format->block_height;
2283 checkGLcall("Upload compressed texture data");
2285 else
2287 unsigned int y, y_count, z, z_count;
2288 bool unpacking_rows = false;
2290 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2291 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2292 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2293 update_d, format_gl->format, format_gl->type, addr);
2295 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2297 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2298 y_count = 1;
2299 unpacking_rows = true;
2301 else
2303 y_count = update_h;
2304 update_h = 1;
2307 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2309 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2310 z_count = 1;
2312 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2314 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2315 src_slice_pitch / (update_w * src_format->byte_count));
2316 z_count = 1;
2318 else
2320 z_count = update_d;
2321 update_d = 1;
2324 for (z = 0; z < z_count; ++z)
2326 for (y = 0; y < y_count; ++y)
2328 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2329 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2331 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2332 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2334 else if (target == GL_TEXTURE_1D)
2336 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2337 update_w, format_gl->format, format_gl->type, upload_addr);
2339 else
2341 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2342 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2346 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2347 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2348 checkGLcall("Upload texture data");
2352 static const struct d3dfmt_alpha_fixup
2354 enum wined3d_format_id format_id, conv_format_id;
2356 formats_src_alpha_fixup[] =
2358 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2359 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2360 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2363 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2364 const struct wined3d_format *dst_format)
2366 unsigned int i;
2368 if (!(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED) && !dst_format->alpha_size)
2369 return WINED3DFMT_UNKNOWN;
2371 for (i = 0; i < ARRAY_SIZE(formats_src_alpha_fixup); ++i)
2373 if (formats_src_alpha_fixup[i].format_id == format_id)
2374 return formats_src_alpha_fixup[i].conv_format_id;
2377 return WINED3DFMT_UNKNOWN;
2380 static void wined3d_fixup_alpha(const struct wined3d_format *format, const uint8_t *src,
2381 unsigned int src_row_pitch, uint8_t *dst, unsigned int dst_row_pitch,
2382 unsigned int width, unsigned int height)
2384 unsigned int byte_count, alpha_mask;
2385 unsigned int x, y;
2387 byte_count = format->byte_count;
2388 alpha_mask = wined3d_mask_from_size(format->alpha_size) << format->alpha_offset;
2390 switch (byte_count)
2392 case 2:
2393 for (y = 0; y < height; ++y)
2395 const uint16_t *src_row = (const uint16_t *)&src[y * src_row_pitch];
2396 uint16_t *dst_row = (uint16_t *)&dst[y * dst_row_pitch];
2398 for (x = 0; x < width; ++x)
2400 dst_row[x] = src_row[x] | alpha_mask;
2403 break;
2405 case 4:
2406 for (y = 0; y < height; ++y)
2408 const uint32_t *src_row = (const uint32_t *)&src[y * src_row_pitch];
2409 uint32_t *dst_row = (uint32_t *)&dst[y * dst_row_pitch];
2411 for (x = 0; x < width; ++x)
2413 dst_row[x] = src_row[x] | alpha_mask;
2416 break;
2418 default:
2419 ERR("Unsupported byte count %u.\n", byte_count);
2420 break;
2424 static void wined3d_texture_gl_upload_data(struct wined3d_context *context,
2425 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
2426 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2427 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
2428 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
2430 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2431 enum wined3d_format_id alpha_fixup_format_id = WINED3DFMT_UNKNOWN;
2432 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2433 unsigned int update_w = src_box->right - src_box->left;
2434 unsigned int update_h = src_box->bottom - src_box->top;
2435 unsigned int update_d = src_box->back - src_box->front;
2436 struct wined3d_bo_address bo;
2437 unsigned int level;
2438 BOOL srgb = FALSE;
2439 BOOL decompress;
2440 GLenum target;
2442 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
2443 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
2444 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
2445 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
2446 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
2448 if (dst_location == WINED3D_LOCATION_TEXTURE_SRGB)
2450 srgb = TRUE;
2452 else if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
2454 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
2455 return;
2458 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), wined3d_context_gl(context), srgb);
2460 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count)
2462 WARN("Uploading a texture that is currently mapped, pinning sysmem.\n");
2463 dst_texture->resource.pin_sysmem = 1;
2466 if (src_format->attrs & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
2468 update_h *= src_format->height_scale.numerator;
2469 update_h /= src_format->height_scale.denominator;
2472 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(dst_texture), dst_sub_resource_idx);
2473 level = dst_sub_resource_idx % dst_texture->level_count;
2475 switch (target)
2477 case GL_TEXTURE_1D_ARRAY:
2478 dst_y = dst_sub_resource_idx / dst_texture->level_count;
2479 update_h = 1;
2480 break;
2481 case GL_TEXTURE_2D_ARRAY:
2482 dst_z = dst_sub_resource_idx / dst_texture->level_count;
2483 update_d = 1;
2484 break;
2485 case GL_TEXTURE_2D_MULTISAMPLE:
2486 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2487 FIXME("Not supported for multisample textures.\n");
2488 return;
2491 bo.buffer_object = src_bo_addr->buffer_object;
2492 bo.addr = (BYTE *)src_bo_addr->addr + src_box->front * src_slice_pitch;
2493 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2495 bo.addr += (src_box->top / src_format->block_height) * src_row_pitch;
2496 bo.addr += (src_box->left / src_format->block_width) * src_format->block_byte_count;
2498 else
2500 bo.addr += src_box->top * src_row_pitch;
2501 bo.addr += src_box->left * src_format->byte_count;
2504 decompress = (dst_texture->resource.format_caps & WINED3D_FORMAT_CAP_DECOMPRESS)
2505 || (src_format->decompress && src_format->id != dst_texture->resource.format->id);
2507 if (src_format->upload || decompress
2508 || (alpha_fixup_format_id = wined3d_get_alpha_fixup_format(src_format->id,
2509 dst_texture->resource.format)) != WINED3DFMT_UNKNOWN)
2511 const struct wined3d_format *compressed_format = src_format;
2512 unsigned int dst_row_pitch, dst_slice_pitch;
2513 struct wined3d_format_gl f;
2514 void *converted_mem;
2515 unsigned int z;
2516 BYTE *src_mem;
2518 if (decompress)
2520 src_format = wined3d_resource_get_decompress_format(&dst_texture->resource);
2522 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2524 src_format = wined3d_get_format(context->device->adapter, alpha_fixup_format_id, 0);
2525 assert(!!src_format);
2527 else
2529 if (dst_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_BLOCKS)
2530 ERR("Converting a block-based format.\n");
2532 f = *wined3d_format_gl(src_format);
2533 f.f.byte_count = src_format->conv_byte_count;
2534 src_format = &f.f;
2537 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2539 if (!(converted_mem = heap_alloc(dst_slice_pitch)))
2541 ERR("Failed to allocate upload buffer.\n");
2542 return;
2545 src_mem = wined3d_context_gl_map_bo_address(context_gl, &bo, src_slice_pitch * update_d, WINED3D_MAP_READ);
2547 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2548 checkGLcall("glBindBuffer");
2550 for (z = 0; z < update_d; ++z, src_mem += src_slice_pitch)
2552 if (decompress)
2553 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2554 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2555 else if (alpha_fixup_format_id != WINED3DFMT_UNKNOWN)
2556 wined3d_fixup_alpha(src_format, src_mem, src_row_pitch, converted_mem, dst_row_pitch,
2557 update_w, update_h);
2558 else
2559 src_format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2560 dst_row_pitch, dst_slice_pitch, update_w, update_h, 1);
2562 wined3d_texture_gl_upload_bo(src_format, target, level, dst_row_pitch, dst_slice_pitch, dst_x,
2563 dst_y, dst_z + z, update_w, update_h, 1, converted_mem, srgb, dst_texture, gl_info);
2566 wined3d_context_gl_unmap_bo_address(context_gl, &bo, 0, NULL);
2567 heap_free(converted_mem);
2569 else
2571 const uint8_t *offset = bo.addr;
2573 if (bo.buffer_object)
2575 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, wined3d_bo_gl(bo.buffer_object)->id));
2576 checkGLcall("glBindBuffer");
2577 offset += bo.buffer_object->buffer_offset;
2579 else
2581 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2582 checkGLcall("glBindBuffer");
2585 wined3d_texture_gl_upload_bo(src_format, target, level, src_row_pitch, src_slice_pitch, dst_x,
2586 dst_y, dst_z, update_w, update_h, update_d, offset, srgb, dst_texture, gl_info);
2588 if (bo.buffer_object)
2590 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2591 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(bo.buffer_object));
2592 checkGLcall("glBindBuffer");
2596 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2598 struct wined3d_device *device = dst_texture->resource.device;
2599 unsigned int i;
2601 for (i = 0; i < device->context_count; ++i)
2603 wined3d_context_gl_texture_update(wined3d_context_gl(device->contexts[i]), wined3d_texture_gl(dst_texture));
2608 static void wined3d_texture_gl_download_data_slow_path(struct wined3d_texture_gl *texture_gl,
2609 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, const struct wined3d_bo_address *data)
2611 struct wined3d_bo_gl *bo = wined3d_bo_gl(data->buffer_object);
2612 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2613 struct wined3d_texture_sub_resource *sub_resource;
2614 unsigned int dst_row_pitch, dst_slice_pitch;
2615 unsigned int src_row_pitch, src_slice_pitch;
2616 const struct wined3d_format_gl *format_gl;
2617 BYTE *temporary_mem = NULL;
2618 unsigned int level;
2619 GLenum target;
2620 void *mem;
2622 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2624 /* Only support read back of converted P8 textures. */
2625 if (texture_gl->t.flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT
2626 && !format_gl->f.download)
2628 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2629 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2630 return;
2633 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2634 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
2635 level = sub_resource_idx % texture_gl->t.level_count;
2637 if (target == GL_TEXTURE_1D_ARRAY || target == GL_TEXTURE_2D_ARRAY)
2639 if (format_gl->f.download)
2641 FIXME("Reading back converted array texture %p is not supported.\n", texture_gl);
2642 return;
2645 /* NP2 emulation is not allowed on array textures. */
2646 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2647 ERR("Array texture %p uses NP2 emulation.\n", texture_gl);
2649 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2651 if (!(temporary_mem = heap_calloc(texture_gl->t.layer_count, sub_resource->size)))
2653 ERR("Out of memory.\n");
2654 return;
2658 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2660 if (format_gl->f.download)
2662 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture_gl);
2663 return;
2666 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2667 wined3d_format_calculate_pitch(&format_gl->f, texture_gl->t.resource.device->surface_alignment,
2668 wined3d_texture_get_level_pow2_width(&texture_gl->t, level),
2669 wined3d_texture_get_level_pow2_height(&texture_gl->t, level),
2670 &src_row_pitch, &src_slice_pitch);
2671 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2673 ERR("Out of memory.\n");
2674 return;
2677 if (bo)
2678 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2679 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2680 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2683 if (format_gl->f.download)
2685 struct wined3d_format f;
2687 if (bo)
2688 ERR("Converted texture %p uses PBO unexpectedly.\n", texture_gl);
2690 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2691 texture_gl, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2693 f = format_gl->f;
2694 f.byte_count = format_gl->f.conv_byte_count;
2695 wined3d_texture_get_pitch(&texture_gl->t, level, &dst_row_pitch, &dst_slice_pitch);
2696 wined3d_format_calculate_pitch(&f, texture_gl->t.resource.device->surface_alignment,
2697 wined3d_texture_get_level_width(&texture_gl->t, level),
2698 wined3d_texture_get_level_height(&texture_gl->t, level),
2699 &src_row_pitch, &src_slice_pitch);
2701 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2703 ERR("Failed to allocate memory.\n");
2704 return;
2708 if (temporary_mem)
2710 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2711 checkGLcall("glBindBuffer");
2712 mem = temporary_mem;
2714 else if (bo)
2716 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2717 checkGLcall("glBindBuffer");
2718 mem = (uint8_t *)data->addr + bo->b.buffer_offset;
2720 else
2722 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2723 checkGLcall("glBindBuffer");
2724 mem = data->addr;
2727 if (texture_gl->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2729 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2730 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2732 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2733 checkGLcall("glGetCompressedTexImage");
2735 else
2737 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2738 texture_gl, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2740 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2741 checkGLcall("glGetTexImage");
2744 if (format_gl->f.download)
2746 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2747 wined3d_texture_get_level_width(&texture_gl->t, level),
2748 wined3d_texture_get_level_height(&texture_gl->t, level), 1);
2750 else if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2752 const BYTE *src_data;
2753 unsigned int h, y;
2754 BYTE *dst_data;
2755 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2756 * pitches, preventing us from using the texture pitch to box NPOT
2757 * textures. Instead, we repack the texture's CPU copy so that its
2758 * pitch equals bpp * width instead of bpp * pow2width.
2760 * Instead of boxing the texture:
2762 * │<── texture width ──>│ pow2 width ──>│
2763 * ├─────────────────────┼───────────────┼─
2764 * │111111111111111111111│ │ʌ
2765 * │222222222222222222222│ ││
2766 * │333333333333333333333│ padding │texture height
2767 * │444444444444444444444│ ││
2768 * │555555555555555555555│ │v
2769 * ├─────────────────────┘ ├─
2770 * │ │pow2 height
2771 * │ padding padding ││
2772 * │ │v
2773 * └─────────────────────────────────────┴─
2775 * we're repacking the data to the expected texture width
2777 * │<── texture width ──>│ pow2 width ──>│
2778 * ├─────────────────────┴───────────────┼─
2779 * │1111111111111111111112222222222222222│ʌ
2780 * │2222233333333333333333333344444444444││
2781 * │4444444444555555555555555555555 │texture height
2782 * │ ││
2783 * │ padding padding │v
2784 * │ ├─
2785 * │ │pow2 height
2786 * │ padding padding ││
2787 * │ │v
2788 * └─────────────────────────────────────┴─
2790 * == is the same as
2792 * │<── texture width ──>│
2793 * ├─────────────────────┼─
2794 * │111111111111111111111│ʌ
2795 * │222222222222222222222││
2796 * │333333333333333333333│texture height
2797 * │444444444444444444444││
2798 * │555555555555555555555│v
2799 * └─────────────────────┴─
2801 * This also means that any references to surface memory should work
2802 * with the data as if it were a standard texture with a NPOT width
2803 * instead of a texture boxed up to be a power-of-two texture. */
2804 src_data = mem;
2805 dst_data = data->addr;
2806 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2807 h = wined3d_texture_get_level_height(&texture_gl->t, level);
2808 for (y = 0; y < h; ++y)
2810 memcpy(dst_data, src_data, dst_row_pitch);
2811 src_data += src_row_pitch;
2812 dst_data += dst_row_pitch;
2815 else if (temporary_mem)
2817 unsigned int layer = sub_resource_idx / texture_gl->t.level_count;
2818 void *src_data = temporary_mem + layer * sub_resource->size;
2819 if (bo)
2821 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, bo->id));
2822 checkGLcall("glBindBuffer");
2823 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER,
2824 (GLintptr)data->addr + bo->b.buffer_offset, sub_resource->size, src_data));
2825 checkGLcall("glBufferSubData");
2827 else
2829 memcpy(data->addr, src_data, sub_resource->size);
2833 if (bo)
2835 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2836 wined3d_context_gl_reference_bo(context_gl, bo);
2837 checkGLcall("glBindBuffer");
2840 heap_free(temporary_mem);
2843 static void wined3d_texture_gl_download_data(struct wined3d_context *context,
2844 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
2845 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
2846 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
2847 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
2849 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
2850 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
2851 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2852 unsigned int src_level, src_width, src_height, src_depth;
2853 unsigned int src_row_pitch, src_slice_pitch;
2854 const struct wined3d_format_gl *format_gl;
2855 uint8_t *offset = dst_bo_addr->addr;
2856 struct wined3d_bo *dst_bo;
2857 BOOL srgb = FALSE;
2858 GLenum target;
2860 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
2861 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
2862 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
2863 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
2864 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
2866 if (src_location == WINED3D_LOCATION_TEXTURE_SRGB)
2868 srgb = TRUE;
2870 else if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
2872 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
2873 return;
2876 src_level = src_sub_resource_idx % src_texture->level_count;
2877 src_width = wined3d_texture_get_level_width(src_texture, src_level);
2878 src_height = wined3d_texture_get_level_height(src_texture, src_level);
2879 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
2880 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
2881 || src_box->front || src_box->back != src_depth)
2883 FIXME("Unhandled source box %s.\n", debug_box(src_box));
2884 return;
2887 if (dst_x || dst_y || dst_z)
2889 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
2890 return;
2893 if (dst_format->id != src_texture->resource.format->id)
2895 FIXME("Unhandled format conversion (%s -> %s).\n",
2896 debug_d3dformat(src_texture->resource.format->id),
2897 debug_d3dformat(dst_format->id));
2898 return;
2901 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
2902 if (src_row_pitch != dst_row_pitch || src_slice_pitch != dst_slice_pitch)
2904 FIXME("Unhandled destination pitches %u/%u (source pitches %u/%u).\n",
2905 dst_row_pitch, dst_slice_pitch, src_row_pitch, src_slice_pitch);
2906 return;
2909 wined3d_texture_gl_bind_and_dirtify(src_texture_gl, context_gl, srgb);
2911 format_gl = wined3d_format_gl(src_texture->resource.format);
2912 target = wined3d_texture_gl_get_sub_resource_target(src_texture_gl, src_sub_resource_idx);
2914 if ((src_texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2915 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2916 || src_texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2917 || target == GL_TEXTURE_1D_ARRAY)
2919 wined3d_texture_gl_download_data_slow_path(src_texture_gl, src_sub_resource_idx, context_gl, dst_bo_addr);
2920 return;
2923 if (format_gl->f.conv_byte_count)
2925 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2926 debug_d3dresourcetype(src_texture->resource.type),
2927 debug_d3dformat(format_gl->f.id));
2928 return;
2931 if ((dst_bo = dst_bo_addr->buffer_object))
2933 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, wined3d_bo_gl(dst_bo)->id));
2934 checkGLcall("glBindBuffer");
2935 offset += dst_bo->buffer_offset;
2937 else
2939 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2940 checkGLcall("glBindBuffer");
2943 if (src_texture->resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
2945 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2946 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
2948 GL_EXTCALL(glGetCompressedTexImage(target, src_level, offset));
2949 checkGLcall("glGetCompressedTexImage");
2951 else
2953 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2954 src_texture, src_sub_resource_idx, src_level, format_gl->format, format_gl->type, offset);
2956 gl_info->gl_ops.gl.p_glGetTexImage(target, src_level, format_gl->format, format_gl->type, offset);
2957 checkGLcall("glGetTexImage");
2960 if (dst_bo)
2962 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2963 wined3d_context_gl_reference_bo(context_gl, wined3d_bo_gl(dst_bo));
2964 checkGLcall("glBindBuffer");
2968 /* Context activation is done by the caller. */
2969 static BOOL wined3d_texture_gl_load_sysmem(struct wined3d_texture_gl *texture_gl,
2970 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, DWORD dst_location)
2972 struct wined3d_texture_sub_resource *sub_resource;
2974 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2976 /* We cannot download data from multisample textures directly. */
2977 if (wined3d_texture_gl_is_multisample_location(texture_gl, WINED3D_LOCATION_TEXTURE_RGB))
2979 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_RB_RESOLVED);
2980 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
2981 WINED3D_LOCATION_RB_RESOLVED, dst_location);
2982 return TRUE;
2985 if (sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED))
2986 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
2988 /* Download the sub-resource to system memory. */
2989 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2991 unsigned int row_pitch, slice_pitch, level;
2992 struct wined3d_bo_address data;
2993 struct wined3d_box src_box;
2994 unsigned int src_location;
2996 level = sub_resource_idx % texture_gl->t.level_count;
2997 wined3d_texture_get_bo_address(&texture_gl->t, sub_resource_idx, &data, dst_location);
2998 src_location = sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB
2999 ? WINED3D_LOCATION_TEXTURE_RGB : WINED3D_LOCATION_TEXTURE_SRGB;
3000 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3001 wined3d_texture_get_pitch(&texture_gl->t, level, &row_pitch, &slice_pitch);
3002 wined3d_texture_gl_download_data(&context_gl->c, &texture_gl->t, sub_resource_idx, src_location,
3003 &src_box, &data, texture_gl->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
3005 ++texture_gl->t.download_count;
3006 return TRUE;
3009 if (!(texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3010 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3012 texture2d_read_from_framebuffer(&texture_gl->t, sub_resource_idx, &context_gl->c,
3013 texture_gl->t.resource.draw_binding, dst_location);
3014 return TRUE;
3017 FIXME("Can't load texture %p, %u with location flags %s into sysmem.\n",
3018 texture_gl, sub_resource_idx, wined3d_debug_location(sub_resource->locations));
3020 return FALSE;
3023 static BOOL wined3d_texture_load_drawable(struct wined3d_texture *texture,
3024 unsigned int sub_resource_idx, struct wined3d_context *context)
3026 struct wined3d_device *device;
3027 unsigned int level;
3028 RECT r;
3030 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3032 DWORD current = texture->sub_resources[sub_resource_idx].locations;
3033 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3034 wined3d_debug_location(current));
3035 return FALSE;
3038 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
3039 && wined3d_resource_is_offscreen(&texture->resource))
3041 ERR("Trying to load offscreen texture into WINED3D_LOCATION_DRAWABLE.\n");
3042 return FALSE;
3045 device = texture->resource.device;
3046 level = sub_resource_idx % texture->level_count;
3047 SetRect(&r, 0, 0, wined3d_texture_get_level_width(texture, level),
3048 wined3d_texture_get_level_height(texture, level));
3049 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3050 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_COLOR_BLIT, context,
3051 texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &r,
3052 texture, sub_resource_idx, WINED3D_LOCATION_DRAWABLE, &r,
3053 NULL, WINED3D_TEXF_POINT, NULL);
3055 return TRUE;
3058 static BOOL wined3d_texture_load_renderbuffer(struct wined3d_texture *texture,
3059 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD dst_location)
3061 unsigned int level = sub_resource_idx % texture->level_count;
3062 const RECT rect = {0, 0,
3063 wined3d_texture_get_level_width(texture, level),
3064 wined3d_texture_get_level_height(texture, level)};
3065 struct wined3d_texture_sub_resource *sub_resource;
3066 DWORD src_location, locations;
3068 sub_resource = &texture->sub_resources[sub_resource_idx];
3069 locations = sub_resource->locations;
3070 if (texture->resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL)
3072 FIXME("Unimplemented copy from %s for depth/stencil buffers.\n",
3073 wined3d_debug_location(locations));
3074 return FALSE;
3077 if (locations & WINED3D_LOCATION_RB_MULTISAMPLE)
3078 src_location = WINED3D_LOCATION_RB_MULTISAMPLE;
3079 else if (locations & WINED3D_LOCATION_RB_RESOLVED)
3080 src_location = WINED3D_LOCATION_RB_RESOLVED;
3081 else if (locations & WINED3D_LOCATION_TEXTURE_SRGB)
3082 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
3083 else if (locations & WINED3D_LOCATION_TEXTURE_RGB)
3084 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3085 else if (locations & WINED3D_LOCATION_DRAWABLE)
3086 src_location = WINED3D_LOCATION_DRAWABLE;
3087 else /* texture2d_blt_fbo() will load the source location if necessary. */
3088 src_location = WINED3D_LOCATION_TEXTURE_RGB;
3090 texture2d_blt_fbo(texture->resource.device, context, WINED3D_TEXF_POINT, texture,
3091 sub_resource_idx, src_location, &rect, texture, sub_resource_idx, dst_location, &rect, NULL);
3093 return TRUE;
3096 static BOOL wined3d_texture_gl_load_texture(struct wined3d_texture_gl *texture_gl,
3097 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, BOOL srgb)
3099 unsigned int width, height, level, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch;
3100 struct wined3d_device *device = texture_gl->t.resource.device;
3101 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3102 const struct wined3d_color_key_conversion *conversion;
3103 struct wined3d_texture_sub_resource *sub_resource;
3104 const struct wined3d_format *format;
3105 struct wined3d_bo_address data;
3106 BYTE *src_mem, *dst_mem = NULL;
3107 struct wined3d_box src_box;
3108 DWORD dst_location;
3109 BOOL depth;
3111 depth = texture_gl->t.resource.bind_flags & WINED3D_BIND_DEPTH_STENCIL;
3112 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
3114 if (!depth && wined3d_settings.offscreen_rendering_mode != ORM_FBO
3115 && wined3d_resource_is_offscreen(&texture_gl->t.resource)
3116 && (sub_resource->locations & WINED3D_LOCATION_DRAWABLE))
3118 texture2d_load_fb_texture(texture_gl, sub_resource_idx, srgb, &context_gl->c);
3120 return TRUE;
3123 level = sub_resource_idx % texture_gl->t.level_count;
3124 wined3d_texture_get_level_box(&texture_gl->t, level, &src_box);
3126 if (!depth && sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | WINED3D_LOCATION_TEXTURE_RGB)
3127 && (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)
3128 && fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3129 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_RGB,
3130 &texture_gl->t.resource, WINED3D_LOCATION_TEXTURE_SRGB))
3132 RECT src_rect;
3134 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3135 if (srgb)
3136 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3137 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect,
3138 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect, NULL);
3139 else
3140 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT,
3141 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_SRGB, &src_rect,
3142 &texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, &src_rect, NULL);
3144 return TRUE;
3147 if (!depth && sub_resource->locations & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED)
3148 && (!srgb || (texture_gl->t.resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB)))
3150 DWORD src_location = sub_resource->locations & WINED3D_LOCATION_RB_RESOLVED ?
3151 WINED3D_LOCATION_RB_RESOLVED : WINED3D_LOCATION_RB_MULTISAMPLE;
3152 RECT src_rect;
3154 SetRect(&src_rect, src_box.left, src_box.top, src_box.right, src_box.bottom);
3155 dst_location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
3156 if (fbo_blitter_supported(WINED3D_BLIT_OP_COLOR_BLIT, gl_info,
3157 &texture_gl->t.resource, src_location, &texture_gl->t.resource, dst_location))
3158 texture2d_blt_fbo(device, &context_gl->c, WINED3D_TEXF_POINT, &texture_gl->t, sub_resource_idx,
3159 src_location, &src_rect, &texture_gl->t, sub_resource_idx, dst_location, &src_rect, NULL);
3161 return TRUE;
3164 /* Upload from system memory */
3166 if (srgb)
3168 dst_location = WINED3D_LOCATION_TEXTURE_SRGB;
3169 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | texture_gl->t.resource.map_binding))
3170 == WINED3D_LOCATION_TEXTURE_RGB)
3172 FIXME_(d3d_perf)("Downloading RGB texture %p, %u to reload it as sRGB.\n", texture_gl, sub_resource_idx);
3173 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3174 &context_gl->c, texture_gl->t.resource.map_binding);
3177 else
3179 dst_location = WINED3D_LOCATION_TEXTURE_RGB;
3180 if ((sub_resource->locations & (WINED3D_LOCATION_TEXTURE_SRGB | texture_gl->t.resource.map_binding))
3181 == WINED3D_LOCATION_TEXTURE_SRGB)
3183 FIXME_(d3d_perf)("Downloading sRGB texture %p, %u to reload it as RGB.\n", texture_gl, sub_resource_idx);
3184 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx,
3185 &context_gl->c, texture_gl->t.resource.map_binding);
3189 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
3191 WARN("Trying to load a texture from sysmem, but no simple location is valid.\n");
3192 /* Lets hope we get it from somewhere... */
3193 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3196 wined3d_texture_get_pitch(&texture_gl->t, level, &src_row_pitch, &src_slice_pitch);
3198 format = texture_gl->t.resource.format;
3199 if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
3200 format = wined3d_get_format(device->adapter, conversion->dst_format, texture_gl->t.resource.bind_flags);
3202 /* Don't use PBOs for converted surfaces. During PBO conversion we look at
3203 * WINED3D_TEXTURE_CONVERTED but it isn't set (yet) in all cases it is
3204 * getting called. */
3205 if (conversion && sub_resource->bo)
3207 TRACE("Removing the pbo attached to texture %p, %u.\n", texture_gl, sub_resource_idx);
3209 wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM);
3210 wined3d_texture_set_map_binding(&texture_gl->t, WINED3D_LOCATION_SYSMEM);
3213 wined3d_texture_get_memory(&texture_gl->t, sub_resource_idx, &context_gl->c, &data);
3214 if (conversion)
3216 width = src_box.right - src_box.left;
3217 height = src_box.bottom - src_box.top;
3218 wined3d_format_calculate_pitch(format, device->surface_alignment,
3219 width, height, &dst_row_pitch, &dst_slice_pitch);
3221 src_mem = wined3d_context_gl_map_bo_address(context_gl, &data, src_slice_pitch, WINED3D_MAP_READ);
3222 if (!(dst_mem = heap_alloc(dst_slice_pitch)))
3224 ERR("Out of memory (%u).\n", dst_slice_pitch);
3225 return FALSE;
3227 conversion->convert(src_mem, src_row_pitch, dst_mem, dst_row_pitch,
3228 width, height, &texture_gl->t.async.gl_color_key);
3229 src_row_pitch = dst_row_pitch;
3230 src_slice_pitch = dst_slice_pitch;
3231 wined3d_context_gl_unmap_bo_address(context_gl, &data, 0, NULL);
3233 data.buffer_object = 0;
3234 data.addr = dst_mem;
3237 wined3d_texture_gl_upload_data(&context_gl->c, wined3d_const_bo_address(&data), format, &src_box,
3238 src_row_pitch, src_slice_pitch, &texture_gl->t, sub_resource_idx, dst_location, 0, 0, 0);
3240 heap_free(dst_mem);
3242 return TRUE;
3245 static BOOL wined3d_texture_gl_prepare_location(struct wined3d_texture *texture,
3246 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
3248 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3249 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3251 switch (location)
3253 case WINED3D_LOCATION_SYSMEM:
3254 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
3255 : wined3d_resource_prepare_sysmem(&texture->resource);
3257 case WINED3D_LOCATION_BUFFER:
3258 wined3d_texture_gl_prepare_buffer_object(texture_gl, sub_resource_idx, context_gl);
3259 return TRUE;
3261 case WINED3D_LOCATION_TEXTURE_RGB:
3262 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
3263 return TRUE;
3265 case WINED3D_LOCATION_TEXTURE_SRGB:
3266 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, TRUE);
3267 return TRUE;
3269 case WINED3D_LOCATION_DRAWABLE:
3270 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
3271 ERR("Texture %p does not have a drawable.\n", texture);
3272 return TRUE;
3274 case WINED3D_LOCATION_RB_MULTISAMPLE:
3275 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, TRUE);
3276 return TRUE;
3278 case WINED3D_LOCATION_RB_RESOLVED:
3279 wined3d_texture_gl_prepare_rb(texture_gl, context_gl->gl_info, FALSE);
3280 return TRUE;
3282 default:
3283 ERR("Invalid location %s.\n", wined3d_debug_location(location));
3284 return FALSE;
3288 static bool use_ffp_clear(const struct wined3d_texture *texture, unsigned int location)
3290 if (location == WINED3D_LOCATION_DRAWABLE)
3291 return true;
3293 /* If we are not using FBOs (and not rendering to the drawable), always
3294 * upload. The upload should always succeed in this case; we cannot have
3295 * ARB_texture_multisample without ARB_framebuffer_object. */
3296 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO)
3297 return false;
3299 if (location == WINED3D_LOCATION_TEXTURE_RGB
3300 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))
3301 return false;
3302 if (location == WINED3D_LOCATION_TEXTURE_SRGB
3303 && !(texture->resource.format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE_SRGB))
3304 return false;
3306 return location & (WINED3D_LOCATION_RB_MULTISAMPLE | WINED3D_LOCATION_RB_RESOLVED
3307 | WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
3310 static bool wined3d_texture_gl_clear(struct wined3d_texture *texture,
3311 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl, unsigned int location)
3313 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3314 const struct wined3d_format *format = texture->resource.format;
3315 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
3316 struct wined3d_bo_address addr;
3318 /* The code that delays clears is Vulkan-specific, so here we should only
3319 * encounter WINED3D_LOCATION_CLEARED on newly created resources and thus
3320 * a zero clear value. */
3321 if (!format->depth_size && !format->stencil_size)
3323 if (sub_resource->clear_value.colour.r || sub_resource->clear_value.colour.g
3324 || sub_resource->clear_value.colour.b || sub_resource->clear_value.colour.a)
3326 ERR("Unexpected color clear value r=%08e, g=%08e, b=%08e, a=%08e.\n",
3327 sub_resource->clear_value.colour.r, sub_resource->clear_value.colour.g,
3328 sub_resource->clear_value.colour.b, sub_resource->clear_value.colour.a);
3331 else
3333 if (format->depth_size && sub_resource->clear_value.depth)
3334 ERR("Unexpected depth clear value %08e.\n", sub_resource->clear_value.depth);
3335 if (format->stencil_size && sub_resource->clear_value.stencil)
3336 ERR("Unexpected stencil clear value %x.\n", sub_resource->clear_value.stencil);
3339 if (use_ffp_clear(texture, location))
3341 GLbitfield clear_mask = 0;
3343 context_gl_apply_texture_draw_state(context_gl, texture, sub_resource_idx, location);
3345 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
3346 context_invalidate_state(&context_gl->c, STATE_RASTERIZER);
3348 if (format->depth_size)
3350 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
3351 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3353 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
3354 GL_EXTCALL(glClearDepthf(0.0f));
3355 else
3356 gl_info->gl_ops.gl.p_glClearDepth(0.0);
3357 clear_mask |= GL_DEPTH_BUFFER_BIT;
3360 if (format->stencil_size)
3362 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
3363 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3364 gl_info->gl_ops.gl.p_glStencilMask(~0u);
3365 context_invalidate_state(&context_gl->c, STATE_DEPTH_STENCIL);
3366 gl_info->gl_ops.gl.p_glClearStencil(0);
3367 clear_mask |= GL_STENCIL_BUFFER_BIT;
3370 if (!format->depth_size && !format->stencil_size)
3372 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3373 context_invalidate_state(&context_gl->c, STATE_BLEND);
3374 gl_info->gl_ops.gl.p_glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
3375 clear_mask |= GL_COLOR_BUFFER_BIT;
3378 gl_info->gl_ops.gl.p_glClear(clear_mask);
3379 checkGLcall("clear texture");
3381 wined3d_texture_validate_location(texture, sub_resource_idx, location);
3382 return true;
3385 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, &context_gl->c, WINED3D_LOCATION_SYSMEM))
3386 return false;
3387 wined3d_texture_get_bo_address(texture, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
3388 memset(addr.addr, 0, sub_resource->size);
3389 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
3390 return true;
3393 /* Context activation is done by the caller. */
3394 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3395 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
3397 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3398 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3399 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3401 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3402 texture, sub_resource_idx, context, wined3d_debug_location(location));
3404 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3405 return FALSE;
3407 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
3409 if (!wined3d_texture_gl_clear(texture, sub_resource_idx, context_gl, location))
3410 return FALSE;
3412 if (sub_resource->locations & location)
3413 return TRUE;
3416 switch (location)
3418 case WINED3D_LOCATION_SYSMEM:
3419 case WINED3D_LOCATION_BUFFER:
3420 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3422 case WINED3D_LOCATION_DRAWABLE:
3423 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3425 case WINED3D_LOCATION_RB_RESOLVED:
3426 case WINED3D_LOCATION_RB_MULTISAMPLE:
3427 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3429 case WINED3D_LOCATION_TEXTURE_RGB:
3430 case WINED3D_LOCATION_TEXTURE_SRGB:
3431 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3432 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3434 default:
3435 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3436 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3437 return FALSE;
3441 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3442 struct wined3d_context *context, unsigned int location)
3444 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3445 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3446 struct wined3d_renderbuffer_entry *entry, *entry2;
3447 unsigned int i, sub_count;
3449 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3451 switch (location)
3453 case WINED3D_LOCATION_BUFFER:
3454 sub_count = texture->level_count * texture->layer_count;
3455 for (i = 0; i < sub_count; ++i)
3457 if (texture_gl->t.sub_resources[i].bo)
3458 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3460 break;
3462 case WINED3D_LOCATION_TEXTURE_RGB:
3463 if (texture_gl->texture_rgb.name)
3464 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3465 break;
3467 case WINED3D_LOCATION_TEXTURE_SRGB:
3468 if (texture_gl->texture_srgb.name)
3469 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3470 break;
3472 case WINED3D_LOCATION_RB_MULTISAMPLE:
3473 if (texture_gl->rb_multisample)
3475 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3476 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3477 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3478 texture_gl->rb_multisample = 0;
3480 break;
3482 case WINED3D_LOCATION_RB_RESOLVED:
3483 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3484 struct wined3d_renderbuffer_entry, entry)
3486 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3487 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3488 list_remove(&entry->entry);
3489 heap_free(entry);
3491 list_init(&texture_gl->renderbuffers);
3492 texture_gl->current_renderbuffer = NULL;
3494 if (texture_gl->rb_resolved)
3496 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3497 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3498 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3499 texture_gl->rb_resolved = 0;
3501 break;
3503 default:
3504 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3505 break;
3509 static const struct wined3d_texture_ops texture_gl_ops =
3511 wined3d_texture_gl_prepare_location,
3512 wined3d_texture_gl_load_location,
3513 wined3d_texture_gl_unload_location,
3514 wined3d_texture_gl_upload_data,
3515 wined3d_texture_gl_download_data,
3518 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3520 return texture_from_resource(resource);
3523 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3525 return wined3d_texture_incref(texture_from_resource(resource));
3528 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3530 return wined3d_texture_decref(texture_from_resource(resource));
3533 static void texture_resource_preload(struct wined3d_resource *resource)
3535 struct wined3d_texture *texture = texture_from_resource(resource);
3536 struct wined3d_context *context;
3538 context = context_acquire(resource->device, NULL, 0);
3539 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3540 context_release(context);
3543 static void texture_resource_unload(struct wined3d_resource *resource)
3545 struct wined3d_texture *texture = texture_from_resource(resource);
3546 struct wined3d_device *device = resource->device;
3547 unsigned int location = resource->map_binding;
3548 struct wined3d_context *context;
3549 unsigned int sub_count, i;
3551 TRACE("resource %p.\n", resource);
3553 /* D3D is not initialised, so no GPU locations should currently exist.
3554 * Moreover, we may not be able to acquire a valid context. */
3555 if (!device->d3d_initialized)
3556 return;
3558 context = context_acquire(device, NULL, 0);
3560 if (location == WINED3D_LOCATION_BUFFER)
3561 location = WINED3D_LOCATION_SYSMEM;
3563 sub_count = texture->level_count * texture->layer_count;
3564 for (i = 0; i < sub_count; ++i)
3566 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3567 && wined3d_texture_load_location(texture, i, context, location))
3569 wined3d_texture_invalidate_location(texture, i, ~location);
3571 else
3573 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3574 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3575 debug_d3dresourcetype(resource->type), resource, i,
3576 wined3d_debug_resource_access(resource->access));
3577 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3578 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3582 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3583 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3584 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3585 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3586 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3588 context_release(context);
3590 wined3d_texture_force_reload(texture);
3591 if (texture->resource.bind_count)
3592 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3593 wined3d_texture_set_dirty(texture);
3595 resource_unload(&texture->resource);
3598 static HRESULT texture_resource_sub_resource_get_desc(struct wined3d_resource *resource,
3599 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3601 const struct wined3d_texture *texture = texture_from_resource(resource);
3603 return wined3d_texture_get_sub_resource_desc(texture, sub_resource_idx, desc);
3606 static void texture_resource_sub_resource_get_map_pitch(struct wined3d_resource *resource,
3607 unsigned int sub_resource_idx, unsigned int *row_pitch, unsigned int *slice_pitch)
3609 const struct wined3d_texture *texture = texture_from_resource(resource);
3610 unsigned int level = sub_resource_idx % texture->level_count;
3612 if (resource->format_attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3614 *row_pitch = wined3d_texture_get_level_width(texture, level) * resource->format->byte_count;
3615 *slice_pitch = wined3d_texture_get_level_height(texture, level) * (*row_pitch);
3617 else
3619 wined3d_texture_get_pitch(texture, level, row_pitch, slice_pitch);
3623 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3624 void **map_ptr, const struct wined3d_box *box, uint32_t flags)
3626 struct wined3d_texture_sub_resource *sub_resource;
3627 struct wined3d_device *device = resource->device;
3628 struct wined3d_context *context;
3629 struct wined3d_texture *texture;
3630 struct wined3d_bo_address data;
3631 unsigned int texture_level;
3632 BYTE *base_memory;
3633 BOOL ret = TRUE;
3635 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
3636 resource, sub_resource_idx, map_ptr, debug_box(box), flags);
3638 texture = texture_from_resource(resource);
3639 sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx);
3641 texture_level = sub_resource_idx % texture->level_count;
3643 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3645 WARN("DC is in use.\n");
3646 return WINED3DERR_INVALIDCALL;
3649 if (sub_resource->map_count)
3651 WARN("Sub-resource is already mapped.\n");
3652 return WINED3DERR_INVALIDCALL;
3655 context = context_acquire(device, NULL, 0);
3657 if (flags & WINED3D_MAP_DISCARD)
3659 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3660 wined3d_debug_location(resource->map_binding));
3661 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3662 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3664 else
3666 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3667 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3668 if (!texture_level)
3670 unsigned int i;
3672 for (i = 0; i < texture->level_count; ++i)
3674 if (!(ret = wined3d_texture_load_location(texture, sub_resource_idx + i, context, resource->map_binding)))
3675 break;
3678 else
3680 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3684 if (!ret)
3686 ERR("Failed to prepare location.\n");
3687 context_release(context);
3688 return E_OUTOFMEMORY;
3691 /* We only record dirty regions for the top-most level. */
3692 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3693 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3694 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3696 if (flags & WINED3D_MAP_WRITE)
3698 if (!texture_level)
3700 unsigned int i;
3702 for (i = 0; i < texture->level_count; ++i)
3703 wined3d_texture_invalidate_location(texture, sub_resource_idx + i, ~resource->map_binding);
3705 else
3707 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3711 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, resource->map_binding);
3712 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3713 sub_resource->map_flags = flags;
3714 TRACE("Base memory pointer %p.\n", base_memory);
3716 context_release(context);
3718 *map_ptr = resource_offset_map_pointer(resource, sub_resource_idx, base_memory, box);
3720 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3722 RECT *r = &texture->swapchain->front_buffer_update;
3724 SetRect(r, box->left, box->top, box->right, box->bottom);
3725 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3728 ++resource->map_count;
3729 ++sub_resource->map_count;
3731 TRACE("Returning memory %p.\n", *map_ptr);
3733 return WINED3D_OK;
3736 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3738 struct wined3d_texture_sub_resource *sub_resource;
3739 struct wined3d_device *device = resource->device;
3740 struct wined3d_context *context;
3741 struct wined3d_texture *texture;
3742 struct wined3d_bo_address data;
3743 struct wined3d_range range;
3745 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3747 texture = texture_from_resource(resource);
3748 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3749 return E_INVALIDARG;
3751 if (!sub_resource->map_count)
3753 WARN("Trying to unmap unmapped sub-resource.\n");
3754 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3755 return WINED3D_OK;
3756 return WINEDDERR_NOTLOCKED;
3759 context = context_acquire(device, NULL, 0);
3761 wined3d_texture_get_bo_address(texture, sub_resource_idx, &data, texture->resource.map_binding);
3762 range.offset = 0;
3763 range.size = sub_resource->size;
3764 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3766 context_release(context);
3768 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3770 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3771 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3774 --sub_resource->map_count;
3775 if (!--resource->map_count && texture->update_map_binding)
3776 wined3d_texture_update_map_binding(texture);
3778 return WINED3D_OK;
3781 static const struct wined3d_resource_ops texture_resource_ops =
3783 texture_resource_incref,
3784 texture_resource_decref,
3785 texture_resource_preload,
3786 texture_resource_unload,
3787 texture_resource_sub_resource_get_desc,
3788 texture_resource_sub_resource_get_map_pitch,
3789 texture_resource_sub_resource_map,
3790 texture_resource_sub_resource_unmap,
3793 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3794 unsigned int layer_count, unsigned int level_count, uint32_t flags, struct wined3d_device *device,
3795 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3796 const struct wined3d_texture_ops *texture_ops)
3798 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3799 struct wined3d_device_parent *device_parent = device->device_parent;
3800 unsigned int sub_count, i, j, size, offset = 0;
3801 unsigned int pow2_width, pow2_height;
3802 const struct wined3d_format *format;
3803 HRESULT hr;
3805 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3806 "usage %s, bind_flags %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3807 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3808 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format), desc->multisample_type,
3809 desc->multisample_quality, debug_d3dusage(desc->usage), wined3d_debug_bind_flags(desc->bind_flags),
3810 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3811 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3813 if (!desc->width || !desc->height || !desc->depth)
3814 return WINED3DERR_INVALIDCALL;
3816 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3818 ERR("Invalid layer count for volume texture.\n");
3819 return E_INVALIDARG;
3822 texture->sub_resources = sub_resources;
3824 /* TODO: It should only be possible to create textures for formats
3825 * that are reported as supported. */
3826 if (WINED3DFMT_UNKNOWN >= desc->format)
3828 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3829 return WINED3DERR_INVALIDCALL;
3831 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3833 if ((desc->usage & WINED3DUSAGE_DYNAMIC) && (desc->usage & (WINED3DUSAGE_MANAGED | WINED3DUSAGE_SCRATCH)))
3835 WARN("Attempted to create a dynamic texture with usage %s.\n", debug_d3dusage(desc->usage));
3836 return WINED3DERR_INVALIDCALL;
3839 pow2_width = desc->width;
3840 pow2_height = desc->height;
3841 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3842 && !d3d_info->texture_npot)
3844 /* level_count == 0 returns an error as well. */
3845 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3847 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3849 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3850 "texture without unconditional NPOT support.\n");
3851 return WINED3DERR_INVALIDCALL;
3854 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3856 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3858 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3860 /* TODO: Add support for non-power-of-two compressed textures. */
3861 if (format->attrs & (WINED3D_FORMAT_ATTR_COMPRESSED | WINED3D_FORMAT_ATTR_HEIGHT_SCALE))
3863 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3864 desc->width, desc->height);
3865 return WINED3DERR_NOTAVAILABLE;
3868 /* Find the nearest pow2 match. */
3869 pow2_width = pow2_height = 1;
3870 while (pow2_width < desc->width)
3871 pow2_width <<= 1;
3872 while (pow2_height < desc->height)
3873 pow2_height <<= 1;
3874 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3877 texture->pow2_width = pow2_width;
3878 texture->pow2_height = pow2_height;
3880 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3881 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3883 /* One of four options:
3884 * 1: Do the same as we do with NPOT and scale the texture. (Any
3885 * texture ops would require the texture to be scaled which is
3886 * potentially slow.)
3887 * 2: Set the texture to the maximum size (bad idea).
3888 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3889 * 4: Create the surface, but allow it to be used only for DirectDraw
3890 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3891 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3892 * the render target. */
3893 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3895 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3896 return WINED3DERR_NOTAVAILABLE;
3899 /* We should never use this surface in combination with OpenGL. */
3900 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3903 for (i = 0; i < layer_count; ++i)
3905 for (j = 0; j < level_count; ++j)
3907 unsigned int idx = i * level_count + j;
3909 size = wined3d_format_calculate_size(format, device->surface_alignment,
3910 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3911 texture->sub_resources[idx].offset = offset;
3912 texture->sub_resources[idx].size = size;
3913 offset += size;
3915 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3918 if (!offset)
3919 return WINED3DERR_INVALIDCALL;
3921 /* Ensure the last mip-level is at least large enough to hold a single
3922 * compressed block. It is questionable how useful these mip-levels are to
3923 * the application with "broken pitch" formats, but we want to avoid
3924 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3925 if (format->attrs & WINED3D_FORMAT_ATTR_BROKEN_PITCH)
3927 unsigned int min_size;
3929 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3930 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3931 if (min_size > offset)
3932 offset = min_size;
3935 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3936 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3937 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3939 static unsigned int once;
3941 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3942 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3943 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3944 && !(format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_TEXTURE)
3945 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3946 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3948 WARN("Failed to initialize resource, returning %#lx\n", hr);
3949 return hr;
3951 wined3d_resource_update_draw_binding(&texture->resource);
3953 texture->texture_ops = texture_ops;
3955 texture->layer_count = layer_count;
3956 texture->level_count = level_count;
3957 texture->lod = 0;
3958 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_DOWNLOADABLE;
3959 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3961 texture->flags |= WINED3D_TEXTURE_GET_DC_LENIENT;
3962 texture->resource.pin_sysmem = 1;
3964 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3965 texture->flags |= WINED3D_TEXTURE_GET_DC;
3966 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3967 texture->flags |= WINED3D_TEXTURE_DISCARD;
3968 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3970 if (!(texture->resource.format_caps & WINED3D_FORMAT_CAP_GEN_MIPMAP))
3971 WARN("Format doesn't support mipmaps generation, "
3972 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3973 else
3974 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3977 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS)
3979 if (!(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3981 wined3d_texture_cleanup_sync(texture);
3982 return E_OUTOFMEMORY;
3984 for (i = 0; i < texture->layer_count; ++i)
3985 wined3d_texture_dirty_region_add(texture, i, NULL);
3988 /* Precalculated scaling for 'faked' non power of two texture coords. */
3989 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3991 texture->pow2_matrix[0] = (float)desc->width;
3992 texture->pow2_matrix[5] = (float)desc->height;
3993 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3995 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3997 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3998 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3999 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
4001 else
4003 texture->pow2_matrix[0] = 1.0f;
4004 texture->pow2_matrix[5] = 1.0f;
4006 texture->pow2_matrix[10] = 1.0f;
4007 texture->pow2_matrix[15] = 1.0f;
4008 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
4010 if (wined3d_texture_use_pbo(texture, d3d_info))
4011 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
4013 sub_count = level_count * layer_count;
4014 if (sub_count / layer_count != level_count)
4016 wined3d_texture_cleanup_sync(texture);
4017 return E_OUTOFMEMORY;
4020 if (desc->usage & WINED3DUSAGE_OVERLAY)
4022 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
4024 wined3d_texture_cleanup_sync(texture);
4025 return E_OUTOFMEMORY;
4028 for (i = 0; i < sub_count; ++i)
4030 list_init(&texture->overlay_info[i].entry);
4031 list_init(&texture->overlay_info[i].overlays);
4035 /* Generate all sub-resources. */
4036 for (i = 0; i < sub_count; ++i)
4038 struct wined3d_texture_sub_resource *sub_resource;
4040 sub_resource = &texture->sub_resources[i];
4041 sub_resource->locations = WINED3D_LOCATION_CLEARED;
4043 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
4044 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
4046 WARN("Failed to create sub-resource parent, hr %#lx.\n", hr);
4047 sub_resource->parent = NULL;
4048 wined3d_texture_cleanup_sync(texture);
4049 return hr;
4052 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
4054 TRACE("Created sub-resource %u (level %u, layer %u).\n",
4055 i, i % texture->level_count, i / texture->level_count);
4057 if (desc->usage & WINED3DUSAGE_OWNDC)
4059 struct wined3d_texture_idx texture_idx = {texture, i};
4061 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4062 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4063 if (!texture->dc_info || !texture->dc_info[i].dc)
4065 wined3d_texture_cleanup_sync(texture);
4066 return WINED3DERR_INVALIDCALL;
4071 return WINED3D_OK;
4074 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
4075 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
4076 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
4077 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
4079 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
4080 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
4081 HRESULT hr;
4083 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
4084 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
4085 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
4086 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
4088 if (!wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx)
4089 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4090 return WINED3DERR_INVALIDCALL;
4092 if (!wined3d_texture_validate_sub_resource_idx(src_texture, src_sub_resource_idx)
4093 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4094 return WINED3DERR_INVALIDCALL;
4096 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
4097 && filter != WINED3D_TEXF_LINEAR)
4098 return WINED3DERR_INVALIDCALL;
4100 if (FAILED(hr = wined3d_resource_check_box_dimensions(&dst_texture->resource, dst_sub_resource_idx, &dst_box)))
4101 return hr;
4103 if (FAILED(hr = wined3d_resource_check_box_dimensions(&src_texture->resource, src_sub_resource_idx, &src_box)))
4104 return hr;
4106 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
4107 || src_texture->sub_resources[src_sub_resource_idx].map_count)
4109 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
4110 return WINEDDERR_SURFACEBUSY;
4113 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
4114 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
4116 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
4117 return WINED3DERR_INVALIDCALL;
4120 if (dst_texture->resource.device != src_texture->resource.device)
4122 FIXME("Rejecting cross-device blit.\n");
4123 return E_NOTIMPL;
4126 wined3d_device_context_emit_blt_sub_resource(context, &dst_texture->resource, dst_sub_resource_idx,
4127 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
4129 if (dst_texture->dirty_regions)
4130 wined3d_texture_add_dirty_region(dst_texture, dst_sub_resource_idx, &dst_box);
4132 return WINED3D_OK;
4135 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
4136 unsigned int sub_resource_idx, LONG *x, LONG *y)
4138 struct wined3d_overlay_info *overlay;
4140 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\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 if (!overlay->dst_texture)
4149 TRACE("Overlay not visible.\n");
4150 *x = 0;
4151 *y = 0;
4152 return WINEDDERR_OVERLAYNOTVISIBLE;
4155 *x = overlay->dst_rect.left;
4156 *y = overlay->dst_rect.top;
4158 TRACE("Returning position %ld, %ld.\n", *x, *y);
4160 return WINED3D_OK;
4163 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
4164 unsigned int sub_resource_idx, LONG x, LONG y)
4166 struct wined3d_overlay_info *overlay;
4167 LONG w, h;
4169 TRACE("texture %p, sub_resource_idx %u, x %ld, y %ld.\n", texture, sub_resource_idx, x, y);
4171 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4172 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4173 return WINEDDERR_NOTAOVERLAYSURFACE;
4175 overlay = &texture->overlay_info[sub_resource_idx];
4176 w = overlay->dst_rect.right - overlay->dst_rect.left;
4177 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
4178 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
4180 return WINED3D_OK;
4183 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4184 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4185 const RECT *dst_rect, uint32_t flags)
4187 struct wined3d_overlay_info *overlay;
4188 unsigned int level, dst_level;
4190 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
4191 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
4192 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
4193 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
4195 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4196 || !wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4197 return WINEDDERR_NOTAOVERLAYSURFACE;
4199 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4200 || !wined3d_texture_validate_sub_resource_idx(dst_texture, dst_sub_resource_idx))
4201 return WINED3DERR_INVALIDCALL;
4203 overlay = &texture->overlay_info[sub_resource_idx];
4205 level = sub_resource_idx % texture->level_count;
4206 if (src_rect)
4207 overlay->src_rect = *src_rect;
4208 else
4209 SetRect(&overlay->src_rect, 0, 0,
4210 wined3d_texture_get_level_width(texture, level),
4211 wined3d_texture_get_level_height(texture, level));
4213 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4214 if (dst_rect)
4215 overlay->dst_rect = *dst_rect;
4216 else
4217 SetRect(&overlay->dst_rect, 0, 0,
4218 wined3d_texture_get_level_width(dst_texture, dst_level),
4219 wined3d_texture_get_level_height(dst_texture, dst_level));
4221 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4222 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4224 overlay->dst_texture = NULL;
4225 list_remove(&overlay->entry);
4228 if (flags & WINEDDOVER_SHOW)
4230 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4232 overlay->dst_texture = dst_texture;
4233 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4234 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4237 else if (flags & WINEDDOVER_HIDE)
4239 /* Tests show that the rectangles are erased on hide. */
4240 SetRectEmpty(&overlay->src_rect);
4241 SetRectEmpty(&overlay->dst_rect);
4242 overlay->dst_texture = NULL;
4245 return WINED3D_OK;
4248 struct wined3d_swapchain * CDECL wined3d_texture_get_swapchain(struct wined3d_texture *texture)
4250 return texture->swapchain;
4253 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4255 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4257 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4258 return NULL;
4260 return texture->sub_resources[sub_resource_idx].parent;
4263 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4264 unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops)
4266 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4268 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4269 return;
4271 texture->sub_resources[sub_resource_idx].parent = parent;
4272 texture->sub_resources[sub_resource_idx].parent_ops = parent_ops;
4275 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4276 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4278 const struct wined3d_resource *resource;
4279 unsigned int level_idx;
4281 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4283 if (!wined3d_texture_validate_sub_resource_idx(texture, sub_resource_idx))
4284 return WINED3DERR_INVALIDCALL;
4286 resource = &texture->resource;
4287 desc->format = resource->format->id;
4288 desc->multisample_type = resource->multisample_type;
4289 desc->multisample_quality = resource->multisample_quality;
4290 desc->usage = resource->usage;
4291 desc->bind_flags = resource->bind_flags;
4292 desc->access = resource->access;
4294 level_idx = sub_resource_idx % texture->level_count;
4295 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4296 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4297 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4298 desc->size = texture->sub_resources[sub_resource_idx].size;
4300 return WINED3D_OK;
4303 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4304 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4305 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4307 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device->adapter)->gl_info;
4308 HRESULT hr;
4310 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4311 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4312 texture_gl, device, desc, layer_count,
4313 level_count, flags, parent, parent_ops);
4315 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4316 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4318 WARN("OpenGL implementation does not support array textures.\n");
4319 return WINED3DERR_INVALIDCALL;
4322 switch (desc->resource_type)
4324 case WINED3D_RTYPE_TEXTURE_1D:
4325 if (layer_count > 1)
4326 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4327 else
4328 texture_gl->target = GL_TEXTURE_1D;
4329 break;
4331 case WINED3D_RTYPE_TEXTURE_2D:
4332 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4334 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4336 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4338 if (layer_count > 1)
4339 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4340 else
4341 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4343 else
4345 if (layer_count > 1)
4346 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4347 else
4348 texture_gl->target = GL_TEXTURE_2D;
4350 break;
4352 case WINED3D_RTYPE_TEXTURE_3D:
4353 if (!gl_info->supported[EXT_TEXTURE3D])
4355 WARN("OpenGL implementation does not support 3D textures.\n");
4356 return WINED3DERR_INVALIDCALL;
4358 texture_gl->target = GL_TEXTURE_3D;
4359 break;
4361 default:
4362 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4363 return WINED3DERR_INVALIDCALL;
4366 list_init(&texture_gl->renderbuffers);
4368 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4369 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4370 return hr;
4372 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4373 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4375 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4376 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4378 return WINED3D_OK;
4381 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4382 UINT layer_count, UINT level_count, uint32_t flags, const struct wined3d_sub_resource_data *data,
4383 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4385 unsigned int sub_count = level_count * layer_count;
4386 unsigned int i;
4387 HRESULT hr;
4389 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4390 "parent %p, parent_ops %p, texture %p.\n",
4391 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4393 if (!layer_count)
4395 WARN("Invalid layer count.\n");
4396 return E_INVALIDARG;
4398 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4400 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4401 layer_count = 6;
4404 if (!level_count)
4406 WARN("Invalid level count.\n");
4407 return WINED3DERR_INVALIDCALL;
4410 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4412 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4414 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4415 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4417 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4418 desc->multisample_quality);
4419 return WINED3DERR_NOTAVAILABLE;
4421 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4422 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4423 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4425 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4426 desc->multisample_quality);
4427 return WINED3DERR_NOTAVAILABLE;
4431 if (data)
4433 for (i = 0; i < sub_count; ++i)
4435 if (data[i].data)
4436 continue;
4438 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4439 return E_INVALIDARG;
4443 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4444 layer_count, level_count, flags, parent, parent_ops, texture)))
4445 return hr;
4447 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4448 * in this case. */
4449 if (data)
4451 struct wined3d_box box;
4453 for (i = 0; i < sub_count; ++i)
4455 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4456 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4457 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4461 TRACE("Created texture %p.\n", *texture);
4463 return WINED3D_OK;
4466 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4468 struct wined3d_device *device = texture->resource.device;
4469 struct wined3d_texture_sub_resource *sub_resource;
4470 struct wined3d_dc_info *dc_info;
4472 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4474 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4476 WARN("Texture does not support GetDC\n");
4477 /* Don't touch the DC */
4478 return WINED3DERR_INVALIDCALL;
4481 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4482 return WINED3DERR_INVALIDCALL;
4484 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4486 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4487 return WINED3DERR_INVALIDCALL;
4490 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4491 return WINED3DERR_INVALIDCALL;
4493 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4495 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4497 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4498 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4499 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4500 return WINED3DERR_INVALIDCALL;
4503 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4504 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4505 ++texture->resource.map_count;
4506 ++sub_resource->map_count;
4508 *dc = dc_info[sub_resource_idx].dc;
4509 TRACE("Returning dc %p.\n", *dc);
4511 return WINED3D_OK;
4514 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4516 struct wined3d_device *device = texture->resource.device;
4517 struct wined3d_texture_sub_resource *sub_resource;
4518 struct wined3d_dc_info *dc_info;
4520 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4522 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4523 return WINED3DERR_INVALIDCALL;
4525 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4527 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4528 return WINED3DERR_INVALIDCALL;
4531 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4532 return WINED3DERR_INVALIDCALL;
4534 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4536 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4537 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4538 return WINED3DERR_INVALIDCALL;
4541 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4543 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4545 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4546 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4549 --sub_resource->map_count;
4550 if (!--texture->resource.map_count && texture->update_map_binding)
4551 wined3d_texture_update_map_binding(texture);
4552 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4553 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4555 return WINED3D_OK;
4558 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4559 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4560 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4562 unsigned int src_row_pitch, src_slice_pitch;
4563 unsigned int update_w, update_h, update_d;
4564 unsigned int src_level, dst_level;
4565 struct wined3d_context *context;
4566 struct wined3d_bo_address data;
4568 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4569 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4570 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4571 src_texture, src_sub_resource_idx, debug_box(src_box));
4573 context = context_acquire(dst_texture->resource.device, NULL, 0);
4575 /* Only load the sub-resource for partial updates. For newly allocated
4576 * textures the texture wouldn't be the current location, and we'd upload
4577 * zeroes just to overwrite them again. */
4578 update_w = src_box->right - src_box->left;
4579 update_h = src_box->bottom - src_box->top;
4580 update_d = src_box->back - src_box->front;
4581 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4582 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4583 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4584 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4585 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4586 else
4587 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4589 src_level = src_sub_resource_idx % src_texture->level_count;
4590 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, context, &data);
4591 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4593 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4594 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4595 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4597 context_release(context);
4599 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4600 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4603 /* Partial downloads are not supported. */
4604 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4605 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4607 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4608 unsigned int dst_location = dst_texture->resource.map_binding;
4609 struct wined3d_context *context;
4610 struct wined3d_bo_address data;
4611 struct wined3d_box src_box;
4612 unsigned int src_location;
4614 context = context_acquire(src_texture->resource.device, NULL, 0);
4616 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4617 wined3d_texture_get_bo_address(dst_texture, dst_sub_resource_idx, &data, dst_location);
4619 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4620 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4621 else
4622 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4623 src_level = src_sub_resource_idx % src_texture->level_count;
4624 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4626 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4627 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4629 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4630 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4632 context_release(context);
4634 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4635 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4638 static void wined3d_texture_set_bo(struct wined3d_texture *texture,
4639 unsigned sub_resource_idx, struct wined3d_context *context, struct wined3d_bo *bo)
4641 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
4642 struct wined3d_bo *prev_bo = sub_resource->bo;
4644 TRACE("texture %p, sub_resource_idx %u, context %p, bo %p.\n", texture, sub_resource_idx, context, bo);
4646 if (prev_bo)
4648 struct wined3d_bo_user *bo_user;
4650 LIST_FOR_EACH_ENTRY(bo_user, &prev_bo->users, struct wined3d_bo_user, entry)
4651 bo_user->valid = false;
4652 list_init(&prev_bo->users);
4654 assert(list_empty(&bo->users));
4656 wined3d_context_destroy_bo(context, prev_bo);
4657 heap_free(prev_bo);
4660 sub_resource->bo = bo;
4663 void wined3d_texture_update_sub_resource(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4664 struct wined3d_context *context, const struct upload_bo *upload_bo, const struct wined3d_box *box,
4665 unsigned int row_pitch, unsigned int slice_pitch)
4667 unsigned int level = sub_resource_idx % texture->level_count;
4668 unsigned int width = wined3d_texture_get_level_width(texture, level);
4669 unsigned int height = wined3d_texture_get_level_height(texture, level);
4670 unsigned int depth = wined3d_texture_get_level_depth(texture, level);
4671 struct wined3d_box src_box;
4673 if (upload_bo->flags & UPLOAD_BO_RENAME_ON_UNMAP)
4675 wined3d_texture_set_bo(texture, sub_resource_idx, context, upload_bo->addr.buffer_object);
4676 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
4677 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_BUFFER);
4678 /* Try to free address space if we are not mapping persistently. */
4679 if (upload_bo->addr.buffer_object->map_ptr)
4680 wined3d_context_unmap_bo_address(context, (const struct wined3d_bo_address *)&upload_bo->addr, 0, NULL);
4683 /* Only load the sub-resource for partial updates. */
4684 if (!box->left && !box->top && !box->front
4685 && box->right == width && box->bottom == height && box->back == depth)
4686 wined3d_texture_prepare_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4687 else
4688 wined3d_texture_load_location(texture, sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4690 wined3d_box_set(&src_box, 0, 0, box->right - box->left, box->bottom - box->top, 0, box->back - box->front);
4691 texture->texture_ops->texture_upload_data(context, &upload_bo->addr, texture->resource.format,
4692 &src_box, row_pitch, slice_pitch, texture, sub_resource_idx,
4693 WINED3D_LOCATION_TEXTURE_RGB, box->left, box->top, box->front);
4695 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4696 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4699 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4700 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4701 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4702 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4703 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4705 FIXME("Not implemented.\n");
4708 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4709 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4710 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4711 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4712 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4714 FIXME("Not implemented.\n");
4717 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4718 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4720 if (location == WINED3D_LOCATION_SYSMEM)
4721 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4722 : wined3d_resource_prepare_sysmem(&texture->resource);
4724 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4725 return FALSE;
4728 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4729 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
4731 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4732 texture, sub_resource_idx, context, wined3d_debug_location(location));
4734 if (location == WINED3D_LOCATION_SYSMEM)
4735 return TRUE;
4737 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4739 return FALSE;
4742 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4743 struct wined3d_context *context, unsigned int location)
4745 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4748 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4750 wined3d_texture_no3d_prepare_location,
4751 wined3d_texture_no3d_load_location,
4752 wined3d_texture_no3d_unload_location,
4753 wined3d_texture_no3d_upload_data,
4754 wined3d_texture_no3d_download_data,
4757 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4758 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4759 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4761 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4762 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4763 texture_no3d, device, desc, layer_count,
4764 level_count, flags, parent, parent_ops);
4766 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4767 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4770 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4772 static const VkComponentSwizzle swizzle_source[] =
4774 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4775 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4776 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4777 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4778 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4779 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4782 mapping->r = swizzle_source[fixup.x_source];
4783 mapping->g = swizzle_source[fixup.y_source];
4784 mapping->b = swizzle_source[fixup.z_source];
4785 mapping->a = swizzle_source[fixup.w_source];
4788 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4789 struct wined3d_context_vk *context_vk)
4791 const struct wined3d_format_vk *format_vk;
4792 const struct wined3d_vk_info *vk_info;
4793 struct wined3d_device_vk *device_vk;
4794 VkImageViewCreateInfo create_info;
4795 struct color_fixup_desc fixup;
4796 uint32_t flags = 0;
4797 VkResult vr;
4799 if (texture_vk->default_image_info.imageView)
4800 return &texture_vk->default_image_info;
4802 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4803 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4804 vk_info = context_vk->vk_info;
4806 if (texture_vk->t.layer_count > 1)
4807 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4809 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4810 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4811 create_info.pNext = NULL;
4812 create_info.flags = 0;
4813 create_info.image = texture_vk->image.vk_image;
4814 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4815 create_info.format = format_vk->vk_format;
4816 fixup = format_vk->f.color_fixup;
4817 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4819 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4820 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4821 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4822 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4824 else
4826 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4828 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4829 create_info.subresourceRange.baseMipLevel = 0;
4830 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4831 create_info.subresourceRange.baseArrayLayer = 0;
4832 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4833 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4834 NULL, &texture_vk->default_image_info.imageView))) < 0)
4836 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4837 return NULL;
4840 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4842 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4844 /* The default image view is used for SRVs, UAVs and RTVs when the d3d view encompasses the entire
4845 * resource. Any UAV capable resource will always use VK_IMAGE_LAYOUT_GENERAL, so we can use the
4846 * same image info for SRVs and UAVs. For render targets wined3d_rendertarget_view_vk_get_image_view
4847 * only cares about the VkImageView, not entire image info. So using SHADER_READ_ONLY_OPTIMAL works,
4848 * but relies on what the callers of the function do and don't do with the descriptor we return.
4850 * Note that VkWriteDescriptorSet for SRV/UAV use takes a VkDescriptorImageInfo *, so we need a
4851 * place to store the VkDescriptorImageInfo. So returning onlky a VkImageView from this function
4852 * would bring its own problems. */
4853 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
4854 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
4855 else
4856 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
4858 return &texture_vk->default_image_info;
4861 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4862 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4863 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4864 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4865 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4867 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4868 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4869 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4870 struct wined3d_texture_sub_resource *sub_resource;
4871 unsigned int src_width, src_height, src_depth;
4872 struct wined3d_bo_address staging_bo_addr;
4873 VkPipelineStageFlags bo_stage_flags = 0;
4874 const struct wined3d_vk_info *vk_info;
4875 VkCommandBuffer vk_command_buffer;
4876 VkBufferMemoryBarrier vk_barrier;
4877 VkImageSubresourceRange vk_range;
4878 struct wined3d_bo_vk staging_bo;
4879 VkImageAspectFlags aspect_mask;
4880 struct wined3d_bo_vk *src_bo;
4881 struct wined3d_range range;
4882 VkBufferImageCopy region;
4883 size_t src_offset;
4884 void *map_ptr;
4886 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4887 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4888 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4889 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4890 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4892 if (src_format->id != dst_texture->resource.format->id)
4894 FIXME("Unhandled format conversion (%s -> %s).\n",
4895 debug_d3dformat(src_format->id),
4896 debug_d3dformat(dst_texture->resource.format->id));
4897 return;
4900 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4901 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4902 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4903 src_row_pitch = dst_row_pitch = 0;
4904 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4905 src_slice_pitch = dst_slice_pitch = 0;
4907 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4909 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4910 return;
4913 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4915 FIXME("Not supported for multisample textures.\n");
4916 return;
4919 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4920 if (wined3d_popcount(aspect_mask) > 1)
4922 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4923 return;
4926 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4927 vk_info = context_vk->vk_info;
4929 src_width = src_box->right - src_box->left;
4930 src_height = src_box->bottom - src_box->top;
4931 src_depth = src_box->back - src_box->front;
4933 src_offset = src_box->front * src_slice_pitch
4934 + (src_box->top / src_format->block_height) * src_row_pitch
4935 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4937 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4939 ERR("Failed to get command buffer.\n");
4940 return;
4943 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
4944 wined3d_context_vk_end_current_render_pass(context_vk);
4945 if (!src_bo_addr->buffer_object)
4947 unsigned int staging_row_pitch, staging_slice_pitch, staging_size;
4949 wined3d_format_calculate_pitch(src_format, context->device->surface_alignment, src_width, src_height,
4950 &staging_row_pitch, &staging_slice_pitch);
4951 staging_size = staging_slice_pitch * src_depth;
4953 if (!wined3d_context_vk_create_bo(context_vk, staging_size,
4954 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4956 ERR("Failed to create staging bo.\n");
4957 return;
4960 staging_bo_addr.buffer_object = &staging_bo.b;
4961 staging_bo_addr.addr = NULL;
4962 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4963 staging_size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4965 ERR("Failed to map staging bo.\n");
4966 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4967 return;
4970 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch, src_slice_pitch,
4971 map_ptr, staging_row_pitch, staging_slice_pitch, src_width, src_height, src_depth);
4973 range.offset = 0;
4974 range.size = staging_size;
4975 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4977 src_bo = &staging_bo;
4979 src_offset = 0;
4980 src_row_pitch = staging_row_pitch;
4981 src_slice_pitch = staging_slice_pitch;
4983 else
4985 src_bo = wined3d_bo_vk(src_bo_addr->buffer_object);
4987 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
4988 vk_barrier.pNext = NULL;
4989 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(src_bo->usage) & ~WINED3D_READ_ONLY_ACCESS_FLAGS;
4990 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
4991 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4992 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4993 vk_barrier.buffer = src_bo->vk_buffer;
4994 vk_barrier.offset = src_bo->b.buffer_offset + (size_t)src_bo_addr->addr;
4995 vk_barrier.size = sub_resource->size;
4997 src_offset += (size_t)src_bo_addr->addr;
4999 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(src_bo->usage);
5000 if (vk_barrier.srcAccessMask)
5001 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5002 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5005 vk_range.aspectMask = aspect_mask;
5006 vk_range.baseMipLevel = dst_level;
5007 vk_range.levelCount = 1;
5008 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
5009 vk_range.layerCount = 1;
5011 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5012 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5013 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5014 VK_ACCESS_TRANSFER_WRITE_BIT,
5015 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
5016 dst_texture_vk->image.vk_image, &vk_range);
5018 region.bufferOffset = src_bo->b.buffer_offset + src_offset;
5019 region.bufferRowLength = (src_row_pitch / src_format->block_byte_count) * src_format->block_width;
5020 if (src_row_pitch)
5021 region.bufferImageHeight = (src_slice_pitch / src_row_pitch) * src_format->block_height;
5022 else
5023 region.bufferImageHeight = 1;
5024 region.imageSubresource.aspectMask = vk_range.aspectMask;
5025 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5026 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5027 region.imageSubresource.layerCount = vk_range.layerCount;
5028 region.imageOffset.x = dst_x;
5029 region.imageOffset.y = dst_y;
5030 region.imageOffset.z = dst_z;
5031 region.imageExtent.width = src_width;
5032 region.imageExtent.height = src_height;
5033 region.imageExtent.depth = src_depth;
5035 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, src_bo->vk_buffer,
5036 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
5038 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5039 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5040 VK_ACCESS_TRANSFER_WRITE_BIT,
5041 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
5042 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
5043 dst_texture_vk->image.vk_image, &vk_range);
5044 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
5045 wined3d_context_vk_reference_bo(context_vk, src_bo);
5047 if (src_bo == &staging_bo)
5049 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5051 else if (vk_barrier.srcAccessMask)
5053 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5054 bo_stage_flags, 0, 0, NULL, 0, NULL, 0, NULL));
5058 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
5059 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
5060 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
5061 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
5062 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
5064 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
5065 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5066 unsigned int src_level, src_width, src_height, src_depth;
5067 struct wined3d_texture_sub_resource *sub_resource;
5068 unsigned int src_row_pitch, src_slice_pitch;
5069 struct wined3d_bo_address staging_bo_addr;
5070 VkPipelineStageFlags bo_stage_flags = 0;
5071 const struct wined3d_vk_info *vk_info;
5072 VkCommandBuffer vk_command_buffer;
5073 VkImageSubresourceRange vk_range;
5074 VkBufferMemoryBarrier vk_barrier;
5075 struct wined3d_bo_vk staging_bo;
5076 VkImageAspectFlags aspect_mask;
5077 struct wined3d_bo_vk *dst_bo;
5078 VkBufferImageCopy region;
5079 size_t dst_offset = 0;
5080 void *map_ptr;
5082 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
5083 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
5084 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
5085 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
5086 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
5088 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
5090 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
5091 return;
5094 src_level = src_sub_resource_idx % src_texture->level_count;
5095 src_width = wined3d_texture_get_level_width(src_texture, src_level);
5096 src_height = wined3d_texture_get_level_height(src_texture, src_level);
5097 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
5098 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
5099 || src_box->front || src_box->back != src_depth)
5101 FIXME("Unhandled source box %s.\n", debug_box(src_box));
5102 return;
5105 if (dst_format->id != src_texture->resource.format->id)
5107 FIXME("Unhandled format conversion (%s -> %s).\n",
5108 debug_d3dformat(src_texture->resource.format->id),
5109 debug_d3dformat(dst_format->id));
5110 return;
5113 if (dst_x || dst_y || dst_z)
5115 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
5116 return;
5119 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
5121 FIXME("Not supported for multisample textures.\n");
5122 return;
5125 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
5126 if (wined3d_popcount(aspect_mask) > 1)
5128 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
5129 return;
5132 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
5133 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
5134 src_row_pitch = dst_row_pitch = 0;
5135 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
5136 src_slice_pitch = dst_slice_pitch = 0;
5138 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
5139 vk_info = context_vk->vk_info;
5140 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5142 ERR("Failed to get command buffer.\n");
5143 return;
5146 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyImageToBuffer() calls below. */
5147 wined3d_context_vk_end_current_render_pass(context_vk);
5149 if (!dst_bo_addr->buffer_object)
5151 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5152 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
5154 ERR("Failed to create staging bo.\n");
5155 return;
5158 dst_bo = &staging_bo;
5160 else
5162 dst_bo = wined3d_bo_vk(dst_bo_addr->buffer_object);
5164 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
5165 vk_barrier.pNext = NULL;
5166 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(dst_bo->usage);
5167 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5168 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5169 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
5170 vk_barrier.buffer = dst_bo->vk_buffer;
5171 vk_barrier.offset = dst_bo->b.buffer_offset + (size_t)dst_bo_addr->addr;
5172 vk_barrier.size = sub_resource->size;
5174 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(dst_bo->usage);
5175 dst_offset = (size_t)dst_bo_addr->addr;
5177 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
5178 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5181 vk_range.aspectMask = aspect_mask;
5182 vk_range.baseMipLevel = src_level;
5183 vk_range.levelCount = 1;
5184 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
5185 vk_range.layerCount = 1;
5187 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5188 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5189 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5190 VK_ACCESS_TRANSFER_READ_BIT,
5191 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5192 src_texture_vk->image.vk_image, &vk_range);
5194 region.bufferOffset = dst_bo->b.buffer_offset + dst_offset;
5195 region.bufferRowLength = 0;
5196 region.bufferImageHeight = 0;
5197 region.imageSubresource.aspectMask = vk_range.aspectMask;
5198 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5199 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5200 region.imageSubresource.layerCount = vk_range.layerCount;
5201 region.imageOffset.x = 0;
5202 region.imageOffset.y = 0;
5203 region.imageOffset.z = 0;
5204 region.imageExtent.width = src_width;
5205 region.imageExtent.height = src_height;
5206 region.imageExtent.depth = src_depth;
5208 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
5209 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_bo->vk_buffer, 1, &region));
5211 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5212 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5213 VK_ACCESS_TRANSFER_READ_BIT,
5214 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5215 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
5216 src_texture_vk->image.vk_image, &vk_range);
5218 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
5219 wined3d_context_vk_reference_bo(context_vk, dst_bo);
5221 if (dst_bo == &staging_bo)
5223 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5224 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
5226 staging_bo_addr.buffer_object = &staging_bo.b;
5227 staging_bo_addr.addr = NULL;
5228 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5229 sub_resource->size, WINED3D_MAP_READ)))
5231 ERR("Failed to map staging bo.\n");
5232 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5233 return;
5236 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
5237 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
5238 src_box->bottom - src_box->top, src_box->back - src_box->front);
5240 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
5241 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5243 else
5245 vk_barrier.dstAccessMask = vk_barrier.srcAccessMask;
5246 vk_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5248 if (dst_bo->host_synced)
5250 vk_barrier.dstAccessMask |= VK_ACCESS_HOST_READ_BIT;
5251 bo_stage_flags |= VK_PIPELINE_STAGE_HOST_BIT;
5254 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5255 bo_stage_flags, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5256 /* Start the download so we don't stall waiting for the result. */
5257 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5261 static bool wined3d_texture_vk_clear(struct wined3d_texture_vk *texture_vk,
5262 unsigned int sub_resource_idx, struct wined3d_context *context)
5264 struct wined3d_texture_sub_resource *sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5265 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5266 const struct wined3d_format *format = texture_vk->t.resource.format;
5267 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
5268 VkClearDepthStencilValue depth_value;
5269 VkCommandBuffer vk_command_buffer;
5270 VkImageSubresourceRange vk_range;
5271 VkClearColorValue colour_value;
5272 VkImageAspectFlags aspect_mask;
5273 VkImage vk_image;
5275 if (texture_vk->t.resource.format_attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5277 struct wined3d_bo_address addr;
5278 struct wined3d_color *c = &sub_resource->clear_value.colour;
5280 if (c->r || c->g || c-> b || c->a)
5281 FIXME("Compressed resource %p is cleared to a non-zero color.\n", &texture_vk->t);
5283 if (!wined3d_texture_prepare_location(&texture_vk->t, sub_resource_idx, context, WINED3D_LOCATION_SYSMEM))
5284 return false;
5285 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &addr, WINED3D_LOCATION_SYSMEM);
5286 memset(addr.addr, 0, sub_resource->size);
5287 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
5288 return true;
5291 vk_image = texture_vk->image.vk_image;
5293 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5295 ERR("Failed to get command buffer.\n");
5296 return false;
5299 aspect_mask = vk_aspect_mask_from_format(format);
5301 vk_range.aspectMask = aspect_mask;
5302 vk_range.baseMipLevel = sub_resource_idx % texture_vk->t.level_count;
5303 vk_range.levelCount = 1;
5304 vk_range.baseArrayLayer = sub_resource_idx / texture_vk->t.level_count;
5305 vk_range.layerCount = 1;
5307 wined3d_context_vk_end_current_render_pass(context_vk);
5309 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5310 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
5311 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags), VK_ACCESS_TRANSFER_WRITE_BIT,
5312 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, vk_image, &vk_range);
5314 if (format->depth_size || format->stencil_size)
5316 depth_value.depth = sub_resource->clear_value.depth;
5317 depth_value.stencil = sub_resource->clear_value.stencil;
5318 VK_CALL(vkCmdClearDepthStencilImage(vk_command_buffer, vk_image,
5319 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &depth_value, 1, &vk_range));
5321 else
5323 wined3d_format_colour_to_vk(format, &sub_resource->clear_value.colour, &colour_value);
5324 VK_CALL(vkCmdClearColorImage(vk_command_buffer, vk_image,
5325 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &colour_value, 1, &vk_range));
5328 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5329 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5330 VK_ACCESS_TRANSFER_WRITE_BIT, vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
5331 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texture_vk->layout, vk_image, &vk_range);
5332 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5334 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
5335 return true;
5338 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
5339 unsigned int sub_resource_idx, struct wined3d_context *context)
5341 struct wined3d_texture_sub_resource *sub_resource;
5342 unsigned int level, row_pitch, slice_pitch;
5343 struct wined3d_bo_address data;
5344 struct wined3d_box src_box;
5346 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5348 if (sub_resource->locations & WINED3D_LOCATION_CLEARED)
5350 if (!wined3d_texture_vk_clear(texture_vk, sub_resource_idx, context))
5351 return FALSE;
5353 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
5354 return TRUE;
5357 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
5359 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5360 return FALSE;
5363 level = sub_resource_idx % texture_vk->t.level_count;
5364 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, context, &data);
5365 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5366 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5367 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
5368 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
5369 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
5371 return TRUE;
5374 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
5375 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5377 struct wined3d_texture_sub_resource *sub_resource;
5378 unsigned int level, row_pitch, slice_pitch;
5379 struct wined3d_bo_address data;
5380 struct wined3d_box src_box;
5382 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5383 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
5385 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5386 return FALSE;
5389 level = sub_resource_idx % texture_vk->t.level_count;
5390 wined3d_texture_get_bo_address(&texture_vk->t, sub_resource_idx, &data, location);
5391 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5392 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5393 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
5394 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
5396 return TRUE;
5399 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
5400 struct wined3d_context_vk *context_vk)
5402 const struct wined3d_format_vk *format_vk;
5403 struct wined3d_resource *resource;
5404 VkCommandBuffer vk_command_buffer;
5405 VkImageSubresourceRange vk_range;
5406 VkImageUsageFlags vk_usage;
5407 VkImageType vk_image_type;
5408 unsigned int flags = 0;
5410 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
5411 return TRUE;
5413 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5415 ERR("Failed to get command buffer.\n");
5416 return FALSE;
5419 resource = &texture_vk->t.resource;
5420 format_vk = wined3d_format_vk(resource->format);
5422 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain
5423 || (texture_vk->t.resource.bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
5425 /* For UAVs, we need this in case a clear necessitates creation of a new view
5426 * with a different format. */
5427 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
5430 switch (resource->type)
5432 case WINED3D_RTYPE_TEXTURE_1D:
5433 vk_image_type = VK_IMAGE_TYPE_1D;
5434 break;
5435 case WINED3D_RTYPE_TEXTURE_2D:
5436 vk_image_type = VK_IMAGE_TYPE_2D;
5437 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
5438 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
5439 break;
5440 case WINED3D_RTYPE_TEXTURE_3D:
5441 vk_image_type = VK_IMAGE_TYPE_3D;
5442 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
5443 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
5444 break;
5445 default:
5446 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
5447 vk_image_type = VK_IMAGE_TYPE_2D;
5448 break;
5451 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5452 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5453 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5454 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5455 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5456 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5457 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5458 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5459 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5461 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5462 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5463 else if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5464 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5465 else if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5466 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5467 else if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5468 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5469 else
5471 FIXME("unexpected bind flags %s, using VK_IMAGE_LAYOUT_GENERAL\n", wined3d_debug_bind_flags(resource->bind_flags));
5472 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5475 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5476 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5477 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5479 return FALSE;
5482 /* We can't use a zero src access mask without synchronization2. Set the last-used bind mask to something
5483 * non-zero to avoid this. */
5484 texture_vk->bind_mask = resource->bind_flags;
5486 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5487 vk_range.baseMipLevel = 0;
5488 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5489 vk_range.baseArrayLayer = 0;
5490 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5492 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5493 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5494 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5495 0, 0,
5496 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5497 texture_vk->image.vk_image, &vk_range);
5499 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5501 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5502 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5504 return TRUE;
5507 static BOOL wined3d_texture_vk_prepare_buffer_object(struct wined3d_texture_vk *texture_vk,
5508 unsigned int sub_resource_idx, struct wined3d_context_vk *context_vk)
5510 struct wined3d_texture_sub_resource *sub_resource;
5511 struct wined3d_bo_vk *bo;
5513 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5514 if (sub_resource->bo)
5515 return TRUE;
5517 if (!(bo = heap_alloc(sizeof(*bo))))
5518 return FALSE;
5520 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5521 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
5522 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bo))
5524 heap_free(bo);
5525 return FALSE;
5528 /* Texture buffer objects receive a barrier to HOST_READ in wined3d_texture_vk_download_data(),
5529 * so they don't need it when they are mapped for reading. */
5530 bo->host_synced = true;
5531 sub_resource->bo = &bo->b;
5532 TRACE("Created buffer object %p for texture %p, sub-resource %u.\n", bo, texture_vk, sub_resource_idx);
5533 return TRUE;
5536 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5537 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5539 switch (location)
5541 case WINED3D_LOCATION_SYSMEM:
5542 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5543 : wined3d_resource_prepare_sysmem(&texture->resource);
5545 case WINED3D_LOCATION_TEXTURE_RGB:
5546 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5548 case WINED3D_LOCATION_BUFFER:
5549 return wined3d_texture_vk_prepare_buffer_object(wined3d_texture_vk(texture), sub_resource_idx,
5550 wined3d_context_vk(context));
5552 default:
5553 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5554 return FALSE;
5558 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5559 unsigned int sub_resource_idx, struct wined3d_context *context, uint32_t location)
5561 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5562 return FALSE;
5564 switch (location)
5566 case WINED3D_LOCATION_TEXTURE_RGB:
5567 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5569 case WINED3D_LOCATION_SYSMEM:
5570 case WINED3D_LOCATION_BUFFER:
5571 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context, location);
5573 default:
5574 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5575 return FALSE;
5579 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5580 struct wined3d_context *context, unsigned int location)
5582 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5583 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5584 unsigned int i, sub_count;
5586 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5588 switch (location)
5590 case WINED3D_LOCATION_TEXTURE_RGB:
5591 if (texture_vk->default_image_info.imageView)
5593 wined3d_context_vk_destroy_vk_image_view(context_vk,
5594 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5595 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5598 if (texture_vk->image.vk_image)
5599 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5600 break;
5602 case WINED3D_LOCATION_BUFFER:
5603 sub_count = texture->level_count * texture->layer_count;
5604 for (i = 0; i < sub_count; ++i)
5606 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
5608 if (sub_resource->bo)
5610 struct wined3d_bo_vk *bo_vk = wined3d_bo_vk(sub_resource->bo);
5612 wined3d_context_vk_destroy_bo(context_vk, bo_vk);
5613 heap_free(bo_vk);
5614 sub_resource->bo = NULL;
5617 break;
5619 case WINED3D_LOCATION_TEXTURE_SRGB:
5620 case WINED3D_LOCATION_RB_MULTISAMPLE:
5621 case WINED3D_LOCATION_RB_RESOLVED:
5622 break;
5624 default:
5625 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5626 break;
5630 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5632 wined3d_texture_vk_prepare_location,
5633 wined3d_texture_vk_load_location,
5634 wined3d_texture_vk_unload_location,
5635 wined3d_texture_vk_upload_data,
5636 wined3d_texture_vk_download_data,
5639 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5640 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5641 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5643 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5644 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5645 texture_vk, device, desc, layer_count,
5646 level_count, flags, parent, parent_ops);
5648 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5649 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5652 enum VkImageLayout wined3d_layout_from_bind_mask(const struct wined3d_texture_vk *texture_vk, const uint32_t bind_mask)
5654 assert(wined3d_popcount(bind_mask) == 1);
5656 /* We want to avoid switching between LAYOUT_GENERAL and other layouts. In Radeon GPUs (and presumably
5657 * others), this will trigger decompressing and recompressing the texture. We also hardcode the layout
5658 * into views when they are created. */
5659 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
5660 return VK_IMAGE_LAYOUT_GENERAL;
5662 switch (bind_mask)
5664 case WINED3D_BIND_RENDER_TARGET:
5665 return VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5667 case WINED3D_BIND_DEPTH_STENCIL:
5668 return VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5670 case WINED3D_BIND_SHADER_RESOURCE:
5671 return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5673 default:
5674 ERR("Unexpected bind mask %s.\n", wined3d_debug_bind_flags(bind_mask));
5675 return VK_IMAGE_LAYOUT_GENERAL;
5679 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5680 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5682 enum VkImageLayout new_layout;
5683 uint32_t src_bind_mask = 0;
5685 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5686 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5688 new_layout = wined3d_layout_from_bind_mask(texture_vk, bind_mask);
5690 /* A layout transition is potentially a read-write operation, so even if we
5691 * prepare the texture to e.g. read only shader resource mode, we have to wait
5692 * for past operations to finish. */
5693 if (bind_mask & ~WINED3D_READ_ONLY_BIND_MASK || new_layout != texture_vk->layout)
5695 src_bind_mask = texture_vk->bind_mask & WINED3D_READ_ONLY_BIND_MASK;
5696 if (!src_bind_mask)
5697 src_bind_mask = texture_vk->bind_mask;
5699 texture_vk->bind_mask = bind_mask;
5701 else if ((texture_vk->bind_mask & bind_mask) != bind_mask)
5703 src_bind_mask = texture_vk->bind_mask & ~WINED3D_READ_ONLY_BIND_MASK;
5704 texture_vk->bind_mask |= bind_mask;
5707 if (src_bind_mask)
5709 VkImageSubresourceRange vk_range;
5711 TRACE(" %s(%x) -> %s(%x).\n",
5712 wined3d_debug_bind_flags(src_bind_mask), texture_vk->layout,
5713 wined3d_debug_bind_flags(bind_mask), new_layout);
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_mask_from_bind_flags(src_bind_mask),
5723 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5724 vk_access_mask_from_bind_flags(src_bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5725 texture_vk->layout, new_layout, texture_vk->image.vk_image, &vk_range);
5727 texture_vk->layout = new_layout;
5731 /* This is called when a texture is used as render target and shader resource
5732 * or depth stencil and shader resource at the same time. This can either be
5733 * read-only simultaneos use as depth stencil, but also for rendering to one
5734 * subresource while reading from another. Without tracking of barriers and
5735 * layouts per subresource VK_IMAGE_LAYOUT_GENERAL is the only thing we can do. */
5736 void wined3d_texture_vk_make_generic(struct wined3d_texture_vk *texture_vk,
5737 struct wined3d_context_vk *context_vk)
5739 VkImageSubresourceRange vk_range;
5741 if (texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
5742 return;
5744 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5745 vk_range.baseMipLevel = 0;
5746 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5747 vk_range.baseArrayLayer = 0;
5748 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5750 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5751 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5752 VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5753 0, 0,
5754 texture_vk->layout, VK_IMAGE_LAYOUT_GENERAL, texture_vk->image.vk_image, &vk_range);
5756 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5757 texture_vk->default_image_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
5760 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5762 struct wined3d_blitter *next;
5764 if ((next = blitter->next))
5765 next->ops->blitter_destroy(next, context);
5767 heap_free(blitter);
5770 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5771 const struct wined3d_resource *src_resource, DWORD src_location,
5772 const struct wined3d_resource *dst_resource, DWORD dst_location)
5774 const struct wined3d_format *src_format = src_resource->format;
5775 const struct wined3d_format *dst_format = dst_resource->format;
5776 bool decompress;
5778 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5779 return false;
5781 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
5782 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
5783 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5785 TRACE("Source or destination resource is not GPU accessible.\n");
5786 return false;
5789 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5791 if (dst_format->depth_size || dst_format->stencil_size)
5792 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5793 else
5794 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5797 switch (blit_op)
5799 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5800 if (context->d3d_info->shader_color_key)
5802 TRACE("Colour keying requires converted textures.\n");
5803 return false;
5805 case WINED3D_BLIT_OP_COLOR_BLIT:
5806 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5807 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5808 return false;
5810 if (TRACE_ON(d3d))
5812 TRACE("Checking support for fixup:\n");
5813 dump_color_fixup_desc(src_format->color_fixup);
5816 /* We only support identity conversions. */
5817 if (!is_identity_fixup(src_format->color_fixup)
5818 || !is_identity_fixup(dst_format->color_fixup))
5820 if (dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5822 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
5823 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5824 else if (context->device->shader_backend == &none_shader_backend)
5825 WARN("Claiming fixup support because of no shader backend.\n");
5826 return true;
5828 else
5830 TRACE("Fixups are not supported.\n");
5831 return false;
5835 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5837 TRACE("Can only blit to render targets.\n");
5838 return false;
5840 return true;
5842 default:
5843 TRACE("Unsupported blit operation %#x.\n", blit_op);
5844 return false;
5848 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5850 unsigned int height = rtv->height;
5851 unsigned int width = rtv->width;
5853 /* partial draw rect */
5854 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5855 return false;
5857 /* partial clear rect */
5858 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5859 || clear_rect->right < width || clear_rect->bottom < height))
5860 return false;
5862 return true;
5865 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5866 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5867 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5869 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5870 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5871 const struct wined3d_state *state = &device->cs->state;
5872 struct wined3d_texture *depth_stencil = NULL;
5873 unsigned int drawable_width, drawable_height;
5874 const struct wined3d_gl_info *gl_info;
5875 struct wined3d_context_gl *context_gl;
5876 struct wined3d_texture *target = NULL;
5877 struct wined3d_color colour_srgb;
5878 struct wined3d_context *context;
5879 GLbitfield clear_mask = 0;
5880 bool render_offscreen;
5881 unsigned int i;
5883 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5885 target = texture_from_resource(rtv->resource);
5886 context = context_acquire(device, target, rtv->sub_resource_idx);
5888 else
5890 context = context_acquire(device, NULL, 0);
5892 context_gl = wined3d_context_gl(context);
5894 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5895 depth_stencil = texture_from_resource(dsv->resource);
5897 if (!context_gl->valid)
5899 context_release(context);
5900 WARN("Invalid context, skipping clear.\n");
5901 return;
5903 gl_info = context_gl->gl_info;
5905 /* When we're clearing parts of the drawable, make sure that the target
5906 * surface is well up to date in the drawable. After the clear we'll mark
5907 * the drawable up to date, so we have to make sure that this is true for
5908 * the cleared parts, and the untouched parts.
5910 * If we're clearing the whole target there is no need to copy it into the
5911 * drawable, it will be overwritten anyway. If we're not clearing the
5912 * colour buffer we don't have to copy either since we're not going to set
5913 * the drawable up to date. We have to check all settings that limit the
5914 * clear area though. Do not bother checking all this if the destination
5915 * surface is in the drawable anyway. */
5916 for (i = 0; i < rt_count; ++i)
5918 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5920 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5922 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5923 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5924 else
5925 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5929 if (target)
5931 render_offscreen = context->render_offscreen;
5932 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5934 else
5936 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5938 render_offscreen = true;
5939 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5940 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5943 if (depth_stencil)
5945 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5947 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5948 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5949 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5950 else
5951 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5953 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5955 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5956 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5960 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5962 context_release(context);
5963 WARN("Failed to apply clear state, skipping clear.\n");
5964 return;
5967 /* Only set the values up once, as they are not changing. */
5968 if (flags & WINED3DCLEAR_STENCIL)
5970 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5971 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5972 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5973 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5974 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5975 checkGLcall("glClearStencil");
5976 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5979 if (flags & WINED3DCLEAR_ZBUFFER)
5981 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5982 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5983 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5984 GL_EXTCALL(glClearDepthf(depth));
5985 else
5986 gl_info->gl_ops.gl.p_glClearDepth(depth);
5987 checkGLcall("glClearDepth");
5988 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5991 if (flags & WINED3DCLEAR_TARGET)
5993 for (i = 0; i < rt_count; ++i)
5995 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5997 if (!rtv)
5998 continue;
6000 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
6002 FIXME("Not supported on buffer resources.\n");
6003 continue;
6006 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
6007 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
6010 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
6012 if (rt_count > 1)
6013 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
6014 "support, this might cause graphical issues.\n");
6016 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
6017 colour = &colour_srgb;
6020 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
6021 context_invalidate_state(context, STATE_BLEND);
6022 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
6023 checkGLcall("glClearColor");
6024 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
6027 if (!rect_count)
6029 if (render_offscreen)
6031 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
6032 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6034 else
6036 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
6037 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
6039 gl_info->gl_ops.gl.p_glClear(clear_mask);
6041 else
6043 RECT current_rect;
6045 /* Now process each rect in turn. */
6046 for (i = 0; i < rect_count; ++i)
6048 /* Note that GL uses lower left, width/height. */
6049 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
6051 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
6052 wine_dbgstr_rect(&clear_rect[i]),
6053 wine_dbgstr_rect(&current_rect));
6055 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
6056 * silently. The rectangle is not cleared, no error is returned,
6057 * but further rectangles are still cleared if they are valid. */
6058 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
6060 TRACE("Rectangle with negative dimensions, ignoring.\n");
6061 continue;
6064 if (render_offscreen)
6066 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
6067 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6069 else
6071 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
6072 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
6074 gl_info->gl_ops.gl.p_glClear(clear_mask);
6077 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
6078 checkGLcall("clear");
6080 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
6081 gl_info->gl_ops.gl.p_glFlush();
6083 context_release(context);
6086 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
6088 struct wined3d_resource *resource;
6089 struct wined3d_texture *texture;
6090 DWORD locations;
6092 resource = view->resource;
6093 if (resource->type == WINED3D_RTYPE_BUFFER)
6094 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
6096 texture = texture_from_resource(resource);
6097 locations = texture->sub_resources[view->sub_resource_idx].locations;
6098 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
6099 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6100 || texture->resource.pin_sysmem;
6102 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
6103 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
6106 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6107 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6108 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6110 struct wined3d_rendertarget_view *view, *previous = NULL;
6111 bool have_identical_size = TRUE;
6112 struct wined3d_fb_state tmp_fb;
6113 unsigned int next_rt_count = 0;
6114 struct wined3d_blitter *next;
6115 DWORD next_flags = 0;
6116 unsigned int i;
6118 if (flags & WINED3DCLEAR_TARGET)
6120 for (i = 0; i < rt_count; ++i)
6122 if (!(view = fb->render_targets[i]))
6123 continue;
6125 if (blitter_use_cpu_clear(view)
6126 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
6127 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
6128 || !(view->format_caps & WINED3D_FORMAT_CAP_FBO_ATTACHABLE))))
6130 next_flags |= WINED3DCLEAR_TARGET;
6131 flags &= ~WINED3DCLEAR_TARGET;
6132 next_rt_count = rt_count;
6133 rt_count = 0;
6134 break;
6137 /* FIXME: We should reject colour fills on formats with fixups,
6138 * but this would break P8 colour fills for example. */
6142 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6143 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6144 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6145 && blitter_use_cpu_clear(view))
6147 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6148 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6151 if (flags)
6153 for (i = 0; i < rt_count; ++i)
6155 if (!(view = fb->render_targets[i]))
6156 continue;
6158 if (previous && (previous->width != view->width || previous->height != view->height))
6159 have_identical_size = false;
6160 previous = view;
6162 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6164 view = fb->depth_stencil;
6166 if (previous && (previous->width != view->width || previous->height != view->height))
6167 have_identical_size = false;
6170 if (have_identical_size)
6172 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
6173 clear_rects, draw_rect, flags, colour, depth, stencil);
6175 else
6177 for (i = 0; i < rt_count; ++i)
6179 if (!(view = fb->render_targets[i]))
6180 continue;
6182 tmp_fb.render_targets[0] = view;
6183 tmp_fb.depth_stencil = NULL;
6184 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
6185 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6187 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6189 tmp_fb.render_targets[0] = NULL;
6190 tmp_fb.depth_stencil = fb->depth_stencil;
6191 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
6192 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6197 if (next_flags && (next = blitter->next))
6198 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6199 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6202 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6203 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6204 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6205 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6206 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6207 const struct wined3d_format *resolve_format)
6209 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6210 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6211 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6212 struct wined3d_resource *src_resource, *dst_resource;
6213 struct wined3d_texture *staging_texture = NULL;
6214 struct wined3d_color_key old_blt_key;
6215 struct wined3d_device *device;
6216 struct wined3d_blitter *next;
6217 DWORD old_colour_key_flags;
6218 RECT r;
6220 src_resource = &src_texture->resource;
6221 dst_resource = &dst_texture->resource;
6222 device = dst_resource->device;
6224 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
6226 if ((next = blitter->next))
6227 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6228 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6229 resolve_format);
6232 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
6233 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
6235 old_blt_key = src_texture->async.src_blt_color_key;
6236 old_colour_key_flags = src_texture->async.color_key_flags;
6237 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
6239 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
6241 struct wined3d_resource_desc desc;
6242 struct wined3d_box upload_box;
6243 unsigned int src_level;
6244 HRESULT hr;
6246 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
6248 src_level = src_sub_resource_idx % src_texture->level_count;
6249 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
6250 desc.format = src_texture->resource.format->id;
6251 desc.multisample_type = src_texture->resource.multisample_type;
6252 desc.multisample_quality = src_texture->resource.multisample_quality;
6253 desc.usage = WINED3DUSAGE_PRIVATE;
6254 desc.bind_flags = 0;
6255 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
6256 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
6257 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
6258 desc.depth = 1;
6259 desc.size = 0;
6261 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
6262 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
6264 ERR("Failed to create staging texture, hr %#lx.\n", hr);
6265 return dst_location;
6268 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
6269 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
6270 src_texture, src_sub_resource_idx, &upload_box);
6272 src_texture = staging_texture;
6273 src_texture_gl = wined3d_texture_gl(src_texture);
6274 src_sub_resource_idx = 0;
6276 else
6278 /* Make sure the surface is up-to-date. This should probably use
6279 * surface_load_location() and worry about the destination surface
6280 * too, unless we're overwriting it completely. */
6281 wined3d_texture_load(src_texture, context, FALSE);
6284 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
6286 if (dst_location == WINED3D_LOCATION_DRAWABLE)
6288 r = *dst_rect;
6289 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
6290 dst_rect = &r;
6293 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
6295 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
6296 checkGLcall("glEnable(target)");
6298 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6300 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
6301 checkGLcall("glEnable(GL_ALPHA_TEST)");
6304 if (colour_key)
6306 /* For P8 surfaces, the alpha component contains the palette index.
6307 * Which means that the colourkey is one of the palette entries. In
6308 * other cases pixels that should be masked away have alpha set to 0. */
6309 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
6310 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
6311 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
6312 else
6313 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
6314 checkGLcall("glAlphaFunc");
6317 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
6318 src_sub_resource_idx, src_rect, dst_rect, filter);
6320 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
6322 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
6323 checkGLcall("glDisable(GL_ALPHA_TEST)");
6326 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
6327 checkGLcall("glDisable(GL_TEXTURE_2D)");
6328 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
6330 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
6331 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
6333 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6335 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
6336 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6339 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
6340 gl_info->gl_ops.gl.p_glFlush();
6342 /* Restore the colour key parameters */
6343 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
6344 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
6346 if (staging_texture)
6347 wined3d_texture_decref(staging_texture);
6349 return dst_location;
6352 static const struct wined3d_blitter_ops ffp_blitter_ops =
6354 ffp_blitter_destroy,
6355 ffp_blitter_clear,
6356 ffp_blitter_blit,
6359 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6361 struct wined3d_blitter *blitter;
6363 if (!(blitter = heap_alloc(sizeof(*blitter))))
6364 return;
6366 TRACE("Created blitter %p.\n", blitter);
6368 blitter->ops = &ffp_blitter_ops;
6369 blitter->next = *next;
6370 *next = blitter;
6373 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6375 struct wined3d_blitter *next;
6377 if ((next = blitter->next))
6378 next->ops->blitter_destroy(next, context);
6380 heap_free(blitter);
6383 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6384 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6385 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6387 struct wined3d_blitter *next;
6389 if ((next = blitter->next))
6390 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6391 clear_rects, draw_rect, flags, colour, depth, stencil);
6394 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6395 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6396 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6397 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6398 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6399 const struct wined3d_format *resolve_format)
6401 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6402 struct wined3d_resource *src_resource, *dst_resource;
6403 enum wined3d_blit_op blit_op = op;
6404 struct wined3d_device *device;
6405 struct wined3d_blitter *next;
6407 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6408 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6409 "colour_key %p, filter %s, resolve_format %p.\n",
6410 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6411 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6412 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6414 src_resource = &src_texture->resource;
6415 dst_resource = &dst_texture->resource;
6417 device = dst_resource->device;
6419 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
6421 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
6422 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
6423 else
6424 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
6427 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
6428 src_resource, src_location, dst_resource, dst_location))
6430 if (!(next = blitter->next))
6432 ERR("No blitter to handle blit op %#x.\n", op);
6433 return dst_location;
6436 TRACE("Forwarding to blitter %p.\n", next);
6437 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6438 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6439 resolve_format);
6442 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
6444 TRACE("Colour blit.\n");
6445 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
6446 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
6447 return dst_location;
6450 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
6452 TRACE("Depth/stencil blit.\n");
6453 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
6454 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
6455 return dst_location;
6458 ERR("This blitter does not implement blit op %#x.\n", blit_op);
6459 return dst_location;
6462 static const struct wined3d_blitter_ops fbo_blitter_ops =
6464 fbo_blitter_destroy,
6465 fbo_blitter_clear,
6466 fbo_blitter_blit,
6469 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6471 struct wined3d_blitter *blitter;
6473 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
6474 return;
6476 if (!(blitter = heap_alloc(sizeof(*blitter))))
6477 return;
6479 TRACE("Created blitter %p.\n", blitter);
6481 blitter->ops = &fbo_blitter_ops;
6482 blitter->next = *next;
6483 *next = blitter;
6486 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6488 struct wined3d_blitter *next;
6490 if ((next = blitter->next))
6491 next->ops->blitter_destroy(next, context);
6493 heap_free(blitter);
6496 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6497 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6498 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6500 struct wined3d_blitter *next;
6502 if (!(next = blitter->next))
6504 ERR("No blitter to handle clear.\n");
6505 return;
6508 TRACE("Forwarding to blitter %p.\n", next);
6509 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6510 clear_rects, draw_rect, flags, colour, depth, stencil);
6513 static bool gl_formats_compatible(struct wined3d_texture *src_texture, DWORD src_location,
6514 struct wined3d_texture *dst_texture, DWORD dst_location)
6516 GLuint src_internal, dst_internal;
6517 bool src_ds, dst_ds;
6519 src_ds = src_texture->resource.format->depth_size || src_texture->resource.format->stencil_size;
6520 dst_ds = dst_texture->resource.format->depth_size || dst_texture->resource.format->stencil_size;
6521 if (src_ds == dst_ds)
6522 return true;
6523 /* Also check the internal format because, e.g. WINED3DFMT_D24_UNORM_S8_UINT has nonzero depth and stencil
6524 * sizes as does WINED3DFMT_R24G8_TYPELESS when bound with flag WINED3D_BIND_DEPTH_STENCIL, but these share
6525 * the same internal format with WINED3DFMT_R24_UNORM_X8_TYPELESS. */
6526 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
6527 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
6528 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
6529 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
6530 return src_internal == dst_internal;
6533 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6534 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6535 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6536 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6537 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6538 const struct wined3d_format *resolve_format)
6540 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6541 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6542 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6543 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6544 unsigned int src_level, src_layer, dst_level, dst_layer;
6545 struct wined3d_blitter *next;
6546 GLuint src_name, dst_name;
6547 DWORD location;
6549 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6550 * be better off using the FBO blitter directly, since we'd need to use it
6551 * to copy the resource contents to the texture anyway.
6553 * We also can't copy between depth/stencil and colour resources, since
6554 * the formats are considered incompatible in OpenGL. */
6555 if (op != WINED3D_BLIT_OP_RAW_BLIT || !gl_formats_compatible(src_texture, src_location, dst_texture, dst_location)
6556 || ((src_texture->resource.format_attrs | dst_texture->resource.format_attrs)
6557 & WINED3D_FORMAT_ATTR_HEIGHT_SCALE)
6558 || (src_texture->resource.format->id == dst_texture->resource.format->id
6559 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6560 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6562 if (!(next = blitter->next))
6564 ERR("No blitter to handle blit op %#x.\n", op);
6565 return dst_location;
6568 TRACE("Forwarding to blitter %p.\n", next);
6569 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6570 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6571 resolve_format);
6574 TRACE("Blit using ARB_copy_image.\n");
6576 src_level = src_sub_resource_idx % src_texture->level_count;
6577 src_layer = src_sub_resource_idx / src_texture->level_count;
6579 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6580 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6582 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6583 if (!location)
6584 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6585 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6586 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6587 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6588 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6589 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6591 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6592 if (!location)
6593 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6594 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6595 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6597 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6598 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6600 else
6602 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6603 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6605 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6606 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6608 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6609 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6610 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6611 src_rect->bottom - src_rect->top, 1));
6612 checkGLcall("copy image data");
6614 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6615 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6616 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6617 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6619 return dst_location | location;
6622 static const struct wined3d_blitter_ops raw_blitter_ops =
6624 raw_blitter_destroy,
6625 raw_blitter_clear,
6626 raw_blitter_blit,
6629 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6631 struct wined3d_blitter *blitter;
6633 if (!gl_info->supported[ARB_COPY_IMAGE])
6634 return;
6636 if (!(blitter = heap_alloc(sizeof(*blitter))))
6637 return;
6639 TRACE("Created blitter %p.\n", blitter);
6641 blitter->ops = &raw_blitter_ops;
6642 blitter->next = *next;
6643 *next = blitter;
6646 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6648 struct wined3d_blitter *next;
6650 TRACE("blitter %p, context %p.\n", blitter, context);
6652 if ((next = blitter->next))
6653 next->ops->blitter_destroy(next, context);
6655 heap_free(blitter);
6658 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6659 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6660 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6662 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6663 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6664 unsigned int i, attachment_count, delay_count = 0;
6665 struct wined3d_rendertarget_view_vk *rtv_vk;
6666 struct wined3d_rendertarget_view *view;
6667 const struct wined3d_vk_info *vk_info;
6668 struct wined3d_device_vk *device_vk;
6669 VkCommandBuffer vk_command_buffer;
6670 VkRenderPassBeginInfo begin_desc;
6671 VkFramebufferCreateInfo fb_desc;
6672 VkFramebuffer vk_framebuffer;
6673 VkRenderPass vk_render_pass;
6674 bool depth_stencil = false;
6675 unsigned int layer_count;
6676 VkClearColorValue *c;
6677 VkResult vr;
6678 RECT r;
6680 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6681 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6682 context_vk, rt_count, fb, rect_count, clear_rects,
6683 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6685 device_vk = wined3d_device_vk(context_vk->c.device);
6686 vk_info = context_vk->vk_info;
6688 if (!(flags & WINED3DCLEAR_TARGET))
6689 rt_count = 0;
6691 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6693 if (!(view = fb->render_targets[i]))
6694 continue;
6696 /* Don't delay typeless clears because the data written into the resource depends on the
6697 * view format. Except all-zero clears, those should result in zeros in either case.
6699 * We could store the clear format along with the clear value, but then we'd have to
6700 * create a matching RTV at draw time, which would need its own render pass, thus mooting
6701 * the point of the delayed clear. (Unless we are lucky enough that the application
6702 * draws with the same RTV as it clears.) */
6703 if (is_full_clear(view, draw_rect, clear_rects)
6704 && (!wined3d_format_is_typeless(view->resource->format) || (!colour->r && !colour->g
6705 && !colour->b && !colour->a)))
6707 struct wined3d_texture *texture = texture_from_resource(view->resource);
6708 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6709 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6710 texture->sub_resources[view->sub_resource_idx].clear_value.colour = *colour;
6711 delay_count++;
6712 continue;
6714 else
6716 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6718 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6719 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6721 rtv_vk = wined3d_rendertarget_view_vk(view);
6722 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6723 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6725 c = &clear_values[attachment_count].color;
6726 wined3d_format_colour_to_vk(view->format, colour, c);
6728 if (view->layer_count > layer_count)
6729 layer_count = view->layer_count;
6731 ++attachment_count;
6734 if (!attachment_count)
6735 flags &= ~WINED3DCLEAR_TARGET;
6737 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6739 DWORD full_flags = 0;
6741 /* Vulkan can clear only depth or stencil, but at the moment we can't put the depth and
6742 * stencil parts in separate locations. It isn't easy to do either, as such a half-cleared
6743 * texture would need to be handled not just as a DS target but also when used as a shader
6744 * resource or accessed on sysmem. */
6745 if (view->format->depth_size)
6746 full_flags = WINED3DCLEAR_ZBUFFER;
6747 if (view->format->stencil_size)
6748 full_flags |= WINED3DCLEAR_STENCIL;
6750 if (!is_full_clear(view, draw_rect, clear_rects) || (flags & full_flags) != full_flags
6751 || (wined3d_format_is_typeless(view->resource->format) && (depth || stencil)))
6753 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6754 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6755 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6757 rtv_vk = wined3d_rendertarget_view_vk(view);
6758 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6759 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6761 clear_values[attachment_count].depthStencil.depth = depth;
6762 clear_values[attachment_count].depthStencil.stencil = stencil;
6764 if (view->layer_count > layer_count)
6765 layer_count = view->layer_count;
6767 depth_stencil = true;
6768 ++attachment_count;
6770 else
6772 struct wined3d_texture *texture = texture_from_resource(view->resource);
6773 texture->sub_resources[view->sub_resource_idx].clear_value.depth = depth;
6774 texture->sub_resources[view->sub_resource_idx].clear_value.stencil = stencil;
6775 wined3d_rendertarget_view_validate_location(view, WINED3D_LOCATION_CLEARED);
6776 wined3d_rendertarget_view_invalidate_location(view, ~WINED3D_LOCATION_CLEARED);
6777 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6778 delay_count++;
6782 if (!attachment_count)
6784 TRACE("The clear has been delayed until draw time.\n");
6785 return;
6788 TRACE("Doing an immediate clear of %u attachments.\n", attachment_count);
6789 if (delay_count)
6790 TRACE_(d3d_perf)("Partial clear: %u immediate, %u delayed.\n", attachment_count, delay_count);
6792 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6793 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6795 ERR("Failed to get render pass.\n");
6796 return;
6799 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6801 ERR("Failed to get command buffer.\n");
6802 return;
6805 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6806 fb_desc.pNext = NULL;
6807 fb_desc.flags = 0;
6808 fb_desc.renderPass = vk_render_pass;
6809 fb_desc.attachmentCount = attachment_count;
6810 fb_desc.pAttachments = views;
6811 fb_desc.width = draw_rect->right - draw_rect->left;
6812 fb_desc.height = draw_rect->bottom - draw_rect->top;
6813 fb_desc.layers = layer_count;
6814 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6816 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6817 return;
6820 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6821 begin_desc.pNext = NULL;
6822 begin_desc.renderPass = vk_render_pass;
6823 begin_desc.framebuffer = vk_framebuffer;
6824 begin_desc.clearValueCount = attachment_count;
6825 begin_desc.pClearValues = clear_values;
6827 wined3d_context_vk_end_current_render_pass(context_vk);
6829 for (i = 0; i < rect_count; ++i)
6831 r.left = max(clear_rects[i].left, draw_rect->left);
6832 r.top = max(clear_rects[i].top, draw_rect->top);
6833 r.right = min(clear_rects[i].right, draw_rect->right);
6834 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6836 if (r.left >= r.right || r.top >= r.bottom)
6837 continue;
6839 begin_desc.renderArea.offset.x = r.left;
6840 begin_desc.renderArea.offset.y = r.top;
6841 begin_desc.renderArea.extent.width = r.right - r.left;
6842 begin_desc.renderArea.extent.height = r.bottom - r.top;
6843 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6844 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6847 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6849 for (i = 0; i < rt_count; ++i)
6851 if (!(view = fb->render_targets[i]))
6852 continue;
6854 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6857 if (depth_stencil)
6859 view = fb->depth_stencil;
6860 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6864 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6865 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6866 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6868 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6869 struct wined3d_rendertarget_view *view, *previous = NULL;
6870 struct wined3d_context_vk *context_vk;
6871 bool have_identical_size = true;
6872 struct wined3d_fb_state tmp_fb;
6873 unsigned int next_rt_count = 0;
6874 struct wined3d_blitter *next;
6875 uint32_t next_flags = 0;
6876 unsigned int i;
6878 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6879 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6880 blitter, device, rt_count, fb, rect_count, clear_rects,
6881 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6883 if (!rect_count)
6885 rect_count = 1;
6886 clear_rects = draw_rect;
6889 if (flags & WINED3DCLEAR_TARGET)
6891 for (i = 0; i < rt_count; ++i)
6893 if (!(view = fb->render_targets[i]))
6894 continue;
6896 if (blitter_use_cpu_clear(view))
6898 next_flags |= WINED3DCLEAR_TARGET;
6899 flags &= ~WINED3DCLEAR_TARGET;
6900 next_rt_count = rt_count;
6901 rt_count = 0;
6902 break;
6907 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6908 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6909 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6910 && blitter_use_cpu_clear(view))
6912 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6913 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6916 if (flags)
6918 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6920 for (i = 0; i < rt_count; ++i)
6922 if (!(view = fb->render_targets[i]))
6923 continue;
6925 if (previous && (previous->width != view->width || previous->height != view->height))
6926 have_identical_size = false;
6927 previous = view;
6929 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6931 view = fb->depth_stencil;
6933 if (previous && (previous->width != view->width || previous->height != view->height))
6934 have_identical_size = false;
6937 if (have_identical_size)
6939 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6940 clear_rects, draw_rect, flags, colour, depth, stencil);
6942 else
6944 for (i = 0; i < rt_count; ++i)
6946 if (!(view = fb->render_targets[i]))
6947 continue;
6949 tmp_fb.render_targets[0] = view;
6950 tmp_fb.depth_stencil = NULL;
6951 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6952 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6954 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6956 tmp_fb.render_targets[0] = NULL;
6957 tmp_fb.depth_stencil = fb->depth_stencil;
6958 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6959 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6963 context_release(&context_vk->c);
6966 if (!next_flags)
6967 return;
6969 if (!(next = blitter->next))
6971 ERR("No blitter to handle clear.\n");
6972 return;
6975 TRACE("Forwarding to blitter %p.\n", next);
6976 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6977 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6980 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6981 const struct wined3d_resource *src_resource, const RECT *src_rect,
6982 const struct wined3d_resource *dst_resource, const RECT *dst_rect, const struct wined3d_format *resolve_format)
6984 const struct wined3d_format *src_format = src_resource->format;
6985 const struct wined3d_format *dst_format = dst_resource->format;
6987 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6989 TRACE("Destination resource does not have GPU access.\n");
6990 return false;
6993 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6995 TRACE("Source resource does not have GPU access.\n");
6996 return false;
6999 if (dst_format->id != src_format->id)
7001 if (!is_identity_fixup(dst_format->color_fixup))
7003 TRACE("Destination fixups are not supported.\n");
7004 return false;
7007 if (!is_identity_fixup(src_format->color_fixup))
7009 TRACE("Source fixups are not supported.\n");
7010 return false;
7013 if (op != WINED3D_BLIT_OP_RAW_BLIT
7014 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format
7015 && ((!wined3d_format_is_typeless(src_format) && !wined3d_format_is_typeless(dst_format))
7016 || !resolve_format))
7018 TRACE("Format conversion not supported.\n");
7019 return false;
7023 if (wined3d_resource_get_sample_count(dst_resource) > 1)
7025 TRACE("Multi-sample destination resource not supported.\n");
7026 return false;
7029 if (op == WINED3D_BLIT_OP_RAW_BLIT)
7030 return true;
7032 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
7034 TRACE("Unsupported blit operation %#x.\n", op);
7035 return false;
7038 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
7039 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
7041 TRACE("Scaling not supported.\n");
7042 return false;
7045 return true;
7048 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
7049 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
7050 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
7051 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
7052 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
7053 const struct wined3d_format *resolve_format)
7055 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
7056 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
7057 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
7058 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
7059 VkImageSubresourceRange vk_src_range, vk_dst_range;
7060 VkImageLayout src_layout, dst_layout;
7061 VkCommandBuffer vk_command_buffer;
7062 struct wined3d_blitter *next;
7063 unsigned src_sample_count;
7064 bool resolve = false;
7066 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
7067 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
7068 "colour_key %p, filter %s, resolve format %p.\n",
7069 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
7070 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
7071 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
7073 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect,
7074 resolve_format))
7075 goto next;
7077 src_sample_count = wined3d_resource_get_sample_count(&src_texture_vk->t.resource);
7078 if (src_sample_count > 1)
7079 resolve = true;
7081 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
7082 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
7083 vk_src_range.levelCount = 1;
7084 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
7085 vk_src_range.layerCount = 1;
7087 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
7088 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
7089 vk_dst_range.levelCount = 1;
7090 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
7091 vk_dst_range.layerCount = 1;
7093 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7094 ERR("Failed to load the source sub-resource.\n");
7096 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
7098 if (!wined3d_texture_prepare_location(dst_texture,
7099 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7101 ERR("Failed to prepare the destination sub-resource.\n");
7102 goto next;
7105 else
7107 if (!wined3d_texture_load_location(dst_texture,
7108 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
7110 ERR("Failed to load the destination sub-resource.\n");
7111 goto next;
7115 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
7117 ERR("Failed to get command buffer.\n");
7118 goto next;
7121 if (src_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7122 src_layout = VK_IMAGE_LAYOUT_GENERAL;
7123 else
7124 src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7126 if (dst_texture_vk->layout == VK_IMAGE_LAYOUT_GENERAL)
7127 dst_layout = VK_IMAGE_LAYOUT_GENERAL;
7128 else
7129 dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7131 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7132 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7133 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7134 VK_ACCESS_TRANSFER_READ_BIT, src_texture_vk->layout, src_layout,
7135 src_texture_vk->image.vk_image, &vk_src_range);
7136 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7137 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7138 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7139 VK_ACCESS_TRANSFER_WRITE_BIT, dst_texture_vk->layout, dst_layout,
7140 dst_texture_vk->image.vk_image, &vk_dst_range);
7142 if (resolve)
7144 const struct wined3d_format_vk *src_format_vk = wined3d_format_vk(src_texture->resource.format);
7145 const struct wined3d_format_vk *dst_format_vk = wined3d_format_vk(dst_texture->resource.format);
7146 const unsigned int usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
7147 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
7148 VkImageLayout resolve_src_layout, resolve_dst_layout;
7149 VkImage src_vk_image, dst_vk_image;
7150 VkImageSubresourceRange vk_range;
7151 VkImageResolve resolve_region;
7152 VkImageType vk_image_type;
7153 VkImageCopy copy_region;
7154 VkFormat vk_format;
7156 if (resolve_format)
7158 vk_format = wined3d_format_vk(resolve_format)->vk_format;
7160 else if (!wined3d_format_is_typeless(src_texture->resource.format))
7162 vk_format = src_format_vk->vk_format;
7164 else
7166 vk_format = dst_format_vk->vk_format;
7169 switch (src_texture->resource.type)
7171 case WINED3D_RTYPE_TEXTURE_1D:
7172 vk_image_type = VK_IMAGE_TYPE_1D;
7173 break;
7174 case WINED3D_RTYPE_TEXTURE_2D:
7175 vk_image_type = VK_IMAGE_TYPE_2D;
7176 break;
7177 case WINED3D_RTYPE_TEXTURE_3D:
7178 vk_image_type = VK_IMAGE_TYPE_3D;
7179 break;
7180 default:
7181 ERR("Unexpected resource type: %s\n", debug_d3dresourcetype(src_texture->resource.type));
7182 goto barrier_next;
7185 vk_range.baseMipLevel = 0;
7186 vk_range.levelCount = 1;
7187 vk_range.baseArrayLayer = 0;
7188 vk_range.layerCount = 1;
7190 resolve_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7191 resolve_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7192 resolve_region.extent.width = src_rect->right - src_rect->left;
7193 resolve_region.extent.height = src_rect->bottom - src_rect->top;
7194 resolve_region.extent.depth = 1;
7196 /* In case of typeless resolve the texture type may not match the resolve type.
7197 * To handle that, allocate intermediate texture(s) to resolve from/to.
7198 * A possible performance improvement would be to resolve using a shader instead. */
7199 if (src_format_vk->vk_format != vk_format)
7201 struct wined3d_image_vk src_image;
7203 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7204 resolve_region.extent.width, resolve_region.extent.height, 1,
7205 src_sample_count, 1, 1, 0, &src_image))
7206 goto barrier_next;
7208 wined3d_context_vk_reference_image(context_vk, &src_image);
7209 src_vk_image = src_image.vk_image;
7210 wined3d_context_vk_destroy_image(context_vk, &src_image);
7212 vk_range.aspectMask = vk_src_range.aspectMask;
7214 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7215 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7216 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
7217 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, src_vk_image, &vk_range);
7219 copy_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7220 copy_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7221 copy_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7222 copy_region.srcSubresource.layerCount = 1;
7223 copy_region.srcOffset.x = src_rect->left;
7224 copy_region.srcOffset.y = src_rect->top;
7225 copy_region.srcOffset.z = 0;
7226 copy_region.dstSubresource.aspectMask = vk_src_range.aspectMask;
7227 copy_region.dstSubresource.mipLevel = 0;
7228 copy_region.dstSubresource.baseArrayLayer = 0;
7229 copy_region.dstSubresource.layerCount = 1;
7230 copy_region.dstOffset.x = 0;
7231 copy_region.dstOffset.y = 0;
7232 copy_region.dstOffset.z = 0;
7233 copy_region.extent.width = resolve_region.extent.width;
7234 copy_region.extent.height = resolve_region.extent.height;
7235 copy_region.extent.depth = 1;
7237 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image,
7238 src_layout, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
7239 1, &copy_region));
7241 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7242 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7243 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7244 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7245 src_vk_image, &vk_range);
7246 resolve_src_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
7248 resolve_region.srcSubresource.mipLevel = 0;
7249 resolve_region.srcSubresource.baseArrayLayer = 0;
7250 resolve_region.srcSubresource.layerCount = 1;
7251 resolve_region.srcOffset.x = 0;
7252 resolve_region.srcOffset.y = 0;
7253 resolve_region.srcOffset.z = 0;
7255 else
7257 src_vk_image = src_texture_vk->image.vk_image;
7258 resolve_src_layout = src_layout;
7260 resolve_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7261 resolve_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7262 resolve_region.srcSubresource.layerCount = 1;
7263 resolve_region.srcOffset.x = src_rect->left;
7264 resolve_region.srcOffset.y = src_rect->top;
7265 resolve_region.srcOffset.z = 0;
7268 if (dst_format_vk->vk_format != vk_format)
7270 struct wined3d_image_vk dst_image;
7272 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
7273 resolve_region.extent.width, resolve_region.extent.height, 1,
7274 VK_SAMPLE_COUNT_1_BIT, 1, 1, 0, &dst_image))
7275 goto barrier_next;
7277 wined3d_context_vk_reference_image(context_vk, &dst_image);
7278 dst_vk_image = dst_image.vk_image;
7279 wined3d_context_vk_destroy_image(context_vk, &dst_image);
7281 vk_range.aspectMask = vk_dst_range.aspectMask;
7282 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7283 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
7284 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_vk_image, &vk_range);
7285 resolve_dst_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
7287 resolve_region.dstSubresource.mipLevel = 0;
7288 resolve_region.dstSubresource.baseArrayLayer = 0;
7289 resolve_region.dstSubresource.layerCount = 1;
7290 resolve_region.dstOffset.x = 0;
7291 resolve_region.dstOffset.y = 0;
7292 resolve_region.dstOffset.z = 0;
7294 else
7296 dst_vk_image = dst_texture_vk->image.vk_image;
7297 resolve_dst_layout = dst_layout;
7299 resolve_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7300 resolve_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7301 resolve_region.dstSubresource.layerCount = 1;
7302 resolve_region.dstOffset.x = dst_rect->left;
7303 resolve_region.dstOffset.y = dst_rect->top;
7304 resolve_region.dstOffset.z = 0;
7307 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_vk_image, resolve_src_layout,
7308 dst_vk_image, resolve_dst_layout, 1, &resolve_region));
7310 if (dst_vk_image != dst_texture_vk->image.vk_image)
7312 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7313 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
7314 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
7315 resolve_dst_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7316 dst_vk_image, &vk_range);
7318 copy_region.srcSubresource.aspectMask = vk_dst_range.aspectMask;
7319 copy_region.srcSubresource.mipLevel = 0;
7320 copy_region.srcSubresource.baseArrayLayer = 0;
7321 copy_region.srcSubresource.layerCount = 1;
7322 copy_region.srcOffset.x = 0;
7323 copy_region.srcOffset.y = 0;
7324 copy_region.srcOffset.z = 0;
7325 copy_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7326 copy_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7327 copy_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7328 copy_region.dstSubresource.layerCount = 1;
7329 copy_region.dstOffset.x = dst_rect->left;
7330 copy_region.dstOffset.y = dst_rect->top;
7331 copy_region.dstOffset.z = 0;
7332 copy_region.extent.width = resolve_region.extent.width;
7333 copy_region.extent.height = resolve_region.extent.height;
7334 copy_region.extent.depth = 1;
7336 VK_CALL(vkCmdCopyImage(vk_command_buffer, dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
7337 dst_texture_vk->image.vk_image, dst_layout, 1, &copy_region));
7340 else
7342 VkImageCopy region;
7344 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
7345 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
7346 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
7347 region.srcSubresource.layerCount = vk_src_range.layerCount;
7348 region.srcOffset.x = src_rect->left;
7349 region.srcOffset.y = src_rect->top;
7350 region.srcOffset.z = 0;
7351 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
7352 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
7353 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
7354 region.dstSubresource.layerCount = vk_dst_range.layerCount;
7355 region.dstOffset.x = dst_rect->left;
7356 region.dstOffset.y = dst_rect->top;
7357 region.dstOffset.z = 0;
7358 region.extent.width = src_rect->right - src_rect->left;
7359 region.extent.height = src_rect->bottom - src_rect->top;
7360 region.extent.depth = 1;
7362 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, src_layout,
7363 dst_texture_vk->image.vk_image, dst_layout, 1, &region));
7366 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7367 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7368 VK_ACCESS_TRANSFER_WRITE_BIT,
7369 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7370 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7371 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7372 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7373 VK_ACCESS_TRANSFER_READ_BIT,
7374 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7375 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7377 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
7378 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
7379 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
7380 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
7382 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
7383 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
7385 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
7387 barrier_next:
7388 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7389 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7390 VK_ACCESS_TRANSFER_WRITE_BIT,
7391 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7392 dst_layout, dst_texture_vk->layout, dst_texture_vk->image.vk_image, &vk_dst_range);
7393 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7394 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7395 VK_ACCESS_TRANSFER_READ_BIT,
7396 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7397 src_layout, src_texture_vk->layout, src_texture_vk->image.vk_image, &vk_src_range);
7399 next:
7400 if (!(next = blitter->next))
7402 ERR("No blitter to handle blit op %#x.\n", op);
7403 return dst_location;
7406 TRACE("Forwarding to blitter %p.\n", next);
7407 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
7408 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
7411 static const struct wined3d_blitter_ops vk_blitter_ops =
7413 .blitter_destroy = vk_blitter_destroy,
7414 .blitter_clear = vk_blitter_clear,
7415 .blitter_blit = vk_blitter_blit,
7418 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
7420 struct wined3d_blitter *blitter;
7422 if (!(blitter = heap_alloc(sizeof(*blitter))))
7423 return;
7425 TRACE("Created blitter %p.\n", blitter);
7427 blitter->ops = &vk_blitter_ops;
7428 blitter->next = *next;
7429 *next = blitter;