wined3d: Use wined3d_mask_from_size() in shader_glsl_gather4().
[wine.git] / dlls / wined3d / texture.c
blob34d7624908406ea0cbaad0793c54e9b7bc77bae9
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 "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag);
30 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
32 static const uint32_t wined3d_texture_sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_BUFFER;
33 static const struct wined3d_texture_ops texture_gl_ops;
35 struct wined3d_texture_idx
37 struct wined3d_texture *texture;
38 unsigned int sub_resource_idx;
41 struct wined3d_rect_f
43 float l;
44 float t;
45 float r;
46 float b;
49 BOOL wined3d_texture_can_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
51 if (!d3d_info->pbo || texture->resource.format->conv_byte_count
52 || (texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED)))
53 return FALSE;
55 return TRUE;
58 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_d3d_info *d3d_info)
60 if (!wined3d_texture_can_use_pbo(texture, d3d_info))
61 return FALSE;
63 /* Use a PBO for dynamic textures and read-only staging textures. */
64 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
65 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
66 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
69 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
70 const struct wined3d_gl_info *gl_info)
72 /* We don't expect to create texture views for textures with height-scaled formats.
73 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
74 return gl_info->supported[ARB_TEXTURE_STORAGE]
75 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
78 /* Front buffer coordinates are always full screen coordinates, but our GL
79 * drawable is limited to the window's client area. The sysmem and texture
80 * copies do have the full screen size. Note that GL has a bottom-left
81 * origin, while D3D has a top-left origin. */
82 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
84 unsigned int drawable_height;
85 POINT offset = {0, 0};
86 RECT windowsize;
88 if (!texture->swapchain)
89 return;
91 if (texture == texture->swapchain->front_buffer)
93 ScreenToClient(window, &offset);
94 OffsetRect(rect, offset.x, offset.y);
97 GetClientRect(window, &windowsize);
98 drawable_height = windowsize.bottom - windowsize.top;
100 rect->top = drawable_height - rect->top;
101 rect->bottom = drawable_height - rect->bottom;
104 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
106 const struct wined3d_swapchain *swapchain = texture->swapchain;
108 TRACE("texture %p.\n", texture);
110 if (!swapchain)
112 ERR("Texture %p is not part of a swapchain.\n", texture);
113 return GL_NONE;
116 if (texture == swapchain->front_buffer)
118 TRACE("Returning GL_FRONT.\n");
119 return GL_FRONT;
122 if (texture == swapchain->back_buffers[0])
124 TRACE("Returning GL_BACK.\n");
125 return GL_BACK;
128 FIXME("Higher back buffer, returning GL_BACK.\n");
129 return GL_BACK;
132 static DWORD wined3d_resource_access_from_location(DWORD location)
134 switch (location)
136 case WINED3D_LOCATION_DISCARDED:
137 return 0;
139 case WINED3D_LOCATION_SYSMEM:
140 return WINED3D_RESOURCE_ACCESS_CPU;
142 case WINED3D_LOCATION_BUFFER:
143 case WINED3D_LOCATION_DRAWABLE:
144 case WINED3D_LOCATION_TEXTURE_RGB:
145 case WINED3D_LOCATION_TEXTURE_SRGB:
146 case WINED3D_LOCATION_RB_MULTISAMPLE:
147 case WINED3D_LOCATION_RB_RESOLVED:
148 return WINED3D_RESOURCE_ACCESS_GPU;
150 default:
151 FIXME("Unhandled location %#x.\n", location);
152 return 0;
156 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
158 f->l = ((r->left * 2.0f) / w) - 1.0f;
159 f->t = ((r->top * 2.0f) / h) - 1.0f;
160 f->r = ((r->right * 2.0f) / w) - 1.0f;
161 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
164 void texture2d_get_blt_info(const struct wined3d_texture_gl *texture_gl,
165 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
167 struct wined3d_vec3 *coords = info->texcoords;
168 struct wined3d_rect_f f;
169 unsigned int level;
170 GLenum target;
171 GLsizei w, h;
173 level = sub_resource_idx % texture_gl->t.level_count;
174 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
175 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
176 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, sub_resource_idx);
178 switch (target)
180 default:
181 FIXME("Unsupported texture target %#x.\n", target);
182 /* Fall back to GL_TEXTURE_2D */
183 case GL_TEXTURE_2D:
184 info->bind_target = GL_TEXTURE_2D;
185 coords[0].x = (float)rect->left / w;
186 coords[0].y = (float)rect->top / h;
187 coords[0].z = 0.0f;
189 coords[1].x = (float)rect->right / w;
190 coords[1].y = (float)rect->top / h;
191 coords[1].z = 0.0f;
193 coords[2].x = (float)rect->left / w;
194 coords[2].y = (float)rect->bottom / h;
195 coords[2].z = 0.0f;
197 coords[3].x = (float)rect->right / w;
198 coords[3].y = (float)rect->bottom / h;
199 coords[3].z = 0.0f;
200 break;
202 case GL_TEXTURE_RECTANGLE_ARB:
203 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
204 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
205 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
206 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
207 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
208 break;
210 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
211 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
212 cube_coords_float(rect, w, h, &f);
214 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
215 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
216 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
217 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
218 break;
220 case GL_TEXTURE_CUBE_MAP_NEGATIVE_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_POSITIVE_Y:
231 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
232 cube_coords_float(rect, w, h, &f);
234 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
235 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
236 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
237 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
238 break;
240 case GL_TEXTURE_CUBE_MAP_NEGATIVE_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_POSITIVE_Z:
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 = -f.t; coords[0].z = 1.0f;
255 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
256 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
257 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
258 break;
260 case GL_TEXTURE_CUBE_MAP_NEGATIVE_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;
272 static bool fbo_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_gl_info *gl_info,
273 const struct wined3d_resource *src_resource, DWORD src_location,
274 const struct wined3d_resource *dst_resource, DWORD dst_location)
276 const struct wined3d_format *src_format = src_resource->format;
277 const struct wined3d_format *dst_format = dst_resource->format;
278 bool src_ds, dst_ds;
280 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
281 return false;
283 /* Source and/or destination need to be on the GL side. */
284 if (!(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
285 return false;
287 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
288 return false;
290 /* We can't copy between depth/stencil and colour attachments. One notable
291 * way we can end up here is when copying between typeless resources with
292 * formats like R16_TYPELESS, which can end up using either a
293 * depth/stencil or a colour format on the OpenGL side, depending on the
294 * resource's bind flags. */
295 src_ds = src_format->depth_size || src_format->stencil_size;
296 dst_ds = dst_format->depth_size || dst_format->stencil_size;
297 if (src_ds != dst_ds)
298 return false;
300 switch (blit_op)
302 case WINED3D_BLIT_OP_COLOR_BLIT:
303 if (!((src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
304 || (src_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
305 return false;
306 if (!((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
307 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
308 return false;
309 if ((src_format->id != dst_format->id || dst_location == WINED3D_LOCATION_DRAWABLE)
310 && (!is_identity_fixup(src_format->color_fixup) || !is_identity_fixup(dst_format->color_fixup)))
311 return false;
312 break;
314 case WINED3D_BLIT_OP_DEPTH_BLIT:
315 if (!(src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
316 return false;
317 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_DEPTH_STENCIL))
318 return false;
319 /* Accept pure swizzle fixups for depth formats. In general we
320 * ignore the stencil component (if present) at the moment and the
321 * swizzle is not relevant with just the depth component. */
322 if (is_complex_fixup(src_format->color_fixup) || is_complex_fixup(dst_format->color_fixup)
323 || is_scaling_fixup(src_format->color_fixup) || is_scaling_fixup(dst_format->color_fixup))
324 return false;
325 break;
327 default:
328 return false;
331 return true;
334 /* Blit between surface locations. Onscreen on different swapchains is not supported.
335 * Depth / stencil is not supported. Context activation is done by the caller. */
336 static void texture2d_blt_fbo(struct wined3d_device *device, struct wined3d_context *context,
337 enum wined3d_texture_filter_type filter, struct wined3d_texture *src_texture,
338 unsigned int src_sub_resource_idx, DWORD src_location, const RECT *src_rect,
339 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, DWORD dst_location,
340 const RECT *dst_rect, const struct wined3d_format *resolve_format)
342 struct wined3d_texture *required_texture, *restore_texture = NULL, *dst_save_texture = dst_texture;
343 unsigned int restore_idx, dst_save_sub_resource_idx = dst_sub_resource_idx;
344 struct wined3d_texture *src_staging_texture = NULL;
345 const struct wined3d_gl_info *gl_info;
346 struct wined3d_context_gl *context_gl;
347 bool resolve, scaled_resolve;
348 GLenum gl_filter;
349 GLenum buffer;
350 RECT s, d;
352 TRACE("device %p, context %p, filter %s, src_texture %p, src_sub_resource_idx %u, src_location %s, "
353 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, resolve format %p.\n",
354 device, context, debug_d3dtexturefiltertype(filter), src_texture, src_sub_resource_idx,
355 wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect), dst_texture,
356 dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect), resolve_format);
358 resolve = wined3d_texture_gl_is_multisample_location(wined3d_texture_gl(src_texture), src_location);
359 scaled_resolve = resolve
360 && (abs(src_rect->bottom - src_rect->top) != abs(dst_rect->bottom - dst_rect->top)
361 || abs(src_rect->right - src_rect->left) != abs(dst_rect->right - dst_rect->left));
363 if (filter == WINED3D_TEXF_LINEAR)
364 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_NICEST_EXT : GL_LINEAR;
365 else
366 gl_filter = scaled_resolve ? GL_SCALED_RESOLVE_FASTEST_EXT : GL_NEAREST;
368 if (resolve)
370 GLint resolve_internal, src_internal, dst_internal;
371 enum wined3d_format_id resolve_format_id;
373 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
374 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
375 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
376 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
378 if (resolve_format)
380 resolve_internal = wined3d_format_gl(resolve_format)->internal;
381 resolve_format_id = resolve_format->id;
383 else if (!wined3d_format_is_typeless(src_texture->resource.format))
385 resolve_internal = src_internal;
386 resolve_format_id = src_texture->resource.format->id;
388 else
390 resolve_internal = dst_internal;
391 resolve_format_id = dst_texture->resource.format->id;
394 /* In case of typeless resolve the texture type may not match the resolve type.
395 * To handle that, allocate intermediate texture(s) to resolve from/to.
396 * A possible performance improvement would be to resolve using a shader instead. */
397 if (src_internal != resolve_internal)
399 struct wined3d_resource_desc desc;
400 unsigned src_level;
401 HRESULT hr;
403 src_level = src_sub_resource_idx % src_texture->level_count;
404 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
405 desc.format = resolve_format_id;
406 desc.multisample_type = src_texture->resource.multisample_type;
407 desc.multisample_quality = src_texture->resource.multisample_quality;
408 desc.usage = WINED3DUSAGE_PRIVATE;
409 desc.bind_flags = 0;
410 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
411 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
412 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
413 desc.depth = 1;
414 desc.size = 0;
416 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
417 &src_staging_texture);
418 if (FAILED(hr))
420 ERR("Failed to create staging texture, hr %#x.\n", hr);
421 goto done;
424 if (src_location == WINED3D_LOCATION_DRAWABLE)
425 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the source of a typeless resolve.\n");
427 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
428 src_texture, src_sub_resource_idx, src_location, src_rect,
429 src_staging_texture, 0, src_location, src_rect,
430 NULL, WINED3D_TEXF_NONE, NULL);
432 src_texture = src_staging_texture;
433 src_sub_resource_idx = 0;
436 if (dst_internal != resolve_internal)
438 struct wined3d_resource_desc desc;
439 unsigned dst_level;
440 HRESULT hr;
442 dst_level = dst_sub_resource_idx % dst_texture->level_count;
443 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
444 desc.format = resolve_format_id;
445 desc.multisample_type = dst_texture->resource.multisample_type;
446 desc.multisample_quality = dst_texture->resource.multisample_quality;
447 desc.usage = WINED3DUSAGE_PRIVATE;
448 desc.bind_flags = 0;
449 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
450 desc.width = wined3d_texture_get_level_width(dst_texture, dst_level);
451 desc.height = wined3d_texture_get_level_height(dst_texture, dst_level);
452 desc.depth = 1;
453 desc.size = 0;
455 hr = wined3d_texture_create(device, &desc, 1, 1, 0, NULL, NULL, &wined3d_null_parent_ops,
456 &dst_texture);
457 if (FAILED(hr))
459 ERR("Failed to create staging texture, hr %#x.\n", hr);
460 goto done;
463 wined3d_texture_load_location(dst_texture, 0, context, dst_location);
464 dst_sub_resource_idx = 0;
468 /* Make sure the locations are up-to-date. Loading the destination
469 * surface isn't required if the entire surface is overwritten. (And is
470 * in fact harmful if we're being called by surface_load_location() with
471 * the purpose of loading the destination surface.) */
472 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
473 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
474 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
475 else
476 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
478 /* Acquire a context for the front-buffer, even though we may be blitting
479 * to/from a back-buffer. Since context_acquire() doesn't take the
480 * resource location into account, it may consider the back-buffer to be
481 * offscreen. */
482 if (src_location == WINED3D_LOCATION_DRAWABLE)
483 required_texture = src_texture->swapchain->front_buffer;
484 else if (dst_location == WINED3D_LOCATION_DRAWABLE)
485 required_texture = dst_texture->swapchain->front_buffer;
486 else
487 required_texture = NULL;
489 restore_texture = context->current_rt.texture;
490 restore_idx = context->current_rt.sub_resource_idx;
491 if (restore_texture != required_texture)
492 context = context_acquire(device, required_texture, 0);
493 else
494 restore_texture = NULL;
496 context_gl = wined3d_context_gl(context);
497 if (!context_gl->valid)
499 context_release(context);
500 WARN("Invalid context, skipping blit.\n");
501 restore_texture = NULL;
502 goto done;
505 gl_info = context_gl->gl_info;
507 if (src_location == WINED3D_LOCATION_DRAWABLE)
509 TRACE("Source texture %p is onscreen.\n", src_texture);
510 buffer = wined3d_texture_get_gl_buffer(src_texture);
511 s = *src_rect;
512 wined3d_texture_translate_drawable_coords(src_texture, context_gl->window, &s);
513 src_rect = &s;
515 else
517 TRACE("Source texture %p is offscreen.\n", src_texture);
518 buffer = GL_COLOR_ATTACHMENT0;
521 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER,
522 &src_texture->resource, src_sub_resource_idx, NULL, 0, src_location);
523 gl_info->gl_ops.gl.p_glReadBuffer(buffer);
524 checkGLcall("glReadBuffer()");
525 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
527 if (dst_location == WINED3D_LOCATION_DRAWABLE)
529 TRACE("Destination texture %p is onscreen.\n", dst_texture);
530 buffer = wined3d_texture_get_gl_buffer(dst_texture);
531 d = *dst_rect;
532 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
533 dst_rect = &d;
535 else
537 TRACE("Destination texture %p is offscreen.\n", dst_texture);
538 buffer = GL_COLOR_ATTACHMENT0;
541 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
542 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
543 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
544 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
545 context_invalidate_state(context, STATE_FRAMEBUFFER);
547 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
548 context_invalidate_state(context, STATE_BLEND);
550 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
551 context_invalidate_state(context, STATE_RASTERIZER);
553 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
554 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, GL_COLOR_BUFFER_BIT, gl_filter);
555 checkGLcall("glBlitFramebuffer()");
557 if (dst_location == WINED3D_LOCATION_DRAWABLE && dst_texture->swapchain->front_buffer == dst_texture)
558 gl_info->gl_ops.gl.p_glFlush();
560 if (dst_texture != dst_save_texture)
562 if (dst_location == WINED3D_LOCATION_DRAWABLE)
563 FIXME("WINED3D_LOCATION_DRAWABLE not supported for the destination of a typeless resolve.\n");
565 device->blitter->ops->blitter_blit(device->blitter, WINED3D_BLIT_OP_RAW_BLIT, context,
566 dst_texture, 0, dst_location, dst_rect,
567 dst_save_texture, dst_save_sub_resource_idx, dst_location, dst_rect,
568 NULL, WINED3D_TEXF_NONE, NULL);
571 done:
572 if (dst_texture != dst_save_texture)
573 wined3d_texture_decref(dst_texture);
575 if (src_staging_texture)
576 wined3d_texture_decref(src_staging_texture);
578 if (restore_texture)
579 context_restore(context, restore_texture, restore_idx);
582 static void texture2d_depth_blt_fbo(const struct wined3d_device *device, struct wined3d_context *context,
583 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, DWORD src_location,
584 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
585 DWORD dst_location, const RECT *dst_rect)
587 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
588 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
589 GLbitfield src_mask, dst_mask;
590 GLbitfield gl_mask;
592 TRACE("device %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
593 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s.\n", device,
594 src_texture, src_sub_resource_idx, wined3d_debug_location(src_location), wine_dbgstr_rect(src_rect),
595 dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location), wine_dbgstr_rect(dst_rect));
597 src_mask = 0;
598 if (src_texture->resource.format->depth_size)
599 src_mask |= GL_DEPTH_BUFFER_BIT;
600 if (src_texture->resource.format->stencil_size)
601 src_mask |= GL_STENCIL_BUFFER_BIT;
603 dst_mask = 0;
604 if (dst_texture->resource.format->depth_size)
605 dst_mask |= GL_DEPTH_BUFFER_BIT;
606 if (dst_texture->resource.format->stencil_size)
607 dst_mask |= GL_STENCIL_BUFFER_BIT;
609 if (src_mask != dst_mask)
611 ERR("Incompatible formats %s and %s.\n",
612 debug_d3dformat(src_texture->resource.format->id),
613 debug_d3dformat(dst_texture->resource.format->id));
614 return;
617 if (!src_mask)
619 ERR("Not a depth / stencil format: %s.\n",
620 debug_d3dformat(src_texture->resource.format->id));
621 return;
623 gl_mask = src_mask;
625 /* Make sure the locations are up-to-date. Loading the destination
626 * surface isn't required if the entire surface is overwritten. */
627 wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, src_location);
628 if (!wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
629 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
630 else
631 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
633 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_READ_FRAMEBUFFER, NULL, 0,
634 &src_texture->resource, src_sub_resource_idx, src_location);
635 wined3d_context_gl_check_fbo_status(context_gl, GL_READ_FRAMEBUFFER);
637 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER, NULL, 0,
638 &dst_texture->resource, dst_sub_resource_idx, dst_location);
639 wined3d_context_gl_set_draw_buffer(context_gl, GL_NONE);
640 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
641 context_invalidate_state(context, STATE_FRAMEBUFFER);
643 if (gl_mask & GL_DEPTH_BUFFER_BIT)
645 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
646 context_invalidate_state(context, STATE_DEPTH_STENCIL);
648 if (gl_mask & GL_STENCIL_BUFFER_BIT)
650 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
651 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
652 gl_info->gl_ops.gl.p_glStencilMask(~0U);
653 context_invalidate_state(context, STATE_DEPTH_STENCIL);
656 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
657 context_invalidate_state(context, STATE_RASTERIZER);
659 gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
660 dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
661 checkGLcall("glBlitFramebuffer()");
664 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
666 struct wined3d_texture_sub_resource *sub_resource;
667 unsigned int i, sub_count;
669 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
670 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
672 TRACE("Not evicting system memory for texture %p.\n", texture);
673 return;
676 TRACE("Evicting system memory for texture %p.\n", texture);
678 sub_count = texture->level_count * texture->layer_count;
679 for (i = 0; i < sub_count; ++i)
681 sub_resource = &texture->sub_resources[i];
682 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
683 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
684 i, texture);
685 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
687 wined3d_resource_free_sysmem(&texture->resource);
690 void wined3d_texture_validate_location(struct wined3d_texture *texture,
691 unsigned int sub_resource_idx, DWORD location)
693 struct wined3d_texture_sub_resource *sub_resource;
694 DWORD previous_locations;
696 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
697 texture, sub_resource_idx, wined3d_debug_location(location));
699 sub_resource = &texture->sub_resources[sub_resource_idx];
700 previous_locations = sub_resource->locations;
701 sub_resource->locations |= location;
702 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
703 && !--texture->sysmem_count)
704 wined3d_texture_evict_sysmem(texture);
706 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
709 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
711 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
714 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
715 unsigned int sub_resource_idx, DWORD location)
717 struct wined3d_texture_sub_resource *sub_resource;
718 DWORD previous_locations;
720 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
721 texture, sub_resource_idx, wined3d_debug_location(location));
723 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
724 wined3d_texture_set_dirty(texture);
726 sub_resource = &texture->sub_resources[sub_resource_idx];
727 previous_locations = sub_resource->locations;
728 sub_resource->locations &= ~location;
729 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
730 ++texture->sysmem_count;
732 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
734 if (!sub_resource->locations)
735 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
736 sub_resource_idx, texture);
739 void wined3d_texture_clear_dirty_regions(struct wined3d_texture *texture)
741 unsigned int i;
743 TRACE("texture %p\n", texture);
745 if (!texture->dirty_regions)
746 return;
748 for (i = 0; i < texture->layer_count; ++i)
750 texture->dirty_regions[i].box_count = 0;
754 /* Context activation is done by the caller. Context may be NULL in
755 * WINED3D_NO3D mode. */
756 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
757 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
759 DWORD current = texture->sub_resources[sub_resource_idx].locations;
760 BOOL ret;
762 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
763 texture, sub_resource_idx, context, wined3d_debug_location(location));
765 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
767 if (current & location)
769 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
770 return TRUE;
773 if (WARN_ON(d3d))
775 DWORD required_access = wined3d_resource_access_from_location(location);
776 if ((texture->resource.access & required_access) != required_access)
777 WARN("Operation requires %#x access, but texture only has %#x.\n",
778 required_access, texture->resource.access);
781 if (current & WINED3D_LOCATION_DISCARDED)
783 TRACE("Sub-resource previously discarded, nothing to do.\n");
784 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
785 return FALSE;
786 wined3d_texture_validate_location(texture, sub_resource_idx, location);
787 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
788 return TRUE;
791 if (!current)
793 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
794 sub_resource_idx, texture);
795 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
796 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
799 if ((location & wined3d_texture_sysmem_locations) && (current & wined3d_texture_sysmem_locations))
801 unsigned int size = texture->sub_resources[sub_resource_idx].size;
802 struct wined3d_bo_address source, destination;
804 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
805 return FALSE;
806 wined3d_texture_get_memory(texture, sub_resource_idx, &source, current);
807 wined3d_texture_get_memory(texture, sub_resource_idx, &destination, location);
808 wined3d_context_copy_bo_address(context, &destination, &source, size);
809 ret = TRUE;
811 else
812 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
814 if (ret)
815 wined3d_texture_validate_location(texture, sub_resource_idx, location);
817 return ret;
820 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
821 struct wined3d_bo_address *data, DWORD locations)
823 struct wined3d_texture_sub_resource *sub_resource;
825 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
826 texture, sub_resource_idx, data, wined3d_debug_location(locations));
828 sub_resource = &texture->sub_resources[sub_resource_idx];
829 if (locations & WINED3D_LOCATION_BUFFER)
831 data->addr = NULL;
832 data->buffer_object = &sub_resource->bo.b;
833 return;
836 if (locations & WINED3D_LOCATION_SYSMEM)
838 if (texture->sub_resources[sub_resource_idx].user_memory)
840 data->addr = texture->sub_resources[sub_resource_idx].user_memory;
842 else
844 data->addr = texture->resource.heap_memory;
845 data->addr += sub_resource->offset;
847 data->buffer_object = 0;
848 return;
851 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
852 data->addr = NULL;
853 data->buffer_object = 0;
856 /* Context activation is done by the caller. */
857 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
858 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
860 struct wined3d_bo_gl *bo = &texture->sub_resources[sub_resource_idx].bo.gl;
862 TRACE("texture %p, sub_resource_idx %u, context_gl %p.\n", texture, sub_resource_idx, context_gl);
864 wined3d_context_gl_destroy_bo(context_gl, bo);
865 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
868 static void wined3d_texture_unload_location(struct wined3d_texture *texture,
869 struct wined3d_context *context, unsigned int location)
871 texture->texture_ops->texture_unload_location(texture, context, location);
874 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
876 unsigned int sub_count = texture->level_count * texture->layer_count;
877 struct wined3d_device *device = texture->resource.device;
878 DWORD map_binding = texture->update_map_binding;
879 struct wined3d_context *context;
880 unsigned int i;
882 context = context_acquire(device, NULL, 0);
884 for (i = 0; i < sub_count; ++i)
886 if (texture->sub_resources[i].locations == texture->resource.map_binding
887 && !wined3d_texture_load_location(texture, i, context, map_binding))
888 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
891 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
892 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
894 context_release(context);
896 texture->resource.map_binding = map_binding;
897 texture->update_map_binding = 0;
900 static void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
902 texture->update_map_binding = map_binding;
903 if (!texture->resource.map_count)
904 wined3d_texture_update_map_binding(texture);
907 /* A GL context is provided by the caller */
908 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
909 struct gl_texture *tex)
911 context_gl_resource_released(device, tex->name, FALSE);
912 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
913 tex->name = 0;
916 /* Context activation is done by the caller. */
917 /* The caller is responsible for binding the correct texture. */
918 static void wined3d_texture_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
919 GLenum gl_internal_format, const struct wined3d_format_gl *format,
920 const struct wined3d_gl_info *gl_info)
922 unsigned int level, level_count, layer, layer_count;
923 GLsizei width, height, depth;
924 GLenum target;
926 level_count = texture_gl->t.level_count;
927 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
928 layer_count = 1;
929 else
930 layer_count = texture_gl->t.layer_count;
932 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
933 checkGLcall("glBindBuffer");
935 for (layer = 0; layer < layer_count; ++layer)
937 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
939 for (level = 0; level < level_count; ++level)
941 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
942 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
943 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
945 height *= format->f.height_scale.numerator;
946 height /= format->f.height_scale.denominator;
949 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
950 texture_gl, layer, level, target, width, height);
952 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
954 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
955 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
956 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.layer_count : depth, 0,
957 format->format, format->type, NULL));
958 checkGLcall("glTexImage3D");
960 else if (target == GL_TEXTURE_1D)
962 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
963 width, 0, format->format, format->type, NULL);
965 else
967 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
968 target == GL_TEXTURE_1D_ARRAY ? texture_gl->t.layer_count : height, 0,
969 format->format, format->type, NULL);
970 checkGLcall("glTexImage2D");
976 /* Context activation is done by the caller. */
977 /* The caller is responsible for binding the correct texture. */
978 static void wined3d_texture_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
979 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
981 unsigned int samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
982 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
983 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
984 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
985 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
987 switch (texture_gl->target)
989 case GL_TEXTURE_3D:
990 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
991 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
992 break;
993 case GL_TEXTURE_2D_ARRAY:
994 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
995 gl_internal_format, width, height, texture_gl->t.layer_count));
996 break;
997 case GL_TEXTURE_2D_MULTISAMPLE:
998 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
999 gl_internal_format, width, height, standard_pattern));
1000 break;
1001 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1002 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
1003 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
1004 break;
1005 case GL_TEXTURE_1D_ARRAY:
1006 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1007 gl_internal_format, width, texture_gl->t.layer_count));
1008 break;
1009 case GL_TEXTURE_1D:
1010 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
1011 break;
1012 default:
1013 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
1014 gl_internal_format, width, height));
1015 break;
1018 checkGLcall("allocate immutable storage");
1021 void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
1023 unsigned int sub_count = texture->level_count * texture->layer_count;
1024 struct wined3d_texture_sub_resource *sub_resource;
1025 unsigned int i;
1027 for (i = 0; i < sub_count; ++i)
1029 sub_resource = &texture->sub_resources[i];
1030 if (sub_resource->parent)
1032 TRACE("sub-resource %u.\n", i);
1033 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
1034 sub_resource->parent = NULL;
1039 static void wined3d_texture_create_dc(void *object)
1041 const struct wined3d_texture_idx *idx = object;
1042 struct wined3d_context *context = NULL;
1043 unsigned int sub_resource_idx, level;
1044 const struct wined3d_format *format;
1045 unsigned int row_pitch, slice_pitch;
1046 struct wined3d_texture *texture;
1047 struct wined3d_dc_info *dc_info;
1048 struct wined3d_bo_address data;
1049 D3DKMT_CREATEDCFROMMEMORY desc;
1050 struct wined3d_device *device;
1051 NTSTATUS status;
1053 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1055 texture = idx->texture;
1056 sub_resource_idx = idx->sub_resource_idx;
1057 level = sub_resource_idx % texture->level_count;
1058 device = texture->resource.device;
1060 format = texture->resource.format;
1061 if (!format->ddi_format)
1063 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1064 return;
1067 if (!texture->dc_info)
1069 unsigned int sub_count = texture->level_count * texture->layer_count;
1071 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
1073 ERR("Failed to allocate DC info.\n");
1074 return;
1078 if (!(texture->sub_resources[sub_resource_idx].locations & texture->resource.map_binding))
1080 context = context_acquire(device, NULL, 0);
1081 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1083 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1084 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
1085 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1086 if (data.buffer_object)
1088 if (!context)
1089 context = context_acquire(device, NULL, 0);
1090 desc.pMemory = wined3d_context_map_bo_address(context, &data,
1091 texture->sub_resources[sub_resource_idx].size, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
1093 else
1095 desc.pMemory = data.addr;
1098 if (context)
1099 context_release(context);
1101 desc.Format = format->ddi_format;
1102 desc.Width = wined3d_texture_get_level_width(texture, level);
1103 desc.Height = wined3d_texture_get_level_height(texture, level);
1104 desc.Pitch = row_pitch;
1105 desc.hDeviceDc = CreateCompatibleDC(NULL);
1106 desc.pColorTable = NULL;
1108 status = D3DKMTCreateDCFromMemory(&desc);
1109 DeleteDC(desc.hDeviceDc);
1110 if (status)
1112 WARN("Failed to create DC, status %#x.\n", status);
1113 return;
1116 dc_info = &texture->dc_info[sub_resource_idx];
1117 dc_info->dc = desc.hDc;
1118 dc_info->bitmap = desc.hBitmap;
1120 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
1123 static void wined3d_texture_destroy_dc(void *object)
1125 const struct wined3d_texture_idx *idx = object;
1126 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1127 struct wined3d_context *context;
1128 struct wined3d_texture *texture;
1129 struct wined3d_dc_info *dc_info;
1130 struct wined3d_bo_address data;
1131 unsigned int sub_resource_idx;
1132 struct wined3d_device *device;
1133 struct wined3d_range range;
1134 NTSTATUS status;
1136 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
1138 texture = idx->texture;
1139 sub_resource_idx = idx->sub_resource_idx;
1140 device = texture->resource.device;
1141 dc_info = &texture->dc_info[sub_resource_idx];
1143 if (!dc_info->dc)
1145 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
1146 return;
1149 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
1151 destroy_desc.hDc = dc_info->dc;
1152 destroy_desc.hBitmap = dc_info->bitmap;
1153 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1154 ERR("Failed to destroy dc, status %#x.\n", status);
1155 dc_info->dc = NULL;
1156 dc_info->bitmap = NULL;
1158 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1159 if (data.buffer_object)
1161 context = context_acquire(device, NULL, 0);
1162 range.offset = 0;
1163 range.size = texture->sub_resources[sub_resource_idx].size;
1164 wined3d_context_unmap_bo_address(context, &data, 1, &range);
1165 context_release(context);
1169 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
1171 texture->swapchain = swapchain;
1172 wined3d_resource_update_draw_binding(&texture->resource);
1175 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
1177 static const GLenum swizzle_source[] =
1179 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
1180 GL_ONE, /* CHANNEL_SOURCE_ONE */
1181 GL_RED, /* CHANNEL_SOURCE_X */
1182 GL_GREEN, /* CHANNEL_SOURCE_Y */
1183 GL_BLUE, /* CHANNEL_SOURCE_Z */
1184 GL_ALPHA, /* CHANNEL_SOURCE_W */
1187 swizzle[0] = swizzle_source[fixup.x_source];
1188 swizzle[1] = swizzle_source[fixup.y_source];
1189 swizzle[2] = swizzle_source[fixup.z_source];
1190 swizzle[3] = swizzle_source[fixup.w_source];
1193 /* Context activation is done by the caller. */
1194 void wined3d_texture_gl_bind(struct wined3d_texture_gl *texture_gl,
1195 struct wined3d_context_gl *context_gl, BOOL srgb)
1197 const struct wined3d_format *format = texture_gl->t.resource.format;
1198 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1199 const struct color_fixup_desc fixup = format->color_fixup;
1200 struct gl_texture *gl_tex;
1201 GLenum target;
1203 TRACE("texture_gl %p, context_gl %p, srgb %#x.\n", texture_gl, context_gl, srgb);
1205 if (!needs_separate_srgb_gl_texture(&context_gl->c, &texture_gl->t))
1206 srgb = FALSE;
1208 /* sRGB mode cache for preload() calls outside drawprim. */
1209 if (srgb)
1210 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
1211 else
1212 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
1214 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1215 target = texture_gl->target;
1217 if (gl_tex->name)
1219 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1220 return;
1223 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
1224 checkGLcall("glGenTextures");
1225 TRACE("Generated texture %d.\n", gl_tex->name);
1227 if (!gl_tex->name)
1229 ERR("Failed to generate a texture name.\n");
1230 return;
1233 /* Initialise the state of the texture object to the OpenGL defaults, not
1234 * the wined3d defaults. */
1235 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1236 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1237 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1238 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1239 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1240 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1241 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1242 gl_tex->sampler_desc.lod_bias = 0.0f;
1243 gl_tex->sampler_desc.min_lod = -1000.0f;
1244 gl_tex->sampler_desc.max_lod = 1000.0f;
1245 gl_tex->sampler_desc.max_anisotropy = 1;
1246 gl_tex->sampler_desc.compare = FALSE;
1247 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1248 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1249 gl_tex->sampler_desc.srgb_decode = TRUE;
1250 else
1251 gl_tex->sampler_desc.srgb_decode = srgb;
1252 gl_tex->base_level = 0;
1253 wined3d_texture_set_dirty(&texture_gl->t);
1255 wined3d_context_gl_bind_texture(context_gl, target, gl_tex->name);
1257 /* For a new texture we have to set the texture levels after binding the
1258 * texture. Beware that texture rectangles do not support mipmapping, but
1259 * set the maxmiplevel if we're relying on the partial
1260 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1261 * (I.e., do not care about cond_np2 here, just look for
1262 * GL_TEXTURE_RECTANGLE_ARB.) */
1263 if (target != GL_TEXTURE_RECTANGLE_ARB)
1265 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture_gl->t.level_count - 1);
1266 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1267 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1270 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1272 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1273 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1274 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1275 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1278 if (texture_gl->t.flags & WINED3D_TEXTURE_COND_NP2)
1280 /* Conditional non power of two textures use a different clamping
1281 * default. If we're using the GL_WINE_normalized_texrect partial
1282 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1283 * has the address mode set to repeat - something that prevents us
1284 * from hitting the accelerated codepath. Thus manually set the GL
1285 * state. The same applies to filtering. Even if the texture has only
1286 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1287 * fallback on macos. */
1288 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1289 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1290 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1291 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1292 checkGLcall("glTexParameteri");
1293 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1294 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1295 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1296 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1297 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1300 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1302 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1303 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1306 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(context_gl->c.d3d_info, format))
1308 GLint swizzle[4];
1310 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1311 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1312 checkGLcall("set format swizzle");
1316 /* Context activation is done by the caller. */
1317 void wined3d_texture_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
1318 struct wined3d_context_gl *context_gl, BOOL srgb)
1320 /* We don't need a specific texture unit, but after binding the texture
1321 * the current unit is dirty. Read the unit back instead of switching to
1322 * 0, this avoids messing around with the state manager's GL states. The
1323 * current texture unit should always be a valid one.
1325 * To be more specific, this is tricky because we can implicitly be
1326 * called from sampler() in state.c. This means we can't touch anything
1327 * other than whatever happens to be the currently active texture, or we
1328 * would risk marking already applied sampler states dirty again. */
1329 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1331 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1332 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1333 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1335 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1336 * a shader. */
1337 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1338 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1340 wined3d_texture_gl_bind(texture_gl, context_gl, srgb);
1343 /* Context activation is done by the caller (state handler). */
1344 /* This function relies on the correct texture being bound and loaded. */
1345 void wined3d_texture_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
1346 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context_gl *context_gl)
1348 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1349 GLenum target = texture_gl->target;
1350 struct gl_texture *gl_tex;
1351 DWORD state;
1353 TRACE("texture_gl %p, sampler_desc %p, context_gl %p.\n", texture_gl, sampler_desc, context_gl);
1355 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1357 state = sampler_desc->address_u;
1358 if (state != gl_tex->sampler_desc.address_u)
1360 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1361 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1362 gl_tex->sampler_desc.address_u = state;
1365 state = sampler_desc->address_v;
1366 if (state != gl_tex->sampler_desc.address_v)
1368 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1369 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1370 gl_tex->sampler_desc.address_v = state;
1373 state = sampler_desc->address_w;
1374 if (state != gl_tex->sampler_desc.address_w)
1376 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1377 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1378 gl_tex->sampler_desc.address_w = state;
1381 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1382 sizeof(gl_tex->sampler_desc.border_color)))
1384 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1385 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1386 sizeof(gl_tex->sampler_desc.border_color));
1389 state = sampler_desc->mag_filter;
1390 if (state != gl_tex->sampler_desc.mag_filter)
1392 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1393 gl_tex->sampler_desc.mag_filter = state;
1396 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1397 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1399 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1400 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1401 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1402 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1405 state = sampler_desc->max_anisotropy;
1406 if (state != gl_tex->sampler_desc.max_anisotropy)
1408 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1409 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1410 else
1411 WARN("Anisotropic filtering not supported.\n");
1412 gl_tex->sampler_desc.max_anisotropy = state;
1415 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1416 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1417 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1419 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1420 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1421 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1424 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1426 if (sampler_desc->compare)
1427 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1428 else
1429 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1430 gl_tex->sampler_desc.compare = sampler_desc->compare;
1433 checkGLcall("Texture parameter application");
1435 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1437 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1438 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1439 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1443 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1445 ULONG refcount;
1447 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1449 if (texture->swapchain)
1450 return wined3d_swapchain_incref(texture->swapchain);
1452 refcount = InterlockedIncrement(&texture->resource.ref);
1453 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1455 return refcount;
1458 static void wined3d_texture_destroy_object(void *object)
1460 struct wined3d_texture *texture = object;
1461 struct wined3d_resource *resource;
1462 struct wined3d_dc_info *dc_info;
1463 unsigned int sub_count;
1464 unsigned int i;
1466 TRACE("texture %p.\n", texture);
1468 resource = &texture->resource;
1469 sub_count = texture->level_count * texture->layer_count;
1471 if ((dc_info = texture->dc_info))
1473 for (i = 0; i < sub_count; ++i)
1475 if (dc_info[i].dc)
1477 struct wined3d_texture_idx texture_idx = {texture, i};
1479 wined3d_texture_destroy_dc(&texture_idx);
1482 heap_free(dc_info);
1485 if (texture->overlay_info)
1487 for (i = 0; i < sub_count; ++i)
1489 struct wined3d_overlay_info *info = &texture->overlay_info[i];
1490 struct wined3d_overlay_info *overlay, *cur;
1492 list_remove(&info->entry);
1493 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
1495 list_remove(&overlay->entry);
1498 heap_free(texture->overlay_info);
1501 if (texture->dirty_regions)
1503 for (i = 0; i < texture->layer_count; ++i)
1505 heap_free(texture->dirty_regions[i].boxes);
1507 heap_free(texture->dirty_regions);
1510 /* Discard the contents of resources with CPU access, to avoid downloading
1511 * them to SYSMEM on unload. */
1512 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
1514 for (i = 0; i < sub_count; ++i)
1516 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1517 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1520 resource->resource_ops->resource_unload(resource);
1523 void wined3d_texture_cleanup(struct wined3d_texture *texture)
1525 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1526 resource_cleanup(&texture->resource);
1529 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1531 wined3d_texture_sub_resources_destroyed(texture);
1532 wined3d_texture_cleanup(texture);
1533 wined3d_resource_wait_idle(&texture->resource);
1536 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1538 unsigned int i, sub_resource_count;
1539 ULONG refcount;
1541 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1543 if (texture->swapchain)
1544 return wined3d_swapchain_decref(texture->swapchain);
1546 refcount = InterlockedDecrement(&texture->resource.ref);
1547 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1549 if (!refcount)
1551 bool in_cs_thread = GetCurrentThreadId() == texture->resource.device->cs->thread_id;
1553 /* This is called from the CS thread to destroy temporary textures. */
1554 if (!in_cs_thread)
1555 wined3d_mutex_lock();
1556 /* Wait for the texture to become idle if it's using user memory,
1557 * since the application is allowed to free that memory once the
1558 * texture is destroyed. Note that this implies that
1559 * the destroy handler can't access that memory either. */
1560 sub_resource_count = texture->layer_count * texture->level_count;
1561 for (i = 0; i < sub_resource_count; ++i)
1563 if (texture->sub_resources[i].user_memory)
1565 wined3d_resource_wait_idle(&texture->resource);
1566 break;
1569 texture->resource.device->adapter->adapter_ops->adapter_destroy_texture(texture);
1570 if (!in_cs_thread)
1571 wined3d_mutex_unlock();
1574 return refcount;
1577 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1579 TRACE("texture %p.\n", texture);
1581 return &texture->resource;
1584 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1586 return c1->color_space_low_value == c2->color_space_low_value
1587 && c1->color_space_high_value == c2->color_space_high_value;
1590 /* Context activation is done by the caller */
1591 void wined3d_texture_load(struct wined3d_texture *texture,
1592 struct wined3d_context *context, BOOL srgb)
1594 UINT sub_count = texture->level_count * texture->layer_count;
1595 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1596 DWORD flag;
1597 UINT i;
1599 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1601 if (!needs_separate_srgb_gl_texture(context, texture))
1602 srgb = FALSE;
1604 if (srgb)
1605 flag = WINED3D_TEXTURE_SRGB_VALID;
1606 else
1607 flag = WINED3D_TEXTURE_RGB_VALID;
1609 if (!d3d_info->shader_color_key
1610 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1611 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1612 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1613 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1615 unsigned int sub_count = texture->level_count * texture->layer_count;
1616 unsigned int i;
1618 TRACE("Reloading because of color key value change.\n");
1619 for (i = 0; i < sub_count; i++)
1621 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1622 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1623 else
1624 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1627 texture->async.gl_color_key = texture->async.src_blt_color_key;
1630 if (texture->flags & flag)
1632 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1633 return;
1636 /* Reload the surfaces if the texture is marked dirty. */
1637 for (i = 0; i < sub_count; ++i)
1639 if (!wined3d_texture_load_location(texture, i, context,
1640 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1641 ERR("Failed to load location (srgb %#x).\n", srgb);
1643 texture->flags |= flag;
1646 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1648 TRACE("texture %p.\n", texture);
1650 return texture->resource.parent;
1653 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1654 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1656 const struct wined3d_resource *resource = &texture->resource;
1657 unsigned int width = wined3d_texture_get_level_width(texture, level);
1658 unsigned int height = wined3d_texture_get_level_height(texture, level);
1660 if (texture->row_pitch)
1662 *row_pitch = texture->row_pitch;
1663 *slice_pitch = texture->slice_pitch;
1664 return;
1667 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1668 width, height, row_pitch, slice_pitch);
1671 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1673 struct wined3d_resource *resource;
1674 DWORD old = texture->lod;
1676 TRACE("texture %p, lod %u.\n", texture, lod);
1678 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1679 * textures. The call always returns 0, and GetLOD always returns 0. */
1680 resource = &texture->resource;
1681 if (!wined3d_resource_access_is_managed(resource->access))
1683 TRACE("Ignoring LOD on texture with resource access %s.\n",
1684 wined3d_debug_resource_access(resource->access));
1685 return 0;
1688 if (lod >= texture->level_count)
1689 lod = texture->level_count - 1;
1691 if (texture->lod != lod)
1693 struct wined3d_device *device = resource->device;
1695 wined3d_resource_wait_idle(resource);
1696 texture->lod = lod;
1698 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1699 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1700 if (resource->bind_count)
1701 wined3d_device_context_emit_set_sampler_state(&device->cs->c, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1702 device->cs->c.state->sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1705 return old;
1708 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1710 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1712 return texture->lod;
1715 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1717 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1719 return texture->level_count;
1722 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1723 DWORD flags, const struct wined3d_color_key *color_key)
1725 struct wined3d_device *device = texture->resource.device;
1726 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1727 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1729 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1731 if (flags & ~all_flags)
1733 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1734 return WINED3DERR_INVALIDCALL;
1737 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1739 return WINED3D_OK;
1742 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1743 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1744 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1745 /* Context activation is done by the caller. */
1746 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1747 struct wined3d_context_gl *context_gl, unsigned int level, const struct wined3d_rendertarget_info *rt)
1749 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1750 struct wined3d_renderbuffer_entry *entry;
1751 unsigned int src_width, src_height;
1752 unsigned int width, height;
1753 GLuint renderbuffer = 0;
1755 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1756 return;
1758 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1760 struct wined3d_texture *rt_texture;
1761 unsigned int rt_level;
1763 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1765 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1766 return;
1768 rt_texture = wined3d_texture_from_resource(rt->resource);
1769 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1771 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1772 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1774 else
1776 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1777 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1780 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1781 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1783 /* A depth stencil smaller than the render target is not valid */
1784 if (width > src_width || height > src_height)
1785 return;
1787 /* Remove any renderbuffer set if the sizes match */
1788 if (width == src_width && height == src_height)
1790 texture_gl->current_renderbuffer = NULL;
1791 return;
1794 /* Look if we've already got a renderbuffer of the correct dimensions */
1795 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1797 if (entry->width == width && entry->height == height)
1799 renderbuffer = entry->id;
1800 texture_gl->current_renderbuffer = entry;
1801 break;
1805 if (!renderbuffer)
1807 const struct wined3d_format_gl *format_gl;
1809 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1810 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1811 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1812 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1814 entry = heap_alloc(sizeof(*entry));
1815 entry->width = width;
1816 entry->height = height;
1817 entry->id = renderbuffer;
1818 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1820 texture_gl->current_renderbuffer = entry;
1823 checkGLcall("set compatible renderbuffer");
1826 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1827 UINT width, UINT height, enum wined3d_format_id format_id,
1828 enum wined3d_multisample_type multisample_type, UINT multisample_quality, void *mem, UINT pitch)
1830 struct wined3d_texture_sub_resource *sub_resource;
1831 unsigned int i, level, sub_resource_count;
1832 const struct wined3d_d3d_info *d3d_info;
1833 const struct wined3d_gl_info *gl_info;
1834 const struct wined3d_format *format;
1835 struct wined3d_device *device;
1836 unsigned int resource_size;
1837 const struct wined3d *d3d;
1838 unsigned int slice_pitch;
1839 bool update_memory_only;
1840 bool create_dib = false;
1842 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1843 "mem %p, pitch %u, sub_resource_idx %u.\n",
1844 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch,
1845 sub_resource_idx);
1847 device = texture->resource.device;
1848 d3d = device->wined3d;
1849 gl_info = &device->adapter->gl_info;
1850 d3d_info = &device->adapter->d3d_info;
1851 format = wined3d_get_format(device->adapter, format_id, texture->resource.bind_flags);
1852 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1853 level = sub_resource_idx % texture->level_count;
1854 sub_resource_count = texture->level_count * texture->layer_count;
1856 update_memory_only = width == wined3d_texture_get_level_width(texture, level)
1857 && height == wined3d_texture_get_level_height(texture, level)
1858 && format_id == texture->resource.format->id && multisample_type == texture->resource.multisample_type
1859 && multisample_quality == texture->resource.multisample_quality;
1861 if (pitch)
1862 slice_pitch = height * pitch;
1863 else
1864 wined3d_format_calculate_pitch(format, 1, width, height, &pitch, &slice_pitch);
1866 if (update_memory_only)
1868 unsigned int current_row_pitch, current_slice_pitch;
1870 wined3d_texture_get_pitch(texture, level, &current_row_pitch, &current_slice_pitch);
1871 update_memory_only = pitch == current_row_pitch && slice_pitch == current_slice_pitch;
1874 if (!resource_size)
1875 return WINED3DERR_INVALIDCALL;
1877 if (sub_resource_count > 1 && !update_memory_only)
1879 FIXME("Texture has multiple sub-resources, not supported.\n");
1880 return WINED3DERR_INVALIDCALL;
1883 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1885 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1886 return WINED3DERR_INVALIDCALL;
1889 if (texture->resource.map_count)
1891 WARN("Texture is mapped.\n");
1892 return WINED3DERR_INVALIDCALL;
1895 /* We have no way of supporting a pitch that is not a multiple of the pixel
1896 * byte width short of uploading the texture row-by-row.
1897 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1898 * for user-memory textures (it always expects packed data) while DirectDraw
1899 * requires a 4-byte aligned pitch and doesn't support texture formats
1900 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1901 * This check is here to verify that the assumption holds. */
1902 if (pitch % format->byte_count)
1904 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1905 return WINED3DERR_INVALIDCALL;
1908 if (device->d3d_initialized)
1909 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1910 wined3d_resource_wait_idle(&texture->resource);
1912 if (texture->dc_info && texture->dc_info[0].dc)
1914 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
1916 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1917 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1918 create_dib = true;
1921 texture->sub_resources[sub_resource_idx].user_memory = mem;
1923 if (update_memory_only)
1925 for (i = 0; i < sub_resource_count; ++i)
1926 if (!texture->sub_resources[i].user_memory)
1927 break;
1929 if (i == sub_resource_count)
1930 wined3d_resource_free_sysmem(&texture->resource);
1932 else
1934 wined3d_resource_free_sysmem(&texture->resource);
1936 sub_resource = &texture->sub_resources[sub_resource_idx];
1938 texture->row_pitch = pitch;
1939 texture->slice_pitch = slice_pitch;
1941 texture->resource.format = format;
1942 texture->resource.multisample_type = multisample_type;
1943 texture->resource.multisample_quality = multisample_quality;
1944 texture->resource.width = width;
1945 texture->resource.height = height;
1946 if (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU) && d3d->flags & WINED3D_VIDMEM_ACCOUNTING)
1947 adapter_adjust_memory(device->adapter, (INT64)texture->slice_pitch - texture->resource.size);
1948 texture->resource.size = texture->slice_pitch;
1949 sub_resource->size = texture->slice_pitch;
1950 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1952 if (texture->texture_ops == &texture_gl_ops)
1954 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1956 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1957 texture->flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
1959 else
1961 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1962 texture->flags |= WINED3D_TEXTURE_DOWNLOADABLE;
1966 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1967 && !d3d_info->texture_npot_conditional)
1969 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1970 texture->pow2_width = texture->pow2_height = 1;
1971 while (texture->pow2_width < width)
1972 texture->pow2_width <<= 1;
1973 while (texture->pow2_height < height)
1974 texture->pow2_height <<= 1;
1976 else
1978 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1979 texture->pow2_width = width;
1980 texture->pow2_height = height;
1984 if (!mem && !wined3d_resource_prepare_sysmem(&texture->resource))
1985 ERR("Failed to allocate resource memory.\n");
1987 /* The format might be changed to a format that needs conversion.
1988 * If the surface didn't use PBOs previously but could now, don't
1989 * change it - whatever made us not use PBOs might come back, e.g.
1990 * color keys. */
1991 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, d3d_info))
1992 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1994 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_SYSMEM);
1995 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~WINED3D_LOCATION_SYSMEM);
1997 if (create_dib)
1999 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
2001 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
2002 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2005 return WINED3D_OK;
2008 /* Context activation is done by the caller. */
2009 static void wined3d_texture_gl_prepare_buffer_object(struct wined3d_texture_gl *texture_gl,
2010 unsigned int sub_resource_idx, struct wined3d_context_gl *context_gl)
2012 struct wined3d_texture_sub_resource *sub_resource;
2013 struct wined3d_bo_gl *bo;
2015 sub_resource = &texture_gl->t.sub_resources[sub_resource_idx];
2016 bo = &sub_resource->bo.gl;
2017 if (bo->id)
2018 return;
2020 if (!wined3d_context_gl_create_bo(context_gl, sub_resource->size, GL_PIXEL_UNPACK_BUFFER,
2021 GL_STREAM_DRAW, true, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo))
2022 return;
2024 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n", bo->id, texture_gl, sub_resource_idx);
2027 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
2029 unsigned int sub_count = texture->level_count * texture->layer_count;
2030 unsigned int i;
2032 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
2033 | WINED3D_TEXTURE_CONVERTED);
2034 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2035 for (i = 0; i < sub_count; ++i)
2037 wined3d_texture_invalidate_location(texture, i,
2038 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
2042 /* Context activation is done by the caller. */
2043 void wined3d_texture_gl_prepare_texture(struct wined3d_texture_gl *texture_gl,
2044 struct wined3d_context_gl *context_gl, BOOL srgb)
2046 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
2047 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
2048 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2049 struct wined3d_resource *resource = &texture_gl->t.resource;
2050 const struct wined3d_device *device = resource->device;
2051 const struct wined3d_format *format = resource->format;
2052 const struct wined3d_color_key_conversion *conversion;
2053 const struct wined3d_format_gl *format_gl;
2054 GLenum internal;
2056 TRACE("texture_gl %p, context_gl %p, format %s.\n", texture_gl, context_gl, debug_d3dformat(format->id));
2058 if (!d3d_info->shader_color_key
2059 && !(texture_gl->t.async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
2060 != !(texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT))
2062 wined3d_texture_force_reload(&texture_gl->t);
2064 if (texture_gl->t.async.color_key_flags & WINED3D_CKEY_SRC_BLT)
2065 texture_gl->t.async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
2068 if (texture_gl->t.flags & alloc_flag)
2069 return;
2071 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
2073 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
2074 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2075 format = wined3d_resource_get_decompress_format(resource);
2077 else if (format->conv_byte_count)
2079 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2081 else if ((conversion = wined3d_format_get_color_key_conversion(&texture_gl->t, TRUE)))
2083 texture_gl->t.flags |= WINED3D_TEXTURE_CONVERTED;
2084 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->bind_flags);
2085 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
2087 format_gl = wined3d_format_gl(format);
2089 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
2091 internal = wined3d_gl_get_internal_format(resource, format_gl, srgb);
2092 if (!internal)
2093 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
2095 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
2097 if (wined3d_texture_use_immutable_storage(&texture_gl->t, gl_info))
2098 wined3d_texture_gl_allocate_immutable_storage(texture_gl, internal, gl_info);
2099 else
2100 wined3d_texture_gl_allocate_mutable_storage(texture_gl, internal, format_gl, gl_info);
2101 texture_gl->t.flags |= alloc_flag;
2104 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
2105 const struct wined3d_gl_info *gl_info, BOOL multisample)
2107 const struct wined3d_format_gl *format_gl;
2109 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
2110 if (multisample)
2112 DWORD samples;
2114 if (texture_gl->rb_multisample)
2115 return;
2117 samples = wined3d_resource_get_sample_count(&texture_gl->t.resource);
2119 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
2120 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
2121 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
2122 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
2123 checkGLcall("glRenderbufferStorageMultisample()");
2124 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
2126 else
2128 if (texture_gl->rb_resolved)
2129 return;
2131 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
2132 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
2133 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
2134 texture_gl->t.resource.width, texture_gl->t.resource.height);
2135 checkGLcall("glRenderbufferStorage()");
2136 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
2140 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture,
2141 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
2143 return texture->texture_ops->texture_prepare_location(texture, sub_resource_idx, context, location);
2146 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
2147 unsigned int sub_resource_idx)
2149 UINT sub_count = texture->level_count * texture->layer_count;
2151 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2153 if (sub_resource_idx >= sub_count)
2155 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2156 return NULL;
2159 return &texture->sub_resources[sub_resource_idx];
2162 static void wined3d_texture_dirty_region_add(struct wined3d_texture *texture,
2163 unsigned int layer, const struct wined3d_box *box)
2165 struct wined3d_dirty_regions *regions;
2166 unsigned int count;
2168 if (!texture->dirty_regions)
2169 return;
2171 regions = &texture->dirty_regions[layer];
2172 count = regions->box_count + 1;
2173 if (count >= WINED3D_MAX_DIRTY_REGION_COUNT || !box
2174 || (!box->left && !box->top && !box->front
2175 && box->right == texture->resource.width
2176 && box->bottom == texture->resource.height
2177 && box->back == texture->resource.depth))
2179 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2180 return;
2183 if (!wined3d_array_reserve((void **)&regions->boxes, &regions->boxes_size, count, sizeof(*regions->boxes)))
2185 WARN("Failed to grow boxes array, marking entire texture dirty.\n");
2186 regions->box_count = WINED3D_MAX_DIRTY_REGION_COUNT;
2187 return;
2190 regions->boxes[regions->box_count++] = *box;
2193 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
2194 UINT layer, const struct wined3d_box *dirty_region)
2196 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
2198 if (layer >= texture->layer_count)
2200 WARN("Invalid layer %u specified.\n", layer);
2201 return WINED3DERR_INVALIDCALL;
2204 if (dirty_region && FAILED(wined3d_resource_check_box_dimensions(&texture->resource, 0, dirty_region)))
2206 WARN("Invalid dirty_region %s specified.\n", debug_box(dirty_region));
2207 return WINED3DERR_INVALIDCALL;
2210 wined3d_texture_dirty_region_add(texture, layer, dirty_region);
2211 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
2213 return WINED3D_OK;
2216 static void wined3d_texture_gl_upload_bo(const struct wined3d_format *src_format, GLenum target,
2217 unsigned int level, unsigned int src_row_pitch, unsigned int src_slice_pitch,
2218 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, unsigned int update_w,
2219 unsigned int update_h, unsigned int update_d, const BYTE *addr, BOOL srgb,
2220 struct wined3d_texture *dst_texture, const struct wined3d_gl_info *gl_info)
2222 const struct wined3d_format_gl *format_gl = wined3d_format_gl(src_format);
2224 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2226 GLenum internal = wined3d_gl_get_internal_format(&dst_texture->resource, format_gl, srgb);
2227 unsigned int dst_row_pitch, dst_slice_pitch;
2229 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2231 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2232 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2233 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2234 update_d, internal, dst_slice_pitch, addr);
2236 if (target == GL_TEXTURE_1D)
2238 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2239 update_w, internal, dst_row_pitch, addr));
2241 else
2243 unsigned int row, y, slice, slice_count = 1, row_count = 1;
2245 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2246 * can't use the unpack row length like for glTexSubImage2D. */
2247 if (dst_row_pitch != src_row_pitch)
2249 row_count = (update_h + src_format->block_height - 1) / src_format->block_height;
2250 update_h = src_format->block_height;
2251 wined3d_format_calculate_pitch(src_format, 1, update_w, update_h,
2252 &dst_row_pitch, &dst_slice_pitch);
2255 if (dst_slice_pitch != src_slice_pitch)
2257 slice_count = update_d;
2258 update_d = 1;
2261 for (slice = 0; slice < slice_count; ++slice)
2263 for (row = 0, y = dst_y; row < row_count; ++row)
2265 const BYTE *upload_addr = &addr[slice * src_slice_pitch + row * src_row_pitch];
2267 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2269 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, dst_z + slice, update_w,
2270 update_h, update_d, internal, update_d * dst_slice_pitch, upload_addr));
2272 else
2274 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y, update_w,
2275 update_h, internal, dst_slice_pitch, upload_addr));
2278 y += src_format->block_height;
2282 checkGLcall("Upload compressed texture data");
2284 else
2286 unsigned int y, y_count, z, z_count;
2287 bool unpacking_rows = false;
2289 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2290 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2291 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2292 update_d, format_gl->format, format_gl->type, addr);
2294 if (src_row_pitch && !(src_row_pitch % src_format->byte_count))
2296 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / src_format->byte_count);
2297 y_count = 1;
2298 unpacking_rows = true;
2300 else
2302 y_count = update_h;
2303 update_h = 1;
2306 if (src_slice_pitch && unpacking_rows && !(src_slice_pitch % src_row_pitch))
2308 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, src_slice_pitch / src_row_pitch);
2309 z_count = 1;
2311 else if (src_slice_pitch && !unpacking_rows && !(src_slice_pitch % (update_w * src_format->byte_count)))
2313 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT,
2314 src_slice_pitch / (update_w * src_format->byte_count));
2315 z_count = 1;
2317 else
2319 z_count = update_d;
2320 update_d = 1;
2323 for (z = 0; z < z_count; ++z)
2325 for (y = 0; y < y_count; ++y)
2327 const BYTE *upload_addr = &addr[z * src_slice_pitch + y * src_row_pitch];
2328 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2330 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y + y, dst_z + z, update_w,
2331 update_h, update_d, format_gl->format, format_gl->type, upload_addr));
2333 else if (target == GL_TEXTURE_1D)
2335 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2336 update_w, format_gl->format, format_gl->type, upload_addr);
2338 else
2340 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y + y,
2341 update_w, update_h, format_gl->format, format_gl->type, upload_addr);
2345 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2346 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_IMAGE_HEIGHT, 0);
2347 checkGLcall("Upload texture data");
2351 static const struct d3dfmt_alpha_fixup
2353 enum wined3d_format_id format_id, conv_format_id;
2355 formats_src_alpha_fixup[] =
2357 {WINED3DFMT_B8G8R8X8_UNORM, WINED3DFMT_B8G8R8A8_UNORM},
2358 {WINED3DFMT_B5G5R5X1_UNORM, WINED3DFMT_B5G5R5A1_UNORM},
2359 {WINED3DFMT_B4G4R4X4_UNORM, WINED3DFMT_B4G4R4A4_UNORM},
2362 static enum wined3d_format_id wined3d_get_alpha_fixup_format(enum wined3d_format_id format_id,
2363 const struct wined3d_format *dst_format)
2365 unsigned int i;
2367 if (!(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2368 && !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, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
2463 dst_texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
2466 if (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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_memory(&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_flags & WINED3DFMT_FLAG_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_flags & WINED3DFMT_FLAG_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.gl.id)
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, &data, sub_resource->locations);
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 /* Context activation is done by the caller. */
3289 static BOOL wined3d_texture_gl_load_location(struct wined3d_texture *texture,
3290 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
3292 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3293 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3295 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
3296 texture, sub_resource_idx, context, wined3d_debug_location(location));
3298 if (!wined3d_texture_gl_prepare_location(texture, sub_resource_idx, context, location))
3299 return FALSE;
3301 switch (location)
3303 case WINED3D_LOCATION_SYSMEM:
3304 case WINED3D_LOCATION_BUFFER:
3305 return wined3d_texture_gl_load_sysmem(texture_gl, sub_resource_idx, context_gl, location);
3307 case WINED3D_LOCATION_DRAWABLE:
3308 return wined3d_texture_load_drawable(texture, sub_resource_idx, context);
3310 case WINED3D_LOCATION_RB_RESOLVED:
3311 case WINED3D_LOCATION_RB_MULTISAMPLE:
3312 return wined3d_texture_load_renderbuffer(texture, sub_resource_idx, context, location);
3314 case WINED3D_LOCATION_TEXTURE_RGB:
3315 case WINED3D_LOCATION_TEXTURE_SRGB:
3316 return wined3d_texture_gl_load_texture(texture_gl, sub_resource_idx,
3317 context_gl, location == WINED3D_LOCATION_TEXTURE_SRGB);
3319 default:
3320 FIXME("Unhandled %s load from %s.\n", wined3d_debug_location(location),
3321 wined3d_debug_location(texture->sub_resources[sub_resource_idx].locations));
3322 return FALSE;
3326 static void wined3d_texture_gl_unload_location(struct wined3d_texture *texture,
3327 struct wined3d_context *context, unsigned int location)
3329 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture);
3330 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
3331 struct wined3d_renderbuffer_entry *entry, *entry2;
3332 unsigned int i, sub_count;
3334 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
3336 switch (location)
3338 case WINED3D_LOCATION_BUFFER:
3339 sub_count = texture->level_count * texture->layer_count;
3340 for (i = 0; i < sub_count; ++i)
3342 if (texture_gl->t.sub_resources[i].bo.gl.id)
3343 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context_gl);
3345 break;
3347 case WINED3D_LOCATION_TEXTURE_RGB:
3348 if (texture_gl->texture_rgb.name)
3349 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_rgb);
3350 break;
3352 case WINED3D_LOCATION_TEXTURE_SRGB:
3353 if (texture_gl->texture_srgb.name)
3354 gltexture_delete(texture_gl->t.resource.device, context_gl->gl_info, &texture_gl->texture_srgb);
3355 break;
3357 case WINED3D_LOCATION_RB_MULTISAMPLE:
3358 if (texture_gl->rb_multisample)
3360 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
3361 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_multisample, TRUE);
3362 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
3363 texture_gl->rb_multisample = 0;
3365 break;
3367 case WINED3D_LOCATION_RB_RESOLVED:
3368 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers,
3369 struct wined3d_renderbuffer_entry, entry)
3371 context_gl_resource_released(texture_gl->t.resource.device, entry->id, TRUE);
3372 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
3373 list_remove(&entry->entry);
3374 heap_free(entry);
3376 list_init(&texture_gl->renderbuffers);
3377 texture_gl->current_renderbuffer = NULL;
3379 if (texture_gl->rb_resolved)
3381 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
3382 context_gl_resource_released(texture_gl->t.resource.device, texture_gl->rb_resolved, TRUE);
3383 context_gl->gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
3384 texture_gl->rb_resolved = 0;
3386 break;
3388 default:
3389 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
3390 break;
3394 static const struct wined3d_texture_ops texture_gl_ops =
3396 wined3d_texture_gl_prepare_location,
3397 wined3d_texture_gl_load_location,
3398 wined3d_texture_gl_unload_location,
3399 wined3d_texture_gl_upload_data,
3400 wined3d_texture_gl_download_data,
3403 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
3405 return texture_from_resource(resource);
3408 static ULONG texture_resource_incref(struct wined3d_resource *resource)
3410 return wined3d_texture_incref(texture_from_resource(resource));
3413 static ULONG texture_resource_decref(struct wined3d_resource *resource)
3415 return wined3d_texture_decref(texture_from_resource(resource));
3418 static void texture_resource_preload(struct wined3d_resource *resource)
3420 struct wined3d_texture *texture = texture_from_resource(resource);
3421 struct wined3d_context *context;
3423 context = context_acquire(resource->device, NULL, 0);
3424 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
3425 context_release(context);
3428 static void texture_resource_unload(struct wined3d_resource *resource)
3430 struct wined3d_texture *texture = texture_from_resource(resource);
3431 struct wined3d_device *device = resource->device;
3432 unsigned int location = resource->map_binding;
3433 struct wined3d_context *context;
3434 unsigned int sub_count, i;
3436 TRACE("resource %p.\n", resource);
3438 /* D3D is not initialised, so no GPU locations should currently exist.
3439 * Moreover, we may not be able to acquire a valid context. */
3440 if (!device->d3d_initialized)
3441 return;
3443 context = context_acquire(device, NULL, 0);
3445 if (location == WINED3D_LOCATION_BUFFER)
3446 location = WINED3D_LOCATION_SYSMEM;
3448 sub_count = texture->level_count * texture->layer_count;
3449 for (i = 0; i < sub_count; ++i)
3451 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
3452 && wined3d_texture_load_location(texture, i, context, location))
3454 wined3d_texture_invalidate_location(texture, i, ~location);
3456 else
3458 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU)
3459 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
3460 debug_d3dresourcetype(resource->type), resource, i,
3461 wined3d_debug_resource_access(resource->access));
3462 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
3463 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
3467 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_BUFFER);
3468 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_RGB);
3469 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_TEXTURE_SRGB);
3470 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_MULTISAMPLE);
3471 wined3d_texture_unload_location(texture, context, WINED3D_LOCATION_RB_RESOLVED);
3473 context_release(context);
3475 wined3d_texture_force_reload(texture);
3476 if (texture->resource.bind_count)
3477 device_invalidate_state(device, STATE_SAMPLER(texture->sampler));
3478 wined3d_texture_set_dirty(texture);
3480 resource_unload(&texture->resource);
3483 static HRESULT texture_resource_sub_resource_get_desc(struct wined3d_resource *resource,
3484 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3486 const struct wined3d_texture *texture = texture_from_resource(resource);
3488 return wined3d_texture_get_sub_resource_desc(texture, sub_resource_idx, desc);
3491 static void texture_resource_sub_resource_get_map_pitch(struct wined3d_resource *resource,
3492 unsigned int sub_resource_idx, unsigned int *row_pitch, unsigned int *slice_pitch)
3494 const struct wined3d_texture *texture = texture_from_resource(resource);
3495 unsigned int level = sub_resource_idx % texture->level_count;
3497 if (resource->format_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
3499 *row_pitch = wined3d_texture_get_level_width(texture, level) * resource->format->byte_count;
3500 *slice_pitch = wined3d_texture_get_level_height(texture, level) * (*row_pitch);
3502 else
3504 wined3d_texture_get_pitch(texture, level, row_pitch, slice_pitch);
3508 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
3509 void **map_ptr, const struct wined3d_box *box, DWORD flags)
3511 struct wined3d_texture_sub_resource *sub_resource;
3512 struct wined3d_device *device = resource->device;
3513 struct wined3d_context *context;
3514 struct wined3d_texture *texture;
3515 struct wined3d_bo_address data;
3516 unsigned int texture_level;
3517 BYTE *base_memory;
3518 BOOL ret;
3520 TRACE("resource %p, sub_resource_idx %u, map_ptr %p, box %s, flags %#x.\n",
3521 resource, sub_resource_idx, map_ptr, debug_box(box), flags);
3523 texture = texture_from_resource(resource);
3524 sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx);
3526 texture_level = sub_resource_idx % texture->level_count;
3528 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3530 WARN("DC is in use.\n");
3531 return WINED3DERR_INVALIDCALL;
3534 if (sub_resource->map_count)
3536 WARN("Sub-resource is already mapped.\n");
3537 return WINED3DERR_INVALIDCALL;
3540 context = context_acquire(device, NULL, 0);
3542 if (flags & WINED3D_MAP_DISCARD)
3544 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
3545 wined3d_debug_location(resource->map_binding));
3546 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
3547 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
3549 else
3551 if (resource->usage & WINED3DUSAGE_DYNAMIC)
3552 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
3553 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
3556 if (!ret)
3558 ERR("Failed to prepare location.\n");
3559 context_release(context);
3560 return E_OUTOFMEMORY;
3563 /* We only record dirty regions for the top-most level. */
3564 if (texture->dirty_regions && flags & WINED3D_MAP_WRITE
3565 && !(flags & WINED3D_MAP_NO_DIRTY_UPDATE) && !texture_level)
3566 wined3d_texture_dirty_region_add(texture, sub_resource_idx / texture->level_count, box);
3568 if (flags & WINED3D_MAP_WRITE
3569 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
3570 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
3572 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
3573 base_memory = wined3d_context_map_bo_address(context, &data, sub_resource->size, flags);
3574 sub_resource->map_flags = flags;
3575 TRACE("Base memory pointer %p.\n", base_memory);
3577 context_release(context);
3579 *map_ptr = resource_offset_map_pointer(resource, sub_resource_idx, base_memory, box);
3581 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3583 RECT *r = &texture->swapchain->front_buffer_update;
3585 SetRect(r, box->left, box->top, box->right, box->bottom);
3586 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
3589 ++resource->map_count;
3590 ++sub_resource->map_count;
3592 TRACE("Returning memory %p.\n", *map_ptr);
3594 return WINED3D_OK;
3597 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
3599 struct wined3d_texture_sub_resource *sub_resource;
3600 struct wined3d_device *device = resource->device;
3601 struct wined3d_context *context;
3602 struct wined3d_texture *texture;
3603 struct wined3d_bo_address data;
3604 struct wined3d_range range;
3606 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
3608 texture = texture_from_resource(resource);
3609 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3610 return E_INVALIDARG;
3612 if (!sub_resource->map_count)
3614 WARN("Trying to unmap unmapped sub-resource.\n");
3615 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
3616 return WINED3D_OK;
3617 return WINEDDERR_NOTLOCKED;
3620 context = context_acquire(device, NULL, 0);
3622 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
3623 range.offset = 0;
3624 range.size = sub_resource->size;
3625 wined3d_context_unmap_bo_address(context, &data, !!(sub_resource->map_flags & WINED3D_MAP_WRITE), &range);
3627 context_release(context);
3629 if (texture->swapchain && texture->swapchain->front_buffer == texture)
3631 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
3632 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
3635 --sub_resource->map_count;
3636 if (!--resource->map_count && texture->update_map_binding)
3637 wined3d_texture_update_map_binding(texture);
3639 return WINED3D_OK;
3642 static const struct wined3d_resource_ops texture_resource_ops =
3644 texture_resource_incref,
3645 texture_resource_decref,
3646 texture_resource_preload,
3647 texture_resource_unload,
3648 texture_resource_sub_resource_get_desc,
3649 texture_resource_sub_resource_get_map_pitch,
3650 texture_resource_sub_resource_map,
3651 texture_resource_sub_resource_unmap,
3654 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
3655 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
3656 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
3657 const struct wined3d_texture_ops *texture_ops)
3659 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
3660 struct wined3d_device_parent *device_parent = device->device_parent;
3661 unsigned int sub_count, i, j, size, offset = 0;
3662 unsigned int pow2_width, pow2_height;
3663 const struct wined3d_format *format;
3664 HRESULT hr;
3666 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
3667 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
3668 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
3669 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
3670 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
3671 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
3672 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
3674 if (!desc->width || !desc->height || !desc->depth)
3675 return WINED3DERR_INVALIDCALL;
3677 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
3679 ERR("Invalid layer count for volume texture.\n");
3680 return E_INVALIDARG;
3683 texture->sub_resources = sub_resources;
3685 /* TODO: It should only be possible to create textures for formats
3686 * that are reported as supported. */
3687 if (WINED3DFMT_UNKNOWN >= desc->format)
3689 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
3690 return WINED3DERR_INVALIDCALL;
3692 format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
3694 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
3695 || desc->usage & WINED3DUSAGE_SCRATCH))
3697 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
3698 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
3699 return WINED3DERR_INVALIDCALL;
3702 pow2_width = desc->width;
3703 pow2_height = desc->height;
3704 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
3705 && !d3d_info->texture_npot)
3707 /* level_count == 0 returns an error as well. */
3708 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
3710 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
3712 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
3713 "texture without unconditional NPOT support.\n");
3714 return WINED3DERR_INVALIDCALL;
3717 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
3719 texture->flags |= WINED3D_TEXTURE_COND_NP2;
3721 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
3723 /* TODO: Add support for non-power-of-two compressed textures. */
3724 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
3725 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
3727 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
3728 desc->width, desc->height);
3729 return WINED3DERR_NOTAVAILABLE;
3732 /* Find the nearest pow2 match. */
3733 pow2_width = pow2_height = 1;
3734 while (pow2_width < desc->width)
3735 pow2_width <<= 1;
3736 while (pow2_height < desc->height)
3737 pow2_height <<= 1;
3738 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
3741 texture->pow2_width = pow2_width;
3742 texture->pow2_height = pow2_height;
3744 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
3745 && (desc->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
3747 /* One of four options:
3748 * 1: Do the same as we do with NPOT and scale the texture. (Any
3749 * texture ops would require the texture to be scaled which is
3750 * potentially slow.)
3751 * 2: Set the texture to the maximum size (bad idea).
3752 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
3753 * 4: Create the surface, but allow it to be used only for DirectDraw
3754 * Blts. Some apps (e.g. Swat 3) create textures with a height of
3755 * 16 and a width > 3000 and blt 16x16 letter areas from them to
3756 * the render target. */
3757 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
3759 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
3760 return WINED3DERR_NOTAVAILABLE;
3763 /* We should never use this surface in combination with OpenGL. */
3764 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
3767 for (i = 0; i < layer_count; ++i)
3769 for (j = 0; j < level_count; ++j)
3771 unsigned int idx = i * level_count + j;
3773 size = wined3d_format_calculate_size(format, device->surface_alignment,
3774 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3775 texture->sub_resources[idx].offset = offset;
3776 texture->sub_resources[idx].size = size;
3777 offset += size;
3779 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3782 if (!offset)
3783 return WINED3DERR_INVALIDCALL;
3785 /* Ensure the last mip-level is at least large enough to hold a single
3786 * compressed block. It is questionable how useful these mip-levels are to
3787 * the application with "broken pitch" formats, but we want to avoid
3788 * memory corruption when loading textures into WINED3D_LOCATION_SYSMEM. */
3789 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_BROKEN_PITCH)
3791 unsigned int min_size;
3793 min_size = texture->sub_resources[level_count * layer_count - 1].offset + format->block_byte_count;
3794 min_size = (min_size + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3795 if (min_size > offset)
3796 offset = min_size;
3799 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3800 desc->multisample_type, desc->multisample_quality, desc->usage, desc->bind_flags, desc->access,
3801 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3803 static unsigned int once;
3805 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3806 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3807 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3808 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3809 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3810 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3812 WARN("Failed to initialize resource, returning %#x\n", hr);
3813 return hr;
3815 wined3d_resource_update_draw_binding(&texture->resource);
3817 texture->texture_ops = texture_ops;
3819 texture->layer_count = layer_count;
3820 texture->level_count = level_count;
3821 texture->lod = 0;
3822 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS
3823 | WINED3D_TEXTURE_DOWNLOADABLE;
3824 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3825 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3826 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3827 texture->flags |= WINED3D_TEXTURE_GET_DC;
3828 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3829 texture->flags |= WINED3D_TEXTURE_DISCARD;
3830 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3832 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3833 WARN("Format doesn't support mipmaps generation, "
3834 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3835 else
3836 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3839 if (flags & WINED3D_TEXTURE_CREATE_RECORD_DIRTY_REGIONS
3840 && !(texture->dirty_regions = heap_calloc(texture->layer_count, sizeof(*texture->dirty_regions))))
3842 wined3d_texture_cleanup_sync(texture);
3843 return E_OUTOFMEMORY;
3846 /* Precalculated scaling for 'faked' non power of two texture coords. */
3847 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3849 texture->pow2_matrix[0] = (float)desc->width;
3850 texture->pow2_matrix[5] = (float)desc->height;
3851 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3853 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3855 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3856 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3857 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3859 else
3861 texture->pow2_matrix[0] = 1.0f;
3862 texture->pow2_matrix[5] = 1.0f;
3864 texture->pow2_matrix[10] = 1.0f;
3865 texture->pow2_matrix[15] = 1.0f;
3866 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3868 if (wined3d_texture_use_pbo(texture, d3d_info))
3869 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3871 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3872 || !wined3d_texture_use_pbo(texture, d3d_info))
3874 if (!wined3d_resource_prepare_sysmem(&texture->resource))
3876 wined3d_texture_cleanup_sync(texture);
3877 return E_OUTOFMEMORY;
3881 sub_count = level_count * layer_count;
3882 if (sub_count / layer_count != level_count)
3884 wined3d_texture_cleanup_sync(texture);
3885 return E_OUTOFMEMORY;
3888 if (desc->usage & WINED3DUSAGE_OVERLAY)
3890 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3892 wined3d_texture_cleanup_sync(texture);
3893 return E_OUTOFMEMORY;
3896 for (i = 0; i < sub_count; ++i)
3898 list_init(&texture->overlay_info[i].entry);
3899 list_init(&texture->overlay_info[i].overlays);
3903 /* Generate all sub-resources. */
3904 for (i = 0; i < sub_count; ++i)
3906 struct wined3d_texture_sub_resource *sub_resource;
3908 sub_resource = &texture->sub_resources[i];
3909 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3910 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3912 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3913 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3916 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3917 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3919 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3920 sub_resource->parent = NULL;
3921 wined3d_texture_cleanup_sync(texture);
3922 return hr;
3925 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3927 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3928 i, i % texture->level_count, i / texture->level_count);
3930 if (desc->usage & WINED3DUSAGE_OWNDC)
3932 struct wined3d_texture_idx texture_idx = {texture, i};
3934 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3935 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3936 if (!texture->dc_info || !texture->dc_info[i].dc)
3938 wined3d_texture_cleanup_sync(texture);
3939 return WINED3DERR_INVALIDCALL;
3944 return WINED3D_OK;
3947 HRESULT CDECL wined3d_device_context_blt(struct wined3d_device_context *context,
3948 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, const RECT *dst_rect,
3949 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, const RECT *src_rect,
3950 unsigned int flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3952 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3953 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3954 HRESULT hr;
3956 TRACE("context %p, dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3957 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3958 context, dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3959 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3961 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3962 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3963 return WINED3DERR_INVALIDCALL;
3965 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3966 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3967 return WINED3DERR_INVALIDCALL;
3969 if (filter != WINED3D_TEXF_NONE && filter != WINED3D_TEXF_POINT
3970 && filter != WINED3D_TEXF_LINEAR)
3971 return WINED3DERR_INVALIDCALL;
3973 if (FAILED(hr = wined3d_resource_check_box_dimensions(&dst_texture->resource, dst_sub_resource_idx, &dst_box)))
3974 return hr;
3976 if (FAILED(hr = wined3d_resource_check_box_dimensions(&src_texture->resource, src_sub_resource_idx, &src_box)))
3977 return hr;
3979 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3980 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3982 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3983 return WINEDDERR_SURFACEBUSY;
3986 if (!src_texture->resource.format->depth_size != !dst_texture->resource.format->depth_size
3987 || !src_texture->resource.format->stencil_size != !dst_texture->resource.format->stencil_size)
3989 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3990 return WINED3DERR_INVALIDCALL;
3993 if (dst_texture->resource.device != src_texture->resource.device)
3995 FIXME("Rejecting cross-device blit.\n");
3996 return E_NOTIMPL;
3999 wined3d_device_context_emit_blt_sub_resource(&dst_texture->resource.device->cs->c, &dst_texture->resource,
4000 dst_sub_resource_idx, &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
4002 return WINED3D_OK;
4005 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
4006 unsigned int sub_resource_idx, LONG *x, LONG *y)
4008 struct wined3d_overlay_info *overlay;
4010 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
4012 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4013 || sub_resource_idx >= texture->level_count * texture->layer_count)
4015 WARN("Invalid sub-resource specified.\n");
4016 return WINEDDERR_NOTAOVERLAYSURFACE;
4019 overlay = &texture->overlay_info[sub_resource_idx];
4020 if (!overlay->dst_texture)
4022 TRACE("Overlay not visible.\n");
4023 *x = 0;
4024 *y = 0;
4025 return WINEDDERR_OVERLAYNOTVISIBLE;
4028 *x = overlay->dst_rect.left;
4029 *y = overlay->dst_rect.top;
4031 TRACE("Returning position %d, %d.\n", *x, *y);
4033 return WINED3D_OK;
4036 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
4037 unsigned int sub_resource_idx, LONG x, LONG y)
4039 struct wined3d_overlay_info *overlay;
4040 LONG w, h;
4042 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
4044 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
4045 || sub_resource_idx >= texture->level_count * texture->layer_count)
4047 WARN("Invalid sub-resource specified.\n");
4048 return WINEDDERR_NOTAOVERLAYSURFACE;
4051 overlay = &texture->overlay_info[sub_resource_idx];
4052 w = overlay->dst_rect.right - overlay->dst_rect.left;
4053 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
4054 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
4056 return WINED3D_OK;
4059 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
4060 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4061 const RECT *dst_rect, DWORD flags)
4063 struct wined3d_overlay_info *overlay;
4064 unsigned int level, dst_level;
4066 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
4067 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
4068 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
4069 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
4071 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4072 || sub_resource_idx >= texture->level_count * texture->layer_count)
4074 WARN("Invalid sub-resource specified.\n");
4075 return WINEDDERR_NOTAOVERLAYSURFACE;
4078 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
4079 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
4081 WARN("Invalid destination sub-resource specified.\n");
4082 return WINED3DERR_INVALIDCALL;
4085 overlay = &texture->overlay_info[sub_resource_idx];
4087 level = sub_resource_idx % texture->level_count;
4088 if (src_rect)
4089 overlay->src_rect = *src_rect;
4090 else
4091 SetRect(&overlay->src_rect, 0, 0,
4092 wined3d_texture_get_level_width(texture, level),
4093 wined3d_texture_get_level_height(texture, level));
4095 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4096 if (dst_rect)
4097 overlay->dst_rect = *dst_rect;
4098 else
4099 SetRect(&overlay->dst_rect, 0, 0,
4100 wined3d_texture_get_level_width(dst_texture, dst_level),
4101 wined3d_texture_get_level_height(dst_texture, dst_level));
4103 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
4104 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
4106 overlay->dst_texture = NULL;
4107 list_remove(&overlay->entry);
4110 if (flags & WINEDDOVER_SHOW)
4112 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
4114 overlay->dst_texture = dst_texture;
4115 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
4116 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
4119 else if (flags & WINEDDOVER_HIDE)
4121 /* Tests show that the rectangles are erased on hide. */
4122 SetRectEmpty(&overlay->src_rect);
4123 SetRectEmpty(&overlay->dst_rect);
4124 overlay->dst_texture = NULL;
4127 return WINED3D_OK;
4130 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
4132 unsigned int sub_count = texture->level_count * texture->layer_count;
4134 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
4136 if (sub_resource_idx >= sub_count)
4138 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4139 return NULL;
4142 return texture->sub_resources[sub_resource_idx].parent;
4145 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
4146 unsigned int sub_resource_idx, void *parent)
4148 unsigned int sub_count = texture->level_count * texture->layer_count;
4150 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
4152 if (sub_resource_idx >= sub_count)
4154 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4155 return;
4158 texture->sub_resources[sub_resource_idx].parent = parent;
4161 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
4162 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
4164 unsigned int sub_count = texture->level_count * texture->layer_count;
4165 const struct wined3d_resource *resource;
4166 unsigned int level_idx;
4168 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
4170 if (sub_resource_idx >= sub_count)
4172 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
4173 return WINED3DERR_INVALIDCALL;
4176 resource = &texture->resource;
4177 desc->format = resource->format->id;
4178 desc->multisample_type = resource->multisample_type;
4179 desc->multisample_quality = resource->multisample_quality;
4180 desc->usage = resource->usage;
4181 desc->bind_flags = resource->bind_flags;
4182 desc->access = resource->access;
4184 level_idx = sub_resource_idx % texture->level_count;
4185 desc->width = wined3d_texture_get_level_width(texture, level_idx);
4186 desc->height = wined3d_texture_get_level_height(texture, level_idx);
4187 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
4188 desc->size = texture->sub_resources[sub_resource_idx].size;
4190 return WINED3D_OK;
4193 HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
4194 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4195 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4197 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4198 HRESULT hr;
4200 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, "
4201 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4202 texture_gl, device, desc, layer_count,
4203 level_count, flags, parent, parent_ops);
4205 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
4206 && !gl_info->supported[EXT_TEXTURE_ARRAY])
4208 WARN("OpenGL implementation does not support array textures.\n");
4209 return WINED3DERR_INVALIDCALL;
4212 switch (desc->resource_type)
4214 case WINED3D_RTYPE_TEXTURE_1D:
4215 if (layer_count > 1)
4216 texture_gl->target = GL_TEXTURE_1D_ARRAY;
4217 else
4218 texture_gl->target = GL_TEXTURE_1D;
4219 break;
4221 case WINED3D_RTYPE_TEXTURE_2D:
4222 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
4224 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
4226 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
4228 if (layer_count > 1)
4229 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
4230 else
4231 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
4233 else
4235 if (layer_count > 1)
4236 texture_gl->target = GL_TEXTURE_2D_ARRAY;
4237 else
4238 texture_gl->target = GL_TEXTURE_2D;
4240 break;
4242 case WINED3D_RTYPE_TEXTURE_3D:
4243 if (!gl_info->supported[EXT_TEXTURE3D])
4245 WARN("OpenGL implementation does not support 3D textures.\n");
4246 return WINED3DERR_INVALIDCALL;
4248 texture_gl->target = GL_TEXTURE_3D;
4249 break;
4251 default:
4252 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
4253 return WINED3DERR_INVALIDCALL;
4256 list_init(&texture_gl->renderbuffers);
4258 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
4259 flags, device, parent, parent_ops, &texture_gl[1], &texture_gl_ops)))
4260 return hr;
4262 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
4263 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
4265 if (texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY || texture_gl->target == GL_TEXTURE_2D_MULTISAMPLE)
4266 texture_gl->t.flags &= ~WINED3D_TEXTURE_DOWNLOADABLE;
4268 return WINED3D_OK;
4271 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
4272 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
4273 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
4275 unsigned int sub_count = level_count * layer_count;
4276 unsigned int i;
4277 HRESULT hr;
4279 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
4280 "parent %p, parent_ops %p, texture %p.\n",
4281 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
4283 if (!layer_count)
4285 WARN("Invalid layer count.\n");
4286 return E_INVALIDARG;
4288 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
4290 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
4291 layer_count = 6;
4294 if (!level_count)
4296 WARN("Invalid level count.\n");
4297 return WINED3DERR_INVALIDCALL;
4300 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
4302 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->bind_flags);
4304 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
4305 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
4307 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
4308 desc->multisample_quality);
4309 return WINED3DERR_NOTAVAILABLE;
4311 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
4312 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
4313 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
4315 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
4316 desc->multisample_quality);
4317 return WINED3DERR_NOTAVAILABLE;
4321 if (data)
4323 for (i = 0; i < sub_count; ++i)
4325 if (data[i].data)
4326 continue;
4328 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
4329 return E_INVALIDARG;
4333 if (FAILED(hr = device->adapter->adapter_ops->adapter_create_texture(device, desc,
4334 layer_count, level_count, flags, parent, parent_ops, texture)))
4335 return hr;
4337 /* FIXME: We'd like to avoid ever allocating system memory for the texture
4338 * in this case. */
4339 if (data)
4341 struct wined3d_box box;
4343 for (i = 0; i < sub_count; ++i)
4345 wined3d_texture_get_level_box(*texture, i % (*texture)->level_count, &box);
4346 wined3d_device_context_emit_update_sub_resource(&device->cs->c, &(*texture)->resource,
4347 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
4351 TRACE("Created texture %p.\n", *texture);
4353 return WINED3D_OK;
4356 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
4358 struct wined3d_device *device = texture->resource.device;
4359 struct wined3d_texture_sub_resource *sub_resource;
4360 struct wined3d_dc_info *dc_info;
4362 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4364 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
4366 WARN("Texture does not support GetDC\n");
4367 /* Don't touch the DC */
4368 return WINED3DERR_INVALIDCALL;
4371 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4372 return WINED3DERR_INVALIDCALL;
4374 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4376 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4377 return WINED3DERR_INVALIDCALL;
4380 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4381 return WINED3DERR_INVALIDCALL;
4383 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4385 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4387 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
4388 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4389 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
4390 return WINED3DERR_INVALIDCALL;
4393 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4394 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
4395 ++texture->resource.map_count;
4396 ++sub_resource->map_count;
4398 *dc = dc_info[sub_resource_idx].dc;
4399 TRACE("Returning dc %p.\n", *dc);
4401 return WINED3D_OK;
4404 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
4406 struct wined3d_device *device = texture->resource.device;
4407 struct wined3d_texture_sub_resource *sub_resource;
4408 struct wined3d_dc_info *dc_info;
4410 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
4412 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
4413 return WINED3DERR_INVALIDCALL;
4415 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
4417 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
4418 return WINED3DERR_INVALIDCALL;
4421 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
4422 return WINED3DERR_INVALIDCALL;
4424 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
4426 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
4427 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
4428 return WINED3DERR_INVALIDCALL;
4431 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
4433 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
4435 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
4436 wined3d_cs_finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
4439 --sub_resource->map_count;
4440 if (!--texture->resource.map_count && texture->update_map_binding)
4441 wined3d_texture_update_map_binding(texture);
4442 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
4443 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
4445 return WINED3D_OK;
4448 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4449 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
4450 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
4452 unsigned int src_row_pitch, src_slice_pitch;
4453 unsigned int update_w, update_h, update_d;
4454 unsigned int src_level, dst_level;
4455 struct wined3d_context *context;
4456 struct wined3d_bo_address data;
4458 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
4459 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
4460 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
4461 src_texture, src_sub_resource_idx, debug_box(src_box));
4463 context = context_acquire(dst_texture->resource.device, NULL, 0);
4465 /* Only load the sub-resource for partial updates. For newly allocated
4466 * textures the texture wouldn't be the current location, and we'd upload
4467 * zeroes just to overwrite them again. */
4468 update_w = src_box->right - src_box->left;
4469 update_h = src_box->bottom - src_box->top;
4470 update_d = src_box->back - src_box->front;
4471 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4472 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
4473 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
4474 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
4475 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4476 else
4477 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
4479 src_level = src_sub_resource_idx % src_texture->level_count;
4480 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
4481 src_texture->sub_resources[src_sub_resource_idx].locations);
4482 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4484 dst_texture->texture_ops->texture_upload_data(context, wined3d_const_bo_address(&data),
4485 src_texture->resource.format, src_box, src_row_pitch, src_slice_pitch, dst_texture,
4486 dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB, dst_x, dst_y, dst_z);
4488 context_release(context);
4490 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
4491 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
4494 /* Partial downloads are not supported. */
4495 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
4496 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
4498 unsigned int src_level, dst_level, dst_row_pitch, dst_slice_pitch;
4499 unsigned int dst_location = dst_texture->resource.map_binding;
4500 struct wined3d_context *context;
4501 struct wined3d_bo_address data;
4502 struct wined3d_box src_box;
4503 unsigned int src_location;
4505 context = context_acquire(src_texture->resource.device, NULL, 0);
4507 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
4508 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
4510 if (src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB)
4511 src_location = WINED3D_LOCATION_TEXTURE_RGB;
4512 else
4513 src_location = WINED3D_LOCATION_TEXTURE_SRGB;
4514 src_level = src_sub_resource_idx % src_texture->level_count;
4515 wined3d_texture_get_level_box(src_texture, src_level, &src_box);
4517 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4518 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4520 src_texture->texture_ops->texture_download_data(context, src_texture, src_sub_resource_idx, src_location,
4521 &src_box, &data, dst_texture->resource.format, 0, 0, 0, dst_row_pitch, dst_slice_pitch);
4523 context_release(context);
4525 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
4526 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);
4529 static void wined3d_texture_no3d_upload_data(struct wined3d_context *context,
4530 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4531 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4532 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4533 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4535 FIXME("Not implemented.\n");
4538 static void wined3d_texture_no3d_download_data(struct wined3d_context *context,
4539 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4540 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4541 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4542 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4544 FIXME("Not implemented.\n");
4547 static BOOL wined3d_texture_no3d_prepare_location(struct wined3d_texture *texture,
4548 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
4550 if (location == WINED3D_LOCATION_SYSMEM)
4551 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
4552 : wined3d_resource_prepare_sysmem(&texture->resource);
4554 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
4555 return FALSE;
4558 static BOOL wined3d_texture_no3d_load_location(struct wined3d_texture *texture,
4559 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
4561 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
4562 texture, sub_resource_idx, context, wined3d_debug_location(location));
4564 if (location == WINED3D_LOCATION_SYSMEM)
4565 return TRUE;
4567 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
4569 return FALSE;
4572 static void wined3d_texture_no3d_unload_location(struct wined3d_texture *texture,
4573 struct wined3d_context *context, unsigned int location)
4575 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
4578 static const struct wined3d_texture_ops wined3d_texture_no3d_ops =
4580 wined3d_texture_no3d_prepare_location,
4581 wined3d_texture_no3d_load_location,
4582 wined3d_texture_no3d_unload_location,
4583 wined3d_texture_no3d_upload_data,
4584 wined3d_texture_no3d_download_data,
4587 HRESULT wined3d_texture_no3d_init(struct wined3d_texture *texture_no3d, struct wined3d_device *device,
4588 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
4589 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
4591 TRACE("texture_no3d %p, device %p, desc %p, layer_count %u, "
4592 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
4593 texture_no3d, device, desc, layer_count,
4594 level_count, flags, parent, parent_ops);
4596 return wined3d_texture_init(texture_no3d, desc, layer_count, level_count,
4597 flags, device, parent, parent_ops, &texture_no3d[1], &wined3d_texture_no3d_ops);
4600 void wined3d_vk_swizzle_from_color_fixup(VkComponentMapping *mapping, struct color_fixup_desc fixup)
4602 static const VkComponentSwizzle swizzle_source[] =
4604 VK_COMPONENT_SWIZZLE_ZERO, /* CHANNEL_SOURCE_ZERO */
4605 VK_COMPONENT_SWIZZLE_ONE, /* CHANNEL_SOURCE_ONE */
4606 VK_COMPONENT_SWIZZLE_R, /* CHANNEL_SOURCE_X */
4607 VK_COMPONENT_SWIZZLE_G, /* CHANNEL_SOURCE_Y */
4608 VK_COMPONENT_SWIZZLE_B, /* CHANNEL_SOURCE_Z */
4609 VK_COMPONENT_SWIZZLE_A, /* CHANNEL_SOURCE_W */
4612 mapping->r = swizzle_source[fixup.x_source];
4613 mapping->g = swizzle_source[fixup.y_source];
4614 mapping->b = swizzle_source[fixup.z_source];
4615 mapping->a = swizzle_source[fixup.w_source];
4618 const VkDescriptorImageInfo *wined3d_texture_vk_get_default_image_info(struct wined3d_texture_vk *texture_vk,
4619 struct wined3d_context_vk *context_vk)
4621 const struct wined3d_format_vk *format_vk;
4622 const struct wined3d_vk_info *vk_info;
4623 struct wined3d_device_vk *device_vk;
4624 VkImageViewCreateInfo create_info;
4625 struct color_fixup_desc fixup;
4626 uint32_t flags = 0;
4627 VkResult vr;
4629 if (texture_vk->default_image_info.imageView)
4630 return &texture_vk->default_image_info;
4632 format_vk = wined3d_format_vk(texture_vk->t.resource.format);
4633 device_vk = wined3d_device_vk(texture_vk->t.resource.device);
4634 vk_info = context_vk->vk_info;
4636 if (texture_vk->t.layer_count > 1)
4637 flags |= WINED3D_VIEW_TEXTURE_ARRAY;
4639 wined3d_texture_vk_prepare_texture(texture_vk, context_vk);
4640 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
4641 create_info.pNext = NULL;
4642 create_info.flags = 0;
4643 create_info.image = texture_vk->image.vk_image;
4644 create_info.viewType = vk_image_view_type_from_wined3d(texture_vk->t.resource.type, flags);
4645 create_info.format = format_vk->vk_format;
4646 fixup = format_vk->f.color_fixup;
4647 if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
4649 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
4650 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
4651 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
4652 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
4654 else
4656 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
4658 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
4659 create_info.subresourceRange.baseMipLevel = 0;
4660 create_info.subresourceRange.levelCount = texture_vk->t.level_count;
4661 create_info.subresourceRange.baseArrayLayer = 0;
4662 create_info.subresourceRange.layerCount = texture_vk->t.layer_count;
4663 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info,
4664 NULL, &texture_vk->default_image_info.imageView))) < 0)
4666 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
4667 return NULL;
4670 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(texture_vk->default_image_info.imageView));
4672 texture_vk->default_image_info.sampler = VK_NULL_HANDLE;
4673 texture_vk->default_image_info.imageLayout = texture_vk->layout;
4675 return &texture_vk->default_image_info;
4678 static void wined3d_texture_vk_upload_data(struct wined3d_context *context,
4679 const struct wined3d_const_bo_address *src_bo_addr, const struct wined3d_format *src_format,
4680 const struct wined3d_box *src_box, unsigned int src_row_pitch, unsigned int src_slice_pitch,
4681 struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx, unsigned int dst_location,
4682 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z)
4684 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
4685 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4686 unsigned int dst_level, dst_row_pitch, dst_slice_pitch;
4687 struct wined3d_texture_sub_resource *sub_resource;
4688 struct wined3d_bo_address staging_bo_addr;
4689 VkPipelineStageFlags bo_stage_flags = 0;
4690 const struct wined3d_vk_info *vk_info;
4691 VkCommandBuffer vk_command_buffer;
4692 VkBufferMemoryBarrier vk_barrier;
4693 VkImageSubresourceRange vk_range;
4694 struct wined3d_bo_vk staging_bo;
4695 VkImageAspectFlags aspect_mask;
4696 struct wined3d_bo_vk *src_bo;
4697 struct wined3d_range range;
4698 VkBufferImageCopy region;
4699 size_t src_offset;
4700 void *map_ptr;
4702 TRACE("context %p, src_bo_addr %s, src_format %s, src_box %s, src_row_pitch %u, src_slice_pitch %u, "
4703 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_x %u, dst_y %u, dst_z %u.\n",
4704 context, debug_const_bo_address(src_bo_addr), debug_d3dformat(src_format->id), debug_box(src_box),
4705 src_row_pitch, src_slice_pitch, dst_texture, dst_sub_resource_idx,
4706 wined3d_debug_location(dst_location), dst_x, dst_y, dst_z);
4708 if (src_format->id != dst_texture->resource.format->id)
4710 FIXME("Unhandled format conversion (%s -> %s).\n",
4711 debug_d3dformat(src_format->id),
4712 debug_d3dformat(dst_texture->resource.format->id));
4713 return;
4716 dst_level = dst_sub_resource_idx % dst_texture->level_count;
4717 wined3d_texture_get_pitch(dst_texture, dst_level, &dst_row_pitch, &dst_slice_pitch);
4718 if (dst_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4719 src_row_pitch = dst_row_pitch = 0;
4720 if (dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4721 src_slice_pitch = dst_slice_pitch = 0;
4723 if (dst_location != WINED3D_LOCATION_TEXTURE_RGB)
4725 FIXME("Unhandled location %s.\n", wined3d_debug_location(dst_location));
4726 return;
4729 if (wined3d_resource_get_sample_count(&dst_texture_vk->t.resource) > 1)
4731 FIXME("Not supported for multisample textures.\n");
4732 return;
4735 aspect_mask = vk_aspect_mask_from_format(dst_texture->resource.format);
4736 if (wined3d_popcount(aspect_mask) > 1)
4738 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(dst_texture->resource.format->id));
4739 return;
4742 sub_resource = &dst_texture_vk->t.sub_resources[dst_sub_resource_idx];
4743 vk_info = context_vk->vk_info;
4745 src_offset = src_box->front * src_slice_pitch
4746 + (src_box->top / src_format->block_height) * src_row_pitch
4747 + (src_box->left / src_format->block_width) * src_format->block_byte_count;
4749 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4751 ERR("Failed to get command buffer.\n");
4752 return;
4755 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
4756 wined3d_context_vk_end_current_render_pass(context_vk);
4758 if (!src_bo_addr->buffer_object)
4760 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4761 VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4763 ERR("Failed to create staging bo.\n");
4764 return;
4767 staging_bo_addr.buffer_object = &staging_bo.b;
4768 staging_bo_addr.addr = NULL;
4769 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
4770 sub_resource->size, WINED3D_MAP_DISCARD | WINED3D_MAP_WRITE)))
4772 ERR("Failed to map staging bo.\n");
4773 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4774 return;
4777 wined3d_format_copy_data(src_format, src_bo_addr->addr + src_offset, src_row_pitch,
4778 src_slice_pitch, map_ptr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
4779 src_box->bottom - src_box->top, src_box->back - src_box->front);
4781 range.offset = 0;
4782 range.size = sub_resource->size;
4783 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 1, &range);
4785 src_bo = &staging_bo;
4787 src_offset = 0;
4788 src_row_pitch = dst_row_pitch;
4789 src_slice_pitch = dst_slice_pitch;
4791 else
4793 src_bo = wined3d_bo_vk(src_bo_addr->buffer_object);
4795 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
4796 vk_barrier.pNext = NULL;
4797 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(src_bo->usage) & ~WINED3D_READ_ONLY_ACCESS_FLAGS;
4798 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
4799 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4800 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4801 vk_barrier.buffer = src_bo->vk_buffer;
4802 vk_barrier.offset = src_bo->b.buffer_offset + (size_t)src_bo_addr->addr;
4803 vk_barrier.size = sub_resource->size;
4805 src_offset += (size_t)src_bo_addr->addr;
4807 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(src_bo->usage);
4808 if (vk_barrier.srcAccessMask)
4809 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
4810 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
4813 vk_range.aspectMask = aspect_mask;
4814 vk_range.baseMipLevel = dst_level;
4815 vk_range.levelCount = 1;
4816 vk_range.baseArrayLayer = dst_sub_resource_idx / dst_texture_vk->t.level_count;
4817 vk_range.layerCount = 1;
4819 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4820 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4821 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4822 VK_ACCESS_TRANSFER_WRITE_BIT,
4823 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
4824 dst_texture_vk->image.vk_image, &vk_range);
4826 region.bufferOffset = src_bo->b.buffer_offset + src_offset;
4827 region.bufferRowLength = (src_row_pitch / src_format->block_byte_count) * src_format->block_width;
4828 if (src_row_pitch)
4829 region.bufferImageHeight = (src_slice_pitch / src_row_pitch) * src_format->block_height;
4830 else
4831 region.bufferImageHeight = 1;
4832 region.imageSubresource.aspectMask = vk_range.aspectMask;
4833 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
4834 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
4835 region.imageSubresource.layerCount = vk_range.layerCount;
4836 region.imageOffset.x = dst_x;
4837 region.imageOffset.y = dst_y;
4838 region.imageOffset.z = dst_z;
4839 region.imageExtent.width = src_box->right - src_box->left;
4840 region.imageExtent.height = src_box->bottom - src_box->top;
4841 region.imageExtent.depth = src_box->back - src_box->front;
4843 VK_CALL(vkCmdCopyBufferToImage(vk_command_buffer, src_bo->vk_buffer,
4844 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
4846 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4847 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
4848 VK_ACCESS_TRANSFER_WRITE_BIT,
4849 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
4850 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
4851 dst_texture_vk->image.vk_image, &vk_range);
4852 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
4853 wined3d_context_vk_reference_bo(context_vk, src_bo);
4855 if (src_bo == &staging_bo)
4857 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
4859 else if (vk_barrier.srcAccessMask)
4861 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
4862 bo_stage_flags, 0, 0, NULL, 0, NULL, 0, NULL));
4866 static void wined3d_texture_vk_download_data(struct wined3d_context *context,
4867 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx, unsigned int src_location,
4868 const struct wined3d_box *src_box, const struct wined3d_bo_address *dst_bo_addr,
4869 const struct wined3d_format *dst_format, unsigned int dst_x, unsigned int dst_y, unsigned int dst_z,
4870 unsigned int dst_row_pitch, unsigned int dst_slice_pitch)
4872 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
4873 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
4874 unsigned int src_level, src_width, src_height, src_depth;
4875 struct wined3d_texture_sub_resource *sub_resource;
4876 unsigned int src_row_pitch, src_slice_pitch;
4877 struct wined3d_bo_address staging_bo_addr;
4878 VkPipelineStageFlags bo_stage_flags = 0;
4879 const struct wined3d_vk_info *vk_info;
4880 VkCommandBuffer vk_command_buffer;
4881 VkImageSubresourceRange vk_range;
4882 VkBufferMemoryBarrier vk_barrier;
4883 struct wined3d_bo_vk staging_bo;
4884 VkImageAspectFlags aspect_mask;
4885 struct wined3d_bo_vk *dst_bo;
4886 VkBufferImageCopy region;
4887 size_t dst_offset = 0;
4888 void *map_ptr;
4890 TRACE("context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_box %s, dst_bo_addr %s, "
4891 "dst_format %s, dst_x %u, dst_y %u, dst_z %u, dst_row_pitch %u, dst_slice_pitch %u.\n",
4892 context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
4893 debug_box(src_box), debug_bo_address(dst_bo_addr), debug_d3dformat(dst_format->id),
4894 dst_x, dst_y, dst_z, dst_row_pitch, dst_slice_pitch);
4896 if (src_location != WINED3D_LOCATION_TEXTURE_RGB)
4898 FIXME("Unhandled location %s.\n", wined3d_debug_location(src_location));
4899 return;
4902 src_level = src_sub_resource_idx % src_texture->level_count;
4903 src_width = wined3d_texture_get_level_width(src_texture, src_level);
4904 src_height = wined3d_texture_get_level_height(src_texture, src_level);
4905 src_depth = wined3d_texture_get_level_depth(src_texture, src_level);
4906 if (src_box->left || src_box->top || src_box->right != src_width || src_box->bottom != src_height
4907 || src_box->front || src_box->back != src_depth)
4909 FIXME("Unhandled source box %s.\n", debug_box(src_box));
4910 return;
4913 if (dst_format->id != src_texture->resource.format->id)
4915 FIXME("Unhandled format conversion (%s -> %s).\n",
4916 debug_d3dformat(src_texture->resource.format->id),
4917 debug_d3dformat(dst_format->id));
4918 return;
4921 if (dst_x || dst_y || dst_z)
4923 FIXME("Unhandled destination (%u, %u, %u).\n", dst_x, dst_y, dst_z);
4924 return;
4927 if (wined3d_resource_get_sample_count(&src_texture_vk->t.resource) > 1)
4929 FIXME("Not supported for multisample textures.\n");
4930 return;
4933 aspect_mask = vk_aspect_mask_from_format(src_texture->resource.format);
4934 if (wined3d_popcount(aspect_mask) > 1)
4936 FIXME("Unhandled multi-aspect format %s.\n", debug_d3dformat(src_texture->resource.format->id));
4937 return;
4940 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
4941 if (src_texture->resource.type == WINED3D_RTYPE_TEXTURE_1D)
4942 src_row_pitch = dst_row_pitch = 0;
4943 if (src_texture->resource.type != WINED3D_RTYPE_TEXTURE_3D)
4944 src_slice_pitch = dst_slice_pitch = 0;
4946 sub_resource = &src_texture_vk->t.sub_resources[src_sub_resource_idx];
4947 vk_info = context_vk->vk_info;
4948 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
4950 ERR("Failed to get command buffer.\n");
4951 return;
4954 /* We need to be outside of a render pass for vkCmdPipelineBarrier() and vkCmdCopyBufferToImage() calls below. */
4955 wined3d_context_vk_end_current_render_pass(context_vk);
4957 if (!dst_bo_addr->buffer_object)
4959 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
4960 VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &staging_bo))
4962 ERR("Failed to create staging bo.\n");
4963 return;
4966 dst_bo = &staging_bo;
4968 else
4970 dst_bo = wined3d_bo_vk(dst_bo_addr->buffer_object);
4972 vk_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
4973 vk_barrier.pNext = NULL;
4974 vk_barrier.srcAccessMask = vk_access_mask_from_buffer_usage(dst_bo->usage);
4975 vk_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
4976 vk_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4977 vk_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
4978 vk_barrier.buffer = dst_bo->vk_buffer;
4979 vk_barrier.offset = dst_bo->b.buffer_offset + (size_t)dst_bo_addr->addr;
4980 vk_barrier.size = sub_resource->size;
4982 bo_stage_flags = vk_pipeline_stage_mask_from_buffer_usage(dst_bo->usage);
4983 dst_offset = (size_t)dst_bo_addr->addr;
4985 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, bo_stage_flags,
4986 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
4989 vk_range.aspectMask = aspect_mask;
4990 vk_range.baseMipLevel = src_level;
4991 vk_range.levelCount = 1;
4992 vk_range.baseArrayLayer = src_sub_resource_idx / src_texture_vk->t.level_count;
4993 vk_range.layerCount = 1;
4995 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
4996 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
4997 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
4998 VK_ACCESS_TRANSFER_READ_BIT,
4999 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
5000 src_texture_vk->image.vk_image, &vk_range);
5002 region.bufferOffset = dst_bo->b.buffer_offset + dst_offset;
5003 region.bufferRowLength = 0;
5004 region.bufferImageHeight = 0;
5005 region.imageSubresource.aspectMask = vk_range.aspectMask;
5006 region.imageSubresource.mipLevel = vk_range.baseMipLevel;
5007 region.imageSubresource.baseArrayLayer = vk_range.baseArrayLayer;
5008 region.imageSubresource.layerCount = vk_range.layerCount;
5009 region.imageOffset.x = 0;
5010 region.imageOffset.y = 0;
5011 region.imageOffset.z = 0;
5012 region.imageExtent.width = src_width;
5013 region.imageExtent.height = src_height;
5014 region.imageExtent.depth = src_depth;
5016 VK_CALL(vkCmdCopyImageToBuffer(vk_command_buffer, src_texture_vk->image.vk_image,
5017 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_bo->vk_buffer, 1, &region));
5019 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5020 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
5021 VK_ACCESS_TRANSFER_READ_BIT,
5022 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
5023 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
5024 src_texture_vk->image.vk_image, &vk_range);
5026 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
5027 wined3d_context_vk_reference_bo(context_vk, dst_bo);
5029 if (dst_bo == &staging_bo)
5031 wined3d_context_vk_submit_command_buffer(context_vk, 0, NULL, NULL, 0, NULL);
5032 wined3d_context_vk_wait_command_buffer(context_vk, src_texture_vk->image.command_buffer_id);
5034 staging_bo_addr.buffer_object = &staging_bo.b;
5035 staging_bo_addr.addr = NULL;
5036 if (!(map_ptr = wined3d_context_map_bo_address(context, &staging_bo_addr,
5037 sub_resource->size, WINED3D_MAP_READ)))
5039 ERR("Failed to map staging bo.\n");
5040 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5041 return;
5044 wined3d_format_copy_data(dst_format, map_ptr, src_row_pitch, src_slice_pitch,
5045 dst_bo_addr->addr, dst_row_pitch, dst_slice_pitch, src_box->right - src_box->left,
5046 src_box->bottom - src_box->top, src_box->back - src_box->front);
5048 wined3d_context_unmap_bo_address(context, &staging_bo_addr, 0, NULL);
5049 wined3d_context_vk_destroy_bo(context_vk, &staging_bo);
5051 else
5053 vk_barrier.dstAccessMask = vk_barrier.srcAccessMask;
5054 vk_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
5056 if (dst_bo->host_synced)
5058 vk_barrier.dstAccessMask |= VK_ACCESS_HOST_READ_BIT;
5059 bo_stage_flags |= VK_PIPELINE_STAGE_HOST_BIT;
5062 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT,
5063 bo_stage_flags, 0, 0, NULL, 1, &vk_barrier, 0, NULL));
5067 static BOOL wined3d_texture_vk_load_texture(struct wined3d_texture_vk *texture_vk,
5068 unsigned int sub_resource_idx, struct wined3d_context *context)
5070 struct wined3d_texture_sub_resource *sub_resource;
5071 unsigned int level, row_pitch, slice_pitch;
5072 struct wined3d_bo_address data;
5073 struct wined3d_box src_box;
5075 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5076 if (!(sub_resource->locations & wined3d_texture_sysmem_locations))
5078 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5079 return FALSE;
5082 level = sub_resource_idx % texture_vk->t.level_count;
5083 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, sub_resource->locations);
5084 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5085 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5086 wined3d_texture_vk_upload_data(context, wined3d_const_bo_address(&data), texture_vk->t.resource.format,
5087 &src_box, row_pitch, slice_pitch, &texture_vk->t, sub_resource_idx,
5088 WINED3D_LOCATION_TEXTURE_RGB, 0, 0, 0);
5090 return TRUE;
5093 static BOOL wined3d_texture_vk_load_sysmem(struct wined3d_texture_vk *texture_vk,
5094 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5096 struct wined3d_texture_sub_resource *sub_resource;
5097 unsigned int level, row_pitch, slice_pitch;
5098 struct wined3d_bo_address data;
5099 struct wined3d_box src_box;
5101 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5102 if (!(sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB))
5104 ERR("Unimplemented load from %s.\n", wined3d_debug_location(sub_resource->locations));
5105 return FALSE;
5108 level = sub_resource_idx % texture_vk->t.level_count;
5109 wined3d_texture_get_memory(&texture_vk->t, sub_resource_idx, &data, location);
5110 wined3d_texture_get_level_box(&texture_vk->t, level, &src_box);
5111 wined3d_texture_get_pitch(&texture_vk->t, level, &row_pitch, &slice_pitch);
5112 wined3d_texture_vk_download_data(context, &texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB,
5113 &src_box, &data, texture_vk->t.resource.format, 0, 0, 0, row_pitch, slice_pitch);
5115 return TRUE;
5118 BOOL wined3d_texture_vk_prepare_texture(struct wined3d_texture_vk *texture_vk,
5119 struct wined3d_context_vk *context_vk)
5121 const struct wined3d_format_vk *format_vk;
5122 struct wined3d_resource *resource;
5123 VkCommandBuffer vk_command_buffer;
5124 VkImageSubresourceRange vk_range;
5125 VkImageUsageFlags vk_usage;
5126 VkImageType vk_image_type;
5127 unsigned int flags = 0;
5129 if (texture_vk->t.flags & WINED3D_TEXTURE_RGB_ALLOCATED)
5130 return TRUE;
5132 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
5134 ERR("Failed to get command buffer.\n");
5135 return FALSE;
5138 resource = &texture_vk->t.resource;
5139 format_vk = wined3d_format_vk(resource->format);
5141 if (wined3d_format_is_typeless(&format_vk->f) || texture_vk->t.swapchain
5142 || (texture_vk->t.resource.bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
5144 /* For UAVs, we need this in case a clear necessitates creation of a new view
5145 * with a different format. */
5146 flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
5149 switch (resource->type)
5151 case WINED3D_RTYPE_TEXTURE_1D:
5152 vk_image_type = VK_IMAGE_TYPE_1D;
5153 break;
5154 case WINED3D_RTYPE_TEXTURE_2D:
5155 vk_image_type = VK_IMAGE_TYPE_2D;
5156 if (texture_vk->t.layer_count >= 6 && resource->width == resource->height)
5157 flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
5158 break;
5159 case WINED3D_RTYPE_TEXTURE_3D:
5160 vk_image_type = VK_IMAGE_TYPE_3D;
5161 if (resource->bind_flags & (WINED3D_BIND_RENDER_TARGET | WINED3D_BIND_UNORDERED_ACCESS))
5162 flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
5163 break;
5164 default:
5165 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(resource->type));
5166 vk_image_type = VK_IMAGE_TYPE_2D;
5167 break;
5170 vk_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
5171 if (resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE)
5172 vk_usage |= VK_IMAGE_USAGE_SAMPLED_BIT;
5173 if (resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5174 vk_usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
5175 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
5176 vk_usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
5177 if (resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS)
5178 vk_usage |= VK_IMAGE_USAGE_STORAGE_BIT;
5180 texture_vk->layout = VK_IMAGE_LAYOUT_GENERAL;
5181 if (wined3d_popcount(resource->bind_flags) == 1)
5183 switch (resource->bind_flags)
5185 case WINED3D_BIND_RENDER_TARGET:
5186 texture_vk->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
5187 break;
5189 case WINED3D_BIND_DEPTH_STENCIL:
5190 texture_vk->layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
5191 break;
5193 case WINED3D_BIND_SHADER_RESOURCE:
5194 texture_vk->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
5195 break;
5197 default:
5198 break;
5202 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, vk_usage, format_vk->vk_format,
5203 resource->width, resource->height, resource->depth, max(1, wined3d_resource_get_sample_count(resource)),
5204 texture_vk->t.level_count, texture_vk->t.layer_count, flags, &texture_vk->image))
5206 return FALSE;
5209 vk_range.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
5210 vk_range.baseMipLevel = 0;
5211 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5212 vk_range.baseArrayLayer = 0;
5213 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5215 wined3d_context_vk_reference_texture(context_vk, texture_vk);
5216 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
5217 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
5218 0, 0,
5219 VK_IMAGE_LAYOUT_UNDEFINED, texture_vk->layout,
5220 texture_vk->image.vk_image, &vk_range);
5222 texture_vk->t.flags |= WINED3D_TEXTURE_RGB_ALLOCATED;
5224 TRACE("Created image 0x%s, memory 0x%s for texture %p.\n",
5225 wine_dbgstr_longlong(texture_vk->image.vk_image), wine_dbgstr_longlong(texture_vk->image.vk_memory), texture_vk);
5227 return TRUE;
5230 static BOOL wined3d_texture_vk_prepare_buffer_object(struct wined3d_texture_vk *texture_vk,
5231 unsigned int sub_resource_idx, struct wined3d_context_vk *context_vk)
5233 struct wined3d_texture_sub_resource *sub_resource;
5234 struct wined3d_bo_vk *bo;
5236 sub_resource = &texture_vk->t.sub_resources[sub_resource_idx];
5237 bo = &sub_resource->bo.vk;
5238 if (bo->vk_buffer)
5239 return TRUE;
5241 if (!wined3d_context_vk_create_bo(context_vk, sub_resource->size,
5242 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
5243 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, bo))
5244 return FALSE;
5246 /* Texture buffer objects receive a barrier to HOST_READ in wined3d_texture_vk_download_data(),
5247 * so they don't need it when they are mapped for reading. */
5248 bo->host_synced = true;
5249 TRACE("Created buffer object %p for texture %p, sub-resource %u.\n", bo, texture_vk, sub_resource_idx);
5250 return TRUE;
5253 static BOOL wined3d_texture_vk_prepare_location(struct wined3d_texture *texture,
5254 unsigned int sub_resource_idx, struct wined3d_context *context, unsigned int location)
5256 switch (location)
5258 case WINED3D_LOCATION_SYSMEM:
5259 return texture->sub_resources[sub_resource_idx].user_memory ? TRUE
5260 : wined3d_resource_prepare_sysmem(&texture->resource);
5262 case WINED3D_LOCATION_TEXTURE_RGB:
5263 return wined3d_texture_vk_prepare_texture(wined3d_texture_vk(texture), wined3d_context_vk(context));
5265 case WINED3D_LOCATION_BUFFER:
5266 return wined3d_texture_vk_prepare_buffer_object(wined3d_texture_vk(texture), sub_resource_idx,
5267 wined3d_context_vk(context));
5269 default:
5270 FIXME("Unhandled location %s.\n", wined3d_debug_location(location));
5271 return FALSE;
5275 static BOOL wined3d_texture_vk_load_location(struct wined3d_texture *texture,
5276 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
5278 if (!wined3d_texture_vk_prepare_location(texture, sub_resource_idx, context, location))
5279 return FALSE;
5281 switch (location)
5283 case WINED3D_LOCATION_TEXTURE_RGB:
5284 return wined3d_texture_vk_load_texture(wined3d_texture_vk(texture), sub_resource_idx, context);
5286 case WINED3D_LOCATION_SYSMEM:
5287 case WINED3D_LOCATION_BUFFER:
5288 return wined3d_texture_vk_load_sysmem(wined3d_texture_vk(texture), sub_resource_idx, context, location);
5290 default:
5291 FIXME("Unimplemented location %s.\n", wined3d_debug_location(location));
5292 return FALSE;
5296 static void wined3d_texture_vk_unload_location(struct wined3d_texture *texture,
5297 struct wined3d_context *context, unsigned int location)
5299 struct wined3d_texture_vk *texture_vk = wined3d_texture_vk(texture);
5300 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
5301 unsigned int i, sub_count;
5303 TRACE("texture %p, context %p, location %s.\n", texture, context, wined3d_debug_location(location));
5305 switch (location)
5307 case WINED3D_LOCATION_TEXTURE_RGB:
5308 if (texture_vk->default_image_info.imageView)
5310 wined3d_context_vk_destroy_vk_image_view(context_vk,
5311 texture_vk->default_image_info.imageView, texture_vk->image.command_buffer_id);
5312 texture_vk->default_image_info.imageView = VK_NULL_HANDLE;
5315 if (texture_vk->image.vk_image)
5316 wined3d_context_vk_destroy_image(context_vk, &texture_vk->image);
5317 break;
5319 case WINED3D_LOCATION_BUFFER:
5320 sub_count = texture->level_count * texture->layer_count;
5321 for (i = 0; i < sub_count; ++i)
5323 if (texture->sub_resources[i].bo.vk.vk_buffer)
5325 wined3d_context_vk_destroy_bo(context_vk, &texture->sub_resources[i].bo.vk);
5326 texture->sub_resources[i].bo.vk.vk_buffer = VK_NULL_HANDLE;
5329 break;
5331 case WINED3D_LOCATION_TEXTURE_SRGB:
5332 case WINED3D_LOCATION_RB_MULTISAMPLE:
5333 case WINED3D_LOCATION_RB_RESOLVED:
5334 break;
5336 default:
5337 ERR("Unhandled location %s.\n", wined3d_debug_location(location));
5338 break;
5342 static const struct wined3d_texture_ops wined3d_texture_vk_ops =
5344 wined3d_texture_vk_prepare_location,
5345 wined3d_texture_vk_load_location,
5346 wined3d_texture_vk_unload_location,
5347 wined3d_texture_vk_upload_data,
5348 wined3d_texture_vk_download_data,
5351 HRESULT wined3d_texture_vk_init(struct wined3d_texture_vk *texture_vk, struct wined3d_device *device,
5352 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
5353 uint32_t flags, void *parent, const struct wined3d_parent_ops *parent_ops)
5355 TRACE("texture_vk %p, device %p, desc %p, layer_count %u, "
5356 "level_count %u, flags %#x, parent %p, parent_ops %p.\n",
5357 texture_vk, device, desc, layer_count,
5358 level_count, flags, parent, parent_ops);
5360 return wined3d_texture_init(&texture_vk->t, desc, layer_count, level_count,
5361 flags, device, parent, parent_ops, &texture_vk[1], &wined3d_texture_vk_ops);
5364 void wined3d_texture_vk_barrier(struct wined3d_texture_vk *texture_vk,
5365 struct wined3d_context_vk *context_vk, uint32_t bind_mask)
5367 uint32_t src_bind_mask = 0;
5369 TRACE("texture_vk %p, context_vk %p, bind_mask %s.\n",
5370 texture_vk, context_vk, wined3d_debug_bind_flags(bind_mask));
5372 if (bind_mask & ~WINED3D_READ_ONLY_BIND_MASK)
5374 src_bind_mask = texture_vk->bind_mask & WINED3D_READ_ONLY_BIND_MASK;
5375 if (!src_bind_mask)
5376 src_bind_mask = texture_vk->bind_mask;
5378 texture_vk->bind_mask = bind_mask;
5380 else if ((texture_vk->bind_mask & bind_mask) != bind_mask)
5382 src_bind_mask = texture_vk->bind_mask & ~WINED3D_READ_ONLY_BIND_MASK;
5383 texture_vk->bind_mask |= bind_mask;
5386 if (src_bind_mask)
5388 VkImageSubresourceRange vk_range;
5390 TRACE(" %s -> %s.\n",
5391 wined3d_debug_bind_flags(src_bind_mask), wined3d_debug_bind_flags(bind_mask));
5393 vk_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
5394 vk_range.baseMipLevel = 0;
5395 vk_range.levelCount = VK_REMAINING_MIP_LEVELS;
5396 vk_range.baseArrayLayer = 0;
5397 vk_range.layerCount = VK_REMAINING_ARRAY_LAYERS;
5399 wined3d_context_vk_image_barrier(context_vk, wined3d_context_vk_get_command_buffer(context_vk),
5400 vk_pipeline_stage_mask_from_bind_flags(src_bind_mask),
5401 vk_pipeline_stage_mask_from_bind_flags(bind_mask),
5402 vk_access_mask_from_bind_flags(src_bind_mask), vk_access_mask_from_bind_flags(bind_mask),
5403 texture_vk->layout, texture_vk->layout, texture_vk->image.vk_image, &vk_range);
5407 static void ffp_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
5409 struct wined3d_blitter *next;
5411 if ((next = blitter->next))
5412 next->ops->blitter_destroy(next, context);
5414 heap_free(blitter);
5417 static bool ffp_blit_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
5418 const struct wined3d_resource *src_resource, DWORD src_location,
5419 const struct wined3d_resource *dst_resource, DWORD dst_location)
5421 const struct wined3d_format *src_format = src_resource->format;
5422 const struct wined3d_format *dst_format = dst_resource->format;
5423 bool decompress;
5425 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
5426 return false;
5428 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
5429 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
5430 if (!decompress && !(src_resource->access & dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
5432 TRACE("Source or destination resource is not GPU accessible.\n");
5433 return false;
5436 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
5438 if (dst_format->depth_size || dst_format->stencil_size)
5439 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
5440 else
5441 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
5444 switch (blit_op)
5446 case WINED3D_BLIT_OP_COLOR_BLIT_CKEY:
5447 if (context->d3d_info->shader_color_key)
5449 TRACE("Colour keying requires converted textures.\n");
5450 return false;
5452 case WINED3D_BLIT_OP_COLOR_BLIT:
5453 case WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST:
5454 if (!wined3d_context_gl_const(context)->gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
5455 return false;
5457 if (TRACE_ON(d3d))
5459 TRACE("Checking support for fixup:\n");
5460 dump_color_fixup_desc(src_format->color_fixup);
5463 /* We only support identity conversions. */
5464 if (!is_identity_fixup(src_format->color_fixup)
5465 || !is_identity_fixup(dst_format->color_fixup))
5467 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER
5468 && dst_format->id == src_format->id && dst_location == WINED3D_LOCATION_DRAWABLE)
5470 WARN("Claiming fixup support because of ORM_BACKBUFFER.\n");
5472 else
5474 TRACE("Fixups are not supported.\n");
5475 return false;
5479 if (!(dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET))
5481 TRACE("Can only blit to render targets.\n");
5482 return false;
5484 return true;
5486 default:
5487 TRACE("Unsupported blit operation %#x.\n", blit_op);
5488 return false;
5492 static bool is_full_clear(const struct wined3d_rendertarget_view *rtv, const RECT *draw_rect, const RECT *clear_rect)
5494 unsigned int height = rtv->height;
5495 unsigned int width = rtv->width;
5497 /* partial draw rect */
5498 if (draw_rect->left || draw_rect->top || draw_rect->right < width || draw_rect->bottom < height)
5499 return false;
5501 /* partial clear rect */
5502 if (clear_rect && (clear_rect->left > 0 || clear_rect->top > 0
5503 || clear_rect->right < width || clear_rect->bottom < height))
5504 return false;
5506 return true;
5509 static void ffp_blitter_clear_rendertargets(struct wined3d_device *device, unsigned int rt_count,
5510 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rect, const RECT *draw_rect,
5511 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
5513 struct wined3d_rendertarget_view *rtv = rt_count ? fb->render_targets[0] : NULL;
5514 struct wined3d_rendertarget_view *dsv = fb->depth_stencil;
5515 const struct wined3d_state *state = &device->cs->state;
5516 struct wined3d_texture *depth_stencil = NULL;
5517 unsigned int drawable_width, drawable_height;
5518 const struct wined3d_gl_info *gl_info;
5519 struct wined3d_context_gl *context_gl;
5520 struct wined3d_texture *target = NULL;
5521 struct wined3d_color colour_srgb;
5522 struct wined3d_context *context;
5523 GLbitfield clear_mask = 0;
5524 bool render_offscreen;
5525 unsigned int i;
5527 if (rtv && rtv->resource->type != WINED3D_RTYPE_BUFFER)
5529 target = texture_from_resource(rtv->resource);
5530 context = context_acquire(device, target, rtv->sub_resource_idx);
5532 else
5534 context = context_acquire(device, NULL, 0);
5536 context_gl = wined3d_context_gl(context);
5538 if (dsv && dsv->resource->type != WINED3D_RTYPE_BUFFER)
5539 depth_stencil = texture_from_resource(dsv->resource);
5541 if (!context_gl->valid)
5543 context_release(context);
5544 WARN("Invalid context, skipping clear.\n");
5545 return;
5547 gl_info = context_gl->gl_info;
5549 /* When we're clearing parts of the drawable, make sure that the target
5550 * surface is well up to date in the drawable. After the clear we'll mark
5551 * the drawable up to date, so we have to make sure that this is true for
5552 * the cleared parts, and the untouched parts.
5554 * If we're clearing the whole target there is no need to copy it into the
5555 * drawable, it will be overwritten anyway. If we're not clearing the
5556 * colour buffer we don't have to copy either since we're not going to set
5557 * the drawable up to date. We have to check all settings that limit the
5558 * clear area though. Do not bother checking all this if the destination
5559 * surface is in the drawable anyway. */
5560 for (i = 0; i < rt_count; ++i)
5562 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5564 if (rtv && rtv->format->id != WINED3DFMT_NULL)
5566 if (flags & WINED3DCLEAR_TARGET && !is_full_clear(rtv, draw_rect, rect_count ? clear_rect : NULL))
5567 wined3d_rendertarget_view_load_location(rtv, context, rtv->resource->draw_binding);
5568 else
5569 wined3d_rendertarget_view_prepare_location(rtv, context, rtv->resource->draw_binding);
5573 if (target)
5575 render_offscreen = context->render_offscreen;
5576 wined3d_rendertarget_view_get_drawable_size(rtv, context, &drawable_width, &drawable_height);
5578 else
5580 unsigned int ds_level = dsv->sub_resource_idx % depth_stencil->level_count;
5582 render_offscreen = true;
5583 drawable_width = wined3d_texture_get_level_pow2_width(depth_stencil, ds_level);
5584 drawable_height = wined3d_texture_get_level_pow2_height(depth_stencil, ds_level);
5587 if (depth_stencil)
5589 DWORD ds_location = render_offscreen ? dsv->resource->draw_binding : WINED3D_LOCATION_DRAWABLE;
5591 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)
5592 && !is_full_clear(dsv, draw_rect, rect_count ? clear_rect : NULL))
5593 wined3d_rendertarget_view_load_location(dsv, context, ds_location);
5594 else
5595 wined3d_rendertarget_view_prepare_location(dsv, context, ds_location);
5597 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5599 wined3d_rendertarget_view_validate_location(dsv, ds_location);
5600 wined3d_rendertarget_view_invalidate_location(dsv, ~ds_location);
5604 if (!wined3d_context_gl_apply_clear_state(context_gl, state, rt_count, fb))
5606 context_release(context);
5607 WARN("Failed to apply clear state, skipping clear.\n");
5608 return;
5611 /* Only set the values up once, as they are not changing. */
5612 if (flags & WINED3DCLEAR_STENCIL)
5614 if (gl_info->supported[EXT_STENCIL_TWO_SIDE])
5615 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
5616 gl_info->gl_ops.gl.p_glStencilMask(~0u);
5617 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5618 gl_info->gl_ops.gl.p_glClearStencil(stencil);
5619 checkGLcall("glClearStencil");
5620 clear_mask = clear_mask | GL_STENCIL_BUFFER_BIT;
5623 if (flags & WINED3DCLEAR_ZBUFFER)
5625 gl_info->gl_ops.gl.p_glDepthMask(GL_TRUE);
5626 context_invalidate_state(context, STATE_DEPTH_STENCIL);
5627 if (gl_info->supported[ARB_ES2_COMPATIBILITY])
5628 GL_EXTCALL(glClearDepthf(depth));
5629 else
5630 gl_info->gl_ops.gl.p_glClearDepth(depth);
5631 checkGLcall("glClearDepth");
5632 clear_mask = clear_mask | GL_DEPTH_BUFFER_BIT;
5635 if (flags & WINED3DCLEAR_TARGET)
5637 for (i = 0; i < rt_count; ++i)
5639 struct wined3d_rendertarget_view *rtv = fb->render_targets[i];
5641 if (!rtv)
5642 continue;
5644 if (rtv->resource->type == WINED3D_RTYPE_BUFFER)
5646 FIXME("Not supported on buffer resources.\n");
5647 continue;
5650 wined3d_rendertarget_view_validate_location(rtv, rtv->resource->draw_binding);
5651 wined3d_rendertarget_view_invalidate_location(rtv, ~rtv->resource->draw_binding);
5654 if (!gl_info->supported[ARB_FRAMEBUFFER_SRGB] && needs_srgb_write(context->d3d_info, state, fb))
5656 if (rt_count > 1)
5657 WARN("Clearing multiple sRGB render targets without GL_ARB_framebuffer_sRGB "
5658 "support, this might cause graphical issues.\n");
5660 wined3d_colour_srgb_from_linear(&colour_srgb, colour);
5661 colour = &colour_srgb;
5664 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
5665 context_invalidate_state(context, STATE_BLEND);
5666 gl_info->gl_ops.gl.p_glClearColor(colour->r, colour->g, colour->b, colour->a);
5667 checkGLcall("glClearColor");
5668 clear_mask = clear_mask | GL_COLOR_BUFFER_BIT;
5671 if (!rect_count)
5673 if (render_offscreen)
5675 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, draw_rect->top,
5676 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5678 else
5680 gl_info->gl_ops.gl.p_glScissor(draw_rect->left, drawable_height - draw_rect->bottom,
5681 draw_rect->right - draw_rect->left, draw_rect->bottom - draw_rect->top);
5683 gl_info->gl_ops.gl.p_glClear(clear_mask);
5685 else
5687 RECT current_rect;
5689 /* Now process each rect in turn. */
5690 for (i = 0; i < rect_count; ++i)
5692 /* Note that GL uses lower left, width/height. */
5693 IntersectRect(&current_rect, draw_rect, &clear_rect[i]);
5695 TRACE("clear_rect[%u] %s, current_rect %s.\n", i,
5696 wine_dbgstr_rect(&clear_rect[i]),
5697 wine_dbgstr_rect(&current_rect));
5699 /* Tests show that rectangles where x1 > x2 or y1 > y2 are ignored
5700 * silently. The rectangle is not cleared, no error is returned,
5701 * but further rectangles are still cleared if they are valid. */
5702 if (current_rect.left > current_rect.right || current_rect.top > current_rect.bottom)
5704 TRACE("Rectangle with negative dimensions, ignoring.\n");
5705 continue;
5708 if (render_offscreen)
5710 gl_info->gl_ops.gl.p_glScissor(current_rect.left, current_rect.top,
5711 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5713 else
5715 gl_info->gl_ops.gl.p_glScissor(current_rect.left, drawable_height - current_rect.bottom,
5716 current_rect.right - current_rect.left, current_rect.bottom - current_rect.top);
5718 gl_info->gl_ops.gl.p_glClear(clear_mask);
5721 context->scissor_rect_count = WINED3D_MAX_VIEWPORTS;
5722 checkGLcall("clear");
5724 if (flags & WINED3DCLEAR_TARGET && target->swapchain && target->swapchain->front_buffer == target)
5725 gl_info->gl_ops.gl.p_glFlush();
5727 context_release(context);
5730 static bool blitter_use_cpu_clear(struct wined3d_rendertarget_view *view)
5732 struct wined3d_resource *resource;
5733 struct wined3d_texture *texture;
5734 DWORD locations;
5736 resource = view->resource;
5737 if (resource->type == WINED3D_RTYPE_BUFFER)
5738 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU);
5740 texture = texture_from_resource(resource);
5741 locations = texture->sub_resources[view->sub_resource_idx].locations;
5742 if (locations & (resource->map_binding | WINED3D_LOCATION_DISCARDED))
5743 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5744 || (texture->flags & WINED3D_TEXTURE_PIN_SYSMEM);
5746 return !(resource->access & WINED3D_RESOURCE_ACCESS_GPU)
5747 && !(texture->flags & WINED3D_TEXTURE_CONVERTED);
5750 static void ffp_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
5751 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
5752 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
5754 struct wined3d_rendertarget_view *view, *previous = NULL;
5755 bool have_identical_size = TRUE;
5756 struct wined3d_fb_state tmp_fb;
5757 unsigned int next_rt_count = 0;
5758 struct wined3d_blitter *next;
5759 DWORD next_flags = 0;
5760 unsigned int i;
5762 if (flags & WINED3DCLEAR_TARGET)
5764 for (i = 0; i < rt_count; ++i)
5766 if (!(view = fb->render_targets[i]))
5767 continue;
5769 if (blitter_use_cpu_clear(view)
5770 || (!(view->resource->bind_flags & WINED3D_BIND_RENDER_TARGET)
5771 && (wined3d_settings.offscreen_rendering_mode != ORM_FBO
5772 || !(view->format_flags & WINED3DFMT_FLAG_FBO_ATTACHABLE))))
5774 next_flags |= WINED3DCLEAR_TARGET;
5775 flags &= ~WINED3DCLEAR_TARGET;
5776 next_rt_count = rt_count;
5777 rt_count = 0;
5778 break;
5781 /* FIXME: We should reject colour fills on formats with fixups,
5782 * but this would break P8 colour fills for example. */
5786 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
5787 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
5788 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
5789 && blitter_use_cpu_clear(view))
5791 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5792 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
5795 if (flags)
5797 for (i = 0; i < rt_count; ++i)
5799 if (!(view = fb->render_targets[i]))
5800 continue;
5802 if (previous && (previous->width != view->width || previous->height != view->height))
5803 have_identical_size = false;
5804 previous = view;
5806 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5808 view = fb->depth_stencil;
5810 if (previous && (previous->width != view->width || previous->height != view->height))
5811 have_identical_size = false;
5814 if (have_identical_size)
5816 ffp_blitter_clear_rendertargets(device, rt_count, fb, rect_count,
5817 clear_rects, draw_rect, flags, colour, depth, stencil);
5819 else
5821 for (i = 0; i < rt_count; ++i)
5823 if (!(view = fb->render_targets[i]))
5824 continue;
5826 tmp_fb.render_targets[0] = view;
5827 tmp_fb.depth_stencil = NULL;
5828 ffp_blitter_clear_rendertargets(device, 1, &tmp_fb, rect_count,
5829 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
5831 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
5833 tmp_fb.render_targets[0] = NULL;
5834 tmp_fb.depth_stencil = fb->depth_stencil;
5835 ffp_blitter_clear_rendertargets(device, 0, &tmp_fb, rect_count,
5836 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
5841 if (next_flags && (next = blitter->next))
5842 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
5843 clear_rects, draw_rect, next_flags, colour, depth, stencil);
5846 static DWORD ffp_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
5847 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
5848 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
5849 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
5850 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
5851 const struct wined3d_format *resolve_format)
5853 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
5854 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
5855 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
5856 struct wined3d_resource *src_resource, *dst_resource;
5857 struct wined3d_texture *staging_texture = NULL;
5858 struct wined3d_color_key old_blt_key;
5859 struct wined3d_device *device;
5860 struct wined3d_blitter *next;
5861 DWORD old_colour_key_flags;
5862 RECT r;
5864 src_resource = &src_texture->resource;
5865 dst_resource = &dst_texture->resource;
5866 device = dst_resource->device;
5868 if (!ffp_blit_supported(op, context, src_resource, src_location, dst_resource, dst_location))
5870 if ((next = blitter->next))
5871 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
5872 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
5873 resolve_format);
5876 TRACE("Blt from texture %p, %u to rendertarget %p, %u.\n",
5877 src_texture, src_sub_resource_idx, dst_texture, dst_sub_resource_idx);
5879 old_blt_key = src_texture->async.src_blt_color_key;
5880 old_colour_key_flags = src_texture->async.color_key_flags;
5881 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT, colour_key);
5883 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
5885 struct wined3d_resource_desc desc;
5886 struct wined3d_box upload_box;
5887 unsigned int src_level;
5888 HRESULT hr;
5890 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
5892 src_level = src_sub_resource_idx % src_texture->level_count;
5893 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
5894 desc.format = src_texture->resource.format->id;
5895 desc.multisample_type = src_texture->resource.multisample_type;
5896 desc.multisample_quality = src_texture->resource.multisample_quality;
5897 desc.usage = WINED3DUSAGE_PRIVATE;
5898 desc.bind_flags = 0;
5899 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
5900 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
5901 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
5902 desc.depth = 1;
5903 desc.size = 0;
5905 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
5906 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
5908 ERR("Failed to create staging texture, hr %#x.\n", hr);
5909 return dst_location;
5912 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
5913 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
5914 src_texture, src_sub_resource_idx, &upload_box);
5916 src_texture = staging_texture;
5917 src_texture_gl = wined3d_texture_gl(src_texture);
5918 src_sub_resource_idx = 0;
5920 else
5922 /* Make sure the surface is up-to-date. This should probably use
5923 * surface_load_location() and worry about the destination surface
5924 * too, unless we're overwriting it completely. */
5925 wined3d_texture_load(src_texture, context, FALSE);
5928 wined3d_context_gl_apply_ffp_blit_state(context_gl, device);
5930 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5932 r = *dst_rect;
5933 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &r);
5934 dst_rect = &r;
5937 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
5939 GLenum buffer;
5941 if (dst_location == WINED3D_LOCATION_DRAWABLE)
5943 TRACE("Destination texture %p is onscreen.\n", dst_texture);
5944 buffer = wined3d_texture_get_gl_buffer(dst_texture);
5946 else
5948 TRACE("Destination texture %p is offscreen.\n", dst_texture);
5949 buffer = GL_COLOR_ATTACHMENT0;
5951 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
5952 dst_resource, dst_sub_resource_idx, NULL, 0, dst_location);
5953 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
5954 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
5955 context_invalidate_state(context, STATE_FRAMEBUFFER);
5958 gl_info->gl_ops.gl.p_glEnable(src_texture_gl->target);
5959 checkGLcall("glEnable(target)");
5961 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5963 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
5964 checkGLcall("glEnable(GL_ALPHA_TEST)");
5967 if (colour_key)
5969 /* For P8 surfaces, the alpha component contains the palette index.
5970 * Which means that the colourkey is one of the palette entries. In
5971 * other cases pixels that should be masked away have alpha set to 0. */
5972 if (src_texture->resource.format->id == WINED3DFMT_P8_UINT)
5973 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL,
5974 (float)src_texture->async.src_blt_color_key.color_space_low_value / 255.0f);
5975 else
5976 gl_info->gl_ops.gl.p_glAlphaFunc(GL_NOTEQUAL, 0.0f);
5977 checkGLcall("glAlphaFunc");
5980 wined3d_context_gl_draw_textured_quad(context_gl, src_texture_gl,
5981 src_sub_resource_idx, src_rect, dst_rect, filter);
5983 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST || colour_key)
5985 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
5986 checkGLcall("glDisable(GL_ALPHA_TEST)");
5989 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
5990 checkGLcall("glDisable(GL_TEXTURE_2D)");
5991 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
5993 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5994 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5996 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5998 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
5999 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
6002 if (dst_texture->swapchain && dst_texture->swapchain->front_buffer == dst_texture)
6003 gl_info->gl_ops.gl.p_glFlush();
6005 /* Restore the colour key parameters */
6006 wined3d_texture_set_color_key(src_texture, WINED3D_CKEY_SRC_BLT,
6007 (old_colour_key_flags & WINED3D_CKEY_SRC_BLT) ? &old_blt_key : NULL);
6009 if (staging_texture)
6010 wined3d_texture_decref(staging_texture);
6012 return dst_location;
6015 static const struct wined3d_blitter_ops ffp_blitter_ops =
6017 ffp_blitter_destroy,
6018 ffp_blitter_clear,
6019 ffp_blitter_blit,
6022 void wined3d_ffp_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6024 struct wined3d_blitter *blitter;
6026 if (!(blitter = heap_alloc(sizeof(*blitter))))
6027 return;
6029 TRACE("Created blitter %p.\n", blitter);
6031 blitter->ops = &ffp_blitter_ops;
6032 blitter->next = *next;
6033 *next = blitter;
6036 static void fbo_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6038 struct wined3d_blitter *next;
6040 if ((next = blitter->next))
6041 next->ops->blitter_destroy(next, context);
6043 heap_free(blitter);
6046 static void fbo_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6047 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6048 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6050 struct wined3d_blitter *next;
6052 if ((next = blitter->next))
6053 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6054 clear_rects, draw_rect, flags, colour, depth, stencil);
6057 static DWORD fbo_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6058 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6059 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6060 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6061 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6062 const struct wined3d_format *resolve_format)
6064 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6065 struct wined3d_resource *src_resource, *dst_resource;
6066 enum wined3d_blit_op blit_op = op;
6067 struct wined3d_device *device;
6068 struct wined3d_blitter *next;
6070 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6071 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6072 "colour_key %p, filter %s, resolve_format %p.\n",
6073 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6074 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6075 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6077 src_resource = &src_texture->resource;
6078 dst_resource = &dst_texture->resource;
6080 device = dst_resource->device;
6082 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_resource->format->id == src_resource->format->id)
6084 if (dst_resource->format->depth_size || dst_resource->format->stencil_size)
6085 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
6086 else
6087 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
6090 if (!fbo_blitter_supported(blit_op, context_gl->gl_info,
6091 src_resource, src_location, dst_resource, dst_location))
6093 if (!(next = blitter->next))
6095 ERR("No blitter to handle blit op %#x.\n", op);
6096 return dst_location;
6099 TRACE("Forwarding to blitter %p.\n", next);
6100 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6101 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6102 resolve_format);
6105 if (blit_op == WINED3D_BLIT_OP_COLOR_BLIT)
6107 TRACE("Colour blit.\n");
6108 texture2d_blt_fbo(device, context, filter, src_texture, src_sub_resource_idx, src_location,
6109 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, resolve_format);
6110 return dst_location;
6113 if (blit_op == WINED3D_BLIT_OP_DEPTH_BLIT)
6115 TRACE("Depth/stencil blit.\n");
6116 texture2d_depth_blt_fbo(device, context, src_texture, src_sub_resource_idx, src_location,
6117 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect);
6118 return dst_location;
6121 ERR("This blitter does not implement blit op %#x.\n", blit_op);
6122 return dst_location;
6125 static const struct wined3d_blitter_ops fbo_blitter_ops =
6127 fbo_blitter_destroy,
6128 fbo_blitter_clear,
6129 fbo_blitter_blit,
6132 void wined3d_fbo_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6134 struct wined3d_blitter *blitter;
6136 if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
6137 return;
6139 if (!(blitter = heap_alloc(sizeof(*blitter))))
6140 return;
6142 TRACE("Created blitter %p.\n", blitter);
6144 blitter->ops = &fbo_blitter_ops;
6145 blitter->next = *next;
6146 *next = blitter;
6149 static void raw_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6151 struct wined3d_blitter *next;
6153 if ((next = blitter->next))
6154 next->ops->blitter_destroy(next, context);
6156 heap_free(blitter);
6159 static void raw_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6160 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6161 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6163 struct wined3d_blitter *next;
6165 if (!(next = blitter->next))
6167 ERR("No blitter to handle clear.\n");
6168 return;
6171 TRACE("Forwarding to blitter %p.\n", next);
6172 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
6173 clear_rects, draw_rect, flags, colour, depth, stencil);
6176 static bool gl_formats_compatible(struct wined3d_texture *src_texture, DWORD src_location,
6177 struct wined3d_texture *dst_texture, DWORD dst_location)
6179 GLuint src_internal, dst_internal;
6180 bool src_ds, dst_ds;
6182 src_ds = src_texture->resource.format->depth_size || src_texture->resource.format->stencil_size;
6183 dst_ds = dst_texture->resource.format->depth_size || dst_texture->resource.format->stencil_size;
6184 if (src_ds == dst_ds)
6185 return true;
6186 /* Also check the internal format because, e.g. WINED3DFMT_D24_UNORM_S8_UINT has nonzero depth and stencil
6187 * sizes as does WINED3DFMT_R24G8_TYPELESS when bound with flag WINED3D_BIND_DEPTH_STENCIL, but these share
6188 * the same internal format with WINED3DFMT_R24_UNORM_X8_TYPELESS. */
6189 src_internal = wined3d_gl_get_internal_format(&src_texture->resource,
6190 wined3d_format_gl(src_texture->resource.format), src_location == WINED3D_LOCATION_TEXTURE_SRGB);
6191 dst_internal = wined3d_gl_get_internal_format(&dst_texture->resource,
6192 wined3d_format_gl(dst_texture->resource.format), dst_location == WINED3D_LOCATION_TEXTURE_SRGB);
6193 return src_internal == dst_internal;
6196 static DWORD raw_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6197 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6198 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6199 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6200 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6201 const struct wined3d_format *resolve_format)
6203 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
6204 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
6205 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
6206 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
6207 unsigned int src_level, src_layer, dst_level, dst_layer;
6208 struct wined3d_blitter *next;
6209 GLuint src_name, dst_name;
6210 DWORD location;
6212 /* If we would need to copy from a renderbuffer or drawable, we'd probably
6213 * be better off using the FBO blitter directly, since we'd need to use it
6214 * to copy the resource contents to the texture anyway.
6216 * We also can't copy between depth/stencil and colour resources, since
6217 * the formats are considered incompatible in OpenGL. */
6218 if (op != WINED3D_BLIT_OP_RAW_BLIT || !gl_formats_compatible(src_texture, src_location, dst_texture, dst_location)
6219 || (src_texture->resource.format->id == dst_texture->resource.format->id
6220 && (!(src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
6221 || !(dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB)))))
6223 if (!(next = blitter->next))
6225 ERR("No blitter to handle blit op %#x.\n", op);
6226 return dst_location;
6229 TRACE("Forwarding to blitter %p.\n", next);
6230 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
6231 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
6232 resolve_format);
6235 TRACE("Blit using ARB_copy_image.\n");
6237 src_level = src_sub_resource_idx % src_texture->level_count;
6238 src_layer = src_sub_resource_idx / src_texture->level_count;
6240 dst_level = dst_sub_resource_idx % dst_texture->level_count;
6241 dst_layer = dst_sub_resource_idx / dst_texture->level_count;
6243 location = src_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6244 if (!location)
6245 location = src_texture->flags & WINED3D_TEXTURE_IS_SRGB
6246 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6247 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, location))
6248 ERR("Failed to load the source sub-resource into %s.\n", wined3d_debug_location(location));
6249 src_name = wined3d_texture_gl_get_texture_name(src_texture_gl,
6250 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6252 location = dst_location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
6253 if (!location)
6254 location = dst_texture->flags & WINED3D_TEXTURE_IS_SRGB
6255 ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
6256 if (wined3d_texture_is_full_rect(dst_texture, dst_level, dst_rect))
6258 if (!wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, location))
6259 ERR("Failed to prepare the destination sub-resource into %s.\n", wined3d_debug_location(location));
6261 else
6263 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, location))
6264 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(location));
6266 dst_name = wined3d_texture_gl_get_texture_name(dst_texture_gl,
6267 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
6269 GL_EXTCALL(glCopyImageSubData(src_name, src_texture_gl->target, src_level,
6270 src_rect->left, src_rect->top, src_layer, dst_name, dst_texture_gl->target, dst_level,
6271 dst_rect->left, dst_rect->top, dst_layer, src_rect->right - src_rect->left,
6272 src_rect->bottom - src_rect->top, 1));
6273 checkGLcall("copy image data");
6275 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, location);
6276 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~location);
6277 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6278 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6280 return dst_location | location;
6283 static const struct wined3d_blitter_ops raw_blitter_ops =
6285 raw_blitter_destroy,
6286 raw_blitter_clear,
6287 raw_blitter_blit,
6290 void wined3d_raw_blitter_create(struct wined3d_blitter **next, const struct wined3d_gl_info *gl_info)
6292 struct wined3d_blitter *blitter;
6294 if (!gl_info->supported[ARB_COPY_IMAGE])
6295 return;
6297 if (!(blitter = heap_alloc(sizeof(*blitter))))
6298 return;
6300 TRACE("Created blitter %p.\n", blitter);
6302 blitter->ops = &raw_blitter_ops;
6303 blitter->next = *next;
6304 *next = blitter;
6307 static void vk_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
6309 struct wined3d_blitter *next;
6311 TRACE("blitter %p, context %p.\n", blitter, context);
6313 if ((next = blitter->next))
6314 next->ops->blitter_destroy(next, context);
6316 heap_free(blitter);
6319 static void vk_blitter_clear_rendertargets(struct wined3d_context_vk *context_vk, unsigned int rt_count,
6320 const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects, const RECT *draw_rect,
6321 uint32_t flags, const struct wined3d_color *colour, float depth, unsigned int stencil)
6323 VkClearValue clear_values[WINED3D_MAX_RENDER_TARGETS + 1];
6324 VkImageView views[WINED3D_MAX_RENDER_TARGETS + 1];
6325 struct wined3d_rendertarget_view_vk *rtv_vk;
6326 struct wined3d_rendertarget_view *view;
6327 const struct wined3d_vk_info *vk_info;
6328 struct wined3d_device_vk *device_vk;
6329 VkCommandBuffer vk_command_buffer;
6330 VkRenderPassBeginInfo begin_desc;
6331 unsigned int i, attachment_count;
6332 VkFramebufferCreateInfo fb_desc;
6333 VkFramebuffer vk_framebuffer;
6334 VkRenderPass vk_render_pass;
6335 bool depth_stencil = false;
6336 unsigned int layer_count;
6337 VkClearColorValue *c;
6338 VkResult vr;
6339 RECT r;
6341 TRACE("context_vk %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6342 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6343 context_vk, rt_count, fb, rect_count, clear_rects,
6344 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6346 device_vk = wined3d_device_vk(context_vk->c.device);
6347 vk_info = context_vk->vk_info;
6349 if (!(flags & WINED3DCLEAR_TARGET))
6350 rt_count = 0;
6352 for (i = 0, attachment_count = 0, layer_count = 1; i < rt_count; ++i)
6354 if (!(view = fb->render_targets[i]))
6355 continue;
6357 if (!is_full_clear(view, draw_rect, clear_rects))
6358 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6359 else
6360 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6361 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6362 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6364 rtv_vk = wined3d_rendertarget_view_vk(view);
6365 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6366 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_RENDER_TARGET);
6368 c = &clear_values[attachment_count].color;
6369 if (view->format_flags & WINED3DFMT_FLAG_INTEGER)
6371 c->int32[0] = colour->r;
6372 c->int32[1] = colour->g;
6373 c->int32[2] = colour->b;
6374 c->int32[3] = colour->a;
6376 else
6378 c->float32[0] = colour->r;
6379 c->float32[1] = colour->g;
6380 c->float32[2] = colour->b;
6381 c->float32[3] = colour->a;
6384 if (view->layer_count > layer_count)
6385 layer_count = view->layer_count;
6387 ++attachment_count;
6390 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL) && (view = fb->depth_stencil))
6392 if (!is_full_clear(view, draw_rect, clear_rects))
6393 wined3d_rendertarget_view_load_location(view, &context_vk->c, view->resource->draw_binding);
6394 else
6395 wined3d_rendertarget_view_prepare_location(view, &context_vk->c, view->resource->draw_binding);
6396 wined3d_rendertarget_view_validate_location(view, view->resource->draw_binding);
6397 wined3d_rendertarget_view_invalidate_location(view, ~view->resource->draw_binding);
6399 rtv_vk = wined3d_rendertarget_view_vk(view);
6400 views[attachment_count] = wined3d_rendertarget_view_vk_get_image_view(rtv_vk, context_vk);
6401 wined3d_rendertarget_view_vk_barrier(rtv_vk, context_vk, WINED3D_BIND_DEPTH_STENCIL);
6403 clear_values[attachment_count].depthStencil.depth = depth;
6404 clear_values[attachment_count].depthStencil.stencil = stencil;
6406 if (view->layer_count > layer_count)
6407 layer_count = view->layer_count;
6409 depth_stencil = true;
6410 ++attachment_count;
6413 if (!attachment_count)
6414 return;
6416 if (!(vk_render_pass = wined3d_context_vk_get_render_pass(context_vk, fb,
6417 rt_count, flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL), flags)))
6419 ERR("Failed to get render pass.\n");
6420 return;
6423 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6425 ERR("Failed to get command buffer.\n");
6426 return;
6429 fb_desc.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
6430 fb_desc.pNext = NULL;
6431 fb_desc.flags = 0;
6432 fb_desc.renderPass = vk_render_pass;
6433 fb_desc.attachmentCount = attachment_count;
6434 fb_desc.pAttachments = views;
6435 fb_desc.width = draw_rect->right - draw_rect->left;
6436 fb_desc.height = draw_rect->bottom - draw_rect->top;
6437 fb_desc.layers = layer_count;
6438 if ((vr = VK_CALL(vkCreateFramebuffer(device_vk->vk_device, &fb_desc, NULL, &vk_framebuffer))) < 0)
6440 ERR("Failed to create Vulkan framebuffer, vr %s.\n", wined3d_debug_vkresult(vr));
6441 return;
6444 begin_desc.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
6445 begin_desc.pNext = NULL;
6446 begin_desc.renderPass = vk_render_pass;
6447 begin_desc.framebuffer = vk_framebuffer;
6448 begin_desc.clearValueCount = attachment_count;
6449 begin_desc.pClearValues = clear_values;
6451 wined3d_context_vk_end_current_render_pass(context_vk);
6453 for (i = 0; i < rect_count; ++i)
6455 r.left = max(clear_rects[i].left, draw_rect->left);
6456 r.top = max(clear_rects[i].top, draw_rect->top);
6457 r.right = min(clear_rects[i].right, draw_rect->right);
6458 r.bottom = min(clear_rects[i].bottom, draw_rect->bottom);
6460 if (r.left >= r.right || r.top >= r.bottom)
6461 continue;
6463 begin_desc.renderArea.offset.x = r.left;
6464 begin_desc.renderArea.offset.y = r.top;
6465 begin_desc.renderArea.extent.width = r.right - r.left;
6466 begin_desc.renderArea.extent.height = r.bottom - r.top;
6467 VK_CALL(vkCmdBeginRenderPass(vk_command_buffer, &begin_desc, VK_SUBPASS_CONTENTS_INLINE));
6468 VK_CALL(vkCmdEndRenderPass(vk_command_buffer));
6471 wined3d_context_vk_destroy_vk_framebuffer(context_vk, vk_framebuffer, context_vk->current_command_buffer.id);
6473 for (i = 0; i < rt_count; ++i)
6475 if (!(view = fb->render_targets[i]))
6476 continue;
6478 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6481 if (depth_stencil)
6483 view = fb->depth_stencil;
6484 wined3d_context_vk_reference_rendertarget_view(context_vk, wined3d_rendertarget_view_vk(view));
6488 static void vk_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
6489 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
6490 const RECT *draw_rect, DWORD flags, const struct wined3d_color *colour, float depth, DWORD stencil)
6492 struct wined3d_device_vk *device_vk = wined3d_device_vk(device);
6493 struct wined3d_rendertarget_view *view, *previous = NULL;
6494 struct wined3d_context_vk *context_vk;
6495 bool have_identical_size = true;
6496 struct wined3d_fb_state tmp_fb;
6497 unsigned int next_rt_count = 0;
6498 struct wined3d_blitter *next;
6499 uint32_t next_flags = 0;
6500 unsigned int i;
6502 TRACE("blitter %p, device %p, rt_count %u, fb %p, rect_count %u, clear_rects %p, "
6503 "draw_rect %s, flags %#x, colour %s, depth %.8e, stencil %#x.\n",
6504 blitter, device, rt_count, fb, rect_count, clear_rects,
6505 wine_dbgstr_rect(draw_rect), flags, debug_color(colour), depth, stencil);
6507 if (!rect_count)
6509 rect_count = 1;
6510 clear_rects = draw_rect;
6513 if (flags & WINED3DCLEAR_TARGET)
6515 for (i = 0; i < rt_count; ++i)
6517 if (!(view = fb->render_targets[i]))
6518 continue;
6520 if (blitter_use_cpu_clear(view))
6522 next_flags |= WINED3DCLEAR_TARGET;
6523 flags &= ~WINED3DCLEAR_TARGET;
6524 next_rt_count = rt_count;
6525 rt_count = 0;
6526 break;
6531 if ((flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL)) && (view = fb->depth_stencil)
6532 && (!view->format->depth_size || (flags & WINED3DCLEAR_ZBUFFER))
6533 && (!view->format->stencil_size || (flags & WINED3DCLEAR_STENCIL))
6534 && blitter_use_cpu_clear(view))
6536 next_flags |= flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6537 flags &= ~(WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL);
6540 if (flags)
6542 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
6544 for (i = 0; i < rt_count; ++i)
6546 if (!(view = fb->render_targets[i]))
6547 continue;
6549 if (previous && (previous->width != view->width || previous->height != view->height))
6550 have_identical_size = false;
6551 previous = view;
6553 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6555 view = fb->depth_stencil;
6557 if (previous && (previous->width != view->width || previous->height != view->height))
6558 have_identical_size = false;
6561 if (have_identical_size)
6563 vk_blitter_clear_rendertargets(context_vk, rt_count, fb, rect_count,
6564 clear_rects, draw_rect, flags, colour, depth, stencil);
6566 else
6568 for (i = 0; i < rt_count; ++i)
6570 if (!(view = fb->render_targets[i]))
6571 continue;
6573 tmp_fb.render_targets[0] = view;
6574 tmp_fb.depth_stencil = NULL;
6575 vk_blitter_clear_rendertargets(context_vk, 1, &tmp_fb, rect_count,
6576 clear_rects, draw_rect, WINED3DCLEAR_TARGET, colour, depth, stencil);
6578 if (flags & (WINED3DCLEAR_ZBUFFER | WINED3DCLEAR_STENCIL))
6580 tmp_fb.render_targets[0] = NULL;
6581 tmp_fb.depth_stencil = fb->depth_stencil;
6582 vk_blitter_clear_rendertargets(context_vk, 0, &tmp_fb, rect_count,
6583 clear_rects, draw_rect, flags & ~WINED3DCLEAR_TARGET, colour, depth, stencil);
6587 context_release(&context_vk->c);
6590 if (!next_flags)
6591 return;
6593 if (!(next = blitter->next))
6595 ERR("No blitter to handle clear.\n");
6596 return;
6599 TRACE("Forwarding to blitter %p.\n", next);
6600 next->ops->blitter_clear(next, device, next_rt_count, fb, rect_count,
6601 clear_rects, draw_rect, next_flags, colour, depth, stencil);
6604 static bool vk_blitter_blit_supported(enum wined3d_blit_op op, const struct wined3d_context *context,
6605 const struct wined3d_resource *src_resource, const RECT *src_rect,
6606 const struct wined3d_resource *dst_resource, const RECT *dst_rect, const struct wined3d_format *resolve_format)
6608 const struct wined3d_format *src_format = src_resource->format;
6609 const struct wined3d_format *dst_format = dst_resource->format;
6611 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6613 TRACE("Destination resource does not have GPU access.\n");
6614 return false;
6617 if (!(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
6619 TRACE("Source resource does not have GPU access.\n");
6620 return false;
6623 if (dst_format->id != src_format->id)
6625 if (!is_identity_fixup(dst_format->color_fixup))
6627 TRACE("Destination fixups are not supported.\n");
6628 return false;
6631 if (!is_identity_fixup(src_format->color_fixup))
6633 TRACE("Source fixups are not supported.\n");
6634 return false;
6637 if (op != WINED3D_BLIT_OP_RAW_BLIT
6638 && wined3d_format_vk(src_format)->vk_format != wined3d_format_vk(dst_format)->vk_format
6639 && ((!wined3d_format_is_typeless(src_format) && !wined3d_format_is_typeless(dst_format))
6640 || !resolve_format))
6642 TRACE("Format conversion not supported.\n");
6643 return false;
6647 if (wined3d_resource_get_sample_count(dst_resource) > 1)
6649 TRACE("Multi-sample destination resource not supported.\n");
6650 return false;
6653 if (op == WINED3D_BLIT_OP_RAW_BLIT)
6654 return true;
6656 if (op != WINED3D_BLIT_OP_COLOR_BLIT)
6658 TRACE("Unsupported blit operation %#x.\n", op);
6659 return false;
6662 if ((src_rect->right - src_rect->left != dst_rect->right - dst_rect->left)
6663 || (src_rect->bottom - src_rect->top != dst_rect->bottom - dst_rect->top))
6665 TRACE("Scaling not supported.\n");
6666 return false;
6669 return true;
6672 static DWORD vk_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
6673 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
6674 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
6675 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
6676 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
6677 const struct wined3d_format *resolve_format)
6679 struct wined3d_texture_vk *src_texture_vk = wined3d_texture_vk(src_texture);
6680 struct wined3d_texture_vk *dst_texture_vk = wined3d_texture_vk(dst_texture);
6681 struct wined3d_context_vk *context_vk = wined3d_context_vk(context);
6682 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
6683 VkImageSubresourceRange vk_src_range, vk_dst_range;
6684 VkCommandBuffer vk_command_buffer;
6685 struct wined3d_blitter *next;
6686 unsigned src_sample_count;
6687 bool resolve = false;
6689 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
6690 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
6691 "colour_key %p, filter %s, resolve format %p.\n",
6692 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
6693 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
6694 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
6696 if (!vk_blitter_blit_supported(op, context, &src_texture->resource, src_rect, &dst_texture->resource, dst_rect,
6697 resolve_format))
6698 goto next;
6700 src_sample_count = wined3d_resource_get_sample_count(&src_texture_vk->t.resource);
6701 if (src_sample_count > 1)
6702 resolve = true;
6704 vk_src_range.aspectMask = vk_aspect_mask_from_format(src_texture_vk->t.resource.format);
6705 vk_src_range.baseMipLevel = src_sub_resource_idx % src_texture->level_count;
6706 vk_src_range.levelCount = 1;
6707 vk_src_range.baseArrayLayer = src_sub_resource_idx / src_texture->level_count;
6708 vk_src_range.layerCount = 1;
6710 vk_dst_range.aspectMask = vk_aspect_mask_from_format(dst_texture_vk->t.resource.format);
6711 vk_dst_range.baseMipLevel = dst_sub_resource_idx % dst_texture->level_count;
6712 vk_dst_range.levelCount = 1;
6713 vk_dst_range.baseArrayLayer = dst_sub_resource_idx / dst_texture->level_count;
6714 vk_dst_range.layerCount = 1;
6716 if (!wined3d_texture_load_location(src_texture, src_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6717 ERR("Failed to load the source sub-resource.\n");
6719 if (wined3d_texture_is_full_rect(dst_texture, vk_dst_range.baseMipLevel, dst_rect))
6721 if (!wined3d_texture_prepare_location(dst_texture,
6722 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6724 ERR("Failed to prepare the destination sub-resource.\n");
6725 goto next;
6728 else
6730 if (!wined3d_texture_load_location(dst_texture,
6731 dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB))
6733 ERR("Failed to load the destination sub-resource.\n");
6734 goto next;
6738 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
6740 ERR("Failed to get command buffer.\n");
6741 goto next;
6744 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6745 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6746 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6747 VK_ACCESS_TRANSFER_READ_BIT,
6748 src_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6749 src_texture_vk->image.vk_image, &vk_src_range);
6750 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6751 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6752 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6753 VK_ACCESS_TRANSFER_WRITE_BIT,
6754 dst_texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6755 dst_texture_vk->image.vk_image, &vk_dst_range);
6757 if (resolve)
6759 const struct wined3d_format_vk *src_format_vk = wined3d_format_vk(src_texture->resource.format);
6760 const struct wined3d_format_vk *dst_format_vk = wined3d_format_vk(dst_texture->resource.format);
6761 const unsigned int usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT
6762 | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
6763 VkImage src_vk_image, dst_vk_image;
6764 VkImageSubresourceRange vk_range;
6765 VkImageResolve resolve_region;
6766 VkImageType vk_image_type;
6767 VkImageCopy copy_region;
6768 VkFormat vk_format;
6770 if (resolve_format)
6772 vk_format = wined3d_format_vk(resolve_format)->vk_format;
6774 else if (!wined3d_format_is_typeless(src_texture->resource.format))
6776 vk_format = src_format_vk->vk_format;
6778 else
6780 vk_format = dst_format_vk->vk_format;
6783 switch (src_texture->resource.type)
6785 case WINED3D_RTYPE_TEXTURE_1D:
6786 vk_image_type = VK_IMAGE_TYPE_1D;
6787 break;
6788 case WINED3D_RTYPE_TEXTURE_2D:
6789 vk_image_type = VK_IMAGE_TYPE_2D;
6790 break;
6791 case WINED3D_RTYPE_TEXTURE_3D:
6792 vk_image_type = VK_IMAGE_TYPE_3D;
6793 break;
6794 default:
6795 ERR("Unexpected resource type: %s\n", debug_d3dresourcetype(src_texture->resource.type));
6796 goto barrier_next;
6799 vk_range.baseMipLevel = 0;
6800 vk_range.levelCount = 1;
6801 vk_range.baseArrayLayer = 0;
6802 vk_range.layerCount = 1;
6804 resolve_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6805 resolve_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6806 resolve_region.extent.width = src_rect->right - src_rect->left;
6807 resolve_region.extent.height = src_rect->bottom - src_rect->top;
6808 resolve_region.extent.depth = 1;
6810 /* In case of typeless resolve the texture type may not match the resolve type.
6811 * To handle that, allocate intermediate texture(s) to resolve from/to.
6812 * A possible performance improvement would be to resolve using a shader instead. */
6813 if (src_format_vk->vk_format != vk_format)
6815 struct wined3d_image_vk src_image;
6817 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
6818 resolve_region.extent.width, resolve_region.extent.height, 1,
6819 src_sample_count, 1, 1, 0, &src_image))
6820 goto barrier_next;
6822 wined3d_context_vk_reference_image(context_vk, &src_image);
6823 src_vk_image = src_image.vk_image;
6824 wined3d_context_vk_destroy_image(context_vk, &src_image);
6826 vk_range.aspectMask = vk_src_range.aspectMask;
6828 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6829 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6830 0, VK_ACCESS_TRANSFER_WRITE_BIT, VK_IMAGE_LAYOUT_UNDEFINED,
6831 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, src_vk_image, &vk_range);
6833 copy_region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6834 copy_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6835 copy_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6836 copy_region.srcSubresource.layerCount = 1;
6837 copy_region.srcOffset.x = src_rect->left;
6838 copy_region.srcOffset.y = src_rect->top;
6839 copy_region.srcOffset.z = 0;
6840 copy_region.dstSubresource.aspectMask = vk_src_range.aspectMask;
6841 copy_region.dstSubresource.mipLevel = 0;
6842 copy_region.dstSubresource.baseArrayLayer = 0;
6843 copy_region.dstSubresource.layerCount = 1;
6844 copy_region.dstOffset.x = 0;
6845 copy_region.dstOffset.y = 0;
6846 copy_region.dstOffset.z = 0;
6847 copy_region.extent.width = resolve_region.extent.width;
6848 copy_region.extent.height = resolve_region.extent.height;
6849 copy_region.extent.depth = 1;
6851 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image,
6852 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
6853 1, &copy_region));
6855 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6856 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6857 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
6858 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6859 src_vk_image, &vk_range);
6861 resolve_region.srcSubresource.mipLevel = 0;
6862 resolve_region.srcSubresource.baseArrayLayer = 0;
6863 resolve_region.srcSubresource.layerCount = 1;
6864 resolve_region.srcOffset.x = 0;
6865 resolve_region.srcOffset.y = 0;
6866 resolve_region.srcOffset.z = 0;
6868 else
6870 src_vk_image = src_texture_vk->image.vk_image;
6872 resolve_region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6873 resolve_region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6874 resolve_region.srcSubresource.layerCount = 1;
6875 resolve_region.srcOffset.x = src_rect->left;
6876 resolve_region.srcOffset.y = src_rect->top;
6877 resolve_region.srcOffset.z = 0;
6880 if (dst_format_vk->vk_format != vk_format)
6882 struct wined3d_image_vk dst_image;
6884 if (!wined3d_context_vk_create_image(context_vk, vk_image_type, usage, vk_format,
6885 resolve_region.extent.width, resolve_region.extent.height, 1,
6886 VK_SAMPLE_COUNT_1_BIT, 1, 1, 0, &dst_image))
6887 goto barrier_next;
6889 wined3d_context_vk_reference_image(context_vk, &dst_image);
6890 dst_vk_image = dst_image.vk_image;
6891 wined3d_context_vk_destroy_image(context_vk, &dst_image);
6893 vk_range.aspectMask = vk_dst_range.aspectMask;
6894 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6895 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, VK_ACCESS_TRANSFER_WRITE_BIT,
6896 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_vk_image, &vk_range);
6898 resolve_region.dstSubresource.mipLevel = 0;
6899 resolve_region.dstSubresource.baseArrayLayer = 0;
6900 resolve_region.dstSubresource.layerCount = 1;
6901 resolve_region.dstOffset.x = 0;
6902 resolve_region.dstOffset.y = 0;
6903 resolve_region.dstOffset.z = 0;
6905 else
6907 dst_vk_image = dst_texture_vk->image.vk_image;
6909 resolve_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6910 resolve_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6911 resolve_region.dstSubresource.layerCount = 1;
6912 resolve_region.dstOffset.x = dst_rect->left;
6913 resolve_region.dstOffset.y = dst_rect->top;
6914 resolve_region.dstOffset.z = 0;
6917 VK_CALL(vkCmdResolveImage(vk_command_buffer, src_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6918 dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &resolve_region));
6920 if (dst_vk_image != dst_texture_vk->image.vk_image)
6922 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6923 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
6924 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
6925 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6926 dst_vk_image, &vk_range);
6928 copy_region.srcSubresource.aspectMask = vk_dst_range.aspectMask;
6929 copy_region.srcSubresource.mipLevel = 0;
6930 copy_region.srcSubresource.baseArrayLayer = 0;
6931 copy_region.srcSubresource.layerCount = 1;
6932 copy_region.srcOffset.x = 0;
6933 copy_region.srcOffset.y = 0;
6934 copy_region.srcOffset.z = 0;
6935 copy_region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6936 copy_region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6937 copy_region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6938 copy_region.dstSubresource.layerCount = 1;
6939 copy_region.dstOffset.x = dst_rect->left;
6940 copy_region.dstOffset.y = dst_rect->top;
6941 copy_region.dstOffset.z = 0;
6942 copy_region.extent.width = resolve_region.extent.width;
6943 copy_region.extent.height = resolve_region.extent.height;
6944 copy_region.extent.depth = 1;
6946 VK_CALL(vkCmdCopyImage(vk_command_buffer, dst_vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6947 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copy_region));
6950 else
6952 VkImageCopy region;
6954 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
6955 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
6956 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
6957 region.srcSubresource.layerCount = vk_src_range.layerCount;
6958 region.srcOffset.x = src_rect->left;
6959 region.srcOffset.y = src_rect->top;
6960 region.srcOffset.z = 0;
6961 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
6962 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
6963 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
6964 region.dstSubresource.layerCount = vk_dst_range.layerCount;
6965 region.dstOffset.x = dst_rect->left;
6966 region.dstOffset.y = dst_rect->top;
6967 region.dstOffset.z = 0;
6968 region.extent.width = src_rect->right - src_rect->left;
6969 region.extent.height = src_rect->bottom - src_rect->top;
6970 region.extent.depth = 1;
6972 VK_CALL(vkCmdCopyImage(vk_command_buffer, src_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
6973 dst_texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region));
6976 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6977 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6978 VK_ACCESS_TRANSFER_WRITE_BIT,
6979 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
6980 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
6981 dst_texture_vk->image.vk_image, &vk_dst_range);
6982 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
6983 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
6984 VK_ACCESS_TRANSFER_READ_BIT,
6985 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
6986 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
6987 src_texture_vk->image.vk_image, &vk_src_range);
6989 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
6990 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
6991 if (!wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location))
6992 ERR("Failed to load the destination sub-resource into %s.\n", wined3d_debug_location(dst_location));
6994 wined3d_context_vk_reference_texture(context_vk, src_texture_vk);
6995 wined3d_context_vk_reference_texture(context_vk, dst_texture_vk);
6997 return dst_location | WINED3D_LOCATION_TEXTURE_RGB;
6999 barrier_next:
7000 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7001 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7002 VK_ACCESS_TRANSFER_WRITE_BIT,
7003 vk_access_mask_from_bind_flags(dst_texture_vk->t.resource.bind_flags),
7004 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst_texture_vk->layout,
7005 dst_texture_vk->image.vk_image, &vk_dst_range);
7006 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
7007 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
7008 VK_ACCESS_TRANSFER_READ_BIT,
7009 vk_access_mask_from_bind_flags(src_texture_vk->t.resource.bind_flags),
7010 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src_texture_vk->layout,
7011 src_texture_vk->image.vk_image, &vk_src_range);
7013 next:
7014 if (!(next = blitter->next))
7016 ERR("No blitter to handle blit op %#x.\n", op);
7017 return dst_location;
7020 TRACE("Forwarding to blitter %p.\n", next);
7021 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
7022 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter, resolve_format);
7025 static const struct wined3d_blitter_ops vk_blitter_ops =
7027 .blitter_destroy = vk_blitter_destroy,
7028 .blitter_clear = vk_blitter_clear,
7029 .blitter_blit = vk_blitter_blit,
7032 void wined3d_vk_blitter_create(struct wined3d_blitter **next)
7034 struct wined3d_blitter *blitter;
7036 if (!(blitter = heap_alloc(sizeof(*blitter))))
7037 return;
7039 TRACE("Created blitter %p.\n", blitter);
7041 blitter->ops = &vk_blitter_ops;
7042 blitter->next = *next;
7043 *next = blitter;